blob: 10803f72c57da4b0f1a8dbd3713248f251024c20 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04002 * libata-core.c - helper library for ATA
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35#include <linux/config.h>
36#include <linux/kernel.h>
37#include <linux/module.h>
38#include <linux/pci.h>
39#include <linux/init.h>
40#include <linux/list.h>
41#include <linux/mm.h>
42#include <linux/highmem.h>
43#include <linux/spinlock.h>
44#include <linux/blkdev.h>
45#include <linux/delay.h>
46#include <linux/timer.h>
47#include <linux/interrupt.h>
48#include <linux/completion.h>
49#include <linux/suspend.h>
50#include <linux/workqueue.h>
Jeff Garzik67846b32005-10-05 02:58:32 -040051#include <linux/jiffies.h>
David Hardeman378f0582005-09-17 17:55:31 +100052#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "scsi_priv.h"
Jeff Garzik193515d2005-11-07 00:59:37 -050055#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <scsi/scsi_host.h>
57#include <linux/libata.h>
58#include <asm/io.h>
59#include <asm/semaphore.h>
60#include <asm/byteorder.h>
61
62#include "libata.h"
63
Tejun Heo6aff8f12006-02-15 18:24:09 +090064static unsigned int ata_dev_init_params(struct ata_port *ap,
65 struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void ata_set_mode(struct ata_port *ap);
67static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
Jeff Garzik057ace52005-10-22 14:27:05 -040068static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int fgb(u32 bitmap);
Jeff Garzik057ace52005-10-22 14:27:05 -040070static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 u8 *xfer_mode_out,
72 unsigned int *xfer_shift_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static unsigned int ata_unique_id = 1;
75static struct workqueue_struct *ata_wq;
76
Jeff Garzik1623c812005-08-30 03:37:42 -040077int atapi_enabled = 0;
78module_param(atapi_enabled, int, 0444);
79MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
80
Jeff Garzikc3c013a2006-02-27 22:31:19 -050081int libata_fua = 0;
82module_param_named(fua, libata_fua, int, 0444);
83MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085MODULE_AUTHOR("Jeff Garzik");
86MODULE_DESCRIPTION("Library module for ATA devices");
87MODULE_LICENSE("GPL");
88MODULE_VERSION(DRV_VERSION);
89
Edward Falk0baab862005-06-02 18:17:13 -040090
91/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
93 * @tf: Taskfile to convert
94 * @fis: Buffer into which data will output
95 * @pmp: Port multiplier port
96 *
97 * Converts a standard ATA taskfile to a Serial ATA
98 * FIS structure (Register - Host to Device).
99 *
100 * LOCKING:
101 * Inherited from caller.
102 */
103
Jeff Garzik057ace52005-10-22 14:27:05 -0400104void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 fis[0] = 0x27; /* Register - Host to Device FIS */
107 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
108 bit 7 indicates Command FIS */
109 fis[2] = tf->command;
110 fis[3] = tf->feature;
111
112 fis[4] = tf->lbal;
113 fis[5] = tf->lbam;
114 fis[6] = tf->lbah;
115 fis[7] = tf->device;
116
117 fis[8] = tf->hob_lbal;
118 fis[9] = tf->hob_lbam;
119 fis[10] = tf->hob_lbah;
120 fis[11] = tf->hob_feature;
121
122 fis[12] = tf->nsect;
123 fis[13] = tf->hob_nsect;
124 fis[14] = 0;
125 fis[15] = tf->ctl;
126
127 fis[16] = 0;
128 fis[17] = 0;
129 fis[18] = 0;
130 fis[19] = 0;
131}
132
133/**
134 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
135 * @fis: Buffer from which data will be input
136 * @tf: Taskfile to output
137 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500138 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 *
140 * LOCKING:
141 * Inherited from caller.
142 */
143
Jeff Garzik057ace52005-10-22 14:27:05 -0400144void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 tf->command = fis[2]; /* status */
147 tf->feature = fis[3]; /* error */
148
149 tf->lbal = fis[4];
150 tf->lbam = fis[5];
151 tf->lbah = fis[6];
152 tf->device = fis[7];
153
154 tf->hob_lbal = fis[8];
155 tf->hob_lbam = fis[9];
156 tf->hob_lbah = fis[10];
157
158 tf->nsect = fis[12];
159 tf->hob_nsect = fis[13];
160}
161
Albert Lee8cbd6df2005-10-12 15:06:27 +0800162static const u8 ata_rw_cmds[] = {
163 /* pio multi */
164 ATA_CMD_READ_MULTI,
165 ATA_CMD_WRITE_MULTI,
166 ATA_CMD_READ_MULTI_EXT,
167 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100168 0,
169 0,
170 0,
171 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800172 /* pio */
173 ATA_CMD_PIO_READ,
174 ATA_CMD_PIO_WRITE,
175 ATA_CMD_PIO_READ_EXT,
176 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100177 0,
178 0,
179 0,
180 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800181 /* dma */
182 ATA_CMD_READ,
183 ATA_CMD_WRITE,
184 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100185 ATA_CMD_WRITE_EXT,
186 0,
187 0,
188 0,
189 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800190};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800193 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
194 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800196 * Examine the device configuration and tf->flags to calculate
197 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 *
199 * LOCKING:
200 * caller.
201 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100202int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800204 struct ata_taskfile *tf = &qc->tf;
205 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100206 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100208 int index, fua, lba48, write;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800209
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100210 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800211 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
212 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Albert Lee8cbd6df2005-10-12 15:06:27 +0800214 if (dev->flags & ATA_DFLAG_PIO) {
215 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100216 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000217 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
218 /* Unable to use DMA due to host limitation */
219 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800220 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800221 } else {
222 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100223 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100226 cmd = ata_rw_cmds[index + fua + lba48 + write];
227 if (cmd) {
228 tf->command = cmd;
229 return 0;
230 }
231 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100234static const char * const xfer_mode_str[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 "UDMA/16",
236 "UDMA/25",
237 "UDMA/33",
238 "UDMA/44",
239 "UDMA/66",
240 "UDMA/100",
241 "UDMA/133",
242 "UDMA7",
243 "MWDMA0",
244 "MWDMA1",
245 "MWDMA2",
246 "PIO0",
247 "PIO1",
248 "PIO2",
249 "PIO3",
250 "PIO4",
251};
252
253/**
254 * ata_udma_string - convert UDMA bit offset to string
255 * @mask: mask of bits supported; only highest bit counts.
256 *
257 * Determine string which represents the highest speed
258 * (highest bit in @udma_mask).
259 *
260 * LOCKING:
261 * None.
262 *
263 * RETURNS:
264 * Constant C string representing highest speed listed in
265 * @udma_mask, or the constant C string "<n/a>".
266 */
267
268static const char *ata_mode_string(unsigned int mask)
269{
270 int i;
271
272 for (i = 7; i >= 0; i--)
273 if (mask & (1 << i))
274 goto out;
275 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
276 if (mask & (1 << i))
277 goto out;
278 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
279 if (mask & (1 << i))
280 goto out;
281
282 return "<n/a>";
283
284out:
285 return xfer_mode_str[i];
286}
287
288/**
289 * ata_pio_devchk - PATA device presence detection
290 * @ap: ATA channel to examine
291 * @device: Device to examine (starting at zero)
292 *
293 * This technique was originally described in
294 * Hale Landis's ATADRVR (www.ata-atapi.com), and
295 * later found its way into the ATA/ATAPI spec.
296 *
297 * Write a pattern to the ATA shadow registers,
298 * and if a device is present, it will respond by
299 * correctly storing and echoing back the
300 * ATA shadow register contents.
301 *
302 * LOCKING:
303 * caller.
304 */
305
306static unsigned int ata_pio_devchk(struct ata_port *ap,
307 unsigned int device)
308{
309 struct ata_ioports *ioaddr = &ap->ioaddr;
310 u8 nsect, lbal;
311
312 ap->ops->dev_select(ap, device);
313
314 outb(0x55, ioaddr->nsect_addr);
315 outb(0xaa, ioaddr->lbal_addr);
316
317 outb(0xaa, ioaddr->nsect_addr);
318 outb(0x55, ioaddr->lbal_addr);
319
320 outb(0x55, ioaddr->nsect_addr);
321 outb(0xaa, ioaddr->lbal_addr);
322
323 nsect = inb(ioaddr->nsect_addr);
324 lbal = inb(ioaddr->lbal_addr);
325
326 if ((nsect == 0x55) && (lbal == 0xaa))
327 return 1; /* we found a device */
328
329 return 0; /* nothing found */
330}
331
332/**
333 * ata_mmio_devchk - PATA device presence detection
334 * @ap: ATA channel to examine
335 * @device: Device to examine (starting at zero)
336 *
337 * This technique was originally described in
338 * Hale Landis's ATADRVR (www.ata-atapi.com), and
339 * later found its way into the ATA/ATAPI spec.
340 *
341 * Write a pattern to the ATA shadow registers,
342 * and if a device is present, it will respond by
343 * correctly storing and echoing back the
344 * ATA shadow register contents.
345 *
346 * LOCKING:
347 * caller.
348 */
349
350static unsigned int ata_mmio_devchk(struct ata_port *ap,
351 unsigned int device)
352{
353 struct ata_ioports *ioaddr = &ap->ioaddr;
354 u8 nsect, lbal;
355
356 ap->ops->dev_select(ap, device);
357
358 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
359 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
360
361 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
362 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
363
364 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
365 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
366
367 nsect = readb((void __iomem *) ioaddr->nsect_addr);
368 lbal = readb((void __iomem *) ioaddr->lbal_addr);
369
370 if ((nsect == 0x55) && (lbal == 0xaa))
371 return 1; /* we found a device */
372
373 return 0; /* nothing found */
374}
375
376/**
377 * ata_devchk - PATA device presence detection
378 * @ap: ATA channel to examine
379 * @device: Device to examine (starting at zero)
380 *
381 * Dispatch ATA device presence detection, depending
382 * on whether we are using PIO or MMIO to talk to the
383 * ATA shadow registers.
384 *
385 * LOCKING:
386 * caller.
387 */
388
389static unsigned int ata_devchk(struct ata_port *ap,
390 unsigned int device)
391{
392 if (ap->flags & ATA_FLAG_MMIO)
393 return ata_mmio_devchk(ap, device);
394 return ata_pio_devchk(ap, device);
395}
396
397/**
398 * ata_dev_classify - determine device type based on ATA-spec signature
399 * @tf: ATA taskfile register set for device to be identified
400 *
401 * Determine from taskfile register contents whether a device is
402 * ATA or ATAPI, as per "Signature and persistence" section
403 * of ATA/PI spec (volume 1, sect 5.14).
404 *
405 * LOCKING:
406 * None.
407 *
408 * RETURNS:
409 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
410 * the event of failure.
411 */
412
Jeff Garzik057ace52005-10-22 14:27:05 -0400413unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 /* Apple's open source Darwin code hints that some devices only
416 * put a proper signature into the LBA mid/high registers,
417 * So, we only check those. It's sufficient for uniqueness.
418 */
419
420 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
421 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
422 DPRINTK("found ATA device by sig\n");
423 return ATA_DEV_ATA;
424 }
425
426 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
427 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
428 DPRINTK("found ATAPI device by sig\n");
429 return ATA_DEV_ATAPI;
430 }
431
432 DPRINTK("unknown device\n");
433 return ATA_DEV_UNKNOWN;
434}
435
436/**
437 * ata_dev_try_classify - Parse returned ATA device signature
438 * @ap: ATA channel to examine
439 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900440 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 *
442 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
443 * an ATA/ATAPI-defined set of values is placed in the ATA
444 * shadow registers, indicating the results of device detection
445 * and diagnostics.
446 *
447 * Select the ATA device, and read the values from the ATA shadow
448 * registers. Then parse according to the Error register value,
449 * and the spec-defined values examined by ata_dev_classify().
450 *
451 * LOCKING:
452 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900453 *
454 * RETURNS:
455 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 */
457
Tejun Heob4dc7622006-01-24 17:05:22 +0900458static unsigned int
459ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct ata_taskfile tf;
462 unsigned int class;
463 u8 err;
464
465 ap->ops->dev_select(ap, device);
466
467 memset(&tf, 0, sizeof(tf));
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400470 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900471 if (r_err)
472 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 /* see if device passed diags */
475 if (err == 1)
476 /* do nothing */ ;
477 else if ((device == 0) && (err == 0x81))
478 /* do nothing */ ;
479 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900480 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Tejun Heob4dc7622006-01-24 17:05:22 +0900482 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900486 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900488 return ATA_DEV_NONE;
489 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900493 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * @id: IDENTIFY DEVICE results we will examine
495 * @s: string into which data is output
496 * @ofs: offset into identify device page
497 * @len: length of string to return. must be an even number.
498 *
499 * The strings in the IDENTIFY DEVICE page are broken up into
500 * 16-bit chunks. Run through the string, and output each
501 * 8-bit chunk linearly, regardless of platform.
502 *
503 * LOCKING:
504 * caller.
505 */
506
Tejun Heo6a62a042006-02-13 10:02:46 +0900507void ata_id_string(const u16 *id, unsigned char *s,
508 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 unsigned int c;
511
512 while (len > 0) {
513 c = id[ofs] >> 8;
514 *s = c;
515 s++;
516
517 c = id[ofs] & 0xff;
518 *s = c;
519 s++;
520
521 ofs++;
522 len -= 2;
523 }
524}
525
Tejun Heo0e949ff2006-02-12 22:47:04 +0900526/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900527 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900528 * @id: IDENTIFY DEVICE results we will examine
529 * @s: string into which data is output
530 * @ofs: offset into identify device page
531 * @len: length of string to return. must be an odd number.
532 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900533 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900534 * trims trailing spaces and terminates the resulting string with
535 * null. @len must be actual maximum length (even number) + 1.
536 *
537 * LOCKING:
538 * caller.
539 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900540void ata_id_c_string(const u16 *id, unsigned char *s,
541 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900542{
543 unsigned char *p;
544
545 WARN_ON(!(len & 1));
546
Tejun Heo6a62a042006-02-13 10:02:46 +0900547 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900548
549 p = s + strnlen(s, len - 1);
550 while (p > s && p[-1] == ' ')
551 p--;
552 *p = '\0';
553}
Edward Falk0baab862005-06-02 18:17:13 -0400554
Tejun Heo29407402006-02-12 22:47:04 +0900555static u64 ata_id_n_sectors(const u16 *id)
556{
557 if (ata_id_has_lba(id)) {
558 if (ata_id_has_lba48(id))
559 return ata_id_u64(id, 100);
560 else
561 return ata_id_u32(id, 60);
562 } else {
563 if (ata_id_current_chs_valid(id))
564 return ata_id_u32(id, 57);
565 else
566 return id[1] * id[3] * id[6];
567 }
568}
569
Edward Falk0baab862005-06-02 18:17:13 -0400570/**
571 * ata_noop_dev_select - Select device 0/1 on ATA bus
572 * @ap: ATA channel to manipulate
573 * @device: ATA device (numbered from zero) to select
574 *
575 * This function performs no actual function.
576 *
577 * May be used as the dev_select() entry in ata_port_operations.
578 *
579 * LOCKING:
580 * caller.
581 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
583{
584}
585
Edward Falk0baab862005-06-02 18:17:13 -0400586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587/**
588 * ata_std_dev_select - Select device 0/1 on ATA bus
589 * @ap: ATA channel to manipulate
590 * @device: ATA device (numbered from zero) to select
591 *
592 * Use the method defined in the ATA specification to
593 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400594 * ATA channel. Works with both PIO and MMIO.
595 *
596 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 *
598 * LOCKING:
599 * caller.
600 */
601
602void ata_std_dev_select (struct ata_port *ap, unsigned int device)
603{
604 u8 tmp;
605
606 if (device == 0)
607 tmp = ATA_DEVICE_OBS;
608 else
609 tmp = ATA_DEVICE_OBS | ATA_DEV1;
610
611 if (ap->flags & ATA_FLAG_MMIO) {
612 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
613 } else {
614 outb(tmp, ap->ioaddr.device_addr);
615 }
616 ata_pause(ap); /* needed; also flushes, for mmio */
617}
618
619/**
620 * ata_dev_select - Select device 0/1 on ATA bus
621 * @ap: ATA channel to manipulate
622 * @device: ATA device (numbered from zero) to select
623 * @wait: non-zero to wait for Status register BSY bit to clear
624 * @can_sleep: non-zero if context allows sleeping
625 *
626 * Use the method defined in the ATA specification to
627 * make either device 0, or device 1, active on the
628 * ATA channel.
629 *
630 * This is a high-level version of ata_std_dev_select(),
631 * which additionally provides the services of inserting
632 * the proper pauses and status polling, where needed.
633 *
634 * LOCKING:
635 * caller.
636 */
637
638void ata_dev_select(struct ata_port *ap, unsigned int device,
639 unsigned int wait, unsigned int can_sleep)
640{
641 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
642 ap->id, device, wait);
643
644 if (wait)
645 ata_wait_idle(ap);
646
647 ap->ops->dev_select(ap, device);
648
649 if (wait) {
650 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
651 msleep(150);
652 ata_wait_idle(ap);
653 }
654}
655
656/**
657 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +0900658 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 *
Tejun Heo0bd33002006-02-12 22:47:05 +0900660 * Dump selected 16-bit words from the given IDENTIFY DEVICE
661 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 *
663 * LOCKING:
664 * caller.
665 */
666
Tejun Heo0bd33002006-02-12 22:47:05 +0900667static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 DPRINTK("49==0x%04x "
670 "53==0x%04x "
671 "63==0x%04x "
672 "64==0x%04x "
673 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900674 id[49],
675 id[53],
676 id[63],
677 id[64],
678 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 DPRINTK("80==0x%04x "
680 "81==0x%04x "
681 "82==0x%04x "
682 "83==0x%04x "
683 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900684 id[80],
685 id[81],
686 id[82],
687 id[83],
688 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 DPRINTK("88==0x%04x "
690 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900691 id[88],
692 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Alan Cox11e29e22005-10-21 18:46:32 -0400695/*
696 * Compute the PIO modes available for this device. This is not as
697 * trivial as it seems if we must consider early devices correctly.
698 *
699 * FIXME: pre IDE drive timing (do we care ?).
700 */
701
Jeff Garzik057ace52005-10-22 14:27:05 -0400702static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -0400703{
704 u16 modes;
705
Alan Coxffa29452006-01-09 17:14:40 +0000706 /* Usual case. Word 53 indicates word 64 is valid */
707 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
Alan Cox11e29e22005-10-21 18:46:32 -0400708 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
709 modes <<= 3;
710 modes |= 0x7;
711 return modes;
712 }
713
Alan Coxffa29452006-01-09 17:14:40 +0000714 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
715 number for the maximum. Turn it into a mask and return it */
716 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
Alan Cox11e29e22005-10-21 18:46:32 -0400717 return modes;
Alan Coxffa29452006-01-09 17:14:40 +0000718 /* But wait.. there's more. Design your standards by committee and
719 you too can get a free iordy field to process. However its the
720 speeds not the modes that are supported... Note drivers using the
721 timing API will get this right anyway */
Alan Cox11e29e22005-10-21 18:46:32 -0400722}
723
Tejun Heo95064372006-01-23 13:09:37 +0900724static inline void
725ata_queue_packet_task(struct ata_port *ap)
726{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900727 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
728 queue_work(ata_wq, &ap->packet_task);
Tejun Heo95064372006-01-23 13:09:37 +0900729}
730
731static inline void
732ata_queue_pio_task(struct ata_port *ap)
733{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900734 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
735 queue_work(ata_wq, &ap->pio_task);
Tejun Heo95064372006-01-23 13:09:37 +0900736}
737
738static inline void
739ata_queue_delayed_pio_task(struct ata_port *ap, unsigned long delay)
740{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900741 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
742 queue_delayed_work(ata_wq, &ap->pio_task, delay);
743}
744
745/**
746 * ata_flush_pio_tasks - Flush pio_task and packet_task
747 * @ap: the target ata_port
748 *
749 * After this function completes, pio_task and packet_task are
750 * guranteed not to be running or scheduled.
751 *
752 * LOCKING:
753 * Kernel thread context (may sleep)
754 */
755
756static void ata_flush_pio_tasks(struct ata_port *ap)
757{
758 int tmp = 0;
759 unsigned long flags;
760
761 DPRINTK("ENTER\n");
762
763 spin_lock_irqsave(&ap->host_set->lock, flags);
764 ap->flags |= ATA_FLAG_FLUSH_PIO_TASK;
765 spin_unlock_irqrestore(&ap->host_set->lock, flags);
766
767 DPRINTK("flush #1\n");
768 flush_workqueue(ata_wq);
769
770 /*
771 * At this point, if a task is running, it's guaranteed to see
772 * the FLUSH flag; thus, it will never queue pio tasks again.
773 * Cancel and flush.
774 */
775 tmp |= cancel_delayed_work(&ap->pio_task);
776 tmp |= cancel_delayed_work(&ap->packet_task);
777 if (!tmp) {
778 DPRINTK("flush #2\n");
779 flush_workqueue(ata_wq);
780 }
781
782 spin_lock_irqsave(&ap->host_set->lock, flags);
783 ap->flags &= ~ATA_FLAG_FLUSH_PIO_TASK;
784 spin_unlock_irqrestore(&ap->host_set->lock, flags);
785
786 DPRINTK("EXIT\n");
Tejun Heo95064372006-01-23 13:09:37 +0900787}
788
Tejun Heo77853bf2006-01-23 13:09:36 +0900789void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +0900790{
Tejun Heo77853bf2006-01-23 13:09:36 +0900791 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900792
Tejun Heo77853bf2006-01-23 13:09:36 +0900793 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900794 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900795}
796
797/**
798 * ata_exec_internal - execute libata internal command
799 * @ap: Port to which the command is sent
800 * @dev: Device to which the command is sent
801 * @tf: Taskfile registers for the command and the result
802 * @dma_dir: Data tranfer direction of the command
803 * @buf: Data buffer of the command
804 * @buflen: Length of data buffer
805 *
806 * Executes libata internal command with timeout. @tf contains
807 * command on entry and result on return. Timeout and error
808 * conditions are reported via return value. No recovery action
809 * is taken after a command times out. It's caller's duty to
810 * clean up after timeout.
811 *
812 * LOCKING:
813 * None. Should be called with kernel context, might sleep.
814 */
815
816static unsigned
817ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
818 struct ata_taskfile *tf,
819 int dma_dir, void *buf, unsigned int buflen)
820{
821 u8 command = tf->command;
822 struct ata_queued_cmd *qc;
823 DECLARE_COMPLETION(wait);
824 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +0900825 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900826
827 spin_lock_irqsave(&ap->host_set->lock, flags);
828
829 qc = ata_qc_new_init(ap, dev);
830 BUG_ON(qc == NULL);
831
832 qc->tf = *tf;
833 qc->dma_dir = dma_dir;
834 if (dma_dir != DMA_NONE) {
835 ata_sg_init_one(qc, buf, buflen);
836 qc->nsect = buflen / ATA_SECT_SIZE;
837 }
838
Tejun Heo77853bf2006-01-23 13:09:36 +0900839 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900840 qc->complete_fn = ata_qc_complete_internal;
841
Tejun Heo9a3d9eb2006-01-23 13:09:36 +0900842 qc->err_mask = ata_qc_issue(qc);
843 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +0900844 ata_qc_complete(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900845
846 spin_unlock_irqrestore(&ap->host_set->lock, flags);
847
848 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
849 spin_lock_irqsave(&ap->host_set->lock, flags);
850
851 /* We're racing with irq here. If we lose, the
852 * following test prevents us from completing the qc
853 * again. If completion irq occurs after here but
854 * before the caller cleans up, it will result in a
855 * spurious interrupt. We can live with that.
856 */
Tejun Heo77853bf2006-01-23 13:09:36 +0900857 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +0900858 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900859 ata_qc_complete(qc);
860 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
861 ap->id, command);
862 }
863
864 spin_unlock_irqrestore(&ap->host_set->lock, flags);
865 }
866
Tejun Heo77853bf2006-01-23 13:09:36 +0900867 *tf = qc->tf;
868 err_mask = qc->err_mask;
869
870 ata_qc_free(qc);
871
872 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +0000876 * ata_pio_need_iordy - check if iordy needed
877 * @adev: ATA device
878 *
879 * Check if the current speed of the device requires IORDY. Used
880 * by various controllers for chip configuration.
881 */
882
883unsigned int ata_pio_need_iordy(const struct ata_device *adev)
884{
885 int pio;
886 int speed = adev->pio_mode - XFER_PIO_0;
887
888 if (speed < 2)
889 return 0;
890 if (speed > 2)
891 return 1;
892
893 /* If we have no drive specific rule, then PIO 2 is non IORDY */
894
895 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
896 pio = adev->id[ATA_ID_EIDE_PIO];
897 /* Is the speed faster than the drive allows non IORDY ? */
898 if (pio) {
899 /* This is cycle times not frequency - watch the logic! */
900 if (pio > 240) /* PIO2 is 240nS per cycle */
901 return 1;
902 return 0;
903 }
904 }
905 return 0;
906}
907
908/**
Tejun Heo49016ac2006-02-21 02:12:11 +0900909 * ata_dev_read_id - Read ID data from the specified device
910 * @ap: port on which target device resides
911 * @dev: target device
912 * @p_class: pointer to class of the target device (may be changed)
913 * @post_reset: is this read ID post-reset?
Tejun Heod9572b12006-03-01 16:09:35 +0900914 * @p_id: read IDENTIFY page (newly allocated)
Tejun Heo49016ac2006-02-21 02:12:11 +0900915 *
916 * Read ID data from the specified device. ATA_CMD_ID_ATA is
917 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
918 * devices. This function also takes care of EDD signature
919 * misreporting (to be removed once EDD support is gone) and
920 * issues ATA_CMD_INIT_DEV_PARAMS for pre-ATA4 drives.
921 *
922 * LOCKING:
923 * Kernel thread context (may sleep)
924 *
925 * RETURNS:
926 * 0 on success, -errno otherwise.
927 */
928static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
Tejun Heod9572b12006-03-01 16:09:35 +0900929 unsigned int *p_class, int post_reset, u16 **p_id)
Tejun Heo49016ac2006-02-21 02:12:11 +0900930{
931 unsigned int class = *p_class;
932 unsigned int using_edd;
933 struct ata_taskfile tf;
934 unsigned int err_mask = 0;
Tejun Heod9572b12006-03-01 16:09:35 +0900935 u16 *id;
Tejun Heo49016ac2006-02-21 02:12:11 +0900936 const char *reason;
937 int rc;
938
939 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
940
941 if (ap->ops->probe_reset ||
942 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
943 using_edd = 0;
944 else
945 using_edd = 1;
946
947 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
948
Tejun Heod9572b12006-03-01 16:09:35 +0900949 id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
950 if (id == NULL) {
951 rc = -ENOMEM;
952 reason = "out of memory";
953 goto err_out;
954 }
955
Tejun Heo49016ac2006-02-21 02:12:11 +0900956 retry:
957 ata_tf_init(ap, &tf, dev->devno);
958
959 switch (class) {
960 case ATA_DEV_ATA:
961 tf.command = ATA_CMD_ID_ATA;
962 break;
963 case ATA_DEV_ATAPI:
964 tf.command = ATA_CMD_ID_ATAPI;
965 break;
966 default:
967 rc = -ENODEV;
968 reason = "unsupported class";
969 goto err_out;
970 }
971
972 tf.protocol = ATA_PROT_PIO;
973
974 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
975 id, sizeof(id[0]) * ATA_ID_WORDS);
976
977 if (err_mask) {
978 rc = -EIO;
979 reason = "I/O error";
980
981 if (err_mask & ~AC_ERR_DEV)
982 goto err_out;
983
984 /*
985 * arg! EDD works for all test cases, but seems to return
986 * the ATA signature for some ATAPI devices. Until the
987 * reason for this is found and fixed, we fix up the mess
988 * here. If IDENTIFY DEVICE returns command aborted
989 * (as ATAPI devices do), then we issue an
990 * IDENTIFY PACKET DEVICE.
991 *
992 * ATA software reset (SRST, the default) does not appear
993 * to have this problem.
994 */
995 if ((using_edd) && (class == ATA_DEV_ATA)) {
996 u8 err = tf.feature;
997 if (err & ATA_ABORTED) {
998 class = ATA_DEV_ATAPI;
999 goto retry;
1000 }
1001 }
1002 goto err_out;
1003 }
1004
1005 swap_buf_le16(id, ATA_ID_WORDS);
1006
1007 /* print device capabilities */
1008 printk(KERN_DEBUG "ata%u: dev %u cfg "
1009 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1010 ap->id, dev->devno,
1011 id[49], id[82], id[83], id[84], id[85], id[86], id[87], id[88]);
1012
1013 /* sanity check */
1014 if ((class == ATA_DEV_ATA) != ata_id_is_ata(id)) {
1015 rc = -EINVAL;
1016 reason = "device reports illegal type";
1017 goto err_out;
1018 }
1019
1020 if (post_reset && class == ATA_DEV_ATA) {
1021 /*
1022 * The exact sequence expected by certain pre-ATA4 drives is:
1023 * SRST RESET
1024 * IDENTIFY
1025 * INITIALIZE DEVICE PARAMETERS
1026 * anything else..
1027 * Some drives were very specific about that exact sequence.
1028 */
1029 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1030 err_mask = ata_dev_init_params(ap, dev);
1031 if (err_mask) {
1032 rc = -EIO;
1033 reason = "INIT_DEV_PARAMS failed";
1034 goto err_out;
1035 }
1036
1037 /* current CHS translation info (id[53-58]) might be
1038 * changed. reread the identify device info.
1039 */
1040 post_reset = 0;
1041 goto retry;
1042 }
1043 }
1044
1045 *p_class = class;
Tejun Heod9572b12006-03-01 16:09:35 +09001046 *p_id = id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001047 return 0;
1048
1049 err_out:
1050 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1051 ap->id, dev->devno, reason);
Tejun Heod9572b12006-03-01 16:09:35 +09001052 kfree(id);
Tejun Heo49016ac2006-02-21 02:12:11 +09001053 return rc;
1054}
1055
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001056static inline u8 ata_dev_knobble(const struct ata_port *ap,
1057 struct ata_device *dev)
1058{
1059 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1060}
1061
Tejun Heo49016ac2006-02-21 02:12:11 +09001062/**
Tejun Heoffeae412006-03-01 16:09:35 +09001063 * ata_dev_configure - Configure the specified ATA/ATAPI device
1064 * @ap: Port on which target device resides
1065 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 *
Tejun Heoffeae412006-03-01 16:09:35 +09001067 * Configure @dev according to @dev->id. Generic and low-level
1068 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 *
1070 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001071 * Kernel thread context (may sleep)
1072 *
1073 * RETURNS:
1074 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 */
Tejun Heoffeae412006-03-01 16:09:35 +09001076static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 unsigned long xfer_modes;
Tejun Heo6e7846e2006-02-12 23:32:58 +09001079 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 if (!ata_dev_present(dev)) {
1082 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001083 ap->id, dev->devno);
1084 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086
Tejun Heoffeae412006-03-01 16:09:35 +09001087 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 /*
1090 * common ATA, ATAPI feature tests
1091 */
1092
Albert Lee8bf62ec2005-05-12 15:29:42 -04001093 /* we require DMA support (bits 8 of word 49) */
1094 if (!ata_id_has_dma(dev->id)) {
1095 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Tejun Heoffeae412006-03-01 16:09:35 +09001096 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 goto err_out_nosup;
1098 }
1099
1100 /* quick-n-dirty find max transfer mode; for printk only */
1101 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1102 if (!xfer_modes)
1103 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001104 if (!xfer_modes)
1105 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Tejun Heo0bd33002006-02-12 22:47:05 +09001107 ata_dump_id(dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 /* ATA-specific feature tests */
1110 if (dev->class == ATA_DEV_ATA) {
Tejun Heo29407402006-02-12 22:47:04 +09001111 dev->n_sectors = ata_id_n_sectors(dev->id);
1112
Albert Lee8bf62ec2005-05-12 15:29:42 -04001113 if (ata_id_has_lba(dev->id)) {
1114 dev->flags |= ATA_DFLAG_LBA;
1115
Tejun Heo29407402006-02-12 22:47:04 +09001116 if (ata_id_has_lba48(dev->id))
Albert Lee8bf62ec2005-05-12 15:29:42 -04001117 dev->flags |= ATA_DFLAG_LBA48;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001118
1119 /* print device info to dmesg */
1120 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001121 ap->id, dev->devno,
Tejun Heo49016ac2006-02-21 02:12:11 +09001122 ata_id_major_version(dev->id),
Albert Lee8bf62ec2005-05-12 15:29:42 -04001123 ata_mode_string(xfer_modes),
1124 (unsigned long long)dev->n_sectors,
1125 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
Tejun Heoffeae412006-03-01 16:09:35 +09001126 } else {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001127 /* CHS */
1128
1129 /* Default translation */
1130 dev->cylinders = dev->id[1];
1131 dev->heads = dev->id[3];
1132 dev->sectors = dev->id[6];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001133
1134 if (ata_id_current_chs_valid(dev->id)) {
1135 /* Current CHS translation is valid. */
1136 dev->cylinders = dev->id[54];
1137 dev->heads = dev->id[55];
1138 dev->sectors = dev->id[56];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001139 }
1140
1141 /* print device info to dmesg */
1142 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001143 ap->id, dev->devno,
Tejun Heo49016ac2006-02-21 02:12:11 +09001144 ata_id_major_version(dev->id),
Albert Lee8bf62ec2005-05-12 15:29:42 -04001145 ata_mode_string(xfer_modes),
1146 (unsigned long long)dev->n_sectors,
1147 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
1150
Tejun Heo6e7846e2006-02-12 23:32:58 +09001151 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
1153
1154 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001155 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 rc = atapi_cdb_len(dev->id);
1157 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1158 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
Tejun Heoffeae412006-03-01 16:09:35 +09001159 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 goto err_out_nosup;
1161 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001162 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 /* print device info to dmesg */
1165 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001166 ap->id, dev->devno,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 ata_mode_string(xfer_modes));
1168 }
1169
Tejun Heo6e7846e2006-02-12 23:32:58 +09001170 ap->host->max_cmd_len = 0;
1171 for (i = 0; i < ATA_MAX_DEVICES; i++)
1172 ap->host->max_cmd_len = max_t(unsigned int,
1173 ap->host->max_cmd_len,
1174 ap->device[i].cdb_len);
1175
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001176 /* limit bridge transfers to udma5, 200 sectors */
1177 if (ata_dev_knobble(ap, dev)) {
1178 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1179 ap->id, dev->devno);
1180 ap->udma_mask &= ATA_UDMA5;
1181 dev->max_sectors = ATA_MAX_SECTORS;
1182 }
1183
1184 if (ap->ops->dev_config)
1185 ap->ops->dev_config(ap, dev);
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09001188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190err_out_nosup:
1191 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001192 ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 DPRINTK("EXIT, err\n");
Tejun Heoffeae412006-03-01 16:09:35 +09001194 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
1197/**
1198 * ata_bus_probe - Reset and probe ATA bus
1199 * @ap: Bus to probe
1200 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001201 * Master ATA bus probing function. Initiates a hardware-dependent
1202 * bus reset, then attempts to identify any devices found on
1203 * the bus.
1204 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001206 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 *
1208 * RETURNS:
1209 * Zero on success, non-zero on error.
1210 */
1211
1212static int ata_bus_probe(struct ata_port *ap)
1213{
1214 unsigned int i, found = 0;
1215
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001216 if (ap->ops->probe_reset) {
1217 unsigned int classes[ATA_MAX_DEVICES];
1218 int rc;
1219
1220 ata_port_probe(ap);
1221
1222 rc = ap->ops->probe_reset(ap, classes);
1223 if (rc == 0) {
Tejun Heo06ab7822006-02-12 15:01:49 +09001224 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1225 if (classes[i] == ATA_DEV_UNKNOWN)
1226 classes[i] = ATA_DEV_NONE;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001227 ap->device[i].class = classes[i];
Tejun Heo06ab7822006-02-12 15:01:49 +09001228 }
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001229 } else {
1230 printk(KERN_ERR "ata%u: probe reset failed, "
1231 "disabling port\n", ap->id);
1232 ata_port_disable(ap);
1233 }
1234 } else
1235 ap->ops->phy_reset(ap);
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1238 goto err_out;
1239
1240 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoffeae412006-03-01 16:09:35 +09001241 struct ata_device *dev = &ap->device[i];
1242
1243 if (!ata_dev_present(dev))
1244 continue;
1245
1246 WARN_ON(dev->id != NULL);
1247 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1248 dev->class = ATA_DEV_NONE;
1249 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
Tejun Heoffeae412006-03-01 16:09:35 +09001251
1252 if (ata_dev_configure(ap, dev)) {
1253 dev->class++; /* disable device */
1254 continue;
1255 }
1256
Tejun Heoffeae412006-03-01 16:09:35 +09001257 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259
1260 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1261 goto err_out_disable;
1262
1263 ata_set_mode(ap);
1264 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1265 goto err_out_disable;
1266
1267 return 0;
1268
1269err_out_disable:
1270 ap->ops->port_disable(ap);
1271err_out:
1272 return -1;
1273}
1274
1275/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001276 * ata_port_probe - Mark port as enabled
1277 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001279 * Modify @ap data structure such that the system
1280 * thinks that the entire port is enabled.
1281 *
1282 * LOCKING: host_set lock, or some other form of
1283 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 */
1285
1286void ata_port_probe(struct ata_port *ap)
1287{
1288 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1289}
1290
1291/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001292 * sata_print_link_status - Print SATA link status
1293 * @ap: SATA port to printk link status about
1294 *
1295 * This function prints link speed and status of a SATA link.
1296 *
1297 * LOCKING:
1298 * None.
1299 */
1300static void sata_print_link_status(struct ata_port *ap)
1301{
1302 u32 sstatus, tmp;
1303 const char *speed;
1304
1305 if (!ap->ops->scr_read)
1306 return;
1307
1308 sstatus = scr_read(ap, SCR_STATUS);
1309
1310 if (sata_dev_present(ap)) {
1311 tmp = (sstatus >> 4) & 0xf;
1312 if (tmp & (1 << 0))
1313 speed = "1.5";
1314 else if (tmp & (1 << 1))
1315 speed = "3.0";
1316 else
1317 speed = "<unknown>";
1318 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1319 ap->id, speed, sstatus);
1320 } else {
1321 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1322 ap->id, sstatus);
1323 }
1324}
1325
1326/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001327 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1328 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001330 * This function issues commands to standard SATA Sxxx
1331 * PHY registers, to wake up the phy (and device), and
1332 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 *
1334 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001335 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 *
1337 */
1338void __sata_phy_reset(struct ata_port *ap)
1339{
1340 u32 sstatus;
1341 unsigned long timeout = jiffies + (HZ * 5);
1342
1343 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001344 /* issue phy wake/reset */
1345 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001346 /* Couldn't find anything in SATA I/II specs, but
1347 * AHCI-1.1 10.4.2 says at least 1 ms. */
1348 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
Brett Russcdcca892005-03-28 15:10:27 -05001350 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 /* wait for phy to become ready, if necessary */
1353 do {
1354 msleep(200);
1355 sstatus = scr_read(ap, SCR_STATUS);
1356 if ((sstatus & 0xf) != 1)
1357 break;
1358 } while (time_before(jiffies, timeout));
1359
Tejun Heo3be680b2005-12-19 22:35:02 +09001360 /* print link status */
1361 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001362
Tejun Heo3be680b2005-12-19 22:35:02 +09001363 /* TODO: phy layer with polling, timeouts, etc. */
1364 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001366 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1370 return;
1371
1372 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1373 ata_port_disable(ap);
1374 return;
1375 }
1376
1377 ap->cbl = ATA_CBL_SATA;
1378}
1379
1380/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001381 * sata_phy_reset - Reset SATA bus.
1382 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001384 * This function resets the SATA bus, and then probes
1385 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 *
1387 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001388 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 *
1390 */
1391void sata_phy_reset(struct ata_port *ap)
1392{
1393 __sata_phy_reset(ap);
1394 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1395 return;
1396 ata_bus_reset(ap);
1397}
1398
1399/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001400 * ata_port_disable - Disable port.
1401 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001403 * Modify @ap data structure such that the system
1404 * thinks that the entire port is disabled, and should
1405 * never attempt to probe or communicate with devices
1406 * on this port.
1407 *
1408 * LOCKING: host_set lock, or some other form of
1409 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 */
1411
1412void ata_port_disable(struct ata_port *ap)
1413{
1414 ap->device[0].class = ATA_DEV_NONE;
1415 ap->device[1].class = ATA_DEV_NONE;
1416 ap->flags |= ATA_FLAG_PORT_DISABLED;
1417}
1418
Alan Cox452503f2005-10-21 19:01:32 -04001419/*
1420 * This mode timing computation functionality is ported over from
1421 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1422 */
1423/*
1424 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1425 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1426 * for PIO 5, which is a nonstandard extension and UDMA6, which
1427 * is currently supported only by Maxtor drives.
1428 */
1429
1430static const struct ata_timing ata_timing[] = {
1431
1432 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1433 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1434 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1435 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1436
1437 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1438 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1439 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1440
1441/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1442
1443 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1444 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1445 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1446
1447 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1448 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1449 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1450
1451/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1452 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1453 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1454
1455 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1456 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1457 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1458
1459/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1460
1461 { 0xFF }
1462};
1463
1464#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1465#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1466
1467static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1468{
1469 q->setup = EZ(t->setup * 1000, T);
1470 q->act8b = EZ(t->act8b * 1000, T);
1471 q->rec8b = EZ(t->rec8b * 1000, T);
1472 q->cyc8b = EZ(t->cyc8b * 1000, T);
1473 q->active = EZ(t->active * 1000, T);
1474 q->recover = EZ(t->recover * 1000, T);
1475 q->cycle = EZ(t->cycle * 1000, T);
1476 q->udma = EZ(t->udma * 1000, UT);
1477}
1478
1479void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1480 struct ata_timing *m, unsigned int what)
1481{
1482 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1483 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1484 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1485 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1486 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1487 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1488 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1489 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1490}
1491
1492static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1493{
1494 const struct ata_timing *t;
1495
1496 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001497 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001498 return NULL;
1499 return t;
1500}
1501
1502int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1503 struct ata_timing *t, int T, int UT)
1504{
1505 const struct ata_timing *s;
1506 struct ata_timing p;
1507
1508 /*
1509 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001510 */
Alan Cox452503f2005-10-21 19:01:32 -04001511
1512 if (!(s = ata_timing_find_mode(speed)))
1513 return -EINVAL;
1514
Albert Lee75b1f2f2005-11-16 17:06:18 +08001515 memcpy(t, s, sizeof(*s));
1516
Alan Cox452503f2005-10-21 19:01:32 -04001517 /*
1518 * If the drive is an EIDE drive, it can tell us it needs extended
1519 * PIO/MW_DMA cycle timing.
1520 */
1521
1522 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1523 memset(&p, 0, sizeof(p));
1524 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1525 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1526 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1527 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1528 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1529 }
1530 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1531 }
1532
1533 /*
1534 * Convert the timing to bus clock counts.
1535 */
1536
Albert Lee75b1f2f2005-11-16 17:06:18 +08001537 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001538
1539 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001540 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1541 * S.M.A.R.T * and some other commands. We have to ensure that the
1542 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001543 */
1544
1545 if (speed > XFER_PIO_4) {
1546 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1547 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1548 }
1549
1550 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001551 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001552 */
1553
1554 if (t->act8b + t->rec8b < t->cyc8b) {
1555 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1556 t->rec8b = t->cyc8b - t->act8b;
1557 }
1558
1559 if (t->active + t->recover < t->cycle) {
1560 t->active += (t->cycle - (t->active + t->recover)) / 2;
1561 t->recover = t->cycle - t->active;
1562 }
1563
1564 return 0;
1565}
1566
Jeff Garzik057ace52005-10-22 14:27:05 -04001567static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 unsigned int shift;
1569 u8 base;
1570} xfer_mode_classes[] = {
1571 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1572 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1573 { ATA_SHIFT_PIO, XFER_PIO_0 },
1574};
1575
Arjan van de Ven858119e2006-01-14 13:20:43 -08001576static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
1578 int i;
1579
1580 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1581 if (xfer_mode_classes[i].shift == shift)
1582 return xfer_mode_classes[i].base;
1583
1584 return 0xff;
1585}
1586
1587static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1588{
1589 int ofs, idx;
1590 u8 base;
1591
1592 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1593 return;
1594
1595 if (dev->xfer_shift == ATA_SHIFT_PIO)
1596 dev->flags |= ATA_DFLAG_PIO;
1597
1598 ata_dev_set_xfermode(ap, dev);
1599
1600 base = base_from_shift(dev->xfer_shift);
1601 ofs = dev->xfer_mode - base;
1602 idx = ofs + dev->xfer_shift;
1603 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1604
1605 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1606 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1607
1608 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1609 ap->id, dev->devno, xfer_mode_str[idx]);
1610}
1611
1612static int ata_host_set_pio(struct ata_port *ap)
1613{
1614 unsigned int mask;
1615 int x, i;
1616 u8 base, xfer_mode;
1617
1618 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1619 x = fgb(mask);
1620 if (x < 0) {
1621 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1622 return -1;
1623 }
1624
1625 base = base_from_shift(ATA_SHIFT_PIO);
1626 xfer_mode = base + x;
1627
1628 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1629 (int)base, (int)xfer_mode, mask, x);
1630
1631 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1632 struct ata_device *dev = &ap->device[i];
1633 if (ata_dev_present(dev)) {
1634 dev->pio_mode = xfer_mode;
1635 dev->xfer_mode = xfer_mode;
1636 dev->xfer_shift = ATA_SHIFT_PIO;
1637 if (ap->ops->set_piomode)
1638 ap->ops->set_piomode(ap, dev);
1639 }
1640 }
1641
1642 return 0;
1643}
1644
1645static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1646 unsigned int xfer_shift)
1647{
1648 int i;
1649
1650 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1651 struct ata_device *dev = &ap->device[i];
1652 if (ata_dev_present(dev)) {
1653 dev->dma_mode = xfer_mode;
1654 dev->xfer_mode = xfer_mode;
1655 dev->xfer_shift = xfer_shift;
1656 if (ap->ops->set_dmamode)
1657 ap->ops->set_dmamode(ap, dev);
1658 }
1659 }
1660}
1661
1662/**
1663 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1664 * @ap: port on which timings will be programmed
1665 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001666 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1667 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001669 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 */
1671static void ata_set_mode(struct ata_port *ap)
1672{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001673 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 u8 xfer_mode;
1675 int rc;
1676
1677 /* step 1: always set host PIO timings */
1678 rc = ata_host_set_pio(ap);
1679 if (rc)
1680 goto err_out;
1681
1682 /* step 2: choose the best data xfer mode */
1683 xfer_mode = xfer_shift = 0;
1684 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1685 if (rc)
1686 goto err_out;
1687
1688 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1689 if (xfer_shift != ATA_SHIFT_PIO)
1690 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1691
1692 /* step 4: update devices' xfer mode */
1693 ata_dev_set_mode(ap, &ap->device[0]);
1694 ata_dev_set_mode(ap, &ap->device[1]);
1695
1696 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1697 return;
1698
1699 if (ap->ops->post_set_mode)
1700 ap->ops->post_set_mode(ap);
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return;
1703
1704err_out:
1705 ata_port_disable(ap);
1706}
1707
1708/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001709 * ata_tf_to_host - issue ATA taskfile to host controller
1710 * @ap: port to which command is being issued
1711 * @tf: ATA taskfile register set
1712 *
1713 * Issues ATA taskfile register set to ATA host controller,
1714 * with proper synchronization with interrupt handler and
1715 * other threads.
1716 *
1717 * LOCKING:
1718 * spin_lock_irqsave(host_set lock)
1719 */
1720
1721static inline void ata_tf_to_host(struct ata_port *ap,
1722 const struct ata_taskfile *tf)
1723{
1724 ap->ops->tf_load(ap, tf);
1725 ap->ops->exec_command(ap, tf);
1726}
1727
1728/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 * ata_busy_sleep - sleep until BSY clears, or timeout
1730 * @ap: port containing status register to be polled
1731 * @tmout_pat: impatience timeout
1732 * @tmout: overall timeout
1733 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001734 * Sleep until ATA Status register bit BSY clears,
1735 * or a timeout occurs.
1736 *
1737 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 */
1739
Tejun Heo6f8b9952006-01-24 17:05:21 +09001740unsigned int ata_busy_sleep (struct ata_port *ap,
1741 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
1743 unsigned long timer_start, timeout;
1744 u8 status;
1745
1746 status = ata_busy_wait(ap, ATA_BUSY, 300);
1747 timer_start = jiffies;
1748 timeout = timer_start + tmout_pat;
1749 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1750 msleep(50);
1751 status = ata_busy_wait(ap, ATA_BUSY, 3);
1752 }
1753
1754 if (status & ATA_BUSY)
1755 printk(KERN_WARNING "ata%u is slow to respond, "
1756 "please be patient\n", ap->id);
1757
1758 timeout = timer_start + tmout;
1759 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1760 msleep(50);
1761 status = ata_chk_status(ap);
1762 }
1763
1764 if (status & ATA_BUSY) {
1765 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1766 ap->id, tmout / HZ);
1767 return 1;
1768 }
1769
1770 return 0;
1771}
1772
1773static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1774{
1775 struct ata_ioports *ioaddr = &ap->ioaddr;
1776 unsigned int dev0 = devmask & (1 << 0);
1777 unsigned int dev1 = devmask & (1 << 1);
1778 unsigned long timeout;
1779
1780 /* if device 0 was found in ata_devchk, wait for its
1781 * BSY bit to clear
1782 */
1783 if (dev0)
1784 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1785
1786 /* if device 1 was found in ata_devchk, wait for
1787 * register access, then wait for BSY to clear
1788 */
1789 timeout = jiffies + ATA_TMOUT_BOOT;
1790 while (dev1) {
1791 u8 nsect, lbal;
1792
1793 ap->ops->dev_select(ap, 1);
1794 if (ap->flags & ATA_FLAG_MMIO) {
1795 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1796 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1797 } else {
1798 nsect = inb(ioaddr->nsect_addr);
1799 lbal = inb(ioaddr->lbal_addr);
1800 }
1801 if ((nsect == 1) && (lbal == 1))
1802 break;
1803 if (time_after(jiffies, timeout)) {
1804 dev1 = 0;
1805 break;
1806 }
1807 msleep(50); /* give drive a breather */
1808 }
1809 if (dev1)
1810 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1811
1812 /* is all this really necessary? */
1813 ap->ops->dev_select(ap, 0);
1814 if (dev1)
1815 ap->ops->dev_select(ap, 1);
1816 if (dev0)
1817 ap->ops->dev_select(ap, 0);
1818}
1819
1820/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001821 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1822 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001824 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1825 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 *
1827 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001828 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001829 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 *
1831 */
1832
1833static unsigned int ata_bus_edd(struct ata_port *ap)
1834{
1835 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001836 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 /* set up execute-device-diag (bus reset) taskfile */
1839 /* also, take interrupts to a known state (disabled) */
1840 DPRINTK("execute-device-diag\n");
1841 ata_tf_init(ap, &tf, 0);
1842 tf.ctl |= ATA_NIEN;
1843 tf.command = ATA_CMD_EDD;
1844 tf.protocol = ATA_PROT_NODATA;
1845
1846 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05001847 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05001849 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 /* spec says at least 2ms. but who knows with those
1852 * crazy ATAPI devices...
1853 */
1854 msleep(150);
1855
1856 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1857}
1858
1859static unsigned int ata_bus_softreset(struct ata_port *ap,
1860 unsigned int devmask)
1861{
1862 struct ata_ioports *ioaddr = &ap->ioaddr;
1863
1864 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1865
1866 /* software reset. causes dev0 to be selected */
1867 if (ap->flags & ATA_FLAG_MMIO) {
1868 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1869 udelay(20); /* FIXME: flush */
1870 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1871 udelay(20); /* FIXME: flush */
1872 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1873 } else {
1874 outb(ap->ctl, ioaddr->ctl_addr);
1875 udelay(10);
1876 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1877 udelay(10);
1878 outb(ap->ctl, ioaddr->ctl_addr);
1879 }
1880
1881 /* spec mandates ">= 2ms" before checking status.
1882 * We wait 150ms, because that was the magic delay used for
1883 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1884 * between when the ATA command register is written, and then
1885 * status is checked. Because waiting for "a while" before
1886 * checking status is fine, post SRST, we perform this magic
1887 * delay here as well.
1888 */
1889 msleep(150);
1890
1891 ata_bus_post_reset(ap, devmask);
1892
1893 return 0;
1894}
1895
1896/**
1897 * ata_bus_reset - reset host port and associated ATA channel
1898 * @ap: port to reset
1899 *
1900 * This is typically the first time we actually start issuing
1901 * commands to the ATA channel. We wait for BSY to clear, then
1902 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1903 * result. Determine what devices, if any, are on the channel
1904 * by looking at the device 0/1 error register. Look at the signature
1905 * stored in each device's taskfile registers, to determine if
1906 * the device is ATA or ATAPI.
1907 *
1908 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001909 * PCI/etc. bus probe sem.
1910 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 *
1912 * SIDE EFFECTS:
1913 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1914 */
1915
1916void ata_bus_reset(struct ata_port *ap)
1917{
1918 struct ata_ioports *ioaddr = &ap->ioaddr;
1919 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1920 u8 err;
1921 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1922
1923 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1924
1925 /* determine if device 0/1 are present */
1926 if (ap->flags & ATA_FLAG_SATA_RESET)
1927 dev0 = 1;
1928 else {
1929 dev0 = ata_devchk(ap, 0);
1930 if (slave_possible)
1931 dev1 = ata_devchk(ap, 1);
1932 }
1933
1934 if (dev0)
1935 devmask |= (1 << 0);
1936 if (dev1)
1937 devmask |= (1 << 1);
1938
1939 /* select device 0 again */
1940 ap->ops->dev_select(ap, 0);
1941
1942 /* issue bus reset */
1943 if (ap->flags & ATA_FLAG_SRST)
1944 rc = ata_bus_softreset(ap, devmask);
1945 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1946 /* set up device control */
1947 if (ap->flags & ATA_FLAG_MMIO)
1948 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1949 else
1950 outb(ap->ctl, ioaddr->ctl_addr);
1951 rc = ata_bus_edd(ap);
1952 }
1953
1954 if (rc)
1955 goto err_out;
1956
1957 /*
1958 * determine by signature whether we have ATA or ATAPI devices
1959 */
Tejun Heob4dc7622006-01-24 17:05:22 +09001960 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09001962 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 /* re-enable interrupts */
1965 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1966 ata_irq_on(ap);
1967
1968 /* is double-select really necessary? */
1969 if (ap->device[1].class != ATA_DEV_NONE)
1970 ap->ops->dev_select(ap, 1);
1971 if (ap->device[0].class != ATA_DEV_NONE)
1972 ap->ops->dev_select(ap, 0);
1973
1974 /* if no devices were detected, disable this port */
1975 if ((ap->device[0].class == ATA_DEV_NONE) &&
1976 (ap->device[1].class == ATA_DEV_NONE))
1977 goto err_out;
1978
1979 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1980 /* set up device control for ATA_FLAG_SATA_RESET */
1981 if (ap->flags & ATA_FLAG_MMIO)
1982 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1983 else
1984 outb(ap->ctl, ioaddr->ctl_addr);
1985 }
1986
1987 DPRINTK("EXIT\n");
1988 return;
1989
1990err_out:
1991 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1992 ap->ops->port_disable(ap);
1993
1994 DPRINTK("EXIT\n");
1995}
1996
Tejun Heo7a7921e2006-02-02 18:20:00 +09001997static int sata_phy_resume(struct ata_port *ap)
1998{
1999 unsigned long timeout = jiffies + (HZ * 5);
2000 u32 sstatus;
2001
2002 scr_write_flush(ap, SCR_CONTROL, 0x300);
2003
2004 /* Wait for phy to become ready, if necessary. */
2005 do {
2006 msleep(200);
2007 sstatus = scr_read(ap, SCR_STATUS);
2008 if ((sstatus & 0xf) != 1)
2009 return 0;
2010 } while (time_before(jiffies, timeout));
2011
2012 return -1;
2013}
2014
Tejun Heoc2bd5802006-01-24 17:05:22 +09002015/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09002016 * ata_std_probeinit - initialize probing
2017 * @ap: port to be probed
2018 *
2019 * @ap is about to be probed. Initialize it. This function is
2020 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09002021 *
2022 * NOTE!!! Do not use this function as probeinit if a low level
2023 * driver implements only hardreset. Just pass NULL as probeinit
2024 * in that case. Using this function is probably okay but doing
2025 * so makes reset sequence different from the original
2026 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09002027 */
2028extern void ata_std_probeinit(struct ata_port *ap)
2029{
Tejun Heo3a397462006-02-10 23:58:48 +09002030 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
Tejun Heo8a19ac82006-02-02 18:20:00 +09002031 sata_phy_resume(ap);
Tejun Heo3a397462006-02-10 23:58:48 +09002032 if (sata_dev_present(ap))
2033 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2034 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09002035}
2036
2037/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09002038 * ata_std_softreset - reset host port via ATA SRST
2039 * @ap: port to reset
2040 * @verbose: fail verbosely
2041 * @classes: resulting classes of attached devices
2042 *
2043 * Reset host port using ATA SRST. This function is to be used
2044 * as standard callback for ata_drive_*_reset() functions.
2045 *
2046 * LOCKING:
2047 * Kernel thread context (may sleep)
2048 *
2049 * RETURNS:
2050 * 0 on success, -errno otherwise.
2051 */
2052int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2053{
2054 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2055 unsigned int devmask = 0, err_mask;
2056 u8 err;
2057
2058 DPRINTK("ENTER\n");
2059
Tejun Heo3a397462006-02-10 23:58:48 +09002060 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2061 classes[0] = ATA_DEV_NONE;
2062 goto out;
2063 }
2064
Tejun Heoc2bd5802006-01-24 17:05:22 +09002065 /* determine if device 0/1 are present */
2066 if (ata_devchk(ap, 0))
2067 devmask |= (1 << 0);
2068 if (slave_possible && ata_devchk(ap, 1))
2069 devmask |= (1 << 1);
2070
Tejun Heoc2bd5802006-01-24 17:05:22 +09002071 /* select device 0 again */
2072 ap->ops->dev_select(ap, 0);
2073
2074 /* issue bus reset */
2075 DPRINTK("about to softreset, devmask=%x\n", devmask);
2076 err_mask = ata_bus_softreset(ap, devmask);
2077 if (err_mask) {
2078 if (verbose)
2079 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2080 ap->id, err_mask);
2081 else
2082 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2083 err_mask);
2084 return -EIO;
2085 }
2086
2087 /* determine by signature whether we have ATA or ATAPI devices */
2088 classes[0] = ata_dev_try_classify(ap, 0, &err);
2089 if (slave_possible && err != 0x81)
2090 classes[1] = ata_dev_try_classify(ap, 1, &err);
2091
Tejun Heo3a397462006-02-10 23:58:48 +09002092 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002093 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2094 return 0;
2095}
2096
2097/**
2098 * sata_std_hardreset - reset host port via SATA phy reset
2099 * @ap: port to reset
2100 * @verbose: fail verbosely
2101 * @class: resulting class of attached device
2102 *
2103 * SATA phy-reset host port using DET bits of SControl register.
2104 * This function is to be used as standard callback for
2105 * ata_drive_*_reset().
2106 *
2107 * LOCKING:
2108 * Kernel thread context (may sleep)
2109 *
2110 * RETURNS:
2111 * 0 on success, -errno otherwise.
2112 */
2113int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2114{
Tejun Heoc2bd5802006-01-24 17:05:22 +09002115 DPRINTK("ENTER\n");
2116
2117 /* Issue phy wake/reset */
2118 scr_write_flush(ap, SCR_CONTROL, 0x301);
2119
2120 /*
2121 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2122 * 10.4.2 says at least 1 ms.
2123 */
2124 msleep(1);
2125
Tejun Heo7a7921e2006-02-02 18:20:00 +09002126 /* Bring phy back */
2127 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002128
Tejun Heoc2bd5802006-01-24 17:05:22 +09002129 /* TODO: phy layer with polling, timeouts, etc. */
2130 if (!sata_dev_present(ap)) {
2131 *class = ATA_DEV_NONE;
2132 DPRINTK("EXIT, link offline\n");
2133 return 0;
2134 }
2135
2136 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2137 if (verbose)
2138 printk(KERN_ERR "ata%u: COMRESET failed "
2139 "(device not ready)\n", ap->id);
2140 else
2141 DPRINTK("EXIT, device not ready\n");
2142 return -EIO;
2143 }
2144
Tejun Heo3a397462006-02-10 23:58:48 +09002145 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2146
Tejun Heoc2bd5802006-01-24 17:05:22 +09002147 *class = ata_dev_try_classify(ap, 0, NULL);
2148
2149 DPRINTK("EXIT, class=%u\n", *class);
2150 return 0;
2151}
2152
2153/**
2154 * ata_std_postreset - standard postreset callback
2155 * @ap: the target ata_port
2156 * @classes: classes of attached devices
2157 *
2158 * This function is invoked after a successful reset. Note that
2159 * the device might have been reset more than once using
2160 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002161 *
2162 * This function is to be used as standard callback for
2163 * ata_drive_*_reset().
2164 *
2165 * LOCKING:
2166 * Kernel thread context (may sleep)
2167 */
2168void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2169{
2170 DPRINTK("ENTER\n");
2171
Tejun Heo56497bd2006-02-15 15:01:42 +09002172 /* set cable type if it isn't already set */
Tejun Heoc2bd5802006-01-24 17:05:22 +09002173 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2174 ap->cbl = ATA_CBL_SATA;
2175
2176 /* print link status */
2177 if (ap->cbl == ATA_CBL_SATA)
2178 sata_print_link_status(ap);
2179
Tejun Heo3a397462006-02-10 23:58:48 +09002180 /* re-enable interrupts */
2181 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2182 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002183
2184 /* is double-select really necessary? */
2185 if (classes[0] != ATA_DEV_NONE)
2186 ap->ops->dev_select(ap, 1);
2187 if (classes[1] != ATA_DEV_NONE)
2188 ap->ops->dev_select(ap, 0);
2189
Tejun Heo3a397462006-02-10 23:58:48 +09002190 /* bail out if no device is present */
2191 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2192 DPRINTK("EXIT, no device\n");
2193 return;
2194 }
2195
2196 /* set up device control */
2197 if (ap->ioaddr.ctl_addr) {
2198 if (ap->flags & ATA_FLAG_MMIO)
2199 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2200 else
2201 outb(ap->ctl, ap->ioaddr.ctl_addr);
2202 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002203
2204 DPRINTK("EXIT\n");
2205}
2206
2207/**
2208 * ata_std_probe_reset - standard probe reset method
2209 * @ap: prot to perform probe-reset
2210 * @classes: resulting classes of attached devices
2211 *
2212 * The stock off-the-shelf ->probe_reset method.
2213 *
2214 * LOCKING:
2215 * Kernel thread context (may sleep)
2216 *
2217 * RETURNS:
2218 * 0 on success, -errno otherwise.
2219 */
2220int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2221{
2222 ata_reset_fn_t hardreset;
2223
2224 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002225 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002226 hardreset = sata_std_hardreset;
2227
Tejun Heo8a19ac82006-02-02 18:20:00 +09002228 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002229 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002230 ata_std_postreset, classes);
2231}
2232
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002233static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2234 ata_postreset_fn_t postreset,
2235 unsigned int *classes)
2236{
2237 int i, rc;
2238
2239 for (i = 0; i < ATA_MAX_DEVICES; i++)
2240 classes[i] = ATA_DEV_UNKNOWN;
2241
2242 rc = reset(ap, 0, classes);
2243 if (rc)
2244 return rc;
2245
2246 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2247 * is complete and convert all ATA_DEV_UNKNOWN to
2248 * ATA_DEV_NONE.
2249 */
2250 for (i = 0; i < ATA_MAX_DEVICES; i++)
2251 if (classes[i] != ATA_DEV_UNKNOWN)
2252 break;
2253
2254 if (i < ATA_MAX_DEVICES)
2255 for (i = 0; i < ATA_MAX_DEVICES; i++)
2256 if (classes[i] == ATA_DEV_UNKNOWN)
2257 classes[i] = ATA_DEV_NONE;
2258
2259 if (postreset)
2260 postreset(ap, classes);
2261
2262 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2263}
2264
2265/**
2266 * ata_drive_probe_reset - Perform probe reset with given methods
2267 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002268 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002269 * @softreset: softreset method (can be NULL)
2270 * @hardreset: hardreset method (can be NULL)
2271 * @postreset: postreset method (can be NULL)
2272 * @classes: resulting classes of attached devices
2273 *
2274 * Reset the specified port and classify attached devices using
2275 * given methods. This function prefers softreset but tries all
2276 * possible reset sequences to reset and classify devices. This
2277 * function is intended to be used for constructing ->probe_reset
2278 * callback by low level drivers.
2279 *
2280 * Reset methods should follow the following rules.
2281 *
2282 * - Return 0 on sucess, -errno on failure.
2283 * - If classification is supported, fill classes[] with
2284 * recognized class codes.
2285 * - If classification is not supported, leave classes[] alone.
2286 * - If verbose is non-zero, print error message on failure;
2287 * otherwise, shut up.
2288 *
2289 * LOCKING:
2290 * Kernel thread context (may sleep)
2291 *
2292 * RETURNS:
2293 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2294 * if classification fails, and any error code from reset
2295 * methods.
2296 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002297int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002298 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2299 ata_postreset_fn_t postreset, unsigned int *classes)
2300{
2301 int rc = -EINVAL;
2302
Tejun Heo7944ea92006-02-02 18:20:00 +09002303 if (probeinit)
2304 probeinit(ap);
2305
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002306 if (softreset) {
2307 rc = do_probe_reset(ap, softreset, postreset, classes);
2308 if (rc == 0)
2309 return 0;
2310 }
2311
2312 if (!hardreset)
2313 return rc;
2314
2315 rc = do_probe_reset(ap, hardreset, postreset, classes);
2316 if (rc == 0 || rc != -ENODEV)
2317 return rc;
2318
2319 if (softreset)
2320 rc = do_probe_reset(ap, softreset, postreset, classes);
2321
2322 return rc;
2323}
2324
Jeff Garzik057ace52005-10-22 14:27:05 -04002325static void ata_pr_blacklisted(const struct ata_port *ap,
2326 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
2328 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2329 ap->id, dev->devno);
2330}
2331
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002332static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 "WDC AC11000H",
2334 "WDC AC22100H",
2335 "WDC AC32500H",
2336 "WDC AC33100H",
2337 "WDC AC31600H",
2338 "WDC AC32100H",
2339 "WDC AC23200L",
2340 "Compaq CRD-8241B",
2341 "CRD-8400B",
2342 "CRD-8480B",
2343 "CRD-8482B",
2344 "CRD-84",
2345 "SanDisk SDP3B",
2346 "SanDisk SDP3B-64",
2347 "SANYO CD-ROM CRD",
2348 "HITACHI CDR-8",
2349 "HITACHI CDR-8335",
2350 "HITACHI CDR-8435",
2351 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002352 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 "CD-532E-A",
2354 "E-IDE CD-ROM CR-840",
2355 "CD-ROM Drive/F5A",
2356 "WPI CDD-820",
2357 "SAMSUNG CD-ROM SC-148C",
2358 "SAMSUNG CD-ROM SC",
2359 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2361 "_NEC DV5800A",
2362};
2363
Jeff Garzik057ace52005-10-22 14:27:05 -04002364static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
Tejun Heo2e026712006-02-12 22:47:04 +09002366 unsigned char model_num[41];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 int i;
2368
Tejun Heo6a62a042006-02-13 10:02:46 +09002369 ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
Tejun Heo2e026712006-02-12 22:47:04 +09002372 if (!strcmp(ata_dma_blacklist[i], model_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 return 1;
2374
2375 return 0;
2376}
2377
Jeff Garzik057ace52005-10-22 14:27:05 -04002378static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
Jeff Garzik057ace52005-10-22 14:27:05 -04002380 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 unsigned int mask;
2382
2383 master = &ap->device[0];
2384 slave = &ap->device[1];
2385
Tejun Heoa4631472006-02-11 19:11:13 +09002386 WARN_ON(!ata_dev_present(master) && !ata_dev_present(slave));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
2388 if (shift == ATA_SHIFT_UDMA) {
2389 mask = ap->udma_mask;
2390 if (ata_dev_present(master)) {
2391 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002392 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 mask = 0;
2394 ata_pr_blacklisted(ap, master);
2395 }
2396 }
2397 if (ata_dev_present(slave)) {
2398 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002399 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 mask = 0;
2401 ata_pr_blacklisted(ap, slave);
2402 }
2403 }
2404 }
2405 else if (shift == ATA_SHIFT_MWDMA) {
2406 mask = ap->mwdma_mask;
2407 if (ata_dev_present(master)) {
2408 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002409 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 mask = 0;
2411 ata_pr_blacklisted(ap, master);
2412 }
2413 }
2414 if (ata_dev_present(slave)) {
2415 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002416 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 mask = 0;
2418 ata_pr_blacklisted(ap, slave);
2419 }
2420 }
2421 }
2422 else if (shift == ATA_SHIFT_PIO) {
2423 mask = ap->pio_mask;
2424 if (ata_dev_present(master)) {
2425 /* spec doesn't return explicit support for
2426 * PIO0-2, so we fake it
2427 */
2428 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2429 tmp_mode <<= 3;
2430 tmp_mode |= 0x7;
2431 mask &= tmp_mode;
2432 }
2433 if (ata_dev_present(slave)) {
2434 /* spec doesn't return explicit support for
2435 * PIO0-2, so we fake it
2436 */
2437 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2438 tmp_mode <<= 3;
2439 tmp_mode |= 0x7;
2440 mask &= tmp_mode;
2441 }
2442 }
2443 else {
2444 mask = 0xffffffff; /* shut up compiler warning */
2445 BUG();
2446 }
2447
2448 return mask;
2449}
2450
2451/* find greatest bit */
2452static int fgb(u32 bitmap)
2453{
2454 unsigned int i;
2455 int x = -1;
2456
2457 for (i = 0; i < 32; i++)
2458 if (bitmap & (1 << i))
2459 x = i;
2460
2461 return x;
2462}
2463
2464/**
2465 * ata_choose_xfer_mode - attempt to find best transfer mode
2466 * @ap: Port for which an xfer mode will be selected
2467 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2468 * @xfer_shift_out: (output) bit shift that selects this mode
2469 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002470 * Based on host and device capabilities, determine the
2471 * maximum transfer mode that is amenable to all.
2472 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002474 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 *
2476 * RETURNS:
2477 * Zero on success, negative on error.
2478 */
2479
Jeff Garzik057ace52005-10-22 14:27:05 -04002480static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 u8 *xfer_mode_out,
2482 unsigned int *xfer_shift_out)
2483{
2484 unsigned int mask, shift;
2485 int x, i;
2486
2487 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2488 shift = xfer_mode_classes[i].shift;
2489 mask = ata_get_mode_mask(ap, shift);
2490
2491 x = fgb(mask);
2492 if (x >= 0) {
2493 *xfer_mode_out = xfer_mode_classes[i].base + x;
2494 *xfer_shift_out = shift;
2495 return 0;
2496 }
2497 }
2498
2499 return -1;
2500}
2501
2502/**
2503 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2504 * @ap: Port associated with device @dev
2505 * @dev: Device to which command will be sent
2506 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002507 * Issue SET FEATURES - XFER MODE command to device @dev
2508 * on port @ap.
2509 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002511 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 */
2513
2514static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2515{
Tejun Heoa0123702005-12-13 14:49:31 +09002516 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 /* set up set-features taskfile */
2519 DPRINTK("set features - xfer mode\n");
2520
Tejun Heoa0123702005-12-13 14:49:31 +09002521 ata_tf_init(ap, &tf, dev->devno);
2522 tf.command = ATA_CMD_SET_FEATURES;
2523 tf.feature = SETFEATURES_XFER;
2524 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2525 tf.protocol = ATA_PROT_NODATA;
2526 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Tejun Heoa0123702005-12-13 14:49:31 +09002528 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2529 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2530 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534 DPRINTK("EXIT\n");
2535}
2536
2537/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002538 * ata_dev_init_params - Issue INIT DEV PARAMS command
2539 * @ap: Port associated with device @dev
2540 * @dev: Device to which command will be sent
2541 *
2542 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09002543 * Kernel thread context (may sleep)
2544 *
2545 * RETURNS:
2546 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ec2005-05-12 15:29:42 -04002547 */
2548
Tejun Heo6aff8f12006-02-15 18:24:09 +09002549static unsigned int ata_dev_init_params(struct ata_port *ap,
2550 struct ata_device *dev)
Albert Lee8bf62ec2005-05-12 15:29:42 -04002551{
Tejun Heoa0123702005-12-13 14:49:31 +09002552 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09002553 unsigned int err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002554 u16 sectors = dev->id[6];
2555 u16 heads = dev->id[3];
2556
2557 /* Number of sectors per track 1-255. Number of heads 1-16 */
2558 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Tejun Heo6aff8f12006-02-15 18:24:09 +09002559 return 0;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002560
2561 /* set up init dev params taskfile */
2562 DPRINTK("init dev params \n");
2563
Tejun Heoa0123702005-12-13 14:49:31 +09002564 ata_tf_init(ap, &tf, dev->devno);
2565 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2566 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2567 tf.protocol = ATA_PROT_NODATA;
2568 tf.nsect = sectors;
2569 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002570
Tejun Heo6aff8f12006-02-15 18:24:09 +09002571 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002572
Tejun Heo6aff8f12006-02-15 18:24:09 +09002573 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2574 return err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002575}
2576
2577/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002578 * ata_sg_clean - Unmap DMA memory associated with command
2579 * @qc: Command containing DMA memory to be released
2580 *
2581 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 *
2583 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002584 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 */
2586
2587static void ata_sg_clean(struct ata_queued_cmd *qc)
2588{
2589 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002590 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002592 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Tejun Heoa4631472006-02-11 19:11:13 +09002594 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2595 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
2597 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05002598 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002600 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002602 /* if we padded the buffer out to 32-bit bound, and data
2603 * xfer direction is from-device, we must copy from the
2604 * pad buffer back into the supplied buffer
2605 */
2606 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2607 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2608
2609 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002610 if (qc->n_elem)
2611 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002612 /* restore last sg */
2613 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2614 if (pad_buf) {
2615 struct scatterlist *psg = &qc->pad_sgent;
2616 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2617 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002618 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002619 }
2620 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09002621 if (qc->n_elem)
Jeff Garzike1410f22005-11-14 14:06:26 -05002622 dma_unmap_single(ap->host_set->dev,
2623 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2624 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002625 /* restore sg */
2626 sg->length += qc->pad_len;
2627 if (pad_buf)
2628 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2629 pad_buf, qc->pad_len);
2630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
2632 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002633 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634}
2635
2636/**
2637 * ata_fill_sg - Fill PCI IDE PRD table
2638 * @qc: Metadata associated with taskfile to be transferred
2639 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002640 * Fill PCI IDE PRD (scatter-gather) table with segments
2641 * associated with the current disk command.
2642 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002644 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 *
2646 */
2647static void ata_fill_sg(struct ata_queued_cmd *qc)
2648{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002650 struct scatterlist *sg;
2651 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Tejun Heoa4631472006-02-11 19:11:13 +09002653 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05002654 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
2656 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002657 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 u32 addr, offset;
2659 u32 sg_len, len;
2660
2661 /* determine if physical DMA addr spans 64K boundary.
2662 * Note h/w doesn't support 64-bit, so we unconditionally
2663 * truncate dma_addr_t to u32.
2664 */
2665 addr = (u32) sg_dma_address(sg);
2666 sg_len = sg_dma_len(sg);
2667
2668 while (sg_len) {
2669 offset = addr & 0xffff;
2670 len = sg_len;
2671 if ((offset + sg_len) > 0x10000)
2672 len = 0x10000 - offset;
2673
2674 ap->prd[idx].addr = cpu_to_le32(addr);
2675 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2676 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2677
2678 idx++;
2679 sg_len -= len;
2680 addr += len;
2681 }
2682 }
2683
2684 if (idx)
2685 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2686}
2687/**
2688 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2689 * @qc: Metadata associated with taskfile to check
2690 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002691 * Allow low-level driver to filter ATA PACKET commands, returning
2692 * a status indicating whether or not it is OK to use DMA for the
2693 * supplied PACKET command.
2694 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002696 * spin_lock_irqsave(host_set lock)
2697 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 * RETURNS: 0 when ATAPI DMA can be used
2699 * nonzero otherwise
2700 */
2701int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2702{
2703 struct ata_port *ap = qc->ap;
2704 int rc = 0; /* Assume ATAPI DMA is OK by default */
2705
2706 if (ap->ops->check_atapi_dma)
2707 rc = ap->ops->check_atapi_dma(qc);
2708
2709 return rc;
2710}
2711/**
2712 * ata_qc_prep - Prepare taskfile for submission
2713 * @qc: Metadata associated with taskfile to be prepared
2714 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002715 * Prepare ATA taskfile for submission.
2716 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 * LOCKING:
2718 * spin_lock_irqsave(host_set lock)
2719 */
2720void ata_qc_prep(struct ata_queued_cmd *qc)
2721{
2722 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2723 return;
2724
2725 ata_fill_sg(qc);
2726}
2727
Jeff Garzik0cba6322005-05-30 19:49:12 -04002728/**
2729 * ata_sg_init_one - Associate command with memory buffer
2730 * @qc: Command to be associated
2731 * @buf: Memory buffer
2732 * @buflen: Length of memory buffer, in bytes.
2733 *
2734 * Initialize the data-related elements of queued_cmd @qc
2735 * to point to a single memory buffer, @buf of byte length @buflen.
2736 *
2737 * LOCKING:
2738 * spin_lock_irqsave(host_set lock)
2739 */
2740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2742{
2743 struct scatterlist *sg;
2744
2745 qc->flags |= ATA_QCFLAG_SINGLE;
2746
2747 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002748 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002750 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 qc->buf_virt = buf;
2752
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002753 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002754 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755}
2756
Jeff Garzik0cba6322005-05-30 19:49:12 -04002757/**
2758 * ata_sg_init - Associate command with scatter-gather table.
2759 * @qc: Command to be associated
2760 * @sg: Scatter-gather table.
2761 * @n_elem: Number of elements in s/g table.
2762 *
2763 * Initialize the data-related elements of queued_cmd @qc
2764 * to point to a scatter-gather table @sg, containing @n_elem
2765 * elements.
2766 *
2767 * LOCKING:
2768 * spin_lock_irqsave(host_set lock)
2769 */
2770
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2772 unsigned int n_elem)
2773{
2774 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002775 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002777 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778}
2779
2780/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002781 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2782 * @qc: Command with memory buffer to be mapped.
2783 *
2784 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 *
2786 * LOCKING:
2787 * spin_lock_irqsave(host_set lock)
2788 *
2789 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002790 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 */
2792
2793static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2794{
2795 struct ata_port *ap = qc->ap;
2796 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002797 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09002799 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002801 /* we must lengthen transfers to end on a 32-bit boundary */
2802 qc->pad_len = sg->length & 3;
2803 if (qc->pad_len) {
2804 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2805 struct scatterlist *psg = &qc->pad_sgent;
2806
Tejun Heoa4631472006-02-11 19:11:13 +09002807 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002808
2809 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2810
2811 if (qc->tf.flags & ATA_TFLAG_WRITE)
2812 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2813 qc->pad_len);
2814
2815 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2816 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2817 /* trim sg */
2818 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09002819 if (sg->length == 0)
2820 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002821
2822 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2823 sg->length, qc->pad_len);
2824 }
2825
Tejun Heo2e242fa2006-02-20 23:48:38 +09002826 if (trim_sg) {
2827 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05002828 goto skip_map;
2829 }
2830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002832 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002833 if (dma_mapping_error(dma_address)) {
2834 /* restore sg */
2835 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
2839 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04002840 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Tejun Heo2e242fa2006-02-20 23:48:38 +09002842skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2844 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2845
2846 return 0;
2847}
2848
2849/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002850 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2851 * @qc: Command with scatter-gather table to be mapped.
2852 *
2853 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 *
2855 * LOCKING:
2856 * spin_lock_irqsave(host_set lock)
2857 *
2858 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002859 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 *
2861 */
2862
2863static int ata_sg_setup(struct ata_queued_cmd *qc)
2864{
2865 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002866 struct scatterlist *sg = qc->__sg;
2867 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002868 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa4631472006-02-11 19:11:13 +09002871 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002873 /* we must lengthen transfers to end on a 32-bit boundary */
2874 qc->pad_len = lsg->length & 3;
2875 if (qc->pad_len) {
2876 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2877 struct scatterlist *psg = &qc->pad_sgent;
2878 unsigned int offset;
2879
Tejun Heoa4631472006-02-11 19:11:13 +09002880 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002881
2882 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2883
2884 /*
2885 * psg->page/offset are used to copy to-be-written
2886 * data in this function or read data in ata_sg_clean.
2887 */
2888 offset = lsg->offset + lsg->length - qc->pad_len;
2889 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2890 psg->offset = offset_in_page(offset);
2891
2892 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2893 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2894 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002895 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002896 }
2897
2898 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2899 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2900 /* trim last sg */
2901 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002902 if (lsg->length == 0)
2903 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002904
2905 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2906 qc->n_elem - 1, lsg->length, qc->pad_len);
2907 }
2908
Jeff Garzike1410f22005-11-14 14:06:26 -05002909 pre_n_elem = qc->n_elem;
2910 if (trim_sg && pre_n_elem)
2911 pre_n_elem--;
2912
2913 if (!pre_n_elem) {
2914 n_elem = 0;
2915 goto skip_map;
2916 }
2917
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002919 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002920 if (n_elem < 1) {
2921 /* restore last sg */
2922 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
2926 DPRINTK("%d sg elements mapped\n", n_elem);
2927
Jeff Garzike1410f22005-11-14 14:06:26 -05002928skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 qc->n_elem = n_elem;
2930
2931 return 0;
2932}
2933
2934/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002935 * ata_poll_qc_complete - turn irq back on and finish qc
2936 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002937 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002938 *
2939 * LOCKING:
2940 * None. (grabs host lock)
2941 */
2942
Albert Leea22e2eb2005-12-05 15:38:02 +08002943void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002944{
2945 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002946 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002947
Jeff Garzikb8f61532005-08-25 22:01:20 -04002948 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002949 ap->flags &= ~ATA_FLAG_NOINTR;
2950 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002951 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002952 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002953}
2954
2955/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002956 * ata_pio_poll - poll using PIO, depending on current state
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002957 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 *
2959 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002960 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 *
2962 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002963 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 */
2965
2966static unsigned long ata_pio_poll(struct ata_port *ap)
2967{
Albert Leec14b8332005-12-05 15:36:08 +08002968 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002970 unsigned int poll_state = HSM_ST_UNKNOWN;
2971 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Albert Leec14b8332005-12-05 15:36:08 +08002973 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09002974 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08002975
Albert Lee14be71f2005-09-27 17:36:35 +08002976 switch (ap->hsm_task_state) {
2977 case HSM_ST:
2978 case HSM_ST_POLL:
2979 poll_state = HSM_ST_POLL;
2980 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002982 case HSM_ST_LAST:
2983 case HSM_ST_LAST_POLL:
2984 poll_state = HSM_ST_LAST_POLL;
2985 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 break;
2987 default:
2988 BUG();
2989 break;
2990 }
2991
2992 status = ata_chk_status(ap);
2993 if (status & ATA_BUSY) {
2994 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09002995 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08002996 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 return 0;
2998 }
Albert Lee14be71f2005-09-27 17:36:35 +08002999 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 return ATA_SHORT_PAUSE;
3001 }
3002
Albert Lee14be71f2005-09-27 17:36:35 +08003003 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 return 0;
3005}
3006
3007/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003008 * ata_pio_complete - check if drive is busy or idle
3009 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 *
3011 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003012 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003013 *
3014 * RETURNS:
3015 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 */
3017
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003018static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
3020 struct ata_queued_cmd *qc;
3021 u8 drv_stat;
3022
3023 /*
Alan Cox31433ea2005-08-26 15:56:47 +01003024 * This is purely heuristic. This is a fast path. Sometimes when
3025 * we enter, BSY will be cleared in a chk-status or two. If not,
3026 * the drive is probably seeking or something. Snooze for a couple
3027 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003028 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 */
Albert Leefe79e682005-12-06 11:34:59 +08003030 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3031 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08003033 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3034 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003035 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003037 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
3039 }
3040
Albert Leec14b8332005-12-05 15:36:08 +08003041 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003042 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 drv_stat = ata_wait_idle(ap);
3045 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003046 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003047 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 }
3050
Albert Lee14be71f2005-09-27 17:36:35 +08003051 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Tejun Heoa4631472006-02-11 19:11:13 +09003053 WARN_ON(qc->err_mask);
Albert Leea22e2eb2005-12-05 15:38:02 +08003054 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003055
3056 /* another command may start at this point */
3057
3058 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059}
3060
Edward Falk0baab862005-06-02 18:17:13 -04003061
3062/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003063 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003064 * @buf: Buffer to swap
3065 * @buf_words: Number of 16-bit words in buffer.
3066 *
3067 * Swap halves of 16-bit words if needed to convert from
3068 * little-endian byte order to native cpu byte order, or
3069 * vice-versa.
3070 *
3071 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003072 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003073 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074void swap_buf_le16(u16 *buf, unsigned int buf_words)
3075{
3076#ifdef __BIG_ENDIAN
3077 unsigned int i;
3078
3079 for (i = 0; i < buf_words; i++)
3080 buf[i] = le16_to_cpu(buf[i]);
3081#endif /* __BIG_ENDIAN */
3082}
3083
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003084/**
3085 * ata_mmio_data_xfer - Transfer data by MMIO
3086 * @ap: port to read/write
3087 * @buf: data buffer
3088 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003089 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003090 *
3091 * Transfer data from/to the device data register by MMIO.
3092 *
3093 * LOCKING:
3094 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003095 */
3096
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3098 unsigned int buflen, int write_data)
3099{
3100 unsigned int i;
3101 unsigned int words = buflen >> 1;
3102 u16 *buf16 = (u16 *) buf;
3103 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3104
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003105 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 if (write_data) {
3107 for (i = 0; i < words; i++)
3108 writew(le16_to_cpu(buf16[i]), mmio);
3109 } else {
3110 for (i = 0; i < words; i++)
3111 buf16[i] = cpu_to_le16(readw(mmio));
3112 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003113
3114 /* Transfer trailing 1 byte, if any. */
3115 if (unlikely(buflen & 0x01)) {
3116 u16 align_buf[1] = { 0 };
3117 unsigned char *trailing_buf = buf + buflen - 1;
3118
3119 if (write_data) {
3120 memcpy(align_buf, trailing_buf, 1);
3121 writew(le16_to_cpu(align_buf[0]), mmio);
3122 } else {
3123 align_buf[0] = cpu_to_le16(readw(mmio));
3124 memcpy(trailing_buf, align_buf, 1);
3125 }
3126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127}
3128
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003129/**
3130 * ata_pio_data_xfer - Transfer data by PIO
3131 * @ap: port to read/write
3132 * @buf: data buffer
3133 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003134 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003135 *
3136 * Transfer data from/to the device data register by PIO.
3137 *
3138 * LOCKING:
3139 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003140 */
3141
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3143 unsigned int buflen, int write_data)
3144{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003145 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003147 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003149 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003151 insw(ap->ioaddr.data_addr, buf, words);
3152
3153 /* Transfer trailing 1 byte, if any. */
3154 if (unlikely(buflen & 0x01)) {
3155 u16 align_buf[1] = { 0 };
3156 unsigned char *trailing_buf = buf + buflen - 1;
3157
3158 if (write_data) {
3159 memcpy(align_buf, trailing_buf, 1);
3160 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3161 } else {
3162 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3163 memcpy(trailing_buf, align_buf, 1);
3164 }
3165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166}
3167
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003168/**
3169 * ata_data_xfer - Transfer data from/to the data register.
3170 * @ap: port to read/write
3171 * @buf: data buffer
3172 * @buflen: buffer length
3173 * @do_write: read/write
3174 *
3175 * Transfer data from/to the device data register.
3176 *
3177 * LOCKING:
3178 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003179 */
3180
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3182 unsigned int buflen, int do_write)
3183{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003184 /* Make the crap hardware pay the costs not the good stuff */
3185 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3186 unsigned long flags;
3187 local_irq_save(flags);
3188 if (ap->flags & ATA_FLAG_MMIO)
3189 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3190 else
3191 ata_pio_data_xfer(ap, buf, buflen, do_write);
3192 local_irq_restore(flags);
3193 } else {
3194 if (ap->flags & ATA_FLAG_MMIO)
3195 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3196 else
3197 ata_pio_data_xfer(ap, buf, buflen, do_write);
3198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199}
3200
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003201/**
3202 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3203 * @qc: Command on going
3204 *
3205 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3206 *
3207 * LOCKING:
3208 * Inherited from caller.
3209 */
3210
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211static void ata_pio_sector(struct ata_queued_cmd *qc)
3212{
3213 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003214 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 struct ata_port *ap = qc->ap;
3216 struct page *page;
3217 unsigned int offset;
3218 unsigned char *buf;
3219
3220 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003221 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
3223 page = sg[qc->cursg].page;
3224 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3225
3226 /* get the current page and offset */
3227 page = nth_page(page, (offset >> PAGE_SHIFT));
3228 offset %= PAGE_SIZE;
3229
3230 buf = kmap(page) + offset;
3231
3232 qc->cursect++;
3233 qc->cursg_ofs++;
3234
Albert Lee32529e02005-05-26 03:49:42 -04003235 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 qc->cursg++;
3237 qc->cursg_ofs = 0;
3238 }
3239
3240 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3241
3242 /* do the actual data transfer */
3243 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3244 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3245
3246 kunmap(page);
3247}
3248
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003249/**
3250 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3251 * @qc: Command on going
3252 * @bytes: number of bytes
3253 *
3254 * Transfer Transfer data from/to the ATAPI device.
3255 *
3256 * LOCKING:
3257 * Inherited from caller.
3258 *
3259 */
3260
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3262{
3263 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003264 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 struct ata_port *ap = qc->ap;
3266 struct page *page;
3267 unsigned char *buf;
3268 unsigned int offset, count;
3269
Albert Lee563a6e12005-08-12 14:17:50 +08003270 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003271 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
3273next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003274 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003275 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003276 * The end of qc->sg is reached and the device expects
3277 * more data to transfer. In order not to overrun qc->sg
3278 * and fulfill length specified in the byte count register,
3279 * - for read case, discard trailing data from the device
3280 * - for write case, padding zero data to the device
3281 */
3282 u16 pad_buf[1] = { 0 };
3283 unsigned int words = bytes >> 1;
3284 unsigned int i;
3285
3286 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003287 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003288 ap->id, bytes);
3289
3290 for (i = 0; i < words; i++)
3291 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3292
Albert Lee14be71f2005-09-27 17:36:35 +08003293 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003294 return;
3295 }
3296
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003297 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 page = sg->page;
3300 offset = sg->offset + qc->cursg_ofs;
3301
3302 /* get the current page and offset */
3303 page = nth_page(page, (offset >> PAGE_SHIFT));
3304 offset %= PAGE_SIZE;
3305
Albert Lee6952df02005-06-06 15:56:03 +08003306 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003307 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
3309 /* don't cross page boundaries */
3310 count = min(count, (unsigned int)PAGE_SIZE - offset);
3311
3312 buf = kmap(page) + offset;
3313
3314 bytes -= count;
3315 qc->curbytes += count;
3316 qc->cursg_ofs += count;
3317
Albert Lee32529e02005-05-26 03:49:42 -04003318 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 qc->cursg++;
3320 qc->cursg_ofs = 0;
3321 }
3322
3323 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3324
3325 /* do the actual data transfer */
3326 ata_data_xfer(ap, buf, count, do_write);
3327
3328 kunmap(page);
3329
Albert Lee563a6e12005-08-12 14:17:50 +08003330 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332}
3333
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003334/**
3335 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3336 * @qc: Command on going
3337 *
3338 * Transfer Transfer data from/to the ATAPI device.
3339 *
3340 * LOCKING:
3341 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003342 */
3343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3345{
3346 struct ata_port *ap = qc->ap;
3347 struct ata_device *dev = qc->dev;
3348 unsigned int ireason, bc_lo, bc_hi, bytes;
3349 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3350
3351 ap->ops->tf_read(ap, &qc->tf);
3352 ireason = qc->tf.nsect;
3353 bc_lo = qc->tf.lbam;
3354 bc_hi = qc->tf.lbah;
3355 bytes = (bc_hi << 8) | bc_lo;
3356
3357 /* shall be cleared to zero, indicating xfer of data */
3358 if (ireason & (1 << 0))
3359 goto err_out;
3360
3361 /* make sure transfer direction matches expected */
3362 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3363 if (do_write != i_write)
3364 goto err_out;
3365
3366 __atapi_pio_bytes(qc, bytes);
3367
3368 return;
3369
3370err_out:
3371 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3372 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003373 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003374 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375}
3376
3377/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003378 * ata_pio_block - start PIO on a block
3379 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 *
3381 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003382 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 */
3384
3385static void ata_pio_block(struct ata_port *ap)
3386{
3387 struct ata_queued_cmd *qc;
3388 u8 status;
3389
3390 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003391 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 * Sometimes when we enter, BSY will be cleared in
3393 * a chk-status or two. If not, the drive is probably seeking
3394 * or something. Snooze for a couple msecs, then
3395 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003396 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 */
3398 status = ata_busy_wait(ap, ATA_BUSY, 5);
3399 if (status & ATA_BUSY) {
3400 msleep(2);
3401 status = ata_busy_wait(ap, ATA_BUSY, 10);
3402 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003403 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3405 return;
3406 }
3407 }
3408
3409 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003410 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411
Albert Leefe79e682005-12-06 11:34:59 +08003412 /* check error */
3413 if (status & (ATA_ERR | ATA_DF)) {
3414 qc->err_mask |= AC_ERR_DEV;
3415 ap->hsm_task_state = HSM_ST_ERR;
3416 return;
3417 }
3418
3419 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003421 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003423 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 return;
3425 }
3426
3427 atapi_pio_bytes(qc);
3428 } else {
3429 /* handle BSY=0, DRQ=0 as error */
3430 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003431 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003432 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 return;
3434 }
3435
3436 ata_pio_sector(qc);
3437 }
3438}
3439
3440static void ata_pio_error(struct ata_port *ap)
3441{
3442 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003443
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003445 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446
Albert Lee0565c262006-02-13 18:55:25 +08003447 if (qc->tf.command != ATA_CMD_PACKET)
3448 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3449
Albert Lee1c848982005-12-05 15:40:15 +08003450 /* make sure qc->err_mask is available to
3451 * know what's wrong and recover
3452 */
Tejun Heoa4631472006-02-11 19:11:13 +09003453 WARN_ON(qc->err_mask == 0);
Albert Lee1c848982005-12-05 15:40:15 +08003454
Albert Lee14be71f2005-09-27 17:36:35 +08003455 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
Albert Leea22e2eb2005-12-05 15:38:02 +08003457 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458}
3459
3460static void ata_pio_task(void *_data)
3461{
3462 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003463 unsigned long timeout;
3464 int qc_completed;
3465
3466fsm_start:
3467 timeout = 0;
3468 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Albert Lee14be71f2005-09-27 17:36:35 +08003470 switch (ap->hsm_task_state) {
3471 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 return;
3473
Albert Lee14be71f2005-09-27 17:36:35 +08003474 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 ata_pio_block(ap);
3476 break;
3477
Albert Lee14be71f2005-09-27 17:36:35 +08003478 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003479 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 break;
3481
Albert Lee14be71f2005-09-27 17:36:35 +08003482 case HSM_ST_POLL:
3483 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 timeout = ata_pio_poll(ap);
3485 break;
3486
Albert Lee14be71f2005-09-27 17:36:35 +08003487 case HSM_ST_TMOUT:
3488 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 ata_pio_error(ap);
3490 return;
3491 }
3492
3493 if (timeout)
Tejun Heo95064372006-01-23 13:09:37 +09003494 ata_queue_delayed_pio_task(ap, timeout);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003495 else if (!qc_completed)
3496 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497}
3498
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499/**
3500 * ata_qc_timeout - Handle timeout of queued command
3501 * @qc: Command that timed out
3502 *
3503 * Some part of the kernel (currently, only the SCSI layer)
3504 * has noticed that the active command on port @ap has not
3505 * completed after a specified length of time. Handle this
3506 * condition by disabling DMA (if necessary) and completing
3507 * transactions, with error if necessary.
3508 *
3509 * This also handles the case of the "lost interrupt", where
3510 * for some reason (possibly hardware bug, possibly driver bug)
3511 * an interrupt was not delivered to the driver, even though the
3512 * transaction completed successfully.
3513 *
3514 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003515 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 */
3517
3518static void ata_qc_timeout(struct ata_queued_cmd *qc)
3519{
3520 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003521 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003523 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
3525 DPRINTK("ENTER\n");
3526
Tejun Heoc18d06f2006-02-02 00:56:10 +09003527 ata_flush_pio_tasks(ap);
3528 ap->hsm_task_state = HSM_ST_IDLE;
3529
Jeff Garzikb8f61532005-08-25 22:01:20 -04003530 spin_lock_irqsave(&host_set->lock, flags);
3531
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 switch (qc->tf.protocol) {
3533
3534 case ATA_PROT_DMA:
3535 case ATA_PROT_ATAPI_DMA:
3536 host_stat = ap->ops->bmdma_status(ap);
3537
3538 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003539 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
3541 /* fall through */
3542
3543 default:
3544 ata_altstatus(ap);
3545 drv_stat = ata_chk_status(ap);
3546
3547 /* ack bmdma irq events */
3548 ap->ops->irq_clear(ap);
3549
3550 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3551 ap->id, qc->tf.command, drv_stat, host_stat);
3552
3553 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003554 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 break;
3556 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003557
3558 spin_unlock_irqrestore(&host_set->lock, flags);
3559
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003560 ata_eh_qc_complete(qc);
3561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 DPRINTK("EXIT\n");
3563}
3564
3565/**
3566 * ata_eng_timeout - Handle timeout of queued command
3567 * @ap: Port on which timed-out command is active
3568 *
3569 * Some part of the kernel (currently, only the SCSI layer)
3570 * has noticed that the active command on port @ap has not
3571 * completed after a specified length of time. Handle this
3572 * condition by disabling DMA (if necessary) and completing
3573 * transactions, with error if necessary.
3574 *
3575 * This also handles the case of the "lost interrupt", where
3576 * for some reason (possibly hardware bug, possibly driver bug)
3577 * an interrupt was not delivered to the driver, even though the
3578 * transaction completed successfully.
3579 *
3580 * LOCKING:
3581 * Inherited from SCSI layer (none, can sleep)
3582 */
3583
3584void ata_eng_timeout(struct ata_port *ap)
3585{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 DPRINTK("ENTER\n");
3587
Tejun Heof6379022006-02-10 15:10:48 +09003588 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 DPRINTK("EXIT\n");
3591}
3592
3593/**
3594 * ata_qc_new - Request an available ATA command, for queueing
3595 * @ap: Port associated with device @dev
3596 * @dev: Device from whom we request an available command structure
3597 *
3598 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003599 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 */
3601
3602static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3603{
3604 struct ata_queued_cmd *qc = NULL;
3605 unsigned int i;
3606
3607 for (i = 0; i < ATA_MAX_QUEUE; i++)
3608 if (!test_and_set_bit(i, &ap->qactive)) {
3609 qc = ata_qc_from_tag(ap, i);
3610 break;
3611 }
3612
3613 if (qc)
3614 qc->tag = i;
3615
3616 return qc;
3617}
3618
3619/**
3620 * ata_qc_new_init - Request an available ATA command, and initialize it
3621 * @ap: Port associated with device @dev
3622 * @dev: Device from whom we request an available command structure
3623 *
3624 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003625 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 */
3627
3628struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3629 struct ata_device *dev)
3630{
3631 struct ata_queued_cmd *qc;
3632
3633 qc = ata_qc_new(ap);
3634 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 qc->scsicmd = NULL;
3636 qc->ap = ap;
3637 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003639 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 }
3641
3642 return qc;
3643}
3644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645/**
3646 * ata_qc_free - free unused ata_queued_cmd
3647 * @qc: Command to complete
3648 *
3649 * Designed to free unused ata_queued_cmd object
3650 * in case something prevents using it.
3651 *
3652 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003653 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 */
3655void ata_qc_free(struct ata_queued_cmd *qc)
3656{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003657 struct ata_port *ap = qc->ap;
3658 unsigned int tag;
3659
Tejun Heoa4631472006-02-11 19:11:13 +09003660 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Tejun Heo4ba946e2006-01-23 13:09:36 +09003662 qc->flags = 0;
3663 tag = qc->tag;
3664 if (likely(ata_tag_valid(tag))) {
3665 if (tag == ap->active_tag)
3666 ap->active_tag = ATA_TAG_POISON;
3667 qc->tag = ATA_TAG_POISON;
3668 clear_bit(tag, &ap->qactive);
3669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670}
3671
Tejun Heo76014422006-02-11 15:13:49 +09003672void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673{
Tejun Heoa4631472006-02-11 19:11:13 +09003674 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3675 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
3677 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3678 ata_sg_clean(qc);
3679
Albert Lee3f3791d2005-08-16 14:25:38 +08003680 /* atapi: mark qc as inactive to prevent the interrupt handler
3681 * from completing the command twice later, before the error handler
3682 * is called. (when rc != 0 and atapi request sense is needed)
3683 */
3684 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003687 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688}
3689
3690static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3691{
3692 struct ata_port *ap = qc->ap;
3693
3694 switch (qc->tf.protocol) {
3695 case ATA_PROT_DMA:
3696 case ATA_PROT_ATAPI_DMA:
3697 return 1;
3698
3699 case ATA_PROT_ATAPI:
3700 case ATA_PROT_PIO:
3701 case ATA_PROT_PIO_MULT:
3702 if (ap->flags & ATA_FLAG_PIO_DMA)
3703 return 1;
3704
3705 /* fall through */
3706
3707 default:
3708 return 0;
3709 }
3710
3711 /* never reached */
3712}
3713
3714/**
3715 * ata_qc_issue - issue taskfile to device
3716 * @qc: command to issue to device
3717 *
3718 * Prepare an ATA command to submission to device.
3719 * This includes mapping the data into a DMA-able
3720 * area, filling in the S/G table, and finally
3721 * writing the taskfile to hardware, starting the command.
3722 *
3723 * LOCKING:
3724 * spin_lock_irqsave(host_set lock)
3725 *
3726 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003727 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 */
3729
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003730unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731{
3732 struct ata_port *ap = qc->ap;
3733
3734 if (ata_should_dma_map(qc)) {
3735 if (qc->flags & ATA_QCFLAG_SG) {
3736 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003737 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3739 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003740 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 }
3742 } else {
3743 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3744 }
3745
3746 ap->ops->qc_prep(qc);
3747
3748 qc->ap->active_tag = qc->tag;
3749 qc->flags |= ATA_QCFLAG_ACTIVE;
3750
3751 return ap->ops->qc_issue(qc);
3752
Tejun Heo8e436af2006-01-23 13:09:36 +09003753sg_err:
3754 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003755 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756}
3757
Edward Falk0baab862005-06-02 18:17:13 -04003758
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759/**
3760 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3761 * @qc: command to issue to device
3762 *
3763 * Using various libata functions and hooks, this function
3764 * starts an ATA command. ATA commands are grouped into
3765 * classes called "protocols", and issuing each type of protocol
3766 * is slightly different.
3767 *
Edward Falk0baab862005-06-02 18:17:13 -04003768 * May be used as the qc_issue() entry in ata_port_operations.
3769 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 * LOCKING:
3771 * spin_lock_irqsave(host_set lock)
3772 *
3773 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003774 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 */
3776
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003777unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778{
3779 struct ata_port *ap = qc->ap;
3780
3781 ata_dev_select(ap, qc->dev->devno, 1, 0);
3782
3783 switch (qc->tf.protocol) {
3784 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05003785 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 break;
3787
3788 case ATA_PROT_DMA:
3789 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3790 ap->ops->bmdma_setup(qc); /* set up bmdma */
3791 ap->ops->bmdma_start(qc); /* initiate bmdma */
3792 break;
3793
3794 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3795 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003796 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003797 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09003798 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 break;
3800
3801 case ATA_PROT_ATAPI:
3802 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003803 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003804 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 break;
3806
3807 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003808 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05003809 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003810 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 break;
3812
3813 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003814 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3816 ap->ops->bmdma_setup(qc); /* set up bmdma */
Tejun Heo95064372006-01-23 13:09:37 +09003817 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 break;
3819
3820 default:
3821 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003822 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 }
3824
3825 return 0;
3826}
3827
3828/**
Edward Falk0baab862005-06-02 18:17:13 -04003829 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 * @qc: Info associated with this ATA transaction.
3831 *
3832 * LOCKING:
3833 * spin_lock_irqsave(host_set lock)
3834 */
3835
3836static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3837{
3838 struct ata_port *ap = qc->ap;
3839 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3840 u8 dmactl;
3841 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3842
3843 /* load PRD table addr. */
3844 mb(); /* make sure PRD table writes are visible to controller */
3845 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3846
3847 /* specify data direction, triple-check start bit is clear */
3848 dmactl = readb(mmio + ATA_DMA_CMD);
3849 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3850 if (!rw)
3851 dmactl |= ATA_DMA_WR;
3852 writeb(dmactl, mmio + ATA_DMA_CMD);
3853
3854 /* issue r/w command */
3855 ap->ops->exec_command(ap, &qc->tf);
3856}
3857
3858/**
Alan Coxb73fc892005-08-26 16:03:19 +01003859 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 * @qc: Info associated with this ATA transaction.
3861 *
3862 * LOCKING:
3863 * spin_lock_irqsave(host_set lock)
3864 */
3865
3866static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3867{
3868 struct ata_port *ap = qc->ap;
3869 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3870 u8 dmactl;
3871
3872 /* start host DMA transaction */
3873 dmactl = readb(mmio + ATA_DMA_CMD);
3874 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3875
3876 /* Strictly, one may wish to issue a readb() here, to
3877 * flush the mmio write. However, control also passes
3878 * to the hardware at this point, and it will interrupt
3879 * us when we are to resume control. So, in effect,
3880 * we don't care when the mmio write flushes.
3881 * Further, a read of the DMA status register _immediately_
3882 * following the write may not be what certain flaky hardware
3883 * is expected, so I think it is best to not add a readb()
3884 * without first all the MMIO ATA cards/mobos.
3885 * Or maybe I'm just being paranoid.
3886 */
3887}
3888
3889/**
3890 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3891 * @qc: Info associated with this ATA transaction.
3892 *
3893 * LOCKING:
3894 * spin_lock_irqsave(host_set lock)
3895 */
3896
3897static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3898{
3899 struct ata_port *ap = qc->ap;
3900 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3901 u8 dmactl;
3902
3903 /* load PRD table addr. */
3904 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3905
3906 /* specify data direction, triple-check start bit is clear */
3907 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3908 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3909 if (!rw)
3910 dmactl |= ATA_DMA_WR;
3911 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3912
3913 /* issue r/w command */
3914 ap->ops->exec_command(ap, &qc->tf);
3915}
3916
3917/**
3918 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3919 * @qc: Info associated with this ATA transaction.
3920 *
3921 * LOCKING:
3922 * spin_lock_irqsave(host_set lock)
3923 */
3924
3925static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3926{
3927 struct ata_port *ap = qc->ap;
3928 u8 dmactl;
3929
3930 /* start host DMA transaction */
3931 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3932 outb(dmactl | ATA_DMA_START,
3933 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3934}
3935
Edward Falk0baab862005-06-02 18:17:13 -04003936
3937/**
3938 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3939 * @qc: Info associated with this ATA transaction.
3940 *
3941 * Writes the ATA_DMA_START flag to the DMA command register.
3942 *
3943 * May be used as the bmdma_start() entry in ata_port_operations.
3944 *
3945 * LOCKING:
3946 * spin_lock_irqsave(host_set lock)
3947 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948void ata_bmdma_start(struct ata_queued_cmd *qc)
3949{
3950 if (qc->ap->flags & ATA_FLAG_MMIO)
3951 ata_bmdma_start_mmio(qc);
3952 else
3953 ata_bmdma_start_pio(qc);
3954}
3955
Edward Falk0baab862005-06-02 18:17:13 -04003956
3957/**
3958 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3959 * @qc: Info associated with this ATA transaction.
3960 *
3961 * Writes address of PRD table to device's PRD Table Address
3962 * register, sets the DMA control register, and calls
3963 * ops->exec_command() to start the transfer.
3964 *
3965 * May be used as the bmdma_setup() entry in ata_port_operations.
3966 *
3967 * LOCKING:
3968 * spin_lock_irqsave(host_set lock)
3969 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970void ata_bmdma_setup(struct ata_queued_cmd *qc)
3971{
3972 if (qc->ap->flags & ATA_FLAG_MMIO)
3973 ata_bmdma_setup_mmio(qc);
3974 else
3975 ata_bmdma_setup_pio(qc);
3976}
3977
Edward Falk0baab862005-06-02 18:17:13 -04003978
3979/**
3980 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003981 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003982 *
3983 * Clear interrupt and error flags in DMA status register.
3984 *
3985 * May be used as the irq_clear() entry in ata_port_operations.
3986 *
3987 * LOCKING:
3988 * spin_lock_irqsave(host_set lock)
3989 */
3990
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991void ata_bmdma_irq_clear(struct ata_port *ap)
3992{
3993 if (ap->flags & ATA_FLAG_MMIO) {
3994 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3995 writeb(readb(mmio), mmio);
3996 } else {
3997 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3998 outb(inb(addr), addr);
3999 }
4000
4001}
4002
Edward Falk0baab862005-06-02 18:17:13 -04004003
4004/**
4005 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004006 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004007 *
4008 * Read and return BMDMA status register.
4009 *
4010 * May be used as the bmdma_status() entry in ata_port_operations.
4011 *
4012 * LOCKING:
4013 * spin_lock_irqsave(host_set lock)
4014 */
4015
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016u8 ata_bmdma_status(struct ata_port *ap)
4017{
4018 u8 host_stat;
4019 if (ap->flags & ATA_FLAG_MMIO) {
4020 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4021 host_stat = readb(mmio + ATA_DMA_STATUS);
4022 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004023 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 return host_stat;
4025}
4026
Edward Falk0baab862005-06-02 18:17:13 -04004027
4028/**
4029 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004030 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004031 *
4032 * Clears the ATA_DMA_START flag in the dma control register
4033 *
4034 * May be used as the bmdma_stop() entry in ata_port_operations.
4035 *
4036 * LOCKING:
4037 * spin_lock_irqsave(host_set lock)
4038 */
4039
Alan Coxb73fc892005-08-26 16:03:19 +01004040void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041{
Alan Coxb73fc892005-08-26 16:03:19 +01004042 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 if (ap->flags & ATA_FLAG_MMIO) {
4044 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4045
4046 /* clear start/stop bit */
4047 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4048 mmio + ATA_DMA_CMD);
4049 } else {
4050 /* clear start/stop bit */
4051 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4052 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4053 }
4054
4055 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4056 ata_altstatus(ap); /* dummy read */
4057}
4058
4059/**
4060 * ata_host_intr - Handle host interrupt for given (port, task)
4061 * @ap: Port on which interrupt arrived (possibly...)
4062 * @qc: Taskfile currently active in engine
4063 *
4064 * Handle host interrupt for given queued command. Currently,
4065 * only DMA interrupts are handled. All other commands are
4066 * handled via polling with interrupts disabled (nIEN bit).
4067 *
4068 * LOCKING:
4069 * spin_lock_irqsave(host_set lock)
4070 *
4071 * RETURNS:
4072 * One if interrupt was handled, zero if not (shared irq).
4073 */
4074
4075inline unsigned int ata_host_intr (struct ata_port *ap,
4076 struct ata_queued_cmd *qc)
4077{
4078 u8 status, host_stat;
4079
4080 switch (qc->tf.protocol) {
4081
4082 case ATA_PROT_DMA:
4083 case ATA_PROT_ATAPI_DMA:
4084 case ATA_PROT_ATAPI:
4085 /* check status of DMA engine */
4086 host_stat = ap->ops->bmdma_status(ap);
4087 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4088
4089 /* if it's not our irq... */
4090 if (!(host_stat & ATA_DMA_INTR))
4091 goto idle_irq;
4092
4093 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004094 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
4096 /* fall through */
4097
4098 case ATA_PROT_ATAPI_NODATA:
4099 case ATA_PROT_NODATA:
4100 /* check altstatus */
4101 status = ata_altstatus(ap);
4102 if (status & ATA_BUSY)
4103 goto idle_irq;
4104
4105 /* check main status, clearing INTRQ */
4106 status = ata_chk_status(ap);
4107 if (unlikely(status & ATA_BUSY))
4108 goto idle_irq;
4109 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4110 ap->id, qc->tf.protocol, status);
4111
4112 /* ack bmdma irq events */
4113 ap->ops->irq_clear(ap);
4114
4115 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004116 qc->err_mask |= ac_err_mask(status);
4117 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 break;
4119
4120 default:
4121 goto idle_irq;
4122 }
4123
4124 return 1; /* irq handled */
4125
4126idle_irq:
4127 ap->stats.idle_irq++;
4128
4129#ifdef ATA_IRQ_TRAP
4130 if ((ap->stats.idle_irq % 1000) == 0) {
4131 handled = 1;
4132 ata_irq_ack(ap, 0); /* debug trap */
4133 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4134 }
4135#endif
4136 return 0; /* irq not handled */
4137}
4138
4139/**
4140 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004141 * @irq: irq line (unused)
4142 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 * @regs: unused
4144 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004145 * Default interrupt handler for PCI IDE devices. Calls
4146 * ata_host_intr() for each port that is not disabled.
4147 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004149 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 *
4151 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004152 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 */
4154
4155irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4156{
4157 struct ata_host_set *host_set = dev_instance;
4158 unsigned int i;
4159 unsigned int handled = 0;
4160 unsigned long flags;
4161
4162 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4163 spin_lock_irqsave(&host_set->lock, flags);
4164
4165 for (i = 0; i < host_set->n_ports; i++) {
4166 struct ata_port *ap;
4167
4168 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004169 if (ap &&
4170 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 struct ata_queued_cmd *qc;
4172
4173 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004174 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4175 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 handled |= ata_host_intr(ap, qc);
4177 }
4178 }
4179
4180 spin_unlock_irqrestore(&host_set->lock, flags);
4181
4182 return IRQ_RETVAL(handled);
4183}
4184
4185/**
4186 * atapi_packet_task - Write CDB bytes to hardware
4187 * @_data: Port to which ATAPI device is attached.
4188 *
4189 * When device has indicated its readiness to accept
4190 * a CDB, this function is called. Send the CDB.
4191 * If DMA is to be performed, exit immediately.
4192 * Otherwise, we are in polling mode, so poll
4193 * status under operation succeeds or fails.
4194 *
4195 * LOCKING:
4196 * Kernel thread context (may sleep)
4197 */
4198
4199static void atapi_packet_task(void *_data)
4200{
4201 struct ata_port *ap = _data;
4202 struct ata_queued_cmd *qc;
4203 u8 status;
4204
4205 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09004206 WARN_ON(qc == NULL);
4207 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
4209 /* sleep-wait for BSY to clear */
4210 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004211 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004212 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Leed8fe4522005-12-05 15:42:17 +08004213 goto err_out;
4214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
4216 /* make sure DRQ is set */
4217 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004218 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004219 qc->err_mask |= AC_ERR_HSM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222
4223 /* send SCSI cdb */
4224 DPRINTK("send cdb\n");
Tejun Heo6e7846e2006-02-12 23:32:58 +09004225 WARN_ON(qc->dev->cdb_len < 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226
Tejun Heoc1389502005-08-22 14:59:24 +09004227 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4228 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4229 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230
Tejun Heoc1389502005-08-22 14:59:24 +09004231 /* Once we're done issuing command and kicking bmdma,
4232 * irq handler takes over. To not lose irq, we need
4233 * to clear NOINTR flag before sending cdb, but
4234 * interrupt handler shouldn't be invoked before we're
4235 * finished. Hence, the following locking.
4236 */
4237 spin_lock_irqsave(&ap->host_set->lock, flags);
4238 ap->flags &= ~ATA_FLAG_NOINTR;
Tejun Heo6e7846e2006-02-12 23:32:58 +09004239 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
Tejun Heoc1389502005-08-22 14:59:24 +09004240 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4241 ap->ops->bmdma_start(qc); /* initiate bmdma */
4242 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4243 } else {
Tejun Heo6e7846e2006-02-12 23:32:58 +09004244 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
Tejun Heoc1389502005-08-22 14:59:24 +09004246 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004247 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09004248 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 }
4250
4251 return;
4252
4253err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004254 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255}
4256
Edward Falk0baab862005-06-02 18:17:13 -04004257
Jens Axboe9b847542006-01-06 09:28:07 +01004258/*
4259 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4260 * without filling any other registers
4261 */
4262static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4263 u8 cmd)
4264{
4265 struct ata_taskfile tf;
4266 int err;
4267
4268 ata_tf_init(ap, &tf, dev->devno);
4269
4270 tf.command = cmd;
4271 tf.flags |= ATA_TFLAG_DEVICE;
4272 tf.protocol = ATA_PROT_NODATA;
4273
4274 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4275 if (err)
4276 printk(KERN_ERR "%s: ata command failed: %d\n",
4277 __FUNCTION__, err);
4278
4279 return err;
4280}
4281
4282static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4283{
4284 u8 cmd;
4285
4286 if (!ata_try_flush_cache(dev))
4287 return 0;
4288
4289 if (ata_id_has_flush_ext(dev->id))
4290 cmd = ATA_CMD_FLUSH_EXT;
4291 else
4292 cmd = ATA_CMD_FLUSH;
4293
4294 return ata_do_simple_cmd(ap, dev, cmd);
4295}
4296
4297static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4298{
4299 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4300}
4301
4302static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4303{
4304 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4305}
4306
4307/**
4308 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004309 * @ap: port the device is connected to
4310 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004311 *
4312 * Kick the drive back into action, by sending it an idle immediate
4313 * command and making sure its transfer mode matches between drive
4314 * and host.
4315 *
4316 */
4317int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4318{
4319 if (ap->flags & ATA_FLAG_SUSPENDED) {
4320 ap->flags &= ~ATA_FLAG_SUSPENDED;
4321 ata_set_mode(ap);
4322 }
4323 if (!ata_dev_present(dev))
4324 return 0;
4325 if (dev->class == ATA_DEV_ATA)
4326 ata_start_drive(ap, dev);
4327
4328 return 0;
4329}
4330
4331/**
4332 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004333 * @ap: port the device is connected to
4334 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004335 *
4336 * Flush the cache on the drive, if appropriate, then issue a
4337 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004338 */
4339int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4340{
4341 if (!ata_dev_present(dev))
4342 return 0;
4343 if (dev->class == ATA_DEV_ATA)
4344 ata_flush_cache(ap, dev);
4345
4346 ata_standby_drive(ap, dev);
4347 ap->flags |= ATA_FLAG_SUSPENDED;
4348 return 0;
4349}
4350
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004351/**
4352 * ata_port_start - Set port up for dma.
4353 * @ap: Port to initialize
4354 *
4355 * Called just after data structures for each port are
4356 * initialized. Allocates space for PRD table.
4357 *
4358 * May be used as the port_start() entry in ata_port_operations.
4359 *
4360 * LOCKING:
4361 * Inherited from caller.
4362 */
4363
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364int ata_port_start (struct ata_port *ap)
4365{
4366 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004367 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
4369 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4370 if (!ap->prd)
4371 return -ENOMEM;
4372
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004373 rc = ata_pad_alloc(ap, dev);
4374 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004375 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004376 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004377 }
4378
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4380
4381 return 0;
4382}
4383
Edward Falk0baab862005-06-02 18:17:13 -04004384
4385/**
4386 * ata_port_stop - Undo ata_port_start()
4387 * @ap: Port to shut down
4388 *
4389 * Frees the PRD table.
4390 *
4391 * May be used as the port_stop() entry in ata_port_operations.
4392 *
4393 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004394 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004395 */
4396
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397void ata_port_stop (struct ata_port *ap)
4398{
4399 struct device *dev = ap->host_set->dev;
4400
4401 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004402 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403}
4404
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004405void ata_host_stop (struct ata_host_set *host_set)
4406{
4407 if (host_set->mmio_base)
4408 iounmap(host_set->mmio_base);
4409}
4410
4411
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412/**
4413 * ata_host_remove - Unregister SCSI host structure with upper layers
4414 * @ap: Port to unregister
4415 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4416 *
4417 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004418 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 */
4420
4421static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4422{
4423 struct Scsi_Host *sh = ap->host;
4424
4425 DPRINTK("ENTER\n");
4426
4427 if (do_unregister)
4428 scsi_remove_host(sh);
4429
4430 ap->ops->port_stop(ap);
4431}
4432
4433/**
4434 * ata_host_init - Initialize an ata_port structure
4435 * @ap: Structure to initialize
4436 * @host: associated SCSI mid-layer structure
4437 * @host_set: Collection of hosts to which @ap belongs
4438 * @ent: Probe information provided by low-level driver
4439 * @port_no: Port number associated with this ata_port
4440 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004441 * Initialize a new ata_port structure, and its associated
4442 * scsi_host.
4443 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004445 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 */
4447
4448static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4449 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004450 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451{
4452 unsigned int i;
4453
4454 host->max_id = 16;
4455 host->max_lun = 1;
4456 host->max_channel = 1;
4457 host->unique_id = ata_unique_id++;
4458 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004459
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 ap->flags = ATA_FLAG_PORT_DISABLED;
4461 ap->id = host->unique_id;
4462 ap->host = host;
4463 ap->ctl = ATA_DEVCTL_OBS;
4464 ap->host_set = host_set;
4465 ap->port_no = port_no;
4466 ap->hard_port_no =
4467 ent->legacy_mode ? ent->hard_port_no : port_no;
4468 ap->pio_mask = ent->pio_mask;
4469 ap->mwdma_mask = ent->mwdma_mask;
4470 ap->udma_mask = ent->udma_mask;
4471 ap->flags |= ent->host_flags;
4472 ap->ops = ent->port_ops;
4473 ap->cbl = ATA_CBL_NONE;
4474 ap->active_tag = ATA_TAG_POISON;
4475 ap->last_ctl = 0xFF;
4476
4477 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4478 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004479 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
4481 for (i = 0; i < ATA_MAX_DEVICES; i++)
4482 ap->device[i].devno = i;
4483
4484#ifdef ATA_IRQ_TRAP
4485 ap->stats.unhandled_irq = 1;
4486 ap->stats.idle_irq = 1;
4487#endif
4488
4489 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4490}
4491
4492/**
4493 * ata_host_add - Attach low-level ATA driver to system
4494 * @ent: Information provided by low-level driver
4495 * @host_set: Collections of ports to which we add
4496 * @port_no: Port number associated with this host
4497 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004498 * Attach low-level ATA driver to system.
4499 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004501 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 *
4503 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004504 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 */
4506
Jeff Garzik057ace52005-10-22 14:27:05 -04004507static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 struct ata_host_set *host_set,
4509 unsigned int port_no)
4510{
4511 struct Scsi_Host *host;
4512 struct ata_port *ap;
4513 int rc;
4514
4515 DPRINTK("ENTER\n");
4516 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4517 if (!host)
4518 return NULL;
4519
4520 ap = (struct ata_port *) &host->hostdata[0];
4521
4522 ata_host_init(ap, host, host_set, ent, port_no);
4523
4524 rc = ap->ops->port_start(ap);
4525 if (rc)
4526 goto err_out;
4527
4528 return ap;
4529
4530err_out:
4531 scsi_host_put(host);
4532 return NULL;
4533}
4534
4535/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004536 * ata_device_add - Register hardware device with ATA and SCSI layers
4537 * @ent: Probe information describing hardware device to be registered
4538 *
4539 * This function processes the information provided in the probe
4540 * information struct @ent, allocates the necessary ATA and SCSI
4541 * host information structures, initializes them, and registers
4542 * everything with requisite kernel subsystems.
4543 *
4544 * This function requests irqs, probes the ATA bus, and probes
4545 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 *
4547 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004548 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 *
4550 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004551 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 */
4553
Jeff Garzik057ace52005-10-22 14:27:05 -04004554int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555{
4556 unsigned int count = 0, i;
4557 struct device *dev = ent->dev;
4558 struct ata_host_set *host_set;
4559
4560 DPRINTK("ENTER\n");
4561 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004562 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4564 if (!host_set)
4565 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 spin_lock_init(&host_set->lock);
4567
4568 host_set->dev = dev;
4569 host_set->n_ports = ent->n_ports;
4570 host_set->irq = ent->irq;
4571 host_set->mmio_base = ent->mmio_base;
4572 host_set->private_data = ent->private_data;
4573 host_set->ops = ent->port_ops;
4574
4575 /* register each port bound to this device */
4576 for (i = 0; i < ent->n_ports; i++) {
4577 struct ata_port *ap;
4578 unsigned long xfer_mode_mask;
4579
4580 ap = ata_host_add(ent, host_set, i);
4581 if (!ap)
4582 goto err_out;
4583
4584 host_set->ports[i] = ap;
4585 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4586 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4587 (ap->pio_mask << ATA_SHIFT_PIO);
4588
4589 /* print per-port info to dmesg */
4590 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4591 "bmdma 0x%lX irq %lu\n",
4592 ap->id,
4593 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4594 ata_mode_string(xfer_mode_mask),
4595 ap->ioaddr.cmd_addr,
4596 ap->ioaddr.ctl_addr,
4597 ap->ioaddr.bmdma_addr,
4598 ent->irq);
4599
4600 ata_chk_status(ap);
4601 host_set->ops->irq_clear(ap);
4602 count++;
4603 }
4604
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004605 if (!count)
4606 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607
4608 /* obtain irq, that is shared between channels */
4609 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4610 DRV_NAME, host_set))
4611 goto err_out;
4612
4613 /* perform each probe synchronously */
4614 DPRINTK("probe begin\n");
4615 for (i = 0; i < count; i++) {
4616 struct ata_port *ap;
4617 int rc;
4618
4619 ap = host_set->ports[i];
4620
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004621 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004623 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
4625 if (rc) {
4626 /* FIXME: do something useful here?
4627 * Current libata behavior will
4628 * tear down everything when
4629 * the module is removed
4630 * or the h/w is unplugged.
4631 */
4632 }
4633
4634 rc = scsi_add_host(ap->host, dev);
4635 if (rc) {
4636 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4637 ap->id);
4638 /* FIXME: do something useful here */
4639 /* FIXME: handle unconditional calls to
4640 * scsi_scan_host and ata_host_remove, below,
4641 * at the very least
4642 */
4643 }
4644 }
4645
4646 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004647 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 for (i = 0; i < count; i++) {
4649 struct ata_port *ap = host_set->ports[i];
4650
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004651 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 }
4653
4654 dev_set_drvdata(dev, host_set);
4655
4656 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4657 return ent->n_ports; /* success */
4658
4659err_out:
4660 for (i = 0; i < count; i++) {
4661 ata_host_remove(host_set->ports[i], 1);
4662 scsi_host_put(host_set->ports[i]->host);
4663 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004664err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 kfree(host_set);
4666 VPRINTK("EXIT, returning 0\n");
4667 return 0;
4668}
4669
4670/**
Alan Cox17b14452005-09-15 15:44:00 +01004671 * ata_host_set_remove - PCI layer callback for device removal
4672 * @host_set: ATA host set that was removed
4673 *
4674 * Unregister all objects associated with this host set. Free those
4675 * objects.
4676 *
4677 * LOCKING:
4678 * Inherited from calling layer (may sleep).
4679 */
4680
Alan Cox17b14452005-09-15 15:44:00 +01004681void ata_host_set_remove(struct ata_host_set *host_set)
4682{
4683 struct ata_port *ap;
4684 unsigned int i;
4685
4686 for (i = 0; i < host_set->n_ports; i++) {
4687 ap = host_set->ports[i];
4688 scsi_remove_host(ap->host);
4689 }
4690
4691 free_irq(host_set->irq, host_set);
4692
4693 for (i = 0; i < host_set->n_ports; i++) {
4694 ap = host_set->ports[i];
4695
4696 ata_scsi_release(ap->host);
4697
4698 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4699 struct ata_ioports *ioaddr = &ap->ioaddr;
4700
4701 if (ioaddr->cmd_addr == 0x1f0)
4702 release_region(0x1f0, 8);
4703 else if (ioaddr->cmd_addr == 0x170)
4704 release_region(0x170, 8);
4705 }
4706
4707 scsi_host_put(ap->host);
4708 }
4709
4710 if (host_set->ops->host_stop)
4711 host_set->ops->host_stop(host_set);
4712
4713 kfree(host_set);
4714}
4715
4716/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 * ata_scsi_release - SCSI layer callback hook for host unload
4718 * @host: libata host to be unloaded
4719 *
4720 * Performs all duties necessary to shut down a libata port...
4721 * Kill port kthread, disable port, and release resources.
4722 *
4723 * LOCKING:
4724 * Inherited from SCSI layer.
4725 *
4726 * RETURNS:
4727 * One.
4728 */
4729
4730int ata_scsi_release(struct Scsi_Host *host)
4731{
4732 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
Tejun Heod9572b12006-03-01 16:09:35 +09004733 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734
4735 DPRINTK("ENTER\n");
4736
4737 ap->ops->port_disable(ap);
4738 ata_host_remove(ap, 0);
Tejun Heod9572b12006-03-01 16:09:35 +09004739 for (i = 0; i < ATA_MAX_DEVICES; i++)
4740 kfree(ap->device[i].id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
4742 DPRINTK("EXIT\n");
4743 return 1;
4744}
4745
4746/**
4747 * ata_std_ports - initialize ioaddr with standard port offsets.
4748 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004749 *
4750 * Utility function which initializes data_addr, error_addr,
4751 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4752 * device_addr, status_addr, and command_addr to standard offsets
4753 * relative to cmd_addr.
4754 *
4755 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 */
Edward Falk0baab862005-06-02 18:17:13 -04004757
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758void ata_std_ports(struct ata_ioports *ioaddr)
4759{
4760 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4761 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4762 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4763 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4764 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4765 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4766 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4767 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4768 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4769 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4770}
4771
Edward Falk0baab862005-06-02 18:17:13 -04004772
Jeff Garzik374b1872005-08-30 05:42:52 -04004773#ifdef CONFIG_PCI
4774
4775void ata_pci_host_stop (struct ata_host_set *host_set)
4776{
4777 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4778
4779 pci_iounmap(pdev, host_set->mmio_base);
4780}
4781
Edward Falk0baab862005-06-02 18:17:13 -04004782/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 * ata_pci_remove_one - PCI layer callback for device removal
4784 * @pdev: PCI device that was removed
4785 *
4786 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004787 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 * Handle this by unregistering all objects associated
4789 * with this PCI device. Free those objects. Then finally
4790 * release PCI resources and disable device.
4791 *
4792 * LOCKING:
4793 * Inherited from PCI layer (may sleep).
4794 */
4795
4796void ata_pci_remove_one (struct pci_dev *pdev)
4797{
4798 struct device *dev = pci_dev_to_dev(pdev);
4799 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800
Alan Cox17b14452005-09-15 15:44:00 +01004801 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 pci_release_regions(pdev);
4803 pci_disable_device(pdev);
4804 dev_set_drvdata(dev, NULL);
4805}
4806
4807/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004808int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809{
4810 unsigned long tmp = 0;
4811
4812 switch (bits->width) {
4813 case 1: {
4814 u8 tmp8 = 0;
4815 pci_read_config_byte(pdev, bits->reg, &tmp8);
4816 tmp = tmp8;
4817 break;
4818 }
4819 case 2: {
4820 u16 tmp16 = 0;
4821 pci_read_config_word(pdev, bits->reg, &tmp16);
4822 tmp = tmp16;
4823 break;
4824 }
4825 case 4: {
4826 u32 tmp32 = 0;
4827 pci_read_config_dword(pdev, bits->reg, &tmp32);
4828 tmp = tmp32;
4829 break;
4830 }
4831
4832 default:
4833 return -EINVAL;
4834 }
4835
4836 tmp &= bits->mask;
4837
4838 return (tmp == bits->val) ? 1 : 0;
4839}
Jens Axboe9b847542006-01-06 09:28:07 +01004840
4841int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4842{
4843 pci_save_state(pdev);
4844 pci_disable_device(pdev);
4845 pci_set_power_state(pdev, PCI_D3hot);
4846 return 0;
4847}
4848
4849int ata_pci_device_resume(struct pci_dev *pdev)
4850{
4851 pci_set_power_state(pdev, PCI_D0);
4852 pci_restore_state(pdev);
4853 pci_enable_device(pdev);
4854 pci_set_master(pdev);
4855 return 0;
4856}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857#endif /* CONFIG_PCI */
4858
4859
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860static int __init ata_init(void)
4861{
4862 ata_wq = create_workqueue("ata");
4863 if (!ata_wq)
4864 return -ENOMEM;
4865
4866 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4867 return 0;
4868}
4869
4870static void __exit ata_exit(void)
4871{
4872 destroy_workqueue(ata_wq);
4873}
4874
4875module_init(ata_init);
4876module_exit(ata_exit);
4877
Jeff Garzik67846b32005-10-05 02:58:32 -04004878static unsigned long ratelimit_time;
4879static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4880
4881int ata_ratelimit(void)
4882{
4883 int rc;
4884 unsigned long flags;
4885
4886 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4887
4888 if (time_after(jiffies, ratelimit_time)) {
4889 rc = 1;
4890 ratelimit_time = jiffies + (HZ/5);
4891 } else
4892 rc = 0;
4893
4894 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4895
4896 return rc;
4897}
4898
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899/*
4900 * libata is essentially a library of internal helper functions for
4901 * low-level ATA host controller drivers. As such, the API/ABI is
4902 * likely to change as new drivers are added and updated.
4903 * Do not depend on ABI/API stability.
4904 */
4905
4906EXPORT_SYMBOL_GPL(ata_std_bios_param);
4907EXPORT_SYMBOL_GPL(ata_std_ports);
4908EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01004909EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910EXPORT_SYMBOL_GPL(ata_sg_init);
4911EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo76014422006-02-11 15:13:49 +09004912EXPORT_SYMBOL_GPL(__ata_qc_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4914EXPORT_SYMBOL_GPL(ata_eng_timeout);
4915EXPORT_SYMBOL_GPL(ata_tf_load);
4916EXPORT_SYMBOL_GPL(ata_tf_read);
4917EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4918EXPORT_SYMBOL_GPL(ata_std_dev_select);
4919EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4920EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4921EXPORT_SYMBOL_GPL(ata_check_status);
4922EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923EXPORT_SYMBOL_GPL(ata_exec_command);
4924EXPORT_SYMBOL_GPL(ata_port_start);
4925EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004926EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927EXPORT_SYMBOL_GPL(ata_interrupt);
4928EXPORT_SYMBOL_GPL(ata_qc_prep);
4929EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4930EXPORT_SYMBOL_GPL(ata_bmdma_start);
4931EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4932EXPORT_SYMBOL_GPL(ata_bmdma_status);
4933EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4934EXPORT_SYMBOL_GPL(ata_port_probe);
4935EXPORT_SYMBOL_GPL(sata_phy_reset);
4936EXPORT_SYMBOL_GPL(__sata_phy_reset);
4937EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09004938EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004939EXPORT_SYMBOL_GPL(ata_std_softreset);
4940EXPORT_SYMBOL_GPL(sata_std_hardreset);
4941EXPORT_SYMBOL_GPL(ata_std_postreset);
4942EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09004943EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04004945EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09004946EXPORT_SYMBOL_GPL(ata_busy_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4948EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Tejun Heof29841e2006-02-10 15:10:48 +09004949EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950EXPORT_SYMBOL_GPL(ata_scsi_error);
4951EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4952EXPORT_SYMBOL_GPL(ata_scsi_release);
4953EXPORT_SYMBOL_GPL(ata_host_intr);
4954EXPORT_SYMBOL_GPL(ata_dev_classify);
Tejun Heo6a62a042006-02-13 10:02:46 +09004955EXPORT_SYMBOL_GPL(ata_id_string);
4956EXPORT_SYMBOL_GPL(ata_id_c_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004958EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
4959EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
Alan Cox1bc4ccf2006-01-09 17:18:14 +00004961EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04004962EXPORT_SYMBOL_GPL(ata_timing_compute);
4963EXPORT_SYMBOL_GPL(ata_timing_merge);
4964
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965#ifdef CONFIG_PCI
4966EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04004967EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4969EXPORT_SYMBOL_GPL(ata_pci_init_one);
4970EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01004971EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
4972EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01004974
4975EXPORT_SYMBOL_GPL(ata_device_suspend);
4976EXPORT_SYMBOL_GPL(ata_device_resume);
4977EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
4978EXPORT_SYMBOL_GPL(ata_scsi_device_resume);