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