[PATCH] libata: modify ata_dev_try_classify
[deliverable/linux.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
1488 ap->ops->phy_reset(ap);
1489 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1490 goto err_out;
1491
1492 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1493 ata_dev_identify(ap, i);
1494 if (ata_dev_present(&ap->device[i])) {
1495 found = 1;
6f2f3812 1496 ata_dev_config(ap,i);
1da177e4
LT
1497 }
1498 }
1499
1500 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1501 goto err_out_disable;
1502
1503 ata_set_mode(ap);
1504 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1505 goto err_out_disable;
1506
1507 return 0;
1508
1509err_out_disable:
1510 ap->ops->port_disable(ap);
1511err_out:
1512 return -1;
1513}
1514
1515/**
0cba632b
JG
1516 * ata_port_probe - Mark port as enabled
1517 * @ap: Port for which we indicate enablement
1da177e4 1518 *
0cba632b
JG
1519 * Modify @ap data structure such that the system
1520 * thinks that the entire port is enabled.
1521 *
1522 * LOCKING: host_set lock, or some other form of
1523 * serialization.
1da177e4
LT
1524 */
1525
1526void ata_port_probe(struct ata_port *ap)
1527{
1528 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1529}
1530
3be680b7
TH
1531/**
1532 * sata_print_link_status - Print SATA link status
1533 * @ap: SATA port to printk link status about
1534 *
1535 * This function prints link speed and status of a SATA link.
1536 *
1537 * LOCKING:
1538 * None.
1539 */
1540static void sata_print_link_status(struct ata_port *ap)
1541{
1542 u32 sstatus, tmp;
1543 const char *speed;
1544
1545 if (!ap->ops->scr_read)
1546 return;
1547
1548 sstatus = scr_read(ap, SCR_STATUS);
1549
1550 if (sata_dev_present(ap)) {
1551 tmp = (sstatus >> 4) & 0xf;
1552 if (tmp & (1 << 0))
1553 speed = "1.5";
1554 else if (tmp & (1 << 1))
1555 speed = "3.0";
1556 else
1557 speed = "<unknown>";
1558 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1559 ap->id, speed, sstatus);
1560 } else {
1561 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1562 ap->id, sstatus);
1563 }
1564}
1565
1da177e4 1566/**
780a87f7
JG
1567 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1568 * @ap: SATA port associated with target SATA PHY.
1da177e4 1569 *
780a87f7
JG
1570 * This function issues commands to standard SATA Sxxx
1571 * PHY registers, to wake up the phy (and device), and
1572 * clear any reset condition.
1da177e4
LT
1573 *
1574 * LOCKING:
0cba632b 1575 * PCI/etc. bus probe sem.
1da177e4
LT
1576 *
1577 */
1578void __sata_phy_reset(struct ata_port *ap)
1579{
1580 u32 sstatus;
1581 unsigned long timeout = jiffies + (HZ * 5);
1582
1583 if (ap->flags & ATA_FLAG_SATA_RESET) {
cdcca89e
BR
1584 /* issue phy wake/reset */
1585 scr_write_flush(ap, SCR_CONTROL, 0x301);
62ba2841
TH
1586 /* Couldn't find anything in SATA I/II specs, but
1587 * AHCI-1.1 10.4.2 says at least 1 ms. */
1588 mdelay(1);
1da177e4 1589 }
cdcca89e 1590 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1da177e4
LT
1591
1592 /* wait for phy to become ready, if necessary */
1593 do {
1594 msleep(200);
1595 sstatus = scr_read(ap, SCR_STATUS);
1596 if ((sstatus & 0xf) != 1)
1597 break;
1598 } while (time_before(jiffies, timeout));
1599
3be680b7
TH
1600 /* print link status */
1601 sata_print_link_status(ap);
656563e3 1602
3be680b7
TH
1603 /* TODO: phy layer with polling, timeouts, etc. */
1604 if (sata_dev_present(ap))
1da177e4 1605 ata_port_probe(ap);
3be680b7 1606 else
1da177e4 1607 ata_port_disable(ap);
1da177e4
LT
1608
1609 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1610 return;
1611
1612 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1613 ata_port_disable(ap);
1614 return;
1615 }
1616
1617 ap->cbl = ATA_CBL_SATA;
1618}
1619
1620/**
780a87f7
JG
1621 * sata_phy_reset - Reset SATA bus.
1622 * @ap: SATA port associated with target SATA PHY.
1da177e4 1623 *
780a87f7
JG
1624 * This function resets the SATA bus, and then probes
1625 * the bus for devices.
1da177e4
LT
1626 *
1627 * LOCKING:
0cba632b 1628 * PCI/etc. bus probe sem.
1da177e4
LT
1629 *
1630 */
1631void sata_phy_reset(struct ata_port *ap)
1632{
1633 __sata_phy_reset(ap);
1634 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1635 return;
1636 ata_bus_reset(ap);
1637}
1638
1639/**
780a87f7
JG
1640 * ata_port_disable - Disable port.
1641 * @ap: Port to be disabled.
1da177e4 1642 *
780a87f7
JG
1643 * Modify @ap data structure such that the system
1644 * thinks that the entire port is disabled, and should
1645 * never attempt to probe or communicate with devices
1646 * on this port.
1647 *
1648 * LOCKING: host_set lock, or some other form of
1649 * serialization.
1da177e4
LT
1650 */
1651
1652void ata_port_disable(struct ata_port *ap)
1653{
1654 ap->device[0].class = ATA_DEV_NONE;
1655 ap->device[1].class = ATA_DEV_NONE;
1656 ap->flags |= ATA_FLAG_PORT_DISABLED;
1657}
1658
452503f9
AC
1659/*
1660 * This mode timing computation functionality is ported over from
1661 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1662 */
1663/*
1664 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1665 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1666 * for PIO 5, which is a nonstandard extension and UDMA6, which
1667 * is currently supported only by Maxtor drives.
1668 */
1669
1670static const struct ata_timing ata_timing[] = {
1671
1672 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1673 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1674 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1675 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1676
1677 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1678 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1679 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1680
1681/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1682
1683 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1684 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1685 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1686
1687 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1688 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1689 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1690
1691/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1692 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1693 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1694
1695 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1696 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1697 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1698
1699/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1700
1701 { 0xFF }
1702};
1703
1704#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1705#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1706
1707static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1708{
1709 q->setup = EZ(t->setup * 1000, T);
1710 q->act8b = EZ(t->act8b * 1000, T);
1711 q->rec8b = EZ(t->rec8b * 1000, T);
1712 q->cyc8b = EZ(t->cyc8b * 1000, T);
1713 q->active = EZ(t->active * 1000, T);
1714 q->recover = EZ(t->recover * 1000, T);
1715 q->cycle = EZ(t->cycle * 1000, T);
1716 q->udma = EZ(t->udma * 1000, UT);
1717}
1718
1719void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1720 struct ata_timing *m, unsigned int what)
1721{
1722 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1723 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1724 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1725 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1726 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1727 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1728 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1729 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1730}
1731
1732static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1733{
1734 const struct ata_timing *t;
1735
1736 for (t = ata_timing; t->mode != speed; t++)
91190758 1737 if (t->mode == 0xFF)
452503f9
AC
1738 return NULL;
1739 return t;
1740}
1741
1742int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1743 struct ata_timing *t, int T, int UT)
1744{
1745 const struct ata_timing *s;
1746 struct ata_timing p;
1747
1748 /*
1749 * Find the mode.
75b1f2f8 1750 */
452503f9
AC
1751
1752 if (!(s = ata_timing_find_mode(speed)))
1753 return -EINVAL;
1754
75b1f2f8
AL
1755 memcpy(t, s, sizeof(*s));
1756
452503f9
AC
1757 /*
1758 * If the drive is an EIDE drive, it can tell us it needs extended
1759 * PIO/MW_DMA cycle timing.
1760 */
1761
1762 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1763 memset(&p, 0, sizeof(p));
1764 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1765 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1766 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1767 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1768 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1769 }
1770 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1771 }
1772
1773 /*
1774 * Convert the timing to bus clock counts.
1775 */
1776
75b1f2f8 1777 ata_timing_quantize(t, t, T, UT);
452503f9
AC
1778
1779 /*
1780 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1781 * and some other commands. We have to ensure that the DMA cycle timing is
1782 * slower/equal than the fastest PIO timing.
1783 */
1784
1785 if (speed > XFER_PIO_4) {
1786 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1787 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1788 }
1789
1790 /*
1791 * Lenghten active & recovery time so that cycle time is correct.
1792 */
1793
1794 if (t->act8b + t->rec8b < t->cyc8b) {
1795 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1796 t->rec8b = t->cyc8b - t->act8b;
1797 }
1798
1799 if (t->active + t->recover < t->cycle) {
1800 t->active += (t->cycle - (t->active + t->recover)) / 2;
1801 t->recover = t->cycle - t->active;
1802 }
1803
1804 return 0;
1805}
1806
057ace5e 1807static const struct {
1da177e4
LT
1808 unsigned int shift;
1809 u8 base;
1810} xfer_mode_classes[] = {
1811 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1812 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1813 { ATA_SHIFT_PIO, XFER_PIO_0 },
1814};
1815
858119e1 1816static u8 base_from_shift(unsigned int shift)
1da177e4
LT
1817{
1818 int i;
1819
1820 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1821 if (xfer_mode_classes[i].shift == shift)
1822 return xfer_mode_classes[i].base;
1823
1824 return 0xff;
1825}
1826
1827static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1828{
1829 int ofs, idx;
1830 u8 base;
1831
1832 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1833 return;
1834
1835 if (dev->xfer_shift == ATA_SHIFT_PIO)
1836 dev->flags |= ATA_DFLAG_PIO;
1837
1838 ata_dev_set_xfermode(ap, dev);
1839
1840 base = base_from_shift(dev->xfer_shift);
1841 ofs = dev->xfer_mode - base;
1842 idx = ofs + dev->xfer_shift;
1843 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1844
1845 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1846 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1847
1848 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1849 ap->id, dev->devno, xfer_mode_str[idx]);
1850}
1851
1852static int ata_host_set_pio(struct ata_port *ap)
1853{
1854 unsigned int mask;
1855 int x, i;
1856 u8 base, xfer_mode;
1857
1858 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1859 x = fgb(mask);
1860 if (x < 0) {
1861 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1862 return -1;
1863 }
1864
1865 base = base_from_shift(ATA_SHIFT_PIO);
1866 xfer_mode = base + x;
1867
1868 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1869 (int)base, (int)xfer_mode, mask, x);
1870
1871 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1872 struct ata_device *dev = &ap->device[i];
1873 if (ata_dev_present(dev)) {
1874 dev->pio_mode = xfer_mode;
1875 dev->xfer_mode = xfer_mode;
1876 dev->xfer_shift = ATA_SHIFT_PIO;
1877 if (ap->ops->set_piomode)
1878 ap->ops->set_piomode(ap, dev);
1879 }
1880 }
1881
1882 return 0;
1883}
1884
1885static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1886 unsigned int xfer_shift)
1887{
1888 int i;
1889
1890 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1891 struct ata_device *dev = &ap->device[i];
1892 if (ata_dev_present(dev)) {
1893 dev->dma_mode = xfer_mode;
1894 dev->xfer_mode = xfer_mode;
1895 dev->xfer_shift = xfer_shift;
1896 if (ap->ops->set_dmamode)
1897 ap->ops->set_dmamode(ap, dev);
1898 }
1899 }
1900}
1901
1902/**
1903 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1904 * @ap: port on which timings will be programmed
1905 *
780a87f7
JG
1906 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1907 *
1da177e4 1908 * LOCKING:
0cba632b 1909 * PCI/etc. bus probe sem.
1da177e4
LT
1910 *
1911 */
1912static void ata_set_mode(struct ata_port *ap)
1913{
8cbd6df1 1914 unsigned int xfer_shift;
1da177e4
LT
1915 u8 xfer_mode;
1916 int rc;
1917
1918 /* step 1: always set host PIO timings */
1919 rc = ata_host_set_pio(ap);
1920 if (rc)
1921 goto err_out;
1922
1923 /* step 2: choose the best data xfer mode */
1924 xfer_mode = xfer_shift = 0;
1925 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1926 if (rc)
1927 goto err_out;
1928
1929 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1930 if (xfer_shift != ATA_SHIFT_PIO)
1931 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1932
1933 /* step 4: update devices' xfer mode */
1934 ata_dev_set_mode(ap, &ap->device[0]);
1935 ata_dev_set_mode(ap, &ap->device[1]);
1936
1937 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1938 return;
1939
1940 if (ap->ops->post_set_mode)
1941 ap->ops->post_set_mode(ap);
1942
1da177e4
LT
1943 return;
1944
1945err_out:
1946 ata_port_disable(ap);
1947}
1948
1949/**
1950 * ata_busy_sleep - sleep until BSY clears, or timeout
1951 * @ap: port containing status register to be polled
1952 * @tmout_pat: impatience timeout
1953 * @tmout: overall timeout
1954 *
780a87f7
JG
1955 * Sleep until ATA Status register bit BSY clears,
1956 * or a timeout occurs.
1957 *
1958 * LOCKING: None.
1da177e4
LT
1959 *
1960 */
1961
6f8b9958
TH
1962unsigned int ata_busy_sleep (struct ata_port *ap,
1963 unsigned long tmout_pat, unsigned long tmout)
1da177e4
LT
1964{
1965 unsigned long timer_start, timeout;
1966 u8 status;
1967
1968 status = ata_busy_wait(ap, ATA_BUSY, 300);
1969 timer_start = jiffies;
1970 timeout = timer_start + tmout_pat;
1971 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1972 msleep(50);
1973 status = ata_busy_wait(ap, ATA_BUSY, 3);
1974 }
1975
1976 if (status & ATA_BUSY)
1977 printk(KERN_WARNING "ata%u is slow to respond, "
1978 "please be patient\n", ap->id);
1979
1980 timeout = timer_start + tmout;
1981 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1982 msleep(50);
1983 status = ata_chk_status(ap);
1984 }
1985
1986 if (status & ATA_BUSY) {
1987 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1988 ap->id, tmout / HZ);
1989 return 1;
1990 }
1991
1992 return 0;
1993}
1994
1995static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1996{
1997 struct ata_ioports *ioaddr = &ap->ioaddr;
1998 unsigned int dev0 = devmask & (1 << 0);
1999 unsigned int dev1 = devmask & (1 << 1);
2000 unsigned long timeout;
2001
2002 /* if device 0 was found in ata_devchk, wait for its
2003 * BSY bit to clear
2004 */
2005 if (dev0)
2006 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2007
2008 /* if device 1 was found in ata_devchk, wait for
2009 * register access, then wait for BSY to clear
2010 */
2011 timeout = jiffies + ATA_TMOUT_BOOT;
2012 while (dev1) {
2013 u8 nsect, lbal;
2014
2015 ap->ops->dev_select(ap, 1);
2016 if (ap->flags & ATA_FLAG_MMIO) {
2017 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2018 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2019 } else {
2020 nsect = inb(ioaddr->nsect_addr);
2021 lbal = inb(ioaddr->lbal_addr);
2022 }
2023 if ((nsect == 1) && (lbal == 1))
2024 break;
2025 if (time_after(jiffies, timeout)) {
2026 dev1 = 0;
2027 break;
2028 }
2029 msleep(50); /* give drive a breather */
2030 }
2031 if (dev1)
2032 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2033
2034 /* is all this really necessary? */
2035 ap->ops->dev_select(ap, 0);
2036 if (dev1)
2037 ap->ops->dev_select(ap, 1);
2038 if (dev0)
2039 ap->ops->dev_select(ap, 0);
2040}
2041
2042/**
0cba632b
JG
2043 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
2044 * @ap: Port to reset and probe
2045 *
2046 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
2047 * probe the bus. Not often used these days.
1da177e4
LT
2048 *
2049 * LOCKING:
0cba632b 2050 * PCI/etc. bus probe sem.
e5338254 2051 * Obtains host_set lock.
1da177e4
LT
2052 *
2053 */
2054
2055static unsigned int ata_bus_edd(struct ata_port *ap)
2056{
2057 struct ata_taskfile tf;
e5338254 2058 unsigned long flags;
1da177e4
LT
2059
2060 /* set up execute-device-diag (bus reset) taskfile */
2061 /* also, take interrupts to a known state (disabled) */
2062 DPRINTK("execute-device-diag\n");
2063 ata_tf_init(ap, &tf, 0);
2064 tf.ctl |= ATA_NIEN;
2065 tf.command = ATA_CMD_EDD;
2066 tf.protocol = ATA_PROT_NODATA;
2067
2068 /* do bus reset */
e5338254 2069 spin_lock_irqsave(&ap->host_set->lock, flags);
1da177e4 2070 ata_tf_to_host(ap, &tf);
e5338254 2071 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1da177e4
LT
2072
2073 /* spec says at least 2ms. but who knows with those
2074 * crazy ATAPI devices...
2075 */
2076 msleep(150);
2077
2078 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2079}
2080
2081static unsigned int ata_bus_softreset(struct ata_port *ap,
2082 unsigned int devmask)
2083{
2084 struct ata_ioports *ioaddr = &ap->ioaddr;
2085
2086 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2087
2088 /* software reset. causes dev0 to be selected */
2089 if (ap->flags & ATA_FLAG_MMIO) {
2090 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2091 udelay(20); /* FIXME: flush */
2092 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2093 udelay(20); /* FIXME: flush */
2094 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2095 } else {
2096 outb(ap->ctl, ioaddr->ctl_addr);
2097 udelay(10);
2098 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2099 udelay(10);
2100 outb(ap->ctl, ioaddr->ctl_addr);
2101 }
2102
2103 /* spec mandates ">= 2ms" before checking status.
2104 * We wait 150ms, because that was the magic delay used for
2105 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2106 * between when the ATA command register is written, and then
2107 * status is checked. Because waiting for "a while" before
2108 * checking status is fine, post SRST, we perform this magic
2109 * delay here as well.
2110 */
2111 msleep(150);
2112
2113 ata_bus_post_reset(ap, devmask);
2114
2115 return 0;
2116}
2117
2118/**
2119 * ata_bus_reset - reset host port and associated ATA channel
2120 * @ap: port to reset
2121 *
2122 * This is typically the first time we actually start issuing
2123 * commands to the ATA channel. We wait for BSY to clear, then
2124 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2125 * result. Determine what devices, if any, are on the channel
2126 * by looking at the device 0/1 error register. Look at the signature
2127 * stored in each device's taskfile registers, to determine if
2128 * the device is ATA or ATAPI.
2129 *
2130 * LOCKING:
0cba632b
JG
2131 * PCI/etc. bus probe sem.
2132 * Obtains host_set lock.
1da177e4
LT
2133 *
2134 * SIDE EFFECTS:
2135 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2136 */
2137
2138void ata_bus_reset(struct ata_port *ap)
2139{
2140 struct ata_ioports *ioaddr = &ap->ioaddr;
2141 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2142 u8 err;
2143 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2144
2145 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2146
2147 /* determine if device 0/1 are present */
2148 if (ap->flags & ATA_FLAG_SATA_RESET)
2149 dev0 = 1;
2150 else {
2151 dev0 = ata_devchk(ap, 0);
2152 if (slave_possible)
2153 dev1 = ata_devchk(ap, 1);
2154 }
2155
2156 if (dev0)
2157 devmask |= (1 << 0);
2158 if (dev1)
2159 devmask |= (1 << 1);
2160
2161 /* select device 0 again */
2162 ap->ops->dev_select(ap, 0);
2163
2164 /* issue bus reset */
2165 if (ap->flags & ATA_FLAG_SRST)
2166 rc = ata_bus_softreset(ap, devmask);
2167 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2168 /* set up device control */
2169 if (ap->flags & ATA_FLAG_MMIO)
2170 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2171 else
2172 outb(ap->ctl, ioaddr->ctl_addr);
2173 rc = ata_bus_edd(ap);
2174 }
2175
2176 if (rc)
2177 goto err_out;
2178
2179 /*
2180 * determine by signature whether we have ATA or ATAPI devices
2181 */
b4dc7623 2182 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
1da177e4 2183 if ((slave_possible) && (err != 0x81))
b4dc7623 2184 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
1da177e4
LT
2185
2186 /* re-enable interrupts */
2187 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2188 ata_irq_on(ap);
2189
2190 /* is double-select really necessary? */
2191 if (ap->device[1].class != ATA_DEV_NONE)
2192 ap->ops->dev_select(ap, 1);
2193 if (ap->device[0].class != ATA_DEV_NONE)
2194 ap->ops->dev_select(ap, 0);
2195
2196 /* if no devices were detected, disable this port */
2197 if ((ap->device[0].class == ATA_DEV_NONE) &&
2198 (ap->device[1].class == ATA_DEV_NONE))
2199 goto err_out;
2200
2201 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2202 /* set up device control for ATA_FLAG_SATA_RESET */
2203 if (ap->flags & ATA_FLAG_MMIO)
2204 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2205 else
2206 outb(ap->ctl, ioaddr->ctl_addr);
2207 }
2208
2209 DPRINTK("EXIT\n");
2210 return;
2211
2212err_out:
2213 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2214 ap->ops->port_disable(ap);
2215
2216 DPRINTK("EXIT\n");
2217}
2218
057ace5e
JG
2219static void ata_pr_blacklisted(const struct ata_port *ap,
2220 const struct ata_device *dev)
1da177e4
LT
2221{
2222 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2223 ap->id, dev->devno);
2224}
2225
98ac62de 2226static const char * const ata_dma_blacklist [] = {
1da177e4
LT
2227 "WDC AC11000H",
2228 "WDC AC22100H",
2229 "WDC AC32500H",
2230 "WDC AC33100H",
2231 "WDC AC31600H",
2232 "WDC AC32100H",
2233 "WDC AC23200L",
2234 "Compaq CRD-8241B",
2235 "CRD-8400B",
2236 "CRD-8480B",
2237 "CRD-8482B",
2238 "CRD-84",
2239 "SanDisk SDP3B",
2240 "SanDisk SDP3B-64",
2241 "SANYO CD-ROM CRD",
2242 "HITACHI CDR-8",
2243 "HITACHI CDR-8335",
2244 "HITACHI CDR-8435",
2245 "Toshiba CD-ROM XM-6202B",
e922256a 2246 "TOSHIBA CD-ROM XM-1702BC",
1da177e4
LT
2247 "CD-532E-A",
2248 "E-IDE CD-ROM CR-840",
2249 "CD-ROM Drive/F5A",
2250 "WPI CDD-820",
2251 "SAMSUNG CD-ROM SC-148C",
2252 "SAMSUNG CD-ROM SC",
2253 "SanDisk SDP3B-64",
1da177e4
LT
2254 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2255 "_NEC DV5800A",
2256};
2257
057ace5e 2258static int ata_dma_blacklisted(const struct ata_device *dev)
1da177e4
LT
2259{
2260 unsigned char model_num[40];
2261 char *s;
2262 unsigned int len;
2263 int i;
2264
2265 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2266 sizeof(model_num));
2267 s = &model_num[0];
2268 len = strnlen(s, sizeof(model_num));
2269
2270 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2271 while ((len > 0) && (s[len - 1] == ' ')) {
2272 len--;
2273 s[len] = 0;
2274 }
2275
2276 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2277 if (!strncmp(ata_dma_blacklist[i], s, len))
2278 return 1;
2279
2280 return 0;
2281}
2282
057ace5e 2283static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
1da177e4 2284{
057ace5e 2285 const struct ata_device *master, *slave;
1da177e4
LT
2286 unsigned int mask;
2287
2288 master = &ap->device[0];
2289 slave = &ap->device[1];
2290
2291 assert (ata_dev_present(master) || ata_dev_present(slave));
2292
2293 if (shift == ATA_SHIFT_UDMA) {
2294 mask = ap->udma_mask;
2295 if (ata_dev_present(master)) {
2296 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
057ace5e 2297 if (ata_dma_blacklisted(master)) {
1da177e4
LT
2298 mask = 0;
2299 ata_pr_blacklisted(ap, master);
2300 }
2301 }
2302 if (ata_dev_present(slave)) {
2303 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
057ace5e 2304 if (ata_dma_blacklisted(slave)) {
1da177e4
LT
2305 mask = 0;
2306 ata_pr_blacklisted(ap, slave);
2307 }
2308 }
2309 }
2310 else if (shift == ATA_SHIFT_MWDMA) {
2311 mask = ap->mwdma_mask;
2312 if (ata_dev_present(master)) {
2313 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
057ace5e 2314 if (ata_dma_blacklisted(master)) {
1da177e4
LT
2315 mask = 0;
2316 ata_pr_blacklisted(ap, master);
2317 }
2318 }
2319 if (ata_dev_present(slave)) {
2320 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
057ace5e 2321 if (ata_dma_blacklisted(slave)) {
1da177e4
LT
2322 mask = 0;
2323 ata_pr_blacklisted(ap, slave);
2324 }
2325 }
2326 }
2327 else if (shift == ATA_SHIFT_PIO) {
2328 mask = ap->pio_mask;
2329 if (ata_dev_present(master)) {
2330 /* spec doesn't return explicit support for
2331 * PIO0-2, so we fake it
2332 */
2333 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2334 tmp_mode <<= 3;
2335 tmp_mode |= 0x7;
2336 mask &= tmp_mode;
2337 }
2338 if (ata_dev_present(slave)) {
2339 /* spec doesn't return explicit support for
2340 * PIO0-2, so we fake it
2341 */
2342 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2343 tmp_mode <<= 3;
2344 tmp_mode |= 0x7;
2345 mask &= tmp_mode;
2346 }
2347 }
2348 else {
2349 mask = 0xffffffff; /* shut up compiler warning */
2350 BUG();
2351 }
2352
2353 return mask;
2354}
2355
2356/* find greatest bit */
2357static int fgb(u32 bitmap)
2358{
2359 unsigned int i;
2360 int x = -1;
2361
2362 for (i = 0; i < 32; i++)
2363 if (bitmap & (1 << i))
2364 x = i;
2365
2366 return x;
2367}
2368
2369/**
2370 * ata_choose_xfer_mode - attempt to find best transfer mode
2371 * @ap: Port for which an xfer mode will be selected
2372 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2373 * @xfer_shift_out: (output) bit shift that selects this mode
2374 *
0cba632b
JG
2375 * Based on host and device capabilities, determine the
2376 * maximum transfer mode that is amenable to all.
2377 *
1da177e4 2378 * LOCKING:
0cba632b 2379 * PCI/etc. bus probe sem.
1da177e4
LT
2380 *
2381 * RETURNS:
2382 * Zero on success, negative on error.
2383 */
2384
057ace5e 2385static int ata_choose_xfer_mode(const struct ata_port *ap,
1da177e4
LT
2386 u8 *xfer_mode_out,
2387 unsigned int *xfer_shift_out)
2388{
2389 unsigned int mask, shift;
2390 int x, i;
2391
2392 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2393 shift = xfer_mode_classes[i].shift;
2394 mask = ata_get_mode_mask(ap, shift);
2395
2396 x = fgb(mask);
2397 if (x >= 0) {
2398 *xfer_mode_out = xfer_mode_classes[i].base + x;
2399 *xfer_shift_out = shift;
2400 return 0;
2401 }
2402 }
2403
2404 return -1;
2405}
2406
2407/**
2408 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2409 * @ap: Port associated with device @dev
2410 * @dev: Device to which command will be sent
2411 *
780a87f7
JG
2412 * Issue SET FEATURES - XFER MODE command to device @dev
2413 * on port @ap.
2414 *
1da177e4 2415 * LOCKING:
0cba632b 2416 * PCI/etc. bus probe sem.
1da177e4
LT
2417 */
2418
2419static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2420{
a0123703 2421 struct ata_taskfile tf;
1da177e4
LT
2422
2423 /* set up set-features taskfile */
2424 DPRINTK("set features - xfer mode\n");
2425
a0123703
TH
2426 ata_tf_init(ap, &tf, dev->devno);
2427 tf.command = ATA_CMD_SET_FEATURES;
2428 tf.feature = SETFEATURES_XFER;
2429 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2430 tf.protocol = ATA_PROT_NODATA;
2431 tf.nsect = dev->xfer_mode;
1da177e4 2432
a0123703
TH
2433 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2434 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2435 ap->id);
1da177e4 2436 ata_port_disable(ap);
a0123703 2437 }
1da177e4
LT
2438
2439 DPRINTK("EXIT\n");
2440}
2441
59a10b17
AL
2442/**
2443 * ata_dev_reread_id - Reread the device identify device info
2444 * @ap: port where the device is
2445 * @dev: device to reread the identify device info
2446 *
2447 * LOCKING:
2448 */
2449
2450static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2451{
a0123703 2452 struct ata_taskfile tf;
59a10b17 2453
a0123703 2454 ata_tf_init(ap, &tf, dev->devno);
59a10b17
AL
2455
2456 if (dev->class == ATA_DEV_ATA) {
a0123703 2457 tf.command = ATA_CMD_ID_ATA;
59a10b17
AL
2458 DPRINTK("do ATA identify\n");
2459 } else {
a0123703 2460 tf.command = ATA_CMD_ID_ATAPI;
59a10b17
AL
2461 DPRINTK("do ATAPI identify\n");
2462 }
2463
a0123703
TH
2464 tf.flags |= ATA_TFLAG_DEVICE;
2465 tf.protocol = ATA_PROT_PIO;
59a10b17 2466
a0123703
TH
2467 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2468 dev->id, sizeof(dev->id)))
59a10b17
AL
2469 goto err_out;
2470
59a10b17
AL
2471 swap_buf_le16(dev->id, ATA_ID_WORDS);
2472
2473 ata_dump_id(dev);
2474
2475 DPRINTK("EXIT\n");
2476
2477 return;
2478err_out:
a0123703 2479 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
59a10b17
AL
2480 ata_port_disable(ap);
2481}
2482
8bf62ece
AL
2483/**
2484 * ata_dev_init_params - Issue INIT DEV PARAMS command
2485 * @ap: Port associated with device @dev
2486 * @dev: Device to which command will be sent
2487 *
2488 * LOCKING:
2489 */
2490
2491static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2492{
a0123703 2493 struct ata_taskfile tf;
8bf62ece
AL
2494 u16 sectors = dev->id[6];
2495 u16 heads = dev->id[3];
2496
2497 /* Number of sectors per track 1-255. Number of heads 1-16 */
2498 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2499 return;
2500
2501 /* set up init dev params taskfile */
2502 DPRINTK("init dev params \n");
2503
a0123703
TH
2504 ata_tf_init(ap, &tf, dev->devno);
2505 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2506 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2507 tf.protocol = ATA_PROT_NODATA;
2508 tf.nsect = sectors;
2509 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
8bf62ece 2510
a0123703
TH
2511 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2512 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2513 ap->id);
8bf62ece 2514 ata_port_disable(ap);
a0123703 2515 }
8bf62ece
AL
2516
2517 DPRINTK("EXIT\n");
2518}
2519
1da177e4 2520/**
0cba632b
JG
2521 * ata_sg_clean - Unmap DMA memory associated with command
2522 * @qc: Command containing DMA memory to be released
2523 *
2524 * Unmap all mapped DMA memory associated with this command.
1da177e4
LT
2525 *
2526 * LOCKING:
0cba632b 2527 * spin_lock_irqsave(host_set lock)
1da177e4
LT
2528 */
2529
2530static void ata_sg_clean(struct ata_queued_cmd *qc)
2531{
2532 struct ata_port *ap = qc->ap;
cedc9a47 2533 struct scatterlist *sg = qc->__sg;
1da177e4 2534 int dir = qc->dma_dir;
cedc9a47 2535 void *pad_buf = NULL;
1da177e4
LT
2536
2537 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2538 assert(sg != NULL);
2539
2540 if (qc->flags & ATA_QCFLAG_SINGLE)
2541 assert(qc->n_elem == 1);
2542
2c13b7ce 2543 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
1da177e4 2544
cedc9a47
JG
2545 /* if we padded the buffer out to 32-bit bound, and data
2546 * xfer direction is from-device, we must copy from the
2547 * pad buffer back into the supplied buffer
2548 */
2549 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2550 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2551
2552 if (qc->flags & ATA_QCFLAG_SG) {
e1410f2d
JG
2553 if (qc->n_elem)
2554 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
cedc9a47
JG
2555 /* restore last sg */
2556 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2557 if (pad_buf) {
2558 struct scatterlist *psg = &qc->pad_sgent;
2559 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2560 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
dfa15988 2561 kunmap_atomic(addr, KM_IRQ0);
cedc9a47
JG
2562 }
2563 } else {
e1410f2d
JG
2564 if (sg_dma_len(&sg[0]) > 0)
2565 dma_unmap_single(ap->host_set->dev,
2566 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2567 dir);
cedc9a47
JG
2568 /* restore sg */
2569 sg->length += qc->pad_len;
2570 if (pad_buf)
2571 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2572 pad_buf, qc->pad_len);
2573 }
1da177e4
LT
2574
2575 qc->flags &= ~ATA_QCFLAG_DMAMAP;
cedc9a47 2576 qc->__sg = NULL;
1da177e4
LT
2577}
2578
2579/**
2580 * ata_fill_sg - Fill PCI IDE PRD table
2581 * @qc: Metadata associated with taskfile to be transferred
2582 *
780a87f7
JG
2583 * Fill PCI IDE PRD (scatter-gather) table with segments
2584 * associated with the current disk command.
2585 *
1da177e4 2586 * LOCKING:
780a87f7 2587 * spin_lock_irqsave(host_set lock)
1da177e4
LT
2588 *
2589 */
2590static void ata_fill_sg(struct ata_queued_cmd *qc)
2591{
1da177e4 2592 struct ata_port *ap = qc->ap;
cedc9a47
JG
2593 struct scatterlist *sg;
2594 unsigned int idx;
1da177e4 2595
cedc9a47 2596 assert(qc->__sg != NULL);
1da177e4
LT
2597 assert(qc->n_elem > 0);
2598
2599 idx = 0;
cedc9a47 2600 ata_for_each_sg(sg, qc) {
1da177e4
LT
2601 u32 addr, offset;
2602 u32 sg_len, len;
2603
2604 /* determine if physical DMA addr spans 64K boundary.
2605 * Note h/w doesn't support 64-bit, so we unconditionally
2606 * truncate dma_addr_t to u32.
2607 */
2608 addr = (u32) sg_dma_address(sg);
2609 sg_len = sg_dma_len(sg);
2610
2611 while (sg_len) {
2612 offset = addr & 0xffff;
2613 len = sg_len;
2614 if ((offset + sg_len) > 0x10000)
2615 len = 0x10000 - offset;
2616
2617 ap->prd[idx].addr = cpu_to_le32(addr);
2618 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2619 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2620
2621 idx++;
2622 sg_len -= len;
2623 addr += len;
2624 }
2625 }
2626
2627 if (idx)
2628 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2629}
2630/**
2631 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2632 * @qc: Metadata associated with taskfile to check
2633 *
780a87f7
JG
2634 * Allow low-level driver to filter ATA PACKET commands, returning
2635 * a status indicating whether or not it is OK to use DMA for the
2636 * supplied PACKET command.
2637 *
1da177e4 2638 * LOCKING:
0cba632b
JG
2639 * spin_lock_irqsave(host_set lock)
2640 *
1da177e4
LT
2641 * RETURNS: 0 when ATAPI DMA can be used
2642 * nonzero otherwise
2643 */
2644int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2645{
2646 struct ata_port *ap = qc->ap;
2647 int rc = 0; /* Assume ATAPI DMA is OK by default */
2648
2649 if (ap->ops->check_atapi_dma)
2650 rc = ap->ops->check_atapi_dma(qc);
2651
2652 return rc;
2653}
2654/**
2655 * ata_qc_prep - Prepare taskfile for submission
2656 * @qc: Metadata associated with taskfile to be prepared
2657 *
780a87f7
JG
2658 * Prepare ATA taskfile for submission.
2659 *
1da177e4
LT
2660 * LOCKING:
2661 * spin_lock_irqsave(host_set lock)
2662 */
2663void ata_qc_prep(struct ata_queued_cmd *qc)
2664{
2665 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2666 return;
2667
2668 ata_fill_sg(qc);
2669}
2670
0cba632b
JG
2671/**
2672 * ata_sg_init_one - Associate command with memory buffer
2673 * @qc: Command to be associated
2674 * @buf: Memory buffer
2675 * @buflen: Length of memory buffer, in bytes.
2676 *
2677 * Initialize the data-related elements of queued_cmd @qc
2678 * to point to a single memory buffer, @buf of byte length @buflen.
2679 *
2680 * LOCKING:
2681 * spin_lock_irqsave(host_set lock)
2682 */
2683
1da177e4
LT
2684void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2685{
2686 struct scatterlist *sg;
2687
2688 qc->flags |= ATA_QCFLAG_SINGLE;
2689
2690 memset(&qc->sgent, 0, sizeof(qc->sgent));
cedc9a47 2691 qc->__sg = &qc->sgent;
1da177e4 2692 qc->n_elem = 1;
cedc9a47 2693 qc->orig_n_elem = 1;
1da177e4
LT
2694 qc->buf_virt = buf;
2695
cedc9a47 2696 sg = qc->__sg;
f0612bbc 2697 sg_init_one(sg, buf, buflen);
1da177e4
LT
2698}
2699
0cba632b
JG
2700/**
2701 * ata_sg_init - Associate command with scatter-gather table.
2702 * @qc: Command to be associated
2703 * @sg: Scatter-gather table.
2704 * @n_elem: Number of elements in s/g table.
2705 *
2706 * Initialize the data-related elements of queued_cmd @qc
2707 * to point to a scatter-gather table @sg, containing @n_elem
2708 * elements.
2709 *
2710 * LOCKING:
2711 * spin_lock_irqsave(host_set lock)
2712 */
2713
1da177e4
LT
2714void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2715 unsigned int n_elem)
2716{
2717 qc->flags |= ATA_QCFLAG_SG;
cedc9a47 2718 qc->__sg = sg;
1da177e4 2719 qc->n_elem = n_elem;
cedc9a47 2720 qc->orig_n_elem = n_elem;
1da177e4
LT
2721}
2722
2723/**
0cba632b
JG
2724 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2725 * @qc: Command with memory buffer to be mapped.
2726 *
2727 * DMA-map the memory buffer associated with queued_cmd @qc.
1da177e4
LT
2728 *
2729 * LOCKING:
2730 * spin_lock_irqsave(host_set lock)
2731 *
2732 * RETURNS:
0cba632b 2733 * Zero on success, negative on error.
1da177e4
LT
2734 */
2735
2736static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2737{
2738 struct ata_port *ap = qc->ap;
2739 int dir = qc->dma_dir;
cedc9a47 2740 struct scatterlist *sg = qc->__sg;
1da177e4
LT
2741 dma_addr_t dma_address;
2742
cedc9a47
JG
2743 /* we must lengthen transfers to end on a 32-bit boundary */
2744 qc->pad_len = sg->length & 3;
2745 if (qc->pad_len) {
2746 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2747 struct scatterlist *psg = &qc->pad_sgent;
2748
2749 assert(qc->dev->class == ATA_DEV_ATAPI);
2750
2751 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2752
2753 if (qc->tf.flags & ATA_TFLAG_WRITE)
2754 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2755 qc->pad_len);
2756
2757 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2758 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2759 /* trim sg */
2760 sg->length -= qc->pad_len;
2761
2762 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2763 sg->length, qc->pad_len);
2764 }
2765
e1410f2d
JG
2766 if (!sg->length) {
2767 sg_dma_address(sg) = 0;
2768 goto skip_map;
2769 }
2770
1da177e4 2771 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
32529e01 2772 sg->length, dir);
537a95d9
TH
2773 if (dma_mapping_error(dma_address)) {
2774 /* restore sg */
2775 sg->length += qc->pad_len;
1da177e4 2776 return -1;
537a95d9 2777 }
1da177e4
LT
2778
2779 sg_dma_address(sg) = dma_address;
e1410f2d 2780skip_map:
32529e01 2781 sg_dma_len(sg) = sg->length;
1da177e4
LT
2782
2783 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2784 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2785
2786 return 0;
2787}
2788
2789/**
0cba632b
JG
2790 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2791 * @qc: Command with scatter-gather table to be mapped.
2792 *
2793 * DMA-map the scatter-gather table associated with queued_cmd @qc.
1da177e4
LT
2794 *
2795 * LOCKING:
2796 * spin_lock_irqsave(host_set lock)
2797 *
2798 * RETURNS:
0cba632b 2799 * Zero on success, negative on error.
1da177e4
LT
2800 *
2801 */
2802
2803static int ata_sg_setup(struct ata_queued_cmd *qc)
2804{
2805 struct ata_port *ap = qc->ap;
cedc9a47
JG
2806 struct scatterlist *sg = qc->__sg;
2807 struct scatterlist *lsg = &sg[qc->n_elem - 1];
e1410f2d 2808 int n_elem, pre_n_elem, dir, trim_sg = 0;
1da177e4
LT
2809
2810 VPRINTK("ENTER, ata%u\n", ap->id);
2811 assert(qc->flags & ATA_QCFLAG_SG);
2812
cedc9a47
JG
2813 /* we must lengthen transfers to end on a 32-bit boundary */
2814 qc->pad_len = lsg->length & 3;
2815 if (qc->pad_len) {
2816 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2817 struct scatterlist *psg = &qc->pad_sgent;
2818 unsigned int offset;
2819
2820 assert(qc->dev->class == ATA_DEV_ATAPI);
2821
2822 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2823
2824 /*
2825 * psg->page/offset are used to copy to-be-written
2826 * data in this function or read data in ata_sg_clean.
2827 */
2828 offset = lsg->offset + lsg->length - qc->pad_len;
2829 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2830 psg->offset = offset_in_page(offset);
2831
2832 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2833 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2834 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
dfa15988 2835 kunmap_atomic(addr, KM_IRQ0);
cedc9a47
JG
2836 }
2837
2838 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2839 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2840 /* trim last sg */
2841 lsg->length -= qc->pad_len;
e1410f2d
JG
2842 if (lsg->length == 0)
2843 trim_sg = 1;
cedc9a47
JG
2844
2845 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2846 qc->n_elem - 1, lsg->length, qc->pad_len);
2847 }
2848
e1410f2d
JG
2849 pre_n_elem = qc->n_elem;
2850 if (trim_sg && pre_n_elem)
2851 pre_n_elem--;
2852
2853 if (!pre_n_elem) {
2854 n_elem = 0;
2855 goto skip_map;
2856 }
2857
1da177e4 2858 dir = qc->dma_dir;
e1410f2d 2859 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
537a95d9
TH
2860 if (n_elem < 1) {
2861 /* restore last sg */
2862 lsg->length += qc->pad_len;
1da177e4 2863 return -1;
537a95d9 2864 }
1da177e4
LT
2865
2866 DPRINTK("%d sg elements mapped\n", n_elem);
2867
e1410f2d 2868skip_map:
1da177e4
LT
2869 qc->n_elem = n_elem;
2870
2871 return 0;
2872}
2873
40e8c82c
TH
2874/**
2875 * ata_poll_qc_complete - turn irq back on and finish qc
2876 * @qc: Command to complete
8e8b77dd 2877 * @err_mask: ATA status register content
40e8c82c
TH
2878 *
2879 * LOCKING:
2880 * None. (grabs host lock)
2881 */
2882
a22e2eb0 2883void ata_poll_qc_complete(struct ata_queued_cmd *qc)
40e8c82c
TH
2884{
2885 struct ata_port *ap = qc->ap;
b8f6153e 2886 unsigned long flags;
40e8c82c 2887
b8f6153e 2888 spin_lock_irqsave(&ap->host_set->lock, flags);
40e8c82c
TH
2889 ap->flags &= ~ATA_FLAG_NOINTR;
2890 ata_irq_on(ap);
a22e2eb0 2891 ata_qc_complete(qc);
b8f6153e 2892 spin_unlock_irqrestore(&ap->host_set->lock, flags);
40e8c82c
TH
2893}
2894
1da177e4
LT
2895/**
2896 * ata_pio_poll -
6f0ef4fa 2897 * @ap: the target ata_port
1da177e4
LT
2898 *
2899 * LOCKING:
0cba632b 2900 * None. (executing in kernel thread context)
1da177e4
LT
2901 *
2902 * RETURNS:
6f0ef4fa 2903 * timeout value to use
1da177e4
LT
2904 */
2905
2906static unsigned long ata_pio_poll(struct ata_port *ap)
2907{
c14b8331 2908 struct ata_queued_cmd *qc;
1da177e4 2909 u8 status;
14be71f4
AL
2910 unsigned int poll_state = HSM_ST_UNKNOWN;
2911 unsigned int reg_state = HSM_ST_UNKNOWN;
14be71f4 2912
c14b8331
AL
2913 qc = ata_qc_from_tag(ap, ap->active_tag);
2914 assert(qc != NULL);
2915
14be71f4
AL
2916 switch (ap->hsm_task_state) {
2917 case HSM_ST:
2918 case HSM_ST_POLL:
2919 poll_state = HSM_ST_POLL;
2920 reg_state = HSM_ST;
1da177e4 2921 break;
14be71f4
AL
2922 case HSM_ST_LAST:
2923 case HSM_ST_LAST_POLL:
2924 poll_state = HSM_ST_LAST_POLL;
2925 reg_state = HSM_ST_LAST;
1da177e4
LT
2926 break;
2927 default:
2928 BUG();
2929 break;
2930 }
2931
2932 status = ata_chk_status(ap);
2933 if (status & ATA_BUSY) {
2934 if (time_after(jiffies, ap->pio_task_timeout)) {
11a56d24 2935 qc->err_mask |= AC_ERR_TIMEOUT;
7c398335 2936 ap->hsm_task_state = HSM_ST_TMOUT;
1da177e4
LT
2937 return 0;
2938 }
14be71f4 2939 ap->hsm_task_state = poll_state;
1da177e4
LT
2940 return ATA_SHORT_PAUSE;
2941 }
2942
14be71f4 2943 ap->hsm_task_state = reg_state;
1da177e4
LT
2944 return 0;
2945}
2946
2947/**
6f0ef4fa
RD
2948 * ata_pio_complete - check if drive is busy or idle
2949 * @ap: the target ata_port
1da177e4
LT
2950 *
2951 * LOCKING:
0cba632b 2952 * None. (executing in kernel thread context)
7fb6ec28
JG
2953 *
2954 * RETURNS:
2955 * Non-zero if qc completed, zero otherwise.
1da177e4
LT
2956 */
2957
7fb6ec28 2958static int ata_pio_complete (struct ata_port *ap)
1da177e4
LT
2959{
2960 struct ata_queued_cmd *qc;
2961 u8 drv_stat;
2962
2963 /*
31433ea3
AC
2964 * This is purely heuristic. This is a fast path. Sometimes when
2965 * we enter, BSY will be cleared in a chk-status or two. If not,
2966 * the drive is probably seeking or something. Snooze for a couple
2967 * msecs, then chk-status again. If still busy, fall back to
14be71f4 2968 * HSM_ST_POLL state.
1da177e4 2969 */
fe79e683
AL
2970 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2971 if (drv_stat & ATA_BUSY) {
1da177e4 2972 msleep(2);
fe79e683
AL
2973 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2974 if (drv_stat & ATA_BUSY) {
14be71f4 2975 ap->hsm_task_state = HSM_ST_LAST_POLL;
1da177e4 2976 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
7fb6ec28 2977 return 0;
1da177e4
LT
2978 }
2979 }
2980
c14b8331
AL
2981 qc = ata_qc_from_tag(ap, ap->active_tag);
2982 assert(qc != NULL);
2983
1da177e4
LT
2984 drv_stat = ata_wait_idle(ap);
2985 if (!ata_ok(drv_stat)) {
1c848984 2986 qc->err_mask |= __ac_err_mask(drv_stat);
14be71f4 2987 ap->hsm_task_state = HSM_ST_ERR;
7fb6ec28 2988 return 0;
1da177e4
LT
2989 }
2990
14be71f4 2991 ap->hsm_task_state = HSM_ST_IDLE;
1da177e4 2992
a22e2eb0
AL
2993 assert(qc->err_mask == 0);
2994 ata_poll_qc_complete(qc);
7fb6ec28
JG
2995
2996 /* another command may start at this point */
2997
2998 return 1;
1da177e4
LT
2999}
3000
0baab86b
EF
3001
3002/**
6f0ef4fa 3003 * swap_buf_le16 - swap halves of 16-words in place
0baab86b
EF
3004 * @buf: Buffer to swap
3005 * @buf_words: Number of 16-bit words in buffer.
3006 *
3007 * Swap halves of 16-bit words if needed to convert from
3008 * little-endian byte order to native cpu byte order, or
3009 * vice-versa.
3010 *
3011 * LOCKING:
6f0ef4fa 3012 * Inherited from caller.
0baab86b 3013 */
1da177e4
LT
3014void swap_buf_le16(u16 *buf, unsigned int buf_words)
3015{
3016#ifdef __BIG_ENDIAN
3017 unsigned int i;
3018
3019 for (i = 0; i < buf_words; i++)
3020 buf[i] = le16_to_cpu(buf[i]);
3021#endif /* __BIG_ENDIAN */
3022}
3023
6ae4cfb5
AL
3024/**
3025 * ata_mmio_data_xfer - Transfer data by MMIO
3026 * @ap: port to read/write
3027 * @buf: data buffer
3028 * @buflen: buffer length
344babaa 3029 * @write_data: read/write
6ae4cfb5
AL
3030 *
3031 * Transfer data from/to the device data register by MMIO.
3032 *
3033 * LOCKING:
3034 * Inherited from caller.
6ae4cfb5
AL
3035 */
3036
1da177e4
LT
3037static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3038 unsigned int buflen, int write_data)
3039{
3040 unsigned int i;
3041 unsigned int words = buflen >> 1;
3042 u16 *buf16 = (u16 *) buf;
3043 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3044
6ae4cfb5 3045 /* Transfer multiple of 2 bytes */
1da177e4
LT
3046 if (write_data) {
3047 for (i = 0; i < words; i++)
3048 writew(le16_to_cpu(buf16[i]), mmio);
3049 } else {
3050 for (i = 0; i < words; i++)
3051 buf16[i] = cpu_to_le16(readw(mmio));
3052 }
6ae4cfb5
AL
3053
3054 /* Transfer trailing 1 byte, if any. */
3055 if (unlikely(buflen & 0x01)) {
3056 u16 align_buf[1] = { 0 };
3057 unsigned char *trailing_buf = buf + buflen - 1;
3058
3059 if (write_data) {
3060 memcpy(align_buf, trailing_buf, 1);
3061 writew(le16_to_cpu(align_buf[0]), mmio);
3062 } else {
3063 align_buf[0] = cpu_to_le16(readw(mmio));
3064 memcpy(trailing_buf, align_buf, 1);
3065 }
3066 }
1da177e4
LT
3067}
3068
6ae4cfb5
AL
3069/**
3070 * ata_pio_data_xfer - Transfer data by PIO
3071 * @ap: port to read/write
3072 * @buf: data buffer
3073 * @buflen: buffer length
344babaa 3074 * @write_data: read/write
6ae4cfb5
AL
3075 *
3076 * Transfer data from/to the device data register by PIO.
3077 *
3078 * LOCKING:
3079 * Inherited from caller.
6ae4cfb5
AL
3080 */
3081
1da177e4
LT
3082static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3083 unsigned int buflen, int write_data)
3084{
6ae4cfb5 3085 unsigned int words = buflen >> 1;
1da177e4 3086
6ae4cfb5 3087 /* Transfer multiple of 2 bytes */
1da177e4 3088 if (write_data)
6ae4cfb5 3089 outsw(ap->ioaddr.data_addr, buf, words);
1da177e4 3090 else
6ae4cfb5
AL
3091 insw(ap->ioaddr.data_addr, buf, words);
3092
3093 /* Transfer trailing 1 byte, if any. */
3094 if (unlikely(buflen & 0x01)) {
3095 u16 align_buf[1] = { 0 };
3096 unsigned char *trailing_buf = buf + buflen - 1;
3097
3098 if (write_data) {
3099 memcpy(align_buf, trailing_buf, 1);
3100 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3101 } else {
3102 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3103 memcpy(trailing_buf, align_buf, 1);
3104 }
3105 }
1da177e4
LT
3106}
3107
6ae4cfb5
AL
3108/**
3109 * ata_data_xfer - Transfer data from/to the data register.
3110 * @ap: port to read/write
3111 * @buf: data buffer
3112 * @buflen: buffer length
3113 * @do_write: read/write
3114 *
3115 * Transfer data from/to the device data register.
3116 *
3117 * LOCKING:
3118 * Inherited from caller.
6ae4cfb5
AL
3119 */
3120
1da177e4
LT
3121static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3122 unsigned int buflen, int do_write)
3123{
a1bd9e68
AC
3124 /* Make the crap hardware pay the costs not the good stuff */
3125 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3126 unsigned long flags;
3127 local_irq_save(flags);
3128 if (ap->flags & ATA_FLAG_MMIO)
3129 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3130 else
3131 ata_pio_data_xfer(ap, buf, buflen, do_write);
3132 local_irq_restore(flags);
3133 } else {
3134 if (ap->flags & ATA_FLAG_MMIO)
3135 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3136 else
3137 ata_pio_data_xfer(ap, buf, buflen, do_write);
3138 }
1da177e4
LT
3139}
3140
6ae4cfb5
AL
3141/**
3142 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3143 * @qc: Command on going
3144 *
3145 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3146 *
3147 * LOCKING:
3148 * Inherited from caller.
3149 */
3150
1da177e4
LT
3151static void ata_pio_sector(struct ata_queued_cmd *qc)
3152{
3153 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
cedc9a47 3154 struct scatterlist *sg = qc->__sg;
1da177e4
LT
3155 struct ata_port *ap = qc->ap;
3156 struct page *page;
3157 unsigned int offset;
3158 unsigned char *buf;
3159
3160 if (qc->cursect == (qc->nsect - 1))
14be71f4 3161 ap->hsm_task_state = HSM_ST_LAST;
1da177e4
LT
3162
3163 page = sg[qc->cursg].page;
3164 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3165
3166 /* get the current page and offset */
3167 page = nth_page(page, (offset >> PAGE_SHIFT));
3168 offset %= PAGE_SIZE;
3169
3170 buf = kmap(page) + offset;
3171
3172 qc->cursect++;
3173 qc->cursg_ofs++;
3174
32529e01 3175 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
1da177e4
LT
3176 qc->cursg++;
3177 qc->cursg_ofs = 0;
3178 }
3179
3180 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3181
3182 /* do the actual data transfer */
3183 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3184 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3185
3186 kunmap(page);
3187}
3188
6ae4cfb5
AL
3189/**
3190 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3191 * @qc: Command on going
3192 * @bytes: number of bytes
3193 *
3194 * Transfer Transfer data from/to the ATAPI device.
3195 *
3196 * LOCKING:
3197 * Inherited from caller.
3198 *
3199 */
3200
1da177e4
LT
3201static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3202{
3203 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
cedc9a47 3204 struct scatterlist *sg = qc->__sg;
1da177e4
LT
3205 struct ata_port *ap = qc->ap;
3206 struct page *page;
3207 unsigned char *buf;
3208 unsigned int offset, count;
3209
563a6e1f 3210 if (qc->curbytes + bytes >= qc->nbytes)
14be71f4 3211 ap->hsm_task_state = HSM_ST_LAST;
1da177e4
LT
3212
3213next_sg:
563a6e1f 3214 if (unlikely(qc->cursg >= qc->n_elem)) {
7fb6ec28 3215 /*
563a6e1f
AL
3216 * The end of qc->sg is reached and the device expects
3217 * more data to transfer. In order not to overrun qc->sg
3218 * and fulfill length specified in the byte count register,
3219 * - for read case, discard trailing data from the device
3220 * - for write case, padding zero data to the device
3221 */
3222 u16 pad_buf[1] = { 0 };
3223 unsigned int words = bytes >> 1;
3224 unsigned int i;
3225
3226 if (words) /* warning if bytes > 1 */
7fb6ec28 3227 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
563a6e1f
AL
3228 ap->id, bytes);
3229
3230 for (i = 0; i < words; i++)
3231 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3232
14be71f4 3233 ap->hsm_task_state = HSM_ST_LAST;
563a6e1f
AL
3234 return;
3235 }
3236
cedc9a47 3237 sg = &qc->__sg[qc->cursg];
1da177e4 3238
1da177e4
LT
3239 page = sg->page;
3240 offset = sg->offset + qc->cursg_ofs;
3241
3242 /* get the current page and offset */
3243 page = nth_page(page, (offset >> PAGE_SHIFT));
3244 offset %= PAGE_SIZE;
3245
6952df03 3246 /* don't overrun current sg */
32529e01 3247 count = min(sg->length - qc->cursg_ofs, bytes);
1da177e4
LT
3248
3249 /* don't cross page boundaries */
3250 count = min(count, (unsigned int)PAGE_SIZE - offset);
3251
3252 buf = kmap(page) + offset;
3253
3254 bytes -= count;
3255 qc->curbytes += count;
3256 qc->cursg_ofs += count;
3257
32529e01 3258 if (qc->cursg_ofs == sg->length) {
1da177e4
LT
3259 qc->cursg++;
3260 qc->cursg_ofs = 0;
3261 }
3262
3263 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3264
3265 /* do the actual data transfer */
3266 ata_data_xfer(ap, buf, count, do_write);
3267
3268 kunmap(page);
3269
563a6e1f 3270 if (bytes)
1da177e4 3271 goto next_sg;
1da177e4
LT
3272}
3273
6ae4cfb5
AL
3274/**
3275 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3276 * @qc: Command on going
3277 *
3278 * Transfer Transfer data from/to the ATAPI device.
3279 *
3280 * LOCKING:
3281 * Inherited from caller.
6ae4cfb5
AL
3282 */
3283
1da177e4
LT
3284static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3285{
3286 struct ata_port *ap = qc->ap;
3287 struct ata_device *dev = qc->dev;
3288 unsigned int ireason, bc_lo, bc_hi, bytes;
3289 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3290
3291 ap->ops->tf_read(ap, &qc->tf);
3292 ireason = qc->tf.nsect;
3293 bc_lo = qc->tf.lbam;
3294 bc_hi = qc->tf.lbah;
3295 bytes = (bc_hi << 8) | bc_lo;
3296
3297 /* shall be cleared to zero, indicating xfer of data */
3298 if (ireason & (1 << 0))
3299 goto err_out;
3300
3301 /* make sure transfer direction matches expected */
3302 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3303 if (do_write != i_write)
3304 goto err_out;
3305
3306 __atapi_pio_bytes(qc, bytes);
3307
3308 return;
3309
3310err_out:
3311 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3312 ap->id, dev->devno);
11a56d24 3313 qc->err_mask |= AC_ERR_HSM;
14be71f4 3314 ap->hsm_task_state = HSM_ST_ERR;
1da177e4
LT
3315}
3316
3317/**
6f0ef4fa
RD
3318 * ata_pio_block - start PIO on a block
3319 * @ap: the target ata_port
1da177e4
LT
3320 *
3321 * LOCKING:
0cba632b 3322 * None. (executing in kernel thread context)
1da177e4
LT
3323 */
3324
3325static void ata_pio_block(struct ata_port *ap)
3326{
3327 struct ata_queued_cmd *qc;
3328 u8 status;
3329
3330 /*
6f0ef4fa 3331 * This is purely heuristic. This is a fast path.
1da177e4
LT
3332 * Sometimes when we enter, BSY will be cleared in
3333 * a chk-status or two. If not, the drive is probably seeking
3334 * or something. Snooze for a couple msecs, then
3335 * chk-status again. If still busy, fall back to
14be71f4 3336 * HSM_ST_POLL state.
1da177e4
LT
3337 */
3338 status = ata_busy_wait(ap, ATA_BUSY, 5);
3339 if (status & ATA_BUSY) {
3340 msleep(2);
3341 status = ata_busy_wait(ap, ATA_BUSY, 10);
3342 if (status & ATA_BUSY) {
14be71f4 3343 ap->hsm_task_state = HSM_ST_POLL;
1da177e4
LT
3344 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3345 return;
3346 }
3347 }
3348
3349 qc = ata_qc_from_tag(ap, ap->active_tag);
3350 assert(qc != NULL);
3351
fe79e683
AL
3352 /* check error */
3353 if (status & (ATA_ERR | ATA_DF)) {
3354 qc->err_mask |= AC_ERR_DEV;
3355 ap->hsm_task_state = HSM_ST_ERR;
3356 return;
3357 }
3358
3359 /* transfer data if any */
1da177e4 3360 if (is_atapi_taskfile(&qc->tf)) {
fe79e683 3361 /* DRQ=0 means no more data to transfer */
1da177e4 3362 if ((status & ATA_DRQ) == 0) {
14be71f4 3363 ap->hsm_task_state = HSM_ST_LAST;
1da177e4
LT
3364 return;
3365 }
3366
3367 atapi_pio_bytes(qc);
3368 } else {
3369 /* handle BSY=0, DRQ=0 as error */
3370 if ((status & ATA_DRQ) == 0) {
11a56d24 3371 qc->err_mask |= AC_ERR_HSM;
14be71f4 3372 ap->hsm_task_state = HSM_ST_ERR;
1da177e4
LT
3373 return;
3374 }
3375
3376 ata_pio_sector(qc);
3377 }
3378}
3379
3380static void ata_pio_error(struct ata_port *ap)
3381{
3382 struct ata_queued_cmd *qc;
a7dac447
JG
3383
3384 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
1da177e4
LT
3385
3386 qc = ata_qc_from_tag(ap, ap->active_tag);
3387 assert(qc != NULL);
3388
1c848984
AL
3389 /* make sure qc->err_mask is available to
3390 * know what's wrong and recover
3391 */
3392 assert(qc->err_mask);
3393
14be71f4 3394 ap->hsm_task_state = HSM_ST_IDLE;
1da177e4 3395
a22e2eb0 3396 ata_poll_qc_complete(qc);
1da177e4
LT
3397}
3398
3399static void ata_pio_task(void *_data)
3400{
3401 struct ata_port *ap = _data;
7fb6ec28
JG
3402 unsigned long timeout;
3403 int qc_completed;
3404
3405fsm_start:
3406 timeout = 0;
3407 qc_completed = 0;
1da177e4 3408
14be71f4
AL
3409 switch (ap->hsm_task_state) {
3410 case HSM_ST_IDLE:
1da177e4
LT
3411 return;
3412
14be71f4 3413 case HSM_ST:
1da177e4
LT
3414 ata_pio_block(ap);
3415 break;
3416
14be71f4 3417 case HSM_ST_LAST:
7fb6ec28 3418 qc_completed = ata_pio_complete(ap);
1da177e4
LT
3419 break;
3420
14be71f4
AL
3421 case HSM_ST_POLL:
3422 case HSM_ST_LAST_POLL:
1da177e4
LT
3423 timeout = ata_pio_poll(ap);
3424 break;
3425
14be71f4
AL
3426 case HSM_ST_TMOUT:
3427 case HSM_ST_ERR:
1da177e4
LT
3428 ata_pio_error(ap);
3429 return;
3430 }
3431
3432 if (timeout)
95064379 3433 ata_queue_delayed_pio_task(ap, timeout);
7fb6ec28
JG
3434 else if (!qc_completed)
3435 goto fsm_start;
1da177e4
LT
3436}
3437
1da177e4
LT
3438/**
3439 * ata_qc_timeout - Handle timeout of queued command
3440 * @qc: Command that timed out
3441 *
3442 * Some part of the kernel (currently, only the SCSI layer)
3443 * has noticed that the active command on port @ap has not
3444 * completed after a specified length of time. Handle this
3445 * condition by disabling DMA (if necessary) and completing
3446 * transactions, with error if necessary.
3447 *
3448 * This also handles the case of the "lost interrupt", where
3449 * for some reason (possibly hardware bug, possibly driver bug)
3450 * an interrupt was not delivered to the driver, even though the
3451 * transaction completed successfully.
3452 *
3453 * LOCKING:
0cba632b 3454 * Inherited from SCSI layer (none, can sleep)
1da177e4
LT
3455 */
3456
3457static void ata_qc_timeout(struct ata_queued_cmd *qc)
3458{
3459 struct ata_port *ap = qc->ap;
b8f6153e 3460 struct ata_host_set *host_set = ap->host_set;
1da177e4 3461 u8 host_stat = 0, drv_stat;
b8f6153e 3462 unsigned long flags;
1da177e4
LT
3463
3464 DPRINTK("ENTER\n");
3465
b8f6153e
JG
3466 spin_lock_irqsave(&host_set->lock, flags);
3467
1da177e4
LT
3468 switch (qc->tf.protocol) {
3469
3470 case ATA_PROT_DMA:
3471 case ATA_PROT_ATAPI_DMA:
3472 host_stat = ap->ops->bmdma_status(ap);
3473
3474 /* before we do anything else, clear DMA-Start bit */
b73fc89f 3475 ap->ops->bmdma_stop(qc);
1da177e4
LT
3476
3477 /* fall through */
3478
3479 default:
3480 ata_altstatus(ap);
3481 drv_stat = ata_chk_status(ap);
3482
3483 /* ack bmdma irq events */
3484 ap->ops->irq_clear(ap);
3485
3486 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3487 ap->id, qc->tf.command, drv_stat, host_stat);
3488
3489 /* complete taskfile transaction */
a22e2eb0 3490 qc->err_mask |= ac_err_mask(drv_stat);
1da177e4
LT
3491 break;
3492 }
b8f6153e
JG
3493
3494 spin_unlock_irqrestore(&host_set->lock, flags);
3495
a72ec4ce
TH
3496 ata_eh_qc_complete(qc);
3497
1da177e4
LT
3498 DPRINTK("EXIT\n");
3499}
3500
3501/**
3502 * ata_eng_timeout - Handle timeout of queued command
3503 * @ap: Port on which timed-out command is active
3504 *
3505 * Some part of the kernel (currently, only the SCSI layer)
3506 * has noticed that the active command on port @ap has not
3507 * completed after a specified length of time. Handle this
3508 * condition by disabling DMA (if necessary) and completing
3509 * transactions, with error if necessary.
3510 *
3511 * This also handles the case of the "lost interrupt", where
3512 * for some reason (possibly hardware bug, possibly driver bug)
3513 * an interrupt was not delivered to the driver, even though the
3514 * transaction completed successfully.
3515 *
3516 * LOCKING:
3517 * Inherited from SCSI layer (none, can sleep)
3518 */
3519
3520void ata_eng_timeout(struct ata_port *ap)
3521{
3522 struct ata_queued_cmd *qc;
3523
3524 DPRINTK("ENTER\n");
3525
3526 qc = ata_qc_from_tag(ap, ap->active_tag);
e12669e7
JG
3527 if (qc)
3528 ata_qc_timeout(qc);
3529 else {
1da177e4
LT
3530 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3531 ap->id);
3532 goto out;
3533 }
3534
1da177e4
LT
3535out:
3536 DPRINTK("EXIT\n");
3537}
3538
3539/**
3540 * ata_qc_new - Request an available ATA command, for queueing
3541 * @ap: Port associated with device @dev
3542 * @dev: Device from whom we request an available command structure
3543 *
3544 * LOCKING:
0cba632b 3545 * None.
1da177e4
LT
3546 */
3547
3548static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3549{
3550 struct ata_queued_cmd *qc = NULL;
3551 unsigned int i;
3552
3553 for (i = 0; i < ATA_MAX_QUEUE; i++)
3554 if (!test_and_set_bit(i, &ap->qactive)) {
3555 qc = ata_qc_from_tag(ap, i);
3556 break;
3557 }
3558
3559 if (qc)
3560 qc->tag = i;
3561
3562 return qc;
3563}
3564
3565/**
3566 * ata_qc_new_init - Request an available ATA command, and initialize it
3567 * @ap: Port associated with device @dev
3568 * @dev: Device from whom we request an available command structure
3569 *
3570 * LOCKING:
0cba632b 3571 * None.
1da177e4
LT
3572 */
3573
3574struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3575 struct ata_device *dev)
3576{
3577 struct ata_queued_cmd *qc;
3578
3579 qc = ata_qc_new(ap);
3580 if (qc) {
1da177e4
LT
3581 qc->scsicmd = NULL;
3582 qc->ap = ap;
3583 qc->dev = dev;
1da177e4 3584
2c13b7ce 3585 ata_qc_reinit(qc);
1da177e4
LT
3586 }
3587
3588 return qc;
3589}
3590
1da177e4
LT
3591/**
3592 * ata_qc_free - free unused ata_queued_cmd
3593 * @qc: Command to complete
3594 *
3595 * Designed to free unused ata_queued_cmd object
3596 * in case something prevents using it.
3597 *
3598 * LOCKING:
0cba632b 3599 * spin_lock_irqsave(host_set lock)
1da177e4
LT
3600 */
3601void ata_qc_free(struct ata_queued_cmd *qc)
3602{
4ba946e9
TH
3603 struct ata_port *ap = qc->ap;
3604 unsigned int tag;
3605
1da177e4 3606 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
1da177e4 3607
4ba946e9
TH
3608 qc->flags = 0;
3609 tag = qc->tag;
3610 if (likely(ata_tag_valid(tag))) {
3611 if (tag == ap->active_tag)
3612 ap->active_tag = ATA_TAG_POISON;
3613 qc->tag = ATA_TAG_POISON;
3614 clear_bit(tag, &ap->qactive);
3615 }
1da177e4
LT
3616}
3617
3618/**
3619 * ata_qc_complete - Complete an active ATA command
3620 * @qc: Command to complete
8e8b77dd 3621 * @err_mask: ATA Status register contents
0cba632b
JG
3622 *
3623 * Indicate to the mid and upper layers that an ATA
3624 * command has completed, with either an ok or not-ok status.
1da177e4
LT
3625 *
3626 * LOCKING:
0cba632b 3627 * spin_lock_irqsave(host_set lock)
1da177e4
LT
3628 */
3629
a22e2eb0 3630void ata_qc_complete(struct ata_queued_cmd *qc)
1da177e4 3631{
1da177e4
LT
3632 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3633 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3634
3635 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3636 ata_sg_clean(qc);
3637
3f3791d3
AL
3638 /* atapi: mark qc as inactive to prevent the interrupt handler
3639 * from completing the command twice later, before the error handler
3640 * is called. (when rc != 0 and atapi request sense is needed)
3641 */
3642 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3643
1da177e4 3644 /* call completion callback */
77853bf2 3645 qc->complete_fn(qc);
1da177e4
LT
3646}
3647
3648static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3649{
3650 struct ata_port *ap = qc->ap;
3651
3652 switch (qc->tf.protocol) {
3653 case ATA_PROT_DMA:
3654 case ATA_PROT_ATAPI_DMA:
3655 return 1;
3656
3657 case ATA_PROT_ATAPI:
3658 case ATA_PROT_PIO:
3659 case ATA_PROT_PIO_MULT:
3660 if (ap->flags & ATA_FLAG_PIO_DMA)
3661 return 1;
3662
3663 /* fall through */
3664
3665 default:
3666 return 0;
3667 }
3668
3669 /* never reached */
3670}
3671
3672/**
3673 * ata_qc_issue - issue taskfile to device
3674 * @qc: command to issue to device
3675 *
3676 * Prepare an ATA command to submission to device.
3677 * This includes mapping the data into a DMA-able
3678 * area, filling in the S/G table, and finally
3679 * writing the taskfile to hardware, starting the command.
3680 *
3681 * LOCKING:
3682 * spin_lock_irqsave(host_set lock)
3683 *
3684 * RETURNS:
9a3d9eb0 3685 * Zero on success, AC_ERR_* mask on failure
1da177e4
LT
3686 */
3687
9a3d9eb0 3688unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
1da177e4
LT
3689{
3690 struct ata_port *ap = qc->ap;
3691
3692 if (ata_should_dma_map(qc)) {
3693 if (qc->flags & ATA_QCFLAG_SG) {
3694 if (ata_sg_setup(qc))
8e436af9 3695 goto sg_err;
1da177e4
LT
3696 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3697 if (ata_sg_setup_one(qc))
8e436af9 3698 goto sg_err;
1da177e4
LT
3699 }
3700 } else {
3701 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3702 }
3703
3704 ap->ops->qc_prep(qc);
3705
3706 qc->ap->active_tag = qc->tag;
3707 qc->flags |= ATA_QCFLAG_ACTIVE;
3708
3709 return ap->ops->qc_issue(qc);
3710
8e436af9
TH
3711sg_err:
3712 qc->flags &= ~ATA_QCFLAG_DMAMAP;
9a3d9eb0 3713 return AC_ERR_SYSTEM;
1da177e4
LT
3714}
3715
0baab86b 3716
1da177e4
LT
3717/**
3718 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3719 * @qc: command to issue to device
3720 *
3721 * Using various libata functions and hooks, this function
3722 * starts an ATA command. ATA commands are grouped into
3723 * classes called "protocols", and issuing each type of protocol
3724 * is slightly different.
3725 *
0baab86b
EF
3726 * May be used as the qc_issue() entry in ata_port_operations.
3727 *
1da177e4
LT
3728 * LOCKING:
3729 * spin_lock_irqsave(host_set lock)
3730 *
3731 * RETURNS:
9a3d9eb0 3732 * Zero on success, AC_ERR_* mask on failure
1da177e4
LT
3733 */
3734
9a3d9eb0 3735unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
1da177e4
LT
3736{
3737 struct ata_port *ap = qc->ap;
3738
3739 ata_dev_select(ap, qc->dev->devno, 1, 0);
3740
3741 switch (qc->tf.protocol) {
3742 case ATA_PROT_NODATA:
e5338254 3743 ata_tf_to_host(ap, &qc->tf);
1da177e4
LT
3744 break;
3745
3746 case ATA_PROT_DMA:
3747 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3748 ap->ops->bmdma_setup(qc); /* set up bmdma */
3749 ap->ops->bmdma_start(qc); /* initiate bmdma */
3750 break;
3751
3752 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3753 ata_qc_set_polling(qc);
e5338254 3754 ata_tf_to_host(ap, &qc->tf);
14be71f4 3755 ap->hsm_task_state = HSM_ST;
95064379 3756 ata_queue_pio_task(ap);
1da177e4
LT
3757 break;
3758
3759 case ATA_PROT_ATAPI:
3760 ata_qc_set_polling(qc);
e5338254 3761 ata_tf_to_host(ap, &qc->tf);
95064379 3762 ata_queue_packet_task(ap);
1da177e4
LT
3763 break;
3764
3765 case ATA_PROT_ATAPI_NODATA:
c1389503 3766 ap->flags |= ATA_FLAG_NOINTR;
e5338254 3767 ata_tf_to_host(ap, &qc->tf);
95064379 3768 ata_queue_packet_task(ap);
1da177e4
LT
3769 break;
3770
3771 case ATA_PROT_ATAPI_DMA:
c1389503 3772 ap->flags |= ATA_FLAG_NOINTR;
1da177e4
LT
3773 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3774 ap->ops->bmdma_setup(qc); /* set up bmdma */
95064379 3775 ata_queue_packet_task(ap);
1da177e4
LT
3776 break;
3777
3778 default:
3779 WARN_ON(1);
9a3d9eb0 3780 return AC_ERR_SYSTEM;
1da177e4
LT
3781 }
3782
3783 return 0;
3784}
3785
3786/**
0baab86b 3787 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
1da177e4
LT
3788 * @qc: Info associated with this ATA transaction.
3789 *
3790 * LOCKING:
3791 * spin_lock_irqsave(host_set lock)
3792 */
3793
3794static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3795{
3796 struct ata_port *ap = qc->ap;
3797 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3798 u8 dmactl;
3799 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3800
3801 /* load PRD table addr. */
3802 mb(); /* make sure PRD table writes are visible to controller */
3803 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3804
3805 /* specify data direction, triple-check start bit is clear */
3806 dmactl = readb(mmio + ATA_DMA_CMD);
3807 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3808 if (!rw)
3809 dmactl |= ATA_DMA_WR;
3810 writeb(dmactl, mmio + ATA_DMA_CMD);
3811
3812 /* issue r/w command */
3813 ap->ops->exec_command(ap, &qc->tf);
3814}
3815
3816/**
b73fc89f 3817 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
1da177e4
LT
3818 * @qc: Info associated with this ATA transaction.
3819 *
3820 * LOCKING:
3821 * spin_lock_irqsave(host_set lock)
3822 */
3823
3824static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3825{
3826 struct ata_port *ap = qc->ap;
3827 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3828 u8 dmactl;
3829
3830 /* start host DMA transaction */
3831 dmactl = readb(mmio + ATA_DMA_CMD);
3832 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3833
3834 /* Strictly, one may wish to issue a readb() here, to
3835 * flush the mmio write. However, control also passes
3836 * to the hardware at this point, and it will interrupt
3837 * us when we are to resume control. So, in effect,
3838 * we don't care when the mmio write flushes.
3839 * Further, a read of the DMA status register _immediately_
3840 * following the write may not be what certain flaky hardware
3841 * is expected, so I think it is best to not add a readb()
3842 * without first all the MMIO ATA cards/mobos.
3843 * Or maybe I'm just being paranoid.
3844 */
3845}
3846
3847/**
3848 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3849 * @qc: Info associated with this ATA transaction.
3850 *
3851 * LOCKING:
3852 * spin_lock_irqsave(host_set lock)
3853 */
3854
3855static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3856{
3857 struct ata_port *ap = qc->ap;
3858 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3859 u8 dmactl;
3860
3861 /* load PRD table addr. */
3862 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3863
3864 /* specify data direction, triple-check start bit is clear */
3865 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3866 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3867 if (!rw)
3868 dmactl |= ATA_DMA_WR;
3869 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3870
3871 /* issue r/w command */
3872 ap->ops->exec_command(ap, &qc->tf);
3873}
3874
3875/**
3876 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3877 * @qc: Info associated with this ATA transaction.
3878 *
3879 * LOCKING:
3880 * spin_lock_irqsave(host_set lock)
3881 */
3882
3883static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3884{
3885 struct ata_port *ap = qc->ap;
3886 u8 dmactl;
3887
3888 /* start host DMA transaction */
3889 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3890 outb(dmactl | ATA_DMA_START,
3891 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3892}
3893
0baab86b
EF
3894
3895/**
3896 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3897 * @qc: Info associated with this ATA transaction.
3898 *
3899 * Writes the ATA_DMA_START flag to the DMA command register.
3900 *
3901 * May be used as the bmdma_start() entry in ata_port_operations.
3902 *
3903 * LOCKING:
3904 * spin_lock_irqsave(host_set lock)
3905 */
1da177e4
LT
3906void ata_bmdma_start(struct ata_queued_cmd *qc)
3907{
3908 if (qc->ap->flags & ATA_FLAG_MMIO)
3909 ata_bmdma_start_mmio(qc);
3910 else
3911 ata_bmdma_start_pio(qc);
3912}
3913
0baab86b
EF
3914
3915/**
3916 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3917 * @qc: Info associated with this ATA transaction.
3918 *
3919 * Writes address of PRD table to device's PRD Table Address
3920 * register, sets the DMA control register, and calls
3921 * ops->exec_command() to start the transfer.
3922 *
3923 * May be used as the bmdma_setup() entry in ata_port_operations.
3924 *
3925 * LOCKING:
3926 * spin_lock_irqsave(host_set lock)
3927 */
1da177e4
LT
3928void ata_bmdma_setup(struct ata_queued_cmd *qc)
3929{
3930 if (qc->ap->flags & ATA_FLAG_MMIO)
3931 ata_bmdma_setup_mmio(qc);
3932 else
3933 ata_bmdma_setup_pio(qc);
3934}
3935
0baab86b
EF
3936
3937/**
3938 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
decc6d0b 3939 * @ap: Port associated with this ATA transaction.
0baab86b
EF
3940 *
3941 * Clear interrupt and error flags in DMA status register.
3942 *
3943 * May be used as the irq_clear() entry in ata_port_operations.
3944 *
3945 * LOCKING:
3946 * spin_lock_irqsave(host_set lock)
3947 */
3948
1da177e4
LT
3949void ata_bmdma_irq_clear(struct ata_port *ap)
3950{
3951 if (ap->flags & ATA_FLAG_MMIO) {
3952 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3953 writeb(readb(mmio), mmio);
3954 } else {
3955 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3956 outb(inb(addr), addr);
3957 }
3958
3959}
3960
0baab86b
EF
3961
3962/**
3963 * ata_bmdma_status - Read PCI IDE BMDMA status
decc6d0b 3964 * @ap: Port associated with this ATA transaction.
0baab86b
EF
3965 *
3966 * Read and return BMDMA status register.
3967 *
3968 * May be used as the bmdma_status() entry in ata_port_operations.
3969 *
3970 * LOCKING:
3971 * spin_lock_irqsave(host_set lock)
3972 */
3973
1da177e4
LT
3974u8 ata_bmdma_status(struct ata_port *ap)
3975{
3976 u8 host_stat;
3977 if (ap->flags & ATA_FLAG_MMIO) {
3978 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3979 host_stat = readb(mmio + ATA_DMA_STATUS);
3980 } else
ee500aab 3981 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
1da177e4
LT
3982 return host_stat;
3983}
3984
0baab86b
EF
3985
3986/**
3987 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
b73fc89f 3988 * @qc: Command we are ending DMA for
0baab86b
EF
3989 *
3990 * Clears the ATA_DMA_START flag in the dma control register
3991 *
3992 * May be used as the bmdma_stop() entry in ata_port_operations.
3993 *
3994 * LOCKING:
3995 * spin_lock_irqsave(host_set lock)
3996 */
3997
b73fc89f 3998void ata_bmdma_stop(struct ata_queued_cmd *qc)
1da177e4 3999{
b73fc89f 4000 struct ata_port *ap = qc->ap;
1da177e4
LT
4001 if (ap->flags & ATA_FLAG_MMIO) {
4002 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4003
4004 /* clear start/stop bit */
4005 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4006 mmio + ATA_DMA_CMD);
4007 } else {
4008 /* clear start/stop bit */
4009 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4010 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4011 }
4012
4013 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4014 ata_altstatus(ap); /* dummy read */
4015}
4016
4017/**
4018 * ata_host_intr - Handle host interrupt for given (port, task)
4019 * @ap: Port on which interrupt arrived (possibly...)
4020 * @qc: Taskfile currently active in engine
4021 *
4022 * Handle host interrupt for given queued command. Currently,
4023 * only DMA interrupts are handled. All other commands are
4024 * handled via polling with interrupts disabled (nIEN bit).
4025 *
4026 * LOCKING:
4027 * spin_lock_irqsave(host_set lock)
4028 *
4029 * RETURNS:
4030 * One if interrupt was handled, zero if not (shared irq).
4031 */
4032
4033inline unsigned int ata_host_intr (struct ata_port *ap,
4034 struct ata_queued_cmd *qc)
4035{
4036 u8 status, host_stat;
4037
4038 switch (qc->tf.protocol) {
4039
4040 case ATA_PROT_DMA:
4041 case ATA_PROT_ATAPI_DMA:
4042 case ATA_PROT_ATAPI:
4043 /* check status of DMA engine */
4044 host_stat = ap->ops->bmdma_status(ap);
4045 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4046
4047 /* if it's not our irq... */
4048 if (!(host_stat & ATA_DMA_INTR))
4049 goto idle_irq;
4050
4051 /* before we do anything else, clear DMA-Start bit */
b73fc89f 4052 ap->ops->bmdma_stop(qc);
1da177e4
LT
4053
4054 /* fall through */
4055
4056 case ATA_PROT_ATAPI_NODATA:
4057 case ATA_PROT_NODATA:
4058 /* check altstatus */
4059 status = ata_altstatus(ap);
4060 if (status & ATA_BUSY)
4061 goto idle_irq;
4062
4063 /* check main status, clearing INTRQ */
4064 status = ata_chk_status(ap);
4065 if (unlikely(status & ATA_BUSY))
4066 goto idle_irq;
4067 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4068 ap->id, qc->tf.protocol, status);
4069
4070 /* ack bmdma irq events */
4071 ap->ops->irq_clear(ap);
4072
4073 /* complete taskfile transaction */
a22e2eb0
AL
4074 qc->err_mask |= ac_err_mask(status);
4075 ata_qc_complete(qc);
1da177e4
LT
4076 break;
4077
4078 default:
4079 goto idle_irq;
4080 }
4081
4082 return 1; /* irq handled */
4083
4084idle_irq:
4085 ap->stats.idle_irq++;
4086
4087#ifdef ATA_IRQ_TRAP
4088 if ((ap->stats.idle_irq % 1000) == 0) {
4089 handled = 1;
4090 ata_irq_ack(ap, 0); /* debug trap */
4091 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4092 }
4093#endif
4094 return 0; /* irq not handled */
4095}
4096
4097/**
4098 * ata_interrupt - Default ATA host interrupt handler
0cba632b
JG
4099 * @irq: irq line (unused)
4100 * @dev_instance: pointer to our ata_host_set information structure
1da177e4
LT
4101 * @regs: unused
4102 *
0cba632b
JG
4103 * Default interrupt handler for PCI IDE devices. Calls
4104 * ata_host_intr() for each port that is not disabled.
4105 *
1da177e4 4106 * LOCKING:
0cba632b 4107 * Obtains host_set lock during operation.
1da177e4
LT
4108 *
4109 * RETURNS:
0cba632b 4110 * IRQ_NONE or IRQ_HANDLED.
1da177e4
LT
4111 */
4112
4113irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4114{
4115 struct ata_host_set *host_set = dev_instance;
4116 unsigned int i;
4117 unsigned int handled = 0;
4118 unsigned long flags;
4119
4120 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4121 spin_lock_irqsave(&host_set->lock, flags);
4122
4123 for (i = 0; i < host_set->n_ports; i++) {
4124 struct ata_port *ap;
4125
4126 ap = host_set->ports[i];
c1389503
TH
4127 if (ap &&
4128 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
1da177e4
LT
4129 struct ata_queued_cmd *qc;
4130
4131 qc = ata_qc_from_tag(ap, ap->active_tag);
21b1ed74
AL
4132 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4133 (qc->flags & ATA_QCFLAG_ACTIVE))
1da177e4
LT
4134 handled |= ata_host_intr(ap, qc);
4135 }
4136 }
4137
4138 spin_unlock_irqrestore(&host_set->lock, flags);
4139
4140 return IRQ_RETVAL(handled);
4141}
4142
4143/**
4144 * atapi_packet_task - Write CDB bytes to hardware
4145 * @_data: Port to which ATAPI device is attached.
4146 *
4147 * When device has indicated its readiness to accept
4148 * a CDB, this function is called. Send the CDB.
4149 * If DMA is to be performed, exit immediately.
4150 * Otherwise, we are in polling mode, so poll
4151 * status under operation succeeds or fails.
4152 *
4153 * LOCKING:
4154 * Kernel thread context (may sleep)
4155 */
4156
4157static void atapi_packet_task(void *_data)
4158{
4159 struct ata_port *ap = _data;
4160 struct ata_queued_cmd *qc;
4161 u8 status;
4162
4163 qc = ata_qc_from_tag(ap, ap->active_tag);
4164 assert(qc != NULL);
4165 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4166
4167 /* sleep-wait for BSY to clear */
4168 DPRINTK("busy wait\n");
d8fe452b 4169 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
11a56d24 4170 qc->err_mask |= AC_ERR_TIMEOUT;
d8fe452b
AL
4171 goto err_out;
4172 }
1da177e4
LT
4173
4174 /* make sure DRQ is set */
4175 status = ata_chk_status(ap);
d8fe452b 4176 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
11a56d24 4177 qc->err_mask |= AC_ERR_HSM;
1da177e4 4178 goto err_out;
d8fe452b 4179 }
1da177e4
LT
4180
4181 /* send SCSI cdb */
4182 DPRINTK("send cdb\n");
4183 assert(ap->cdb_len >= 12);
1da177e4 4184
c1389503
TH
4185 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4186 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4187 unsigned long flags;
1da177e4 4188
c1389503
TH
4189 /* Once we're done issuing command and kicking bmdma,
4190 * irq handler takes over. To not lose irq, we need
4191 * to clear NOINTR flag before sending cdb, but
4192 * interrupt handler shouldn't be invoked before we're
4193 * finished. Hence, the following locking.
4194 */
4195 spin_lock_irqsave(&ap->host_set->lock, flags);
4196 ap->flags &= ~ATA_FLAG_NOINTR;
4197 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4198 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4199 ap->ops->bmdma_start(qc); /* initiate bmdma */
4200 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4201 } else {
4202 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
1da177e4 4203
c1389503 4204 /* PIO commands are handled by polling */
14be71f4 4205 ap->hsm_task_state = HSM_ST;
95064379 4206 ata_queue_pio_task(ap);
1da177e4
LT
4207 }
4208
4209 return;
4210
4211err_out:
a22e2eb0 4212 ata_poll_qc_complete(qc);
1da177e4
LT
4213}
4214
0baab86b
EF
4215
4216/**
4217 * ata_port_start - Set port up for dma.
4218 * @ap: Port to initialize
4219 *
4220 * Called just after data structures for each port are
4221 * initialized. Allocates space for PRD table.
4222 *
4223 * May be used as the port_start() entry in ata_port_operations.
4224 *
4225 * LOCKING:
6f0ef4fa 4226 * Inherited from caller.
0baab86b
EF
4227 */
4228
9b847548
JA
4229/*
4230 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4231 * without filling any other registers
4232 */
4233static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4234 u8 cmd)
4235{
4236 struct ata_taskfile tf;
4237 int err;
4238
4239 ata_tf_init(ap, &tf, dev->devno);
4240
4241 tf.command = cmd;
4242 tf.flags |= ATA_TFLAG_DEVICE;
4243 tf.protocol = ATA_PROT_NODATA;
4244
4245 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4246 if (err)
4247 printk(KERN_ERR "%s: ata command failed: %d\n",
4248 __FUNCTION__, err);
4249
4250 return err;
4251}
4252
4253static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4254{
4255 u8 cmd;
4256
4257 if (!ata_try_flush_cache(dev))
4258 return 0;
4259
4260 if (ata_id_has_flush_ext(dev->id))
4261 cmd = ATA_CMD_FLUSH_EXT;
4262 else
4263 cmd = ATA_CMD_FLUSH;
4264
4265 return ata_do_simple_cmd(ap, dev, cmd);
4266}
4267
4268static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4269{
4270 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4271}
4272
4273static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4274{
4275 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4276}
4277
4278/**
4279 * ata_device_resume - wakeup a previously suspended devices
4280 *
4281 * Kick the drive back into action, by sending it an idle immediate
4282 * command and making sure its transfer mode matches between drive
4283 * and host.
4284 *
4285 */
4286int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4287{
4288 if (ap->flags & ATA_FLAG_SUSPENDED) {
4289 ap->flags &= ~ATA_FLAG_SUSPENDED;
4290 ata_set_mode(ap);
4291 }
4292 if (!ata_dev_present(dev))
4293 return 0;
4294 if (dev->class == ATA_DEV_ATA)
4295 ata_start_drive(ap, dev);
4296
4297 return 0;
4298}
4299
4300/**
4301 * ata_device_suspend - prepare a device for suspend
4302 *
4303 * Flush the cache on the drive, if appropriate, then issue a
4304 * standbynow command.
4305 *
4306 */
4307int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4308{
4309 if (!ata_dev_present(dev))
4310 return 0;
4311 if (dev->class == ATA_DEV_ATA)
4312 ata_flush_cache(ap, dev);
4313
4314 ata_standby_drive(ap, dev);
4315 ap->flags |= ATA_FLAG_SUSPENDED;
4316 return 0;
4317}
4318
1da177e4
LT
4319int ata_port_start (struct ata_port *ap)
4320{
4321 struct device *dev = ap->host_set->dev;
6037d6bb 4322 int rc;
1da177e4
LT
4323
4324 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4325 if (!ap->prd)
4326 return -ENOMEM;
4327
6037d6bb
JG
4328 rc = ata_pad_alloc(ap, dev);
4329 if (rc) {
cedc9a47 4330 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
6037d6bb 4331 return rc;
cedc9a47
JG
4332 }
4333
1da177e4
LT
4334 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4335
4336 return 0;
4337}
4338
0baab86b
EF
4339
4340/**
4341 * ata_port_stop - Undo ata_port_start()
4342 * @ap: Port to shut down
4343 *
4344 * Frees the PRD table.
4345 *
4346 * May be used as the port_stop() entry in ata_port_operations.
4347 *
4348 * LOCKING:
6f0ef4fa 4349 * Inherited from caller.
0baab86b
EF
4350 */
4351
1da177e4
LT
4352void ata_port_stop (struct ata_port *ap)
4353{
4354 struct device *dev = ap->host_set->dev;
4355
4356 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
6037d6bb 4357 ata_pad_free(ap, dev);
1da177e4
LT
4358}
4359
aa8f0dc6
JG
4360void ata_host_stop (struct ata_host_set *host_set)
4361{
4362 if (host_set->mmio_base)
4363 iounmap(host_set->mmio_base);
4364}
4365
4366
1da177e4
LT
4367/**
4368 * ata_host_remove - Unregister SCSI host structure with upper layers
4369 * @ap: Port to unregister
4370 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4371 *
4372 * LOCKING:
6f0ef4fa 4373 * Inherited from caller.
1da177e4
LT
4374 */
4375
4376static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4377{
4378 struct Scsi_Host *sh = ap->host;
4379
4380 DPRINTK("ENTER\n");
4381
4382 if (do_unregister)
4383 scsi_remove_host(sh);
4384
4385 ap->ops->port_stop(ap);
4386}
4387
4388/**
4389 * ata_host_init - Initialize an ata_port structure
4390 * @ap: Structure to initialize
4391 * @host: associated SCSI mid-layer structure
4392 * @host_set: Collection of hosts to which @ap belongs
4393 * @ent: Probe information provided by low-level driver
4394 * @port_no: Port number associated with this ata_port
4395 *
0cba632b
JG
4396 * Initialize a new ata_port structure, and its associated
4397 * scsi_host.
4398 *
1da177e4 4399 * LOCKING:
0cba632b 4400 * Inherited from caller.
1da177e4
LT
4401 */
4402
4403static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4404 struct ata_host_set *host_set,
057ace5e 4405 const struct ata_probe_ent *ent, unsigned int port_no)
1da177e4
LT
4406{
4407 unsigned int i;
4408
4409 host->max_id = 16;
4410 host->max_lun = 1;
4411 host->max_channel = 1;
4412 host->unique_id = ata_unique_id++;
4413 host->max_cmd_len = 12;
12413197 4414
1da177e4
LT
4415 ap->flags = ATA_FLAG_PORT_DISABLED;
4416 ap->id = host->unique_id;
4417 ap->host = host;
4418 ap->ctl = ATA_DEVCTL_OBS;
4419 ap->host_set = host_set;
4420 ap->port_no = port_no;
4421 ap->hard_port_no =
4422 ent->legacy_mode ? ent->hard_port_no : port_no;
4423 ap->pio_mask = ent->pio_mask;
4424 ap->mwdma_mask = ent->mwdma_mask;
4425 ap->udma_mask = ent->udma_mask;
4426 ap->flags |= ent->host_flags;
4427 ap->ops = ent->port_ops;
4428 ap->cbl = ATA_CBL_NONE;
4429 ap->active_tag = ATA_TAG_POISON;
4430 ap->last_ctl = 0xFF;
4431
4432 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4433 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
a72ec4ce 4434 INIT_LIST_HEAD(&ap->eh_done_q);
1da177e4
LT
4435
4436 for (i = 0; i < ATA_MAX_DEVICES; i++)
4437 ap->device[i].devno = i;
4438
4439#ifdef ATA_IRQ_TRAP
4440 ap->stats.unhandled_irq = 1;
4441 ap->stats.idle_irq = 1;
4442#endif
4443
4444 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4445}
4446
4447/**
4448 * ata_host_add - Attach low-level ATA driver to system
4449 * @ent: Information provided by low-level driver
4450 * @host_set: Collections of ports to which we add
4451 * @port_no: Port number associated with this host
4452 *
0cba632b
JG
4453 * Attach low-level ATA driver to system.
4454 *
1da177e4 4455 * LOCKING:
0cba632b 4456 * PCI/etc. bus probe sem.
1da177e4
LT
4457 *
4458 * RETURNS:
0cba632b 4459 * New ata_port on success, for NULL on error.
1da177e4
LT
4460 */
4461
057ace5e 4462static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
1da177e4
LT
4463 struct ata_host_set *host_set,
4464 unsigned int port_no)
4465{
4466 struct Scsi_Host *host;
4467 struct ata_port *ap;
4468 int rc;
4469
4470 DPRINTK("ENTER\n");
4471 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4472 if (!host)
4473 return NULL;
4474
4475 ap = (struct ata_port *) &host->hostdata[0];
4476
4477 ata_host_init(ap, host, host_set, ent, port_no);
4478
4479 rc = ap->ops->port_start(ap);
4480 if (rc)
4481 goto err_out;
4482
4483 return ap;
4484
4485err_out:
4486 scsi_host_put(host);
4487 return NULL;
4488}
4489
4490/**
0cba632b
JG
4491 * ata_device_add - Register hardware device with ATA and SCSI layers
4492 * @ent: Probe information describing hardware device to be registered
4493 *
4494 * This function processes the information provided in the probe
4495 * information struct @ent, allocates the necessary ATA and SCSI
4496 * host information structures, initializes them, and registers
4497 * everything with requisite kernel subsystems.
4498 *
4499 * This function requests irqs, probes the ATA bus, and probes
4500 * the SCSI bus.
1da177e4
LT
4501 *
4502 * LOCKING:
0cba632b 4503 * PCI/etc. bus probe sem.
1da177e4
LT
4504 *
4505 * RETURNS:
0cba632b 4506 * Number of ports registered. Zero on error (no ports registered).
1da177e4
LT
4507 */
4508
057ace5e 4509int ata_device_add(const struct ata_probe_ent *ent)
1da177e4
LT
4510{
4511 unsigned int count = 0, i;
4512 struct device *dev = ent->dev;
4513 struct ata_host_set *host_set;
4514
4515 DPRINTK("ENTER\n");
4516 /* alloc a container for our list of ATA ports (buses) */
57f3bda8 4517 host_set = kzalloc(sizeof(struct ata_host_set) +
1da177e4
LT
4518 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4519 if (!host_set)
4520 return 0;
1da177e4
LT
4521 spin_lock_init(&host_set->lock);
4522
4523 host_set->dev = dev;
4524 host_set->n_ports = ent->n_ports;
4525 host_set->irq = ent->irq;
4526 host_set->mmio_base = ent->mmio_base;
4527 host_set->private_data = ent->private_data;
4528 host_set->ops = ent->port_ops;
4529
4530 /* register each port bound to this device */
4531 for (i = 0; i < ent->n_ports; i++) {
4532 struct ata_port *ap;
4533 unsigned long xfer_mode_mask;
4534
4535 ap = ata_host_add(ent, host_set, i);
4536 if (!ap)
4537 goto err_out;
4538
4539 host_set->ports[i] = ap;
4540 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4541 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4542 (ap->pio_mask << ATA_SHIFT_PIO);
4543
4544 /* print per-port info to dmesg */
4545 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4546 "bmdma 0x%lX irq %lu\n",
4547 ap->id,
4548 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4549 ata_mode_string(xfer_mode_mask),
4550 ap->ioaddr.cmd_addr,
4551 ap->ioaddr.ctl_addr,
4552 ap->ioaddr.bmdma_addr,
4553 ent->irq);
4554
4555 ata_chk_status(ap);
4556 host_set->ops->irq_clear(ap);
4557 count++;
4558 }
4559
57f3bda8
RD
4560 if (!count)
4561 goto err_free_ret;
1da177e4
LT
4562
4563 /* obtain irq, that is shared between channels */
4564 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4565 DRV_NAME, host_set))
4566 goto err_out;
4567
4568 /* perform each probe synchronously */
4569 DPRINTK("probe begin\n");
4570 for (i = 0; i < count; i++) {
4571 struct ata_port *ap;
4572 int rc;
4573
4574 ap = host_set->ports[i];
4575
4576 DPRINTK("ata%u: probe begin\n", ap->id);
4577 rc = ata_bus_probe(ap);
4578 DPRINTK("ata%u: probe end\n", ap->id);
4579
4580 if (rc) {
4581 /* FIXME: do something useful here?
4582 * Current libata behavior will
4583 * tear down everything when
4584 * the module is removed
4585 * or the h/w is unplugged.
4586 */
4587 }
4588
4589 rc = scsi_add_host(ap->host, dev);
4590 if (rc) {
4591 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4592 ap->id);
4593 /* FIXME: do something useful here */
4594 /* FIXME: handle unconditional calls to
4595 * scsi_scan_host and ata_host_remove, below,
4596 * at the very least
4597 */
4598 }
4599 }
4600
4601 /* probes are done, now scan each port's disk(s) */
4602 DPRINTK("probe begin\n");
4603 for (i = 0; i < count; i++) {
4604 struct ata_port *ap = host_set->ports[i];
4605
644dd0cc 4606 ata_scsi_scan_host(ap);
1da177e4
LT
4607 }
4608
4609 dev_set_drvdata(dev, host_set);
4610
4611 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4612 return ent->n_ports; /* success */
4613
4614err_out:
4615 for (i = 0; i < count; i++) {
4616 ata_host_remove(host_set->ports[i], 1);
4617 scsi_host_put(host_set->ports[i]->host);
4618 }
57f3bda8 4619err_free_ret:
1da177e4
LT
4620 kfree(host_set);
4621 VPRINTK("EXIT, returning 0\n");
4622 return 0;
4623}
4624
17b14451
AC
4625/**
4626 * ata_host_set_remove - PCI layer callback for device removal
4627 * @host_set: ATA host set that was removed
4628 *
4629 * Unregister all objects associated with this host set. Free those
4630 * objects.
4631 *
4632 * LOCKING:
4633 * Inherited from calling layer (may sleep).
4634 */
4635
17b14451
AC
4636void ata_host_set_remove(struct ata_host_set *host_set)
4637{
4638 struct ata_port *ap;
4639 unsigned int i;
4640
4641 for (i = 0; i < host_set->n_ports; i++) {
4642 ap = host_set->ports[i];
4643 scsi_remove_host(ap->host);
4644 }
4645
4646 free_irq(host_set->irq, host_set);
4647
4648 for (i = 0; i < host_set->n_ports; i++) {
4649 ap = host_set->ports[i];
4650
4651 ata_scsi_release(ap->host);
4652
4653 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4654 struct ata_ioports *ioaddr = &ap->ioaddr;
4655
4656 if (ioaddr->cmd_addr == 0x1f0)
4657 release_region(0x1f0, 8);
4658 else if (ioaddr->cmd_addr == 0x170)
4659 release_region(0x170, 8);
4660 }
4661
4662 scsi_host_put(ap->host);
4663 }
4664
4665 if (host_set->ops->host_stop)
4666 host_set->ops->host_stop(host_set);
4667
4668 kfree(host_set);
4669}
4670
1da177e4
LT
4671/**
4672 * ata_scsi_release - SCSI layer callback hook for host unload
4673 * @host: libata host to be unloaded
4674 *
4675 * Performs all duties necessary to shut down a libata port...
4676 * Kill port kthread, disable port, and release resources.
4677 *
4678 * LOCKING:
4679 * Inherited from SCSI layer.
4680 *
4681 * RETURNS:
4682 * One.
4683 */
4684
4685int ata_scsi_release(struct Scsi_Host *host)
4686{
4687 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4688
4689 DPRINTK("ENTER\n");
4690
4691 ap->ops->port_disable(ap);
4692 ata_host_remove(ap, 0);
4693
4694 DPRINTK("EXIT\n");
4695 return 1;
4696}
4697
4698/**
4699 * ata_std_ports - initialize ioaddr with standard port offsets.
4700 * @ioaddr: IO address structure to be initialized
0baab86b
EF
4701 *
4702 * Utility function which initializes data_addr, error_addr,
4703 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4704 * device_addr, status_addr, and command_addr to standard offsets
4705 * relative to cmd_addr.
4706 *
4707 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
1da177e4 4708 */
0baab86b 4709
1da177e4
LT
4710void ata_std_ports(struct ata_ioports *ioaddr)
4711{
4712 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4713 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4714 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4715 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4716 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4717 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4718 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4719 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4720 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4721 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4722}
4723
4724static struct ata_probe_ent *
057ace5e 4725ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
1da177e4
LT
4726{
4727 struct ata_probe_ent *probe_ent;
4728
57f3bda8 4729 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
1da177e4
LT
4730 if (!probe_ent) {
4731 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4732 kobject_name(&(dev->kobj)));
4733 return NULL;
4734 }
4735
1da177e4
LT
4736 INIT_LIST_HEAD(&probe_ent->node);
4737 probe_ent->dev = dev;
4738
4739 probe_ent->sht = port->sht;
4740 probe_ent->host_flags = port->host_flags;
4741 probe_ent->pio_mask = port->pio_mask;
4742 probe_ent->mwdma_mask = port->mwdma_mask;
4743 probe_ent->udma_mask = port->udma_mask;
4744 probe_ent->port_ops = port->port_ops;
4745
4746 return probe_ent;
4747}
4748
0baab86b
EF
4749
4750
374b1873
JG
4751#ifdef CONFIG_PCI
4752
4753void ata_pci_host_stop (struct ata_host_set *host_set)
4754{
4755 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4756
4757 pci_iounmap(pdev, host_set->mmio_base);
4758}
4759
0baab86b
EF
4760/**
4761 * ata_pci_init_native_mode - Initialize native-mode driver
4762 * @pdev: pci device to be initialized
4763 * @port: array[2] of pointers to port info structures.
47a86593 4764 * @ports: bitmap of ports present
0baab86b
EF
4765 *
4766 * Utility function which allocates and initializes an
4767 * ata_probe_ent structure for a standard dual-port
4768 * PIO-based IDE controller. The returned ata_probe_ent
4769 * structure can be passed to ata_device_add(). The returned
4770 * ata_probe_ent structure should then be freed with kfree().
47a86593
AC
4771 *
4772 * The caller need only pass the address of the primary port, the
4773 * secondary will be deduced automatically. If the device has non
4774 * standard secondary port mappings this function can be called twice,
4775 * once for each interface.
0baab86b
EF
4776 */
4777
1da177e4 4778struct ata_probe_ent *
47a86593 4779ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
1da177e4
LT
4780{
4781 struct ata_probe_ent *probe_ent =
4782 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
47a86593
AC
4783 int p = 0;
4784
1da177e4
LT
4785 if (!probe_ent)
4786 return NULL;
4787
1da177e4
LT
4788 probe_ent->irq = pdev->irq;
4789 probe_ent->irq_flags = SA_SHIRQ;
e99f8b5e 4790 probe_ent->private_data = port[0]->private_data;
1da177e4 4791
47a86593
AC
4792 if (ports & ATA_PORT_PRIMARY) {
4793 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4794 probe_ent->port[p].altstatus_addr =
4795 probe_ent->port[p].ctl_addr =
4796 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4797 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4798 ata_std_ports(&probe_ent->port[p]);
4799 p++;
4800 }
1da177e4 4801
47a86593
AC
4802 if (ports & ATA_PORT_SECONDARY) {
4803 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4804 probe_ent->port[p].altstatus_addr =
4805 probe_ent->port[p].ctl_addr =
4806 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4807 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4808 ata_std_ports(&probe_ent->port[p]);
4809 p++;
4810 }
1da177e4 4811
47a86593 4812 probe_ent->n_ports = p;
1da177e4
LT
4813 return probe_ent;
4814}
4815
0f0d5192 4816static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev, struct ata_port_info *port, int port_num)
1da177e4 4817{
47a86593 4818 struct ata_probe_ent *probe_ent;
1da177e4 4819
0f0d5192 4820 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
1da177e4
LT
4821 if (!probe_ent)
4822 return NULL;
1da177e4 4823
1da177e4 4824 probe_ent->legacy_mode = 1;
47a86593
AC
4825 probe_ent->n_ports = 1;
4826 probe_ent->hard_port_no = port_num;
e99f8b5e 4827 probe_ent->private_data = port->private_data;
47a86593
AC
4828
4829 switch(port_num)
4830 {
4831 case 0:
4832 probe_ent->irq = 14;
4833 probe_ent->port[0].cmd_addr = 0x1f0;
4834 probe_ent->port[0].altstatus_addr =
4835 probe_ent->port[0].ctl_addr = 0x3f6;
4836 break;
4837 case 1:
4838 probe_ent->irq = 15;
4839 probe_ent->port[0].cmd_addr = 0x170;
4840 probe_ent->port[0].altstatus_addr =
4841 probe_ent->port[0].ctl_addr = 0x376;
4842 break;
4843 }
4844 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
1da177e4 4845 ata_std_ports(&probe_ent->port[0]);
1da177e4
LT
4846 return probe_ent;
4847}
4848
4849/**
4850 * ata_pci_init_one - Initialize/register PCI IDE host controller
4851 * @pdev: Controller to be initialized
4852 * @port_info: Information from low-level host driver
4853 * @n_ports: Number of ports attached to host controller
4854 *
0baab86b
EF
4855 * This is a helper function which can be called from a driver's
4856 * xxx_init_one() probe function if the hardware uses traditional
4857 * IDE taskfile registers.
4858 *
4859 * This function calls pci_enable_device(), reserves its register
4860 * regions, sets the dma mask, enables bus master mode, and calls
4861 * ata_device_add()
4862 *
1da177e4
LT
4863 * LOCKING:
4864 * Inherited from PCI layer (may sleep).
4865 *
4866 * RETURNS:
0cba632b 4867 * Zero on success, negative on errno-based value on error.
1da177e4
LT
4868 */
4869
4870int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4871 unsigned int n_ports)
4872{
47a86593 4873 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
1da177e4
LT
4874 struct ata_port_info *port[2];
4875 u8 tmp8, mask;
4876 unsigned int legacy_mode = 0;
4877 int disable_dev_on_err = 1;
4878 int rc;
4879
4880 DPRINTK("ENTER\n");
4881
4882 port[0] = port_info[0];
4883 if (n_ports > 1)
4884 port[1] = port_info[1];
4885 else
4886 port[1] = port[0];
4887
4888 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4889 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
47a86593 4890 /* TODO: What if one channel is in native mode ... */
1da177e4
LT
4891 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4892 mask = (1 << 2) | (1 << 0);
4893 if ((tmp8 & mask) != mask)
4894 legacy_mode = (1 << 3);
4895 }
4896
4897 /* FIXME... */
47a86593
AC
4898 if ((!legacy_mode) && (n_ports > 2)) {
4899 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4900 n_ports = 2;
4901 /* For now */
1da177e4
LT
4902 }
4903
47a86593
AC
4904 /* FIXME: Really for ATA it isn't safe because the device may be
4905 multi-purpose and we want to leave it alone if it was already
4906 enabled. Secondly for shared use as Arjan says we want refcounting
4907
4908 Checking dev->is_enabled is insufficient as this is not set at
4909 boot for the primary video which is BIOS enabled
4910 */
4911
1da177e4
LT
4912 rc = pci_enable_device(pdev);
4913 if (rc)
4914 return rc;
4915
4916 rc = pci_request_regions(pdev, DRV_NAME);
4917 if (rc) {
4918 disable_dev_on_err = 0;
4919 goto err_out;
4920 }
4921
47a86593 4922 /* FIXME: Should use platform specific mappers for legacy port ranges */
1da177e4
LT
4923 if (legacy_mode) {
4924 if (!request_region(0x1f0, 8, "libata")) {
4925 struct resource *conflict, res;
4926 res.start = 0x1f0;
4927 res.end = 0x1f0 + 8 - 1;
4928 conflict = ____request_resource(&ioport_resource, &res);
4929 if (!strcmp(conflict->name, "libata"))
4930 legacy_mode |= (1 << 0);
4931 else {
4932 disable_dev_on_err = 0;
4933 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4934 }
4935 } else
4936 legacy_mode |= (1 << 0);
4937
4938 if (!request_region(0x170, 8, "libata")) {
4939 struct resource *conflict, res;
4940 res.start = 0x170;
4941 res.end = 0x170 + 8 - 1;
4942 conflict = ____request_resource(&ioport_resource, &res);
4943 if (!strcmp(conflict->name, "libata"))
4944 legacy_mode |= (1 << 1);
4945 else {
4946 disable_dev_on_err = 0;
4947 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4948 }
4949 } else
4950 legacy_mode |= (1 << 1);
4951 }
4952
4953 /* we have legacy mode, but all ports are unavailable */
4954 if (legacy_mode == (1 << 3)) {
4955 rc = -EBUSY;
4956 goto err_out_regions;
4957 }
4958
4959 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4960 if (rc)
4961 goto err_out_regions;
4962 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4963 if (rc)
4964 goto err_out_regions;
4965
4966 if (legacy_mode) {
47a86593 4967 if (legacy_mode & (1 << 0))
0f0d5192 4968 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
47a86593 4969 if (legacy_mode & (1 << 1))
0f0d5192 4970 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
47a86593
AC
4971 } else {
4972 if (n_ports == 2)
4973 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4974 else
4975 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4976 }
4977 if (!probe_ent && !probe_ent2) {
1da177e4
LT
4978 rc = -ENOMEM;
4979 goto err_out_regions;
4980 }
4981
4982 pci_set_master(pdev);
4983
4984 /* FIXME: check ata_device_add return */
4985 if (legacy_mode) {
4986 if (legacy_mode & (1 << 0))
4987 ata_device_add(probe_ent);
4988 if (legacy_mode & (1 << 1))
4989 ata_device_add(probe_ent2);
4990 } else
4991 ata_device_add(probe_ent);
4992
4993 kfree(probe_ent);
4994 kfree(probe_ent2);
4995
4996 return 0;
4997
4998err_out_regions:
4999 if (legacy_mode & (1 << 0))
5000 release_region(0x1f0, 8);
5001 if (legacy_mode & (1 << 1))
5002 release_region(0x170, 8);
5003 pci_release_regions(pdev);
5004err_out:
5005 if (disable_dev_on_err)
5006 pci_disable_device(pdev);
5007 return rc;
5008}
5009
5010/**
5011 * ata_pci_remove_one - PCI layer callback for device removal
5012 * @pdev: PCI device that was removed
5013 *
5014 * PCI layer indicates to libata via this hook that
6f0ef4fa 5015 * hot-unplug or module unload event has occurred.
1da177e4
LT
5016 * Handle this by unregistering all objects associated
5017 * with this PCI device. Free those objects. Then finally
5018 * release PCI resources and disable device.
5019 *
5020 * LOCKING:
5021 * Inherited from PCI layer (may sleep).
5022 */
5023
5024void ata_pci_remove_one (struct pci_dev *pdev)
5025{
5026 struct device *dev = pci_dev_to_dev(pdev);
5027 struct ata_host_set *host_set = dev_get_drvdata(dev);
1da177e4 5028
17b14451 5029 ata_host_set_remove(host_set);
1da177e4
LT
5030 pci_release_regions(pdev);
5031 pci_disable_device(pdev);
5032 dev_set_drvdata(dev, NULL);
5033}
5034
5035/* move to PCI subsystem */
057ace5e 5036int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
1da177e4
LT
5037{
5038 unsigned long tmp = 0;
5039
5040 switch (bits->width) {
5041 case 1: {
5042 u8 tmp8 = 0;
5043 pci_read_config_byte(pdev, bits->reg, &tmp8);
5044 tmp = tmp8;
5045 break;
5046 }
5047 case 2: {
5048 u16 tmp16 = 0;
5049 pci_read_config_word(pdev, bits->reg, &tmp16);
5050 tmp = tmp16;
5051 break;
5052 }
5053 case 4: {
5054 u32 tmp32 = 0;
5055 pci_read_config_dword(pdev, bits->reg, &tmp32);
5056 tmp = tmp32;
5057 break;
5058 }
5059
5060 default:
5061 return -EINVAL;
5062 }
5063
5064 tmp &= bits->mask;
5065
5066 return (tmp == bits->val) ? 1 : 0;
5067}
9b847548
JA
5068
5069int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5070{
5071 pci_save_state(pdev);
5072 pci_disable_device(pdev);
5073 pci_set_power_state(pdev, PCI_D3hot);
5074 return 0;
5075}
5076
5077int ata_pci_device_resume(struct pci_dev *pdev)
5078{
5079 pci_set_power_state(pdev, PCI_D0);
5080 pci_restore_state(pdev);
5081 pci_enable_device(pdev);
5082 pci_set_master(pdev);
5083 return 0;
5084}
1da177e4
LT
5085#endif /* CONFIG_PCI */
5086
5087
1da177e4
LT
5088static int __init ata_init(void)
5089{
5090 ata_wq = create_workqueue("ata");
5091 if (!ata_wq)
5092 return -ENOMEM;
5093
5094 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5095 return 0;
5096}
5097
5098static void __exit ata_exit(void)
5099{
5100 destroy_workqueue(ata_wq);
5101}
5102
5103module_init(ata_init);
5104module_exit(ata_exit);
5105
67846b30
JG
5106static unsigned long ratelimit_time;
5107static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5108
5109int ata_ratelimit(void)
5110{
5111 int rc;
5112 unsigned long flags;
5113
5114 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5115
5116 if (time_after(jiffies, ratelimit_time)) {
5117 rc = 1;
5118 ratelimit_time = jiffies + (HZ/5);
5119 } else
5120 rc = 0;
5121
5122 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5123
5124 return rc;
5125}
5126
1da177e4
LT
5127/*
5128 * libata is essentially a library of internal helper functions for
5129 * low-level ATA host controller drivers. As such, the API/ABI is
5130 * likely to change as new drivers are added and updated.
5131 * Do not depend on ABI/API stability.
5132 */
5133
5134EXPORT_SYMBOL_GPL(ata_std_bios_param);
5135EXPORT_SYMBOL_GPL(ata_std_ports);
5136EXPORT_SYMBOL_GPL(ata_device_add);
17b14451 5137EXPORT_SYMBOL_GPL(ata_host_set_remove);
1da177e4
LT
5138EXPORT_SYMBOL_GPL(ata_sg_init);
5139EXPORT_SYMBOL_GPL(ata_sg_init_one);
5140EXPORT_SYMBOL_GPL(ata_qc_complete);
5141EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5142EXPORT_SYMBOL_GPL(ata_eng_timeout);
5143EXPORT_SYMBOL_GPL(ata_tf_load);
5144EXPORT_SYMBOL_GPL(ata_tf_read);
5145EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5146EXPORT_SYMBOL_GPL(ata_std_dev_select);
5147EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5148EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5149EXPORT_SYMBOL_GPL(ata_check_status);
5150EXPORT_SYMBOL_GPL(ata_altstatus);
1da177e4
LT
5151EXPORT_SYMBOL_GPL(ata_exec_command);
5152EXPORT_SYMBOL_GPL(ata_port_start);
5153EXPORT_SYMBOL_GPL(ata_port_stop);
aa8f0dc6 5154EXPORT_SYMBOL_GPL(ata_host_stop);
1da177e4
LT
5155EXPORT_SYMBOL_GPL(ata_interrupt);
5156EXPORT_SYMBOL_GPL(ata_qc_prep);
5157EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5158EXPORT_SYMBOL_GPL(ata_bmdma_start);
5159EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5160EXPORT_SYMBOL_GPL(ata_bmdma_status);
5161EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5162EXPORT_SYMBOL_GPL(ata_port_probe);
5163EXPORT_SYMBOL_GPL(sata_phy_reset);
5164EXPORT_SYMBOL_GPL(__sata_phy_reset);
5165EXPORT_SYMBOL_GPL(ata_bus_reset);
5166EXPORT_SYMBOL_GPL(ata_port_disable);
67846b30 5167EXPORT_SYMBOL_GPL(ata_ratelimit);
6f8b9958 5168EXPORT_SYMBOL_GPL(ata_busy_sleep);
1da177e4
LT
5169EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5170EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5171EXPORT_SYMBOL_GPL(ata_scsi_error);
5172EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5173EXPORT_SYMBOL_GPL(ata_scsi_release);
5174EXPORT_SYMBOL_GPL(ata_host_intr);
5175EXPORT_SYMBOL_GPL(ata_dev_classify);
5176EXPORT_SYMBOL_GPL(ata_dev_id_string);
6f2f3812 5177EXPORT_SYMBOL_GPL(ata_dev_config);
1da177e4 5178EXPORT_SYMBOL_GPL(ata_scsi_simulate);
a72ec4ce
TH
5179EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5180EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
1da177e4 5181
1bc4ccff 5182EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
452503f9
AC
5183EXPORT_SYMBOL_GPL(ata_timing_compute);
5184EXPORT_SYMBOL_GPL(ata_timing_merge);
5185
1da177e4
LT
5186#ifdef CONFIG_PCI
5187EXPORT_SYMBOL_GPL(pci_test_config_bits);
374b1873 5188EXPORT_SYMBOL_GPL(ata_pci_host_stop);
1da177e4
LT
5189EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5190EXPORT_SYMBOL_GPL(ata_pci_init_one);
5191EXPORT_SYMBOL_GPL(ata_pci_remove_one);
9b847548
JA
5192EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5193EXPORT_SYMBOL_GPL(ata_pci_device_resume);
1da177e4 5194#endif /* CONFIG_PCI */
9b847548
JA
5195
5196EXPORT_SYMBOL_GPL(ata_device_suspend);
5197EXPORT_SYMBOL_GPL(ata_device_resume);
5198EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5199EXPORT_SYMBOL_GPL(ata_scsi_device_resume);
This page took 0.651773 seconds and 5 git commands to generate.