blob: ba26c66cc11bf5a73b2a5caf0e7ddfb1060840ef [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
1056/**
Tejun Heoffeae412006-03-01 16:09:35 +09001057 * ata_dev_configure - Configure the specified ATA/ATAPI device
1058 * @ap: Port on which target device resides
1059 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 *
Tejun Heoffeae412006-03-01 16:09:35 +09001061 * Configure @dev according to @dev->id. Generic and low-level
1062 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 *
1064 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001065 * Kernel thread context (may sleep)
1066 *
1067 * RETURNS:
1068 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
Tejun Heoffeae412006-03-01 16:09:35 +09001070static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 unsigned long xfer_modes;
Tejun Heo6e7846e2006-02-12 23:32:58 +09001073 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 if (!ata_dev_present(dev)) {
1076 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001077 ap->id, dev->devno);
1078 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080
Tejun Heoffeae412006-03-01 16:09:35 +09001081 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 /*
1084 * common ATA, ATAPI feature tests
1085 */
1086
Albert Lee8bf62ece2005-05-12 15:29:42 -04001087 /* we require DMA support (bits 8 of word 49) */
1088 if (!ata_id_has_dma(dev->id)) {
1089 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Tejun Heoffeae412006-03-01 16:09:35 +09001090 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 goto err_out_nosup;
1092 }
1093
1094 /* quick-n-dirty find max transfer mode; for printk only */
1095 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1096 if (!xfer_modes)
1097 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001098 if (!xfer_modes)
1099 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Tejun Heo0bd33002006-02-12 22:47:05 +09001101 ata_dump_id(dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 /* ATA-specific feature tests */
1104 if (dev->class == ATA_DEV_ATA) {
Tejun Heo29407402006-02-12 22:47:04 +09001105 dev->n_sectors = ata_id_n_sectors(dev->id);
1106
Albert Lee8bf62ece2005-05-12 15:29:42 -04001107 if (ata_id_has_lba(dev->id)) {
1108 dev->flags |= ATA_DFLAG_LBA;
1109
Tejun Heo29407402006-02-12 22:47:04 +09001110 if (ata_id_has_lba48(dev->id))
Albert Lee8bf62ece2005-05-12 15:29:42 -04001111 dev->flags |= ATA_DFLAG_LBA48;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001112
1113 /* print device info to dmesg */
1114 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001115 ap->id, dev->devno,
Tejun Heo49016ac2006-02-21 02:12:11 +09001116 ata_id_major_version(dev->id),
Albert Lee8bf62ece2005-05-12 15:29:42 -04001117 ata_mode_string(xfer_modes),
1118 (unsigned long long)dev->n_sectors,
1119 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
Tejun Heoffeae412006-03-01 16:09:35 +09001120 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001121 /* CHS */
1122
1123 /* Default translation */
1124 dev->cylinders = dev->id[1];
1125 dev->heads = dev->id[3];
1126 dev->sectors = dev->id[6];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001127
1128 if (ata_id_current_chs_valid(dev->id)) {
1129 /* Current CHS translation is valid. */
1130 dev->cylinders = dev->id[54];
1131 dev->heads = dev->id[55];
1132 dev->sectors = dev->id[56];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001133 }
1134
1135 /* print device info to dmesg */
1136 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 +09001137 ap->id, dev->devno,
Tejun Heo49016ac2006-02-21 02:12:11 +09001138 ata_id_major_version(dev->id),
Albert Lee8bf62ece2005-05-12 15:29:42 -04001139 ata_mode_string(xfer_modes),
1140 (unsigned long long)dev->n_sectors,
1141 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144
Tejun Heo6e7846e2006-02-12 23:32:58 +09001145 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147
1148 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001149 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 rc = atapi_cdb_len(dev->id);
1151 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1152 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
Tejun Heoffeae412006-03-01 16:09:35 +09001153 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 goto err_out_nosup;
1155 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001156 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 /* print device info to dmesg */
1159 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001160 ap->id, dev->devno,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 ata_mode_string(xfer_modes));
1162 }
1163
Tejun Heo6e7846e2006-02-12 23:32:58 +09001164 ap->host->max_cmd_len = 0;
1165 for (i = 0; i < ATA_MAX_DEVICES; i++)
1166 ap->host->max_cmd_len = max_t(unsigned int,
1167 ap->host->max_cmd_len,
1168 ap->device[i].cdb_len);
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09001171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173err_out_nosup:
1174 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001175 ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 DPRINTK("EXIT, err\n");
Tejun Heoffeae412006-03-01 16:09:35 +09001177 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
Brad Campbell6f2f3812005-05-12 15:07:47 -04001180
Tejun Heo8eabd022006-02-12 23:32:58 +09001181static inline u8 ata_dev_knobble(const struct ata_port *ap,
1182 struct ata_device *dev)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001183{
Tejun Heo8eabd022006-02-12 23:32:58 +09001184 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Brad Campbell6f2f3812005-05-12 15:07:47 -04001185}
1186
1187/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001188 * ata_dev_config - Run device specific handlers & check for SATA->PATA bridges
1189 * @ap: Bus
1190 * @i: Device
Brad Campbell6f2f3812005-05-12 15:07:47 -04001191 *
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001192 * LOCKING:
Brad Campbell6f2f3812005-05-12 15:07:47 -04001193 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001194
Brad Campbell6f2f3812005-05-12 15:07:47 -04001195void ata_dev_config(struct ata_port *ap, unsigned int i)
1196{
1197 /* limit bridge transfers to udma5, 200 sectors */
Tejun Heo8eabd022006-02-12 23:32:58 +09001198 if (ata_dev_knobble(ap, &ap->device[i])) {
Brad Campbell6f2f3812005-05-12 15:07:47 -04001199 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
Tejun Heo8eabd022006-02-12 23:32:58 +09001200 ap->id, i);
Brad Campbell6f2f3812005-05-12 15:07:47 -04001201 ap->udma_mask &= ATA_UDMA5;
Tejun Heob00eec12006-02-12 23:32:59 +09001202 ap->device[i].max_sectors = ATA_MAX_SECTORS;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001203 }
1204
1205 if (ap->ops->dev_config)
1206 ap->ops->dev_config(ap, &ap->device[i]);
1207}
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209/**
1210 * ata_bus_probe - Reset and probe ATA bus
1211 * @ap: Bus to probe
1212 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001213 * Master ATA bus probing function. Initiates a hardware-dependent
1214 * bus reset, then attempts to identify any devices found on
1215 * the bus.
1216 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001218 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 *
1220 * RETURNS:
1221 * Zero on success, non-zero on error.
1222 */
1223
1224static int ata_bus_probe(struct ata_port *ap)
1225{
1226 unsigned int i, found = 0;
1227
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001228 if (ap->ops->probe_reset) {
1229 unsigned int classes[ATA_MAX_DEVICES];
1230 int rc;
1231
1232 ata_port_probe(ap);
1233
1234 rc = ap->ops->probe_reset(ap, classes);
1235 if (rc == 0) {
Tejun Heo06ab7822006-02-12 15:01:49 +09001236 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1237 if (classes[i] == ATA_DEV_UNKNOWN)
1238 classes[i] = ATA_DEV_NONE;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001239 ap->device[i].class = classes[i];
Tejun Heo06ab7822006-02-12 15:01:49 +09001240 }
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001241 } else {
1242 printk(KERN_ERR "ata%u: probe reset failed, "
1243 "disabling port\n", ap->id);
1244 ata_port_disable(ap);
1245 }
1246 } else
1247 ap->ops->phy_reset(ap);
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1250 goto err_out;
1251
1252 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoffeae412006-03-01 16:09:35 +09001253 struct ata_device *dev = &ap->device[i];
1254
1255 if (!ata_dev_present(dev))
1256 continue;
1257
1258 WARN_ON(dev->id != NULL);
1259 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1260 dev->class = ATA_DEV_NONE;
1261 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
Tejun Heoffeae412006-03-01 16:09:35 +09001263
1264 if (ata_dev_configure(ap, dev)) {
1265 dev->class++; /* disable device */
1266 continue;
1267 }
1268
1269 ata_dev_config(ap, i);
1270 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
1272
1273 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1274 goto err_out_disable;
1275
1276 ata_set_mode(ap);
1277 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1278 goto err_out_disable;
1279
1280 return 0;
1281
1282err_out_disable:
1283 ap->ops->port_disable(ap);
1284err_out:
1285 return -1;
1286}
1287
1288/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001289 * ata_port_probe - Mark port as enabled
1290 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001292 * Modify @ap data structure such that the system
1293 * thinks that the entire port is enabled.
1294 *
1295 * LOCKING: host_set lock, or some other form of
1296 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 */
1298
1299void ata_port_probe(struct ata_port *ap)
1300{
1301 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1302}
1303
1304/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001305 * sata_print_link_status - Print SATA link status
1306 * @ap: SATA port to printk link status about
1307 *
1308 * This function prints link speed and status of a SATA link.
1309 *
1310 * LOCKING:
1311 * None.
1312 */
1313static void sata_print_link_status(struct ata_port *ap)
1314{
1315 u32 sstatus, tmp;
1316 const char *speed;
1317
1318 if (!ap->ops->scr_read)
1319 return;
1320
1321 sstatus = scr_read(ap, SCR_STATUS);
1322
1323 if (sata_dev_present(ap)) {
1324 tmp = (sstatus >> 4) & 0xf;
1325 if (tmp & (1 << 0))
1326 speed = "1.5";
1327 else if (tmp & (1 << 1))
1328 speed = "3.0";
1329 else
1330 speed = "<unknown>";
1331 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1332 ap->id, speed, sstatus);
1333 } else {
1334 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1335 ap->id, sstatus);
1336 }
1337}
1338
1339/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001340 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1341 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001343 * This function issues commands to standard SATA Sxxx
1344 * PHY registers, to wake up the phy (and device), and
1345 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 *
1347 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001348 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 *
1350 */
1351void __sata_phy_reset(struct ata_port *ap)
1352{
1353 u32 sstatus;
1354 unsigned long timeout = jiffies + (HZ * 5);
1355
1356 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca89e2005-03-28 15:10:27 -05001357 /* issue phy wake/reset */
1358 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001359 /* Couldn't find anything in SATA I/II specs, but
1360 * AHCI-1.1 10.4.2 says at least 1 ms. */
1361 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
Brett Russcdcca89e2005-03-28 15:10:27 -05001363 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 /* wait for phy to become ready, if necessary */
1366 do {
1367 msleep(200);
1368 sstatus = scr_read(ap, SCR_STATUS);
1369 if ((sstatus & 0xf) != 1)
1370 break;
1371 } while (time_before(jiffies, timeout));
1372
Tejun Heo3be680b2005-12-19 22:35:02 +09001373 /* print link status */
1374 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001375
Tejun Heo3be680b2005-12-19 22:35:02 +09001376 /* TODO: phy layer with polling, timeouts, etc. */
1377 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001379 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1383 return;
1384
1385 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1386 ata_port_disable(ap);
1387 return;
1388 }
1389
1390 ap->cbl = ATA_CBL_SATA;
1391}
1392
1393/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001394 * sata_phy_reset - Reset SATA bus.
1395 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001397 * This function resets the SATA bus, and then probes
1398 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 *
1400 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001401 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 *
1403 */
1404void sata_phy_reset(struct ata_port *ap)
1405{
1406 __sata_phy_reset(ap);
1407 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1408 return;
1409 ata_bus_reset(ap);
1410}
1411
1412/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001413 * ata_port_disable - Disable port.
1414 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001416 * Modify @ap data structure such that the system
1417 * thinks that the entire port is disabled, and should
1418 * never attempt to probe or communicate with devices
1419 * on this port.
1420 *
1421 * LOCKING: host_set lock, or some other form of
1422 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 */
1424
1425void ata_port_disable(struct ata_port *ap)
1426{
1427 ap->device[0].class = ATA_DEV_NONE;
1428 ap->device[1].class = ATA_DEV_NONE;
1429 ap->flags |= ATA_FLAG_PORT_DISABLED;
1430}
1431
Alan Cox452503f2005-10-21 19:01:32 -04001432/*
1433 * This mode timing computation functionality is ported over from
1434 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1435 */
1436/*
1437 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1438 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1439 * for PIO 5, which is a nonstandard extension and UDMA6, which
1440 * is currently supported only by Maxtor drives.
1441 */
1442
1443static const struct ata_timing ata_timing[] = {
1444
1445 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1446 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1447 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1448 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1449
1450 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1451 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1452 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1453
1454/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1455
1456 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1457 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1458 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1459
1460 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1461 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1462 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1463
1464/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1465 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1466 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1467
1468 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1469 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1470 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1471
1472/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1473
1474 { 0xFF }
1475};
1476
1477#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1478#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1479
1480static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1481{
1482 q->setup = EZ(t->setup * 1000, T);
1483 q->act8b = EZ(t->act8b * 1000, T);
1484 q->rec8b = EZ(t->rec8b * 1000, T);
1485 q->cyc8b = EZ(t->cyc8b * 1000, T);
1486 q->active = EZ(t->active * 1000, T);
1487 q->recover = EZ(t->recover * 1000, T);
1488 q->cycle = EZ(t->cycle * 1000, T);
1489 q->udma = EZ(t->udma * 1000, UT);
1490}
1491
1492void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1493 struct ata_timing *m, unsigned int what)
1494{
1495 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1496 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1497 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1498 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1499 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1500 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1501 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1502 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1503}
1504
1505static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1506{
1507 const struct ata_timing *t;
1508
1509 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001510 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001511 return NULL;
1512 return t;
1513}
1514
1515int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1516 struct ata_timing *t, int T, int UT)
1517{
1518 const struct ata_timing *s;
1519 struct ata_timing p;
1520
1521 /*
1522 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001523 */
Alan Cox452503f2005-10-21 19:01:32 -04001524
1525 if (!(s = ata_timing_find_mode(speed)))
1526 return -EINVAL;
1527
Albert Lee75b1f2f2005-11-16 17:06:18 +08001528 memcpy(t, s, sizeof(*s));
1529
Alan Cox452503f2005-10-21 19:01:32 -04001530 /*
1531 * If the drive is an EIDE drive, it can tell us it needs extended
1532 * PIO/MW_DMA cycle timing.
1533 */
1534
1535 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1536 memset(&p, 0, sizeof(p));
1537 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1538 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1539 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1540 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1541 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1542 }
1543 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1544 }
1545
1546 /*
1547 * Convert the timing to bus clock counts.
1548 */
1549
Albert Lee75b1f2f2005-11-16 17:06:18 +08001550 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001551
1552 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001553 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1554 * S.M.A.R.T * and some other commands. We have to ensure that the
1555 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001556 */
1557
1558 if (speed > XFER_PIO_4) {
1559 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1560 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1561 }
1562
1563 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001564 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001565 */
1566
1567 if (t->act8b + t->rec8b < t->cyc8b) {
1568 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1569 t->rec8b = t->cyc8b - t->act8b;
1570 }
1571
1572 if (t->active + t->recover < t->cycle) {
1573 t->active += (t->cycle - (t->active + t->recover)) / 2;
1574 t->recover = t->cycle - t->active;
1575 }
1576
1577 return 0;
1578}
1579
Jeff Garzik057ace52005-10-22 14:27:05 -04001580static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 unsigned int shift;
1582 u8 base;
1583} xfer_mode_classes[] = {
1584 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1585 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1586 { ATA_SHIFT_PIO, XFER_PIO_0 },
1587};
1588
Arjan van de Ven858119e2006-01-14 13:20:43 -08001589static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
1591 int i;
1592
1593 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1594 if (xfer_mode_classes[i].shift == shift)
1595 return xfer_mode_classes[i].base;
1596
1597 return 0xff;
1598}
1599
1600static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1601{
1602 int ofs, idx;
1603 u8 base;
1604
1605 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1606 return;
1607
1608 if (dev->xfer_shift == ATA_SHIFT_PIO)
1609 dev->flags |= ATA_DFLAG_PIO;
1610
1611 ata_dev_set_xfermode(ap, dev);
1612
1613 base = base_from_shift(dev->xfer_shift);
1614 ofs = dev->xfer_mode - base;
1615 idx = ofs + dev->xfer_shift;
1616 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1617
1618 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1619 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1620
1621 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1622 ap->id, dev->devno, xfer_mode_str[idx]);
1623}
1624
1625static int ata_host_set_pio(struct ata_port *ap)
1626{
1627 unsigned int mask;
1628 int x, i;
1629 u8 base, xfer_mode;
1630
1631 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1632 x = fgb(mask);
1633 if (x < 0) {
1634 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1635 return -1;
1636 }
1637
1638 base = base_from_shift(ATA_SHIFT_PIO);
1639 xfer_mode = base + x;
1640
1641 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1642 (int)base, (int)xfer_mode, mask, x);
1643
1644 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1645 struct ata_device *dev = &ap->device[i];
1646 if (ata_dev_present(dev)) {
1647 dev->pio_mode = xfer_mode;
1648 dev->xfer_mode = xfer_mode;
1649 dev->xfer_shift = ATA_SHIFT_PIO;
1650 if (ap->ops->set_piomode)
1651 ap->ops->set_piomode(ap, dev);
1652 }
1653 }
1654
1655 return 0;
1656}
1657
1658static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1659 unsigned int xfer_shift)
1660{
1661 int i;
1662
1663 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1664 struct ata_device *dev = &ap->device[i];
1665 if (ata_dev_present(dev)) {
1666 dev->dma_mode = xfer_mode;
1667 dev->xfer_mode = xfer_mode;
1668 dev->xfer_shift = xfer_shift;
1669 if (ap->ops->set_dmamode)
1670 ap->ops->set_dmamode(ap, dev);
1671 }
1672 }
1673}
1674
1675/**
1676 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1677 * @ap: port on which timings will be programmed
1678 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001679 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1680 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001682 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 */
1684static void ata_set_mode(struct ata_port *ap)
1685{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001686 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 u8 xfer_mode;
1688 int rc;
1689
1690 /* step 1: always set host PIO timings */
1691 rc = ata_host_set_pio(ap);
1692 if (rc)
1693 goto err_out;
1694
1695 /* step 2: choose the best data xfer mode */
1696 xfer_mode = xfer_shift = 0;
1697 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1698 if (rc)
1699 goto err_out;
1700
1701 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1702 if (xfer_shift != ATA_SHIFT_PIO)
1703 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1704
1705 /* step 4: update devices' xfer mode */
1706 ata_dev_set_mode(ap, &ap->device[0]);
1707 ata_dev_set_mode(ap, &ap->device[1]);
1708
1709 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1710 return;
1711
1712 if (ap->ops->post_set_mode)
1713 ap->ops->post_set_mode(ap);
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 return;
1716
1717err_out:
1718 ata_port_disable(ap);
1719}
1720
1721/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001722 * ata_tf_to_host - issue ATA taskfile to host controller
1723 * @ap: port to which command is being issued
1724 * @tf: ATA taskfile register set
1725 *
1726 * Issues ATA taskfile register set to ATA host controller,
1727 * with proper synchronization with interrupt handler and
1728 * other threads.
1729 *
1730 * LOCKING:
1731 * spin_lock_irqsave(host_set lock)
1732 */
1733
1734static inline void ata_tf_to_host(struct ata_port *ap,
1735 const struct ata_taskfile *tf)
1736{
1737 ap->ops->tf_load(ap, tf);
1738 ap->ops->exec_command(ap, tf);
1739}
1740
1741/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 * ata_busy_sleep - sleep until BSY clears, or timeout
1743 * @ap: port containing status register to be polled
1744 * @tmout_pat: impatience timeout
1745 * @tmout: overall timeout
1746 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001747 * Sleep until ATA Status register bit BSY clears,
1748 * or a timeout occurs.
1749 *
1750 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 */
1752
Tejun Heo6f8b9952006-01-24 17:05:21 +09001753unsigned int ata_busy_sleep (struct ata_port *ap,
1754 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
1756 unsigned long timer_start, timeout;
1757 u8 status;
1758
1759 status = ata_busy_wait(ap, ATA_BUSY, 300);
1760 timer_start = jiffies;
1761 timeout = timer_start + tmout_pat;
1762 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1763 msleep(50);
1764 status = ata_busy_wait(ap, ATA_BUSY, 3);
1765 }
1766
1767 if (status & ATA_BUSY)
1768 printk(KERN_WARNING "ata%u is slow to respond, "
1769 "please be patient\n", ap->id);
1770
1771 timeout = timer_start + tmout;
1772 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1773 msleep(50);
1774 status = ata_chk_status(ap);
1775 }
1776
1777 if (status & ATA_BUSY) {
1778 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1779 ap->id, tmout / HZ);
1780 return 1;
1781 }
1782
1783 return 0;
1784}
1785
1786static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1787{
1788 struct ata_ioports *ioaddr = &ap->ioaddr;
1789 unsigned int dev0 = devmask & (1 << 0);
1790 unsigned int dev1 = devmask & (1 << 1);
1791 unsigned long timeout;
1792
1793 /* if device 0 was found in ata_devchk, wait for its
1794 * BSY bit to clear
1795 */
1796 if (dev0)
1797 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1798
1799 /* if device 1 was found in ata_devchk, wait for
1800 * register access, then wait for BSY to clear
1801 */
1802 timeout = jiffies + ATA_TMOUT_BOOT;
1803 while (dev1) {
1804 u8 nsect, lbal;
1805
1806 ap->ops->dev_select(ap, 1);
1807 if (ap->flags & ATA_FLAG_MMIO) {
1808 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1809 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1810 } else {
1811 nsect = inb(ioaddr->nsect_addr);
1812 lbal = inb(ioaddr->lbal_addr);
1813 }
1814 if ((nsect == 1) && (lbal == 1))
1815 break;
1816 if (time_after(jiffies, timeout)) {
1817 dev1 = 0;
1818 break;
1819 }
1820 msleep(50); /* give drive a breather */
1821 }
1822 if (dev1)
1823 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1824
1825 /* is all this really necessary? */
1826 ap->ops->dev_select(ap, 0);
1827 if (dev1)
1828 ap->ops->dev_select(ap, 1);
1829 if (dev0)
1830 ap->ops->dev_select(ap, 0);
1831}
1832
1833/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001834 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1835 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001837 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1838 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 *
1840 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001841 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001842 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 *
1844 */
1845
1846static unsigned int ata_bus_edd(struct ata_port *ap)
1847{
1848 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001849 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 /* set up execute-device-diag (bus reset) taskfile */
1852 /* also, take interrupts to a known state (disabled) */
1853 DPRINTK("execute-device-diag\n");
1854 ata_tf_init(ap, &tf, 0);
1855 tf.ctl |= ATA_NIEN;
1856 tf.command = ATA_CMD_EDD;
1857 tf.protocol = ATA_PROT_NODATA;
1858
1859 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05001860 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05001862 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864 /* spec says at least 2ms. but who knows with those
1865 * crazy ATAPI devices...
1866 */
1867 msleep(150);
1868
1869 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1870}
1871
1872static unsigned int ata_bus_softreset(struct ata_port *ap,
1873 unsigned int devmask)
1874{
1875 struct ata_ioports *ioaddr = &ap->ioaddr;
1876
1877 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1878
1879 /* software reset. causes dev0 to be selected */
1880 if (ap->flags & ATA_FLAG_MMIO) {
1881 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1882 udelay(20); /* FIXME: flush */
1883 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1884 udelay(20); /* FIXME: flush */
1885 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1886 } else {
1887 outb(ap->ctl, ioaddr->ctl_addr);
1888 udelay(10);
1889 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1890 udelay(10);
1891 outb(ap->ctl, ioaddr->ctl_addr);
1892 }
1893
1894 /* spec mandates ">= 2ms" before checking status.
1895 * We wait 150ms, because that was the magic delay used for
1896 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1897 * between when the ATA command register is written, and then
1898 * status is checked. Because waiting for "a while" before
1899 * checking status is fine, post SRST, we perform this magic
1900 * delay here as well.
1901 */
1902 msleep(150);
1903
1904 ata_bus_post_reset(ap, devmask);
1905
1906 return 0;
1907}
1908
1909/**
1910 * ata_bus_reset - reset host port and associated ATA channel
1911 * @ap: port to reset
1912 *
1913 * This is typically the first time we actually start issuing
1914 * commands to the ATA channel. We wait for BSY to clear, then
1915 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1916 * result. Determine what devices, if any, are on the channel
1917 * by looking at the device 0/1 error register. Look at the signature
1918 * stored in each device's taskfile registers, to determine if
1919 * the device is ATA or ATAPI.
1920 *
1921 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001922 * PCI/etc. bus probe sem.
1923 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 *
1925 * SIDE EFFECTS:
1926 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1927 */
1928
1929void ata_bus_reset(struct ata_port *ap)
1930{
1931 struct ata_ioports *ioaddr = &ap->ioaddr;
1932 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1933 u8 err;
1934 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1935
1936 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1937
1938 /* determine if device 0/1 are present */
1939 if (ap->flags & ATA_FLAG_SATA_RESET)
1940 dev0 = 1;
1941 else {
1942 dev0 = ata_devchk(ap, 0);
1943 if (slave_possible)
1944 dev1 = ata_devchk(ap, 1);
1945 }
1946
1947 if (dev0)
1948 devmask |= (1 << 0);
1949 if (dev1)
1950 devmask |= (1 << 1);
1951
1952 /* select device 0 again */
1953 ap->ops->dev_select(ap, 0);
1954
1955 /* issue bus reset */
1956 if (ap->flags & ATA_FLAG_SRST)
1957 rc = ata_bus_softreset(ap, devmask);
1958 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1959 /* set up device control */
1960 if (ap->flags & ATA_FLAG_MMIO)
1961 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1962 else
1963 outb(ap->ctl, ioaddr->ctl_addr);
1964 rc = ata_bus_edd(ap);
1965 }
1966
1967 if (rc)
1968 goto err_out;
1969
1970 /*
1971 * determine by signature whether we have ATA or ATAPI devices
1972 */
Tejun Heob4dc7622006-01-24 17:05:22 +09001973 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09001975 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 /* re-enable interrupts */
1978 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1979 ata_irq_on(ap);
1980
1981 /* is double-select really necessary? */
1982 if (ap->device[1].class != ATA_DEV_NONE)
1983 ap->ops->dev_select(ap, 1);
1984 if (ap->device[0].class != ATA_DEV_NONE)
1985 ap->ops->dev_select(ap, 0);
1986
1987 /* if no devices were detected, disable this port */
1988 if ((ap->device[0].class == ATA_DEV_NONE) &&
1989 (ap->device[1].class == ATA_DEV_NONE))
1990 goto err_out;
1991
1992 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1993 /* set up device control for ATA_FLAG_SATA_RESET */
1994 if (ap->flags & ATA_FLAG_MMIO)
1995 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1996 else
1997 outb(ap->ctl, ioaddr->ctl_addr);
1998 }
1999
2000 DPRINTK("EXIT\n");
2001 return;
2002
2003err_out:
2004 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2005 ap->ops->port_disable(ap);
2006
2007 DPRINTK("EXIT\n");
2008}
2009
Tejun Heo7a7921e2006-02-02 18:20:00 +09002010static int sata_phy_resume(struct ata_port *ap)
2011{
2012 unsigned long timeout = jiffies + (HZ * 5);
2013 u32 sstatus;
2014
2015 scr_write_flush(ap, SCR_CONTROL, 0x300);
2016
2017 /* Wait for phy to become ready, if necessary. */
2018 do {
2019 msleep(200);
2020 sstatus = scr_read(ap, SCR_STATUS);
2021 if ((sstatus & 0xf) != 1)
2022 return 0;
2023 } while (time_before(jiffies, timeout));
2024
2025 return -1;
2026}
2027
Tejun Heoc2bd5802006-01-24 17:05:22 +09002028/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09002029 * ata_std_probeinit - initialize probing
2030 * @ap: port to be probed
2031 *
2032 * @ap is about to be probed. Initialize it. This function is
2033 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09002034 *
2035 * NOTE!!! Do not use this function as probeinit if a low level
2036 * driver implements only hardreset. Just pass NULL as probeinit
2037 * in that case. Using this function is probably okay but doing
2038 * so makes reset sequence different from the original
2039 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09002040 */
2041extern void ata_std_probeinit(struct ata_port *ap)
2042{
Tejun Heo3a397462006-02-10 23:58:48 +09002043 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
Tejun Heo8a19ac82006-02-02 18:20:00 +09002044 sata_phy_resume(ap);
Tejun Heo3a397462006-02-10 23:58:48 +09002045 if (sata_dev_present(ap))
2046 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2047 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09002048}
2049
2050/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09002051 * ata_std_softreset - reset host port via ATA SRST
2052 * @ap: port to reset
2053 * @verbose: fail verbosely
2054 * @classes: resulting classes of attached devices
2055 *
2056 * Reset host port using ATA SRST. This function is to be used
2057 * as standard callback for ata_drive_*_reset() functions.
2058 *
2059 * LOCKING:
2060 * Kernel thread context (may sleep)
2061 *
2062 * RETURNS:
2063 * 0 on success, -errno otherwise.
2064 */
2065int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2066{
2067 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2068 unsigned int devmask = 0, err_mask;
2069 u8 err;
2070
2071 DPRINTK("ENTER\n");
2072
Tejun Heo3a397462006-02-10 23:58:48 +09002073 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2074 classes[0] = ATA_DEV_NONE;
2075 goto out;
2076 }
2077
Tejun Heoc2bd5802006-01-24 17:05:22 +09002078 /* determine if device 0/1 are present */
2079 if (ata_devchk(ap, 0))
2080 devmask |= (1 << 0);
2081 if (slave_possible && ata_devchk(ap, 1))
2082 devmask |= (1 << 1);
2083
Tejun Heoc2bd5802006-01-24 17:05:22 +09002084 /* select device 0 again */
2085 ap->ops->dev_select(ap, 0);
2086
2087 /* issue bus reset */
2088 DPRINTK("about to softreset, devmask=%x\n", devmask);
2089 err_mask = ata_bus_softreset(ap, devmask);
2090 if (err_mask) {
2091 if (verbose)
2092 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2093 ap->id, err_mask);
2094 else
2095 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2096 err_mask);
2097 return -EIO;
2098 }
2099
2100 /* determine by signature whether we have ATA or ATAPI devices */
2101 classes[0] = ata_dev_try_classify(ap, 0, &err);
2102 if (slave_possible && err != 0x81)
2103 classes[1] = ata_dev_try_classify(ap, 1, &err);
2104
Tejun Heo3a397462006-02-10 23:58:48 +09002105 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002106 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2107 return 0;
2108}
2109
2110/**
2111 * sata_std_hardreset - reset host port via SATA phy reset
2112 * @ap: port to reset
2113 * @verbose: fail verbosely
2114 * @class: resulting class of attached device
2115 *
2116 * SATA phy-reset host port using DET bits of SControl register.
2117 * This function is to be used as standard callback for
2118 * ata_drive_*_reset().
2119 *
2120 * LOCKING:
2121 * Kernel thread context (may sleep)
2122 *
2123 * RETURNS:
2124 * 0 on success, -errno otherwise.
2125 */
2126int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2127{
Tejun Heoc2bd5802006-01-24 17:05:22 +09002128 DPRINTK("ENTER\n");
2129
2130 /* Issue phy wake/reset */
2131 scr_write_flush(ap, SCR_CONTROL, 0x301);
2132
2133 /*
2134 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2135 * 10.4.2 says at least 1 ms.
2136 */
2137 msleep(1);
2138
Tejun Heo7a7921e2006-02-02 18:20:00 +09002139 /* Bring phy back */
2140 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002141
Tejun Heoc2bd5802006-01-24 17:05:22 +09002142 /* TODO: phy layer with polling, timeouts, etc. */
2143 if (!sata_dev_present(ap)) {
2144 *class = ATA_DEV_NONE;
2145 DPRINTK("EXIT, link offline\n");
2146 return 0;
2147 }
2148
2149 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2150 if (verbose)
2151 printk(KERN_ERR "ata%u: COMRESET failed "
2152 "(device not ready)\n", ap->id);
2153 else
2154 DPRINTK("EXIT, device not ready\n");
2155 return -EIO;
2156 }
2157
Tejun Heo3a397462006-02-10 23:58:48 +09002158 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2159
Tejun Heoc2bd5802006-01-24 17:05:22 +09002160 *class = ata_dev_try_classify(ap, 0, NULL);
2161
2162 DPRINTK("EXIT, class=%u\n", *class);
2163 return 0;
2164}
2165
2166/**
2167 * ata_std_postreset - standard postreset callback
2168 * @ap: the target ata_port
2169 * @classes: classes of attached devices
2170 *
2171 * This function is invoked after a successful reset. Note that
2172 * the device might have been reset more than once using
2173 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002174 *
2175 * This function is to be used as standard callback for
2176 * ata_drive_*_reset().
2177 *
2178 * LOCKING:
2179 * Kernel thread context (may sleep)
2180 */
2181void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2182{
2183 DPRINTK("ENTER\n");
2184
Tejun Heo56497bd2006-02-15 15:01:42 +09002185 /* set cable type if it isn't already set */
Tejun Heoc2bd5802006-01-24 17:05:22 +09002186 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2187 ap->cbl = ATA_CBL_SATA;
2188
2189 /* print link status */
2190 if (ap->cbl == ATA_CBL_SATA)
2191 sata_print_link_status(ap);
2192
Tejun Heo3a397462006-02-10 23:58:48 +09002193 /* re-enable interrupts */
2194 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2195 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002196
2197 /* is double-select really necessary? */
2198 if (classes[0] != ATA_DEV_NONE)
2199 ap->ops->dev_select(ap, 1);
2200 if (classes[1] != ATA_DEV_NONE)
2201 ap->ops->dev_select(ap, 0);
2202
Tejun Heo3a397462006-02-10 23:58:48 +09002203 /* bail out if no device is present */
2204 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2205 DPRINTK("EXIT, no device\n");
2206 return;
2207 }
2208
2209 /* set up device control */
2210 if (ap->ioaddr.ctl_addr) {
2211 if (ap->flags & ATA_FLAG_MMIO)
2212 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2213 else
2214 outb(ap->ctl, ap->ioaddr.ctl_addr);
2215 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002216
2217 DPRINTK("EXIT\n");
2218}
2219
2220/**
2221 * ata_std_probe_reset - standard probe reset method
2222 * @ap: prot to perform probe-reset
2223 * @classes: resulting classes of attached devices
2224 *
2225 * The stock off-the-shelf ->probe_reset method.
2226 *
2227 * LOCKING:
2228 * Kernel thread context (may sleep)
2229 *
2230 * RETURNS:
2231 * 0 on success, -errno otherwise.
2232 */
2233int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2234{
2235 ata_reset_fn_t hardreset;
2236
2237 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002238 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002239 hardreset = sata_std_hardreset;
2240
Tejun Heo8a19ac82006-02-02 18:20:00 +09002241 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002242 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002243 ata_std_postreset, classes);
2244}
2245
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002246static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2247 ata_postreset_fn_t postreset,
2248 unsigned int *classes)
2249{
2250 int i, rc;
2251
2252 for (i = 0; i < ATA_MAX_DEVICES; i++)
2253 classes[i] = ATA_DEV_UNKNOWN;
2254
2255 rc = reset(ap, 0, classes);
2256 if (rc)
2257 return rc;
2258
2259 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2260 * is complete and convert all ATA_DEV_UNKNOWN to
2261 * ATA_DEV_NONE.
2262 */
2263 for (i = 0; i < ATA_MAX_DEVICES; i++)
2264 if (classes[i] != ATA_DEV_UNKNOWN)
2265 break;
2266
2267 if (i < ATA_MAX_DEVICES)
2268 for (i = 0; i < ATA_MAX_DEVICES; i++)
2269 if (classes[i] == ATA_DEV_UNKNOWN)
2270 classes[i] = ATA_DEV_NONE;
2271
2272 if (postreset)
2273 postreset(ap, classes);
2274
2275 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2276}
2277
2278/**
2279 * ata_drive_probe_reset - Perform probe reset with given methods
2280 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002281 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002282 * @softreset: softreset method (can be NULL)
2283 * @hardreset: hardreset method (can be NULL)
2284 * @postreset: postreset method (can be NULL)
2285 * @classes: resulting classes of attached devices
2286 *
2287 * Reset the specified port and classify attached devices using
2288 * given methods. This function prefers softreset but tries all
2289 * possible reset sequences to reset and classify devices. This
2290 * function is intended to be used for constructing ->probe_reset
2291 * callback by low level drivers.
2292 *
2293 * Reset methods should follow the following rules.
2294 *
2295 * - Return 0 on sucess, -errno on failure.
2296 * - If classification is supported, fill classes[] with
2297 * recognized class codes.
2298 * - If classification is not supported, leave classes[] alone.
2299 * - If verbose is non-zero, print error message on failure;
2300 * otherwise, shut up.
2301 *
2302 * LOCKING:
2303 * Kernel thread context (may sleep)
2304 *
2305 * RETURNS:
2306 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2307 * if classification fails, and any error code from reset
2308 * methods.
2309 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002310int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002311 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2312 ata_postreset_fn_t postreset, unsigned int *classes)
2313{
2314 int rc = -EINVAL;
2315
Tejun Heo7944ea92006-02-02 18:20:00 +09002316 if (probeinit)
2317 probeinit(ap);
2318
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002319 if (softreset) {
2320 rc = do_probe_reset(ap, softreset, postreset, classes);
2321 if (rc == 0)
2322 return 0;
2323 }
2324
2325 if (!hardreset)
2326 return rc;
2327
2328 rc = do_probe_reset(ap, hardreset, postreset, classes);
2329 if (rc == 0 || rc != -ENODEV)
2330 return rc;
2331
2332 if (softreset)
2333 rc = do_probe_reset(ap, softreset, postreset, classes);
2334
2335 return rc;
2336}
2337
Jeff Garzik057ace52005-10-22 14:27:05 -04002338static void ata_pr_blacklisted(const struct ata_port *ap,
2339 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
2341 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2342 ap->id, dev->devno);
2343}
2344
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002345static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 "WDC AC11000H",
2347 "WDC AC22100H",
2348 "WDC AC32500H",
2349 "WDC AC33100H",
2350 "WDC AC31600H",
2351 "WDC AC32100H",
2352 "WDC AC23200L",
2353 "Compaq CRD-8241B",
2354 "CRD-8400B",
2355 "CRD-8480B",
2356 "CRD-8482B",
2357 "CRD-84",
2358 "SanDisk SDP3B",
2359 "SanDisk SDP3B-64",
2360 "SANYO CD-ROM CRD",
2361 "HITACHI CDR-8",
2362 "HITACHI CDR-8335",
2363 "HITACHI CDR-8435",
2364 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002365 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 "CD-532E-A",
2367 "E-IDE CD-ROM CR-840",
2368 "CD-ROM Drive/F5A",
2369 "WPI CDD-820",
2370 "SAMSUNG CD-ROM SC-148C",
2371 "SAMSUNG CD-ROM SC",
2372 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2374 "_NEC DV5800A",
2375};
2376
Jeff Garzik057ace52005-10-22 14:27:05 -04002377static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378{
Tejun Heo2e026712006-02-12 22:47:04 +09002379 unsigned char model_num[41];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 int i;
2381
Tejun Heo6a62a042006-02-13 10:02:46 +09002382 ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
Tejun Heo2e026712006-02-12 22:47:04 +09002385 if (!strcmp(ata_dma_blacklist[i], model_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 return 1;
2387
2388 return 0;
2389}
2390
Jeff Garzik057ace52005-10-22 14:27:05 -04002391static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
Jeff Garzik057ace52005-10-22 14:27:05 -04002393 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 unsigned int mask;
2395
2396 master = &ap->device[0];
2397 slave = &ap->device[1];
2398
Tejun Heoa46314742006-02-11 19:11:13 +09002399 WARN_ON(!ata_dev_present(master) && !ata_dev_present(slave));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
2401 if (shift == ATA_SHIFT_UDMA) {
2402 mask = ap->udma_mask;
2403 if (ata_dev_present(master)) {
2404 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002405 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 mask = 0;
2407 ata_pr_blacklisted(ap, master);
2408 }
2409 }
2410 if (ata_dev_present(slave)) {
2411 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002412 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 mask = 0;
2414 ata_pr_blacklisted(ap, slave);
2415 }
2416 }
2417 }
2418 else if (shift == ATA_SHIFT_MWDMA) {
2419 mask = ap->mwdma_mask;
2420 if (ata_dev_present(master)) {
2421 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002422 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 mask = 0;
2424 ata_pr_blacklisted(ap, master);
2425 }
2426 }
2427 if (ata_dev_present(slave)) {
2428 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002429 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 mask = 0;
2431 ata_pr_blacklisted(ap, slave);
2432 }
2433 }
2434 }
2435 else if (shift == ATA_SHIFT_PIO) {
2436 mask = ap->pio_mask;
2437 if (ata_dev_present(master)) {
2438 /* spec doesn't return explicit support for
2439 * PIO0-2, so we fake it
2440 */
2441 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2442 tmp_mode <<= 3;
2443 tmp_mode |= 0x7;
2444 mask &= tmp_mode;
2445 }
2446 if (ata_dev_present(slave)) {
2447 /* spec doesn't return explicit support for
2448 * PIO0-2, so we fake it
2449 */
2450 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2451 tmp_mode <<= 3;
2452 tmp_mode |= 0x7;
2453 mask &= tmp_mode;
2454 }
2455 }
2456 else {
2457 mask = 0xffffffff; /* shut up compiler warning */
2458 BUG();
2459 }
2460
2461 return mask;
2462}
2463
2464/* find greatest bit */
2465static int fgb(u32 bitmap)
2466{
2467 unsigned int i;
2468 int x = -1;
2469
2470 for (i = 0; i < 32; i++)
2471 if (bitmap & (1 << i))
2472 x = i;
2473
2474 return x;
2475}
2476
2477/**
2478 * ata_choose_xfer_mode - attempt to find best transfer mode
2479 * @ap: Port for which an xfer mode will be selected
2480 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2481 * @xfer_shift_out: (output) bit shift that selects this mode
2482 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002483 * Based on host and device capabilities, determine the
2484 * maximum transfer mode that is amenable to all.
2485 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002487 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 *
2489 * RETURNS:
2490 * Zero on success, negative on error.
2491 */
2492
Jeff Garzik057ace52005-10-22 14:27:05 -04002493static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 u8 *xfer_mode_out,
2495 unsigned int *xfer_shift_out)
2496{
2497 unsigned int mask, shift;
2498 int x, i;
2499
2500 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2501 shift = xfer_mode_classes[i].shift;
2502 mask = ata_get_mode_mask(ap, shift);
2503
2504 x = fgb(mask);
2505 if (x >= 0) {
2506 *xfer_mode_out = xfer_mode_classes[i].base + x;
2507 *xfer_shift_out = shift;
2508 return 0;
2509 }
2510 }
2511
2512 return -1;
2513}
2514
2515/**
2516 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2517 * @ap: Port associated with device @dev
2518 * @dev: Device to which command will be sent
2519 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002520 * Issue SET FEATURES - XFER MODE command to device @dev
2521 * on port @ap.
2522 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002524 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 */
2526
2527static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2528{
Tejun Heoa0123702005-12-13 14:49:31 +09002529 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
2531 /* set up set-features taskfile */
2532 DPRINTK("set features - xfer mode\n");
2533
Tejun Heoa0123702005-12-13 14:49:31 +09002534 ata_tf_init(ap, &tf, dev->devno);
2535 tf.command = ATA_CMD_SET_FEATURES;
2536 tf.feature = SETFEATURES_XFER;
2537 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2538 tf.protocol = ATA_PROT_NODATA;
2539 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Tejun Heoa0123702005-12-13 14:49:31 +09002541 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2542 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2543 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
2547 DPRINTK("EXIT\n");
2548}
2549
2550/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04002551 * ata_dev_init_params - Issue INIT DEV PARAMS command
2552 * @ap: Port associated with device @dev
2553 * @dev: Device to which command will be sent
2554 *
2555 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09002556 * Kernel thread context (may sleep)
2557 *
2558 * RETURNS:
2559 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ece2005-05-12 15:29:42 -04002560 */
2561
Tejun Heo6aff8f12006-02-15 18:24:09 +09002562static unsigned int ata_dev_init_params(struct ata_port *ap,
2563 struct ata_device *dev)
Albert Lee8bf62ece2005-05-12 15:29:42 -04002564{
Tejun Heoa0123702005-12-13 14:49:31 +09002565 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09002566 unsigned int err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04002567 u16 sectors = dev->id[6];
2568 u16 heads = dev->id[3];
2569
2570 /* Number of sectors per track 1-255. Number of heads 1-16 */
2571 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Tejun Heo6aff8f12006-02-15 18:24:09 +09002572 return 0;
Albert Lee8bf62ece2005-05-12 15:29:42 -04002573
2574 /* set up init dev params taskfile */
2575 DPRINTK("init dev params \n");
2576
Tejun Heoa0123702005-12-13 14:49:31 +09002577 ata_tf_init(ap, &tf, dev->devno);
2578 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2579 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2580 tf.protocol = ATA_PROT_NODATA;
2581 tf.nsect = sectors;
2582 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04002583
Tejun Heo6aff8f12006-02-15 18:24:09 +09002584 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
Albert Lee8bf62ece2005-05-12 15:29:42 -04002585
Tejun Heo6aff8f12006-02-15 18:24:09 +09002586 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2587 return err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04002588}
2589
2590/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002591 * ata_sg_clean - Unmap DMA memory associated with command
2592 * @qc: Command containing DMA memory to be released
2593 *
2594 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 *
2596 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002597 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 */
2599
2600static void ata_sg_clean(struct ata_queued_cmd *qc)
2601{
2602 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002603 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002605 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Tejun Heoa46314742006-02-11 19:11:13 +09002607 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2608 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05002611 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002613 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002615 /* if we padded the buffer out to 32-bit bound, and data
2616 * xfer direction is from-device, we must copy from the
2617 * pad buffer back into the supplied buffer
2618 */
2619 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2620 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2621
2622 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002623 if (qc->n_elem)
2624 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002625 /* restore last sg */
2626 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2627 if (pad_buf) {
2628 struct scatterlist *psg = &qc->pad_sgent;
2629 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2630 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002631 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002632 }
2633 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09002634 if (qc->n_elem)
Jeff Garzike1410f22005-11-14 14:06:26 -05002635 dma_unmap_single(ap->host_set->dev,
2636 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2637 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002638 /* restore sg */
2639 sg->length += qc->pad_len;
2640 if (pad_buf)
2641 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2642 pad_buf, qc->pad_len);
2643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
2645 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002646 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647}
2648
2649/**
2650 * ata_fill_sg - Fill PCI IDE PRD table
2651 * @qc: Metadata associated with taskfile to be transferred
2652 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002653 * Fill PCI IDE PRD (scatter-gather) table with segments
2654 * associated with the current disk command.
2655 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002657 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 *
2659 */
2660static void ata_fill_sg(struct ata_queued_cmd *qc)
2661{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002663 struct scatterlist *sg;
2664 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Tejun Heoa46314742006-02-11 19:11:13 +09002666 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05002667 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
2669 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002670 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 u32 addr, offset;
2672 u32 sg_len, len;
2673
2674 /* determine if physical DMA addr spans 64K boundary.
2675 * Note h/w doesn't support 64-bit, so we unconditionally
2676 * truncate dma_addr_t to u32.
2677 */
2678 addr = (u32) sg_dma_address(sg);
2679 sg_len = sg_dma_len(sg);
2680
2681 while (sg_len) {
2682 offset = addr & 0xffff;
2683 len = sg_len;
2684 if ((offset + sg_len) > 0x10000)
2685 len = 0x10000 - offset;
2686
2687 ap->prd[idx].addr = cpu_to_le32(addr);
2688 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2689 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2690
2691 idx++;
2692 sg_len -= len;
2693 addr += len;
2694 }
2695 }
2696
2697 if (idx)
2698 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2699}
2700/**
2701 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2702 * @qc: Metadata associated with taskfile to check
2703 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002704 * Allow low-level driver to filter ATA PACKET commands, returning
2705 * a status indicating whether or not it is OK to use DMA for the
2706 * supplied PACKET command.
2707 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002709 * spin_lock_irqsave(host_set lock)
2710 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 * RETURNS: 0 when ATAPI DMA can be used
2712 * nonzero otherwise
2713 */
2714int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2715{
2716 struct ata_port *ap = qc->ap;
2717 int rc = 0; /* Assume ATAPI DMA is OK by default */
2718
2719 if (ap->ops->check_atapi_dma)
2720 rc = ap->ops->check_atapi_dma(qc);
2721
2722 return rc;
2723}
2724/**
2725 * ata_qc_prep - Prepare taskfile for submission
2726 * @qc: Metadata associated with taskfile to be prepared
2727 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002728 * Prepare ATA taskfile for submission.
2729 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 * LOCKING:
2731 * spin_lock_irqsave(host_set lock)
2732 */
2733void ata_qc_prep(struct ata_queued_cmd *qc)
2734{
2735 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2736 return;
2737
2738 ata_fill_sg(qc);
2739}
2740
Jeff Garzik0cba6322005-05-30 19:49:12 -04002741/**
2742 * ata_sg_init_one - Associate command with memory buffer
2743 * @qc: Command to be associated
2744 * @buf: Memory buffer
2745 * @buflen: Length of memory buffer, in bytes.
2746 *
2747 * Initialize the data-related elements of queued_cmd @qc
2748 * to point to a single memory buffer, @buf of byte length @buflen.
2749 *
2750 * LOCKING:
2751 * spin_lock_irqsave(host_set lock)
2752 */
2753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2755{
2756 struct scatterlist *sg;
2757
2758 qc->flags |= ATA_QCFLAG_SINGLE;
2759
2760 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002761 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002763 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 qc->buf_virt = buf;
2765
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002766 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002767 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768}
2769
Jeff Garzik0cba6322005-05-30 19:49:12 -04002770/**
2771 * ata_sg_init - Associate command with scatter-gather table.
2772 * @qc: Command to be associated
2773 * @sg: Scatter-gather table.
2774 * @n_elem: Number of elements in s/g table.
2775 *
2776 * Initialize the data-related elements of queued_cmd @qc
2777 * to point to a scatter-gather table @sg, containing @n_elem
2778 * elements.
2779 *
2780 * LOCKING:
2781 * spin_lock_irqsave(host_set lock)
2782 */
2783
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2785 unsigned int n_elem)
2786{
2787 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002788 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002790 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791}
2792
2793/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002794 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2795 * @qc: Command with memory buffer to be mapped.
2796 *
2797 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 *
2799 * LOCKING:
2800 * spin_lock_irqsave(host_set lock)
2801 *
2802 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002803 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 */
2805
2806static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2807{
2808 struct ata_port *ap = qc->ap;
2809 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002810 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09002812 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002814 /* we must lengthen transfers to end on a 32-bit boundary */
2815 qc->pad_len = sg->length & 3;
2816 if (qc->pad_len) {
2817 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2818 struct scatterlist *psg = &qc->pad_sgent;
2819
Tejun Heoa46314742006-02-11 19:11:13 +09002820 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002821
2822 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2823
2824 if (qc->tf.flags & ATA_TFLAG_WRITE)
2825 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2826 qc->pad_len);
2827
2828 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2829 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2830 /* trim sg */
2831 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09002832 if (sg->length == 0)
2833 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002834
2835 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2836 sg->length, qc->pad_len);
2837 }
2838
Tejun Heo2e242fa2006-02-20 23:48:38 +09002839 if (trim_sg) {
2840 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05002841 goto skip_map;
2842 }
2843
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002845 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002846 if (dma_mapping_error(dma_address)) {
2847 /* restore sg */
2848 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
2852 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04002853 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Tejun Heo2e242fa2006-02-20 23:48:38 +09002855skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2857 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2858
2859 return 0;
2860}
2861
2862/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002863 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2864 * @qc: Command with scatter-gather table to be mapped.
2865 *
2866 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 *
2868 * LOCKING:
2869 * spin_lock_irqsave(host_set lock)
2870 *
2871 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002872 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 *
2874 */
2875
2876static int ata_sg_setup(struct ata_queued_cmd *qc)
2877{
2878 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002879 struct scatterlist *sg = qc->__sg;
2880 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002881 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa46314742006-02-11 19:11:13 +09002884 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002886 /* we must lengthen transfers to end on a 32-bit boundary */
2887 qc->pad_len = lsg->length & 3;
2888 if (qc->pad_len) {
2889 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2890 struct scatterlist *psg = &qc->pad_sgent;
2891 unsigned int offset;
2892
Tejun Heoa46314742006-02-11 19:11:13 +09002893 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002894
2895 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2896
2897 /*
2898 * psg->page/offset are used to copy to-be-written
2899 * data in this function or read data in ata_sg_clean.
2900 */
2901 offset = lsg->offset + lsg->length - qc->pad_len;
2902 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2903 psg->offset = offset_in_page(offset);
2904
2905 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2906 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2907 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002908 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002909 }
2910
2911 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2912 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2913 /* trim last sg */
2914 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002915 if (lsg->length == 0)
2916 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002917
2918 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2919 qc->n_elem - 1, lsg->length, qc->pad_len);
2920 }
2921
Jeff Garzike1410f22005-11-14 14:06:26 -05002922 pre_n_elem = qc->n_elem;
2923 if (trim_sg && pre_n_elem)
2924 pre_n_elem--;
2925
2926 if (!pre_n_elem) {
2927 n_elem = 0;
2928 goto skip_map;
2929 }
2930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002932 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002933 if (n_elem < 1) {
2934 /* restore last sg */
2935 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
2939 DPRINTK("%d sg elements mapped\n", n_elem);
2940
Jeff Garzike1410f22005-11-14 14:06:26 -05002941skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 qc->n_elem = n_elem;
2943
2944 return 0;
2945}
2946
2947/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002948 * ata_poll_qc_complete - turn irq back on and finish qc
2949 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002950 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002951 *
2952 * LOCKING:
2953 * None. (grabs host lock)
2954 */
2955
Albert Leea22e2eb2005-12-05 15:38:02 +08002956void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002957{
2958 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002959 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002960
Jeff Garzikb8f61532005-08-25 22:01:20 -04002961 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002962 ap->flags &= ~ATA_FLAG_NOINTR;
2963 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002964 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002965 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002966}
2967
2968/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002969 * ata_pio_poll - poll using PIO, depending on current state
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002970 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 *
2972 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002973 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 *
2975 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002976 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 */
2978
2979static unsigned long ata_pio_poll(struct ata_port *ap)
2980{
Albert Leec14b8332005-12-05 15:36:08 +08002981 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002983 unsigned int poll_state = HSM_ST_UNKNOWN;
2984 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Albert Leec14b8332005-12-05 15:36:08 +08002986 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09002987 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08002988
Albert Lee14be71f2005-09-27 17:36:35 +08002989 switch (ap->hsm_task_state) {
2990 case HSM_ST:
2991 case HSM_ST_POLL:
2992 poll_state = HSM_ST_POLL;
2993 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002995 case HSM_ST_LAST:
2996 case HSM_ST_LAST_POLL:
2997 poll_state = HSM_ST_LAST_POLL;
2998 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 break;
3000 default:
3001 BUG();
3002 break;
3003 }
3004
3005 status = ata_chk_status(ap);
3006 if (status & ATA_BUSY) {
3007 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003008 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08003009 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 return 0;
3011 }
Albert Lee14be71f2005-09-27 17:36:35 +08003012 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 return ATA_SHORT_PAUSE;
3014 }
3015
Albert Lee14be71f2005-09-27 17:36:35 +08003016 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 return 0;
3018}
3019
3020/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003021 * ata_pio_complete - check if drive is busy or idle
3022 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 *
3024 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003025 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003026 *
3027 * RETURNS:
3028 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 */
3030
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003031static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
3033 struct ata_queued_cmd *qc;
3034 u8 drv_stat;
3035
3036 /*
Alan Cox31433ea2005-08-26 15:56:47 +01003037 * This is purely heuristic. This is a fast path. Sometimes when
3038 * we enter, BSY will be cleared in a chk-status or two. If not,
3039 * the drive is probably seeking or something. Snooze for a couple
3040 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003041 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 */
Albert Leefe79e682005-12-06 11:34:59 +08003043 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3044 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08003046 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3047 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003048 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003050 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 }
3052 }
3053
Albert Leec14b8332005-12-05 15:36:08 +08003054 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003055 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003056
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 drv_stat = ata_wait_idle(ap);
3058 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003059 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003060 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003061 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 }
3063
Albert Lee14be71f2005-09-27 17:36:35 +08003064 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Tejun Heoa46314742006-02-11 19:11:13 +09003066 WARN_ON(qc->err_mask);
Albert Leea22e2eb2005-12-05 15:38:02 +08003067 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003068
3069 /* another command may start at this point */
3070
3071 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072}
3073
Edward Falk0baab862005-06-02 18:17:13 -04003074
3075/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003076 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003077 * @buf: Buffer to swap
3078 * @buf_words: Number of 16-bit words in buffer.
3079 *
3080 * Swap halves of 16-bit words if needed to convert from
3081 * little-endian byte order to native cpu byte order, or
3082 * vice-versa.
3083 *
3084 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003085 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003086 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087void swap_buf_le16(u16 *buf, unsigned int buf_words)
3088{
3089#ifdef __BIG_ENDIAN
3090 unsigned int i;
3091
3092 for (i = 0; i < buf_words; i++)
3093 buf[i] = le16_to_cpu(buf[i]);
3094#endif /* __BIG_ENDIAN */
3095}
3096
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003097/**
3098 * ata_mmio_data_xfer - Transfer data by MMIO
3099 * @ap: port to read/write
3100 * @buf: data buffer
3101 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003102 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003103 *
3104 * Transfer data from/to the device data register by MMIO.
3105 *
3106 * LOCKING:
3107 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003108 */
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3111 unsigned int buflen, int write_data)
3112{
3113 unsigned int i;
3114 unsigned int words = buflen >> 1;
3115 u16 *buf16 = (u16 *) buf;
3116 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3117
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003118 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 if (write_data) {
3120 for (i = 0; i < words; i++)
3121 writew(le16_to_cpu(buf16[i]), mmio);
3122 } else {
3123 for (i = 0; i < words; i++)
3124 buf16[i] = cpu_to_le16(readw(mmio));
3125 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003126
3127 /* Transfer trailing 1 byte, if any. */
3128 if (unlikely(buflen & 0x01)) {
3129 u16 align_buf[1] = { 0 };
3130 unsigned char *trailing_buf = buf + buflen - 1;
3131
3132 if (write_data) {
3133 memcpy(align_buf, trailing_buf, 1);
3134 writew(le16_to_cpu(align_buf[0]), mmio);
3135 } else {
3136 align_buf[0] = cpu_to_le16(readw(mmio));
3137 memcpy(trailing_buf, align_buf, 1);
3138 }
3139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140}
3141
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003142/**
3143 * ata_pio_data_xfer - Transfer data by PIO
3144 * @ap: port to read/write
3145 * @buf: data buffer
3146 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003147 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003148 *
3149 * Transfer data from/to the device data register by PIO.
3150 *
3151 * LOCKING:
3152 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003153 */
3154
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3156 unsigned int buflen, int write_data)
3157{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003158 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003160 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003162 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003164 insw(ap->ioaddr.data_addr, buf, words);
3165
3166 /* Transfer trailing 1 byte, if any. */
3167 if (unlikely(buflen & 0x01)) {
3168 u16 align_buf[1] = { 0 };
3169 unsigned char *trailing_buf = buf + buflen - 1;
3170
3171 if (write_data) {
3172 memcpy(align_buf, trailing_buf, 1);
3173 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3174 } else {
3175 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3176 memcpy(trailing_buf, align_buf, 1);
3177 }
3178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179}
3180
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003181/**
3182 * ata_data_xfer - Transfer data from/to the data register.
3183 * @ap: port to read/write
3184 * @buf: data buffer
3185 * @buflen: buffer length
3186 * @do_write: read/write
3187 *
3188 * Transfer data from/to the device data register.
3189 *
3190 * LOCKING:
3191 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003192 */
3193
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3195 unsigned int buflen, int do_write)
3196{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003197 /* Make the crap hardware pay the costs not the good stuff */
3198 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3199 unsigned long flags;
3200 local_irq_save(flags);
3201 if (ap->flags & ATA_FLAG_MMIO)
3202 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3203 else
3204 ata_pio_data_xfer(ap, buf, buflen, do_write);
3205 local_irq_restore(flags);
3206 } else {
3207 if (ap->flags & ATA_FLAG_MMIO)
3208 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3209 else
3210 ata_pio_data_xfer(ap, buf, buflen, do_write);
3211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212}
3213
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003214/**
3215 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3216 * @qc: Command on going
3217 *
3218 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3219 *
3220 * LOCKING:
3221 * Inherited from caller.
3222 */
3223
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224static void ata_pio_sector(struct ata_queued_cmd *qc)
3225{
3226 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003227 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 struct ata_port *ap = qc->ap;
3229 struct page *page;
3230 unsigned int offset;
3231 unsigned char *buf;
3232
3233 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003234 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
3236 page = sg[qc->cursg].page;
3237 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3238
3239 /* get the current page and offset */
3240 page = nth_page(page, (offset >> PAGE_SHIFT));
3241 offset %= PAGE_SIZE;
3242
3243 buf = kmap(page) + offset;
3244
3245 qc->cursect++;
3246 qc->cursg_ofs++;
3247
Albert Lee32529e02005-05-26 03:49:42 -04003248 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 qc->cursg++;
3250 qc->cursg_ofs = 0;
3251 }
3252
3253 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3254
3255 /* do the actual data transfer */
3256 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3257 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3258
3259 kunmap(page);
3260}
3261
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003262/**
3263 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3264 * @qc: Command on going
3265 * @bytes: number of bytes
3266 *
3267 * Transfer Transfer data from/to the ATAPI device.
3268 *
3269 * LOCKING:
3270 * Inherited from caller.
3271 *
3272 */
3273
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3275{
3276 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003277 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 struct ata_port *ap = qc->ap;
3279 struct page *page;
3280 unsigned char *buf;
3281 unsigned int offset, count;
3282
Albert Lee563a6e12005-08-12 14:17:50 +08003283 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003284 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
3286next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003287 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003288 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003289 * The end of qc->sg is reached and the device expects
3290 * more data to transfer. In order not to overrun qc->sg
3291 * and fulfill length specified in the byte count register,
3292 * - for read case, discard trailing data from the device
3293 * - for write case, padding zero data to the device
3294 */
3295 u16 pad_buf[1] = { 0 };
3296 unsigned int words = bytes >> 1;
3297 unsigned int i;
3298
3299 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003300 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003301 ap->id, bytes);
3302
3303 for (i = 0; i < words; i++)
3304 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3305
Albert Lee14be71f2005-09-27 17:36:35 +08003306 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003307 return;
3308 }
3309
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003310 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 page = sg->page;
3313 offset = sg->offset + qc->cursg_ofs;
3314
3315 /* get the current page and offset */
3316 page = nth_page(page, (offset >> PAGE_SHIFT));
3317 offset %= PAGE_SIZE;
3318
Albert Lee6952df02005-06-06 15:56:03 +08003319 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003320 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
3322 /* don't cross page boundaries */
3323 count = min(count, (unsigned int)PAGE_SIZE - offset);
3324
3325 buf = kmap(page) + offset;
3326
3327 bytes -= count;
3328 qc->curbytes += count;
3329 qc->cursg_ofs += count;
3330
Albert Lee32529e02005-05-26 03:49:42 -04003331 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 qc->cursg++;
3333 qc->cursg_ofs = 0;
3334 }
3335
3336 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3337
3338 /* do the actual data transfer */
3339 ata_data_xfer(ap, buf, count, do_write);
3340
3341 kunmap(page);
3342
Albert Lee563a6e12005-08-12 14:17:50 +08003343 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345}
3346
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003347/**
3348 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3349 * @qc: Command on going
3350 *
3351 * Transfer Transfer data from/to the ATAPI device.
3352 *
3353 * LOCKING:
3354 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003355 */
3356
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3358{
3359 struct ata_port *ap = qc->ap;
3360 struct ata_device *dev = qc->dev;
3361 unsigned int ireason, bc_lo, bc_hi, bytes;
3362 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3363
3364 ap->ops->tf_read(ap, &qc->tf);
3365 ireason = qc->tf.nsect;
3366 bc_lo = qc->tf.lbam;
3367 bc_hi = qc->tf.lbah;
3368 bytes = (bc_hi << 8) | bc_lo;
3369
3370 /* shall be cleared to zero, indicating xfer of data */
3371 if (ireason & (1 << 0))
3372 goto err_out;
3373
3374 /* make sure transfer direction matches expected */
3375 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3376 if (do_write != i_write)
3377 goto err_out;
3378
3379 __atapi_pio_bytes(qc, bytes);
3380
3381 return;
3382
3383err_out:
3384 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3385 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003386 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003387 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388}
3389
3390/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003391 * ata_pio_block - start PIO on a block
3392 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 *
3394 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003395 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 */
3397
3398static void ata_pio_block(struct ata_port *ap)
3399{
3400 struct ata_queued_cmd *qc;
3401 u8 status;
3402
3403 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003404 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 * Sometimes when we enter, BSY will be cleared in
3406 * a chk-status or two. If not, the drive is probably seeking
3407 * or something. Snooze for a couple msecs, then
3408 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003409 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 */
3411 status = ata_busy_wait(ap, ATA_BUSY, 5);
3412 if (status & ATA_BUSY) {
3413 msleep(2);
3414 status = ata_busy_wait(ap, ATA_BUSY, 10);
3415 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003416 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3418 return;
3419 }
3420 }
3421
3422 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003423 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
Albert Leefe79e682005-12-06 11:34:59 +08003425 /* check error */
3426 if (status & (ATA_ERR | ATA_DF)) {
3427 qc->err_mask |= AC_ERR_DEV;
3428 ap->hsm_task_state = HSM_ST_ERR;
3429 return;
3430 }
3431
3432 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003434 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003436 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 return;
3438 }
3439
3440 atapi_pio_bytes(qc);
3441 } else {
3442 /* handle BSY=0, DRQ=0 as error */
3443 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003444 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003445 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 return;
3447 }
3448
3449 ata_pio_sector(qc);
3450 }
3451}
3452
3453static void ata_pio_error(struct ata_port *ap)
3454{
3455 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003456
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003458 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Albert Lee0565c262006-02-13 18:55:25 +08003460 if (qc->tf.command != ATA_CMD_PACKET)
3461 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3462
Albert Lee1c848982005-12-05 15:40:15 +08003463 /* make sure qc->err_mask is available to
3464 * know what's wrong and recover
3465 */
Tejun Heoa46314742006-02-11 19:11:13 +09003466 WARN_ON(qc->err_mask == 0);
Albert Lee1c848982005-12-05 15:40:15 +08003467
Albert Lee14be71f2005-09-27 17:36:35 +08003468 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Albert Leea22e2eb2005-12-05 15:38:02 +08003470 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471}
3472
3473static void ata_pio_task(void *_data)
3474{
3475 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003476 unsigned long timeout;
3477 int qc_completed;
3478
3479fsm_start:
3480 timeout = 0;
3481 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
Albert Lee14be71f2005-09-27 17:36:35 +08003483 switch (ap->hsm_task_state) {
3484 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 return;
3486
Albert Lee14be71f2005-09-27 17:36:35 +08003487 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 ata_pio_block(ap);
3489 break;
3490
Albert Lee14be71f2005-09-27 17:36:35 +08003491 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003492 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 break;
3494
Albert Lee14be71f2005-09-27 17:36:35 +08003495 case HSM_ST_POLL:
3496 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 timeout = ata_pio_poll(ap);
3498 break;
3499
Albert Lee14be71f2005-09-27 17:36:35 +08003500 case HSM_ST_TMOUT:
3501 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 ata_pio_error(ap);
3503 return;
3504 }
3505
3506 if (timeout)
Tejun Heo95064372006-01-23 13:09:37 +09003507 ata_queue_delayed_pio_task(ap, timeout);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003508 else if (!qc_completed)
3509 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512/**
3513 * ata_qc_timeout - Handle timeout of queued command
3514 * @qc: Command that timed out
3515 *
3516 * Some part of the kernel (currently, only the SCSI layer)
3517 * has noticed that the active command on port @ap has not
3518 * completed after a specified length of time. Handle this
3519 * condition by disabling DMA (if necessary) and completing
3520 * transactions, with error if necessary.
3521 *
3522 * This also handles the case of the "lost interrupt", where
3523 * for some reason (possibly hardware bug, possibly driver bug)
3524 * an interrupt was not delivered to the driver, even though the
3525 * transaction completed successfully.
3526 *
3527 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003528 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 */
3530
3531static void ata_qc_timeout(struct ata_queued_cmd *qc)
3532{
3533 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003534 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003536 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
3538 DPRINTK("ENTER\n");
3539
Tejun Heoc18d06f2006-02-02 00:56:10 +09003540 ata_flush_pio_tasks(ap);
3541 ap->hsm_task_state = HSM_ST_IDLE;
3542
Jeff Garzikb8f61532005-08-25 22:01:20 -04003543 spin_lock_irqsave(&host_set->lock, flags);
3544
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 switch (qc->tf.protocol) {
3546
3547 case ATA_PROT_DMA:
3548 case ATA_PROT_ATAPI_DMA:
3549 host_stat = ap->ops->bmdma_status(ap);
3550
3551 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003552 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
3554 /* fall through */
3555
3556 default:
3557 ata_altstatus(ap);
3558 drv_stat = ata_chk_status(ap);
3559
3560 /* ack bmdma irq events */
3561 ap->ops->irq_clear(ap);
3562
3563 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3564 ap->id, qc->tf.command, drv_stat, host_stat);
3565
3566 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003567 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 break;
3569 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003570
3571 spin_unlock_irqrestore(&host_set->lock, flags);
3572
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003573 ata_eh_qc_complete(qc);
3574
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 DPRINTK("EXIT\n");
3576}
3577
3578/**
3579 * ata_eng_timeout - Handle timeout of queued command
3580 * @ap: Port on which timed-out command is active
3581 *
3582 * Some part of the kernel (currently, only the SCSI layer)
3583 * has noticed that the active command on port @ap has not
3584 * completed after a specified length of time. Handle this
3585 * condition by disabling DMA (if necessary) and completing
3586 * transactions, with error if necessary.
3587 *
3588 * This also handles the case of the "lost interrupt", where
3589 * for some reason (possibly hardware bug, possibly driver bug)
3590 * an interrupt was not delivered to the driver, even though the
3591 * transaction completed successfully.
3592 *
3593 * LOCKING:
3594 * Inherited from SCSI layer (none, can sleep)
3595 */
3596
3597void ata_eng_timeout(struct ata_port *ap)
3598{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 DPRINTK("ENTER\n");
3600
Tejun Heof6379022006-02-10 15:10:48 +09003601 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 DPRINTK("EXIT\n");
3604}
3605
3606/**
3607 * ata_qc_new - Request an available ATA command, for queueing
3608 * @ap: Port associated with device @dev
3609 * @dev: Device from whom we request an available command structure
3610 *
3611 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003612 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 */
3614
3615static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3616{
3617 struct ata_queued_cmd *qc = NULL;
3618 unsigned int i;
3619
3620 for (i = 0; i < ATA_MAX_QUEUE; i++)
3621 if (!test_and_set_bit(i, &ap->qactive)) {
3622 qc = ata_qc_from_tag(ap, i);
3623 break;
3624 }
3625
3626 if (qc)
3627 qc->tag = i;
3628
3629 return qc;
3630}
3631
3632/**
3633 * ata_qc_new_init - Request an available ATA command, and initialize it
3634 * @ap: Port associated with device @dev
3635 * @dev: Device from whom we request an available command structure
3636 *
3637 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003638 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 */
3640
3641struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3642 struct ata_device *dev)
3643{
3644 struct ata_queued_cmd *qc;
3645
3646 qc = ata_qc_new(ap);
3647 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 qc->scsicmd = NULL;
3649 qc->ap = ap;
3650 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003652 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 }
3654
3655 return qc;
3656}
3657
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658/**
3659 * ata_qc_free - free unused ata_queued_cmd
3660 * @qc: Command to complete
3661 *
3662 * Designed to free unused ata_queued_cmd object
3663 * in case something prevents using it.
3664 *
3665 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003666 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 */
3668void ata_qc_free(struct ata_queued_cmd *qc)
3669{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003670 struct ata_port *ap = qc->ap;
3671 unsigned int tag;
3672
Tejun Heoa46314742006-02-11 19:11:13 +09003673 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
Tejun Heo4ba946e2006-01-23 13:09:36 +09003675 qc->flags = 0;
3676 tag = qc->tag;
3677 if (likely(ata_tag_valid(tag))) {
3678 if (tag == ap->active_tag)
3679 ap->active_tag = ATA_TAG_POISON;
3680 qc->tag = ATA_TAG_POISON;
3681 clear_bit(tag, &ap->qactive);
3682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683}
3684
Tejun Heo76014422006-02-11 15:13:49 +09003685void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686{
Tejun Heoa46314742006-02-11 19:11:13 +09003687 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3688 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689
3690 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3691 ata_sg_clean(qc);
3692
Albert Lee3f3791d2005-08-16 14:25:38 +08003693 /* atapi: mark qc as inactive to prevent the interrupt handler
3694 * from completing the command twice later, before the error handler
3695 * is called. (when rc != 0 and atapi request sense is needed)
3696 */
3697 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3698
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003700 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701}
3702
3703static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3704{
3705 struct ata_port *ap = qc->ap;
3706
3707 switch (qc->tf.protocol) {
3708 case ATA_PROT_DMA:
3709 case ATA_PROT_ATAPI_DMA:
3710 return 1;
3711
3712 case ATA_PROT_ATAPI:
3713 case ATA_PROT_PIO:
3714 case ATA_PROT_PIO_MULT:
3715 if (ap->flags & ATA_FLAG_PIO_DMA)
3716 return 1;
3717
3718 /* fall through */
3719
3720 default:
3721 return 0;
3722 }
3723
3724 /* never reached */
3725}
3726
3727/**
3728 * ata_qc_issue - issue taskfile to device
3729 * @qc: command to issue to device
3730 *
3731 * Prepare an ATA command to submission to device.
3732 * This includes mapping the data into a DMA-able
3733 * area, filling in the S/G table, and finally
3734 * writing the taskfile to hardware, starting the command.
3735 *
3736 * LOCKING:
3737 * spin_lock_irqsave(host_set lock)
3738 *
3739 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003740 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 */
3742
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003743unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744{
3745 struct ata_port *ap = qc->ap;
3746
3747 if (ata_should_dma_map(qc)) {
3748 if (qc->flags & ATA_QCFLAG_SG) {
3749 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003750 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3752 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003753 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 }
3755 } else {
3756 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3757 }
3758
3759 ap->ops->qc_prep(qc);
3760
3761 qc->ap->active_tag = qc->tag;
3762 qc->flags |= ATA_QCFLAG_ACTIVE;
3763
3764 return ap->ops->qc_issue(qc);
3765
Tejun Heo8e436af2006-01-23 13:09:36 +09003766sg_err:
3767 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003768 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769}
3770
Edward Falk0baab862005-06-02 18:17:13 -04003771
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772/**
3773 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3774 * @qc: command to issue to device
3775 *
3776 * Using various libata functions and hooks, this function
3777 * starts an ATA command. ATA commands are grouped into
3778 * classes called "protocols", and issuing each type of protocol
3779 * is slightly different.
3780 *
Edward Falk0baab862005-06-02 18:17:13 -04003781 * May be used as the qc_issue() entry in ata_port_operations.
3782 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 * LOCKING:
3784 * spin_lock_irqsave(host_set lock)
3785 *
3786 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003787 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 */
3789
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003790unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791{
3792 struct ata_port *ap = qc->ap;
3793
3794 ata_dev_select(ap, qc->dev->devno, 1, 0);
3795
3796 switch (qc->tf.protocol) {
3797 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05003798 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 break;
3800
3801 case ATA_PROT_DMA:
3802 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3803 ap->ops->bmdma_setup(qc); /* set up bmdma */
3804 ap->ops->bmdma_start(qc); /* initiate bmdma */
3805 break;
3806
3807 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3808 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003809 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003810 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09003811 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 break;
3813
3814 case ATA_PROT_ATAPI:
3815 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003816 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003817 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 break;
3819
3820 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003821 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05003822 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003823 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 break;
3825
3826 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003827 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3829 ap->ops->bmdma_setup(qc); /* set up bmdma */
Tejun Heo95064372006-01-23 13:09:37 +09003830 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 break;
3832
3833 default:
3834 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003835 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 }
3837
3838 return 0;
3839}
3840
3841/**
Edward Falk0baab862005-06-02 18:17:13 -04003842 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 * @qc: Info associated with this ATA transaction.
3844 *
3845 * LOCKING:
3846 * spin_lock_irqsave(host_set lock)
3847 */
3848
3849static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3850{
3851 struct ata_port *ap = qc->ap;
3852 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3853 u8 dmactl;
3854 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3855
3856 /* load PRD table addr. */
3857 mb(); /* make sure PRD table writes are visible to controller */
3858 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3859
3860 /* specify data direction, triple-check start bit is clear */
3861 dmactl = readb(mmio + ATA_DMA_CMD);
3862 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3863 if (!rw)
3864 dmactl |= ATA_DMA_WR;
3865 writeb(dmactl, mmio + ATA_DMA_CMD);
3866
3867 /* issue r/w command */
3868 ap->ops->exec_command(ap, &qc->tf);
3869}
3870
3871/**
Alan Coxb73fc892005-08-26 16:03:19 +01003872 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 * @qc: Info associated with this ATA transaction.
3874 *
3875 * LOCKING:
3876 * spin_lock_irqsave(host_set lock)
3877 */
3878
3879static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3880{
3881 struct ata_port *ap = qc->ap;
3882 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3883 u8 dmactl;
3884
3885 /* start host DMA transaction */
3886 dmactl = readb(mmio + ATA_DMA_CMD);
3887 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3888
3889 /* Strictly, one may wish to issue a readb() here, to
3890 * flush the mmio write. However, control also passes
3891 * to the hardware at this point, and it will interrupt
3892 * us when we are to resume control. So, in effect,
3893 * we don't care when the mmio write flushes.
3894 * Further, a read of the DMA status register _immediately_
3895 * following the write may not be what certain flaky hardware
3896 * is expected, so I think it is best to not add a readb()
3897 * without first all the MMIO ATA cards/mobos.
3898 * Or maybe I'm just being paranoid.
3899 */
3900}
3901
3902/**
3903 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3904 * @qc: Info associated with this ATA transaction.
3905 *
3906 * LOCKING:
3907 * spin_lock_irqsave(host_set lock)
3908 */
3909
3910static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3911{
3912 struct ata_port *ap = qc->ap;
3913 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3914 u8 dmactl;
3915
3916 /* load PRD table addr. */
3917 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3918
3919 /* specify data direction, triple-check start bit is clear */
3920 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3921 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3922 if (!rw)
3923 dmactl |= ATA_DMA_WR;
3924 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3925
3926 /* issue r/w command */
3927 ap->ops->exec_command(ap, &qc->tf);
3928}
3929
3930/**
3931 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3932 * @qc: Info associated with this ATA transaction.
3933 *
3934 * LOCKING:
3935 * spin_lock_irqsave(host_set lock)
3936 */
3937
3938static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3939{
3940 struct ata_port *ap = qc->ap;
3941 u8 dmactl;
3942
3943 /* start host DMA transaction */
3944 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3945 outb(dmactl | ATA_DMA_START,
3946 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3947}
3948
Edward Falk0baab862005-06-02 18:17:13 -04003949
3950/**
3951 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3952 * @qc: Info associated with this ATA transaction.
3953 *
3954 * Writes the ATA_DMA_START flag to the DMA command register.
3955 *
3956 * May be used as the bmdma_start() entry in ata_port_operations.
3957 *
3958 * LOCKING:
3959 * spin_lock_irqsave(host_set lock)
3960 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961void ata_bmdma_start(struct ata_queued_cmd *qc)
3962{
3963 if (qc->ap->flags & ATA_FLAG_MMIO)
3964 ata_bmdma_start_mmio(qc);
3965 else
3966 ata_bmdma_start_pio(qc);
3967}
3968
Edward Falk0baab862005-06-02 18:17:13 -04003969
3970/**
3971 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3972 * @qc: Info associated with this ATA transaction.
3973 *
3974 * Writes address of PRD table to device's PRD Table Address
3975 * register, sets the DMA control register, and calls
3976 * ops->exec_command() to start the transfer.
3977 *
3978 * May be used as the bmdma_setup() entry in ata_port_operations.
3979 *
3980 * LOCKING:
3981 * spin_lock_irqsave(host_set lock)
3982 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983void ata_bmdma_setup(struct ata_queued_cmd *qc)
3984{
3985 if (qc->ap->flags & ATA_FLAG_MMIO)
3986 ata_bmdma_setup_mmio(qc);
3987 else
3988 ata_bmdma_setup_pio(qc);
3989}
3990
Edward Falk0baab862005-06-02 18:17:13 -04003991
3992/**
3993 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003994 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003995 *
3996 * Clear interrupt and error flags in DMA status register.
3997 *
3998 * May be used as the irq_clear() entry in ata_port_operations.
3999 *
4000 * LOCKING:
4001 * spin_lock_irqsave(host_set lock)
4002 */
4003
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004void ata_bmdma_irq_clear(struct ata_port *ap)
4005{
4006 if (ap->flags & ATA_FLAG_MMIO) {
4007 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4008 writeb(readb(mmio), mmio);
4009 } else {
4010 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4011 outb(inb(addr), addr);
4012 }
4013
4014}
4015
Edward Falk0baab862005-06-02 18:17:13 -04004016
4017/**
4018 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004019 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004020 *
4021 * Read and return BMDMA status register.
4022 *
4023 * May be used as the bmdma_status() entry in ata_port_operations.
4024 *
4025 * LOCKING:
4026 * spin_lock_irqsave(host_set lock)
4027 */
4028
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029u8 ata_bmdma_status(struct ata_port *ap)
4030{
4031 u8 host_stat;
4032 if (ap->flags & ATA_FLAG_MMIO) {
4033 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4034 host_stat = readb(mmio + ATA_DMA_STATUS);
4035 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004036 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 return host_stat;
4038}
4039
Edward Falk0baab862005-06-02 18:17:13 -04004040
4041/**
4042 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004043 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004044 *
4045 * Clears the ATA_DMA_START flag in the dma control register
4046 *
4047 * May be used as the bmdma_stop() entry in ata_port_operations.
4048 *
4049 * LOCKING:
4050 * spin_lock_irqsave(host_set lock)
4051 */
4052
Alan Coxb73fc892005-08-26 16:03:19 +01004053void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054{
Alan Coxb73fc892005-08-26 16:03:19 +01004055 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 if (ap->flags & ATA_FLAG_MMIO) {
4057 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4058
4059 /* clear start/stop bit */
4060 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4061 mmio + ATA_DMA_CMD);
4062 } else {
4063 /* clear start/stop bit */
4064 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4065 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4066 }
4067
4068 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4069 ata_altstatus(ap); /* dummy read */
4070}
4071
4072/**
4073 * ata_host_intr - Handle host interrupt for given (port, task)
4074 * @ap: Port on which interrupt arrived (possibly...)
4075 * @qc: Taskfile currently active in engine
4076 *
4077 * Handle host interrupt for given queued command. Currently,
4078 * only DMA interrupts are handled. All other commands are
4079 * handled via polling with interrupts disabled (nIEN bit).
4080 *
4081 * LOCKING:
4082 * spin_lock_irqsave(host_set lock)
4083 *
4084 * RETURNS:
4085 * One if interrupt was handled, zero if not (shared irq).
4086 */
4087
4088inline unsigned int ata_host_intr (struct ata_port *ap,
4089 struct ata_queued_cmd *qc)
4090{
4091 u8 status, host_stat;
4092
4093 switch (qc->tf.protocol) {
4094
4095 case ATA_PROT_DMA:
4096 case ATA_PROT_ATAPI_DMA:
4097 case ATA_PROT_ATAPI:
4098 /* check status of DMA engine */
4099 host_stat = ap->ops->bmdma_status(ap);
4100 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4101
4102 /* if it's not our irq... */
4103 if (!(host_stat & ATA_DMA_INTR))
4104 goto idle_irq;
4105
4106 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004107 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
4109 /* fall through */
4110
4111 case ATA_PROT_ATAPI_NODATA:
4112 case ATA_PROT_NODATA:
4113 /* check altstatus */
4114 status = ata_altstatus(ap);
4115 if (status & ATA_BUSY)
4116 goto idle_irq;
4117
4118 /* check main status, clearing INTRQ */
4119 status = ata_chk_status(ap);
4120 if (unlikely(status & ATA_BUSY))
4121 goto idle_irq;
4122 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4123 ap->id, qc->tf.protocol, status);
4124
4125 /* ack bmdma irq events */
4126 ap->ops->irq_clear(ap);
4127
4128 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004129 qc->err_mask |= ac_err_mask(status);
4130 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 break;
4132
4133 default:
4134 goto idle_irq;
4135 }
4136
4137 return 1; /* irq handled */
4138
4139idle_irq:
4140 ap->stats.idle_irq++;
4141
4142#ifdef ATA_IRQ_TRAP
4143 if ((ap->stats.idle_irq % 1000) == 0) {
4144 handled = 1;
4145 ata_irq_ack(ap, 0); /* debug trap */
4146 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4147 }
4148#endif
4149 return 0; /* irq not handled */
4150}
4151
4152/**
4153 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004154 * @irq: irq line (unused)
4155 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 * @regs: unused
4157 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004158 * Default interrupt handler for PCI IDE devices. Calls
4159 * ata_host_intr() for each port that is not disabled.
4160 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004162 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 *
4164 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004165 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 */
4167
4168irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4169{
4170 struct ata_host_set *host_set = dev_instance;
4171 unsigned int i;
4172 unsigned int handled = 0;
4173 unsigned long flags;
4174
4175 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4176 spin_lock_irqsave(&host_set->lock, flags);
4177
4178 for (i = 0; i < host_set->n_ports; i++) {
4179 struct ata_port *ap;
4180
4181 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004182 if (ap &&
4183 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 struct ata_queued_cmd *qc;
4185
4186 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004187 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4188 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 handled |= ata_host_intr(ap, qc);
4190 }
4191 }
4192
4193 spin_unlock_irqrestore(&host_set->lock, flags);
4194
4195 return IRQ_RETVAL(handled);
4196}
4197
4198/**
4199 * atapi_packet_task - Write CDB bytes to hardware
4200 * @_data: Port to which ATAPI device is attached.
4201 *
4202 * When device has indicated its readiness to accept
4203 * a CDB, this function is called. Send the CDB.
4204 * If DMA is to be performed, exit immediately.
4205 * Otherwise, we are in polling mode, so poll
4206 * status under operation succeeds or fails.
4207 *
4208 * LOCKING:
4209 * Kernel thread context (may sleep)
4210 */
4211
4212static void atapi_packet_task(void *_data)
4213{
4214 struct ata_port *ap = _data;
4215 struct ata_queued_cmd *qc;
4216 u8 status;
4217
4218 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09004219 WARN_ON(qc == NULL);
4220 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221
4222 /* sleep-wait for BSY to clear */
4223 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004224 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004225 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Leed8fe4522005-12-05 15:42:17 +08004226 goto err_out;
4227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
4229 /* make sure DRQ is set */
4230 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004231 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004232 qc->err_mask |= AC_ERR_HSM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235
4236 /* send SCSI cdb */
4237 DPRINTK("send cdb\n");
Tejun Heo6e7846e2006-02-12 23:32:58 +09004238 WARN_ON(qc->dev->cdb_len < 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
Tejun Heoc1389502005-08-22 14:59:24 +09004240 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4241 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4242 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
Tejun Heoc1389502005-08-22 14:59:24 +09004244 /* Once we're done issuing command and kicking bmdma,
4245 * irq handler takes over. To not lose irq, we need
4246 * to clear NOINTR flag before sending cdb, but
4247 * interrupt handler shouldn't be invoked before we're
4248 * finished. Hence, the following locking.
4249 */
4250 spin_lock_irqsave(&ap->host_set->lock, flags);
4251 ap->flags &= ~ATA_FLAG_NOINTR;
Tejun Heo6e7846e2006-02-12 23:32:58 +09004252 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
Tejun Heoc1389502005-08-22 14:59:24 +09004253 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4254 ap->ops->bmdma_start(qc); /* initiate bmdma */
4255 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4256 } else {
Tejun Heo6e7846e2006-02-12 23:32:58 +09004257 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258
Tejun Heoc1389502005-08-22 14:59:24 +09004259 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004260 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09004261 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 }
4263
4264 return;
4265
4266err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004267 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268}
4269
Edward Falk0baab862005-06-02 18:17:13 -04004270
Jens Axboe9b847542006-01-06 09:28:07 +01004271/*
4272 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4273 * without filling any other registers
4274 */
4275static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4276 u8 cmd)
4277{
4278 struct ata_taskfile tf;
4279 int err;
4280
4281 ata_tf_init(ap, &tf, dev->devno);
4282
4283 tf.command = cmd;
4284 tf.flags |= ATA_TFLAG_DEVICE;
4285 tf.protocol = ATA_PROT_NODATA;
4286
4287 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4288 if (err)
4289 printk(KERN_ERR "%s: ata command failed: %d\n",
4290 __FUNCTION__, err);
4291
4292 return err;
4293}
4294
4295static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4296{
4297 u8 cmd;
4298
4299 if (!ata_try_flush_cache(dev))
4300 return 0;
4301
4302 if (ata_id_has_flush_ext(dev->id))
4303 cmd = ATA_CMD_FLUSH_EXT;
4304 else
4305 cmd = ATA_CMD_FLUSH;
4306
4307 return ata_do_simple_cmd(ap, dev, cmd);
4308}
4309
4310static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4311{
4312 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4313}
4314
4315static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4316{
4317 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4318}
4319
4320/**
4321 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004322 * @ap: port the device is connected to
4323 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004324 *
4325 * Kick the drive back into action, by sending it an idle immediate
4326 * command and making sure its transfer mode matches between drive
4327 * and host.
4328 *
4329 */
4330int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4331{
4332 if (ap->flags & ATA_FLAG_SUSPENDED) {
4333 ap->flags &= ~ATA_FLAG_SUSPENDED;
4334 ata_set_mode(ap);
4335 }
4336 if (!ata_dev_present(dev))
4337 return 0;
4338 if (dev->class == ATA_DEV_ATA)
4339 ata_start_drive(ap, dev);
4340
4341 return 0;
4342}
4343
4344/**
4345 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004346 * @ap: port the device is connected to
4347 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004348 *
4349 * Flush the cache on the drive, if appropriate, then issue a
4350 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004351 */
4352int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4353{
4354 if (!ata_dev_present(dev))
4355 return 0;
4356 if (dev->class == ATA_DEV_ATA)
4357 ata_flush_cache(ap, dev);
4358
4359 ata_standby_drive(ap, dev);
4360 ap->flags |= ATA_FLAG_SUSPENDED;
4361 return 0;
4362}
4363
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004364/**
4365 * ata_port_start - Set port up for dma.
4366 * @ap: Port to initialize
4367 *
4368 * Called just after data structures for each port are
4369 * initialized. Allocates space for PRD table.
4370 *
4371 * May be used as the port_start() entry in ata_port_operations.
4372 *
4373 * LOCKING:
4374 * Inherited from caller.
4375 */
4376
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377int ata_port_start (struct ata_port *ap)
4378{
4379 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004380 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381
4382 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4383 if (!ap->prd)
4384 return -ENOMEM;
4385
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004386 rc = ata_pad_alloc(ap, dev);
4387 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004388 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004389 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004390 }
4391
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4393
4394 return 0;
4395}
4396
Edward Falk0baab862005-06-02 18:17:13 -04004397
4398/**
4399 * ata_port_stop - Undo ata_port_start()
4400 * @ap: Port to shut down
4401 *
4402 * Frees the PRD table.
4403 *
4404 * May be used as the port_stop() entry in ata_port_operations.
4405 *
4406 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004407 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004408 */
4409
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410void ata_port_stop (struct ata_port *ap)
4411{
4412 struct device *dev = ap->host_set->dev;
4413
4414 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004415 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416}
4417
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004418void ata_host_stop (struct ata_host_set *host_set)
4419{
4420 if (host_set->mmio_base)
4421 iounmap(host_set->mmio_base);
4422}
4423
4424
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425/**
4426 * ata_host_remove - Unregister SCSI host structure with upper layers
4427 * @ap: Port to unregister
4428 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4429 *
4430 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004431 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 */
4433
4434static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4435{
4436 struct Scsi_Host *sh = ap->host;
4437
4438 DPRINTK("ENTER\n");
4439
4440 if (do_unregister)
4441 scsi_remove_host(sh);
4442
4443 ap->ops->port_stop(ap);
4444}
4445
4446/**
4447 * ata_host_init - Initialize an ata_port structure
4448 * @ap: Structure to initialize
4449 * @host: associated SCSI mid-layer structure
4450 * @host_set: Collection of hosts to which @ap belongs
4451 * @ent: Probe information provided by low-level driver
4452 * @port_no: Port number associated with this ata_port
4453 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004454 * Initialize a new ata_port structure, and its associated
4455 * scsi_host.
4456 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004458 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 */
4460
4461static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4462 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004463 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464{
4465 unsigned int i;
4466
4467 host->max_id = 16;
4468 host->max_lun = 1;
4469 host->max_channel = 1;
4470 host->unique_id = ata_unique_id++;
4471 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004472
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 ap->flags = ATA_FLAG_PORT_DISABLED;
4474 ap->id = host->unique_id;
4475 ap->host = host;
4476 ap->ctl = ATA_DEVCTL_OBS;
4477 ap->host_set = host_set;
4478 ap->port_no = port_no;
4479 ap->hard_port_no =
4480 ent->legacy_mode ? ent->hard_port_no : port_no;
4481 ap->pio_mask = ent->pio_mask;
4482 ap->mwdma_mask = ent->mwdma_mask;
4483 ap->udma_mask = ent->udma_mask;
4484 ap->flags |= ent->host_flags;
4485 ap->ops = ent->port_ops;
4486 ap->cbl = ATA_CBL_NONE;
4487 ap->active_tag = ATA_TAG_POISON;
4488 ap->last_ctl = 0xFF;
4489
4490 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4491 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004492 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
4494 for (i = 0; i < ATA_MAX_DEVICES; i++)
4495 ap->device[i].devno = i;
4496
4497#ifdef ATA_IRQ_TRAP
4498 ap->stats.unhandled_irq = 1;
4499 ap->stats.idle_irq = 1;
4500#endif
4501
4502 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4503}
4504
4505/**
4506 * ata_host_add - Attach low-level ATA driver to system
4507 * @ent: Information provided by low-level driver
4508 * @host_set: Collections of ports to which we add
4509 * @port_no: Port number associated with this host
4510 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004511 * Attach low-level ATA driver to system.
4512 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004514 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 *
4516 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004517 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 */
4519
Jeff Garzik057ace52005-10-22 14:27:05 -04004520static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 struct ata_host_set *host_set,
4522 unsigned int port_no)
4523{
4524 struct Scsi_Host *host;
4525 struct ata_port *ap;
4526 int rc;
4527
4528 DPRINTK("ENTER\n");
4529 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4530 if (!host)
4531 return NULL;
4532
4533 ap = (struct ata_port *) &host->hostdata[0];
4534
4535 ata_host_init(ap, host, host_set, ent, port_no);
4536
4537 rc = ap->ops->port_start(ap);
4538 if (rc)
4539 goto err_out;
4540
4541 return ap;
4542
4543err_out:
4544 scsi_host_put(host);
4545 return NULL;
4546}
4547
4548/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004549 * ata_device_add - Register hardware device with ATA and SCSI layers
4550 * @ent: Probe information describing hardware device to be registered
4551 *
4552 * This function processes the information provided in the probe
4553 * information struct @ent, allocates the necessary ATA and SCSI
4554 * host information structures, initializes them, and registers
4555 * everything with requisite kernel subsystems.
4556 *
4557 * This function requests irqs, probes the ATA bus, and probes
4558 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 *
4560 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004561 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 *
4563 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004564 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 */
4566
Jeff Garzik057ace52005-10-22 14:27:05 -04004567int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568{
4569 unsigned int count = 0, i;
4570 struct device *dev = ent->dev;
4571 struct ata_host_set *host_set;
4572
4573 DPRINTK("ENTER\n");
4574 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004575 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4577 if (!host_set)
4578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 spin_lock_init(&host_set->lock);
4580
4581 host_set->dev = dev;
4582 host_set->n_ports = ent->n_ports;
4583 host_set->irq = ent->irq;
4584 host_set->mmio_base = ent->mmio_base;
4585 host_set->private_data = ent->private_data;
4586 host_set->ops = ent->port_ops;
4587
4588 /* register each port bound to this device */
4589 for (i = 0; i < ent->n_ports; i++) {
4590 struct ata_port *ap;
4591 unsigned long xfer_mode_mask;
4592
4593 ap = ata_host_add(ent, host_set, i);
4594 if (!ap)
4595 goto err_out;
4596
4597 host_set->ports[i] = ap;
4598 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4599 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4600 (ap->pio_mask << ATA_SHIFT_PIO);
4601
4602 /* print per-port info to dmesg */
4603 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4604 "bmdma 0x%lX irq %lu\n",
4605 ap->id,
4606 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4607 ata_mode_string(xfer_mode_mask),
4608 ap->ioaddr.cmd_addr,
4609 ap->ioaddr.ctl_addr,
4610 ap->ioaddr.bmdma_addr,
4611 ent->irq);
4612
4613 ata_chk_status(ap);
4614 host_set->ops->irq_clear(ap);
4615 count++;
4616 }
4617
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004618 if (!count)
4619 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
4621 /* obtain irq, that is shared between channels */
4622 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4623 DRV_NAME, host_set))
4624 goto err_out;
4625
4626 /* perform each probe synchronously */
4627 DPRINTK("probe begin\n");
4628 for (i = 0; i < count; i++) {
4629 struct ata_port *ap;
4630 int rc;
4631
4632 ap = host_set->ports[i];
4633
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004634 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004636 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
4638 if (rc) {
4639 /* FIXME: do something useful here?
4640 * Current libata behavior will
4641 * tear down everything when
4642 * the module is removed
4643 * or the h/w is unplugged.
4644 */
4645 }
4646
4647 rc = scsi_add_host(ap->host, dev);
4648 if (rc) {
4649 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4650 ap->id);
4651 /* FIXME: do something useful here */
4652 /* FIXME: handle unconditional calls to
4653 * scsi_scan_host and ata_host_remove, below,
4654 * at the very least
4655 */
4656 }
4657 }
4658
4659 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004660 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 for (i = 0; i < count; i++) {
4662 struct ata_port *ap = host_set->ports[i];
4663
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004664 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 }
4666
4667 dev_set_drvdata(dev, host_set);
4668
4669 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4670 return ent->n_ports; /* success */
4671
4672err_out:
4673 for (i = 0; i < count; i++) {
4674 ata_host_remove(host_set->ports[i], 1);
4675 scsi_host_put(host_set->ports[i]->host);
4676 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004677err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 kfree(host_set);
4679 VPRINTK("EXIT, returning 0\n");
4680 return 0;
4681}
4682
4683/**
Alan Cox17b14452005-09-15 15:44:00 +01004684 * ata_host_set_remove - PCI layer callback for device removal
4685 * @host_set: ATA host set that was removed
4686 *
4687 * Unregister all objects associated with this host set. Free those
4688 * objects.
4689 *
4690 * LOCKING:
4691 * Inherited from calling layer (may sleep).
4692 */
4693
Alan Cox17b14452005-09-15 15:44:00 +01004694void ata_host_set_remove(struct ata_host_set *host_set)
4695{
4696 struct ata_port *ap;
4697 unsigned int i;
4698
4699 for (i = 0; i < host_set->n_ports; i++) {
4700 ap = host_set->ports[i];
4701 scsi_remove_host(ap->host);
4702 }
4703
4704 free_irq(host_set->irq, host_set);
4705
4706 for (i = 0; i < host_set->n_ports; i++) {
4707 ap = host_set->ports[i];
4708
4709 ata_scsi_release(ap->host);
4710
4711 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4712 struct ata_ioports *ioaddr = &ap->ioaddr;
4713
4714 if (ioaddr->cmd_addr == 0x1f0)
4715 release_region(0x1f0, 8);
4716 else if (ioaddr->cmd_addr == 0x170)
4717 release_region(0x170, 8);
4718 }
4719
4720 scsi_host_put(ap->host);
4721 }
4722
4723 if (host_set->ops->host_stop)
4724 host_set->ops->host_stop(host_set);
4725
4726 kfree(host_set);
4727}
4728
4729/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 * ata_scsi_release - SCSI layer callback hook for host unload
4731 * @host: libata host to be unloaded
4732 *
4733 * Performs all duties necessary to shut down a libata port...
4734 * Kill port kthread, disable port, and release resources.
4735 *
4736 * LOCKING:
4737 * Inherited from SCSI layer.
4738 *
4739 * RETURNS:
4740 * One.
4741 */
4742
4743int ata_scsi_release(struct Scsi_Host *host)
4744{
4745 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
Tejun Heod9572b12006-03-01 16:09:35 +09004746 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747
4748 DPRINTK("ENTER\n");
4749
4750 ap->ops->port_disable(ap);
4751 ata_host_remove(ap, 0);
Tejun Heod9572b12006-03-01 16:09:35 +09004752 for (i = 0; i < ATA_MAX_DEVICES; i++)
4753 kfree(ap->device[i].id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
4755 DPRINTK("EXIT\n");
4756 return 1;
4757}
4758
4759/**
4760 * ata_std_ports - initialize ioaddr with standard port offsets.
4761 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004762 *
4763 * Utility function which initializes data_addr, error_addr,
4764 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4765 * device_addr, status_addr, and command_addr to standard offsets
4766 * relative to cmd_addr.
4767 *
4768 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 */
Edward Falk0baab862005-06-02 18:17:13 -04004770
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771void ata_std_ports(struct ata_ioports *ioaddr)
4772{
4773 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4774 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4775 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4776 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4777 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4778 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4779 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4780 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4781 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4782 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4783}
4784
Edward Falk0baab862005-06-02 18:17:13 -04004785
Jeff Garzik374b1872005-08-30 05:42:52 -04004786#ifdef CONFIG_PCI
4787
4788void ata_pci_host_stop (struct ata_host_set *host_set)
4789{
4790 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4791
4792 pci_iounmap(pdev, host_set->mmio_base);
4793}
4794
Edward Falk0baab862005-06-02 18:17:13 -04004795/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 * ata_pci_remove_one - PCI layer callback for device removal
4797 * @pdev: PCI device that was removed
4798 *
4799 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004800 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 * Handle this by unregistering all objects associated
4802 * with this PCI device. Free those objects. Then finally
4803 * release PCI resources and disable device.
4804 *
4805 * LOCKING:
4806 * Inherited from PCI layer (may sleep).
4807 */
4808
4809void ata_pci_remove_one (struct pci_dev *pdev)
4810{
4811 struct device *dev = pci_dev_to_dev(pdev);
4812 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
Alan Cox17b14452005-09-15 15:44:00 +01004814 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 pci_release_regions(pdev);
4816 pci_disable_device(pdev);
4817 dev_set_drvdata(dev, NULL);
4818}
4819
4820/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004821int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822{
4823 unsigned long tmp = 0;
4824
4825 switch (bits->width) {
4826 case 1: {
4827 u8 tmp8 = 0;
4828 pci_read_config_byte(pdev, bits->reg, &tmp8);
4829 tmp = tmp8;
4830 break;
4831 }
4832 case 2: {
4833 u16 tmp16 = 0;
4834 pci_read_config_word(pdev, bits->reg, &tmp16);
4835 tmp = tmp16;
4836 break;
4837 }
4838 case 4: {
4839 u32 tmp32 = 0;
4840 pci_read_config_dword(pdev, bits->reg, &tmp32);
4841 tmp = tmp32;
4842 break;
4843 }
4844
4845 default:
4846 return -EINVAL;
4847 }
4848
4849 tmp &= bits->mask;
4850
4851 return (tmp == bits->val) ? 1 : 0;
4852}
Jens Axboe9b847542006-01-06 09:28:07 +01004853
4854int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4855{
4856 pci_save_state(pdev);
4857 pci_disable_device(pdev);
4858 pci_set_power_state(pdev, PCI_D3hot);
4859 return 0;
4860}
4861
4862int ata_pci_device_resume(struct pci_dev *pdev)
4863{
4864 pci_set_power_state(pdev, PCI_D0);
4865 pci_restore_state(pdev);
4866 pci_enable_device(pdev);
4867 pci_set_master(pdev);
4868 return 0;
4869}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870#endif /* CONFIG_PCI */
4871
4872
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873static int __init ata_init(void)
4874{
4875 ata_wq = create_workqueue("ata");
4876 if (!ata_wq)
4877 return -ENOMEM;
4878
4879 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4880 return 0;
4881}
4882
4883static void __exit ata_exit(void)
4884{
4885 destroy_workqueue(ata_wq);
4886}
4887
4888module_init(ata_init);
4889module_exit(ata_exit);
4890
Jeff Garzik67846b32005-10-05 02:58:32 -04004891static unsigned long ratelimit_time;
4892static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4893
4894int ata_ratelimit(void)
4895{
4896 int rc;
4897 unsigned long flags;
4898
4899 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4900
4901 if (time_after(jiffies, ratelimit_time)) {
4902 rc = 1;
4903 ratelimit_time = jiffies + (HZ/5);
4904 } else
4905 rc = 0;
4906
4907 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4908
4909 return rc;
4910}
4911
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912/*
4913 * libata is essentially a library of internal helper functions for
4914 * low-level ATA host controller drivers. As such, the API/ABI is
4915 * likely to change as new drivers are added and updated.
4916 * Do not depend on ABI/API stability.
4917 */
4918
4919EXPORT_SYMBOL_GPL(ata_std_bios_param);
4920EXPORT_SYMBOL_GPL(ata_std_ports);
4921EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01004922EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923EXPORT_SYMBOL_GPL(ata_sg_init);
4924EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo76014422006-02-11 15:13:49 +09004925EXPORT_SYMBOL_GPL(__ata_qc_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4927EXPORT_SYMBOL_GPL(ata_eng_timeout);
4928EXPORT_SYMBOL_GPL(ata_tf_load);
4929EXPORT_SYMBOL_GPL(ata_tf_read);
4930EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4931EXPORT_SYMBOL_GPL(ata_std_dev_select);
4932EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4933EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4934EXPORT_SYMBOL_GPL(ata_check_status);
4935EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936EXPORT_SYMBOL_GPL(ata_exec_command);
4937EXPORT_SYMBOL_GPL(ata_port_start);
4938EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004939EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940EXPORT_SYMBOL_GPL(ata_interrupt);
4941EXPORT_SYMBOL_GPL(ata_qc_prep);
4942EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4943EXPORT_SYMBOL_GPL(ata_bmdma_start);
4944EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4945EXPORT_SYMBOL_GPL(ata_bmdma_status);
4946EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4947EXPORT_SYMBOL_GPL(ata_port_probe);
4948EXPORT_SYMBOL_GPL(sata_phy_reset);
4949EXPORT_SYMBOL_GPL(__sata_phy_reset);
4950EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09004951EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004952EXPORT_SYMBOL_GPL(ata_std_softreset);
4953EXPORT_SYMBOL_GPL(sata_std_hardreset);
4954EXPORT_SYMBOL_GPL(ata_std_postreset);
4955EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09004956EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04004958EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09004959EXPORT_SYMBOL_GPL(ata_busy_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4961EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Tejun Heof29841e2006-02-10 15:10:48 +09004962EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963EXPORT_SYMBOL_GPL(ata_scsi_error);
4964EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4965EXPORT_SYMBOL_GPL(ata_scsi_release);
4966EXPORT_SYMBOL_GPL(ata_host_intr);
4967EXPORT_SYMBOL_GPL(ata_dev_classify);
Tejun Heo6a62a042006-02-13 10:02:46 +09004968EXPORT_SYMBOL_GPL(ata_id_string);
4969EXPORT_SYMBOL_GPL(ata_id_c_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04004970EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004972EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
4973EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974
Alan Cox1bc4ccf2006-01-09 17:18:14 +00004975EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04004976EXPORT_SYMBOL_GPL(ata_timing_compute);
4977EXPORT_SYMBOL_GPL(ata_timing_merge);
4978
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979#ifdef CONFIG_PCI
4980EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04004981EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4983EXPORT_SYMBOL_GPL(ata_pci_init_one);
4984EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01004985EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
4986EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01004988
4989EXPORT_SYMBOL_GPL(ata_device_suspend);
4990EXPORT_SYMBOL_GPL(ata_device_resume);
4991EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
4992EXPORT_SYMBOL_GPL(ata_scsi_device_resume);