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