blob: 8424bd9ee26d718000c8b5364bbfe1b4344f066b [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
Albert Lee59a10b12005-10-12 15:09:42 +080064static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
Albert Lee8bf62ec2005-05-12 15:29:42 -040065static void ata_dev_init_params(struct ata_port *ap, 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070081MODULE_AUTHOR("Jeff Garzik");
82MODULE_DESCRIPTION("Library module for ATA devices");
83MODULE_LICENSE("GPL");
84MODULE_VERSION(DRV_VERSION);
85
Edward Falk0baab862005-06-02 18:17:13 -040086
87/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
89 * @tf: Taskfile to convert
90 * @fis: Buffer into which data will output
91 * @pmp: Port multiplier port
92 *
93 * Converts a standard ATA taskfile to a Serial ATA
94 * FIS structure (Register - Host to Device).
95 *
96 * LOCKING:
97 * Inherited from caller.
98 */
99
Jeff Garzik057ace52005-10-22 14:27:05 -0400100void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 fis[0] = 0x27; /* Register - Host to Device FIS */
103 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
104 bit 7 indicates Command FIS */
105 fis[2] = tf->command;
106 fis[3] = tf->feature;
107
108 fis[4] = tf->lbal;
109 fis[5] = tf->lbam;
110 fis[6] = tf->lbah;
111 fis[7] = tf->device;
112
113 fis[8] = tf->hob_lbal;
114 fis[9] = tf->hob_lbam;
115 fis[10] = tf->hob_lbah;
116 fis[11] = tf->hob_feature;
117
118 fis[12] = tf->nsect;
119 fis[13] = tf->hob_nsect;
120 fis[14] = 0;
121 fis[15] = tf->ctl;
122
123 fis[16] = 0;
124 fis[17] = 0;
125 fis[18] = 0;
126 fis[19] = 0;
127}
128
129/**
130 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
131 * @fis: Buffer from which data will be input
132 * @tf: Taskfile to output
133 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500134 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 *
136 * LOCKING:
137 * Inherited from caller.
138 */
139
Jeff Garzik057ace52005-10-22 14:27:05 -0400140void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 tf->command = fis[2]; /* status */
143 tf->feature = fis[3]; /* error */
144
145 tf->lbal = fis[4];
146 tf->lbam = fis[5];
147 tf->lbah = fis[6];
148 tf->device = fis[7];
149
150 tf->hob_lbal = fis[8];
151 tf->hob_lbam = fis[9];
152 tf->hob_lbah = fis[10];
153
154 tf->nsect = fis[12];
155 tf->hob_nsect = fis[13];
156}
157
Albert Lee8cbd6df2005-10-12 15:06:27 +0800158static const u8 ata_rw_cmds[] = {
159 /* pio multi */
160 ATA_CMD_READ_MULTI,
161 ATA_CMD_WRITE_MULTI,
162 ATA_CMD_READ_MULTI_EXT,
163 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100164 0,
165 0,
166 0,
167 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800168 /* pio */
169 ATA_CMD_PIO_READ,
170 ATA_CMD_PIO_WRITE,
171 ATA_CMD_PIO_READ_EXT,
172 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100173 0,
174 0,
175 0,
176 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800177 /* dma */
178 ATA_CMD_READ,
179 ATA_CMD_WRITE,
180 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100181 ATA_CMD_WRITE_EXT,
182 0,
183 0,
184 0,
185 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800186};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800189 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
190 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800192 * Examine the device configuration and tf->flags to calculate
193 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 *
195 * LOCKING:
196 * caller.
197 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100198int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800200 struct ata_taskfile *tf = &qc->tf;
201 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100202 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100204 int index, fua, lba48, write;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800205
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100206 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800207 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
208 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Albert Lee8cbd6df2005-10-12 15:06:27 +0800210 if (dev->flags & ATA_DFLAG_PIO) {
211 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100212 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000213 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
214 /* Unable to use DMA due to host limitation */
215 tf->protocol = ATA_PROT_PIO;
216 index = dev->multi_count ? 0 : 4;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800217 } else {
218 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100219 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100222 cmd = ata_rw_cmds[index + fua + lba48 + write];
223 if (cmd) {
224 tf->command = cmd;
225 return 0;
226 }
227 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100230static const char * const xfer_mode_str[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 "UDMA/16",
232 "UDMA/25",
233 "UDMA/33",
234 "UDMA/44",
235 "UDMA/66",
236 "UDMA/100",
237 "UDMA/133",
238 "UDMA7",
239 "MWDMA0",
240 "MWDMA1",
241 "MWDMA2",
242 "PIO0",
243 "PIO1",
244 "PIO2",
245 "PIO3",
246 "PIO4",
247};
248
249/**
250 * ata_udma_string - convert UDMA bit offset to string
251 * @mask: mask of bits supported; only highest bit counts.
252 *
253 * Determine string which represents the highest speed
254 * (highest bit in @udma_mask).
255 *
256 * LOCKING:
257 * None.
258 *
259 * RETURNS:
260 * Constant C string representing highest speed listed in
261 * @udma_mask, or the constant C string "<n/a>".
262 */
263
264static const char *ata_mode_string(unsigned int mask)
265{
266 int i;
267
268 for (i = 7; i >= 0; i--)
269 if (mask & (1 << i))
270 goto out;
271 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
272 if (mask & (1 << i))
273 goto out;
274 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
275 if (mask & (1 << i))
276 goto out;
277
278 return "<n/a>";
279
280out:
281 return xfer_mode_str[i];
282}
283
284/**
285 * ata_pio_devchk - PATA device presence detection
286 * @ap: ATA channel to examine
287 * @device: Device to examine (starting at zero)
288 *
289 * This technique was originally described in
290 * Hale Landis's ATADRVR (www.ata-atapi.com), and
291 * later found its way into the ATA/ATAPI spec.
292 *
293 * Write a pattern to the ATA shadow registers,
294 * and if a device is present, it will respond by
295 * correctly storing and echoing back the
296 * ATA shadow register contents.
297 *
298 * LOCKING:
299 * caller.
300 */
301
302static unsigned int ata_pio_devchk(struct ata_port *ap,
303 unsigned int device)
304{
305 struct ata_ioports *ioaddr = &ap->ioaddr;
306 u8 nsect, lbal;
307
308 ap->ops->dev_select(ap, device);
309
310 outb(0x55, ioaddr->nsect_addr);
311 outb(0xaa, ioaddr->lbal_addr);
312
313 outb(0xaa, ioaddr->nsect_addr);
314 outb(0x55, ioaddr->lbal_addr);
315
316 outb(0x55, ioaddr->nsect_addr);
317 outb(0xaa, ioaddr->lbal_addr);
318
319 nsect = inb(ioaddr->nsect_addr);
320 lbal = inb(ioaddr->lbal_addr);
321
322 if ((nsect == 0x55) && (lbal == 0xaa))
323 return 1; /* we found a device */
324
325 return 0; /* nothing found */
326}
327
328/**
329 * ata_mmio_devchk - PATA device presence detection
330 * @ap: ATA channel to examine
331 * @device: Device to examine (starting at zero)
332 *
333 * This technique was originally described in
334 * Hale Landis's ATADRVR (www.ata-atapi.com), and
335 * later found its way into the ATA/ATAPI spec.
336 *
337 * Write a pattern to the ATA shadow registers,
338 * and if a device is present, it will respond by
339 * correctly storing and echoing back the
340 * ATA shadow register contents.
341 *
342 * LOCKING:
343 * caller.
344 */
345
346static unsigned int ata_mmio_devchk(struct ata_port *ap,
347 unsigned int device)
348{
349 struct ata_ioports *ioaddr = &ap->ioaddr;
350 u8 nsect, lbal;
351
352 ap->ops->dev_select(ap, device);
353
354 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
355 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
356
357 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
358 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
359
360 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
361 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
362
363 nsect = readb((void __iomem *) ioaddr->nsect_addr);
364 lbal = readb((void __iomem *) ioaddr->lbal_addr);
365
366 if ((nsect == 0x55) && (lbal == 0xaa))
367 return 1; /* we found a device */
368
369 return 0; /* nothing found */
370}
371
372/**
373 * ata_devchk - PATA device presence detection
374 * @ap: ATA channel to examine
375 * @device: Device to examine (starting at zero)
376 *
377 * Dispatch ATA device presence detection, depending
378 * on whether we are using PIO or MMIO to talk to the
379 * ATA shadow registers.
380 *
381 * LOCKING:
382 * caller.
383 */
384
385static unsigned int ata_devchk(struct ata_port *ap,
386 unsigned int device)
387{
388 if (ap->flags & ATA_FLAG_MMIO)
389 return ata_mmio_devchk(ap, device);
390 return ata_pio_devchk(ap, device);
391}
392
393/**
394 * ata_dev_classify - determine device type based on ATA-spec signature
395 * @tf: ATA taskfile register set for device to be identified
396 *
397 * Determine from taskfile register contents whether a device is
398 * ATA or ATAPI, as per "Signature and persistence" section
399 * of ATA/PI spec (volume 1, sect 5.14).
400 *
401 * LOCKING:
402 * None.
403 *
404 * RETURNS:
405 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
406 * the event of failure.
407 */
408
Jeff Garzik057ace52005-10-22 14:27:05 -0400409unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 /* Apple's open source Darwin code hints that some devices only
412 * put a proper signature into the LBA mid/high registers,
413 * So, we only check those. It's sufficient for uniqueness.
414 */
415
416 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
417 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
418 DPRINTK("found ATA device by sig\n");
419 return ATA_DEV_ATA;
420 }
421
422 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
423 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
424 DPRINTK("found ATAPI device by sig\n");
425 return ATA_DEV_ATAPI;
426 }
427
428 DPRINTK("unknown device\n");
429 return ATA_DEV_UNKNOWN;
430}
431
432/**
433 * ata_dev_try_classify - Parse returned ATA device signature
434 * @ap: ATA channel to examine
435 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900436 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 *
438 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
439 * an ATA/ATAPI-defined set of values is placed in the ATA
440 * shadow registers, indicating the results of device detection
441 * and diagnostics.
442 *
443 * Select the ATA device, and read the values from the ATA shadow
444 * registers. Then parse according to the Error register value,
445 * and the spec-defined values examined by ata_dev_classify().
446 *
447 * LOCKING:
448 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900449 *
450 * RETURNS:
451 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 */
453
Tejun Heob4dc7622006-01-24 17:05:22 +0900454static unsigned int
455ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct ata_taskfile tf;
458 unsigned int class;
459 u8 err;
460
461 ap->ops->dev_select(ap, device);
462
463 memset(&tf, 0, sizeof(tf));
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400466 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900467 if (r_err)
468 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 /* see if device passed diags */
471 if (err == 1)
472 /* do nothing */ ;
473 else if ((device == 0) && (err == 0x81))
474 /* do nothing */ ;
475 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900476 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Tejun Heob4dc7622006-01-24 17:05:22 +0900478 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900482 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900484 return ATA_DEV_NONE;
485 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488/**
489 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
490 * @id: IDENTIFY DEVICE results we will examine
491 * @s: string into which data is output
492 * @ofs: offset into identify device page
493 * @len: length of string to return. must be an even number.
494 *
495 * The strings in the IDENTIFY DEVICE page are broken up into
496 * 16-bit chunks. Run through the string, and output each
497 * 8-bit chunk linearly, regardless of platform.
498 *
499 * LOCKING:
500 * caller.
501 */
502
Jeff Garzik057ace52005-10-22 14:27:05 -0400503void ata_dev_id_string(const u16 *id, unsigned char *s,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 unsigned int ofs, unsigned int len)
505{
506 unsigned int c;
507
508 while (len > 0) {
509 c = id[ofs] >> 8;
510 *s = c;
511 s++;
512
513 c = id[ofs] & 0xff;
514 *s = c;
515 s++;
516
517 ofs++;
518 len -= 2;
519 }
520}
521
Edward Falk0baab862005-06-02 18:17:13 -0400522
523/**
524 * ata_noop_dev_select - Select device 0/1 on ATA bus
525 * @ap: ATA channel to manipulate
526 * @device: ATA device (numbered from zero) to select
527 *
528 * This function performs no actual function.
529 *
530 * May be used as the dev_select() entry in ata_port_operations.
531 *
532 * LOCKING:
533 * caller.
534 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
536{
537}
538
Edward Falk0baab862005-06-02 18:17:13 -0400539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540/**
541 * ata_std_dev_select - Select device 0/1 on ATA bus
542 * @ap: ATA channel to manipulate
543 * @device: ATA device (numbered from zero) to select
544 *
545 * Use the method defined in the ATA specification to
546 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400547 * ATA channel. Works with both PIO and MMIO.
548 *
549 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 *
551 * LOCKING:
552 * caller.
553 */
554
555void ata_std_dev_select (struct ata_port *ap, unsigned int device)
556{
557 u8 tmp;
558
559 if (device == 0)
560 tmp = ATA_DEVICE_OBS;
561 else
562 tmp = ATA_DEVICE_OBS | ATA_DEV1;
563
564 if (ap->flags & ATA_FLAG_MMIO) {
565 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
566 } else {
567 outb(tmp, ap->ioaddr.device_addr);
568 }
569 ata_pause(ap); /* needed; also flushes, for mmio */
570}
571
572/**
573 * ata_dev_select - Select device 0/1 on ATA bus
574 * @ap: ATA channel to manipulate
575 * @device: ATA device (numbered from zero) to select
576 * @wait: non-zero to wait for Status register BSY bit to clear
577 * @can_sleep: non-zero if context allows sleeping
578 *
579 * Use the method defined in the ATA specification to
580 * make either device 0, or device 1, active on the
581 * ATA channel.
582 *
583 * This is a high-level version of ata_std_dev_select(),
584 * which additionally provides the services of inserting
585 * the proper pauses and status polling, where needed.
586 *
587 * LOCKING:
588 * caller.
589 */
590
591void ata_dev_select(struct ata_port *ap, unsigned int device,
592 unsigned int wait, unsigned int can_sleep)
593{
594 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
595 ap->id, device, wait);
596
597 if (wait)
598 ata_wait_idle(ap);
599
600 ap->ops->dev_select(ap, device);
601
602 if (wait) {
603 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
604 msleep(150);
605 ata_wait_idle(ap);
606 }
607}
608
609/**
610 * ata_dump_id - IDENTIFY DEVICE info debugging output
611 * @dev: Device whose IDENTIFY DEVICE page we will dump
612 *
613 * Dump selected 16-bit words from a detected device's
614 * IDENTIFY PAGE page.
615 *
616 * LOCKING:
617 * caller.
618 */
619
Jeff Garzik057ace52005-10-22 14:27:05 -0400620static inline void ata_dump_id(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 DPRINTK("49==0x%04x "
623 "53==0x%04x "
624 "63==0x%04x "
625 "64==0x%04x "
626 "75==0x%04x \n",
627 dev->id[49],
628 dev->id[53],
629 dev->id[63],
630 dev->id[64],
631 dev->id[75]);
632 DPRINTK("80==0x%04x "
633 "81==0x%04x "
634 "82==0x%04x "
635 "83==0x%04x "
636 "84==0x%04x \n",
637 dev->id[80],
638 dev->id[81],
639 dev->id[82],
640 dev->id[83],
641 dev->id[84]);
642 DPRINTK("88==0x%04x "
643 "93==0x%04x\n",
644 dev->id[88],
645 dev->id[93]);
646}
647
Alan Cox11e29e22005-10-21 18:46:32 -0400648/*
649 * Compute the PIO modes available for this device. This is not as
650 * trivial as it seems if we must consider early devices correctly.
651 *
652 * FIXME: pre IDE drive timing (do we care ?).
653 */
654
Jeff Garzik057ace52005-10-22 14:27:05 -0400655static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -0400656{
657 u16 modes;
658
Alan Coxffa29452006-01-09 17:14:40 +0000659 /* Usual case. Word 53 indicates word 64 is valid */
660 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
Alan Cox11e29e22005-10-21 18:46:32 -0400661 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
662 modes <<= 3;
663 modes |= 0x7;
664 return modes;
665 }
666
Alan Coxffa29452006-01-09 17:14:40 +0000667 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
668 number for the maximum. Turn it into a mask and return it */
669 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
Alan Cox11e29e22005-10-21 18:46:32 -0400670 return modes;
Alan Coxffa29452006-01-09 17:14:40 +0000671 /* But wait.. there's more. Design your standards by committee and
672 you too can get a free iordy field to process. However its the
673 speeds not the modes that are supported... Note drivers using the
674 timing API will get this right anyway */
Alan Cox11e29e22005-10-21 18:46:32 -0400675}
676
Tejun Heo95064372006-01-23 13:09:37 +0900677static inline void
678ata_queue_packet_task(struct ata_port *ap)
679{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900680 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
681 queue_work(ata_wq, &ap->packet_task);
Tejun Heo95064372006-01-23 13:09:37 +0900682}
683
684static inline void
685ata_queue_pio_task(struct ata_port *ap)
686{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900687 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
688 queue_work(ata_wq, &ap->pio_task);
Tejun Heo95064372006-01-23 13:09:37 +0900689}
690
691static inline void
692ata_queue_delayed_pio_task(struct ata_port *ap, unsigned long delay)
693{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900694 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
695 queue_delayed_work(ata_wq, &ap->pio_task, delay);
696}
697
698/**
699 * ata_flush_pio_tasks - Flush pio_task and packet_task
700 * @ap: the target ata_port
701 *
702 * After this function completes, pio_task and packet_task are
703 * guranteed not to be running or scheduled.
704 *
705 * LOCKING:
706 * Kernel thread context (may sleep)
707 */
708
709static void ata_flush_pio_tasks(struct ata_port *ap)
710{
711 int tmp = 0;
712 unsigned long flags;
713
714 DPRINTK("ENTER\n");
715
716 spin_lock_irqsave(&ap->host_set->lock, flags);
717 ap->flags |= ATA_FLAG_FLUSH_PIO_TASK;
718 spin_unlock_irqrestore(&ap->host_set->lock, flags);
719
720 DPRINTK("flush #1\n");
721 flush_workqueue(ata_wq);
722
723 /*
724 * At this point, if a task is running, it's guaranteed to see
725 * the FLUSH flag; thus, it will never queue pio tasks again.
726 * Cancel and flush.
727 */
728 tmp |= cancel_delayed_work(&ap->pio_task);
729 tmp |= cancel_delayed_work(&ap->packet_task);
730 if (!tmp) {
731 DPRINTK("flush #2\n");
732 flush_workqueue(ata_wq);
733 }
734
735 spin_lock_irqsave(&ap->host_set->lock, flags);
736 ap->flags &= ~ATA_FLAG_FLUSH_PIO_TASK;
737 spin_unlock_irqrestore(&ap->host_set->lock, flags);
738
739 DPRINTK("EXIT\n");
Tejun Heo95064372006-01-23 13:09:37 +0900740}
741
Tejun Heo77853bf2006-01-23 13:09:36 +0900742void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +0900743{
Tejun Heo77853bf2006-01-23 13:09:36 +0900744 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900745
Tejun Heo77853bf2006-01-23 13:09:36 +0900746 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900747 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900748}
749
750/**
751 * ata_exec_internal - execute libata internal command
752 * @ap: Port to which the command is sent
753 * @dev: Device to which the command is sent
754 * @tf: Taskfile registers for the command and the result
755 * @dma_dir: Data tranfer direction of the command
756 * @buf: Data buffer of the command
757 * @buflen: Length of data buffer
758 *
759 * Executes libata internal command with timeout. @tf contains
760 * command on entry and result on return. Timeout and error
761 * conditions are reported via return value. No recovery action
762 * is taken after a command times out. It's caller's duty to
763 * clean up after timeout.
764 *
765 * LOCKING:
766 * None. Should be called with kernel context, might sleep.
767 */
768
769static unsigned
770ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
771 struct ata_taskfile *tf,
772 int dma_dir, void *buf, unsigned int buflen)
773{
774 u8 command = tf->command;
775 struct ata_queued_cmd *qc;
776 DECLARE_COMPLETION(wait);
777 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +0900778 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900779
780 spin_lock_irqsave(&ap->host_set->lock, flags);
781
782 qc = ata_qc_new_init(ap, dev);
783 BUG_ON(qc == NULL);
784
785 qc->tf = *tf;
786 qc->dma_dir = dma_dir;
787 if (dma_dir != DMA_NONE) {
788 ata_sg_init_one(qc, buf, buflen);
789 qc->nsect = buflen / ATA_SECT_SIZE;
790 }
791
Tejun Heo77853bf2006-01-23 13:09:36 +0900792 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900793 qc->complete_fn = ata_qc_complete_internal;
794
Tejun Heo9a3d9eb2006-01-23 13:09:36 +0900795 qc->err_mask = ata_qc_issue(qc);
796 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +0900797 ata_qc_complete(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900798
799 spin_unlock_irqrestore(&ap->host_set->lock, flags);
800
801 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
802 spin_lock_irqsave(&ap->host_set->lock, flags);
803
804 /* We're racing with irq here. If we lose, the
805 * following test prevents us from completing the qc
806 * again. If completion irq occurs after here but
807 * before the caller cleans up, it will result in a
808 * spurious interrupt. We can live with that.
809 */
Tejun Heo77853bf2006-01-23 13:09:36 +0900810 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +0900811 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900812 ata_qc_complete(qc);
813 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
814 ap->id, command);
815 }
816
817 spin_unlock_irqrestore(&ap->host_set->lock, flags);
818 }
819
Tejun Heo77853bf2006-01-23 13:09:36 +0900820 *tf = qc->tf;
821 err_mask = qc->err_mask;
822
823 ata_qc_free(qc);
824
825 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900826}
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +0000829 * ata_pio_need_iordy - check if iordy needed
830 * @adev: ATA device
831 *
832 * Check if the current speed of the device requires IORDY. Used
833 * by various controllers for chip configuration.
834 */
835
836unsigned int ata_pio_need_iordy(const struct ata_device *adev)
837{
838 int pio;
839 int speed = adev->pio_mode - XFER_PIO_0;
840
841 if (speed < 2)
842 return 0;
843 if (speed > 2)
844 return 1;
845
846 /* If we have no drive specific rule, then PIO 2 is non IORDY */
847
848 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
849 pio = adev->id[ATA_ID_EIDE_PIO];
850 /* Is the speed faster than the drive allows non IORDY ? */
851 if (pio) {
852 /* This is cycle times not frequency - watch the logic! */
853 if (pio > 240) /* PIO2 is 240nS per cycle */
854 return 1;
855 return 0;
856 }
857 }
858 return 0;
859}
860
861/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 * ata_dev_identify - obtain IDENTIFY x DEVICE page
863 * @ap: port on which device we wish to probe resides
864 * @device: device bus address, starting at zero
865 *
866 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
867 * command, and read back the 512-byte device information page.
868 * The device information page is fed to us via the standard
869 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
870 * using standard PIO-IN paths)
871 *
872 * After reading the device information page, we use several
873 * bits of information from it to initialize data structures
874 * that will be used during the lifetime of the ata_device.
875 * Other data from the info page is used to disqualify certain
876 * older ATA devices we do not wish to support.
877 *
878 * LOCKING:
879 * Inherited from caller. Some functions called by this function
880 * obtain the host_set lock.
881 */
882
883static void ata_dev_identify(struct ata_port *ap, unsigned int device)
884{
885 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ec2005-05-12 15:29:42 -0400886 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 u16 tmp;
888 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 unsigned int using_edd;
Tejun Heoa0123702005-12-13 14:49:31 +0900890 struct ata_taskfile tf;
891 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 int rc;
893
894 if (!ata_dev_present(dev)) {
895 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
896 ap->id, device);
897 return;
898 }
899
900 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
901 using_edd = 0;
902 else
903 using_edd = 1;
904
905 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
906
907 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
908 dev->class == ATA_DEV_NONE);
909
910 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912retry:
Tejun Heoa0123702005-12-13 14:49:31 +0900913 ata_tf_init(ap, &tf, device);
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +0900916 tf.command = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 DPRINTK("do ATA identify\n");
918 } else {
Tejun Heoa0123702005-12-13 14:49:31 +0900919 tf.command = ATA_CMD_ID_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 DPRINTK("do ATAPI identify\n");
921 }
922
Tejun Heoa0123702005-12-13 14:49:31 +0900923 tf.protocol = ATA_PROT_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Tejun Heoa0123702005-12-13 14:49:31 +0900925 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
926 dev->id, sizeof(dev->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Tejun Heoa0123702005-12-13 14:49:31 +0900928 if (err_mask) {
929 if (err_mask & ~AC_ERR_DEV)
930 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /*
933 * arg! EDD works for all test cases, but seems to return
934 * the ATA signature for some ATAPI devices. Until the
935 * reason for this is found and fixed, we fix up the mess
936 * here. If IDENTIFY DEVICE returns command aborted
937 * (as ATAPI devices do), then we issue an
938 * IDENTIFY PACKET DEVICE.
939 *
940 * ATA software reset (SRST, the default) does not appear
941 * to have this problem.
942 */
Albert Lee7c398332005-11-09 13:03:30 +0800943 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
Tejun Heoa0123702005-12-13 14:49:31 +0900944 u8 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (err & ATA_ABORTED) {
946 dev->class = ATA_DEV_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 goto retry;
948 }
949 }
950 goto err_out;
951 }
952
953 swap_buf_le16(dev->id, ATA_ID_WORDS);
954
955 /* print device capabilities */
956 printk(KERN_DEBUG "ata%u: dev %u cfg "
957 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
958 ap->id, device, dev->id[49],
959 dev->id[82], dev->id[83], dev->id[84],
960 dev->id[85], dev->id[86], dev->id[87],
961 dev->id[88]);
962
963 /*
964 * common ATA, ATAPI feature tests
965 */
966
Albert Lee8bf62ec2005-05-12 15:29:42 -0400967 /* we require DMA support (bits 8 of word 49) */
968 if (!ata_id_has_dma(dev->id)) {
969 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 goto err_out_nosup;
971 }
972
973 /* quick-n-dirty find max transfer mode; for printk only */
974 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
975 if (!xfer_modes)
976 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -0400977 if (!xfer_modes)
978 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 ata_dump_id(dev);
981
982 /* ATA-specific feature tests */
983 if (dev->class == ATA_DEV_ATA) {
984 if (!ata_id_is_ata(dev->id)) /* sanity check */
985 goto err_out_nosup;
986
Albert Lee8bf62ec2005-05-12 15:29:42 -0400987 /* get major version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 tmp = dev->id[ATA_ID_MAJOR_VER];
Albert Lee8bf62ec2005-05-12 15:29:42 -0400989 for (major_version = 14; major_version >= 1; major_version--)
990 if (tmp & (1 << major_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 break;
992
Albert Lee8bf62ec2005-05-12 15:29:42 -0400993 /*
994 * The exact sequence expected by certain pre-ATA4 drives is:
995 * SRST RESET
996 * IDENTIFY
997 * INITIALIZE DEVICE PARAMETERS
998 * anything else..
999 * Some drives were very specific about that exact sequence.
1000 */
Albert Lee59a10b12005-10-12 15:09:42 +08001001 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001002 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Albert Lee59a10b12005-10-12 15:09:42 +08001004 /* current CHS translation info (id[53-58]) might be
1005 * changed. reread the identify device info.
1006 */
1007 ata_dev_reread_id(ap, dev);
1008 }
1009
Albert Lee8bf62ec2005-05-12 15:29:42 -04001010 if (ata_id_has_lba(dev->id)) {
1011 dev->flags |= ATA_DFLAG_LBA;
1012
1013 if (ata_id_has_lba48(dev->id)) {
1014 dev->flags |= ATA_DFLAG_LBA48;
1015 dev->n_sectors = ata_id_u64(dev->id, 100);
1016 } else {
1017 dev->n_sectors = ata_id_u32(dev->id, 60);
1018 }
1019
1020 /* print device info to dmesg */
1021 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1022 ap->id, device,
1023 major_version,
1024 ata_mode_string(xfer_modes),
1025 (unsigned long long)dev->n_sectors,
1026 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1027 } else {
1028 /* CHS */
1029
1030 /* Default translation */
1031 dev->cylinders = dev->id[1];
1032 dev->heads = dev->id[3];
1033 dev->sectors = dev->id[6];
1034 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1035
1036 if (ata_id_current_chs_valid(dev->id)) {
1037 /* Current CHS translation is valid. */
1038 dev->cylinders = dev->id[54];
1039 dev->heads = dev->id[55];
1040 dev->sectors = dev->id[56];
1041
1042 dev->n_sectors = ata_id_u32(dev->id, 57);
1043 }
1044
1045 /* print device info to dmesg */
1046 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1047 ap->id, device,
1048 major_version,
1049 ata_mode_string(xfer_modes),
1050 (unsigned long long)dev->n_sectors,
1051 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054
1055 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057
1058 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001059 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (ata_id_is_ata(dev->id)) /* sanity check */
1061 goto err_out_nosup;
1062
1063 rc = atapi_cdb_len(dev->id);
1064 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1065 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1066 goto err_out_nosup;
1067 }
1068 ap->cdb_len = (unsigned int) rc;
1069 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1070
1071 /* print device info to dmesg */
1072 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1073 ap->id, device,
1074 ata_mode_string(xfer_modes));
1075 }
1076
1077 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1078 return;
1079
1080err_out_nosup:
1081 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1082 ap->id, device);
1083err_out:
1084 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1085 DPRINTK("EXIT, err\n");
1086}
1087
Brad Campbell6f2f3812005-05-12 15:07:47 -04001088
Jeff Garzik057ace52005-10-22 14:27:05 -04001089static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001090{
1091 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1092}
1093
1094/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001095 * ata_dev_config - Run device specific handlers & check for SATA->PATA bridges
1096 * @ap: Bus
1097 * @i: Device
Brad Campbell6f2f3812005-05-12 15:07:47 -04001098 *
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001099 * LOCKING:
Brad Campbell6f2f3812005-05-12 15:07:47 -04001100 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001101
Brad Campbell6f2f3812005-05-12 15:07:47 -04001102void ata_dev_config(struct ata_port *ap, unsigned int i)
1103{
1104 /* limit bridge transfers to udma5, 200 sectors */
1105 if (ata_dev_knobble(ap)) {
1106 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1107 ap->id, ap->device->devno);
1108 ap->udma_mask &= ATA_UDMA5;
1109 ap->host->max_sectors = ATA_MAX_SECTORS;
1110 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
Alan Cox9d824d02006-01-17 20:51:55 +00001111 ap->device[i].flags |= ATA_DFLAG_LOCK_SECTORS;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001112 }
1113
1114 if (ap->ops->dev_config)
1115 ap->ops->dev_config(ap, &ap->device[i]);
1116}
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118/**
1119 * ata_bus_probe - Reset and probe ATA bus
1120 * @ap: Bus to probe
1121 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001122 * Master ATA bus probing function. Initiates a hardware-dependent
1123 * bus reset, then attempts to identify any devices found on
1124 * the bus.
1125 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001127 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 *
1129 * RETURNS:
1130 * Zero on success, non-zero on error.
1131 */
1132
1133static int ata_bus_probe(struct ata_port *ap)
1134{
1135 unsigned int i, found = 0;
1136
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001137 if (ap->ops->probe_reset) {
1138 unsigned int classes[ATA_MAX_DEVICES];
1139 int rc;
1140
1141 ata_port_probe(ap);
1142
1143 rc = ap->ops->probe_reset(ap, classes);
1144 if (rc == 0) {
1145 for (i = 0; i < ATA_MAX_DEVICES; i++)
1146 ap->device[i].class = classes[i];
1147 } else {
1148 printk(KERN_ERR "ata%u: probe reset failed, "
1149 "disabling port\n", ap->id);
1150 ata_port_disable(ap);
1151 }
1152 } else
1153 ap->ops->phy_reset(ap);
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1156 goto err_out;
1157
1158 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1159 ata_dev_identify(ap, i);
1160 if (ata_dev_present(&ap->device[i])) {
1161 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001162 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
1164 }
1165
1166 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1167 goto err_out_disable;
1168
1169 ata_set_mode(ap);
1170 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1171 goto err_out_disable;
1172
1173 return 0;
1174
1175err_out_disable:
1176 ap->ops->port_disable(ap);
1177err_out:
1178 return -1;
1179}
1180
1181/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001182 * ata_port_probe - Mark port as enabled
1183 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001185 * Modify @ap data structure such that the system
1186 * thinks that the entire port is enabled.
1187 *
1188 * LOCKING: host_set lock, or some other form of
1189 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 */
1191
1192void ata_port_probe(struct ata_port *ap)
1193{
1194 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1195}
1196
1197/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001198 * sata_print_link_status - Print SATA link status
1199 * @ap: SATA port to printk link status about
1200 *
1201 * This function prints link speed and status of a SATA link.
1202 *
1203 * LOCKING:
1204 * None.
1205 */
1206static void sata_print_link_status(struct ata_port *ap)
1207{
1208 u32 sstatus, tmp;
1209 const char *speed;
1210
1211 if (!ap->ops->scr_read)
1212 return;
1213
1214 sstatus = scr_read(ap, SCR_STATUS);
1215
1216 if (sata_dev_present(ap)) {
1217 tmp = (sstatus >> 4) & 0xf;
1218 if (tmp & (1 << 0))
1219 speed = "1.5";
1220 else if (tmp & (1 << 1))
1221 speed = "3.0";
1222 else
1223 speed = "<unknown>";
1224 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1225 ap->id, speed, sstatus);
1226 } else {
1227 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1228 ap->id, sstatus);
1229 }
1230}
1231
1232/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001233 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1234 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001236 * This function issues commands to standard SATA Sxxx
1237 * PHY registers, to wake up the phy (and device), and
1238 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 *
1240 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001241 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 *
1243 */
1244void __sata_phy_reset(struct ata_port *ap)
1245{
1246 u32 sstatus;
1247 unsigned long timeout = jiffies + (HZ * 5);
1248
1249 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001250 /* issue phy wake/reset */
1251 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001252 /* Couldn't find anything in SATA I/II specs, but
1253 * AHCI-1.1 10.4.2 says at least 1 ms. */
1254 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
Brett Russcdcca892005-03-28 15:10:27 -05001256 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 /* wait for phy to become ready, if necessary */
1259 do {
1260 msleep(200);
1261 sstatus = scr_read(ap, SCR_STATUS);
1262 if ((sstatus & 0xf) != 1)
1263 break;
1264 } while (time_before(jiffies, timeout));
1265
Tejun Heo3be680b2005-12-19 22:35:02 +09001266 /* print link status */
1267 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001268
Tejun Heo3be680b2005-12-19 22:35:02 +09001269 /* TODO: phy layer with polling, timeouts, etc. */
1270 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001272 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1276 return;
1277
1278 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1279 ata_port_disable(ap);
1280 return;
1281 }
1282
1283 ap->cbl = ATA_CBL_SATA;
1284}
1285
1286/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001287 * sata_phy_reset - Reset SATA bus.
1288 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001290 * This function resets the SATA bus, and then probes
1291 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 *
1293 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001294 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 *
1296 */
1297void sata_phy_reset(struct ata_port *ap)
1298{
1299 __sata_phy_reset(ap);
1300 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1301 return;
1302 ata_bus_reset(ap);
1303}
1304
1305/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001306 * ata_port_disable - Disable port.
1307 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001309 * Modify @ap data structure such that the system
1310 * thinks that the entire port is disabled, and should
1311 * never attempt to probe or communicate with devices
1312 * on this port.
1313 *
1314 * LOCKING: host_set lock, or some other form of
1315 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 */
1317
1318void ata_port_disable(struct ata_port *ap)
1319{
1320 ap->device[0].class = ATA_DEV_NONE;
1321 ap->device[1].class = ATA_DEV_NONE;
1322 ap->flags |= ATA_FLAG_PORT_DISABLED;
1323}
1324
Alan Cox452503f2005-10-21 19:01:32 -04001325/*
1326 * This mode timing computation functionality is ported over from
1327 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1328 */
1329/*
1330 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1331 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1332 * for PIO 5, which is a nonstandard extension and UDMA6, which
1333 * is currently supported only by Maxtor drives.
1334 */
1335
1336static const struct ata_timing ata_timing[] = {
1337
1338 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1339 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1340 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1341 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1342
1343 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1344 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1345 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1346
1347/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1348
1349 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1350 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1351 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1352
1353 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1354 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1355 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1356
1357/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1358 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1359 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1360
1361 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1362 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1363 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1364
1365/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1366
1367 { 0xFF }
1368};
1369
1370#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1371#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1372
1373static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1374{
1375 q->setup = EZ(t->setup * 1000, T);
1376 q->act8b = EZ(t->act8b * 1000, T);
1377 q->rec8b = EZ(t->rec8b * 1000, T);
1378 q->cyc8b = EZ(t->cyc8b * 1000, T);
1379 q->active = EZ(t->active * 1000, T);
1380 q->recover = EZ(t->recover * 1000, T);
1381 q->cycle = EZ(t->cycle * 1000, T);
1382 q->udma = EZ(t->udma * 1000, UT);
1383}
1384
1385void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1386 struct ata_timing *m, unsigned int what)
1387{
1388 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1389 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1390 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1391 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1392 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1393 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1394 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1395 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1396}
1397
1398static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1399{
1400 const struct ata_timing *t;
1401
1402 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001403 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001404 return NULL;
1405 return t;
1406}
1407
1408int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1409 struct ata_timing *t, int T, int UT)
1410{
1411 const struct ata_timing *s;
1412 struct ata_timing p;
1413
1414 /*
1415 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001416 */
Alan Cox452503f2005-10-21 19:01:32 -04001417
1418 if (!(s = ata_timing_find_mode(speed)))
1419 return -EINVAL;
1420
Albert Lee75b1f2f2005-11-16 17:06:18 +08001421 memcpy(t, s, sizeof(*s));
1422
Alan Cox452503f2005-10-21 19:01:32 -04001423 /*
1424 * If the drive is an EIDE drive, it can tell us it needs extended
1425 * PIO/MW_DMA cycle timing.
1426 */
1427
1428 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1429 memset(&p, 0, sizeof(p));
1430 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1431 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1432 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1433 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1434 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1435 }
1436 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1437 }
1438
1439 /*
1440 * Convert the timing to bus clock counts.
1441 */
1442
Albert Lee75b1f2f2005-11-16 17:06:18 +08001443 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001444
1445 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001446 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1447 * S.M.A.R.T * and some other commands. We have to ensure that the
1448 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001449 */
1450
1451 if (speed > XFER_PIO_4) {
1452 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1453 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1454 }
1455
1456 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001457 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001458 */
1459
1460 if (t->act8b + t->rec8b < t->cyc8b) {
1461 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1462 t->rec8b = t->cyc8b - t->act8b;
1463 }
1464
1465 if (t->active + t->recover < t->cycle) {
1466 t->active += (t->cycle - (t->active + t->recover)) / 2;
1467 t->recover = t->cycle - t->active;
1468 }
1469
1470 return 0;
1471}
1472
Jeff Garzik057ace52005-10-22 14:27:05 -04001473static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 unsigned int shift;
1475 u8 base;
1476} xfer_mode_classes[] = {
1477 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1478 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1479 { ATA_SHIFT_PIO, XFER_PIO_0 },
1480};
1481
Arjan van de Ven858119e2006-01-14 13:20:43 -08001482static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 int i;
1485
1486 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1487 if (xfer_mode_classes[i].shift == shift)
1488 return xfer_mode_classes[i].base;
1489
1490 return 0xff;
1491}
1492
1493static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1494{
1495 int ofs, idx;
1496 u8 base;
1497
1498 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1499 return;
1500
1501 if (dev->xfer_shift == ATA_SHIFT_PIO)
1502 dev->flags |= ATA_DFLAG_PIO;
1503
1504 ata_dev_set_xfermode(ap, dev);
1505
1506 base = base_from_shift(dev->xfer_shift);
1507 ofs = dev->xfer_mode - base;
1508 idx = ofs + dev->xfer_shift;
1509 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1510
1511 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1512 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1513
1514 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1515 ap->id, dev->devno, xfer_mode_str[idx]);
1516}
1517
1518static int ata_host_set_pio(struct ata_port *ap)
1519{
1520 unsigned int mask;
1521 int x, i;
1522 u8 base, xfer_mode;
1523
1524 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1525 x = fgb(mask);
1526 if (x < 0) {
1527 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1528 return -1;
1529 }
1530
1531 base = base_from_shift(ATA_SHIFT_PIO);
1532 xfer_mode = base + x;
1533
1534 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1535 (int)base, (int)xfer_mode, mask, x);
1536
1537 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1538 struct ata_device *dev = &ap->device[i];
1539 if (ata_dev_present(dev)) {
1540 dev->pio_mode = xfer_mode;
1541 dev->xfer_mode = xfer_mode;
1542 dev->xfer_shift = ATA_SHIFT_PIO;
1543 if (ap->ops->set_piomode)
1544 ap->ops->set_piomode(ap, dev);
1545 }
1546 }
1547
1548 return 0;
1549}
1550
1551static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1552 unsigned int xfer_shift)
1553{
1554 int i;
1555
1556 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1557 struct ata_device *dev = &ap->device[i];
1558 if (ata_dev_present(dev)) {
1559 dev->dma_mode = xfer_mode;
1560 dev->xfer_mode = xfer_mode;
1561 dev->xfer_shift = xfer_shift;
1562 if (ap->ops->set_dmamode)
1563 ap->ops->set_dmamode(ap, dev);
1564 }
1565 }
1566}
1567
1568/**
1569 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1570 * @ap: port on which timings will be programmed
1571 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001572 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1573 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001575 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 */
1577static void ata_set_mode(struct ata_port *ap)
1578{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001579 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 u8 xfer_mode;
1581 int rc;
1582
1583 /* step 1: always set host PIO timings */
1584 rc = ata_host_set_pio(ap);
1585 if (rc)
1586 goto err_out;
1587
1588 /* step 2: choose the best data xfer mode */
1589 xfer_mode = xfer_shift = 0;
1590 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1591 if (rc)
1592 goto err_out;
1593
1594 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1595 if (xfer_shift != ATA_SHIFT_PIO)
1596 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1597
1598 /* step 4: update devices' xfer mode */
1599 ata_dev_set_mode(ap, &ap->device[0]);
1600 ata_dev_set_mode(ap, &ap->device[1]);
1601
1602 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1603 return;
1604
1605 if (ap->ops->post_set_mode)
1606 ap->ops->post_set_mode(ap);
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return;
1609
1610err_out:
1611 ata_port_disable(ap);
1612}
1613
1614/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001615 * ata_tf_to_host - issue ATA taskfile to host controller
1616 * @ap: port to which command is being issued
1617 * @tf: ATA taskfile register set
1618 *
1619 * Issues ATA taskfile register set to ATA host controller,
1620 * with proper synchronization with interrupt handler and
1621 * other threads.
1622 *
1623 * LOCKING:
1624 * spin_lock_irqsave(host_set lock)
1625 */
1626
1627static inline void ata_tf_to_host(struct ata_port *ap,
1628 const struct ata_taskfile *tf)
1629{
1630 ap->ops->tf_load(ap, tf);
1631 ap->ops->exec_command(ap, tf);
1632}
1633
1634/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 * ata_busy_sleep - sleep until BSY clears, or timeout
1636 * @ap: port containing status register to be polled
1637 * @tmout_pat: impatience timeout
1638 * @tmout: overall timeout
1639 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001640 * Sleep until ATA Status register bit BSY clears,
1641 * or a timeout occurs.
1642 *
1643 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 */
1645
Tejun Heo6f8b9952006-01-24 17:05:21 +09001646unsigned int ata_busy_sleep (struct ata_port *ap,
1647 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 unsigned long timer_start, timeout;
1650 u8 status;
1651
1652 status = ata_busy_wait(ap, ATA_BUSY, 300);
1653 timer_start = jiffies;
1654 timeout = timer_start + tmout_pat;
1655 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1656 msleep(50);
1657 status = ata_busy_wait(ap, ATA_BUSY, 3);
1658 }
1659
1660 if (status & ATA_BUSY)
1661 printk(KERN_WARNING "ata%u is slow to respond, "
1662 "please be patient\n", ap->id);
1663
1664 timeout = timer_start + tmout;
1665 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1666 msleep(50);
1667 status = ata_chk_status(ap);
1668 }
1669
1670 if (status & ATA_BUSY) {
1671 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1672 ap->id, tmout / HZ);
1673 return 1;
1674 }
1675
1676 return 0;
1677}
1678
1679static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1680{
1681 struct ata_ioports *ioaddr = &ap->ioaddr;
1682 unsigned int dev0 = devmask & (1 << 0);
1683 unsigned int dev1 = devmask & (1 << 1);
1684 unsigned long timeout;
1685
1686 /* if device 0 was found in ata_devchk, wait for its
1687 * BSY bit to clear
1688 */
1689 if (dev0)
1690 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1691
1692 /* if device 1 was found in ata_devchk, wait for
1693 * register access, then wait for BSY to clear
1694 */
1695 timeout = jiffies + ATA_TMOUT_BOOT;
1696 while (dev1) {
1697 u8 nsect, lbal;
1698
1699 ap->ops->dev_select(ap, 1);
1700 if (ap->flags & ATA_FLAG_MMIO) {
1701 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1702 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1703 } else {
1704 nsect = inb(ioaddr->nsect_addr);
1705 lbal = inb(ioaddr->lbal_addr);
1706 }
1707 if ((nsect == 1) && (lbal == 1))
1708 break;
1709 if (time_after(jiffies, timeout)) {
1710 dev1 = 0;
1711 break;
1712 }
1713 msleep(50); /* give drive a breather */
1714 }
1715 if (dev1)
1716 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1717
1718 /* is all this really necessary? */
1719 ap->ops->dev_select(ap, 0);
1720 if (dev1)
1721 ap->ops->dev_select(ap, 1);
1722 if (dev0)
1723 ap->ops->dev_select(ap, 0);
1724}
1725
1726/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001727 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1728 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001730 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1731 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 *
1733 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001734 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001735 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 *
1737 */
1738
1739static unsigned int ata_bus_edd(struct ata_port *ap)
1740{
1741 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001742 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 /* set up execute-device-diag (bus reset) taskfile */
1745 /* also, take interrupts to a known state (disabled) */
1746 DPRINTK("execute-device-diag\n");
1747 ata_tf_init(ap, &tf, 0);
1748 tf.ctl |= ATA_NIEN;
1749 tf.command = ATA_CMD_EDD;
1750 tf.protocol = ATA_PROT_NODATA;
1751
1752 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05001753 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05001755 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 /* spec says at least 2ms. but who knows with those
1758 * crazy ATAPI devices...
1759 */
1760 msleep(150);
1761
1762 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1763}
1764
1765static unsigned int ata_bus_softreset(struct ata_port *ap,
1766 unsigned int devmask)
1767{
1768 struct ata_ioports *ioaddr = &ap->ioaddr;
1769
1770 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1771
1772 /* software reset. causes dev0 to be selected */
1773 if (ap->flags & ATA_FLAG_MMIO) {
1774 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1775 udelay(20); /* FIXME: flush */
1776 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1777 udelay(20); /* FIXME: flush */
1778 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1779 } else {
1780 outb(ap->ctl, ioaddr->ctl_addr);
1781 udelay(10);
1782 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1783 udelay(10);
1784 outb(ap->ctl, ioaddr->ctl_addr);
1785 }
1786
1787 /* spec mandates ">= 2ms" before checking status.
1788 * We wait 150ms, because that was the magic delay used for
1789 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1790 * between when the ATA command register is written, and then
1791 * status is checked. Because waiting for "a while" before
1792 * checking status is fine, post SRST, we perform this magic
1793 * delay here as well.
1794 */
1795 msleep(150);
1796
1797 ata_bus_post_reset(ap, devmask);
1798
1799 return 0;
1800}
1801
1802/**
1803 * ata_bus_reset - reset host port and associated ATA channel
1804 * @ap: port to reset
1805 *
1806 * This is typically the first time we actually start issuing
1807 * commands to the ATA channel. We wait for BSY to clear, then
1808 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1809 * result. Determine what devices, if any, are on the channel
1810 * by looking at the device 0/1 error register. Look at the signature
1811 * stored in each device's taskfile registers, to determine if
1812 * the device is ATA or ATAPI.
1813 *
1814 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001815 * PCI/etc. bus probe sem.
1816 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 *
1818 * SIDE EFFECTS:
1819 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1820 */
1821
1822void ata_bus_reset(struct ata_port *ap)
1823{
1824 struct ata_ioports *ioaddr = &ap->ioaddr;
1825 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1826 u8 err;
1827 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1828
1829 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1830
1831 /* determine if device 0/1 are present */
1832 if (ap->flags & ATA_FLAG_SATA_RESET)
1833 dev0 = 1;
1834 else {
1835 dev0 = ata_devchk(ap, 0);
1836 if (slave_possible)
1837 dev1 = ata_devchk(ap, 1);
1838 }
1839
1840 if (dev0)
1841 devmask |= (1 << 0);
1842 if (dev1)
1843 devmask |= (1 << 1);
1844
1845 /* select device 0 again */
1846 ap->ops->dev_select(ap, 0);
1847
1848 /* issue bus reset */
1849 if (ap->flags & ATA_FLAG_SRST)
1850 rc = ata_bus_softreset(ap, devmask);
1851 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1852 /* set up device control */
1853 if (ap->flags & ATA_FLAG_MMIO)
1854 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1855 else
1856 outb(ap->ctl, ioaddr->ctl_addr);
1857 rc = ata_bus_edd(ap);
1858 }
1859
1860 if (rc)
1861 goto err_out;
1862
1863 /*
1864 * determine by signature whether we have ATA or ATAPI devices
1865 */
Tejun Heob4dc7622006-01-24 17:05:22 +09001866 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09001868 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 /* re-enable interrupts */
1871 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1872 ata_irq_on(ap);
1873
1874 /* is double-select really necessary? */
1875 if (ap->device[1].class != ATA_DEV_NONE)
1876 ap->ops->dev_select(ap, 1);
1877 if (ap->device[0].class != ATA_DEV_NONE)
1878 ap->ops->dev_select(ap, 0);
1879
1880 /* if no devices were detected, disable this port */
1881 if ((ap->device[0].class == ATA_DEV_NONE) &&
1882 (ap->device[1].class == ATA_DEV_NONE))
1883 goto err_out;
1884
1885 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1886 /* set up device control for ATA_FLAG_SATA_RESET */
1887 if (ap->flags & ATA_FLAG_MMIO)
1888 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1889 else
1890 outb(ap->ctl, ioaddr->ctl_addr);
1891 }
1892
1893 DPRINTK("EXIT\n");
1894 return;
1895
1896err_out:
1897 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1898 ap->ops->port_disable(ap);
1899
1900 DPRINTK("EXIT\n");
1901}
1902
Tejun Heo7a7921e2006-02-02 18:20:00 +09001903static int sata_phy_resume(struct ata_port *ap)
1904{
1905 unsigned long timeout = jiffies + (HZ * 5);
1906 u32 sstatus;
1907
1908 scr_write_flush(ap, SCR_CONTROL, 0x300);
1909
1910 /* Wait for phy to become ready, if necessary. */
1911 do {
1912 msleep(200);
1913 sstatus = scr_read(ap, SCR_STATUS);
1914 if ((sstatus & 0xf) != 1)
1915 return 0;
1916 } while (time_before(jiffies, timeout));
1917
1918 return -1;
1919}
1920
Tejun Heoc2bd5802006-01-24 17:05:22 +09001921/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09001922 * ata_std_probeinit - initialize probing
1923 * @ap: port to be probed
1924 *
1925 * @ap is about to be probed. Initialize it. This function is
1926 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09001927 *
1928 * NOTE!!! Do not use this function as probeinit if a low level
1929 * driver implements only hardreset. Just pass NULL as probeinit
1930 * in that case. Using this function is probably okay but doing
1931 * so makes reset sequence different from the original
1932 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09001933 */
1934extern void ata_std_probeinit(struct ata_port *ap)
1935{
Tejun Heo3a397462006-02-10 23:58:48 +09001936 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
Tejun Heo8a19ac82006-02-02 18:20:00 +09001937 sata_phy_resume(ap);
Tejun Heo3a397462006-02-10 23:58:48 +09001938 if (sata_dev_present(ap))
1939 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1940 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09001941}
1942
1943/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09001944 * ata_std_softreset - reset host port via ATA SRST
1945 * @ap: port to reset
1946 * @verbose: fail verbosely
1947 * @classes: resulting classes of attached devices
1948 *
1949 * Reset host port using ATA SRST. This function is to be used
1950 * as standard callback for ata_drive_*_reset() functions.
1951 *
1952 * LOCKING:
1953 * Kernel thread context (may sleep)
1954 *
1955 * RETURNS:
1956 * 0 on success, -errno otherwise.
1957 */
1958int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
1959{
1960 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1961 unsigned int devmask = 0, err_mask;
1962 u8 err;
1963
1964 DPRINTK("ENTER\n");
1965
Tejun Heo3a397462006-02-10 23:58:48 +09001966 if (ap->ops->scr_read && !sata_dev_present(ap)) {
1967 classes[0] = ATA_DEV_NONE;
1968 goto out;
1969 }
1970
Tejun Heoc2bd5802006-01-24 17:05:22 +09001971 /* determine if device 0/1 are present */
1972 if (ata_devchk(ap, 0))
1973 devmask |= (1 << 0);
1974 if (slave_possible && ata_devchk(ap, 1))
1975 devmask |= (1 << 1);
1976
Tejun Heoc2bd5802006-01-24 17:05:22 +09001977 /* select device 0 again */
1978 ap->ops->dev_select(ap, 0);
1979
1980 /* issue bus reset */
1981 DPRINTK("about to softreset, devmask=%x\n", devmask);
1982 err_mask = ata_bus_softreset(ap, devmask);
1983 if (err_mask) {
1984 if (verbose)
1985 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
1986 ap->id, err_mask);
1987 else
1988 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
1989 err_mask);
1990 return -EIO;
1991 }
1992
1993 /* determine by signature whether we have ATA or ATAPI devices */
1994 classes[0] = ata_dev_try_classify(ap, 0, &err);
1995 if (slave_possible && err != 0x81)
1996 classes[1] = ata_dev_try_classify(ap, 1, &err);
1997
Tejun Heo3a397462006-02-10 23:58:48 +09001998 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09001999 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2000 return 0;
2001}
2002
2003/**
2004 * sata_std_hardreset - reset host port via SATA phy reset
2005 * @ap: port to reset
2006 * @verbose: fail verbosely
2007 * @class: resulting class of attached device
2008 *
2009 * SATA phy-reset host port using DET bits of SControl register.
2010 * This function is to be used as standard callback for
2011 * ata_drive_*_reset().
2012 *
2013 * LOCKING:
2014 * Kernel thread context (may sleep)
2015 *
2016 * RETURNS:
2017 * 0 on success, -errno otherwise.
2018 */
2019int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2020{
Tejun Heo7a7921e2006-02-02 18:20:00 +09002021 u32 serror;
Tejun Heoc2bd5802006-01-24 17:05:22 +09002022
2023 DPRINTK("ENTER\n");
2024
2025 /* Issue phy wake/reset */
2026 scr_write_flush(ap, SCR_CONTROL, 0x301);
2027
2028 /*
2029 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2030 * 10.4.2 says at least 1 ms.
2031 */
2032 msleep(1);
2033
Tejun Heo7a7921e2006-02-02 18:20:00 +09002034 /* Bring phy back */
2035 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002036
2037 /* Clear SError */
2038 serror = scr_read(ap, SCR_ERROR);
2039 scr_write(ap, SCR_ERROR, serror);
2040
2041 /* TODO: phy layer with polling, timeouts, etc. */
2042 if (!sata_dev_present(ap)) {
2043 *class = ATA_DEV_NONE;
2044 DPRINTK("EXIT, link offline\n");
2045 return 0;
2046 }
2047
2048 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2049 if (verbose)
2050 printk(KERN_ERR "ata%u: COMRESET failed "
2051 "(device not ready)\n", ap->id);
2052 else
2053 DPRINTK("EXIT, device not ready\n");
2054 return -EIO;
2055 }
2056
Tejun Heo3a397462006-02-10 23:58:48 +09002057 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2058
Tejun Heoc2bd5802006-01-24 17:05:22 +09002059 *class = ata_dev_try_classify(ap, 0, NULL);
2060
2061 DPRINTK("EXIT, class=%u\n", *class);
2062 return 0;
2063}
2064
2065/**
2066 * ata_std_postreset - standard postreset callback
2067 * @ap: the target ata_port
2068 * @classes: classes of attached devices
2069 *
2070 * This function is invoked after a successful reset. Note that
2071 * the device might have been reset more than once using
2072 * different reset methods before postreset is invoked.
2073 * postreset is also reponsible for setting cable type.
2074 *
2075 * This function is to be used as standard callback for
2076 * ata_drive_*_reset().
2077 *
2078 * LOCKING:
2079 * Kernel thread context (may sleep)
2080 */
2081void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2082{
2083 DPRINTK("ENTER\n");
2084
2085 /* set cable type */
2086 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2087 ap->cbl = ATA_CBL_SATA;
2088
2089 /* print link status */
2090 if (ap->cbl == ATA_CBL_SATA)
2091 sata_print_link_status(ap);
2092
Tejun Heo3a397462006-02-10 23:58:48 +09002093 /* re-enable interrupts */
2094 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2095 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002096
2097 /* is double-select really necessary? */
2098 if (classes[0] != ATA_DEV_NONE)
2099 ap->ops->dev_select(ap, 1);
2100 if (classes[1] != ATA_DEV_NONE)
2101 ap->ops->dev_select(ap, 0);
2102
Tejun Heo3a397462006-02-10 23:58:48 +09002103 /* bail out if no device is present */
2104 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2105 DPRINTK("EXIT, no device\n");
2106 return;
2107 }
2108
2109 /* set up device control */
2110 if (ap->ioaddr.ctl_addr) {
2111 if (ap->flags & ATA_FLAG_MMIO)
2112 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2113 else
2114 outb(ap->ctl, ap->ioaddr.ctl_addr);
2115 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002116
2117 DPRINTK("EXIT\n");
2118}
2119
2120/**
2121 * ata_std_probe_reset - standard probe reset method
2122 * @ap: prot to perform probe-reset
2123 * @classes: resulting classes of attached devices
2124 *
2125 * The stock off-the-shelf ->probe_reset method.
2126 *
2127 * LOCKING:
2128 * Kernel thread context (may sleep)
2129 *
2130 * RETURNS:
2131 * 0 on success, -errno otherwise.
2132 */
2133int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2134{
2135 ata_reset_fn_t hardreset;
2136
2137 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002138 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002139 hardreset = sata_std_hardreset;
2140
Tejun Heo8a19ac82006-02-02 18:20:00 +09002141 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002142 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002143 ata_std_postreset, classes);
2144}
2145
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002146static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2147 ata_postreset_fn_t postreset,
2148 unsigned int *classes)
2149{
2150 int i, rc;
2151
2152 for (i = 0; i < ATA_MAX_DEVICES; i++)
2153 classes[i] = ATA_DEV_UNKNOWN;
2154
2155 rc = reset(ap, 0, classes);
2156 if (rc)
2157 return rc;
2158
2159 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2160 * is complete and convert all ATA_DEV_UNKNOWN to
2161 * ATA_DEV_NONE.
2162 */
2163 for (i = 0; i < ATA_MAX_DEVICES; i++)
2164 if (classes[i] != ATA_DEV_UNKNOWN)
2165 break;
2166
2167 if (i < ATA_MAX_DEVICES)
2168 for (i = 0; i < ATA_MAX_DEVICES; i++)
2169 if (classes[i] == ATA_DEV_UNKNOWN)
2170 classes[i] = ATA_DEV_NONE;
2171
2172 if (postreset)
2173 postreset(ap, classes);
2174
2175 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2176}
2177
2178/**
2179 * ata_drive_probe_reset - Perform probe reset with given methods
2180 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002181 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002182 * @softreset: softreset method (can be NULL)
2183 * @hardreset: hardreset method (can be NULL)
2184 * @postreset: postreset method (can be NULL)
2185 * @classes: resulting classes of attached devices
2186 *
2187 * Reset the specified port and classify attached devices using
2188 * given methods. This function prefers softreset but tries all
2189 * possible reset sequences to reset and classify devices. This
2190 * function is intended to be used for constructing ->probe_reset
2191 * callback by low level drivers.
2192 *
2193 * Reset methods should follow the following rules.
2194 *
2195 * - Return 0 on sucess, -errno on failure.
2196 * - If classification is supported, fill classes[] with
2197 * recognized class codes.
2198 * - If classification is not supported, leave classes[] alone.
2199 * - If verbose is non-zero, print error message on failure;
2200 * otherwise, shut up.
2201 *
2202 * LOCKING:
2203 * Kernel thread context (may sleep)
2204 *
2205 * RETURNS:
2206 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2207 * if classification fails, and any error code from reset
2208 * methods.
2209 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002210int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002211 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2212 ata_postreset_fn_t postreset, unsigned int *classes)
2213{
2214 int rc = -EINVAL;
2215
Tejun Heo7944ea92006-02-02 18:20:00 +09002216 if (probeinit)
2217 probeinit(ap);
2218
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002219 if (softreset) {
2220 rc = do_probe_reset(ap, softreset, postreset, classes);
2221 if (rc == 0)
2222 return 0;
2223 }
2224
2225 if (!hardreset)
2226 return rc;
2227
2228 rc = do_probe_reset(ap, hardreset, postreset, classes);
2229 if (rc == 0 || rc != -ENODEV)
2230 return rc;
2231
2232 if (softreset)
2233 rc = do_probe_reset(ap, softreset, postreset, classes);
2234
2235 return rc;
2236}
2237
Jeff Garzik057ace52005-10-22 14:27:05 -04002238static void ata_pr_blacklisted(const struct ata_port *ap,
2239 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
2241 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2242 ap->id, dev->devno);
2243}
2244
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002245static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 "WDC AC11000H",
2247 "WDC AC22100H",
2248 "WDC AC32500H",
2249 "WDC AC33100H",
2250 "WDC AC31600H",
2251 "WDC AC32100H",
2252 "WDC AC23200L",
2253 "Compaq CRD-8241B",
2254 "CRD-8400B",
2255 "CRD-8480B",
2256 "CRD-8482B",
2257 "CRD-84",
2258 "SanDisk SDP3B",
2259 "SanDisk SDP3B-64",
2260 "SANYO CD-ROM CRD",
2261 "HITACHI CDR-8",
2262 "HITACHI CDR-8335",
2263 "HITACHI CDR-8435",
2264 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002265 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 "CD-532E-A",
2267 "E-IDE CD-ROM CR-840",
2268 "CD-ROM Drive/F5A",
2269 "WPI CDD-820",
2270 "SAMSUNG CD-ROM SC-148C",
2271 "SAMSUNG CD-ROM SC",
2272 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2274 "_NEC DV5800A",
2275};
2276
Jeff Garzik057ace52005-10-22 14:27:05 -04002277static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
2279 unsigned char model_num[40];
2280 char *s;
2281 unsigned int len;
2282 int i;
2283
2284 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2285 sizeof(model_num));
2286 s = &model_num[0];
2287 len = strnlen(s, sizeof(model_num));
2288
2289 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2290 while ((len > 0) && (s[len - 1] == ' ')) {
2291 len--;
2292 s[len] = 0;
2293 }
2294
2295 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2296 if (!strncmp(ata_dma_blacklist[i], s, len))
2297 return 1;
2298
2299 return 0;
2300}
2301
Jeff Garzik057ace52005-10-22 14:27:05 -04002302static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
Jeff Garzik057ace52005-10-22 14:27:05 -04002304 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 unsigned int mask;
2306
2307 master = &ap->device[0];
2308 slave = &ap->device[1];
2309
2310 assert (ata_dev_present(master) || ata_dev_present(slave));
2311
2312 if (shift == ATA_SHIFT_UDMA) {
2313 mask = ap->udma_mask;
2314 if (ata_dev_present(master)) {
2315 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002316 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 mask = 0;
2318 ata_pr_blacklisted(ap, master);
2319 }
2320 }
2321 if (ata_dev_present(slave)) {
2322 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002323 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 mask = 0;
2325 ata_pr_blacklisted(ap, slave);
2326 }
2327 }
2328 }
2329 else if (shift == ATA_SHIFT_MWDMA) {
2330 mask = ap->mwdma_mask;
2331 if (ata_dev_present(master)) {
2332 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002333 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 mask = 0;
2335 ata_pr_blacklisted(ap, master);
2336 }
2337 }
2338 if (ata_dev_present(slave)) {
2339 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002340 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 mask = 0;
2342 ata_pr_blacklisted(ap, slave);
2343 }
2344 }
2345 }
2346 else if (shift == ATA_SHIFT_PIO) {
2347 mask = ap->pio_mask;
2348 if (ata_dev_present(master)) {
2349 /* spec doesn't return explicit support for
2350 * PIO0-2, so we fake it
2351 */
2352 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2353 tmp_mode <<= 3;
2354 tmp_mode |= 0x7;
2355 mask &= tmp_mode;
2356 }
2357 if (ata_dev_present(slave)) {
2358 /* spec doesn't return explicit support for
2359 * PIO0-2, so we fake it
2360 */
2361 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2362 tmp_mode <<= 3;
2363 tmp_mode |= 0x7;
2364 mask &= tmp_mode;
2365 }
2366 }
2367 else {
2368 mask = 0xffffffff; /* shut up compiler warning */
2369 BUG();
2370 }
2371
2372 return mask;
2373}
2374
2375/* find greatest bit */
2376static int fgb(u32 bitmap)
2377{
2378 unsigned int i;
2379 int x = -1;
2380
2381 for (i = 0; i < 32; i++)
2382 if (bitmap & (1 << i))
2383 x = i;
2384
2385 return x;
2386}
2387
2388/**
2389 * ata_choose_xfer_mode - attempt to find best transfer mode
2390 * @ap: Port for which an xfer mode will be selected
2391 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2392 * @xfer_shift_out: (output) bit shift that selects this mode
2393 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002394 * Based on host and device capabilities, determine the
2395 * maximum transfer mode that is amenable to all.
2396 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002398 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 *
2400 * RETURNS:
2401 * Zero on success, negative on error.
2402 */
2403
Jeff Garzik057ace52005-10-22 14:27:05 -04002404static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 u8 *xfer_mode_out,
2406 unsigned int *xfer_shift_out)
2407{
2408 unsigned int mask, shift;
2409 int x, i;
2410
2411 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2412 shift = xfer_mode_classes[i].shift;
2413 mask = ata_get_mode_mask(ap, shift);
2414
2415 x = fgb(mask);
2416 if (x >= 0) {
2417 *xfer_mode_out = xfer_mode_classes[i].base + x;
2418 *xfer_shift_out = shift;
2419 return 0;
2420 }
2421 }
2422
2423 return -1;
2424}
2425
2426/**
2427 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2428 * @ap: Port associated with device @dev
2429 * @dev: Device to which command will be sent
2430 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002431 * Issue SET FEATURES - XFER MODE command to device @dev
2432 * on port @ap.
2433 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002435 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 */
2437
2438static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2439{
Tejun Heoa0123702005-12-13 14:49:31 +09002440 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 /* set up set-features taskfile */
2443 DPRINTK("set features - xfer mode\n");
2444
Tejun Heoa0123702005-12-13 14:49:31 +09002445 ata_tf_init(ap, &tf, dev->devno);
2446 tf.command = ATA_CMD_SET_FEATURES;
2447 tf.feature = SETFEATURES_XFER;
2448 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2449 tf.protocol = ATA_PROT_NODATA;
2450 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Tejun Heoa0123702005-12-13 14:49:31 +09002452 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2453 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2454 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
2458 DPRINTK("EXIT\n");
2459}
2460
2461/**
Albert Lee59a10b12005-10-12 15:09:42 +08002462 * ata_dev_reread_id - Reread the device identify device info
2463 * @ap: port where the device is
2464 * @dev: device to reread the identify device info
2465 *
2466 * LOCKING:
2467 */
2468
2469static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2470{
Tejun Heoa0123702005-12-13 14:49:31 +09002471 struct ata_taskfile tf;
Albert Lee59a10b12005-10-12 15:09:42 +08002472
Tejun Heoa0123702005-12-13 14:49:31 +09002473 ata_tf_init(ap, &tf, dev->devno);
Albert Lee59a10b12005-10-12 15:09:42 +08002474
2475 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09002476 tf.command = ATA_CMD_ID_ATA;
Albert Lee59a10b12005-10-12 15:09:42 +08002477 DPRINTK("do ATA identify\n");
2478 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09002479 tf.command = ATA_CMD_ID_ATAPI;
Albert Lee59a10b12005-10-12 15:09:42 +08002480 DPRINTK("do ATAPI identify\n");
2481 }
2482
Tejun Heoa0123702005-12-13 14:49:31 +09002483 tf.flags |= ATA_TFLAG_DEVICE;
2484 tf.protocol = ATA_PROT_PIO;
Albert Lee59a10b12005-10-12 15:09:42 +08002485
Tejun Heoa0123702005-12-13 14:49:31 +09002486 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2487 dev->id, sizeof(dev->id)))
Albert Lee59a10b12005-10-12 15:09:42 +08002488 goto err_out;
2489
Albert Lee59a10b12005-10-12 15:09:42 +08002490 swap_buf_le16(dev->id, ATA_ID_WORDS);
2491
2492 ata_dump_id(dev);
2493
2494 DPRINTK("EXIT\n");
2495
2496 return;
2497err_out:
Tejun Heoa0123702005-12-13 14:49:31 +09002498 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002499 ata_port_disable(ap);
2500}
2501
2502/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002503 * ata_dev_init_params - Issue INIT DEV PARAMS command
2504 * @ap: Port associated with device @dev
2505 * @dev: Device to which command will be sent
2506 *
2507 * LOCKING:
2508 */
2509
2510static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2511{
Tejun Heoa0123702005-12-13 14:49:31 +09002512 struct ata_taskfile tf;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002513 u16 sectors = dev->id[6];
2514 u16 heads = dev->id[3];
2515
2516 /* Number of sectors per track 1-255. Number of heads 1-16 */
2517 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2518 return;
2519
2520 /* set up init dev params taskfile */
2521 DPRINTK("init dev params \n");
2522
Tejun Heoa0123702005-12-13 14:49:31 +09002523 ata_tf_init(ap, &tf, dev->devno);
2524 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2525 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2526 tf.protocol = ATA_PROT_NODATA;
2527 tf.nsect = sectors;
2528 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002529
Tejun Heoa0123702005-12-13 14:49:31 +09002530 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2531 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2532 ap->id);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002533 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002534 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04002535
2536 DPRINTK("EXIT\n");
2537}
2538
2539/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002540 * ata_sg_clean - Unmap DMA memory associated with command
2541 * @qc: Command containing DMA memory to be released
2542 *
2543 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 *
2545 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002546 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 */
2548
2549static void ata_sg_clean(struct ata_queued_cmd *qc)
2550{
2551 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002552 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002554 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
2556 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2557 assert(sg != NULL);
2558
2559 if (qc->flags & ATA_QCFLAG_SINGLE)
2560 assert(qc->n_elem == 1);
2561
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002562 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002564 /* if we padded the buffer out to 32-bit bound, and data
2565 * xfer direction is from-device, we must copy from the
2566 * pad buffer back into the supplied buffer
2567 */
2568 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2569 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2570
2571 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002572 if (qc->n_elem)
2573 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002574 /* restore last sg */
2575 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2576 if (pad_buf) {
2577 struct scatterlist *psg = &qc->pad_sgent;
2578 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2579 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002580 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002581 }
2582 } else {
Jeff Garzike1410f22005-11-14 14:06:26 -05002583 if (sg_dma_len(&sg[0]) > 0)
2584 dma_unmap_single(ap->host_set->dev,
2585 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2586 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002587 /* restore sg */
2588 sg->length += qc->pad_len;
2589 if (pad_buf)
2590 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2591 pad_buf, qc->pad_len);
2592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
2594 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002595 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596}
2597
2598/**
2599 * ata_fill_sg - Fill PCI IDE PRD table
2600 * @qc: Metadata associated with taskfile to be transferred
2601 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002602 * Fill PCI IDE PRD (scatter-gather) table with segments
2603 * associated with the current disk command.
2604 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002606 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 *
2608 */
2609static void ata_fill_sg(struct ata_queued_cmd *qc)
2610{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002612 struct scatterlist *sg;
2613 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002615 assert(qc->__sg != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 assert(qc->n_elem > 0);
2617
2618 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002619 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 u32 addr, offset;
2621 u32 sg_len, len;
2622
2623 /* determine if physical DMA addr spans 64K boundary.
2624 * Note h/w doesn't support 64-bit, so we unconditionally
2625 * truncate dma_addr_t to u32.
2626 */
2627 addr = (u32) sg_dma_address(sg);
2628 sg_len = sg_dma_len(sg);
2629
2630 while (sg_len) {
2631 offset = addr & 0xffff;
2632 len = sg_len;
2633 if ((offset + sg_len) > 0x10000)
2634 len = 0x10000 - offset;
2635
2636 ap->prd[idx].addr = cpu_to_le32(addr);
2637 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2638 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2639
2640 idx++;
2641 sg_len -= len;
2642 addr += len;
2643 }
2644 }
2645
2646 if (idx)
2647 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2648}
2649/**
2650 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2651 * @qc: Metadata associated with taskfile to check
2652 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002653 * Allow low-level driver to filter ATA PACKET commands, returning
2654 * a status indicating whether or not it is OK to use DMA for the
2655 * supplied PACKET command.
2656 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002658 * spin_lock_irqsave(host_set lock)
2659 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 * RETURNS: 0 when ATAPI DMA can be used
2661 * nonzero otherwise
2662 */
2663int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2664{
2665 struct ata_port *ap = qc->ap;
2666 int rc = 0; /* Assume ATAPI DMA is OK by default */
2667
2668 if (ap->ops->check_atapi_dma)
2669 rc = ap->ops->check_atapi_dma(qc);
2670
2671 return rc;
2672}
2673/**
2674 * ata_qc_prep - Prepare taskfile for submission
2675 * @qc: Metadata associated with taskfile to be prepared
2676 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002677 * Prepare ATA taskfile for submission.
2678 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 * LOCKING:
2680 * spin_lock_irqsave(host_set lock)
2681 */
2682void ata_qc_prep(struct ata_queued_cmd *qc)
2683{
2684 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2685 return;
2686
2687 ata_fill_sg(qc);
2688}
2689
Jeff Garzik0cba6322005-05-30 19:49:12 -04002690/**
2691 * ata_sg_init_one - Associate command with memory buffer
2692 * @qc: Command to be associated
2693 * @buf: Memory buffer
2694 * @buflen: Length of memory buffer, in bytes.
2695 *
2696 * Initialize the data-related elements of queued_cmd @qc
2697 * to point to a single memory buffer, @buf of byte length @buflen.
2698 *
2699 * LOCKING:
2700 * spin_lock_irqsave(host_set lock)
2701 */
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2704{
2705 struct scatterlist *sg;
2706
2707 qc->flags |= ATA_QCFLAG_SINGLE;
2708
2709 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002710 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002712 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 qc->buf_virt = buf;
2714
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002715 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002716 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}
2718
Jeff Garzik0cba6322005-05-30 19:49:12 -04002719/**
2720 * ata_sg_init - Associate command with scatter-gather table.
2721 * @qc: Command to be associated
2722 * @sg: Scatter-gather table.
2723 * @n_elem: Number of elements in s/g table.
2724 *
2725 * Initialize the data-related elements of queued_cmd @qc
2726 * to point to a scatter-gather table @sg, containing @n_elem
2727 * elements.
2728 *
2729 * LOCKING:
2730 * spin_lock_irqsave(host_set lock)
2731 */
2732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2734 unsigned int n_elem)
2735{
2736 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002737 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002739 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740}
2741
2742/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002743 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2744 * @qc: Command with memory buffer to be mapped.
2745 *
2746 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 *
2748 * LOCKING:
2749 * spin_lock_irqsave(host_set lock)
2750 *
2751 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002752 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 */
2754
2755static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2756{
2757 struct ata_port *ap = qc->ap;
2758 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002759 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 dma_addr_t dma_address;
2761
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002762 /* we must lengthen transfers to end on a 32-bit boundary */
2763 qc->pad_len = sg->length & 3;
2764 if (qc->pad_len) {
2765 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2766 struct scatterlist *psg = &qc->pad_sgent;
2767
2768 assert(qc->dev->class == ATA_DEV_ATAPI);
2769
2770 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2771
2772 if (qc->tf.flags & ATA_TFLAG_WRITE)
2773 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2774 qc->pad_len);
2775
2776 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2777 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2778 /* trim sg */
2779 sg->length -= qc->pad_len;
2780
2781 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2782 sg->length, qc->pad_len);
2783 }
2784
Jeff Garzike1410f22005-11-14 14:06:26 -05002785 if (!sg->length) {
2786 sg_dma_address(sg) = 0;
2787 goto skip_map;
2788 }
2789
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002791 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002792 if (dma_mapping_error(dma_address)) {
2793 /* restore sg */
2794 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798 sg_dma_address(sg) = dma_address;
Jeff Garzike1410f22005-11-14 14:06:26 -05002799skip_map:
Albert Lee32529e02005-05-26 03:49:42 -04002800 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
2802 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2803 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2804
2805 return 0;
2806}
2807
2808/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002809 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2810 * @qc: Command with scatter-gather table to be mapped.
2811 *
2812 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 *
2814 * LOCKING:
2815 * spin_lock_irqsave(host_set lock)
2816 *
2817 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002818 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 *
2820 */
2821
2822static int ata_sg_setup(struct ata_queued_cmd *qc)
2823{
2824 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002825 struct scatterlist *sg = qc->__sg;
2826 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002827 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829 VPRINTK("ENTER, ata%u\n", ap->id);
2830 assert(qc->flags & ATA_QCFLAG_SG);
2831
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002832 /* we must lengthen transfers to end on a 32-bit boundary */
2833 qc->pad_len = lsg->length & 3;
2834 if (qc->pad_len) {
2835 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2836 struct scatterlist *psg = &qc->pad_sgent;
2837 unsigned int offset;
2838
2839 assert(qc->dev->class == ATA_DEV_ATAPI);
2840
2841 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2842
2843 /*
2844 * psg->page/offset are used to copy to-be-written
2845 * data in this function or read data in ata_sg_clean.
2846 */
2847 offset = lsg->offset + lsg->length - qc->pad_len;
2848 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2849 psg->offset = offset_in_page(offset);
2850
2851 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2852 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2853 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002854 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002855 }
2856
2857 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2858 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2859 /* trim last sg */
2860 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002861 if (lsg->length == 0)
2862 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002863
2864 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2865 qc->n_elem - 1, lsg->length, qc->pad_len);
2866 }
2867
Jeff Garzike1410f22005-11-14 14:06:26 -05002868 pre_n_elem = qc->n_elem;
2869 if (trim_sg && pre_n_elem)
2870 pre_n_elem--;
2871
2872 if (!pre_n_elem) {
2873 n_elem = 0;
2874 goto skip_map;
2875 }
2876
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002878 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002879 if (n_elem < 1) {
2880 /* restore last sg */
2881 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
2885 DPRINTK("%d sg elements mapped\n", n_elem);
2886
Jeff Garzike1410f22005-11-14 14:06:26 -05002887skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 qc->n_elem = n_elem;
2889
2890 return 0;
2891}
2892
2893/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002894 * ata_poll_qc_complete - turn irq back on and finish qc
2895 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002896 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002897 *
2898 * LOCKING:
2899 * None. (grabs host lock)
2900 */
2901
Albert Leea22e2eb2005-12-05 15:38:02 +08002902void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002903{
2904 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002905 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002906
Jeff Garzikb8f61532005-08-25 22:01:20 -04002907 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002908 ap->flags &= ~ATA_FLAG_NOINTR;
2909 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002910 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002911 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002912}
2913
2914/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002915 * ata_pio_poll - poll using PIO, depending on current state
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002916 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 *
2918 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002919 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 *
2921 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002922 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 */
2924
2925static unsigned long ata_pio_poll(struct ata_port *ap)
2926{
Albert Leec14b8332005-12-05 15:36:08 +08002927 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002929 unsigned int poll_state = HSM_ST_UNKNOWN;
2930 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Albert Leec14b8332005-12-05 15:36:08 +08002932 qc = ata_qc_from_tag(ap, ap->active_tag);
2933 assert(qc != NULL);
2934
Albert Lee14be71f2005-09-27 17:36:35 +08002935 switch (ap->hsm_task_state) {
2936 case HSM_ST:
2937 case HSM_ST_POLL:
2938 poll_state = HSM_ST_POLL;
2939 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002941 case HSM_ST_LAST:
2942 case HSM_ST_LAST_POLL:
2943 poll_state = HSM_ST_LAST_POLL;
2944 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 break;
2946 default:
2947 BUG();
2948 break;
2949 }
2950
2951 status = ata_chk_status(ap);
2952 if (status & ATA_BUSY) {
2953 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09002954 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08002955 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 return 0;
2957 }
Albert Lee14be71f2005-09-27 17:36:35 +08002958 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 return ATA_SHORT_PAUSE;
2960 }
2961
Albert Lee14be71f2005-09-27 17:36:35 +08002962 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 return 0;
2964}
2965
2966/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002967 * ata_pio_complete - check if drive is busy or idle
2968 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 *
2970 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002971 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002972 *
2973 * RETURNS:
2974 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 */
2976
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002977static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
2979 struct ata_queued_cmd *qc;
2980 u8 drv_stat;
2981
2982 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002983 * This is purely heuristic. This is a fast path. Sometimes when
2984 * we enter, BSY will be cleared in a chk-status or two. If not,
2985 * the drive is probably seeking or something. Snooze for a couple
2986 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08002987 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 */
Albert Leefe79e682005-12-06 11:34:59 +08002989 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2990 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08002992 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2993 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08002994 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002996 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 }
2998 }
2999
Albert Leec14b8332005-12-05 15:36:08 +08003000 qc = ata_qc_from_tag(ap, ap->active_tag);
3001 assert(qc != NULL);
3002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 drv_stat = ata_wait_idle(ap);
3004 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003005 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003006 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003007 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 }
3009
Albert Lee14be71f2005-09-27 17:36:35 +08003010 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Albert Leea22e2eb2005-12-05 15:38:02 +08003012 assert(qc->err_mask == 0);
3013 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003014
3015 /* another command may start at this point */
3016
3017 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018}
3019
Edward Falk0baab862005-06-02 18:17:13 -04003020
3021/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003022 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003023 * @buf: Buffer to swap
3024 * @buf_words: Number of 16-bit words in buffer.
3025 *
3026 * Swap halves of 16-bit words if needed to convert from
3027 * little-endian byte order to native cpu byte order, or
3028 * vice-versa.
3029 *
3030 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003031 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003032 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033void swap_buf_le16(u16 *buf, unsigned int buf_words)
3034{
3035#ifdef __BIG_ENDIAN
3036 unsigned int i;
3037
3038 for (i = 0; i < buf_words; i++)
3039 buf[i] = le16_to_cpu(buf[i]);
3040#endif /* __BIG_ENDIAN */
3041}
3042
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003043/**
3044 * ata_mmio_data_xfer - Transfer data by MMIO
3045 * @ap: port to read/write
3046 * @buf: data buffer
3047 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003048 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003049 *
3050 * Transfer data from/to the device data register by MMIO.
3051 *
3052 * LOCKING:
3053 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003054 */
3055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3057 unsigned int buflen, int write_data)
3058{
3059 unsigned int i;
3060 unsigned int words = buflen >> 1;
3061 u16 *buf16 = (u16 *) buf;
3062 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3063
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003064 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 if (write_data) {
3066 for (i = 0; i < words; i++)
3067 writew(le16_to_cpu(buf16[i]), mmio);
3068 } else {
3069 for (i = 0; i < words; i++)
3070 buf16[i] = cpu_to_le16(readw(mmio));
3071 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003072
3073 /* Transfer trailing 1 byte, if any. */
3074 if (unlikely(buflen & 0x01)) {
3075 u16 align_buf[1] = { 0 };
3076 unsigned char *trailing_buf = buf + buflen - 1;
3077
3078 if (write_data) {
3079 memcpy(align_buf, trailing_buf, 1);
3080 writew(le16_to_cpu(align_buf[0]), mmio);
3081 } else {
3082 align_buf[0] = cpu_to_le16(readw(mmio));
3083 memcpy(trailing_buf, align_buf, 1);
3084 }
3085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086}
3087
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003088/**
3089 * ata_pio_data_xfer - Transfer data by PIO
3090 * @ap: port to read/write
3091 * @buf: data buffer
3092 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003093 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003094 *
3095 * Transfer data from/to the device data register by PIO.
3096 *
3097 * LOCKING:
3098 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003099 */
3100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3102 unsigned int buflen, int write_data)
3103{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003104 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003106 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003108 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003110 insw(ap->ioaddr.data_addr, buf, words);
3111
3112 /* Transfer trailing 1 byte, if any. */
3113 if (unlikely(buflen & 0x01)) {
3114 u16 align_buf[1] = { 0 };
3115 unsigned char *trailing_buf = buf + buflen - 1;
3116
3117 if (write_data) {
3118 memcpy(align_buf, trailing_buf, 1);
3119 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3120 } else {
3121 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3122 memcpy(trailing_buf, align_buf, 1);
3123 }
3124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125}
3126
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003127/**
3128 * ata_data_xfer - Transfer data from/to the data register.
3129 * @ap: port to read/write
3130 * @buf: data buffer
3131 * @buflen: buffer length
3132 * @do_write: read/write
3133 *
3134 * Transfer data from/to the device data register.
3135 *
3136 * LOCKING:
3137 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003138 */
3139
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3141 unsigned int buflen, int do_write)
3142{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003143 /* Make the crap hardware pay the costs not the good stuff */
3144 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3145 unsigned long flags;
3146 local_irq_save(flags);
3147 if (ap->flags & ATA_FLAG_MMIO)
3148 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3149 else
3150 ata_pio_data_xfer(ap, buf, buflen, do_write);
3151 local_irq_restore(flags);
3152 } else {
3153 if (ap->flags & ATA_FLAG_MMIO)
3154 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3155 else
3156 ata_pio_data_xfer(ap, buf, buflen, do_write);
3157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158}
3159
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003160/**
3161 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3162 * @qc: Command on going
3163 *
3164 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3165 *
3166 * LOCKING:
3167 * Inherited from caller.
3168 */
3169
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170static void ata_pio_sector(struct ata_queued_cmd *qc)
3171{
3172 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003173 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 struct ata_port *ap = qc->ap;
3175 struct page *page;
3176 unsigned int offset;
3177 unsigned char *buf;
3178
3179 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003180 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
3182 page = sg[qc->cursg].page;
3183 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3184
3185 /* get the current page and offset */
3186 page = nth_page(page, (offset >> PAGE_SHIFT));
3187 offset %= PAGE_SIZE;
3188
3189 buf = kmap(page) + offset;
3190
3191 qc->cursect++;
3192 qc->cursg_ofs++;
3193
Albert Lee32529e02005-05-26 03:49:42 -04003194 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 qc->cursg++;
3196 qc->cursg_ofs = 0;
3197 }
3198
3199 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3200
3201 /* do the actual data transfer */
3202 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3203 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3204
3205 kunmap(page);
3206}
3207
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003208/**
3209 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3210 * @qc: Command on going
3211 * @bytes: number of bytes
3212 *
3213 * Transfer Transfer data from/to the ATAPI device.
3214 *
3215 * LOCKING:
3216 * Inherited from caller.
3217 *
3218 */
3219
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3221{
3222 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003223 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 struct ata_port *ap = qc->ap;
3225 struct page *page;
3226 unsigned char *buf;
3227 unsigned int offset, count;
3228
Albert Lee563a6e12005-08-12 14:17:50 +08003229 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003230 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
3232next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003233 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003234 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003235 * The end of qc->sg is reached and the device expects
3236 * more data to transfer. In order not to overrun qc->sg
3237 * and fulfill length specified in the byte count register,
3238 * - for read case, discard trailing data from the device
3239 * - for write case, padding zero data to the device
3240 */
3241 u16 pad_buf[1] = { 0 };
3242 unsigned int words = bytes >> 1;
3243 unsigned int i;
3244
3245 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003246 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003247 ap->id, bytes);
3248
3249 for (i = 0; i < words; i++)
3250 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3251
Albert Lee14be71f2005-09-27 17:36:35 +08003252 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003253 return;
3254 }
3255
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003256 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 page = sg->page;
3259 offset = sg->offset + qc->cursg_ofs;
3260
3261 /* get the current page and offset */
3262 page = nth_page(page, (offset >> PAGE_SHIFT));
3263 offset %= PAGE_SIZE;
3264
Albert Lee6952df02005-06-06 15:56:03 +08003265 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003266 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
3268 /* don't cross page boundaries */
3269 count = min(count, (unsigned int)PAGE_SIZE - offset);
3270
3271 buf = kmap(page) + offset;
3272
3273 bytes -= count;
3274 qc->curbytes += count;
3275 qc->cursg_ofs += count;
3276
Albert Lee32529e02005-05-26 03:49:42 -04003277 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 qc->cursg++;
3279 qc->cursg_ofs = 0;
3280 }
3281
3282 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3283
3284 /* do the actual data transfer */
3285 ata_data_xfer(ap, buf, count, do_write);
3286
3287 kunmap(page);
3288
Albert Lee563a6e12005-08-12 14:17:50 +08003289 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291}
3292
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003293/**
3294 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3295 * @qc: Command on going
3296 *
3297 * Transfer Transfer data from/to the ATAPI device.
3298 *
3299 * LOCKING:
3300 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003301 */
3302
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3304{
3305 struct ata_port *ap = qc->ap;
3306 struct ata_device *dev = qc->dev;
3307 unsigned int ireason, bc_lo, bc_hi, bytes;
3308 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3309
3310 ap->ops->tf_read(ap, &qc->tf);
3311 ireason = qc->tf.nsect;
3312 bc_lo = qc->tf.lbam;
3313 bc_hi = qc->tf.lbah;
3314 bytes = (bc_hi << 8) | bc_lo;
3315
3316 /* shall be cleared to zero, indicating xfer of data */
3317 if (ireason & (1 << 0))
3318 goto err_out;
3319
3320 /* make sure transfer direction matches expected */
3321 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3322 if (do_write != i_write)
3323 goto err_out;
3324
3325 __atapi_pio_bytes(qc, bytes);
3326
3327 return;
3328
3329err_out:
3330 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3331 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003332 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003333 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334}
3335
3336/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003337 * ata_pio_block - start PIO on a block
3338 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 *
3340 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003341 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 */
3343
3344static void ata_pio_block(struct ata_port *ap)
3345{
3346 struct ata_queued_cmd *qc;
3347 u8 status;
3348
3349 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003350 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 * Sometimes when we enter, BSY will be cleared in
3352 * a chk-status or two. If not, the drive is probably seeking
3353 * or something. Snooze for a couple msecs, then
3354 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003355 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 */
3357 status = ata_busy_wait(ap, ATA_BUSY, 5);
3358 if (status & ATA_BUSY) {
3359 msleep(2);
3360 status = ata_busy_wait(ap, ATA_BUSY, 10);
3361 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003362 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3364 return;
3365 }
3366 }
3367
3368 qc = ata_qc_from_tag(ap, ap->active_tag);
3369 assert(qc != NULL);
3370
Albert Leefe79e682005-12-06 11:34:59 +08003371 /* check error */
3372 if (status & (ATA_ERR | ATA_DF)) {
3373 qc->err_mask |= AC_ERR_DEV;
3374 ap->hsm_task_state = HSM_ST_ERR;
3375 return;
3376 }
3377
3378 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003380 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003382 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 return;
3384 }
3385
3386 atapi_pio_bytes(qc);
3387 } else {
3388 /* handle BSY=0, DRQ=0 as error */
3389 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003390 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003391 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 return;
3393 }
3394
3395 ata_pio_sector(qc);
3396 }
3397}
3398
3399static void ata_pio_error(struct ata_port *ap)
3400{
3401 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003402
3403 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
3405 qc = ata_qc_from_tag(ap, ap->active_tag);
3406 assert(qc != NULL);
3407
Albert Lee1c848982005-12-05 15:40:15 +08003408 /* make sure qc->err_mask is available to
3409 * know what's wrong and recover
3410 */
3411 assert(qc->err_mask);
3412
Albert Lee14be71f2005-09-27 17:36:35 +08003413 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
Albert Leea22e2eb2005-12-05 15:38:02 +08003415 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416}
3417
3418static void ata_pio_task(void *_data)
3419{
3420 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003421 unsigned long timeout;
3422 int qc_completed;
3423
3424fsm_start:
3425 timeout = 0;
3426 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
Albert Lee14be71f2005-09-27 17:36:35 +08003428 switch (ap->hsm_task_state) {
3429 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 return;
3431
Albert Lee14be71f2005-09-27 17:36:35 +08003432 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 ata_pio_block(ap);
3434 break;
3435
Albert Lee14be71f2005-09-27 17:36:35 +08003436 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003437 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 break;
3439
Albert Lee14be71f2005-09-27 17:36:35 +08003440 case HSM_ST_POLL:
3441 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 timeout = ata_pio_poll(ap);
3443 break;
3444
Albert Lee14be71f2005-09-27 17:36:35 +08003445 case HSM_ST_TMOUT:
3446 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 ata_pio_error(ap);
3448 return;
3449 }
3450
3451 if (timeout)
Tejun Heo95064372006-01-23 13:09:37 +09003452 ata_queue_delayed_pio_task(ap, timeout);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003453 else if (!qc_completed)
3454 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455}
3456
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457/**
3458 * ata_qc_timeout - Handle timeout of queued command
3459 * @qc: Command that timed out
3460 *
3461 * Some part of the kernel (currently, only the SCSI layer)
3462 * has noticed that the active command on port @ap has not
3463 * completed after a specified length of time. Handle this
3464 * condition by disabling DMA (if necessary) and completing
3465 * transactions, with error if necessary.
3466 *
3467 * This also handles the case of the "lost interrupt", where
3468 * for some reason (possibly hardware bug, possibly driver bug)
3469 * an interrupt was not delivered to the driver, even though the
3470 * transaction completed successfully.
3471 *
3472 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003473 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 */
3475
3476static void ata_qc_timeout(struct ata_queued_cmd *qc)
3477{
3478 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003479 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003481 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
3483 DPRINTK("ENTER\n");
3484
Tejun Heoc18d06f2006-02-02 00:56:10 +09003485 ata_flush_pio_tasks(ap);
3486 ap->hsm_task_state = HSM_ST_IDLE;
3487
Jeff Garzikb8f61532005-08-25 22:01:20 -04003488 spin_lock_irqsave(&host_set->lock, flags);
3489
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 switch (qc->tf.protocol) {
3491
3492 case ATA_PROT_DMA:
3493 case ATA_PROT_ATAPI_DMA:
3494 host_stat = ap->ops->bmdma_status(ap);
3495
3496 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003497 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
3499 /* fall through */
3500
3501 default:
3502 ata_altstatus(ap);
3503 drv_stat = ata_chk_status(ap);
3504
3505 /* ack bmdma irq events */
3506 ap->ops->irq_clear(ap);
3507
3508 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3509 ap->id, qc->tf.command, drv_stat, host_stat);
3510
3511 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003512 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 break;
3514 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003515
3516 spin_unlock_irqrestore(&host_set->lock, flags);
3517
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003518 ata_eh_qc_complete(qc);
3519
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 DPRINTK("EXIT\n");
3521}
3522
3523/**
3524 * ata_eng_timeout - Handle timeout of queued command
3525 * @ap: Port on which timed-out command is active
3526 *
3527 * Some part of the kernel (currently, only the SCSI layer)
3528 * has noticed that the active command on port @ap has not
3529 * completed after a specified length of time. Handle this
3530 * condition by disabling DMA (if necessary) and completing
3531 * transactions, with error if necessary.
3532 *
3533 * This also handles the case of the "lost interrupt", where
3534 * for some reason (possibly hardware bug, possibly driver bug)
3535 * an interrupt was not delivered to the driver, even though the
3536 * transaction completed successfully.
3537 *
3538 * LOCKING:
3539 * Inherited from SCSI layer (none, can sleep)
3540 */
3541
3542void ata_eng_timeout(struct ata_port *ap)
3543{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 DPRINTK("ENTER\n");
3545
Tejun Heof6379022006-02-10 15:10:48 +09003546 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 DPRINTK("EXIT\n");
3549}
3550
3551/**
3552 * ata_qc_new - Request an available ATA command, for queueing
3553 * @ap: Port associated with device @dev
3554 * @dev: Device from whom we request an available command structure
3555 *
3556 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003557 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 */
3559
3560static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3561{
3562 struct ata_queued_cmd *qc = NULL;
3563 unsigned int i;
3564
3565 for (i = 0; i < ATA_MAX_QUEUE; i++)
3566 if (!test_and_set_bit(i, &ap->qactive)) {
3567 qc = ata_qc_from_tag(ap, i);
3568 break;
3569 }
3570
3571 if (qc)
3572 qc->tag = i;
3573
3574 return qc;
3575}
3576
3577/**
3578 * ata_qc_new_init - Request an available ATA command, and initialize it
3579 * @ap: Port associated with device @dev
3580 * @dev: Device from whom we request an available command structure
3581 *
3582 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003583 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 */
3585
3586struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3587 struct ata_device *dev)
3588{
3589 struct ata_queued_cmd *qc;
3590
3591 qc = ata_qc_new(ap);
3592 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 qc->scsicmd = NULL;
3594 qc->ap = ap;
3595 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003597 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 }
3599
3600 return qc;
3601}
3602
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603/**
3604 * ata_qc_free - free unused ata_queued_cmd
3605 * @qc: Command to complete
3606 *
3607 * Designed to free unused ata_queued_cmd object
3608 * in case something prevents using it.
3609 *
3610 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003611 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 */
3613void ata_qc_free(struct ata_queued_cmd *qc)
3614{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003615 struct ata_port *ap = qc->ap;
3616 unsigned int tag;
3617
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
Tejun Heo4ba946e2006-01-23 13:09:36 +09003620 qc->flags = 0;
3621 tag = qc->tag;
3622 if (likely(ata_tag_valid(tag))) {
3623 if (tag == ap->active_tag)
3624 ap->active_tag = ATA_TAG_POISON;
3625 qc->tag = ATA_TAG_POISON;
3626 clear_bit(tag, &ap->qactive);
3627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628}
3629
Tejun Heo341963b2006-02-10 15:10:48 +09003630inline void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3633 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3634
3635 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3636 ata_sg_clean(qc);
3637
Albert Lee3f3791d2005-08-16 14:25:38 +08003638 /* atapi: mark qc as inactive to prevent the interrupt handler
3639 * from completing the command twice later, before the error handler
3640 * is called. (when rc != 0 and atapi request sense is needed)
3641 */
3642 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3643
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003645 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646}
3647
Tejun Heo341963b2006-02-10 15:10:48 +09003648/**
3649 * ata_qc_complete - Complete an active ATA command
3650 * @qc: Command to complete
3651 * @err_mask: ATA Status register contents
3652 *
3653 * Indicate to the mid and upper layers that an ATA
3654 * command has completed, with either an ok or not-ok status.
3655 *
3656 * LOCKING:
3657 * spin_lock_irqsave(host_set lock)
3658 */
3659void ata_qc_complete(struct ata_queued_cmd *qc)
3660{
3661 if (unlikely(qc->flags & ATA_QCFLAG_EH_SCHEDULED))
3662 return;
3663
3664 __ata_qc_complete(qc);
3665}
3666
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3668{
3669 struct ata_port *ap = qc->ap;
3670
3671 switch (qc->tf.protocol) {
3672 case ATA_PROT_DMA:
3673 case ATA_PROT_ATAPI_DMA:
3674 return 1;
3675
3676 case ATA_PROT_ATAPI:
3677 case ATA_PROT_PIO:
3678 case ATA_PROT_PIO_MULT:
3679 if (ap->flags & ATA_FLAG_PIO_DMA)
3680 return 1;
3681
3682 /* fall through */
3683
3684 default:
3685 return 0;
3686 }
3687
3688 /* never reached */
3689}
3690
3691/**
3692 * ata_qc_issue - issue taskfile to device
3693 * @qc: command to issue to device
3694 *
3695 * Prepare an ATA command to submission to device.
3696 * This includes mapping the data into a DMA-able
3697 * area, filling in the S/G table, and finally
3698 * writing the taskfile to hardware, starting the command.
3699 *
3700 * LOCKING:
3701 * spin_lock_irqsave(host_set lock)
3702 *
3703 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003704 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 */
3706
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003707unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708{
3709 struct ata_port *ap = qc->ap;
3710
3711 if (ata_should_dma_map(qc)) {
3712 if (qc->flags & ATA_QCFLAG_SG) {
3713 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003714 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3716 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003717 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 }
3719 } else {
3720 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3721 }
3722
3723 ap->ops->qc_prep(qc);
3724
3725 qc->ap->active_tag = qc->tag;
3726 qc->flags |= ATA_QCFLAG_ACTIVE;
3727
3728 return ap->ops->qc_issue(qc);
3729
Tejun Heo8e436af2006-01-23 13:09:36 +09003730sg_err:
3731 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003732 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733}
3734
Edward Falk0baab862005-06-02 18:17:13 -04003735
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736/**
3737 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3738 * @qc: command to issue to device
3739 *
3740 * Using various libata functions and hooks, this function
3741 * starts an ATA command. ATA commands are grouped into
3742 * classes called "protocols", and issuing each type of protocol
3743 * is slightly different.
3744 *
Edward Falk0baab862005-06-02 18:17:13 -04003745 * May be used as the qc_issue() entry in ata_port_operations.
3746 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 * LOCKING:
3748 * spin_lock_irqsave(host_set lock)
3749 *
3750 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003751 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 */
3753
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003754unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755{
3756 struct ata_port *ap = qc->ap;
3757
3758 ata_dev_select(ap, qc->dev->devno, 1, 0);
3759
3760 switch (qc->tf.protocol) {
3761 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05003762 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 break;
3764
3765 case ATA_PROT_DMA:
3766 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3767 ap->ops->bmdma_setup(qc); /* set up bmdma */
3768 ap->ops->bmdma_start(qc); /* initiate bmdma */
3769 break;
3770
3771 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3772 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003773 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003774 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09003775 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 break;
3777
3778 case ATA_PROT_ATAPI:
3779 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003780 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003781 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 break;
3783
3784 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003785 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05003786 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003787 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 break;
3789
3790 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003791 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3793 ap->ops->bmdma_setup(qc); /* set up bmdma */
Tejun Heo95064372006-01-23 13:09:37 +09003794 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 break;
3796
3797 default:
3798 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003799 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 }
3801
3802 return 0;
3803}
3804
3805/**
Edward Falk0baab862005-06-02 18:17:13 -04003806 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 * @qc: Info associated with this ATA transaction.
3808 *
3809 * LOCKING:
3810 * spin_lock_irqsave(host_set lock)
3811 */
3812
3813static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3814{
3815 struct ata_port *ap = qc->ap;
3816 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3817 u8 dmactl;
3818 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3819
3820 /* load PRD table addr. */
3821 mb(); /* make sure PRD table writes are visible to controller */
3822 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3823
3824 /* specify data direction, triple-check start bit is clear */
3825 dmactl = readb(mmio + ATA_DMA_CMD);
3826 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3827 if (!rw)
3828 dmactl |= ATA_DMA_WR;
3829 writeb(dmactl, mmio + ATA_DMA_CMD);
3830
3831 /* issue r/w command */
3832 ap->ops->exec_command(ap, &qc->tf);
3833}
3834
3835/**
Alan Coxb73fc892005-08-26 16:03:19 +01003836 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 * @qc: Info associated with this ATA transaction.
3838 *
3839 * LOCKING:
3840 * spin_lock_irqsave(host_set lock)
3841 */
3842
3843static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3844{
3845 struct ata_port *ap = qc->ap;
3846 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3847 u8 dmactl;
3848
3849 /* start host DMA transaction */
3850 dmactl = readb(mmio + ATA_DMA_CMD);
3851 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3852
3853 /* Strictly, one may wish to issue a readb() here, to
3854 * flush the mmio write. However, control also passes
3855 * to the hardware at this point, and it will interrupt
3856 * us when we are to resume control. So, in effect,
3857 * we don't care when the mmio write flushes.
3858 * Further, a read of the DMA status register _immediately_
3859 * following the write may not be what certain flaky hardware
3860 * is expected, so I think it is best to not add a readb()
3861 * without first all the MMIO ATA cards/mobos.
3862 * Or maybe I'm just being paranoid.
3863 */
3864}
3865
3866/**
3867 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3868 * @qc: Info associated with this ATA transaction.
3869 *
3870 * LOCKING:
3871 * spin_lock_irqsave(host_set lock)
3872 */
3873
3874static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3875{
3876 struct ata_port *ap = qc->ap;
3877 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3878 u8 dmactl;
3879
3880 /* load PRD table addr. */
3881 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3882
3883 /* specify data direction, triple-check start bit is clear */
3884 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3885 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3886 if (!rw)
3887 dmactl |= ATA_DMA_WR;
3888 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3889
3890 /* issue r/w command */
3891 ap->ops->exec_command(ap, &qc->tf);
3892}
3893
3894/**
3895 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3896 * @qc: Info associated with this ATA transaction.
3897 *
3898 * LOCKING:
3899 * spin_lock_irqsave(host_set lock)
3900 */
3901
3902static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3903{
3904 struct ata_port *ap = qc->ap;
3905 u8 dmactl;
3906
3907 /* start host DMA transaction */
3908 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3909 outb(dmactl | ATA_DMA_START,
3910 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3911}
3912
Edward Falk0baab862005-06-02 18:17:13 -04003913
3914/**
3915 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3916 * @qc: Info associated with this ATA transaction.
3917 *
3918 * Writes the ATA_DMA_START flag to the DMA command register.
3919 *
3920 * May be used as the bmdma_start() entry in ata_port_operations.
3921 *
3922 * LOCKING:
3923 * spin_lock_irqsave(host_set lock)
3924 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925void ata_bmdma_start(struct ata_queued_cmd *qc)
3926{
3927 if (qc->ap->flags & ATA_FLAG_MMIO)
3928 ata_bmdma_start_mmio(qc);
3929 else
3930 ata_bmdma_start_pio(qc);
3931}
3932
Edward Falk0baab862005-06-02 18:17:13 -04003933
3934/**
3935 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3936 * @qc: Info associated with this ATA transaction.
3937 *
3938 * Writes address of PRD table to device's PRD Table Address
3939 * register, sets the DMA control register, and calls
3940 * ops->exec_command() to start the transfer.
3941 *
3942 * May be used as the bmdma_setup() entry in ata_port_operations.
3943 *
3944 * LOCKING:
3945 * spin_lock_irqsave(host_set lock)
3946 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947void ata_bmdma_setup(struct ata_queued_cmd *qc)
3948{
3949 if (qc->ap->flags & ATA_FLAG_MMIO)
3950 ata_bmdma_setup_mmio(qc);
3951 else
3952 ata_bmdma_setup_pio(qc);
3953}
3954
Edward Falk0baab862005-06-02 18:17:13 -04003955
3956/**
3957 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003958 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003959 *
3960 * Clear interrupt and error flags in DMA status register.
3961 *
3962 * May be used as the irq_clear() entry in ata_port_operations.
3963 *
3964 * LOCKING:
3965 * spin_lock_irqsave(host_set lock)
3966 */
3967
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968void ata_bmdma_irq_clear(struct ata_port *ap)
3969{
3970 if (ap->flags & ATA_FLAG_MMIO) {
3971 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3972 writeb(readb(mmio), mmio);
3973 } else {
3974 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3975 outb(inb(addr), addr);
3976 }
3977
3978}
3979
Edward Falk0baab862005-06-02 18:17:13 -04003980
3981/**
3982 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003983 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003984 *
3985 * Read and return BMDMA status register.
3986 *
3987 * May be used as the bmdma_status() entry in ata_port_operations.
3988 *
3989 * LOCKING:
3990 * spin_lock_irqsave(host_set lock)
3991 */
3992
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993u8 ata_bmdma_status(struct ata_port *ap)
3994{
3995 u8 host_stat;
3996 if (ap->flags & ATA_FLAG_MMIO) {
3997 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3998 host_stat = readb(mmio + ATA_DMA_STATUS);
3999 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004000 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 return host_stat;
4002}
4003
Edward Falk0baab862005-06-02 18:17:13 -04004004
4005/**
4006 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004007 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004008 *
4009 * Clears the ATA_DMA_START flag in the dma control register
4010 *
4011 * May be used as the bmdma_stop() entry in ata_port_operations.
4012 *
4013 * LOCKING:
4014 * spin_lock_irqsave(host_set lock)
4015 */
4016
Alan Coxb73fc892005-08-26 16:03:19 +01004017void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018{
Alan Coxb73fc892005-08-26 16:03:19 +01004019 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 if (ap->flags & ATA_FLAG_MMIO) {
4021 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4022
4023 /* clear start/stop bit */
4024 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4025 mmio + ATA_DMA_CMD);
4026 } else {
4027 /* clear start/stop bit */
4028 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4029 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4030 }
4031
4032 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4033 ata_altstatus(ap); /* dummy read */
4034}
4035
4036/**
4037 * ata_host_intr - Handle host interrupt for given (port, task)
4038 * @ap: Port on which interrupt arrived (possibly...)
4039 * @qc: Taskfile currently active in engine
4040 *
4041 * Handle host interrupt for given queued command. Currently,
4042 * only DMA interrupts are handled. All other commands are
4043 * handled via polling with interrupts disabled (nIEN bit).
4044 *
4045 * LOCKING:
4046 * spin_lock_irqsave(host_set lock)
4047 *
4048 * RETURNS:
4049 * One if interrupt was handled, zero if not (shared irq).
4050 */
4051
4052inline unsigned int ata_host_intr (struct ata_port *ap,
4053 struct ata_queued_cmd *qc)
4054{
4055 u8 status, host_stat;
4056
4057 switch (qc->tf.protocol) {
4058
4059 case ATA_PROT_DMA:
4060 case ATA_PROT_ATAPI_DMA:
4061 case ATA_PROT_ATAPI:
4062 /* check status of DMA engine */
4063 host_stat = ap->ops->bmdma_status(ap);
4064 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4065
4066 /* if it's not our irq... */
4067 if (!(host_stat & ATA_DMA_INTR))
4068 goto idle_irq;
4069
4070 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004071 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072
4073 /* fall through */
4074
4075 case ATA_PROT_ATAPI_NODATA:
4076 case ATA_PROT_NODATA:
4077 /* check altstatus */
4078 status = ata_altstatus(ap);
4079 if (status & ATA_BUSY)
4080 goto idle_irq;
4081
4082 /* check main status, clearing INTRQ */
4083 status = ata_chk_status(ap);
4084 if (unlikely(status & ATA_BUSY))
4085 goto idle_irq;
4086 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4087 ap->id, qc->tf.protocol, status);
4088
4089 /* ack bmdma irq events */
4090 ap->ops->irq_clear(ap);
4091
4092 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004093 qc->err_mask |= ac_err_mask(status);
4094 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 break;
4096
4097 default:
4098 goto idle_irq;
4099 }
4100
4101 return 1; /* irq handled */
4102
4103idle_irq:
4104 ap->stats.idle_irq++;
4105
4106#ifdef ATA_IRQ_TRAP
4107 if ((ap->stats.idle_irq % 1000) == 0) {
4108 handled = 1;
4109 ata_irq_ack(ap, 0); /* debug trap */
4110 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4111 }
4112#endif
4113 return 0; /* irq not handled */
4114}
4115
4116/**
4117 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004118 * @irq: irq line (unused)
4119 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 * @regs: unused
4121 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004122 * Default interrupt handler for PCI IDE devices. Calls
4123 * ata_host_intr() for each port that is not disabled.
4124 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004126 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 *
4128 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004129 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 */
4131
4132irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4133{
4134 struct ata_host_set *host_set = dev_instance;
4135 unsigned int i;
4136 unsigned int handled = 0;
4137 unsigned long flags;
4138
4139 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4140 spin_lock_irqsave(&host_set->lock, flags);
4141
4142 for (i = 0; i < host_set->n_ports; i++) {
4143 struct ata_port *ap;
4144
4145 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004146 if (ap &&
4147 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 struct ata_queued_cmd *qc;
4149
4150 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004151 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4152 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 handled |= ata_host_intr(ap, qc);
4154 }
4155 }
4156
4157 spin_unlock_irqrestore(&host_set->lock, flags);
4158
4159 return IRQ_RETVAL(handled);
4160}
4161
4162/**
4163 * atapi_packet_task - Write CDB bytes to hardware
4164 * @_data: Port to which ATAPI device is attached.
4165 *
4166 * When device has indicated its readiness to accept
4167 * a CDB, this function is called. Send the CDB.
4168 * If DMA is to be performed, exit immediately.
4169 * Otherwise, we are in polling mode, so poll
4170 * status under operation succeeds or fails.
4171 *
4172 * LOCKING:
4173 * Kernel thread context (may sleep)
4174 */
4175
4176static void atapi_packet_task(void *_data)
4177{
4178 struct ata_port *ap = _data;
4179 struct ata_queued_cmd *qc;
4180 u8 status;
4181
4182 qc = ata_qc_from_tag(ap, ap->active_tag);
4183 assert(qc != NULL);
4184 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4185
4186 /* sleep-wait for BSY to clear */
4187 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004188 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004189 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Leed8fe4522005-12-05 15:42:17 +08004190 goto err_out;
4191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192
4193 /* make sure DRQ is set */
4194 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004195 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004196 qc->err_mask |= AC_ERR_HSM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199
4200 /* send SCSI cdb */
4201 DPRINTK("send cdb\n");
4202 assert(ap->cdb_len >= 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Tejun Heoc1389502005-08-22 14:59:24 +09004204 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4205 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4206 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
Tejun Heoc1389502005-08-22 14:59:24 +09004208 /* Once we're done issuing command and kicking bmdma,
4209 * irq handler takes over. To not lose irq, we need
4210 * to clear NOINTR flag before sending cdb, but
4211 * interrupt handler shouldn't be invoked before we're
4212 * finished. Hence, the following locking.
4213 */
4214 spin_lock_irqsave(&ap->host_set->lock, flags);
4215 ap->flags &= ~ATA_FLAG_NOINTR;
4216 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4217 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4218 ap->ops->bmdma_start(qc); /* initiate bmdma */
4219 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4220 } else {
4221 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222
Tejun Heoc1389502005-08-22 14:59:24 +09004223 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004224 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09004225 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 }
4227
4228 return;
4229
4230err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004231 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232}
4233
Edward Falk0baab862005-06-02 18:17:13 -04004234
Jens Axboe9b847542006-01-06 09:28:07 +01004235/*
4236 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4237 * without filling any other registers
4238 */
4239static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4240 u8 cmd)
4241{
4242 struct ata_taskfile tf;
4243 int err;
4244
4245 ata_tf_init(ap, &tf, dev->devno);
4246
4247 tf.command = cmd;
4248 tf.flags |= ATA_TFLAG_DEVICE;
4249 tf.protocol = ATA_PROT_NODATA;
4250
4251 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4252 if (err)
4253 printk(KERN_ERR "%s: ata command failed: %d\n",
4254 __FUNCTION__, err);
4255
4256 return err;
4257}
4258
4259static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4260{
4261 u8 cmd;
4262
4263 if (!ata_try_flush_cache(dev))
4264 return 0;
4265
4266 if (ata_id_has_flush_ext(dev->id))
4267 cmd = ATA_CMD_FLUSH_EXT;
4268 else
4269 cmd = ATA_CMD_FLUSH;
4270
4271 return ata_do_simple_cmd(ap, dev, cmd);
4272}
4273
4274static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4275{
4276 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4277}
4278
4279static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4280{
4281 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4282}
4283
4284/**
4285 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004286 * @ap: port the device is connected to
4287 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004288 *
4289 * Kick the drive back into action, by sending it an idle immediate
4290 * command and making sure its transfer mode matches between drive
4291 * and host.
4292 *
4293 */
4294int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4295{
4296 if (ap->flags & ATA_FLAG_SUSPENDED) {
4297 ap->flags &= ~ATA_FLAG_SUSPENDED;
4298 ata_set_mode(ap);
4299 }
4300 if (!ata_dev_present(dev))
4301 return 0;
4302 if (dev->class == ATA_DEV_ATA)
4303 ata_start_drive(ap, dev);
4304
4305 return 0;
4306}
4307
4308/**
4309 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004310 * @ap: port the device is connected to
4311 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004312 *
4313 * Flush the cache on the drive, if appropriate, then issue a
4314 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004315 */
4316int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4317{
4318 if (!ata_dev_present(dev))
4319 return 0;
4320 if (dev->class == ATA_DEV_ATA)
4321 ata_flush_cache(ap, dev);
4322
4323 ata_standby_drive(ap, dev);
4324 ap->flags |= ATA_FLAG_SUSPENDED;
4325 return 0;
4326}
4327
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004328/**
4329 * ata_port_start - Set port up for dma.
4330 * @ap: Port to initialize
4331 *
4332 * Called just after data structures for each port are
4333 * initialized. Allocates space for PRD table.
4334 *
4335 * May be used as the port_start() entry in ata_port_operations.
4336 *
4337 * LOCKING:
4338 * Inherited from caller.
4339 */
4340
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341int ata_port_start (struct ata_port *ap)
4342{
4343 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004344 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
4346 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4347 if (!ap->prd)
4348 return -ENOMEM;
4349
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004350 rc = ata_pad_alloc(ap, dev);
4351 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004352 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004353 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004354 }
4355
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4357
4358 return 0;
4359}
4360
Edward Falk0baab862005-06-02 18:17:13 -04004361
4362/**
4363 * ata_port_stop - Undo ata_port_start()
4364 * @ap: Port to shut down
4365 *
4366 * Frees the PRD table.
4367 *
4368 * May be used as the port_stop() entry in ata_port_operations.
4369 *
4370 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004371 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004372 */
4373
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374void ata_port_stop (struct ata_port *ap)
4375{
4376 struct device *dev = ap->host_set->dev;
4377
4378 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004379 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380}
4381
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004382void ata_host_stop (struct ata_host_set *host_set)
4383{
4384 if (host_set->mmio_base)
4385 iounmap(host_set->mmio_base);
4386}
4387
4388
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389/**
4390 * ata_host_remove - Unregister SCSI host structure with upper layers
4391 * @ap: Port to unregister
4392 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4393 *
4394 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004395 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 */
4397
4398static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4399{
4400 struct Scsi_Host *sh = ap->host;
4401
4402 DPRINTK("ENTER\n");
4403
4404 if (do_unregister)
4405 scsi_remove_host(sh);
4406
4407 ap->ops->port_stop(ap);
4408}
4409
4410/**
4411 * ata_host_init - Initialize an ata_port structure
4412 * @ap: Structure to initialize
4413 * @host: associated SCSI mid-layer structure
4414 * @host_set: Collection of hosts to which @ap belongs
4415 * @ent: Probe information provided by low-level driver
4416 * @port_no: Port number associated with this ata_port
4417 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004418 * Initialize a new ata_port structure, and its associated
4419 * scsi_host.
4420 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004422 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 */
4424
4425static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4426 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004427 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428{
4429 unsigned int i;
4430
4431 host->max_id = 16;
4432 host->max_lun = 1;
4433 host->max_channel = 1;
4434 host->unique_id = ata_unique_id++;
4435 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004436
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 ap->flags = ATA_FLAG_PORT_DISABLED;
4438 ap->id = host->unique_id;
4439 ap->host = host;
4440 ap->ctl = ATA_DEVCTL_OBS;
4441 ap->host_set = host_set;
4442 ap->port_no = port_no;
4443 ap->hard_port_no =
4444 ent->legacy_mode ? ent->hard_port_no : port_no;
4445 ap->pio_mask = ent->pio_mask;
4446 ap->mwdma_mask = ent->mwdma_mask;
4447 ap->udma_mask = ent->udma_mask;
4448 ap->flags |= ent->host_flags;
4449 ap->ops = ent->port_ops;
4450 ap->cbl = ATA_CBL_NONE;
4451 ap->active_tag = ATA_TAG_POISON;
4452 ap->last_ctl = 0xFF;
4453
4454 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4455 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004456 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
4458 for (i = 0; i < ATA_MAX_DEVICES; i++)
4459 ap->device[i].devno = i;
4460
4461#ifdef ATA_IRQ_TRAP
4462 ap->stats.unhandled_irq = 1;
4463 ap->stats.idle_irq = 1;
4464#endif
4465
4466 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4467}
4468
4469/**
4470 * ata_host_add - Attach low-level ATA driver to system
4471 * @ent: Information provided by low-level driver
4472 * @host_set: Collections of ports to which we add
4473 * @port_no: Port number associated with this host
4474 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004475 * Attach low-level ATA driver to system.
4476 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004478 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 *
4480 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004481 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 */
4483
Jeff Garzik057ace52005-10-22 14:27:05 -04004484static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 struct ata_host_set *host_set,
4486 unsigned int port_no)
4487{
4488 struct Scsi_Host *host;
4489 struct ata_port *ap;
4490 int rc;
4491
4492 DPRINTK("ENTER\n");
4493 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4494 if (!host)
4495 return NULL;
4496
4497 ap = (struct ata_port *) &host->hostdata[0];
4498
4499 ata_host_init(ap, host, host_set, ent, port_no);
4500
4501 rc = ap->ops->port_start(ap);
4502 if (rc)
4503 goto err_out;
4504
4505 return ap;
4506
4507err_out:
4508 scsi_host_put(host);
4509 return NULL;
4510}
4511
4512/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004513 * ata_device_add - Register hardware device with ATA and SCSI layers
4514 * @ent: Probe information describing hardware device to be registered
4515 *
4516 * This function processes the information provided in the probe
4517 * information struct @ent, allocates the necessary ATA and SCSI
4518 * host information structures, initializes them, and registers
4519 * everything with requisite kernel subsystems.
4520 *
4521 * This function requests irqs, probes the ATA bus, and probes
4522 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 *
4524 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004525 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 *
4527 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004528 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 */
4530
Jeff Garzik057ace52005-10-22 14:27:05 -04004531int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532{
4533 unsigned int count = 0, i;
4534 struct device *dev = ent->dev;
4535 struct ata_host_set *host_set;
4536
4537 DPRINTK("ENTER\n");
4538 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004539 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4541 if (!host_set)
4542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 spin_lock_init(&host_set->lock);
4544
4545 host_set->dev = dev;
4546 host_set->n_ports = ent->n_ports;
4547 host_set->irq = ent->irq;
4548 host_set->mmio_base = ent->mmio_base;
4549 host_set->private_data = ent->private_data;
4550 host_set->ops = ent->port_ops;
4551
4552 /* register each port bound to this device */
4553 for (i = 0; i < ent->n_ports; i++) {
4554 struct ata_port *ap;
4555 unsigned long xfer_mode_mask;
4556
4557 ap = ata_host_add(ent, host_set, i);
4558 if (!ap)
4559 goto err_out;
4560
4561 host_set->ports[i] = ap;
4562 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4563 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4564 (ap->pio_mask << ATA_SHIFT_PIO);
4565
4566 /* print per-port info to dmesg */
4567 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4568 "bmdma 0x%lX irq %lu\n",
4569 ap->id,
4570 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4571 ata_mode_string(xfer_mode_mask),
4572 ap->ioaddr.cmd_addr,
4573 ap->ioaddr.ctl_addr,
4574 ap->ioaddr.bmdma_addr,
4575 ent->irq);
4576
4577 ata_chk_status(ap);
4578 host_set->ops->irq_clear(ap);
4579 count++;
4580 }
4581
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004582 if (!count)
4583 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
4585 /* obtain irq, that is shared between channels */
4586 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4587 DRV_NAME, host_set))
4588 goto err_out;
4589
4590 /* perform each probe synchronously */
4591 DPRINTK("probe begin\n");
4592 for (i = 0; i < count; i++) {
4593 struct ata_port *ap;
4594 int rc;
4595
4596 ap = host_set->ports[i];
4597
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004598 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004600 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
4602 if (rc) {
4603 /* FIXME: do something useful here?
4604 * Current libata behavior will
4605 * tear down everything when
4606 * the module is removed
4607 * or the h/w is unplugged.
4608 */
4609 }
4610
4611 rc = scsi_add_host(ap->host, dev);
4612 if (rc) {
4613 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4614 ap->id);
4615 /* FIXME: do something useful here */
4616 /* FIXME: handle unconditional calls to
4617 * scsi_scan_host and ata_host_remove, below,
4618 * at the very least
4619 */
4620 }
4621 }
4622
4623 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004624 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 for (i = 0; i < count; i++) {
4626 struct ata_port *ap = host_set->ports[i];
4627
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004628 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 }
4630
4631 dev_set_drvdata(dev, host_set);
4632
4633 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4634 return ent->n_ports; /* success */
4635
4636err_out:
4637 for (i = 0; i < count; i++) {
4638 ata_host_remove(host_set->ports[i], 1);
4639 scsi_host_put(host_set->ports[i]->host);
4640 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004641err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 kfree(host_set);
4643 VPRINTK("EXIT, returning 0\n");
4644 return 0;
4645}
4646
4647/**
Alan Cox17b14452005-09-15 15:44:00 +01004648 * ata_host_set_remove - PCI layer callback for device removal
4649 * @host_set: ATA host set that was removed
4650 *
4651 * Unregister all objects associated with this host set. Free those
4652 * objects.
4653 *
4654 * LOCKING:
4655 * Inherited from calling layer (may sleep).
4656 */
4657
Alan Cox17b14452005-09-15 15:44:00 +01004658void ata_host_set_remove(struct ata_host_set *host_set)
4659{
4660 struct ata_port *ap;
4661 unsigned int i;
4662
4663 for (i = 0; i < host_set->n_ports; i++) {
4664 ap = host_set->ports[i];
4665 scsi_remove_host(ap->host);
4666 }
4667
4668 free_irq(host_set->irq, host_set);
4669
4670 for (i = 0; i < host_set->n_ports; i++) {
4671 ap = host_set->ports[i];
4672
4673 ata_scsi_release(ap->host);
4674
4675 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4676 struct ata_ioports *ioaddr = &ap->ioaddr;
4677
4678 if (ioaddr->cmd_addr == 0x1f0)
4679 release_region(0x1f0, 8);
4680 else if (ioaddr->cmd_addr == 0x170)
4681 release_region(0x170, 8);
4682 }
4683
4684 scsi_host_put(ap->host);
4685 }
4686
4687 if (host_set->ops->host_stop)
4688 host_set->ops->host_stop(host_set);
4689
4690 kfree(host_set);
4691}
4692
4693/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 * ata_scsi_release - SCSI layer callback hook for host unload
4695 * @host: libata host to be unloaded
4696 *
4697 * Performs all duties necessary to shut down a libata port...
4698 * Kill port kthread, disable port, and release resources.
4699 *
4700 * LOCKING:
4701 * Inherited from SCSI layer.
4702 *
4703 * RETURNS:
4704 * One.
4705 */
4706
4707int ata_scsi_release(struct Scsi_Host *host)
4708{
4709 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4710
4711 DPRINTK("ENTER\n");
4712
4713 ap->ops->port_disable(ap);
4714 ata_host_remove(ap, 0);
4715
4716 DPRINTK("EXIT\n");
4717 return 1;
4718}
4719
4720/**
4721 * ata_std_ports - initialize ioaddr with standard port offsets.
4722 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004723 *
4724 * Utility function which initializes data_addr, error_addr,
4725 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4726 * device_addr, status_addr, and command_addr to standard offsets
4727 * relative to cmd_addr.
4728 *
4729 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 */
Edward Falk0baab862005-06-02 18:17:13 -04004731
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732void ata_std_ports(struct ata_ioports *ioaddr)
4733{
4734 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4735 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4736 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4737 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4738 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4739 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4740 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4741 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4742 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4743 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4744}
4745
Edward Falk0baab862005-06-02 18:17:13 -04004746
Jeff Garzik374b1872005-08-30 05:42:52 -04004747#ifdef CONFIG_PCI
4748
4749void ata_pci_host_stop (struct ata_host_set *host_set)
4750{
4751 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4752
4753 pci_iounmap(pdev, host_set->mmio_base);
4754}
4755
Edward Falk0baab862005-06-02 18:17:13 -04004756/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 * ata_pci_remove_one - PCI layer callback for device removal
4758 * @pdev: PCI device that was removed
4759 *
4760 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004761 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 * Handle this by unregistering all objects associated
4763 * with this PCI device. Free those objects. Then finally
4764 * release PCI resources and disable device.
4765 *
4766 * LOCKING:
4767 * Inherited from PCI layer (may sleep).
4768 */
4769
4770void ata_pci_remove_one (struct pci_dev *pdev)
4771{
4772 struct device *dev = pci_dev_to_dev(pdev);
4773 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
Alan Cox17b14452005-09-15 15:44:00 +01004775 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 pci_release_regions(pdev);
4777 pci_disable_device(pdev);
4778 dev_set_drvdata(dev, NULL);
4779}
4780
4781/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004782int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783{
4784 unsigned long tmp = 0;
4785
4786 switch (bits->width) {
4787 case 1: {
4788 u8 tmp8 = 0;
4789 pci_read_config_byte(pdev, bits->reg, &tmp8);
4790 tmp = tmp8;
4791 break;
4792 }
4793 case 2: {
4794 u16 tmp16 = 0;
4795 pci_read_config_word(pdev, bits->reg, &tmp16);
4796 tmp = tmp16;
4797 break;
4798 }
4799 case 4: {
4800 u32 tmp32 = 0;
4801 pci_read_config_dword(pdev, bits->reg, &tmp32);
4802 tmp = tmp32;
4803 break;
4804 }
4805
4806 default:
4807 return -EINVAL;
4808 }
4809
4810 tmp &= bits->mask;
4811
4812 return (tmp == bits->val) ? 1 : 0;
4813}
Jens Axboe9b847542006-01-06 09:28:07 +01004814
4815int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4816{
4817 pci_save_state(pdev);
4818 pci_disable_device(pdev);
4819 pci_set_power_state(pdev, PCI_D3hot);
4820 return 0;
4821}
4822
4823int ata_pci_device_resume(struct pci_dev *pdev)
4824{
4825 pci_set_power_state(pdev, PCI_D0);
4826 pci_restore_state(pdev);
4827 pci_enable_device(pdev);
4828 pci_set_master(pdev);
4829 return 0;
4830}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831#endif /* CONFIG_PCI */
4832
4833
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834static int __init ata_init(void)
4835{
4836 ata_wq = create_workqueue("ata");
4837 if (!ata_wq)
4838 return -ENOMEM;
4839
4840 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4841 return 0;
4842}
4843
4844static void __exit ata_exit(void)
4845{
4846 destroy_workqueue(ata_wq);
4847}
4848
4849module_init(ata_init);
4850module_exit(ata_exit);
4851
Jeff Garzik67846b32005-10-05 02:58:32 -04004852static unsigned long ratelimit_time;
4853static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4854
4855int ata_ratelimit(void)
4856{
4857 int rc;
4858 unsigned long flags;
4859
4860 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4861
4862 if (time_after(jiffies, ratelimit_time)) {
4863 rc = 1;
4864 ratelimit_time = jiffies + (HZ/5);
4865 } else
4866 rc = 0;
4867
4868 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4869
4870 return rc;
4871}
4872
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873/*
4874 * libata is essentially a library of internal helper functions for
4875 * low-level ATA host controller drivers. As such, the API/ABI is
4876 * likely to change as new drivers are added and updated.
4877 * Do not depend on ABI/API stability.
4878 */
4879
4880EXPORT_SYMBOL_GPL(ata_std_bios_param);
4881EXPORT_SYMBOL_GPL(ata_std_ports);
4882EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01004883EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884EXPORT_SYMBOL_GPL(ata_sg_init);
4885EXPORT_SYMBOL_GPL(ata_sg_init_one);
4886EXPORT_SYMBOL_GPL(ata_qc_complete);
4887EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4888EXPORT_SYMBOL_GPL(ata_eng_timeout);
4889EXPORT_SYMBOL_GPL(ata_tf_load);
4890EXPORT_SYMBOL_GPL(ata_tf_read);
4891EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4892EXPORT_SYMBOL_GPL(ata_std_dev_select);
4893EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4894EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4895EXPORT_SYMBOL_GPL(ata_check_status);
4896EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897EXPORT_SYMBOL_GPL(ata_exec_command);
4898EXPORT_SYMBOL_GPL(ata_port_start);
4899EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004900EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901EXPORT_SYMBOL_GPL(ata_interrupt);
4902EXPORT_SYMBOL_GPL(ata_qc_prep);
4903EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4904EXPORT_SYMBOL_GPL(ata_bmdma_start);
4905EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4906EXPORT_SYMBOL_GPL(ata_bmdma_status);
4907EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4908EXPORT_SYMBOL_GPL(ata_port_probe);
4909EXPORT_SYMBOL_GPL(sata_phy_reset);
4910EXPORT_SYMBOL_GPL(__sata_phy_reset);
4911EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09004912EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004913EXPORT_SYMBOL_GPL(ata_std_softreset);
4914EXPORT_SYMBOL_GPL(sata_std_hardreset);
4915EXPORT_SYMBOL_GPL(ata_std_postreset);
4916EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09004917EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04004919EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09004920EXPORT_SYMBOL_GPL(ata_busy_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4922EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Tejun Heof29841e2006-02-10 15:10:48 +09004923EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924EXPORT_SYMBOL_GPL(ata_scsi_error);
4925EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4926EXPORT_SYMBOL_GPL(ata_scsi_release);
4927EXPORT_SYMBOL_GPL(ata_host_intr);
4928EXPORT_SYMBOL_GPL(ata_dev_classify);
4929EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04004930EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004932EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
4933EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934
Alan Cox1bc4ccf2006-01-09 17:18:14 +00004935EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04004936EXPORT_SYMBOL_GPL(ata_timing_compute);
4937EXPORT_SYMBOL_GPL(ata_timing_merge);
4938
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939#ifdef CONFIG_PCI
4940EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04004941EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4943EXPORT_SYMBOL_GPL(ata_pci_init_one);
4944EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01004945EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
4946EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01004948
4949EXPORT_SYMBOL_GPL(ata_device_suspend);
4950EXPORT_SYMBOL_GPL(ata_device_resume);
4951EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
4952EXPORT_SYMBOL_GPL(ata_scsi_device_resume);