]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/scsi/libata-core.c
Merge branch 'upstream'
[mirror_ubuntu-eoan-kernel.git] / drivers / scsi / libata-core.c
CommitLineData
1da177e4 1/*
af36d7f0
JG
2 * 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 *
1da177e4
LT
33 */
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>
67846b30 51#include <linux/jiffies.h>
378f058c 52#include <linux/scatterlist.h>
1da177e4 53#include <scsi/scsi.h>
1da177e4 54#include "scsi_priv.h"
193515d5 55#include <scsi/scsi_cmnd.h>
1da177e4
LT
56#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
6aff8f1f 64static unsigned int ata_dev_init_params(struct ata_port *ap,
00b6f5e9
AL
65 struct ata_device *dev,
66 u16 heads,
67 u16 sectors);
1da177e4 68static void ata_set_mode(struct ata_port *ap);
83206a29
TH
69static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
70 struct ata_device *dev);
acf356b1 71static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev);
1da177e4
LT
72
73static unsigned int ata_unique_id = 1;
74static struct workqueue_struct *ata_wq;
75
418dc1f5 76int atapi_enabled = 1;
1623c81e
JG
77module_param(atapi_enabled, int, 0444);
78MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
79
c3c013a2
JG
80int libata_fua = 0;
81module_param_named(fua, libata_fua, int, 0444);
82MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
83
1da177e4
LT
84MODULE_AUTHOR("Jeff Garzik");
85MODULE_DESCRIPTION("Library module for ATA devices");
86MODULE_LICENSE("GPL");
87MODULE_VERSION(DRV_VERSION);
88
0baab86b 89
1da177e4
LT
90/**
91 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
92 * @tf: Taskfile to convert
93 * @fis: Buffer into which data will output
94 * @pmp: Port multiplier port
95 *
96 * Converts a standard ATA taskfile to a Serial ATA
97 * FIS structure (Register - Host to Device).
98 *
99 * LOCKING:
100 * Inherited from caller.
101 */
102
057ace5e 103void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
1da177e4
LT
104{
105 fis[0] = 0x27; /* Register - Host to Device FIS */
106 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
107 bit 7 indicates Command FIS */
108 fis[2] = tf->command;
109 fis[3] = tf->feature;
110
111 fis[4] = tf->lbal;
112 fis[5] = tf->lbam;
113 fis[6] = tf->lbah;
114 fis[7] = tf->device;
115
116 fis[8] = tf->hob_lbal;
117 fis[9] = tf->hob_lbam;
118 fis[10] = tf->hob_lbah;
119 fis[11] = tf->hob_feature;
120
121 fis[12] = tf->nsect;
122 fis[13] = tf->hob_nsect;
123 fis[14] = 0;
124 fis[15] = tf->ctl;
125
126 fis[16] = 0;
127 fis[17] = 0;
128 fis[18] = 0;
129 fis[19] = 0;
130}
131
132/**
133 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
134 * @fis: Buffer from which data will be input
135 * @tf: Taskfile to output
136 *
e12a1be6 137 * Converts a serial ATA FIS structure to a standard ATA taskfile.
1da177e4
LT
138 *
139 * LOCKING:
140 * Inherited from caller.
141 */
142
057ace5e 143void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
1da177e4
LT
144{
145 tf->command = fis[2]; /* status */
146 tf->feature = fis[3]; /* error */
147
148 tf->lbal = fis[4];
149 tf->lbam = fis[5];
150 tf->lbah = fis[6];
151 tf->device = fis[7];
152
153 tf->hob_lbal = fis[8];
154 tf->hob_lbam = fis[9];
155 tf->hob_lbah = fis[10];
156
157 tf->nsect = fis[12];
158 tf->hob_nsect = fis[13];
159}
160
8cbd6df1
AL
161static const u8 ata_rw_cmds[] = {
162 /* pio multi */
163 ATA_CMD_READ_MULTI,
164 ATA_CMD_WRITE_MULTI,
165 ATA_CMD_READ_MULTI_EXT,
166 ATA_CMD_WRITE_MULTI_EXT,
9a3dccc4
TH
167 0,
168 0,
169 0,
170 ATA_CMD_WRITE_MULTI_FUA_EXT,
8cbd6df1
AL
171 /* pio */
172 ATA_CMD_PIO_READ,
173 ATA_CMD_PIO_WRITE,
174 ATA_CMD_PIO_READ_EXT,
175 ATA_CMD_PIO_WRITE_EXT,
9a3dccc4
TH
176 0,
177 0,
178 0,
179 0,
8cbd6df1
AL
180 /* dma */
181 ATA_CMD_READ,
182 ATA_CMD_WRITE,
183 ATA_CMD_READ_EXT,
9a3dccc4
TH
184 ATA_CMD_WRITE_EXT,
185 0,
186 0,
187 0,
188 ATA_CMD_WRITE_FUA_EXT
8cbd6df1 189};
1da177e4
LT
190
191/**
8cbd6df1
AL
192 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
193 * @qc: command to examine and configure
1da177e4 194 *
2e9edbf8 195 * Examine the device configuration and tf->flags to calculate
8cbd6df1 196 * the proper read/write commands and protocol to use.
1da177e4
LT
197 *
198 * LOCKING:
199 * caller.
200 */
9a3dccc4 201int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
1da177e4 202{
8cbd6df1
AL
203 struct ata_taskfile *tf = &qc->tf;
204 struct ata_device *dev = qc->dev;
9a3dccc4 205 u8 cmd;
1da177e4 206
9a3dccc4 207 int index, fua, lba48, write;
2e9edbf8 208
9a3dccc4 209 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
8cbd6df1
AL
210 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
211 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
1da177e4 212
8cbd6df1
AL
213 if (dev->flags & ATA_DFLAG_PIO) {
214 tf->protocol = ATA_PROT_PIO;
9a3dccc4 215 index = dev->multi_count ? 0 : 8;
8d238e01
AC
216 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
217 /* Unable to use DMA due to host limitation */
218 tf->protocol = ATA_PROT_PIO;
aef9d533 219 index = dev->multi_count ? 0 : 8;
8cbd6df1
AL
220 } else {
221 tf->protocol = ATA_PROT_DMA;
9a3dccc4 222 index = 16;
8cbd6df1 223 }
1da177e4 224
9a3dccc4
TH
225 cmd = ata_rw_cmds[index + fua + lba48 + write];
226 if (cmd) {
227 tf->command = cmd;
228 return 0;
229 }
230 return -1;
1da177e4
LT
231}
232
cb95d562
TH
233/**
234 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
235 * @pio_mask: pio_mask
236 * @mwdma_mask: mwdma_mask
237 * @udma_mask: udma_mask
238 *
239 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
240 * unsigned int xfer_mask.
241 *
242 * LOCKING:
243 * None.
244 *
245 * RETURNS:
246 * Packed xfer_mask.
247 */
248static unsigned int ata_pack_xfermask(unsigned int pio_mask,
249 unsigned int mwdma_mask,
250 unsigned int udma_mask)
251{
252 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
253 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
254 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
255}
256
c0489e4e
TH
257/**
258 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
259 * @xfer_mask: xfer_mask to unpack
260 * @pio_mask: resulting pio_mask
261 * @mwdma_mask: resulting mwdma_mask
262 * @udma_mask: resulting udma_mask
263 *
264 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
265 * Any NULL distination masks will be ignored.
266 */
267static void ata_unpack_xfermask(unsigned int xfer_mask,
268 unsigned int *pio_mask,
269 unsigned int *mwdma_mask,
270 unsigned int *udma_mask)
271{
272 if (pio_mask)
273 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
274 if (mwdma_mask)
275 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
276 if (udma_mask)
277 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
278}
279
cb95d562
TH
280static const struct ata_xfer_ent {
281 unsigned int shift, bits;
282 u8 base;
283} ata_xfer_tbl[] = {
284 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
285 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
286 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
287 { -1, },
1da177e4
LT
288};
289
290/**
cb95d562
TH
291 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
292 * @xfer_mask: xfer_mask of interest
1da177e4 293 *
cb95d562
TH
294 * Return matching XFER_* value for @xfer_mask. Only the highest
295 * bit of @xfer_mask is considered.
1da177e4
LT
296 *
297 * LOCKING:
298 * None.
299 *
300 * RETURNS:
cb95d562 301 * Matching XFER_* value, 0 if no match found.
1da177e4 302 */
cb95d562
TH
303static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
304{
305 int highbit = fls(xfer_mask) - 1;
306 const struct ata_xfer_ent *ent;
1da177e4 307
cb95d562
TH
308 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
309 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
310 return ent->base + highbit - ent->shift;
311 return 0;
312}
313
314/**
315 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
316 * @xfer_mode: XFER_* of interest
317 *
318 * Return matching xfer_mask for @xfer_mode.
319 *
320 * LOCKING:
321 * None.
322 *
323 * RETURNS:
324 * Matching xfer_mask, 0 if no match found.
325 */
326static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
1da177e4 327{
cb95d562 328 const struct ata_xfer_ent *ent;
1da177e4 329
cb95d562
TH
330 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
331 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
332 return 1 << (ent->shift + xfer_mode - ent->base);
333 return 0;
334}
1da177e4 335
cb95d562
TH
336/**
337 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
338 * @xfer_mode: XFER_* of interest
339 *
340 * Return matching xfer_shift for @xfer_mode.
341 *
342 * LOCKING:
343 * None.
344 *
345 * RETURNS:
346 * Matching xfer_shift, -1 if no match found.
347 */
348static int ata_xfer_mode2shift(unsigned int xfer_mode)
349{
350 const struct ata_xfer_ent *ent;
1da177e4 351
cb95d562
TH
352 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
353 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
354 return ent->shift;
355 return -1;
356}
357
1da177e4 358/**
1da7b0d0
TH
359 * ata_mode_string - convert xfer_mask to string
360 * @xfer_mask: mask of bits supported; only highest bit counts.
1da177e4
LT
361 *
362 * Determine string which represents the highest speed
1da7b0d0 363 * (highest bit in @modemask).
1da177e4
LT
364 *
365 * LOCKING:
366 * None.
367 *
368 * RETURNS:
369 * Constant C string representing highest speed listed in
1da7b0d0 370 * @mode_mask, or the constant C string "<n/a>".
1da177e4 371 */
1da7b0d0 372static const char *ata_mode_string(unsigned int xfer_mask)
1da177e4 373{
75f554bc
TH
374 static const char * const xfer_mode_str[] = {
375 "PIO0",
376 "PIO1",
377 "PIO2",
378 "PIO3",
379 "PIO4",
380 "MWDMA0",
381 "MWDMA1",
382 "MWDMA2",
383 "UDMA/16",
384 "UDMA/25",
385 "UDMA/33",
386 "UDMA/44",
387 "UDMA/66",
388 "UDMA/100",
389 "UDMA/133",
390 "UDMA7",
391 };
1da7b0d0 392 int highbit;
1da177e4 393
1da7b0d0
TH
394 highbit = fls(xfer_mask) - 1;
395 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
396 return xfer_mode_str[highbit];
1da177e4 397 return "<n/a>";
1da177e4
LT
398}
399
0b8efb0a
TH
400static void ata_dev_disable(struct ata_port *ap, struct ata_device *dev)
401{
402 if (ata_dev_present(dev)) {
403 printk(KERN_WARNING "ata%u: dev %u disabled\n",
404 ap->id, dev->devno);
405 dev->class++;
406 }
407}
408
1da177e4
LT
409/**
410 * ata_pio_devchk - PATA device presence detection
411 * @ap: ATA channel to examine
412 * @device: Device to examine (starting at zero)
413 *
414 * This technique was originally described in
415 * Hale Landis's ATADRVR (www.ata-atapi.com), and
416 * later found its way into the ATA/ATAPI spec.
417 *
418 * Write a pattern to the ATA shadow registers,
419 * and if a device is present, it will respond by
420 * correctly storing and echoing back the
421 * ATA shadow register contents.
422 *
423 * LOCKING:
424 * caller.
425 */
426
427static unsigned int ata_pio_devchk(struct ata_port *ap,
428 unsigned int device)
429{
430 struct ata_ioports *ioaddr = &ap->ioaddr;
431 u8 nsect, lbal;
432
433 ap->ops->dev_select(ap, device);
434
435 outb(0x55, ioaddr->nsect_addr);
436 outb(0xaa, ioaddr->lbal_addr);
437
438 outb(0xaa, ioaddr->nsect_addr);
439 outb(0x55, ioaddr->lbal_addr);
440
441 outb(0x55, ioaddr->nsect_addr);
442 outb(0xaa, ioaddr->lbal_addr);
443
444 nsect = inb(ioaddr->nsect_addr);
445 lbal = inb(ioaddr->lbal_addr);
446
447 if ((nsect == 0x55) && (lbal == 0xaa))
448 return 1; /* we found a device */
449
450 return 0; /* nothing found */
451}
452
453/**
454 * ata_mmio_devchk - PATA device presence detection
455 * @ap: ATA channel to examine
456 * @device: Device to examine (starting at zero)
457 *
458 * This technique was originally described in
459 * Hale Landis's ATADRVR (www.ata-atapi.com), and
460 * later found its way into the ATA/ATAPI spec.
461 *
462 * Write a pattern to the ATA shadow registers,
463 * and if a device is present, it will respond by
464 * correctly storing and echoing back the
465 * ATA shadow register contents.
466 *
467 * LOCKING:
468 * caller.
469 */
470
471static unsigned int ata_mmio_devchk(struct ata_port *ap,
472 unsigned int device)
473{
474 struct ata_ioports *ioaddr = &ap->ioaddr;
475 u8 nsect, lbal;
476
477 ap->ops->dev_select(ap, device);
478
479 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
480 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
481
482 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
483 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
484
485 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
486 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
487
488 nsect = readb((void __iomem *) ioaddr->nsect_addr);
489 lbal = readb((void __iomem *) ioaddr->lbal_addr);
490
491 if ((nsect == 0x55) && (lbal == 0xaa))
492 return 1; /* we found a device */
493
494 return 0; /* nothing found */
495}
496
497/**
498 * ata_devchk - PATA device presence detection
499 * @ap: ATA channel to examine
500 * @device: Device to examine (starting at zero)
501 *
502 * Dispatch ATA device presence detection, depending
503 * on whether we are using PIO or MMIO to talk to the
504 * ATA shadow registers.
505 *
506 * LOCKING:
507 * caller.
508 */
509
510static unsigned int ata_devchk(struct ata_port *ap,
511 unsigned int device)
512{
513 if (ap->flags & ATA_FLAG_MMIO)
514 return ata_mmio_devchk(ap, device);
515 return ata_pio_devchk(ap, device);
516}
517
518/**
519 * ata_dev_classify - determine device type based on ATA-spec signature
520 * @tf: ATA taskfile register set for device to be identified
521 *
522 * Determine from taskfile register contents whether a device is
523 * ATA or ATAPI, as per "Signature and persistence" section
524 * of ATA/PI spec (volume 1, sect 5.14).
525 *
526 * LOCKING:
527 * None.
528 *
529 * RETURNS:
530 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
531 * the event of failure.
532 */
533
057ace5e 534unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1da177e4
LT
535{
536 /* Apple's open source Darwin code hints that some devices only
537 * put a proper signature into the LBA mid/high registers,
538 * So, we only check those. It's sufficient for uniqueness.
539 */
540
541 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
542 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
543 DPRINTK("found ATA device by sig\n");
544 return ATA_DEV_ATA;
545 }
546
547 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
548 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
549 DPRINTK("found ATAPI device by sig\n");
550 return ATA_DEV_ATAPI;
551 }
552
553 DPRINTK("unknown device\n");
554 return ATA_DEV_UNKNOWN;
555}
556
557/**
558 * ata_dev_try_classify - Parse returned ATA device signature
559 * @ap: ATA channel to examine
560 * @device: Device to examine (starting at zero)
b4dc7623 561 * @r_err: Value of error register on completion
1da177e4
LT
562 *
563 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
564 * an ATA/ATAPI-defined set of values is placed in the ATA
565 * shadow registers, indicating the results of device detection
566 * and diagnostics.
567 *
568 * Select the ATA device, and read the values from the ATA shadow
569 * registers. Then parse according to the Error register value,
570 * and the spec-defined values examined by ata_dev_classify().
571 *
572 * LOCKING:
573 * caller.
b4dc7623
TH
574 *
575 * RETURNS:
576 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
1da177e4
LT
577 */
578
b4dc7623
TH
579static unsigned int
580ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
1da177e4 581{
1da177e4
LT
582 struct ata_taskfile tf;
583 unsigned int class;
584 u8 err;
585
586 ap->ops->dev_select(ap, device);
587
588 memset(&tf, 0, sizeof(tf));
589
1da177e4 590 ap->ops->tf_read(ap, &tf);
0169e284 591 err = tf.feature;
b4dc7623
TH
592 if (r_err)
593 *r_err = err;
1da177e4
LT
594
595 /* see if device passed diags */
596 if (err == 1)
597 /* do nothing */ ;
598 else if ((device == 0) && (err == 0x81))
599 /* do nothing */ ;
600 else
b4dc7623 601 return ATA_DEV_NONE;
1da177e4 602
b4dc7623 603 /* determine if device is ATA or ATAPI */
1da177e4 604 class = ata_dev_classify(&tf);
b4dc7623 605
1da177e4 606 if (class == ATA_DEV_UNKNOWN)
b4dc7623 607 return ATA_DEV_NONE;
1da177e4 608 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
b4dc7623
TH
609 return ATA_DEV_NONE;
610 return class;
1da177e4
LT
611}
612
613/**
6a62a04d 614 * ata_id_string - Convert IDENTIFY DEVICE page into string
1da177e4
LT
615 * @id: IDENTIFY DEVICE results we will examine
616 * @s: string into which data is output
617 * @ofs: offset into identify device page
618 * @len: length of string to return. must be an even number.
619 *
620 * The strings in the IDENTIFY DEVICE page are broken up into
621 * 16-bit chunks. Run through the string, and output each
622 * 8-bit chunk linearly, regardless of platform.
623 *
624 * LOCKING:
625 * caller.
626 */
627
6a62a04d
TH
628void ata_id_string(const u16 *id, unsigned char *s,
629 unsigned int ofs, unsigned int len)
1da177e4
LT
630{
631 unsigned int c;
632
633 while (len > 0) {
634 c = id[ofs] >> 8;
635 *s = c;
636 s++;
637
638 c = id[ofs] & 0xff;
639 *s = c;
640 s++;
641
642 ofs++;
643 len -= 2;
644 }
645}
646
0e949ff3 647/**
6a62a04d 648 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
0e949ff3
TH
649 * @id: IDENTIFY DEVICE results we will examine
650 * @s: string into which data is output
651 * @ofs: offset into identify device page
652 * @len: length of string to return. must be an odd number.
653 *
6a62a04d 654 * This function is identical to ata_id_string except that it
0e949ff3
TH
655 * trims trailing spaces and terminates the resulting string with
656 * null. @len must be actual maximum length (even number) + 1.
657 *
658 * LOCKING:
659 * caller.
660 */
6a62a04d
TH
661void ata_id_c_string(const u16 *id, unsigned char *s,
662 unsigned int ofs, unsigned int len)
0e949ff3
TH
663{
664 unsigned char *p;
665
666 WARN_ON(!(len & 1));
667
6a62a04d 668 ata_id_string(id, s, ofs, len - 1);
0e949ff3
TH
669
670 p = s + strnlen(s, len - 1);
671 while (p > s && p[-1] == ' ')
672 p--;
673 *p = '\0';
674}
0baab86b 675
2940740b
TH
676static u64 ata_id_n_sectors(const u16 *id)
677{
678 if (ata_id_has_lba(id)) {
679 if (ata_id_has_lba48(id))
680 return ata_id_u64(id, 100);
681 else
682 return ata_id_u32(id, 60);
683 } else {
684 if (ata_id_current_chs_valid(id))
685 return ata_id_u32(id, 57);
686 else
687 return id[1] * id[3] * id[6];
688 }
689}
0baab86b
EF
690
691/**
692 * ata_noop_dev_select - Select device 0/1 on ATA bus
693 * @ap: ATA channel to manipulate
694 * @device: ATA device (numbered from zero) to select
695 *
696 * This function performs no actual function.
697 *
698 * May be used as the dev_select() entry in ata_port_operations.
699 *
700 * LOCKING:
701 * caller.
702 */
1da177e4
LT
703void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
704{
705}
706
0baab86b 707
1da177e4
LT
708/**
709 * ata_std_dev_select - Select device 0/1 on ATA bus
710 * @ap: ATA channel to manipulate
711 * @device: ATA device (numbered from zero) to select
712 *
713 * Use the method defined in the ATA specification to
714 * make either device 0, or device 1, active on the
0baab86b
EF
715 * ATA channel. Works with both PIO and MMIO.
716 *
717 * May be used as the dev_select() entry in ata_port_operations.
1da177e4
LT
718 *
719 * LOCKING:
720 * caller.
721 */
722
723void ata_std_dev_select (struct ata_port *ap, unsigned int device)
724{
725 u8 tmp;
726
727 if (device == 0)
728 tmp = ATA_DEVICE_OBS;
729 else
730 tmp = ATA_DEVICE_OBS | ATA_DEV1;
731
732 if (ap->flags & ATA_FLAG_MMIO) {
733 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
734 } else {
735 outb(tmp, ap->ioaddr.device_addr);
736 }
737 ata_pause(ap); /* needed; also flushes, for mmio */
738}
739
740/**
741 * ata_dev_select - Select device 0/1 on ATA bus
742 * @ap: ATA channel to manipulate
743 * @device: ATA device (numbered from zero) to select
744 * @wait: non-zero to wait for Status register BSY bit to clear
745 * @can_sleep: non-zero if context allows sleeping
746 *
747 * Use the method defined in the ATA specification to
748 * make either device 0, or device 1, active on the
749 * ATA channel.
750 *
751 * This is a high-level version of ata_std_dev_select(),
752 * which additionally provides the services of inserting
753 * the proper pauses and status polling, where needed.
754 *
755 * LOCKING:
756 * caller.
757 */
758
759void ata_dev_select(struct ata_port *ap, unsigned int device,
760 unsigned int wait, unsigned int can_sleep)
761{
762 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
763 ap->id, device, wait);
764
765 if (wait)
766 ata_wait_idle(ap);
767
768 ap->ops->dev_select(ap, device);
769
770 if (wait) {
771 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
772 msleep(150);
773 ata_wait_idle(ap);
774 }
775}
776
777/**
778 * ata_dump_id - IDENTIFY DEVICE info debugging output
0bd3300a 779 * @id: IDENTIFY DEVICE page to dump
1da177e4 780 *
0bd3300a
TH
781 * Dump selected 16-bit words from the given IDENTIFY DEVICE
782 * page.
1da177e4
LT
783 *
784 * LOCKING:
785 * caller.
786 */
787
0bd3300a 788static inline void ata_dump_id(const u16 *id)
1da177e4
LT
789{
790 DPRINTK("49==0x%04x "
791 "53==0x%04x "
792 "63==0x%04x "
793 "64==0x%04x "
794 "75==0x%04x \n",
0bd3300a
TH
795 id[49],
796 id[53],
797 id[63],
798 id[64],
799 id[75]);
1da177e4
LT
800 DPRINTK("80==0x%04x "
801 "81==0x%04x "
802 "82==0x%04x "
803 "83==0x%04x "
804 "84==0x%04x \n",
0bd3300a
TH
805 id[80],
806 id[81],
807 id[82],
808 id[83],
809 id[84]);
1da177e4
LT
810 DPRINTK("88==0x%04x "
811 "93==0x%04x\n",
0bd3300a
TH
812 id[88],
813 id[93]);
1da177e4
LT
814}
815
cb95d562
TH
816/**
817 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
818 * @id: IDENTIFY data to compute xfer mask from
11e29e21 819 *
cb95d562
TH
820 * Compute the xfermask for this device. This is not as trivial
821 * as it seems if we must consider early devices correctly.
822 *
823 * FIXME: pre IDE drive timing (do we care ?).
824 *
825 * LOCKING:
826 * None.
827 *
828 * RETURNS:
829 * Computed xfermask
11e29e21 830 */
cb95d562 831static unsigned int ata_id_xfermask(const u16 *id)
11e29e21 832{
cb95d562 833 unsigned int pio_mask, mwdma_mask, udma_mask;
11e29e21 834
ffa29456 835 /* Usual case. Word 53 indicates word 64 is valid */
cb95d562
TH
836 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
837 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
838 pio_mask <<= 3;
839 pio_mask |= 0x7;
840 } else {
841 /* If word 64 isn't valid then Word 51 high byte holds
842 * the PIO timing number for the maximum. Turn it into
843 * a mask.
844 */
845 pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
846
847 /* But wait.. there's more. Design your standards by
848 * committee and you too can get a free iordy field to
849 * process. However its the speeds not the modes that
850 * are supported... Note drivers using the timing API
851 * will get this right anyway
852 */
11e29e21
AC
853 }
854
cb95d562 855 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
fb21f0d0
TH
856
857 udma_mask = 0;
858 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
859 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
11e29e21 860
cb95d562 861 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
95064379
TH
862}
863
86e45b6b
TH
864/**
865 * ata_port_queue_task - Queue port_task
866 * @ap: The ata_port to queue port_task for
867 *
868 * Schedule @fn(@data) for execution after @delay jiffies using
869 * port_task. There is one port_task per port and it's the
870 * user(low level driver)'s responsibility to make sure that only
871 * one task is active at any given time.
872 *
873 * libata core layer takes care of synchronization between
874 * port_task and EH. ata_port_queue_task() may be ignored for EH
875 * synchronization.
876 *
877 * LOCKING:
878 * Inherited from caller.
879 */
880void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
881 unsigned long delay)
95064379 882{
86e45b6b
TH
883 int rc;
884
2e755f68 885 if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
86e45b6b
TH
886 return;
887
888 PREPARE_WORK(&ap->port_task, fn, data);
889
890 if (!delay)
891 rc = queue_work(ata_wq, &ap->port_task);
892 else
893 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
894
895 /* rc == 0 means that another user is using port task */
896 WARN_ON(rc == 0);
c18d06f8
TH
897}
898
899/**
86e45b6b
TH
900 * ata_port_flush_task - Flush port_task
901 * @ap: The ata_port to flush port_task for
c18d06f8 902 *
86e45b6b
TH
903 * After this function completes, port_task is guranteed not to
904 * be running or scheduled.
c18d06f8
TH
905 *
906 * LOCKING:
907 * Kernel thread context (may sleep)
908 */
86e45b6b 909void ata_port_flush_task(struct ata_port *ap)
c18d06f8 910{
c18d06f8
TH
911 unsigned long flags;
912
913 DPRINTK("ENTER\n");
914
915 spin_lock_irqsave(&ap->host_set->lock, flags);
2e755f68 916 ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
c18d06f8
TH
917 spin_unlock_irqrestore(&ap->host_set->lock, flags);
918
919 DPRINTK("flush #1\n");
920 flush_workqueue(ata_wq);
921
922 /*
923 * At this point, if a task is running, it's guaranteed to see
924 * the FLUSH flag; thus, it will never queue pio tasks again.
925 * Cancel and flush.
926 */
86e45b6b 927 if (!cancel_delayed_work(&ap->port_task)) {
c18d06f8
TH
928 DPRINTK("flush #2\n");
929 flush_workqueue(ata_wq);
930 }
931
932 spin_lock_irqsave(&ap->host_set->lock, flags);
2e755f68 933 ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
c18d06f8
TH
934 spin_unlock_irqrestore(&ap->host_set->lock, flags);
935
936 DPRINTK("EXIT\n");
95064379
TH
937}
938
77853bf2 939void ata_qc_complete_internal(struct ata_queued_cmd *qc)
64f043d8 940{
77853bf2 941 struct completion *waiting = qc->private_data;
64f043d8 942
77853bf2 943 qc->ap->ops->tf_read(qc->ap, &qc->tf);
a2a7a662 944 complete(waiting);
a2a7a662
TH
945}
946
947/**
948 * ata_exec_internal - execute libata internal command
949 * @ap: Port to which the command is sent
950 * @dev: Device to which the command is sent
951 * @tf: Taskfile registers for the command and the result
952 * @dma_dir: Data tranfer direction of the command
953 * @buf: Data buffer of the command
954 * @buflen: Length of data buffer
955 *
956 * Executes libata internal command with timeout. @tf contains
957 * command on entry and result on return. Timeout and error
958 * conditions are reported via return value. No recovery action
959 * is taken after a command times out. It's caller's duty to
960 * clean up after timeout.
961 *
962 * LOCKING:
963 * None. Should be called with kernel context, might sleep.
964 */
965
966static unsigned
967ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
968 struct ata_taskfile *tf,
969 int dma_dir, void *buf, unsigned int buflen)
970{
971 u8 command = tf->command;
972 struct ata_queued_cmd *qc;
973 DECLARE_COMPLETION(wait);
974 unsigned long flags;
77853bf2 975 unsigned int err_mask;
a2a7a662
TH
976
977 spin_lock_irqsave(&ap->host_set->lock, flags);
978
979 qc = ata_qc_new_init(ap, dev);
980 BUG_ON(qc == NULL);
981
982 qc->tf = *tf;
983 qc->dma_dir = dma_dir;
984 if (dma_dir != DMA_NONE) {
985 ata_sg_init_one(qc, buf, buflen);
986 qc->nsect = buflen / ATA_SECT_SIZE;
987 }
988
77853bf2 989 qc->private_data = &wait;
a2a7a662
TH
990 qc->complete_fn = ata_qc_complete_internal;
991
9a3d9eb0
TH
992 qc->err_mask = ata_qc_issue(qc);
993 if (qc->err_mask)
8e436af9 994 ata_qc_complete(qc);
a2a7a662
TH
995
996 spin_unlock_irqrestore(&ap->host_set->lock, flags);
997
998 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
41ade50c
AL
999 ata_port_flush_task(ap);
1000
a2a7a662
TH
1001 spin_lock_irqsave(&ap->host_set->lock, flags);
1002
1003 /* We're racing with irq here. If we lose, the
1004 * following test prevents us from completing the qc
1005 * again. If completion irq occurs after here but
1006 * before the caller cleans up, it will result in a
1007 * spurious interrupt. We can live with that.
1008 */
77853bf2 1009 if (qc->flags & ATA_QCFLAG_ACTIVE) {
11a56d24 1010 qc->err_mask = AC_ERR_TIMEOUT;
a2a7a662
TH
1011 ata_qc_complete(qc);
1012 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1013 ap->id, command);
64f043d8
JG
1014 }
1015
a2a7a662 1016 spin_unlock_irqrestore(&ap->host_set->lock, flags);
64f043d8
JG
1017 }
1018
77853bf2
TH
1019 *tf = qc->tf;
1020 err_mask = qc->err_mask;
1021
1022 ata_qc_free(qc);
1023
1f7dd3e9
TH
1024 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1025 * Until those drivers are fixed, we detect the condition
1026 * here, fail the command with AC_ERR_SYSTEM and reenable the
1027 * port.
1028 *
1029 * Note that this doesn't change any behavior as internal
1030 * command failure results in disabling the device in the
1031 * higher layer for LLDDs without new reset/EH callbacks.
1032 *
1033 * Kill the following code as soon as those drivers are fixed.
1034 */
1035 if (ap->flags & ATA_FLAG_PORT_DISABLED) {
1036 err_mask |= AC_ERR_SYSTEM;
1037 ata_port_probe(ap);
1038 }
1039
77853bf2 1040 return err_mask;
64f043d8
JG
1041}
1042
1bc4ccff
AC
1043/**
1044 * ata_pio_need_iordy - check if iordy needed
1045 * @adev: ATA device
1046 *
1047 * Check if the current speed of the device requires IORDY. Used
1048 * by various controllers for chip configuration.
1049 */
1050
1051unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1052{
1053 int pio;
1054 int speed = adev->pio_mode - XFER_PIO_0;
1055
1056 if (speed < 2)
1057 return 0;
1058 if (speed > 2)
1059 return 1;
2e9edbf8 1060
1bc4ccff
AC
1061 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1062
1063 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1064 pio = adev->id[ATA_ID_EIDE_PIO];
1065 /* Is the speed faster than the drive allows non IORDY ? */
1066 if (pio) {
1067 /* This is cycle times not frequency - watch the logic! */
1068 if (pio > 240) /* PIO2 is 240nS per cycle */
1069 return 1;
1070 return 0;
1071 }
1072 }
1073 return 0;
1074}
1075
1da177e4 1076/**
49016aca
TH
1077 * ata_dev_read_id - Read ID data from the specified device
1078 * @ap: port on which target device resides
1079 * @dev: target device
1080 * @p_class: pointer to class of the target device (may be changed)
1081 * @post_reset: is this read ID post-reset?
d9572b1d 1082 * @p_id: read IDENTIFY page (newly allocated)
1da177e4 1083 *
49016aca
TH
1084 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1085 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
aec5c3c1
TH
1086 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1087 * for pre-ATA4 drives.
1da177e4
LT
1088 *
1089 * LOCKING:
49016aca
TH
1090 * Kernel thread context (may sleep)
1091 *
1092 * RETURNS:
1093 * 0 on success, -errno otherwise.
1da177e4 1094 */
49016aca 1095static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
d9572b1d 1096 unsigned int *p_class, int post_reset, u16 **p_id)
1da177e4 1097{
49016aca 1098 unsigned int class = *p_class;
a0123703 1099 struct ata_taskfile tf;
49016aca 1100 unsigned int err_mask = 0;
d9572b1d 1101 u16 *id;
49016aca
TH
1102 const char *reason;
1103 int rc;
1da177e4 1104
49016aca 1105 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1da177e4 1106
49016aca 1107 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1da177e4 1108
d9572b1d
TH
1109 id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
1110 if (id == NULL) {
1111 rc = -ENOMEM;
1112 reason = "out of memory";
1113 goto err_out;
1114 }
1115
49016aca
TH
1116 retry:
1117 ata_tf_init(ap, &tf, dev->devno);
a0123703 1118
49016aca
TH
1119 switch (class) {
1120 case ATA_DEV_ATA:
a0123703 1121 tf.command = ATA_CMD_ID_ATA;
49016aca
TH
1122 break;
1123 case ATA_DEV_ATAPI:
a0123703 1124 tf.command = ATA_CMD_ID_ATAPI;
49016aca
TH
1125 break;
1126 default:
1127 rc = -ENODEV;
1128 reason = "unsupported class";
1129 goto err_out;
1da177e4
LT
1130 }
1131
a0123703 1132 tf.protocol = ATA_PROT_PIO;
1da177e4 1133
a0123703 1134 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
49016aca 1135 id, sizeof(id[0]) * ATA_ID_WORDS);
a0123703 1136 if (err_mask) {
49016aca
TH
1137 rc = -EIO;
1138 reason = "I/O error";
1da177e4
LT
1139 goto err_out;
1140 }
1141
49016aca 1142 swap_buf_le16(id, ATA_ID_WORDS);
1da177e4 1143
49016aca 1144 /* sanity check */
692785e7 1145 if ((class == ATA_DEV_ATA) != (ata_id_is_ata(id) | ata_id_is_cfa(id))) {
49016aca
TH
1146 rc = -EINVAL;
1147 reason = "device reports illegal type";
1148 goto err_out;
1149 }
1150
1151 if (post_reset && class == ATA_DEV_ATA) {
1152 /*
1153 * The exact sequence expected by certain pre-ATA4 drives is:
1154 * SRST RESET
1155 * IDENTIFY
1156 * INITIALIZE DEVICE PARAMETERS
1157 * anything else..
1158 * Some drives were very specific about that exact sequence.
1159 */
1160 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
00b6f5e9 1161 err_mask = ata_dev_init_params(ap, dev, id[3], id[6]);
49016aca
TH
1162 if (err_mask) {
1163 rc = -EIO;
1164 reason = "INIT_DEV_PARAMS failed";
1165 goto err_out;
1166 }
1167
1168 /* current CHS translation info (id[53-58]) might be
1169 * changed. reread the identify device info.
1170 */
1171 post_reset = 0;
1172 goto retry;
1173 }
1174 }
1175
1176 *p_class = class;
d9572b1d 1177 *p_id = id;
49016aca
TH
1178 return 0;
1179
1180 err_out:
1181 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1182 ap->id, dev->devno, reason);
d9572b1d 1183 kfree(id);
49016aca
TH
1184 return rc;
1185}
1186
4b2f3ede
TH
1187static inline u8 ata_dev_knobble(const struct ata_port *ap,
1188 struct ata_device *dev)
1189{
1190 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1191}
1192
49016aca 1193/**
ffeae418
TH
1194 * ata_dev_configure - Configure the specified ATA/ATAPI device
1195 * @ap: Port on which target device resides
1196 * @dev: Target device to configure
4c2d721a 1197 * @print_info: Enable device info printout
ffeae418
TH
1198 *
1199 * Configure @dev according to @dev->id. Generic and low-level
1200 * driver specific fixups are also applied.
49016aca
TH
1201 *
1202 * LOCKING:
ffeae418
TH
1203 * Kernel thread context (may sleep)
1204 *
1205 * RETURNS:
1206 * 0 on success, -errno otherwise
49016aca 1207 */
4c2d721a
TH
1208static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
1209 int print_info)
49016aca 1210{
1148c3a7 1211 const u16 *id = dev->id;
ff8854b2 1212 unsigned int xfer_mask;
49016aca
TH
1213 int i, rc;
1214
1215 if (!ata_dev_present(dev)) {
1216 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
ffeae418
TH
1217 ap->id, dev->devno);
1218 return 0;
49016aca
TH
1219 }
1220
ffeae418 1221 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
49016aca 1222
c39f5ebe
TH
1223 /* print device capabilities */
1224 if (print_info)
1225 printk(KERN_DEBUG "ata%u: dev %u cfg 49:%04x 82:%04x 83:%04x "
1226 "84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1227 ap->id, dev->devno, id[49], id[82], id[83],
1228 id[84], id[85], id[86], id[87], id[88]);
1229
208a9933
TH
1230 /* initialize to-be-configured parameters */
1231 dev->flags = 0;
1232 dev->max_sectors = 0;
1233 dev->cdb_len = 0;
1234 dev->n_sectors = 0;
1235 dev->cylinders = 0;
1236 dev->heads = 0;
1237 dev->sectors = 0;
1da177e4
LT
1238
1239 /*
1240 * common ATA, ATAPI feature tests
1241 */
1242
ff8854b2 1243 /* find max transfer mode; for printk only */
1148c3a7 1244 xfer_mask = ata_id_xfermask(id);
1da177e4 1245
1148c3a7 1246 ata_dump_id(id);
1da177e4
LT
1247
1248 /* ATA-specific feature tests */
1249 if (dev->class == ATA_DEV_ATA) {
1148c3a7 1250 dev->n_sectors = ata_id_n_sectors(id);
2940740b 1251
1148c3a7 1252 if (ata_id_has_lba(id)) {
4c2d721a 1253 const char *lba_desc;
8bf62ece 1254
4c2d721a
TH
1255 lba_desc = "LBA";
1256 dev->flags |= ATA_DFLAG_LBA;
1148c3a7 1257 if (ata_id_has_lba48(id)) {
8bf62ece 1258 dev->flags |= ATA_DFLAG_LBA48;
4c2d721a
TH
1259 lba_desc = "LBA48";
1260 }
8bf62ece
AL
1261
1262 /* print device info to dmesg */
4c2d721a
TH
1263 if (print_info)
1264 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1265 "max %s, %Lu sectors: %s\n",
1266 ap->id, dev->devno,
1148c3a7 1267 ata_id_major_version(id),
ff8854b2 1268 ata_mode_string(xfer_mask),
4c2d721a
TH
1269 (unsigned long long)dev->n_sectors,
1270 lba_desc);
ffeae418 1271 } else {
8bf62ece
AL
1272 /* CHS */
1273
1274 /* Default translation */
1148c3a7
TH
1275 dev->cylinders = id[1];
1276 dev->heads = id[3];
1277 dev->sectors = id[6];
8bf62ece 1278
1148c3a7 1279 if (ata_id_current_chs_valid(id)) {
8bf62ece 1280 /* Current CHS translation is valid. */
1148c3a7
TH
1281 dev->cylinders = id[54];
1282 dev->heads = id[55];
1283 dev->sectors = id[56];
8bf62ece
AL
1284 }
1285
1286 /* print device info to dmesg */
4c2d721a
TH
1287 if (print_info)
1288 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1289 "max %s, %Lu sectors: CHS %u/%u/%u\n",
1290 ap->id, dev->devno,
1148c3a7 1291 ata_id_major_version(id),
ff8854b2 1292 ata_mode_string(xfer_mask),
4c2d721a
TH
1293 (unsigned long long)dev->n_sectors,
1294 dev->cylinders, dev->heads, dev->sectors);
1da177e4
LT
1295 }
1296
07f6f7d0
AL
1297 if (dev->id[59] & 0x100) {
1298 dev->multi_count = dev->id[59] & 0xff;
1299 DPRINTK("ata%u: dev %u multi count %u\n",
999bb6f4 1300 ap->id, dev->devno, dev->multi_count);
07f6f7d0
AL
1301 }
1302
13ee4628 1303 dev->cdb_len = 16;
1da177e4
LT
1304 }
1305
1306 /* ATAPI-specific feature tests */
2c13b7ce 1307 else if (dev->class == ATA_DEV_ATAPI) {
1148c3a7 1308 rc = atapi_cdb_len(id);
1da177e4
LT
1309 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1310 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
ffeae418 1311 rc = -EINVAL;
1da177e4
LT
1312 goto err_out_nosup;
1313 }
6e7846e9 1314 dev->cdb_len = (unsigned int) rc;
1da177e4 1315
312f7da2
AL
1316 if (ata_id_cdb_intr(dev->id))
1317 dev->flags |= ATA_DFLAG_CDB_INTR;
1318
1da177e4 1319 /* print device info to dmesg */
4c2d721a
TH
1320 if (print_info)
1321 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
ff8854b2 1322 ap->id, dev->devno, ata_mode_string(xfer_mask));
1da177e4
LT
1323 }
1324
6e7846e9
TH
1325 ap->host->max_cmd_len = 0;
1326 for (i = 0; i < ATA_MAX_DEVICES; i++)
1327 ap->host->max_cmd_len = max_t(unsigned int,
1328 ap->host->max_cmd_len,
1329 ap->device[i].cdb_len);
1330
6f2f3812 1331 /* limit bridge transfers to udma5, 200 sectors */
4b2f3ede 1332 if (ata_dev_knobble(ap, dev)) {
4c2d721a
TH
1333 if (print_info)
1334 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1335 ap->id, dev->devno);
5a529139 1336 dev->udma_mask &= ATA_UDMA5;
4b2f3ede 1337 dev->max_sectors = ATA_MAX_SECTORS;
6f2f3812
BC
1338 }
1339
1340 if (ap->ops->dev_config)
4b2f3ede
TH
1341 ap->ops->dev_config(ap, dev);
1342
1da177e4 1343 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
ffeae418 1344 return 0;
1da177e4
LT
1345
1346err_out_nosup:
1da177e4 1347 DPRINTK("EXIT, err\n");
ffeae418 1348 return rc;
6f2f3812
BC
1349}
1350
1da177e4
LT
1351/**
1352 * ata_bus_probe - Reset and probe ATA bus
1353 * @ap: Bus to probe
1354 *
0cba632b
JG
1355 * Master ATA bus probing function. Initiates a hardware-dependent
1356 * bus reset, then attempts to identify any devices found on
1357 * the bus.
1358 *
1da177e4 1359 * LOCKING:
0cba632b 1360 * PCI/etc. bus probe sem.
1da177e4
LT
1361 *
1362 * RETURNS:
1363 * Zero on success, non-zero on error.
1364 */
1365
1366static int ata_bus_probe(struct ata_port *ap)
1367{
28ca5c57
TH
1368 unsigned int classes[ATA_MAX_DEVICES];
1369 unsigned int i, rc, found = 0;
1da177e4 1370
28ca5c57 1371 ata_port_probe(ap);
c19ba8af 1372
2044470c
TH
1373 /* reset and determine device classes */
1374 for (i = 0; i < ATA_MAX_DEVICES; i++)
1375 classes[i] = ATA_DEV_UNKNOWN;
c19ba8af 1376
2044470c 1377 if (ap->ops->probe_reset) {
c19ba8af 1378 rc = ap->ops->probe_reset(ap, classes);
28ca5c57
TH
1379 if (rc) {
1380 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1381 return rc;
c19ba8af 1382 }
28ca5c57 1383 } else {
c19ba8af
TH
1384 ap->ops->phy_reset(ap);
1385
2044470c
TH
1386 if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
1387 for (i = 0; i < ATA_MAX_DEVICES; i++)
28ca5c57 1388 classes[i] = ap->device[i].class;
2044470c 1389
28ca5c57
TH
1390 ata_port_probe(ap);
1391 }
1da177e4 1392
2044470c
TH
1393 for (i = 0; i < ATA_MAX_DEVICES; i++)
1394 if (classes[i] == ATA_DEV_UNKNOWN)
1395 classes[i] = ATA_DEV_NONE;
1396
28ca5c57 1397 /* read IDENTIFY page and configure devices */
1da177e4 1398 for (i = 0; i < ATA_MAX_DEVICES; i++) {
ffeae418
TH
1399 struct ata_device *dev = &ap->device[i];
1400
28ca5c57
TH
1401 dev->class = classes[i];
1402
ffeae418
TH
1403 if (!ata_dev_present(dev))
1404 continue;
1405
1406 WARN_ON(dev->id != NULL);
1407 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1408 dev->class = ATA_DEV_NONE;
1409 continue;
1410 }
1411
4c2d721a 1412 if (ata_dev_configure(ap, dev, 1)) {
fcef978f 1413 ata_dev_disable(ap, dev);
ffeae418 1414 continue;
1da177e4 1415 }
ffeae418 1416
ffeae418 1417 found = 1;
1da177e4
LT
1418 }
1419
28ca5c57 1420 if (!found)
1da177e4
LT
1421 goto err_out_disable;
1422
e35a9e01
AC
1423 if (ap->ops->set_mode)
1424 ap->ops->set_mode(ap);
1425 else
1426 ata_set_mode(ap);
1427
1da177e4
LT
1428 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1429 goto err_out_disable;
1430
1431 return 0;
1432
1433err_out_disable:
1434 ap->ops->port_disable(ap);
1da177e4
LT
1435 return -1;
1436}
1437
1438/**
0cba632b
JG
1439 * ata_port_probe - Mark port as enabled
1440 * @ap: Port for which we indicate enablement
1da177e4 1441 *
0cba632b
JG
1442 * Modify @ap data structure such that the system
1443 * thinks that the entire port is enabled.
1444 *
1445 * LOCKING: host_set lock, or some other form of
1446 * serialization.
1da177e4
LT
1447 */
1448
1449void ata_port_probe(struct ata_port *ap)
1450{
1451 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1452}
1453
3be680b7
TH
1454/**
1455 * sata_print_link_status - Print SATA link status
1456 * @ap: SATA port to printk link status about
1457 *
1458 * This function prints link speed and status of a SATA link.
1459 *
1460 * LOCKING:
1461 * None.
1462 */
1463static void sata_print_link_status(struct ata_port *ap)
1464{
1465 u32 sstatus, tmp;
1466 const char *speed;
1467
1468 if (!ap->ops->scr_read)
1469 return;
1470
1471 sstatus = scr_read(ap, SCR_STATUS);
1472
1473 if (sata_dev_present(ap)) {
1474 tmp = (sstatus >> 4) & 0xf;
1475 if (tmp & (1 << 0))
1476 speed = "1.5";
1477 else if (tmp & (1 << 1))
1478 speed = "3.0";
1479 else
1480 speed = "<unknown>";
1481 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1482 ap->id, speed, sstatus);
1483 } else {
1484 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1485 ap->id, sstatus);
1486 }
1487}
1488
1da177e4 1489/**
780a87f7
JG
1490 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1491 * @ap: SATA port associated with target SATA PHY.
1da177e4 1492 *
780a87f7
JG
1493 * This function issues commands to standard SATA Sxxx
1494 * PHY registers, to wake up the phy (and device), and
1495 * clear any reset condition.
1da177e4
LT
1496 *
1497 * LOCKING:
0cba632b 1498 * PCI/etc. bus probe sem.
1da177e4
LT
1499 *
1500 */
1501void __sata_phy_reset(struct ata_port *ap)
1502{
1503 u32 sstatus;
1504 unsigned long timeout = jiffies + (HZ * 5);
1505
1506 if (ap->flags & ATA_FLAG_SATA_RESET) {
cdcca89e
BR
1507 /* issue phy wake/reset */
1508 scr_write_flush(ap, SCR_CONTROL, 0x301);
62ba2841
TH
1509 /* Couldn't find anything in SATA I/II specs, but
1510 * AHCI-1.1 10.4.2 says at least 1 ms. */
1511 mdelay(1);
1da177e4 1512 }
cdcca89e 1513 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1da177e4
LT
1514
1515 /* wait for phy to become ready, if necessary */
1516 do {
1517 msleep(200);
1518 sstatus = scr_read(ap, SCR_STATUS);
1519 if ((sstatus & 0xf) != 1)
1520 break;
1521 } while (time_before(jiffies, timeout));
1522
3be680b7
TH
1523 /* print link status */
1524 sata_print_link_status(ap);
656563e3 1525
3be680b7
TH
1526 /* TODO: phy layer with polling, timeouts, etc. */
1527 if (sata_dev_present(ap))
1da177e4 1528 ata_port_probe(ap);
3be680b7 1529 else
1da177e4 1530 ata_port_disable(ap);
1da177e4
LT
1531
1532 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1533 return;
1534
1535 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1536 ata_port_disable(ap);
1537 return;
1538 }
1539
1540 ap->cbl = ATA_CBL_SATA;
1541}
1542
1543/**
780a87f7
JG
1544 * sata_phy_reset - Reset SATA bus.
1545 * @ap: SATA port associated with target SATA PHY.
1da177e4 1546 *
780a87f7
JG
1547 * This function resets the SATA bus, and then probes
1548 * the bus for devices.
1da177e4
LT
1549 *
1550 * LOCKING:
0cba632b 1551 * PCI/etc. bus probe sem.
1da177e4
LT
1552 *
1553 */
1554void sata_phy_reset(struct ata_port *ap)
1555{
1556 __sata_phy_reset(ap);
1557 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1558 return;
1559 ata_bus_reset(ap);
1560}
1561
ebdfca6e
AC
1562/**
1563 * ata_dev_pair - return other device on cable
1564 * @ap: port
1565 * @adev: device
1566 *
1567 * Obtain the other device on the same cable, or if none is
1568 * present NULL is returned
1569 */
2e9edbf8 1570
ebdfca6e
AC
1571struct ata_device *ata_dev_pair(struct ata_port *ap, struct ata_device *adev)
1572{
1573 struct ata_device *pair = &ap->device[1 - adev->devno];
1574 if (!ata_dev_present(pair))
1575 return NULL;
1576 return pair;
1577}
1578
1da177e4 1579/**
780a87f7
JG
1580 * ata_port_disable - Disable port.
1581 * @ap: Port to be disabled.
1da177e4 1582 *
780a87f7
JG
1583 * Modify @ap data structure such that the system
1584 * thinks that the entire port is disabled, and should
1585 * never attempt to probe or communicate with devices
1586 * on this port.
1587 *
1588 * LOCKING: host_set lock, or some other form of
1589 * serialization.
1da177e4
LT
1590 */
1591
1592void ata_port_disable(struct ata_port *ap)
1593{
1594 ap->device[0].class = ATA_DEV_NONE;
1595 ap->device[1].class = ATA_DEV_NONE;
1596 ap->flags |= ATA_FLAG_PORT_DISABLED;
1597}
1598
452503f9
AC
1599/*
1600 * This mode timing computation functionality is ported over from
1601 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1602 */
1603/*
1604 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1605 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1606 * for PIO 5, which is a nonstandard extension and UDMA6, which
2e9edbf8 1607 * is currently supported only by Maxtor drives.
452503f9
AC
1608 */
1609
1610static const struct ata_timing ata_timing[] = {
1611
1612 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1613 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1614 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1615 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1616
1617 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1618 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1619 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1620
1621/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
2e9edbf8 1622
452503f9
AC
1623 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1624 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1625 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
2e9edbf8 1626
452503f9
AC
1627 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1628 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1629 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1630
1631/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1632 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1633 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1634
1635 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1636 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1637 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1638
1639/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1640
1641 { 0xFF }
1642};
1643
1644#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1645#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1646
1647static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1648{
1649 q->setup = EZ(t->setup * 1000, T);
1650 q->act8b = EZ(t->act8b * 1000, T);
1651 q->rec8b = EZ(t->rec8b * 1000, T);
1652 q->cyc8b = EZ(t->cyc8b * 1000, T);
1653 q->active = EZ(t->active * 1000, T);
1654 q->recover = EZ(t->recover * 1000, T);
1655 q->cycle = EZ(t->cycle * 1000, T);
1656 q->udma = EZ(t->udma * 1000, UT);
1657}
1658
1659void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1660 struct ata_timing *m, unsigned int what)
1661{
1662 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1663 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1664 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1665 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1666 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1667 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1668 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1669 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1670}
1671
1672static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1673{
1674 const struct ata_timing *t;
1675
1676 for (t = ata_timing; t->mode != speed; t++)
91190758 1677 if (t->mode == 0xFF)
452503f9 1678 return NULL;
2e9edbf8 1679 return t;
452503f9
AC
1680}
1681
1682int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1683 struct ata_timing *t, int T, int UT)
1684{
1685 const struct ata_timing *s;
1686 struct ata_timing p;
1687
1688 /*
2e9edbf8 1689 * Find the mode.
75b1f2f8 1690 */
452503f9
AC
1691
1692 if (!(s = ata_timing_find_mode(speed)))
1693 return -EINVAL;
1694
75b1f2f8
AL
1695 memcpy(t, s, sizeof(*s));
1696
452503f9
AC
1697 /*
1698 * If the drive is an EIDE drive, it can tell us it needs extended
1699 * PIO/MW_DMA cycle timing.
1700 */
1701
1702 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1703 memset(&p, 0, sizeof(p));
1704 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1705 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1706 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1707 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1708 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1709 }
1710 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1711 }
1712
1713 /*
1714 * Convert the timing to bus clock counts.
1715 */
1716
75b1f2f8 1717 ata_timing_quantize(t, t, T, UT);
452503f9
AC
1718
1719 /*
c893a3ae
RD
1720 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1721 * S.M.A.R.T * and some other commands. We have to ensure that the
1722 * DMA cycle timing is slower/equal than the fastest PIO timing.
452503f9
AC
1723 */
1724
1725 if (speed > XFER_PIO_4) {
1726 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1727 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1728 }
1729
1730 /*
c893a3ae 1731 * Lengthen active & recovery time so that cycle time is correct.
452503f9
AC
1732 */
1733
1734 if (t->act8b + t->rec8b < t->cyc8b) {
1735 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1736 t->rec8b = t->cyc8b - t->act8b;
1737 }
1738
1739 if (t->active + t->recover < t->cycle) {
1740 t->active += (t->cycle - (t->active + t->recover)) / 2;
1741 t->recover = t->cycle - t->active;
1742 }
1743
1744 return 0;
1745}
1746
83206a29 1747static int ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1da177e4 1748{
83206a29
TH
1749 unsigned int err_mask;
1750 int rc;
1da177e4
LT
1751
1752 if (dev->xfer_shift == ATA_SHIFT_PIO)
1753 dev->flags |= ATA_DFLAG_PIO;
1754
83206a29
TH
1755 err_mask = ata_dev_set_xfermode(ap, dev);
1756 if (err_mask) {
1757 printk(KERN_ERR
1758 "ata%u: failed to set xfermode (err_mask=0x%x)\n",
1759 ap->id, err_mask);
1760 return -EIO;
1761 }
1da177e4 1762
83206a29
TH
1763 rc = ata_dev_revalidate(ap, dev, 0);
1764 if (rc) {
1765 printk(KERN_ERR
1766 "ata%u: failed to revalidate after set xfermode\n",
1767 ap->id);
1768 return rc;
48a8a14f 1769 }
1da177e4 1770
23e71c3d
TH
1771 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
1772 dev->xfer_shift, (int)dev->xfer_mode);
1da177e4
LT
1773
1774 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
23e71c3d
TH
1775 ap->id, dev->devno,
1776 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
83206a29 1777 return 0;
1da177e4
LT
1778}
1779
1780static int ata_host_set_pio(struct ata_port *ap)
1781{
a6d5a51c 1782 int i;
1da177e4
LT
1783
1784 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1785 struct ata_device *dev = &ap->device[i];
a6d5a51c
TH
1786
1787 if (!ata_dev_present(dev))
1788 continue;
1789
1790 if (!dev->pio_mode) {
88f93a31 1791 printk(KERN_WARNING "ata%u: no PIO support for device %d.\n", ap->id, i);
a6d5a51c 1792 return -1;
1da177e4 1793 }
a6d5a51c
TH
1794
1795 dev->xfer_mode = dev->pio_mode;
1796 dev->xfer_shift = ATA_SHIFT_PIO;
1797 if (ap->ops->set_piomode)
1798 ap->ops->set_piomode(ap, dev);
1da177e4
LT
1799 }
1800
1801 return 0;
1802}
1803
a6d5a51c 1804static void ata_host_set_dma(struct ata_port *ap)
1da177e4
LT
1805{
1806 int i;
1807
1808 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1809 struct ata_device *dev = &ap->device[i];
a6d5a51c
TH
1810
1811 if (!ata_dev_present(dev) || !dev->dma_mode)
1812 continue;
1813
1814 dev->xfer_mode = dev->dma_mode;
1815 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
1816 if (ap->ops->set_dmamode)
1817 ap->ops->set_dmamode(ap, dev);
1da177e4
LT
1818 }
1819}
1820
1821/**
1822 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1823 * @ap: port on which timings will be programmed
1824 *
780a87f7
JG
1825 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1826 *
1da177e4 1827 * LOCKING:
0cba632b 1828 * PCI/etc. bus probe sem.
1da177e4
LT
1829 */
1830static void ata_set_mode(struct ata_port *ap)
1831{
5444a6f4 1832 int i, rc, used_dma = 0;
1da177e4 1833
a6d5a51c
TH
1834 /* step 1: calculate xfer_mask */
1835 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1836 struct ata_device *dev = &ap->device[i];
acf356b1 1837 unsigned int pio_mask, dma_mask;
a6d5a51c
TH
1838
1839 if (!ata_dev_present(dev))
1840 continue;
1841
acf356b1 1842 ata_dev_xfermask(ap, dev);
1da177e4 1843
acf356b1 1844 /* TODO: let LLDD filter dev->*_mask here */
1da177e4 1845
acf356b1
TH
1846 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
1847 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
1848 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
1849 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
5444a6f4
AC
1850
1851 if (dev->dma_mode)
1852 used_dma = 1;
a6d5a51c
TH
1853 }
1854
1855 /* step 2: always set host PIO timings */
1856 rc = ata_host_set_pio(ap);
1da177e4
LT
1857 if (rc)
1858 goto err_out;
1859
a6d5a51c
TH
1860 /* step 3: set host DMA timings */
1861 ata_host_set_dma(ap);
1da177e4
LT
1862
1863 /* step 4: update devices' xfer mode */
83206a29
TH
1864 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1865 struct ata_device *dev = &ap->device[i];
1da177e4 1866
83206a29
TH
1867 if (!ata_dev_present(dev))
1868 continue;
1869
1870 if (ata_dev_set_mode(ap, dev))
1871 goto err_out;
1872 }
1da177e4 1873
5444a6f4
AC
1874 /*
1875 * Record simplex status. If we selected DMA then the other
1876 * host channels are not permitted to do so.
1877 */
1878
1879 if (used_dma && (ap->host_set->flags & ATA_HOST_SIMPLEX))
1880 ap->host_set->simplex_claimed = 1;
1881
1882 /*
1883 * Chip specific finalisation
1884 */
1da177e4
LT
1885 if (ap->ops->post_set_mode)
1886 ap->ops->post_set_mode(ap);
1887
1da177e4
LT
1888 return;
1889
1890err_out:
1891 ata_port_disable(ap);
1892}
1893
1fdffbce
JG
1894/**
1895 * ata_tf_to_host - issue ATA taskfile to host controller
1896 * @ap: port to which command is being issued
1897 * @tf: ATA taskfile register set
1898 *
1899 * Issues ATA taskfile register set to ATA host controller,
1900 * with proper synchronization with interrupt handler and
1901 * other threads.
1902 *
1903 * LOCKING:
1904 * spin_lock_irqsave(host_set lock)
1905 */
1906
1907static inline void ata_tf_to_host(struct ata_port *ap,
1908 const struct ata_taskfile *tf)
1909{
1910 ap->ops->tf_load(ap, tf);
1911 ap->ops->exec_command(ap, tf);
1912}
1913
1da177e4
LT
1914/**
1915 * ata_busy_sleep - sleep until BSY clears, or timeout
1916 * @ap: port containing status register to be polled
1917 * @tmout_pat: impatience timeout
1918 * @tmout: overall timeout
1919 *
780a87f7
JG
1920 * Sleep until ATA Status register bit BSY clears,
1921 * or a timeout occurs.
1922 *
1923 * LOCKING: None.
1da177e4
LT
1924 */
1925
6f8b9958
TH
1926unsigned int ata_busy_sleep (struct ata_port *ap,
1927 unsigned long tmout_pat, unsigned long tmout)
1da177e4
LT
1928{
1929 unsigned long timer_start, timeout;
1930 u8 status;
1931
1932 status = ata_busy_wait(ap, ATA_BUSY, 300);
1933 timer_start = jiffies;
1934 timeout = timer_start + tmout_pat;
1935 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1936 msleep(50);
1937 status = ata_busy_wait(ap, ATA_BUSY, 3);
1938 }
1939
1940 if (status & ATA_BUSY)
1941 printk(KERN_WARNING "ata%u is slow to respond, "
1942 "please be patient\n", ap->id);
1943
1944 timeout = timer_start + tmout;
1945 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1946 msleep(50);
1947 status = ata_chk_status(ap);
1948 }
1949
1950 if (status & ATA_BUSY) {
1951 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1952 ap->id, tmout / HZ);
1953 return 1;
1954 }
1955
1956 return 0;
1957}
1958
1959static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1960{
1961 struct ata_ioports *ioaddr = &ap->ioaddr;
1962 unsigned int dev0 = devmask & (1 << 0);
1963 unsigned int dev1 = devmask & (1 << 1);
1964 unsigned long timeout;
1965
1966 /* if device 0 was found in ata_devchk, wait for its
1967 * BSY bit to clear
1968 */
1969 if (dev0)
1970 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1971
1972 /* if device 1 was found in ata_devchk, wait for
1973 * register access, then wait for BSY to clear
1974 */
1975 timeout = jiffies + ATA_TMOUT_BOOT;
1976 while (dev1) {
1977 u8 nsect, lbal;
1978
1979 ap->ops->dev_select(ap, 1);
1980 if (ap->flags & ATA_FLAG_MMIO) {
1981 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1982 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1983 } else {
1984 nsect = inb(ioaddr->nsect_addr);
1985 lbal = inb(ioaddr->lbal_addr);
1986 }
1987 if ((nsect == 1) && (lbal == 1))
1988 break;
1989 if (time_after(jiffies, timeout)) {
1990 dev1 = 0;
1991 break;
1992 }
1993 msleep(50); /* give drive a breather */
1994 }
1995 if (dev1)
1996 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1997
1998 /* is all this really necessary? */
1999 ap->ops->dev_select(ap, 0);
2000 if (dev1)
2001 ap->ops->dev_select(ap, 1);
2002 if (dev0)
2003 ap->ops->dev_select(ap, 0);
2004}
2005
1da177e4
LT
2006static unsigned int ata_bus_softreset(struct ata_port *ap,
2007 unsigned int devmask)
2008{
2009 struct ata_ioports *ioaddr = &ap->ioaddr;
2010
2011 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2012
2013 /* software reset. causes dev0 to be selected */
2014 if (ap->flags & ATA_FLAG_MMIO) {
2015 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2016 udelay(20); /* FIXME: flush */
2017 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2018 udelay(20); /* FIXME: flush */
2019 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2020 } else {
2021 outb(ap->ctl, ioaddr->ctl_addr);
2022 udelay(10);
2023 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2024 udelay(10);
2025 outb(ap->ctl, ioaddr->ctl_addr);
2026 }
2027
2028 /* spec mandates ">= 2ms" before checking status.
2029 * We wait 150ms, because that was the magic delay used for
2030 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2031 * between when the ATA command register is written, and then
2032 * status is checked. Because waiting for "a while" before
2033 * checking status is fine, post SRST, we perform this magic
2034 * delay here as well.
09c7ad79
AC
2035 *
2036 * Old drivers/ide uses the 2mS rule and then waits for ready
1da177e4
LT
2037 */
2038 msleep(150);
2039
2e9edbf8 2040 /* Before we perform post reset processing we want to see if
298a41ca
TH
2041 * the bus shows 0xFF because the odd clown forgets the D7
2042 * pulldown resistor.
2043 */
09c7ad79 2044 if (ata_check_status(ap) == 0xFF)
298a41ca 2045 return AC_ERR_OTHER;
09c7ad79 2046
1da177e4
LT
2047 ata_bus_post_reset(ap, devmask);
2048
2049 return 0;
2050}
2051
2052/**
2053 * ata_bus_reset - reset host port and associated ATA channel
2054 * @ap: port to reset
2055 *
2056 * This is typically the first time we actually start issuing
2057 * commands to the ATA channel. We wait for BSY to clear, then
2058 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2059 * result. Determine what devices, if any, are on the channel
2060 * by looking at the device 0/1 error register. Look at the signature
2061 * stored in each device's taskfile registers, to determine if
2062 * the device is ATA or ATAPI.
2063 *
2064 * LOCKING:
0cba632b
JG
2065 * PCI/etc. bus probe sem.
2066 * Obtains host_set lock.
1da177e4
LT
2067 *
2068 * SIDE EFFECTS:
2069 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2070 */
2071
2072void ata_bus_reset(struct ata_port *ap)
2073{
2074 struct ata_ioports *ioaddr = &ap->ioaddr;
2075 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2076 u8 err;
aec5c3c1 2077 unsigned int dev0, dev1 = 0, devmask = 0;
1da177e4
LT
2078
2079 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2080
2081 /* determine if device 0/1 are present */
2082 if (ap->flags & ATA_FLAG_SATA_RESET)
2083 dev0 = 1;
2084 else {
2085 dev0 = ata_devchk(ap, 0);
2086 if (slave_possible)
2087 dev1 = ata_devchk(ap, 1);
2088 }
2089
2090 if (dev0)
2091 devmask |= (1 << 0);
2092 if (dev1)
2093 devmask |= (1 << 1);
2094
2095 /* select device 0 again */
2096 ap->ops->dev_select(ap, 0);
2097
2098 /* issue bus reset */
2099 if (ap->flags & ATA_FLAG_SRST)
aec5c3c1
TH
2100 if (ata_bus_softreset(ap, devmask))
2101 goto err_out;
1da177e4
LT
2102
2103 /*
2104 * determine by signature whether we have ATA or ATAPI devices
2105 */
b4dc7623 2106 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
1da177e4 2107 if ((slave_possible) && (err != 0x81))
b4dc7623 2108 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
1da177e4
LT
2109
2110 /* re-enable interrupts */
2111 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2112 ata_irq_on(ap);
2113
2114 /* is double-select really necessary? */
2115 if (ap->device[1].class != ATA_DEV_NONE)
2116 ap->ops->dev_select(ap, 1);
2117 if (ap->device[0].class != ATA_DEV_NONE)
2118 ap->ops->dev_select(ap, 0);
2119
2120 /* if no devices were detected, disable this port */
2121 if ((ap->device[0].class == ATA_DEV_NONE) &&
2122 (ap->device[1].class == ATA_DEV_NONE))
2123 goto err_out;
2124
2125 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2126 /* set up device control for ATA_FLAG_SATA_RESET */
2127 if (ap->flags & ATA_FLAG_MMIO)
2128 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2129 else
2130 outb(ap->ctl, ioaddr->ctl_addr);
2131 }
2132
2133 DPRINTK("EXIT\n");
2134 return;
2135
2136err_out:
2137 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2138 ap->ops->port_disable(ap);
2139
2140 DPRINTK("EXIT\n");
2141}
2142
7a7921e8
TH
2143static int sata_phy_resume(struct ata_port *ap)
2144{
2145 unsigned long timeout = jiffies + (HZ * 5);
2146 u32 sstatus;
2147
2148 scr_write_flush(ap, SCR_CONTROL, 0x300);
2149
2150 /* Wait for phy to become ready, if necessary. */
2151 do {
2152 msleep(200);
2153 sstatus = scr_read(ap, SCR_STATUS);
2154 if ((sstatus & 0xf) != 1)
2155 return 0;
2156 } while (time_before(jiffies, timeout));
2157
2158 return -1;
2159}
2160
8a19ac89
TH
2161/**
2162 * ata_std_probeinit - initialize probing
2163 * @ap: port to be probed
2164 *
2165 * @ap is about to be probed. Initialize it. This function is
2166 * to be used as standard callback for ata_drive_probe_reset().
3a39746a
TH
2167 *
2168 * NOTE!!! Do not use this function as probeinit if a low level
2169 * driver implements only hardreset. Just pass NULL as probeinit
2170 * in that case. Using this function is probably okay but doing
2171 * so makes reset sequence different from the original
2172 * ->phy_reset implementation and Jeff nervous. :-P
8a19ac89 2173 */
17efc5f7 2174void ata_std_probeinit(struct ata_port *ap)
8a19ac89 2175{
17efc5f7 2176 if ((ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read) {
8a19ac89 2177 sata_phy_resume(ap);
3a39746a
TH
2178 if (sata_dev_present(ap))
2179 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2180 }
8a19ac89
TH
2181}
2182
c2bd5804
TH
2183/**
2184 * ata_std_softreset - reset host port via ATA SRST
2185 * @ap: port to reset
2186 * @verbose: fail verbosely
2187 * @classes: resulting classes of attached devices
2188 *
2189 * Reset host port using ATA SRST. This function is to be used
2190 * as standard callback for ata_drive_*_reset() functions.
2191 *
2192 * LOCKING:
2193 * Kernel thread context (may sleep)
2194 *
2195 * RETURNS:
2196 * 0 on success, -errno otherwise.
2197 */
2198int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2199{
2200 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2201 unsigned int devmask = 0, err_mask;
2202 u8 err;
2203
2204 DPRINTK("ENTER\n");
2205
3a39746a
TH
2206 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2207 classes[0] = ATA_DEV_NONE;
2208 goto out;
2209 }
2210
c2bd5804
TH
2211 /* determine if device 0/1 are present */
2212 if (ata_devchk(ap, 0))
2213 devmask |= (1 << 0);
2214 if (slave_possible && ata_devchk(ap, 1))
2215 devmask |= (1 << 1);
2216
c2bd5804
TH
2217 /* select device 0 again */
2218 ap->ops->dev_select(ap, 0);
2219
2220 /* issue bus reset */
2221 DPRINTK("about to softreset, devmask=%x\n", devmask);
2222 err_mask = ata_bus_softreset(ap, devmask);
2223 if (err_mask) {
2224 if (verbose)
2225 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2226 ap->id, err_mask);
2227 else
2228 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2229 err_mask);
2230 return -EIO;
2231 }
2232
2233 /* determine by signature whether we have ATA or ATAPI devices */
2234 classes[0] = ata_dev_try_classify(ap, 0, &err);
2235 if (slave_possible && err != 0x81)
2236 classes[1] = ata_dev_try_classify(ap, 1, &err);
2237
3a39746a 2238 out:
c2bd5804
TH
2239 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2240 return 0;
2241}
2242
2243/**
2244 * sata_std_hardreset - reset host port via SATA phy reset
2245 * @ap: port to reset
2246 * @verbose: fail verbosely
2247 * @class: resulting class of attached device
2248 *
2249 * SATA phy-reset host port using DET bits of SControl register.
2250 * This function is to be used as standard callback for
2251 * ata_drive_*_reset().
2252 *
2253 * LOCKING:
2254 * Kernel thread context (may sleep)
2255 *
2256 * RETURNS:
2257 * 0 on success, -errno otherwise.
2258 */
2259int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2260{
c2bd5804
TH
2261 DPRINTK("ENTER\n");
2262
2263 /* Issue phy wake/reset */
2264 scr_write_flush(ap, SCR_CONTROL, 0x301);
2265
2266 /*
2267 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2268 * 10.4.2 says at least 1 ms.
2269 */
2270 msleep(1);
2271
7a7921e8
TH
2272 /* Bring phy back */
2273 sata_phy_resume(ap);
c2bd5804 2274
c2bd5804
TH
2275 /* TODO: phy layer with polling, timeouts, etc. */
2276 if (!sata_dev_present(ap)) {
2277 *class = ATA_DEV_NONE;
2278 DPRINTK("EXIT, link offline\n");
2279 return 0;
2280 }
2281
2282 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2283 if (verbose)
2284 printk(KERN_ERR "ata%u: COMRESET failed "
2285 "(device not ready)\n", ap->id);
2286 else
2287 DPRINTK("EXIT, device not ready\n");
2288 return -EIO;
2289 }
2290
3a39746a
TH
2291 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2292
c2bd5804
TH
2293 *class = ata_dev_try_classify(ap, 0, NULL);
2294
2295 DPRINTK("EXIT, class=%u\n", *class);
2296 return 0;
2297}
2298
2299/**
2300 * ata_std_postreset - standard postreset callback
2301 * @ap: the target ata_port
2302 * @classes: classes of attached devices
2303 *
2304 * This function is invoked after a successful reset. Note that
2305 * the device might have been reset more than once using
2306 * different reset methods before postreset is invoked.
c2bd5804
TH
2307 *
2308 * This function is to be used as standard callback for
2309 * ata_drive_*_reset().
2310 *
2311 * LOCKING:
2312 * Kernel thread context (may sleep)
2313 */
2314void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2315{
2316 DPRINTK("ENTER\n");
2317
56497bd5 2318 /* set cable type if it isn't already set */
c2bd5804
TH
2319 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2320 ap->cbl = ATA_CBL_SATA;
2321
2322 /* print link status */
2323 if (ap->cbl == ATA_CBL_SATA)
2324 sata_print_link_status(ap);
2325
3a39746a
TH
2326 /* re-enable interrupts */
2327 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2328 ata_irq_on(ap);
c2bd5804
TH
2329
2330 /* is double-select really necessary? */
2331 if (classes[0] != ATA_DEV_NONE)
2332 ap->ops->dev_select(ap, 1);
2333 if (classes[1] != ATA_DEV_NONE)
2334 ap->ops->dev_select(ap, 0);
2335
3a39746a
TH
2336 /* bail out if no device is present */
2337 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2338 DPRINTK("EXIT, no device\n");
2339 return;
2340 }
2341
2342 /* set up device control */
2343 if (ap->ioaddr.ctl_addr) {
2344 if (ap->flags & ATA_FLAG_MMIO)
2345 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2346 else
2347 outb(ap->ctl, ap->ioaddr.ctl_addr);
2348 }
c2bd5804
TH
2349
2350 DPRINTK("EXIT\n");
2351}
2352
2353/**
2354 * ata_std_probe_reset - standard probe reset method
2355 * @ap: prot to perform probe-reset
2356 * @classes: resulting classes of attached devices
2357 *
2358 * The stock off-the-shelf ->probe_reset method.
2359 *
2360 * LOCKING:
2361 * Kernel thread context (may sleep)
2362 *
2363 * RETURNS:
2364 * 0 on success, -errno otherwise.
2365 */
2366int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2367{
2368 ata_reset_fn_t hardreset;
2369
2370 hardreset = NULL;
b911fc3a 2371 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
c2bd5804
TH
2372 hardreset = sata_std_hardreset;
2373
8a19ac89 2374 return ata_drive_probe_reset(ap, ata_std_probeinit,
7944ea95 2375 ata_std_softreset, hardreset,
c2bd5804
TH
2376 ata_std_postreset, classes);
2377}
2378
a62c0fc5
TH
2379static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2380 ata_postreset_fn_t postreset,
2381 unsigned int *classes)
2382{
2383 int i, rc;
2384
2385 for (i = 0; i < ATA_MAX_DEVICES; i++)
2386 classes[i] = ATA_DEV_UNKNOWN;
2387
2388 rc = reset(ap, 0, classes);
2389 if (rc)
2390 return rc;
2391
2392 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2393 * is complete and convert all ATA_DEV_UNKNOWN to
2394 * ATA_DEV_NONE.
2395 */
2396 for (i = 0; i < ATA_MAX_DEVICES; i++)
2397 if (classes[i] != ATA_DEV_UNKNOWN)
2398 break;
2399
2400 if (i < ATA_MAX_DEVICES)
2401 for (i = 0; i < ATA_MAX_DEVICES; i++)
2402 if (classes[i] == ATA_DEV_UNKNOWN)
2403 classes[i] = ATA_DEV_NONE;
2404
2405 if (postreset)
2406 postreset(ap, classes);
2407
2408 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2409}
2410
2411/**
2412 * ata_drive_probe_reset - Perform probe reset with given methods
2413 * @ap: port to reset
7944ea95 2414 * @probeinit: probeinit method (can be NULL)
a62c0fc5
TH
2415 * @softreset: softreset method (can be NULL)
2416 * @hardreset: hardreset method (can be NULL)
2417 * @postreset: postreset method (can be NULL)
2418 * @classes: resulting classes of attached devices
2419 *
2420 * Reset the specified port and classify attached devices using
2421 * given methods. This function prefers softreset but tries all
2422 * possible reset sequences to reset and classify devices. This
2423 * function is intended to be used for constructing ->probe_reset
2424 * callback by low level drivers.
2425 *
2426 * Reset methods should follow the following rules.
2427 *
2428 * - Return 0 on sucess, -errno on failure.
2429 * - If classification is supported, fill classes[] with
2430 * recognized class codes.
2431 * - If classification is not supported, leave classes[] alone.
2432 * - If verbose is non-zero, print error message on failure;
2433 * otherwise, shut up.
2434 *
2435 * LOCKING:
2436 * Kernel thread context (may sleep)
2437 *
2438 * RETURNS:
2439 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2440 * if classification fails, and any error code from reset
2441 * methods.
2442 */
7944ea95 2443int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
a62c0fc5
TH
2444 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2445 ata_postreset_fn_t postreset, unsigned int *classes)
2446{
2447 int rc = -EINVAL;
2448
7944ea95
TH
2449 if (probeinit)
2450 probeinit(ap);
2451
a62c0fc5
TH
2452 if (softreset) {
2453 rc = do_probe_reset(ap, softreset, postreset, classes);
2454 if (rc == 0)
2455 return 0;
2456 }
2457
2458 if (!hardreset)
2459 return rc;
2460
2461 rc = do_probe_reset(ap, hardreset, postreset, classes);
2462 if (rc == 0 || rc != -ENODEV)
2463 return rc;
2464
2465 if (softreset)
2466 rc = do_probe_reset(ap, softreset, postreset, classes);
2467
2468 return rc;
2469}
2470
623a3128
TH
2471/**
2472 * ata_dev_same_device - Determine whether new ID matches configured device
2473 * @ap: port on which the device to compare against resides
2474 * @dev: device to compare against
2475 * @new_class: class of the new device
2476 * @new_id: IDENTIFY page of the new device
2477 *
2478 * Compare @new_class and @new_id against @dev and determine
2479 * whether @dev is the device indicated by @new_class and
2480 * @new_id.
2481 *
2482 * LOCKING:
2483 * None.
2484 *
2485 * RETURNS:
2486 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2487 */
2488static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
2489 unsigned int new_class, const u16 *new_id)
2490{
2491 const u16 *old_id = dev->id;
2492 unsigned char model[2][41], serial[2][21];
2493 u64 new_n_sectors;
2494
2495 if (dev->class != new_class) {
2496 printk(KERN_INFO
2497 "ata%u: dev %u class mismatch %d != %d\n",
2498 ap->id, dev->devno, dev->class, new_class);
2499 return 0;
2500 }
2501
2502 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2503 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2504 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2505 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2506 new_n_sectors = ata_id_n_sectors(new_id);
2507
2508 if (strcmp(model[0], model[1])) {
2509 printk(KERN_INFO
2510 "ata%u: dev %u model number mismatch '%s' != '%s'\n",
2511 ap->id, dev->devno, model[0], model[1]);
2512 return 0;
2513 }
2514
2515 if (strcmp(serial[0], serial[1])) {
2516 printk(KERN_INFO
2517 "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
2518 ap->id, dev->devno, serial[0], serial[1]);
2519 return 0;
2520 }
2521
2522 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
2523 printk(KERN_INFO
2524 "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
2525 ap->id, dev->devno, (unsigned long long)dev->n_sectors,
2526 (unsigned long long)new_n_sectors);
2527 return 0;
2528 }
2529
2530 return 1;
2531}
2532
2533/**
2534 * ata_dev_revalidate - Revalidate ATA device
2535 * @ap: port on which the device to revalidate resides
2536 * @dev: device to revalidate
2537 * @post_reset: is this revalidation after reset?
2538 *
2539 * Re-read IDENTIFY page and make sure @dev is still attached to
2540 * the port.
2541 *
2542 * LOCKING:
2543 * Kernel thread context (may sleep)
2544 *
2545 * RETURNS:
2546 * 0 on success, negative errno otherwise
2547 */
2548int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
2549 int post_reset)
1da177e4 2550{
623a3128
TH
2551 unsigned int class;
2552 u16 *id;
2553 int rc;
2554
2555 if (!ata_dev_present(dev))
2556 return -ENODEV;
2557
2558 class = dev->class;
2559 id = NULL;
2560
2561 /* allocate & read ID data */
2562 rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
2563 if (rc)
2564 goto fail;
2565
2566 /* is the device still there? */
2567 if (!ata_dev_same_device(ap, dev, class, id)) {
2568 rc = -ENODEV;
2569 goto fail;
2570 }
2571
2572 kfree(dev->id);
2573 dev->id = id;
2574
2575 /* configure device according to the new ID */
2576 return ata_dev_configure(ap, dev, 0);
2577
2578 fail:
2579 printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
2580 ap->id, dev->devno, rc);
2581 kfree(id);
2582 return rc;
1da177e4
LT
2583}
2584
98ac62de 2585static const char * const ata_dma_blacklist [] = {
f4b15fef
AC
2586 "WDC AC11000H", NULL,
2587 "WDC AC22100H", NULL,
2588 "WDC AC32500H", NULL,
2589 "WDC AC33100H", NULL,
2590 "WDC AC31600H", NULL,
2591 "WDC AC32100H", "24.09P07",
2592 "WDC AC23200L", "21.10N21",
2593 "Compaq CRD-8241B", NULL,
2594 "CRD-8400B", NULL,
2595 "CRD-8480B", NULL,
2596 "CRD-8482B", NULL,
2597 "CRD-84", NULL,
2598 "SanDisk SDP3B", NULL,
2599 "SanDisk SDP3B-64", NULL,
2600 "SANYO CD-ROM CRD", NULL,
2601 "HITACHI CDR-8", NULL,
2e9edbf8 2602 "HITACHI CDR-8335", NULL,
f4b15fef 2603 "HITACHI CDR-8435", NULL,
2e9edbf8
JG
2604 "Toshiba CD-ROM XM-6202B", NULL,
2605 "TOSHIBA CD-ROM XM-1702BC", NULL,
2606 "CD-532E-A", NULL,
2607 "E-IDE CD-ROM CR-840", NULL,
2608 "CD-ROM Drive/F5A", NULL,
2609 "WPI CDD-820", NULL,
f4b15fef 2610 "SAMSUNG CD-ROM SC-148C", NULL,
2e9edbf8 2611 "SAMSUNG CD-ROM SC", NULL,
f4b15fef
AC
2612 "SanDisk SDP3B-64", NULL,
2613 "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,
2614 "_NEC DV5800A", NULL,
2615 "SAMSUNG CD-ROM SN-124", "N001"
1da177e4 2616};
2e9edbf8 2617
f4b15fef
AC
2618static int ata_strim(char *s, size_t len)
2619{
2620 len = strnlen(s, len);
2621
2622 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2623 while ((len > 0) && (s[len - 1] == ' ')) {
2624 len--;
2625 s[len] = 0;
2626 }
2627 return len;
2628}
1da177e4 2629
057ace5e 2630static int ata_dma_blacklisted(const struct ata_device *dev)
1da177e4 2631{
f4b15fef
AC
2632 unsigned char model_num[40];
2633 unsigned char model_rev[16];
2634 unsigned int nlen, rlen;
1da177e4
LT
2635 int i;
2636
f4b15fef
AC
2637 ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2638 sizeof(model_num));
2639 ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS,
2640 sizeof(model_rev));
2641 nlen = ata_strim(model_num, sizeof(model_num));
2642 rlen = ata_strim(model_rev, sizeof(model_rev));
1da177e4 2643
f4b15fef
AC
2644 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i += 2) {
2645 if (!strncmp(ata_dma_blacklist[i], model_num, nlen)) {
2646 if (ata_dma_blacklist[i+1] == NULL)
2647 return 1;
2648 if (!strncmp(ata_dma_blacklist[i], model_rev, rlen))
2649 return 1;
2650 }
2651 }
1da177e4
LT
2652 return 0;
2653}
2654
1da177e4 2655/**
a6d5a51c
TH
2656 * ata_dev_xfermask - Compute supported xfermask of the given device
2657 * @ap: Port on which the device to compute xfermask for resides
2658 * @dev: Device to compute xfermask for
1da177e4 2659 *
acf356b1
TH
2660 * Compute supported xfermask of @dev and store it in
2661 * dev->*_mask. This function is responsible for applying all
2662 * known limits including host controller limits, device
2663 * blacklist, etc...
0cba632b 2664 *
600511e8
TH
2665 * FIXME: The current implementation limits all transfer modes to
2666 * the fastest of the lowested device on the port. This is not
05c8e0ac 2667 * required on most controllers.
600511e8 2668 *
1da177e4 2669 * LOCKING:
a6d5a51c 2670 * None.
1da177e4 2671 */
acf356b1 2672static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev)
1da177e4 2673{
5444a6f4 2674 struct ata_host_set *hs = ap->host_set;
a6d5a51c
TH
2675 unsigned long xfer_mask;
2676 int i;
1da177e4 2677
a6d5a51c
TH
2678 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
2679 ap->udma_mask);
1da177e4 2680
5444a6f4 2681 /* FIXME: Use port-wide xfermask for now */
a6d5a51c
TH
2682 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2683 struct ata_device *d = &ap->device[i];
2684 if (!ata_dev_present(d))
2685 continue;
acf356b1
TH
2686 xfer_mask &= ata_pack_xfermask(d->pio_mask, d->mwdma_mask,
2687 d->udma_mask);
a6d5a51c
TH
2688 xfer_mask &= ata_id_xfermask(d->id);
2689 if (ata_dma_blacklisted(d))
2690 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
5444a6f4
AC
2691 /* Apply cable rule here. Don't apply it early because when
2692 we handle hot plug the cable type can itself change */
2693 if (ap->cbl == ATA_CBL_PATA40)
2694 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
1da177e4
LT
2695 }
2696
a6d5a51c
TH
2697 if (ata_dma_blacklisted(dev))
2698 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, "
2699 "disabling DMA\n", ap->id, dev->devno);
2700
5444a6f4
AC
2701 if (hs->flags & ATA_HOST_SIMPLEX) {
2702 if (hs->simplex_claimed)
2703 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2704 }
2705 if (ap->ops->mode_filter)
2706 xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask);
2707
acf356b1
TH
2708 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
2709 &dev->udma_mask);
1da177e4
LT
2710}
2711
2712/**
2713 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2714 * @ap: Port associated with device @dev
2715 * @dev: Device to which command will be sent
2716 *
780a87f7
JG
2717 * Issue SET FEATURES - XFER MODE command to device @dev
2718 * on port @ap.
2719 *
1da177e4 2720 * LOCKING:
0cba632b 2721 * PCI/etc. bus probe sem.
83206a29
TH
2722 *
2723 * RETURNS:
2724 * 0 on success, AC_ERR_* mask otherwise.
1da177e4
LT
2725 */
2726
83206a29
TH
2727static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
2728 struct ata_device *dev)
1da177e4 2729{
a0123703 2730 struct ata_taskfile tf;
83206a29 2731 unsigned int err_mask;
1da177e4
LT
2732
2733 /* set up set-features taskfile */
2734 DPRINTK("set features - xfer mode\n");
2735
a0123703
TH
2736 ata_tf_init(ap, &tf, dev->devno);
2737 tf.command = ATA_CMD_SET_FEATURES;
2738 tf.feature = SETFEATURES_XFER;
2739 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2740 tf.protocol = ATA_PROT_NODATA;
2741 tf.nsect = dev->xfer_mode;
1da177e4 2742
83206a29 2743 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
1da177e4 2744
83206a29
TH
2745 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2746 return err_mask;
1da177e4
LT
2747}
2748
8bf62ece
AL
2749/**
2750 * ata_dev_init_params - Issue INIT DEV PARAMS command
2751 * @ap: Port associated with device @dev
2752 * @dev: Device to which command will be sent
2753 *
2754 * LOCKING:
6aff8f1f
TH
2755 * Kernel thread context (may sleep)
2756 *
2757 * RETURNS:
2758 * 0 on success, AC_ERR_* mask otherwise.
8bf62ece
AL
2759 */
2760
6aff8f1f 2761static unsigned int ata_dev_init_params(struct ata_port *ap,
00b6f5e9
AL
2762 struct ata_device *dev,
2763 u16 heads,
2764 u16 sectors)
8bf62ece 2765{
a0123703 2766 struct ata_taskfile tf;
6aff8f1f 2767 unsigned int err_mask;
8bf62ece
AL
2768
2769 /* Number of sectors per track 1-255. Number of heads 1-16 */
2770 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
00b6f5e9 2771 return AC_ERR_INVALID;
8bf62ece
AL
2772
2773 /* set up init dev params taskfile */
2774 DPRINTK("init dev params \n");
2775
a0123703
TH
2776 ata_tf_init(ap, &tf, dev->devno);
2777 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2778 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2779 tf.protocol = ATA_PROT_NODATA;
2780 tf.nsect = sectors;
2781 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
8bf62ece 2782
6aff8f1f 2783 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
8bf62ece 2784
6aff8f1f
TH
2785 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2786 return err_mask;
8bf62ece
AL
2787}
2788
1da177e4 2789/**
0cba632b
JG
2790 * ata_sg_clean - Unmap DMA memory associated with command
2791 * @qc: Command containing DMA memory to be released
2792 *
2793 * Unmap all mapped DMA memory associated with this command.
1da177e4
LT
2794 *
2795 * LOCKING:
0cba632b 2796 * spin_lock_irqsave(host_set lock)
1da177e4
LT
2797 */
2798
2799static void ata_sg_clean(struct ata_queued_cmd *qc)
2800{
2801 struct ata_port *ap = qc->ap;
cedc9a47 2802 struct scatterlist *sg = qc->__sg;
1da177e4 2803 int dir = qc->dma_dir;
cedc9a47 2804 void *pad_buf = NULL;
1da177e4 2805
a4631474
TH
2806 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2807 WARN_ON(sg == NULL);
1da177e4
LT
2808
2809 if (qc->flags & ATA_QCFLAG_SINGLE)
f131883e 2810 WARN_ON(qc->n_elem > 1);
1da177e4 2811
2c13b7ce 2812 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
1da177e4 2813
cedc9a47
JG
2814 /* if we padded the buffer out to 32-bit bound, and data
2815 * xfer direction is from-device, we must copy from the
2816 * pad buffer back into the supplied buffer
2817 */
2818 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2819 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2820
2821 if (qc->flags & ATA_QCFLAG_SG) {
e1410f2d 2822 if (qc->n_elem)
2f1f610b 2823 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
cedc9a47
JG
2824 /* restore last sg */
2825 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2826 if (pad_buf) {
2827 struct scatterlist *psg = &qc->pad_sgent;
2828 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2829 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
dfa15988 2830 kunmap_atomic(addr, KM_IRQ0);
cedc9a47
JG
2831 }
2832 } else {
2e242fa9 2833 if (qc->n_elem)
2f1f610b 2834 dma_unmap_single(ap->dev,
e1410f2d
JG
2835 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2836 dir);
cedc9a47
JG
2837 /* restore sg */
2838 sg->length += qc->pad_len;
2839 if (pad_buf)
2840 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2841 pad_buf, qc->pad_len);
2842 }
1da177e4
LT
2843
2844 qc->flags &= ~ATA_QCFLAG_DMAMAP;
cedc9a47 2845 qc->__sg = NULL;
1da177e4
LT
2846}
2847
2848/**
2849 * ata_fill_sg - Fill PCI IDE PRD table
2850 * @qc: Metadata associated with taskfile to be transferred
2851 *
780a87f7
JG
2852 * Fill PCI IDE PRD (scatter-gather) table with segments
2853 * associated with the current disk command.
2854 *
1da177e4 2855 * LOCKING:
780a87f7 2856 * spin_lock_irqsave(host_set lock)
1da177e4
LT
2857 *
2858 */
2859static void ata_fill_sg(struct ata_queued_cmd *qc)
2860{
1da177e4 2861 struct ata_port *ap = qc->ap;
cedc9a47
JG
2862 struct scatterlist *sg;
2863 unsigned int idx;
1da177e4 2864
a4631474 2865 WARN_ON(qc->__sg == NULL);
f131883e 2866 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
1da177e4
LT
2867
2868 idx = 0;
cedc9a47 2869 ata_for_each_sg(sg, qc) {
1da177e4
LT
2870 u32 addr, offset;
2871 u32 sg_len, len;
2872
2873 /* determine if physical DMA addr spans 64K boundary.
2874 * Note h/w doesn't support 64-bit, so we unconditionally
2875 * truncate dma_addr_t to u32.
2876 */
2877 addr = (u32) sg_dma_address(sg);
2878 sg_len = sg_dma_len(sg);
2879
2880 while (sg_len) {
2881 offset = addr & 0xffff;
2882 len = sg_len;
2883 if ((offset + sg_len) > 0x10000)
2884 len = 0x10000 - offset;
2885
2886 ap->prd[idx].addr = cpu_to_le32(addr);
2887 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2888 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2889
2890 idx++;
2891 sg_len -= len;
2892 addr += len;
2893 }
2894 }
2895
2896 if (idx)
2897 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2898}
2899/**
2900 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2901 * @qc: Metadata associated with taskfile to check
2902 *
780a87f7
JG
2903 * Allow low-level driver to filter ATA PACKET commands, returning
2904 * a status indicating whether or not it is OK to use DMA for the
2905 * supplied PACKET command.
2906 *
1da177e4 2907 * LOCKING:
0cba632b
JG
2908 * spin_lock_irqsave(host_set lock)
2909 *
1da177e4
LT
2910 * RETURNS: 0 when ATAPI DMA can be used
2911 * nonzero otherwise
2912 */
2913int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2914{
2915 struct ata_port *ap = qc->ap;
2916 int rc = 0; /* Assume ATAPI DMA is OK by default */
2917
2918 if (ap->ops->check_atapi_dma)
2919 rc = ap->ops->check_atapi_dma(qc);
2920
c2bbc551
AL
2921 /* We don't support polling DMA.
2922 * Use PIO if the LLDD handles only interrupts in
2923 * the HSM_ST_LAST state and the ATAPI device
2924 * generates CDB interrupts.
2925 */
2926 if ((ap->flags & ATA_FLAG_PIO_POLLING) &&
2927 (qc->dev->flags & ATA_DFLAG_CDB_INTR))
2928 rc = 1;
2929
1da177e4
LT
2930 return rc;
2931}
2932/**
2933 * ata_qc_prep - Prepare taskfile for submission
2934 * @qc: Metadata associated with taskfile to be prepared
2935 *
780a87f7
JG
2936 * Prepare ATA taskfile for submission.
2937 *
1da177e4
LT
2938 * LOCKING:
2939 * spin_lock_irqsave(host_set lock)
2940 */
2941void ata_qc_prep(struct ata_queued_cmd *qc)
2942{
2943 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2944 return;
2945
2946 ata_fill_sg(qc);
2947}
2948
e46834cd
BK
2949void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
2950
0cba632b
JG
2951/**
2952 * ata_sg_init_one - Associate command with memory buffer
2953 * @qc: Command to be associated
2954 * @buf: Memory buffer
2955 * @buflen: Length of memory buffer, in bytes.
2956 *
2957 * Initialize the data-related elements of queued_cmd @qc
2958 * to point to a single memory buffer, @buf of byte length @buflen.
2959 *
2960 * LOCKING:
2961 * spin_lock_irqsave(host_set lock)
2962 */
2963
1da177e4
LT
2964void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2965{
2966 struct scatterlist *sg;
2967
2968 qc->flags |= ATA_QCFLAG_SINGLE;
2969
2970 memset(&qc->sgent, 0, sizeof(qc->sgent));
cedc9a47 2971 qc->__sg = &qc->sgent;
1da177e4 2972 qc->n_elem = 1;
cedc9a47 2973 qc->orig_n_elem = 1;
1da177e4
LT
2974 qc->buf_virt = buf;
2975
cedc9a47 2976 sg = qc->__sg;
f0612bbc 2977 sg_init_one(sg, buf, buflen);
1da177e4
LT
2978}
2979
0cba632b
JG
2980/**
2981 * ata_sg_init - Associate command with scatter-gather table.
2982 * @qc: Command to be associated
2983 * @sg: Scatter-gather table.
2984 * @n_elem: Number of elements in s/g table.
2985 *
2986 * Initialize the data-related elements of queued_cmd @qc
2987 * to point to a scatter-gather table @sg, containing @n_elem
2988 * elements.
2989 *
2990 * LOCKING:
2991 * spin_lock_irqsave(host_set lock)
2992 */
2993
1da177e4
LT
2994void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2995 unsigned int n_elem)
2996{
2997 qc->flags |= ATA_QCFLAG_SG;
cedc9a47 2998 qc->__sg = sg;
1da177e4 2999 qc->n_elem = n_elem;
cedc9a47 3000 qc->orig_n_elem = n_elem;
1da177e4
LT
3001}
3002
3003/**
0cba632b
JG
3004 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
3005 * @qc: Command with memory buffer to be mapped.
3006 *
3007 * DMA-map the memory buffer associated with queued_cmd @qc.
1da177e4
LT
3008 *
3009 * LOCKING:
3010 * spin_lock_irqsave(host_set lock)
3011 *
3012 * RETURNS:
0cba632b 3013 * Zero on success, negative on error.
1da177e4
LT
3014 */
3015
3016static int ata_sg_setup_one(struct ata_queued_cmd *qc)
3017{
3018 struct ata_port *ap = qc->ap;
3019 int dir = qc->dma_dir;
cedc9a47 3020 struct scatterlist *sg = qc->__sg;
1da177e4 3021 dma_addr_t dma_address;
2e242fa9 3022 int trim_sg = 0;
1da177e4 3023
cedc9a47
JG
3024 /* we must lengthen transfers to end on a 32-bit boundary */
3025 qc->pad_len = sg->length & 3;
3026 if (qc->pad_len) {
3027 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3028 struct scatterlist *psg = &qc->pad_sgent;
3029
a4631474 3030 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
cedc9a47
JG
3031
3032 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3033
3034 if (qc->tf.flags & ATA_TFLAG_WRITE)
3035 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
3036 qc->pad_len);
3037
3038 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3039 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3040 /* trim sg */
3041 sg->length -= qc->pad_len;
2e242fa9
TH
3042 if (sg->length == 0)
3043 trim_sg = 1;
cedc9a47
JG
3044
3045 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
3046 sg->length, qc->pad_len);
3047 }
3048
2e242fa9
TH
3049 if (trim_sg) {
3050 qc->n_elem--;
e1410f2d
JG
3051 goto skip_map;
3052 }
3053
2f1f610b 3054 dma_address = dma_map_single(ap->dev, qc->buf_virt,
32529e01 3055 sg->length, dir);
537a95d9
TH
3056 if (dma_mapping_error(dma_address)) {
3057 /* restore sg */
3058 sg->length += qc->pad_len;
1da177e4 3059 return -1;
537a95d9 3060 }
1da177e4
LT
3061
3062 sg_dma_address(sg) = dma_address;
32529e01 3063 sg_dma_len(sg) = sg->length;
1da177e4 3064
2e242fa9 3065skip_map:
1da177e4
LT
3066 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
3067 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3068
3069 return 0;
3070}
3071
3072/**
0cba632b
JG
3073 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
3074 * @qc: Command with scatter-gather table to be mapped.
3075 *
3076 * DMA-map the scatter-gather table associated with queued_cmd @qc.
1da177e4
LT
3077 *
3078 * LOCKING:
3079 * spin_lock_irqsave(host_set lock)
3080 *
3081 * RETURNS:
0cba632b 3082 * Zero on success, negative on error.
1da177e4
LT
3083 *
3084 */
3085
3086static int ata_sg_setup(struct ata_queued_cmd *qc)
3087{
3088 struct ata_port *ap = qc->ap;
cedc9a47
JG
3089 struct scatterlist *sg = qc->__sg;
3090 struct scatterlist *lsg = &sg[qc->n_elem - 1];
e1410f2d 3091 int n_elem, pre_n_elem, dir, trim_sg = 0;
1da177e4
LT
3092
3093 VPRINTK("ENTER, ata%u\n", ap->id);
a4631474 3094 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
1da177e4 3095
cedc9a47
JG
3096 /* we must lengthen transfers to end on a 32-bit boundary */
3097 qc->pad_len = lsg->length & 3;
3098 if (qc->pad_len) {
3099 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3100 struct scatterlist *psg = &qc->pad_sgent;
3101 unsigned int offset;
3102
a4631474 3103 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
cedc9a47
JG
3104
3105 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3106
3107 /*
3108 * psg->page/offset are used to copy to-be-written
3109 * data in this function or read data in ata_sg_clean.
3110 */
3111 offset = lsg->offset + lsg->length - qc->pad_len;
3112 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3113 psg->offset = offset_in_page(offset);
3114
3115 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3116 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3117 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
dfa15988 3118 kunmap_atomic(addr, KM_IRQ0);
cedc9a47
JG
3119 }
3120
3121 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3122 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3123 /* trim last sg */
3124 lsg->length -= qc->pad_len;
e1410f2d
JG
3125 if (lsg->length == 0)
3126 trim_sg = 1;
cedc9a47
JG
3127
3128 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3129 qc->n_elem - 1, lsg->length, qc->pad_len);
3130 }
3131
e1410f2d
JG
3132 pre_n_elem = qc->n_elem;
3133 if (trim_sg && pre_n_elem)
3134 pre_n_elem--;
3135
3136 if (!pre_n_elem) {
3137 n_elem = 0;
3138 goto skip_map;
3139 }
3140
1da177e4 3141 dir = qc->dma_dir;
2f1f610b 3142 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
537a95d9
TH
3143 if (n_elem < 1) {
3144 /* restore last sg */
3145 lsg->length += qc->pad_len;
1da177e4 3146 return -1;
537a95d9 3147 }
1da177e4
LT
3148
3149 DPRINTK("%d sg elements mapped\n", n_elem);
3150
e1410f2d 3151skip_map:
1da177e4
LT
3152 qc->n_elem = n_elem;
3153
3154 return 0;
3155}
3156
40e8c82c
TH
3157/**
3158 * ata_poll_qc_complete - turn irq back on and finish qc
3159 * @qc: Command to complete
8e8b77dd 3160 * @err_mask: ATA status register content
40e8c82c
TH
3161 *
3162 * LOCKING:
3163 * None. (grabs host lock)
3164 */
3165
a22e2eb0 3166void ata_poll_qc_complete(struct ata_queued_cmd *qc)
40e8c82c
TH
3167{
3168 struct ata_port *ap = qc->ap;
b8f6153e 3169 unsigned long flags;
40e8c82c 3170
b8f6153e 3171 spin_lock_irqsave(&ap->host_set->lock, flags);
40e8c82c 3172 ata_irq_on(ap);
a22e2eb0 3173 ata_qc_complete(qc);
b8f6153e 3174 spin_unlock_irqrestore(&ap->host_set->lock, flags);
40e8c82c
TH
3175}
3176
0baab86b 3177/**
c893a3ae 3178 * swap_buf_le16 - swap halves of 16-bit words in place
0baab86b
EF
3179 * @buf: Buffer to swap
3180 * @buf_words: Number of 16-bit words in buffer.
3181 *
3182 * Swap halves of 16-bit words if needed to convert from
3183 * little-endian byte order to native cpu byte order, or
3184 * vice-versa.
3185 *
3186 * LOCKING:
6f0ef4fa 3187 * Inherited from caller.
0baab86b 3188 */
1da177e4
LT
3189void swap_buf_le16(u16 *buf, unsigned int buf_words)
3190{
3191#ifdef __BIG_ENDIAN
3192 unsigned int i;
3193
3194 for (i = 0; i < buf_words; i++)
3195 buf[i] = le16_to_cpu(buf[i]);
3196#endif /* __BIG_ENDIAN */
3197}
3198
6ae4cfb5
AL
3199/**
3200 * ata_mmio_data_xfer - Transfer data by MMIO
3201 * @ap: port to read/write
3202 * @buf: data buffer
3203 * @buflen: buffer length
344babaa 3204 * @write_data: read/write
6ae4cfb5
AL
3205 *
3206 * Transfer data from/to the device data register by MMIO.
3207 *
3208 * LOCKING:
3209 * Inherited from caller.
6ae4cfb5
AL
3210 */
3211
1da177e4
LT
3212static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3213 unsigned int buflen, int write_data)
3214{
3215 unsigned int i;
3216 unsigned int words = buflen >> 1;
3217 u16 *buf16 = (u16 *) buf;
3218 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3219
6ae4cfb5 3220 /* Transfer multiple of 2 bytes */
1da177e4
LT
3221 if (write_data) {
3222 for (i = 0; i < words; i++)
3223 writew(le16_to_cpu(buf16[i]), mmio);
3224 } else {
3225 for (i = 0; i < words; i++)
3226 buf16[i] = cpu_to_le16(readw(mmio));
3227 }
6ae4cfb5
AL
3228
3229 /* Transfer trailing 1 byte, if any. */
3230 if (unlikely(buflen & 0x01)) {
3231 u16 align_buf[1] = { 0 };
3232 unsigned char *trailing_buf = buf + buflen - 1;
3233
3234 if (write_data) {
3235 memcpy(align_buf, trailing_buf, 1);
3236 writew(le16_to_cpu(align_buf[0]), mmio);
3237 } else {
3238 align_buf[0] = cpu_to_le16(readw(mmio));
3239 memcpy(trailing_buf, align_buf, 1);
3240 }
3241 }
1da177e4
LT
3242}
3243
6ae4cfb5
AL
3244/**
3245 * ata_pio_data_xfer - Transfer data by PIO
3246 * @ap: port to read/write
3247 * @buf: data buffer
3248 * @buflen: buffer length
344babaa 3249 * @write_data: read/write
6ae4cfb5
AL
3250 *
3251 * Transfer data from/to the device data register by PIO.
3252 *
3253 * LOCKING:
3254 * Inherited from caller.
6ae4cfb5
AL
3255 */
3256
1da177e4
LT
3257static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3258 unsigned int buflen, int write_data)
3259{
6ae4cfb5 3260 unsigned int words = buflen >> 1;
1da177e4 3261
6ae4cfb5 3262 /* Transfer multiple of 2 bytes */
1da177e4 3263 if (write_data)
6ae4cfb5 3264 outsw(ap->ioaddr.data_addr, buf, words);
1da177e4 3265 else
6ae4cfb5
AL
3266 insw(ap->ioaddr.data_addr, buf, words);
3267
3268 /* Transfer trailing 1 byte, if any. */
3269 if (unlikely(buflen & 0x01)) {
3270 u16 align_buf[1] = { 0 };
3271 unsigned char *trailing_buf = buf + buflen - 1;
3272
3273 if (write_data) {
3274 memcpy(align_buf, trailing_buf, 1);
3275 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3276 } else {
3277 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3278 memcpy(trailing_buf, align_buf, 1);
3279 }
3280 }
1da177e4
LT
3281}
3282
6ae4cfb5
AL
3283/**
3284 * ata_data_xfer - Transfer data from/to the data register.
3285 * @ap: port to read/write
3286 * @buf: data buffer
3287 * @buflen: buffer length
3288 * @do_write: read/write
3289 *
3290 * Transfer data from/to the device data register.
3291 *
3292 * LOCKING:
3293 * Inherited from caller.
6ae4cfb5
AL
3294 */
3295
1da177e4
LT
3296static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3297 unsigned int buflen, int do_write)
3298{
a1bd9e68
AC
3299 /* Make the crap hardware pay the costs not the good stuff */
3300 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3301 unsigned long flags;
3302 local_irq_save(flags);
3303 if (ap->flags & ATA_FLAG_MMIO)
3304 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3305 else
3306 ata_pio_data_xfer(ap, buf, buflen, do_write);
3307 local_irq_restore(flags);
3308 } else {
3309 if (ap->flags & ATA_FLAG_MMIO)
3310 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3311 else
3312 ata_pio_data_xfer(ap, buf, buflen, do_write);
3313 }
1da177e4
LT
3314}
3315
6ae4cfb5
AL
3316/**
3317 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3318 * @qc: Command on going
3319 *
3320 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3321 *
3322 * LOCKING:
3323 * Inherited from caller.
3324 */
3325
1da177e4
LT
3326static void ata_pio_sector(struct ata_queued_cmd *qc)
3327{
3328 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
cedc9a47 3329 struct scatterlist *sg = qc->__sg;
1da177e4
LT
3330 struct ata_port *ap = qc->ap;
3331 struct page *page;
3332 unsigned int offset;
3333 unsigned char *buf;
3334
3335 if (qc->cursect == (qc->nsect - 1))
14be71f4 3336 ap->hsm_task_state = HSM_ST_LAST;
1da177e4
LT
3337
3338 page = sg[qc->cursg].page;
3339 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3340
3341 /* get the current page and offset */
3342 page = nth_page(page, (offset >> PAGE_SHIFT));
3343 offset %= PAGE_SIZE;
3344
1da177e4
LT
3345 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3346
91b8b313
AL
3347 if (PageHighMem(page)) {
3348 unsigned long flags;
3349
3350 local_irq_save(flags);
3351 buf = kmap_atomic(page, KM_IRQ0);
083958d3 3352
91b8b313
AL
3353 /* do the actual data transfer */
3354 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
1da177e4 3355
91b8b313
AL
3356 kunmap_atomic(buf, KM_IRQ0);
3357 local_irq_restore(flags);
3358 } else {
3359 buf = page_address(page);
3360 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3361 }
7282aa4b
AL
3362
3363 qc->cursect++;
3364 qc->cursg_ofs++;
3365
3366 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
3367 qc->cursg++;
3368 qc->cursg_ofs = 0;
3369 }
1da177e4
LT
3370}
3371
07f6f7d0
AL
3372/**
3373 * ata_pio_sectors - Transfer one or many 512-byte sectors.
3374 * @qc: Command on going
3375 *
3376 * Transfer one or many ATA_SECT_SIZE of data from/to the
3377 * ATA device for the DRQ request.
3378 *
3379 * LOCKING:
3380 * Inherited from caller.
3381 */
3382
3383static void ata_pio_sectors(struct ata_queued_cmd *qc)
3384{
3385 if (is_multi_taskfile(&qc->tf)) {
3386 /* READ/WRITE MULTIPLE */
3387 unsigned int nsect;
3388
587005de 3389 WARN_ON(qc->dev->multi_count == 0);
07f6f7d0
AL
3390
3391 nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
3392 while (nsect--)
3393 ata_pio_sector(qc);
3394 } else
3395 ata_pio_sector(qc);
3396}
3397
c71c1857
AL
3398/**
3399 * atapi_send_cdb - Write CDB bytes to hardware
3400 * @ap: Port to which ATAPI device is attached.
3401 * @qc: Taskfile currently active
3402 *
3403 * When device has indicated its readiness to accept
3404 * a CDB, this function is called. Send the CDB.
3405 *
3406 * LOCKING:
3407 * caller.
3408 */
3409
3410static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3411{
3412 /* send SCSI cdb */
3413 DPRINTK("send cdb\n");
db024d53 3414 WARN_ON(qc->dev->cdb_len < 12);
c71c1857 3415
db024d53 3416 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
c71c1857
AL
3417 ata_altstatus(ap); /* flush */
3418
3419 switch (qc->tf.protocol) {
3420 case ATA_PROT_ATAPI:
3421 ap->hsm_task_state = HSM_ST;
3422 break;
3423 case ATA_PROT_ATAPI_NODATA:
3424 ap->hsm_task_state = HSM_ST_LAST;
3425 break;
3426 case ATA_PROT_ATAPI_DMA:
3427 ap->hsm_task_state = HSM_ST_LAST;
3428 /* initiate bmdma */
3429 ap->ops->bmdma_start(qc);
3430 break;
3431 }
3432}
3433
6ae4cfb5
AL
3434/**
3435 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3436 * @qc: Command on going
3437 * @bytes: number of bytes
3438 *
3439 * Transfer Transfer data from/to the ATAPI device.
3440 *
3441 * LOCKING:
3442 * Inherited from caller.
3443 *
3444 */
3445
1da177e4
LT
3446static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3447{
3448 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
cedc9a47 3449 struct scatterlist *sg = qc->__sg;
1da177e4
LT
3450 struct ata_port *ap = qc->ap;
3451 struct page *page;
3452 unsigned char *buf;
3453 unsigned int offset, count;
3454
563a6e1f 3455 if (qc->curbytes + bytes >= qc->nbytes)
14be71f4 3456 ap->hsm_task_state = HSM_ST_LAST;
1da177e4
LT
3457
3458next_sg:
563a6e1f 3459 if (unlikely(qc->cursg >= qc->n_elem)) {
7fb6ec28 3460 /*
563a6e1f
AL
3461 * The end of qc->sg is reached and the device expects
3462 * more data to transfer. In order not to overrun qc->sg
3463 * and fulfill length specified in the byte count register,
3464 * - for read case, discard trailing data from the device
3465 * - for write case, padding zero data to the device
3466 */
3467 u16 pad_buf[1] = { 0 };
3468 unsigned int words = bytes >> 1;
3469 unsigned int i;
3470
3471 if (words) /* warning if bytes > 1 */
7fb6ec28 3472 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
563a6e1f
AL
3473 ap->id, bytes);
3474
3475 for (i = 0; i < words; i++)
3476 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3477
14be71f4 3478 ap->hsm_task_state = HSM_ST_LAST;
563a6e1f
AL
3479 return;
3480 }
3481
cedc9a47 3482 sg = &qc->__sg[qc->cursg];
1da177e4 3483
1da177e4
LT
3484 page = sg->page;
3485 offset = sg->offset + qc->cursg_ofs;
3486
3487 /* get the current page and offset */
3488 page = nth_page(page, (offset >> PAGE_SHIFT));
3489 offset %= PAGE_SIZE;
3490
6952df03 3491 /* don't overrun current sg */
32529e01 3492 count = min(sg->length - qc->cursg_ofs, bytes);
1da177e4
LT
3493
3494 /* don't cross page boundaries */
3495 count = min(count, (unsigned int)PAGE_SIZE - offset);
3496
7282aa4b
AL
3497 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3498
91b8b313
AL
3499 if (PageHighMem(page)) {
3500 unsigned long flags;
3501
3502 local_irq_save(flags);
3503 buf = kmap_atomic(page, KM_IRQ0);
083958d3 3504
91b8b313
AL
3505 /* do the actual data transfer */
3506 ata_data_xfer(ap, buf + offset, count, do_write);
7282aa4b 3507
91b8b313
AL
3508 kunmap_atomic(buf, KM_IRQ0);
3509 local_irq_restore(flags);
3510 } else {
3511 buf = page_address(page);
3512 ata_data_xfer(ap, buf + offset, count, do_write);
3513 }
7282aa4b 3514
1da177e4
LT
3515 bytes -= count;
3516 qc->curbytes += count;
3517 qc->cursg_ofs += count;
3518
32529e01 3519 if (qc->cursg_ofs == sg->length) {
1da177e4
LT
3520 qc->cursg++;
3521 qc->cursg_ofs = 0;
3522 }
3523
563a6e1f 3524 if (bytes)
1da177e4 3525 goto next_sg;
1da177e4
LT
3526}
3527
6ae4cfb5
AL
3528/**
3529 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3530 * @qc: Command on going
3531 *
3532 * Transfer Transfer data from/to the ATAPI device.
3533 *
3534 * LOCKING:
3535 * Inherited from caller.
6ae4cfb5
AL
3536 */
3537
1da177e4
LT
3538static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3539{
3540 struct ata_port *ap = qc->ap;
3541 struct ata_device *dev = qc->dev;
3542 unsigned int ireason, bc_lo, bc_hi, bytes;
3543 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3544
3545 ap->ops->tf_read(ap, &qc->tf);
3546 ireason = qc->tf.nsect;
3547 bc_lo = qc->tf.lbam;
3548 bc_hi = qc->tf.lbah;
3549 bytes = (bc_hi << 8) | bc_lo;
3550
3551 /* shall be cleared to zero, indicating xfer of data */
3552 if (ireason & (1 << 0))
3553 goto err_out;
3554
3555 /* make sure transfer direction matches expected */
3556 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3557 if (do_write != i_write)
3558 goto err_out;
3559
312f7da2
AL
3560 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3561
1da177e4
LT
3562 __atapi_pio_bytes(qc, bytes);
3563
3564 return;
3565
3566err_out:
3567 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3568 ap->id, dev->devno);
11a56d24 3569 qc->err_mask |= AC_ERR_HSM;
14be71f4 3570 ap->hsm_task_state = HSM_ST_ERR;
1da177e4
LT
3571}
3572
c234fb00
AL
3573/**
3574 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
3575 * @ap: the target ata_port
3576 * @qc: qc on going
3577 *
3578 * RETURNS:
3579 * 1 if ok in workqueue, 0 otherwise.
3580 */
3581
3582static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
3583{
3584 if (qc->tf.flags & ATA_TFLAG_POLLING)
3585 return 1;
3586
3587 if (ap->hsm_task_state == HSM_ST_FIRST) {
3588 if (qc->tf.protocol == ATA_PROT_PIO &&
3589 (qc->tf.flags & ATA_TFLAG_WRITE))
3590 return 1;
3591
3592 if (is_atapi_taskfile(&qc->tf) &&
3593 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
3594 return 1;
3595 }
3596
3597 return 0;
3598}
3599
bb5cb290
AL
3600/**
3601 * ata_hsm_move - move the HSM to the next state.
3602 * @ap: the target ata_port
3603 * @qc: qc on going
3604 * @status: current device status
3605 * @in_wq: 1 if called from workqueue, 0 otherwise
3606 *
3607 * RETURNS:
3608 * 1 when poll next status needed, 0 otherwise.
3609 */
3610
3611static int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
3612 u8 status, int in_wq)
e2cec771 3613{
bb5cb290
AL
3614 unsigned long flags = 0;
3615 int poll_next;
3616
6912ccd5
AL
3617 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
3618
bb5cb290
AL
3619 /* Make sure ata_qc_issue_prot() does not throw things
3620 * like DMA polling into the workqueue. Notice that
3621 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
3622 */
c234fb00 3623 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
bb5cb290 3624
e2cec771 3625fsm_start:
999bb6f4
AL
3626 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
3627 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
3628
e2cec771
AL
3629 switch (ap->hsm_task_state) {
3630 case HSM_ST_FIRST:
bb5cb290
AL
3631 /* Send first data block or PACKET CDB */
3632
3633 /* If polling, we will stay in the work queue after
3634 * sending the data. Otherwise, interrupt handler
3635 * takes over after sending the data.
3636 */
3637 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
3638
e2cec771
AL
3639 /* check device status */
3640 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
3641 /* Wrong status. Let EH handle this */
3642 qc->err_mask |= AC_ERR_HSM;
3643 ap->hsm_task_state = HSM_ST_ERR;
3644 goto fsm_start;
3645 }
3646
71601958
AL
3647 /* Device should not ask for data transfer (DRQ=1)
3648 * when it finds something wrong.
3649 * Anyway, we respect DRQ here and let HSM go on
3650 * without changing hsm_task_state to HSM_ST_ERR.
3651 */
3652 if (unlikely(status & (ATA_ERR | ATA_DF))) {
3653 printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
3654 ap->id, status);
3655 qc->err_mask |= AC_ERR_DEV;
3656 }
3657
bb5cb290
AL
3658 /* Send the CDB (atapi) or the first data block (ata pio out).
3659 * During the state transition, interrupt handler shouldn't
3660 * be invoked before the data transfer is complete and
3661 * hsm_task_state is changed. Hence, the following locking.
3662 */
3663 if (in_wq)
3664 spin_lock_irqsave(&ap->host_set->lock, flags);
3665
3666 if (qc->tf.protocol == ATA_PROT_PIO) {
3667 /* PIO data out protocol.
3668 * send first data block.
3669 */
e2cec771 3670
bb5cb290
AL
3671 /* ata_pio_sectors() might change the state
3672 * to HSM_ST_LAST. so, the state is changed here
3673 * before ata_pio_sectors().
3674 */
3675 ap->hsm_task_state = HSM_ST;
3676 ata_pio_sectors(qc);
3677 ata_altstatus(ap); /* flush */
3678 } else
3679 /* send CDB */
3680 atapi_send_cdb(ap, qc);
3681
3682 if (in_wq)
3683 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3684
3685 /* if polling, ata_pio_task() handles the rest.
3686 * otherwise, interrupt handler takes over from here.
3687 */
e2cec771
AL
3688 break;
3689
3690 case HSM_ST:
3691 /* complete command or read/write the data register */
3692 if (qc->tf.protocol == ATA_PROT_ATAPI) {
3693 /* ATAPI PIO protocol */
3694 if ((status & ATA_DRQ) == 0) {
3695 /* no more data to transfer */
3696 ap->hsm_task_state = HSM_ST_LAST;
3697 goto fsm_start;
3698 }
3699
71601958
AL
3700 /* Device should not ask for data transfer (DRQ=1)
3701 * when it finds something wrong.
3702 * Anyway, we respect DRQ here and let HSM go on
3703 * without changing hsm_task_state to HSM_ST_ERR.
3704 */
3705 if (unlikely(status & (ATA_ERR | ATA_DF))) {
3706 printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
3707 ap->id, status);
3708 qc->err_mask |= AC_ERR_DEV;
3709 }
3710
e2cec771
AL
3711 atapi_pio_bytes(qc);
3712
3713 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
3714 /* bad ireason reported by device */
3715 goto fsm_start;
3716
3717 } else {
3718 /* ATA PIO protocol */
3719 if (unlikely((status & ATA_DRQ) == 0)) {
3720 /* handle BSY=0, DRQ=0 as error */
3721 qc->err_mask |= AC_ERR_HSM;
3722 ap->hsm_task_state = HSM_ST_ERR;
3723 goto fsm_start;
3724 }
3725
71601958
AL
3726 /* Some devices may ask for data transfer (DRQ=1)
3727 * alone with ERR=1 for PIO reads.
3728 * We respect DRQ here and let HSM go on without
3729 * changing hsm_task_state to HSM_ST_ERR.
3730 */
3731 if (unlikely(status & (ATA_ERR | ATA_DF))) {
3732 /* For writes, ERR=1 DRQ=1 doesn't make
3733 * sense since the data block has been
3734 * transferred to the device.
3735 */
3736 WARN_ON(qc->tf.flags & ATA_TFLAG_WRITE);
3737
3738 /* data might be corrputed */
3739 qc->err_mask |= AC_ERR_DEV;
3740 }
3741
e2cec771
AL
3742 ata_pio_sectors(qc);
3743
3744 if (ap->hsm_task_state == HSM_ST_LAST &&
3745 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
3746 /* all data read */
3747 ata_altstatus(ap);
52a32205 3748 status = ata_wait_idle(ap);
e2cec771
AL
3749 goto fsm_start;
3750 }
3751 }
3752
3753 ata_altstatus(ap); /* flush */
bb5cb290 3754 poll_next = 1;
e2cec771
AL
3755 break;
3756
3757 case HSM_ST_LAST:
6912ccd5
AL
3758 if (unlikely(!ata_ok(status))) {
3759 qc->err_mask |= __ac_err_mask(status);
e2cec771
AL
3760 ap->hsm_task_state = HSM_ST_ERR;
3761 goto fsm_start;
3762 }
3763
3764 /* no more data to transfer */
3765 DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
3766 ap->id, status);
3767
6912ccd5
AL
3768 WARN_ON(qc->err_mask);
3769
e2cec771
AL
3770 ap->hsm_task_state = HSM_ST_IDLE;
3771
3772 /* complete taskfile transaction */
bb5cb290
AL
3773 if (in_wq)
3774 ata_poll_qc_complete(qc);
3775 else
3776 ata_qc_complete(qc);
3777
3778 poll_next = 0;
e2cec771
AL
3779 break;
3780
3781 case HSM_ST_ERR:
3782 if (qc->tf.command != ATA_CMD_PACKET)
6912ccd5
AL
3783 printk(KERN_ERR "ata%u: command error, drv_stat 0x%x\n",
3784 ap->id, status);
e2cec771
AL
3785
3786 /* make sure qc->err_mask is available to
3787 * know what's wrong and recover
3788 */
3789 WARN_ON(qc->err_mask == 0);
3790
3791 ap->hsm_task_state = HSM_ST_IDLE;
bb5cb290 3792
999bb6f4 3793 /* complete taskfile transaction */
bb5cb290
AL
3794 if (in_wq)
3795 ata_poll_qc_complete(qc);
3796 else
3797 ata_qc_complete(qc);
3798
3799 poll_next = 0;
e2cec771
AL
3800 break;
3801 default:
bb5cb290 3802 poll_next = 0;
6912ccd5 3803 BUG();
e2cec771
AL
3804 }
3805
bb5cb290 3806 return poll_next;
e2cec771
AL
3807}
3808
1da177e4
LT
3809static void ata_pio_task(void *_data)
3810{
3811 struct ata_port *ap = _data;
a1af3734
AL
3812 struct ata_queued_cmd *qc;
3813 u8 status;
3814 int poll_next;
7fb6ec28
JG
3815
3816fsm_start:
a1af3734 3817 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
1da177e4 3818
a1af3734
AL
3819 qc = ata_qc_from_tag(ap, ap->active_tag);
3820 WARN_ON(qc == NULL);
467b16d4 3821
a1af3734
AL
3822 /*
3823 * This is purely heuristic. This is a fast path.
3824 * Sometimes when we enter, BSY will be cleared in
3825 * a chk-status or two. If not, the drive is probably seeking
3826 * or something. Snooze for a couple msecs, then
3827 * chk-status again. If still busy, queue delayed work.
3828 */
3829 status = ata_busy_wait(ap, ATA_BUSY, 5);
3830 if (status & ATA_BUSY) {
3831 msleep(2);
3832 status = ata_busy_wait(ap, ATA_BUSY, 10);
3833 if (status & ATA_BUSY) {
3834 ata_port_queue_task(ap, ata_pio_task, ap, ATA_SHORT_PAUSE);
3835 return;
3836 }
1da177e4
LT
3837 }
3838
a1af3734
AL
3839 /* move the HSM */
3840 poll_next = ata_hsm_move(ap, qc, status, 1);
3841
3842 /* another command or interrupt handler
3843 * may be running at this point.
3844 */
3845 if (poll_next)
7fb6ec28 3846 goto fsm_start;
1da177e4
LT
3847}
3848
1da177e4
LT
3849/**
3850 * ata_qc_timeout - Handle timeout of queued command
3851 * @qc: Command that timed out
3852 *
3853 * Some part of the kernel (currently, only the SCSI layer)
3854 * has noticed that the active command on port @ap has not
3855 * completed after a specified length of time. Handle this
3856 * condition by disabling DMA (if necessary) and completing
3857 * transactions, with error if necessary.
3858 *
3859 * This also handles the case of the "lost interrupt", where
3860 * for some reason (possibly hardware bug, possibly driver bug)
3861 * an interrupt was not delivered to the driver, even though the
3862 * transaction completed successfully.
3863 *
3864 * LOCKING:
0cba632b 3865 * Inherited from SCSI layer (none, can sleep)
1da177e4
LT
3866 */
3867
3868static void ata_qc_timeout(struct ata_queued_cmd *qc)
3869{
3870 struct ata_port *ap = qc->ap;
b8f6153e 3871 struct ata_host_set *host_set = ap->host_set;
1da177e4 3872 u8 host_stat = 0, drv_stat;
b8f6153e 3873 unsigned long flags;
1da177e4
LT
3874
3875 DPRINTK("ENTER\n");
3876
c18d06f8
TH
3877 ap->hsm_task_state = HSM_ST_IDLE;
3878
b8f6153e
JG
3879 spin_lock_irqsave(&host_set->lock, flags);
3880
1da177e4
LT
3881 switch (qc->tf.protocol) {
3882
3883 case ATA_PROT_DMA:
3884 case ATA_PROT_ATAPI_DMA:
3885 host_stat = ap->ops->bmdma_status(ap);
3886
3887 /* before we do anything else, clear DMA-Start bit */
b73fc89f 3888 ap->ops->bmdma_stop(qc);
1da177e4
LT
3889
3890 /* fall through */
3891
3892 default:
3893 ata_altstatus(ap);
3894 drv_stat = ata_chk_status(ap);
3895
3896 /* ack bmdma irq events */
3897 ap->ops->irq_clear(ap);
3898
3899 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3900 ap->id, qc->tf.command, drv_stat, host_stat);
3901
312f7da2
AL
3902 ap->hsm_task_state = HSM_ST_IDLE;
3903
1da177e4 3904 /* complete taskfile transaction */
555a8965 3905 qc->err_mask |= AC_ERR_TIMEOUT;
1da177e4
LT
3906 break;
3907 }
b8f6153e
JG
3908
3909 spin_unlock_irqrestore(&host_set->lock, flags);
3910
a72ec4ce
TH
3911 ata_eh_qc_complete(qc);
3912
1da177e4
LT
3913 DPRINTK("EXIT\n");
3914}
3915
3916/**
3917 * ata_eng_timeout - Handle timeout of queued command
3918 * @ap: Port on which timed-out command is active
3919 *
3920 * Some part of the kernel (currently, only the SCSI layer)
3921 * has noticed that the active command on port @ap has not
3922 * completed after a specified length of time. Handle this
3923 * condition by disabling DMA (if necessary) and completing
3924 * transactions, with error if necessary.
3925 *
3926 * This also handles the case of the "lost interrupt", where
3927 * for some reason (possibly hardware bug, possibly driver bug)
3928 * an interrupt was not delivered to the driver, even though the
3929 * transaction completed successfully.
3930 *
3931 * LOCKING:
3932 * Inherited from SCSI layer (none, can sleep)
3933 */
3934
3935void ata_eng_timeout(struct ata_port *ap)
3936{
1da177e4
LT
3937 DPRINTK("ENTER\n");
3938
f6379020 3939 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
1da177e4 3940
1da177e4
LT
3941 DPRINTK("EXIT\n");
3942}
3943
3944/**
3945 * ata_qc_new - Request an available ATA command, for queueing
3946 * @ap: Port associated with device @dev
3947 * @dev: Device from whom we request an available command structure
3948 *
3949 * LOCKING:
0cba632b 3950 * None.
1da177e4
LT
3951 */
3952
3953static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3954{
3955 struct ata_queued_cmd *qc = NULL;
3956 unsigned int i;
3957
3958 for (i = 0; i < ATA_MAX_QUEUE; i++)
3959 if (!test_and_set_bit(i, &ap->qactive)) {
3960 qc = ata_qc_from_tag(ap, i);
3961 break;
3962 }
3963
3964 if (qc)
3965 qc->tag = i;
3966
3967 return qc;
3968}
3969
3970/**
3971 * ata_qc_new_init - Request an available ATA command, and initialize it
3972 * @ap: Port associated with device @dev
3973 * @dev: Device from whom we request an available command structure
3974 *
3975 * LOCKING:
0cba632b 3976 * None.
1da177e4
LT
3977 */
3978
3979struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3980 struct ata_device *dev)
3981{
3982 struct ata_queued_cmd *qc;
3983
3984 qc = ata_qc_new(ap);
3985 if (qc) {
1da177e4
LT
3986 qc->scsicmd = NULL;
3987 qc->ap = ap;
3988 qc->dev = dev;
1da177e4 3989
2c13b7ce 3990 ata_qc_reinit(qc);
1da177e4
LT
3991 }
3992
3993 return qc;
3994}
3995
1da177e4
LT
3996/**
3997 * ata_qc_free - free unused ata_queued_cmd
3998 * @qc: Command to complete
3999 *
4000 * Designed to free unused ata_queued_cmd object
4001 * in case something prevents using it.
4002 *
4003 * LOCKING:
0cba632b 4004 * spin_lock_irqsave(host_set lock)
1da177e4
LT
4005 */
4006void ata_qc_free(struct ata_queued_cmd *qc)
4007{
4ba946e9
TH
4008 struct ata_port *ap = qc->ap;
4009 unsigned int tag;
4010
a4631474 4011 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
1da177e4 4012
4ba946e9
TH
4013 qc->flags = 0;
4014 tag = qc->tag;
4015 if (likely(ata_tag_valid(tag))) {
4016 if (tag == ap->active_tag)
4017 ap->active_tag = ATA_TAG_POISON;
4018 qc->tag = ATA_TAG_POISON;
4019 clear_bit(tag, &ap->qactive);
4020 }
1da177e4
LT
4021}
4022
76014427 4023void __ata_qc_complete(struct ata_queued_cmd *qc)
1da177e4 4024{
a4631474
TH
4025 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4026 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
1da177e4
LT
4027
4028 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4029 ata_sg_clean(qc);
4030
3f3791d3
AL
4031 /* atapi: mark qc as inactive to prevent the interrupt handler
4032 * from completing the command twice later, before the error handler
4033 * is called. (when rc != 0 and atapi request sense is needed)
4034 */
4035 qc->flags &= ~ATA_QCFLAG_ACTIVE;
4036
1da177e4 4037 /* call completion callback */
77853bf2 4038 qc->complete_fn(qc);
1da177e4
LT
4039}
4040
4041static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
4042{
4043 struct ata_port *ap = qc->ap;
4044
4045 switch (qc->tf.protocol) {
4046 case ATA_PROT_DMA:
4047 case ATA_PROT_ATAPI_DMA:
4048 return 1;
4049
4050 case ATA_PROT_ATAPI:
4051 case ATA_PROT_PIO:
1da177e4
LT
4052 if (ap->flags & ATA_FLAG_PIO_DMA)
4053 return 1;
4054
4055 /* fall through */
4056
4057 default:
4058 return 0;
4059 }
4060
4061 /* never reached */
4062}
4063
4064/**
4065 * ata_qc_issue - issue taskfile to device
4066 * @qc: command to issue to device
4067 *
4068 * Prepare an ATA command to submission to device.
4069 * This includes mapping the data into a DMA-able
4070 * area, filling in the S/G table, and finally
4071 * writing the taskfile to hardware, starting the command.
4072 *
4073 * LOCKING:
4074 * spin_lock_irqsave(host_set lock)
4075 *
4076 * RETURNS:
9a3d9eb0 4077 * Zero on success, AC_ERR_* mask on failure
1da177e4
LT
4078 */
4079
9a3d9eb0 4080unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
1da177e4
LT
4081{
4082 struct ata_port *ap = qc->ap;
4083
4084 if (ata_should_dma_map(qc)) {
4085 if (qc->flags & ATA_QCFLAG_SG) {
4086 if (ata_sg_setup(qc))
8e436af9 4087 goto sg_err;
1da177e4
LT
4088 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
4089 if (ata_sg_setup_one(qc))
8e436af9 4090 goto sg_err;
1da177e4
LT
4091 }
4092 } else {
4093 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4094 }
4095
4096 ap->ops->qc_prep(qc);
4097
4098 qc->ap->active_tag = qc->tag;
4099 qc->flags |= ATA_QCFLAG_ACTIVE;
4100
4101 return ap->ops->qc_issue(qc);
4102
8e436af9
TH
4103sg_err:
4104 qc->flags &= ~ATA_QCFLAG_DMAMAP;
9a3d9eb0 4105 return AC_ERR_SYSTEM;
1da177e4
LT
4106}
4107
0baab86b 4108
1da177e4
LT
4109/**
4110 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
4111 * @qc: command to issue to device
4112 *
4113 * Using various libata functions and hooks, this function
4114 * starts an ATA command. ATA commands are grouped into
4115 * classes called "protocols", and issuing each type of protocol
4116 * is slightly different.
4117 *
0baab86b
EF
4118 * May be used as the qc_issue() entry in ata_port_operations.
4119 *
1da177e4
LT
4120 * LOCKING:
4121 * spin_lock_irqsave(host_set lock)
4122 *
4123 * RETURNS:
9a3d9eb0 4124 * Zero on success, AC_ERR_* mask on failure
1da177e4
LT
4125 */
4126
9a3d9eb0 4127unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
1da177e4
LT
4128{
4129 struct ata_port *ap = qc->ap;
4130
e50362ec
AL
4131 /* Use polling pio if the LLD doesn't handle
4132 * interrupt driven pio and atapi CDB interrupt.
4133 */
4134 if (ap->flags & ATA_FLAG_PIO_POLLING) {
4135 switch (qc->tf.protocol) {
4136 case ATA_PROT_PIO:
4137 case ATA_PROT_ATAPI:
4138 case ATA_PROT_ATAPI_NODATA:
4139 qc->tf.flags |= ATA_TFLAG_POLLING;
4140 break;
4141 case ATA_PROT_ATAPI_DMA:
4142 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
c2bbc551 4143 /* see ata_check_atapi_dma() */
e50362ec
AL
4144 BUG();
4145 break;
4146 default:
4147 break;
4148 }
4149 }
4150
312f7da2 4151 /* select the device */
1da177e4
LT
4152 ata_dev_select(ap, qc->dev->devno, 1, 0);
4153
312f7da2 4154 /* start the command */
1da177e4
LT
4155 switch (qc->tf.protocol) {
4156 case ATA_PROT_NODATA:
312f7da2
AL
4157 if (qc->tf.flags & ATA_TFLAG_POLLING)
4158 ata_qc_set_polling(qc);
4159
e5338254 4160 ata_tf_to_host(ap, &qc->tf);
312f7da2
AL
4161 ap->hsm_task_state = HSM_ST_LAST;
4162
4163 if (qc->tf.flags & ATA_TFLAG_POLLING)
46e202ec 4164 ata_port_queue_task(ap, ata_pio_task, ap, 0);
312f7da2 4165
1da177e4
LT
4166 break;
4167
4168 case ATA_PROT_DMA:
587005de 4169 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
312f7da2 4170
1da177e4
LT
4171 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4172 ap->ops->bmdma_setup(qc); /* set up bmdma */
4173 ap->ops->bmdma_start(qc); /* initiate bmdma */
312f7da2 4174 ap->hsm_task_state = HSM_ST_LAST;
1da177e4
LT
4175 break;
4176
312f7da2
AL
4177 case ATA_PROT_PIO:
4178 if (qc->tf.flags & ATA_TFLAG_POLLING)
4179 ata_qc_set_polling(qc);
4180
e5338254 4181 ata_tf_to_host(ap, &qc->tf);
312f7da2 4182
54f00389
AL
4183 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4184 /* PIO data out protocol */
4185 ap->hsm_task_state = HSM_ST_FIRST;
ce1e7a2a 4186 ata_port_queue_task(ap, ata_pio_task, ap, 0);
54f00389
AL
4187
4188 /* always send first data block using
e27486db 4189 * the ata_pio_task() codepath.
54f00389 4190 */
312f7da2 4191 } else {
54f00389
AL
4192 /* PIO data in protocol */
4193 ap->hsm_task_state = HSM_ST;
4194
4195 if (qc->tf.flags & ATA_TFLAG_POLLING)
ce1e7a2a 4196 ata_port_queue_task(ap, ata_pio_task, ap, 0);
54f00389
AL
4197
4198 /* if polling, ata_pio_task() handles the rest.
4199 * otherwise, interrupt handler takes over from here.
4200 */
312f7da2
AL
4201 }
4202
1da177e4
LT
4203 break;
4204
4205 case ATA_PROT_ATAPI:
1da177e4 4206 case ATA_PROT_ATAPI_NODATA:
312f7da2
AL
4207 if (qc->tf.flags & ATA_TFLAG_POLLING)
4208 ata_qc_set_polling(qc);
4209
e5338254 4210 ata_tf_to_host(ap, &qc->tf);
f6ef65e6 4211
312f7da2
AL
4212 ap->hsm_task_state = HSM_ST_FIRST;
4213
4214 /* send cdb by polling if no cdb interrupt */
4215 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
4216 (qc->tf.flags & ATA_TFLAG_POLLING))
13ee4628 4217 ata_port_queue_task(ap, ata_pio_task, ap, 0);
1da177e4
LT
4218 break;
4219
4220 case ATA_PROT_ATAPI_DMA:
587005de 4221 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
312f7da2 4222
1da177e4
LT
4223 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4224 ap->ops->bmdma_setup(qc); /* set up bmdma */
312f7da2
AL
4225 ap->hsm_task_state = HSM_ST_FIRST;
4226
4227 /* send cdb by polling if no cdb interrupt */
4228 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
13ee4628 4229 ata_port_queue_task(ap, ata_pio_task, ap, 0);
1da177e4
LT
4230 break;
4231
4232 default:
4233 WARN_ON(1);
9a3d9eb0 4234 return AC_ERR_SYSTEM;
1da177e4
LT
4235 }
4236
4237 return 0;
4238}
4239
1da177e4
LT
4240/**
4241 * ata_host_intr - Handle host interrupt for given (port, task)
4242 * @ap: Port on which interrupt arrived (possibly...)
4243 * @qc: Taskfile currently active in engine
4244 *
4245 * Handle host interrupt for given queued command. Currently,
4246 * only DMA interrupts are handled. All other commands are
4247 * handled via polling with interrupts disabled (nIEN bit).
4248 *
4249 * LOCKING:
4250 * spin_lock_irqsave(host_set lock)
4251 *
4252 * RETURNS:
4253 * One if interrupt was handled, zero if not (shared irq).
4254 */
4255
4256inline unsigned int ata_host_intr (struct ata_port *ap,
4257 struct ata_queued_cmd *qc)
4258{
312f7da2 4259 u8 status, host_stat = 0;
1da177e4 4260
312f7da2
AL
4261 VPRINTK("ata%u: protocol %d task_state %d\n",
4262 ap->id, qc->tf.protocol, ap->hsm_task_state);
1da177e4 4263
312f7da2
AL
4264 /* Check whether we are expecting interrupt in this state */
4265 switch (ap->hsm_task_state) {
4266 case HSM_ST_FIRST:
6912ccd5
AL
4267 /* Some pre-ATAPI-4 devices assert INTRQ
4268 * at this state when ready to receive CDB.
4269 */
4270
312f7da2
AL
4271 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
4272 * The flag was turned on only for atapi devices.
4273 * No need to check is_atapi_taskfile(&qc->tf) again.
4274 */
4275 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
1da177e4 4276 goto idle_irq;
312f7da2
AL
4277 break;
4278 case HSM_ST_LAST:
4279 if (qc->tf.protocol == ATA_PROT_DMA ||
4280 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
4281 /* check status of DMA engine */
4282 host_stat = ap->ops->bmdma_status(ap);
4283 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4284
4285 /* if it's not our irq... */
4286 if (!(host_stat & ATA_DMA_INTR))
4287 goto idle_irq;
4288
4289 /* before we do anything else, clear DMA-Start bit */
4290 ap->ops->bmdma_stop(qc);
a4f16610
AL
4291
4292 if (unlikely(host_stat & ATA_DMA_ERR)) {
4293 /* error when transfering data to/from memory */
4294 qc->err_mask |= AC_ERR_HOST_BUS;
4295 ap->hsm_task_state = HSM_ST_ERR;
4296 }
312f7da2
AL
4297 }
4298 break;
4299 case HSM_ST:
4300 break;
4301 default:
4302 goto idle_irq;
4303 }
1da177e4 4304
312f7da2
AL
4305 /* check altstatus */
4306 status = ata_altstatus(ap);
4307 if (status & ATA_BUSY)
4308 goto idle_irq;
1da177e4 4309
312f7da2
AL
4310 /* check main status, clearing INTRQ */
4311 status = ata_chk_status(ap);
4312 if (unlikely(status & ATA_BUSY))
4313 goto idle_irq;
1da177e4 4314
312f7da2
AL
4315 /* ack bmdma irq events */
4316 ap->ops->irq_clear(ap);
1da177e4 4317
bb5cb290 4318 ata_hsm_move(ap, qc, status, 0);
1da177e4
LT
4319 return 1; /* irq handled */
4320
4321idle_irq:
4322 ap->stats.idle_irq++;
4323
4324#ifdef ATA_IRQ_TRAP
4325 if ((ap->stats.idle_irq % 1000) == 0) {
1da177e4
LT
4326 ata_irq_ack(ap, 0); /* debug trap */
4327 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
23cfce89 4328 return 1;
1da177e4
LT
4329 }
4330#endif
4331 return 0; /* irq not handled */
4332}
4333
4334/**
4335 * ata_interrupt - Default ATA host interrupt handler
0cba632b
JG
4336 * @irq: irq line (unused)
4337 * @dev_instance: pointer to our ata_host_set information structure
1da177e4
LT
4338 * @regs: unused
4339 *
0cba632b
JG
4340 * Default interrupt handler for PCI IDE devices. Calls
4341 * ata_host_intr() for each port that is not disabled.
4342 *
1da177e4 4343 * LOCKING:
0cba632b 4344 * Obtains host_set lock during operation.
1da177e4
LT
4345 *
4346 * RETURNS:
0cba632b 4347 * IRQ_NONE or IRQ_HANDLED.
1da177e4
LT
4348 */
4349
4350irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4351{
4352 struct ata_host_set *host_set = dev_instance;
4353 unsigned int i;
4354 unsigned int handled = 0;
4355 unsigned long flags;
4356
4357 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4358 spin_lock_irqsave(&host_set->lock, flags);
4359
4360 for (i = 0; i < host_set->n_ports; i++) {
4361 struct ata_port *ap;
4362
4363 ap = host_set->ports[i];
c1389503 4364 if (ap &&
312f7da2 4365 !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
1da177e4
LT
4366 struct ata_queued_cmd *qc;
4367
4368 qc = ata_qc_from_tag(ap, ap->active_tag);
312f7da2 4369 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
21b1ed74 4370 (qc->flags & ATA_QCFLAG_ACTIVE))
1da177e4
LT
4371 handled |= ata_host_intr(ap, qc);
4372 }
4373 }
4374
4375 spin_unlock_irqrestore(&host_set->lock, flags);
4376
4377 return IRQ_RETVAL(handled);
4378}
4379
0baab86b 4380
9b847548
JA
4381/*
4382 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4383 * without filling any other registers
4384 */
4385static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4386 u8 cmd)
4387{
4388 struct ata_taskfile tf;
4389 int err;
4390
4391 ata_tf_init(ap, &tf, dev->devno);
4392
4393 tf.command = cmd;
4394 tf.flags |= ATA_TFLAG_DEVICE;
4395 tf.protocol = ATA_PROT_NODATA;
4396
4397 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4398 if (err)
4399 printk(KERN_ERR "%s: ata command failed: %d\n",
4400 __FUNCTION__, err);
4401
4402 return err;
4403}
4404
4405static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4406{
4407 u8 cmd;
4408
4409 if (!ata_try_flush_cache(dev))
4410 return 0;
4411
4412 if (ata_id_has_flush_ext(dev->id))
4413 cmd = ATA_CMD_FLUSH_EXT;
4414 else
4415 cmd = ATA_CMD_FLUSH;
4416
4417 return ata_do_simple_cmd(ap, dev, cmd);
4418}
4419
4420static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4421{
4422 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4423}
4424
4425static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4426{
4427 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4428}
4429
4430/**
4431 * ata_device_resume - wakeup a previously suspended devices
c893a3ae
RD
4432 * @ap: port the device is connected to
4433 * @dev: the device to resume
9b847548
JA
4434 *
4435 * Kick the drive back into action, by sending it an idle immediate
4436 * command and making sure its transfer mode matches between drive
4437 * and host.
4438 *
4439 */
4440int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4441{
4442 if (ap->flags & ATA_FLAG_SUSPENDED) {
4443 ap->flags &= ~ATA_FLAG_SUSPENDED;
4444 ata_set_mode(ap);
4445 }
4446 if (!ata_dev_present(dev))
4447 return 0;
4448 if (dev->class == ATA_DEV_ATA)
4449 ata_start_drive(ap, dev);
4450
4451 return 0;
4452}
4453
4454/**
4455 * ata_device_suspend - prepare a device for suspend
c893a3ae
RD
4456 * @ap: port the device is connected to
4457 * @dev: the device to suspend
9b847548
JA
4458 *
4459 * Flush the cache on the drive, if appropriate, then issue a
4460 * standbynow command.
9b847548 4461 */
082776e4 4462int ata_device_suspend(struct ata_port *ap, struct ata_device *dev, pm_message_t state)
9b847548
JA
4463{
4464 if (!ata_dev_present(dev))
4465 return 0;
4466 if (dev->class == ATA_DEV_ATA)
4467 ata_flush_cache(ap, dev);
4468
082776e4
NC
4469 if (state.event != PM_EVENT_FREEZE)
4470 ata_standby_drive(ap, dev);
9b847548
JA
4471 ap->flags |= ATA_FLAG_SUSPENDED;
4472 return 0;
4473}
4474
332b5a52
AL
4475/**
4476 * ata_port_start - Set port up for dma.
4477 * @ap: Port to initialize
4478 *
4479 * Called just after data structures for each port are
4480 * initialized. Allocates space for PRD table.
4481 *
4482 * May be used as the port_start() entry in ata_port_operations.
4483 *
4484 * LOCKING:
4485 * Inherited from caller.
4486 */
4487
1da177e4
LT
4488int ata_port_start (struct ata_port *ap)
4489{
2f1f610b 4490 struct device *dev = ap->dev;
6037d6bb 4491 int rc;
1da177e4
LT
4492
4493 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4494 if (!ap->prd)
4495 return -ENOMEM;
4496
6037d6bb
JG
4497 rc = ata_pad_alloc(ap, dev);
4498 if (rc) {
cedc9a47 4499 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
6037d6bb 4500 return rc;
cedc9a47
JG
4501 }
4502
1da177e4
LT
4503 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4504
4505 return 0;
4506}
4507
0baab86b
EF
4508
4509/**
4510 * ata_port_stop - Undo ata_port_start()
4511 * @ap: Port to shut down
4512 *
4513 * Frees the PRD table.
4514 *
4515 * May be used as the port_stop() entry in ata_port_operations.
4516 *
4517 * LOCKING:
6f0ef4fa 4518 * Inherited from caller.
0baab86b
EF
4519 */
4520
1da177e4
LT
4521void ata_port_stop (struct ata_port *ap)
4522{
2f1f610b 4523 struct device *dev = ap->dev;
1da177e4
LT
4524
4525 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
6037d6bb 4526 ata_pad_free(ap, dev);
1da177e4
LT
4527}
4528
aa8f0dc6
JG
4529void ata_host_stop (struct ata_host_set *host_set)
4530{
4531 if (host_set->mmio_base)
4532 iounmap(host_set->mmio_base);
4533}
4534
4535
1da177e4
LT
4536/**
4537 * ata_host_remove - Unregister SCSI host structure with upper layers
4538 * @ap: Port to unregister
4539 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4540 *
4541 * LOCKING:
6f0ef4fa 4542 * Inherited from caller.
1da177e4
LT
4543 */
4544
4545static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4546{
4547 struct Scsi_Host *sh = ap->host;
4548
4549 DPRINTK("ENTER\n");
4550
4551 if (do_unregister)
4552 scsi_remove_host(sh);
4553
4554 ap->ops->port_stop(ap);
4555}
4556
4557/**
4558 * ata_host_init - Initialize an ata_port structure
4559 * @ap: Structure to initialize
4560 * @host: associated SCSI mid-layer structure
4561 * @host_set: Collection of hosts to which @ap belongs
4562 * @ent: Probe information provided by low-level driver
4563 * @port_no: Port number associated with this ata_port
4564 *
0cba632b
JG
4565 * Initialize a new ata_port structure, and its associated
4566 * scsi_host.
4567 *
1da177e4 4568 * LOCKING:
0cba632b 4569 * Inherited from caller.
1da177e4
LT
4570 */
4571
4572static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4573 struct ata_host_set *host_set,
057ace5e 4574 const struct ata_probe_ent *ent, unsigned int port_no)
1da177e4
LT
4575{
4576 unsigned int i;
4577
4578 host->max_id = 16;
4579 host->max_lun = 1;
4580 host->max_channel = 1;
4581 host->unique_id = ata_unique_id++;
4582 host->max_cmd_len = 12;
12413197 4583
1da177e4
LT
4584 ap->flags = ATA_FLAG_PORT_DISABLED;
4585 ap->id = host->unique_id;
4586 ap->host = host;
4587 ap->ctl = ATA_DEVCTL_OBS;
4588 ap->host_set = host_set;
2f1f610b 4589 ap->dev = ent->dev;
1da177e4
LT
4590 ap->port_no = port_no;
4591 ap->hard_port_no =
4592 ent->legacy_mode ? ent->hard_port_no : port_no;
4593 ap->pio_mask = ent->pio_mask;
4594 ap->mwdma_mask = ent->mwdma_mask;
4595 ap->udma_mask = ent->udma_mask;
4596 ap->flags |= ent->host_flags;
4597 ap->ops = ent->port_ops;
4598 ap->cbl = ATA_CBL_NONE;
4599 ap->active_tag = ATA_TAG_POISON;
4600 ap->last_ctl = 0xFF;
4601
86e45b6b 4602 INIT_WORK(&ap->port_task, NULL, NULL);
a72ec4ce 4603 INIT_LIST_HEAD(&ap->eh_done_q);
1da177e4 4604
acf356b1
TH
4605 for (i = 0; i < ATA_MAX_DEVICES; i++) {
4606 struct ata_device *dev = &ap->device[i];
4607 dev->devno = i;
4608 dev->pio_mask = UINT_MAX;
4609 dev->mwdma_mask = UINT_MAX;
4610 dev->udma_mask = UINT_MAX;
4611 }
1da177e4
LT
4612
4613#ifdef ATA_IRQ_TRAP
4614 ap->stats.unhandled_irq = 1;
4615 ap->stats.idle_irq = 1;
4616#endif
4617
4618 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4619}
4620
4621/**
4622 * ata_host_add - Attach low-level ATA driver to system
4623 * @ent: Information provided by low-level driver
4624 * @host_set: Collections of ports to which we add
4625 * @port_no: Port number associated with this host
4626 *
0cba632b
JG
4627 * Attach low-level ATA driver to system.
4628 *
1da177e4 4629 * LOCKING:
0cba632b 4630 * PCI/etc. bus probe sem.
1da177e4
LT
4631 *
4632 * RETURNS:
0cba632b 4633 * New ata_port on success, for NULL on error.
1da177e4
LT
4634 */
4635
057ace5e 4636static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
1da177e4
LT
4637 struct ata_host_set *host_set,
4638 unsigned int port_no)
4639{
4640 struct Scsi_Host *host;
4641 struct ata_port *ap;
4642 int rc;
4643
4644 DPRINTK("ENTER\n");
aec5c3c1
TH
4645
4646 if (!ent->port_ops->probe_reset &&
4647 !(ent->host_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) {
4648 printk(KERN_ERR "ata%u: no reset mechanism available\n",
4649 port_no);
4650 return NULL;
4651 }
4652
1da177e4
LT
4653 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4654 if (!host)
4655 return NULL;
4656
30afc84c
TH
4657 host->transportt = &ata_scsi_transport_template;
4658
1da177e4
LT
4659 ap = (struct ata_port *) &host->hostdata[0];
4660
4661 ata_host_init(ap, host, host_set, ent, port_no);
4662
4663 rc = ap->ops->port_start(ap);
4664 if (rc)
4665 goto err_out;
4666
4667 return ap;
4668
4669err_out:
4670 scsi_host_put(host);
4671 return NULL;
4672}
4673
4674/**
0cba632b
JG
4675 * ata_device_add - Register hardware device with ATA and SCSI layers
4676 * @ent: Probe information describing hardware device to be registered
4677 *
4678 * This function processes the information provided in the probe
4679 * information struct @ent, allocates the necessary ATA and SCSI
4680 * host information structures, initializes them, and registers
4681 * everything with requisite kernel subsystems.
4682 *
4683 * This function requests irqs, probes the ATA bus, and probes
4684 * the SCSI bus.
1da177e4
LT
4685 *
4686 * LOCKING:
0cba632b 4687 * PCI/etc. bus probe sem.
1da177e4
LT
4688 *
4689 * RETURNS:
0cba632b 4690 * Number of ports registered. Zero on error (no ports registered).
1da177e4
LT
4691 */
4692
057ace5e 4693int ata_device_add(const struct ata_probe_ent *ent)
1da177e4
LT
4694{
4695 unsigned int count = 0, i;
4696 struct device *dev = ent->dev;
4697 struct ata_host_set *host_set;
4698
4699 DPRINTK("ENTER\n");
4700 /* alloc a container for our list of ATA ports (buses) */
57f3bda8 4701 host_set = kzalloc(sizeof(struct ata_host_set) +
1da177e4
LT
4702 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4703 if (!host_set)
4704 return 0;
1da177e4
LT
4705 spin_lock_init(&host_set->lock);
4706
4707 host_set->dev = dev;
4708 host_set->n_ports = ent->n_ports;
4709 host_set->irq = ent->irq;
4710 host_set->mmio_base = ent->mmio_base;
4711 host_set->private_data = ent->private_data;
4712 host_set->ops = ent->port_ops;
5444a6f4 4713 host_set->flags = ent->host_set_flags;
1da177e4
LT
4714
4715 /* register each port bound to this device */
4716 for (i = 0; i < ent->n_ports; i++) {
4717 struct ata_port *ap;
4718 unsigned long xfer_mode_mask;
4719
4720 ap = ata_host_add(ent, host_set, i);
4721 if (!ap)
4722 goto err_out;
4723
4724 host_set->ports[i] = ap;
4725 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4726 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4727 (ap->pio_mask << ATA_SHIFT_PIO);
4728
4729 /* print per-port info to dmesg */
4730 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4731 "bmdma 0x%lX irq %lu\n",
4732 ap->id,
4733 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4734 ata_mode_string(xfer_mode_mask),
4735 ap->ioaddr.cmd_addr,
4736 ap->ioaddr.ctl_addr,
4737 ap->ioaddr.bmdma_addr,
4738 ent->irq);
4739
4740 ata_chk_status(ap);
4741 host_set->ops->irq_clear(ap);
4742 count++;
4743 }
4744
57f3bda8
RD
4745 if (!count)
4746 goto err_free_ret;
1da177e4
LT
4747
4748 /* obtain irq, that is shared between channels */
4749 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4750 DRV_NAME, host_set))
4751 goto err_out;
4752
4753 /* perform each probe synchronously */
4754 DPRINTK("probe begin\n");
4755 for (i = 0; i < count; i++) {
4756 struct ata_port *ap;
4757 int rc;
4758
4759 ap = host_set->ports[i];
4760
c893a3ae 4761 DPRINTK("ata%u: bus probe begin\n", ap->id);
1da177e4 4762 rc = ata_bus_probe(ap);
c893a3ae 4763 DPRINTK("ata%u: bus probe end\n", ap->id);
1da177e4
LT
4764
4765 if (rc) {
4766 /* FIXME: do something useful here?
4767 * Current libata behavior will
4768 * tear down everything when
4769 * the module is removed
4770 * or the h/w is unplugged.
4771 */
4772 }
4773
4774 rc = scsi_add_host(ap->host, dev);
4775 if (rc) {
4776 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4777 ap->id);
4778 /* FIXME: do something useful here */
4779 /* FIXME: handle unconditional calls to
4780 * scsi_scan_host and ata_host_remove, below,
4781 * at the very least
4782 */
4783 }
4784 }
4785
4786 /* probes are done, now scan each port's disk(s) */
c893a3ae 4787 DPRINTK("host probe begin\n");
1da177e4
LT
4788 for (i = 0; i < count; i++) {
4789 struct ata_port *ap = host_set->ports[i];
4790
644dd0cc 4791 ata_scsi_scan_host(ap);
1da177e4
LT
4792 }
4793
4794 dev_set_drvdata(dev, host_set);
4795
4796 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4797 return ent->n_ports; /* success */
4798
4799err_out:
4800 for (i = 0; i < count; i++) {
4801 ata_host_remove(host_set->ports[i], 1);
4802 scsi_host_put(host_set->ports[i]->host);
4803 }
57f3bda8 4804err_free_ret:
1da177e4
LT
4805 kfree(host_set);
4806 VPRINTK("EXIT, returning 0\n");
4807 return 0;
4808}
4809
17b14451
AC
4810/**
4811 * ata_host_set_remove - PCI layer callback for device removal
4812 * @host_set: ATA host set that was removed
4813 *
2e9edbf8 4814 * Unregister all objects associated with this host set. Free those
17b14451
AC
4815 * objects.
4816 *
4817 * LOCKING:
4818 * Inherited from calling layer (may sleep).
4819 */
4820
17b14451
AC
4821void ata_host_set_remove(struct ata_host_set *host_set)
4822{
4823 struct ata_port *ap;
4824 unsigned int i;
4825
4826 for (i = 0; i < host_set->n_ports; i++) {
4827 ap = host_set->ports[i];
4828 scsi_remove_host(ap->host);
4829 }
4830
4831 free_irq(host_set->irq, host_set);
4832
4833 for (i = 0; i < host_set->n_ports; i++) {
4834 ap = host_set->ports[i];
4835
4836 ata_scsi_release(ap->host);
4837
4838 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4839 struct ata_ioports *ioaddr = &ap->ioaddr;
4840
4841 if (ioaddr->cmd_addr == 0x1f0)
4842 release_region(0x1f0, 8);
4843 else if (ioaddr->cmd_addr == 0x170)
4844 release_region(0x170, 8);
4845 }
4846
4847 scsi_host_put(ap->host);
4848 }
4849
4850 if (host_set->ops->host_stop)
4851 host_set->ops->host_stop(host_set);
4852
4853 kfree(host_set);
4854}
4855
1da177e4
LT
4856/**
4857 * ata_scsi_release - SCSI layer callback hook for host unload
4858 * @host: libata host to be unloaded
4859 *
4860 * Performs all duties necessary to shut down a libata port...
4861 * Kill port kthread, disable port, and release resources.
4862 *
4863 * LOCKING:
4864 * Inherited from SCSI layer.
4865 *
4866 * RETURNS:
4867 * One.
4868 */
4869
4870int ata_scsi_release(struct Scsi_Host *host)
4871{
4872 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
d9572b1d 4873 int i;
1da177e4
LT
4874
4875 DPRINTK("ENTER\n");
4876
4877 ap->ops->port_disable(ap);
4878 ata_host_remove(ap, 0);
d9572b1d
TH
4879 for (i = 0; i < ATA_MAX_DEVICES; i++)
4880 kfree(ap->device[i].id);
1da177e4
LT
4881
4882 DPRINTK("EXIT\n");
4883 return 1;
4884}
4885
4886/**
4887 * ata_std_ports - initialize ioaddr with standard port offsets.
4888 * @ioaddr: IO address structure to be initialized
0baab86b
EF
4889 *
4890 * Utility function which initializes data_addr, error_addr,
4891 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4892 * device_addr, status_addr, and command_addr to standard offsets
4893 * relative to cmd_addr.
4894 *
4895 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
1da177e4 4896 */
0baab86b 4897
1da177e4
LT
4898void ata_std_ports(struct ata_ioports *ioaddr)
4899{
4900 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4901 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4902 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4903 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4904 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4905 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4906 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4907 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4908 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4909 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4910}
4911
0baab86b 4912
374b1873
JG
4913#ifdef CONFIG_PCI
4914
4915void ata_pci_host_stop (struct ata_host_set *host_set)
4916{
4917 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4918
4919 pci_iounmap(pdev, host_set->mmio_base);
4920}
4921
1da177e4
LT
4922/**
4923 * ata_pci_remove_one - PCI layer callback for device removal
4924 * @pdev: PCI device that was removed
4925 *
4926 * PCI layer indicates to libata via this hook that
6f0ef4fa 4927 * hot-unplug or module unload event has occurred.
1da177e4
LT
4928 * Handle this by unregistering all objects associated
4929 * with this PCI device. Free those objects. Then finally
4930 * release PCI resources and disable device.
4931 *
4932 * LOCKING:
4933 * Inherited from PCI layer (may sleep).
4934 */
4935
4936void ata_pci_remove_one (struct pci_dev *pdev)
4937{
4938 struct device *dev = pci_dev_to_dev(pdev);
4939 struct ata_host_set *host_set = dev_get_drvdata(dev);
1da177e4 4940
17b14451 4941 ata_host_set_remove(host_set);
1da177e4
LT
4942 pci_release_regions(pdev);
4943 pci_disable_device(pdev);
4944 dev_set_drvdata(dev, NULL);
4945}
4946
4947/* move to PCI subsystem */
057ace5e 4948int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
1da177e4
LT
4949{
4950 unsigned long tmp = 0;
4951
4952 switch (bits->width) {
4953 case 1: {
4954 u8 tmp8 = 0;
4955 pci_read_config_byte(pdev, bits->reg, &tmp8);
4956 tmp = tmp8;
4957 break;
4958 }
4959 case 2: {
4960 u16 tmp16 = 0;
4961 pci_read_config_word(pdev, bits->reg, &tmp16);
4962 tmp = tmp16;
4963 break;
4964 }
4965 case 4: {
4966 u32 tmp32 = 0;
4967 pci_read_config_dword(pdev, bits->reg, &tmp32);
4968 tmp = tmp32;
4969 break;
4970 }
4971
4972 default:
4973 return -EINVAL;
4974 }
4975
4976 tmp &= bits->mask;
4977
4978 return (tmp == bits->val) ? 1 : 0;
4979}
9b847548
JA
4980
4981int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4982{
4983 pci_save_state(pdev);
4984 pci_disable_device(pdev);
4985 pci_set_power_state(pdev, PCI_D3hot);
4986 return 0;
4987}
4988
4989int ata_pci_device_resume(struct pci_dev *pdev)
4990{
4991 pci_set_power_state(pdev, PCI_D0);
4992 pci_restore_state(pdev);
4993 pci_enable_device(pdev);
4994 pci_set_master(pdev);
4995 return 0;
4996}
1da177e4
LT
4997#endif /* CONFIG_PCI */
4998
4999
1da177e4
LT
5000static int __init ata_init(void)
5001{
5002 ata_wq = create_workqueue("ata");
5003 if (!ata_wq)
5004 return -ENOMEM;
5005
5006 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5007 return 0;
5008}
5009
5010static void __exit ata_exit(void)
5011{
5012 destroy_workqueue(ata_wq);
5013}
5014
5015module_init(ata_init);
5016module_exit(ata_exit);
5017
67846b30
JG
5018static unsigned long ratelimit_time;
5019static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5020
5021int ata_ratelimit(void)
5022{
5023 int rc;
5024 unsigned long flags;
5025
5026 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5027
5028 if (time_after(jiffies, ratelimit_time)) {
5029 rc = 1;
5030 ratelimit_time = jiffies + (HZ/5);
5031 } else
5032 rc = 0;
5033
5034 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5035
5036 return rc;
5037}
5038
1da177e4
LT
5039/*
5040 * libata is essentially a library of internal helper functions for
5041 * low-level ATA host controller drivers. As such, the API/ABI is
5042 * likely to change as new drivers are added and updated.
5043 * Do not depend on ABI/API stability.
5044 */
5045
5046EXPORT_SYMBOL_GPL(ata_std_bios_param);
5047EXPORT_SYMBOL_GPL(ata_std_ports);
5048EXPORT_SYMBOL_GPL(ata_device_add);
17b14451 5049EXPORT_SYMBOL_GPL(ata_host_set_remove);
1da177e4
LT
5050EXPORT_SYMBOL_GPL(ata_sg_init);
5051EXPORT_SYMBOL_GPL(ata_sg_init_one);
76014427 5052EXPORT_SYMBOL_GPL(__ata_qc_complete);
1da177e4
LT
5053EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5054EXPORT_SYMBOL_GPL(ata_eng_timeout);
5055EXPORT_SYMBOL_GPL(ata_tf_load);
5056EXPORT_SYMBOL_GPL(ata_tf_read);
5057EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5058EXPORT_SYMBOL_GPL(ata_std_dev_select);
5059EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5060EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5061EXPORT_SYMBOL_GPL(ata_check_status);
5062EXPORT_SYMBOL_GPL(ata_altstatus);
1da177e4
LT
5063EXPORT_SYMBOL_GPL(ata_exec_command);
5064EXPORT_SYMBOL_GPL(ata_port_start);
5065EXPORT_SYMBOL_GPL(ata_port_stop);
aa8f0dc6 5066EXPORT_SYMBOL_GPL(ata_host_stop);
1da177e4
LT
5067EXPORT_SYMBOL_GPL(ata_interrupt);
5068EXPORT_SYMBOL_GPL(ata_qc_prep);
e46834cd 5069EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
1da177e4
LT
5070EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5071EXPORT_SYMBOL_GPL(ata_bmdma_start);
5072EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5073EXPORT_SYMBOL_GPL(ata_bmdma_status);
5074EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5075EXPORT_SYMBOL_GPL(ata_port_probe);
5076EXPORT_SYMBOL_GPL(sata_phy_reset);
5077EXPORT_SYMBOL_GPL(__sata_phy_reset);
5078EXPORT_SYMBOL_GPL(ata_bus_reset);
8a19ac89 5079EXPORT_SYMBOL_GPL(ata_std_probeinit);
c2bd5804
TH
5080EXPORT_SYMBOL_GPL(ata_std_softreset);
5081EXPORT_SYMBOL_GPL(sata_std_hardreset);
5082EXPORT_SYMBOL_GPL(ata_std_postreset);
5083EXPORT_SYMBOL_GPL(ata_std_probe_reset);
a62c0fc5 5084EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
623a3128 5085EXPORT_SYMBOL_GPL(ata_dev_revalidate);
2e9edbf8
JG
5086EXPORT_SYMBOL_GPL(ata_dev_classify);
5087EXPORT_SYMBOL_GPL(ata_dev_pair);
1da177e4 5088EXPORT_SYMBOL_GPL(ata_port_disable);
67846b30 5089EXPORT_SYMBOL_GPL(ata_ratelimit);
6f8b9958 5090EXPORT_SYMBOL_GPL(ata_busy_sleep);
86e45b6b 5091EXPORT_SYMBOL_GPL(ata_port_queue_task);
1da177e4
LT
5092EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5093EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5094EXPORT_SYMBOL_GPL(ata_scsi_error);
5095EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5096EXPORT_SYMBOL_GPL(ata_scsi_release);
5097EXPORT_SYMBOL_GPL(ata_host_intr);
6a62a04d
TH
5098EXPORT_SYMBOL_GPL(ata_id_string);
5099EXPORT_SYMBOL_GPL(ata_id_c_string);
1da177e4 5100EXPORT_SYMBOL_GPL(ata_scsi_simulate);
a72ec4ce
TH
5101EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5102EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
1da177e4 5103
1bc4ccff 5104EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
452503f9
AC
5105EXPORT_SYMBOL_GPL(ata_timing_compute);
5106EXPORT_SYMBOL_GPL(ata_timing_merge);
5107
1da177e4
LT
5108#ifdef CONFIG_PCI
5109EXPORT_SYMBOL_GPL(pci_test_config_bits);
374b1873 5110EXPORT_SYMBOL_GPL(ata_pci_host_stop);
1da177e4
LT
5111EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5112EXPORT_SYMBOL_GPL(ata_pci_init_one);
5113EXPORT_SYMBOL_GPL(ata_pci_remove_one);
9b847548
JA
5114EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5115EXPORT_SYMBOL_GPL(ata_pci_device_resume);
67951ade
AC
5116EXPORT_SYMBOL_GPL(ata_pci_default_filter);
5117EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
1da177e4 5118#endif /* CONFIG_PCI */
9b847548
JA
5119
5120EXPORT_SYMBOL_GPL(ata_device_suspend);
5121EXPORT_SYMBOL_GPL(ata_device_resume);
5122EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5123EXPORT_SYMBOL_GPL(ata_scsi_device_resume);