libata: implement and use ops inheritance
[deliverable/linux.git] / drivers / ata / 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 * Standards documents from:
34 * http://www.t13.org (ATA standards, PCI DMA IDE spec)
35 * http://www.t10.org (SCSI MMC - for ATAPI MMC)
36 * http://www.sata-io.org (SATA)
37 * http://www.compactflash.org (CF)
38 * http://www.qic.org (QIC157 - Tape and DSC)
39 * http://www.ce-ata.org (CE-ATA: not supported)
40 *
41 */
42
43 #include <linux/kernel.h>
44 #include <linux/module.h>
45 #include <linux/pci.h>
46 #include <linux/init.h>
47 #include <linux/list.h>
48 #include <linux/mm.h>
49 #include <linux/highmem.h>
50 #include <linux/spinlock.h>
51 #include <linux/blkdev.h>
52 #include <linux/delay.h>
53 #include <linux/timer.h>
54 #include <linux/interrupt.h>
55 #include <linux/completion.h>
56 #include <linux/suspend.h>
57 #include <linux/workqueue.h>
58 #include <linux/jiffies.h>
59 #include <linux/scatterlist.h>
60 #include <linux/io.h>
61 #include <scsi/scsi.h>
62 #include <scsi/scsi_cmnd.h>
63 #include <scsi/scsi_host.h>
64 #include <linux/libata.h>
65 #include <asm/semaphore.h>
66 #include <asm/byteorder.h>
67 #include <linux/cdrom.h>
68
69 #include "libata.h"
70
71
72 /* debounce timing parameters in msecs { interval, duration, timeout } */
73 const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
74 const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
75 const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
76
77 const struct ata_port_operations ata_base_port_ops = {
78 .irq_clear = ata_noop_irq_clear,
79 };
80
81 const struct ata_port_operations sata_port_ops = {
82 .inherits = &ata_base_port_ops,
83
84 .qc_defer = ata_std_qc_defer,
85 .dev_select = ata_noop_dev_select,
86 };
87
88 const struct ata_port_operations sata_pmp_port_ops = {
89 .inherits = &sata_port_ops,
90 };
91
92 const struct ata_port_operations ata_sff_port_ops = {
93 .inherits = &ata_base_port_ops,
94
95 .qc_prep = ata_qc_prep,
96 .qc_issue = ata_qc_issue_prot,
97
98 .freeze = ata_bmdma_freeze,
99 .thaw = ata_bmdma_thaw,
100 .error_handler = ata_bmdma_error_handler,
101 .post_internal_cmd = ata_bmdma_post_internal_cmd,
102
103 .dev_select = ata_std_dev_select,
104 .check_status = ata_check_status,
105 .tf_load = ata_tf_load,
106 .tf_read = ata_tf_read,
107 .exec_command = ata_exec_command,
108 .data_xfer = ata_data_xfer,
109 .irq_on = ata_irq_on,
110
111 .port_start = ata_sff_port_start,
112 .irq_handler = ata_interrupt,
113 };
114
115 const struct ata_port_operations ata_bmdma_port_ops = {
116 .inherits = &ata_sff_port_ops,
117
118 .mode_filter = ata_pci_default_filter,
119
120 .bmdma_setup = ata_bmdma_setup,
121 .bmdma_start = ata_bmdma_start,
122 .bmdma_stop = ata_bmdma_stop,
123 .bmdma_status = ata_bmdma_status,
124 .irq_clear = ata_bmdma_irq_clear,
125 };
126
127 static unsigned int ata_dev_init_params(struct ata_device *dev,
128 u16 heads, u16 sectors);
129 static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
130 static unsigned int ata_dev_set_feature(struct ata_device *dev,
131 u8 enable, u8 feature);
132 static void ata_dev_xfermask(struct ata_device *dev);
133 static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
134
135 unsigned int ata_print_id = 1;
136 static struct workqueue_struct *ata_wq;
137
138 struct workqueue_struct *ata_aux_wq;
139
140 struct ata_force_param {
141 const char *name;
142 unsigned int cbl;
143 int spd_limit;
144 unsigned long xfer_mask;
145 unsigned int horkage_on;
146 unsigned int horkage_off;
147 };
148
149 struct ata_force_ent {
150 int port;
151 int device;
152 struct ata_force_param param;
153 };
154
155 static struct ata_force_ent *ata_force_tbl;
156 static int ata_force_tbl_size;
157
158 static char ata_force_param_buf[PAGE_SIZE] __initdata;
159 /* param_buf is thrown away after initialization, disallow read */
160 module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
161 MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/kernel-parameters.txt for details)");
162
163 int atapi_enabled = 1;
164 module_param(atapi_enabled, int, 0444);
165 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
166
167 static int atapi_dmadir = 0;
168 module_param(atapi_dmadir, int, 0444);
169 MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
170
171 int atapi_passthru16 = 1;
172 module_param(atapi_passthru16, int, 0444);
173 MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices; on by default (0=off, 1=on)");
174
175 int libata_fua = 0;
176 module_param_named(fua, libata_fua, int, 0444);
177 MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
178
179 static int ata_ignore_hpa;
180 module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
181 MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
182
183 static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
184 module_param_named(dma, libata_dma_mask, int, 0444);
185 MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
186
187 static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
188 module_param(ata_probe_timeout, int, 0444);
189 MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
190
191 int libata_noacpi = 0;
192 module_param_named(noacpi, libata_noacpi, int, 0444);
193 MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in probe/suspend/resume when set");
194
195 int libata_allow_tpm = 0;
196 module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
197 MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands");
198
199 MODULE_AUTHOR("Jeff Garzik");
200 MODULE_DESCRIPTION("Library module for ATA devices");
201 MODULE_LICENSE("GPL");
202 MODULE_VERSION(DRV_VERSION);
203
204
205 /**
206 * ata_force_cbl - force cable type according to libata.force
207 * @ap: ATA port of interest
208 *
209 * Force cable type according to libata.force and whine about it.
210 * The last entry which has matching port number is used, so it
211 * can be specified as part of device force parameters. For
212 * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
213 * same effect.
214 *
215 * LOCKING:
216 * EH context.
217 */
218 void ata_force_cbl(struct ata_port *ap)
219 {
220 int i;
221
222 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
223 const struct ata_force_ent *fe = &ata_force_tbl[i];
224
225 if (fe->port != -1 && fe->port != ap->print_id)
226 continue;
227
228 if (fe->param.cbl == ATA_CBL_NONE)
229 continue;
230
231 ap->cbl = fe->param.cbl;
232 ata_port_printk(ap, KERN_NOTICE,
233 "FORCE: cable set to %s\n", fe->param.name);
234 return;
235 }
236 }
237
238 /**
239 * ata_force_spd_limit - force SATA spd limit according to libata.force
240 * @link: ATA link of interest
241 *
242 * Force SATA spd limit according to libata.force and whine about
243 * it. When only the port part is specified (e.g. 1:), the limit
244 * applies to all links connected to both the host link and all
245 * fan-out ports connected via PMP. If the device part is
246 * specified as 0 (e.g. 1.00:), it specifies the first fan-out
247 * link not the host link. Device number 15 always points to the
248 * host link whether PMP is attached or not.
249 *
250 * LOCKING:
251 * EH context.
252 */
253 static void ata_force_spd_limit(struct ata_link *link)
254 {
255 int linkno, i;
256
257 if (ata_is_host_link(link))
258 linkno = 15;
259 else
260 linkno = link->pmp;
261
262 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
263 const struct ata_force_ent *fe = &ata_force_tbl[i];
264
265 if (fe->port != -1 && fe->port != link->ap->print_id)
266 continue;
267
268 if (fe->device != -1 && fe->device != linkno)
269 continue;
270
271 if (!fe->param.spd_limit)
272 continue;
273
274 link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
275 ata_link_printk(link, KERN_NOTICE,
276 "FORCE: PHY spd limit set to %s\n", fe->param.name);
277 return;
278 }
279 }
280
281 /**
282 * ata_force_xfermask - force xfermask according to libata.force
283 * @dev: ATA device of interest
284 *
285 * Force xfer_mask according to libata.force and whine about it.
286 * For consistency with link selection, device number 15 selects
287 * the first device connected to the host link.
288 *
289 * LOCKING:
290 * EH context.
291 */
292 static void ata_force_xfermask(struct ata_device *dev)
293 {
294 int devno = dev->link->pmp + dev->devno;
295 int alt_devno = devno;
296 int i;
297
298 /* allow n.15 for the first device attached to host port */
299 if (ata_is_host_link(dev->link) && devno == 0)
300 alt_devno = 15;
301
302 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
303 const struct ata_force_ent *fe = &ata_force_tbl[i];
304 unsigned long pio_mask, mwdma_mask, udma_mask;
305
306 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
307 continue;
308
309 if (fe->device != -1 && fe->device != devno &&
310 fe->device != alt_devno)
311 continue;
312
313 if (!fe->param.xfer_mask)
314 continue;
315
316 ata_unpack_xfermask(fe->param.xfer_mask,
317 &pio_mask, &mwdma_mask, &udma_mask);
318 if (udma_mask)
319 dev->udma_mask = udma_mask;
320 else if (mwdma_mask) {
321 dev->udma_mask = 0;
322 dev->mwdma_mask = mwdma_mask;
323 } else {
324 dev->udma_mask = 0;
325 dev->mwdma_mask = 0;
326 dev->pio_mask = pio_mask;
327 }
328
329 ata_dev_printk(dev, KERN_NOTICE,
330 "FORCE: xfer_mask set to %s\n", fe->param.name);
331 return;
332 }
333 }
334
335 /**
336 * ata_force_horkage - force horkage according to libata.force
337 * @dev: ATA device of interest
338 *
339 * Force horkage according to libata.force and whine about it.
340 * For consistency with link selection, device number 15 selects
341 * the first device connected to the host link.
342 *
343 * LOCKING:
344 * EH context.
345 */
346 static void ata_force_horkage(struct ata_device *dev)
347 {
348 int devno = dev->link->pmp + dev->devno;
349 int alt_devno = devno;
350 int i;
351
352 /* allow n.15 for the first device attached to host port */
353 if (ata_is_host_link(dev->link) && devno == 0)
354 alt_devno = 15;
355
356 for (i = 0; i < ata_force_tbl_size; i++) {
357 const struct ata_force_ent *fe = &ata_force_tbl[i];
358
359 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
360 continue;
361
362 if (fe->device != -1 && fe->device != devno &&
363 fe->device != alt_devno)
364 continue;
365
366 if (!(~dev->horkage & fe->param.horkage_on) &&
367 !(dev->horkage & fe->param.horkage_off))
368 continue;
369
370 dev->horkage |= fe->param.horkage_on;
371 dev->horkage &= ~fe->param.horkage_off;
372
373 ata_dev_printk(dev, KERN_NOTICE,
374 "FORCE: horkage modified (%s)\n", fe->param.name);
375 }
376 }
377
378 /**
379 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
380 * @opcode: SCSI opcode
381 *
382 * Determine ATAPI command type from @opcode.
383 *
384 * LOCKING:
385 * None.
386 *
387 * RETURNS:
388 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
389 */
390 int atapi_cmd_type(u8 opcode)
391 {
392 switch (opcode) {
393 case GPCMD_READ_10:
394 case GPCMD_READ_12:
395 return ATAPI_READ;
396
397 case GPCMD_WRITE_10:
398 case GPCMD_WRITE_12:
399 case GPCMD_WRITE_AND_VERIFY_10:
400 return ATAPI_WRITE;
401
402 case GPCMD_READ_CD:
403 case GPCMD_READ_CD_MSF:
404 return ATAPI_READ_CD;
405
406 case ATA_16:
407 case ATA_12:
408 if (atapi_passthru16)
409 return ATAPI_PASS_THRU;
410 /* fall thru */
411 default:
412 return ATAPI_MISC;
413 }
414 }
415
416 /**
417 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
418 * @tf: Taskfile to convert
419 * @pmp: Port multiplier port
420 * @is_cmd: This FIS is for command
421 * @fis: Buffer into which data will output
422 *
423 * Converts a standard ATA taskfile to a Serial ATA
424 * FIS structure (Register - Host to Device).
425 *
426 * LOCKING:
427 * Inherited from caller.
428 */
429 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
430 {
431 fis[0] = 0x27; /* Register - Host to Device FIS */
432 fis[1] = pmp & 0xf; /* Port multiplier number*/
433 if (is_cmd)
434 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
435
436 fis[2] = tf->command;
437 fis[3] = tf->feature;
438
439 fis[4] = tf->lbal;
440 fis[5] = tf->lbam;
441 fis[6] = tf->lbah;
442 fis[7] = tf->device;
443
444 fis[8] = tf->hob_lbal;
445 fis[9] = tf->hob_lbam;
446 fis[10] = tf->hob_lbah;
447 fis[11] = tf->hob_feature;
448
449 fis[12] = tf->nsect;
450 fis[13] = tf->hob_nsect;
451 fis[14] = 0;
452 fis[15] = tf->ctl;
453
454 fis[16] = 0;
455 fis[17] = 0;
456 fis[18] = 0;
457 fis[19] = 0;
458 }
459
460 /**
461 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
462 * @fis: Buffer from which data will be input
463 * @tf: Taskfile to output
464 *
465 * Converts a serial ATA FIS structure to a standard ATA taskfile.
466 *
467 * LOCKING:
468 * Inherited from caller.
469 */
470
471 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
472 {
473 tf->command = fis[2]; /* status */
474 tf->feature = fis[3]; /* error */
475
476 tf->lbal = fis[4];
477 tf->lbam = fis[5];
478 tf->lbah = fis[6];
479 tf->device = fis[7];
480
481 tf->hob_lbal = fis[8];
482 tf->hob_lbam = fis[9];
483 tf->hob_lbah = fis[10];
484
485 tf->nsect = fis[12];
486 tf->hob_nsect = fis[13];
487 }
488
489 static const u8 ata_rw_cmds[] = {
490 /* pio multi */
491 ATA_CMD_READ_MULTI,
492 ATA_CMD_WRITE_MULTI,
493 ATA_CMD_READ_MULTI_EXT,
494 ATA_CMD_WRITE_MULTI_EXT,
495 0,
496 0,
497 0,
498 ATA_CMD_WRITE_MULTI_FUA_EXT,
499 /* pio */
500 ATA_CMD_PIO_READ,
501 ATA_CMD_PIO_WRITE,
502 ATA_CMD_PIO_READ_EXT,
503 ATA_CMD_PIO_WRITE_EXT,
504 0,
505 0,
506 0,
507 0,
508 /* dma */
509 ATA_CMD_READ,
510 ATA_CMD_WRITE,
511 ATA_CMD_READ_EXT,
512 ATA_CMD_WRITE_EXT,
513 0,
514 0,
515 0,
516 ATA_CMD_WRITE_FUA_EXT
517 };
518
519 /**
520 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
521 * @tf: command to examine and configure
522 * @dev: device tf belongs to
523 *
524 * Examine the device configuration and tf->flags to calculate
525 * the proper read/write commands and protocol to use.
526 *
527 * LOCKING:
528 * caller.
529 */
530 static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
531 {
532 u8 cmd;
533
534 int index, fua, lba48, write;
535
536 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
537 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
538 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
539
540 if (dev->flags & ATA_DFLAG_PIO) {
541 tf->protocol = ATA_PROT_PIO;
542 index = dev->multi_count ? 0 : 8;
543 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
544 /* Unable to use DMA due to host limitation */
545 tf->protocol = ATA_PROT_PIO;
546 index = dev->multi_count ? 0 : 8;
547 } else {
548 tf->protocol = ATA_PROT_DMA;
549 index = 16;
550 }
551
552 cmd = ata_rw_cmds[index + fua + lba48 + write];
553 if (cmd) {
554 tf->command = cmd;
555 return 0;
556 }
557 return -1;
558 }
559
560 /**
561 * ata_tf_read_block - Read block address from ATA taskfile
562 * @tf: ATA taskfile of interest
563 * @dev: ATA device @tf belongs to
564 *
565 * LOCKING:
566 * None.
567 *
568 * Read block address from @tf. This function can handle all
569 * three address formats - LBA, LBA48 and CHS. tf->protocol and
570 * flags select the address format to use.
571 *
572 * RETURNS:
573 * Block address read from @tf.
574 */
575 u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
576 {
577 u64 block = 0;
578
579 if (tf->flags & ATA_TFLAG_LBA) {
580 if (tf->flags & ATA_TFLAG_LBA48) {
581 block |= (u64)tf->hob_lbah << 40;
582 block |= (u64)tf->hob_lbam << 32;
583 block |= tf->hob_lbal << 24;
584 } else
585 block |= (tf->device & 0xf) << 24;
586
587 block |= tf->lbah << 16;
588 block |= tf->lbam << 8;
589 block |= tf->lbal;
590 } else {
591 u32 cyl, head, sect;
592
593 cyl = tf->lbam | (tf->lbah << 8);
594 head = tf->device & 0xf;
595 sect = tf->lbal;
596
597 block = (cyl * dev->heads + head) * dev->sectors + sect;
598 }
599
600 return block;
601 }
602
603 /**
604 * ata_build_rw_tf - Build ATA taskfile for given read/write request
605 * @tf: Target ATA taskfile
606 * @dev: ATA device @tf belongs to
607 * @block: Block address
608 * @n_block: Number of blocks
609 * @tf_flags: RW/FUA etc...
610 * @tag: tag
611 *
612 * LOCKING:
613 * None.
614 *
615 * Build ATA taskfile @tf for read/write request described by
616 * @block, @n_block, @tf_flags and @tag on @dev.
617 *
618 * RETURNS:
619 *
620 * 0 on success, -ERANGE if the request is too large for @dev,
621 * -EINVAL if the request is invalid.
622 */
623 int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
624 u64 block, u32 n_block, unsigned int tf_flags,
625 unsigned int tag)
626 {
627 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
628 tf->flags |= tf_flags;
629
630 if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
631 /* yay, NCQ */
632 if (!lba_48_ok(block, n_block))
633 return -ERANGE;
634
635 tf->protocol = ATA_PROT_NCQ;
636 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
637
638 if (tf->flags & ATA_TFLAG_WRITE)
639 tf->command = ATA_CMD_FPDMA_WRITE;
640 else
641 tf->command = ATA_CMD_FPDMA_READ;
642
643 tf->nsect = tag << 3;
644 tf->hob_feature = (n_block >> 8) & 0xff;
645 tf->feature = n_block & 0xff;
646
647 tf->hob_lbah = (block >> 40) & 0xff;
648 tf->hob_lbam = (block >> 32) & 0xff;
649 tf->hob_lbal = (block >> 24) & 0xff;
650 tf->lbah = (block >> 16) & 0xff;
651 tf->lbam = (block >> 8) & 0xff;
652 tf->lbal = block & 0xff;
653
654 tf->device = 1 << 6;
655 if (tf->flags & ATA_TFLAG_FUA)
656 tf->device |= 1 << 7;
657 } else if (dev->flags & ATA_DFLAG_LBA) {
658 tf->flags |= ATA_TFLAG_LBA;
659
660 if (lba_28_ok(block, n_block)) {
661 /* use LBA28 */
662 tf->device |= (block >> 24) & 0xf;
663 } else if (lba_48_ok(block, n_block)) {
664 if (!(dev->flags & ATA_DFLAG_LBA48))
665 return -ERANGE;
666
667 /* use LBA48 */
668 tf->flags |= ATA_TFLAG_LBA48;
669
670 tf->hob_nsect = (n_block >> 8) & 0xff;
671
672 tf->hob_lbah = (block >> 40) & 0xff;
673 tf->hob_lbam = (block >> 32) & 0xff;
674 tf->hob_lbal = (block >> 24) & 0xff;
675 } else
676 /* request too large even for LBA48 */
677 return -ERANGE;
678
679 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
680 return -EINVAL;
681
682 tf->nsect = n_block & 0xff;
683
684 tf->lbah = (block >> 16) & 0xff;
685 tf->lbam = (block >> 8) & 0xff;
686 tf->lbal = block & 0xff;
687
688 tf->device |= ATA_LBA;
689 } else {
690 /* CHS */
691 u32 sect, head, cyl, track;
692
693 /* The request -may- be too large for CHS addressing. */
694 if (!lba_28_ok(block, n_block))
695 return -ERANGE;
696
697 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
698 return -EINVAL;
699
700 /* Convert LBA to CHS */
701 track = (u32)block / dev->sectors;
702 cyl = track / dev->heads;
703 head = track % dev->heads;
704 sect = (u32)block % dev->sectors + 1;
705
706 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
707 (u32)block, track, cyl, head, sect);
708
709 /* Check whether the converted CHS can fit.
710 Cylinder: 0-65535
711 Head: 0-15
712 Sector: 1-255*/
713 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
714 return -ERANGE;
715
716 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
717 tf->lbal = sect;
718 tf->lbam = cyl;
719 tf->lbah = cyl >> 8;
720 tf->device |= head;
721 }
722
723 return 0;
724 }
725
726 /**
727 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
728 * @pio_mask: pio_mask
729 * @mwdma_mask: mwdma_mask
730 * @udma_mask: udma_mask
731 *
732 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
733 * unsigned int xfer_mask.
734 *
735 * LOCKING:
736 * None.
737 *
738 * RETURNS:
739 * Packed xfer_mask.
740 */
741 unsigned long ata_pack_xfermask(unsigned long pio_mask,
742 unsigned long mwdma_mask,
743 unsigned long udma_mask)
744 {
745 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
746 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
747 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
748 }
749
750 /**
751 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
752 * @xfer_mask: xfer_mask to unpack
753 * @pio_mask: resulting pio_mask
754 * @mwdma_mask: resulting mwdma_mask
755 * @udma_mask: resulting udma_mask
756 *
757 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
758 * Any NULL distination masks will be ignored.
759 */
760 void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
761 unsigned long *mwdma_mask, unsigned long *udma_mask)
762 {
763 if (pio_mask)
764 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
765 if (mwdma_mask)
766 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
767 if (udma_mask)
768 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
769 }
770
771 static const struct ata_xfer_ent {
772 int shift, bits;
773 u8 base;
774 } ata_xfer_tbl[] = {
775 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
776 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
777 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
778 { -1, },
779 };
780
781 /**
782 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
783 * @xfer_mask: xfer_mask of interest
784 *
785 * Return matching XFER_* value for @xfer_mask. Only the highest
786 * bit of @xfer_mask is considered.
787 *
788 * LOCKING:
789 * None.
790 *
791 * RETURNS:
792 * Matching XFER_* value, 0xff if no match found.
793 */
794 u8 ata_xfer_mask2mode(unsigned long xfer_mask)
795 {
796 int highbit = fls(xfer_mask) - 1;
797 const struct ata_xfer_ent *ent;
798
799 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
800 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
801 return ent->base + highbit - ent->shift;
802 return 0xff;
803 }
804
805 /**
806 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
807 * @xfer_mode: XFER_* of interest
808 *
809 * Return matching xfer_mask for @xfer_mode.
810 *
811 * LOCKING:
812 * None.
813 *
814 * RETURNS:
815 * Matching xfer_mask, 0 if no match found.
816 */
817 unsigned long ata_xfer_mode2mask(u8 xfer_mode)
818 {
819 const struct ata_xfer_ent *ent;
820
821 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
822 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
823 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
824 & ~((1 << ent->shift) - 1);
825 return 0;
826 }
827
828 /**
829 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
830 * @xfer_mode: XFER_* of interest
831 *
832 * Return matching xfer_shift for @xfer_mode.
833 *
834 * LOCKING:
835 * None.
836 *
837 * RETURNS:
838 * Matching xfer_shift, -1 if no match found.
839 */
840 int ata_xfer_mode2shift(unsigned long xfer_mode)
841 {
842 const struct ata_xfer_ent *ent;
843
844 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
845 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
846 return ent->shift;
847 return -1;
848 }
849
850 /**
851 * ata_mode_string - convert xfer_mask to string
852 * @xfer_mask: mask of bits supported; only highest bit counts.
853 *
854 * Determine string which represents the highest speed
855 * (highest bit in @modemask).
856 *
857 * LOCKING:
858 * None.
859 *
860 * RETURNS:
861 * Constant C string representing highest speed listed in
862 * @mode_mask, or the constant C string "<n/a>".
863 */
864 const char *ata_mode_string(unsigned long xfer_mask)
865 {
866 static const char * const xfer_mode_str[] = {
867 "PIO0",
868 "PIO1",
869 "PIO2",
870 "PIO3",
871 "PIO4",
872 "PIO5",
873 "PIO6",
874 "MWDMA0",
875 "MWDMA1",
876 "MWDMA2",
877 "MWDMA3",
878 "MWDMA4",
879 "UDMA/16",
880 "UDMA/25",
881 "UDMA/33",
882 "UDMA/44",
883 "UDMA/66",
884 "UDMA/100",
885 "UDMA/133",
886 "UDMA7",
887 };
888 int highbit;
889
890 highbit = fls(xfer_mask) - 1;
891 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
892 return xfer_mode_str[highbit];
893 return "<n/a>";
894 }
895
896 static const char *sata_spd_string(unsigned int spd)
897 {
898 static const char * const spd_str[] = {
899 "1.5 Gbps",
900 "3.0 Gbps",
901 };
902
903 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
904 return "<unknown>";
905 return spd_str[spd - 1];
906 }
907
908 void ata_dev_disable(struct ata_device *dev)
909 {
910 if (ata_dev_enabled(dev)) {
911 if (ata_msg_drv(dev->link->ap))
912 ata_dev_printk(dev, KERN_WARNING, "disabled\n");
913 ata_acpi_on_disable(dev);
914 ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 |
915 ATA_DNXFER_QUIET);
916 dev->class++;
917 }
918 }
919
920 static int ata_dev_set_dipm(struct ata_device *dev, enum link_pm policy)
921 {
922 struct ata_link *link = dev->link;
923 struct ata_port *ap = link->ap;
924 u32 scontrol;
925 unsigned int err_mask;
926 int rc;
927
928 /*
929 * disallow DIPM for drivers which haven't set
930 * ATA_FLAG_IPM. This is because when DIPM is enabled,
931 * phy ready will be set in the interrupt status on
932 * state changes, which will cause some drivers to
933 * think there are errors - additionally drivers will
934 * need to disable hot plug.
935 */
936 if (!(ap->flags & ATA_FLAG_IPM) || !ata_dev_enabled(dev)) {
937 ap->pm_policy = NOT_AVAILABLE;
938 return -EINVAL;
939 }
940
941 /*
942 * For DIPM, we will only enable it for the
943 * min_power setting.
944 *
945 * Why? Because Disks are too stupid to know that
946 * If the host rejects a request to go to SLUMBER
947 * they should retry at PARTIAL, and instead it
948 * just would give up. So, for medium_power to
949 * work at all, we need to only allow HIPM.
950 */
951 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
952 if (rc)
953 return rc;
954
955 switch (policy) {
956 case MIN_POWER:
957 /* no restrictions on IPM transitions */
958 scontrol &= ~(0x3 << 8);
959 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
960 if (rc)
961 return rc;
962
963 /* enable DIPM */
964 if (dev->flags & ATA_DFLAG_DIPM)
965 err_mask = ata_dev_set_feature(dev,
966 SETFEATURES_SATA_ENABLE, SATA_DIPM);
967 break;
968 case MEDIUM_POWER:
969 /* allow IPM to PARTIAL */
970 scontrol &= ~(0x1 << 8);
971 scontrol |= (0x2 << 8);
972 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
973 if (rc)
974 return rc;
975
976 /*
977 * we don't have to disable DIPM since IPM flags
978 * disallow transitions to SLUMBER, which effectively
979 * disable DIPM if it does not support PARTIAL
980 */
981 break;
982 case NOT_AVAILABLE:
983 case MAX_PERFORMANCE:
984 /* disable all IPM transitions */
985 scontrol |= (0x3 << 8);
986 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
987 if (rc)
988 return rc;
989
990 /*
991 * we don't have to disable DIPM since IPM flags
992 * disallow all transitions which effectively
993 * disable DIPM anyway.
994 */
995 break;
996 }
997
998 /* FIXME: handle SET FEATURES failure */
999 (void) err_mask;
1000
1001 return 0;
1002 }
1003
1004 /**
1005 * ata_dev_enable_pm - enable SATA interface power management
1006 * @dev: device to enable power management
1007 * @policy: the link power management policy
1008 *
1009 * Enable SATA Interface power management. This will enable
1010 * Device Interface Power Management (DIPM) for min_power
1011 * policy, and then call driver specific callbacks for
1012 * enabling Host Initiated Power management.
1013 *
1014 * Locking: Caller.
1015 * Returns: -EINVAL if IPM is not supported, 0 otherwise.
1016 */
1017 void ata_dev_enable_pm(struct ata_device *dev, enum link_pm policy)
1018 {
1019 int rc = 0;
1020 struct ata_port *ap = dev->link->ap;
1021
1022 /* set HIPM first, then DIPM */
1023 if (ap->ops->enable_pm)
1024 rc = ap->ops->enable_pm(ap, policy);
1025 if (rc)
1026 goto enable_pm_out;
1027 rc = ata_dev_set_dipm(dev, policy);
1028
1029 enable_pm_out:
1030 if (rc)
1031 ap->pm_policy = MAX_PERFORMANCE;
1032 else
1033 ap->pm_policy = policy;
1034 return /* rc */; /* hopefully we can use 'rc' eventually */
1035 }
1036
1037 #ifdef CONFIG_PM
1038 /**
1039 * ata_dev_disable_pm - disable SATA interface power management
1040 * @dev: device to disable power management
1041 *
1042 * Disable SATA Interface power management. This will disable
1043 * Device Interface Power Management (DIPM) without changing
1044 * policy, call driver specific callbacks for disabling Host
1045 * Initiated Power management.
1046 *
1047 * Locking: Caller.
1048 * Returns: void
1049 */
1050 static void ata_dev_disable_pm(struct ata_device *dev)
1051 {
1052 struct ata_port *ap = dev->link->ap;
1053
1054 ata_dev_set_dipm(dev, MAX_PERFORMANCE);
1055 if (ap->ops->disable_pm)
1056 ap->ops->disable_pm(ap);
1057 }
1058 #endif /* CONFIG_PM */
1059
1060 void ata_lpm_schedule(struct ata_port *ap, enum link_pm policy)
1061 {
1062 ap->pm_policy = policy;
1063 ap->link.eh_info.action |= ATA_EH_LPM;
1064 ap->link.eh_info.flags |= ATA_EHI_NO_AUTOPSY;
1065 ata_port_schedule_eh(ap);
1066 }
1067
1068 #ifdef CONFIG_PM
1069 static void ata_lpm_enable(struct ata_host *host)
1070 {
1071 struct ata_link *link;
1072 struct ata_port *ap;
1073 struct ata_device *dev;
1074 int i;
1075
1076 for (i = 0; i < host->n_ports; i++) {
1077 ap = host->ports[i];
1078 ata_port_for_each_link(link, ap) {
1079 ata_link_for_each_dev(dev, link)
1080 ata_dev_disable_pm(dev);
1081 }
1082 }
1083 }
1084
1085 static void ata_lpm_disable(struct ata_host *host)
1086 {
1087 int i;
1088
1089 for (i = 0; i < host->n_ports; i++) {
1090 struct ata_port *ap = host->ports[i];
1091 ata_lpm_schedule(ap, ap->pm_policy);
1092 }
1093 }
1094 #endif /* CONFIG_PM */
1095
1096
1097 /**
1098 * ata_devchk - PATA device presence detection
1099 * @ap: ATA channel to examine
1100 * @device: Device to examine (starting at zero)
1101 *
1102 * This technique was originally described in
1103 * Hale Landis's ATADRVR (www.ata-atapi.com), and
1104 * later found its way into the ATA/ATAPI spec.
1105 *
1106 * Write a pattern to the ATA shadow registers,
1107 * and if a device is present, it will respond by
1108 * correctly storing and echoing back the
1109 * ATA shadow register contents.
1110 *
1111 * LOCKING:
1112 * caller.
1113 */
1114
1115 static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
1116 {
1117 struct ata_ioports *ioaddr = &ap->ioaddr;
1118 u8 nsect, lbal;
1119
1120 ap->ops->dev_select(ap, device);
1121
1122 iowrite8(0x55, ioaddr->nsect_addr);
1123 iowrite8(0xaa, ioaddr->lbal_addr);
1124
1125 iowrite8(0xaa, ioaddr->nsect_addr);
1126 iowrite8(0x55, ioaddr->lbal_addr);
1127
1128 iowrite8(0x55, ioaddr->nsect_addr);
1129 iowrite8(0xaa, ioaddr->lbal_addr);
1130
1131 nsect = ioread8(ioaddr->nsect_addr);
1132 lbal = ioread8(ioaddr->lbal_addr);
1133
1134 if ((nsect == 0x55) && (lbal == 0xaa))
1135 return 1; /* we found a device */
1136
1137 return 0; /* nothing found */
1138 }
1139
1140 /**
1141 * ata_dev_classify - determine device type based on ATA-spec signature
1142 * @tf: ATA taskfile register set for device to be identified
1143 *
1144 * Determine from taskfile register contents whether a device is
1145 * ATA or ATAPI, as per "Signature and persistence" section
1146 * of ATA/PI spec (volume 1, sect 5.14).
1147 *
1148 * LOCKING:
1149 * None.
1150 *
1151 * RETURNS:
1152 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
1153 * %ATA_DEV_UNKNOWN the event of failure.
1154 */
1155 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1156 {
1157 /* Apple's open source Darwin code hints that some devices only
1158 * put a proper signature into the LBA mid/high registers,
1159 * So, we only check those. It's sufficient for uniqueness.
1160 *
1161 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1162 * signatures for ATA and ATAPI devices attached on SerialATA,
1163 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
1164 * spec has never mentioned about using different signatures
1165 * for ATA/ATAPI devices. Then, Serial ATA II: Port
1166 * Multiplier specification began to use 0x69/0x96 to identify
1167 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1168 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1169 * 0x69/0x96 shortly and described them as reserved for
1170 * SerialATA.
1171 *
1172 * We follow the current spec and consider that 0x69/0x96
1173 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
1174 */
1175 if ((tf->lbam == 0) && (tf->lbah == 0)) {
1176 DPRINTK("found ATA device by sig\n");
1177 return ATA_DEV_ATA;
1178 }
1179
1180 if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
1181 DPRINTK("found ATAPI device by sig\n");
1182 return ATA_DEV_ATAPI;
1183 }
1184
1185 if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1186 DPRINTK("found PMP device by sig\n");
1187 return ATA_DEV_PMP;
1188 }
1189
1190 if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
1191 printk(KERN_INFO "ata: SEMB device ignored\n");
1192 return ATA_DEV_SEMB_UNSUP; /* not yet */
1193 }
1194
1195 DPRINTK("unknown device\n");
1196 return ATA_DEV_UNKNOWN;
1197 }
1198
1199 /**
1200 * ata_dev_try_classify - Parse returned ATA device signature
1201 * @dev: ATA device to classify (starting at zero)
1202 * @present: device seems present
1203 * @r_err: Value of error register on completion
1204 *
1205 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
1206 * an ATA/ATAPI-defined set of values is placed in the ATA
1207 * shadow registers, indicating the results of device detection
1208 * and diagnostics.
1209 *
1210 * Select the ATA device, and read the values from the ATA shadow
1211 * registers. Then parse according to the Error register value,
1212 * and the spec-defined values examined by ata_dev_classify().
1213 *
1214 * LOCKING:
1215 * caller.
1216 *
1217 * RETURNS:
1218 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
1219 */
1220 unsigned int ata_dev_try_classify(struct ata_device *dev, int present,
1221 u8 *r_err)
1222 {
1223 struct ata_port *ap = dev->link->ap;
1224 struct ata_taskfile tf;
1225 unsigned int class;
1226 u8 err;
1227
1228 ap->ops->dev_select(ap, dev->devno);
1229
1230 memset(&tf, 0, sizeof(tf));
1231
1232 ap->ops->tf_read(ap, &tf);
1233 err = tf.feature;
1234 if (r_err)
1235 *r_err = err;
1236
1237 /* see if device passed diags: continue and warn later */
1238 if (err == 0)
1239 /* diagnostic fail : do nothing _YET_ */
1240 dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
1241 else if (err == 1)
1242 /* do nothing */ ;
1243 else if ((dev->devno == 0) && (err == 0x81))
1244 /* do nothing */ ;
1245 else
1246 return ATA_DEV_NONE;
1247
1248 /* determine if device is ATA or ATAPI */
1249 class = ata_dev_classify(&tf);
1250
1251 if (class == ATA_DEV_UNKNOWN) {
1252 /* If the device failed diagnostic, it's likely to
1253 * have reported incorrect device signature too.
1254 * Assume ATA device if the device seems present but
1255 * device signature is invalid with diagnostic
1256 * failure.
1257 */
1258 if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
1259 class = ATA_DEV_ATA;
1260 else
1261 class = ATA_DEV_NONE;
1262 } else if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
1263 class = ATA_DEV_NONE;
1264
1265 return class;
1266 }
1267
1268 /**
1269 * ata_id_string - Convert IDENTIFY DEVICE page into string
1270 * @id: IDENTIFY DEVICE results we will examine
1271 * @s: string into which data is output
1272 * @ofs: offset into identify device page
1273 * @len: length of string to return. must be an even number.
1274 *
1275 * The strings in the IDENTIFY DEVICE page are broken up into
1276 * 16-bit chunks. Run through the string, and output each
1277 * 8-bit chunk linearly, regardless of platform.
1278 *
1279 * LOCKING:
1280 * caller.
1281 */
1282
1283 void ata_id_string(const u16 *id, unsigned char *s,
1284 unsigned int ofs, unsigned int len)
1285 {
1286 unsigned int c;
1287
1288 while (len > 0) {
1289 c = id[ofs] >> 8;
1290 *s = c;
1291 s++;
1292
1293 c = id[ofs] & 0xff;
1294 *s = c;
1295 s++;
1296
1297 ofs++;
1298 len -= 2;
1299 }
1300 }
1301
1302 /**
1303 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1304 * @id: IDENTIFY DEVICE results we will examine
1305 * @s: string into which data is output
1306 * @ofs: offset into identify device page
1307 * @len: length of string to return. must be an odd number.
1308 *
1309 * This function is identical to ata_id_string except that it
1310 * trims trailing spaces and terminates the resulting string with
1311 * null. @len must be actual maximum length (even number) + 1.
1312 *
1313 * LOCKING:
1314 * caller.
1315 */
1316 void ata_id_c_string(const u16 *id, unsigned char *s,
1317 unsigned int ofs, unsigned int len)
1318 {
1319 unsigned char *p;
1320
1321 WARN_ON(!(len & 1));
1322
1323 ata_id_string(id, s, ofs, len - 1);
1324
1325 p = s + strnlen(s, len - 1);
1326 while (p > s && p[-1] == ' ')
1327 p--;
1328 *p = '\0';
1329 }
1330
1331 static u64 ata_id_n_sectors(const u16 *id)
1332 {
1333 if (ata_id_has_lba(id)) {
1334 if (ata_id_has_lba48(id))
1335 return ata_id_u64(id, 100);
1336 else
1337 return ata_id_u32(id, 60);
1338 } else {
1339 if (ata_id_current_chs_valid(id))
1340 return ata_id_u32(id, 57);
1341 else
1342 return id[1] * id[3] * id[6];
1343 }
1344 }
1345
1346 static u64 ata_tf_to_lba48(struct ata_taskfile *tf)
1347 {
1348 u64 sectors = 0;
1349
1350 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1351 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1352 sectors |= (tf->hob_lbal & 0xff) << 24;
1353 sectors |= (tf->lbah & 0xff) << 16;
1354 sectors |= (tf->lbam & 0xff) << 8;
1355 sectors |= (tf->lbal & 0xff);
1356
1357 return ++sectors;
1358 }
1359
1360 static u64 ata_tf_to_lba(struct ata_taskfile *tf)
1361 {
1362 u64 sectors = 0;
1363
1364 sectors |= (tf->device & 0x0f) << 24;
1365 sectors |= (tf->lbah & 0xff) << 16;
1366 sectors |= (tf->lbam & 0xff) << 8;
1367 sectors |= (tf->lbal & 0xff);
1368
1369 return ++sectors;
1370 }
1371
1372 /**
1373 * ata_read_native_max_address - Read native max address
1374 * @dev: target device
1375 * @max_sectors: out parameter for the result native max address
1376 *
1377 * Perform an LBA48 or LBA28 native size query upon the device in
1378 * question.
1379 *
1380 * RETURNS:
1381 * 0 on success, -EACCES if command is aborted by the drive.
1382 * -EIO on other errors.
1383 */
1384 static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
1385 {
1386 unsigned int err_mask;
1387 struct ata_taskfile tf;
1388 int lba48 = ata_id_has_lba48(dev->id);
1389
1390 ata_tf_init(dev, &tf);
1391
1392 /* always clear all address registers */
1393 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1394
1395 if (lba48) {
1396 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1397 tf.flags |= ATA_TFLAG_LBA48;
1398 } else
1399 tf.command = ATA_CMD_READ_NATIVE_MAX;
1400
1401 tf.protocol |= ATA_PROT_NODATA;
1402 tf.device |= ATA_LBA;
1403
1404 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1405 if (err_mask) {
1406 ata_dev_printk(dev, KERN_WARNING, "failed to read native "
1407 "max address (err_mask=0x%x)\n", err_mask);
1408 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1409 return -EACCES;
1410 return -EIO;
1411 }
1412
1413 if (lba48)
1414 *max_sectors = ata_tf_to_lba48(&tf);
1415 else
1416 *max_sectors = ata_tf_to_lba(&tf);
1417 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
1418 (*max_sectors)--;
1419 return 0;
1420 }
1421
1422 /**
1423 * ata_set_max_sectors - Set max sectors
1424 * @dev: target device
1425 * @new_sectors: new max sectors value to set for the device
1426 *
1427 * Set max sectors of @dev to @new_sectors.
1428 *
1429 * RETURNS:
1430 * 0 on success, -EACCES if command is aborted or denied (due to
1431 * previous non-volatile SET_MAX) by the drive. -EIO on other
1432 * errors.
1433 */
1434 static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
1435 {
1436 unsigned int err_mask;
1437 struct ata_taskfile tf;
1438 int lba48 = ata_id_has_lba48(dev->id);
1439
1440 new_sectors--;
1441
1442 ata_tf_init(dev, &tf);
1443
1444 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1445
1446 if (lba48) {
1447 tf.command = ATA_CMD_SET_MAX_EXT;
1448 tf.flags |= ATA_TFLAG_LBA48;
1449
1450 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1451 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1452 tf.hob_lbah = (new_sectors >> 40) & 0xff;
1453 } else {
1454 tf.command = ATA_CMD_SET_MAX;
1455
1456 tf.device |= (new_sectors >> 24) & 0xf;
1457 }
1458
1459 tf.protocol |= ATA_PROT_NODATA;
1460 tf.device |= ATA_LBA;
1461
1462 tf.lbal = (new_sectors >> 0) & 0xff;
1463 tf.lbam = (new_sectors >> 8) & 0xff;
1464 tf.lbah = (new_sectors >> 16) & 0xff;
1465
1466 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1467 if (err_mask) {
1468 ata_dev_printk(dev, KERN_WARNING, "failed to set "
1469 "max address (err_mask=0x%x)\n", err_mask);
1470 if (err_mask == AC_ERR_DEV &&
1471 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1472 return -EACCES;
1473 return -EIO;
1474 }
1475
1476 return 0;
1477 }
1478
1479 /**
1480 * ata_hpa_resize - Resize a device with an HPA set
1481 * @dev: Device to resize
1482 *
1483 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1484 * it if required to the full size of the media. The caller must check
1485 * the drive has the HPA feature set enabled.
1486 *
1487 * RETURNS:
1488 * 0 on success, -errno on failure.
1489 */
1490 static int ata_hpa_resize(struct ata_device *dev)
1491 {
1492 struct ata_eh_context *ehc = &dev->link->eh_context;
1493 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
1494 u64 sectors = ata_id_n_sectors(dev->id);
1495 u64 native_sectors;
1496 int rc;
1497
1498 /* do we need to do it? */
1499 if (dev->class != ATA_DEV_ATA ||
1500 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1501 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1502 return 0;
1503
1504 /* read native max address */
1505 rc = ata_read_native_max_address(dev, &native_sectors);
1506 if (rc) {
1507 /* If device aborted the command or HPA isn't going to
1508 * be unlocked, skip HPA resizing.
1509 */
1510 if (rc == -EACCES || !ata_ignore_hpa) {
1511 ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
1512 "broken, skipping HPA handling\n");
1513 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1514
1515 /* we can continue if device aborted the command */
1516 if (rc == -EACCES)
1517 rc = 0;
1518 }
1519
1520 return rc;
1521 }
1522
1523 /* nothing to do? */
1524 if (native_sectors <= sectors || !ata_ignore_hpa) {
1525 if (!print_info || native_sectors == sectors)
1526 return 0;
1527
1528 if (native_sectors > sectors)
1529 ata_dev_printk(dev, KERN_INFO,
1530 "HPA detected: current %llu, native %llu\n",
1531 (unsigned long long)sectors,
1532 (unsigned long long)native_sectors);
1533 else if (native_sectors < sectors)
1534 ata_dev_printk(dev, KERN_WARNING,
1535 "native sectors (%llu) is smaller than "
1536 "sectors (%llu)\n",
1537 (unsigned long long)native_sectors,
1538 (unsigned long long)sectors);
1539 return 0;
1540 }
1541
1542 /* let's unlock HPA */
1543 rc = ata_set_max_sectors(dev, native_sectors);
1544 if (rc == -EACCES) {
1545 /* if device aborted the command, skip HPA resizing */
1546 ata_dev_printk(dev, KERN_WARNING, "device aborted resize "
1547 "(%llu -> %llu), skipping HPA handling\n",
1548 (unsigned long long)sectors,
1549 (unsigned long long)native_sectors);
1550 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1551 return 0;
1552 } else if (rc)
1553 return rc;
1554
1555 /* re-read IDENTIFY data */
1556 rc = ata_dev_reread_id(dev, 0);
1557 if (rc) {
1558 ata_dev_printk(dev, KERN_ERR, "failed to re-read IDENTIFY "
1559 "data after HPA resizing\n");
1560 return rc;
1561 }
1562
1563 if (print_info) {
1564 u64 new_sectors = ata_id_n_sectors(dev->id);
1565 ata_dev_printk(dev, KERN_INFO,
1566 "HPA unlocked: %llu -> %llu, native %llu\n",
1567 (unsigned long long)sectors,
1568 (unsigned long long)new_sectors,
1569 (unsigned long long)native_sectors);
1570 }
1571
1572 return 0;
1573 }
1574
1575 /**
1576 * ata_noop_dev_select - Select device 0/1 on ATA bus
1577 * @ap: ATA channel to manipulate
1578 * @device: ATA device (numbered from zero) to select
1579 *
1580 * This function performs no actual function.
1581 *
1582 * May be used as the dev_select() entry in ata_port_operations.
1583 *
1584 * LOCKING:
1585 * caller.
1586 */
1587 void ata_noop_dev_select(struct ata_port *ap, unsigned int device)
1588 {
1589 }
1590
1591
1592 /**
1593 * ata_std_dev_select - Select device 0/1 on ATA bus
1594 * @ap: ATA channel to manipulate
1595 * @device: ATA device (numbered from zero) to select
1596 *
1597 * Use the method defined in the ATA specification to
1598 * make either device 0, or device 1, active on the
1599 * ATA channel. Works with both PIO and MMIO.
1600 *
1601 * May be used as the dev_select() entry in ata_port_operations.
1602 *
1603 * LOCKING:
1604 * caller.
1605 */
1606
1607 void ata_std_dev_select(struct ata_port *ap, unsigned int device)
1608 {
1609 u8 tmp;
1610
1611 if (device == 0)
1612 tmp = ATA_DEVICE_OBS;
1613 else
1614 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1615
1616 iowrite8(tmp, ap->ioaddr.device_addr);
1617 ata_pause(ap); /* needed; also flushes, for mmio */
1618 }
1619
1620 /**
1621 * ata_dev_select - Select device 0/1 on ATA bus
1622 * @ap: ATA channel to manipulate
1623 * @device: ATA device (numbered from zero) to select
1624 * @wait: non-zero to wait for Status register BSY bit to clear
1625 * @can_sleep: non-zero if context allows sleeping
1626 *
1627 * Use the method defined in the ATA specification to
1628 * make either device 0, or device 1, active on the
1629 * ATA channel.
1630 *
1631 * This is a high-level version of ata_std_dev_select(),
1632 * which additionally provides the services of inserting
1633 * the proper pauses and status polling, where needed.
1634 *
1635 * LOCKING:
1636 * caller.
1637 */
1638
1639 void ata_dev_select(struct ata_port *ap, unsigned int device,
1640 unsigned int wait, unsigned int can_sleep)
1641 {
1642 if (ata_msg_probe(ap))
1643 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, "
1644 "device %u, wait %u\n", device, wait);
1645
1646 if (wait)
1647 ata_wait_idle(ap);
1648
1649 ap->ops->dev_select(ap, device);
1650
1651 if (wait) {
1652 if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
1653 msleep(150);
1654 ata_wait_idle(ap);
1655 }
1656 }
1657
1658 /**
1659 * ata_dump_id - IDENTIFY DEVICE info debugging output
1660 * @id: IDENTIFY DEVICE page to dump
1661 *
1662 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1663 * page.
1664 *
1665 * LOCKING:
1666 * caller.
1667 */
1668
1669 static inline void ata_dump_id(const u16 *id)
1670 {
1671 DPRINTK("49==0x%04x "
1672 "53==0x%04x "
1673 "63==0x%04x "
1674 "64==0x%04x "
1675 "75==0x%04x \n",
1676 id[49],
1677 id[53],
1678 id[63],
1679 id[64],
1680 id[75]);
1681 DPRINTK("80==0x%04x "
1682 "81==0x%04x "
1683 "82==0x%04x "
1684 "83==0x%04x "
1685 "84==0x%04x \n",
1686 id[80],
1687 id[81],
1688 id[82],
1689 id[83],
1690 id[84]);
1691 DPRINTK("88==0x%04x "
1692 "93==0x%04x\n",
1693 id[88],
1694 id[93]);
1695 }
1696
1697 /**
1698 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1699 * @id: IDENTIFY data to compute xfer mask from
1700 *
1701 * Compute the xfermask for this device. This is not as trivial
1702 * as it seems if we must consider early devices correctly.
1703 *
1704 * FIXME: pre IDE drive timing (do we care ?).
1705 *
1706 * LOCKING:
1707 * None.
1708 *
1709 * RETURNS:
1710 * Computed xfermask
1711 */
1712 unsigned long ata_id_xfermask(const u16 *id)
1713 {
1714 unsigned long pio_mask, mwdma_mask, udma_mask;
1715
1716 /* Usual case. Word 53 indicates word 64 is valid */
1717 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1718 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1719 pio_mask <<= 3;
1720 pio_mask |= 0x7;
1721 } else {
1722 /* If word 64 isn't valid then Word 51 high byte holds
1723 * the PIO timing number for the maximum. Turn it into
1724 * a mask.
1725 */
1726 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
1727 if (mode < 5) /* Valid PIO range */
1728 pio_mask = (2 << mode) - 1;
1729 else
1730 pio_mask = 1;
1731
1732 /* But wait.. there's more. Design your standards by
1733 * committee and you too can get a free iordy field to
1734 * process. However its the speeds not the modes that
1735 * are supported... Note drivers using the timing API
1736 * will get this right anyway
1737 */
1738 }
1739
1740 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1741
1742 if (ata_id_is_cfa(id)) {
1743 /*
1744 * Process compact flash extended modes
1745 */
1746 int pio = id[163] & 0x7;
1747 int dma = (id[163] >> 3) & 7;
1748
1749 if (pio)
1750 pio_mask |= (1 << 5);
1751 if (pio > 1)
1752 pio_mask |= (1 << 6);
1753 if (dma)
1754 mwdma_mask |= (1 << 3);
1755 if (dma > 1)
1756 mwdma_mask |= (1 << 4);
1757 }
1758
1759 udma_mask = 0;
1760 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1761 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1762
1763 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1764 }
1765
1766 /**
1767 * ata_pio_queue_task - Queue port_task
1768 * @ap: The ata_port to queue port_task for
1769 * @fn: workqueue function to be scheduled
1770 * @data: data for @fn to use
1771 * @delay: delay time for workqueue function
1772 *
1773 * Schedule @fn(@data) for execution after @delay jiffies using
1774 * port_task. There is one port_task per port and it's the
1775 * user(low level driver)'s responsibility to make sure that only
1776 * one task is active at any given time.
1777 *
1778 * libata core layer takes care of synchronization between
1779 * port_task and EH. ata_pio_queue_task() may be ignored for EH
1780 * synchronization.
1781 *
1782 * LOCKING:
1783 * Inherited from caller.
1784 */
1785 static void ata_pio_queue_task(struct ata_port *ap, void *data,
1786 unsigned long delay)
1787 {
1788 ap->port_task_data = data;
1789
1790 /* may fail if ata_port_flush_task() in progress */
1791 queue_delayed_work(ata_wq, &ap->port_task, delay);
1792 }
1793
1794 /**
1795 * ata_port_flush_task - Flush port_task
1796 * @ap: The ata_port to flush port_task for
1797 *
1798 * After this function completes, port_task is guranteed not to
1799 * be running or scheduled.
1800 *
1801 * LOCKING:
1802 * Kernel thread context (may sleep)
1803 */
1804 void ata_port_flush_task(struct ata_port *ap)
1805 {
1806 DPRINTK("ENTER\n");
1807
1808 cancel_rearming_delayed_work(&ap->port_task);
1809
1810 if (ata_msg_ctl(ap))
1811 ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __func__);
1812 }
1813
1814 static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1815 {
1816 struct completion *waiting = qc->private_data;
1817
1818 complete(waiting);
1819 }
1820
1821 /**
1822 * ata_exec_internal_sg - execute libata internal command
1823 * @dev: Device to which the command is sent
1824 * @tf: Taskfile registers for the command and the result
1825 * @cdb: CDB for packet command
1826 * @dma_dir: Data tranfer direction of the command
1827 * @sgl: sg list for the data buffer of the command
1828 * @n_elem: Number of sg entries
1829 * @timeout: Timeout in msecs (0 for default)
1830 *
1831 * Executes libata internal command with timeout. @tf contains
1832 * command on entry and result on return. Timeout and error
1833 * conditions are reported via return value. No recovery action
1834 * is taken after a command times out. It's caller's duty to
1835 * clean up after timeout.
1836 *
1837 * LOCKING:
1838 * None. Should be called with kernel context, might sleep.
1839 *
1840 * RETURNS:
1841 * Zero on success, AC_ERR_* mask on failure
1842 */
1843 unsigned ata_exec_internal_sg(struct ata_device *dev,
1844 struct ata_taskfile *tf, const u8 *cdb,
1845 int dma_dir, struct scatterlist *sgl,
1846 unsigned int n_elem, unsigned long timeout)
1847 {
1848 struct ata_link *link = dev->link;
1849 struct ata_port *ap = link->ap;
1850 u8 command = tf->command;
1851 struct ata_queued_cmd *qc;
1852 unsigned int tag, preempted_tag;
1853 u32 preempted_sactive, preempted_qc_active;
1854 int preempted_nr_active_links;
1855 DECLARE_COMPLETION_ONSTACK(wait);
1856 unsigned long flags;
1857 unsigned int err_mask;
1858 int rc;
1859
1860 spin_lock_irqsave(ap->lock, flags);
1861
1862 /* no internal command while frozen */
1863 if (ap->pflags & ATA_PFLAG_FROZEN) {
1864 spin_unlock_irqrestore(ap->lock, flags);
1865 return AC_ERR_SYSTEM;
1866 }
1867
1868 /* initialize internal qc */
1869
1870 /* XXX: Tag 0 is used for drivers with legacy EH as some
1871 * drivers choke if any other tag is given. This breaks
1872 * ata_tag_internal() test for those drivers. Don't use new
1873 * EH stuff without converting to it.
1874 */
1875 if (ap->ops->error_handler)
1876 tag = ATA_TAG_INTERNAL;
1877 else
1878 tag = 0;
1879
1880 if (test_and_set_bit(tag, &ap->qc_allocated))
1881 BUG();
1882 qc = __ata_qc_from_tag(ap, tag);
1883
1884 qc->tag = tag;
1885 qc->scsicmd = NULL;
1886 qc->ap = ap;
1887 qc->dev = dev;
1888 ata_qc_reinit(qc);
1889
1890 preempted_tag = link->active_tag;
1891 preempted_sactive = link->sactive;
1892 preempted_qc_active = ap->qc_active;
1893 preempted_nr_active_links = ap->nr_active_links;
1894 link->active_tag = ATA_TAG_POISON;
1895 link->sactive = 0;
1896 ap->qc_active = 0;
1897 ap->nr_active_links = 0;
1898
1899 /* prepare & issue qc */
1900 qc->tf = *tf;
1901 if (cdb)
1902 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1903 qc->flags |= ATA_QCFLAG_RESULT_TF;
1904 qc->dma_dir = dma_dir;
1905 if (dma_dir != DMA_NONE) {
1906 unsigned int i, buflen = 0;
1907 struct scatterlist *sg;
1908
1909 for_each_sg(sgl, sg, n_elem, i)
1910 buflen += sg->length;
1911
1912 ata_sg_init(qc, sgl, n_elem);
1913 qc->nbytes = buflen;
1914 }
1915
1916 qc->private_data = &wait;
1917 qc->complete_fn = ata_qc_complete_internal;
1918
1919 ata_qc_issue(qc);
1920
1921 spin_unlock_irqrestore(ap->lock, flags);
1922
1923 if (!timeout)
1924 timeout = ata_probe_timeout * 1000 / HZ;
1925
1926 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1927
1928 ata_port_flush_task(ap);
1929
1930 if (!rc) {
1931 spin_lock_irqsave(ap->lock, flags);
1932
1933 /* We're racing with irq here. If we lose, the
1934 * following test prevents us from completing the qc
1935 * twice. If we win, the port is frozen and will be
1936 * cleaned up by ->post_internal_cmd().
1937 */
1938 if (qc->flags & ATA_QCFLAG_ACTIVE) {
1939 qc->err_mask |= AC_ERR_TIMEOUT;
1940
1941 if (ap->ops->error_handler)
1942 ata_port_freeze(ap);
1943 else
1944 ata_qc_complete(qc);
1945
1946 if (ata_msg_warn(ap))
1947 ata_dev_printk(dev, KERN_WARNING,
1948 "qc timeout (cmd 0x%x)\n", command);
1949 }
1950
1951 spin_unlock_irqrestore(ap->lock, flags);
1952 }
1953
1954 /* do post_internal_cmd */
1955 if (ap->ops->post_internal_cmd)
1956 ap->ops->post_internal_cmd(qc);
1957
1958 /* perform minimal error analysis */
1959 if (qc->flags & ATA_QCFLAG_FAILED) {
1960 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1961 qc->err_mask |= AC_ERR_DEV;
1962
1963 if (!qc->err_mask)
1964 qc->err_mask |= AC_ERR_OTHER;
1965
1966 if (qc->err_mask & ~AC_ERR_OTHER)
1967 qc->err_mask &= ~AC_ERR_OTHER;
1968 }
1969
1970 /* finish up */
1971 spin_lock_irqsave(ap->lock, flags);
1972
1973 *tf = qc->result_tf;
1974 err_mask = qc->err_mask;
1975
1976 ata_qc_free(qc);
1977 link->active_tag = preempted_tag;
1978 link->sactive = preempted_sactive;
1979 ap->qc_active = preempted_qc_active;
1980 ap->nr_active_links = preempted_nr_active_links;
1981
1982 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1983 * Until those drivers are fixed, we detect the condition
1984 * here, fail the command with AC_ERR_SYSTEM and reenable the
1985 * port.
1986 *
1987 * Note that this doesn't change any behavior as internal
1988 * command failure results in disabling the device in the
1989 * higher layer for LLDDs without new reset/EH callbacks.
1990 *
1991 * Kill the following code as soon as those drivers are fixed.
1992 */
1993 if (ap->flags & ATA_FLAG_DISABLED) {
1994 err_mask |= AC_ERR_SYSTEM;
1995 ata_port_probe(ap);
1996 }
1997
1998 spin_unlock_irqrestore(ap->lock, flags);
1999
2000 return err_mask;
2001 }
2002
2003 /**
2004 * ata_exec_internal - execute libata internal command
2005 * @dev: Device to which the command is sent
2006 * @tf: Taskfile registers for the command and the result
2007 * @cdb: CDB for packet command
2008 * @dma_dir: Data tranfer direction of the command
2009 * @buf: Data buffer of the command
2010 * @buflen: Length of data buffer
2011 * @timeout: Timeout in msecs (0 for default)
2012 *
2013 * Wrapper around ata_exec_internal_sg() which takes simple
2014 * buffer instead of sg list.
2015 *
2016 * LOCKING:
2017 * None. Should be called with kernel context, might sleep.
2018 *
2019 * RETURNS:
2020 * Zero on success, AC_ERR_* mask on failure
2021 */
2022 unsigned ata_exec_internal(struct ata_device *dev,
2023 struct ata_taskfile *tf, const u8 *cdb,
2024 int dma_dir, void *buf, unsigned int buflen,
2025 unsigned long timeout)
2026 {
2027 struct scatterlist *psg = NULL, sg;
2028 unsigned int n_elem = 0;
2029
2030 if (dma_dir != DMA_NONE) {
2031 WARN_ON(!buf);
2032 sg_init_one(&sg, buf, buflen);
2033 psg = &sg;
2034 n_elem++;
2035 }
2036
2037 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
2038 timeout);
2039 }
2040
2041 /**
2042 * ata_do_simple_cmd - execute simple internal command
2043 * @dev: Device to which the command is sent
2044 * @cmd: Opcode to execute
2045 *
2046 * Execute a 'simple' command, that only consists of the opcode
2047 * 'cmd' itself, without filling any other registers
2048 *
2049 * LOCKING:
2050 * Kernel thread context (may sleep).
2051 *
2052 * RETURNS:
2053 * Zero on success, AC_ERR_* mask on failure
2054 */
2055 unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
2056 {
2057 struct ata_taskfile tf;
2058
2059 ata_tf_init(dev, &tf);
2060
2061 tf.command = cmd;
2062 tf.flags |= ATA_TFLAG_DEVICE;
2063 tf.protocol = ATA_PROT_NODATA;
2064
2065 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
2066 }
2067
2068 /**
2069 * ata_pio_need_iordy - check if iordy needed
2070 * @adev: ATA device
2071 *
2072 * Check if the current speed of the device requires IORDY. Used
2073 * by various controllers for chip configuration.
2074 */
2075
2076 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
2077 {
2078 /* Controller doesn't support IORDY. Probably a pointless check
2079 as the caller should know this */
2080 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
2081 return 0;
2082 /* PIO3 and higher it is mandatory */
2083 if (adev->pio_mode > XFER_PIO_2)
2084 return 1;
2085 /* We turn it on when possible */
2086 if (ata_id_has_iordy(adev->id))
2087 return 1;
2088 return 0;
2089 }
2090
2091 /**
2092 * ata_pio_mask_no_iordy - Return the non IORDY mask
2093 * @adev: ATA device
2094 *
2095 * Compute the highest mode possible if we are not using iordy. Return
2096 * -1 if no iordy mode is available.
2097 */
2098
2099 static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
2100 {
2101 /* If we have no drive specific rule, then PIO 2 is non IORDY */
2102 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
2103 u16 pio = adev->id[ATA_ID_EIDE_PIO];
2104 /* Is the speed faster than the drive allows non IORDY ? */
2105 if (pio) {
2106 /* This is cycle times not frequency - watch the logic! */
2107 if (pio > 240) /* PIO2 is 240nS per cycle */
2108 return 3 << ATA_SHIFT_PIO;
2109 return 7 << ATA_SHIFT_PIO;
2110 }
2111 }
2112 return 3 << ATA_SHIFT_PIO;
2113 }
2114
2115 /**
2116 * ata_dev_read_id - Read ID data from the specified device
2117 * @dev: target device
2118 * @p_class: pointer to class of the target device (may be changed)
2119 * @flags: ATA_READID_* flags
2120 * @id: buffer to read IDENTIFY data into
2121 *
2122 * Read ID data from the specified device. ATA_CMD_ID_ATA is
2123 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
2124 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
2125 * for pre-ATA4 drives.
2126 *
2127 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
2128 * now we abort if we hit that case.
2129 *
2130 * LOCKING:
2131 * Kernel thread context (may sleep)
2132 *
2133 * RETURNS:
2134 * 0 on success, -errno otherwise.
2135 */
2136 int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
2137 unsigned int flags, u16 *id)
2138 {
2139 struct ata_port *ap = dev->link->ap;
2140 unsigned int class = *p_class;
2141 struct ata_taskfile tf;
2142 unsigned int err_mask = 0;
2143 const char *reason;
2144 int may_fallback = 1, tried_spinup = 0;
2145 int rc;
2146
2147 if (ata_msg_ctl(ap))
2148 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
2149
2150 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
2151 retry:
2152 ata_tf_init(dev, &tf);
2153
2154 switch (class) {
2155 case ATA_DEV_ATA:
2156 tf.command = ATA_CMD_ID_ATA;
2157 break;
2158 case ATA_DEV_ATAPI:
2159 tf.command = ATA_CMD_ID_ATAPI;
2160 break;
2161 default:
2162 rc = -ENODEV;
2163 reason = "unsupported class";
2164 goto err_out;
2165 }
2166
2167 tf.protocol = ATA_PROT_PIO;
2168
2169 /* Some devices choke if TF registers contain garbage. Make
2170 * sure those are properly initialized.
2171 */
2172 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2173
2174 /* Device presence detection is unreliable on some
2175 * controllers. Always poll IDENTIFY if available.
2176 */
2177 tf.flags |= ATA_TFLAG_POLLING;
2178
2179 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
2180 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
2181 if (err_mask) {
2182 if (err_mask & AC_ERR_NODEV_HINT) {
2183 ata_dev_printk(dev, KERN_DEBUG,
2184 "NODEV after polling detection\n");
2185 return -ENOENT;
2186 }
2187
2188 if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
2189 /* Device or controller might have reported
2190 * the wrong device class. Give a shot at the
2191 * other IDENTIFY if the current one is
2192 * aborted by the device.
2193 */
2194 if (may_fallback) {
2195 may_fallback = 0;
2196
2197 if (class == ATA_DEV_ATA)
2198 class = ATA_DEV_ATAPI;
2199 else
2200 class = ATA_DEV_ATA;
2201 goto retry;
2202 }
2203
2204 /* Control reaches here iff the device aborted
2205 * both flavors of IDENTIFYs which happens
2206 * sometimes with phantom devices.
2207 */
2208 ata_dev_printk(dev, KERN_DEBUG,
2209 "both IDENTIFYs aborted, assuming NODEV\n");
2210 return -ENOENT;
2211 }
2212
2213 rc = -EIO;
2214 reason = "I/O error";
2215 goto err_out;
2216 }
2217
2218 /* Falling back doesn't make sense if ID data was read
2219 * successfully at least once.
2220 */
2221 may_fallback = 0;
2222
2223 swap_buf_le16(id, ATA_ID_WORDS);
2224
2225 /* sanity check */
2226 rc = -EINVAL;
2227 reason = "device reports invalid type";
2228
2229 if (class == ATA_DEV_ATA) {
2230 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
2231 goto err_out;
2232 } else {
2233 if (ata_id_is_ata(id))
2234 goto err_out;
2235 }
2236
2237 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
2238 tried_spinup = 1;
2239 /*
2240 * Drive powered-up in standby mode, and requires a specific
2241 * SET_FEATURES spin-up subcommand before it will accept
2242 * anything other than the original IDENTIFY command.
2243 */
2244 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
2245 if (err_mask && id[2] != 0x738c) {
2246 rc = -EIO;
2247 reason = "SPINUP failed";
2248 goto err_out;
2249 }
2250 /*
2251 * If the drive initially returned incomplete IDENTIFY info,
2252 * we now must reissue the IDENTIFY command.
2253 */
2254 if (id[2] == 0x37c8)
2255 goto retry;
2256 }
2257
2258 if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
2259 /*
2260 * The exact sequence expected by certain pre-ATA4 drives is:
2261 * SRST RESET
2262 * IDENTIFY (optional in early ATA)
2263 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2264 * anything else..
2265 * Some drives were very specific about that exact sequence.
2266 *
2267 * Note that ATA4 says lba is mandatory so the second check
2268 * shoud never trigger.
2269 */
2270 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
2271 err_mask = ata_dev_init_params(dev, id[3], id[6]);
2272 if (err_mask) {
2273 rc = -EIO;
2274 reason = "INIT_DEV_PARAMS failed";
2275 goto err_out;
2276 }
2277
2278 /* current CHS translation info (id[53-58]) might be
2279 * changed. reread the identify device info.
2280 */
2281 flags &= ~ATA_READID_POSTRESET;
2282 goto retry;
2283 }
2284 }
2285
2286 *p_class = class;
2287
2288 return 0;
2289
2290 err_out:
2291 if (ata_msg_warn(ap))
2292 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
2293 "(%s, err_mask=0x%x)\n", reason, err_mask);
2294 return rc;
2295 }
2296
2297 static inline u8 ata_dev_knobble(struct ata_device *dev)
2298 {
2299 struct ata_port *ap = dev->link->ap;
2300 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2301 }
2302
2303 static void ata_dev_config_ncq(struct ata_device *dev,
2304 char *desc, size_t desc_sz)
2305 {
2306 struct ata_port *ap = dev->link->ap;
2307 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2308
2309 if (!ata_id_has_ncq(dev->id)) {
2310 desc[0] = '\0';
2311 return;
2312 }
2313 if (dev->horkage & ATA_HORKAGE_NONCQ) {
2314 snprintf(desc, desc_sz, "NCQ (not used)");
2315 return;
2316 }
2317 if (ap->flags & ATA_FLAG_NCQ) {
2318 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
2319 dev->flags |= ATA_DFLAG_NCQ;
2320 }
2321
2322 if (hdepth >= ddepth)
2323 snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
2324 else
2325 snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
2326 }
2327
2328 /**
2329 * ata_dev_configure - Configure the specified ATA/ATAPI device
2330 * @dev: Target device to configure
2331 *
2332 * Configure @dev according to @dev->id. Generic and low-level
2333 * driver specific fixups are also applied.
2334 *
2335 * LOCKING:
2336 * Kernel thread context (may sleep)
2337 *
2338 * RETURNS:
2339 * 0 on success, -errno otherwise
2340 */
2341 int ata_dev_configure(struct ata_device *dev)
2342 {
2343 struct ata_port *ap = dev->link->ap;
2344 struct ata_eh_context *ehc = &dev->link->eh_context;
2345 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2346 const u16 *id = dev->id;
2347 unsigned long xfer_mask;
2348 char revbuf[7]; /* XYZ-99\0 */
2349 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2350 char modelbuf[ATA_ID_PROD_LEN+1];
2351 int rc;
2352
2353 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
2354 ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
2355 __func__);
2356 return 0;
2357 }
2358
2359 if (ata_msg_probe(ap))
2360 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
2361
2362 /* set horkage */
2363 dev->horkage |= ata_dev_blacklisted(dev);
2364 ata_force_horkage(dev);
2365
2366 /* let ACPI work its magic */
2367 rc = ata_acpi_on_devcfg(dev);
2368 if (rc)
2369 return rc;
2370
2371 /* massage HPA, do it early as it might change IDENTIFY data */
2372 rc = ata_hpa_resize(dev);
2373 if (rc)
2374 return rc;
2375
2376 /* print device capabilities */
2377 if (ata_msg_probe(ap))
2378 ata_dev_printk(dev, KERN_DEBUG,
2379 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2380 "85:%04x 86:%04x 87:%04x 88:%04x\n",
2381 __func__,
2382 id[49], id[82], id[83], id[84],
2383 id[85], id[86], id[87], id[88]);
2384
2385 /* initialize to-be-configured parameters */
2386 dev->flags &= ~ATA_DFLAG_CFG_MASK;
2387 dev->max_sectors = 0;
2388 dev->cdb_len = 0;
2389 dev->n_sectors = 0;
2390 dev->cylinders = 0;
2391 dev->heads = 0;
2392 dev->sectors = 0;
2393
2394 /*
2395 * common ATA, ATAPI feature tests
2396 */
2397
2398 /* find max transfer mode; for printk only */
2399 xfer_mask = ata_id_xfermask(id);
2400
2401 if (ata_msg_probe(ap))
2402 ata_dump_id(id);
2403
2404 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2405 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2406 sizeof(fwrevbuf));
2407
2408 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2409 sizeof(modelbuf));
2410
2411 /* ATA-specific feature tests */
2412 if (dev->class == ATA_DEV_ATA) {
2413 if (ata_id_is_cfa(id)) {
2414 if (id[162] & 1) /* CPRM may make this media unusable */
2415 ata_dev_printk(dev, KERN_WARNING,
2416 "supports DRM functions and may "
2417 "not be fully accessable.\n");
2418 snprintf(revbuf, 7, "CFA");
2419 } else {
2420 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2421 /* Warn the user if the device has TPM extensions */
2422 if (ata_id_has_tpm(id))
2423 ata_dev_printk(dev, KERN_WARNING,
2424 "supports DRM functions and may "
2425 "not be fully accessable.\n");
2426 }
2427
2428 dev->n_sectors = ata_id_n_sectors(id);
2429
2430 if (dev->id[59] & 0x100)
2431 dev->multi_count = dev->id[59] & 0xff;
2432
2433 if (ata_id_has_lba(id)) {
2434 const char *lba_desc;
2435 char ncq_desc[20];
2436
2437 lba_desc = "LBA";
2438 dev->flags |= ATA_DFLAG_LBA;
2439 if (ata_id_has_lba48(id)) {
2440 dev->flags |= ATA_DFLAG_LBA48;
2441 lba_desc = "LBA48";
2442
2443 if (dev->n_sectors >= (1UL << 28) &&
2444 ata_id_has_flush_ext(id))
2445 dev->flags |= ATA_DFLAG_FLUSH_EXT;
2446 }
2447
2448 /* config NCQ */
2449 ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2450
2451 /* print device info to dmesg */
2452 if (ata_msg_drv(ap) && print_info) {
2453 ata_dev_printk(dev, KERN_INFO,
2454 "%s: %s, %s, max %s\n",
2455 revbuf, modelbuf, fwrevbuf,
2456 ata_mode_string(xfer_mask));
2457 ata_dev_printk(dev, KERN_INFO,
2458 "%Lu sectors, multi %u: %s %s\n",
2459 (unsigned long long)dev->n_sectors,
2460 dev->multi_count, lba_desc, ncq_desc);
2461 }
2462 } else {
2463 /* CHS */
2464
2465 /* Default translation */
2466 dev->cylinders = id[1];
2467 dev->heads = id[3];
2468 dev->sectors = id[6];
2469
2470 if (ata_id_current_chs_valid(id)) {
2471 /* Current CHS translation is valid. */
2472 dev->cylinders = id[54];
2473 dev->heads = id[55];
2474 dev->sectors = id[56];
2475 }
2476
2477 /* print device info to dmesg */
2478 if (ata_msg_drv(ap) && print_info) {
2479 ata_dev_printk(dev, KERN_INFO,
2480 "%s: %s, %s, max %s\n",
2481 revbuf, modelbuf, fwrevbuf,
2482 ata_mode_string(xfer_mask));
2483 ata_dev_printk(dev, KERN_INFO,
2484 "%Lu sectors, multi %u, CHS %u/%u/%u\n",
2485 (unsigned long long)dev->n_sectors,
2486 dev->multi_count, dev->cylinders,
2487 dev->heads, dev->sectors);
2488 }
2489 }
2490
2491 dev->cdb_len = 16;
2492 }
2493
2494 /* ATAPI-specific feature tests */
2495 else if (dev->class == ATA_DEV_ATAPI) {
2496 const char *cdb_intr_string = "";
2497 const char *atapi_an_string = "";
2498 const char *dma_dir_string = "";
2499 u32 sntf;
2500
2501 rc = atapi_cdb_len(id);
2502 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2503 if (ata_msg_warn(ap))
2504 ata_dev_printk(dev, KERN_WARNING,
2505 "unsupported CDB len\n");
2506 rc = -EINVAL;
2507 goto err_out_nosup;
2508 }
2509 dev->cdb_len = (unsigned int) rc;
2510
2511 /* Enable ATAPI AN if both the host and device have
2512 * the support. If PMP is attached, SNTF is required
2513 * to enable ATAPI AN to discern between PHY status
2514 * changed notifications and ATAPI ANs.
2515 */
2516 if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2517 (!ap->nr_pmp_links ||
2518 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
2519 unsigned int err_mask;
2520
2521 /* issue SET feature command to turn this on */
2522 err_mask = ata_dev_set_feature(dev,
2523 SETFEATURES_SATA_ENABLE, SATA_AN);
2524 if (err_mask)
2525 ata_dev_printk(dev, KERN_ERR,
2526 "failed to enable ATAPI AN "
2527 "(err_mask=0x%x)\n", err_mask);
2528 else {
2529 dev->flags |= ATA_DFLAG_AN;
2530 atapi_an_string = ", ATAPI AN";
2531 }
2532 }
2533
2534 if (ata_id_cdb_intr(dev->id)) {
2535 dev->flags |= ATA_DFLAG_CDB_INTR;
2536 cdb_intr_string = ", CDB intr";
2537 }
2538
2539 if (atapi_dmadir || atapi_id_dmadir(dev->id)) {
2540 dev->flags |= ATA_DFLAG_DMADIR;
2541 dma_dir_string = ", DMADIR";
2542 }
2543
2544 /* print device info to dmesg */
2545 if (ata_msg_drv(ap) && print_info)
2546 ata_dev_printk(dev, KERN_INFO,
2547 "ATAPI: %s, %s, max %s%s%s%s\n",
2548 modelbuf, fwrevbuf,
2549 ata_mode_string(xfer_mask),
2550 cdb_intr_string, atapi_an_string,
2551 dma_dir_string);
2552 }
2553
2554 /* determine max_sectors */
2555 dev->max_sectors = ATA_MAX_SECTORS;
2556 if (dev->flags & ATA_DFLAG_LBA48)
2557 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2558
2559 if (!(dev->horkage & ATA_HORKAGE_IPM)) {
2560 if (ata_id_has_hipm(dev->id))
2561 dev->flags |= ATA_DFLAG_HIPM;
2562 if (ata_id_has_dipm(dev->id))
2563 dev->flags |= ATA_DFLAG_DIPM;
2564 }
2565
2566 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2567 200 sectors */
2568 if (ata_dev_knobble(dev)) {
2569 if (ata_msg_drv(ap) && print_info)
2570 ata_dev_printk(dev, KERN_INFO,
2571 "applying bridge limits\n");
2572 dev->udma_mask &= ATA_UDMA5;
2573 dev->max_sectors = ATA_MAX_SECTORS;
2574 }
2575
2576 if ((dev->class == ATA_DEV_ATAPI) &&
2577 (atapi_command_packet_set(id) == TYPE_TAPE)) {
2578 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2579 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2580 }
2581
2582 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
2583 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2584 dev->max_sectors);
2585
2586 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_IPM) {
2587 dev->horkage |= ATA_HORKAGE_IPM;
2588
2589 /* reset link pm_policy for this port to no pm */
2590 ap->pm_policy = MAX_PERFORMANCE;
2591 }
2592
2593 if (ap->ops->dev_config)
2594 ap->ops->dev_config(dev);
2595
2596 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2597 /* Let the user know. We don't want to disallow opens for
2598 rescue purposes, or in case the vendor is just a blithering
2599 idiot. Do this after the dev_config call as some controllers
2600 with buggy firmware may want to avoid reporting false device
2601 bugs */
2602
2603 if (print_info) {
2604 ata_dev_printk(dev, KERN_WARNING,
2605 "Drive reports diagnostics failure. This may indicate a drive\n");
2606 ata_dev_printk(dev, KERN_WARNING,
2607 "fault or invalid emulation. Contact drive vendor for information.\n");
2608 }
2609 }
2610
2611 if (ata_msg_probe(ap))
2612 ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n",
2613 __func__, ata_chk_status(ap));
2614 return 0;
2615
2616 err_out_nosup:
2617 if (ata_msg_probe(ap))
2618 ata_dev_printk(dev, KERN_DEBUG,
2619 "%s: EXIT, err\n", __func__);
2620 return rc;
2621 }
2622
2623 /**
2624 * ata_cable_40wire - return 40 wire cable type
2625 * @ap: port
2626 *
2627 * Helper method for drivers which want to hardwire 40 wire cable
2628 * detection.
2629 */
2630
2631 int ata_cable_40wire(struct ata_port *ap)
2632 {
2633 return ATA_CBL_PATA40;
2634 }
2635
2636 /**
2637 * ata_cable_80wire - return 80 wire cable type
2638 * @ap: port
2639 *
2640 * Helper method for drivers which want to hardwire 80 wire cable
2641 * detection.
2642 */
2643
2644 int ata_cable_80wire(struct ata_port *ap)
2645 {
2646 return ATA_CBL_PATA80;
2647 }
2648
2649 /**
2650 * ata_cable_unknown - return unknown PATA cable.
2651 * @ap: port
2652 *
2653 * Helper method for drivers which have no PATA cable detection.
2654 */
2655
2656 int ata_cable_unknown(struct ata_port *ap)
2657 {
2658 return ATA_CBL_PATA_UNK;
2659 }
2660
2661 /**
2662 * ata_cable_ignore - return ignored PATA cable.
2663 * @ap: port
2664 *
2665 * Helper method for drivers which don't use cable type to limit
2666 * transfer mode.
2667 */
2668 int ata_cable_ignore(struct ata_port *ap)
2669 {
2670 return ATA_CBL_PATA_IGN;
2671 }
2672
2673 /**
2674 * ata_cable_sata - return SATA cable type
2675 * @ap: port
2676 *
2677 * Helper method for drivers which have SATA cables
2678 */
2679
2680 int ata_cable_sata(struct ata_port *ap)
2681 {
2682 return ATA_CBL_SATA;
2683 }
2684
2685 /**
2686 * ata_bus_probe - Reset and probe ATA bus
2687 * @ap: Bus to probe
2688 *
2689 * Master ATA bus probing function. Initiates a hardware-dependent
2690 * bus reset, then attempts to identify any devices found on
2691 * the bus.
2692 *
2693 * LOCKING:
2694 * PCI/etc. bus probe sem.
2695 *
2696 * RETURNS:
2697 * Zero on success, negative errno otherwise.
2698 */
2699
2700 int ata_bus_probe(struct ata_port *ap)
2701 {
2702 unsigned int classes[ATA_MAX_DEVICES];
2703 int tries[ATA_MAX_DEVICES];
2704 int rc;
2705 struct ata_device *dev;
2706
2707 ata_port_probe(ap);
2708
2709 ata_link_for_each_dev(dev, &ap->link)
2710 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2711
2712 retry:
2713 ata_link_for_each_dev(dev, &ap->link) {
2714 /* If we issue an SRST then an ATA drive (not ATAPI)
2715 * may change configuration and be in PIO0 timing. If
2716 * we do a hard reset (or are coming from power on)
2717 * this is true for ATA or ATAPI. Until we've set a
2718 * suitable controller mode we should not touch the
2719 * bus as we may be talking too fast.
2720 */
2721 dev->pio_mode = XFER_PIO_0;
2722
2723 /* If the controller has a pio mode setup function
2724 * then use it to set the chipset to rights. Don't
2725 * touch the DMA setup as that will be dealt with when
2726 * configuring devices.
2727 */
2728 if (ap->ops->set_piomode)
2729 ap->ops->set_piomode(ap, dev);
2730 }
2731
2732 /* reset and determine device classes */
2733 ap->ops->phy_reset(ap);
2734
2735 ata_link_for_each_dev(dev, &ap->link) {
2736 if (!(ap->flags & ATA_FLAG_DISABLED) &&
2737 dev->class != ATA_DEV_UNKNOWN)
2738 classes[dev->devno] = dev->class;
2739 else
2740 classes[dev->devno] = ATA_DEV_NONE;
2741
2742 dev->class = ATA_DEV_UNKNOWN;
2743 }
2744
2745 ata_port_probe(ap);
2746
2747 /* read IDENTIFY page and configure devices. We have to do the identify
2748 specific sequence bass-ackwards so that PDIAG- is released by
2749 the slave device */
2750
2751 ata_link_for_each_dev_reverse(dev, &ap->link) {
2752 if (tries[dev->devno])
2753 dev->class = classes[dev->devno];
2754
2755 if (!ata_dev_enabled(dev))
2756 continue;
2757
2758 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2759 dev->id);
2760 if (rc)
2761 goto fail;
2762 }
2763
2764 /* Now ask for the cable type as PDIAG- should have been released */
2765 if (ap->ops->cable_detect)
2766 ap->cbl = ap->ops->cable_detect(ap);
2767
2768 /* We may have SATA bridge glue hiding here irrespective of the
2769 reported cable types and sensed types */
2770 ata_link_for_each_dev(dev, &ap->link) {
2771 if (!ata_dev_enabled(dev))
2772 continue;
2773 /* SATA drives indicate we have a bridge. We don't know which
2774 end of the link the bridge is which is a problem */
2775 if (ata_id_is_sata(dev->id))
2776 ap->cbl = ATA_CBL_SATA;
2777 }
2778
2779 /* After the identify sequence we can now set up the devices. We do
2780 this in the normal order so that the user doesn't get confused */
2781
2782 ata_link_for_each_dev(dev, &ap->link) {
2783 if (!ata_dev_enabled(dev))
2784 continue;
2785
2786 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2787 rc = ata_dev_configure(dev);
2788 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2789 if (rc)
2790 goto fail;
2791 }
2792
2793 /* configure transfer mode */
2794 rc = ata_set_mode(&ap->link, &dev);
2795 if (rc)
2796 goto fail;
2797
2798 ata_link_for_each_dev(dev, &ap->link)
2799 if (ata_dev_enabled(dev))
2800 return 0;
2801
2802 /* no device present, disable port */
2803 ata_port_disable(ap);
2804 return -ENODEV;
2805
2806 fail:
2807 tries[dev->devno]--;
2808
2809 switch (rc) {
2810 case -EINVAL:
2811 /* eeek, something went very wrong, give up */
2812 tries[dev->devno] = 0;
2813 break;
2814
2815 case -ENODEV:
2816 /* give it just one more chance */
2817 tries[dev->devno] = min(tries[dev->devno], 1);
2818 case -EIO:
2819 if (tries[dev->devno] == 1) {
2820 /* This is the last chance, better to slow
2821 * down than lose it.
2822 */
2823 sata_down_spd_limit(&ap->link);
2824 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2825 }
2826 }
2827
2828 if (!tries[dev->devno])
2829 ata_dev_disable(dev);
2830
2831 goto retry;
2832 }
2833
2834 /**
2835 * ata_port_probe - Mark port as enabled
2836 * @ap: Port for which we indicate enablement
2837 *
2838 * Modify @ap data structure such that the system
2839 * thinks that the entire port is enabled.
2840 *
2841 * LOCKING: host lock, or some other form of
2842 * serialization.
2843 */
2844
2845 void ata_port_probe(struct ata_port *ap)
2846 {
2847 ap->flags &= ~ATA_FLAG_DISABLED;
2848 }
2849
2850 /**
2851 * sata_print_link_status - Print SATA link status
2852 * @link: SATA link to printk link status about
2853 *
2854 * This function prints link speed and status of a SATA link.
2855 *
2856 * LOCKING:
2857 * None.
2858 */
2859 void sata_print_link_status(struct ata_link *link)
2860 {
2861 u32 sstatus, scontrol, tmp;
2862
2863 if (sata_scr_read(link, SCR_STATUS, &sstatus))
2864 return;
2865 sata_scr_read(link, SCR_CONTROL, &scontrol);
2866
2867 if (ata_link_online(link)) {
2868 tmp = (sstatus >> 4) & 0xf;
2869 ata_link_printk(link, KERN_INFO,
2870 "SATA link up %s (SStatus %X SControl %X)\n",
2871 sata_spd_string(tmp), sstatus, scontrol);
2872 } else {
2873 ata_link_printk(link, KERN_INFO,
2874 "SATA link down (SStatus %X SControl %X)\n",
2875 sstatus, scontrol);
2876 }
2877 }
2878
2879 /**
2880 * ata_dev_pair - return other device on cable
2881 * @adev: device
2882 *
2883 * Obtain the other device on the same cable, or if none is
2884 * present NULL is returned
2885 */
2886
2887 struct ata_device *ata_dev_pair(struct ata_device *adev)
2888 {
2889 struct ata_link *link = adev->link;
2890 struct ata_device *pair = &link->device[1 - adev->devno];
2891 if (!ata_dev_enabled(pair))
2892 return NULL;
2893 return pair;
2894 }
2895
2896 /**
2897 * ata_port_disable - Disable port.
2898 * @ap: Port to be disabled.
2899 *
2900 * Modify @ap data structure such that the system
2901 * thinks that the entire port is disabled, and should
2902 * never attempt to probe or communicate with devices
2903 * on this port.
2904 *
2905 * LOCKING: host lock, or some other form of
2906 * serialization.
2907 */
2908
2909 void ata_port_disable(struct ata_port *ap)
2910 {
2911 ap->link.device[0].class = ATA_DEV_NONE;
2912 ap->link.device[1].class = ATA_DEV_NONE;
2913 ap->flags |= ATA_FLAG_DISABLED;
2914 }
2915
2916 /**
2917 * sata_down_spd_limit - adjust SATA spd limit downward
2918 * @link: Link to adjust SATA spd limit for
2919 *
2920 * Adjust SATA spd limit of @link downward. Note that this
2921 * function only adjusts the limit. The change must be applied
2922 * using sata_set_spd().
2923 *
2924 * LOCKING:
2925 * Inherited from caller.
2926 *
2927 * RETURNS:
2928 * 0 on success, negative errno on failure
2929 */
2930 int sata_down_spd_limit(struct ata_link *link)
2931 {
2932 u32 sstatus, spd, mask;
2933 int rc, highbit;
2934
2935 if (!sata_scr_valid(link))
2936 return -EOPNOTSUPP;
2937
2938 /* If SCR can be read, use it to determine the current SPD.
2939 * If not, use cached value in link->sata_spd.
2940 */
2941 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
2942 if (rc == 0)
2943 spd = (sstatus >> 4) & 0xf;
2944 else
2945 spd = link->sata_spd;
2946
2947 mask = link->sata_spd_limit;
2948 if (mask <= 1)
2949 return -EINVAL;
2950
2951 /* unconditionally mask off the highest bit */
2952 highbit = fls(mask) - 1;
2953 mask &= ~(1 << highbit);
2954
2955 /* Mask off all speeds higher than or equal to the current
2956 * one. Force 1.5Gbps if current SPD is not available.
2957 */
2958 if (spd > 1)
2959 mask &= (1 << (spd - 1)) - 1;
2960 else
2961 mask &= 1;
2962
2963 /* were we already at the bottom? */
2964 if (!mask)
2965 return -EINVAL;
2966
2967 link->sata_spd_limit = mask;
2968
2969 ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
2970 sata_spd_string(fls(mask)));
2971
2972 return 0;
2973 }
2974
2975 static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
2976 {
2977 struct ata_link *host_link = &link->ap->link;
2978 u32 limit, target, spd;
2979
2980 limit = link->sata_spd_limit;
2981
2982 /* Don't configure downstream link faster than upstream link.
2983 * It doesn't speed up anything and some PMPs choke on such
2984 * configuration.
2985 */
2986 if (!ata_is_host_link(link) && host_link->sata_spd)
2987 limit &= (1 << host_link->sata_spd) - 1;
2988
2989 if (limit == UINT_MAX)
2990 target = 0;
2991 else
2992 target = fls(limit);
2993
2994 spd = (*scontrol >> 4) & 0xf;
2995 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
2996
2997 return spd != target;
2998 }
2999
3000 /**
3001 * sata_set_spd_needed - is SATA spd configuration needed
3002 * @link: Link in question
3003 *
3004 * Test whether the spd limit in SControl matches
3005 * @link->sata_spd_limit. This function is used to determine
3006 * whether hardreset is necessary to apply SATA spd
3007 * configuration.
3008 *
3009 * LOCKING:
3010 * Inherited from caller.
3011 *
3012 * RETURNS:
3013 * 1 if SATA spd configuration is needed, 0 otherwise.
3014 */
3015 int sata_set_spd_needed(struct ata_link *link)
3016 {
3017 u32 scontrol;
3018
3019 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
3020 return 1;
3021
3022 return __sata_set_spd_needed(link, &scontrol);
3023 }
3024
3025 /**
3026 * sata_set_spd - set SATA spd according to spd limit
3027 * @link: Link to set SATA spd for
3028 *
3029 * Set SATA spd of @link according to sata_spd_limit.
3030 *
3031 * LOCKING:
3032 * Inherited from caller.
3033 *
3034 * RETURNS:
3035 * 0 if spd doesn't need to be changed, 1 if spd has been
3036 * changed. Negative errno if SCR registers are inaccessible.
3037 */
3038 int sata_set_spd(struct ata_link *link)
3039 {
3040 u32 scontrol;
3041 int rc;
3042
3043 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3044 return rc;
3045
3046 if (!__sata_set_spd_needed(link, &scontrol))
3047 return 0;
3048
3049 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3050 return rc;
3051
3052 return 1;
3053 }
3054
3055 /*
3056 * This mode timing computation functionality is ported over from
3057 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
3058 */
3059 /*
3060 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
3061 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
3062 * for UDMA6, which is currently supported only by Maxtor drives.
3063 *
3064 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
3065 */
3066
3067 static const struct ata_timing ata_timing[] = {
3068 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
3069 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
3070 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
3071 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
3072 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
3073 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
3074 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
3075 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
3076
3077 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
3078 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
3079 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
3080
3081 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
3082 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
3083 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
3084 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
3085 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
3086
3087 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
3088 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
3089 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
3090 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
3091 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
3092 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
3093 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
3094 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
3095
3096 { 0xFF }
3097 };
3098
3099 #define ENOUGH(v, unit) (((v)-1)/(unit)+1)
3100 #define EZ(v, unit) ((v)?ENOUGH(v, unit):0)
3101
3102 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
3103 {
3104 q->setup = EZ(t->setup * 1000, T);
3105 q->act8b = EZ(t->act8b * 1000, T);
3106 q->rec8b = EZ(t->rec8b * 1000, T);
3107 q->cyc8b = EZ(t->cyc8b * 1000, T);
3108 q->active = EZ(t->active * 1000, T);
3109 q->recover = EZ(t->recover * 1000, T);
3110 q->cycle = EZ(t->cycle * 1000, T);
3111 q->udma = EZ(t->udma * 1000, UT);
3112 }
3113
3114 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
3115 struct ata_timing *m, unsigned int what)
3116 {
3117 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
3118 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
3119 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
3120 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
3121 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
3122 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
3123 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
3124 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
3125 }
3126
3127 const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
3128 {
3129 const struct ata_timing *t = ata_timing;
3130
3131 while (xfer_mode > t->mode)
3132 t++;
3133
3134 if (xfer_mode == t->mode)
3135 return t;
3136 return NULL;
3137 }
3138
3139 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
3140 struct ata_timing *t, int T, int UT)
3141 {
3142 const struct ata_timing *s;
3143 struct ata_timing p;
3144
3145 /*
3146 * Find the mode.
3147 */
3148
3149 if (!(s = ata_timing_find_mode(speed)))
3150 return -EINVAL;
3151
3152 memcpy(t, s, sizeof(*s));
3153
3154 /*
3155 * If the drive is an EIDE drive, it can tell us it needs extended
3156 * PIO/MW_DMA cycle timing.
3157 */
3158
3159 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
3160 memset(&p, 0, sizeof(p));
3161 if (speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
3162 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
3163 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
3164 } else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
3165 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
3166 }
3167 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
3168 }
3169
3170 /*
3171 * Convert the timing to bus clock counts.
3172 */
3173
3174 ata_timing_quantize(t, t, T, UT);
3175
3176 /*
3177 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
3178 * S.M.A.R.T * and some other commands. We have to ensure that the
3179 * DMA cycle timing is slower/equal than the fastest PIO timing.
3180 */
3181
3182 if (speed > XFER_PIO_6) {
3183 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3184 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3185 }
3186
3187 /*
3188 * Lengthen active & recovery time so that cycle time is correct.
3189 */
3190
3191 if (t->act8b + t->rec8b < t->cyc8b) {
3192 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3193 t->rec8b = t->cyc8b - t->act8b;
3194 }
3195
3196 if (t->active + t->recover < t->cycle) {
3197 t->active += (t->cycle - (t->active + t->recover)) / 2;
3198 t->recover = t->cycle - t->active;
3199 }
3200
3201 /* In a few cases quantisation may produce enough errors to
3202 leave t->cycle too low for the sum of active and recovery
3203 if so we must correct this */
3204 if (t->active + t->recover > t->cycle)
3205 t->cycle = t->active + t->recover;
3206
3207 return 0;
3208 }
3209
3210 /**
3211 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3212 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3213 * @cycle: cycle duration in ns
3214 *
3215 * Return matching xfer mode for @cycle. The returned mode is of
3216 * the transfer type specified by @xfer_shift. If @cycle is too
3217 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3218 * than the fastest known mode, the fasted mode is returned.
3219 *
3220 * LOCKING:
3221 * None.
3222 *
3223 * RETURNS:
3224 * Matching xfer_mode, 0xff if no match found.
3225 */
3226 u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3227 {
3228 u8 base_mode = 0xff, last_mode = 0xff;
3229 const struct ata_xfer_ent *ent;
3230 const struct ata_timing *t;
3231
3232 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3233 if (ent->shift == xfer_shift)
3234 base_mode = ent->base;
3235
3236 for (t = ata_timing_find_mode(base_mode);
3237 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3238 unsigned short this_cycle;
3239
3240 switch (xfer_shift) {
3241 case ATA_SHIFT_PIO:
3242 case ATA_SHIFT_MWDMA:
3243 this_cycle = t->cycle;
3244 break;
3245 case ATA_SHIFT_UDMA:
3246 this_cycle = t->udma;
3247 break;
3248 default:
3249 return 0xff;
3250 }
3251
3252 if (cycle > this_cycle)
3253 break;
3254
3255 last_mode = t->mode;
3256 }
3257
3258 return last_mode;
3259 }
3260
3261 /**
3262 * ata_down_xfermask_limit - adjust dev xfer masks downward
3263 * @dev: Device to adjust xfer masks
3264 * @sel: ATA_DNXFER_* selector
3265 *
3266 * Adjust xfer masks of @dev downward. Note that this function
3267 * does not apply the change. Invoking ata_set_mode() afterwards
3268 * will apply the limit.
3269 *
3270 * LOCKING:
3271 * Inherited from caller.
3272 *
3273 * RETURNS:
3274 * 0 on success, negative errno on failure
3275 */
3276 int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3277 {
3278 char buf[32];
3279 unsigned long orig_mask, xfer_mask;
3280 unsigned long pio_mask, mwdma_mask, udma_mask;
3281 int quiet, highbit;
3282
3283 quiet = !!(sel & ATA_DNXFER_QUIET);
3284 sel &= ~ATA_DNXFER_QUIET;
3285
3286 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3287 dev->mwdma_mask,
3288 dev->udma_mask);
3289 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3290
3291 switch (sel) {
3292 case ATA_DNXFER_PIO:
3293 highbit = fls(pio_mask) - 1;
3294 pio_mask &= ~(1 << highbit);
3295 break;
3296
3297 case ATA_DNXFER_DMA:
3298 if (udma_mask) {
3299 highbit = fls(udma_mask) - 1;
3300 udma_mask &= ~(1 << highbit);
3301 if (!udma_mask)
3302 return -ENOENT;
3303 } else if (mwdma_mask) {
3304 highbit = fls(mwdma_mask) - 1;
3305 mwdma_mask &= ~(1 << highbit);
3306 if (!mwdma_mask)
3307 return -ENOENT;
3308 }
3309 break;
3310
3311 case ATA_DNXFER_40C:
3312 udma_mask &= ATA_UDMA_MASK_40C;
3313 break;
3314
3315 case ATA_DNXFER_FORCE_PIO0:
3316 pio_mask &= 1;
3317 case ATA_DNXFER_FORCE_PIO:
3318 mwdma_mask = 0;
3319 udma_mask = 0;
3320 break;
3321
3322 default:
3323 BUG();
3324 }
3325
3326 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3327
3328 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3329 return -ENOENT;
3330
3331 if (!quiet) {
3332 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3333 snprintf(buf, sizeof(buf), "%s:%s",
3334 ata_mode_string(xfer_mask),
3335 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3336 else
3337 snprintf(buf, sizeof(buf), "%s",
3338 ata_mode_string(xfer_mask));
3339
3340 ata_dev_printk(dev, KERN_WARNING,
3341 "limiting speed to %s\n", buf);
3342 }
3343
3344 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3345 &dev->udma_mask);
3346
3347 return 0;
3348 }
3349
3350 static int ata_dev_set_mode(struct ata_device *dev)
3351 {
3352 struct ata_eh_context *ehc = &dev->link->eh_context;
3353 const char *dev_err_whine = "";
3354 int ign_dev_err = 0;
3355 unsigned int err_mask;
3356 int rc;
3357
3358 dev->flags &= ~ATA_DFLAG_PIO;
3359 if (dev->xfer_shift == ATA_SHIFT_PIO)
3360 dev->flags |= ATA_DFLAG_PIO;
3361
3362 err_mask = ata_dev_set_xfermode(dev);
3363
3364 if (err_mask & ~AC_ERR_DEV)
3365 goto fail;
3366
3367 /* revalidate */
3368 ehc->i.flags |= ATA_EHI_POST_SETMODE;
3369 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3370 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3371 if (rc)
3372 return rc;
3373
3374 /* Old CFA may refuse this command, which is just fine */
3375 if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
3376 ign_dev_err = 1;
3377
3378 /* Some very old devices and some bad newer ones fail any kind of
3379 SET_XFERMODE request but support PIO0-2 timings and no IORDY */
3380 if (dev->xfer_shift == ATA_SHIFT_PIO && !ata_id_has_iordy(dev->id) &&
3381 dev->pio_mode <= XFER_PIO_2)
3382 ign_dev_err = 1;
3383
3384 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3385 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3386 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3387 dev->dma_mode == XFER_MW_DMA_0 &&
3388 (dev->id[63] >> 8) & 1)
3389 ign_dev_err = 1;
3390
3391 /* if the device is actually configured correctly, ignore dev err */
3392 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
3393 ign_dev_err = 1;
3394
3395 if (err_mask & AC_ERR_DEV) {
3396 if (!ign_dev_err)
3397 goto fail;
3398 else
3399 dev_err_whine = " (device error ignored)";
3400 }
3401
3402 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3403 dev->xfer_shift, (int)dev->xfer_mode);
3404
3405 ata_dev_printk(dev, KERN_INFO, "configured for %s%s\n",
3406 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
3407 dev_err_whine);
3408
3409 return 0;
3410
3411 fail:
3412 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
3413 "(err_mask=0x%x)\n", err_mask);
3414 return -EIO;
3415 }
3416
3417 /**
3418 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
3419 * @link: link on which timings will be programmed
3420 * @r_failed_dev: out parameter for failed device
3421 *
3422 * Standard implementation of the function used to tune and set
3423 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3424 * ata_dev_set_mode() fails, pointer to the failing device is
3425 * returned in @r_failed_dev.
3426 *
3427 * LOCKING:
3428 * PCI/etc. bus probe sem.
3429 *
3430 * RETURNS:
3431 * 0 on success, negative errno otherwise
3432 */
3433
3434 int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3435 {
3436 struct ata_port *ap = link->ap;
3437 struct ata_device *dev;
3438 int rc = 0, used_dma = 0, found = 0;
3439
3440 /* step 1: calculate xfer_mask */
3441 ata_link_for_each_dev(dev, link) {
3442 unsigned long pio_mask, dma_mask;
3443 unsigned int mode_mask;
3444
3445 if (!ata_dev_enabled(dev))
3446 continue;
3447
3448 mode_mask = ATA_DMA_MASK_ATA;
3449 if (dev->class == ATA_DEV_ATAPI)
3450 mode_mask = ATA_DMA_MASK_ATAPI;
3451 else if (ata_id_is_cfa(dev->id))
3452 mode_mask = ATA_DMA_MASK_CFA;
3453
3454 ata_dev_xfermask(dev);
3455 ata_force_xfermask(dev);
3456
3457 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3458 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3459
3460 if (libata_dma_mask & mode_mask)
3461 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3462 else
3463 dma_mask = 0;
3464
3465 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3466 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3467
3468 found = 1;
3469 if (dev->dma_mode != 0xff)
3470 used_dma = 1;
3471 }
3472 if (!found)
3473 goto out;
3474
3475 /* step 2: always set host PIO timings */
3476 ata_link_for_each_dev(dev, link) {
3477 if (!ata_dev_enabled(dev))
3478 continue;
3479
3480 if (dev->pio_mode == 0xff) {
3481 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
3482 rc = -EINVAL;
3483 goto out;
3484 }
3485
3486 dev->xfer_mode = dev->pio_mode;
3487 dev->xfer_shift = ATA_SHIFT_PIO;
3488 if (ap->ops->set_piomode)
3489 ap->ops->set_piomode(ap, dev);
3490 }
3491
3492 /* step 3: set host DMA timings */
3493 ata_link_for_each_dev(dev, link) {
3494 if (!ata_dev_enabled(dev) || dev->dma_mode == 0xff)
3495 continue;
3496
3497 dev->xfer_mode = dev->dma_mode;
3498 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3499 if (ap->ops->set_dmamode)
3500 ap->ops->set_dmamode(ap, dev);
3501 }
3502
3503 /* step 4: update devices' xfer mode */
3504 ata_link_for_each_dev(dev, link) {
3505 /* don't update suspended devices' xfer mode */
3506 if (!ata_dev_enabled(dev))
3507 continue;
3508
3509 rc = ata_dev_set_mode(dev);
3510 if (rc)
3511 goto out;
3512 }
3513
3514 /* Record simplex status. If we selected DMA then the other
3515 * host channels are not permitted to do so.
3516 */
3517 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3518 ap->host->simplex_claimed = ap;
3519
3520 out:
3521 if (rc)
3522 *r_failed_dev = dev;
3523 return rc;
3524 }
3525
3526 /**
3527 * ata_tf_to_host - issue ATA taskfile to host controller
3528 * @ap: port to which command is being issued
3529 * @tf: ATA taskfile register set
3530 *
3531 * Issues ATA taskfile register set to ATA host controller,
3532 * with proper synchronization with interrupt handler and
3533 * other threads.
3534 *
3535 * LOCKING:
3536 * spin_lock_irqsave(host lock)
3537 */
3538
3539 static inline void ata_tf_to_host(struct ata_port *ap,
3540 const struct ata_taskfile *tf)
3541 {
3542 ap->ops->tf_load(ap, tf);
3543 ap->ops->exec_command(ap, tf);
3544 }
3545
3546 /**
3547 * ata_busy_sleep - sleep until BSY clears, or timeout
3548 * @ap: port containing status register to be polled
3549 * @tmout_pat: impatience timeout
3550 * @tmout: overall timeout
3551 *
3552 * Sleep until ATA Status register bit BSY clears,
3553 * or a timeout occurs.
3554 *
3555 * LOCKING:
3556 * Kernel thread context (may sleep).
3557 *
3558 * RETURNS:
3559 * 0 on success, -errno otherwise.
3560 */
3561 int ata_busy_sleep(struct ata_port *ap,
3562 unsigned long tmout_pat, unsigned long tmout)
3563 {
3564 unsigned long timer_start, timeout;
3565 u8 status;
3566
3567 status = ata_busy_wait(ap, ATA_BUSY, 300);
3568 timer_start = jiffies;
3569 timeout = timer_start + tmout_pat;
3570 while (status != 0xff && (status & ATA_BUSY) &&
3571 time_before(jiffies, timeout)) {
3572 msleep(50);
3573 status = ata_busy_wait(ap, ATA_BUSY, 3);
3574 }
3575
3576 if (status != 0xff && (status & ATA_BUSY))
3577 ata_port_printk(ap, KERN_WARNING,
3578 "port is slow to respond, please be patient "
3579 "(Status 0x%x)\n", status);
3580
3581 timeout = timer_start + tmout;
3582 while (status != 0xff && (status & ATA_BUSY) &&
3583 time_before(jiffies, timeout)) {
3584 msleep(50);
3585 status = ata_chk_status(ap);
3586 }
3587
3588 if (status == 0xff)
3589 return -ENODEV;
3590
3591 if (status & ATA_BUSY) {
3592 ata_port_printk(ap, KERN_ERR, "port failed to respond "
3593 "(%lu secs, Status 0x%x)\n",
3594 tmout / HZ, status);
3595 return -EBUSY;
3596 }
3597
3598 return 0;
3599 }
3600
3601 /**
3602 * ata_wait_after_reset - wait before checking status after reset
3603 * @ap: port containing status register to be polled
3604 * @deadline: deadline jiffies for the operation
3605 *
3606 * After reset, we need to pause a while before reading status.
3607 * Also, certain combination of controller and device report 0xff
3608 * for some duration (e.g. until SATA PHY is up and running)
3609 * which is interpreted as empty port in ATA world. This
3610 * function also waits for such devices to get out of 0xff
3611 * status.
3612 *
3613 * LOCKING:
3614 * Kernel thread context (may sleep).
3615 */
3616 void ata_wait_after_reset(struct ata_port *ap, unsigned long deadline)
3617 {
3618 unsigned long until = jiffies + ATA_TMOUT_FF_WAIT;
3619
3620 if (time_before(until, deadline))
3621 deadline = until;
3622
3623 /* Spec mandates ">= 2ms" before checking status. We wait
3624 * 150ms, because that was the magic delay used for ATAPI
3625 * devices in Hale Landis's ATADRVR, for the period of time
3626 * between when the ATA command register is written, and then
3627 * status is checked. Because waiting for "a while" before
3628 * checking status is fine, post SRST, we perform this magic
3629 * delay here as well.
3630 *
3631 * Old drivers/ide uses the 2mS rule and then waits for ready.
3632 */
3633 msleep(150);
3634
3635 /* Wait for 0xff to clear. Some SATA devices take a long time
3636 * to clear 0xff after reset. For example, HHD424020F7SV00
3637 * iVDR needs >= 800ms while. Quantum GoVault needs even more
3638 * than that.
3639 *
3640 * Note that some PATA controllers (pata_ali) explode if
3641 * status register is read more than once when there's no
3642 * device attached.
3643 */
3644 if (ap->flags & ATA_FLAG_SATA) {
3645 while (1) {
3646 u8 status = ata_chk_status(ap);
3647
3648 if (status != 0xff || time_after(jiffies, deadline))
3649 return;
3650
3651 msleep(50);
3652 }
3653 }
3654 }
3655
3656 /**
3657 * ata_wait_ready - sleep until BSY clears, or timeout
3658 * @ap: port containing status register to be polled
3659 * @deadline: deadline jiffies for the operation
3660 *
3661 * Sleep until ATA Status register bit BSY clears, or timeout
3662 * occurs.
3663 *
3664 * LOCKING:
3665 * Kernel thread context (may sleep).
3666 *
3667 * RETURNS:
3668 * 0 on success, -errno otherwise.
3669 */
3670 int ata_wait_ready(struct ata_port *ap, unsigned long deadline)
3671 {
3672 unsigned long start = jiffies;
3673 int warned = 0;
3674
3675 while (1) {
3676 u8 status = ata_chk_status(ap);
3677 unsigned long now = jiffies;
3678
3679 if (!(status & ATA_BUSY))
3680 return 0;
3681 if (!ata_link_online(&ap->link) && status == 0xff)
3682 return -ENODEV;
3683 if (time_after(now, deadline))
3684 return -EBUSY;
3685
3686 if (!warned && time_after(now, start + 5 * HZ) &&
3687 (deadline - now > 3 * HZ)) {
3688 ata_port_printk(ap, KERN_WARNING,
3689 "port is slow to respond, please be patient "
3690 "(Status 0x%x)\n", status);
3691 warned = 1;
3692 }
3693
3694 msleep(50);
3695 }
3696 }
3697
3698 static int ata_bus_post_reset(struct ata_port *ap, unsigned int devmask,
3699 unsigned long deadline)
3700 {
3701 struct ata_ioports *ioaddr = &ap->ioaddr;
3702 unsigned int dev0 = devmask & (1 << 0);
3703 unsigned int dev1 = devmask & (1 << 1);
3704 int rc, ret = 0;
3705
3706 /* if device 0 was found in ata_devchk, wait for its
3707 * BSY bit to clear
3708 */
3709 if (dev0) {
3710 rc = ata_wait_ready(ap, deadline);
3711 if (rc) {
3712 if (rc != -ENODEV)
3713 return rc;
3714 ret = rc;
3715 }
3716 }
3717
3718 /* if device 1 was found in ata_devchk, wait for register
3719 * access briefly, then wait for BSY to clear.
3720 */
3721 if (dev1) {
3722 int i;
3723
3724 ap->ops->dev_select(ap, 1);
3725
3726 /* Wait for register access. Some ATAPI devices fail
3727 * to set nsect/lbal after reset, so don't waste too
3728 * much time on it. We're gonna wait for !BSY anyway.
3729 */
3730 for (i = 0; i < 2; i++) {
3731 u8 nsect, lbal;
3732
3733 nsect = ioread8(ioaddr->nsect_addr);
3734 lbal = ioread8(ioaddr->lbal_addr);
3735 if ((nsect == 1) && (lbal == 1))
3736 break;
3737 msleep(50); /* give drive a breather */
3738 }
3739
3740 rc = ata_wait_ready(ap, deadline);
3741 if (rc) {
3742 if (rc != -ENODEV)
3743 return rc;
3744 ret = rc;
3745 }
3746 }
3747
3748 /* is all this really necessary? */
3749 ap->ops->dev_select(ap, 0);
3750 if (dev1)
3751 ap->ops->dev_select(ap, 1);
3752 if (dev0)
3753 ap->ops->dev_select(ap, 0);
3754
3755 return ret;
3756 }
3757
3758 static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
3759 unsigned long deadline)
3760 {
3761 struct ata_ioports *ioaddr = &ap->ioaddr;
3762
3763 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
3764
3765 /* software reset. causes dev0 to be selected */
3766 iowrite8(ap->ctl, ioaddr->ctl_addr);
3767 udelay(20); /* FIXME: flush */
3768 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
3769 udelay(20); /* FIXME: flush */
3770 iowrite8(ap->ctl, ioaddr->ctl_addr);
3771
3772 /* wait a while before checking status */
3773 ata_wait_after_reset(ap, deadline);
3774
3775 /* Before we perform post reset processing we want to see if
3776 * the bus shows 0xFF because the odd clown forgets the D7
3777 * pulldown resistor.
3778 */
3779 if (ata_chk_status(ap) == 0xFF)
3780 return -ENODEV;
3781
3782 return ata_bus_post_reset(ap, devmask, deadline);
3783 }
3784
3785 /**
3786 * ata_bus_reset - reset host port and associated ATA channel
3787 * @ap: port to reset
3788 *
3789 * This is typically the first time we actually start issuing
3790 * commands to the ATA channel. We wait for BSY to clear, then
3791 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
3792 * result. Determine what devices, if any, are on the channel
3793 * by looking at the device 0/1 error register. Look at the signature
3794 * stored in each device's taskfile registers, to determine if
3795 * the device is ATA or ATAPI.
3796 *
3797 * LOCKING:
3798 * PCI/etc. bus probe sem.
3799 * Obtains host lock.
3800 *
3801 * SIDE EFFECTS:
3802 * Sets ATA_FLAG_DISABLED if bus reset fails.
3803 */
3804
3805 void ata_bus_reset(struct ata_port *ap)
3806 {
3807 struct ata_device *device = ap->link.device;
3808 struct ata_ioports *ioaddr = &ap->ioaddr;
3809 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
3810 u8 err;
3811 unsigned int dev0, dev1 = 0, devmask = 0;
3812 int rc;
3813
3814 DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
3815
3816 /* determine if device 0/1 are present */
3817 if (ap->flags & ATA_FLAG_SATA_RESET)
3818 dev0 = 1;
3819 else {
3820 dev0 = ata_devchk(ap, 0);
3821 if (slave_possible)
3822 dev1 = ata_devchk(ap, 1);
3823 }
3824
3825 if (dev0)
3826 devmask |= (1 << 0);
3827 if (dev1)
3828 devmask |= (1 << 1);
3829
3830 /* select device 0 again */
3831 ap->ops->dev_select(ap, 0);
3832
3833 /* issue bus reset */
3834 if (ap->flags & ATA_FLAG_SRST) {
3835 rc = ata_bus_softreset(ap, devmask, jiffies + 40 * HZ);
3836 if (rc && rc != -ENODEV)
3837 goto err_out;
3838 }
3839
3840 /*
3841 * determine by signature whether we have ATA or ATAPI devices
3842 */
3843 device[0].class = ata_dev_try_classify(&device[0], dev0, &err);
3844 if ((slave_possible) && (err != 0x81))
3845 device[1].class = ata_dev_try_classify(&device[1], dev1, &err);
3846
3847 /* is double-select really necessary? */
3848 if (device[1].class != ATA_DEV_NONE)
3849 ap->ops->dev_select(ap, 1);
3850 if (device[0].class != ATA_DEV_NONE)
3851 ap->ops->dev_select(ap, 0);
3852
3853 /* if no devices were detected, disable this port */
3854 if ((device[0].class == ATA_DEV_NONE) &&
3855 (device[1].class == ATA_DEV_NONE))
3856 goto err_out;
3857
3858 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
3859 /* set up device control for ATA_FLAG_SATA_RESET */
3860 iowrite8(ap->ctl, ioaddr->ctl_addr);
3861 }
3862
3863 DPRINTK("EXIT\n");
3864 return;
3865
3866 err_out:
3867 ata_port_printk(ap, KERN_ERR, "disabling port\n");
3868 ata_port_disable(ap);
3869
3870 DPRINTK("EXIT\n");
3871 }
3872
3873 /**
3874 * sata_link_debounce - debounce SATA phy status
3875 * @link: ATA link to debounce SATA phy status for
3876 * @params: timing parameters { interval, duratinon, timeout } in msec
3877 * @deadline: deadline jiffies for the operation
3878 *
3879 * Make sure SStatus of @link reaches stable state, determined by
3880 * holding the same value where DET is not 1 for @duration polled
3881 * every @interval, before @timeout. Timeout constraints the
3882 * beginning of the stable state. Because DET gets stuck at 1 on
3883 * some controllers after hot unplugging, this functions waits
3884 * until timeout then returns 0 if DET is stable at 1.
3885 *
3886 * @timeout is further limited by @deadline. The sooner of the
3887 * two is used.
3888 *
3889 * LOCKING:
3890 * Kernel thread context (may sleep)
3891 *
3892 * RETURNS:
3893 * 0 on success, -errno on failure.
3894 */
3895 int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3896 unsigned long deadline)
3897 {
3898 unsigned long interval_msec = params[0];
3899 unsigned long duration = msecs_to_jiffies(params[1]);
3900 unsigned long last_jiffies, t;
3901 u32 last, cur;
3902 int rc;
3903
3904 t = jiffies + msecs_to_jiffies(params[2]);
3905 if (time_before(t, deadline))
3906 deadline = t;
3907
3908 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3909 return rc;
3910 cur &= 0xf;
3911
3912 last = cur;
3913 last_jiffies = jiffies;
3914
3915 while (1) {
3916 msleep(interval_msec);
3917 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3918 return rc;
3919 cur &= 0xf;
3920
3921 /* DET stable? */
3922 if (cur == last) {
3923 if (cur == 1 && time_before(jiffies, deadline))
3924 continue;
3925 if (time_after(jiffies, last_jiffies + duration))
3926 return 0;
3927 continue;
3928 }
3929
3930 /* unstable, start over */
3931 last = cur;
3932 last_jiffies = jiffies;
3933
3934 /* Check deadline. If debouncing failed, return
3935 * -EPIPE to tell upper layer to lower link speed.
3936 */
3937 if (time_after(jiffies, deadline))
3938 return -EPIPE;
3939 }
3940 }
3941
3942 /**
3943 * sata_link_resume - resume SATA link
3944 * @link: ATA link to resume SATA
3945 * @params: timing parameters { interval, duratinon, timeout } in msec
3946 * @deadline: deadline jiffies for the operation
3947 *
3948 * Resume SATA phy @link and debounce it.
3949 *
3950 * LOCKING:
3951 * Kernel thread context (may sleep)
3952 *
3953 * RETURNS:
3954 * 0 on success, -errno on failure.
3955 */
3956 int sata_link_resume(struct ata_link *link, const unsigned long *params,
3957 unsigned long deadline)
3958 {
3959 u32 scontrol;
3960 int rc;
3961
3962 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3963 return rc;
3964
3965 scontrol = (scontrol & 0x0f0) | 0x300;
3966
3967 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3968 return rc;
3969
3970 /* Some PHYs react badly if SStatus is pounded immediately
3971 * after resuming. Delay 200ms before debouncing.
3972 */
3973 msleep(200);
3974
3975 return sata_link_debounce(link, params, deadline);
3976 }
3977
3978 /**
3979 * ata_std_prereset - prepare for reset
3980 * @link: ATA link to be reset
3981 * @deadline: deadline jiffies for the operation
3982 *
3983 * @link is about to be reset. Initialize it. Failure from
3984 * prereset makes libata abort whole reset sequence and give up
3985 * that port, so prereset should be best-effort. It does its
3986 * best to prepare for reset sequence but if things go wrong, it
3987 * should just whine, not fail.
3988 *
3989 * LOCKING:
3990 * Kernel thread context (may sleep)
3991 *
3992 * RETURNS:
3993 * 0 on success, -errno otherwise.
3994 */
3995 int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3996 {
3997 struct ata_port *ap = link->ap;
3998 struct ata_eh_context *ehc = &link->eh_context;
3999 const unsigned long *timing = sata_ehc_deb_timing(ehc);
4000 int rc;
4001
4002 /* if we're about to do hardreset, nothing more to do */
4003 if (ehc->i.action & ATA_EH_HARDRESET)
4004 return 0;
4005
4006 /* if SATA, resume link */
4007 if (ap->flags & ATA_FLAG_SATA) {
4008 rc = sata_link_resume(link, timing, deadline);
4009 /* whine about phy resume failure but proceed */
4010 if (rc && rc != -EOPNOTSUPP)
4011 ata_link_printk(link, KERN_WARNING, "failed to resume "
4012 "link for reset (errno=%d)\n", rc);
4013 }
4014
4015 /* wait for !BSY if we don't know that no device is attached */
4016 if (!ata_link_offline(link)) {
4017 rc = ata_wait_ready(ap, deadline);
4018 if (rc && rc != -ENODEV) {
4019 ata_link_printk(link, KERN_WARNING, "device not ready "
4020 "(errno=%d), forcing hardreset\n", rc);
4021 ehc->i.action |= ATA_EH_HARDRESET;
4022 }
4023 }
4024
4025 return 0;
4026 }
4027
4028 /**
4029 * ata_std_softreset - reset host port via ATA SRST
4030 * @link: ATA link to reset
4031 * @classes: resulting classes of attached devices
4032 * @deadline: deadline jiffies for the operation
4033 *
4034 * Reset host port using ATA SRST.
4035 *
4036 * LOCKING:
4037 * Kernel thread context (may sleep)
4038 *
4039 * RETURNS:
4040 * 0 on success, -errno otherwise.
4041 */
4042 int ata_std_softreset(struct ata_link *link, unsigned int *classes,
4043 unsigned long deadline)
4044 {
4045 struct ata_port *ap = link->ap;
4046 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
4047 unsigned int devmask = 0;
4048 int rc;
4049 u8 err;
4050
4051 DPRINTK("ENTER\n");
4052
4053 if (ata_link_offline(link)) {
4054 classes[0] = ATA_DEV_NONE;
4055 goto out;
4056 }
4057
4058 /* determine if device 0/1 are present */
4059 if (ata_devchk(ap, 0))
4060 devmask |= (1 << 0);
4061 if (slave_possible && ata_devchk(ap, 1))
4062 devmask |= (1 << 1);
4063
4064 /* select device 0 again */
4065 ap->ops->dev_select(ap, 0);
4066
4067 /* issue bus reset */
4068 DPRINTK("about to softreset, devmask=%x\n", devmask);
4069 rc = ata_bus_softreset(ap, devmask, deadline);
4070 /* if link is occupied, -ENODEV too is an error */
4071 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
4072 ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
4073 return rc;
4074 }
4075
4076 /* determine by signature whether we have ATA or ATAPI devices */
4077 classes[0] = ata_dev_try_classify(&link->device[0],
4078 devmask & (1 << 0), &err);
4079 if (slave_possible && err != 0x81)
4080 classes[1] = ata_dev_try_classify(&link->device[1],
4081 devmask & (1 << 1), &err);
4082
4083 out:
4084 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
4085 return 0;
4086 }
4087
4088 /**
4089 * sata_link_hardreset - reset link via SATA phy reset
4090 * @link: link to reset
4091 * @timing: timing parameters { interval, duratinon, timeout } in msec
4092 * @deadline: deadline jiffies for the operation
4093 *
4094 * SATA phy-reset @link using DET bits of SControl register.
4095 *
4096 * LOCKING:
4097 * Kernel thread context (may sleep)
4098 *
4099 * RETURNS:
4100 * 0 on success, -errno otherwise.
4101 */
4102 int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
4103 unsigned long deadline)
4104 {
4105 u32 scontrol;
4106 int rc;
4107
4108 DPRINTK("ENTER\n");
4109
4110 if (sata_set_spd_needed(link)) {
4111 /* SATA spec says nothing about how to reconfigure
4112 * spd. To be on the safe side, turn off phy during
4113 * reconfiguration. This works for at least ICH7 AHCI
4114 * and Sil3124.
4115 */
4116 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
4117 goto out;
4118
4119 scontrol = (scontrol & 0x0f0) | 0x304;
4120
4121 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
4122 goto out;
4123
4124 sata_set_spd(link);
4125 }
4126
4127 /* issue phy wake/reset */
4128 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
4129 goto out;
4130
4131 scontrol = (scontrol & 0x0f0) | 0x301;
4132
4133 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
4134 goto out;
4135
4136 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
4137 * 10.4.2 says at least 1 ms.
4138 */
4139 msleep(1);
4140
4141 /* bring link back */
4142 rc = sata_link_resume(link, timing, deadline);
4143 out:
4144 DPRINTK("EXIT, rc=%d\n", rc);
4145 return rc;
4146 }
4147
4148 /**
4149 * sata_std_hardreset - reset host port via SATA phy reset
4150 * @link: link to reset
4151 * @class: resulting class of attached device
4152 * @deadline: deadline jiffies for the operation
4153 *
4154 * SATA phy-reset host port using DET bits of SControl register,
4155 * wait for !BSY and classify the attached device.
4156 *
4157 * LOCKING:
4158 * Kernel thread context (may sleep)
4159 *
4160 * RETURNS:
4161 * 0 on success, -errno otherwise.
4162 */
4163 int sata_std_hardreset(struct ata_link *link, unsigned int *class,
4164 unsigned long deadline)
4165 {
4166 struct ata_port *ap = link->ap;
4167 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
4168 int rc;
4169
4170 DPRINTK("ENTER\n");
4171
4172 /* do hardreset */
4173 rc = sata_link_hardreset(link, timing, deadline);
4174 if (rc) {
4175 ata_link_printk(link, KERN_ERR,
4176 "COMRESET failed (errno=%d)\n", rc);
4177 return rc;
4178 }
4179
4180 /* TODO: phy layer with polling, timeouts, etc. */
4181 if (ata_link_offline(link)) {
4182 *class = ATA_DEV_NONE;
4183 DPRINTK("EXIT, link offline\n");
4184 return 0;
4185 }
4186
4187 /* wait a while before checking status */
4188 ata_wait_after_reset(ap, deadline);
4189
4190 /* If PMP is supported, we have to do follow-up SRST. Note
4191 * that some PMPs don't send D2H Reg FIS after hardreset at
4192 * all if the first port is empty. Wait for it just for a
4193 * second and request follow-up SRST.
4194 */
4195 if (ap->flags & ATA_FLAG_PMP) {
4196 ata_wait_ready(ap, jiffies + HZ);
4197 return -EAGAIN;
4198 }
4199
4200 rc = ata_wait_ready(ap, deadline);
4201 /* link occupied, -ENODEV too is an error */
4202 if (rc) {
4203 ata_link_printk(link, KERN_ERR,
4204 "COMRESET failed (errno=%d)\n", rc);
4205 return rc;
4206 }
4207
4208 ap->ops->dev_select(ap, 0); /* probably unnecessary */
4209
4210 *class = ata_dev_try_classify(link->device, 1, NULL);
4211
4212 DPRINTK("EXIT, class=%u\n", *class);
4213 return 0;
4214 }
4215
4216 /**
4217 * ata_std_postreset - standard postreset callback
4218 * @link: the target ata_link
4219 * @classes: classes of attached devices
4220 *
4221 * This function is invoked after a successful reset. Note that
4222 * the device might have been reset more than once using
4223 * different reset methods before postreset is invoked.
4224 *
4225 * LOCKING:
4226 * Kernel thread context (may sleep)
4227 */
4228 void ata_std_postreset(struct ata_link *link, unsigned int *classes)
4229 {
4230 struct ata_port *ap = link->ap;
4231 u32 serror;
4232
4233 DPRINTK("ENTER\n");
4234
4235 /* print link status */
4236 sata_print_link_status(link);
4237
4238 /* clear SError */
4239 if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
4240 sata_scr_write(link, SCR_ERROR, serror);
4241 link->eh_info.serror = 0;
4242
4243 /* is double-select really necessary? */
4244 if (classes[0] != ATA_DEV_NONE)
4245 ap->ops->dev_select(ap, 1);
4246 if (classes[1] != ATA_DEV_NONE)
4247 ap->ops->dev_select(ap, 0);
4248
4249 /* bail out if no device is present */
4250 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
4251 DPRINTK("EXIT, no device\n");
4252 return;
4253 }
4254
4255 /* set up device control */
4256 if (ap->ioaddr.ctl_addr)
4257 iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
4258
4259 DPRINTK("EXIT\n");
4260 }
4261
4262 /**
4263 * ata_dev_same_device - Determine whether new ID matches configured device
4264 * @dev: device to compare against
4265 * @new_class: class of the new device
4266 * @new_id: IDENTIFY page of the new device
4267 *
4268 * Compare @new_class and @new_id against @dev and determine
4269 * whether @dev is the device indicated by @new_class and
4270 * @new_id.
4271 *
4272 * LOCKING:
4273 * None.
4274 *
4275 * RETURNS:
4276 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
4277 */
4278 static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
4279 const u16 *new_id)
4280 {
4281 const u16 *old_id = dev->id;
4282 unsigned char model[2][ATA_ID_PROD_LEN + 1];
4283 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
4284
4285 if (dev->class != new_class) {
4286 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
4287 dev->class, new_class);
4288 return 0;
4289 }
4290
4291 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
4292 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
4293 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
4294 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
4295
4296 if (strcmp(model[0], model[1])) {
4297 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
4298 "'%s' != '%s'\n", model[0], model[1]);
4299 return 0;
4300 }
4301
4302 if (strcmp(serial[0], serial[1])) {
4303 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
4304 "'%s' != '%s'\n", serial[0], serial[1]);
4305 return 0;
4306 }
4307
4308 return 1;
4309 }
4310
4311 /**
4312 * ata_dev_reread_id - Re-read IDENTIFY data
4313 * @dev: target ATA device
4314 * @readid_flags: read ID flags
4315 *
4316 * Re-read IDENTIFY page and make sure @dev is still attached to
4317 * the port.
4318 *
4319 * LOCKING:
4320 * Kernel thread context (may sleep)
4321 *
4322 * RETURNS:
4323 * 0 on success, negative errno otherwise
4324 */
4325 int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
4326 {
4327 unsigned int class = dev->class;
4328 u16 *id = (void *)dev->link->ap->sector_buf;
4329 int rc;
4330
4331 /* read ID data */
4332 rc = ata_dev_read_id(dev, &class, readid_flags, id);
4333 if (rc)
4334 return rc;
4335
4336 /* is the device still there? */
4337 if (!ata_dev_same_device(dev, class, id))
4338 return -ENODEV;
4339
4340 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
4341 return 0;
4342 }
4343
4344 /**
4345 * ata_dev_revalidate - Revalidate ATA device
4346 * @dev: device to revalidate
4347 * @new_class: new class code
4348 * @readid_flags: read ID flags
4349 *
4350 * Re-read IDENTIFY page, make sure @dev is still attached to the
4351 * port and reconfigure it according to the new IDENTIFY page.
4352 *
4353 * LOCKING:
4354 * Kernel thread context (may sleep)
4355 *
4356 * RETURNS:
4357 * 0 on success, negative errno otherwise
4358 */
4359 int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4360 unsigned int readid_flags)
4361 {
4362 u64 n_sectors = dev->n_sectors;
4363 int rc;
4364
4365 if (!ata_dev_enabled(dev))
4366 return -ENODEV;
4367
4368 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4369 if (ata_class_enabled(new_class) &&
4370 new_class != ATA_DEV_ATA && new_class != ATA_DEV_ATAPI) {
4371 ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
4372 dev->class, new_class);
4373 rc = -ENODEV;
4374 goto fail;
4375 }
4376
4377 /* re-read ID */
4378 rc = ata_dev_reread_id(dev, readid_flags);
4379 if (rc)
4380 goto fail;
4381
4382 /* configure device according to the new ID */
4383 rc = ata_dev_configure(dev);
4384 if (rc)
4385 goto fail;
4386
4387 /* verify n_sectors hasn't changed */
4388 if (dev->class == ATA_DEV_ATA && n_sectors &&
4389 dev->n_sectors != n_sectors) {
4390 ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
4391 "%llu != %llu\n",
4392 (unsigned long long)n_sectors,
4393 (unsigned long long)dev->n_sectors);
4394
4395 /* restore original n_sectors */
4396 dev->n_sectors = n_sectors;
4397
4398 rc = -ENODEV;
4399 goto fail;
4400 }
4401
4402 return 0;
4403
4404 fail:
4405 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
4406 return rc;
4407 }
4408
4409 struct ata_blacklist_entry {
4410 const char *model_num;
4411 const char *model_rev;
4412 unsigned long horkage;
4413 };
4414
4415 static const struct ata_blacklist_entry ata_device_blacklist [] = {
4416 /* Devices with DMA related problems under Linux */
4417 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
4418 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
4419 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
4420 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
4421 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
4422 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
4423 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
4424 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
4425 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
4426 { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
4427 { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
4428 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
4429 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
4430 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
4431 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
4432 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
4433 { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
4434 { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
4435 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
4436 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
4437 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
4438 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
4439 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
4440 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
4441 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
4442 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
4443 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
4444 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
4445 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
4446 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
4447 /* Odd clown on sil3726/4726 PMPs */
4448 { "Config Disk", NULL, ATA_HORKAGE_NODMA |
4449 ATA_HORKAGE_SKIP_PM },
4450
4451 /* Weird ATAPI devices */
4452 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
4453
4454 /* Devices we expect to fail diagnostics */
4455
4456 /* Devices where NCQ should be avoided */
4457 /* NCQ is slow */
4458 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
4459 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
4460 /* http://thread.gmane.org/gmane.linux.ide/14907 */
4461 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
4462 /* NCQ is broken */
4463 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
4464 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
4465 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
4466 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
4467
4468 /* Blacklist entries taken from Silicon Image 3124/3132
4469 Windows driver .inf file - also several Linux problem reports */
4470 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
4471 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
4472 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
4473
4474 /* devices which puke on READ_NATIVE_MAX */
4475 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
4476 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
4477 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
4478 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
4479
4480 /* Devices which report 1 sector over size HPA */
4481 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
4482 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
4483 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
4484
4485 /* Devices which get the IVB wrong */
4486 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4487 { "TSSTcorp CDDVDW SH-S202J", "SB00", ATA_HORKAGE_IVB, },
4488 { "TSSTcorp CDDVDW SH-S202J", "SB01", ATA_HORKAGE_IVB, },
4489 { "TSSTcorp CDDVDW SH-S202N", "SB00", ATA_HORKAGE_IVB, },
4490 { "TSSTcorp CDDVDW SH-S202N", "SB01", ATA_HORKAGE_IVB, },
4491
4492 /* End Marker */
4493 { }
4494 };
4495
4496 static int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
4497 {
4498 const char *p;
4499 int len;
4500
4501 /*
4502 * check for trailing wildcard: *\0
4503 */
4504 p = strchr(patt, wildchar);
4505 if (p && ((*(p + 1)) == 0))
4506 len = p - patt;
4507 else {
4508 len = strlen(name);
4509 if (!len) {
4510 if (!*patt)
4511 return 0;
4512 return -1;
4513 }
4514 }
4515
4516 return strncmp(patt, name, len);
4517 }
4518
4519 static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4520 {
4521 unsigned char model_num[ATA_ID_PROD_LEN + 1];
4522 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
4523 const struct ata_blacklist_entry *ad = ata_device_blacklist;
4524
4525 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
4526 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4527
4528 while (ad->model_num) {
4529 if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
4530 if (ad->model_rev == NULL)
4531 return ad->horkage;
4532 if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
4533 return ad->horkage;
4534 }
4535 ad++;
4536 }
4537 return 0;
4538 }
4539
4540 static int ata_dma_blacklisted(const struct ata_device *dev)
4541 {
4542 /* We don't support polling DMA.
4543 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
4544 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
4545 */
4546 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
4547 (dev->flags & ATA_DFLAG_CDB_INTR))
4548 return 1;
4549 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
4550 }
4551
4552 /**
4553 * ata_is_40wire - check drive side detection
4554 * @dev: device
4555 *
4556 * Perform drive side detection decoding, allowing for device vendors
4557 * who can't follow the documentation.
4558 */
4559
4560 static int ata_is_40wire(struct ata_device *dev)
4561 {
4562 if (dev->horkage & ATA_HORKAGE_IVB)
4563 return ata_drive_40wire_relaxed(dev->id);
4564 return ata_drive_40wire(dev->id);
4565 }
4566
4567 /**
4568 * ata_dev_xfermask - Compute supported xfermask of the given device
4569 * @dev: Device to compute xfermask for
4570 *
4571 * Compute supported xfermask of @dev and store it in
4572 * dev->*_mask. This function is responsible for applying all
4573 * known limits including host controller limits, device
4574 * blacklist, etc...
4575 *
4576 * LOCKING:
4577 * None.
4578 */
4579 static void ata_dev_xfermask(struct ata_device *dev)
4580 {
4581 struct ata_link *link = dev->link;
4582 struct ata_port *ap = link->ap;
4583 struct ata_host *host = ap->host;
4584 unsigned long xfer_mask;
4585
4586 /* controller modes available */
4587 xfer_mask = ata_pack_xfermask(ap->pio_mask,
4588 ap->mwdma_mask, ap->udma_mask);
4589
4590 /* drive modes available */
4591 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4592 dev->mwdma_mask, dev->udma_mask);
4593 xfer_mask &= ata_id_xfermask(dev->id);
4594
4595 /*
4596 * CFA Advanced TrueIDE timings are not allowed on a shared
4597 * cable
4598 */
4599 if (ata_dev_pair(dev)) {
4600 /* No PIO5 or PIO6 */
4601 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4602 /* No MWDMA3 or MWDMA 4 */
4603 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4604 }
4605
4606 if (ata_dma_blacklisted(dev)) {
4607 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4608 ata_dev_printk(dev, KERN_WARNING,
4609 "device is on DMA blacklist, disabling DMA\n");
4610 }
4611
4612 if ((host->flags & ATA_HOST_SIMPLEX) &&
4613 host->simplex_claimed && host->simplex_claimed != ap) {
4614 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4615 ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
4616 "other device, disabling DMA\n");
4617 }
4618
4619 if (ap->flags & ATA_FLAG_NO_IORDY)
4620 xfer_mask &= ata_pio_mask_no_iordy(dev);
4621
4622 if (ap->ops->mode_filter)
4623 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4624
4625 /* Apply cable rule here. Don't apply it early because when
4626 * we handle hot plug the cable type can itself change.
4627 * Check this last so that we know if the transfer rate was
4628 * solely limited by the cable.
4629 * Unknown or 80 wire cables reported host side are checked
4630 * drive side as well. Cases where we know a 40wire cable
4631 * is used safely for 80 are not checked here.
4632 */
4633 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4634 /* UDMA/44 or higher would be available */
4635 if ((ap->cbl == ATA_CBL_PATA40) ||
4636 (ata_is_40wire(dev) &&
4637 (ap->cbl == ATA_CBL_PATA_UNK ||
4638 ap->cbl == ATA_CBL_PATA80))) {
4639 ata_dev_printk(dev, KERN_WARNING,
4640 "limited to UDMA/33 due to 40-wire cable\n");
4641 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4642 }
4643
4644 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4645 &dev->mwdma_mask, &dev->udma_mask);
4646 }
4647
4648 /**
4649 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4650 * @dev: Device to which command will be sent
4651 *
4652 * Issue SET FEATURES - XFER MODE command to device @dev
4653 * on port @ap.
4654 *
4655 * LOCKING:
4656 * PCI/etc. bus probe sem.
4657 *
4658 * RETURNS:
4659 * 0 on success, AC_ERR_* mask otherwise.
4660 */
4661
4662 static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4663 {
4664 struct ata_taskfile tf;
4665 unsigned int err_mask;
4666
4667 /* set up set-features taskfile */
4668 DPRINTK("set features - xfer mode\n");
4669
4670 /* Some controllers and ATAPI devices show flaky interrupt
4671 * behavior after setting xfer mode. Use polling instead.
4672 */
4673 ata_tf_init(dev, &tf);
4674 tf.command = ATA_CMD_SET_FEATURES;
4675 tf.feature = SETFEATURES_XFER;
4676 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4677 tf.protocol = ATA_PROT_NODATA;
4678 /* If we are using IORDY we must send the mode setting command */
4679 if (ata_pio_need_iordy(dev))
4680 tf.nsect = dev->xfer_mode;
4681 /* If the device has IORDY and the controller does not - turn it off */
4682 else if (ata_id_has_iordy(dev->id))
4683 tf.nsect = 0x01;
4684 else /* In the ancient relic department - skip all of this */
4685 return 0;
4686
4687 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4688
4689 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4690 return err_mask;
4691 }
4692 /**
4693 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
4694 * @dev: Device to which command will be sent
4695 * @enable: Whether to enable or disable the feature
4696 * @feature: The sector count represents the feature to set
4697 *
4698 * Issue SET FEATURES - SATA FEATURES command to device @dev
4699 * on port @ap with sector count
4700 *
4701 * LOCKING:
4702 * PCI/etc. bus probe sem.
4703 *
4704 * RETURNS:
4705 * 0 on success, AC_ERR_* mask otherwise.
4706 */
4707 static unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable,
4708 u8 feature)
4709 {
4710 struct ata_taskfile tf;
4711 unsigned int err_mask;
4712
4713 /* set up set-features taskfile */
4714 DPRINTK("set features - SATA features\n");
4715
4716 ata_tf_init(dev, &tf);
4717 tf.command = ATA_CMD_SET_FEATURES;
4718 tf.feature = enable;
4719 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4720 tf.protocol = ATA_PROT_NODATA;
4721 tf.nsect = feature;
4722
4723 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4724
4725 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4726 return err_mask;
4727 }
4728
4729 /**
4730 * ata_dev_init_params - Issue INIT DEV PARAMS command
4731 * @dev: Device to which command will be sent
4732 * @heads: Number of heads (taskfile parameter)
4733 * @sectors: Number of sectors (taskfile parameter)
4734 *
4735 * LOCKING:
4736 * Kernel thread context (may sleep)
4737 *
4738 * RETURNS:
4739 * 0 on success, AC_ERR_* mask otherwise.
4740 */
4741 static unsigned int ata_dev_init_params(struct ata_device *dev,
4742 u16 heads, u16 sectors)
4743 {
4744 struct ata_taskfile tf;
4745 unsigned int err_mask;
4746
4747 /* Number of sectors per track 1-255. Number of heads 1-16 */
4748 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4749 return AC_ERR_INVALID;
4750
4751 /* set up init dev params taskfile */
4752 DPRINTK("init dev params \n");
4753
4754 ata_tf_init(dev, &tf);
4755 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4756 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4757 tf.protocol = ATA_PROT_NODATA;
4758 tf.nsect = sectors;
4759 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4760
4761 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4762 /* A clean abort indicates an original or just out of spec drive
4763 and we should continue as we issue the setup based on the
4764 drive reported working geometry */
4765 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4766 err_mask = 0;
4767
4768 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4769 return err_mask;
4770 }
4771
4772 /**
4773 * ata_sg_clean - Unmap DMA memory associated with command
4774 * @qc: Command containing DMA memory to be released
4775 *
4776 * Unmap all mapped DMA memory associated with this command.
4777 *
4778 * LOCKING:
4779 * spin_lock_irqsave(host lock)
4780 */
4781 void ata_sg_clean(struct ata_queued_cmd *qc)
4782 {
4783 struct ata_port *ap = qc->ap;
4784 struct scatterlist *sg = qc->sg;
4785 int dir = qc->dma_dir;
4786
4787 WARN_ON(sg == NULL);
4788
4789 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4790
4791 if (qc->n_elem)
4792 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
4793
4794 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4795 qc->sg = NULL;
4796 }
4797
4798 /**
4799 * ata_fill_sg - Fill PCI IDE PRD table
4800 * @qc: Metadata associated with taskfile to be transferred
4801 *
4802 * Fill PCI IDE PRD (scatter-gather) table with segments
4803 * associated with the current disk command.
4804 *
4805 * LOCKING:
4806 * spin_lock_irqsave(host lock)
4807 *
4808 */
4809 static void ata_fill_sg(struct ata_queued_cmd *qc)
4810 {
4811 struct ata_port *ap = qc->ap;
4812 struct scatterlist *sg;
4813 unsigned int si, pi;
4814
4815 pi = 0;
4816 for_each_sg(qc->sg, sg, qc->n_elem, si) {
4817 u32 addr, offset;
4818 u32 sg_len, len;
4819
4820 /* determine if physical DMA addr spans 64K boundary.
4821 * Note h/w doesn't support 64-bit, so we unconditionally
4822 * truncate dma_addr_t to u32.
4823 */
4824 addr = (u32) sg_dma_address(sg);
4825 sg_len = sg_dma_len(sg);
4826
4827 while (sg_len) {
4828 offset = addr & 0xffff;
4829 len = sg_len;
4830 if ((offset + sg_len) > 0x10000)
4831 len = 0x10000 - offset;
4832
4833 ap->prd[pi].addr = cpu_to_le32(addr);
4834 ap->prd[pi].flags_len = cpu_to_le32(len & 0xffff);
4835 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
4836
4837 pi++;
4838 sg_len -= len;
4839 addr += len;
4840 }
4841 }
4842
4843 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4844 }
4845
4846 /**
4847 * ata_fill_sg_dumb - Fill PCI IDE PRD table
4848 * @qc: Metadata associated with taskfile to be transferred
4849 *
4850 * Fill PCI IDE PRD (scatter-gather) table with segments
4851 * associated with the current disk command. Perform the fill
4852 * so that we avoid writing any length 64K records for
4853 * controllers that don't follow the spec.
4854 *
4855 * LOCKING:
4856 * spin_lock_irqsave(host lock)
4857 *
4858 */
4859 static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
4860 {
4861 struct ata_port *ap = qc->ap;
4862 struct scatterlist *sg;
4863 unsigned int si, pi;
4864
4865 pi = 0;
4866 for_each_sg(qc->sg, sg, qc->n_elem, si) {
4867 u32 addr, offset;
4868 u32 sg_len, len, blen;
4869
4870 /* determine if physical DMA addr spans 64K boundary.
4871 * Note h/w doesn't support 64-bit, so we unconditionally
4872 * truncate dma_addr_t to u32.
4873 */
4874 addr = (u32) sg_dma_address(sg);
4875 sg_len = sg_dma_len(sg);
4876
4877 while (sg_len) {
4878 offset = addr & 0xffff;
4879 len = sg_len;
4880 if ((offset + sg_len) > 0x10000)
4881 len = 0x10000 - offset;
4882
4883 blen = len & 0xffff;
4884 ap->prd[pi].addr = cpu_to_le32(addr);
4885 if (blen == 0) {
4886 /* Some PATA chipsets like the CS5530 can't
4887 cope with 0x0000 meaning 64K as the spec says */
4888 ap->prd[pi].flags_len = cpu_to_le32(0x8000);
4889 blen = 0x8000;
4890 ap->prd[++pi].addr = cpu_to_le32(addr + 0x8000);
4891 }
4892 ap->prd[pi].flags_len = cpu_to_le32(blen);
4893 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
4894
4895 pi++;
4896 sg_len -= len;
4897 addr += len;
4898 }
4899 }
4900
4901 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4902 }
4903
4904 /**
4905 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
4906 * @qc: Metadata associated with taskfile to check
4907 *
4908 * Allow low-level driver to filter ATA PACKET commands, returning
4909 * a status indicating whether or not it is OK to use DMA for the
4910 * supplied PACKET command.
4911 *
4912 * LOCKING:
4913 * spin_lock_irqsave(host lock)
4914 *
4915 * RETURNS: 0 when ATAPI DMA can be used
4916 * nonzero otherwise
4917 */
4918 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
4919 {
4920 struct ata_port *ap = qc->ap;
4921
4922 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4923 * few ATAPI devices choke on such DMA requests.
4924 */
4925 if (unlikely(qc->nbytes & 15))
4926 return 1;
4927
4928 if (ap->ops->check_atapi_dma)
4929 return ap->ops->check_atapi_dma(qc);
4930
4931 return 0;
4932 }
4933
4934 /**
4935 * ata_std_qc_defer - Check whether a qc needs to be deferred
4936 * @qc: ATA command in question
4937 *
4938 * Non-NCQ commands cannot run with any other command, NCQ or
4939 * not. As upper layer only knows the queue depth, we are
4940 * responsible for maintaining exclusion. This function checks
4941 * whether a new command @qc can be issued.
4942 *
4943 * LOCKING:
4944 * spin_lock_irqsave(host lock)
4945 *
4946 * RETURNS:
4947 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4948 */
4949 int ata_std_qc_defer(struct ata_queued_cmd *qc)
4950 {
4951 struct ata_link *link = qc->dev->link;
4952
4953 if (qc->tf.protocol == ATA_PROT_NCQ) {
4954 if (!ata_tag_valid(link->active_tag))
4955 return 0;
4956 } else {
4957 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4958 return 0;
4959 }
4960
4961 return ATA_DEFER_LINK;
4962 }
4963
4964 /**
4965 * ata_qc_prep - Prepare taskfile for submission
4966 * @qc: Metadata associated with taskfile to be prepared
4967 *
4968 * Prepare ATA taskfile for submission.
4969 *
4970 * LOCKING:
4971 * spin_lock_irqsave(host lock)
4972 */
4973 void ata_qc_prep(struct ata_queued_cmd *qc)
4974 {
4975 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
4976 return;
4977
4978 ata_fill_sg(qc);
4979 }
4980
4981 /**
4982 * ata_dumb_qc_prep - Prepare taskfile for submission
4983 * @qc: Metadata associated with taskfile to be prepared
4984 *
4985 * Prepare ATA taskfile for submission.
4986 *
4987 * LOCKING:
4988 * spin_lock_irqsave(host lock)
4989 */
4990 void ata_dumb_qc_prep(struct ata_queued_cmd *qc)
4991 {
4992 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
4993 return;
4994
4995 ata_fill_sg_dumb(qc);
4996 }
4997
4998 void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4999
5000 /**
5001 * ata_sg_init - Associate command with scatter-gather table.
5002 * @qc: Command to be associated
5003 * @sg: Scatter-gather table.
5004 * @n_elem: Number of elements in s/g table.
5005 *
5006 * Initialize the data-related elements of queued_cmd @qc
5007 * to point to a scatter-gather table @sg, containing @n_elem
5008 * elements.
5009 *
5010 * LOCKING:
5011 * spin_lock_irqsave(host lock)
5012 */
5013 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
5014 unsigned int n_elem)
5015 {
5016 qc->sg = sg;
5017 qc->n_elem = n_elem;
5018 qc->cursg = qc->sg;
5019 }
5020
5021 /**
5022 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
5023 * @qc: Command with scatter-gather table to be mapped.
5024 *
5025 * DMA-map the scatter-gather table associated with queued_cmd @qc.
5026 *
5027 * LOCKING:
5028 * spin_lock_irqsave(host lock)
5029 *
5030 * RETURNS:
5031 * Zero on success, negative on error.
5032 *
5033 */
5034 static int ata_sg_setup(struct ata_queued_cmd *qc)
5035 {
5036 struct ata_port *ap = qc->ap;
5037 unsigned int n_elem;
5038
5039 VPRINTK("ENTER, ata%u\n", ap->print_id);
5040
5041 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
5042 if (n_elem < 1)
5043 return -1;
5044
5045 DPRINTK("%d sg elements mapped\n", n_elem);
5046
5047 qc->n_elem = n_elem;
5048 qc->flags |= ATA_QCFLAG_DMAMAP;
5049
5050 return 0;
5051 }
5052
5053 /**
5054 * swap_buf_le16 - swap halves of 16-bit words in place
5055 * @buf: Buffer to swap
5056 * @buf_words: Number of 16-bit words in buffer.
5057 *
5058 * Swap halves of 16-bit words if needed to convert from
5059 * little-endian byte order to native cpu byte order, or
5060 * vice-versa.
5061 *
5062 * LOCKING:
5063 * Inherited from caller.
5064 */
5065 void swap_buf_le16(u16 *buf, unsigned int buf_words)
5066 {
5067 #ifdef __BIG_ENDIAN
5068 unsigned int i;
5069
5070 for (i = 0; i < buf_words; i++)
5071 buf[i] = le16_to_cpu(buf[i]);
5072 #endif /* __BIG_ENDIAN */
5073 }
5074
5075 /**
5076 * ata_data_xfer - Transfer data by PIO
5077 * @dev: device to target
5078 * @buf: data buffer
5079 * @buflen: buffer length
5080 * @rw: read/write
5081 *
5082 * Transfer data from/to the device data register by PIO.
5083 *
5084 * LOCKING:
5085 * Inherited from caller.
5086 *
5087 * RETURNS:
5088 * Bytes consumed.
5089 */
5090 unsigned int ata_data_xfer(struct ata_device *dev, unsigned char *buf,
5091 unsigned int buflen, int rw)
5092 {
5093 struct ata_port *ap = dev->link->ap;
5094 void __iomem *data_addr = ap->ioaddr.data_addr;
5095 unsigned int words = buflen >> 1;
5096
5097 /* Transfer multiple of 2 bytes */
5098 if (rw == READ)
5099 ioread16_rep(data_addr, buf, words);
5100 else
5101 iowrite16_rep(data_addr, buf, words);
5102
5103 /* Transfer trailing 1 byte, if any. */
5104 if (unlikely(buflen & 0x01)) {
5105 __le16 align_buf[1] = { 0 };
5106 unsigned char *trailing_buf = buf + buflen - 1;
5107
5108 if (rw == READ) {
5109 align_buf[0] = cpu_to_le16(ioread16(data_addr));
5110 memcpy(trailing_buf, align_buf, 1);
5111 } else {
5112 memcpy(align_buf, trailing_buf, 1);
5113 iowrite16(le16_to_cpu(align_buf[0]), data_addr);
5114 }
5115 words++;
5116 }
5117
5118 return words << 1;
5119 }
5120
5121 /**
5122 * ata_data_xfer_noirq - Transfer data by PIO
5123 * @dev: device to target
5124 * @buf: data buffer
5125 * @buflen: buffer length
5126 * @rw: read/write
5127 *
5128 * Transfer data from/to the device data register by PIO. Do the
5129 * transfer with interrupts disabled.
5130 *
5131 * LOCKING:
5132 * Inherited from caller.
5133 *
5134 * RETURNS:
5135 * Bytes consumed.
5136 */
5137 unsigned int ata_data_xfer_noirq(struct ata_device *dev, unsigned char *buf,
5138 unsigned int buflen, int rw)
5139 {
5140 unsigned long flags;
5141 unsigned int consumed;
5142
5143 local_irq_save(flags);
5144 consumed = ata_data_xfer(dev, buf, buflen, rw);
5145 local_irq_restore(flags);
5146
5147 return consumed;
5148 }
5149
5150
5151 /**
5152 * ata_pio_sector - Transfer a sector of data.
5153 * @qc: Command on going
5154 *
5155 * Transfer qc->sect_size bytes of data from/to the ATA device.
5156 *
5157 * LOCKING:
5158 * Inherited from caller.
5159 */
5160
5161 static void ata_pio_sector(struct ata_queued_cmd *qc)
5162 {
5163 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
5164 struct ata_port *ap = qc->ap;
5165 struct page *page;
5166 unsigned int offset;
5167 unsigned char *buf;
5168
5169 if (qc->curbytes == qc->nbytes - qc->sect_size)
5170 ap->hsm_task_state = HSM_ST_LAST;
5171
5172 page = sg_page(qc->cursg);
5173 offset = qc->cursg->offset + qc->cursg_ofs;
5174
5175 /* get the current page and offset */
5176 page = nth_page(page, (offset >> PAGE_SHIFT));
5177 offset %= PAGE_SIZE;
5178
5179 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
5180
5181 if (PageHighMem(page)) {
5182 unsigned long flags;
5183
5184 /* FIXME: use a bounce buffer */
5185 local_irq_save(flags);
5186 buf = kmap_atomic(page, KM_IRQ0);
5187
5188 /* do the actual data transfer */
5189 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
5190
5191 kunmap_atomic(buf, KM_IRQ0);
5192 local_irq_restore(flags);
5193 } else {
5194 buf = page_address(page);
5195 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
5196 }
5197
5198 qc->curbytes += qc->sect_size;
5199 qc->cursg_ofs += qc->sect_size;
5200
5201 if (qc->cursg_ofs == qc->cursg->length) {
5202 qc->cursg = sg_next(qc->cursg);
5203 qc->cursg_ofs = 0;
5204 }
5205 }
5206
5207 /**
5208 * ata_pio_sectors - Transfer one or many sectors.
5209 * @qc: Command on going
5210 *
5211 * Transfer one or many sectors of data from/to the
5212 * ATA device for the DRQ request.
5213 *
5214 * LOCKING:
5215 * Inherited from caller.
5216 */
5217
5218 static void ata_pio_sectors(struct ata_queued_cmd *qc)
5219 {
5220 if (is_multi_taskfile(&qc->tf)) {
5221 /* READ/WRITE MULTIPLE */
5222 unsigned int nsect;
5223
5224 WARN_ON(qc->dev->multi_count == 0);
5225
5226 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
5227 qc->dev->multi_count);
5228 while (nsect--)
5229 ata_pio_sector(qc);
5230 } else
5231 ata_pio_sector(qc);
5232
5233 ata_altstatus(qc->ap); /* flush */
5234 }
5235
5236 /**
5237 * atapi_send_cdb - Write CDB bytes to hardware
5238 * @ap: Port to which ATAPI device is attached.
5239 * @qc: Taskfile currently active
5240 *
5241 * When device has indicated its readiness to accept
5242 * a CDB, this function is called. Send the CDB.
5243 *
5244 * LOCKING:
5245 * caller.
5246 */
5247
5248 static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
5249 {
5250 /* send SCSI cdb */
5251 DPRINTK("send cdb\n");
5252 WARN_ON(qc->dev->cdb_len < 12);
5253
5254 ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
5255 ata_altstatus(ap); /* flush */
5256
5257 switch (qc->tf.protocol) {
5258 case ATAPI_PROT_PIO:
5259 ap->hsm_task_state = HSM_ST;
5260 break;
5261 case ATAPI_PROT_NODATA:
5262 ap->hsm_task_state = HSM_ST_LAST;
5263 break;
5264 case ATAPI_PROT_DMA:
5265 ap->hsm_task_state = HSM_ST_LAST;
5266 /* initiate bmdma */
5267 ap->ops->bmdma_start(qc);
5268 break;
5269 }
5270 }
5271
5272 /**
5273 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
5274 * @qc: Command on going
5275 * @bytes: number of bytes
5276 *
5277 * Transfer Transfer data from/to the ATAPI device.
5278 *
5279 * LOCKING:
5280 * Inherited from caller.
5281 *
5282 */
5283 static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
5284 {
5285 int rw = (qc->tf.flags & ATA_TFLAG_WRITE) ? WRITE : READ;
5286 struct ata_port *ap = qc->ap;
5287 struct ata_device *dev = qc->dev;
5288 struct ata_eh_info *ehi = &dev->link->eh_info;
5289 struct scatterlist *sg;
5290 struct page *page;
5291 unsigned char *buf;
5292 unsigned int offset, count, consumed;
5293
5294 next_sg:
5295 sg = qc->cursg;
5296 if (unlikely(!sg)) {
5297 ata_ehi_push_desc(ehi, "unexpected or too much trailing data "
5298 "buf=%u cur=%u bytes=%u",
5299 qc->nbytes, qc->curbytes, bytes);
5300 return -1;
5301 }
5302
5303 page = sg_page(sg);
5304 offset = sg->offset + qc->cursg_ofs;
5305
5306 /* get the current page and offset */
5307 page = nth_page(page, (offset >> PAGE_SHIFT));
5308 offset %= PAGE_SIZE;
5309
5310 /* don't overrun current sg */
5311 count = min(sg->length - qc->cursg_ofs, bytes);
5312
5313 /* don't cross page boundaries */
5314 count = min(count, (unsigned int)PAGE_SIZE - offset);
5315
5316 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
5317
5318 if (PageHighMem(page)) {
5319 unsigned long flags;
5320
5321 /* FIXME: use bounce buffer */
5322 local_irq_save(flags);
5323 buf = kmap_atomic(page, KM_IRQ0);
5324
5325 /* do the actual data transfer */
5326 consumed = ap->ops->data_xfer(dev, buf + offset, count, rw);
5327
5328 kunmap_atomic(buf, KM_IRQ0);
5329 local_irq_restore(flags);
5330 } else {
5331 buf = page_address(page);
5332 consumed = ap->ops->data_xfer(dev, buf + offset, count, rw);
5333 }
5334
5335 bytes -= min(bytes, consumed);
5336 qc->curbytes += count;
5337 qc->cursg_ofs += count;
5338
5339 if (qc->cursg_ofs == sg->length) {
5340 qc->cursg = sg_next(qc->cursg);
5341 qc->cursg_ofs = 0;
5342 }
5343
5344 /* consumed can be larger than count only for the last transfer */
5345 WARN_ON(qc->cursg && count != consumed);
5346
5347 if (bytes)
5348 goto next_sg;
5349 return 0;
5350 }
5351
5352 /**
5353 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
5354 * @qc: Command on going
5355 *
5356 * Transfer Transfer data from/to the ATAPI device.
5357 *
5358 * LOCKING:
5359 * Inherited from caller.
5360 */
5361
5362 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
5363 {
5364 struct ata_port *ap = qc->ap;
5365 struct ata_device *dev = qc->dev;
5366 struct ata_eh_info *ehi = &dev->link->eh_info;
5367 unsigned int ireason, bc_lo, bc_hi, bytes;
5368 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
5369
5370 /* Abuse qc->result_tf for temp storage of intermediate TF
5371 * here to save some kernel stack usage.
5372 * For normal completion, qc->result_tf is not relevant. For
5373 * error, qc->result_tf is later overwritten by ata_qc_complete().
5374 * So, the correctness of qc->result_tf is not affected.
5375 */
5376 ap->ops->tf_read(ap, &qc->result_tf);
5377 ireason = qc->result_tf.nsect;
5378 bc_lo = qc->result_tf.lbam;
5379 bc_hi = qc->result_tf.lbah;
5380 bytes = (bc_hi << 8) | bc_lo;
5381
5382 /* shall be cleared to zero, indicating xfer of data */
5383 if (unlikely(ireason & (1 << 0)))
5384 goto atapi_check;
5385
5386 /* make sure transfer direction matches expected */
5387 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
5388 if (unlikely(do_write != i_write))
5389 goto atapi_check;
5390
5391 if (unlikely(!bytes))
5392 goto atapi_check;
5393
5394 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
5395
5396 if (unlikely(__atapi_pio_bytes(qc, bytes)))
5397 goto err_out;
5398 ata_altstatus(ap); /* flush */
5399
5400 return;
5401
5402 atapi_check:
5403 ata_ehi_push_desc(ehi, "ATAPI check failed (ireason=0x%x bytes=%u)",
5404 ireason, bytes);
5405 err_out:
5406 qc->err_mask |= AC_ERR_HSM;
5407 ap->hsm_task_state = HSM_ST_ERR;
5408 }
5409
5410 /**
5411 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
5412 * @ap: the target ata_port
5413 * @qc: qc on going
5414 *
5415 * RETURNS:
5416 * 1 if ok in workqueue, 0 otherwise.
5417 */
5418
5419 static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
5420 {
5421 if (qc->tf.flags & ATA_TFLAG_POLLING)
5422 return 1;
5423
5424 if (ap->hsm_task_state == HSM_ST_FIRST) {
5425 if (qc->tf.protocol == ATA_PROT_PIO &&
5426 (qc->tf.flags & ATA_TFLAG_WRITE))
5427 return 1;
5428
5429 if (ata_is_atapi(qc->tf.protocol) &&
5430 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
5431 return 1;
5432 }
5433
5434 return 0;
5435 }
5436
5437 /**
5438 * ata_hsm_qc_complete - finish a qc running on standard HSM
5439 * @qc: Command to complete
5440 * @in_wq: 1 if called from workqueue, 0 otherwise
5441 *
5442 * Finish @qc which is running on standard HSM.
5443 *
5444 * LOCKING:
5445 * If @in_wq is zero, spin_lock_irqsave(host lock).
5446 * Otherwise, none on entry and grabs host lock.
5447 */
5448 static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
5449 {
5450 struct ata_port *ap = qc->ap;
5451 unsigned long flags;
5452
5453 if (ap->ops->error_handler) {
5454 if (in_wq) {
5455 spin_lock_irqsave(ap->lock, flags);
5456
5457 /* EH might have kicked in while host lock is
5458 * released.
5459 */
5460 qc = ata_qc_from_tag(ap, qc->tag);
5461 if (qc) {
5462 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
5463 ap->ops->irq_on(ap);
5464 ata_qc_complete(qc);
5465 } else
5466 ata_port_freeze(ap);
5467 }
5468
5469 spin_unlock_irqrestore(ap->lock, flags);
5470 } else {
5471 if (likely(!(qc->err_mask & AC_ERR_HSM)))
5472 ata_qc_complete(qc);
5473 else
5474 ata_port_freeze(ap);
5475 }
5476 } else {
5477 if (in_wq) {
5478 spin_lock_irqsave(ap->lock, flags);
5479 ap->ops->irq_on(ap);
5480 ata_qc_complete(qc);
5481 spin_unlock_irqrestore(ap->lock, flags);
5482 } else
5483 ata_qc_complete(qc);
5484 }
5485 }
5486
5487 /**
5488 * ata_hsm_move - move the HSM to the next state.
5489 * @ap: the target ata_port
5490 * @qc: qc on going
5491 * @status: current device status
5492 * @in_wq: 1 if called from workqueue, 0 otherwise
5493 *
5494 * RETURNS:
5495 * 1 when poll next status needed, 0 otherwise.
5496 */
5497 int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
5498 u8 status, int in_wq)
5499 {
5500 unsigned long flags = 0;
5501 int poll_next;
5502
5503 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
5504
5505 /* Make sure ata_qc_issue_prot() does not throw things
5506 * like DMA polling into the workqueue. Notice that
5507 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
5508 */
5509 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
5510
5511 fsm_start:
5512 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
5513 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
5514
5515 switch (ap->hsm_task_state) {
5516 case HSM_ST_FIRST:
5517 /* Send first data block or PACKET CDB */
5518
5519 /* If polling, we will stay in the work queue after
5520 * sending the data. Otherwise, interrupt handler
5521 * takes over after sending the data.
5522 */
5523 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
5524
5525 /* check device status */
5526 if (unlikely((status & ATA_DRQ) == 0)) {
5527 /* handle BSY=0, DRQ=0 as error */
5528 if (likely(status & (ATA_ERR | ATA_DF)))
5529 /* device stops HSM for abort/error */
5530 qc->err_mask |= AC_ERR_DEV;
5531 else
5532 /* HSM violation. Let EH handle this */
5533 qc->err_mask |= AC_ERR_HSM;
5534
5535 ap->hsm_task_state = HSM_ST_ERR;
5536 goto fsm_start;
5537 }
5538
5539 /* Device should not ask for data transfer (DRQ=1)
5540 * when it finds something wrong.
5541 * We ignore DRQ here and stop the HSM by
5542 * changing hsm_task_state to HSM_ST_ERR and
5543 * let the EH abort the command or reset the device.
5544 */
5545 if (unlikely(status & (ATA_ERR | ATA_DF))) {
5546 /* Some ATAPI tape drives forget to clear the ERR bit
5547 * when doing the next command (mostly request sense).
5548 * We ignore ERR here to workaround and proceed sending
5549 * the CDB.
5550 */
5551 if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
5552 ata_port_printk(ap, KERN_WARNING,
5553 "DRQ=1 with device error, "
5554 "dev_stat 0x%X\n", status);
5555 qc->err_mask |= AC_ERR_HSM;
5556 ap->hsm_task_state = HSM_ST_ERR;
5557 goto fsm_start;
5558 }
5559 }
5560
5561 /* Send the CDB (atapi) or the first data block (ata pio out).
5562 * During the state transition, interrupt handler shouldn't
5563 * be invoked before the data transfer is complete and
5564 * hsm_task_state is changed. Hence, the following locking.
5565 */
5566 if (in_wq)
5567 spin_lock_irqsave(ap->lock, flags);
5568
5569 if (qc->tf.protocol == ATA_PROT_PIO) {
5570 /* PIO data out protocol.
5571 * send first data block.
5572 */
5573
5574 /* ata_pio_sectors() might change the state
5575 * to HSM_ST_LAST. so, the state is changed here
5576 * before ata_pio_sectors().
5577 */
5578 ap->hsm_task_state = HSM_ST;
5579 ata_pio_sectors(qc);
5580 } else
5581 /* send CDB */
5582 atapi_send_cdb(ap, qc);
5583
5584 if (in_wq)
5585 spin_unlock_irqrestore(ap->lock, flags);
5586
5587 /* if polling, ata_pio_task() handles the rest.
5588 * otherwise, interrupt handler takes over from here.
5589 */
5590 break;
5591
5592 case HSM_ST:
5593 /* complete command or read/write the data register */
5594 if (qc->tf.protocol == ATAPI_PROT_PIO) {
5595 /* ATAPI PIO protocol */
5596 if ((status & ATA_DRQ) == 0) {
5597 /* No more data to transfer or device error.
5598 * Device error will be tagged in HSM_ST_LAST.
5599 */
5600 ap->hsm_task_state = HSM_ST_LAST;
5601 goto fsm_start;
5602 }
5603
5604 /* Device should not ask for data transfer (DRQ=1)
5605 * when it finds something wrong.
5606 * We ignore DRQ here and stop the HSM by
5607 * changing hsm_task_state to HSM_ST_ERR and
5608 * let the EH abort the command or reset the device.
5609 */
5610 if (unlikely(status & (ATA_ERR | ATA_DF))) {
5611 ata_port_printk(ap, KERN_WARNING, "DRQ=1 with "
5612 "device error, dev_stat 0x%X\n",
5613 status);
5614 qc->err_mask |= AC_ERR_HSM;
5615 ap->hsm_task_state = HSM_ST_ERR;
5616 goto fsm_start;
5617 }
5618
5619 atapi_pio_bytes(qc);
5620
5621 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
5622 /* bad ireason reported by device */
5623 goto fsm_start;
5624
5625 } else {
5626 /* ATA PIO protocol */
5627 if (unlikely((status & ATA_DRQ) == 0)) {
5628 /* handle BSY=0, DRQ=0 as error */
5629 if (likely(status & (ATA_ERR | ATA_DF)))
5630 /* device stops HSM for abort/error */
5631 qc->err_mask |= AC_ERR_DEV;
5632 else
5633 /* HSM violation. Let EH handle this.
5634 * Phantom devices also trigger this
5635 * condition. Mark hint.
5636 */
5637 qc->err_mask |= AC_ERR_HSM |
5638 AC_ERR_NODEV_HINT;
5639
5640 ap->hsm_task_state = HSM_ST_ERR;
5641 goto fsm_start;
5642 }
5643
5644 /* For PIO reads, some devices may ask for
5645 * data transfer (DRQ=1) alone with ERR=1.
5646 * We respect DRQ here and transfer one
5647 * block of junk data before changing the
5648 * hsm_task_state to HSM_ST_ERR.
5649 *
5650 * For PIO writes, ERR=1 DRQ=1 doesn't make
5651 * sense since the data block has been
5652 * transferred to the device.
5653 */
5654 if (unlikely(status & (ATA_ERR | ATA_DF))) {
5655 /* data might be corrputed */
5656 qc->err_mask |= AC_ERR_DEV;
5657
5658 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
5659 ata_pio_sectors(qc);
5660 status = ata_wait_idle(ap);
5661 }
5662
5663 if (status & (ATA_BUSY | ATA_DRQ))
5664 qc->err_mask |= AC_ERR_HSM;
5665
5666 /* ata_pio_sectors() might change the
5667 * state to HSM_ST_LAST. so, the state
5668 * is changed after ata_pio_sectors().
5669 */
5670 ap->hsm_task_state = HSM_ST_ERR;
5671 goto fsm_start;
5672 }
5673
5674 ata_pio_sectors(qc);
5675
5676 if (ap->hsm_task_state == HSM_ST_LAST &&
5677 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
5678 /* all data read */
5679 status = ata_wait_idle(ap);
5680 goto fsm_start;
5681 }
5682 }
5683
5684 poll_next = 1;
5685 break;
5686
5687 case HSM_ST_LAST:
5688 if (unlikely(!ata_ok(status))) {
5689 qc->err_mask |= __ac_err_mask(status);
5690 ap->hsm_task_state = HSM_ST_ERR;
5691 goto fsm_start;
5692 }
5693
5694 /* no more data to transfer */
5695 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
5696 ap->print_id, qc->dev->devno, status);
5697
5698 WARN_ON(qc->err_mask);
5699
5700 ap->hsm_task_state = HSM_ST_IDLE;
5701
5702 /* complete taskfile transaction */
5703 ata_hsm_qc_complete(qc, in_wq);
5704
5705 poll_next = 0;
5706 break;
5707
5708 case HSM_ST_ERR:
5709 /* make sure qc->err_mask is available to
5710 * know what's wrong and recover
5711 */
5712 WARN_ON(qc->err_mask == 0);
5713
5714 ap->hsm_task_state = HSM_ST_IDLE;
5715
5716 /* complete taskfile transaction */
5717 ata_hsm_qc_complete(qc, in_wq);
5718
5719 poll_next = 0;
5720 break;
5721 default:
5722 poll_next = 0;
5723 BUG();
5724 }
5725
5726 return poll_next;
5727 }
5728
5729 static void ata_pio_task(struct work_struct *work)
5730 {
5731 struct ata_port *ap =
5732 container_of(work, struct ata_port, port_task.work);
5733 struct ata_queued_cmd *qc = ap->port_task_data;
5734 u8 status;
5735 int poll_next;
5736
5737 fsm_start:
5738 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
5739
5740 /*
5741 * This is purely heuristic. This is a fast path.
5742 * Sometimes when we enter, BSY will be cleared in
5743 * a chk-status or two. If not, the drive is probably seeking
5744 * or something. Snooze for a couple msecs, then
5745 * chk-status again. If still busy, queue delayed work.
5746 */
5747 status = ata_busy_wait(ap, ATA_BUSY, 5);
5748 if (status & ATA_BUSY) {
5749 msleep(2);
5750 status = ata_busy_wait(ap, ATA_BUSY, 10);
5751 if (status & ATA_BUSY) {
5752 ata_pio_queue_task(ap, qc, ATA_SHORT_PAUSE);
5753 return;
5754 }
5755 }
5756
5757 /* move the HSM */
5758 poll_next = ata_hsm_move(ap, qc, status, 1);
5759
5760 /* another command or interrupt handler
5761 * may be running at this point.
5762 */
5763 if (poll_next)
5764 goto fsm_start;
5765 }
5766
5767 /**
5768 * ata_qc_new - Request an available ATA command, for queueing
5769 * @ap: Port associated with device @dev
5770 * @dev: Device from whom we request an available command structure
5771 *
5772 * LOCKING:
5773 * None.
5774 */
5775
5776 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
5777 {
5778 struct ata_queued_cmd *qc = NULL;
5779 unsigned int i;
5780
5781 /* no command while frozen */
5782 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
5783 return NULL;
5784
5785 /* the last tag is reserved for internal command. */
5786 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
5787 if (!test_and_set_bit(i, &ap->qc_allocated)) {
5788 qc = __ata_qc_from_tag(ap, i);
5789 break;
5790 }
5791
5792 if (qc)
5793 qc->tag = i;
5794
5795 return qc;
5796 }
5797
5798 /**
5799 * ata_qc_new_init - Request an available ATA command, and initialize it
5800 * @dev: Device from whom we request an available command structure
5801 *
5802 * LOCKING:
5803 * None.
5804 */
5805
5806 struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
5807 {
5808 struct ata_port *ap = dev->link->ap;
5809 struct ata_queued_cmd *qc;
5810
5811 qc = ata_qc_new(ap);
5812 if (qc) {
5813 qc->scsicmd = NULL;
5814 qc->ap = ap;
5815 qc->dev = dev;
5816
5817 ata_qc_reinit(qc);
5818 }
5819
5820 return qc;
5821 }
5822
5823 /**
5824 * ata_qc_free - free unused ata_queued_cmd
5825 * @qc: Command to complete
5826 *
5827 * Designed to free unused ata_queued_cmd object
5828 * in case something prevents using it.
5829 *
5830 * LOCKING:
5831 * spin_lock_irqsave(host lock)
5832 */
5833 void ata_qc_free(struct ata_queued_cmd *qc)
5834 {
5835 struct ata_port *ap = qc->ap;
5836 unsigned int tag;
5837
5838 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5839
5840 qc->flags = 0;
5841 tag = qc->tag;
5842 if (likely(ata_tag_valid(tag))) {
5843 qc->tag = ATA_TAG_POISON;
5844 clear_bit(tag, &ap->qc_allocated);
5845 }
5846 }
5847
5848 void __ata_qc_complete(struct ata_queued_cmd *qc)
5849 {
5850 struct ata_port *ap = qc->ap;
5851 struct ata_link *link = qc->dev->link;
5852
5853 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5854 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
5855
5856 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
5857 ata_sg_clean(qc);
5858
5859 /* command should be marked inactive atomically with qc completion */
5860 if (qc->tf.protocol == ATA_PROT_NCQ) {
5861 link->sactive &= ~(1 << qc->tag);
5862 if (!link->sactive)
5863 ap->nr_active_links--;
5864 } else {
5865 link->active_tag = ATA_TAG_POISON;
5866 ap->nr_active_links--;
5867 }
5868
5869 /* clear exclusive status */
5870 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
5871 ap->excl_link == link))
5872 ap->excl_link = NULL;
5873
5874 /* atapi: mark qc as inactive to prevent the interrupt handler
5875 * from completing the command twice later, before the error handler
5876 * is called. (when rc != 0 and atapi request sense is needed)
5877 */
5878 qc->flags &= ~ATA_QCFLAG_ACTIVE;
5879 ap->qc_active &= ~(1 << qc->tag);
5880
5881 /* call completion callback */
5882 qc->complete_fn(qc);
5883 }
5884
5885 static void fill_result_tf(struct ata_queued_cmd *qc)
5886 {
5887 struct ata_port *ap = qc->ap;
5888
5889 qc->result_tf.flags = qc->tf.flags;
5890 ap->ops->tf_read(ap, &qc->result_tf);
5891 }
5892
5893 static void ata_verify_xfer(struct ata_queued_cmd *qc)
5894 {
5895 struct ata_device *dev = qc->dev;
5896
5897 if (ata_tag_internal(qc->tag))
5898 return;
5899
5900 if (ata_is_nodata(qc->tf.protocol))
5901 return;
5902
5903 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
5904 return;
5905
5906 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
5907 }
5908
5909 /**
5910 * ata_qc_complete - Complete an active ATA command
5911 * @qc: Command to complete
5912 * @err_mask: ATA Status register contents
5913 *
5914 * Indicate to the mid and upper layers that an ATA
5915 * command has completed, with either an ok or not-ok status.
5916 *
5917 * LOCKING:
5918 * spin_lock_irqsave(host lock)
5919 */
5920 void ata_qc_complete(struct ata_queued_cmd *qc)
5921 {
5922 struct ata_port *ap = qc->ap;
5923
5924 /* XXX: New EH and old EH use different mechanisms to
5925 * synchronize EH with regular execution path.
5926 *
5927 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5928 * Normal execution path is responsible for not accessing a
5929 * failed qc. libata core enforces the rule by returning NULL
5930 * from ata_qc_from_tag() for failed qcs.
5931 *
5932 * Old EH depends on ata_qc_complete() nullifying completion
5933 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
5934 * not synchronize with interrupt handler. Only PIO task is
5935 * taken care of.
5936 */
5937 if (ap->ops->error_handler) {
5938 struct ata_device *dev = qc->dev;
5939 struct ata_eh_info *ehi = &dev->link->eh_info;
5940
5941 WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
5942
5943 if (unlikely(qc->err_mask))
5944 qc->flags |= ATA_QCFLAG_FAILED;
5945
5946 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
5947 if (!ata_tag_internal(qc->tag)) {
5948 /* always fill result TF for failed qc */
5949 fill_result_tf(qc);
5950 ata_qc_schedule_eh(qc);
5951 return;
5952 }
5953 }
5954
5955 /* read result TF if requested */
5956 if (qc->flags & ATA_QCFLAG_RESULT_TF)
5957 fill_result_tf(qc);
5958
5959 /* Some commands need post-processing after successful
5960 * completion.
5961 */
5962 switch (qc->tf.command) {
5963 case ATA_CMD_SET_FEATURES:
5964 if (qc->tf.feature != SETFEATURES_WC_ON &&
5965 qc->tf.feature != SETFEATURES_WC_OFF)
5966 break;
5967 /* fall through */
5968 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
5969 case ATA_CMD_SET_MULTI: /* multi_count changed */
5970 /* revalidate device */
5971 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
5972 ata_port_schedule_eh(ap);
5973 break;
5974
5975 case ATA_CMD_SLEEP:
5976 dev->flags |= ATA_DFLAG_SLEEPING;
5977 break;
5978 }
5979
5980 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
5981 ata_verify_xfer(qc);
5982
5983 __ata_qc_complete(qc);
5984 } else {
5985 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5986 return;
5987
5988 /* read result TF if failed or requested */
5989 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
5990 fill_result_tf(qc);
5991
5992 __ata_qc_complete(qc);
5993 }
5994 }
5995
5996 /**
5997 * ata_qc_complete_multiple - Complete multiple qcs successfully
5998 * @ap: port in question
5999 * @qc_active: new qc_active mask
6000 * @finish_qc: LLDD callback invoked before completing a qc
6001 *
6002 * Complete in-flight commands. This functions is meant to be
6003 * called from low-level driver's interrupt routine to complete
6004 * requests normally. ap->qc_active and @qc_active is compared
6005 * and commands are completed accordingly.
6006 *
6007 * LOCKING:
6008 * spin_lock_irqsave(host lock)
6009 *
6010 * RETURNS:
6011 * Number of completed commands on success, -errno otherwise.
6012 */
6013 int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
6014 void (*finish_qc)(struct ata_queued_cmd *))
6015 {
6016 int nr_done = 0;
6017 u32 done_mask;
6018 int i;
6019
6020 done_mask = ap->qc_active ^ qc_active;
6021
6022 if (unlikely(done_mask & qc_active)) {
6023 ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
6024 "(%08x->%08x)\n", ap->qc_active, qc_active);
6025 return -EINVAL;
6026 }
6027
6028 for (i = 0; i < ATA_MAX_QUEUE; i++) {
6029 struct ata_queued_cmd *qc;
6030
6031 if (!(done_mask & (1 << i)))
6032 continue;
6033
6034 if ((qc = ata_qc_from_tag(ap, i))) {
6035 if (finish_qc)
6036 finish_qc(qc);
6037 ata_qc_complete(qc);
6038 nr_done++;
6039 }
6040 }
6041
6042 return nr_done;
6043 }
6044
6045 /**
6046 * ata_qc_issue - issue taskfile to device
6047 * @qc: command to issue to device
6048 *
6049 * Prepare an ATA command to submission to device.
6050 * This includes mapping the data into a DMA-able
6051 * area, filling in the S/G table, and finally
6052 * writing the taskfile to hardware, starting the command.
6053 *
6054 * LOCKING:
6055 * spin_lock_irqsave(host lock)
6056 */
6057 void ata_qc_issue(struct ata_queued_cmd *qc)
6058 {
6059 struct ata_port *ap = qc->ap;
6060 struct ata_link *link = qc->dev->link;
6061 u8 prot = qc->tf.protocol;
6062
6063 /* Make sure only one non-NCQ command is outstanding. The
6064 * check is skipped for old EH because it reuses active qc to
6065 * request ATAPI sense.
6066 */
6067 WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
6068
6069 if (ata_is_ncq(prot)) {
6070 WARN_ON(link->sactive & (1 << qc->tag));
6071
6072 if (!link->sactive)
6073 ap->nr_active_links++;
6074 link->sactive |= 1 << qc->tag;
6075 } else {
6076 WARN_ON(link->sactive);
6077
6078 ap->nr_active_links++;
6079 link->active_tag = qc->tag;
6080 }
6081
6082 qc->flags |= ATA_QCFLAG_ACTIVE;
6083 ap->qc_active |= 1 << qc->tag;
6084
6085 /* We guarantee to LLDs that they will have at least one
6086 * non-zero sg if the command is a data command.
6087 */
6088 BUG_ON(ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes));
6089
6090 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
6091 (ap->flags & ATA_FLAG_PIO_DMA)))
6092 if (ata_sg_setup(qc))
6093 goto sg_err;
6094
6095 /* if device is sleeping, schedule reset and abort the link */
6096 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
6097 link->eh_info.action |= ATA_EH_RESET;
6098 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
6099 ata_link_abort(link);
6100 return;
6101 }
6102
6103 ap->ops->qc_prep(qc);
6104
6105 qc->err_mask |= ap->ops->qc_issue(qc);
6106 if (unlikely(qc->err_mask))
6107 goto err;
6108 return;
6109
6110 sg_err:
6111 qc->err_mask |= AC_ERR_SYSTEM;
6112 err:
6113 ata_qc_complete(qc);
6114 }
6115
6116 /**
6117 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
6118 * @qc: command to issue to device
6119 *
6120 * Using various libata functions and hooks, this function
6121 * starts an ATA command. ATA commands are grouped into
6122 * classes called "protocols", and issuing each type of protocol
6123 * is slightly different.
6124 *
6125 * May be used as the qc_issue() entry in ata_port_operations.
6126 *
6127 * LOCKING:
6128 * spin_lock_irqsave(host lock)
6129 *
6130 * RETURNS:
6131 * Zero on success, AC_ERR_* mask on failure
6132 */
6133
6134 unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
6135 {
6136 struct ata_port *ap = qc->ap;
6137
6138 /* Use polling pio if the LLD doesn't handle
6139 * interrupt driven pio and atapi CDB interrupt.
6140 */
6141 if (ap->flags & ATA_FLAG_PIO_POLLING) {
6142 switch (qc->tf.protocol) {
6143 case ATA_PROT_PIO:
6144 case ATA_PROT_NODATA:
6145 case ATAPI_PROT_PIO:
6146 case ATAPI_PROT_NODATA:
6147 qc->tf.flags |= ATA_TFLAG_POLLING;
6148 break;
6149 case ATAPI_PROT_DMA:
6150 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
6151 /* see ata_dma_blacklisted() */
6152 BUG();
6153 break;
6154 default:
6155 break;
6156 }
6157 }
6158
6159 /* select the device */
6160 ata_dev_select(ap, qc->dev->devno, 1, 0);
6161
6162 /* start the command */
6163 switch (qc->tf.protocol) {
6164 case ATA_PROT_NODATA:
6165 if (qc->tf.flags & ATA_TFLAG_POLLING)
6166 ata_qc_set_polling(qc);
6167
6168 ata_tf_to_host(ap, &qc->tf);
6169 ap->hsm_task_state = HSM_ST_LAST;
6170
6171 if (qc->tf.flags & ATA_TFLAG_POLLING)
6172 ata_pio_queue_task(ap, qc, 0);
6173
6174 break;
6175
6176 case ATA_PROT_DMA:
6177 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
6178
6179 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
6180 ap->ops->bmdma_setup(qc); /* set up bmdma */
6181 ap->ops->bmdma_start(qc); /* initiate bmdma */
6182 ap->hsm_task_state = HSM_ST_LAST;
6183 break;
6184
6185 case ATA_PROT_PIO:
6186 if (qc->tf.flags & ATA_TFLAG_POLLING)
6187 ata_qc_set_polling(qc);
6188
6189 ata_tf_to_host(ap, &qc->tf);
6190
6191 if (qc->tf.flags & ATA_TFLAG_WRITE) {
6192 /* PIO data out protocol */
6193 ap->hsm_task_state = HSM_ST_FIRST;
6194 ata_pio_queue_task(ap, qc, 0);
6195
6196 /* always send first data block using
6197 * the ata_pio_task() codepath.
6198 */
6199 } else {
6200 /* PIO data in protocol */
6201 ap->hsm_task_state = HSM_ST;
6202
6203 if (qc->tf.flags & ATA_TFLAG_POLLING)
6204 ata_pio_queue_task(ap, qc, 0);
6205
6206 /* if polling, ata_pio_task() handles the rest.
6207 * otherwise, interrupt handler takes over from here.
6208 */
6209 }
6210
6211 break;
6212
6213 case ATAPI_PROT_PIO:
6214 case ATAPI_PROT_NODATA:
6215 if (qc->tf.flags & ATA_TFLAG_POLLING)
6216 ata_qc_set_polling(qc);
6217
6218 ata_tf_to_host(ap, &qc->tf);
6219
6220 ap->hsm_task_state = HSM_ST_FIRST;
6221
6222 /* send cdb by polling if no cdb interrupt */
6223 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
6224 (qc->tf.flags & ATA_TFLAG_POLLING))
6225 ata_pio_queue_task(ap, qc, 0);
6226 break;
6227
6228 case ATAPI_PROT_DMA:
6229 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
6230
6231 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
6232 ap->ops->bmdma_setup(qc); /* set up bmdma */
6233 ap->hsm_task_state = HSM_ST_FIRST;
6234
6235 /* send cdb by polling if no cdb interrupt */
6236 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
6237 ata_pio_queue_task(ap, qc, 0);
6238 break;
6239
6240 default:
6241 WARN_ON(1);
6242 return AC_ERR_SYSTEM;
6243 }
6244
6245 return 0;
6246 }
6247
6248 /**
6249 * ata_host_intr - Handle host interrupt for given (port, task)
6250 * @ap: Port on which interrupt arrived (possibly...)
6251 * @qc: Taskfile currently active in engine
6252 *
6253 * Handle host interrupt for given queued command. Currently,
6254 * only DMA interrupts are handled. All other commands are
6255 * handled via polling with interrupts disabled (nIEN bit).
6256 *
6257 * LOCKING:
6258 * spin_lock_irqsave(host lock)
6259 *
6260 * RETURNS:
6261 * One if interrupt was handled, zero if not (shared irq).
6262 */
6263
6264 inline unsigned int ata_host_intr(struct ata_port *ap,
6265 struct ata_queued_cmd *qc)
6266 {
6267 struct ata_eh_info *ehi = &ap->link.eh_info;
6268 u8 status, host_stat = 0;
6269
6270 VPRINTK("ata%u: protocol %d task_state %d\n",
6271 ap->print_id, qc->tf.protocol, ap->hsm_task_state);
6272
6273 /* Check whether we are expecting interrupt in this state */
6274 switch (ap->hsm_task_state) {
6275 case HSM_ST_FIRST:
6276 /* Some pre-ATAPI-4 devices assert INTRQ
6277 * at this state when ready to receive CDB.
6278 */
6279
6280 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
6281 * The flag was turned on only for atapi devices. No
6282 * need to check ata_is_atapi(qc->tf.protocol) again.
6283 */
6284 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
6285 goto idle_irq;
6286 break;
6287 case HSM_ST_LAST:
6288 if (qc->tf.protocol == ATA_PROT_DMA ||
6289 qc->tf.protocol == ATAPI_PROT_DMA) {
6290 /* check status of DMA engine */
6291 host_stat = ap->ops->bmdma_status(ap);
6292 VPRINTK("ata%u: host_stat 0x%X\n",
6293 ap->print_id, host_stat);
6294
6295 /* if it's not our irq... */
6296 if (!(host_stat & ATA_DMA_INTR))
6297 goto idle_irq;
6298
6299 /* before we do anything else, clear DMA-Start bit */
6300 ap->ops->bmdma_stop(qc);
6301
6302 if (unlikely(host_stat & ATA_DMA_ERR)) {
6303 /* error when transfering data to/from memory */
6304 qc->err_mask |= AC_ERR_HOST_BUS;
6305 ap->hsm_task_state = HSM_ST_ERR;
6306 }
6307 }
6308 break;
6309 case HSM_ST:
6310 break;
6311 default:
6312 goto idle_irq;
6313 }
6314
6315 /* check altstatus */
6316 status = ata_altstatus(ap);
6317 if (status & ATA_BUSY)
6318 goto idle_irq;
6319
6320 /* check main status, clearing INTRQ */
6321 status = ata_chk_status(ap);
6322 if (unlikely(status & ATA_BUSY))
6323 goto idle_irq;
6324
6325 /* ack bmdma irq events */
6326 ap->ops->irq_clear(ap);
6327
6328 ata_hsm_move(ap, qc, status, 0);
6329
6330 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
6331 qc->tf.protocol == ATAPI_PROT_DMA))
6332 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
6333
6334 return 1; /* irq handled */
6335
6336 idle_irq:
6337 ap->stats.idle_irq++;
6338
6339 #ifdef ATA_IRQ_TRAP
6340 if ((ap->stats.idle_irq % 1000) == 0) {
6341 ata_chk_status(ap);
6342 ap->ops->irq_clear(ap);
6343 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
6344 return 1;
6345 }
6346 #endif
6347 return 0; /* irq not handled */
6348 }
6349
6350 /**
6351 * ata_interrupt - Default ATA host interrupt handler
6352 * @irq: irq line (unused)
6353 * @dev_instance: pointer to our ata_host information structure
6354 *
6355 * Default interrupt handler for PCI IDE devices. Calls
6356 * ata_host_intr() for each port that is not disabled.
6357 *
6358 * LOCKING:
6359 * Obtains host lock during operation.
6360 *
6361 * RETURNS:
6362 * IRQ_NONE or IRQ_HANDLED.
6363 */
6364
6365 irqreturn_t ata_interrupt(int irq, void *dev_instance)
6366 {
6367 struct ata_host *host = dev_instance;
6368 unsigned int i;
6369 unsigned int handled = 0;
6370 unsigned long flags;
6371
6372 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
6373 spin_lock_irqsave(&host->lock, flags);
6374
6375 for (i = 0; i < host->n_ports; i++) {
6376 struct ata_port *ap;
6377
6378 ap = host->ports[i];
6379 if (ap &&
6380 !(ap->flags & ATA_FLAG_DISABLED)) {
6381 struct ata_queued_cmd *qc;
6382
6383 qc = ata_qc_from_tag(ap, ap->link.active_tag);
6384 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
6385 (qc->flags & ATA_QCFLAG_ACTIVE))
6386 handled |= ata_host_intr(ap, qc);
6387 }
6388 }
6389
6390 spin_unlock_irqrestore(&host->lock, flags);
6391
6392 return IRQ_RETVAL(handled);
6393 }
6394
6395 /**
6396 * sata_scr_valid - test whether SCRs are accessible
6397 * @link: ATA link to test SCR accessibility for
6398 *
6399 * Test whether SCRs are accessible for @link.
6400 *
6401 * LOCKING:
6402 * None.
6403 *
6404 * RETURNS:
6405 * 1 if SCRs are accessible, 0 otherwise.
6406 */
6407 int sata_scr_valid(struct ata_link *link)
6408 {
6409 struct ata_port *ap = link->ap;
6410
6411 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
6412 }
6413
6414 /**
6415 * sata_scr_read - read SCR register of the specified port
6416 * @link: ATA link to read SCR for
6417 * @reg: SCR to read
6418 * @val: Place to store read value
6419 *
6420 * Read SCR register @reg of @link into *@val. This function is
6421 * guaranteed to succeed if @link is ap->link, the cable type of
6422 * the port is SATA and the port implements ->scr_read.
6423 *
6424 * LOCKING:
6425 * None if @link is ap->link. Kernel thread context otherwise.
6426 *
6427 * RETURNS:
6428 * 0 on success, negative errno on failure.
6429 */
6430 int sata_scr_read(struct ata_link *link, int reg, u32 *val)
6431 {
6432 if (ata_is_host_link(link)) {
6433 struct ata_port *ap = link->ap;
6434
6435 if (sata_scr_valid(link))
6436 return ap->ops->scr_read(ap, reg, val);
6437 return -EOPNOTSUPP;
6438 }
6439
6440 return sata_pmp_scr_read(link, reg, val);
6441 }
6442
6443 /**
6444 * sata_scr_write - write SCR register of the specified port
6445 * @link: ATA link to write SCR for
6446 * @reg: SCR to write
6447 * @val: value to write
6448 *
6449 * Write @val to SCR register @reg of @link. This function is
6450 * guaranteed to succeed if @link is ap->link, the cable type of
6451 * the port is SATA and the port implements ->scr_read.
6452 *
6453 * LOCKING:
6454 * None if @link is ap->link. Kernel thread context otherwise.
6455 *
6456 * RETURNS:
6457 * 0 on success, negative errno on failure.
6458 */
6459 int sata_scr_write(struct ata_link *link, int reg, u32 val)
6460 {
6461 if (ata_is_host_link(link)) {
6462 struct ata_port *ap = link->ap;
6463
6464 if (sata_scr_valid(link))
6465 return ap->ops->scr_write(ap, reg, val);
6466 return -EOPNOTSUPP;
6467 }
6468
6469 return sata_pmp_scr_write(link, reg, val);
6470 }
6471
6472 /**
6473 * sata_scr_write_flush - write SCR register of the specified port and flush
6474 * @link: ATA link to write SCR for
6475 * @reg: SCR to write
6476 * @val: value to write
6477 *
6478 * This function is identical to sata_scr_write() except that this
6479 * function performs flush after writing to the register.
6480 *
6481 * LOCKING:
6482 * None if @link is ap->link. Kernel thread context otherwise.
6483 *
6484 * RETURNS:
6485 * 0 on success, negative errno on failure.
6486 */
6487 int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
6488 {
6489 if (ata_is_host_link(link)) {
6490 struct ata_port *ap = link->ap;
6491 int rc;
6492
6493 if (sata_scr_valid(link)) {
6494 rc = ap->ops->scr_write(ap, reg, val);
6495 if (rc == 0)
6496 rc = ap->ops->scr_read(ap, reg, &val);
6497 return rc;
6498 }
6499 return -EOPNOTSUPP;
6500 }
6501
6502 return sata_pmp_scr_write(link, reg, val);
6503 }
6504
6505 /**
6506 * ata_link_online - test whether the given link is online
6507 * @link: ATA link to test
6508 *
6509 * Test whether @link is online. Note that this function returns
6510 * 0 if online status of @link cannot be obtained, so
6511 * ata_link_online(link) != !ata_link_offline(link).
6512 *
6513 * LOCKING:
6514 * None.
6515 *
6516 * RETURNS:
6517 * 1 if the port online status is available and online.
6518 */
6519 int ata_link_online(struct ata_link *link)
6520 {
6521 u32 sstatus;
6522
6523 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
6524 (sstatus & 0xf) == 0x3)
6525 return 1;
6526 return 0;
6527 }
6528
6529 /**
6530 * ata_link_offline - test whether the given link is offline
6531 * @link: ATA link to test
6532 *
6533 * Test whether @link is offline. Note that this function
6534 * returns 0 if offline status of @link cannot be obtained, so
6535 * ata_link_online(link) != !ata_link_offline(link).
6536 *
6537 * LOCKING:
6538 * None.
6539 *
6540 * RETURNS:
6541 * 1 if the port offline status is available and offline.
6542 */
6543 int ata_link_offline(struct ata_link *link)
6544 {
6545 u32 sstatus;
6546
6547 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
6548 (sstatus & 0xf) != 0x3)
6549 return 1;
6550 return 0;
6551 }
6552
6553 int ata_flush_cache(struct ata_device *dev)
6554 {
6555 unsigned int err_mask;
6556 u8 cmd;
6557
6558 if (!ata_try_flush_cache(dev))
6559 return 0;
6560
6561 if (dev->flags & ATA_DFLAG_FLUSH_EXT)
6562 cmd = ATA_CMD_FLUSH_EXT;
6563 else
6564 cmd = ATA_CMD_FLUSH;
6565
6566 /* This is wrong. On a failed flush we get back the LBA of the lost
6567 sector and we should (assuming it wasn't aborted as unknown) issue
6568 a further flush command to continue the writeback until it
6569 does not error */
6570 err_mask = ata_do_simple_cmd(dev, cmd);
6571 if (err_mask) {
6572 ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
6573 return -EIO;
6574 }
6575
6576 return 0;
6577 }
6578
6579 #ifdef CONFIG_PM
6580 static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
6581 unsigned int action, unsigned int ehi_flags,
6582 int wait)
6583 {
6584 unsigned long flags;
6585 int i, rc;
6586
6587 for (i = 0; i < host->n_ports; i++) {
6588 struct ata_port *ap = host->ports[i];
6589 struct ata_link *link;
6590
6591 /* Previous resume operation might still be in
6592 * progress. Wait for PM_PENDING to clear.
6593 */
6594 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
6595 ata_port_wait_eh(ap);
6596 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
6597 }
6598
6599 /* request PM ops to EH */
6600 spin_lock_irqsave(ap->lock, flags);
6601
6602 ap->pm_mesg = mesg;
6603 if (wait) {
6604 rc = 0;
6605 ap->pm_result = &rc;
6606 }
6607
6608 ap->pflags |= ATA_PFLAG_PM_PENDING;
6609 __ata_port_for_each_link(link, ap) {
6610 link->eh_info.action |= action;
6611 link->eh_info.flags |= ehi_flags;
6612 }
6613
6614 ata_port_schedule_eh(ap);
6615
6616 spin_unlock_irqrestore(ap->lock, flags);
6617
6618 /* wait and check result */
6619 if (wait) {
6620 ata_port_wait_eh(ap);
6621 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
6622 if (rc)
6623 return rc;
6624 }
6625 }
6626
6627 return 0;
6628 }
6629
6630 /**
6631 * ata_host_suspend - suspend host
6632 * @host: host to suspend
6633 * @mesg: PM message
6634 *
6635 * Suspend @host. Actual operation is performed by EH. This
6636 * function requests EH to perform PM operations and waits for EH
6637 * to finish.
6638 *
6639 * LOCKING:
6640 * Kernel thread context (may sleep).
6641 *
6642 * RETURNS:
6643 * 0 on success, -errno on failure.
6644 */
6645 int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
6646 {
6647 int rc;
6648
6649 /*
6650 * disable link pm on all ports before requesting
6651 * any pm activity
6652 */
6653 ata_lpm_enable(host);
6654
6655 rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
6656 if (rc == 0)
6657 host->dev->power.power_state = mesg;
6658 return rc;
6659 }
6660
6661 /**
6662 * ata_host_resume - resume host
6663 * @host: host to resume
6664 *
6665 * Resume @host. Actual operation is performed by EH. This
6666 * function requests EH to perform PM operations and returns.
6667 * Note that all resume operations are performed parallely.
6668 *
6669 * LOCKING:
6670 * Kernel thread context (may sleep).
6671 */
6672 void ata_host_resume(struct ata_host *host)
6673 {
6674 ata_host_request_pm(host, PMSG_ON, ATA_EH_RESET,
6675 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
6676 host->dev->power.power_state = PMSG_ON;
6677
6678 /* reenable link pm */
6679 ata_lpm_disable(host);
6680 }
6681 #endif
6682
6683 /**
6684 * ata_port_start - Set port up for dma.
6685 * @ap: Port to initialize
6686 *
6687 * Called just after data structures for each port are
6688 * initialized. Allocates space for PRD table.
6689 *
6690 * May be used as the port_start() entry in ata_port_operations.
6691 *
6692 * LOCKING:
6693 * Inherited from caller.
6694 */
6695 int ata_port_start(struct ata_port *ap)
6696 {
6697 struct device *dev = ap->dev;
6698
6699 ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
6700 GFP_KERNEL);
6701 if (!ap->prd)
6702 return -ENOMEM;
6703
6704 return 0;
6705 }
6706
6707 /**
6708 * ata_dev_init - Initialize an ata_device structure
6709 * @dev: Device structure to initialize
6710 *
6711 * Initialize @dev in preparation for probing.
6712 *
6713 * LOCKING:
6714 * Inherited from caller.
6715 */
6716 void ata_dev_init(struct ata_device *dev)
6717 {
6718 struct ata_link *link = dev->link;
6719 struct ata_port *ap = link->ap;
6720 unsigned long flags;
6721
6722 /* SATA spd limit is bound to the first device */
6723 link->sata_spd_limit = link->hw_sata_spd_limit;
6724 link->sata_spd = 0;
6725
6726 /* High bits of dev->flags are used to record warm plug
6727 * requests which occur asynchronously. Synchronize using
6728 * host lock.
6729 */
6730 spin_lock_irqsave(ap->lock, flags);
6731 dev->flags &= ~ATA_DFLAG_INIT_MASK;
6732 dev->horkage = 0;
6733 spin_unlock_irqrestore(ap->lock, flags);
6734
6735 memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
6736 sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
6737 dev->pio_mask = UINT_MAX;
6738 dev->mwdma_mask = UINT_MAX;
6739 dev->udma_mask = UINT_MAX;
6740 }
6741
6742 /**
6743 * ata_link_init - Initialize an ata_link structure
6744 * @ap: ATA port link is attached to
6745 * @link: Link structure to initialize
6746 * @pmp: Port multiplier port number
6747 *
6748 * Initialize @link.
6749 *
6750 * LOCKING:
6751 * Kernel thread context (may sleep)
6752 */
6753 void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
6754 {
6755 int i;
6756
6757 /* clear everything except for devices */
6758 memset(link, 0, offsetof(struct ata_link, device[0]));
6759
6760 link->ap = ap;
6761 link->pmp = pmp;
6762 link->active_tag = ATA_TAG_POISON;
6763 link->hw_sata_spd_limit = UINT_MAX;
6764
6765 /* can't use iterator, ap isn't initialized yet */
6766 for (i = 0; i < ATA_MAX_DEVICES; i++) {
6767 struct ata_device *dev = &link->device[i];
6768
6769 dev->link = link;
6770 dev->devno = dev - link->device;
6771 ata_dev_init(dev);
6772 }
6773 }
6774
6775 /**
6776 * sata_link_init_spd - Initialize link->sata_spd_limit
6777 * @link: Link to configure sata_spd_limit for
6778 *
6779 * Initialize @link->[hw_]sata_spd_limit to the currently
6780 * configured value.
6781 *
6782 * LOCKING:
6783 * Kernel thread context (may sleep).
6784 *
6785 * RETURNS:
6786 * 0 on success, -errno on failure.
6787 */
6788 int sata_link_init_spd(struct ata_link *link)
6789 {
6790 u32 scontrol;
6791 u8 spd;
6792 int rc;
6793
6794 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
6795 if (rc)
6796 return rc;
6797
6798 spd = (scontrol >> 4) & 0xf;
6799 if (spd)
6800 link->hw_sata_spd_limit &= (1 << spd) - 1;
6801
6802 ata_force_spd_limit(link);
6803
6804 link->sata_spd_limit = link->hw_sata_spd_limit;
6805
6806 return 0;
6807 }
6808
6809 /**
6810 * ata_port_alloc - allocate and initialize basic ATA port resources
6811 * @host: ATA host this allocated port belongs to
6812 *
6813 * Allocate and initialize basic ATA port resources.
6814 *
6815 * RETURNS:
6816 * Allocate ATA port on success, NULL on failure.
6817 *
6818 * LOCKING:
6819 * Inherited from calling layer (may sleep).
6820 */
6821 struct ata_port *ata_port_alloc(struct ata_host *host)
6822 {
6823 struct ata_port *ap;
6824
6825 DPRINTK("ENTER\n");
6826
6827 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
6828 if (!ap)
6829 return NULL;
6830
6831 ap->pflags |= ATA_PFLAG_INITIALIZING;
6832 ap->lock = &host->lock;
6833 ap->flags = ATA_FLAG_DISABLED;
6834 ap->print_id = -1;
6835 ap->ctl = ATA_DEVCTL_OBS;
6836 ap->host = host;
6837 ap->dev = host->dev;
6838 ap->last_ctl = 0xFF;
6839
6840 #if defined(ATA_VERBOSE_DEBUG)
6841 /* turn on all debugging levels */
6842 ap->msg_enable = 0x00FF;
6843 #elif defined(ATA_DEBUG)
6844 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
6845 #else
6846 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
6847 #endif
6848
6849 INIT_DELAYED_WORK(&ap->port_task, ata_pio_task);
6850 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
6851 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
6852 INIT_LIST_HEAD(&ap->eh_done_q);
6853 init_waitqueue_head(&ap->eh_wait_q);
6854 init_timer_deferrable(&ap->fastdrain_timer);
6855 ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
6856 ap->fastdrain_timer.data = (unsigned long)ap;
6857
6858 ap->cbl = ATA_CBL_NONE;
6859
6860 ata_link_init(ap, &ap->link, 0);
6861
6862 #ifdef ATA_IRQ_TRAP
6863 ap->stats.unhandled_irq = 1;
6864 ap->stats.idle_irq = 1;
6865 #endif
6866 return ap;
6867 }
6868
6869 static void ata_host_release(struct device *gendev, void *res)
6870 {
6871 struct ata_host *host = dev_get_drvdata(gendev);
6872 int i;
6873
6874 for (i = 0; i < host->n_ports; i++) {
6875 struct ata_port *ap = host->ports[i];
6876
6877 if (!ap)
6878 continue;
6879
6880 if (ap->scsi_host)
6881 scsi_host_put(ap->scsi_host);
6882
6883 kfree(ap->pmp_link);
6884 kfree(ap);
6885 host->ports[i] = NULL;
6886 }
6887
6888 dev_set_drvdata(gendev, NULL);
6889 }
6890
6891 /**
6892 * ata_host_alloc - allocate and init basic ATA host resources
6893 * @dev: generic device this host is associated with
6894 * @max_ports: maximum number of ATA ports associated with this host
6895 *
6896 * Allocate and initialize basic ATA host resources. LLD calls
6897 * this function to allocate a host, initializes it fully and
6898 * attaches it using ata_host_register().
6899 *
6900 * @max_ports ports are allocated and host->n_ports is
6901 * initialized to @max_ports. The caller is allowed to decrease
6902 * host->n_ports before calling ata_host_register(). The unused
6903 * ports will be automatically freed on registration.
6904 *
6905 * RETURNS:
6906 * Allocate ATA host on success, NULL on failure.
6907 *
6908 * LOCKING:
6909 * Inherited from calling layer (may sleep).
6910 */
6911 struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
6912 {
6913 struct ata_host *host;
6914 size_t sz;
6915 int i;
6916
6917 DPRINTK("ENTER\n");
6918
6919 if (!devres_open_group(dev, NULL, GFP_KERNEL))
6920 return NULL;
6921
6922 /* alloc a container for our list of ATA ports (buses) */
6923 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
6924 /* alloc a container for our list of ATA ports (buses) */
6925 host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
6926 if (!host)
6927 goto err_out;
6928
6929 devres_add(dev, host);
6930 dev_set_drvdata(dev, host);
6931
6932 spin_lock_init(&host->lock);
6933 host->dev = dev;
6934 host->n_ports = max_ports;
6935
6936 /* allocate ports bound to this host */
6937 for (i = 0; i < max_ports; i++) {
6938 struct ata_port *ap;
6939
6940 ap = ata_port_alloc(host);
6941 if (!ap)
6942 goto err_out;
6943
6944 ap->port_no = i;
6945 host->ports[i] = ap;
6946 }
6947
6948 devres_remove_group(dev, NULL);
6949 return host;
6950
6951 err_out:
6952 devres_release_group(dev, NULL);
6953 return NULL;
6954 }
6955
6956 /**
6957 * ata_host_alloc_pinfo - alloc host and init with port_info array
6958 * @dev: generic device this host is associated with
6959 * @ppi: array of ATA port_info to initialize host with
6960 * @n_ports: number of ATA ports attached to this host
6961 *
6962 * Allocate ATA host and initialize with info from @ppi. If NULL
6963 * terminated, @ppi may contain fewer entries than @n_ports. The
6964 * last entry will be used for the remaining ports.
6965 *
6966 * RETURNS:
6967 * Allocate ATA host on success, NULL on failure.
6968 *
6969 * LOCKING:
6970 * Inherited from calling layer (may sleep).
6971 */
6972 struct ata_host *ata_host_alloc_pinfo(struct device *dev,
6973 const struct ata_port_info * const * ppi,
6974 int n_ports)
6975 {
6976 const struct ata_port_info *pi;
6977 struct ata_host *host;
6978 int i, j;
6979
6980 host = ata_host_alloc(dev, n_ports);
6981 if (!host)
6982 return NULL;
6983
6984 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
6985 struct ata_port *ap = host->ports[i];
6986
6987 if (ppi[j])
6988 pi = ppi[j++];
6989
6990 ap->pio_mask = pi->pio_mask;
6991 ap->mwdma_mask = pi->mwdma_mask;
6992 ap->udma_mask = pi->udma_mask;
6993 ap->flags |= pi->flags;
6994 ap->link.flags |= pi->link_flags;
6995 ap->ops = pi->port_ops;
6996
6997 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
6998 host->ops = pi->port_ops;
6999 if (!host->private_data && pi->private_data)
7000 host->private_data = pi->private_data;
7001 }
7002
7003 return host;
7004 }
7005
7006 static void ata_host_stop(struct device *gendev, void *res)
7007 {
7008 struct ata_host *host = dev_get_drvdata(gendev);
7009 int i;
7010
7011 WARN_ON(!(host->flags & ATA_HOST_STARTED));
7012
7013 for (i = 0; i < host->n_ports; i++) {
7014 struct ata_port *ap = host->ports[i];
7015
7016 if (ap->ops->port_stop)
7017 ap->ops->port_stop(ap);
7018 }
7019
7020 if (host->ops->host_stop)
7021 host->ops->host_stop(host);
7022 }
7023
7024 /**
7025 * ata_finalize_port_ops - finalize ata_port_operations
7026 * @ops: ata_port_operations to finalize
7027 *
7028 * An ata_port_operations can inherit from another ops and that
7029 * ops can again inherit from another. This can go on as many
7030 * times as necessary as long as there is no loop in the
7031 * inheritance chain.
7032 *
7033 * Ops tables are finalized when the host is started. NULL or
7034 * unspecified entries are inherited from the closet ancestor
7035 * which has the method and the entry is populated with it.
7036 * After finalization, the ops table directly points to all the
7037 * methods and ->inherits is no longer necessary and cleared.
7038 *
7039 * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
7040 *
7041 * LOCKING:
7042 * None.
7043 */
7044 static void ata_finalize_port_ops(struct ata_port_operations *ops)
7045 {
7046 static spinlock_t lock = SPIN_LOCK_UNLOCKED;
7047 const struct ata_port_operations *cur;
7048 void **begin = (void **)ops;
7049 void **end = (void **)&ops->inherits;
7050 void **pp;
7051
7052 if (!ops || !ops->inherits)
7053 return;
7054
7055 spin_lock(&lock);
7056
7057 for (cur = ops->inherits; cur; cur = cur->inherits) {
7058 void **inherit = (void **)cur;
7059
7060 for (pp = begin; pp < end; pp++, inherit++)
7061 if (!*pp)
7062 *pp = *inherit;
7063 }
7064
7065 for (pp = begin; pp < end; pp++)
7066 if (IS_ERR(*pp))
7067 *pp = NULL;
7068
7069 ops->inherits = NULL;
7070
7071 spin_unlock(&lock);
7072 }
7073
7074 /**
7075 * ata_host_start - start and freeze ports of an ATA host
7076 * @host: ATA host to start ports for
7077 *
7078 * Start and then freeze ports of @host. Started status is
7079 * recorded in host->flags, so this function can be called
7080 * multiple times. Ports are guaranteed to get started only
7081 * once. If host->ops isn't initialized yet, its set to the
7082 * first non-dummy port ops.
7083 *
7084 * LOCKING:
7085 * Inherited from calling layer (may sleep).
7086 *
7087 * RETURNS:
7088 * 0 if all ports are started successfully, -errno otherwise.
7089 */
7090 int ata_host_start(struct ata_host *host)
7091 {
7092 int have_stop = 0;
7093 void *start_dr = NULL;
7094 int i, rc;
7095
7096 if (host->flags & ATA_HOST_STARTED)
7097 return 0;
7098
7099 ata_finalize_port_ops(host->ops);
7100
7101 for (i = 0; i < host->n_ports; i++) {
7102 struct ata_port *ap = host->ports[i];
7103
7104 ata_finalize_port_ops(ap->ops);
7105
7106 if (!host->ops && !ata_port_is_dummy(ap))
7107 host->ops = ap->ops;
7108
7109 if (ap->ops->port_stop)
7110 have_stop = 1;
7111 }
7112
7113 if (host->ops->host_stop)
7114 have_stop = 1;
7115
7116 if (have_stop) {
7117 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
7118 if (!start_dr)
7119 return -ENOMEM;
7120 }
7121
7122 for (i = 0; i < host->n_ports; i++) {
7123 struct ata_port *ap = host->ports[i];
7124
7125 if (ap->ops->port_start) {
7126 rc = ap->ops->port_start(ap);
7127 if (rc) {
7128 if (rc != -ENODEV)
7129 dev_printk(KERN_ERR, host->dev,
7130 "failed to start port %d "
7131 "(errno=%d)\n", i, rc);
7132 goto err_out;
7133 }
7134 }
7135 ata_eh_freeze_port(ap);
7136 }
7137
7138 if (start_dr)
7139 devres_add(host->dev, start_dr);
7140 host->flags |= ATA_HOST_STARTED;
7141 return 0;
7142
7143 err_out:
7144 while (--i >= 0) {
7145 struct ata_port *ap = host->ports[i];
7146
7147 if (ap->ops->port_stop)
7148 ap->ops->port_stop(ap);
7149 }
7150 devres_free(start_dr);
7151 return rc;
7152 }
7153
7154 /**
7155 * ata_sas_host_init - Initialize a host struct
7156 * @host: host to initialize
7157 * @dev: device host is attached to
7158 * @flags: host flags
7159 * @ops: port_ops
7160 *
7161 * LOCKING:
7162 * PCI/etc. bus probe sem.
7163 *
7164 */
7165 /* KILLME - the only user left is ipr */
7166 void ata_host_init(struct ata_host *host, struct device *dev,
7167 unsigned long flags, struct ata_port_operations *ops)
7168 {
7169 spin_lock_init(&host->lock);
7170 host->dev = dev;
7171 host->flags = flags;
7172 host->ops = ops;
7173 }
7174
7175 /**
7176 * ata_host_register - register initialized ATA host
7177 * @host: ATA host to register
7178 * @sht: template for SCSI host
7179 *
7180 * Register initialized ATA host. @host is allocated using
7181 * ata_host_alloc() and fully initialized by LLD. This function
7182 * starts ports, registers @host with ATA and SCSI layers and
7183 * probe registered devices.
7184 *
7185 * LOCKING:
7186 * Inherited from calling layer (may sleep).
7187 *
7188 * RETURNS:
7189 * 0 on success, -errno otherwise.
7190 */
7191 int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
7192 {
7193 int i, rc;
7194
7195 /* host must have been started */
7196 if (!(host->flags & ATA_HOST_STARTED)) {
7197 dev_printk(KERN_ERR, host->dev,
7198 "BUG: trying to register unstarted host\n");
7199 WARN_ON(1);
7200 return -EINVAL;
7201 }
7202
7203 /* Blow away unused ports. This happens when LLD can't
7204 * determine the exact number of ports to allocate at
7205 * allocation time.
7206 */
7207 for (i = host->n_ports; host->ports[i]; i++)
7208 kfree(host->ports[i]);
7209
7210 /* give ports names and add SCSI hosts */
7211 for (i = 0; i < host->n_ports; i++)
7212 host->ports[i]->print_id = ata_print_id++;
7213
7214 rc = ata_scsi_add_hosts(host, sht);
7215 if (rc)
7216 return rc;
7217
7218 /* associate with ACPI nodes */
7219 ata_acpi_associate(host);
7220
7221 /* set cable, sata_spd_limit and report */
7222 for (i = 0; i < host->n_ports; i++) {
7223 struct ata_port *ap = host->ports[i];
7224 unsigned long xfer_mask;
7225
7226 /* set SATA cable type if still unset */
7227 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
7228 ap->cbl = ATA_CBL_SATA;
7229
7230 /* init sata_spd_limit to the current value */
7231 sata_link_init_spd(&ap->link);
7232
7233 /* print per-port info to dmesg */
7234 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
7235 ap->udma_mask);
7236
7237 if (!ata_port_is_dummy(ap)) {
7238 ata_port_printk(ap, KERN_INFO,
7239 "%cATA max %s %s\n",
7240 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
7241 ata_mode_string(xfer_mask),
7242 ap->link.eh_info.desc);
7243 ata_ehi_clear_desc(&ap->link.eh_info);
7244 } else
7245 ata_port_printk(ap, KERN_INFO, "DUMMY\n");
7246 }
7247
7248 /* perform each probe synchronously */
7249 DPRINTK("probe begin\n");
7250 for (i = 0; i < host->n_ports; i++) {
7251 struct ata_port *ap = host->ports[i];
7252
7253 /* probe */
7254 if (ap->ops->error_handler) {
7255 struct ata_eh_info *ehi = &ap->link.eh_info;
7256 unsigned long flags;
7257
7258 ata_port_probe(ap);
7259
7260 /* kick EH for boot probing */
7261 spin_lock_irqsave(ap->lock, flags);
7262
7263 ehi->probe_mask |= ATA_ALL_DEVICES;
7264 ehi->action |= ATA_EH_RESET;
7265 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
7266
7267 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
7268 ap->pflags |= ATA_PFLAG_LOADING;
7269 ata_port_schedule_eh(ap);
7270
7271 spin_unlock_irqrestore(ap->lock, flags);
7272
7273 /* wait for EH to finish */
7274 ata_port_wait_eh(ap);
7275 } else {
7276 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
7277 rc = ata_bus_probe(ap);
7278 DPRINTK("ata%u: bus probe end\n", ap->print_id);
7279
7280 if (rc) {
7281 /* FIXME: do something useful here?
7282 * Current libata behavior will
7283 * tear down everything when
7284 * the module is removed
7285 * or the h/w is unplugged.
7286 */
7287 }
7288 }
7289 }
7290
7291 /* probes are done, now scan each port's disk(s) */
7292 DPRINTK("host probe begin\n");
7293 for (i = 0; i < host->n_ports; i++) {
7294 struct ata_port *ap = host->ports[i];
7295
7296 ata_scsi_scan_host(ap, 1);
7297 ata_lpm_schedule(ap, ap->pm_policy);
7298 }
7299
7300 return 0;
7301 }
7302
7303 /**
7304 * ata_host_activate - start host, request IRQ and register it
7305 * @host: target ATA host
7306 * @irq: IRQ to request
7307 * @irq_handler: irq_handler used when requesting IRQ
7308 * @irq_flags: irq_flags used when requesting IRQ
7309 * @sht: scsi_host_template to use when registering the host
7310 *
7311 * After allocating an ATA host and initializing it, most libata
7312 * LLDs perform three steps to activate the host - start host,
7313 * request IRQ and register it. This helper takes necessasry
7314 * arguments and performs the three steps in one go.
7315 *
7316 * An invalid IRQ skips the IRQ registration and expects the host to
7317 * have set polling mode on the port. In this case, @irq_handler
7318 * should be NULL.
7319 *
7320 * LOCKING:
7321 * Inherited from calling layer (may sleep).
7322 *
7323 * RETURNS:
7324 * 0 on success, -errno otherwise.
7325 */
7326 int ata_host_activate(struct ata_host *host, int irq,
7327 irq_handler_t irq_handler, unsigned long irq_flags,
7328 struct scsi_host_template *sht)
7329 {
7330 int i, rc;
7331
7332 rc = ata_host_start(host);
7333 if (rc)
7334 return rc;
7335
7336 /* Special case for polling mode */
7337 if (!irq) {
7338 WARN_ON(irq_handler);
7339 return ata_host_register(host, sht);
7340 }
7341
7342 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
7343 dev_driver_string(host->dev), host);
7344 if (rc)
7345 return rc;
7346
7347 for (i = 0; i < host->n_ports; i++)
7348 ata_port_desc(host->ports[i], "irq %d", irq);
7349
7350 rc = ata_host_register(host, sht);
7351 /* if failed, just free the IRQ and leave ports alone */
7352 if (rc)
7353 devm_free_irq(host->dev, irq, host);
7354
7355 return rc;
7356 }
7357
7358 /**
7359 * ata_port_detach - Detach ATA port in prepration of device removal
7360 * @ap: ATA port to be detached
7361 *
7362 * Detach all ATA devices and the associated SCSI devices of @ap;
7363 * then, remove the associated SCSI host. @ap is guaranteed to
7364 * be quiescent on return from this function.
7365 *
7366 * LOCKING:
7367 * Kernel thread context (may sleep).
7368 */
7369 static void ata_port_detach(struct ata_port *ap)
7370 {
7371 unsigned long flags;
7372 struct ata_link *link;
7373 struct ata_device *dev;
7374
7375 if (!ap->ops->error_handler)
7376 goto skip_eh;
7377
7378 /* tell EH we're leaving & flush EH */
7379 spin_lock_irqsave(ap->lock, flags);
7380 ap->pflags |= ATA_PFLAG_UNLOADING;
7381 spin_unlock_irqrestore(ap->lock, flags);
7382
7383 ata_port_wait_eh(ap);
7384
7385 /* EH is now guaranteed to see UNLOADING - EH context belongs
7386 * to us. Disable all existing devices.
7387 */
7388 ata_port_for_each_link(link, ap) {
7389 ata_link_for_each_dev(dev, link)
7390 ata_dev_disable(dev);
7391 }
7392
7393 /* Final freeze & EH. All in-flight commands are aborted. EH
7394 * will be skipped and retrials will be terminated with bad
7395 * target.
7396 */
7397 spin_lock_irqsave(ap->lock, flags);
7398 ata_port_freeze(ap); /* won't be thawed */
7399 spin_unlock_irqrestore(ap->lock, flags);
7400
7401 ata_port_wait_eh(ap);
7402 cancel_rearming_delayed_work(&ap->hotplug_task);
7403
7404 skip_eh:
7405 /* remove the associated SCSI host */
7406 scsi_remove_host(ap->scsi_host);
7407 }
7408
7409 /**
7410 * ata_host_detach - Detach all ports of an ATA host
7411 * @host: Host to detach
7412 *
7413 * Detach all ports of @host.
7414 *
7415 * LOCKING:
7416 * Kernel thread context (may sleep).
7417 */
7418 void ata_host_detach(struct ata_host *host)
7419 {
7420 int i;
7421
7422 for (i = 0; i < host->n_ports; i++)
7423 ata_port_detach(host->ports[i]);
7424
7425 /* the host is dead now, dissociate ACPI */
7426 ata_acpi_dissociate(host);
7427 }
7428
7429 /**
7430 * ata_std_ports - initialize ioaddr with standard port offsets.
7431 * @ioaddr: IO address structure to be initialized
7432 *
7433 * Utility function which initializes data_addr, error_addr,
7434 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
7435 * device_addr, status_addr, and command_addr to standard offsets
7436 * relative to cmd_addr.
7437 *
7438 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
7439 */
7440
7441 void ata_std_ports(struct ata_ioports *ioaddr)
7442 {
7443 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
7444 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
7445 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
7446 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
7447 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
7448 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
7449 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
7450 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
7451 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
7452 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
7453 }
7454
7455
7456 #ifdef CONFIG_PCI
7457
7458 /**
7459 * ata_pci_remove_one - PCI layer callback for device removal
7460 * @pdev: PCI device that was removed
7461 *
7462 * PCI layer indicates to libata via this hook that hot-unplug or
7463 * module unload event has occurred. Detach all ports. Resource
7464 * release is handled via devres.
7465 *
7466 * LOCKING:
7467 * Inherited from PCI layer (may sleep).
7468 */
7469 void ata_pci_remove_one(struct pci_dev *pdev)
7470 {
7471 struct device *dev = &pdev->dev;
7472 struct ata_host *host = dev_get_drvdata(dev);
7473
7474 ata_host_detach(host);
7475 }
7476
7477 /* move to PCI subsystem */
7478 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
7479 {
7480 unsigned long tmp = 0;
7481
7482 switch (bits->width) {
7483 case 1: {
7484 u8 tmp8 = 0;
7485 pci_read_config_byte(pdev, bits->reg, &tmp8);
7486 tmp = tmp8;
7487 break;
7488 }
7489 case 2: {
7490 u16 tmp16 = 0;
7491 pci_read_config_word(pdev, bits->reg, &tmp16);
7492 tmp = tmp16;
7493 break;
7494 }
7495 case 4: {
7496 u32 tmp32 = 0;
7497 pci_read_config_dword(pdev, bits->reg, &tmp32);
7498 tmp = tmp32;
7499 break;
7500 }
7501
7502 default:
7503 return -EINVAL;
7504 }
7505
7506 tmp &= bits->mask;
7507
7508 return (tmp == bits->val) ? 1 : 0;
7509 }
7510
7511 #ifdef CONFIG_PM
7512 void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
7513 {
7514 pci_save_state(pdev);
7515 pci_disable_device(pdev);
7516
7517 if (mesg.event & PM_EVENT_SLEEP)
7518 pci_set_power_state(pdev, PCI_D3hot);
7519 }
7520
7521 int ata_pci_device_do_resume(struct pci_dev *pdev)
7522 {
7523 int rc;
7524
7525 pci_set_power_state(pdev, PCI_D0);
7526 pci_restore_state(pdev);
7527
7528 rc = pcim_enable_device(pdev);
7529 if (rc) {
7530 dev_printk(KERN_ERR, &pdev->dev,
7531 "failed to enable device after resume (%d)\n", rc);
7532 return rc;
7533 }
7534
7535 pci_set_master(pdev);
7536 return 0;
7537 }
7538
7539 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
7540 {
7541 struct ata_host *host = dev_get_drvdata(&pdev->dev);
7542 int rc = 0;
7543
7544 rc = ata_host_suspend(host, mesg);
7545 if (rc)
7546 return rc;
7547
7548 ata_pci_device_do_suspend(pdev, mesg);
7549
7550 return 0;
7551 }
7552
7553 int ata_pci_device_resume(struct pci_dev *pdev)
7554 {
7555 struct ata_host *host = dev_get_drvdata(&pdev->dev);
7556 int rc;
7557
7558 rc = ata_pci_device_do_resume(pdev);
7559 if (rc == 0)
7560 ata_host_resume(host);
7561 return rc;
7562 }
7563 #endif /* CONFIG_PM */
7564
7565 #endif /* CONFIG_PCI */
7566
7567 static int __init ata_parse_force_one(char **cur,
7568 struct ata_force_ent *force_ent,
7569 const char **reason)
7570 {
7571 /* FIXME: Currently, there's no way to tag init const data and
7572 * using __initdata causes build failure on some versions of
7573 * gcc. Once __initdataconst is implemented, add const to the
7574 * following structure.
7575 */
7576 static struct ata_force_param force_tbl[] __initdata = {
7577 { "40c", .cbl = ATA_CBL_PATA40 },
7578 { "80c", .cbl = ATA_CBL_PATA80 },
7579 { "short40c", .cbl = ATA_CBL_PATA40_SHORT },
7580 { "unk", .cbl = ATA_CBL_PATA_UNK },
7581 { "ign", .cbl = ATA_CBL_PATA_IGN },
7582 { "sata", .cbl = ATA_CBL_SATA },
7583 { "1.5Gbps", .spd_limit = 1 },
7584 { "3.0Gbps", .spd_limit = 2 },
7585 { "noncq", .horkage_on = ATA_HORKAGE_NONCQ },
7586 { "ncq", .horkage_off = ATA_HORKAGE_NONCQ },
7587 { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) },
7588 { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) },
7589 { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) },
7590 { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) },
7591 { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) },
7592 { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) },
7593 { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) },
7594 { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) },
7595 { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) },
7596 { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) },
7597 { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) },
7598 { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) },
7599 { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
7600 { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
7601 { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
7602 { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
7603 { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
7604 { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
7605 { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
7606 { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
7607 { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
7608 { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
7609 { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
7610 { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
7611 { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
7612 { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
7613 { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
7614 { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
7615 { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
7616 { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
7617 { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
7618 { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
7619 { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
7620 { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) },
7621 };
7622 char *start = *cur, *p = *cur;
7623 char *id, *val, *endp;
7624 const struct ata_force_param *match_fp = NULL;
7625 int nr_matches = 0, i;
7626
7627 /* find where this param ends and update *cur */
7628 while (*p != '\0' && *p != ',')
7629 p++;
7630
7631 if (*p == '\0')
7632 *cur = p;
7633 else
7634 *cur = p + 1;
7635
7636 *p = '\0';
7637
7638 /* parse */
7639 p = strchr(start, ':');
7640 if (!p) {
7641 val = strstrip(start);
7642 goto parse_val;
7643 }
7644 *p = '\0';
7645
7646 id = strstrip(start);
7647 val = strstrip(p + 1);
7648
7649 /* parse id */
7650 p = strchr(id, '.');
7651 if (p) {
7652 *p++ = '\0';
7653 force_ent->device = simple_strtoul(p, &endp, 10);
7654 if (p == endp || *endp != '\0') {
7655 *reason = "invalid device";
7656 return -EINVAL;
7657 }
7658 }
7659
7660 force_ent->port = simple_strtoul(id, &endp, 10);
7661 if (p == endp || *endp != '\0') {
7662 *reason = "invalid port/link";
7663 return -EINVAL;
7664 }
7665
7666 parse_val:
7667 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
7668 for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
7669 const struct ata_force_param *fp = &force_tbl[i];
7670
7671 if (strncasecmp(val, fp->name, strlen(val)))
7672 continue;
7673
7674 nr_matches++;
7675 match_fp = fp;
7676
7677 if (strcasecmp(val, fp->name) == 0) {
7678 nr_matches = 1;
7679 break;
7680 }
7681 }
7682
7683 if (!nr_matches) {
7684 *reason = "unknown value";
7685 return -EINVAL;
7686 }
7687 if (nr_matches > 1) {
7688 *reason = "ambigious value";
7689 return -EINVAL;
7690 }
7691
7692 force_ent->param = *match_fp;
7693
7694 return 0;
7695 }
7696
7697 static void __init ata_parse_force_param(void)
7698 {
7699 int idx = 0, size = 1;
7700 int last_port = -1, last_device = -1;
7701 char *p, *cur, *next;
7702
7703 /* calculate maximum number of params and allocate force_tbl */
7704 for (p = ata_force_param_buf; *p; p++)
7705 if (*p == ',')
7706 size++;
7707
7708 ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
7709 if (!ata_force_tbl) {
7710 printk(KERN_WARNING "ata: failed to extend force table, "
7711 "libata.force ignored\n");
7712 return;
7713 }
7714
7715 /* parse and populate the table */
7716 for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
7717 const char *reason = "";
7718 struct ata_force_ent te = { .port = -1, .device = -1 };
7719
7720 next = cur;
7721 if (ata_parse_force_one(&next, &te, &reason)) {
7722 printk(KERN_WARNING "ata: failed to parse force "
7723 "parameter \"%s\" (%s)\n",
7724 cur, reason);
7725 continue;
7726 }
7727
7728 if (te.port == -1) {
7729 te.port = last_port;
7730 te.device = last_device;
7731 }
7732
7733 ata_force_tbl[idx++] = te;
7734
7735 last_port = te.port;
7736 last_device = te.device;
7737 }
7738
7739 ata_force_tbl_size = idx;
7740 }
7741
7742 static int __init ata_init(void)
7743 {
7744 ata_probe_timeout *= HZ;
7745
7746 ata_parse_force_param();
7747
7748 ata_wq = create_workqueue("ata");
7749 if (!ata_wq)
7750 return -ENOMEM;
7751
7752 ata_aux_wq = create_singlethread_workqueue("ata_aux");
7753 if (!ata_aux_wq) {
7754 destroy_workqueue(ata_wq);
7755 return -ENOMEM;
7756 }
7757
7758 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
7759 return 0;
7760 }
7761
7762 static void __exit ata_exit(void)
7763 {
7764 kfree(ata_force_tbl);
7765 destroy_workqueue(ata_wq);
7766 destroy_workqueue(ata_aux_wq);
7767 }
7768
7769 subsys_initcall(ata_init);
7770 module_exit(ata_exit);
7771
7772 static unsigned long ratelimit_time;
7773 static DEFINE_SPINLOCK(ata_ratelimit_lock);
7774
7775 int ata_ratelimit(void)
7776 {
7777 int rc;
7778 unsigned long flags;
7779
7780 spin_lock_irqsave(&ata_ratelimit_lock, flags);
7781
7782 if (time_after(jiffies, ratelimit_time)) {
7783 rc = 1;
7784 ratelimit_time = jiffies + (HZ/5);
7785 } else
7786 rc = 0;
7787
7788 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
7789
7790 return rc;
7791 }
7792
7793 /**
7794 * ata_wait_register - wait until register value changes
7795 * @reg: IO-mapped register
7796 * @mask: Mask to apply to read register value
7797 * @val: Wait condition
7798 * @interval_msec: polling interval in milliseconds
7799 * @timeout_msec: timeout in milliseconds
7800 *
7801 * Waiting for some bits of register to change is a common
7802 * operation for ATA controllers. This function reads 32bit LE
7803 * IO-mapped register @reg and tests for the following condition.
7804 *
7805 * (*@reg & mask) != val
7806 *
7807 * If the condition is met, it returns; otherwise, the process is
7808 * repeated after @interval_msec until timeout.
7809 *
7810 * LOCKING:
7811 * Kernel thread context (may sleep)
7812 *
7813 * RETURNS:
7814 * The final register value.
7815 */
7816 u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
7817 unsigned long interval_msec,
7818 unsigned long timeout_msec)
7819 {
7820 unsigned long timeout;
7821 u32 tmp;
7822
7823 tmp = ioread32(reg);
7824
7825 /* Calculate timeout _after_ the first read to make sure
7826 * preceding writes reach the controller before starting to
7827 * eat away the timeout.
7828 */
7829 timeout = jiffies + (timeout_msec * HZ) / 1000;
7830
7831 while ((tmp & mask) == val && time_before(jiffies, timeout)) {
7832 msleep(interval_msec);
7833 tmp = ioread32(reg);
7834 }
7835
7836 return tmp;
7837 }
7838
7839 /*
7840 * Dummy port_ops
7841 */
7842 static void ata_dummy_noret(struct ata_port *ap) { }
7843 static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
7844 static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
7845
7846 static u8 ata_dummy_check_status(struct ata_port *ap)
7847 {
7848 return ATA_DRDY;
7849 }
7850
7851 static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
7852 {
7853 return AC_ERR_SYSTEM;
7854 }
7855
7856 struct ata_port_operations ata_dummy_port_ops = {
7857 .check_status = ata_dummy_check_status,
7858 .check_altstatus = ata_dummy_check_status,
7859 .dev_select = ata_noop_dev_select,
7860 .qc_prep = ata_noop_qc_prep,
7861 .qc_issue = ata_dummy_qc_issue,
7862 .freeze = ata_dummy_noret,
7863 .thaw = ata_dummy_noret,
7864 .error_handler = ata_dummy_noret,
7865 .post_internal_cmd = ata_dummy_qc_noret,
7866 .irq_clear = ata_dummy_noret,
7867 .port_start = ata_dummy_ret0,
7868 .port_stop = ata_dummy_noret,
7869 };
7870
7871 const struct ata_port_info ata_dummy_port_info = {
7872 .port_ops = &ata_dummy_port_ops,
7873 };
7874
7875 /*
7876 * libata is essentially a library of internal helper functions for
7877 * low-level ATA host controller drivers. As such, the API/ABI is
7878 * likely to change as new drivers are added and updated.
7879 * Do not depend on ABI/API stability.
7880 */
7881 EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
7882 EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
7883 EXPORT_SYMBOL_GPL(sata_deb_timing_long);
7884 EXPORT_SYMBOL_GPL(ata_base_port_ops);
7885 EXPORT_SYMBOL_GPL(sata_port_ops);
7886 EXPORT_SYMBOL_GPL(sata_pmp_port_ops);
7887 EXPORT_SYMBOL_GPL(ata_sff_port_ops);
7888 EXPORT_SYMBOL_GPL(ata_bmdma_port_ops);
7889 EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
7890 EXPORT_SYMBOL_GPL(ata_dummy_port_info);
7891 EXPORT_SYMBOL_GPL(ata_std_bios_param);
7892 EXPORT_SYMBOL_GPL(ata_std_ports);
7893 EXPORT_SYMBOL_GPL(ata_host_init);
7894 EXPORT_SYMBOL_GPL(ata_host_alloc);
7895 EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
7896 EXPORT_SYMBOL_GPL(ata_host_start);
7897 EXPORT_SYMBOL_GPL(ata_host_register);
7898 EXPORT_SYMBOL_GPL(ata_host_activate);
7899 EXPORT_SYMBOL_GPL(ata_host_detach);
7900 EXPORT_SYMBOL_GPL(ata_sg_init);
7901 EXPORT_SYMBOL_GPL(ata_hsm_move);
7902 EXPORT_SYMBOL_GPL(ata_qc_complete);
7903 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
7904 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
7905 EXPORT_SYMBOL_GPL(ata_tf_load);
7906 EXPORT_SYMBOL_GPL(ata_tf_read);
7907 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
7908 EXPORT_SYMBOL_GPL(ata_std_dev_select);
7909 EXPORT_SYMBOL_GPL(sata_print_link_status);
7910 EXPORT_SYMBOL_GPL(atapi_cmd_type);
7911 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
7912 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
7913 EXPORT_SYMBOL_GPL(ata_pack_xfermask);
7914 EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
7915 EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
7916 EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
7917 EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
7918 EXPORT_SYMBOL_GPL(ata_mode_string);
7919 EXPORT_SYMBOL_GPL(ata_id_xfermask);
7920 EXPORT_SYMBOL_GPL(ata_check_status);
7921 EXPORT_SYMBOL_GPL(ata_altstatus);
7922 EXPORT_SYMBOL_GPL(ata_exec_command);
7923 EXPORT_SYMBOL_GPL(ata_port_start);
7924 EXPORT_SYMBOL_GPL(ata_sff_port_start);
7925 EXPORT_SYMBOL_GPL(ata_interrupt);
7926 EXPORT_SYMBOL_GPL(ata_do_set_mode);
7927 EXPORT_SYMBOL_GPL(ata_data_xfer);
7928 EXPORT_SYMBOL_GPL(ata_data_xfer_noirq);
7929 EXPORT_SYMBOL_GPL(ata_std_qc_defer);
7930 EXPORT_SYMBOL_GPL(ata_qc_prep);
7931 EXPORT_SYMBOL_GPL(ata_dumb_qc_prep);
7932 EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
7933 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
7934 EXPORT_SYMBOL_GPL(ata_bmdma_start);
7935 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
7936 EXPORT_SYMBOL_GPL(ata_noop_irq_clear);
7937 EXPORT_SYMBOL_GPL(ata_bmdma_status);
7938 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
7939 EXPORT_SYMBOL_GPL(ata_bmdma_freeze);
7940 EXPORT_SYMBOL_GPL(ata_bmdma_thaw);
7941 EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh);
7942 EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
7943 EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
7944 EXPORT_SYMBOL_GPL(ata_port_probe);
7945 EXPORT_SYMBOL_GPL(ata_dev_disable);
7946 EXPORT_SYMBOL_GPL(sata_set_spd);
7947 EXPORT_SYMBOL_GPL(sata_link_debounce);
7948 EXPORT_SYMBOL_GPL(sata_link_resume);
7949 EXPORT_SYMBOL_GPL(ata_bus_reset);
7950 EXPORT_SYMBOL_GPL(ata_std_prereset);
7951 EXPORT_SYMBOL_GPL(ata_std_softreset);
7952 EXPORT_SYMBOL_GPL(sata_link_hardreset);
7953 EXPORT_SYMBOL_GPL(sata_std_hardreset);
7954 EXPORT_SYMBOL_GPL(ata_std_postreset);
7955 EXPORT_SYMBOL_GPL(ata_dev_classify);
7956 EXPORT_SYMBOL_GPL(ata_dev_pair);
7957 EXPORT_SYMBOL_GPL(ata_port_disable);
7958 EXPORT_SYMBOL_GPL(ata_ratelimit);
7959 EXPORT_SYMBOL_GPL(ata_wait_register);
7960 EXPORT_SYMBOL_GPL(ata_busy_sleep);
7961 EXPORT_SYMBOL_GPL(ata_wait_after_reset);
7962 EXPORT_SYMBOL_GPL(ata_wait_ready);
7963 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
7964 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
7965 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
7966 EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
7967 EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
7968 EXPORT_SYMBOL_GPL(ata_host_intr);
7969 EXPORT_SYMBOL_GPL(sata_scr_valid);
7970 EXPORT_SYMBOL_GPL(sata_scr_read);
7971 EXPORT_SYMBOL_GPL(sata_scr_write);
7972 EXPORT_SYMBOL_GPL(sata_scr_write_flush);
7973 EXPORT_SYMBOL_GPL(ata_link_online);
7974 EXPORT_SYMBOL_GPL(ata_link_offline);
7975 #ifdef CONFIG_PM
7976 EXPORT_SYMBOL_GPL(ata_host_suspend);
7977 EXPORT_SYMBOL_GPL(ata_host_resume);
7978 #endif /* CONFIG_PM */
7979 EXPORT_SYMBOL_GPL(ata_id_string);
7980 EXPORT_SYMBOL_GPL(ata_id_c_string);
7981 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
7982
7983 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
7984 EXPORT_SYMBOL_GPL(ata_timing_find_mode);
7985 EXPORT_SYMBOL_GPL(ata_timing_compute);
7986 EXPORT_SYMBOL_GPL(ata_timing_merge);
7987 EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
7988
7989 #ifdef CONFIG_PCI
7990 EXPORT_SYMBOL_GPL(pci_test_config_bits);
7991 EXPORT_SYMBOL_GPL(ata_pci_init_sff_host);
7992 EXPORT_SYMBOL_GPL(ata_pci_init_bmdma);
7993 EXPORT_SYMBOL_GPL(ata_pci_prepare_sff_host);
7994 EXPORT_SYMBOL_GPL(ata_pci_activate_sff_host);
7995 EXPORT_SYMBOL_GPL(ata_pci_init_one);
7996 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
7997 #ifdef CONFIG_PM
7998 EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
7999 EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
8000 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
8001 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
8002 #endif /* CONFIG_PM */
8003 EXPORT_SYMBOL_GPL(ata_pci_default_filter);
8004 EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
8005 #endif /* CONFIG_PCI */
8006
8007 EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch);
8008 EXPORT_SYMBOL_GPL(sata_pmp_std_prereset);
8009 EXPORT_SYMBOL_GPL(sata_pmp_std_hardreset);
8010 EXPORT_SYMBOL_GPL(sata_pmp_std_postreset);
8011 EXPORT_SYMBOL_GPL(sata_pmp_do_eh);
8012
8013 EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
8014 EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
8015 EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
8016 EXPORT_SYMBOL_GPL(ata_port_desc);
8017 #ifdef CONFIG_PCI
8018 EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
8019 #endif /* CONFIG_PCI */
8020 EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
8021 EXPORT_SYMBOL_GPL(ata_link_abort);
8022 EXPORT_SYMBOL_GPL(ata_port_abort);
8023 EXPORT_SYMBOL_GPL(ata_port_freeze);
8024 EXPORT_SYMBOL_GPL(sata_async_notification);
8025 EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
8026 EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
8027 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
8028 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
8029 EXPORT_SYMBOL_GPL(ata_do_eh);
8030 EXPORT_SYMBOL_GPL(ata_irq_on);
8031 EXPORT_SYMBOL_GPL(ata_dev_try_classify);
8032
8033 EXPORT_SYMBOL_GPL(ata_cable_40wire);
8034 EXPORT_SYMBOL_GPL(ata_cable_80wire);
8035 EXPORT_SYMBOL_GPL(ata_cable_unknown);
8036 EXPORT_SYMBOL_GPL(ata_cable_ignore);
8037 EXPORT_SYMBOL_GPL(ata_cable_sata);
This page took 0.309906 seconds and 5 git commands to generate.