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