blob: 32fa9ee397b6e85b108872c1204c7d21d9dcd8ce [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04002 * 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 *
Alan Cox92c52c52007-11-19 14:30:16 +000033 * 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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#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>
Jeff Garzik67846b32005-10-05 02:58:32 -040058#include <linux/jiffies.h>
David Hardeman378f0582005-09-17 17:55:31 +100059#include <linux/scatterlist.h>
Jeff Garzik2dcb4072007-10-19 06:42:56 -040060#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <scsi/scsi.h>
Jeff Garzik193515d2005-11-07 00:59:37 -050062#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <scsi/scsi_host.h>
64#include <linux/libata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <asm/semaphore.h>
66#include <asm/byteorder.h>
Tejun Heo140b5e52007-12-12 12:21:52 +090067#include <linux/cdrom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#include "libata.h"
70
Jeff Garzikfda0efc2007-01-31 07:43:15 -050071
Tejun Heod7bb4cc2006-05-31 18:27:46 +090072/* debounce timing parameters in msecs { interval, duration, timeout } */
Tejun Heoe9c83912006-07-03 16:07:26 +090073const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
74const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
75const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
Tejun Heod7bb4cc2006-05-31 18:27:46 +090076
Tejun Heo029cfd62008-03-25 12:22:49 +090077const struct ata_port_operations ata_base_port_ops = {
78 .irq_clear = ata_noop_irq_clear,
79};
80
81const 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
88const struct ata_port_operations sata_pmp_port_ops = {
89 .inherits = &sata_port_ops,
90};
91
92const 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
115const 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
Tejun Heo3373efd2006-05-15 20:57:53 +0900127static unsigned int ata_dev_init_params(struct ata_device *dev,
128 u16 heads, u16 sectors);
129static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
Jeff Garzik218f3d32007-10-25 00:33:27 -0400130static unsigned int ata_dev_set_feature(struct ata_device *dev,
131 u8 enable, u8 feature);
Tejun Heo3373efd2006-05-15 20:57:53 +0900132static void ata_dev_xfermask(struct ata_device *dev);
Tejun Heo75683fe2007-07-05 13:31:27 +0900133static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Tejun Heof3187192007-04-17 23:44:07 +0900135unsigned int ata_print_id = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static struct workqueue_struct *ata_wq;
137
Tejun Heo453b07a2006-05-31 18:27:42 +0900138struct workqueue_struct *ata_aux_wq;
139
Tejun Heo33267322008-02-13 09:15:09 +0900140struct 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
149struct ata_force_ent {
150 int port;
151 int device;
152 struct ata_force_param param;
153};
154
155static struct ata_force_ent *ata_force_tbl;
156static int ata_force_tbl_size;
157
158static char ata_force_param_buf[PAGE_SIZE] __initdata;
Tejun Heo7afb4222008-03-09 20:21:53 +0900159/* param_buf is thrown away after initialization, disallow read */
160module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
Tejun Heo33267322008-02-13 09:15:09 +0900161MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/kernel-parameters.txt for details)");
162
Jeff Garzik418dc1f2006-03-11 20:50:08 -0500163int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -0400164module_param(atapi_enabled, int, 0444);
165MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
166
Adrian Bunkc5c61bd2008-02-25 02:07:25 +0200167static int atapi_dmadir = 0;
Albert Lee95de7192006-04-04 10:57:18 +0800168module_param(atapi_dmadir, int, 0444);
169MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
170
Mark Lordbaf4fdf2007-08-08 01:08:45 +0900171int atapi_passthru16 = 1;
172module_param(atapi_passthru16, int, 0444);
173MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices; on by default (0=off, 1=on)");
174
Jeff Garzikc3c013a2006-02-27 22:31:19 -0500175int libata_fua = 0;
176module_param_named(fua, libata_fua, int, 0444);
177MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
178
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400179static int ata_ignore_hpa;
Alan Cox1e999732007-04-11 00:23:13 +0100180module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
181MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
182
Alan Coxb3a70602007-10-02 12:38:26 -0400183static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
184module_param_named(dma, libata_dma_mask, int, 0444);
185MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
186
Andrew Mortona8601e52006-06-25 01:36:52 -0700187static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
188module_param(ata_probe_timeout, int, 0444);
189MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
190
Jeff Garzik6ebe9d82007-10-05 16:28:36 -0400191int libata_noacpi = 0;
Jeff Garzikd7d0dad2007-03-28 01:57:37 -0400192module_param_named(noacpi, libata_noacpi, int, 0444);
Jeff Garzik6ebe9d82007-10-05 16:28:36 -0400193MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in probe/suspend/resume when set");
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700194
Alan Coxae8d4ee2007-11-04 22:05:49 -0500195int libata_allow_tpm = 0;
196module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
197MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands");
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199MODULE_AUTHOR("Jeff Garzik");
200MODULE_DESCRIPTION("Library module for ATA devices");
201MODULE_LICENSE("GPL");
202MODULE_VERSION(DRV_VERSION);
203
Edward Falk0baab862005-06-02 18:17:13 -0400204
205/**
Tejun Heo33267322008-02-13 09:15:09 +0900206 * ata_force_cbl - force cable type according to libata.force
Randy Dunlap4cdfa1b2008-02-22 12:21:37 -0800207 * @ap: ATA port of interest
Tejun Heo33267322008-02-13 09:15:09 +0900208 *
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 */
218void 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 */
253static 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 */
292static 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 */
346static 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/**
Tejun Heo436d34b2008-04-02 17:28:46 +0900379 * 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 */
390int 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
Tejun Heoe52dcc42008-04-02 17:35:19 +0900406 case ATA_16:
407 case ATA_12:
408 if (atapi_passthru16)
409 return ATAPI_PASS_THRU;
410 /* fall thru */
Tejun Heo436d34b2008-04-02 17:28:46 +0900411 default:
412 return ATAPI_MISC;
413 }
414}
415
416/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
418 * @tf: Taskfile to convert
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 * @pmp: Port multiplier port
Tejun Heo99771262007-07-16 14:29:38 +0900420 * @is_cmd: This FIS is for command
421 * @fis: Buffer into which data will output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 *
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 */
Tejun Heo99771262007-07-16 14:29:38 +0900429void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Tejun Heo99771262007-07-16 14:29:38 +0900431 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 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 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500465 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 *
467 * LOCKING:
468 * Inherited from caller.
469 */
470
Jeff Garzik057ace52005-10-22 14:27:05 -0400471void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
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
Albert Lee8cbd6df2005-10-12 15:06:27 +0800489static 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,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100495 0,
496 0,
497 0,
498 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800499 /* pio */
500 ATA_CMD_PIO_READ,
501 ATA_CMD_PIO_WRITE,
502 ATA_CMD_PIO_READ_EXT,
503 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100504 0,
505 0,
506 0,
507 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800508 /* dma */
509 ATA_CMD_READ,
510 ATA_CMD_WRITE,
511 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100512 ATA_CMD_WRITE_EXT,
513 0,
514 0,
515 0,
516 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800517};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800520 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
Tejun Heobd056d72006-11-14 22:47:10 +0900521 * @tf: command to examine and configure
522 * @dev: device tf belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500524 * Examine the device configuration and tf->flags to calculate
Albert Lee8cbd6df2005-10-12 15:06:27 +0800525 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 *
527 * LOCKING:
528 * caller.
529 */
Tejun Heobd056d72006-11-14 22:47:10 +0900530static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100532 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100534 int index, fua, lba48, write;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500535
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100536 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800537 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
538 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Albert Lee8cbd6df2005-10-12 15:06:27 +0800540 if (dev->flags & ATA_DFLAG_PIO) {
541 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100542 index = dev->multi_count ? 0 : 8;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900543 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
Alan Cox8d238e02006-01-17 20:50:31 +0000544 /* Unable to use DMA due to host limitation */
545 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800546 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800547 } else {
548 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100549 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100552 cmd = ata_rw_cmds[index + fua + lba48 + write];
553 if (cmd) {
554 tf->command = cmd;
555 return 0;
556 }
557 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Tejun Heocb95d562006-03-06 04:31:56 +0900560/**
Tejun Heo35b649f2006-11-14 22:37:35 +0900561 * 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 */
575u64 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/**
Tejun Heobd056d72006-11-14 22:47:10 +0900604 * 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 */
623int 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
Tejun Heo6d1245b2007-02-20 23:20:27 +0900630 if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
Tejun Heobd056d72006-11-14 22:47:10 +0900631 /* 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/**
Tejun Heocb95d562006-03-06 04:31:56 +0900727 * 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 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900741unsigned long ata_pack_xfermask(unsigned long pio_mask,
742 unsigned long mwdma_mask,
743 unsigned long udma_mask)
Tejun Heocb95d562006-03-06 04:31:56 +0900744{
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
Tejun Heoc0489e42006-03-24 14:07:49 +0900750/**
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 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900760void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
761 unsigned long *mwdma_mask, unsigned long *udma_mask)
Tejun Heoc0489e42006-03-24 14:07:49 +0900762{
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
Tejun Heocb95d562006-03-06 04:31:56 +0900771static const struct ata_xfer_ent {
Tejun Heobe9a50c82006-03-31 22:48:52 +0900772 int shift, bits;
Tejun Heocb95d562006-03-06 04:31:56 +0900773 u8 base;
774} ata_xfer_tbl[] = {
Tejun Heo70cd0712007-11-27 19:43:40 +0900775 { 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 },
Tejun Heocb95d562006-03-06 04:31:56 +0900778 { -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:
Tejun Heo70cd0712007-11-27 19:43:40 +0900792 * Matching XFER_* value, 0xff if no match found.
Tejun Heocb95d562006-03-06 04:31:56 +0900793 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900794u8 ata_xfer_mask2mode(unsigned long xfer_mask)
Tejun Heocb95d562006-03-06 04:31:56 +0900795{
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;
Tejun Heo70cd0712007-11-27 19:43:40 +0900802 return 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +0900803}
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 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900817unsigned long ata_xfer_mode2mask(u8 xfer_mode)
Tejun Heocb95d562006-03-06 04:31:56 +0900818{
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)
Tejun Heo70cd0712007-11-27 19:43:40 +0900823 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
824 & ~((1 << ent->shift) - 1);
Tejun Heocb95d562006-03-06 04:31:56 +0900825 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 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900840int ata_xfer_mode2shift(unsigned long xfer_mode)
Tejun Heocb95d562006-03-06 04:31:56 +0900841{
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900851 * ata_mode_string - convert xfer_mask to string
852 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 *
854 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900855 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 *
857 * LOCKING:
858 * None.
859 *
860 * RETURNS:
861 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900862 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900864const char *ata_mode_string(unsigned long xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Tejun Heo75f554b2006-03-06 04:31:57 +0900866 static const char * const xfer_mode_str[] = {
867 "PIO0",
868 "PIO1",
869 "PIO2",
870 "PIO3",
871 "PIO4",
Alan Coxb352e572006-08-10 18:52:12 +0100872 "PIO5",
873 "PIO6",
Tejun Heo75f554b2006-03-06 04:31:57 +0900874 "MWDMA0",
875 "MWDMA1",
876 "MWDMA2",
Alan Coxb352e572006-08-10 18:52:12 +0100877 "MWDMA3",
878 "MWDMA4",
Tejun Heo75f554b2006-03-06 04:31:57 +0900879 "UDMA/16",
880 "UDMA/25",
881 "UDMA/33",
882 "UDMA/44",
883 "UDMA/66",
884 "UDMA/100",
885 "UDMA/133",
886 "UDMA7",
887 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900888 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900890 highbit = fls(xfer_mask) - 1;
891 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
892 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
Tejun Heo4c360c82006-04-01 01:38:17 +0900896static 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
Tejun Heo3373efd2006-05-15 20:57:53 +0900908void ata_dev_disable(struct ata_device *dev)
Tejun Heo0b8efb02006-03-24 15:25:31 +0900909{
Tejun Heo09d7f9b2007-06-25 23:34:02 +0900910 if (ata_dev_enabled(dev)) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900911 if (ata_msg_drv(dev->link->ap))
Tejun Heo09d7f9b2007-06-25 23:34:02 +0900912 ata_dev_printk(dev, KERN_WARNING, "disabled\n");
Tejun Heo562f0c22007-12-15 15:05:01 +0900913 ata_acpi_on_disable(dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +0900914 ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 |
915 ATA_DNXFER_QUIET);
Tejun Heo0b8efb02006-03-24 15:25:31 +0900916 dev->class++;
917 }
918}
919
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400920static 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
Kristen Carlson Accardif5456b62007-11-02 16:37:08 -0700976 /*
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 */
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400981 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
Kristen Carlson Accardif5456b62007-11-02 16:37:08 -0700990 /*
991 * we don't have to disable DIPM since IPM flags
992 * disallow all transitions which effectively
993 * disable DIPM anyway.
994 */
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400995 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
Stephen Hemminger48166fd2007-10-31 10:00:27 -07001006 * @dev: device to enable power management
1007 * @policy: the link power management policy
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001008 *
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 */
1017void 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
1029enable_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
Stephen Rothwell1992a5e2007-10-31 14:53:32 +11001037#ifdef CONFIG_PM
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001038/**
1039 * ata_dev_disable_pm - disable SATA interface power management
Stephen Hemminger48166fd2007-10-31 10:00:27 -07001040 * @dev: device to disable power management
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001041 *
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 */
1050static 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}
Stephen Rothwell1992a5e2007-10-31 14:53:32 +11001058#endif /* CONFIG_PM */
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001059
1060void ata_lpm_schedule(struct ata_port *ap, enum link_pm policy)
1061{
1062 ap->pm_policy = policy;
Tejun Heo3ec25eb2008-03-27 18:37:14 +09001063 ap->link.eh_info.action |= ATA_EH_LPM;
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001064 ap->link.eh_info.flags |= ATA_EHI_NO_AUTOPSY;
1065 ata_port_schedule_eh(ap);
1066}
1067
Stephen Rothwell1992a5e2007-10-31 14:53:32 +11001068#ifdef CONFIG_PM
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001069static 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
1085static 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}
Stephen Rothwell1992a5e2007-10-31 14:53:32 +11001094#endif /* CONFIG_PM */
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001095
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 * ata_devchk - PATA device presence detection
1099 * @ap: ATA channel to examine
1100 * @device: Device to examine (starting at zero)
1101 *
Tejun Heo0d5ff562007-02-01 15:06:36 +09001102 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 *
1111 * LOCKING:
1112 * caller.
1113 */
1114
Tejun Heo0d5ff562007-02-01 15:06:36 +09001115static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Tejun Heo0d5ff562007-02-01 15:06:36 +09001117 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
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:
Tejun Heo633273a2007-09-23 13:19:54 +09001152 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
1153 * %ATA_DEV_UNKNOWN the event of failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 */
Jeff Garzik057ace52005-10-22 14:27:05 -04001155unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
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.
Tejun Heo633273a2007-09-23 13:19:54 +09001160 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 */
Tejun Heo633273a2007-09-23 13:19:54 +09001175 if ((tf->lbam == 0) && (tf->lbah == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 DPRINTK("found ATA device by sig\n");
1177 return ATA_DEV_ATA;
1178 }
1179
Tejun Heo633273a2007-09-23 13:19:54 +09001180 if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 DPRINTK("found ATAPI device by sig\n");
1182 return ATA_DEV_ATAPI;
1183 }
1184
Tejun Heo633273a2007-09-23 13:19:54 +09001185 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)) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001191 printk(KERN_INFO "ata: SEMB device ignored\n");
Tejun Heo633273a2007-09-23 13:19:54 +09001192 return ATA_DEV_SEMB_UNSUP; /* not yet */
1193 }
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 DPRINTK("unknown device\n");
1196 return ATA_DEV_UNKNOWN;
1197}
1198
1199/**
1200 * ata_dev_try_classify - Parse returned ATA device signature
Tejun Heo3f198592007-09-02 23:23:57 +09001201 * @dev: ATA device to classify (starting at zero)
1202 * @present: device seems present
Tejun Heob4dc7622006-01-24 17:05:22 +09001203 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 *
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.
Tejun Heob4dc7622006-01-24 17:05:22 +09001216 *
1217 * RETURNS:
1218 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 */
Tejun Heo3f198592007-09-02 23:23:57 +09001220unsigned int ata_dev_try_classify(struct ata_device *dev, int present,
1221 u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Tejun Heo3f198592007-09-02 23:23:57 +09001223 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 struct ata_taskfile tf;
1225 unsigned int class;
1226 u8 err;
1227
Tejun Heo3f198592007-09-02 23:23:57 +09001228 ap->ops->dev_select(ap, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 memset(&tf, 0, sizeof(tf));
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -04001233 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +09001234 if (r_err)
1235 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Alan Coxc5038fc2007-10-25 14:21:16 +01001237 /* see if device passed diags: continue and warn later */
1238 if (err == 0)
Alan Cox93590852006-09-12 16:55:12 +01001239 /* diagnostic fail : do nothing _YET_ */
Tejun Heo3f198592007-09-02 23:23:57 +09001240 dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
Alan Cox93590852006-09-12 16:55:12 +01001241 else if (err == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 /* do nothing */ ;
Tejun Heo3f198592007-09-02 23:23:57 +09001243 else if ((dev->devno == 0) && (err == 0x81))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 /* do nothing */ ;
1245 else
Tejun Heob4dc7622006-01-24 17:05:22 +09001246 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Tejun Heob4dc7622006-01-24 17:05:22 +09001248 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +09001250
Tejun Heod7fbee02007-09-02 23:24:48 +09001251 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
Tejun Heob4dc7622006-01-24 17:05:22 +09001265 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
1268/**
Tejun Heo6a62a042006-02-13 10:02:46 +09001269 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 * @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
Tejun Heo6a62a042006-02-13 10:02:46 +09001283void ata_id_string(const u16 *id, unsigned char *s,
1284 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
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
Tejun Heo0e949ff2006-02-12 22:47:04 +09001302/**
Tejun Heo6a62a042006-02-13 10:02:46 +09001303 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +09001304 * @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 *
Tejun Heo6a62a042006-02-13 10:02:46 +09001309 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +09001310 * 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 */
Tejun Heo6a62a042006-02-13 10:02:46 +09001316void ata_id_c_string(const u16 *id, unsigned char *s,
1317 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +09001318{
1319 unsigned char *p;
1320
1321 WARN_ON(!(len & 1));
1322
Tejun Heo6a62a042006-02-13 10:02:46 +09001323 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +09001324
1325 p = s + strnlen(s, len - 1);
1326 while (p > s && p[-1] == ' ')
1327 p--;
1328 *p = '\0';
1329}
Edward Falk0baab862005-06-02 18:17:13 -04001330
Tejun Heodb6f8752007-09-03 12:31:58 +09001331static 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
Alan Cox1e999732007-04-11 00:23:13 +01001346static 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
1360static 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/**
Tejun Heoc728a912007-09-03 12:32:30 +09001373 * ata_read_native_max_address - Read native max address
1374 * @dev: target device
1375 * @max_sectors: out parameter for the result native max address
Alan Cox1e999732007-04-11 00:23:13 +01001376 *
Tejun Heoc728a912007-09-03 12:32:30 +09001377 * 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.
Alan Cox1e999732007-04-11 00:23:13 +01001383 */
Tejun Heoc728a912007-09-03 12:32:30 +09001384static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
Alan Cox1e999732007-04-11 00:23:13 +01001385{
Tejun Heoc728a912007-09-03 12:32:30 +09001386 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +01001387 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +09001388 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +01001389
1390 ata_tf_init(dev, &tf);
1391
Tejun Heoc728a912007-09-03 12:32:30 +09001392 /* always clear all address registers */
Alan Cox1e999732007-04-11 00:23:13 +01001393 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +09001394
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
Alan Cox1e999732007-04-11 00:23:13 +01001401 tf.protocol |= ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +09001402 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +01001403
Tejun Heo2b789102007-10-09 15:05:44 +09001404 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoc728a912007-09-03 12:32:30 +09001405 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 }
Alan Cox1e999732007-04-11 00:23:13 +01001412
Tejun Heoc728a912007-09-03 12:32:30 +09001413 if (lba48)
1414 *max_sectors = ata_tf_to_lba48(&tf);
1415 else
1416 *max_sectors = ata_tf_to_lba(&tf);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001417 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
Alan Cox93328e12007-09-29 04:06:48 -04001418 (*max_sectors)--;
Tejun Heoc728a912007-09-03 12:32:30 +09001419 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001420}
1421
1422/**
Tejun Heoc728a912007-09-03 12:32:30 +09001423 * ata_set_max_sectors - Set max sectors
1424 * @dev: target device
Randy Dunlap6b38d1d2007-05-01 17:35:55 -07001425 * @new_sectors: new max sectors value to set for the device
Alan Cox1e999732007-04-11 00:23:13 +01001426 *
Tejun Heoc728a912007-09-03 12:32:30 +09001427 * 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.
Alan Cox1e999732007-04-11 00:23:13 +01001433 */
Tejun Heo05027ad2007-09-03 12:32:57 +09001434static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
Alan Cox1e999732007-04-11 00:23:13 +01001435{
Tejun Heoc728a912007-09-03 12:32:30 +09001436 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +01001437 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +09001438 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +01001439
1440 new_sectors--;
1441
1442 ata_tf_init(dev, &tf);
1443
Alan Cox1e999732007-04-11 00:23:13 +01001444 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +09001445
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;
Tejun Heo1e582ba2007-09-21 20:07:14 +09001453 } else {
Tejun Heoc728a912007-09-03 12:32:30 +09001454 tf.command = ATA_CMD_SET_MAX;
1455
Tejun Heo1e582ba2007-09-21 20:07:14 +09001456 tf.device |= (new_sectors >> 24) & 0xf;
1457 }
1458
Alan Cox1e999732007-04-11 00:23:13 +01001459 tf.protocol |= ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +09001460 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +01001461
1462 tf.lbal = (new_sectors >> 0) & 0xff;
1463 tf.lbam = (new_sectors >> 8) & 0xff;
1464 tf.lbah = (new_sectors >> 16) & 0xff;
Alan Cox1e999732007-04-11 00:23:13 +01001465
Tejun Heo2b789102007-10-09 15:05:44 +09001466 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoc728a912007-09-03 12:32:30 +09001467 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 }
Alan Cox1e999732007-04-11 00:23:13 +01001475
Tejun Heoc728a912007-09-03 12:32:30 +09001476 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001477}
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.
Tejun Heo05027ad2007-09-03 12:32:57 +09001486 *
1487 * RETURNS:
1488 * 0 on success, -errno on failure.
Alan Cox1e999732007-04-11 00:23:13 +01001489 */
Tejun Heo05027ad2007-09-03 12:32:57 +09001490static int ata_hpa_resize(struct ata_device *dev)
Alan Cox1e999732007-04-11 00:23:13 +01001491{
Tejun Heo05027ad2007-09-03 12:32:57 +09001492 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;
Tejun Heoc728a912007-09-03 12:32:30 +09001496 int rc;
Jeff Garzika617c092007-05-21 20:14:23 -04001497
Tejun Heo05027ad2007-09-03 12:32:57 +09001498 /* 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))
Tejun Heoc728a912007-09-03 12:32:30 +09001502 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001503
Tejun Heo05027ad2007-09-03 12:32:57 +09001504 /* read native max address */
1505 rc = ata_read_native_max_address(dev, &native_sectors);
1506 if (rc) {
Tejun Heodda7aba2008-03-23 21:05:15 +09001507 /* If device aborted the command or HPA isn't going to
1508 * be unlocked, skip HPA resizing.
Tejun Heo05027ad2007-09-03 12:32:57 +09001509 */
Tejun Heodda7aba2008-03-23 21:05:15 +09001510 if (rc == -EACCES || !ata_ignore_hpa) {
Tejun Heo05027ad2007-09-03 12:32:57 +09001511 ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
Tejun Heodda7aba2008-03-23 21:05:15 +09001512 "broken, skipping HPA handling\n");
Tejun Heo05027ad2007-09-03 12:32:57 +09001513 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
Alan Cox1e999732007-04-11 00:23:13 +01001514
Tejun Heo05027ad2007-09-03 12:32:57 +09001515 /* we can continue if device aborted the command */
1516 if (rc == -EACCES)
1517 rc = 0;
Alan Cox1e999732007-04-11 00:23:13 +01001518 }
Tejun Heo37301a52007-06-25 20:45:54 +09001519
Tejun Heo05027ad2007-09-03 12:32:57 +09001520 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;
Alan Cox1e999732007-04-11 00:23:13 +01001573}
1574
Edward Falk0baab862005-06-02 18:17:13 -04001575/**
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 */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001587void ata_noop_dev_select(struct ata_port *ap, unsigned int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
1589}
1590
Edward Falk0baab862005-06-02 18:17:13 -04001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592/**
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
Edward Falk0baab862005-06-02 18:17:13 -04001599 * ATA channel. Works with both PIO and MMIO.
1600 *
1601 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 *
1603 * LOCKING:
1604 * caller.
1605 */
1606
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001607void ata_std_dev_select(struct ata_port *ap, unsigned int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
1609 u8 tmp;
1610
1611 if (device == 0)
1612 tmp = ATA_DEVICE_OBS;
1613 else
1614 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1615
Tejun Heo0d5ff562007-02-01 15:06:36 +09001616 iowrite8(tmp, ap->ioaddr.device_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 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
1639void ata_dev_select(struct ata_port *ap, unsigned int device,
1640 unsigned int wait, unsigned int can_sleep)
1641{
Tejun Heo88574552006-06-25 20:00:35 +09001642 if (ata_msg_probe(ap))
Tejun Heo44877b42007-02-21 01:06:51 +09001643 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, "
1644 "device %u, wait %u\n", device, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 if (wait)
1647 ata_wait_idle(ap);
1648
1649 ap->ops->dev_select(ap, device);
1650
1651 if (wait) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001652 if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 msleep(150);
1654 ata_wait_idle(ap);
1655 }
1656}
1657
1658/**
1659 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +09001660 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 *
Tejun Heo0bd33002006-02-12 22:47:05 +09001662 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1663 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 *
1665 * LOCKING:
1666 * caller.
1667 */
1668
Tejun Heo0bd33002006-02-12 22:47:05 +09001669static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
1671 DPRINTK("49==0x%04x "
1672 "53==0x%04x "
1673 "63==0x%04x "
1674 "64==0x%04x "
1675 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001676 id[49],
1677 id[53],
1678 id[63],
1679 id[64],
1680 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 DPRINTK("80==0x%04x "
1682 "81==0x%04x "
1683 "82==0x%04x "
1684 "83==0x%04x "
1685 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001686 id[80],
1687 id[81],
1688 id[82],
1689 id[83],
1690 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 DPRINTK("88==0x%04x "
1692 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001693 id[88],
1694 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
Tejun Heocb95d562006-03-06 04:31:56 +09001697/**
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 */
Tejun Heo7dc951a2007-11-27 19:43:42 +09001712unsigned long ata_id_xfermask(const u16 *id)
Tejun Heocb95d562006-03-06 04:31:56 +09001713{
Tejun Heo7dc951a2007-11-27 19:43:42 +09001714 unsigned long pio_mask, mwdma_mask, udma_mask;
Tejun Heocb95d562006-03-06 04:31:56 +09001715
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 */
Lennert Buytenhek7a0f1c82007-01-29 13:28:47 +01001726 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
Alan Cox46767aeb2006-09-29 18:26:47 +01001727 if (mode < 5) /* Valid PIO range */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001728 pio_mask = (2 << mode) - 1;
Alan Cox46767aeb2006-09-29 18:26:47 +01001729 else
1730 pio_mask = 1;
Tejun Heocb95d562006-03-06 04:31:56 +09001731
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;
Tejun Heofb21f0d2006-03-12 12:34:35 +09001741
Alan Coxb352e572006-08-10 18:52:12 +01001742 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
Tejun Heofb21f0d2006-03-12 12:34:35 +09001759 udma_mask = 0;
1760 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1761 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +09001762
1763 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1764}
1765
Tejun Heo86e45b62006-03-05 15:29:09 +09001766/**
Jeff Garzik442eacc2007-12-19 04:25:10 -05001767 * ata_pio_queue_task - Queue port_task
Tejun Heo86e45b62006-03-05 15:29:09 +09001768 * @ap: The ata_port to queue port_task for
Randy Dunlape2a7f772006-05-18 10:50:18 -07001769 * @fn: workqueue function to be scheduled
David Howells65f27f32006-11-22 14:55:48 +00001770 * @data: data for @fn to use
Randy Dunlape2a7f772006-05-18 10:50:18 -07001771 * @delay: delay time for workqueue function
Tejun Heo86e45b62006-03-05 15:29:09 +09001772 *
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
Jeff Garzik442eacc2007-12-19 04:25:10 -05001779 * port_task and EH. ata_pio_queue_task() may be ignored for EH
Tejun Heo86e45b62006-03-05 15:29:09 +09001780 * synchronization.
1781 *
1782 * LOCKING:
1783 * Inherited from caller.
1784 */
Jeff Garzik442eacc2007-12-19 04:25:10 -05001785static void ata_pio_queue_task(struct ata_port *ap, void *data,
1786 unsigned long delay)
Tejun Heo86e45b62006-03-05 15:29:09 +09001787{
David Howells65f27f32006-11-22 14:55:48 +00001788 ap->port_task_data = data;
Tejun Heo86e45b62006-03-05 15:29:09 +09001789
Oleg Nesterov45a66c12007-07-09 11:46:13 -07001790 /* may fail if ata_port_flush_task() in progress */
1791 queue_delayed_work(ata_wq, &ap->port_task, delay);
Tejun Heo86e45b62006-03-05 15:29:09 +09001792}
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 */
1804void ata_port_flush_task(struct ata_port *ap)
1805{
Tejun Heo86e45b62006-03-05 15:29:09 +09001806 DPRINTK("ENTER\n");
1807
Oleg Nesterov45a66c12007-07-09 11:46:13 -07001808 cancel_rearming_delayed_work(&ap->port_task);
Tejun Heo86e45b62006-03-05 15:29:09 +09001809
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001810 if (ata_msg_ctl(ap))
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08001811 ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __func__);
Tejun Heo86e45b62006-03-05 15:29:09 +09001812}
1813
Adrian Bunk7102d232007-01-04 00:09:36 +01001814static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001815{
Tejun Heo77853bf2006-01-23 13:09:36 +09001816 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001817
Tejun Heoa2a7a662005-12-13 14:48:31 +09001818 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001819}
1820
1821/**
Tejun Heo24326972006-11-14 22:47:09 +09001822 * ata_exec_internal_sg - execute libata internal command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001823 * @dev: Device to which the command is sent
1824 * @tf: Taskfile registers for the command and the result
Tejun Heod69cf372006-04-02 18:51:53 +09001825 * @cdb: CDB for packet command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001826 * @dma_dir: Data tranfer direction of the command
Randy Dunlap5c1ad8b2007-10-18 14:12:26 -07001827 * @sgl: sg list for the data buffer of the command
Tejun Heo24326972006-11-14 22:47:09 +09001828 * @n_elem: Number of sg entries
Tejun Heo2b789102007-10-09 15:05:44 +09001829 * @timeout: Timeout in msecs (0 for default)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001830 *
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.
Tejun Heo551e8882006-06-12 14:09:49 +09001839 *
1840 * RETURNS:
1841 * Zero on success, AC_ERR_* mask on failure
Tejun Heoa2a7a662005-12-13 14:48:31 +09001842 */
Tejun Heo24326972006-11-14 22:47:09 +09001843unsigned ata_exec_internal_sg(struct ata_device *dev,
1844 struct ata_taskfile *tf, const u8 *cdb,
Jens Axboe87260212007-10-16 11:14:12 +02001845 int dma_dir, struct scatterlist *sgl,
Tejun Heo2b789102007-10-09 15:05:44 +09001846 unsigned int n_elem, unsigned long timeout)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001847{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001848 struct ata_link *link = dev->link;
1849 struct ata_port *ap = link->ap;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001850 u8 command = tf->command;
1851 struct ata_queued_cmd *qc;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001852 unsigned int tag, preempted_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001853 u32 preempted_sactive, preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001854 int preempted_nr_active_links;
Ingo Molnar60be6b92006-07-03 00:25:26 -07001855 DECLARE_COMPLETION_ONSTACK(wait);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001856 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001857 unsigned int err_mask;
Tejun Heod95a7172006-05-15 20:58:14 +09001858 int rc;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001859
Jeff Garzikba6a1302006-06-22 23:46:10 -04001860 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001861
Tejun Heoe3180492006-05-15 20:58:09 +09001862 /* no internal command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09001863 if (ap->pflags & ATA_PFLAG_FROZEN) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001864 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001865 return AC_ERR_SYSTEM;
1866 }
1867
Tejun Heo2ab7db12006-05-15 20:58:02 +09001868 /* initialize internal qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001869
Tejun Heo2ab7db12006-05-15 20:58:02 +09001870 /* 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
Tejun Heo6cec4a32006-05-15 21:03:41 +09001880 if (test_and_set_bit(tag, &ap->qc_allocated))
Tejun Heo2ab7db12006-05-15 20:58:02 +09001881 BUG();
Tejun Heof69499f2006-05-15 20:58:03 +09001882 qc = __ata_qc_from_tag(ap, tag);
Tejun Heo2ab7db12006-05-15 20:58:02 +09001883
1884 qc->tag = tag;
1885 qc->scsicmd = NULL;
1886 qc->ap = ap;
1887 qc->dev = dev;
1888 ata_qc_reinit(qc);
1889
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001890 preempted_tag = link->active_tag;
1891 preempted_sactive = link->sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001892 preempted_qc_active = ap->qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001893 preempted_nr_active_links = ap->nr_active_links;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001894 link->active_tag = ATA_TAG_POISON;
1895 link->sactive = 0;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001896 ap->qc_active = 0;
Tejun Heoda917d62007-09-23 13:14:12 +09001897 ap->nr_active_links = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001898
1899 /* prepare & issue qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001900 qc->tf = *tf;
Tejun Heod69cf372006-04-02 18:51:53 +09001901 if (cdb)
1902 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
Tejun Heoe61e0672006-05-15 20:57:40 +09001903 qc->flags |= ATA_QCFLAG_RESULT_TF;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001904 qc->dma_dir = dma_dir;
1905 if (dma_dir != DMA_NONE) {
Tejun Heo24326972006-11-14 22:47:09 +09001906 unsigned int i, buflen = 0;
Jens Axboe87260212007-10-16 11:14:12 +02001907 struct scatterlist *sg;
Tejun Heo24326972006-11-14 22:47:09 +09001908
Jens Axboe87260212007-10-16 11:14:12 +02001909 for_each_sg(sgl, sg, n_elem, i)
1910 buflen += sg->length;
Tejun Heo24326972006-11-14 22:47:09 +09001911
Jens Axboe87260212007-10-16 11:14:12 +02001912 ata_sg_init(qc, sgl, n_elem);
Brian King49c80422007-01-30 11:32:26 -06001913 qc->nbytes = buflen;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001914 }
1915
Tejun Heo77853bf2006-01-23 13:09:36 +09001916 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001917 qc->complete_fn = ata_qc_complete_internal;
1918
Tejun Heo8e0e6942006-03-31 20:41:11 +09001919 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001920
Jeff Garzikba6a1302006-06-22 23:46:10 -04001921 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001922
Tejun Heo2b789102007-10-09 15:05:44 +09001923 if (!timeout)
1924 timeout = ata_probe_timeout * 1000 / HZ;
1925
1926 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
Albert Lee41ade502006-03-14 11:19:04 +08001927
Tejun Heod95a7172006-05-15 20:58:14 +09001928 ata_port_flush_task(ap);
1929
1930 if (!rc) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001931 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001932
1933 /* We're racing with irq here. If we lose, the
1934 * following test prevents us from completing the qc
Tejun Heod95a7172006-05-15 20:58:14 +09001935 * twice. If we win, the port is frozen and will be
1936 * cleaned up by ->post_internal_cmd().
Tejun Heoa2a7a662005-12-13 14:48:31 +09001937 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001938 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heod95a7172006-05-15 20:58:14 +09001939 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);
Tejun Heof15a1da2006-05-15 20:57:56 +09001945
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001946 if (ata_msg_warn(ap))
1947 ata_dev_printk(dev, KERN_WARNING,
Tejun Heo88574552006-06-25 20:00:35 +09001948 "qc timeout (cmd 0x%x)\n", command);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001949 }
1950
Jeff Garzikba6a1302006-06-22 23:46:10 -04001951 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001952 }
1953
Tejun Heod95a7172006-05-15 20:58:14 +09001954 /* do post_internal_cmd */
1955 if (ap->ops->post_internal_cmd)
1956 ap->ops->post_internal_cmd(qc);
1957
Tejun Heoa51d6442007-03-20 15:24:11 +09001958 /* 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;
Tejun Heod95a7172006-05-15 20:58:14 +09001968 }
1969
Tejun Heo15869302006-05-15 20:57:33 +09001970 /* finish up */
Jeff Garzikba6a1302006-06-22 23:46:10 -04001971 spin_lock_irqsave(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001972
Tejun Heoe61e0672006-05-15 20:57:40 +09001973 *tf = qc->result_tf;
Tejun Heo77853bf2006-01-23 13:09:36 +09001974 err_mask = qc->err_mask;
1975
1976 ata_qc_free(qc);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001977 link->active_tag = preempted_tag;
1978 link->sactive = preempted_sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001979 ap->qc_active = preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001980 ap->nr_active_links = preempted_nr_active_links;
Tejun Heo77853bf2006-01-23 13:09:36 +09001981
Tejun Heo1f7dd3e2006-03-24 15:25:30 +09001982 /* 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 */
Tejun Heo198e0fe2006-04-02 18:51:52 +09001993 if (ap->flags & ATA_FLAG_DISABLED) {
Tejun Heo1f7dd3e2006-03-24 15:25:30 +09001994 err_mask |= AC_ERR_SYSTEM;
1995 ata_port_probe(ap);
1996 }
1997
Jeff Garzikba6a1302006-06-22 23:46:10 -04001998 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001999
Tejun Heo77853bf2006-01-23 13:09:36 +09002000 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09002001}
2002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003/**
Tejun Heo33480a02006-12-12 02:15:31 +09002004 * ata_exec_internal - execute libata internal command
Tejun Heo24326972006-11-14 22:47:09 +09002005 * @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
Tejun Heo2b789102007-10-09 15:05:44 +09002011 * @timeout: Timeout in msecs (0 for default)
Tejun Heo24326972006-11-14 22:47:09 +09002012 *
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 */
2022unsigned ata_exec_internal(struct ata_device *dev,
2023 struct ata_taskfile *tf, const u8 *cdb,
Tejun Heo2b789102007-10-09 15:05:44 +09002024 int dma_dir, void *buf, unsigned int buflen,
2025 unsigned long timeout)
Tejun Heo24326972006-11-14 22:47:09 +09002026{
Tejun Heo33480a02006-12-12 02:15:31 +09002027 struct scatterlist *psg = NULL, sg;
2028 unsigned int n_elem = 0;
Tejun Heo24326972006-11-14 22:47:09 +09002029
Tejun Heo33480a02006-12-12 02:15:31 +09002030 if (dma_dir != DMA_NONE) {
2031 WARN_ON(!buf);
2032 sg_init_one(&sg, buf, buflen);
2033 psg = &sg;
2034 n_elem++;
2035 }
Tejun Heo24326972006-11-14 22:47:09 +09002036
Tejun Heo2b789102007-10-09 15:05:44 +09002037 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
2038 timeout);
Tejun Heo24326972006-11-14 22:47:09 +09002039}
2040
2041/**
Tejun Heo977e6b92006-06-24 20:30:19 +09002042 * 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
Tejun Heoe58eb582006-06-24 20:30:19 +09002054 */
Tejun Heo77b08fb2006-06-24 20:30:19 +09002055unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
Tejun Heoe58eb582006-06-24 20:30:19 +09002056{
2057 struct ata_taskfile tf;
Tejun Heoe58eb582006-06-24 20:30:19 +09002058
2059 ata_tf_init(dev, &tf);
2060
2061 tf.command = cmd;
2062 tf.flags |= ATA_TFLAG_DEVICE;
2063 tf.protocol = ATA_PROT_NODATA;
2064
Tejun Heo2b789102007-10-09 15:05:44 +09002065 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoe58eb582006-06-24 20:30:19 +09002066}
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00002069 * 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 */
Jeff Garzika617c092007-05-21 20:14:23 -04002075
Alan Cox1bc4ccf2006-01-09 17:18:14 +00002076unsigned int ata_pio_need_iordy(const struct ata_device *adev)
2077{
Alan Cox432729f2007-03-08 23:22:59 +00002078 /* Controller doesn't support IORDY. Probably a pointless check
2079 as the caller should know this */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002080 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00002081 return 0;
Alan Cox432729f2007-03-08 23:22:59 +00002082 /* PIO3 and higher it is mandatory */
2083 if (adev->pio_mode > XFER_PIO_2)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00002084 return 1;
Alan Cox432729f2007-03-08 23:22:59 +00002085 /* We turn it on when possible */
2086 if (ata_id_has_iordy(adev->id))
2087 return 1;
2088 return 0;
2089}
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002090
Alan Cox432729f2007-03-08 23:22:59 +00002091/**
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 */
Jeff Garzika617c092007-05-21 20:14:23 -04002098
Alan Cox432729f2007-03-08 23:22:59 +00002099static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
2100{
Alan Cox1bc4ccf2006-01-09 17:18:14 +00002101 /* If we have no drive specific rule, then PIO 2 is non IORDY */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00002102 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
Alan Cox432729f2007-03-08 23:22:59 +00002103 u16 pio = adev->id[ATA_ID_EIDE_PIO];
Alan Cox1bc4ccf2006-01-09 17:18:14 +00002104 /* 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 */
Alan Cox432729f2007-03-08 23:22:59 +00002108 return 3 << ATA_SHIFT_PIO;
2109 return 7 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00002110 }
2111 }
Alan Cox432729f2007-03-08 23:22:59 +00002112 return 3 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00002113}
2114
2115/**
Tejun Heo49016ac2006-02-21 02:12:11 +09002116 * ata_dev_read_id - Read ID data from the specified device
Tejun Heo49016ac2006-02-21 02:12:11 +09002117 * @dev: target device
2118 * @p_class: pointer to class of the target device (may be changed)
Tejun Heobff04642006-11-10 18:08:10 +09002119 * @flags: ATA_READID_* flags
Tejun Heofe635c72006-05-15 20:57:35 +09002120 * @id: buffer to read IDENTIFY data into
Tejun Heo49016ac2006-02-21 02:12:11 +09002121 *
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
Tejun Heoaec5c3c2006-03-25 01:33:34 +09002124 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
2125 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09002126 *
Alan Cox50a99012007-08-08 14:27:00 +01002127 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002128 * now we abort if we hit that case.
Alan Cox50a99012007-08-08 14:27:00 +01002129 *
Tejun Heo49016ac2006-02-21 02:12:11 +09002130 * LOCKING:
2131 * Kernel thread context (may sleep)
2132 *
2133 * RETURNS:
2134 * 0 on success, -errno otherwise.
2135 */
Tejun Heoa9beec92006-05-31 18:27:44 +09002136int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
Tejun Heobff04642006-11-10 18:08:10 +09002137 unsigned int flags, u16 *id)
Tejun Heo49016ac2006-02-21 02:12:11 +09002138{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002139 struct ata_port *ap = dev->link->ap;
Tejun Heo49016ac2006-02-21 02:12:11 +09002140 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09002141 struct ata_taskfile tf;
2142 unsigned int err_mask = 0;
2143 const char *reason;
Tejun Heo54936f82007-05-11 14:35:29 +02002144 int may_fallback = 1, tried_spinup = 0;
Tejun Heo49016ac2006-02-21 02:12:11 +09002145 int rc;
2146
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002147 if (ata_msg_ctl(ap))
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08002148 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
Tejun Heo49016ac2006-02-21 02:12:11 +09002149
Tejun Heo49016ac2006-02-21 02:12:11 +09002150 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
Tejun Heo49016ac2006-02-21 02:12:11 +09002151 retry:
Tejun Heo3373efd2006-05-15 20:57:53 +09002152 ata_tf_init(dev, &tf);
Tejun Heo49016ac2006-02-21 02:12:11 +09002153
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;
Tejun Heo81afe892007-02-07 12:37:41 -08002168
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;
Tejun Heo49016ac2006-02-21 02:12:11 +09002178
Tejun Heo3373efd2006-05-15 20:57:53 +09002179 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
Tejun Heo2b789102007-10-09 15:05:44 +09002180 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
Tejun Heo49016ac2006-02-21 02:12:11 +09002181 if (err_mask) {
Tejun Heo800b3992006-12-03 21:34:13 +09002182 if (err_mask & AC_ERR_NODEV_HINT) {
Tejun Heo1ffc1512008-03-23 15:16:53 +09002183 ata_dev_printk(dev, KERN_DEBUG,
2184 "NODEV after polling detection\n");
Tejun Heo55a8e2c2006-11-10 18:08:10 +09002185 return -ENOENT;
2186 }
2187
Tejun Heo1ffc1512008-03-23 15:16:53 +09002188 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;
Tejun Heo54936f82007-05-11 14:35:29 +02002196
Tejun Heo1ffc1512008-03-23 15:16:53 +09002197 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;
Tejun Heo54936f82007-05-11 14:35:29 +02002211 }
2212
Tejun Heo49016ac2006-02-21 02:12:11 +09002213 rc = -EIO;
2214 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09002215 goto err_out;
2216 }
2217
Tejun Heo54936f82007-05-11 14:35:29 +02002218 /* Falling back doesn't make sense if ID data was read
2219 * successfully at least once.
2220 */
2221 may_fallback = 0;
2222
Tejun Heo49016ac2006-02-21 02:12:11 +09002223 swap_buf_le16(id, ATA_ID_WORDS);
2224
Tejun Heo49016ac2006-02-21 02:12:11 +09002225 /* sanity check */
Tejun Heoa4f57492006-09-12 20:35:49 -07002226 rc = -EINVAL;
Alan Cox60700682007-06-07 16:13:55 +01002227 reason = "device reports invalid type";
Tejun Heoa4f57492006-09-12 20:35:49 -07002228
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;
Tejun Heo49016ac2006-02-21 02:12:11 +09002235 }
2236
Mark Lord169439c2007-04-17 18:26:07 -04002237 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 */
Jeff Garzik218f3d32007-10-25 00:33:27 -04002244 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
Ryan Powerfb0582f92007-08-10 13:59:35 -07002245 if (err_mask && id[2] != 0x738c) {
Mark Lord169439c2007-04-17 18:26:07 -04002246 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
Tejun Heobff04642006-11-10 18:08:10 +09002258 if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
Tejun Heo49016ac2006-02-21 02:12:11 +09002259 /*
2260 * The exact sequence expected by certain pre-ATA4 drives is:
2261 * SRST RESET
Alan Cox50a99012007-08-08 14:27:00 +01002262 * IDENTIFY (optional in early ATA)
2263 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
Tejun Heo49016ac2006-02-21 02:12:11 +09002264 * anything else..
2265 * Some drives were very specific about that exact sequence.
Alan Cox50a99012007-08-08 14:27:00 +01002266 *
2267 * Note that ATA4 says lba is mandatory so the second check
2268 * shoud never trigger.
Tejun Heo49016ac2006-02-21 02:12:11 +09002269 */
2270 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Tejun Heo3373efd2006-05-15 20:57:53 +09002271 err_mask = ata_dev_init_params(dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09002272 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 */
Tejun Heobff04642006-11-10 18:08:10 +09002281 flags &= ~ATA_READID_POSTRESET;
Tejun Heo49016ac2006-02-21 02:12:11 +09002282 goto retry;
2283 }
2284 }
2285
2286 *p_class = class;
Tejun Heofe635c72006-05-15 20:57:35 +09002287
Tejun Heo49016ac2006-02-21 02:12:11 +09002288 return 0;
2289
2290 err_out:
Tejun Heo88574552006-06-25 20:00:35 +09002291 if (ata_msg_warn(ap))
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002292 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
Tejun Heo88574552006-06-25 20:00:35 +09002293 "(%s, err_mask=0x%x)\n", reason, err_mask);
Tejun Heo49016ac2006-02-21 02:12:11 +09002294 return rc;
2295}
2296
Tejun Heo3373efd2006-05-15 20:57:53 +09002297static inline u8 ata_dev_knobble(struct ata_device *dev)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002298{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002299 struct ata_port *ap = dev->link->ap;
2300 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002301}
2302
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002303static void ata_dev_config_ncq(struct ata_device *dev,
2304 char *desc, size_t desc_sz)
2305{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002306 struct ata_port *ap = dev->link->ap;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002307 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 }
Tejun Heo75683fe2007-07-05 13:31:27 +09002313 if (dev->horkage & ATA_HORKAGE_NONCQ) {
Alan Cox6919a0a2006-10-27 19:08:46 -07002314 snprintf(desc, desc_sz, "NCQ (not used)");
2315 return;
2316 }
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002317 if (ap->flags & ATA_FLAG_NCQ) {
Jeff Garzikcca39742006-08-24 03:19:22 -04002318 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002319 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
Tejun Heo49016ac2006-02-21 02:12:11 +09002328/**
Tejun Heoffeae412006-03-01 16:09:35 +09002329 * ata_dev_configure - Configure the specified ATA/ATAPI device
Tejun Heoffeae412006-03-01 16:09:35 +09002330 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 *
Tejun Heoffeae412006-03-01 16:09:35 +09002332 * Configure @dev according to @dev->id. Generic and low-level
2333 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 *
2335 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09002336 * Kernel thread context (may sleep)
2337 *
2338 * RETURNS:
2339 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 */
Tejun Heoefdaedc2006-11-01 18:38:52 +09002341int ata_dev_configure(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002343 struct ata_port *ap = dev->link->ap;
2344 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo67465442007-05-15 03:28:16 +09002345 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
Tejun Heo1148c3a2006-03-13 19:48:04 +09002346 const u16 *id = dev->id;
Tejun Heo7dc951a2007-11-27 19:43:42 +09002347 unsigned long xfer_mask;
Alan Coxb352e572006-08-10 18:52:12 +01002348 char revbuf[7]; /* XYZ-99\0 */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002349 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2350 char modelbuf[ATA_ID_PROD_LEN+1];
Brian Kinge6d902a2006-06-28 08:30:31 -05002351 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002353 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
Tejun Heo44877b42007-02-21 01:06:51 +09002354 ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08002355 __func__);
Tejun Heoffeae412006-03-01 16:09:35 +09002356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 }
2358
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002359 if (ata_msg_probe(ap))
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08002360 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Tejun Heo75683fe2007-07-05 13:31:27 +09002362 /* set horkage */
2363 dev->horkage |= ata_dev_blacklisted(dev);
Tejun Heo33267322008-02-13 09:15:09 +09002364 ata_force_horkage(dev);
Tejun Heo75683fe2007-07-05 13:31:27 +09002365
Tejun Heo67465442007-05-15 03:28:16 +09002366 /* let ACPI work its magic */
2367 rc = ata_acpi_on_devcfg(dev);
2368 if (rc)
2369 return rc;
Kristen Carlson Accardi08573a82006-11-10 16:14:47 -08002370
Tejun Heo05027ad2007-09-03 12:32:57 +09002371 /* massage HPA, do it early as it might change IDENTIFY data */
2372 rc = ata_hpa_resize(dev);
2373 if (rc)
2374 return rc;
2375
Tejun Heoc39f5eb2006-03-13 19:51:19 +09002376 /* print device capabilities */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002377 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002378 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",
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08002381 __func__,
Tejun Heof15a1da2006-05-15 20:57:56 +09002382 id[49], id[82], id[83], id[84],
2383 id[85], id[86], id[87], id[88]);
Tejun Heoc39f5eb2006-03-13 19:51:19 +09002384
Tejun Heo208a9932006-03-05 17:55:58 +09002385 /* initialize to-be-configured parameters */
Tejun Heoea1dd4e2006-04-02 18:51:53 +09002386 dev->flags &= ~ATA_DFLAG_CFG_MASK;
Tejun Heo208a9932006-03-05 17:55:58 +09002387 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 /*
2395 * common ATA, ATAPI feature tests
2396 */
2397
Tejun Heoff8854b2006-03-06 04:31:56 +09002398 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002399 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002401 if (ata_msg_probe(ap))
2402 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Albert Leeef143d52007-06-05 13:01:33 +08002404 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 /* ATA-specific feature tests */
2412 if (dev->class == ATA_DEV_ATA) {
Alan Coxb352e572006-08-10 18:52:12 +01002413 if (ata_id_is_cfa(id)) {
2414 if (id[162] & 1) /* CPRM may make this media unusable */
Tejun Heo44877b42007-02-21 01:06:51 +09002415 ata_dev_printk(dev, KERN_WARNING,
2416 "supports DRM functions and may "
2417 "not be fully accessable.\n");
Alan Coxb352e572006-08-10 18:52:12 +01002418 snprintf(revbuf, 7, "CFA");
Alan Coxae8d4ee2007-11-04 22:05:49 -05002419 } else {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002420 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
Alan Coxae8d4ee2007-11-04 22:05:49 -05002421 /* 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 }
Alan Coxb352e572006-08-10 18:52:12 +01002427
Tejun Heo1148c3a2006-03-13 19:48:04 +09002428 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09002429
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002430 if (dev->id[59] & 0x100)
2431 dev->multi_count = dev->id[59] & 0xff;
2432
Tejun Heo1148c3a2006-03-13 19:48:04 +09002433 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09002434 const char *lba_desc;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002435 char ncq_desc[20];
Albert Lee8bf62ec2005-05-12 15:29:42 -04002436
Tejun Heo4c2d7212006-03-05 17:55:58 +09002437 lba_desc = "LBA";
2438 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09002439 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04002440 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09002441 lba_desc = "LBA48";
Tejun Heo6fc49ad2006-11-11 20:10:45 +09002442
2443 if (dev->n_sectors >= (1UL << 28) &&
2444 ata_id_has_flush_ext(id))
2445 dev->flags |= ATA_DFLAG_FLUSH_EXT;
Tejun Heo4c2d7212006-03-05 17:55:58 +09002446 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04002447
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002448 /* config NCQ */
2449 ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2450
Albert Lee8bf62ec2005-05-12 15:29:42 -04002451 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002452 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",
Tejun Heof15a1da2006-05-15 20:57:56 +09002459 (unsigned long long)dev->n_sectors,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002460 dev->multi_count, lba_desc, ncq_desc);
2461 }
Tejun Heoffeae412006-03-01 16:09:35 +09002462 } else {
Albert Lee8bf62ec2005-05-12 15:29:42 -04002463 /* CHS */
2464
2465 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002466 dev->cylinders = id[1];
2467 dev->heads = id[3];
2468 dev->sectors = id[6];
Albert Lee8bf62ec2005-05-12 15:29:42 -04002469
Tejun Heo1148c3a2006-03-13 19:48:04 +09002470 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04002471 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002472 dev->cylinders = id[54];
2473 dev->heads = id[55];
2474 dev->sectors = id[56];
Albert Lee8bf62ec2005-05-12 15:29:42 -04002475 }
2476
2477 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002478 if (ata_msg_drv(ap) && print_info) {
Tejun Heo88574552006-06-25 20:00:35 +09002479 ata_dev_printk(dev, KERN_INFO,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002480 "%s: %s, %s, max %s\n",
2481 revbuf, modelbuf, fwrevbuf,
2482 ata_mode_string(xfer_mask));
Jeff Garzika84471f2007-02-26 05:51:33 -05002483 ata_dev_printk(dev, KERN_INFO,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002484 "%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 }
Albert Lee07f6f7d2005-11-01 19:33:20 +08002489 }
2490
Tejun Heo6e7846e2006-02-12 23:32:58 +09002491 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 }
2493
2494 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002495 else if (dev->class == ATA_DEV_ATAPI) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002496 const char *cdb_intr_string = "";
2497 const char *atapi_an_string = "";
Tejun Heo91163002008-02-21 13:25:50 +09002498 const char *dma_dir_string = "";
Tejun Heo7d77b242007-09-23 13:14:13 +09002499 u32 sntf;
Albert Lee08a556d2006-03-31 13:29:04 +08002500
Tejun Heo1148c3a2006-03-13 19:48:04 +09002501 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002503 if (ata_msg_warn(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002504 ata_dev_printk(dev, KERN_WARNING,
2505 "unsupported CDB len\n");
Tejun Heoffeae412006-03-01 16:09:35 +09002506 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 goto err_out_nosup;
2508 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09002509 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Tejun Heo7d77b242007-09-23 13:14:13 +09002511 /* 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.
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002515 */
Tejun Heo7d77b242007-09-23 13:14:13 +09002516 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)) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002519 unsigned int err_mask;
2520
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002521 /* issue SET feature command to turn this on */
Jeff Garzik218f3d32007-10-25 00:33:27 -04002522 err_mask = ata_dev_set_feature(dev,
2523 SETFEATURES_SATA_ENABLE, SATA_AN);
Tejun Heo854c73a2007-09-23 13:14:11 +09002524 if (err_mask)
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002525 ata_dev_printk(dev, KERN_ERR,
Tejun Heo854c73a2007-09-23 13:14:11 +09002526 "failed to enable ATAPI AN "
2527 "(err_mask=0x%x)\n", err_mask);
2528 else {
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002529 dev->flags |= ATA_DFLAG_AN;
Tejun Heo854c73a2007-09-23 13:14:11 +09002530 atapi_an_string = ", ATAPI AN";
2531 }
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002532 }
2533
Albert Lee08a556d2006-03-31 13:29:04 +08002534 if (ata_id_cdb_intr(dev->id)) {
Albert Lee312f7da2005-09-27 17:38:03 +08002535 dev->flags |= ATA_DFLAG_CDB_INTR;
Albert Lee08a556d2006-03-31 13:29:04 +08002536 cdb_intr_string = ", CDB intr";
2537 }
Albert Lee312f7da2005-09-27 17:38:03 +08002538
Tejun Heo91163002008-02-21 13:25:50 +09002539 if (atapi_dmadir || atapi_id_dmadir(dev->id)) {
2540 dev->flags |= ATA_DFLAG_DMADIR;
2541 dma_dir_string = ", DMADIR";
2542 }
2543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02002545 if (ata_msg_drv(ap) && print_info)
Albert Leeef143d52007-06-05 13:01:33 +08002546 ata_dev_printk(dev, KERN_INFO,
Tejun Heo91163002008-02-21 13:25:50 +09002547 "ATAPI: %s, %s, max %s%s%s%s\n",
Albert Leeef143d52007-06-05 13:01:33 +08002548 modelbuf, fwrevbuf,
Tejun Heo12436c32006-05-15 20:59:15 +09002549 ata_mode_string(xfer_mask),
Tejun Heo91163002008-02-21 13:25:50 +09002550 cdb_intr_string, atapi_an_string,
2551 dma_dir_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 }
2553
Tejun Heo914ed352006-11-01 18:39:55 +09002554 /* 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
Kristen Carlson Accardica773292007-10-25 00:58:59 -04002559 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
Alan Coxc5038fc2007-10-25 14:21:16 +01002566 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2567 200 sectors */
Tejun Heo3373efd2006-05-15 20:57:53 +09002568 if (ata_dev_knobble(dev)) {
Borislav Petkov5afc8142006-06-27 14:51:25 +02002569 if (ata_msg_drv(ap) && print_info)
Tejun Heof15a1da2006-05-15 20:57:56 +09002570 ata_dev_printk(dev, KERN_INFO,
2571 "applying bridge limits\n");
Tejun Heo5a529132006-03-24 14:07:50 +09002572 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002573 dev->max_sectors = ATA_MAX_SECTORS;
2574 }
2575
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002576 if ((dev->class == ATA_DEV_ATAPI) &&
Albert Leef442cd82007-11-15 10:35:47 +09002577 (atapi_command_packet_set(id) == TYPE_TAPE)) {
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002578 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
Albert Leef442cd82007-11-15 10:35:47 +09002579 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2580 }
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002581
Tejun Heo75683fe2007-07-05 13:31:27 +09002582 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
Tejun Heo03ec52d2007-04-12 13:38:11 +09002583 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2584 dev->max_sectors);
Albert Lee18d6e9d2007-04-02 11:34:15 +08002585
Kristen Carlson Accardica773292007-10-25 00:58:59 -04002586 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
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002593 if (ap->ops->dev_config)
Alancd0d3bb2007-03-02 00:56:15 +00002594 ap->ops->dev_config(dev);
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002595
Alan Coxc5038fc2007-10-25 14:21:16 +01002596 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
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002611 if (ata_msg_probe(ap))
2612 ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n",
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08002613 __func__, ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09002614 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616err_out_nosup:
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002617 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002618 ata_dev_printk(dev, KERN_DEBUG,
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08002619 "%s: EXIT, err\n", __func__);
Tejun Heoffeae412006-03-01 16:09:35 +09002620 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621}
2622
2623/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002624 * ata_cable_40wire - return 40 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002625 * @ap: port
2626 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002627 * Helper method for drivers which want to hardwire 40 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002628 * detection.
2629 */
2630
2631int ata_cable_40wire(struct ata_port *ap)
2632{
2633 return ATA_CBL_PATA40;
2634}
2635
2636/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002637 * ata_cable_80wire - return 80 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002638 * @ap: port
2639 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002640 * Helper method for drivers which want to hardwire 80 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002641 * detection.
2642 */
2643
2644int 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
2656int ata_cable_unknown(struct ata_port *ap)
2657{
2658 return ATA_CBL_PATA_UNK;
2659}
2660
2661/**
Tejun Heoc88f90c2007-11-27 19:43:48 +09002662 * 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 */
2668int ata_cable_ignore(struct ata_port *ap)
2669{
2670 return ATA_CBL_PATA_IGN;
2671}
2672
2673/**
Alan Coxbe0d18d2007-03-06 02:37:56 -08002674 * ata_cable_sata - return SATA cable type
2675 * @ap: port
2676 *
2677 * Helper method for drivers which have SATA cables
2678 */
2679
2680int ata_cable_sata(struct ata_port *ap)
2681{
2682 return ATA_CBL_SATA;
2683}
2684
2685/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 * ata_bus_probe - Reset and probe ATA bus
2687 * @ap: Bus to probe
2688 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002689 * Master ATA bus probing function. Initiates a hardware-dependent
2690 * bus reset, then attempts to identify any devices found on
2691 * the bus.
2692 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002694 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 *
2696 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09002697 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 */
2699
Brian King80289162006-08-07 14:27:31 -05002700int ata_bus_probe(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701{
Tejun Heo28ca5c52006-03-01 16:09:36 +09002702 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002703 int tries[ATA_MAX_DEVICES];
Tejun Heof58229f2007-08-06 18:36:23 +09002704 int rc;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002705 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Tejun Heo28ca5c52006-03-01 16:09:36 +09002707 ata_port_probe(ap);
2708
Tejun Heof58229f2007-08-06 18:36:23 +09002709 ata_link_for_each_dev(dev, &ap->link)
2710 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002711
2712 retry:
Tejun Heocdeab112007-10-29 16:41:09 +09002713 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
Tejun Heo20444702006-03-13 01:57:01 +09002732 /* reset and determine device classes */
Tejun Heo52783c52006-05-31 18:28:22 +09002733 ap->ops->phy_reset(ap);
Tejun Heo2061a472006-03-12 00:57:39 +09002734
Tejun Heof58229f2007-08-06 18:36:23 +09002735 ata_link_for_each_dev(dev, &ap->link) {
Tejun Heo52783c52006-05-31 18:28:22 +09002736 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;
Tejun Heo20444702006-03-13 01:57:01 +09002741
Tejun Heo52783c52006-05-31 18:28:22 +09002742 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo28ca5c52006-03-01 16:09:36 +09002743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Tejun Heo52783c52006-05-31 18:28:22 +09002745 ata_port_probe(ap);
Tejun Heo20444702006-03-13 01:57:01 +09002746
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002747 /* 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
Bartlomiej Zolnierkiewicza4ba7fe2008-04-02 10:35:15 +09002751 ata_link_for_each_dev_reverse(dev, &ap->link) {
Tejun Heof58229f2007-08-06 18:36:23 +09002752 if (tries[dev->devno])
2753 dev->class = classes[dev->devno];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002754
Tejun Heoe1211e32006-04-01 01:38:18 +09002755 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09002756 continue;
2757
Tejun Heobff04642006-11-10 18:08:10 +09002758 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2759 dev->id);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002760 if (rc)
2761 goto fail;
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002762 }
2763
Alan Coxbe0d18d2007-03-06 02:37:56 -08002764 /* 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
Alan Cox614fe292007-08-22 23:22:45 +01002768 /* 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
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002779 /* 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
Tejun Heof58229f2007-08-06 18:36:23 +09002782 ata_link_for_each_dev(dev, &ap->link) {
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002783 if (!ata_dev_enabled(dev))
2784 continue;
Tejun Heoffeae412006-03-01 16:09:35 +09002785
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002786 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
Tejun Heoefdaedc2006-11-01 18:38:52 +09002787 rc = ata_dev_configure(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002788 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002789 if (rc)
2790 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 }
2792
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002793 /* configure transfer mode */
Tejun Heo02607312007-08-06 18:36:23 +09002794 rc = ata_set_mode(&ap->link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002795 if (rc)
Tejun Heo51713d32006-04-11 22:26:29 +09002796 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Tejun Heof58229f2007-08-06 18:36:23 +09002798 ata_link_for_each_dev(dev, &ap->link)
2799 if (ata_dev_enabled(dev))
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002800 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01002801
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002802 /* no device present, disable port */
2803 ata_port_disable(ap);
Tejun Heo96072e62006-04-01 01:38:17 +09002804 return -ENODEV;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002805
2806 fail:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002807 tries[dev->devno]--;
2808
Tejun Heo14d2bac2006-04-02 17:54:46 +09002809 switch (rc) {
2810 case -EINVAL:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002811 /* eeek, something went very wrong, give up */
Tejun Heo14d2bac2006-04-02 17:54:46 +09002812 tries[dev->devno] = 0;
2813 break;
Tejun Heo4ae72a12007-02-02 16:22:30 +09002814
2815 case -ENODEV:
2816 /* give it just one more chance */
2817 tries[dev->devno] = min(tries[dev->devno], 1);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002818 case -EIO:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002819 if (tries[dev->devno] == 1) {
2820 /* This is the last chance, better to slow
2821 * down than lose it.
2822 */
Tejun Heo936fd732007-08-06 18:36:23 +09002823 sata_down_spd_limit(&ap->link);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002824 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2825 }
Tejun Heo14d2bac2006-04-02 17:54:46 +09002826 }
2827
Tejun Heo4ae72a12007-02-02 16:22:30 +09002828 if (!tries[dev->devno])
Tejun Heo3373efd2006-05-15 20:57:53 +09002829 ata_dev_disable(dev);
Tejun Heoec573752006-04-11 22:26:29 +09002830
Tejun Heo14d2bac2006-04-02 17:54:46 +09002831 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832}
2833
2834/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002835 * ata_port_probe - Mark port as enabled
2836 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002838 * Modify @ap data structure such that the system
2839 * thinks that the entire port is enabled.
2840 *
Jeff Garzikcca39742006-08-24 03:19:22 -04002841 * LOCKING: host lock, or some other form of
Jeff Garzik0cba6322005-05-30 19:49:12 -04002842 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 */
2844
2845void ata_port_probe(struct ata_port *ap)
2846{
Tejun Heo198e0fe2006-04-02 18:51:52 +09002847 ap->flags &= ~ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848}
2849
2850/**
Tejun Heo3be680b2005-12-19 22:35:02 +09002851 * sata_print_link_status - Print SATA link status
Tejun Heo936fd732007-08-06 18:36:23 +09002852 * @link: SATA link to printk link status about
Tejun Heo3be680b2005-12-19 22:35:02 +09002853 *
2854 * This function prints link speed and status of a SATA link.
2855 *
2856 * LOCKING:
2857 * None.
2858 */
Tejun Heo936fd732007-08-06 18:36:23 +09002859void sata_print_link_status(struct ata_link *link)
Tejun Heo3be680b2005-12-19 22:35:02 +09002860{
Tejun Heo6d5f9732006-04-03 00:09:41 +09002861 u32 sstatus, scontrol, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09002862
Tejun Heo936fd732007-08-06 18:36:23 +09002863 if (sata_scr_read(link, SCR_STATUS, &sstatus))
Tejun Heo3be680b2005-12-19 22:35:02 +09002864 return;
Tejun Heo936fd732007-08-06 18:36:23 +09002865 sata_scr_read(link, SCR_CONTROL, &scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002866
Tejun Heo936fd732007-08-06 18:36:23 +09002867 if (ata_link_online(link)) {
Tejun Heo3be680b2005-12-19 22:35:02 +09002868 tmp = (sstatus >> 4) & 0xf;
Tejun Heo936fd732007-08-06 18:36:23 +09002869 ata_link_printk(link, KERN_INFO,
Tejun Heof15a1da2006-05-15 20:57:56 +09002870 "SATA link up %s (SStatus %X SControl %X)\n",
2871 sata_spd_string(tmp), sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002872 } else {
Tejun Heo936fd732007-08-06 18:36:23 +09002873 ata_link_printk(link, KERN_INFO,
Tejun Heof15a1da2006-05-15 20:57:56 +09002874 "SATA link down (SStatus %X SControl %X)\n",
2875 sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002876 }
2877}
2878
2879/**
Alan Coxebdfca62006-03-23 15:38:34 +00002880 * ata_dev_pair - return other device on cable
Alan Coxebdfca62006-03-23 15:38:34 +00002881 * @adev: device
2882 *
2883 * Obtain the other device on the same cable, or if none is
2884 * present NULL is returned
2885 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002886
Tejun Heo3373efd2006-05-15 20:57:53 +09002887struct ata_device *ata_dev_pair(struct ata_device *adev)
Alan Coxebdfca62006-03-23 15:38:34 +00002888{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002889 struct ata_link *link = adev->link;
2890 struct ata_device *pair = &link->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09002891 if (!ata_dev_enabled(pair))
Alan Coxebdfca62006-03-23 15:38:34 +00002892 return NULL;
2893 return pair;
2894}
2895
2896/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002897 * ata_port_disable - Disable port.
2898 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002900 * 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 *
Jeff Garzikcca39742006-08-24 03:19:22 -04002905 * LOCKING: host lock, or some other form of
Jeff Garzik780a87f2005-05-30 15:41:05 -04002906 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 */
2908
2909void ata_port_disable(struct ata_port *ap)
2910{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002911 ap->link.device[0].class = ATA_DEV_NONE;
2912 ap->link.device[1].class = ATA_DEV_NONE;
Tejun Heo198e0fe2006-04-02 18:51:52 +09002913 ap->flags |= ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914}
2915
Tejun Heo1c3fae42006-04-02 20:53:28 +09002916/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002917 * sata_down_spd_limit - adjust SATA spd limit downward
Tejun Heo936fd732007-08-06 18:36:23 +09002918 * @link: Link to adjust SATA spd limit for
Tejun Heo1c3fae42006-04-02 20:53:28 +09002919 *
Tejun Heo936fd732007-08-06 18:36:23 +09002920 * Adjust SATA spd limit of @link downward. Note that this
Tejun Heo1c3fae42006-04-02 20:53:28 +09002921 * function only adjusts the limit. The change must be applied
Tejun Heo3c567b72006-05-15 20:57:23 +09002922 * using sata_set_spd().
Tejun Heo1c3fae42006-04-02 20:53:28 +09002923 *
2924 * LOCKING:
2925 * Inherited from caller.
2926 *
2927 * RETURNS:
2928 * 0 on success, negative errno on failure
2929 */
Tejun Heo936fd732007-08-06 18:36:23 +09002930int sata_down_spd_limit(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002931{
Tejun Heo81952c52006-05-15 20:57:47 +09002932 u32 sstatus, spd, mask;
2933 int rc, highbit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002934
Tejun Heo936fd732007-08-06 18:36:23 +09002935 if (!sata_scr_valid(link))
Tejun Heo008a7892007-07-16 14:29:40 +09002936 return -EOPNOTSUPP;
2937
2938 /* If SCR can be read, use it to determine the current SPD.
Tejun Heo936fd732007-08-06 18:36:23 +09002939 * If not, use cached value in link->sata_spd.
Tejun Heo008a7892007-07-16 14:29:40 +09002940 */
Tejun Heo936fd732007-08-06 18:36:23 +09002941 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
Tejun Heo008a7892007-07-16 14:29:40 +09002942 if (rc == 0)
2943 spd = (sstatus >> 4) & 0xf;
2944 else
Tejun Heo936fd732007-08-06 18:36:23 +09002945 spd = link->sata_spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002946
Tejun Heo936fd732007-08-06 18:36:23 +09002947 mask = link->sata_spd_limit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002948 if (mask <= 1)
2949 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09002950
2951 /* unconditionally mask off the highest bit */
Tejun Heo1c3fae42006-04-02 20:53:28 +09002952 highbit = fls(mask) - 1;
2953 mask &= ~(1 << highbit);
2954
Tejun Heo008a7892007-07-16 14:29:40 +09002955 /* 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? */
Tejun Heo1c3fae42006-04-02 20:53:28 +09002964 if (!mask)
2965 return -EINVAL;
2966
Tejun Heo936fd732007-08-06 18:36:23 +09002967 link->sata_spd_limit = mask;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002968
Tejun Heo936fd732007-08-06 18:36:23 +09002969 ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09002970 sata_spd_string(fls(mask)));
Tejun Heo1c3fae42006-04-02 20:53:28 +09002971
2972 return 0;
2973}
2974
Tejun Heo936fd732007-08-06 18:36:23 +09002975static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002976{
Tejun Heo52702222007-10-31 10:17:07 +09002977 struct ata_link *host_link = &link->ap->link;
2978 u32 limit, target, spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002979
Tejun Heo52702222007-10-31 10:17:07 +09002980 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;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002991 else
Tejun Heo52702222007-10-31 10:17:07 +09002992 target = fls(limit);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002993
2994 spd = (*scontrol >> 4) & 0xf;
Tejun Heo52702222007-10-31 10:17:07 +09002995 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002996
Tejun Heo52702222007-10-31 10:17:07 +09002997 return spd != target;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002998}
2999
3000/**
Tejun Heo3c567b72006-05-15 20:57:23 +09003001 * sata_set_spd_needed - is SATA spd configuration needed
Tejun Heo936fd732007-08-06 18:36:23 +09003002 * @link: Link in question
Tejun Heo1c3fae42006-04-02 20:53:28 +09003003 *
3004 * Test whether the spd limit in SControl matches
Tejun Heo936fd732007-08-06 18:36:23 +09003005 * @link->sata_spd_limit. This function is used to determine
Tejun Heo1c3fae42006-04-02 20:53:28 +09003006 * 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 */
Tejun Heo936fd732007-08-06 18:36:23 +09003015int sata_set_spd_needed(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09003016{
3017 u32 scontrol;
3018
Tejun Heo936fd732007-08-06 18:36:23 +09003019 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
Tejun Heodb64bcf2007-10-31 10:17:06 +09003020 return 1;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003021
Tejun Heo936fd732007-08-06 18:36:23 +09003022 return __sata_set_spd_needed(link, &scontrol);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003023}
3024
3025/**
Tejun Heo3c567b72006-05-15 20:57:23 +09003026 * sata_set_spd - set SATA spd according to spd limit
Tejun Heo936fd732007-08-06 18:36:23 +09003027 * @link: Link to set SATA spd for
Tejun Heo1c3fae42006-04-02 20:53:28 +09003028 *
Tejun Heo936fd732007-08-06 18:36:23 +09003029 * Set SATA spd of @link according to sata_spd_limit.
Tejun Heo1c3fae42006-04-02 20:53:28 +09003030 *
3031 * LOCKING:
3032 * Inherited from caller.
3033 *
3034 * RETURNS:
3035 * 0 if spd doesn't need to be changed, 1 if spd has been
Tejun Heo81952c52006-05-15 20:57:47 +09003036 * changed. Negative errno if SCR registers are inaccessible.
Tejun Heo1c3fae42006-04-02 20:53:28 +09003037 */
Tejun Heo936fd732007-08-06 18:36:23 +09003038int sata_set_spd(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09003039{
3040 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003041 int rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003042
Tejun Heo936fd732007-08-06 18:36:23 +09003043 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003044 return rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003045
Tejun Heo936fd732007-08-06 18:36:23 +09003046 if (!__sata_set_spd_needed(link, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09003047 return 0;
3048
Tejun Heo936fd732007-08-06 18:36:23 +09003049 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003050 return rc;
3051
Tejun Heo1c3fae42006-04-02 20:53:28 +09003052 return 1;
3053}
3054
Alan Cox452503f2005-10-21 19:01:32 -04003055/*
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/*
Alan Coxb352e572006-08-10 18:52:12 +01003060 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
Alan Cox452503f2005-10-21 19:01:32 -04003061 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
Alan Coxb352e572006-08-10 18:52:12 +01003062 * 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.
Alan Cox452503f2005-10-21 19:01:32 -04003065 */
3066
3067static const struct ata_timing ata_timing[] = {
Tejun Heo70cd0712007-11-27 19:43:40 +09003068/* { 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 },
Alan Cox452503f2005-10-21 19:01:32 -04003076
Tejun Heo70cd0712007-11-27 19:43:40 +09003077 { 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 },
Alan Cox452503f2005-10-21 19:01:32 -04003080
Tejun Heo70cd0712007-11-27 19:43:40 +09003081 { 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 },
Alan Coxb352e572006-08-10 18:52:12 +01003084 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
Tejun Heo70cd0712007-11-27 19:43:40 +09003085 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04003086
3087/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
Tejun Heo70cd0712007-11-27 19:43:40 +09003088 { 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 },
Alan Cox452503f2005-10-21 19:01:32 -04003095
3096 { 0xFF }
3097};
3098
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003099#define ENOUGH(v, unit) (((v)-1)/(unit)+1)
3100#define EZ(v, unit) ((v)?ENOUGH(v, unit):0)
Alan Cox452503f2005-10-21 19:01:32 -04003101
3102static 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
3114void 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
Tejun Heo63573572007-11-27 19:43:39 +09003127const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
Alan Cox452503f2005-10-21 19:01:32 -04003128{
Tejun Heo70cd0712007-11-27 19:43:40 +09003129 const struct ata_timing *t = ata_timing;
Alan Cox452503f2005-10-21 19:01:32 -04003130
Tejun Heo70cd0712007-11-27 19:43:40 +09003131 while (xfer_mode > t->mode)
3132 t++;
3133
3134 if (xfer_mode == t->mode)
3135 return t;
3136 return NULL;
Alan Cox452503f2005-10-21 19:01:32 -04003137}
3138
3139int 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 /*
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003146 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08003147 */
Alan Cox452503f2005-10-21 19:01:32 -04003148
3149 if (!(s = ata_timing_find_mode(speed)))
3150 return -EINVAL;
3151
Albert Lee75b1f2f2005-11-16 17:06:18 +08003152 memcpy(t, s, sizeof(*s));
3153
Alan Cox452503f2005-10-21 19:01:32 -04003154 /*
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));
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003161 if (speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
Alan Cox452503f2005-10-21 19:01:32 -04003162 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];
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003164 } else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
Alan Cox452503f2005-10-21 19:01:32 -04003165 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
Albert Lee75b1f2f2005-11-16 17:06:18 +08003174 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04003175
3176 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003177 * 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.
Alan Cox452503f2005-10-21 19:01:32 -04003180 */
3181
Alanfd3367a2006-12-07 12:41:18 +00003182 if (speed > XFER_PIO_6) {
Alan Cox452503f2005-10-21 19:01:32 -04003183 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3184 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3185 }
3186
3187 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003188 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04003189 */
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 }
Jeff Garzika617c092007-05-21 20:14:23 -04003200
Alan Cox4f701d12007-04-23 11:55:36 +01003201 /* 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;
Alan Cox452503f2005-10-21 19:01:32 -04003206
3207 return 0;
3208}
3209
Tejun Heocf176e12006-04-02 17:54:46 +09003210/**
Tejun Heoa0f79b92007-12-18 16:33:05 +09003211 * 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 */
3226u8 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/**
Tejun Heocf176e12006-04-02 17:54:46 +09003262 * ata_down_xfermask_limit - adjust dev xfer masks downward
Tejun Heocf176e12006-04-02 17:54:46 +09003263 * @dev: Device to adjust xfer masks
Tejun Heo458337d2007-02-02 16:22:30 +09003264 * @sel: ATA_DNXFER_* selector
Tejun Heocf176e12006-04-02 17:54:46 +09003265 *
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 */
Tejun Heo458337d2007-02-02 16:22:30 +09003276int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
Tejun Heocf176e12006-04-02 17:54:46 +09003277{
Tejun Heo458337d2007-02-02 16:22:30 +09003278 char buf[32];
Tejun Heo7dc951a2007-11-27 19:43:42 +09003279 unsigned long orig_mask, xfer_mask;
3280 unsigned long pio_mask, mwdma_mask, udma_mask;
Tejun Heo458337d2007-02-02 16:22:30 +09003281 int quiet, highbit;
Tejun Heocf176e12006-04-02 17:54:46 +09003282
Tejun Heo458337d2007-02-02 16:22:30 +09003283 quiet = !!(sel & ATA_DNXFER_QUIET);
3284 sel &= ~ATA_DNXFER_QUIET;
Tejun Heocf176e12006-04-02 17:54:46 +09003285
Tejun Heo458337d2007-02-02 16:22:30 +09003286 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);
Tejun Heocf176e12006-04-02 17:54:46 +09003290
Tejun Heo458337d2007-02-02 16:22:30 +09003291 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
Tejun Heo458337d2007-02-02 16:22:30 +09003322 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 }
Tejun Heocf176e12006-04-02 17:54:46 +09003343
3344 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3345 &dev->udma_mask);
3346
Tejun Heocf176e12006-04-02 17:54:46 +09003347 return 0;
Tejun Heocf176e12006-04-02 17:54:46 +09003348}
3349
Tejun Heo3373efd2006-05-15 20:57:53 +09003350static int ata_dev_set_mode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003352 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo4055dee2008-02-07 10:34:08 +09003353 const char *dev_err_whine = "";
3354 int ign_dev_err = 0;
Tejun Heo83206a22006-03-24 15:25:31 +09003355 unsigned int err_mask;
3356 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
Tejun Heoe8384602006-04-02 18:51:53 +09003358 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 if (dev->xfer_shift == ATA_SHIFT_PIO)
3360 dev->flags |= ATA_DFLAG_PIO;
3361
Tejun Heo3373efd2006-05-15 20:57:53 +09003362 err_mask = ata_dev_set_xfermode(dev);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003363
Tejun Heo4055dee2008-02-07 10:34:08 +09003364 if (err_mask & ~AC_ERR_DEV)
3365 goto fail;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003366
Tejun Heo4055dee2008-02-07 10:34:08 +09003367 /* revalidate */
Tejun Heobaa1e782006-11-01 18:39:27 +09003368 ehc->i.flags |= ATA_EHI_POST_SETMODE;
Tejun Heo422c9da2007-09-23 13:14:12 +09003369 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
Tejun Heobaa1e782006-11-01 18:39:27 +09003370 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
Tejun Heo5eb45c02006-04-02 18:51:52 +09003371 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09003372 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09003373
Tejun Heo4055dee2008-02-07 10:34:08 +09003374 /* 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
Tejun Heo23e71c32006-03-06 04:31:57 +09003402 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3403 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Tejun Heo4055dee2008-02-07 10:34:08 +09003405 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
Tejun Heo83206a22006-03-24 15:25:31 +09003409 return 0;
Tejun Heo4055dee2008-02-07 10:34:08 +09003410
3411 fail:
3412 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
3413 "(err_mask=0x%x)\n", err_mask);
3414 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415}
3416
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417/**
Alan04351822007-03-06 02:37:52 -08003418 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
Tejun Heo02607312007-08-06 18:36:23 +09003419 * @link: link on which timings will be programmed
Joe Perches1967b7f2008-02-03 17:08:11 +02003420 * @r_failed_dev: out parameter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 *
Alan04351822007-03-06 02:37:52 -08003422 * 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
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003425 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04003426 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003428 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003429 *
3430 * RETURNS:
3431 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 */
Alan04351822007-03-06 02:37:52 -08003433
Tejun Heo02607312007-08-06 18:36:23 +09003434int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435{
Tejun Heo02607312007-08-06 18:36:23 +09003436 struct ata_port *ap = link->ap;
Tejun Heoe8e06192006-04-01 01:38:18 +09003437 struct ata_device *dev;
Tejun Heof58229f2007-08-06 18:36:23 +09003438 int rc = 0, used_dma = 0, found = 0;
Tejun Heo3adcebb2006-05-15 20:57:37 +09003439
Tejun Heoa6d5a512006-03-06 04:31:57 +09003440 /* step 1: calculate xfer_mask */
Tejun Heof58229f2007-08-06 18:36:23 +09003441 ata_link_for_each_dev(dev, link) {
Tejun Heo7dc951a2007-11-27 19:43:42 +09003442 unsigned long pio_mask, dma_mask;
Alan Coxb3a70602007-10-02 12:38:26 -04003443 unsigned int mode_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003444
Tejun Heoe1211e32006-04-01 01:38:18 +09003445 if (!ata_dev_enabled(dev))
Tejun Heoa6d5a512006-03-06 04:31:57 +09003446 continue;
3447
Alan Coxb3a70602007-10-02 12:38:26 -04003448 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
Tejun Heo3373efd2006-05-15 20:57:53 +09003454 ata_dev_xfermask(dev);
Tejun Heo33267322008-02-13 09:15:09 +09003455 ata_force_xfermask(dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09003456
Tejun Heoacf356b2006-03-24 14:07:50 +09003457 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3458 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
Alan Coxb3a70602007-10-02 12:38:26 -04003459
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
Tejun Heoacf356b2006-03-24 14:07:50 +09003465 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3466 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01003467
Tejun Heo4f659772006-04-01 01:38:18 +09003468 found = 1;
Tejun Heo70cd0712007-11-27 19:43:40 +09003469 if (dev->dma_mode != 0xff)
Alan Cox5444a6f2006-03-27 18:58:20 +01003470 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003471 }
Tejun Heo4f659772006-04-01 01:38:18 +09003472 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003473 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003474
3475 /* step 2: always set host PIO timings */
Tejun Heof58229f2007-08-06 18:36:23 +09003476 ata_link_for_each_dev(dev, link) {
Tejun Heoe8e06192006-04-01 01:38:18 +09003477 if (!ata_dev_enabled(dev))
3478 continue;
3479
Tejun Heo70cd0712007-11-27 19:43:40 +09003480 if (dev->pio_mode == 0xff) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003481 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
Tejun Heoe8e06192006-04-01 01:38:18 +09003482 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003483 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09003484 }
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
Tejun Heoa6d5a512006-03-06 04:31:57 +09003492 /* step 3: set host DMA timings */
Tejun Heof58229f2007-08-06 18:36:23 +09003493 ata_link_for_each_dev(dev, link) {
Tejun Heo70cd0712007-11-27 19:43:40 +09003494 if (!ata_dev_enabled(dev) || dev->dma_mode == 0xff)
Tejun Heoe8e06192006-04-01 01:38:18 +09003495 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
3503 /* step 4: update devices' xfer mode */
Tejun Heof58229f2007-08-06 18:36:23 +09003504 ata_link_for_each_dev(dev, link) {
Alan18d90de2007-01-24 11:42:38 +00003505 /* don't update suspended devices' xfer mode */
Tejun Heo9666f402007-05-04 21:27:47 +02003506 if (!ata_dev_enabled(dev))
Tejun Heo83206a22006-03-24 15:25:31 +09003507 continue;
3508
Tejun Heo3373efd2006-05-15 20:57:53 +09003509 rc = ata_dev_set_mode(dev);
Tejun Heo5bbc53f2006-04-01 01:38:17 +09003510 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003511 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09003512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
Tejun Heoe8e06192006-04-01 01:38:18 +09003514 /* Record simplex status. If we selected DMA then the other
3515 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01003516 */
Jeff Garzikcca39742006-08-24 03:19:22 -04003517 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
Alan032af1c2007-03-01 17:36:46 +00003518 ap->host->simplex_claimed = ap;
Alan Cox5444a6f2006-03-27 18:58:20 +01003519
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003520 out:
3521 if (rc)
3522 *r_failed_dev = dev;
3523 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524}
3525
3526/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05003527 * 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:
Jeff Garzikcca39742006-08-24 03:19:22 -04003536 * spin_lock_irqsave(host lock)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05003537 */
3538
3539static 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/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 * 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 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003552 * Sleep until ATA Status register bit BSY clears,
3553 * or a timeout occurs.
3554 *
Tejun Heod1adc1b2006-10-09 18:32:15 +09003555 * LOCKING:
3556 * Kernel thread context (may sleep).
3557 *
3558 * RETURNS:
3559 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 */
Tejun Heod1adc1b2006-10-09 18:32:15 +09003561int ata_busy_sleep(struct ata_port *ap,
3562 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563{
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;
Tejun Heod1adc1b2006-10-09 18:32:15 +09003570 while (status != 0xff && (status & ATA_BUSY) &&
3571 time_before(jiffies, timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 msleep(50);
3573 status = ata_busy_wait(ap, ATA_BUSY, 3);
3574 }
3575
Tejun Heod1adc1b2006-10-09 18:32:15 +09003576 if (status != 0xff && (status & ATA_BUSY))
Tejun Heof15a1da2006-05-15 20:57:56 +09003577 ata_port_printk(ap, KERN_WARNING,
Jeff Garzik35aa7a42006-09-28 06:50:24 -04003578 "port is slow to respond, please be patient "
3579 "(Status 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
3581 timeout = timer_start + tmout;
Tejun Heod1adc1b2006-10-09 18:32:15 +09003582 while (status != 0xff && (status & ATA_BUSY) &&
3583 time_before(jiffies, timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 msleep(50);
3585 status = ata_chk_status(ap);
3586 }
3587
Tejun Heod1adc1b2006-10-09 18:32:15 +09003588 if (status == 0xff)
3589 return -ENODEV;
3590
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 if (status & ATA_BUSY) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003592 ata_port_printk(ap, KERN_ERR, "port failed to respond "
Jeff Garzik35aa7a42006-09-28 06:50:24 -04003593 "(%lu secs, Status 0x%x)\n",
3594 tmout / HZ, status);
Tejun Heod1adc1b2006-10-09 18:32:15 +09003595 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 }
3597
3598 return 0;
3599}
3600
Tejun Heod4b2bab2007-02-02 16:50:52 +09003601/**
Tejun Heo88ff6ea2007-10-16 14:21:24 -07003602 * 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 */
3616void 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.
Tejun Heo1974e202007-11-08 11:20:18 +09003639 *
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.
Tejun Heo88ff6ea2007-10-16 14:21:24 -07003643 */
Tejun Heo1974e202007-11-08 11:20:18 +09003644 if (ap->flags & ATA_FLAG_SATA) {
3645 while (1) {
3646 u8 status = ata_chk_status(ap);
Tejun Heo88ff6ea2007-10-16 14:21:24 -07003647
Tejun Heo1974e202007-11-08 11:20:18 +09003648 if (status != 0xff || time_after(jiffies, deadline))
3649 return;
Tejun Heo88ff6ea2007-10-16 14:21:24 -07003650
Tejun Heo1974e202007-11-08 11:20:18 +09003651 msleep(50);
3652 }
Tejun Heo88ff6ea2007-10-16 14:21:24 -07003653 }
3654}
3655
3656/**
Tejun Heod4b2bab2007-02-02 16:50:52 +09003657 * 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 */
3670int 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;
Tejun Heo936fd732007-08-06 18:36:23 +09003681 if (!ata_link_online(&ap->link) && status == 0xff)
Tejun Heod4b2bab2007-02-02 16:50:52 +09003682 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
3698static int ata_bus_post_reset(struct ata_port *ap, unsigned int devmask,
3699 unsigned long deadline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
3701 struct ata_ioports *ioaddr = &ap->ioaddr;
3702 unsigned int dev0 = devmask & (1 << 0);
3703 unsigned int dev1 = devmask & (1 << 1);
Tejun Heo9b893912007-02-02 16:50:52 +09003704 int rc, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705
3706 /* if device 0 was found in ata_devchk, wait for its
3707 * BSY bit to clear
3708 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003709 if (dev0) {
3710 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003711 if (rc) {
3712 if (rc != -ENODEV)
3713 return rc;
3714 ret = rc;
3715 }
Tejun Heod4b2bab2007-02-02 16:50:52 +09003716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717
Tejun Heoe141d992007-06-10 14:26:20 +09003718 /* if device 1 was found in ata_devchk, wait for register
3719 * access briefly, then wait for BSY to clear.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 */
Tejun Heoe141d992007-06-10 14:26:20 +09003721 if (dev1) {
3722 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
3724 ap->ops->dev_select(ap, 1);
Tejun Heoe141d992007-06-10 14:26:20 +09003725
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
Tejun Heod4b2bab2007-02-02 16:50:52 +09003740 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003741 if (rc) {
3742 if (rc != -ENODEV)
3743 return rc;
3744 ret = rc;
3745 }
Tejun Heod4b2bab2007-02-02 16:50:52 +09003746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747
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);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003754
Tejun Heo9b893912007-02-02 16:50:52 +09003755 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756}
3757
Tejun Heod4b2bab2007-02-02 16:50:52 +09003758static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
3759 unsigned long deadline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760{
3761 struct ata_ioports *ioaddr = &ap->ioaddr;
3762
Tejun Heo44877b42007-02-21 01:06:51 +09003763 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
3765 /* software reset. causes dev0 to be selected */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003766 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
Tejun Heo88ff6ea2007-10-16 14:21:24 -07003772 /* wait a while before checking status */
3773 ata_wait_after_reset(ap, deadline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003775 /* Before we perform post reset processing we want to see if
Tejun Heo298a41c2006-03-25 02:58:13 +09003776 * the bus shows 0xFF because the odd clown forgets the D7
3777 * pulldown resistor.
3778 */
Alan Cox150981b2007-10-23 16:50:02 +01003779 if (ata_chk_status(ap) == 0xFF)
Tejun Heo9b893912007-02-02 16:50:52 +09003780 return -ENODEV;
Alan Cox09c7ad72006-03-22 15:52:40 +00003781
Tejun Heod4b2bab2007-02-02 16:50:52 +09003782 return ata_bus_post_reset(ap, devmask, deadline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783}
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:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003798 * PCI/etc. bus probe sem.
Jeff Garzikcca39742006-08-24 03:19:22 -04003799 * Obtains host lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 *
3801 * SIDE EFFECTS:
Tejun Heo198e0fe2006-04-02 18:51:52 +09003802 * Sets ATA_FLAG_DISABLED if bus reset fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 */
3804
3805void ata_bus_reset(struct ata_port *ap)
3806{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003807 struct ata_device *device = ap->link.device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 struct ata_ioports *ioaddr = &ap->ioaddr;
3809 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
3810 u8 err;
Tejun Heoaec5c3c2006-03-25 01:33:34 +09003811 unsigned int dev0, dev1 = 0, devmask = 0;
Tejun Heo9b893912007-02-02 16:50:52 +09003812 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813
Tejun Heo44877b42007-02-21 01:06:51 +09003814 DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
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 */
Tejun Heo9b893912007-02-02 16:50:52 +09003834 if (ap->flags & ATA_FLAG_SRST) {
3835 rc = ata_bus_softreset(ap, devmask, jiffies + 40 * HZ);
3836 if (rc && rc != -ENODEV)
Tejun Heoaec5c3c2006-03-25 01:33:34 +09003837 goto err_out;
Tejun Heo9b893912007-02-02 16:50:52 +09003838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
3840 /*
3841 * determine by signature whether we have ATA or ATAPI devices
3842 */
Tejun Heo3f198592007-09-02 23:23:57 +09003843 device[0].class = ata_dev_try_classify(&device[0], dev0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 if ((slave_possible) && (err != 0x81))
Tejun Heo3f198592007-09-02 23:23:57 +09003845 device[1].class = ata_dev_try_classify(&device[1], dev1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 /* is double-select really necessary? */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003848 if (device[1].class != ATA_DEV_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 ap->ops->dev_select(ap, 1);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003850 if (device[0].class != ATA_DEV_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 ap->ops->dev_select(ap, 0);
3852
3853 /* if no devices were detected, disable this port */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003854 if ((device[0].class == ATA_DEV_NONE) &&
3855 (device[1].class == ATA_DEV_NONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 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 */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003860 iowrite8(ap->ctl, ioaddr->ctl_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 }
3862
3863 DPRINTK("EXIT\n");
3864 return;
3865
3866err_out:
Tejun Heof15a1da2006-05-15 20:57:56 +09003867 ata_port_printk(ap, KERN_ERR, "disabling port\n");
Jeff Garzikac8869d2007-08-16 03:17:03 -04003868 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869
3870 DPRINTK("EXIT\n");
3871}
3872
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003873/**
Tejun Heo936fd732007-08-06 18:36:23 +09003874 * sata_link_debounce - debounce SATA phy status
3875 * @link: ATA link to debounce SATA phy status for
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003876 * @params: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003877 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003878 *
Tejun Heo936fd732007-08-06 18:36:23 +09003879* Make sure SStatus of @link reaches stable state, determined by
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003880 * holding the same value where DET is not 1 for @duration polled
3881 * every @interval, before @timeout. Timeout constraints the
Tejun Heod4b2bab2007-02-02 16:50:52 +09003882 * beginning of the stable state. Because DET gets stuck at 1 on
3883 * some controllers after hot unplugging, this functions waits
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003884 * until timeout then returns 0 if DET is stable at 1.
3885 *
Tejun Heod4b2bab2007-02-02 16:50:52 +09003886 * @timeout is further limited by @deadline. The sooner of the
3887 * two is used.
3888 *
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003889 * LOCKING:
3890 * Kernel thread context (may sleep)
3891 *
3892 * RETURNS:
3893 * 0 on success, -errno on failure.
3894 */
Tejun Heo936fd732007-08-06 18:36:23 +09003895int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3896 unsigned long deadline)
Tejun Heo7a7921e2006-02-02 18:20:00 +09003897{
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003898 unsigned long interval_msec = params[0];
Tejun Heod4b2bab2007-02-02 16:50:52 +09003899 unsigned long duration = msecs_to_jiffies(params[1]);
3900 unsigned long last_jiffies, t;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003901 u32 last, cur;
3902 int rc;
3903
Tejun Heod4b2bab2007-02-02 16:50:52 +09003904 t = jiffies + msecs_to_jiffies(params[2]);
3905 if (time_before(t, deadline))
3906 deadline = t;
3907
Tejun Heo936fd732007-08-06 18:36:23 +09003908 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003909 return rc;
3910 cur &= 0xf;
3911
3912 last = cur;
3913 last_jiffies = jiffies;
3914
3915 while (1) {
3916 msleep(interval_msec);
Tejun Heo936fd732007-08-06 18:36:23 +09003917 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003918 return rc;
3919 cur &= 0xf;
3920
3921 /* DET stable? */
3922 if (cur == last) {
Tejun Heod4b2bab2007-02-02 16:50:52 +09003923 if (cur == 1 && time_before(jiffies, deadline))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003924 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
Tejun Heof1545152007-07-16 14:29:40 +09003934 /* Check deadline. If debouncing failed, return
3935 * -EPIPE to tell upper layer to lower link speed.
3936 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003937 if (time_after(jiffies, deadline))
Tejun Heof1545152007-07-16 14:29:40 +09003938 return -EPIPE;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003939 }
3940}
3941
3942/**
Tejun Heo936fd732007-08-06 18:36:23 +09003943 * sata_link_resume - resume SATA link
3944 * @link: ATA link to resume SATA
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003945 * @params: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003946 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003947 *
Tejun Heo936fd732007-08-06 18:36:23 +09003948 * Resume SATA phy @link and debounce it.
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003949 *
3950 * LOCKING:
3951 * Kernel thread context (may sleep)
3952 *
3953 * RETURNS:
3954 * 0 on success, -errno on failure.
3955 */
Tejun Heo936fd732007-08-06 18:36:23 +09003956int sata_link_resume(struct ata_link *link, const unsigned long *params,
3957 unsigned long deadline)
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003958{
3959 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003960 int rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003961
Tejun Heo936fd732007-08-06 18:36:23 +09003962 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003963 return rc;
3964
Tejun Heo852ee162006-04-01 01:38:18 +09003965 scontrol = (scontrol & 0x0f0) | 0x300;
Tejun Heo81952c52006-05-15 20:57:47 +09003966
Tejun Heo936fd732007-08-06 18:36:23 +09003967 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003968 return rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003969
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003970 /* Some PHYs react badly if SStatus is pounded immediately
3971 * after resuming. Delay 200ms before debouncing.
3972 */
3973 msleep(200);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003974
Tejun Heo936fd732007-08-06 18:36:23 +09003975 return sata_link_debounce(link, params, deadline);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003976}
3977
Tejun Heof5914a42006-05-31 18:27:48 +09003978/**
3979 * ata_std_prereset - prepare for reset
Tejun Heocc0680a2007-08-06 18:36:23 +09003980 * @link: ATA link to be reset
Tejun Heod4b2bab2007-02-02 16:50:52 +09003981 * @deadline: deadline jiffies for the operation
Tejun Heof5914a42006-05-31 18:27:48 +09003982 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003983 * @link is about to be reset. Initialize it. Failure from
Tejun Heob8cffc62007-02-02 16:50:52 +09003984 * 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.
Tejun Heof5914a42006-05-31 18:27:48 +09003988 *
3989 * LOCKING:
3990 * Kernel thread context (may sleep)
3991 *
3992 * RETURNS:
3993 * 0 on success, -errno otherwise.
3994 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003995int ata_std_prereset(struct ata_link *link, unsigned long deadline)
Tejun Heof5914a42006-05-31 18:27:48 +09003996{
Tejun Heocc0680a2007-08-06 18:36:23 +09003997 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09003998 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heoe9c83912006-07-03 16:07:26 +09003999 const unsigned long *timing = sata_ehc_deb_timing(ehc);
Tejun Heof5914a42006-05-31 18:27:48 +09004000 int rc;
4001
Tejun Heof5914a42006-05-31 18:27:48 +09004002 /* if we're about to do hardreset, nothing more to do */
4003 if (ehc->i.action & ATA_EH_HARDRESET)
4004 return 0;
4005
Tejun Heo936fd732007-08-06 18:36:23 +09004006 /* if SATA, resume link */
Tejun Heoa16abc02007-05-21 18:33:47 +02004007 if (ap->flags & ATA_FLAG_SATA) {
Tejun Heo936fd732007-08-06 18:36:23 +09004008 rc = sata_link_resume(link, timing, deadline);
Tejun Heob8cffc62007-02-02 16:50:52 +09004009 /* whine about phy resume failure but proceed */
4010 if (rc && rc != -EOPNOTSUPP)
Tejun Heocc0680a2007-08-06 18:36:23 +09004011 ata_link_printk(link, KERN_WARNING, "failed to resume "
Tejun Heof5914a42006-05-31 18:27:48 +09004012 "link for reset (errno=%d)\n", rc);
Tejun Heof5914a42006-05-31 18:27:48 +09004013 }
4014
Tejun Heo8cebf272008-01-24 00:05:14 +09004015 /* wait for !BSY if we don't know that no device is attached */
4016 if (!ata_link_offline(link)) {
Tejun Heob8cffc62007-02-02 16:50:52 +09004017 rc = ata_wait_ready(ap, deadline);
Tejun Heo6dffaf62007-05-23 11:58:52 +02004018 if (rc && rc != -ENODEV) {
Tejun Heocc0680a2007-08-06 18:36:23 +09004019 ata_link_printk(link, KERN_WARNING, "device not ready "
Tejun Heob8cffc62007-02-02 16:50:52 +09004020 "(errno=%d), forcing hardreset\n", rc);
4021 ehc->i.action |= ATA_EH_HARDRESET;
4022 }
4023 }
Tejun Heof5914a42006-05-31 18:27:48 +09004024
4025 return 0;
4026}
4027
Tejun Heoc2bd5802006-01-24 17:05:22 +09004028/**
4029 * ata_std_softreset - reset host port via ATA SRST
Tejun Heocc0680a2007-08-06 18:36:23 +09004030 * @link: ATA link to reset
Tejun Heoc2bd5802006-01-24 17:05:22 +09004031 * @classes: resulting classes of attached devices
Tejun Heod4b2bab2007-02-02 16:50:52 +09004032 * @deadline: deadline jiffies for the operation
Tejun Heoc2bd5802006-01-24 17:05:22 +09004033 *
Tejun Heo52783c52006-05-31 18:28:22 +09004034 * Reset host port using ATA SRST.
Tejun Heoc2bd5802006-01-24 17:05:22 +09004035 *
4036 * LOCKING:
4037 * Kernel thread context (may sleep)
4038 *
4039 * RETURNS:
4040 * 0 on success, -errno otherwise.
4041 */
Tejun Heocc0680a2007-08-06 18:36:23 +09004042int ata_std_softreset(struct ata_link *link, unsigned int *classes,
Tejun Heod4b2bab2007-02-02 16:50:52 +09004043 unsigned long deadline)
Tejun Heoc2bd5802006-01-24 17:05:22 +09004044{
Tejun Heocc0680a2007-08-06 18:36:23 +09004045 struct ata_port *ap = link->ap;
Tejun Heoc2bd5802006-01-24 17:05:22 +09004046 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
Tejun Heod4b2bab2007-02-02 16:50:52 +09004047 unsigned int devmask = 0;
4048 int rc;
Tejun Heoc2bd5802006-01-24 17:05:22 +09004049 u8 err;
4050
4051 DPRINTK("ENTER\n");
4052
Tejun Heo936fd732007-08-06 18:36:23 +09004053 if (ata_link_offline(link)) {
Tejun Heo3a397462006-02-10 23:58:48 +09004054 classes[0] = ATA_DEV_NONE;
4055 goto out;
4056 }
4057
Tejun Heoc2bd5802006-01-24 17:05:22 +09004058 /* 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
Tejun Heoc2bd5802006-01-24 17:05:22 +09004064 /* 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);
Tejun Heod4b2bab2007-02-02 16:50:52 +09004069 rc = ata_bus_softreset(ap, devmask, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09004070 /* if link is occupied, -ENODEV too is an error */
Tejun Heo936fd732007-08-06 18:36:23 +09004071 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
Tejun Heocc0680a2007-08-06 18:36:23 +09004072 ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
Tejun Heod4b2bab2007-02-02 16:50:52 +09004073 return rc;
Tejun Heoc2bd5802006-01-24 17:05:22 +09004074 }
4075
4076 /* determine by signature whether we have ATA or ATAPI devices */
Tejun Heo3f198592007-09-02 23:23:57 +09004077 classes[0] = ata_dev_try_classify(&link->device[0],
4078 devmask & (1 << 0), &err);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004079 if (slave_possible && err != 0x81)
Tejun Heo3f198592007-09-02 23:23:57 +09004080 classes[1] = ata_dev_try_classify(&link->device[1],
4081 devmask & (1 << 1), &err);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004082
Tejun Heo3a397462006-02-10 23:58:48 +09004083 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09004084 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
4085 return 0;
4086}
4087
4088/**
Tejun Heocc0680a2007-08-06 18:36:23 +09004089 * sata_link_hardreset - reset link via SATA phy reset
4090 * @link: link to reset
Tejun Heob6103f62006-11-01 17:59:53 +09004091 * @timing: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09004092 * @deadline: deadline jiffies for the operation
Tejun Heoc2bd5802006-01-24 17:05:22 +09004093 *
Tejun Heocc0680a2007-08-06 18:36:23 +09004094 * SATA phy-reset @link using DET bits of SControl register.
Tejun Heoc2bd5802006-01-24 17:05:22 +09004095 *
4096 * LOCKING:
4097 * Kernel thread context (may sleep)
4098 *
4099 * RETURNS:
4100 * 0 on success, -errno otherwise.
4101 */
Tejun Heocc0680a2007-08-06 18:36:23 +09004102int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
Tejun Heod4b2bab2007-02-02 16:50:52 +09004103 unsigned long deadline)
Tejun Heoc2bd5802006-01-24 17:05:22 +09004104{
Tejun Heo852ee162006-04-01 01:38:18 +09004105 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09004106 int rc;
Tejun Heo852ee162006-04-01 01:38:18 +09004107
Tejun Heoc2bd5802006-01-24 17:05:22 +09004108 DPRINTK("ENTER\n");
4109
Tejun Heo936fd732007-08-06 18:36:23 +09004110 if (sata_set_spd_needed(link)) {
Tejun Heo1c3fae42006-04-02 20:53:28 +09004111 /* 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 */
Tejun Heo936fd732007-08-06 18:36:23 +09004116 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09004117 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09004118
Martin Hicksa34b6fc2006-07-05 15:06:13 -04004119 scontrol = (scontrol & 0x0f0) | 0x304;
Tejun Heo81952c52006-05-15 20:57:47 +09004120
Tejun Heo936fd732007-08-06 18:36:23 +09004121 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09004122 goto out;
Tejun Heo1c3fae42006-04-02 20:53:28 +09004123
Tejun Heo936fd732007-08-06 18:36:23 +09004124 sata_set_spd(link);
Tejun Heo1c3fae42006-04-02 20:53:28 +09004125 }
4126
4127 /* issue phy wake/reset */
Tejun Heo936fd732007-08-06 18:36:23 +09004128 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09004129 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09004130
Tejun Heo852ee162006-04-01 01:38:18 +09004131 scontrol = (scontrol & 0x0f0) | 0x301;
Tejun Heo81952c52006-05-15 20:57:47 +09004132
Tejun Heo936fd732007-08-06 18:36:23 +09004133 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09004134 goto out;
Tejun Heoc2bd5802006-01-24 17:05:22 +09004135
Tejun Heo1c3fae42006-04-02 20:53:28 +09004136 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
Tejun Heoc2bd5802006-01-24 17:05:22 +09004137 * 10.4.2 says at least 1 ms.
4138 */
4139 msleep(1);
4140
Tejun Heo936fd732007-08-06 18:36:23 +09004141 /* bring link back */
4142 rc = sata_link_resume(link, timing, deadline);
Tejun Heob6103f62006-11-01 17:59:53 +09004143 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
Tejun Heocc0680a2007-08-06 18:36:23 +09004150 * @link: link to reset
Tejun Heob6103f62006-11-01 17:59:53 +09004151 * @class: resulting class of attached device
Tejun Heod4b2bab2007-02-02 16:50:52 +09004152 * @deadline: deadline jiffies for the operation
Tejun Heob6103f62006-11-01 17:59:53 +09004153 *
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 */
Tejun Heocc0680a2007-08-06 18:36:23 +09004163int sata_std_hardreset(struct ata_link *link, unsigned int *class,
Tejun Heod4b2bab2007-02-02 16:50:52 +09004164 unsigned long deadline)
Tejun Heob6103f62006-11-01 17:59:53 +09004165{
Tejun Heocc0680a2007-08-06 18:36:23 +09004166 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09004167 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
Tejun Heob6103f62006-11-01 17:59:53 +09004168 int rc;
4169
4170 DPRINTK("ENTER\n");
4171
4172 /* do hardreset */
Tejun Heocc0680a2007-08-06 18:36:23 +09004173 rc = sata_link_hardreset(link, timing, deadline);
Tejun Heob6103f62006-11-01 17:59:53 +09004174 if (rc) {
Tejun Heocc0680a2007-08-06 18:36:23 +09004175 ata_link_printk(link, KERN_ERR,
Tejun Heob6103f62006-11-01 17:59:53 +09004176 "COMRESET failed (errno=%d)\n", rc);
4177 return rc;
4178 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09004179
Tejun Heoc2bd5802006-01-24 17:05:22 +09004180 /* TODO: phy layer with polling, timeouts, etc. */
Tejun Heo936fd732007-08-06 18:36:23 +09004181 if (ata_link_offline(link)) {
Tejun Heoc2bd5802006-01-24 17:05:22 +09004182 *class = ATA_DEV_NONE;
4183 DPRINTK("EXIT, link offline\n");
4184 return 0;
4185 }
4186
Tejun Heo88ff6ea2007-10-16 14:21:24 -07004187 /* wait a while before checking status */
4188 ata_wait_after_reset(ap, deadline);
Tejun Heo34fee222007-02-02 15:29:27 +09004189
Tejun Heo633273a2007-09-23 13:19:54 +09004190 /* 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
Tejun Heod4b2bab2007-02-02 16:50:52 +09004200 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09004201 /* link occupied, -ENODEV too is an error */
4202 if (rc) {
Tejun Heocc0680a2007-08-06 18:36:23 +09004203 ata_link_printk(link, KERN_ERR,
Tejun Heod4b2bab2007-02-02 16:50:52 +09004204 "COMRESET failed (errno=%d)\n", rc);
4205 return rc;
Tejun Heoc2bd5802006-01-24 17:05:22 +09004206 }
4207
Tejun Heo3a397462006-02-10 23:58:48 +09004208 ap->ops->dev_select(ap, 0); /* probably unnecessary */
4209
Tejun Heo3f198592007-09-02 23:23:57 +09004210 *class = ata_dev_try_classify(link->device, 1, NULL);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004211
4212 DPRINTK("EXIT, class=%u\n", *class);
4213 return 0;
4214}
4215
4216/**
4217 * ata_std_postreset - standard postreset callback
Tejun Heocc0680a2007-08-06 18:36:23 +09004218 * @link: the target ata_link
Tejun Heoc2bd5802006-01-24 17:05:22 +09004219 * @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.
Tejun Heoc2bd5802006-01-24 17:05:22 +09004224 *
Tejun Heoc2bd5802006-01-24 17:05:22 +09004225 * LOCKING:
4226 * Kernel thread context (may sleep)
4227 */
Tejun Heocc0680a2007-08-06 18:36:23 +09004228void ata_std_postreset(struct ata_link *link, unsigned int *classes)
Tejun Heoc2bd5802006-01-24 17:05:22 +09004229{
Tejun Heocc0680a2007-08-06 18:36:23 +09004230 struct ata_port *ap = link->ap;
Tejun Heodc2b3512006-05-15 20:58:00 +09004231 u32 serror;
4232
Tejun Heoc2bd5802006-01-24 17:05:22 +09004233 DPRINTK("ENTER\n");
4234
Tejun Heoc2bd5802006-01-24 17:05:22 +09004235 /* print link status */
Tejun Heo936fd732007-08-06 18:36:23 +09004236 sata_print_link_status(link);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004237
Tejun Heodc2b3512006-05-15 20:58:00 +09004238 /* clear SError */
Tejun Heo936fd732007-08-06 18:36:23 +09004239 if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
4240 sata_scr_write(link, SCR_ERROR, serror);
Tejun Heof7fe7ad2007-12-08 08:47:01 +09004241 link->eh_info.serror = 0;
Tejun Heodc2b3512006-05-15 20:58:00 +09004242
Tejun Heoc2bd5802006-01-24 17:05:22 +09004243 /* 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
Tejun Heo3a397462006-02-10 23:58:48 +09004249 /* 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 */
Tejun Heo0d5ff562007-02-01 15:06:36 +09004256 if (ap->ioaddr.ctl_addr)
4257 iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004258
4259 DPRINTK("EXIT\n");
4260}
4261
Tejun Heoa62c0fc2006-01-24 17:05:22 +09004262/**
Tejun Heo623a3122006-03-05 17:55:58 +09004263 * ata_dev_same_device - Determine whether new ID matches configured device
Tejun Heo623a3122006-03-05 17:55:58 +09004264 * @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 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004278static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
4279 const u16 *new_id)
Tejun Heo623a3122006-03-05 17:55:58 +09004280{
4281 const u16 *old_id = dev->id;
Tejun Heoa0cf7332007-01-02 20:18:49 +09004282 unsigned char model[2][ATA_ID_PROD_LEN + 1];
4283 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
Tejun Heo623a3122006-03-05 17:55:58 +09004284
4285 if (dev->class != new_class) {
Tejun Heof15a1da2006-05-15 20:57:56 +09004286 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
4287 dev->class, new_class);
Tejun Heo623a3122006-03-05 17:55:58 +09004288 return 0;
4289 }
4290
Tejun Heoa0cf7332007-01-02 20:18:49 +09004291 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]));
Tejun Heo623a3122006-03-05 17:55:58 +09004295
4296 if (strcmp(model[0], model[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09004297 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
4298 "'%s' != '%s'\n", model[0], model[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09004299 return 0;
4300 }
4301
4302 if (strcmp(serial[0], serial[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09004303 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
4304 "'%s' != '%s'\n", serial[0], serial[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09004305 return 0;
4306 }
4307
Tejun Heo623a3122006-03-05 17:55:58 +09004308 return 1;
4309}
4310
4311/**
Tejun Heofe309112007-05-15 03:28:15 +09004312 * ata_dev_reread_id - Re-read IDENTIFY data
Henrik Kretzschmar3fae4502007-06-19 10:10:50 +02004313 * @dev: target ATA device
Tejun Heobff04642006-11-10 18:08:10 +09004314 * @readid_flags: read ID flags
Tejun Heo623a3122006-03-05 17:55:58 +09004315 *
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 */
Tejun Heofe309112007-05-15 03:28:15 +09004325int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
Tejun Heo623a3122006-03-05 17:55:58 +09004326{
Tejun Heo5eb45c02006-04-02 18:51:52 +09004327 unsigned int class = dev->class;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004328 u16 *id = (void *)dev->link->ap->sector_buf;
Tejun Heo623a3122006-03-05 17:55:58 +09004329 int rc;
4330
Tejun Heofe635c72006-05-15 20:57:35 +09004331 /* read ID data */
Tejun Heobff04642006-11-10 18:08:10 +09004332 rc = ata_dev_read_id(dev, &class, readid_flags, id);
Tejun Heo623a3122006-03-05 17:55:58 +09004333 if (rc)
Tejun Heofe309112007-05-15 03:28:15 +09004334 return rc;
Tejun Heo623a3122006-03-05 17:55:58 +09004335
4336 /* is the device still there? */
Tejun Heofe309112007-05-15 03:28:15 +09004337 if (!ata_dev_same_device(dev, class, id))
4338 return -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09004339
Tejun Heofe635c72006-05-15 20:57:35 +09004340 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heofe309112007-05-15 03:28:15 +09004341 return 0;
4342}
4343
4344/**
4345 * ata_dev_revalidate - Revalidate ATA device
4346 * @dev: device to revalidate
Tejun Heo422c9da2007-09-23 13:14:12 +09004347 * @new_class: new class code
Tejun Heofe309112007-05-15 03:28:15 +09004348 * @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 */
Tejun Heo422c9da2007-09-23 13:14:12 +09004359int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4360 unsigned int readid_flags)
Tejun Heofe309112007-05-15 03:28:15 +09004361{
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09004362 u64 n_sectors = dev->n_sectors;
Tejun Heofe309112007-05-15 03:28:15 +09004363 int rc;
4364
4365 if (!ata_dev_enabled(dev))
4366 return -ENODEV;
4367
Tejun Heo422c9da2007-09-23 13:14:12 +09004368 /* 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
Tejun Heofe309112007-05-15 03:28:15 +09004377 /* re-read ID */
4378 rc = ata_dev_reread_id(dev, readid_flags);
4379 if (rc)
4380 goto fail;
Tejun Heo623a3122006-03-05 17:55:58 +09004381
4382 /* configure device according to the new ID */
Tejun Heoefdaedc2006-11-01 18:38:52 +09004383 rc = ata_dev_configure(dev);
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09004384 if (rc)
4385 goto fail;
4386
4387 /* verify n_sectors hasn't changed */
Tejun Heob54eebd2007-08-17 18:46:51 +09004388 if (dev->class == ATA_DEV_ATA && n_sectors &&
4389 dev->n_sectors != n_sectors) {
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09004390 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);
Tejun Heo8270bec2007-08-16 03:02:22 +09004394
4395 /* restore original n_sectors */
4396 dev->n_sectors = n_sectors;
4397
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09004398 rc = -ENODEV;
4399 goto fail;
4400 }
4401
4402 return 0;
Tejun Heo623a3122006-03-05 17:55:58 +09004403
4404 fail:
Tejun Heof15a1da2006-05-15 20:57:56 +09004405 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
Tejun Heo623a3122006-03-05 17:55:58 +09004406 return rc;
4407}
4408
Alan Cox6919a0a2006-10-27 19:08:46 -07004409struct ata_blacklist_entry {
4410 const char *model_num;
4411 const char *model_rev;
4412 unsigned long horkage;
4413};
4414
4415static 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 },
Alan Cox6919a0a2006-10-27 19:08:46 -07004443 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
4444 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004445 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
Dave Jones39f19882007-05-21 14:31:03 -04004446 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
Tejun Heo3af9a772007-09-23 13:19:54 +09004447 /* Odd clown on sil3726/4726 PMPs */
4448 { "Config Disk", NULL, ATA_HORKAGE_NODMA |
4449 ATA_HORKAGE_SKIP_PM },
Alan Cox6919a0a2006-10-27 19:08:46 -07004450
Albert Lee18d6e9d2007-04-02 11:34:15 +08004451 /* Weird ATAPI devices */
Tejun Heo40a1d532007-06-27 02:49:38 +09004452 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
Albert Lee18d6e9d2007-04-02 11:34:15 +08004453
Alan Cox6919a0a2006-10-27 19:08:46 -07004454 /* Devices we expect to fail diagnostics */
4455
4456 /* Devices where NCQ should be avoided */
4457 /* NCQ is slow */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004458 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
Tejun Heo459ad682007-12-07 12:46:23 +09004459 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
Tejun Heo09125ea2007-02-28 15:21:23 +09004460 /* http://thread.gmane.org/gmane.linux.ide/14907 */
4461 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
Paul Rolland7acfaf32007-03-26 21:43:44 -08004462 /* NCQ is broken */
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004463 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
Alan Cox0e3dbc02007-09-20 15:22:47 +01004464 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
Paolo Ornatida6f0ec2007-10-04 11:06:56 +09004465 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
Tejun Heoe41bd3e2007-12-09 19:45:39 +09004466 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004467
Robert Hancock36e337d2007-04-02 22:05:29 -06004468 /* 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, },
Alan Cox6919a0a2006-10-27 19:08:46 -07004473
Tejun Heo16c55b02007-08-29 11:58:33 +09004474 /* 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 },
Alan Cox6919a0a2006-10-27 19:08:46 -07004479
Alan Cox93328e12007-09-29 04:06:48 -04004480 /* Devices which report 1 sector over size HPA */
4481 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
4482 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
Mikko Rapelib152fcd2008-02-19 01:41:25 +01004483 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
Alan Cox93328e12007-09-29 04:06:48 -04004484
Alan Cox6bbfd532007-11-05 22:58:58 +00004485 /* Devices which get the IVB wrong */
4486 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4487 { "TSSTcorp CDDVDW SH-S202J", "SB00", ATA_HORKAGE_IVB, },
Peter Missele9f33402007-11-27 18:04:42 +01004488 { "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, },
Alan Cox6bbfd532007-11-05 22:58:58 +00004491
Alan Cox6919a0a2006-10-27 19:08:46 -07004492 /* End Marker */
4493 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05004495
Adrian Bunk741b7762007-10-24 18:23:06 +02004496static int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004497{
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;
Andrew Paprocki317b50b2007-10-15 15:43:12 -04004507 else {
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004508 len = strlen(name);
Andrew Paprocki317b50b2007-10-15 15:43:12 -04004509 if (!len) {
4510 if (!*patt)
4511 return 0;
4512 return -1;
4513 }
4514 }
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004515
4516 return strncmp(patt, name, len);
4517}
4518
Tejun Heo75683fe2007-07-05 13:31:27 +09004519static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520{
Tejun Heo8bfa79f2007-01-02 20:19:40 +09004521 unsigned char model_num[ATA_ID_PROD_LEN + 1];
4522 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
Alan Cox6919a0a2006-10-27 19:08:46 -07004523 const struct ata_blacklist_entry *ad = ata_device_blacklist;
Albert Lee3a778272006-06-22 13:00:25 +08004524
Tejun Heo8bfa79f2007-01-02 20:19:40 +09004525 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
Alan Cox6919a0a2006-10-27 19:08:46 -07004528 while (ad->model_num) {
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004529 if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
Alan Cox6919a0a2006-10-27 19:08:46 -07004530 if (ad->model_rev == NULL)
4531 return ad->horkage;
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004532 if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
Alan Cox6919a0a2006-10-27 19:08:46 -07004533 return ad->horkage;
Alan Coxf4b15fe2006-03-22 15:54:04 +00004534 }
Alan Cox6919a0a2006-10-27 19:08:46 -07004535 ad++;
Alan Coxf4b15fe2006-03-22 15:54:04 +00004536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 return 0;
4538}
4539
Alan Cox6919a0a2006-10-27 19:08:46 -07004540static 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 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004546 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
Alan Cox6919a0a2006-10-27 19:08:46 -07004547 (dev->flags & ATA_DFLAG_CDB_INTR))
4548 return 1;
Tejun Heo75683fe2007-07-05 13:31:27 +09004549 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07004550}
4551
Tejun Heoa6d5a512006-03-06 04:31:57 +09004552/**
Alan Cox6bbfd532007-11-05 22:58:58 +00004553 * 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
4560static 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/**
Tejun Heoa6d5a512006-03-06 04:31:57 +09004568 * ata_dev_xfermask - Compute supported xfermask of the given device
Tejun Heoa6d5a512006-03-06 04:31:57 +09004569 * @dev: Device to compute xfermask for
4570 *
Tejun Heoacf356b2006-03-24 14:07:50 +09004571 * 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...
Tejun Heoa6d5a512006-03-06 04:31:57 +09004575 *
4576 * LOCKING:
4577 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09004578 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004579static void ata_dev_xfermask(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004581 struct ata_link *link = dev->link;
4582 struct ata_port *ap = link->ap;
Jeff Garzikcca39742006-08-24 03:19:22 -04004583 struct ata_host *host = ap->host;
Tejun Heoa6d5a512006-03-06 04:31:57 +09004584 unsigned long xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
Tejun Heo37deecb2006-08-10 16:59:07 +09004586 /* controller modes available */
Tejun Heo565083e2006-04-02 17:54:47 +09004587 xfer_mask = ata_pack_xfermask(ap->pio_mask,
4588 ap->mwdma_mask, ap->udma_mask);
4589
Robert Hancock8343f882007-03-06 02:37:51 -08004590 /* drive modes available */
Tejun Heo37deecb2006-08-10 16:59:07 +09004591 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4592 dev->mwdma_mask, dev->udma_mask);
4593 xfer_mask &= ata_id_xfermask(dev->id);
Tejun Heo565083e2006-04-02 17:54:47 +09004594
Alan Coxb352e572006-08-10 18:52:12 +01004595 /*
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
Tejun Heo37deecb2006-08-10 16:59:07 +09004606 if (ata_dma_blacklisted(dev)) {
4607 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Tejun Heof15a1da2006-05-15 20:57:56 +09004608 ata_dev_printk(dev, KERN_WARNING,
4609 "device is on DMA blacklist, disabling DMA\n");
Tejun Heo37deecb2006-08-10 16:59:07 +09004610 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09004611
Petr Vandrovec14d66ab2007-03-08 10:12:12 +01004612 if ((host->flags & ATA_HOST_SIMPLEX) &&
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004613 host->simplex_claimed && host->simplex_claimed != ap) {
Tejun Heo37deecb2006-08-10 16:59:07 +09004614 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");
Alan Cox5444a6f2006-03-27 18:58:20 +01004617 }
Tejun Heo565083e2006-04-02 17:54:47 +09004618
Jeff Garzike4246752007-03-09 09:56:46 -05004619 if (ap->flags & ATA_FLAG_NO_IORDY)
4620 xfer_mask &= ata_pio_mask_no_iordy(dev);
4621
Alan Cox5444a6f2006-03-27 18:58:20 +01004622 if (ap->ops->mode_filter)
Alan Coxa76b62c2007-03-09 09:34:07 -05004623 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01004624
Robert Hancock8343f882007-03-06 02:37:51 -08004625 /* 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 */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004635 if ((ap->cbl == ATA_CBL_PATA40) ||
Alan Cox6bbfd532007-11-05 22:58:58 +00004636 (ata_is_40wire(dev) &&
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004637 (ap->cbl == ATA_CBL_PATA_UNK ||
4638 ap->cbl == ATA_CBL_PATA80))) {
4639 ata_dev_printk(dev, KERN_WARNING,
Robert Hancock8343f882007-03-06 02:37:51 -08004640 "limited to UDMA/33 due to 40-wire cable\n");
4641 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4642 }
4643
Tejun Heo565083e2006-04-02 17:54:47 +09004644 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4645 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646}
4647
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 * @dev: Device to which command will be sent
4651 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004652 * Issue SET FEATURES - XFER MODE command to device @dev
4653 * on port @ap.
4654 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004656 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09004657 *
4658 * RETURNS:
4659 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 */
4661
Tejun Heo3373efd2006-05-15 20:57:53 +09004662static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663{
Tejun Heoa0123702005-12-13 14:49:31 +09004664 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09004665 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
4667 /* set up set-features taskfile */
4668 DPRINTK("set features - xfer mode\n");
4669
Tejun Heo464cf172007-05-27 15:10:40 +02004670 /* Some controllers and ATAPI devices show flaky interrupt
4671 * behavior after setting xfer mode. Use polling instead.
4672 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004673 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004674 tf.command = ATA_CMD_SET_FEATURES;
4675 tf.feature = SETFEATURES_XFER;
Tejun Heo464cf172007-05-27 15:10:40 +02004676 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
Tejun Heoa0123702005-12-13 14:49:31 +09004677 tf.protocol = ATA_PROT_NODATA;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004678 /* If we are using IORDY we must send the mode setting command */
Jeff Garzik11b7bec2007-11-23 21:12:14 -05004679 if (ata_pio_need_iordy(dev))
4680 tf.nsect = dev->xfer_mode;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004681 /* If the device has IORDY and the controller does not - turn it off */
4682 else if (ata_id_has_iordy(dev->id))
Jeff Garzik11b7bec2007-11-23 21:12:14 -05004683 tf.nsect = 0x01;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004684 else /* In the ancient relic department - skip all of this */
4685 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
Tejun Heo2b789102007-10-09 15:05:44 +09004687 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
Tejun Heo83206a22006-03-24 15:25:31 +09004689 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4690 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692/**
Jeff Garzik218f3d32007-10-25 00:33:27 -04004693 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004694 * @dev: Device to which command will be sent
4695 * @enable: Whether to enable or disable the feature
Jeff Garzik218f3d32007-10-25 00:33:27 -04004696 * @feature: The sector count represents the feature to set
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004697 *
4698 * Issue SET FEATURES - SATA FEATURES command to device @dev
Jeff Garzik218f3d32007-10-25 00:33:27 -04004699 * on port @ap with sector count
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004700 *
4701 * LOCKING:
4702 * PCI/etc. bus probe sem.
4703 *
4704 * RETURNS:
4705 * 0 on success, AC_ERR_* mask otherwise.
4706 */
Jeff Garzik218f3d32007-10-25 00:33:27 -04004707static unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable,
4708 u8 feature)
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004709{
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;
Jeff Garzik218f3d32007-10-25 00:33:27 -04004721 tf.nsect = feature;
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004722
Tejun Heo2b789102007-10-09 15:05:44 +09004723 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004724
4725 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4726 return err_mask;
4727}
4728
4729/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04004730 * ata_dev_init_params - Issue INIT DEV PARAMS command
Albert Lee8bf62ec2005-05-12 15:29:42 -04004731 * @dev: Device to which command will be sent
Randy Dunlape2a7f772006-05-18 10:50:18 -07004732 * @heads: Number of heads (taskfile parameter)
4733 * @sectors: Number of sectors (taskfile parameter)
Albert Lee8bf62ec2005-05-12 15:29:42 -04004734 *
4735 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09004736 * Kernel thread context (may sleep)
4737 *
4738 * RETURNS:
4739 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ec2005-05-12 15:29:42 -04004740 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004741static unsigned int ata_dev_init_params(struct ata_device *dev,
4742 u16 heads, u16 sectors)
Albert Lee8bf62ec2005-05-12 15:29:42 -04004743{
Tejun Heoa0123702005-12-13 14:49:31 +09004744 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09004745 unsigned int err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04004746
4747 /* Number of sectors per track 1-255. Number of heads 1-16 */
4748 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08004749 return AC_ERR_INVALID;
Albert Lee8bf62ec2005-05-12 15:29:42 -04004750
4751 /* set up init dev params taskfile */
4752 DPRINTK("init dev params \n");
4753
Tejun Heo3373efd2006-05-15 20:57:53 +09004754 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004755 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 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04004760
Tejun Heo2b789102007-10-09 15:05:44 +09004761 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Alan Cox18b24662007-08-08 14:28:49 +01004762 /* 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;
Albert Lee8bf62ec2005-05-12 15:29:42 -04004767
Tejun Heo6aff8f12006-02-15 18:24:09 +09004768 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4769 return err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04004770}
4771
4772/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004773 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 *
4778 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004779 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 */
Tejun Heo70e6ad02006-11-14 22:47:10 +09004781void ata_sg_clean(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782{
4783 struct ata_port *ap = qc->ap;
Tejun Heoff2aeb12007-12-05 16:43:11 +09004784 struct scatterlist *sg = qc->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 int dir = qc->dma_dir;
4786
Tejun Heoa4631472006-02-11 19:11:13 +09004787 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
James Bottomleydde20202008-02-19 11:36:56 +01004789 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790
James Bottomleydde20202008-02-19 11:36:56 +01004791 if (qc->n_elem)
4792 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
4794 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heoff2aeb12007-12-05 16:43:11 +09004795 qc->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796}
4797
4798/**
4799 * ata_fill_sg - Fill PCI IDE PRD table
4800 * @qc: Metadata associated with taskfile to be transferred
4801 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004802 * Fill PCI IDE PRD (scatter-gather) table with segments
4803 * associated with the current disk command.
4804 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004806 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 *
4808 */
4809static void ata_fill_sg(struct ata_queued_cmd *qc)
4810{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004812 struct scatterlist *sg;
Tejun Heoff2aeb12007-12-05 16:43:11 +09004813 unsigned int si, pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
Tejun Heoff2aeb12007-12-05 16:43:11 +09004815 pi = 0;
4816 for_each_sg(qc->sg, sg, qc->n_elem, si) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 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
Tejun Heoff2aeb12007-12-05 16:43:11 +09004833 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
Tejun Heoff2aeb12007-12-05 16:43:11 +09004837 pi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 sg_len -= len;
4839 addr += len;
4840 }
4841 }
4842
Tejun Heoff2aeb12007-12-05 16:43:11 +09004843 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844}
Tejun Heob9a41972007-06-27 02:48:43 +09004845
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846/**
Alan Coxd26fc952007-07-06 19:13:52 -04004847 * 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 */
4859static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
4860{
4861 struct ata_port *ap = qc->ap;
4862 struct scatterlist *sg;
Tejun Heoff2aeb12007-12-05 16:43:11 +09004863 unsigned int si, pi;
Alan Coxd26fc952007-07-06 19:13:52 -04004864
Tejun Heoff2aeb12007-12-05 16:43:11 +09004865 pi = 0;
4866 for_each_sg(qc->sg, sg, qc->n_elem, si) {
Alan Coxd26fc952007-07-06 19:13:52 -04004867 u32 addr, offset;
4868 u32 sg_len, len, blen;
4869
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004870 /* determine if physical DMA addr spans 64K boundary.
Alan Coxd26fc952007-07-06 19:13:52 -04004871 * 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;
Tejun Heoff2aeb12007-12-05 16:43:11 +09004884 ap->prd[pi].addr = cpu_to_le32(addr);
Alan Coxd26fc952007-07-06 19:13:52 -04004885 if (blen == 0) {
4886 /* Some PATA chipsets like the CS5530 can't
4887 cope with 0x0000 meaning 64K as the spec says */
Tejun Heoff2aeb12007-12-05 16:43:11 +09004888 ap->prd[pi].flags_len = cpu_to_le32(0x8000);
Alan Coxd26fc952007-07-06 19:13:52 -04004889 blen = 0x8000;
Tejun Heoff2aeb12007-12-05 16:43:11 +09004890 ap->prd[++pi].addr = cpu_to_le32(addr + 0x8000);
Alan Coxd26fc952007-07-06 19:13:52 -04004891 }
Tejun Heoff2aeb12007-12-05 16:43:11 +09004892 ap->prd[pi].flags_len = cpu_to_le32(blen);
4893 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
Alan Coxd26fc952007-07-06 19:13:52 -04004894
Tejun Heoff2aeb12007-12-05 16:43:11 +09004895 pi++;
Alan Coxd26fc952007-07-06 19:13:52 -04004896 sg_len -= len;
4897 addr += len;
4898 }
4899 }
4900
Tejun Heoff2aeb12007-12-05 16:43:11 +09004901 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
Alan Coxd26fc952007-07-06 19:13:52 -04004902}
4903
4904/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
4906 * @qc: Metadata associated with taskfile to check
4907 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004908 * 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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004913 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004914 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 * RETURNS: 0 when ATAPI DMA can be used
4916 * nonzero otherwise
4917 */
4918int ata_check_atapi_dma(struct ata_queued_cmd *qc)
4919{
4920 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
Tejun Heob9a41972007-06-27 02:48:43 +09004922 /* 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;
Albert Lee6f23a312007-04-02 11:39:25 +08004927
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 if (ap->ops->check_atapi_dma)
Tejun Heob9a41972007-06-27 02:48:43 +09004929 return ap->ops->check_atapi_dma(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930
Tejun Heob9a41972007-06-27 02:48:43 +09004931 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932}
Tejun Heob9a41972007-06-27 02:48:43 +09004933
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934/**
Tejun Heo31cc23b2007-09-23 13:14:12 +09004935 * 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 */
4949int 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/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 * ata_qc_prep - Prepare taskfile for submission
4966 * @qc: Metadata associated with taskfile to be prepared
4967 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004968 * Prepare ATA taskfile for submission.
4969 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004971 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 */
4973void 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
Alan Coxd26fc952007-07-06 19:13:52 -04004981/**
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 */
4990void 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
Brian Kinge46834c2006-03-17 17:04:03 -06004998void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4999
Jeff Garzik0cba6322005-05-30 19:49:12 -04005000/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04005001 * 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:
Jeff Garzikcca39742006-08-24 03:19:22 -04005011 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04005012 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
5014 unsigned int n_elem)
5015{
Tejun Heoff2aeb12007-12-05 16:43:11 +09005016 qc->sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 qc->n_elem = n_elem;
Tejun Heoff2aeb12007-12-05 16:43:11 +09005018 qc->cursg = qc->sg;
5019}
5020
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04005022 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 *
5027 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005028 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 *
5030 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005031 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 *
5033 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034static int ata_sg_setup(struct ata_queued_cmd *qc)
5035{
5036 struct ata_port *ap = qc->ap;
James Bottomleydde20202008-02-19 11:36:56 +01005037 unsigned int n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038
Tejun Heo44877b42007-02-21 01:06:51 +09005039 VPRINTK("ENTER, ata%u\n", ap->print_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040
James Bottomleydde20202008-02-19 11:36:56 +01005041 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
5042 if (n_elem < 1)
5043 return -1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04005044
James Bottomleydde20202008-02-19 11:36:56 +01005045 DPRINTK("%d sg elements mapped\n", n_elem);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04005046
James Bottomleydde20202008-02-19 11:36:56 +01005047 qc->n_elem = n_elem;
Tejun Heof92a2632007-12-05 16:43:10 +09005048 qc->flags |= ATA_QCFLAG_DMAMAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049
5050 return 0;
5051}
5052
5053/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005054 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04005055 * @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:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005063 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04005064 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065void 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
Albert Lee6ae4cfb2005-08-12 14:15:34 +08005075/**
Tejun Heo0d5ff562007-02-01 15:06:36 +09005076 * ata_data_xfer - Transfer data by PIO
Tejun Heo55dba312007-12-05 16:43:07 +09005077 * @dev: device to target
Albert Lee6ae4cfb2005-08-12 14:15:34 +08005078 * @buf: data buffer
5079 * @buflen: buffer length
Linus Nilsson0affa452008-01-28 15:56:27 +01005080 * @rw: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08005081 *
5082 * Transfer data from/to the device data register by PIO.
5083 *
5084 * LOCKING:
5085 * Inherited from caller.
Tejun Heo55dba312007-12-05 16:43:07 +09005086 *
5087 * RETURNS:
5088 * Bytes consumed.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08005089 */
Tejun Heo55dba312007-12-05 16:43:07 +09005090unsigned int ata_data_xfer(struct ata_device *dev, unsigned char *buf,
5091 unsigned int buflen, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092{
Tejun Heo55dba312007-12-05 16:43:07 +09005093 struct ata_port *ap = dev->link->ap;
5094 void __iomem *data_addr = ap->ioaddr.data_addr;
Albert Lee6ae4cfb2005-08-12 14:15:34 +08005095 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096
Albert Lee6ae4cfb2005-08-12 14:15:34 +08005097 /* Transfer multiple of 2 bytes */
Tejun Heo55dba312007-12-05 16:43:07 +09005098 if (rw == READ)
5099 ioread16_rep(data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 else
Tejun Heo55dba312007-12-05 16:43:07 +09005101 iowrite16_rep(data_addr, buf, words);
Albert Lee6ae4cfb2005-08-12 14:15:34 +08005102
5103 /* Transfer trailing 1 byte, if any. */
5104 if (unlikely(buflen & 0x01)) {
Al Viro4ca4e432007-12-30 09:32:22 +00005105 __le16 align_buf[1] = { 0 };
Albert Lee6ae4cfb2005-08-12 14:15:34 +08005106 unsigned char *trailing_buf = buf + buflen - 1;
5107
Tejun Heo55dba312007-12-05 16:43:07 +09005108 if (rw == READ) {
5109 align_buf[0] = cpu_to_le16(ioread16(data_addr));
Albert Lee6ae4cfb2005-08-12 14:15:34 +08005110 memcpy(trailing_buf, align_buf, 1);
Tejun Heo55dba312007-12-05 16:43:07 +09005111 } else {
5112 memcpy(align_buf, trailing_buf, 1);
5113 iowrite16(le16_to_cpu(align_buf[0]), data_addr);
Albert Lee6ae4cfb2005-08-12 14:15:34 +08005114 }
Tejun Heo55dba312007-12-05 16:43:07 +09005115 words++;
Albert Lee6ae4cfb2005-08-12 14:15:34 +08005116 }
Tejun Heo55dba312007-12-05 16:43:07 +09005117
5118 return words << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119}
5120
Albert Lee6ae4cfb2005-08-12 14:15:34 +08005121/**
Tejun Heo0d5ff562007-02-01 15:06:36 +09005122 * ata_data_xfer_noirq - Transfer data by PIO
Tejun Heo55dba312007-12-05 16:43:07 +09005123 * @dev: device to target
Alan Cox75e99582006-05-24 14:14:41 +01005124 * @buf: data buffer
5125 * @buflen: buffer length
Linus Nilsson0affa452008-01-28 15:56:27 +01005126 * @rw: read/write
Alan Cox75e99582006-05-24 14:14:41 +01005127 *
Tejun Heo88574552006-06-25 20:00:35 +09005128 * Transfer data from/to the device data register by PIO. Do the
Alan Cox75e99582006-05-24 14:14:41 +01005129 * transfer with interrupts disabled.
5130 *
5131 * LOCKING:
5132 * Inherited from caller.
Tejun Heo55dba312007-12-05 16:43:07 +09005133 *
5134 * RETURNS:
5135 * Bytes consumed.
Alan Cox75e99582006-05-24 14:14:41 +01005136 */
Tejun Heo55dba312007-12-05 16:43:07 +09005137unsigned int ata_data_xfer_noirq(struct ata_device *dev, unsigned char *buf,
5138 unsigned int buflen, int rw)
Alan Cox75e99582006-05-24 14:14:41 +01005139{
5140 unsigned long flags;
Tejun Heo55dba312007-12-05 16:43:07 +09005141 unsigned int consumed;
5142
Alan Cox75e99582006-05-24 14:14:41 +01005143 local_irq_save(flags);
Tejun Heo55dba312007-12-05 16:43:07 +09005144 consumed = ata_data_xfer(dev, buf, buflen, rw);
Alan Cox75e99582006-05-24 14:14:41 +01005145 local_irq_restore(flags);
Tejun Heo55dba312007-12-05 16:43:07 +09005146
5147 return consumed;
Alan Cox75e99582006-05-24 14:14:41 +01005148}
5149
5150
5151/**
Mark Lord5a5dbd12007-03-16 10:22:26 -04005152 * ata_pio_sector - Transfer a sector of data.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08005153 * @qc: Command on going
5154 *
Mark Lord5a5dbd12007-03-16 10:22:26 -04005155 * Transfer qc->sect_size bytes of data from/to the ATA device.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08005156 *
5157 * LOCKING:
5158 * Inherited from caller.
5159 */
5160
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161static void ata_pio_sector(struct ata_queued_cmd *qc)
5162{
5163 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 struct ata_port *ap = qc->ap;
5165 struct page *page;
5166 unsigned int offset;
5167 unsigned char *buf;
5168
Mark Lord5a5dbd12007-03-16 10:22:26 -04005169 if (qc->curbytes == qc->nbytes - qc->sect_size)
Albert Lee14be71f2005-09-27 17:36:35 +08005170 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171
Jens Axboe45711f12007-10-22 21:19:53 +02005172 page = sg_page(qc->cursg);
Jens Axboe87260212007-10-16 11:14:12 +02005173 offset = qc->cursg->offset + qc->cursg_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174
5175 /* get the current page and offset */
5176 page = nth_page(page, (offset >> PAGE_SHIFT));
5177 offset %= PAGE_SIZE;
5178
Albert Lee7282aa42005-10-09 09:46:07 -04005179 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
5180
Albert Lee91b8b312005-10-09 09:48:44 -04005181 if (PageHighMem(page)) {
5182 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04005183
Alan Coxa6b2c5d2006-05-22 16:59:59 +01005184 /* FIXME: use a bounce buffer */
Albert Lee91b8b312005-10-09 09:48:44 -04005185 local_irq_save(flags);
5186 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04005187
Albert Lee91b8b312005-10-09 09:48:44 -04005188 /* do the actual data transfer */
Mark Lord5a5dbd12007-03-16 10:22:26 -04005189 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04005190
5191 kunmap_atomic(buf, KM_IRQ0);
5192 local_irq_restore(flags);
5193 } else {
5194 buf = page_address(page);
Mark Lord5a5dbd12007-03-16 10:22:26 -04005195 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04005196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197
Mark Lord5a5dbd12007-03-16 10:22:26 -04005198 qc->curbytes += qc->sect_size;
5199 qc->cursg_ofs += qc->sect_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200
Jens Axboe87260212007-10-16 11:14:12 +02005201 if (qc->cursg_ofs == qc->cursg->length) {
5202 qc->cursg = sg_next(qc->cursg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 qc->cursg_ofs = 0;
5204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206
Albert Lee6ae4cfb2005-08-12 14:15:34 +08005207/**
Mark Lord5a5dbd12007-03-16 10:22:26 -04005208 * ata_pio_sectors - Transfer one or many sectors.
Albert Lee07f6f7d2005-11-01 19:33:20 +08005209 * @qc: Command on going
5210 *
Mark Lord5a5dbd12007-03-16 10:22:26 -04005211 * Transfer one or many sectors of data from/to the
Albert Lee07f6f7d2005-11-01 19:33:20 +08005212 * ATA device for the DRQ request.
5213 *
5214 * LOCKING:
5215 * Inherited from caller.
5216 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217
Albert Lee07f6f7d2005-11-01 19:33:20 +08005218static 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223
Jeff Garzik587005d2006-02-11 18:17:32 -05005224 WARN_ON(qc->dev->multi_count == 0);
Albert Lee07f6f7d2005-11-01 19:33:20 +08005225
Mark Lord5a5dbd12007-03-16 10:22:26 -04005226 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
Tejun Heo726f0782007-01-03 17:30:39 +09005227 qc->dev->multi_count);
Albert Lee07f6f7d2005-11-01 19:33:20 +08005228 while (nsect--)
5229 ata_pio_sector(qc);
5230 } else
5231 ata_pio_sector(qc);
Albert Lee4cc980b2007-08-15 03:19:45 -04005232
5233 ata_altstatus(qc->ap); /* flush */
Albert Lee07f6f7d2005-11-01 19:33:20 +08005234}
5235
5236/**
Albert Leec71c1852005-10-04 06:03:45 -04005237 * 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
5248static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
5249{
5250 /* send SCSI cdb */
5251 DPRINTK("send cdb\n");
Jeff Garzikdb024d52006-02-13 00:23:57 -05005252 WARN_ON(qc->dev->cdb_len < 12);
Albert Leec71c1852005-10-04 06:03:45 -04005253
Alan Coxa6b2c5d2006-05-22 16:59:59 +01005254 ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
Albert Leec71c1852005-10-04 06:03:45 -04005255 ata_altstatus(ap); /* flush */
5256
5257 switch (qc->tf.protocol) {
Tejun Heo0dc36882007-12-18 16:34:43 -05005258 case ATAPI_PROT_PIO:
Albert Leec71c1852005-10-04 06:03:45 -04005259 ap->hsm_task_state = HSM_ST;
5260 break;
Tejun Heo0dc36882007-12-18 16:34:43 -05005261 case ATAPI_PROT_NODATA:
Albert Leec71c1852005-10-04 06:03:45 -04005262 ap->hsm_task_state = HSM_ST_LAST;
5263 break;
Tejun Heo0dc36882007-12-18 16:34:43 -05005264 case ATAPI_PROT_DMA:
Albert Leec71c1852005-10-04 06:03:45 -04005265 ap->hsm_task_state = HSM_ST_LAST;
5266 /* initiate bmdma */
5267 ap->ops->bmdma_start(qc);
5268 break;
5269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270}
5271
Albert Lee6ae4cfb2005-08-12 14:15:34 +08005272/**
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 */
Tejun Heo140b5e52007-12-12 12:21:52 +09005283static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284{
Tejun Heo56c819d2008-02-19 11:35:37 +01005285 int rw = (qc->tf.flags & ATA_TFLAG_WRITE) ? WRITE : READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 struct ata_port *ap = qc->ap;
Tejun Heo56c819d2008-02-19 11:35:37 +01005287 struct ata_device *dev = qc->dev;
5288 struct ata_eh_info *ehi = &dev->link->eh_info;
Tejun Heo140b5e52007-12-12 12:21:52 +09005289 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 struct page *page;
5291 unsigned char *buf;
Tejun Heo56c819d2008-02-19 11:35:37 +01005292 unsigned int offset, count, consumed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293
5294next_sg:
Tejun Heo140b5e52007-12-12 12:21:52 +09005295 sg = qc->cursg;
5296 if (unlikely(!sg)) {
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01005297 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;
Tejun Heo140b5e52007-12-12 12:21:52 +09005301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302
Jens Axboe45711f12007-10-22 21:19:53 +02005303 page = sg_page(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 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
Albert Lee6952df02005-06-06 15:56:03 +08005310 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04005311 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312
5313 /* don't cross page boundaries */
5314 count = min(count, (unsigned int)PAGE_SIZE - offset);
5315
Albert Lee7282aa42005-10-09 09:46:07 -04005316 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
5317
Albert Lee91b8b312005-10-09 09:48:44 -04005318 if (PageHighMem(page)) {
5319 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04005320
Alan Coxa6b2c5d2006-05-22 16:59:59 +01005321 /* FIXME: use bounce buffer */
Albert Lee91b8b312005-10-09 09:48:44 -04005322 local_irq_save(flags);
5323 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04005324
Albert Lee91b8b312005-10-09 09:48:44 -04005325 /* do the actual data transfer */
Tejun Heo56c819d2008-02-19 11:35:37 +01005326 consumed = ap->ops->data_xfer(dev, buf + offset, count, rw);
Albert Lee91b8b312005-10-09 09:48:44 -04005327
5328 kunmap_atomic(buf, KM_IRQ0);
5329 local_irq_restore(flags);
5330 } else {
5331 buf = page_address(page);
Tejun Heo56c819d2008-02-19 11:35:37 +01005332 consumed = ap->ops->data_xfer(dev, buf + offset, count, rw);
Albert Lee91b8b312005-10-09 09:48:44 -04005333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334
Tejun Heo56c819d2008-02-19 11:35:37 +01005335 bytes -= min(bytes, consumed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 qc->curbytes += count;
5337 qc->cursg_ofs += count;
5338
Albert Lee32529e02005-05-26 03:49:42 -04005339 if (qc->cursg_ofs == sg->length) {
Jens Axboe87260212007-10-16 11:14:12 +02005340 qc->cursg = sg_next(qc->cursg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 qc->cursg_ofs = 0;
5342 }
5343
Tejun Heo56c819d2008-02-19 11:35:37 +01005344 /* consumed can be larger than count only for the last transfer */
5345 WARN_ON(qc->cursg && count != consumed);
5346
Albert Lee563a6e12005-08-12 14:17:50 +08005347 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 goto next_sg;
Tejun Heo140b5e52007-12-12 12:21:52 +09005349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350}
5351
Albert Lee6ae4cfb2005-08-12 14:15:34 +08005352/**
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.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08005360 */
5361
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362static void atapi_pio_bytes(struct ata_queued_cmd *qc)
5363{
5364 struct ata_port *ap = qc->ap;
5365 struct ata_device *dev = qc->dev;
Tejun Heo56c819d2008-02-19 11:35:37 +01005366 struct ata_eh_info *ehi = &dev->link->eh_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 unsigned int ireason, bc_lo, bc_hi, bytes;
5368 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
5369
Albert Leeeec4c3f2006-05-18 17:51:10 +08005370 /* 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 bytes = (bc_hi << 8) | bc_lo;
5381
5382 /* shall be cleared to zero, indicating xfer of data */
Albert Lee01063722007-12-05 16:43:02 +09005383 if (unlikely(ireason & (1 << 0)))
Tejun Heo56c819d2008-02-19 11:35:37 +01005384 goto atapi_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
5386 /* make sure transfer direction matches expected */
5387 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
Albert Lee01063722007-12-05 16:43:02 +09005388 if (unlikely(do_write != i_write))
Tejun Heo56c819d2008-02-19 11:35:37 +01005389 goto atapi_check;
Albert Lee01063722007-12-05 16:43:02 +09005390
5391 if (unlikely(!bytes))
Tejun Heo56c819d2008-02-19 11:35:37 +01005392 goto atapi_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393
Tejun Heo44877b42007-02-21 01:06:51 +09005394 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
Albert Lee312f7da2005-09-27 17:38:03 +08005395
Tejun Heo56c819d2008-02-19 11:35:37 +01005396 if (unlikely(__atapi_pio_bytes(qc, bytes)))
Tejun Heo140b5e52007-12-12 12:21:52 +09005397 goto err_out;
Albert Lee4cc980b2007-08-15 03:19:45 -04005398 ata_altstatus(ap); /* flush */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399
5400 return;
5401
Tejun Heo56c819d2008-02-19 11:35:37 +01005402 atapi_check:
5403 ata_ehi_push_desc(ehi, "ATAPI check failed (ireason=0x%x bytes=%u)",
5404 ireason, bytes);
5405 err_out:
Tejun Heo11a56d22006-01-23 13:09:36 +09005406 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08005407 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408}
5409
5410/**
Albert Leec234fb02006-03-25 17:58:38 +08005411 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 *
Albert Leec234fb02006-03-25 17:58:38 +08005415 * RETURNS:
5416 * 1 if ok in workqueue, 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 */
Albert Leec234fb02006-03-25 17:58:38 +08005418
5419static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420{
Albert Leec234fb02006-03-25 17:58:38 +08005421 if (qc->tf.flags & ATA_TFLAG_POLLING)
5422 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423
Albert Leec234fb02006-03-25 17:58:38 +08005424 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
Tejun Heo405e66b2007-11-27 19:28:53 +09005429 if (ata_is_atapi(qc->tf.protocol) &&
Albert Leec234fb02006-03-25 17:58:38 +08005430 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
5431 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 }
5433
Albert Leec234fb02006-03-25 17:58:38 +08005434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435}
5436
Albert Leec234fb02006-03-25 17:58:38 +08005437/**
Tejun Heoc17ea202006-05-15 20:59:29 +09005438 * 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:
Jeff Garzikcca39742006-08-24 03:19:22 -04005445 * If @in_wq is zero, spin_lock_irqsave(host lock).
Tejun Heoc17ea202006-05-15 20:59:29 +09005446 * Otherwise, none on entry and grabs host lock.
5447 */
5448static 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) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04005455 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09005456
Jeff Garzikcca39742006-08-24 03:19:22 -04005457 /* EH might have kicked in while host lock is
5458 * released.
Tejun Heoc17ea202006-05-15 20:59:29 +09005459 */
5460 qc = ata_qc_from_tag(ap, qc->tag);
5461 if (qc) {
5462 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
Akira Iguchi83625002007-01-26 16:27:32 +09005463 ap->ops->irq_on(ap);
Tejun Heoc17ea202006-05-15 20:59:29 +09005464 ata_qc_complete(qc);
5465 } else
5466 ata_port_freeze(ap);
5467 }
5468
Jeff Garzikba6a1302006-06-22 23:46:10 -04005469 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09005470 } 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) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04005478 spin_lock_irqsave(ap->lock, flags);
Akira Iguchi83625002007-01-26 16:27:32 +09005479 ap->ops->irq_on(ap);
Tejun Heoc17ea202006-05-15 20:59:29 +09005480 ata_qc_complete(qc);
Jeff Garzikba6a1302006-06-22 23:46:10 -04005481 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09005482 } else
5483 ata_qc_complete(qc);
5484 }
5485}
5486
5487/**
Albert Leebb5cb292006-03-25 17:48:02 +08005488 * 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 */
Tejun Heo9a1004d2006-05-31 18:27:52 +09005497int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
5498 u8 status, int in_wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499{
Albert Leebb5cb292006-03-25 17:48:02 +08005500 unsigned long flags = 0;
5501 int poll_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502
Albert Lee6912ccd2006-03-25 17:45:49 +08005503 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
Albert Lee0565c262006-02-13 18:55:25 +08005504
Albert Leebb5cb292006-03-25 17:48:02 +08005505 /* 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).
Albert Lee1c848982005-12-05 15:40:15 +08005508 */
Albert Leec234fb02006-03-25 17:58:38 +08005509 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
Albert Lee1c848982005-12-05 15:40:15 +08005510
Albert Leee2cec772006-03-25 17:43:49 +08005511fsm_start:
Albert Lee999bb6f2006-03-25 18:07:48 +08005512 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
Tejun Heo44877b42007-02-21 01:06:51 +09005513 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514
Albert Leee2cec772006-03-25 17:43:49 +08005515 switch (ap->hsm_task_state) {
5516 case HSM_ST_FIRST:
Albert Leebb5cb292006-03-25 17:48:02 +08005517 /* 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
Albert Leee2cec772006-03-25 17:43:49 +08005525 /* check device status */
Albert Lee3655d1d2006-05-19 11:43:04 +08005526 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
Albert Leee2cec772006-03-25 17:43:49 +08005535 ap->hsm_task_state = HSM_ST_ERR;
5536 goto fsm_start;
5537 }
5538
Albert Lee71601952006-03-25 18:11:12 +08005539 /* Device should not ask for data transfer (DRQ=1)
5540 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08005541 * 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.
Albert Lee71601952006-03-25 18:11:12 +08005544 */
5545 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Albert Lee2d3b8ee2007-11-15 10:35:46 +09005546 /* 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 }
Albert Lee71601952006-03-25 18:11:12 +08005559 }
5560
Albert Leebb5cb292006-03-25 17:48:02 +08005561 /* 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)
Jeff Garzikba6a1302006-06-22 23:46:10 -04005567 spin_lock_irqsave(ap->lock, flags);
Albert Leee2cec772006-03-25 17:43:49 +08005568
Albert Leebb5cb292006-03-25 17:48:02 +08005569 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);
Albert Leebb5cb292006-03-25 17:48:02 +08005580 } else
5581 /* send CDB */
5582 atapi_send_cdb(ap, qc);
5583
5584 if (in_wq)
Jeff Garzikba6a1302006-06-22 23:46:10 -04005585 spin_unlock_irqrestore(ap->lock, flags);
Albert Leebb5cb292006-03-25 17:48:02 +08005586
5587 /* if polling, ata_pio_task() handles the rest.
5588 * otherwise, interrupt handler takes over from here.
5589 */
Albert Leee2cec772006-03-25 17:43:49 +08005590 break;
5591
5592 case HSM_ST:
5593 /* complete command or read/write the data register */
Tejun Heo0dc36882007-12-18 16:34:43 -05005594 if (qc->tf.protocol == ATAPI_PROT_PIO) {
Albert Leee2cec772006-03-25 17:43:49 +08005595 /* ATAPI PIO protocol */
5596 if ((status & ATA_DRQ) == 0) {
Albert Lee3655d1d2006-05-19 11:43:04 +08005597 /* No more data to transfer or device error.
5598 * Device error will be tagged in HSM_ST_LAST.
5599 */
Albert Leee2cec772006-03-25 17:43:49 +08005600 ap->hsm_task_state = HSM_ST_LAST;
5601 goto fsm_start;
5602 }
5603
Albert Lee71601952006-03-25 18:11:12 +08005604 /* Device should not ask for data transfer (DRQ=1)
5605 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08005606 * 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.
Albert Lee71601952006-03-25 18:11:12 +08005609 */
5610 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Tejun Heo44877b42007-02-21 01:06:51 +09005611 ata_port_printk(ap, KERN_WARNING, "DRQ=1 with "
5612 "device error, dev_stat 0x%X\n",
5613 status);
Albert Lee3655d1d2006-05-19 11:43:04 +08005614 qc->err_mask |= AC_ERR_HSM;
Albert Leeeee6c322006-04-01 17:38:43 +08005615 ap->hsm_task_state = HSM_ST_ERR;
5616 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08005617 }
5618
Albert Leee2cec772006-03-25 17:43:49 +08005619 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 */
Albert Lee3655d1d2006-05-19 11:43:04 +08005629 if (likely(status & (ATA_ERR | ATA_DF)))
5630 /* device stops HSM for abort/error */
5631 qc->err_mask |= AC_ERR_DEV;
5632 else
Tejun Heo55a8e2c2006-11-10 18:08:10 +09005633 /* 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;
Albert Lee3655d1d2006-05-19 11:43:04 +08005639
Albert Leee2cec772006-03-25 17:43:49 +08005640 ap->hsm_task_state = HSM_ST_ERR;
5641 goto fsm_start;
5642 }
5643
Albert Leeeee6c322006-04-01 17:38:43 +08005644 /* 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.
Albert Lee71601952006-03-25 18:11:12 +08005653 */
5654 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Albert Lee71601952006-03-25 18:11:12 +08005655 /* data might be corrputed */
5656 qc->err_mask |= AC_ERR_DEV;
Albert Leeeee6c322006-04-01 17:38:43 +08005657
5658 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
5659 ata_pio_sectors(qc);
Albert Leeeee6c322006-04-01 17:38:43 +08005660 status = ata_wait_idle(ap);
5661 }
5662
Albert Lee3655d1d2006-05-19 11:43:04 +08005663 if (status & (ATA_BUSY | ATA_DRQ))
5664 qc->err_mask |= AC_ERR_HSM;
5665
Albert Leeeee6c322006-04-01 17:38:43 +08005666 /* 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;
Albert Lee71601952006-03-25 18:11:12 +08005672 }
5673
Albert Leee2cec772006-03-25 17:43:49 +08005674 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 */
Albert Lee52a32202006-03-25 18:18:15 +08005679 status = ata_wait_idle(ap);
Albert Leee2cec772006-03-25 17:43:49 +08005680 goto fsm_start;
5681 }
5682 }
5683
Albert Leebb5cb292006-03-25 17:48:02 +08005684 poll_next = 1;
Albert Leee2cec772006-03-25 17:43:49 +08005685 break;
5686
5687 case HSM_ST_LAST:
Albert Lee6912ccd2006-03-25 17:45:49 +08005688 if (unlikely(!ata_ok(status))) {
5689 qc->err_mask |= __ac_err_mask(status);
Albert Leee2cec772006-03-25 17:43:49 +08005690 ap->hsm_task_state = HSM_ST_ERR;
5691 goto fsm_start;
5692 }
5693
5694 /* no more data to transfer */
Albert Lee4332a772006-04-03 17:43:24 +08005695 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
Tejun Heo44877b42007-02-21 01:06:51 +09005696 ap->print_id, qc->dev->devno, status);
Albert Leee2cec772006-03-25 17:43:49 +08005697
Albert Lee6912ccd2006-03-25 17:45:49 +08005698 WARN_ON(qc->err_mask);
5699
Albert Leee2cec772006-03-25 17:43:49 +08005700 ap->hsm_task_state = HSM_ST_IDLE;
5701
5702 /* complete taskfile transaction */
Tejun Heoc17ea202006-05-15 20:59:29 +09005703 ata_hsm_qc_complete(qc, in_wq);
Albert Leebb5cb292006-03-25 17:48:02 +08005704
5705 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08005706 break;
5707
5708 case HSM_ST_ERR:
Albert Leee2cec772006-03-25 17:43:49 +08005709 /* 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;
Albert Leebb5cb292006-03-25 17:48:02 +08005715
Albert Lee999bb6f2006-03-25 18:07:48 +08005716 /* complete taskfile transaction */
Tejun Heoc17ea202006-05-15 20:59:29 +09005717 ata_hsm_qc_complete(qc, in_wq);
Albert Leebb5cb292006-03-25 17:48:02 +08005718
5719 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08005720 break;
5721 default:
Albert Leebb5cb292006-03-25 17:48:02 +08005722 poll_next = 0;
Albert Lee6912ccd2006-03-25 17:45:49 +08005723 BUG();
Albert Leee2cec772006-03-25 17:43:49 +08005724 }
5725
Albert Leebb5cb292006-03-25 17:48:02 +08005726 return poll_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727}
5728
David Howells65f27f32006-11-22 14:55:48 +00005729static void ata_pio_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730{
David Howells65f27f32006-11-22 14:55:48 +00005731 struct ata_port *ap =
5732 container_of(work, struct ata_port, port_task.work);
5733 struct ata_queued_cmd *qc = ap->port_task_data;
Albert Leea1af3732006-03-25 17:50:15 +08005734 u8 status;
5735 int poll_next;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04005736
5737fsm_start:
Albert Leea1af3732006-03-25 17:50:15 +08005738 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739
Albert Leea1af3732006-03-25 17:50:15 +08005740 /*
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) {
Jeff Garzik442eacc2007-12-19 04:25:10 -05005752 ata_pio_queue_task(ap, qc, ATA_SHORT_PAUSE);
Albert Leea1af3732006-03-25 17:50:15 +08005753 return;
5754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 }
5756
Albert Leea1af3732006-03-25 17:50:15 +08005757 /* 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)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04005764 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765}
5766
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 * 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:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005773 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 */
5775
5776static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
5777{
5778 struct ata_queued_cmd *qc = NULL;
5779 unsigned int i;
5780
Tejun Heoe3180492006-05-15 20:58:09 +09005781 /* no command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09005782 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
Tejun Heoe3180492006-05-15 20:58:09 +09005783 return NULL;
5784
Tejun Heo2ab7db12006-05-15 20:58:02 +09005785 /* the last tag is reserved for internal command. */
5786 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
Tejun Heo6cec4a32006-05-15 21:03:41 +09005787 if (!test_and_set_bit(i, &ap->qc_allocated)) {
Tejun Heof69499f2006-05-15 20:58:03 +09005788 qc = __ata_qc_from_tag(ap, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 * @dev: Device from whom we request an available command structure
5801 *
5802 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005803 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 */
5805
Tejun Heo3373efd2006-05-15 20:57:53 +09005806struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005808 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 struct ata_queued_cmd *qc;
5810
5811 qc = ata_qc_new(ap);
5812 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 qc->scsicmd = NULL;
5814 qc->ap = ap;
5815 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05005817 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 }
5819
5820 return qc;
5821}
5822
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823/**
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:
Jeff Garzikcca39742006-08-24 03:19:22 -04005831 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 */
5833void ata_qc_free(struct ata_queued_cmd *qc)
5834{
Tejun Heo4ba946e2006-01-23 13:09:36 +09005835 struct ata_port *ap = qc->ap;
5836 unsigned int tag;
5837
Tejun Heoa4631472006-02-11 19:11:13 +09005838 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839
Tejun Heo4ba946e2006-01-23 13:09:36 +09005840 qc->flags = 0;
5841 tag = qc->tag;
5842 if (likely(ata_tag_valid(tag))) {
Tejun Heo4ba946e2006-01-23 13:09:36 +09005843 qc->tag = ATA_TAG_POISON;
Tejun Heo6cec4a32006-05-15 21:03:41 +09005844 clear_bit(tag, &ap->qc_allocated);
Tejun Heo4ba946e2006-01-23 13:09:36 +09005845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846}
5847
Tejun Heo76014422006-02-11 15:13:49 +09005848void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849{
Tejun Heodedaf2b2006-05-15 21:03:43 +09005850 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005851 struct ata_link *link = qc->dev->link;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005852
Tejun Heoa4631472006-02-11 19:11:13 +09005853 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5854 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855
5856 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
5857 ata_sg_clean(qc);
5858
Tejun Heo7401abf2006-05-15 20:57:32 +09005859 /* command should be marked inactive atomically with qc completion */
Tejun Heoda917d62007-09-23 13:14:12 +09005860 if (qc->tf.protocol == ATA_PROT_NCQ) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005861 link->sactive &= ~(1 << qc->tag);
Tejun Heoda917d62007-09-23 13:14:12 +09005862 if (!link->sactive)
5863 ap->nr_active_links--;
5864 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005865 link->active_tag = ATA_TAG_POISON;
Tejun Heoda917d62007-09-23 13:14:12 +09005866 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;
Tejun Heo7401abf2006-05-15 20:57:32 +09005873
Albert Lee3f3791d2005-08-16 14:25:38 +08005874 /* 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;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005879 ap->qc_active &= ~(1 << qc->tag);
Albert Lee3f3791d2005-08-16 14:25:38 +08005880
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09005882 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883}
5884
Tejun Heo39599a52006-11-14 22:37:35 +09005885static void fill_result_tf(struct ata_queued_cmd *qc)
5886{
5887 struct ata_port *ap = qc->ap;
5888
Tejun Heo39599a52006-11-14 22:37:35 +09005889 qc->result_tf.flags = qc->tf.flags;
Mark Lord4742d542007-04-02 16:20:35 -04005890 ap->ops->tf_read(ap, &qc->result_tf);
Tejun Heo39599a52006-11-14 22:37:35 +09005891}
5892
Tejun Heo00115e02007-11-27 19:28:58 +09005893static 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
Tejun Heof686bcb2006-05-15 20:58:05 +09005909/**
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:
Jeff Garzikcca39742006-08-24 03:19:22 -04005918 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +09005919 */
5920void 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) {
Tejun Heo4dbfa392007-10-25 18:22:44 +09005938 struct ata_device *dev = qc->dev;
5939 struct ata_eh_info *ehi = &dev->link->eh_info;
5940
Tejun Heob51e9e52006-06-29 01:29:30 +09005941 WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
Tejun Heof686bcb2006-05-15 20:58:05 +09005942
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 */
Tejun Heo39599a52006-11-14 22:37:35 +09005949 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005950 ata_qc_schedule_eh(qc);
5951 return;
5952 }
5953 }
5954
5955 /* read result TF if requested */
5956 if (qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005957 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005958
Tejun Heo4dbfa392007-10-25 18:22:44 +09005959 /* 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;
Tejun Heo054a5fb2007-10-25 18:30:36 +09005974
5975 case ATA_CMD_SLEEP:
5976 dev->flags |= ATA_DFLAG_SLEEPING;
5977 break;
Tejun Heo4dbfa392007-10-25 18:22:44 +09005978 }
5979
Tejun Heo00115e02007-11-27 19:28:58 +09005980 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
5981 ata_verify_xfer(qc);
5982
Tejun Heof686bcb2006-05-15 20:58:05 +09005983 __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)
Tejun Heo39599a52006-11-14 22:37:35 +09005990 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005991
5992 __ata_qc_complete(qc);
5993 }
5994}
5995
Tejun Heodedaf2b2006-05-15 21:03:43 +09005996/**
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:
Jeff Garzikcca39742006-08-24 03:19:22 -04006008 * spin_lock_irqsave(host lock)
Tejun Heodedaf2b2006-05-15 21:03:43 +09006009 *
6010 * RETURNS:
6011 * Number of completed commands on success, -errno otherwise.
6012 */
6013int 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045/**
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:
Jeff Garzikcca39742006-08-24 03:19:22 -04006055 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09006057void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058{
6059 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006060 struct ata_link *link = qc->dev->link;
Tejun Heo405e66b2007-11-27 19:28:53 +09006061 u8 prot = qc->tf.protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062
Tejun Heodedaf2b2006-05-15 21:03:43 +09006063 /* 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 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006067 WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
Tejun Heodedaf2b2006-05-15 21:03:43 +09006068
Tejun Heo1973a022007-12-05 10:36:13 +09006069 if (ata_is_ncq(prot)) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006070 WARN_ON(link->sactive & (1 << qc->tag));
Tejun Heoda917d62007-09-23 13:14:12 +09006071
6072 if (!link->sactive)
6073 ap->nr_active_links++;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006074 link->sactive |= 1 << qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09006075 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006076 WARN_ON(link->sactive);
Tejun Heoda917d62007-09-23 13:14:12 +09006077
6078 ap->nr_active_links++;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006079 link->active_tag = qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09006080 }
6081
Tejun Heoe4a70e72006-03-31 20:36:47 +09006082 qc->flags |= ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09006083 ap->qc_active |= 1 << qc->tag;
Tejun Heoe4a70e72006-03-31 20:36:47 +09006084
Tejun Heof92a2632007-12-05 16:43:10 +09006085 /* We guarantee to LLDs that they will have at least one
6086 * non-zero sg if the command is a data command.
6087 */
Tejun Heoff2aeb12007-12-05 16:43:11 +09006088 BUG_ON(ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes));
Tejun Heof92a2632007-12-05 16:43:10 +09006089
Tejun Heo405e66b2007-11-27 19:28:53 +09006090 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
Tejun Heof92a2632007-12-05 16:43:10 +09006091 (ap->flags & ATA_FLAG_PIO_DMA)))
Tejun Heo001102d2007-12-05 16:43:09 +09006092 if (ata_sg_setup(qc))
6093 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094
Tejun Heocf480622008-01-24 00:05:14 +09006095 /* if device is sleeping, schedule reset and abort the link */
Tejun Heo054a5fb2007-10-25 18:30:36 +09006096 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
Tejun Heocf480622008-01-24 00:05:14 +09006097 link->eh_info.action |= ATA_EH_RESET;
Tejun Heo054a5fb2007-10-25 18:30:36 +09006098 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
6099 ata_link_abort(link);
6100 return;
6101 }
6102
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 ap->ops->qc_prep(qc);
6104
Tejun Heo8e0e6942006-03-31 20:41:11 +09006105 qc->err_mask |= ap->ops->qc_issue(qc);
6106 if (unlikely(qc->err_mask))
6107 goto err;
6108 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109
Tejun Heo8e436af2006-01-23 13:09:36 +09006110sg_err:
Tejun Heo8e0e6942006-03-31 20:41:11 +09006111 qc->err_mask |= AC_ERR_SYSTEM;
6112err:
6113 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114}
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 *
Edward Falk0baab862005-06-02 18:17:13 -04006125 * May be used as the qc_issue() entry in ata_port_operations.
6126 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04006128 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 *
6130 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09006131 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 */
6133
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09006134unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135{
6136 struct ata_port *ap = qc->ap;
6137
Albert Leee50362e2005-09-27 17:39:50 +08006138 /* 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:
Albert Leee3472cb2006-12-07 11:37:58 +08006144 case ATA_PROT_NODATA:
Tejun Heo0dc36882007-12-18 16:34:43 -05006145 case ATAPI_PROT_PIO:
6146 case ATAPI_PROT_NODATA:
Albert Leee50362e2005-09-27 17:39:50 +08006147 qc->tf.flags |= ATA_TFLAG_POLLING;
6148 break;
Tejun Heo0dc36882007-12-18 16:34:43 -05006149 case ATAPI_PROT_DMA:
Albert Leee50362e2005-09-27 17:39:50 +08006150 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
Albert Lee3a778272006-06-22 13:00:25 +08006151 /* see ata_dma_blacklisted() */
Albert Leee50362e2005-09-27 17:39:50 +08006152 BUG();
6153 break;
6154 default:
6155 break;
6156 }
6157 }
6158
Albert Lee312f7da2005-09-27 17:38:03 +08006159 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160 ata_dev_select(ap, qc->dev->devno, 1, 0);
6161
Albert Lee312f7da2005-09-27 17:38:03 +08006162 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 switch (qc->tf.protocol) {
6164 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08006165 if (qc->tf.flags & ATA_TFLAG_POLLING)
6166 ata_qc_set_polling(qc);
6167
Jeff Garzike5338252005-10-30 21:37:17 -05006168 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08006169 ap->hsm_task_state = HSM_ST_LAST;
6170
6171 if (qc->tf.flags & ATA_TFLAG_POLLING)
Jeff Garzik442eacc2007-12-19 04:25:10 -05006172 ata_pio_queue_task(ap, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08006173
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 break;
6175
6176 case ATA_PROT_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05006177 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08006178
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 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 */
Albert Lee312f7da2005-09-27 17:38:03 +08006182 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 break;
6184
Albert Lee312f7da2005-09-27 17:38:03 +08006185 case ATA_PROT_PIO:
6186 if (qc->tf.flags & ATA_TFLAG_POLLING)
6187 ata_qc_set_polling(qc);
6188
Jeff Garzike5338252005-10-30 21:37:17 -05006189 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08006190
Albert Lee54f00382005-09-30 19:14:19 +08006191 if (qc->tf.flags & ATA_TFLAG_WRITE) {
6192 /* PIO data out protocol */
6193 ap->hsm_task_state = HSM_ST_FIRST;
Jeff Garzik442eacc2007-12-19 04:25:10 -05006194 ata_pio_queue_task(ap, qc, 0);
Albert Lee54f00382005-09-30 19:14:19 +08006195
6196 /* always send first data block using
Albert Leee27486d2005-11-01 19:24:49 +08006197 * the ata_pio_task() codepath.
Albert Lee54f00382005-09-30 19:14:19 +08006198 */
Albert Lee312f7da2005-09-27 17:38:03 +08006199 } else {
Albert Lee54f00382005-09-30 19:14:19 +08006200 /* PIO data in protocol */
6201 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08006202
Albert Lee54f00382005-09-30 19:14:19 +08006203 if (qc->tf.flags & ATA_TFLAG_POLLING)
Jeff Garzik442eacc2007-12-19 04:25:10 -05006204 ata_pio_queue_task(ap, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08006205
Albert Lee54f00382005-09-30 19:14:19 +08006206 /* if polling, ata_pio_task() handles the rest.
6207 * otherwise, interrupt handler takes over from here.
6208 */
Albert Lee312f7da2005-09-27 17:38:03 +08006209 }
6210
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 break;
6212
Tejun Heo0dc36882007-12-18 16:34:43 -05006213 case ATAPI_PROT_PIO:
6214 case ATAPI_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08006215 if (qc->tf.flags & ATA_TFLAG_POLLING)
6216 ata_qc_set_polling(qc);
6217
Jeff Garzike5338252005-10-30 21:37:17 -05006218 ata_tf_to_host(ap, &qc->tf);
Jeff Garzikf6ef65e2006-01-27 02:45:00 -05006219
Albert Lee312f7da2005-09-27 17:38:03 +08006220 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))
Jeff Garzik442eacc2007-12-19 04:25:10 -05006225 ata_pio_queue_task(ap, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 break;
6227
Tejun Heo0dc36882007-12-18 16:34:43 -05006228 case ATAPI_PROT_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05006229 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08006230
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
6232 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08006233 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))
Jeff Garzik442eacc2007-12-19 04:25:10 -05006237 ata_pio_queue_task(ap, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 break;
6239
6240 default:
6241 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09006242 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 }
6244
6245 return 0;
6246}
6247
6248/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 * 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:
Jeff Garzikcca39742006-08-24 03:19:22 -04006258 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 *
6260 * RETURNS:
6261 * One if interrupt was handled, zero if not (shared irq).
6262 */
6263
Jeff Garzik2dcb4072007-10-19 06:42:56 -04006264inline unsigned int ata_host_intr(struct ata_port *ap,
6265 struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006267 struct ata_eh_info *ehi = &ap->link.eh_info;
Albert Lee312f7da2005-09-27 17:38:03 +08006268 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269
Albert Lee312f7da2005-09-27 17:38:03 +08006270 VPRINTK("ata%u: protocol %d task_state %d\n",
Tejun Heo44877b42007-02-21 01:06:51 +09006271 ap->print_id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272
Albert Lee312f7da2005-09-27 17:38:03 +08006273 /* Check whether we are expecting interrupt in this state */
6274 switch (ap->hsm_task_state) {
6275 case HSM_ST_FIRST:
Albert Lee6912ccd2006-03-25 17:45:49 +08006276 /* Some pre-ATAPI-4 devices assert INTRQ
6277 * at this state when ready to receive CDB.
6278 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279
Albert Lee312f7da2005-09-27 17:38:03 +08006280 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
Tejun Heo405e66b2007-11-27 19:28:53 +09006281 * The flag was turned on only for atapi devices. No
6282 * need to check ata_is_atapi(qc->tf.protocol) again.
Albert Lee312f7da2005-09-27 17:38:03 +08006283 */
6284 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 break;
Albert Lee312f7da2005-09-27 17:38:03 +08006287 case HSM_ST_LAST:
6288 if (qc->tf.protocol == ATA_PROT_DMA ||
Tejun Heo0dc36882007-12-18 16:34:43 -05006289 qc->tf.protocol == ATAPI_PROT_DMA) {
Albert Lee312f7da2005-09-27 17:38:03 +08006290 /* check status of DMA engine */
6291 host_stat = ap->ops->bmdma_status(ap);
Tejun Heo44877b42007-02-21 01:06:51 +09006292 VPRINTK("ata%u: host_stat 0x%X\n",
6293 ap->print_id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
Albert Lee312f7da2005-09-27 17:38:03 +08006295 /* if it's not our irq... */
6296 if (!(host_stat & ATA_DMA_INTR))
6297 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298
Albert Lee312f7da2005-09-27 17:38:03 +08006299 /* before we do anything else, clear DMA-Start bit */
6300 ap->ops->bmdma_stop(qc);
Albert Leea4f16612005-12-26 16:40:53 +08006301
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 }
Albert Lee312f7da2005-09-27 17:38:03 +08006307 }
6308 break;
6309 case HSM_ST:
6310 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 default:
6312 goto idle_irq;
6313 }
6314
Albert Lee312f7da2005-09-27 17:38:03 +08006315 /* check altstatus */
6316 status = ata_altstatus(ap);
6317 if (status & ATA_BUSY)
6318 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319
Albert Lee312f7da2005-09-27 17:38:03 +08006320 /* check main status, clearing INTRQ */
6321 status = ata_chk_status(ap);
6322 if (unlikely(status & ATA_BUSY))
6323 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324
Albert Lee312f7da2005-09-27 17:38:03 +08006325 /* ack bmdma irq events */
6326 ap->ops->irq_clear(ap);
6327
Albert Leebb5cb292006-03-25 17:48:02 +08006328 ata_hsm_move(ap, qc, status, 0);
Tejun Heoea547632006-11-17 12:06:21 +09006329
6330 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
Tejun Heo0dc36882007-12-18 16:34:43 -05006331 qc->tf.protocol == ATAPI_PROT_DMA))
Tejun Heoea547632006-11-17 12:06:21 +09006332 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
6333
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 return 1; /* irq handled */
6335
6336idle_irq:
6337 ap->stats.idle_irq++;
6338
6339#ifdef ATA_IRQ_TRAP
6340 if ((ap->stats.idle_irq % 1000) == 0) {
Jeff Garzik6d32d302007-08-15 05:38:46 -04006341 ata_chk_status(ap);
6342 ap->ops->irq_clear(ap);
Tejun Heof15a1da2006-05-15 20:57:56 +09006343 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
Alan Cox23cfce82006-03-21 16:06:53 +00006344 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 }
6346#endif
6347 return 0; /* irq not handled */
6348}
6349
6350/**
6351 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04006352 * @irq: irq line (unused)
Jeff Garzikcca39742006-08-24 03:19:22 -04006353 * @dev_instance: pointer to our ata_host information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04006355 * Default interrupt handler for PCI IDE devices. Calls
6356 * ata_host_intr() for each port that is not disabled.
6357 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04006359 * Obtains host lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 *
6361 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04006362 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 */
6364
Jeff Garzik2dcb4072007-10-19 06:42:56 -04006365irqreturn_t ata_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366{
Jeff Garzikcca39742006-08-24 03:19:22 -04006367 struct ata_host *host = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368 unsigned int i;
6369 unsigned int handled = 0;
6370 unsigned long flags;
6371
6372 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
Jeff Garzikcca39742006-08-24 03:19:22 -04006373 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374
Jeff Garzikcca39742006-08-24 03:19:22 -04006375 for (i = 0; i < host->n_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 struct ata_port *ap;
6377
Jeff Garzikcca39742006-08-24 03:19:22 -04006378 ap = host->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09006379 if (ap &&
Jeff Garzik029f5462006-04-02 10:30:40 -04006380 !(ap->flags & ATA_FLAG_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 struct ata_queued_cmd *qc;
6382
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006383 qc = ata_qc_from_tag(ap, ap->link.active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08006384 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08006385 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 handled |= ata_host_intr(ap, qc);
6387 }
6388 }
6389
Jeff Garzikcca39742006-08-24 03:19:22 -04006390 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391
6392 return IRQ_RETVAL(handled);
6393}
6394
Tejun Heo34bf2172006-05-15 20:57:46 +09006395/**
6396 * sata_scr_valid - test whether SCRs are accessible
Tejun Heo936fd732007-08-06 18:36:23 +09006397 * @link: ATA link to test SCR accessibility for
Tejun Heo34bf2172006-05-15 20:57:46 +09006398 *
Tejun Heo936fd732007-08-06 18:36:23 +09006399 * Test whether SCRs are accessible for @link.
Tejun Heo34bf2172006-05-15 20:57:46 +09006400 *
6401 * LOCKING:
6402 * None.
6403 *
6404 * RETURNS:
6405 * 1 if SCRs are accessible, 0 otherwise.
6406 */
Tejun Heo936fd732007-08-06 18:36:23 +09006407int sata_scr_valid(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09006408{
Tejun Heo936fd732007-08-06 18:36:23 +09006409 struct ata_port *ap = link->ap;
6410
Tejun Heoa16abc02007-05-21 18:33:47 +02006411 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
Tejun Heo34bf2172006-05-15 20:57:46 +09006412}
6413
6414/**
6415 * sata_scr_read - read SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09006416 * @link: ATA link to read SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09006417 * @reg: SCR to read
6418 * @val: Place to store read value
6419 *
Tejun Heo936fd732007-08-06 18:36:23 +09006420 * Read SCR register @reg of @link into *@val. This function is
Tejun Heo633273a2007-09-23 13:19:54 +09006421 * guaranteed to succeed if @link is ap->link, the cable type of
6422 * the port is SATA and the port implements ->scr_read.
Tejun Heo34bf2172006-05-15 20:57:46 +09006423 *
6424 * LOCKING:
Tejun Heo633273a2007-09-23 13:19:54 +09006425 * None if @link is ap->link. Kernel thread context otherwise.
Tejun Heo34bf2172006-05-15 20:57:46 +09006426 *
6427 * RETURNS:
6428 * 0 on success, negative errno on failure.
6429 */
Tejun Heo936fd732007-08-06 18:36:23 +09006430int sata_scr_read(struct ata_link *link, int reg, u32 *val)
Tejun Heo34bf2172006-05-15 20:57:46 +09006431{
Tejun Heo633273a2007-09-23 13:19:54 +09006432 if (ata_is_host_link(link)) {
6433 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09006434
Tejun Heo633273a2007-09-23 13:19:54 +09006435 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);
Tejun Heo34bf2172006-05-15 20:57:46 +09006441}
6442
6443/**
6444 * sata_scr_write - write SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09006445 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09006446 * @reg: SCR to write
6447 * @val: value to write
6448 *
Tejun Heo936fd732007-08-06 18:36:23 +09006449 * Write @val to SCR register @reg of @link. This function is
Tejun Heo633273a2007-09-23 13:19:54 +09006450 * guaranteed to succeed if @link is ap->link, the cable type of
6451 * the port is SATA and the port implements ->scr_read.
Tejun Heo34bf2172006-05-15 20:57:46 +09006452 *
6453 * LOCKING:
Tejun Heo633273a2007-09-23 13:19:54 +09006454 * None if @link is ap->link. Kernel thread context otherwise.
Tejun Heo34bf2172006-05-15 20:57:46 +09006455 *
6456 * RETURNS:
6457 * 0 on success, negative errno on failure.
6458 */
Tejun Heo936fd732007-08-06 18:36:23 +09006459int sata_scr_write(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09006460{
Tejun Heo633273a2007-09-23 13:19:54 +09006461 if (ata_is_host_link(link)) {
6462 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09006463
Tejun Heo633273a2007-09-23 13:19:54 +09006464 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);
Tejun Heo34bf2172006-05-15 20:57:46 +09006470}
6471
6472/**
6473 * sata_scr_write_flush - write SCR register of the specified port and flush
Tejun Heo936fd732007-08-06 18:36:23 +09006474 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09006475 * @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:
Tejun Heo633273a2007-09-23 13:19:54 +09006482 * None if @link is ap->link. Kernel thread context otherwise.
Tejun Heo34bf2172006-05-15 20:57:46 +09006483 *
6484 * RETURNS:
6485 * 0 on success, negative errno on failure.
6486 */
Tejun Heo936fd732007-08-06 18:36:23 +09006487int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09006488{
Tejun Heo633273a2007-09-23 13:19:54 +09006489 if (ata_is_host_link(link)) {
6490 struct ata_port *ap = link->ap;
6491 int rc;
Tejun Heoda3dbb12007-07-16 14:29:40 +09006492
Tejun Heo633273a2007-09-23 13:19:54 +09006493 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;
Tejun Heo34bf2172006-05-15 20:57:46 +09006500 }
Tejun Heo633273a2007-09-23 13:19:54 +09006501
6502 return sata_pmp_scr_write(link, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09006503}
6504
6505/**
Tejun Heo936fd732007-08-06 18:36:23 +09006506 * ata_link_online - test whether the given link is online
6507 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09006508 *
Tejun Heo936fd732007-08-06 18:36:23 +09006509 * 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).
Tejun Heo34bf2172006-05-15 20:57:46 +09006512 *
6513 * LOCKING:
6514 * None.
6515 *
6516 * RETURNS:
6517 * 1 if the port online status is available and online.
6518 */
Tejun Heo936fd732007-08-06 18:36:23 +09006519int ata_link_online(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09006520{
6521 u32 sstatus;
6522
Tejun Heo936fd732007-08-06 18:36:23 +09006523 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
6524 (sstatus & 0xf) == 0x3)
Tejun Heo34bf2172006-05-15 20:57:46 +09006525 return 1;
6526 return 0;
6527}
6528
6529/**
Tejun Heo936fd732007-08-06 18:36:23 +09006530 * ata_link_offline - test whether the given link is offline
6531 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09006532 *
Tejun Heo936fd732007-08-06 18:36:23 +09006533 * 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).
Tejun Heo34bf2172006-05-15 20:57:46 +09006536 *
6537 * LOCKING:
6538 * None.
6539 *
6540 * RETURNS:
6541 * 1 if the port offline status is available and offline.
6542 */
Tejun Heo936fd732007-08-06 18:36:23 +09006543int ata_link_offline(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09006544{
6545 u32 sstatus;
6546
Tejun Heo936fd732007-08-06 18:36:23 +09006547 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
6548 (sstatus & 0xf) != 0x3)
Tejun Heo34bf2172006-05-15 20:57:46 +09006549 return 1;
6550 return 0;
6551}
Edward Falk0baab862005-06-02 18:17:13 -04006552
Tejun Heo77b08fb2006-06-24 20:30:19 +09006553int ata_flush_cache(struct ata_device *dev)
Jens Axboe9b847542006-01-06 09:28:07 +01006554{
Tejun Heo977e6b92006-06-24 20:30:19 +09006555 unsigned int err_mask;
Jens Axboe9b847542006-01-06 09:28:07 +01006556 u8 cmd;
6557
6558 if (!ata_try_flush_cache(dev))
6559 return 0;
6560
Tejun Heo6fc49ad2006-11-11 20:10:45 +09006561 if (dev->flags & ATA_DFLAG_FLUSH_EXT)
Jens Axboe9b847542006-01-06 09:28:07 +01006562 cmd = ATA_CMD_FLUSH_EXT;
6563 else
6564 cmd = ATA_CMD_FLUSH;
6565
Alan Cox4f343372007-08-08 14:30:31 +01006566 /* 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
Jeff Garzik2dcb4072007-10-19 06:42:56 -04006568 a further flush command to continue the writeback until it
Alan Cox4f343372007-08-08 14:30:31 +01006569 does not error */
Tejun Heo977e6b92006-06-24 20:30:19 +09006570 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;
Jens Axboe9b847542006-01-06 09:28:07 +01006577}
6578
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006579#ifdef CONFIG_PM
Jeff Garzikcca39742006-08-24 03:19:22 -04006580static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
6581 unsigned int action, unsigned int ehi_flags,
6582 int wait)
Tejun Heo500530f2006-07-03 16:07:27 +09006583{
6584 unsigned long flags;
6585 int i, rc;
6586
Jeff Garzikcca39742006-08-24 03:19:22 -04006587 for (i = 0; i < host->n_ports; i++) {
6588 struct ata_port *ap = host->ports[i];
Tejun Heoe3667eb2007-08-06 18:36:24 +09006589 struct ata_link *link;
Tejun Heo500530f2006-07-03 16:07:27 +09006590
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;
Tejun Heoe3667eb2007-08-06 18:36:24 +09006609 __ata_port_for_each_link(link, ap) {
6610 link->eh_info.action |= action;
6611 link->eh_info.flags |= ehi_flags;
6612 }
Tejun Heo500530f2006-07-03 16:07:27 +09006613
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/**
Jeff Garzikcca39742006-08-24 03:19:22 -04006631 * ata_host_suspend - suspend host
6632 * @host: host to suspend
Tejun Heo500530f2006-07-03 16:07:27 +09006633 * @mesg: PM message
6634 *
Jeff Garzikcca39742006-08-24 03:19:22 -04006635 * Suspend @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09006636 * 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 */
Jeff Garzikcca39742006-08-24 03:19:22 -04006645int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006646{
Tejun Heo9666f402007-05-04 21:27:47 +02006647 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09006648
Kristen Carlson Accardica773292007-10-25 00:58:59 -04006649 /*
6650 * disable link pm on all ports before requesting
6651 * any pm activity
6652 */
6653 ata_lpm_enable(host);
6654
Jeff Garzikcca39742006-08-24 03:19:22 -04006655 rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
Jeff Garzik72ad6ec2008-02-25 17:31:10 -05006656 if (rc == 0)
6657 host->dev->power.power_state = mesg;
Tejun Heo500530f2006-07-03 16:07:27 +09006658 return rc;
6659}
6660
6661/**
Jeff Garzikcca39742006-08-24 03:19:22 -04006662 * ata_host_resume - resume host
6663 * @host: host to resume
Tejun Heo500530f2006-07-03 16:07:27 +09006664 *
Jeff Garzikcca39742006-08-24 03:19:22 -04006665 * Resume @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09006666 * 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 */
Jeff Garzikcca39742006-08-24 03:19:22 -04006672void ata_host_resume(struct ata_host *host)
Tejun Heo500530f2006-07-03 16:07:27 +09006673{
Tejun Heocf480622008-01-24 00:05:14 +09006674 ata_host_request_pm(host, PMSG_ON, ATA_EH_RESET,
Jeff Garzikcca39742006-08-24 03:19:22 -04006675 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
Jeff Garzik72ad6ec2008-02-25 17:31:10 -05006676 host->dev->power.power_state = PMSG_ON;
Kristen Carlson Accardica773292007-10-25 00:58:59 -04006677
6678 /* reenable link pm */
6679 ata_lpm_disable(host);
Tejun Heo500530f2006-07-03 16:07:27 +09006680}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006681#endif
Tejun Heo500530f2006-07-03 16:07:27 +09006682
Randy Dunlapc893a3a2006-01-28 13:15:32 -05006683/**
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 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09006695int ata_port_start(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696{
Brian King2f1f6102006-03-23 17:30:15 -06006697 struct device *dev = ap->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698
Tejun Heof0d36ef2007-01-20 16:00:28 +09006699 ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
6700 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 if (!ap->prd)
6702 return -ENOMEM;
6703
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 return 0;
6705}
6706
Edward Falk0baab862005-06-02 18:17:13 -04006707/**
Tejun Heo3ef3b432006-05-31 18:27:30 +09006708 * 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 */
6716void ata_dev_init(struct ata_device *dev)
6717{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006718 struct ata_link *link = dev->link;
6719 struct ata_port *ap = link->ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09006720 unsigned long flags;
Tejun Heo3ef3b432006-05-31 18:27:30 +09006721
Tejun Heo5a04bf42006-05-31 18:27:38 +09006722 /* SATA spd limit is bound to the first device */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006723 link->sata_spd_limit = link->hw_sata_spd_limit;
6724 link->sata_spd = 0;
Tejun Heo5a04bf42006-05-31 18:27:38 +09006725
Tejun Heo72fa4b72006-05-31 18:27:32 +09006726 /* High bits of dev->flags are used to record warm plug
6727 * requests which occur asynchronously. Synchronize using
Jeff Garzikcca39742006-08-24 03:19:22 -04006728 * host lock.
Tejun Heo72fa4b72006-05-31 18:27:32 +09006729 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006730 spin_lock_irqsave(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09006731 dev->flags &= ~ATA_DFLAG_INIT_MASK;
Tejun Heo3dcc3232007-09-03 12:20:11 +09006732 dev->horkage = 0;
Jeff Garzikba6a1302006-06-22 23:46:10 -04006733 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09006734
6735 memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
6736 sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
Tejun Heo3ef3b432006-05-31 18:27:30 +09006737 dev->pio_mask = UINT_MAX;
6738 dev->mwdma_mask = UINT_MAX;
6739 dev->udma_mask = UINT_MAX;
6740}
6741
6742/**
Tejun Heo4fb37a22007-08-06 18:36:23 +09006743 * ata_link_init - Initialize an ata_link structure
6744 * @ap: ATA port link is attached to
6745 * @link: Link structure to initialize
Tejun Heo89898052007-08-06 18:36:23 +09006746 * @pmp: Port multiplier port number
Tejun Heo4fb37a22007-08-06 18:36:23 +09006747 *
6748 * Initialize @link.
6749 *
6750 * LOCKING:
6751 * Kernel thread context (may sleep)
6752 */
Tejun Heofb7fd612007-09-23 13:14:12 +09006753void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
Tejun Heo4fb37a22007-08-06 18:36:23 +09006754{
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;
Tejun Heo89898052007-08-06 18:36:23 +09006761 link->pmp = pmp;
Tejun Heo4fb37a22007-08-06 18:36:23 +09006762 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 */
Tejun Heofb7fd612007-09-23 13:14:12 +09006788int sata_link_init_spd(struct ata_link *link)
Tejun Heo4fb37a22007-08-06 18:36:23 +09006789{
Tejun Heo33267322008-02-13 09:15:09 +09006790 u32 scontrol;
6791 u8 spd;
Tejun Heo4fb37a22007-08-06 18:36:23 +09006792 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
Tejun Heo33267322008-02-13 09:15:09 +09006802 ata_force_spd_limit(link);
6803
Tejun Heo4fb37a22007-08-06 18:36:23 +09006804 link->sata_spd_limit = link->hw_sata_spd_limit;
6805
6806 return 0;
6807}
6808
6809/**
Tejun Heof3187192007-04-17 23:44:07 +09006810 * ata_port_alloc - allocate and initialize basic ATA port resources
6811 * @host: ATA host this allocated port belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812 *
Tejun Heof3187192007-04-17 23:44:07 +09006813 * Allocate and initialize basic ATA port resources.
6814 *
6815 * RETURNS:
6816 * Allocate ATA port on success, NULL on failure.
Jeff Garzik0cba6322005-05-30 19:49:12 -04006817 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09006819 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 */
Tejun Heof3187192007-04-17 23:44:07 +09006821struct ata_port *ata_port_alloc(struct ata_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822{
Tejun Heof3187192007-04-17 23:44:07 +09006823 struct ata_port *ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824
Tejun Heof3187192007-04-17 23:44:07 +09006825 DPRINTK("ENTER\n");
6826
6827 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
6828 if (!ap)
6829 return NULL;
6830
Tejun Heof4d6d002007-05-01 11:50:15 +02006831 ap->pflags |= ATA_PFLAG_INITIALIZING;
Jeff Garzikcca39742006-08-24 03:19:22 -04006832 ap->lock = &host->lock;
Tejun Heo198e0fe2006-04-02 18:51:52 +09006833 ap->flags = ATA_FLAG_DISABLED;
Tejun Heof3187192007-04-17 23:44:07 +09006834 ap->print_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835 ap->ctl = ATA_DEVCTL_OBS;
Jeff Garzikcca39742006-08-24 03:19:22 -04006836 ap->host = host;
Tejun Heof3187192007-04-17 23:44:07 +09006837 ap->dev = host->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 ap->last_ctl = 0xFF;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04006839
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;
Tejun Heo88574552006-06-25 20:00:35 +09006845#else
Borislav Petkov0dd4b212006-06-23 02:29:08 -04006846 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04006847#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848
Jeff Garzik442eacc2007-12-19 04:25:10 -05006849 INIT_DELAYED_WORK(&ap->port_task, ata_pio_task);
David Howells65f27f32006-11-22 14:55:48 +00006850 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
6851 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09006852 INIT_LIST_HEAD(&ap->eh_done_q);
Tejun Heoc6cf9e92006-05-31 18:27:27 +09006853 init_waitqueue_head(&ap->eh_wait_q);
Tejun Heo5ddf24c2007-07-16 14:29:41 +09006854 init_timer_deferrable(&ap->fastdrain_timer);
6855 ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
6856 ap->fastdrain_timer.data = (unsigned long)ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857
Tejun Heo838df622006-05-15 20:57:44 +09006858 ap->cbl = ATA_CBL_NONE;
Tejun Heo838df622006-05-15 20:57:44 +09006859
Tejun Heo89898052007-08-06 18:36:23 +09006860 ata_link_init(ap, &ap->link, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861
6862#ifdef ATA_IRQ_TRAP
6863 ap->stats.unhandled_irq = 1;
6864 ap->stats.idle_irq = 1;
6865#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866 return ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867}
6868
Tejun Heof0d36ef2007-01-20 16:00:28 +09006869static 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
Tejun Heoecef7252007-04-17 23:44:06 +09006877 if (!ap)
6878 continue;
6879
Tejun Heo49114872007-04-17 23:44:06 +09006880 if (ap->scsi_host)
Tejun Heo1aa506e2007-03-09 19:36:12 +09006881 scsi_host_put(ap->scsi_host);
6882
Tejun Heo633273a2007-09-23 13:19:54 +09006883 kfree(ap->pmp_link);
Tejun Heo49114872007-04-17 23:44:06 +09006884 kfree(ap);
Tejun Heo1aa506e2007-03-09 19:36:12 +09006885 host->ports[i] = NULL;
6886 }
6887
Tejun Heo1aa56cc2007-02-27 22:33:21 +09006888 dev_set_drvdata(gendev, NULL);
Tejun Heof0d36ef2007-01-20 16:00:28 +09006889}
6890
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891/**
Tejun Heof3187192007-04-17 23:44:07 +09006892 * 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 */
6911struct 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/**
Tejun Heof5cda252007-04-17 23:44:07 +09006957 * 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 */
6972struct 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;
Tejun Heo0c887582007-08-06 18:36:23 +09006994 ap->link.flags |= pi->link_flags;
Tejun Heof5cda252007-04-17 23:44:07 +09006995 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
Tejun Heo32ebbc02007-11-08 13:09:00 +09007006static 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
Tejun Heof5cda252007-04-17 23:44:07 +09007024/**
Tejun Heo029cfd62008-03-25 12:22:49 +09007025 * 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 */
7044static 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/**
Tejun Heoecef7252007-04-17 23:44:06 +09007075 * 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
Tejun Heof3187192007-04-17 23:44:07 +09007081 * once. If host->ops isn't initialized yet, its set to the
7082 * first non-dummy port ops.
Tejun Heoecef7252007-04-17 23:44:06 +09007083 *
7084 * LOCKING:
7085 * Inherited from calling layer (may sleep).
7086 *
7087 * RETURNS:
7088 * 0 if all ports are started successfully, -errno otherwise.
7089 */
7090int ata_host_start(struct ata_host *host)
7091{
Tejun Heo32ebbc02007-11-08 13:09:00 +09007092 int have_stop = 0;
7093 void *start_dr = NULL;
Tejun Heoecef7252007-04-17 23:44:06 +09007094 int i, rc;
7095
7096 if (host->flags & ATA_HOST_STARTED)
7097 return 0;
7098
Tejun Heo029cfd62008-03-25 12:22:49 +09007099 ata_finalize_port_ops(host->ops);
7100
Tejun Heoecef7252007-04-17 23:44:06 +09007101 for (i = 0; i < host->n_ports; i++) {
7102 struct ata_port *ap = host->ports[i];
7103
Tejun Heo029cfd62008-03-25 12:22:49 +09007104 ata_finalize_port_ops(ap->ops);
7105
Tejun Heof3187192007-04-17 23:44:07 +09007106 if (!host->ops && !ata_port_is_dummy(ap))
7107 host->ops = ap->ops;
7108
Tejun Heo32ebbc02007-11-08 13:09:00 +09007109 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
Tejun Heoecef7252007-04-17 23:44:06 +09007125 if (ap->ops->port_start) {
7126 rc = ap->ops->port_start(ap);
7127 if (rc) {
Alan Cox0f9fe9b2007-11-30 15:23:16 +00007128 if (rc != -ENODEV)
Andrew Morton0f757742008-01-10 14:33:09 -08007129 dev_printk(KERN_ERR, host->dev,
7130 "failed to start port %d "
7131 "(errno=%d)\n", i, rc);
Tejun Heoecef7252007-04-17 23:44:06 +09007132 goto err_out;
7133 }
7134 }
Tejun Heoecef7252007-04-17 23:44:06 +09007135 ata_eh_freeze_port(ap);
7136 }
7137
Tejun Heo32ebbc02007-11-08 13:09:00 +09007138 if (start_dr)
7139 devres_add(host->dev, start_dr);
Tejun Heoecef7252007-04-17 23:44:06 +09007140 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 }
Tejun Heo32ebbc02007-11-08 13:09:00 +09007150 devres_free(start_dr);
Tejun Heoecef7252007-04-17 23:44:06 +09007151 return rc;
7152}
7153
7154/**
Jeff Garzikcca39742006-08-24 03:19:22 -04007155 * 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
Brian Kingb03732f2006-08-07 14:27:10 -05007160 *
7161 * LOCKING:
7162 * PCI/etc. bus probe sem.
7163 *
7164 */
Tejun Heof3187192007-04-17 23:44:07 +09007165/* KILLME - the only user left is ipr */
Jeff Garzikcca39742006-08-24 03:19:22 -04007166void ata_host_init(struct ata_host *host, struct device *dev,
Tejun Heo029cfd62008-03-25 12:22:49 +09007167 unsigned long flags, struct ata_port_operations *ops)
Brian Kingb03732f2006-08-07 14:27:10 -05007168{
Jeff Garzikcca39742006-08-24 03:19:22 -04007169 spin_lock_init(&host->lock);
7170 host->dev = dev;
7171 host->flags = flags;
7172 host->ops = ops;
Brian Kingb03732f2006-08-07 14:27:10 -05007173}
7174
7175/**
Tejun Heof3187192007-04-17 23:44:07 +09007176 * ata_host_register - register initialized ATA host
7177 * @host: ATA host to register
7178 * @sht: template for SCSI host
Jeff Garzik0cba6322005-05-30 19:49:12 -04007179 *
Tejun Heof3187192007-04-17 23:44:07 +09007180 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184 *
7185 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09007186 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187 *
7188 * RETURNS:
Tejun Heof3187192007-04-17 23:44:07 +09007189 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190 */
Tejun Heof3187192007-04-17 23:44:07 +09007191int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192{
Tejun Heof3187192007-04-17 23:44:07 +09007193 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194
Tejun Heof3187192007-04-17 23:44:07 +09007195 /* 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;
Alan Cox02f076a2006-09-26 17:35:32 +01007201 }
Tejun Heof0d36ef2007-01-20 16:00:28 +09007202
Tejun Heof3187192007-04-17 23:44:07 +09007203 /* 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209
Tejun Heof3187192007-04-17 23:44:07 +09007210 /* 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++;
Tejun Heof0d36ef2007-01-20 16:00:28 +09007213
Tejun Heof3187192007-04-17 23:44:07 +09007214 rc = ata_scsi_add_hosts(host, sht);
Tejun Heoecef7252007-04-17 23:44:06 +09007215 if (rc)
Tejun Heof3187192007-04-17 23:44:07 +09007216 return rc;
Tejun Heoecef7252007-04-17 23:44:06 +09007217
Tejun Heofafbae82007-05-15 03:28:16 +09007218 /* associate with ACPI nodes */
7219 ata_acpi_associate(host);
7220
Tejun Heof3187192007-04-17 23:44:07 +09007221 /* set cable, sata_spd_limit and report */
Jeff Garzikcca39742006-08-24 03:19:22 -04007222 for (i = 0; i < host->n_ports; i++) {
7223 struct ata_port *ap = host->ports[i];
Tejun Heof3187192007-04-17 23:44:07 +09007224 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229
Tejun Heo5a04bf42006-05-31 18:27:38 +09007230 /* init sata_spd_limit to the current value */
Tejun Heo4fb37a22007-08-06 18:36:23 +09007231 sata_link_init_spd(&ap->link);
Tejun Heo5a04bf42006-05-31 18:27:38 +09007232
Tejun Heocbcdd872007-08-18 13:14:55 +09007233 /* print per-port info to dmesg */
Tejun Heof3187192007-04-17 23:44:07 +09007234 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
7235 ap->udma_mask);
7236
Tejun Heoabf6e8e2007-10-09 14:57:25 +09007237 if (!ata_port_is_dummy(ap)) {
Tejun Heocbcdd872007-08-18 13:14:55 +09007238 ata_port_printk(ap, KERN_INFO,
7239 "%cATA max %s %s\n",
Tejun Heoa16abc02007-05-21 18:33:47 +02007240 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
Tejun Heof3187192007-04-17 23:44:07 +09007241 ata_mode_string(xfer_mask),
Tejun Heocbcdd872007-08-18 13:14:55 +09007242 ap->link.eh_info.desc);
Tejun Heoabf6e8e2007-10-09 14:57:25 +09007243 ata_ehi_clear_desc(&ap->link.eh_info);
7244 } else
Tejun Heof3187192007-04-17 23:44:07 +09007245 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];
Tejun Heof3187192007-04-17 23:44:07 +09007252
7253 /* probe */
Tejun Heo52783c52006-05-31 18:28:22 +09007254 if (ap->ops->error_handler) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09007255 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo3e706392006-05-31 18:28:11 +09007256 unsigned long flags;
7257
7258 ata_port_probe(ap);
7259
7260 /* kick EH for boot probing */
Jeff Garzikba6a1302006-06-22 23:46:10 -04007261 spin_lock_irqsave(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09007262
Tejun Heob558edd2008-01-24 00:05:14 +09007263 ehi->probe_mask |= ATA_ALL_DEVICES;
Tejun Heocf480622008-01-24 00:05:14 +09007264 ehi->action |= ATA_EH_RESET;
Tejun Heo1cdaf532006-07-03 16:07:26 +09007265 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
Tejun Heo3e706392006-05-31 18:28:11 +09007266
Tejun Heof4d6d002007-05-01 11:50:15 +02007267 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
Tejun Heob51e9e52006-06-29 01:29:30 +09007268 ap->pflags |= ATA_PFLAG_LOADING;
Tejun Heo3e706392006-05-31 18:28:11 +09007269 ata_port_schedule_eh(ap);
7270
Jeff Garzikba6a1302006-06-22 23:46:10 -04007271 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09007272
7273 /* wait for EH to finish */
7274 ata_port_wait_eh(ap);
7275 } else {
Tejun Heo44877b42007-02-21 01:06:51 +09007276 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
Tejun Heo3e706392006-05-31 18:28:11 +09007277 rc = ata_bus_probe(ap);
Tejun Heo44877b42007-02-21 01:06:51 +09007278 DPRINTK("ata%u: bus probe end\n", ap->print_id);
Tejun Heo3e706392006-05-31 18:28:11 +09007279
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007289 }
7290
7291 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05007292 DPRINTK("host probe begin\n");
Jeff Garzikcca39742006-08-24 03:19:22 -04007293 for (i = 0; i < host->n_ports; i++) {
7294 struct ata_port *ap = host->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295
Tejun Heo1ae46312007-07-16 14:29:40 +09007296 ata_scsi_scan_host(ap, 1);
Kristen Carlson Accardica773292007-10-25 00:58:59 -04007297 ata_lpm_schedule(ap, ap->pm_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007298 }
7299
Tejun Heof3187192007-04-17 23:44:07 +09007300 return 0;
7301}
7302
7303/**
Tejun Heof5cda252007-04-17 23:44:07 +09007304 * 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 *
Paul Mundt3d46b2e2007-11-08 11:14:56 +09007316 * 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 *
Tejun Heof5cda252007-04-17 23:44:07 +09007320 * LOCKING:
7321 * Inherited from calling layer (may sleep).
7322 *
7323 * RETURNS:
7324 * 0 on success, -errno otherwise.
7325 */
7326int 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{
Tejun Heocbcdd872007-08-18 13:14:55 +09007330 int i, rc;
Tejun Heof5cda252007-04-17 23:44:07 +09007331
7332 rc = ata_host_start(host);
7333 if (rc)
7334 return rc;
7335
Paul Mundt3d46b2e2007-11-08 11:14:56 +09007336 /* Special case for polling mode */
7337 if (!irq) {
7338 WARN_ON(irq_handler);
7339 return ata_host_register(host, sht);
7340 }
7341
Tejun Heof5cda252007-04-17 23:44:07 +09007342 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
Tejun Heocbcdd872007-08-18 13:14:55 +09007347 for (i = 0; i < host->n_ports; i++)
7348 ata_port_desc(host->ports[i], "irq %d", irq);
Tejun Heo40318262007-07-03 01:38:47 +09007349
Tejun Heof5cda252007-04-17 23:44:07 +09007350 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/**
Tejun Heo720ba122006-05-31 18:28:13 +09007359 * 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 */
Adrian Bunk741b7762007-10-24 18:23:06 +02007369static void ata_port_detach(struct ata_port *ap)
Tejun Heo720ba122006-05-31 18:28:13 +09007370{
7371 unsigned long flags;
Tejun Heo41bda9c2007-08-06 18:36:24 +09007372 struct ata_link *link;
Tejun Heof58229f2007-08-06 18:36:23 +09007373 struct ata_device *dev;
Tejun Heo720ba122006-05-31 18:28:13 +09007374
7375 if (!ap->ops->error_handler)
Tejun Heoc3cf30a2006-08-05 03:59:11 +09007376 goto skip_eh;
Tejun Heo720ba122006-05-31 18:28:13 +09007377
7378 /* tell EH we're leaving & flush EH */
Jeff Garzikba6a1302006-06-22 23:46:10 -04007379 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09007380 ap->pflags |= ATA_PFLAG_UNLOADING;
Jeff Garzikba6a1302006-06-22 23:46:10 -04007381 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09007382
7383 ata_port_wait_eh(ap);
7384
Tejun Heo7f9ad9b2007-12-15 15:05:00 +09007385 /* EH is now guaranteed to see UNLOADING - EH context belongs
7386 * to us. Disable all existing devices.
Tejun Heo720ba122006-05-31 18:28:13 +09007387 */
Tejun Heo41bda9c2007-08-06 18:36:24 +09007388 ata_port_for_each_link(link, ap) {
7389 ata_link_for_each_dev(dev, link)
7390 ata_dev_disable(dev);
7391 }
Tejun Heo720ba122006-05-31 18:28:13 +09007392
Tejun Heo720ba122006-05-31 18:28:13 +09007393 /* Final freeze & EH. All in-flight commands are aborted. EH
7394 * will be skipped and retrials will be terminated with bad
7395 * target.
7396 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04007397 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09007398 ata_port_freeze(ap); /* won't be thawed */
Jeff Garzikba6a1302006-06-22 23:46:10 -04007399 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09007400
7401 ata_port_wait_eh(ap);
Oleg Nesterov45a66c12007-07-09 11:46:13 -07007402 cancel_rearming_delayed_work(&ap->hotplug_task);
Tejun Heo720ba122006-05-31 18:28:13 +09007403
Tejun Heoc3cf30a2006-08-05 03:59:11 +09007404 skip_eh:
Tejun Heo720ba122006-05-31 18:28:13 +09007405 /* remove the associated SCSI host */
Jeff Garzikcca39742006-08-24 03:19:22 -04007406 scsi_remove_host(ap->scsi_host);
Tejun Heo720ba122006-05-31 18:28:13 +09007407}
7408
7409/**
Tejun Heo0529c152007-01-20 16:00:26 +09007410 * 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 */
7418void 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]);
Tejun Heo562f0c22007-12-15 15:05:01 +09007424
7425 /* the host is dead now, dissociate ACPI */
7426 ata_acpi_dissociate(host);
Tejun Heo0529c152007-01-20 16:00:26 +09007427}
7428
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429/**
7430 * ata_std_ports - initialize ioaddr with standard port offsets.
7431 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04007432 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439 */
Edward Falk0baab862005-06-02 18:17:13 -04007440
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441void 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
Edward Falk0baab862005-06-02 18:17:13 -04007455
Jeff Garzik374b1872005-08-30 05:42:52 -04007456#ifdef CONFIG_PCI
7457
Edward Falk0baab862005-06-02 18:17:13 -04007458/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 * ata_pci_remove_one - PCI layer callback for device removal
7460 * @pdev: PCI device that was removed
7461 *
Tejun Heob878ca52007-01-20 16:00:28 +09007462 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465 *
7466 * LOCKING:
7467 * Inherited from PCI layer (may sleep).
7468 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09007469void ata_pci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007470{
Jeff Garzik28555682007-10-11 17:12:35 -04007471 struct device *dev = &pdev->dev;
Jeff Garzikcca39742006-08-24 03:19:22 -04007472 struct ata_host *host = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473
Tejun Heob878ca52007-01-20 16:00:28 +09007474 ata_host_detach(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475}
7476
7477/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04007478int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479{
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}
Jens Axboe9b847542006-01-06 09:28:07 +01007510
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007511#ifdef CONFIG_PM
Tejun Heo3c5100c2006-07-26 16:58:33 +09007512void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
Jens Axboe9b847542006-01-06 09:28:07 +01007513{
7514 pci_save_state(pdev);
Tejun Heo4c90d972007-02-20 18:14:48 +09007515 pci_disable_device(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09007516
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01007517 if (mesg.event & PM_EVENT_SLEEP)
Tejun Heo500530f2006-07-03 16:07:27 +09007518 pci_set_power_state(pdev, PCI_D3hot);
Jens Axboe9b847542006-01-06 09:28:07 +01007519}
7520
Tejun Heo553c4aa2006-12-26 19:39:50 +09007521int ata_pci_device_do_resume(struct pci_dev *pdev)
Jens Axboe9b847542006-01-06 09:28:07 +01007522{
Tejun Heo553c4aa2006-12-26 19:39:50 +09007523 int rc;
7524
Jens Axboe9b847542006-01-06 09:28:07 +01007525 pci_set_power_state(pdev, PCI_D0);
7526 pci_restore_state(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09007527
Tejun Heob878ca52007-01-20 16:00:28 +09007528 rc = pcim_enable_device(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09007529 if (rc) {
7530 dev_printk(KERN_ERR, &pdev->dev,
7531 "failed to enable device after resume (%d)\n", rc);
7532 return rc;
7533 }
7534
Jens Axboe9b847542006-01-06 09:28:07 +01007535 pci_set_master(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09007536 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09007537}
7538
Tejun Heo3c5100c2006-07-26 16:58:33 +09007539int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09007540{
Jeff Garzikcca39742006-08-24 03:19:22 -04007541 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo500530f2006-07-03 16:07:27 +09007542 int rc = 0;
7543
Jeff Garzikcca39742006-08-24 03:19:22 -04007544 rc = ata_host_suspend(host, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09007545 if (rc)
7546 return rc;
7547
Tejun Heo3c5100c2006-07-26 16:58:33 +09007548 ata_pci_device_do_suspend(pdev, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09007549
7550 return 0;
7551}
7552
7553int ata_pci_device_resume(struct pci_dev *pdev)
7554{
Jeff Garzikcca39742006-08-24 03:19:22 -04007555 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09007556 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09007557
Tejun Heo553c4aa2006-12-26 19:39:50 +09007558 rc = ata_pci_device_do_resume(pdev);
7559 if (rc == 0)
7560 ata_host_resume(host);
7561 return rc;
Jens Axboe9b847542006-01-06 09:28:07 +01007562}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007563#endif /* CONFIG_PM */
7564
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565#endif /* CONFIG_PCI */
7566
Tejun Heo33267322008-02-13 09:15:09 +09007567static 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
7697static 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007741
Linus Torvalds1da177e2005-04-16 15:20:36 -07007742static int __init ata_init(void)
7743{
Andrew Mortona8601e52006-06-25 01:36:52 -07007744 ata_probe_timeout *= HZ;
Tejun Heo33267322008-02-13 09:15:09 +09007745
7746 ata_parse_force_param();
7747
Linus Torvalds1da177e2005-04-16 15:20:36 -07007748 ata_wq = create_workqueue("ata");
7749 if (!ata_wq)
7750 return -ENOMEM;
7751
Tejun Heo453b07a2006-05-31 18:27:42 +09007752 ata_aux_wq = create_singlethread_workqueue("ata_aux");
7753 if (!ata_aux_wq) {
7754 destroy_workqueue(ata_wq);
7755 return -ENOMEM;
7756 }
7757
Linus Torvalds1da177e2005-04-16 15:20:36 -07007758 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
7759 return 0;
7760}
7761
7762static void __exit ata_exit(void)
7763{
Tejun Heo33267322008-02-13 09:15:09 +09007764 kfree(ata_force_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007765 destroy_workqueue(ata_wq);
Tejun Heo453b07a2006-05-31 18:27:42 +09007766 destroy_workqueue(ata_aux_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007767}
7768
Brian Kinga4625082006-11-13 16:32:36 -06007769subsys_initcall(ata_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007770module_exit(ata_exit);
7771
Jeff Garzik67846b32005-10-05 02:58:32 -04007772static unsigned long ratelimit_time;
Ingo Molnar34af9462006-06-27 02:53:55 -07007773static DEFINE_SPINLOCK(ata_ratelimit_lock);
Jeff Garzik67846b32005-10-05 02:58:32 -04007774
7775int 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
Tejun Heoc22daff2006-04-11 22:22:29 +09007793/**
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 */
7816u32 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07007839/*
Tejun Heodd5b06c2006-08-10 16:59:12 +09007840 * Dummy port_ops
7841 */
7842static void ata_dummy_noret(struct ata_port *ap) { }
7843static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
7844static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
7845
7846static u8 ata_dummy_check_status(struct ata_port *ap)
7847{
7848 return ATA_DRDY;
7849}
7850
7851static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
7852{
7853 return AC_ERR_SYSTEM;
7854}
7855
Tejun Heo029cfd62008-03-25 12:22:49 +09007856struct ata_port_operations ata_dummy_port_ops = {
Tejun Heodd5b06c2006-08-10 16:59:12 +09007857 .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
Tejun Heo21b0ad42007-04-17 23:44:07 +09007871const struct ata_port_info ata_dummy_port_info = {
7872 .port_ops = &ata_dummy_port_ops,
7873};
7874
Tejun Heodd5b06c2006-08-10 16:59:12 +09007875/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007876 * 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 */
Tejun Heoe9c83912006-07-03 16:07:26 +09007881EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
7882EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
7883EXPORT_SYMBOL_GPL(sata_deb_timing_long);
Tejun Heo029cfd62008-03-25 12:22:49 +09007884EXPORT_SYMBOL_GPL(ata_base_port_ops);
7885EXPORT_SYMBOL_GPL(sata_port_ops);
7886EXPORT_SYMBOL_GPL(sata_pmp_port_ops);
7887EXPORT_SYMBOL_GPL(ata_sff_port_ops);
7888EXPORT_SYMBOL_GPL(ata_bmdma_port_ops);
Tejun Heodd5b06c2006-08-10 16:59:12 +09007889EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
Tejun Heo21b0ad42007-04-17 23:44:07 +09007890EXPORT_SYMBOL_GPL(ata_dummy_port_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891EXPORT_SYMBOL_GPL(ata_std_bios_param);
7892EXPORT_SYMBOL_GPL(ata_std_ports);
Jeff Garzikcca39742006-08-24 03:19:22 -04007893EXPORT_SYMBOL_GPL(ata_host_init);
Tejun Heof3187192007-04-17 23:44:07 +09007894EXPORT_SYMBOL_GPL(ata_host_alloc);
Tejun Heof5cda252007-04-17 23:44:07 +09007895EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
Tejun Heoecef7252007-04-17 23:44:06 +09007896EXPORT_SYMBOL_GPL(ata_host_start);
Tejun Heof3187192007-04-17 23:44:07 +09007897EXPORT_SYMBOL_GPL(ata_host_register);
Tejun Heof5cda252007-04-17 23:44:07 +09007898EXPORT_SYMBOL_GPL(ata_host_activate);
Tejun Heo0529c152007-01-20 16:00:26 +09007899EXPORT_SYMBOL_GPL(ata_host_detach);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007900EXPORT_SYMBOL_GPL(ata_sg_init);
Tejun Heo9a1004d2006-05-31 18:27:52 +09007901EXPORT_SYMBOL_GPL(ata_hsm_move);
Tejun Heof686bcb2006-05-15 20:58:05 +09007902EXPORT_SYMBOL_GPL(ata_qc_complete);
Tejun Heodedaf2b2006-05-15 21:03:43 +09007903EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007904EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007905EXPORT_SYMBOL_GPL(ata_tf_load);
7906EXPORT_SYMBOL_GPL(ata_tf_read);
7907EXPORT_SYMBOL_GPL(ata_noop_dev_select);
7908EXPORT_SYMBOL_GPL(ata_std_dev_select);
Jeff Garzik43727fb2007-02-25 16:50:52 -05007909EXPORT_SYMBOL_GPL(sata_print_link_status);
Tejun Heo436d34b2008-04-02 17:28:46 +09007910EXPORT_SYMBOL_GPL(atapi_cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007911EXPORT_SYMBOL_GPL(ata_tf_to_fis);
7912EXPORT_SYMBOL_GPL(ata_tf_from_fis);
Tejun Heo63573572007-11-27 19:43:39 +09007913EXPORT_SYMBOL_GPL(ata_pack_xfermask);
7914EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
7915EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
7916EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
7917EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
7918EXPORT_SYMBOL_GPL(ata_mode_string);
7919EXPORT_SYMBOL_GPL(ata_id_xfermask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007920EXPORT_SYMBOL_GPL(ata_check_status);
7921EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007922EXPORT_SYMBOL_GPL(ata_exec_command);
7923EXPORT_SYMBOL_GPL(ata_port_start);
Alan Coxd92e74d2007-06-07 16:19:15 +01007924EXPORT_SYMBOL_GPL(ata_sff_port_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925EXPORT_SYMBOL_GPL(ata_interrupt);
Alan04351822007-03-06 02:37:52 -08007926EXPORT_SYMBOL_GPL(ata_do_set_mode);
Tejun Heo0d5ff562007-02-01 15:06:36 +09007927EXPORT_SYMBOL_GPL(ata_data_xfer);
7928EXPORT_SYMBOL_GPL(ata_data_xfer_noirq);
Tejun Heo31cc23b2007-09-23 13:14:12 +09007929EXPORT_SYMBOL_GPL(ata_std_qc_defer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930EXPORT_SYMBOL_GPL(ata_qc_prep);
Alan Coxd26fc952007-07-06 19:13:52 -04007931EXPORT_SYMBOL_GPL(ata_dumb_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06007932EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007933EXPORT_SYMBOL_GPL(ata_bmdma_setup);
7934EXPORT_SYMBOL_GPL(ata_bmdma_start);
7935EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
Tejun Heo358f9a72008-03-25 12:22:47 +09007936EXPORT_SYMBOL_GPL(ata_noop_irq_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007937EXPORT_SYMBOL_GPL(ata_bmdma_status);
7938EXPORT_SYMBOL_GPL(ata_bmdma_stop);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09007939EXPORT_SYMBOL_GPL(ata_bmdma_freeze);
7940EXPORT_SYMBOL_GPL(ata_bmdma_thaw);
7941EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh);
7942EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
7943EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007944EXPORT_SYMBOL_GPL(ata_port_probe);
Alan10305f02007-02-20 18:01:59 +00007945EXPORT_SYMBOL_GPL(ata_dev_disable);
Tejun Heo3c567b72006-05-15 20:57:23 +09007946EXPORT_SYMBOL_GPL(sata_set_spd);
Tejun Heo936fd732007-08-06 18:36:23 +09007947EXPORT_SYMBOL_GPL(sata_link_debounce);
7948EXPORT_SYMBOL_GPL(sata_link_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007949EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heof5914a42006-05-31 18:27:48 +09007950EXPORT_SYMBOL_GPL(ata_std_prereset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09007951EXPORT_SYMBOL_GPL(ata_std_softreset);
Tejun Heocc0680a2007-08-06 18:36:23 +09007952EXPORT_SYMBOL_GPL(sata_link_hardreset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09007953EXPORT_SYMBOL_GPL(sata_std_hardreset);
7954EXPORT_SYMBOL_GPL(ata_std_postreset);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05007955EXPORT_SYMBOL_GPL(ata_dev_classify);
7956EXPORT_SYMBOL_GPL(ata_dev_pair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007957EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04007958EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heoc22daff2006-04-11 22:22:29 +09007959EXPORT_SYMBOL_GPL(ata_wait_register);
Tejun Heo6f8b9952006-01-24 17:05:21 +09007960EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heo88ff6ea2007-10-16 14:21:24 -07007961EXPORT_SYMBOL_GPL(ata_wait_after_reset);
Tejun Heod4b2bab2007-02-02 16:50:52 +09007962EXPORT_SYMBOL_GPL(ata_wait_ready);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007963EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
7964EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007965EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
Tejun Heo83c47bc2006-05-31 18:28:07 +09007966EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09007967EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007968EXPORT_SYMBOL_GPL(ata_host_intr);
Tejun Heo34bf2172006-05-15 20:57:46 +09007969EXPORT_SYMBOL_GPL(sata_scr_valid);
7970EXPORT_SYMBOL_GPL(sata_scr_read);
7971EXPORT_SYMBOL_GPL(sata_scr_write);
7972EXPORT_SYMBOL_GPL(sata_scr_write_flush);
Tejun Heo936fd732007-08-06 18:36:23 +09007973EXPORT_SYMBOL_GPL(ata_link_online);
7974EXPORT_SYMBOL_GPL(ata_link_offline);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007975#ifdef CONFIG_PM
Jeff Garzikcca39742006-08-24 03:19:22 -04007976EXPORT_SYMBOL_GPL(ata_host_suspend);
7977EXPORT_SYMBOL_GPL(ata_host_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007978#endif /* CONFIG_PM */
Tejun Heo6a62a042006-02-13 10:02:46 +09007979EXPORT_SYMBOL_GPL(ata_id_string);
7980EXPORT_SYMBOL_GPL(ata_id_c_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007981EXPORT_SYMBOL_GPL(ata_scsi_simulate);
7982
Alan Cox1bc4ccf2006-01-09 17:18:14 +00007983EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Tejun Heo63573572007-11-27 19:43:39 +09007984EXPORT_SYMBOL_GPL(ata_timing_find_mode);
Alan Cox452503f2005-10-21 19:01:32 -04007985EXPORT_SYMBOL_GPL(ata_timing_compute);
7986EXPORT_SYMBOL_GPL(ata_timing_merge);
Tejun Heoa0f79b92007-12-18 16:33:05 +09007987EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
Alan Cox452503f2005-10-21 19:01:32 -04007988
Linus Torvalds1da177e2005-04-16 15:20:36 -07007989#ifdef CONFIG_PCI
7990EXPORT_SYMBOL_GPL(pci_test_config_bits);
Tejun Heod583bc12007-07-04 18:02:07 +09007991EXPORT_SYMBOL_GPL(ata_pci_init_sff_host);
Tejun Heo1626aeb2007-05-04 12:43:58 +02007992EXPORT_SYMBOL_GPL(ata_pci_init_bmdma);
Tejun Heod583bc12007-07-04 18:02:07 +09007993EXPORT_SYMBOL_GPL(ata_pci_prepare_sff_host);
Tejun Heo4e6b79f2008-01-18 18:36:28 +09007994EXPORT_SYMBOL_GPL(ata_pci_activate_sff_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007995EXPORT_SYMBOL_GPL(ata_pci_init_one);
7996EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007997#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09007998EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
7999EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
Jens Axboe9b847542006-01-06 09:28:07 +01008000EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
8001EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09008002#endif /* CONFIG_PM */
Alan Cox67951ad2006-03-22 15:55:54 +00008003EXPORT_SYMBOL_GPL(ata_pci_default_filter);
8004EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008005#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01008006
Tejun Heo31f88382007-09-23 13:19:54 +09008007EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch);
Tejun Heo3af9a772007-09-23 13:19:54 +09008008EXPORT_SYMBOL_GPL(sata_pmp_std_prereset);
8009EXPORT_SYMBOL_GPL(sata_pmp_std_hardreset);
8010EXPORT_SYMBOL_GPL(sata_pmp_std_postreset);
8011EXPORT_SYMBOL_GPL(sata_pmp_do_eh);
8012
Tejun Heob64bbc32007-07-16 14:29:39 +09008013EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
8014EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
8015EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
Tejun Heocbcdd872007-08-18 13:14:55 +09008016EXPORT_SYMBOL_GPL(ata_port_desc);
8017#ifdef CONFIG_PCI
8018EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
8019#endif /* CONFIG_PCI */
Tejun Heo7b70fc02006-05-15 20:58:07 +09008020EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
Tejun Heodbd82612007-08-06 18:36:23 +09008021EXPORT_SYMBOL_GPL(ata_link_abort);
Tejun Heo7b70fc02006-05-15 20:58:07 +09008022EXPORT_SYMBOL_GPL(ata_port_abort);
Tejun Heoe3180492006-05-15 20:58:09 +09008023EXPORT_SYMBOL_GPL(ata_port_freeze);
Tejun Heo7d77b242007-09-23 13:14:13 +09008024EXPORT_SYMBOL_GPL(sata_async_notification);
Tejun Heoe3180492006-05-15 20:58:09 +09008025EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
8026EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
Tejun Heoece1d632006-04-02 18:51:53 +09008027EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
8028EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Tejun Heo022bdb02006-05-15 20:58:22 +09008029EXPORT_SYMBOL_GPL(ata_do_eh);
Akira Iguchi83625002007-01-26 16:27:32 +09008030EXPORT_SYMBOL_GPL(ata_irq_on);
Akira Iguchia619f981b2007-01-26 16:28:18 +09008031EXPORT_SYMBOL_GPL(ata_dev_try_classify);
Alan Coxbe0d18d2007-03-06 02:37:56 -08008032
8033EXPORT_SYMBOL_GPL(ata_cable_40wire);
8034EXPORT_SYMBOL_GPL(ata_cable_80wire);
8035EXPORT_SYMBOL_GPL(ata_cable_unknown);
Tejun Heoc88f90c2007-11-27 19:43:48 +09008036EXPORT_SYMBOL_GPL(ata_cable_ignore);
Alan Coxbe0d18d2007-03-06 02:37:56 -08008037EXPORT_SYMBOL_GPL(ata_cable_sata);