blob: 9121cc443be87cfc33e9323e5b18f376303d9fbb [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/spinlock.h>
50#include <linux/blkdev.h>
51#include <linux/delay.h>
52#include <linux/timer.h>
53#include <linux/interrupt.h>
54#include <linux/completion.h>
55#include <linux/suspend.h>
56#include <linux/workqueue.h>
Jeff Garzik67846b32005-10-05 02:58:32 -040057#include <linux/jiffies.h>
David Hardeman378f0582005-09-17 17:55:31 +100058#include <linux/scatterlist.h>
Jeff Garzik2dcb4072007-10-19 06:42:56 -040059#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <scsi/scsi.h>
Jeff Garzik193515d2005-11-07 00:59:37 -050061#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <scsi/scsi_host.h>
63#include <linux/libata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <asm/semaphore.h>
65#include <asm/byteorder.h>
Tejun Heo140b5e592007-12-12 12:21:52 +090066#include <linux/cdrom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#include "libata.h"
69
Jeff Garzikfda0efc2007-01-31 07:43:15 -050070
Tejun Heod7bb4cc2006-05-31 18:27:46 +090071/* debounce timing parameters in msecs { interval, duration, timeout } */
Tejun Heoe9c83912006-07-03 16:07:26 +090072const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
73const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
74const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
Tejun Heod7bb4cc2006-05-31 18:27:46 +090075
Tejun Heo029cfd62008-03-25 12:22:49 +090076const struct ata_port_operations ata_base_port_ops = {
77 .irq_clear = ata_noop_irq_clear,
Tejun Heoa1efdab2008-03-25 12:22:50 +090078 .prereset = ata_std_prereset,
79 .hardreset = sata_std_hardreset,
80 .postreset = ata_std_postreset,
81 .error_handler = ata_std_error_handler,
Tejun Heo029cfd62008-03-25 12:22:49 +090082};
83
84const struct ata_port_operations sata_port_ops = {
85 .inherits = &ata_base_port_ops,
86
87 .qc_defer = ata_std_qc_defer,
88 .dev_select = ata_noop_dev_select,
89};
90
91const struct ata_port_operations sata_pmp_port_ops = {
92 .inherits = &sata_port_ops,
Tejun Heoa1efdab2008-03-25 12:22:50 +090093
94 .pmp_prereset = sata_pmp_std_prereset,
95 .pmp_hardreset = sata_pmp_std_hardreset,
96 .pmp_postreset = sata_pmp_std_postreset,
97 .error_handler = sata_pmp_error_handler,
Tejun Heo029cfd62008-03-25 12:22:49 +090098};
99
Tejun Heo3373efd2006-05-15 20:57:53 +0900100static unsigned int ata_dev_init_params(struct ata_device *dev,
101 u16 heads, u16 sectors);
102static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
Jeff Garzik218f3d32007-10-25 00:33:27 -0400103static unsigned int ata_dev_set_feature(struct ata_device *dev,
104 u8 enable, u8 feature);
Tejun Heo3373efd2006-05-15 20:57:53 +0900105static void ata_dev_xfermask(struct ata_device *dev);
Tejun Heo75683fe2007-07-05 13:31:27 +0900106static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Tejun Heof3187192007-04-17 23:44:07 +0900108unsigned int ata_print_id = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static struct workqueue_struct *ata_wq;
110
Tejun Heo453b07a2006-05-31 18:27:42 +0900111struct workqueue_struct *ata_aux_wq;
112
Tejun Heo33267322008-02-13 09:15:09 +0900113struct ata_force_param {
114 const char *name;
115 unsigned int cbl;
116 int spd_limit;
117 unsigned long xfer_mask;
118 unsigned int horkage_on;
119 unsigned int horkage_off;
120};
121
122struct ata_force_ent {
123 int port;
124 int device;
125 struct ata_force_param param;
126};
127
128static struct ata_force_ent *ata_force_tbl;
129static int ata_force_tbl_size;
130
131static char ata_force_param_buf[PAGE_SIZE] __initdata;
Tejun Heo7afb4222008-03-09 20:21:53 +0900132/* param_buf is thrown away after initialization, disallow read */
133module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
Tejun Heo33267322008-02-13 09:15:09 +0900134MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/kernel-parameters.txt for details)");
135
Jeff Garzik418dc1f2006-03-11 20:50:08 -0500136int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -0400137module_param(atapi_enabled, int, 0444);
138MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
139
Adrian Bunkc5c61bd2008-02-25 02:07:25 +0200140static int atapi_dmadir = 0;
Albert Lee95de7192006-04-04 10:57:18 +0800141module_param(atapi_dmadir, int, 0444);
142MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
143
Mark Lordbaf4fdf2007-08-08 01:08:45 +0900144int atapi_passthru16 = 1;
145module_param(atapi_passthru16, int, 0444);
146MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices; on by default (0=off, 1=on)");
147
Jeff Garzikc3c013a2006-02-27 22:31:19 -0500148int libata_fua = 0;
149module_param_named(fua, libata_fua, int, 0444);
150MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
151
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400152static int ata_ignore_hpa;
Alan Cox1e999732007-04-11 00:23:13 +0100153module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
154MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
155
Alan Coxb3a70602007-10-02 12:38:26 -0400156static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
157module_param_named(dma, libata_dma_mask, int, 0444);
158MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
159
Andrew Mortona8601e52006-06-25 01:36:52 -0700160static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
161module_param(ata_probe_timeout, int, 0444);
162MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
163
Jeff Garzik6ebe9d82007-10-05 16:28:36 -0400164int libata_noacpi = 0;
Jeff Garzikd7d0dad2007-03-28 01:57:37 -0400165module_param_named(noacpi, libata_noacpi, int, 0444);
Jeff Garzik6ebe9d82007-10-05 16:28:36 -0400166MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in probe/suspend/resume when set");
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700167
Alan Coxae8d4ee2007-11-04 22:05:49 -0500168int libata_allow_tpm = 0;
169module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
170MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands");
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172MODULE_AUTHOR("Jeff Garzik");
173MODULE_DESCRIPTION("Library module for ATA devices");
174MODULE_LICENSE("GPL");
175MODULE_VERSION(DRV_VERSION);
176
Edward Falk0baab862005-06-02 18:17:13 -0400177
178/**
Tejun Heo33267322008-02-13 09:15:09 +0900179 * ata_force_cbl - force cable type according to libata.force
Randy Dunlap4cdfa1b2008-02-22 12:21:37 -0800180 * @ap: ATA port of interest
Tejun Heo33267322008-02-13 09:15:09 +0900181 *
182 * Force cable type according to libata.force and whine about it.
183 * The last entry which has matching port number is used, so it
184 * can be specified as part of device force parameters. For
185 * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
186 * same effect.
187 *
188 * LOCKING:
189 * EH context.
190 */
191void ata_force_cbl(struct ata_port *ap)
192{
193 int i;
194
195 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
196 const struct ata_force_ent *fe = &ata_force_tbl[i];
197
198 if (fe->port != -1 && fe->port != ap->print_id)
199 continue;
200
201 if (fe->param.cbl == ATA_CBL_NONE)
202 continue;
203
204 ap->cbl = fe->param.cbl;
205 ata_port_printk(ap, KERN_NOTICE,
206 "FORCE: cable set to %s\n", fe->param.name);
207 return;
208 }
209}
210
211/**
212 * ata_force_spd_limit - force SATA spd limit according to libata.force
213 * @link: ATA link of interest
214 *
215 * Force SATA spd limit according to libata.force and whine about
216 * it. When only the port part is specified (e.g. 1:), the limit
217 * applies to all links connected to both the host link and all
218 * fan-out ports connected via PMP. If the device part is
219 * specified as 0 (e.g. 1.00:), it specifies the first fan-out
220 * link not the host link. Device number 15 always points to the
221 * host link whether PMP is attached or not.
222 *
223 * LOCKING:
224 * EH context.
225 */
226static void ata_force_spd_limit(struct ata_link *link)
227{
228 int linkno, i;
229
230 if (ata_is_host_link(link))
231 linkno = 15;
232 else
233 linkno = link->pmp;
234
235 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
236 const struct ata_force_ent *fe = &ata_force_tbl[i];
237
238 if (fe->port != -1 && fe->port != link->ap->print_id)
239 continue;
240
241 if (fe->device != -1 && fe->device != linkno)
242 continue;
243
244 if (!fe->param.spd_limit)
245 continue;
246
247 link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
248 ata_link_printk(link, KERN_NOTICE,
249 "FORCE: PHY spd limit set to %s\n", fe->param.name);
250 return;
251 }
252}
253
254/**
255 * ata_force_xfermask - force xfermask according to libata.force
256 * @dev: ATA device of interest
257 *
258 * Force xfer_mask according to libata.force and whine about it.
259 * For consistency with link selection, device number 15 selects
260 * the first device connected to the host link.
261 *
262 * LOCKING:
263 * EH context.
264 */
265static void ata_force_xfermask(struct ata_device *dev)
266{
267 int devno = dev->link->pmp + dev->devno;
268 int alt_devno = devno;
269 int i;
270
271 /* allow n.15 for the first device attached to host port */
272 if (ata_is_host_link(dev->link) && devno == 0)
273 alt_devno = 15;
274
275 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
276 const struct ata_force_ent *fe = &ata_force_tbl[i];
277 unsigned long pio_mask, mwdma_mask, udma_mask;
278
279 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
280 continue;
281
282 if (fe->device != -1 && fe->device != devno &&
283 fe->device != alt_devno)
284 continue;
285
286 if (!fe->param.xfer_mask)
287 continue;
288
289 ata_unpack_xfermask(fe->param.xfer_mask,
290 &pio_mask, &mwdma_mask, &udma_mask);
291 if (udma_mask)
292 dev->udma_mask = udma_mask;
293 else if (mwdma_mask) {
294 dev->udma_mask = 0;
295 dev->mwdma_mask = mwdma_mask;
296 } else {
297 dev->udma_mask = 0;
298 dev->mwdma_mask = 0;
299 dev->pio_mask = pio_mask;
300 }
301
302 ata_dev_printk(dev, KERN_NOTICE,
303 "FORCE: xfer_mask set to %s\n", fe->param.name);
304 return;
305 }
306}
307
308/**
309 * ata_force_horkage - force horkage according to libata.force
310 * @dev: ATA device of interest
311 *
312 * Force horkage according to libata.force and whine about it.
313 * For consistency with link selection, device number 15 selects
314 * the first device connected to the host link.
315 *
316 * LOCKING:
317 * EH context.
318 */
319static void ata_force_horkage(struct ata_device *dev)
320{
321 int devno = dev->link->pmp + dev->devno;
322 int alt_devno = devno;
323 int i;
324
325 /* allow n.15 for the first device attached to host port */
326 if (ata_is_host_link(dev->link) && devno == 0)
327 alt_devno = 15;
328
329 for (i = 0; i < ata_force_tbl_size; i++) {
330 const struct ata_force_ent *fe = &ata_force_tbl[i];
331
332 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
333 continue;
334
335 if (fe->device != -1 && fe->device != devno &&
336 fe->device != alt_devno)
337 continue;
338
339 if (!(~dev->horkage & fe->param.horkage_on) &&
340 !(dev->horkage & fe->param.horkage_off))
341 continue;
342
343 dev->horkage |= fe->param.horkage_on;
344 dev->horkage &= ~fe->param.horkage_off;
345
346 ata_dev_printk(dev, KERN_NOTICE,
347 "FORCE: horkage modified (%s)\n", fe->param.name);
348 }
349}
350
351/**
Tejun Heo436d34b2008-04-02 17:28:46 +0900352 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
353 * @opcode: SCSI opcode
354 *
355 * Determine ATAPI command type from @opcode.
356 *
357 * LOCKING:
358 * None.
359 *
360 * RETURNS:
361 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
362 */
363int atapi_cmd_type(u8 opcode)
364{
365 switch (opcode) {
366 case GPCMD_READ_10:
367 case GPCMD_READ_12:
368 return ATAPI_READ;
369
370 case GPCMD_WRITE_10:
371 case GPCMD_WRITE_12:
372 case GPCMD_WRITE_AND_VERIFY_10:
373 return ATAPI_WRITE;
374
375 case GPCMD_READ_CD:
376 case GPCMD_READ_CD_MSF:
377 return ATAPI_READ_CD;
378
Tejun Heoe52dcc42008-04-02 17:35:19 +0900379 case ATA_16:
380 case ATA_12:
381 if (atapi_passthru16)
382 return ATAPI_PASS_THRU;
383 /* fall thru */
Tejun Heo436d34b2008-04-02 17:28:46 +0900384 default:
385 return ATAPI_MISC;
386 }
387}
388
389/**
Tejun Heo624d5c52008-03-25 22:16:41 +0900390 * ata_noop_irq_clear - Noop placeholder for irq_clear
391 * @ap: Port associated with this ATA transaction.
392 */
393void ata_noop_irq_clear(struct ata_port *ap)
394{
395}
396
397/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
399 * @tf: Taskfile to convert
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 * @pmp: Port multiplier port
Tejun Heo99771262007-07-16 14:29:38 +0900401 * @is_cmd: This FIS is for command
402 * @fis: Buffer into which data will output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 *
404 * Converts a standard ATA taskfile to a Serial ATA
405 * FIS structure (Register - Host to Device).
406 *
407 * LOCKING:
408 * Inherited from caller.
409 */
Tejun Heo99771262007-07-16 14:29:38 +0900410void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Tejun Heo99771262007-07-16 14:29:38 +0900412 fis[0] = 0x27; /* Register - Host to Device FIS */
413 fis[1] = pmp & 0xf; /* Port multiplier number*/
414 if (is_cmd)
415 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 fis[2] = tf->command;
418 fis[3] = tf->feature;
419
420 fis[4] = tf->lbal;
421 fis[5] = tf->lbam;
422 fis[6] = tf->lbah;
423 fis[7] = tf->device;
424
425 fis[8] = tf->hob_lbal;
426 fis[9] = tf->hob_lbam;
427 fis[10] = tf->hob_lbah;
428 fis[11] = tf->hob_feature;
429
430 fis[12] = tf->nsect;
431 fis[13] = tf->hob_nsect;
432 fis[14] = 0;
433 fis[15] = tf->ctl;
434
435 fis[16] = 0;
436 fis[17] = 0;
437 fis[18] = 0;
438 fis[19] = 0;
439}
440
441/**
442 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
443 * @fis: Buffer from which data will be input
444 * @tf: Taskfile to output
445 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500446 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 *
448 * LOCKING:
449 * Inherited from caller.
450 */
451
Jeff Garzik057ace52005-10-22 14:27:05 -0400452void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 tf->command = fis[2]; /* status */
455 tf->feature = fis[3]; /* error */
456
457 tf->lbal = fis[4];
458 tf->lbam = fis[5];
459 tf->lbah = fis[6];
460 tf->device = fis[7];
461
462 tf->hob_lbal = fis[8];
463 tf->hob_lbam = fis[9];
464 tf->hob_lbah = fis[10];
465
466 tf->nsect = fis[12];
467 tf->hob_nsect = fis[13];
468}
469
Albert Lee8cbd6df2005-10-12 15:06:27 +0800470static const u8 ata_rw_cmds[] = {
471 /* pio multi */
472 ATA_CMD_READ_MULTI,
473 ATA_CMD_WRITE_MULTI,
474 ATA_CMD_READ_MULTI_EXT,
475 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100476 0,
477 0,
478 0,
479 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800480 /* pio */
481 ATA_CMD_PIO_READ,
482 ATA_CMD_PIO_WRITE,
483 ATA_CMD_PIO_READ_EXT,
484 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100485 0,
486 0,
487 0,
488 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800489 /* dma */
490 ATA_CMD_READ,
491 ATA_CMD_WRITE,
492 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100493 ATA_CMD_WRITE_EXT,
494 0,
495 0,
496 0,
497 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800498};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800501 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
Tejun Heobd056d72006-11-14 22:47:10 +0900502 * @tf: command to examine and configure
503 * @dev: device tf belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500505 * Examine the device configuration and tf->flags to calculate
Albert Lee8cbd6df2005-10-12 15:06:27 +0800506 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 *
508 * LOCKING:
509 * caller.
510 */
Tejun Heobd056d72006-11-14 22:47:10 +0900511static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100513 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100515 int index, fua, lba48, write;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500516
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100517 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800518 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
519 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Albert Lee8cbd6df2005-10-12 15:06:27 +0800521 if (dev->flags & ATA_DFLAG_PIO) {
522 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100523 index = dev->multi_count ? 0 : 8;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900524 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
Alan Cox8d238e02006-01-17 20:50:31 +0000525 /* Unable to use DMA due to host limitation */
526 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800527 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800528 } else {
529 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100530 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100533 cmd = ata_rw_cmds[index + fua + lba48 + write];
534 if (cmd) {
535 tf->command = cmd;
536 return 0;
537 }
538 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Tejun Heocb95d562006-03-06 04:31:56 +0900541/**
Tejun Heo35b649f2006-11-14 22:37:35 +0900542 * ata_tf_read_block - Read block address from ATA taskfile
543 * @tf: ATA taskfile of interest
544 * @dev: ATA device @tf belongs to
545 *
546 * LOCKING:
547 * None.
548 *
549 * Read block address from @tf. This function can handle all
550 * three address formats - LBA, LBA48 and CHS. tf->protocol and
551 * flags select the address format to use.
552 *
553 * RETURNS:
554 * Block address read from @tf.
555 */
556u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
557{
558 u64 block = 0;
559
560 if (tf->flags & ATA_TFLAG_LBA) {
561 if (tf->flags & ATA_TFLAG_LBA48) {
562 block |= (u64)tf->hob_lbah << 40;
563 block |= (u64)tf->hob_lbam << 32;
564 block |= tf->hob_lbal << 24;
565 } else
566 block |= (tf->device & 0xf) << 24;
567
568 block |= tf->lbah << 16;
569 block |= tf->lbam << 8;
570 block |= tf->lbal;
571 } else {
572 u32 cyl, head, sect;
573
574 cyl = tf->lbam | (tf->lbah << 8);
575 head = tf->device & 0xf;
576 sect = tf->lbal;
577
578 block = (cyl * dev->heads + head) * dev->sectors + sect;
579 }
580
581 return block;
582}
583
584/**
Tejun Heobd056d72006-11-14 22:47:10 +0900585 * ata_build_rw_tf - Build ATA taskfile for given read/write request
586 * @tf: Target ATA taskfile
587 * @dev: ATA device @tf belongs to
588 * @block: Block address
589 * @n_block: Number of blocks
590 * @tf_flags: RW/FUA etc...
591 * @tag: tag
592 *
593 * LOCKING:
594 * None.
595 *
596 * Build ATA taskfile @tf for read/write request described by
597 * @block, @n_block, @tf_flags and @tag on @dev.
598 *
599 * RETURNS:
600 *
601 * 0 on success, -ERANGE if the request is too large for @dev,
602 * -EINVAL if the request is invalid.
603 */
604int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
605 u64 block, u32 n_block, unsigned int tf_flags,
606 unsigned int tag)
607{
608 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
609 tf->flags |= tf_flags;
610
Tejun Heo6d1245b2007-02-20 23:20:27 +0900611 if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
Tejun Heobd056d72006-11-14 22:47:10 +0900612 /* yay, NCQ */
613 if (!lba_48_ok(block, n_block))
614 return -ERANGE;
615
616 tf->protocol = ATA_PROT_NCQ;
617 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
618
619 if (tf->flags & ATA_TFLAG_WRITE)
620 tf->command = ATA_CMD_FPDMA_WRITE;
621 else
622 tf->command = ATA_CMD_FPDMA_READ;
623
624 tf->nsect = tag << 3;
625 tf->hob_feature = (n_block >> 8) & 0xff;
626 tf->feature = n_block & 0xff;
627
628 tf->hob_lbah = (block >> 40) & 0xff;
629 tf->hob_lbam = (block >> 32) & 0xff;
630 tf->hob_lbal = (block >> 24) & 0xff;
631 tf->lbah = (block >> 16) & 0xff;
632 tf->lbam = (block >> 8) & 0xff;
633 tf->lbal = block & 0xff;
634
635 tf->device = 1 << 6;
636 if (tf->flags & ATA_TFLAG_FUA)
637 tf->device |= 1 << 7;
638 } else if (dev->flags & ATA_DFLAG_LBA) {
639 tf->flags |= ATA_TFLAG_LBA;
640
641 if (lba_28_ok(block, n_block)) {
642 /* use LBA28 */
643 tf->device |= (block >> 24) & 0xf;
644 } else if (lba_48_ok(block, n_block)) {
645 if (!(dev->flags & ATA_DFLAG_LBA48))
646 return -ERANGE;
647
648 /* use LBA48 */
649 tf->flags |= ATA_TFLAG_LBA48;
650
651 tf->hob_nsect = (n_block >> 8) & 0xff;
652
653 tf->hob_lbah = (block >> 40) & 0xff;
654 tf->hob_lbam = (block >> 32) & 0xff;
655 tf->hob_lbal = (block >> 24) & 0xff;
656 } else
657 /* request too large even for LBA48 */
658 return -ERANGE;
659
660 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
661 return -EINVAL;
662
663 tf->nsect = n_block & 0xff;
664
665 tf->lbah = (block >> 16) & 0xff;
666 tf->lbam = (block >> 8) & 0xff;
667 tf->lbal = block & 0xff;
668
669 tf->device |= ATA_LBA;
670 } else {
671 /* CHS */
672 u32 sect, head, cyl, track;
673
674 /* The request -may- be too large for CHS addressing. */
675 if (!lba_28_ok(block, n_block))
676 return -ERANGE;
677
678 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
679 return -EINVAL;
680
681 /* Convert LBA to CHS */
682 track = (u32)block / dev->sectors;
683 cyl = track / dev->heads;
684 head = track % dev->heads;
685 sect = (u32)block % dev->sectors + 1;
686
687 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
688 (u32)block, track, cyl, head, sect);
689
690 /* Check whether the converted CHS can fit.
691 Cylinder: 0-65535
692 Head: 0-15
693 Sector: 1-255*/
694 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
695 return -ERANGE;
696
697 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
698 tf->lbal = sect;
699 tf->lbam = cyl;
700 tf->lbah = cyl >> 8;
701 tf->device |= head;
702 }
703
704 return 0;
705}
706
707/**
Tejun Heocb95d562006-03-06 04:31:56 +0900708 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
709 * @pio_mask: pio_mask
710 * @mwdma_mask: mwdma_mask
711 * @udma_mask: udma_mask
712 *
713 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
714 * unsigned int xfer_mask.
715 *
716 * LOCKING:
717 * None.
718 *
719 * RETURNS:
720 * Packed xfer_mask.
721 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900722unsigned long ata_pack_xfermask(unsigned long pio_mask,
723 unsigned long mwdma_mask,
724 unsigned long udma_mask)
Tejun Heocb95d562006-03-06 04:31:56 +0900725{
726 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
727 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
728 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
729}
730
Tejun Heoc0489e42006-03-24 14:07:49 +0900731/**
732 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
733 * @xfer_mask: xfer_mask to unpack
734 * @pio_mask: resulting pio_mask
735 * @mwdma_mask: resulting mwdma_mask
736 * @udma_mask: resulting udma_mask
737 *
738 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
739 * Any NULL distination masks will be ignored.
740 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900741void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
742 unsigned long *mwdma_mask, unsigned long *udma_mask)
Tejun Heoc0489e42006-03-24 14:07:49 +0900743{
744 if (pio_mask)
745 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
746 if (mwdma_mask)
747 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
748 if (udma_mask)
749 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
750}
751
Tejun Heocb95d562006-03-06 04:31:56 +0900752static const struct ata_xfer_ent {
Tejun Heobe9a50c2006-03-31 22:48:52 +0900753 int shift, bits;
Tejun Heocb95d562006-03-06 04:31:56 +0900754 u8 base;
755} ata_xfer_tbl[] = {
Tejun Heo70cd0712007-11-27 19:43:40 +0900756 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
757 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
758 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
Tejun Heocb95d562006-03-06 04:31:56 +0900759 { -1, },
760};
761
762/**
763 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
764 * @xfer_mask: xfer_mask of interest
765 *
766 * Return matching XFER_* value for @xfer_mask. Only the highest
767 * bit of @xfer_mask is considered.
768 *
769 * LOCKING:
770 * None.
771 *
772 * RETURNS:
Tejun Heo70cd0712007-11-27 19:43:40 +0900773 * Matching XFER_* value, 0xff if no match found.
Tejun Heocb95d562006-03-06 04:31:56 +0900774 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900775u8 ata_xfer_mask2mode(unsigned long xfer_mask)
Tejun Heocb95d562006-03-06 04:31:56 +0900776{
777 int highbit = fls(xfer_mask) - 1;
778 const struct ata_xfer_ent *ent;
779
780 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
781 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
782 return ent->base + highbit - ent->shift;
Tejun Heo70cd0712007-11-27 19:43:40 +0900783 return 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +0900784}
785
786/**
787 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
788 * @xfer_mode: XFER_* of interest
789 *
790 * Return matching xfer_mask for @xfer_mode.
791 *
792 * LOCKING:
793 * None.
794 *
795 * RETURNS:
796 * Matching xfer_mask, 0 if no match found.
797 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900798unsigned long ata_xfer_mode2mask(u8 xfer_mode)
Tejun Heocb95d562006-03-06 04:31:56 +0900799{
800 const struct ata_xfer_ent *ent;
801
802 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
803 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
Tejun Heo70cd0712007-11-27 19:43:40 +0900804 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
805 & ~((1 << ent->shift) - 1);
Tejun Heocb95d562006-03-06 04:31:56 +0900806 return 0;
807}
808
809/**
810 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
811 * @xfer_mode: XFER_* of interest
812 *
813 * Return matching xfer_shift for @xfer_mode.
814 *
815 * LOCKING:
816 * None.
817 *
818 * RETURNS:
819 * Matching xfer_shift, -1 if no match found.
820 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900821int ata_xfer_mode2shift(unsigned long xfer_mode)
Tejun Heocb95d562006-03-06 04:31:56 +0900822{
823 const struct ata_xfer_ent *ent;
824
825 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
826 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
827 return ent->shift;
828 return -1;
829}
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900832 * ata_mode_string - convert xfer_mask to string
833 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 *
835 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900836 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 *
838 * LOCKING:
839 * None.
840 *
841 * RETURNS:
842 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900843 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900845const char *ata_mode_string(unsigned long xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Tejun Heo75f554b2006-03-06 04:31:57 +0900847 static const char * const xfer_mode_str[] = {
848 "PIO0",
849 "PIO1",
850 "PIO2",
851 "PIO3",
852 "PIO4",
Alan Coxb352e572006-08-10 18:52:12 +0100853 "PIO5",
854 "PIO6",
Tejun Heo75f554b2006-03-06 04:31:57 +0900855 "MWDMA0",
856 "MWDMA1",
857 "MWDMA2",
Alan Coxb352e572006-08-10 18:52:12 +0100858 "MWDMA3",
859 "MWDMA4",
Tejun Heo75f554b2006-03-06 04:31:57 +0900860 "UDMA/16",
861 "UDMA/25",
862 "UDMA/33",
863 "UDMA/44",
864 "UDMA/66",
865 "UDMA/100",
866 "UDMA/133",
867 "UDMA7",
868 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900869 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900871 highbit = fls(xfer_mask) - 1;
872 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
873 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
Tejun Heo4c360c82006-04-01 01:38:17 +0900877static const char *sata_spd_string(unsigned int spd)
878{
879 static const char * const spd_str[] = {
880 "1.5 Gbps",
881 "3.0 Gbps",
882 };
883
884 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
885 return "<unknown>";
886 return spd_str[spd - 1];
887}
888
Tejun Heo3373efd2006-05-15 20:57:53 +0900889void ata_dev_disable(struct ata_device *dev)
Tejun Heo0b8efb02006-03-24 15:25:31 +0900890{
Tejun Heo09d7f9b2007-06-25 23:34:02 +0900891 if (ata_dev_enabled(dev)) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900892 if (ata_msg_drv(dev->link->ap))
Tejun Heo09d7f9b2007-06-25 23:34:02 +0900893 ata_dev_printk(dev, KERN_WARNING, "disabled\n");
Tejun Heo562f0c22007-12-15 15:05:01 +0900894 ata_acpi_on_disable(dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +0900895 ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 |
896 ATA_DNXFER_QUIET);
Tejun Heo0b8efb02006-03-24 15:25:31 +0900897 dev->class++;
898 }
899}
900
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400901static int ata_dev_set_dipm(struct ata_device *dev, enum link_pm policy)
902{
903 struct ata_link *link = dev->link;
904 struct ata_port *ap = link->ap;
905 u32 scontrol;
906 unsigned int err_mask;
907 int rc;
908
909 /*
910 * disallow DIPM for drivers which haven't set
911 * ATA_FLAG_IPM. This is because when DIPM is enabled,
912 * phy ready will be set in the interrupt status on
913 * state changes, which will cause some drivers to
914 * think there are errors - additionally drivers will
915 * need to disable hot plug.
916 */
917 if (!(ap->flags & ATA_FLAG_IPM) || !ata_dev_enabled(dev)) {
918 ap->pm_policy = NOT_AVAILABLE;
919 return -EINVAL;
920 }
921
922 /*
923 * For DIPM, we will only enable it for the
924 * min_power setting.
925 *
926 * Why? Because Disks are too stupid to know that
927 * If the host rejects a request to go to SLUMBER
928 * they should retry at PARTIAL, and instead it
929 * just would give up. So, for medium_power to
930 * work at all, we need to only allow HIPM.
931 */
932 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
933 if (rc)
934 return rc;
935
936 switch (policy) {
937 case MIN_POWER:
938 /* no restrictions on IPM transitions */
939 scontrol &= ~(0x3 << 8);
940 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
941 if (rc)
942 return rc;
943
944 /* enable DIPM */
945 if (dev->flags & ATA_DFLAG_DIPM)
946 err_mask = ata_dev_set_feature(dev,
947 SETFEATURES_SATA_ENABLE, SATA_DIPM);
948 break;
949 case MEDIUM_POWER:
950 /* allow IPM to PARTIAL */
951 scontrol &= ~(0x1 << 8);
952 scontrol |= (0x2 << 8);
953 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
954 if (rc)
955 return rc;
956
Kristen Carlson Accardif5456b62007-11-02 16:37:08 -0700957 /*
958 * we don't have to disable DIPM since IPM flags
959 * disallow transitions to SLUMBER, which effectively
960 * disable DIPM if it does not support PARTIAL
961 */
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400962 break;
963 case NOT_AVAILABLE:
964 case MAX_PERFORMANCE:
965 /* disable all IPM transitions */
966 scontrol |= (0x3 << 8);
967 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
968 if (rc)
969 return rc;
970
Kristen Carlson Accardif5456b62007-11-02 16:37:08 -0700971 /*
972 * we don't have to disable DIPM since IPM flags
973 * disallow all transitions which effectively
974 * disable DIPM anyway.
975 */
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400976 break;
977 }
978
979 /* FIXME: handle SET FEATURES failure */
980 (void) err_mask;
981
982 return 0;
983}
984
985/**
986 * ata_dev_enable_pm - enable SATA interface power management
Stephen Hemminger48166fd2007-10-31 10:00:27 -0700987 * @dev: device to enable power management
988 * @policy: the link power management policy
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400989 *
990 * Enable SATA Interface power management. This will enable
991 * Device Interface Power Management (DIPM) for min_power
992 * policy, and then call driver specific callbacks for
993 * enabling Host Initiated Power management.
994 *
995 * Locking: Caller.
996 * Returns: -EINVAL if IPM is not supported, 0 otherwise.
997 */
998void ata_dev_enable_pm(struct ata_device *dev, enum link_pm policy)
999{
1000 int rc = 0;
1001 struct ata_port *ap = dev->link->ap;
1002
1003 /* set HIPM first, then DIPM */
1004 if (ap->ops->enable_pm)
1005 rc = ap->ops->enable_pm(ap, policy);
1006 if (rc)
1007 goto enable_pm_out;
1008 rc = ata_dev_set_dipm(dev, policy);
1009
1010enable_pm_out:
1011 if (rc)
1012 ap->pm_policy = MAX_PERFORMANCE;
1013 else
1014 ap->pm_policy = policy;
1015 return /* rc */; /* hopefully we can use 'rc' eventually */
1016}
1017
Stephen Rothwell1992a5e2007-10-31 14:53:32 +11001018#ifdef CONFIG_PM
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001019/**
1020 * ata_dev_disable_pm - disable SATA interface power management
Stephen Hemminger48166fd2007-10-31 10:00:27 -07001021 * @dev: device to disable power management
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001022 *
1023 * Disable SATA Interface power management. This will disable
1024 * Device Interface Power Management (DIPM) without changing
1025 * policy, call driver specific callbacks for disabling Host
1026 * Initiated Power management.
1027 *
1028 * Locking: Caller.
1029 * Returns: void
1030 */
1031static void ata_dev_disable_pm(struct ata_device *dev)
1032{
1033 struct ata_port *ap = dev->link->ap;
1034
1035 ata_dev_set_dipm(dev, MAX_PERFORMANCE);
1036 if (ap->ops->disable_pm)
1037 ap->ops->disable_pm(ap);
1038}
Stephen Rothwell1992a5e2007-10-31 14:53:32 +11001039#endif /* CONFIG_PM */
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001040
1041void ata_lpm_schedule(struct ata_port *ap, enum link_pm policy)
1042{
1043 ap->pm_policy = policy;
Tejun Heo3ec25eb2008-03-27 18:37:14 +09001044 ap->link.eh_info.action |= ATA_EH_LPM;
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001045 ap->link.eh_info.flags |= ATA_EHI_NO_AUTOPSY;
1046 ata_port_schedule_eh(ap);
1047}
1048
Stephen Rothwell1992a5e2007-10-31 14:53:32 +11001049#ifdef CONFIG_PM
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001050static void ata_lpm_enable(struct ata_host *host)
1051{
1052 struct ata_link *link;
1053 struct ata_port *ap;
1054 struct ata_device *dev;
1055 int i;
1056
1057 for (i = 0; i < host->n_ports; i++) {
1058 ap = host->ports[i];
1059 ata_port_for_each_link(link, ap) {
1060 ata_link_for_each_dev(dev, link)
1061 ata_dev_disable_pm(dev);
1062 }
1063 }
1064}
1065
1066static void ata_lpm_disable(struct ata_host *host)
1067{
1068 int i;
1069
1070 for (i = 0; i < host->n_ports; i++) {
1071 struct ata_port *ap = host->ports[i];
1072 ata_lpm_schedule(ap, ap->pm_policy);
1073 }
1074}
Stephen Rothwell1992a5e2007-10-31 14:53:32 +11001075#endif /* CONFIG_PM */
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077/**
1078 * ata_dev_classify - determine device type based on ATA-spec signature
1079 * @tf: ATA taskfile register set for device to be identified
1080 *
1081 * Determine from taskfile register contents whether a device is
1082 * ATA or ATAPI, as per "Signature and persistence" section
1083 * of ATA/PI spec (volume 1, sect 5.14).
1084 *
1085 * LOCKING:
1086 * None.
1087 *
1088 * RETURNS:
Tejun Heo633273a2007-09-23 13:19:54 +09001089 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
1090 * %ATA_DEV_UNKNOWN the event of failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 */
Jeff Garzik057ace52005-10-22 14:27:05 -04001092unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 /* Apple's open source Darwin code hints that some devices only
1095 * put a proper signature into the LBA mid/high registers,
1096 * So, we only check those. It's sufficient for uniqueness.
Tejun Heo633273a2007-09-23 13:19:54 +09001097 *
1098 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1099 * signatures for ATA and ATAPI devices attached on SerialATA,
1100 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
1101 * spec has never mentioned about using different signatures
1102 * for ATA/ATAPI devices. Then, Serial ATA II: Port
1103 * Multiplier specification began to use 0x69/0x96 to identify
1104 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1105 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1106 * 0x69/0x96 shortly and described them as reserved for
1107 * SerialATA.
1108 *
1109 * We follow the current spec and consider that 0x69/0x96
1110 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 */
Tejun Heo633273a2007-09-23 13:19:54 +09001112 if ((tf->lbam == 0) && (tf->lbah == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 DPRINTK("found ATA device by sig\n");
1114 return ATA_DEV_ATA;
1115 }
1116
Tejun Heo633273a2007-09-23 13:19:54 +09001117 if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 DPRINTK("found ATAPI device by sig\n");
1119 return ATA_DEV_ATAPI;
1120 }
1121
Tejun Heo633273a2007-09-23 13:19:54 +09001122 if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1123 DPRINTK("found PMP device by sig\n");
1124 return ATA_DEV_PMP;
1125 }
1126
1127 if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001128 printk(KERN_INFO "ata: SEMB device ignored\n");
Tejun Heo633273a2007-09-23 13:19:54 +09001129 return ATA_DEV_SEMB_UNSUP; /* not yet */
1130 }
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 DPRINTK("unknown device\n");
1133 return ATA_DEV_UNKNOWN;
1134}
1135
1136/**
Tejun Heo6a62a042006-02-13 10:02:46 +09001137 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 * @id: IDENTIFY DEVICE results we will examine
1139 * @s: string into which data is output
1140 * @ofs: offset into identify device page
1141 * @len: length of string to return. must be an even number.
1142 *
1143 * The strings in the IDENTIFY DEVICE page are broken up into
1144 * 16-bit chunks. Run through the string, and output each
1145 * 8-bit chunk linearly, regardless of platform.
1146 *
1147 * LOCKING:
1148 * caller.
1149 */
1150
Tejun Heo6a62a042006-02-13 10:02:46 +09001151void ata_id_string(const u16 *id, unsigned char *s,
1152 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
1154 unsigned int c;
1155
1156 while (len > 0) {
1157 c = id[ofs] >> 8;
1158 *s = c;
1159 s++;
1160
1161 c = id[ofs] & 0xff;
1162 *s = c;
1163 s++;
1164
1165 ofs++;
1166 len -= 2;
1167 }
1168}
1169
Tejun Heo0e949ff2006-02-12 22:47:04 +09001170/**
Tejun Heo6a62a042006-02-13 10:02:46 +09001171 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +09001172 * @id: IDENTIFY DEVICE results we will examine
1173 * @s: string into which data is output
1174 * @ofs: offset into identify device page
1175 * @len: length of string to return. must be an odd number.
1176 *
Tejun Heo6a62a042006-02-13 10:02:46 +09001177 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +09001178 * trims trailing spaces and terminates the resulting string with
1179 * null. @len must be actual maximum length (even number) + 1.
1180 *
1181 * LOCKING:
1182 * caller.
1183 */
Tejun Heo6a62a042006-02-13 10:02:46 +09001184void ata_id_c_string(const u16 *id, unsigned char *s,
1185 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +09001186{
1187 unsigned char *p;
1188
1189 WARN_ON(!(len & 1));
1190
Tejun Heo6a62a042006-02-13 10:02:46 +09001191 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +09001192
1193 p = s + strnlen(s, len - 1);
1194 while (p > s && p[-1] == ' ')
1195 p--;
1196 *p = '\0';
1197}
Edward Falk0baab862005-06-02 18:17:13 -04001198
Tejun Heodb6f8752007-09-03 12:31:58 +09001199static u64 ata_id_n_sectors(const u16 *id)
1200{
1201 if (ata_id_has_lba(id)) {
1202 if (ata_id_has_lba48(id))
1203 return ata_id_u64(id, 100);
1204 else
1205 return ata_id_u32(id, 60);
1206 } else {
1207 if (ata_id_current_chs_valid(id))
1208 return ata_id_u32(id, 57);
1209 else
1210 return id[1] * id[3] * id[6];
1211 }
1212}
1213
Alan Cox1e999732007-04-11 00:23:13 +01001214static u64 ata_tf_to_lba48(struct ata_taskfile *tf)
1215{
1216 u64 sectors = 0;
1217
1218 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1219 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1220 sectors |= (tf->hob_lbal & 0xff) << 24;
1221 sectors |= (tf->lbah & 0xff) << 16;
1222 sectors |= (tf->lbam & 0xff) << 8;
1223 sectors |= (tf->lbal & 0xff);
1224
1225 return ++sectors;
1226}
1227
1228static u64 ata_tf_to_lba(struct ata_taskfile *tf)
1229{
1230 u64 sectors = 0;
1231
1232 sectors |= (tf->device & 0x0f) << 24;
1233 sectors |= (tf->lbah & 0xff) << 16;
1234 sectors |= (tf->lbam & 0xff) << 8;
1235 sectors |= (tf->lbal & 0xff);
1236
1237 return ++sectors;
1238}
1239
1240/**
Tejun Heoc728a912007-09-03 12:32:30 +09001241 * ata_read_native_max_address - Read native max address
1242 * @dev: target device
1243 * @max_sectors: out parameter for the result native max address
Alan Cox1e999732007-04-11 00:23:13 +01001244 *
Tejun Heoc728a912007-09-03 12:32:30 +09001245 * Perform an LBA48 or LBA28 native size query upon the device in
1246 * question.
1247 *
1248 * RETURNS:
1249 * 0 on success, -EACCES if command is aborted by the drive.
1250 * -EIO on other errors.
Alan Cox1e999732007-04-11 00:23:13 +01001251 */
Tejun Heoc728a912007-09-03 12:32:30 +09001252static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
Alan Cox1e999732007-04-11 00:23:13 +01001253{
Tejun Heoc728a912007-09-03 12:32:30 +09001254 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +01001255 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +09001256 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +01001257
1258 ata_tf_init(dev, &tf);
1259
Tejun Heoc728a912007-09-03 12:32:30 +09001260 /* always clear all address registers */
Alan Cox1e999732007-04-11 00:23:13 +01001261 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +09001262
1263 if (lba48) {
1264 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1265 tf.flags |= ATA_TFLAG_LBA48;
1266 } else
1267 tf.command = ATA_CMD_READ_NATIVE_MAX;
1268
Alan Cox1e999732007-04-11 00:23:13 +01001269 tf.protocol |= ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +09001270 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +01001271
Tejun Heo2b789102007-10-09 15:05:44 +09001272 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoc728a912007-09-03 12:32:30 +09001273 if (err_mask) {
1274 ata_dev_printk(dev, KERN_WARNING, "failed to read native "
1275 "max address (err_mask=0x%x)\n", err_mask);
1276 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1277 return -EACCES;
1278 return -EIO;
1279 }
Alan Cox1e999732007-04-11 00:23:13 +01001280
Tejun Heoc728a912007-09-03 12:32:30 +09001281 if (lba48)
1282 *max_sectors = ata_tf_to_lba48(&tf);
1283 else
1284 *max_sectors = ata_tf_to_lba(&tf);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001285 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
Alan Cox93328e12007-09-29 04:06:48 -04001286 (*max_sectors)--;
Tejun Heoc728a912007-09-03 12:32:30 +09001287 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001288}
1289
1290/**
Tejun Heoc728a912007-09-03 12:32:30 +09001291 * ata_set_max_sectors - Set max sectors
1292 * @dev: target device
Randy Dunlap6b38d1d2007-05-01 17:35:55 -07001293 * @new_sectors: new max sectors value to set for the device
Alan Cox1e999732007-04-11 00:23:13 +01001294 *
Tejun Heoc728a912007-09-03 12:32:30 +09001295 * Set max sectors of @dev to @new_sectors.
1296 *
1297 * RETURNS:
1298 * 0 on success, -EACCES if command is aborted or denied (due to
1299 * previous non-volatile SET_MAX) by the drive. -EIO on other
1300 * errors.
Alan Cox1e999732007-04-11 00:23:13 +01001301 */
Tejun Heo05027ad2007-09-03 12:32:57 +09001302static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
Alan Cox1e999732007-04-11 00:23:13 +01001303{
Tejun Heoc728a912007-09-03 12:32:30 +09001304 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +01001305 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +09001306 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +01001307
1308 new_sectors--;
1309
1310 ata_tf_init(dev, &tf);
1311
Alan Cox1e999732007-04-11 00:23:13 +01001312 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +09001313
1314 if (lba48) {
1315 tf.command = ATA_CMD_SET_MAX_EXT;
1316 tf.flags |= ATA_TFLAG_LBA48;
1317
1318 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1319 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1320 tf.hob_lbah = (new_sectors >> 40) & 0xff;
Tejun Heo1e582ba2007-09-21 20:07:14 +09001321 } else {
Tejun Heoc728a912007-09-03 12:32:30 +09001322 tf.command = ATA_CMD_SET_MAX;
1323
Tejun Heo1e582ba2007-09-21 20:07:14 +09001324 tf.device |= (new_sectors >> 24) & 0xf;
1325 }
1326
Alan Cox1e999732007-04-11 00:23:13 +01001327 tf.protocol |= ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +09001328 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +01001329
1330 tf.lbal = (new_sectors >> 0) & 0xff;
1331 tf.lbam = (new_sectors >> 8) & 0xff;
1332 tf.lbah = (new_sectors >> 16) & 0xff;
Alan Cox1e999732007-04-11 00:23:13 +01001333
Tejun Heo2b789102007-10-09 15:05:44 +09001334 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoc728a912007-09-03 12:32:30 +09001335 if (err_mask) {
1336 ata_dev_printk(dev, KERN_WARNING, "failed to set "
1337 "max address (err_mask=0x%x)\n", err_mask);
1338 if (err_mask == AC_ERR_DEV &&
1339 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1340 return -EACCES;
1341 return -EIO;
1342 }
Alan Cox1e999732007-04-11 00:23:13 +01001343
Tejun Heoc728a912007-09-03 12:32:30 +09001344 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001345}
1346
1347/**
1348 * ata_hpa_resize - Resize a device with an HPA set
1349 * @dev: Device to resize
1350 *
1351 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1352 * it if required to the full size of the media. The caller must check
1353 * the drive has the HPA feature set enabled.
Tejun Heo05027ad2007-09-03 12:32:57 +09001354 *
1355 * RETURNS:
1356 * 0 on success, -errno on failure.
Alan Cox1e999732007-04-11 00:23:13 +01001357 */
Tejun Heo05027ad2007-09-03 12:32:57 +09001358static int ata_hpa_resize(struct ata_device *dev)
Alan Cox1e999732007-04-11 00:23:13 +01001359{
Tejun Heo05027ad2007-09-03 12:32:57 +09001360 struct ata_eh_context *ehc = &dev->link->eh_context;
1361 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
1362 u64 sectors = ata_id_n_sectors(dev->id);
1363 u64 native_sectors;
Tejun Heoc728a912007-09-03 12:32:30 +09001364 int rc;
Jeff Garzika617c092007-05-21 20:14:23 -04001365
Tejun Heo05027ad2007-09-03 12:32:57 +09001366 /* do we need to do it? */
1367 if (dev->class != ATA_DEV_ATA ||
1368 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1369 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
Tejun Heoc728a912007-09-03 12:32:30 +09001370 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001371
Tejun Heo05027ad2007-09-03 12:32:57 +09001372 /* read native max address */
1373 rc = ata_read_native_max_address(dev, &native_sectors);
1374 if (rc) {
Tejun Heodda7aba2008-03-23 21:05:15 +09001375 /* If device aborted the command or HPA isn't going to
1376 * be unlocked, skip HPA resizing.
Tejun Heo05027ad2007-09-03 12:32:57 +09001377 */
Tejun Heodda7aba2008-03-23 21:05:15 +09001378 if (rc == -EACCES || !ata_ignore_hpa) {
Tejun Heo05027ad2007-09-03 12:32:57 +09001379 ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
Tejun Heodda7aba2008-03-23 21:05:15 +09001380 "broken, skipping HPA handling\n");
Tejun Heo05027ad2007-09-03 12:32:57 +09001381 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
Alan Cox1e999732007-04-11 00:23:13 +01001382
Tejun Heo05027ad2007-09-03 12:32:57 +09001383 /* we can continue if device aborted the command */
1384 if (rc == -EACCES)
1385 rc = 0;
Alan Cox1e999732007-04-11 00:23:13 +01001386 }
Tejun Heo37301a52007-06-25 20:45:54 +09001387
Tejun Heo05027ad2007-09-03 12:32:57 +09001388 return rc;
1389 }
1390
1391 /* nothing to do? */
1392 if (native_sectors <= sectors || !ata_ignore_hpa) {
1393 if (!print_info || native_sectors == sectors)
1394 return 0;
1395
1396 if (native_sectors > sectors)
1397 ata_dev_printk(dev, KERN_INFO,
1398 "HPA detected: current %llu, native %llu\n",
1399 (unsigned long long)sectors,
1400 (unsigned long long)native_sectors);
1401 else if (native_sectors < sectors)
1402 ata_dev_printk(dev, KERN_WARNING,
1403 "native sectors (%llu) is smaller than "
1404 "sectors (%llu)\n",
1405 (unsigned long long)native_sectors,
1406 (unsigned long long)sectors);
1407 return 0;
1408 }
1409
1410 /* let's unlock HPA */
1411 rc = ata_set_max_sectors(dev, native_sectors);
1412 if (rc == -EACCES) {
1413 /* if device aborted the command, skip HPA resizing */
1414 ata_dev_printk(dev, KERN_WARNING, "device aborted resize "
1415 "(%llu -> %llu), skipping HPA handling\n",
1416 (unsigned long long)sectors,
1417 (unsigned long long)native_sectors);
1418 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1419 return 0;
1420 } else if (rc)
1421 return rc;
1422
1423 /* re-read IDENTIFY data */
1424 rc = ata_dev_reread_id(dev, 0);
1425 if (rc) {
1426 ata_dev_printk(dev, KERN_ERR, "failed to re-read IDENTIFY "
1427 "data after HPA resizing\n");
1428 return rc;
1429 }
1430
1431 if (print_info) {
1432 u64 new_sectors = ata_id_n_sectors(dev->id);
1433 ata_dev_printk(dev, KERN_INFO,
1434 "HPA unlocked: %llu -> %llu, native %llu\n",
1435 (unsigned long long)sectors,
1436 (unsigned long long)new_sectors,
1437 (unsigned long long)native_sectors);
1438 }
1439
1440 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001441}
1442
Edward Falk0baab862005-06-02 18:17:13 -04001443/**
1444 * ata_noop_dev_select - Select device 0/1 on ATA bus
1445 * @ap: ATA channel to manipulate
1446 * @device: ATA device (numbered from zero) to select
1447 *
1448 * This function performs no actual function.
1449 *
1450 * May be used as the dev_select() entry in ata_port_operations.
1451 *
1452 * LOCKING:
1453 * caller.
1454 */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001455void ata_noop_dev_select(struct ata_port *ap, unsigned int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457}
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459/**
1460 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +09001461 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 *
Tejun Heo0bd33002006-02-12 22:47:05 +09001463 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1464 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 *
1466 * LOCKING:
1467 * caller.
1468 */
1469
Tejun Heo0bd33002006-02-12 22:47:05 +09001470static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 DPRINTK("49==0x%04x "
1473 "53==0x%04x "
1474 "63==0x%04x "
1475 "64==0x%04x "
1476 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001477 id[49],
1478 id[53],
1479 id[63],
1480 id[64],
1481 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 DPRINTK("80==0x%04x "
1483 "81==0x%04x "
1484 "82==0x%04x "
1485 "83==0x%04x "
1486 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001487 id[80],
1488 id[81],
1489 id[82],
1490 id[83],
1491 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 DPRINTK("88==0x%04x "
1493 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001494 id[88],
1495 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
Tejun Heocb95d562006-03-06 04:31:56 +09001498/**
1499 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1500 * @id: IDENTIFY data to compute xfer mask from
1501 *
1502 * Compute the xfermask for this device. This is not as trivial
1503 * as it seems if we must consider early devices correctly.
1504 *
1505 * FIXME: pre IDE drive timing (do we care ?).
1506 *
1507 * LOCKING:
1508 * None.
1509 *
1510 * RETURNS:
1511 * Computed xfermask
1512 */
Tejun Heo7dc951a2007-11-27 19:43:42 +09001513unsigned long ata_id_xfermask(const u16 *id)
Tejun Heocb95d562006-03-06 04:31:56 +09001514{
Tejun Heo7dc951a2007-11-27 19:43:42 +09001515 unsigned long pio_mask, mwdma_mask, udma_mask;
Tejun Heocb95d562006-03-06 04:31:56 +09001516
1517 /* Usual case. Word 53 indicates word 64 is valid */
1518 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1519 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1520 pio_mask <<= 3;
1521 pio_mask |= 0x7;
1522 } else {
1523 /* If word 64 isn't valid then Word 51 high byte holds
1524 * the PIO timing number for the maximum. Turn it into
1525 * a mask.
1526 */
Lennert Buytenhek7a0f1c82007-01-29 13:28:47 +01001527 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
Alan Cox46767aeb2006-09-29 18:26:47 +01001528 if (mode < 5) /* Valid PIO range */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001529 pio_mask = (2 << mode) - 1;
Alan Cox46767aeb2006-09-29 18:26:47 +01001530 else
1531 pio_mask = 1;
Tejun Heocb95d562006-03-06 04:31:56 +09001532
1533 /* But wait.. there's more. Design your standards by
1534 * committee and you too can get a free iordy field to
1535 * process. However its the speeds not the modes that
1536 * are supported... Note drivers using the timing API
1537 * will get this right anyway
1538 */
1539 }
1540
1541 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +09001542
Alan Coxb352e572006-08-10 18:52:12 +01001543 if (ata_id_is_cfa(id)) {
1544 /*
1545 * Process compact flash extended modes
1546 */
1547 int pio = id[163] & 0x7;
1548 int dma = (id[163] >> 3) & 7;
1549
1550 if (pio)
1551 pio_mask |= (1 << 5);
1552 if (pio > 1)
1553 pio_mask |= (1 << 6);
1554 if (dma)
1555 mwdma_mask |= (1 << 3);
1556 if (dma > 1)
1557 mwdma_mask |= (1 << 4);
1558 }
1559
Tejun Heofb21f0d2006-03-12 12:34:35 +09001560 udma_mask = 0;
1561 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1562 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +09001563
1564 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1565}
1566
Tejun Heo86e45b62006-03-05 15:29:09 +09001567/**
Jeff Garzik442eacc2007-12-19 04:25:10 -05001568 * ata_pio_queue_task - Queue port_task
Tejun Heo86e45b62006-03-05 15:29:09 +09001569 * @ap: The ata_port to queue port_task for
Randy Dunlape2a7f772006-05-18 10:50:18 -07001570 * @fn: workqueue function to be scheduled
David Howells65f27f32006-11-22 14:55:48 +00001571 * @data: data for @fn to use
Randy Dunlape2a7f772006-05-18 10:50:18 -07001572 * @delay: delay time for workqueue function
Tejun Heo86e45b62006-03-05 15:29:09 +09001573 *
1574 * Schedule @fn(@data) for execution after @delay jiffies using
1575 * port_task. There is one port_task per port and it's the
1576 * user(low level driver)'s responsibility to make sure that only
1577 * one task is active at any given time.
1578 *
1579 * libata core layer takes care of synchronization between
Jeff Garzik442eacc2007-12-19 04:25:10 -05001580 * port_task and EH. ata_pio_queue_task() may be ignored for EH
Tejun Heo86e45b62006-03-05 15:29:09 +09001581 * synchronization.
1582 *
1583 * LOCKING:
1584 * Inherited from caller.
1585 */
Tejun Heo624d5c52008-03-25 22:16:41 +09001586void ata_pio_queue_task(struct ata_port *ap, void *data, unsigned long delay)
Tejun Heo86e45b62006-03-05 15:29:09 +09001587{
David Howells65f27f32006-11-22 14:55:48 +00001588 ap->port_task_data = data;
Tejun Heo86e45b62006-03-05 15:29:09 +09001589
Oleg Nesterov45a66c12007-07-09 11:46:13 -07001590 /* may fail if ata_port_flush_task() in progress */
1591 queue_delayed_work(ata_wq, &ap->port_task, delay);
Tejun Heo86e45b62006-03-05 15:29:09 +09001592}
1593
1594/**
1595 * ata_port_flush_task - Flush port_task
1596 * @ap: The ata_port to flush port_task for
1597 *
1598 * After this function completes, port_task is guranteed not to
1599 * be running or scheduled.
1600 *
1601 * LOCKING:
1602 * Kernel thread context (may sleep)
1603 */
1604void ata_port_flush_task(struct ata_port *ap)
1605{
Tejun Heo86e45b62006-03-05 15:29:09 +09001606 DPRINTK("ENTER\n");
1607
Oleg Nesterov45a66c12007-07-09 11:46:13 -07001608 cancel_rearming_delayed_work(&ap->port_task);
Tejun Heo86e45b62006-03-05 15:29:09 +09001609
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001610 if (ata_msg_ctl(ap))
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08001611 ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __func__);
Tejun Heo86e45b62006-03-05 15:29:09 +09001612}
1613
Adrian Bunk7102d232007-01-04 00:09:36 +01001614static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001615{
Tejun Heo77853bf2006-01-23 13:09:36 +09001616 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001617
Tejun Heoa2a7a662005-12-13 14:48:31 +09001618 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001619}
1620
1621/**
Tejun Heo24326972006-11-14 22:47:09 +09001622 * ata_exec_internal_sg - execute libata internal command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001623 * @dev: Device to which the command is sent
1624 * @tf: Taskfile registers for the command and the result
Tejun Heod69cf372006-04-02 18:51:53 +09001625 * @cdb: CDB for packet command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001626 * @dma_dir: Data tranfer direction of the command
Randy Dunlap5c1ad8b2007-10-18 14:12:26 -07001627 * @sgl: sg list for the data buffer of the command
Tejun Heo24326972006-11-14 22:47:09 +09001628 * @n_elem: Number of sg entries
Tejun Heo2b789102007-10-09 15:05:44 +09001629 * @timeout: Timeout in msecs (0 for default)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001630 *
1631 * Executes libata internal command with timeout. @tf contains
1632 * command on entry and result on return. Timeout and error
1633 * conditions are reported via return value. No recovery action
1634 * is taken after a command times out. It's caller's duty to
1635 * clean up after timeout.
1636 *
1637 * LOCKING:
1638 * None. Should be called with kernel context, might sleep.
Tejun Heo551e8882006-06-12 14:09:49 +09001639 *
1640 * RETURNS:
1641 * Zero on success, AC_ERR_* mask on failure
Tejun Heoa2a7a662005-12-13 14:48:31 +09001642 */
Tejun Heo24326972006-11-14 22:47:09 +09001643unsigned ata_exec_internal_sg(struct ata_device *dev,
1644 struct ata_taskfile *tf, const u8 *cdb,
Jens Axboe87260212007-10-16 11:14:12 +02001645 int dma_dir, struct scatterlist *sgl,
Tejun Heo2b789102007-10-09 15:05:44 +09001646 unsigned int n_elem, unsigned long timeout)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001647{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001648 struct ata_link *link = dev->link;
1649 struct ata_port *ap = link->ap;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001650 u8 command = tf->command;
1651 struct ata_queued_cmd *qc;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001652 unsigned int tag, preempted_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001653 u32 preempted_sactive, preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001654 int preempted_nr_active_links;
Ingo Molnar60be6b92006-07-03 00:25:26 -07001655 DECLARE_COMPLETION_ONSTACK(wait);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001656 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001657 unsigned int err_mask;
Tejun Heod95a7172006-05-15 20:58:14 +09001658 int rc;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001659
Jeff Garzikba6a1302006-06-22 23:46:10 -04001660 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001661
Tejun Heoe3180492006-05-15 20:58:09 +09001662 /* no internal command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09001663 if (ap->pflags & ATA_PFLAG_FROZEN) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001664 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001665 return AC_ERR_SYSTEM;
1666 }
1667
Tejun Heo2ab7db12006-05-15 20:58:02 +09001668 /* initialize internal qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001669
Tejun Heo2ab7db12006-05-15 20:58:02 +09001670 /* XXX: Tag 0 is used for drivers with legacy EH as some
1671 * drivers choke if any other tag is given. This breaks
1672 * ata_tag_internal() test for those drivers. Don't use new
1673 * EH stuff without converting to it.
1674 */
1675 if (ap->ops->error_handler)
1676 tag = ATA_TAG_INTERNAL;
1677 else
1678 tag = 0;
1679
Tejun Heo6cec4a32006-05-15 21:03:41 +09001680 if (test_and_set_bit(tag, &ap->qc_allocated))
Tejun Heo2ab7db12006-05-15 20:58:02 +09001681 BUG();
Tejun Heof69499f2006-05-15 20:58:03 +09001682 qc = __ata_qc_from_tag(ap, tag);
Tejun Heo2ab7db12006-05-15 20:58:02 +09001683
1684 qc->tag = tag;
1685 qc->scsicmd = NULL;
1686 qc->ap = ap;
1687 qc->dev = dev;
1688 ata_qc_reinit(qc);
1689
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001690 preempted_tag = link->active_tag;
1691 preempted_sactive = link->sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001692 preempted_qc_active = ap->qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001693 preempted_nr_active_links = ap->nr_active_links;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001694 link->active_tag = ATA_TAG_POISON;
1695 link->sactive = 0;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001696 ap->qc_active = 0;
Tejun Heoda917d62007-09-23 13:14:12 +09001697 ap->nr_active_links = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001698
1699 /* prepare & issue qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001700 qc->tf = *tf;
Tejun Heod69cf372006-04-02 18:51:53 +09001701 if (cdb)
1702 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
Tejun Heoe61e0672006-05-15 20:57:40 +09001703 qc->flags |= ATA_QCFLAG_RESULT_TF;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001704 qc->dma_dir = dma_dir;
1705 if (dma_dir != DMA_NONE) {
Tejun Heo24326972006-11-14 22:47:09 +09001706 unsigned int i, buflen = 0;
Jens Axboe87260212007-10-16 11:14:12 +02001707 struct scatterlist *sg;
Tejun Heo24326972006-11-14 22:47:09 +09001708
Jens Axboe87260212007-10-16 11:14:12 +02001709 for_each_sg(sgl, sg, n_elem, i)
1710 buflen += sg->length;
Tejun Heo24326972006-11-14 22:47:09 +09001711
Jens Axboe87260212007-10-16 11:14:12 +02001712 ata_sg_init(qc, sgl, n_elem);
Brian King49c80422007-01-30 11:32:26 -06001713 qc->nbytes = buflen;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001714 }
1715
Tejun Heo77853bf2006-01-23 13:09:36 +09001716 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001717 qc->complete_fn = ata_qc_complete_internal;
1718
Tejun Heo8e0e6942006-03-31 20:41:11 +09001719 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001720
Jeff Garzikba6a1302006-06-22 23:46:10 -04001721 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001722
Tejun Heo2b789102007-10-09 15:05:44 +09001723 if (!timeout)
1724 timeout = ata_probe_timeout * 1000 / HZ;
1725
1726 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
Albert Lee41ade502006-03-14 11:19:04 +08001727
Tejun Heod95a7172006-05-15 20:58:14 +09001728 ata_port_flush_task(ap);
1729
1730 if (!rc) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001731 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001732
1733 /* We're racing with irq here. If we lose, the
1734 * following test prevents us from completing the qc
Tejun Heod95a7172006-05-15 20:58:14 +09001735 * twice. If we win, the port is frozen and will be
1736 * cleaned up by ->post_internal_cmd().
Tejun Heoa2a7a662005-12-13 14:48:31 +09001737 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001738 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heod95a7172006-05-15 20:58:14 +09001739 qc->err_mask |= AC_ERR_TIMEOUT;
1740
1741 if (ap->ops->error_handler)
1742 ata_port_freeze(ap);
1743 else
1744 ata_qc_complete(qc);
Tejun Heof15a1da2006-05-15 20:57:56 +09001745
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001746 if (ata_msg_warn(ap))
1747 ata_dev_printk(dev, KERN_WARNING,
Tejun Heo88574552006-06-25 20:00:35 +09001748 "qc timeout (cmd 0x%x)\n", command);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001749 }
1750
Jeff Garzikba6a1302006-06-22 23:46:10 -04001751 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001752 }
1753
Tejun Heod95a7172006-05-15 20:58:14 +09001754 /* do post_internal_cmd */
1755 if (ap->ops->post_internal_cmd)
1756 ap->ops->post_internal_cmd(qc);
1757
Tejun Heoa51d6442007-03-20 15:24:11 +09001758 /* perform minimal error analysis */
1759 if (qc->flags & ATA_QCFLAG_FAILED) {
1760 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1761 qc->err_mask |= AC_ERR_DEV;
1762
1763 if (!qc->err_mask)
1764 qc->err_mask |= AC_ERR_OTHER;
1765
1766 if (qc->err_mask & ~AC_ERR_OTHER)
1767 qc->err_mask &= ~AC_ERR_OTHER;
Tejun Heod95a7172006-05-15 20:58:14 +09001768 }
1769
Tejun Heo15869302006-05-15 20:57:33 +09001770 /* finish up */
Jeff Garzikba6a1302006-06-22 23:46:10 -04001771 spin_lock_irqsave(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001772
Tejun Heoe61e0672006-05-15 20:57:40 +09001773 *tf = qc->result_tf;
Tejun Heo77853bf2006-01-23 13:09:36 +09001774 err_mask = qc->err_mask;
1775
1776 ata_qc_free(qc);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001777 link->active_tag = preempted_tag;
1778 link->sactive = preempted_sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001779 ap->qc_active = preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001780 ap->nr_active_links = preempted_nr_active_links;
Tejun Heo77853bf2006-01-23 13:09:36 +09001781
Tejun Heo1f7dd3e92006-03-24 15:25:30 +09001782 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1783 * Until those drivers are fixed, we detect the condition
1784 * here, fail the command with AC_ERR_SYSTEM and reenable the
1785 * port.
1786 *
1787 * Note that this doesn't change any behavior as internal
1788 * command failure results in disabling the device in the
1789 * higher layer for LLDDs without new reset/EH callbacks.
1790 *
1791 * Kill the following code as soon as those drivers are fixed.
1792 */
Tejun Heo198e0fe2006-04-02 18:51:52 +09001793 if (ap->flags & ATA_FLAG_DISABLED) {
Tejun Heo1f7dd3e92006-03-24 15:25:30 +09001794 err_mask |= AC_ERR_SYSTEM;
1795 ata_port_probe(ap);
1796 }
1797
Jeff Garzikba6a1302006-06-22 23:46:10 -04001798 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001799
Tejun Heo77853bf2006-01-23 13:09:36 +09001800 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001801}
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803/**
Tejun Heo33480a02006-12-12 02:15:31 +09001804 * ata_exec_internal - execute libata internal command
Tejun Heo24326972006-11-14 22:47:09 +09001805 * @dev: Device to which the command is sent
1806 * @tf: Taskfile registers for the command and the result
1807 * @cdb: CDB for packet command
1808 * @dma_dir: Data tranfer direction of the command
1809 * @buf: Data buffer of the command
1810 * @buflen: Length of data buffer
Tejun Heo2b789102007-10-09 15:05:44 +09001811 * @timeout: Timeout in msecs (0 for default)
Tejun Heo24326972006-11-14 22:47:09 +09001812 *
1813 * Wrapper around ata_exec_internal_sg() which takes simple
1814 * buffer instead of sg list.
1815 *
1816 * LOCKING:
1817 * None. Should be called with kernel context, might sleep.
1818 *
1819 * RETURNS:
1820 * Zero on success, AC_ERR_* mask on failure
1821 */
1822unsigned ata_exec_internal(struct ata_device *dev,
1823 struct ata_taskfile *tf, const u8 *cdb,
Tejun Heo2b789102007-10-09 15:05:44 +09001824 int dma_dir, void *buf, unsigned int buflen,
1825 unsigned long timeout)
Tejun Heo24326972006-11-14 22:47:09 +09001826{
Tejun Heo33480a02006-12-12 02:15:31 +09001827 struct scatterlist *psg = NULL, sg;
1828 unsigned int n_elem = 0;
Tejun Heo24326972006-11-14 22:47:09 +09001829
Tejun Heo33480a02006-12-12 02:15:31 +09001830 if (dma_dir != DMA_NONE) {
1831 WARN_ON(!buf);
1832 sg_init_one(&sg, buf, buflen);
1833 psg = &sg;
1834 n_elem++;
1835 }
Tejun Heo24326972006-11-14 22:47:09 +09001836
Tejun Heo2b789102007-10-09 15:05:44 +09001837 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
1838 timeout);
Tejun Heo24326972006-11-14 22:47:09 +09001839}
1840
1841/**
Tejun Heo977e6b92006-06-24 20:30:19 +09001842 * ata_do_simple_cmd - execute simple internal command
1843 * @dev: Device to which the command is sent
1844 * @cmd: Opcode to execute
1845 *
1846 * Execute a 'simple' command, that only consists of the opcode
1847 * 'cmd' itself, without filling any other registers
1848 *
1849 * LOCKING:
1850 * Kernel thread context (may sleep).
1851 *
1852 * RETURNS:
1853 * Zero on success, AC_ERR_* mask on failure
Tejun Heoe58eb582006-06-24 20:30:19 +09001854 */
Tejun Heo77b08fb2006-06-24 20:30:19 +09001855unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
Tejun Heoe58eb582006-06-24 20:30:19 +09001856{
1857 struct ata_taskfile tf;
Tejun Heoe58eb582006-06-24 20:30:19 +09001858
1859 ata_tf_init(dev, &tf);
1860
1861 tf.command = cmd;
1862 tf.flags |= ATA_TFLAG_DEVICE;
1863 tf.protocol = ATA_PROT_NODATA;
1864
Tejun Heo2b789102007-10-09 15:05:44 +09001865 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoe58eb582006-06-24 20:30:19 +09001866}
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001869 * ata_pio_need_iordy - check if iordy needed
1870 * @adev: ATA device
1871 *
1872 * Check if the current speed of the device requires IORDY. Used
1873 * by various controllers for chip configuration.
1874 */
Jeff Garzika617c092007-05-21 20:14:23 -04001875
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001876unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1877{
Alan Cox432729f2007-03-08 23:22:59 +00001878 /* Controller doesn't support IORDY. Probably a pointless check
1879 as the caller should know this */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001880 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001881 return 0;
Alan Cox432729f2007-03-08 23:22:59 +00001882 /* PIO3 and higher it is mandatory */
1883 if (adev->pio_mode > XFER_PIO_2)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001884 return 1;
Alan Cox432729f2007-03-08 23:22:59 +00001885 /* We turn it on when possible */
1886 if (ata_id_has_iordy(adev->id))
1887 return 1;
1888 return 0;
1889}
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001890
Alan Cox432729f2007-03-08 23:22:59 +00001891/**
1892 * ata_pio_mask_no_iordy - Return the non IORDY mask
1893 * @adev: ATA device
1894 *
1895 * Compute the highest mode possible if we are not using iordy. Return
1896 * -1 if no iordy mode is available.
1897 */
Jeff Garzika617c092007-05-21 20:14:23 -04001898
Alan Cox432729f2007-03-08 23:22:59 +00001899static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1900{
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001901 /* If we have no drive specific rule, then PIO 2 is non IORDY */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001902 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
Alan Cox432729f2007-03-08 23:22:59 +00001903 u16 pio = adev->id[ATA_ID_EIDE_PIO];
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001904 /* Is the speed faster than the drive allows non IORDY ? */
1905 if (pio) {
1906 /* This is cycle times not frequency - watch the logic! */
1907 if (pio > 240) /* PIO2 is 240nS per cycle */
Alan Cox432729f2007-03-08 23:22:59 +00001908 return 3 << ATA_SHIFT_PIO;
1909 return 7 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001910 }
1911 }
Alan Cox432729f2007-03-08 23:22:59 +00001912 return 3 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001913}
1914
1915/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001916 * ata_dev_read_id - Read ID data from the specified device
Tejun Heo49016ac2006-02-21 02:12:11 +09001917 * @dev: target device
1918 * @p_class: pointer to class of the target device (may be changed)
Tejun Heobff04642006-11-10 18:08:10 +09001919 * @flags: ATA_READID_* flags
Tejun Heofe635c72006-05-15 20:57:35 +09001920 * @id: buffer to read IDENTIFY data into
Tejun Heo49016ac2006-02-21 02:12:11 +09001921 *
1922 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1923 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09001924 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1925 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09001926 *
Alan Cox50a99012007-08-08 14:27:00 +01001927 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001928 * now we abort if we hit that case.
Alan Cox50a99012007-08-08 14:27:00 +01001929 *
Tejun Heo49016ac2006-02-21 02:12:11 +09001930 * LOCKING:
1931 * Kernel thread context (may sleep)
1932 *
1933 * RETURNS:
1934 * 0 on success, -errno otherwise.
1935 */
Tejun Heoa9beec92006-05-31 18:27:44 +09001936int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
Tejun Heobff04642006-11-10 18:08:10 +09001937 unsigned int flags, u16 *id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001938{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001939 struct ata_port *ap = dev->link->ap;
Tejun Heo49016ac2006-02-21 02:12:11 +09001940 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09001941 struct ata_taskfile tf;
1942 unsigned int err_mask = 0;
1943 const char *reason;
Tejun Heo54936f82007-05-11 14:35:29 +02001944 int may_fallback = 1, tried_spinup = 0;
Tejun Heo49016ac2006-02-21 02:12:11 +09001945 int rc;
1946
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001947 if (ata_msg_ctl(ap))
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08001948 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
Tejun Heo49016ac2006-02-21 02:12:11 +09001949
Tejun Heo49016ac2006-02-21 02:12:11 +09001950 retry:
Tejun Heo3373efd2006-05-15 20:57:53 +09001951 ata_tf_init(dev, &tf);
Tejun Heo49016ac2006-02-21 02:12:11 +09001952
1953 switch (class) {
1954 case ATA_DEV_ATA:
1955 tf.command = ATA_CMD_ID_ATA;
1956 break;
1957 case ATA_DEV_ATAPI:
1958 tf.command = ATA_CMD_ID_ATAPI;
1959 break;
1960 default:
1961 rc = -ENODEV;
1962 reason = "unsupported class";
1963 goto err_out;
1964 }
1965
1966 tf.protocol = ATA_PROT_PIO;
Tejun Heo81afe892007-02-07 12:37:41 -08001967
1968 /* Some devices choke if TF registers contain garbage. Make
1969 * sure those are properly initialized.
1970 */
1971 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1972
1973 /* Device presence detection is unreliable on some
1974 * controllers. Always poll IDENTIFY if available.
1975 */
1976 tf.flags |= ATA_TFLAG_POLLING;
Tejun Heo49016ac2006-02-21 02:12:11 +09001977
Tejun Heo3373efd2006-05-15 20:57:53 +09001978 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
Tejun Heo2b789102007-10-09 15:05:44 +09001979 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
Tejun Heo49016ac2006-02-21 02:12:11 +09001980 if (err_mask) {
Tejun Heo800b3992006-12-03 21:34:13 +09001981 if (err_mask & AC_ERR_NODEV_HINT) {
Tejun Heo1ffc1512008-03-23 15:16:53 +09001982 ata_dev_printk(dev, KERN_DEBUG,
1983 "NODEV after polling detection\n");
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001984 return -ENOENT;
1985 }
1986
Tejun Heo1ffc1512008-03-23 15:16:53 +09001987 if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1988 /* Device or controller might have reported
1989 * the wrong device class. Give a shot at the
1990 * other IDENTIFY if the current one is
1991 * aborted by the device.
1992 */
1993 if (may_fallback) {
1994 may_fallback = 0;
Tejun Heo54936f82007-05-11 14:35:29 +02001995
Tejun Heo1ffc1512008-03-23 15:16:53 +09001996 if (class == ATA_DEV_ATA)
1997 class = ATA_DEV_ATAPI;
1998 else
1999 class = ATA_DEV_ATA;
2000 goto retry;
2001 }
2002
2003 /* Control reaches here iff the device aborted
2004 * both flavors of IDENTIFYs which happens
2005 * sometimes with phantom devices.
2006 */
2007 ata_dev_printk(dev, KERN_DEBUG,
2008 "both IDENTIFYs aborted, assuming NODEV\n");
2009 return -ENOENT;
Tejun Heo54936f82007-05-11 14:35:29 +02002010 }
2011
Tejun Heo49016ac2006-02-21 02:12:11 +09002012 rc = -EIO;
2013 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09002014 goto err_out;
2015 }
2016
Tejun Heo54936f82007-05-11 14:35:29 +02002017 /* Falling back doesn't make sense if ID data was read
2018 * successfully at least once.
2019 */
2020 may_fallback = 0;
2021
Tejun Heo49016ac2006-02-21 02:12:11 +09002022 swap_buf_le16(id, ATA_ID_WORDS);
2023
Tejun Heo49016ac2006-02-21 02:12:11 +09002024 /* sanity check */
Tejun Heoa4f57492006-09-12 20:35:49 -07002025 rc = -EINVAL;
Alan Cox60700682007-06-07 16:13:55 +01002026 reason = "device reports invalid type";
Tejun Heoa4f57492006-09-12 20:35:49 -07002027
2028 if (class == ATA_DEV_ATA) {
2029 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
2030 goto err_out;
2031 } else {
2032 if (ata_id_is_ata(id))
2033 goto err_out;
Tejun Heo49016ac2006-02-21 02:12:11 +09002034 }
2035
Mark Lord169439c2007-04-17 18:26:07 -04002036 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
2037 tried_spinup = 1;
2038 /*
2039 * Drive powered-up in standby mode, and requires a specific
2040 * SET_FEATURES spin-up subcommand before it will accept
2041 * anything other than the original IDENTIFY command.
2042 */
Jeff Garzik218f3d32007-10-25 00:33:27 -04002043 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
Ryan Powerfb0582f92007-08-10 13:59:35 -07002044 if (err_mask && id[2] != 0x738c) {
Mark Lord169439c2007-04-17 18:26:07 -04002045 rc = -EIO;
2046 reason = "SPINUP failed";
2047 goto err_out;
2048 }
2049 /*
2050 * If the drive initially returned incomplete IDENTIFY info,
2051 * we now must reissue the IDENTIFY command.
2052 */
2053 if (id[2] == 0x37c8)
2054 goto retry;
2055 }
2056
Tejun Heobff04642006-11-10 18:08:10 +09002057 if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
Tejun Heo49016ac2006-02-21 02:12:11 +09002058 /*
2059 * The exact sequence expected by certain pre-ATA4 drives is:
2060 * SRST RESET
Alan Cox50a99012007-08-08 14:27:00 +01002061 * IDENTIFY (optional in early ATA)
2062 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
Tejun Heo49016ac2006-02-21 02:12:11 +09002063 * anything else..
2064 * Some drives were very specific about that exact sequence.
Alan Cox50a99012007-08-08 14:27:00 +01002065 *
2066 * Note that ATA4 says lba is mandatory so the second check
2067 * shoud never trigger.
Tejun Heo49016ac2006-02-21 02:12:11 +09002068 */
2069 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Tejun Heo3373efd2006-05-15 20:57:53 +09002070 err_mask = ata_dev_init_params(dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09002071 if (err_mask) {
2072 rc = -EIO;
2073 reason = "INIT_DEV_PARAMS failed";
2074 goto err_out;
2075 }
2076
2077 /* current CHS translation info (id[53-58]) might be
2078 * changed. reread the identify device info.
2079 */
Tejun Heobff04642006-11-10 18:08:10 +09002080 flags &= ~ATA_READID_POSTRESET;
Tejun Heo49016ac2006-02-21 02:12:11 +09002081 goto retry;
2082 }
2083 }
2084
2085 *p_class = class;
Tejun Heofe635c72006-05-15 20:57:35 +09002086
Tejun Heo49016ac2006-02-21 02:12:11 +09002087 return 0;
2088
2089 err_out:
Tejun Heo88574552006-06-25 20:00:35 +09002090 if (ata_msg_warn(ap))
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002091 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
Tejun Heo88574552006-06-25 20:00:35 +09002092 "(%s, err_mask=0x%x)\n", reason, err_mask);
Tejun Heo49016ac2006-02-21 02:12:11 +09002093 return rc;
2094}
2095
Tejun Heo3373efd2006-05-15 20:57:53 +09002096static inline u8 ata_dev_knobble(struct ata_device *dev)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002097{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002098 struct ata_port *ap = dev->link->ap;
2099 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002100}
2101
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002102static void ata_dev_config_ncq(struct ata_device *dev,
2103 char *desc, size_t desc_sz)
2104{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002105 struct ata_port *ap = dev->link->ap;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002106 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2107
2108 if (!ata_id_has_ncq(dev->id)) {
2109 desc[0] = '\0';
2110 return;
2111 }
Tejun Heo75683fe2007-07-05 13:31:27 +09002112 if (dev->horkage & ATA_HORKAGE_NONCQ) {
Alan Cox6919a0a2006-10-27 19:08:46 -07002113 snprintf(desc, desc_sz, "NCQ (not used)");
2114 return;
2115 }
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002116 if (ap->flags & ATA_FLAG_NCQ) {
Jeff Garzikcca39742006-08-24 03:19:22 -04002117 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002118 dev->flags |= ATA_DFLAG_NCQ;
2119 }
2120
2121 if (hdepth >= ddepth)
2122 snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
2123 else
2124 snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
2125}
2126
Tejun Heo49016ac2006-02-21 02:12:11 +09002127/**
Tejun Heoffeae412006-03-01 16:09:35 +09002128 * ata_dev_configure - Configure the specified ATA/ATAPI device
Tejun Heoffeae412006-03-01 16:09:35 +09002129 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 *
Tejun Heoffeae412006-03-01 16:09:35 +09002131 * Configure @dev according to @dev->id. Generic and low-level
2132 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 *
2134 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09002135 * Kernel thread context (may sleep)
2136 *
2137 * RETURNS:
2138 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 */
Tejun Heoefdaedc2006-11-01 18:38:52 +09002140int ata_dev_configure(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002142 struct ata_port *ap = dev->link->ap;
2143 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo67465442007-05-15 03:28:16 +09002144 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
Tejun Heo1148c3a2006-03-13 19:48:04 +09002145 const u16 *id = dev->id;
Tejun Heo7dc951a2007-11-27 19:43:42 +09002146 unsigned long xfer_mask;
Alan Coxb352e572006-08-10 18:52:12 +01002147 char revbuf[7]; /* XYZ-99\0 */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002148 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2149 char modelbuf[ATA_ID_PROD_LEN+1];
Brian Kinge6d902a2006-06-28 08:30:31 -05002150 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002152 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
Tejun Heo44877b42007-02-21 01:06:51 +09002153 ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08002154 __func__);
Tejun Heoffeae412006-03-01 16:09:35 +09002155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 }
2157
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002158 if (ata_msg_probe(ap))
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08002159 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Tejun Heo75683fe2007-07-05 13:31:27 +09002161 /* set horkage */
2162 dev->horkage |= ata_dev_blacklisted(dev);
Tejun Heo33267322008-02-13 09:15:09 +09002163 ata_force_horkage(dev);
Tejun Heo75683fe2007-07-05 13:31:27 +09002164
Tejun Heo67465442007-05-15 03:28:16 +09002165 /* let ACPI work its magic */
2166 rc = ata_acpi_on_devcfg(dev);
2167 if (rc)
2168 return rc;
Kristen Carlson Accardi08573a82006-11-10 16:14:47 -08002169
Tejun Heo05027ad2007-09-03 12:32:57 +09002170 /* massage HPA, do it early as it might change IDENTIFY data */
2171 rc = ata_hpa_resize(dev);
2172 if (rc)
2173 return rc;
2174
Tejun Heoc39f5eb2006-03-13 19:51:19 +09002175 /* print device capabilities */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002176 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002177 ata_dev_printk(dev, KERN_DEBUG,
2178 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2179 "85:%04x 86:%04x 87:%04x 88:%04x\n",
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08002180 __func__,
Tejun Heof15a1da2006-05-15 20:57:56 +09002181 id[49], id[82], id[83], id[84],
2182 id[85], id[86], id[87], id[88]);
Tejun Heoc39f5eb2006-03-13 19:51:19 +09002183
Tejun Heo208a9932006-03-05 17:55:58 +09002184 /* initialize to-be-configured parameters */
Tejun Heoea1dd4e2006-04-02 18:51:53 +09002185 dev->flags &= ~ATA_DFLAG_CFG_MASK;
Tejun Heo208a9932006-03-05 17:55:58 +09002186 dev->max_sectors = 0;
2187 dev->cdb_len = 0;
2188 dev->n_sectors = 0;
2189 dev->cylinders = 0;
2190 dev->heads = 0;
2191 dev->sectors = 0;
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 /*
2194 * common ATA, ATAPI feature tests
2195 */
2196
Tejun Heoff8854b2006-03-06 04:31:56 +09002197 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002198 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002200 if (ata_msg_probe(ap))
2201 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Albert Leeef143d52007-06-05 13:01:33 +08002203 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2204 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2205 sizeof(fwrevbuf));
2206
2207 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2208 sizeof(modelbuf));
2209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 /* ATA-specific feature tests */
2211 if (dev->class == ATA_DEV_ATA) {
Alan Coxb352e572006-08-10 18:52:12 +01002212 if (ata_id_is_cfa(id)) {
2213 if (id[162] & 1) /* CPRM may make this media unusable */
Tejun Heo44877b42007-02-21 01:06:51 +09002214 ata_dev_printk(dev, KERN_WARNING,
2215 "supports DRM functions and may "
2216 "not be fully accessable.\n");
Alan Coxb352e572006-08-10 18:52:12 +01002217 snprintf(revbuf, 7, "CFA");
Alan Coxae8d4ee2007-11-04 22:05:49 -05002218 } else {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002219 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
Alan Coxae8d4ee2007-11-04 22:05:49 -05002220 /* Warn the user if the device has TPM extensions */
2221 if (ata_id_has_tpm(id))
2222 ata_dev_printk(dev, KERN_WARNING,
2223 "supports DRM functions and may "
2224 "not be fully accessable.\n");
2225 }
Alan Coxb352e572006-08-10 18:52:12 +01002226
Tejun Heo1148c3a2006-03-13 19:48:04 +09002227 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09002228
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002229 if (dev->id[59] & 0x100)
2230 dev->multi_count = dev->id[59] & 0xff;
2231
Tejun Heo1148c3a2006-03-13 19:48:04 +09002232 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09002233 const char *lba_desc;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002234 char ncq_desc[20];
Albert Lee8bf62ece2005-05-12 15:29:42 -04002235
Tejun Heo4c2d7212006-03-05 17:55:58 +09002236 lba_desc = "LBA";
2237 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09002238 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04002239 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09002240 lba_desc = "LBA48";
Tejun Heo6fc49ad2006-11-11 20:10:45 +09002241
2242 if (dev->n_sectors >= (1UL << 28) &&
2243 ata_id_has_flush_ext(id))
2244 dev->flags |= ATA_DFLAG_FLUSH_EXT;
Tejun Heo4c2d7212006-03-05 17:55:58 +09002245 }
Albert Lee8bf62ece2005-05-12 15:29:42 -04002246
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002247 /* config NCQ */
2248 ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2249
Albert Lee8bf62ece2005-05-12 15:29:42 -04002250 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002251 if (ata_msg_drv(ap) && print_info) {
2252 ata_dev_printk(dev, KERN_INFO,
2253 "%s: %s, %s, max %s\n",
2254 revbuf, modelbuf, fwrevbuf,
2255 ata_mode_string(xfer_mask));
2256 ata_dev_printk(dev, KERN_INFO,
2257 "%Lu sectors, multi %u: %s %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09002258 (unsigned long long)dev->n_sectors,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002259 dev->multi_count, lba_desc, ncq_desc);
2260 }
Tejun Heoffeae412006-03-01 16:09:35 +09002261 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04002262 /* CHS */
2263
2264 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002265 dev->cylinders = id[1];
2266 dev->heads = id[3];
2267 dev->sectors = id[6];
Albert Lee8bf62ece2005-05-12 15:29:42 -04002268
Tejun Heo1148c3a2006-03-13 19:48:04 +09002269 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04002270 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002271 dev->cylinders = id[54];
2272 dev->heads = id[55];
2273 dev->sectors = id[56];
Albert Lee8bf62ece2005-05-12 15:29:42 -04002274 }
2275
2276 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002277 if (ata_msg_drv(ap) && print_info) {
Tejun Heo88574552006-06-25 20:00:35 +09002278 ata_dev_printk(dev, KERN_INFO,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002279 "%s: %s, %s, max %s\n",
2280 revbuf, modelbuf, fwrevbuf,
2281 ata_mode_string(xfer_mask));
Jeff Garzika84471f2007-02-26 05:51:33 -05002282 ata_dev_printk(dev, KERN_INFO,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002283 "%Lu sectors, multi %u, CHS %u/%u/%u\n",
2284 (unsigned long long)dev->n_sectors,
2285 dev->multi_count, dev->cylinders,
2286 dev->heads, dev->sectors);
2287 }
Albert Lee07f6f7d2005-11-01 19:33:20 +08002288 }
2289
Tejun Heo6e7846e2006-02-12 23:32:58 +09002290 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 }
2292
2293 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002294 else if (dev->class == ATA_DEV_ATAPI) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002295 const char *cdb_intr_string = "";
2296 const char *atapi_an_string = "";
Tejun Heo91163002008-02-21 13:25:50 +09002297 const char *dma_dir_string = "";
Tejun Heo7d77b242007-09-23 13:14:13 +09002298 u32 sntf;
Albert Lee08a556d2006-03-31 13:29:04 +08002299
Tejun Heo1148c3a2006-03-13 19:48:04 +09002300 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002302 if (ata_msg_warn(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002303 ata_dev_printk(dev, KERN_WARNING,
2304 "unsupported CDB len\n");
Tejun Heoffeae412006-03-01 16:09:35 +09002305 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 goto err_out_nosup;
2307 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09002308 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Tejun Heo7d77b242007-09-23 13:14:13 +09002310 /* Enable ATAPI AN if both the host and device have
2311 * the support. If PMP is attached, SNTF is required
2312 * to enable ATAPI AN to discern between PHY status
2313 * changed notifications and ATAPI ANs.
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002314 */
Tejun Heo7d77b242007-09-23 13:14:13 +09002315 if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2316 (!ap->nr_pmp_links ||
2317 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002318 unsigned int err_mask;
2319
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002320 /* issue SET feature command to turn this on */
Jeff Garzik218f3d32007-10-25 00:33:27 -04002321 err_mask = ata_dev_set_feature(dev,
2322 SETFEATURES_SATA_ENABLE, SATA_AN);
Tejun Heo854c73a2007-09-23 13:14:11 +09002323 if (err_mask)
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002324 ata_dev_printk(dev, KERN_ERR,
Tejun Heo854c73a2007-09-23 13:14:11 +09002325 "failed to enable ATAPI AN "
2326 "(err_mask=0x%x)\n", err_mask);
2327 else {
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002328 dev->flags |= ATA_DFLAG_AN;
Tejun Heo854c73a2007-09-23 13:14:11 +09002329 atapi_an_string = ", ATAPI AN";
2330 }
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002331 }
2332
Albert Lee08a556d2006-03-31 13:29:04 +08002333 if (ata_id_cdb_intr(dev->id)) {
Albert Lee312f7da2005-09-27 17:38:03 +08002334 dev->flags |= ATA_DFLAG_CDB_INTR;
Albert Lee08a556d2006-03-31 13:29:04 +08002335 cdb_intr_string = ", CDB intr";
2336 }
Albert Lee312f7da2005-09-27 17:38:03 +08002337
Tejun Heo91163002008-02-21 13:25:50 +09002338 if (atapi_dmadir || atapi_id_dmadir(dev->id)) {
2339 dev->flags |= ATA_DFLAG_DMADIR;
2340 dma_dir_string = ", DMADIR";
2341 }
2342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02002344 if (ata_msg_drv(ap) && print_info)
Albert Leeef143d52007-06-05 13:01:33 +08002345 ata_dev_printk(dev, KERN_INFO,
Tejun Heo91163002008-02-21 13:25:50 +09002346 "ATAPI: %s, %s, max %s%s%s%s\n",
Albert Leeef143d52007-06-05 13:01:33 +08002347 modelbuf, fwrevbuf,
Tejun Heo12436c32006-05-15 20:59:15 +09002348 ata_mode_string(xfer_mask),
Tejun Heo91163002008-02-21 13:25:50 +09002349 cdb_intr_string, atapi_an_string,
2350 dma_dir_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 }
2352
Tejun Heo914ed352006-11-01 18:39:55 +09002353 /* determine max_sectors */
2354 dev->max_sectors = ATA_MAX_SECTORS;
2355 if (dev->flags & ATA_DFLAG_LBA48)
2356 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2357
Kristen Carlson Accardica773292007-10-25 00:58:59 -04002358 if (!(dev->horkage & ATA_HORKAGE_IPM)) {
2359 if (ata_id_has_hipm(dev->id))
2360 dev->flags |= ATA_DFLAG_HIPM;
2361 if (ata_id_has_dipm(dev->id))
2362 dev->flags |= ATA_DFLAG_DIPM;
2363 }
2364
Alan Coxc5038fc2007-10-25 14:21:16 +01002365 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2366 200 sectors */
Tejun Heo3373efd2006-05-15 20:57:53 +09002367 if (ata_dev_knobble(dev)) {
Borislav Petkov5afc8142006-06-27 14:51:25 +02002368 if (ata_msg_drv(ap) && print_info)
Tejun Heof15a1da2006-05-15 20:57:56 +09002369 ata_dev_printk(dev, KERN_INFO,
2370 "applying bridge limits\n");
Tejun Heo5a529132006-03-24 14:07:50 +09002371 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002372 dev->max_sectors = ATA_MAX_SECTORS;
2373 }
2374
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002375 if ((dev->class == ATA_DEV_ATAPI) &&
Albert Leef442cd82007-11-15 10:35:47 +09002376 (atapi_command_packet_set(id) == TYPE_TAPE)) {
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002377 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
Albert Leef442cd82007-11-15 10:35:47 +09002378 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2379 }
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002380
Tejun Heo75683fe2007-07-05 13:31:27 +09002381 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
Tejun Heo03ec52d2007-04-12 13:38:11 +09002382 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2383 dev->max_sectors);
Albert Lee18d6e9d2007-04-02 11:34:15 +08002384
Kristen Carlson Accardica773292007-10-25 00:58:59 -04002385 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_IPM) {
2386 dev->horkage |= ATA_HORKAGE_IPM;
2387
2388 /* reset link pm_policy for this port to no pm */
2389 ap->pm_policy = MAX_PERFORMANCE;
2390 }
2391
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002392 if (ap->ops->dev_config)
Alancd0d3bb2007-03-02 00:56:15 +00002393 ap->ops->dev_config(dev);
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002394
Alan Coxc5038fc2007-10-25 14:21:16 +01002395 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2396 /* Let the user know. We don't want to disallow opens for
2397 rescue purposes, or in case the vendor is just a blithering
2398 idiot. Do this after the dev_config call as some controllers
2399 with buggy firmware may want to avoid reporting false device
2400 bugs */
2401
2402 if (print_info) {
2403 ata_dev_printk(dev, KERN_WARNING,
2404"Drive reports diagnostics failure. This may indicate a drive\n");
2405 ata_dev_printk(dev, KERN_WARNING,
2406"fault or invalid emulation. Contact drive vendor for information.\n");
2407 }
2408 }
2409
Tejun Heoffeae412006-03-01 16:09:35 +09002410 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
2412err_out_nosup:
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002413 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002414 ata_dev_printk(dev, KERN_DEBUG,
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08002415 "%s: EXIT, err\n", __func__);
Tejun Heoffeae412006-03-01 16:09:35 +09002416 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417}
2418
2419/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002420 * ata_cable_40wire - return 40 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002421 * @ap: port
2422 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002423 * Helper method for drivers which want to hardwire 40 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002424 * detection.
2425 */
2426
2427int ata_cable_40wire(struct ata_port *ap)
2428{
2429 return ATA_CBL_PATA40;
2430}
2431
2432/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002433 * ata_cable_80wire - return 80 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002434 * @ap: port
2435 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002436 * Helper method for drivers which want to hardwire 80 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002437 * detection.
2438 */
2439
2440int ata_cable_80wire(struct ata_port *ap)
2441{
2442 return ATA_CBL_PATA80;
2443}
2444
2445/**
2446 * ata_cable_unknown - return unknown PATA cable.
2447 * @ap: port
2448 *
2449 * Helper method for drivers which have no PATA cable detection.
2450 */
2451
2452int ata_cable_unknown(struct ata_port *ap)
2453{
2454 return ATA_CBL_PATA_UNK;
2455}
2456
2457/**
Tejun Heoc88f90c2007-11-27 19:43:48 +09002458 * ata_cable_ignore - return ignored PATA cable.
2459 * @ap: port
2460 *
2461 * Helper method for drivers which don't use cable type to limit
2462 * transfer mode.
2463 */
2464int ata_cable_ignore(struct ata_port *ap)
2465{
2466 return ATA_CBL_PATA_IGN;
2467}
2468
2469/**
Alan Coxbe0d18d2007-03-06 02:37:56 -08002470 * ata_cable_sata - return SATA cable type
2471 * @ap: port
2472 *
2473 * Helper method for drivers which have SATA cables
2474 */
2475
2476int ata_cable_sata(struct ata_port *ap)
2477{
2478 return ATA_CBL_SATA;
2479}
2480
2481/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 * ata_bus_probe - Reset and probe ATA bus
2483 * @ap: Bus to probe
2484 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002485 * Master ATA bus probing function. Initiates a hardware-dependent
2486 * bus reset, then attempts to identify any devices found on
2487 * the bus.
2488 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002490 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 *
2492 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09002493 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 */
2495
Brian King80289162006-08-07 14:27:31 -05002496int ata_bus_probe(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
Tejun Heo28ca5c52006-03-01 16:09:36 +09002498 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002499 int tries[ATA_MAX_DEVICES];
Tejun Heof58229f2007-08-06 18:36:23 +09002500 int rc;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002501 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Tejun Heo28ca5c52006-03-01 16:09:36 +09002503 ata_port_probe(ap);
2504
Tejun Heof58229f2007-08-06 18:36:23 +09002505 ata_link_for_each_dev(dev, &ap->link)
2506 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002507
2508 retry:
Tejun Heocdeab112007-10-29 16:41:09 +09002509 ata_link_for_each_dev(dev, &ap->link) {
2510 /* If we issue an SRST then an ATA drive (not ATAPI)
2511 * may change configuration and be in PIO0 timing. If
2512 * we do a hard reset (or are coming from power on)
2513 * this is true for ATA or ATAPI. Until we've set a
2514 * suitable controller mode we should not touch the
2515 * bus as we may be talking too fast.
2516 */
2517 dev->pio_mode = XFER_PIO_0;
2518
2519 /* If the controller has a pio mode setup function
2520 * then use it to set the chipset to rights. Don't
2521 * touch the DMA setup as that will be dealt with when
2522 * configuring devices.
2523 */
2524 if (ap->ops->set_piomode)
2525 ap->ops->set_piomode(ap, dev);
2526 }
2527
Tejun Heo20444702006-03-13 01:57:01 +09002528 /* reset and determine device classes */
Tejun Heo52783c52006-05-31 18:28:22 +09002529 ap->ops->phy_reset(ap);
Tejun Heo2061a472006-03-12 00:57:39 +09002530
Tejun Heof58229f2007-08-06 18:36:23 +09002531 ata_link_for_each_dev(dev, &ap->link) {
Tejun Heo52783c52006-05-31 18:28:22 +09002532 if (!(ap->flags & ATA_FLAG_DISABLED) &&
2533 dev->class != ATA_DEV_UNKNOWN)
2534 classes[dev->devno] = dev->class;
2535 else
2536 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo20444702006-03-13 01:57:01 +09002537
Tejun Heo52783c52006-05-31 18:28:22 +09002538 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo28ca5c52006-03-01 16:09:36 +09002539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Tejun Heo52783c52006-05-31 18:28:22 +09002541 ata_port_probe(ap);
Tejun Heo20444702006-03-13 01:57:01 +09002542
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002543 /* read IDENTIFY page and configure devices. We have to do the identify
2544 specific sequence bass-ackwards so that PDIAG- is released by
2545 the slave device */
2546
Bartlomiej Zolnierkiewicza4ba7fe2008-04-02 10:35:15 +09002547 ata_link_for_each_dev_reverse(dev, &ap->link) {
Tejun Heof58229f2007-08-06 18:36:23 +09002548 if (tries[dev->devno])
2549 dev->class = classes[dev->devno];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002550
Tejun Heoe1211e32006-04-01 01:38:18 +09002551 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09002552 continue;
2553
Tejun Heobff04642006-11-10 18:08:10 +09002554 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2555 dev->id);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002556 if (rc)
2557 goto fail;
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002558 }
2559
Alan Coxbe0d18d2007-03-06 02:37:56 -08002560 /* Now ask for the cable type as PDIAG- should have been released */
2561 if (ap->ops->cable_detect)
2562 ap->cbl = ap->ops->cable_detect(ap);
2563
Alan Cox614fe292007-08-22 23:22:45 +01002564 /* We may have SATA bridge glue hiding here irrespective of the
2565 reported cable types and sensed types */
2566 ata_link_for_each_dev(dev, &ap->link) {
2567 if (!ata_dev_enabled(dev))
2568 continue;
2569 /* SATA drives indicate we have a bridge. We don't know which
2570 end of the link the bridge is which is a problem */
2571 if (ata_id_is_sata(dev->id))
2572 ap->cbl = ATA_CBL_SATA;
2573 }
2574
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002575 /* After the identify sequence we can now set up the devices. We do
2576 this in the normal order so that the user doesn't get confused */
2577
Tejun Heof58229f2007-08-06 18:36:23 +09002578 ata_link_for_each_dev(dev, &ap->link) {
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002579 if (!ata_dev_enabled(dev))
2580 continue;
Tejun Heoffeae412006-03-01 16:09:35 +09002581
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002582 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
Tejun Heoefdaedc2006-11-01 18:38:52 +09002583 rc = ata_dev_configure(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002584 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002585 if (rc)
2586 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 }
2588
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002589 /* configure transfer mode */
Tejun Heo02607312007-08-06 18:36:23 +09002590 rc = ata_set_mode(&ap->link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002591 if (rc)
Tejun Heo51713d32006-04-11 22:26:29 +09002592 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Tejun Heof58229f2007-08-06 18:36:23 +09002594 ata_link_for_each_dev(dev, &ap->link)
2595 if (ata_dev_enabled(dev))
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002596 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01002597
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002598 /* no device present, disable port */
2599 ata_port_disable(ap);
Tejun Heo96072e62006-04-01 01:38:17 +09002600 return -ENODEV;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002601
2602 fail:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002603 tries[dev->devno]--;
2604
Tejun Heo14d2bac2006-04-02 17:54:46 +09002605 switch (rc) {
2606 case -EINVAL:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002607 /* eeek, something went very wrong, give up */
Tejun Heo14d2bac2006-04-02 17:54:46 +09002608 tries[dev->devno] = 0;
2609 break;
Tejun Heo4ae72a12007-02-02 16:22:30 +09002610
2611 case -ENODEV:
2612 /* give it just one more chance */
2613 tries[dev->devno] = min(tries[dev->devno], 1);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002614 case -EIO:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002615 if (tries[dev->devno] == 1) {
2616 /* This is the last chance, better to slow
2617 * down than lose it.
2618 */
Tejun Heo936fd732007-08-06 18:36:23 +09002619 sata_down_spd_limit(&ap->link);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002620 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2621 }
Tejun Heo14d2bac2006-04-02 17:54:46 +09002622 }
2623
Tejun Heo4ae72a12007-02-02 16:22:30 +09002624 if (!tries[dev->devno])
Tejun Heo3373efd2006-05-15 20:57:53 +09002625 ata_dev_disable(dev);
Tejun Heoec573752006-04-11 22:26:29 +09002626
Tejun Heo14d2bac2006-04-02 17:54:46 +09002627 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628}
2629
2630/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002631 * ata_port_probe - Mark port as enabled
2632 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002634 * Modify @ap data structure such that the system
2635 * thinks that the entire port is enabled.
2636 *
Jeff Garzikcca39742006-08-24 03:19:22 -04002637 * LOCKING: host lock, or some other form of
Jeff Garzik0cba6322005-05-30 19:49:12 -04002638 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 */
2640
2641void ata_port_probe(struct ata_port *ap)
2642{
Tejun Heo198e0fe2006-04-02 18:51:52 +09002643 ap->flags &= ~ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644}
2645
2646/**
Tejun Heo3be680b2005-12-19 22:35:02 +09002647 * sata_print_link_status - Print SATA link status
Tejun Heo936fd732007-08-06 18:36:23 +09002648 * @link: SATA link to printk link status about
Tejun Heo3be680b2005-12-19 22:35:02 +09002649 *
2650 * This function prints link speed and status of a SATA link.
2651 *
2652 * LOCKING:
2653 * None.
2654 */
Tejun Heo936fd732007-08-06 18:36:23 +09002655void sata_print_link_status(struct ata_link *link)
Tejun Heo3be680b2005-12-19 22:35:02 +09002656{
Tejun Heo6d5f9732006-04-03 00:09:41 +09002657 u32 sstatus, scontrol, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09002658
Tejun Heo936fd732007-08-06 18:36:23 +09002659 if (sata_scr_read(link, SCR_STATUS, &sstatus))
Tejun Heo3be680b2005-12-19 22:35:02 +09002660 return;
Tejun Heo936fd732007-08-06 18:36:23 +09002661 sata_scr_read(link, SCR_CONTROL, &scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002662
Tejun Heo936fd732007-08-06 18:36:23 +09002663 if (ata_link_online(link)) {
Tejun Heo3be680b2005-12-19 22:35:02 +09002664 tmp = (sstatus >> 4) & 0xf;
Tejun Heo936fd732007-08-06 18:36:23 +09002665 ata_link_printk(link, KERN_INFO,
Tejun Heof15a1da2006-05-15 20:57:56 +09002666 "SATA link up %s (SStatus %X SControl %X)\n",
2667 sata_spd_string(tmp), sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002668 } else {
Tejun Heo936fd732007-08-06 18:36:23 +09002669 ata_link_printk(link, KERN_INFO,
Tejun Heof15a1da2006-05-15 20:57:56 +09002670 "SATA link down (SStatus %X SControl %X)\n",
2671 sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002672 }
2673}
2674
2675/**
Alan Coxebdfca6e2006-03-23 15:38:34 +00002676 * ata_dev_pair - return other device on cable
Alan Coxebdfca6e2006-03-23 15:38:34 +00002677 * @adev: device
2678 *
2679 * Obtain the other device on the same cable, or if none is
2680 * present NULL is returned
2681 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002682
Tejun Heo3373efd2006-05-15 20:57:53 +09002683struct ata_device *ata_dev_pair(struct ata_device *adev)
Alan Coxebdfca6e2006-03-23 15:38:34 +00002684{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002685 struct ata_link *link = adev->link;
2686 struct ata_device *pair = &link->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09002687 if (!ata_dev_enabled(pair))
Alan Coxebdfca6e2006-03-23 15:38:34 +00002688 return NULL;
2689 return pair;
2690}
2691
2692/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002693 * ata_port_disable - Disable port.
2694 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002696 * Modify @ap data structure such that the system
2697 * thinks that the entire port is disabled, and should
2698 * never attempt to probe or communicate with devices
2699 * on this port.
2700 *
Jeff Garzikcca39742006-08-24 03:19:22 -04002701 * LOCKING: host lock, or some other form of
Jeff Garzik780a87f2005-05-30 15:41:05 -04002702 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 */
2704
2705void ata_port_disable(struct ata_port *ap)
2706{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002707 ap->link.device[0].class = ATA_DEV_NONE;
2708 ap->link.device[1].class = ATA_DEV_NONE;
Tejun Heo198e0fe2006-04-02 18:51:52 +09002709 ap->flags |= ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
2711
Tejun Heo1c3fae42006-04-02 20:53:28 +09002712/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002713 * sata_down_spd_limit - adjust SATA spd limit downward
Tejun Heo936fd732007-08-06 18:36:23 +09002714 * @link: Link to adjust SATA spd limit for
Tejun Heo1c3fae42006-04-02 20:53:28 +09002715 *
Tejun Heo936fd732007-08-06 18:36:23 +09002716 * Adjust SATA spd limit of @link downward. Note that this
Tejun Heo1c3fae42006-04-02 20:53:28 +09002717 * function only adjusts the limit. The change must be applied
Tejun Heo3c567b72006-05-15 20:57:23 +09002718 * using sata_set_spd().
Tejun Heo1c3fae42006-04-02 20:53:28 +09002719 *
2720 * LOCKING:
2721 * Inherited from caller.
2722 *
2723 * RETURNS:
2724 * 0 on success, negative errno on failure
2725 */
Tejun Heo936fd732007-08-06 18:36:23 +09002726int sata_down_spd_limit(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002727{
Tejun Heo81952c52006-05-15 20:57:47 +09002728 u32 sstatus, spd, mask;
2729 int rc, highbit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002730
Tejun Heo936fd732007-08-06 18:36:23 +09002731 if (!sata_scr_valid(link))
Tejun Heo008a7892007-07-16 14:29:40 +09002732 return -EOPNOTSUPP;
2733
2734 /* If SCR can be read, use it to determine the current SPD.
Tejun Heo936fd732007-08-06 18:36:23 +09002735 * If not, use cached value in link->sata_spd.
Tejun Heo008a7892007-07-16 14:29:40 +09002736 */
Tejun Heo936fd732007-08-06 18:36:23 +09002737 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
Tejun Heo008a7892007-07-16 14:29:40 +09002738 if (rc == 0)
2739 spd = (sstatus >> 4) & 0xf;
2740 else
Tejun Heo936fd732007-08-06 18:36:23 +09002741 spd = link->sata_spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002742
Tejun Heo936fd732007-08-06 18:36:23 +09002743 mask = link->sata_spd_limit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002744 if (mask <= 1)
2745 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09002746
2747 /* unconditionally mask off the highest bit */
Tejun Heo1c3fae42006-04-02 20:53:28 +09002748 highbit = fls(mask) - 1;
2749 mask &= ~(1 << highbit);
2750
Tejun Heo008a7892007-07-16 14:29:40 +09002751 /* Mask off all speeds higher than or equal to the current
2752 * one. Force 1.5Gbps if current SPD is not available.
2753 */
2754 if (spd > 1)
2755 mask &= (1 << (spd - 1)) - 1;
2756 else
2757 mask &= 1;
2758
2759 /* were we already at the bottom? */
Tejun Heo1c3fae42006-04-02 20:53:28 +09002760 if (!mask)
2761 return -EINVAL;
2762
Tejun Heo936fd732007-08-06 18:36:23 +09002763 link->sata_spd_limit = mask;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002764
Tejun Heo936fd732007-08-06 18:36:23 +09002765 ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09002766 sata_spd_string(fls(mask)));
Tejun Heo1c3fae42006-04-02 20:53:28 +09002767
2768 return 0;
2769}
2770
Tejun Heo936fd732007-08-06 18:36:23 +09002771static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002772{
Tejun Heo52702222007-10-31 10:17:07 +09002773 struct ata_link *host_link = &link->ap->link;
2774 u32 limit, target, spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002775
Tejun Heo52702222007-10-31 10:17:07 +09002776 limit = link->sata_spd_limit;
2777
2778 /* Don't configure downstream link faster than upstream link.
2779 * It doesn't speed up anything and some PMPs choke on such
2780 * configuration.
2781 */
2782 if (!ata_is_host_link(link) && host_link->sata_spd)
2783 limit &= (1 << host_link->sata_spd) - 1;
2784
2785 if (limit == UINT_MAX)
2786 target = 0;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002787 else
Tejun Heo52702222007-10-31 10:17:07 +09002788 target = fls(limit);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002789
2790 spd = (*scontrol >> 4) & 0xf;
Tejun Heo52702222007-10-31 10:17:07 +09002791 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002792
Tejun Heo52702222007-10-31 10:17:07 +09002793 return spd != target;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002794}
2795
2796/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002797 * sata_set_spd_needed - is SATA spd configuration needed
Tejun Heo936fd732007-08-06 18:36:23 +09002798 * @link: Link in question
Tejun Heo1c3fae42006-04-02 20:53:28 +09002799 *
2800 * Test whether the spd limit in SControl matches
Tejun Heo936fd732007-08-06 18:36:23 +09002801 * @link->sata_spd_limit. This function is used to determine
Tejun Heo1c3fae42006-04-02 20:53:28 +09002802 * whether hardreset is necessary to apply SATA spd
2803 * configuration.
2804 *
2805 * LOCKING:
2806 * Inherited from caller.
2807 *
2808 * RETURNS:
2809 * 1 if SATA spd configuration is needed, 0 otherwise.
2810 */
Tejun Heo936fd732007-08-06 18:36:23 +09002811int sata_set_spd_needed(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002812{
2813 u32 scontrol;
2814
Tejun Heo936fd732007-08-06 18:36:23 +09002815 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
Tejun Heodb64bcf2007-10-31 10:17:06 +09002816 return 1;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002817
Tejun Heo936fd732007-08-06 18:36:23 +09002818 return __sata_set_spd_needed(link, &scontrol);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002819}
2820
2821/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002822 * sata_set_spd - set SATA spd according to spd limit
Tejun Heo936fd732007-08-06 18:36:23 +09002823 * @link: Link to set SATA spd for
Tejun Heo1c3fae42006-04-02 20:53:28 +09002824 *
Tejun Heo936fd732007-08-06 18:36:23 +09002825 * Set SATA spd of @link according to sata_spd_limit.
Tejun Heo1c3fae42006-04-02 20:53:28 +09002826 *
2827 * LOCKING:
2828 * Inherited from caller.
2829 *
2830 * RETURNS:
2831 * 0 if spd doesn't need to be changed, 1 if spd has been
Tejun Heo81952c52006-05-15 20:57:47 +09002832 * changed. Negative errno if SCR registers are inaccessible.
Tejun Heo1c3fae42006-04-02 20:53:28 +09002833 */
Tejun Heo936fd732007-08-06 18:36:23 +09002834int sata_set_spd(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002835{
2836 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09002837 int rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002838
Tejun Heo936fd732007-08-06 18:36:23 +09002839 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09002840 return rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002841
Tejun Heo936fd732007-08-06 18:36:23 +09002842 if (!__sata_set_spd_needed(link, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09002843 return 0;
2844
Tejun Heo936fd732007-08-06 18:36:23 +09002845 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09002846 return rc;
2847
Tejun Heo1c3fae42006-04-02 20:53:28 +09002848 return 1;
2849}
2850
Alan Cox452503f2005-10-21 19:01:32 -04002851/*
2852 * This mode timing computation functionality is ported over from
2853 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
2854 */
2855/*
Alan Coxb352e572006-08-10 18:52:12 +01002856 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
Alan Cox452503f2005-10-21 19:01:32 -04002857 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
Alan Coxb352e572006-08-10 18:52:12 +01002858 * for UDMA6, which is currently supported only by Maxtor drives.
2859 *
2860 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
Alan Cox452503f2005-10-21 19:01:32 -04002861 */
2862
2863static const struct ata_timing ata_timing[] = {
Tejun Heo70cd0712007-11-27 19:43:40 +09002864/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
2865 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
2866 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
2867 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
2868 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
2869 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
2870 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
2871 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04002872
Tejun Heo70cd0712007-11-27 19:43:40 +09002873 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
2874 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
2875 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04002876
Tejun Heo70cd0712007-11-27 19:43:40 +09002877 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
2878 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
2879 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
Alan Coxb352e572006-08-10 18:52:12 +01002880 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
Tejun Heo70cd0712007-11-27 19:43:40 +09002881 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04002882
2883/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
Tejun Heo70cd0712007-11-27 19:43:40 +09002884 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
2885 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
2886 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
2887 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
2888 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
2889 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
2890 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
Alan Cox452503f2005-10-21 19:01:32 -04002891
2892 { 0xFF }
2893};
2894
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002895#define ENOUGH(v, unit) (((v)-1)/(unit)+1)
2896#define EZ(v, unit) ((v)?ENOUGH(v, unit):0)
Alan Cox452503f2005-10-21 19:01:32 -04002897
2898static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
2899{
2900 q->setup = EZ(t->setup * 1000, T);
2901 q->act8b = EZ(t->act8b * 1000, T);
2902 q->rec8b = EZ(t->rec8b * 1000, T);
2903 q->cyc8b = EZ(t->cyc8b * 1000, T);
2904 q->active = EZ(t->active * 1000, T);
2905 q->recover = EZ(t->recover * 1000, T);
2906 q->cycle = EZ(t->cycle * 1000, T);
2907 q->udma = EZ(t->udma * 1000, UT);
2908}
2909
2910void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
2911 struct ata_timing *m, unsigned int what)
2912{
2913 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
2914 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
2915 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
2916 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
2917 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
2918 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
2919 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
2920 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
2921}
2922
Tejun Heo63573572007-11-27 19:43:39 +09002923const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
Alan Cox452503f2005-10-21 19:01:32 -04002924{
Tejun Heo70cd0712007-11-27 19:43:40 +09002925 const struct ata_timing *t = ata_timing;
Alan Cox452503f2005-10-21 19:01:32 -04002926
Tejun Heo70cd0712007-11-27 19:43:40 +09002927 while (xfer_mode > t->mode)
2928 t++;
2929
2930 if (xfer_mode == t->mode)
2931 return t;
2932 return NULL;
Alan Cox452503f2005-10-21 19:01:32 -04002933}
2934
2935int ata_timing_compute(struct ata_device *adev, unsigned short speed,
2936 struct ata_timing *t, int T, int UT)
2937{
2938 const struct ata_timing *s;
2939 struct ata_timing p;
2940
2941 /*
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002942 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08002943 */
Alan Cox452503f2005-10-21 19:01:32 -04002944
2945 if (!(s = ata_timing_find_mode(speed)))
2946 return -EINVAL;
2947
Albert Lee75b1f2f2005-11-16 17:06:18 +08002948 memcpy(t, s, sizeof(*s));
2949
Alan Cox452503f2005-10-21 19:01:32 -04002950 /*
2951 * If the drive is an EIDE drive, it can tell us it needs extended
2952 * PIO/MW_DMA cycle timing.
2953 */
2954
2955 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
2956 memset(&p, 0, sizeof(p));
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002957 if (speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
Alan Cox452503f2005-10-21 19:01:32 -04002958 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
2959 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002960 } else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
Alan Cox452503f2005-10-21 19:01:32 -04002961 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
2962 }
2963 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
2964 }
2965
2966 /*
2967 * Convert the timing to bus clock counts.
2968 */
2969
Albert Lee75b1f2f2005-11-16 17:06:18 +08002970 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04002971
2972 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002973 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
2974 * S.M.A.R.T * and some other commands. We have to ensure that the
2975 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04002976 */
2977
Alanfd3367af2006-12-07 12:41:18 +00002978 if (speed > XFER_PIO_6) {
Alan Cox452503f2005-10-21 19:01:32 -04002979 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
2980 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
2981 }
2982
2983 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002984 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04002985 */
2986
2987 if (t->act8b + t->rec8b < t->cyc8b) {
2988 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
2989 t->rec8b = t->cyc8b - t->act8b;
2990 }
2991
2992 if (t->active + t->recover < t->cycle) {
2993 t->active += (t->cycle - (t->active + t->recover)) / 2;
2994 t->recover = t->cycle - t->active;
2995 }
Jeff Garzika617c092007-05-21 20:14:23 -04002996
Alan Cox4f701d12007-04-23 11:55:36 +01002997 /* In a few cases quantisation may produce enough errors to
2998 leave t->cycle too low for the sum of active and recovery
2999 if so we must correct this */
3000 if (t->active + t->recover > t->cycle)
3001 t->cycle = t->active + t->recover;
Alan Cox452503f2005-10-21 19:01:32 -04003002
3003 return 0;
3004}
3005
Tejun Heocf176e12006-04-02 17:54:46 +09003006/**
Tejun Heoa0f79b92007-12-18 16:33:05 +09003007 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3008 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3009 * @cycle: cycle duration in ns
3010 *
3011 * Return matching xfer mode for @cycle. The returned mode is of
3012 * the transfer type specified by @xfer_shift. If @cycle is too
3013 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3014 * than the fastest known mode, the fasted mode is returned.
3015 *
3016 * LOCKING:
3017 * None.
3018 *
3019 * RETURNS:
3020 * Matching xfer_mode, 0xff if no match found.
3021 */
3022u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3023{
3024 u8 base_mode = 0xff, last_mode = 0xff;
3025 const struct ata_xfer_ent *ent;
3026 const struct ata_timing *t;
3027
3028 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3029 if (ent->shift == xfer_shift)
3030 base_mode = ent->base;
3031
3032 for (t = ata_timing_find_mode(base_mode);
3033 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3034 unsigned short this_cycle;
3035
3036 switch (xfer_shift) {
3037 case ATA_SHIFT_PIO:
3038 case ATA_SHIFT_MWDMA:
3039 this_cycle = t->cycle;
3040 break;
3041 case ATA_SHIFT_UDMA:
3042 this_cycle = t->udma;
3043 break;
3044 default:
3045 return 0xff;
3046 }
3047
3048 if (cycle > this_cycle)
3049 break;
3050
3051 last_mode = t->mode;
3052 }
3053
3054 return last_mode;
3055}
3056
3057/**
Tejun Heocf176e12006-04-02 17:54:46 +09003058 * ata_down_xfermask_limit - adjust dev xfer masks downward
Tejun Heocf176e12006-04-02 17:54:46 +09003059 * @dev: Device to adjust xfer masks
Tejun Heo458337d2007-02-02 16:22:30 +09003060 * @sel: ATA_DNXFER_* selector
Tejun Heocf176e12006-04-02 17:54:46 +09003061 *
3062 * Adjust xfer masks of @dev downward. Note that this function
3063 * does not apply the change. Invoking ata_set_mode() afterwards
3064 * will apply the limit.
3065 *
3066 * LOCKING:
3067 * Inherited from caller.
3068 *
3069 * RETURNS:
3070 * 0 on success, negative errno on failure
3071 */
Tejun Heo458337d2007-02-02 16:22:30 +09003072int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
Tejun Heocf176e12006-04-02 17:54:46 +09003073{
Tejun Heo458337d2007-02-02 16:22:30 +09003074 char buf[32];
Tejun Heo7dc951a2007-11-27 19:43:42 +09003075 unsigned long orig_mask, xfer_mask;
3076 unsigned long pio_mask, mwdma_mask, udma_mask;
Tejun Heo458337d2007-02-02 16:22:30 +09003077 int quiet, highbit;
Tejun Heocf176e12006-04-02 17:54:46 +09003078
Tejun Heo458337d2007-02-02 16:22:30 +09003079 quiet = !!(sel & ATA_DNXFER_QUIET);
3080 sel &= ~ATA_DNXFER_QUIET;
Tejun Heocf176e12006-04-02 17:54:46 +09003081
Tejun Heo458337d2007-02-02 16:22:30 +09003082 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3083 dev->mwdma_mask,
3084 dev->udma_mask);
3085 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
Tejun Heocf176e12006-04-02 17:54:46 +09003086
Tejun Heo458337d2007-02-02 16:22:30 +09003087 switch (sel) {
3088 case ATA_DNXFER_PIO:
3089 highbit = fls(pio_mask) - 1;
3090 pio_mask &= ~(1 << highbit);
3091 break;
3092
3093 case ATA_DNXFER_DMA:
3094 if (udma_mask) {
3095 highbit = fls(udma_mask) - 1;
3096 udma_mask &= ~(1 << highbit);
3097 if (!udma_mask)
3098 return -ENOENT;
3099 } else if (mwdma_mask) {
3100 highbit = fls(mwdma_mask) - 1;
3101 mwdma_mask &= ~(1 << highbit);
3102 if (!mwdma_mask)
3103 return -ENOENT;
3104 }
3105 break;
3106
3107 case ATA_DNXFER_40C:
3108 udma_mask &= ATA_UDMA_MASK_40C;
3109 break;
3110
3111 case ATA_DNXFER_FORCE_PIO0:
3112 pio_mask &= 1;
3113 case ATA_DNXFER_FORCE_PIO:
3114 mwdma_mask = 0;
3115 udma_mask = 0;
3116 break;
3117
Tejun Heo458337d2007-02-02 16:22:30 +09003118 default:
3119 BUG();
3120 }
3121
3122 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3123
3124 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3125 return -ENOENT;
3126
3127 if (!quiet) {
3128 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3129 snprintf(buf, sizeof(buf), "%s:%s",
3130 ata_mode_string(xfer_mask),
3131 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3132 else
3133 snprintf(buf, sizeof(buf), "%s",
3134 ata_mode_string(xfer_mask));
3135
3136 ata_dev_printk(dev, KERN_WARNING,
3137 "limiting speed to %s\n", buf);
3138 }
Tejun Heocf176e12006-04-02 17:54:46 +09003139
3140 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3141 &dev->udma_mask);
3142
Tejun Heocf176e12006-04-02 17:54:46 +09003143 return 0;
Tejun Heocf176e12006-04-02 17:54:46 +09003144}
3145
Tejun Heo3373efd2006-05-15 20:57:53 +09003146static int ata_dev_set_mode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003148 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo4055dee2008-02-07 10:34:08 +09003149 const char *dev_err_whine = "";
3150 int ign_dev_err = 0;
Tejun Heo83206a22006-03-24 15:25:31 +09003151 unsigned int err_mask;
3152 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Tejun Heoe8384602006-04-02 18:51:53 +09003154 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 if (dev->xfer_shift == ATA_SHIFT_PIO)
3156 dev->flags |= ATA_DFLAG_PIO;
3157
Tejun Heo3373efd2006-05-15 20:57:53 +09003158 err_mask = ata_dev_set_xfermode(dev);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003159
Tejun Heo4055dee2008-02-07 10:34:08 +09003160 if (err_mask & ~AC_ERR_DEV)
3161 goto fail;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003162
Tejun Heo4055dee2008-02-07 10:34:08 +09003163 /* revalidate */
Tejun Heobaa1e782006-11-01 18:39:27 +09003164 ehc->i.flags |= ATA_EHI_POST_SETMODE;
Tejun Heo422c9da2007-09-23 13:14:12 +09003165 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
Tejun Heobaa1e782006-11-01 18:39:27 +09003166 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
Tejun Heo5eb45c02006-04-02 18:51:52 +09003167 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09003168 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09003169
Tejun Heo4055dee2008-02-07 10:34:08 +09003170 /* Old CFA may refuse this command, which is just fine */
3171 if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
3172 ign_dev_err = 1;
3173
3174 /* Some very old devices and some bad newer ones fail any kind of
3175 SET_XFERMODE request but support PIO0-2 timings and no IORDY */
3176 if (dev->xfer_shift == ATA_SHIFT_PIO && !ata_id_has_iordy(dev->id) &&
3177 dev->pio_mode <= XFER_PIO_2)
3178 ign_dev_err = 1;
3179
3180 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3181 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3182 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3183 dev->dma_mode == XFER_MW_DMA_0 &&
3184 (dev->id[63] >> 8) & 1)
3185 ign_dev_err = 1;
3186
3187 /* if the device is actually configured correctly, ignore dev err */
3188 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
3189 ign_dev_err = 1;
3190
3191 if (err_mask & AC_ERR_DEV) {
3192 if (!ign_dev_err)
3193 goto fail;
3194 else
3195 dev_err_whine = " (device error ignored)";
3196 }
3197
Tejun Heo23e71c32006-03-06 04:31:57 +09003198 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3199 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
Tejun Heo4055dee2008-02-07 10:34:08 +09003201 ata_dev_printk(dev, KERN_INFO, "configured for %s%s\n",
3202 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
3203 dev_err_whine);
3204
Tejun Heo83206a22006-03-24 15:25:31 +09003205 return 0;
Tejun Heo4055dee2008-02-07 10:34:08 +09003206
3207 fail:
3208 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
3209 "(err_mask=0x%x)\n", err_mask);
3210 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211}
3212
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213/**
Alan04351822007-03-06 02:37:52 -08003214 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
Tejun Heo02607312007-08-06 18:36:23 +09003215 * @link: link on which timings will be programmed
Joe Perches1967b7f2008-02-03 17:08:11 +02003216 * @r_failed_dev: out parameter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 *
Alan04351822007-03-06 02:37:52 -08003218 * Standard implementation of the function used to tune and set
3219 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3220 * ata_dev_set_mode() fails, pointer to the failing device is
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003221 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04003222 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003224 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003225 *
3226 * RETURNS:
3227 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 */
Alan04351822007-03-06 02:37:52 -08003229
Tejun Heo02607312007-08-06 18:36:23 +09003230int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231{
Tejun Heo02607312007-08-06 18:36:23 +09003232 struct ata_port *ap = link->ap;
Tejun Heoe8e06192006-04-01 01:38:18 +09003233 struct ata_device *dev;
Tejun Heof58229f2007-08-06 18:36:23 +09003234 int rc = 0, used_dma = 0, found = 0;
Tejun Heo3adcebb2006-05-15 20:57:37 +09003235
Tejun Heoa6d5a512006-03-06 04:31:57 +09003236 /* step 1: calculate xfer_mask */
Tejun Heof58229f2007-08-06 18:36:23 +09003237 ata_link_for_each_dev(dev, link) {
Tejun Heo7dc951a2007-11-27 19:43:42 +09003238 unsigned long pio_mask, dma_mask;
Alan Coxb3a70602007-10-02 12:38:26 -04003239 unsigned int mode_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003240
Tejun Heoe1211e32006-04-01 01:38:18 +09003241 if (!ata_dev_enabled(dev))
Tejun Heoa6d5a512006-03-06 04:31:57 +09003242 continue;
3243
Alan Coxb3a70602007-10-02 12:38:26 -04003244 mode_mask = ATA_DMA_MASK_ATA;
3245 if (dev->class == ATA_DEV_ATAPI)
3246 mode_mask = ATA_DMA_MASK_ATAPI;
3247 else if (ata_id_is_cfa(dev->id))
3248 mode_mask = ATA_DMA_MASK_CFA;
3249
Tejun Heo3373efd2006-05-15 20:57:53 +09003250 ata_dev_xfermask(dev);
Tejun Heo33267322008-02-13 09:15:09 +09003251 ata_force_xfermask(dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09003252
Tejun Heoacf356b2006-03-24 14:07:50 +09003253 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3254 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
Alan Coxb3a70602007-10-02 12:38:26 -04003255
3256 if (libata_dma_mask & mode_mask)
3257 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3258 else
3259 dma_mask = 0;
3260
Tejun Heoacf356b2006-03-24 14:07:50 +09003261 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3262 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01003263
Tejun Heo4f659772006-04-01 01:38:18 +09003264 found = 1;
Tejun Heo70cd0712007-11-27 19:43:40 +09003265 if (dev->dma_mode != 0xff)
Alan Cox5444a6f2006-03-27 18:58:20 +01003266 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003267 }
Tejun Heo4f659772006-04-01 01:38:18 +09003268 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003269 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003270
3271 /* step 2: always set host PIO timings */
Tejun Heof58229f2007-08-06 18:36:23 +09003272 ata_link_for_each_dev(dev, link) {
Tejun Heoe8e06192006-04-01 01:38:18 +09003273 if (!ata_dev_enabled(dev))
3274 continue;
3275
Tejun Heo70cd0712007-11-27 19:43:40 +09003276 if (dev->pio_mode == 0xff) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003277 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
Tejun Heoe8e06192006-04-01 01:38:18 +09003278 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003279 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09003280 }
3281
3282 dev->xfer_mode = dev->pio_mode;
3283 dev->xfer_shift = ATA_SHIFT_PIO;
3284 if (ap->ops->set_piomode)
3285 ap->ops->set_piomode(ap, dev);
3286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Tejun Heoa6d5a512006-03-06 04:31:57 +09003288 /* step 3: set host DMA timings */
Tejun Heof58229f2007-08-06 18:36:23 +09003289 ata_link_for_each_dev(dev, link) {
Tejun Heo70cd0712007-11-27 19:43:40 +09003290 if (!ata_dev_enabled(dev) || dev->dma_mode == 0xff)
Tejun Heoe8e06192006-04-01 01:38:18 +09003291 continue;
3292
3293 dev->xfer_mode = dev->dma_mode;
3294 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3295 if (ap->ops->set_dmamode)
3296 ap->ops->set_dmamode(ap, dev);
3297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298
3299 /* step 4: update devices' xfer mode */
Tejun Heof58229f2007-08-06 18:36:23 +09003300 ata_link_for_each_dev(dev, link) {
Alan18d90de2007-01-24 11:42:38 +00003301 /* don't update suspended devices' xfer mode */
Tejun Heo9666f402007-05-04 21:27:47 +02003302 if (!ata_dev_enabled(dev))
Tejun Heo83206a22006-03-24 15:25:31 +09003303 continue;
3304
Tejun Heo3373efd2006-05-15 20:57:53 +09003305 rc = ata_dev_set_mode(dev);
Tejun Heo5bbc53f2006-04-01 01:38:17 +09003306 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003307 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09003308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Tejun Heoe8e06192006-04-01 01:38:18 +09003310 /* Record simplex status. If we selected DMA then the other
3311 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01003312 */
Jeff Garzikcca39742006-08-24 03:19:22 -04003313 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
Alan032af1c2007-03-01 17:36:46 +00003314 ap->host->simplex_claimed = ap;
Alan Cox5444a6f2006-03-27 18:58:20 +01003315
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003316 out:
3317 if (rc)
3318 *r_failed_dev = dev;
3319 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320}
3321
3322/**
Tejun Heo936fd732007-08-06 18:36:23 +09003323 * sata_link_debounce - debounce SATA phy status
3324 * @link: ATA link to debounce SATA phy status for
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003325 * @params: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003326 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003327 *
Tejun Heo936fd732007-08-06 18:36:23 +09003328* Make sure SStatus of @link reaches stable state, determined by
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003329 * holding the same value where DET is not 1 for @duration polled
3330 * every @interval, before @timeout. Timeout constraints the
Tejun Heod4b2bab2007-02-02 16:50:52 +09003331 * beginning of the stable state. Because DET gets stuck at 1 on
3332 * some controllers after hot unplugging, this functions waits
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003333 * until timeout then returns 0 if DET is stable at 1.
3334 *
Tejun Heod4b2bab2007-02-02 16:50:52 +09003335 * @timeout is further limited by @deadline. The sooner of the
3336 * two is used.
3337 *
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003338 * LOCKING:
3339 * Kernel thread context (may sleep)
3340 *
3341 * RETURNS:
3342 * 0 on success, -errno on failure.
3343 */
Tejun Heo936fd732007-08-06 18:36:23 +09003344int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3345 unsigned long deadline)
Tejun Heo7a7921e2006-02-02 18:20:00 +09003346{
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003347 unsigned long interval_msec = params[0];
Tejun Heod4b2bab2007-02-02 16:50:52 +09003348 unsigned long duration = msecs_to_jiffies(params[1]);
3349 unsigned long last_jiffies, t;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003350 u32 last, cur;
3351 int rc;
3352
Tejun Heod4b2bab2007-02-02 16:50:52 +09003353 t = jiffies + msecs_to_jiffies(params[2]);
3354 if (time_before(t, deadline))
3355 deadline = t;
3356
Tejun Heo936fd732007-08-06 18:36:23 +09003357 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003358 return rc;
3359 cur &= 0xf;
3360
3361 last = cur;
3362 last_jiffies = jiffies;
3363
3364 while (1) {
3365 msleep(interval_msec);
Tejun Heo936fd732007-08-06 18:36:23 +09003366 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003367 return rc;
3368 cur &= 0xf;
3369
3370 /* DET stable? */
3371 if (cur == last) {
Tejun Heod4b2bab2007-02-02 16:50:52 +09003372 if (cur == 1 && time_before(jiffies, deadline))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003373 continue;
3374 if (time_after(jiffies, last_jiffies + duration))
3375 return 0;
3376 continue;
3377 }
3378
3379 /* unstable, start over */
3380 last = cur;
3381 last_jiffies = jiffies;
3382
Tejun Heof1545152007-07-16 14:29:40 +09003383 /* Check deadline. If debouncing failed, return
3384 * -EPIPE to tell upper layer to lower link speed.
3385 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003386 if (time_after(jiffies, deadline))
Tejun Heof1545152007-07-16 14:29:40 +09003387 return -EPIPE;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003388 }
3389}
3390
3391/**
Tejun Heo936fd732007-08-06 18:36:23 +09003392 * sata_link_resume - resume SATA link
3393 * @link: ATA link to resume SATA
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003394 * @params: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003395 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003396 *
Tejun Heo936fd732007-08-06 18:36:23 +09003397 * Resume SATA phy @link and debounce it.
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003398 *
3399 * LOCKING:
3400 * Kernel thread context (may sleep)
3401 *
3402 * RETURNS:
3403 * 0 on success, -errno on failure.
3404 */
Tejun Heo936fd732007-08-06 18:36:23 +09003405int sata_link_resume(struct ata_link *link, const unsigned long *params,
3406 unsigned long deadline)
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003407{
3408 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003409 int rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003410
Tejun Heo936fd732007-08-06 18:36:23 +09003411 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003412 return rc;
3413
Tejun Heo852ee162006-04-01 01:38:18 +09003414 scontrol = (scontrol & 0x0f0) | 0x300;
Tejun Heo81952c52006-05-15 20:57:47 +09003415
Tejun Heo936fd732007-08-06 18:36:23 +09003416 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003417 return rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003418
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003419 /* Some PHYs react badly if SStatus is pounded immediately
3420 * after resuming. Delay 200ms before debouncing.
3421 */
3422 msleep(200);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003423
Tejun Heo936fd732007-08-06 18:36:23 +09003424 return sata_link_debounce(link, params, deadline);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003425}
3426
Tejun Heof5914a42006-05-31 18:27:48 +09003427/**
3428 * ata_std_prereset - prepare for reset
Tejun Heocc0680a2007-08-06 18:36:23 +09003429 * @link: ATA link to be reset
Tejun Heod4b2bab2007-02-02 16:50:52 +09003430 * @deadline: deadline jiffies for the operation
Tejun Heof5914a42006-05-31 18:27:48 +09003431 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003432 * @link is about to be reset. Initialize it. Failure from
Tejun Heob8cffc62007-02-02 16:50:52 +09003433 * prereset makes libata abort whole reset sequence and give up
3434 * that port, so prereset should be best-effort. It does its
3435 * best to prepare for reset sequence but if things go wrong, it
3436 * should just whine, not fail.
Tejun Heof5914a42006-05-31 18:27:48 +09003437 *
3438 * LOCKING:
3439 * Kernel thread context (may sleep)
3440 *
3441 * RETURNS:
3442 * 0 on success, -errno otherwise.
3443 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003444int ata_std_prereset(struct ata_link *link, unsigned long deadline)
Tejun Heof5914a42006-05-31 18:27:48 +09003445{
Tejun Heocc0680a2007-08-06 18:36:23 +09003446 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09003447 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heoe9c83912006-07-03 16:07:26 +09003448 const unsigned long *timing = sata_ehc_deb_timing(ehc);
Tejun Heof5914a42006-05-31 18:27:48 +09003449 int rc;
3450
Tejun Heof5914a42006-05-31 18:27:48 +09003451 /* if we're about to do hardreset, nothing more to do */
3452 if (ehc->i.action & ATA_EH_HARDRESET)
3453 return 0;
3454
Tejun Heo936fd732007-08-06 18:36:23 +09003455 /* if SATA, resume link */
Tejun Heoa16abc02007-05-21 18:33:47 +02003456 if (ap->flags & ATA_FLAG_SATA) {
Tejun Heo936fd732007-08-06 18:36:23 +09003457 rc = sata_link_resume(link, timing, deadline);
Tejun Heob8cffc62007-02-02 16:50:52 +09003458 /* whine about phy resume failure but proceed */
3459 if (rc && rc != -EOPNOTSUPP)
Tejun Heocc0680a2007-08-06 18:36:23 +09003460 ata_link_printk(link, KERN_WARNING, "failed to resume "
Tejun Heof5914a42006-05-31 18:27:48 +09003461 "link for reset (errno=%d)\n", rc);
Tejun Heof5914a42006-05-31 18:27:48 +09003462 }
3463
Tejun Heo8cebf272008-01-24 00:05:14 +09003464 /* wait for !BSY if we don't know that no device is attached */
3465 if (!ata_link_offline(link)) {
Tejun Heob8cffc62007-02-02 16:50:52 +09003466 rc = ata_wait_ready(ap, deadline);
Tejun Heo6dffaf62007-05-23 11:58:52 +02003467 if (rc && rc != -ENODEV) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003468 ata_link_printk(link, KERN_WARNING, "device not ready "
Tejun Heob8cffc62007-02-02 16:50:52 +09003469 "(errno=%d), forcing hardreset\n", rc);
3470 ehc->i.action |= ATA_EH_HARDRESET;
3471 }
3472 }
Tejun Heof5914a42006-05-31 18:27:48 +09003473
3474 return 0;
3475}
3476
Tejun Heoc2bd5802006-01-24 17:05:22 +09003477/**
Tejun Heocc0680a2007-08-06 18:36:23 +09003478 * sata_link_hardreset - reset link via SATA phy reset
3479 * @link: link to reset
Tejun Heob6103f62006-11-01 17:59:53 +09003480 * @timing: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003481 * @deadline: deadline jiffies for the operation
Tejun Heoc2bd5802006-01-24 17:05:22 +09003482 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003483 * SATA phy-reset @link using DET bits of SControl register.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003484 *
3485 * LOCKING:
3486 * Kernel thread context (may sleep)
3487 *
3488 * RETURNS:
3489 * 0 on success, -errno otherwise.
3490 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003491int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003492 unsigned long deadline)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003493{
Tejun Heo852ee162006-04-01 01:38:18 +09003494 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003495 int rc;
Tejun Heo852ee162006-04-01 01:38:18 +09003496
Tejun Heoc2bd5802006-01-24 17:05:22 +09003497 DPRINTK("ENTER\n");
3498
Tejun Heo936fd732007-08-06 18:36:23 +09003499 if (sata_set_spd_needed(link)) {
Tejun Heo1c3fae42006-04-02 20:53:28 +09003500 /* SATA spec says nothing about how to reconfigure
3501 * spd. To be on the safe side, turn off phy during
3502 * reconfiguration. This works for at least ICH7 AHCI
3503 * and Sil3124.
3504 */
Tejun Heo936fd732007-08-06 18:36:23 +09003505 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003506 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09003507
Martin Hicksa34b6fc2006-07-05 15:06:13 -04003508 scontrol = (scontrol & 0x0f0) | 0x304;
Tejun Heo81952c52006-05-15 20:57:47 +09003509
Tejun Heo936fd732007-08-06 18:36:23 +09003510 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003511 goto out;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003512
Tejun Heo936fd732007-08-06 18:36:23 +09003513 sata_set_spd(link);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003514 }
3515
3516 /* issue phy wake/reset */
Tejun Heo936fd732007-08-06 18:36:23 +09003517 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003518 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09003519
Tejun Heo852ee162006-04-01 01:38:18 +09003520 scontrol = (scontrol & 0x0f0) | 0x301;
Tejun Heo81952c52006-05-15 20:57:47 +09003521
Tejun Heo936fd732007-08-06 18:36:23 +09003522 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003523 goto out;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003524
Tejun Heo1c3fae42006-04-02 20:53:28 +09003525 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
Tejun Heoc2bd5802006-01-24 17:05:22 +09003526 * 10.4.2 says at least 1 ms.
3527 */
3528 msleep(1);
3529
Tejun Heo936fd732007-08-06 18:36:23 +09003530 /* bring link back */
3531 rc = sata_link_resume(link, timing, deadline);
Tejun Heob6103f62006-11-01 17:59:53 +09003532 out:
3533 DPRINTK("EXIT, rc=%d\n", rc);
3534 return rc;
3535}
3536
3537/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09003538 * ata_std_postreset - standard postreset callback
Tejun Heocc0680a2007-08-06 18:36:23 +09003539 * @link: the target ata_link
Tejun Heoc2bd5802006-01-24 17:05:22 +09003540 * @classes: classes of attached devices
3541 *
3542 * This function is invoked after a successful reset. Note that
3543 * the device might have been reset more than once using
3544 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003545 *
Tejun Heoc2bd5802006-01-24 17:05:22 +09003546 * LOCKING:
3547 * Kernel thread context (may sleep)
3548 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003549void ata_std_postreset(struct ata_link *link, unsigned int *classes)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003550{
Tejun Heocc0680a2007-08-06 18:36:23 +09003551 struct ata_port *ap = link->ap;
Tejun Heodc2b3512006-05-15 20:58:00 +09003552 u32 serror;
3553
Tejun Heoc2bd5802006-01-24 17:05:22 +09003554 DPRINTK("ENTER\n");
3555
Tejun Heoc2bd5802006-01-24 17:05:22 +09003556 /* print link status */
Tejun Heo936fd732007-08-06 18:36:23 +09003557 sata_print_link_status(link);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003558
Tejun Heodc2b3512006-05-15 20:58:00 +09003559 /* clear SError */
Tejun Heo936fd732007-08-06 18:36:23 +09003560 if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
3561 sata_scr_write(link, SCR_ERROR, serror);
Tejun Heof7fe7ad2007-12-08 08:47:01 +09003562 link->eh_info.serror = 0;
Tejun Heodc2b3512006-05-15 20:58:00 +09003563
Tejun Heoc2bd5802006-01-24 17:05:22 +09003564 /* is double-select really necessary? */
3565 if (classes[0] != ATA_DEV_NONE)
3566 ap->ops->dev_select(ap, 1);
3567 if (classes[1] != ATA_DEV_NONE)
3568 ap->ops->dev_select(ap, 0);
3569
Tejun Heo3a397462006-02-10 23:58:48 +09003570 /* bail out if no device is present */
3571 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
3572 DPRINTK("EXIT, no device\n");
3573 return;
3574 }
3575
3576 /* set up device control */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003577 if (ap->ioaddr.ctl_addr)
3578 iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003579
3580 DPRINTK("EXIT\n");
3581}
3582
Tejun Heoa62c0fc2006-01-24 17:05:22 +09003583/**
Tejun Heo623a3122006-03-05 17:55:58 +09003584 * ata_dev_same_device - Determine whether new ID matches configured device
Tejun Heo623a3122006-03-05 17:55:58 +09003585 * @dev: device to compare against
3586 * @new_class: class of the new device
3587 * @new_id: IDENTIFY page of the new device
3588 *
3589 * Compare @new_class and @new_id against @dev and determine
3590 * whether @dev is the device indicated by @new_class and
3591 * @new_id.
3592 *
3593 * LOCKING:
3594 * None.
3595 *
3596 * RETURNS:
3597 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3598 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003599static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3600 const u16 *new_id)
Tejun Heo623a3122006-03-05 17:55:58 +09003601{
3602 const u16 *old_id = dev->id;
Tejun Heoa0cf7332007-01-02 20:18:49 +09003603 unsigned char model[2][ATA_ID_PROD_LEN + 1];
3604 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
Tejun Heo623a3122006-03-05 17:55:58 +09003605
3606 if (dev->class != new_class) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003607 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
3608 dev->class, new_class);
Tejun Heo623a3122006-03-05 17:55:58 +09003609 return 0;
3610 }
3611
Tejun Heoa0cf7332007-01-02 20:18:49 +09003612 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3613 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3614 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3615 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
Tejun Heo623a3122006-03-05 17:55:58 +09003616
3617 if (strcmp(model[0], model[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003618 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
3619 "'%s' != '%s'\n", model[0], model[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003620 return 0;
3621 }
3622
3623 if (strcmp(serial[0], serial[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003624 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
3625 "'%s' != '%s'\n", serial[0], serial[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003626 return 0;
3627 }
3628
Tejun Heo623a3122006-03-05 17:55:58 +09003629 return 1;
3630}
3631
3632/**
Tejun Heofe309112007-05-15 03:28:15 +09003633 * ata_dev_reread_id - Re-read IDENTIFY data
Henrik Kretzschmar3fae4502007-06-19 10:10:50 +02003634 * @dev: target ATA device
Tejun Heobff04642006-11-10 18:08:10 +09003635 * @readid_flags: read ID flags
Tejun Heo623a3122006-03-05 17:55:58 +09003636 *
3637 * Re-read IDENTIFY page and make sure @dev is still attached to
3638 * the port.
3639 *
3640 * LOCKING:
3641 * Kernel thread context (may sleep)
3642 *
3643 * RETURNS:
3644 * 0 on success, negative errno otherwise
3645 */
Tejun Heofe309112007-05-15 03:28:15 +09003646int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
Tejun Heo623a3122006-03-05 17:55:58 +09003647{
Tejun Heo5eb45c02006-04-02 18:51:52 +09003648 unsigned int class = dev->class;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003649 u16 *id = (void *)dev->link->ap->sector_buf;
Tejun Heo623a3122006-03-05 17:55:58 +09003650 int rc;
3651
Tejun Heofe635c72006-05-15 20:57:35 +09003652 /* read ID data */
Tejun Heobff04642006-11-10 18:08:10 +09003653 rc = ata_dev_read_id(dev, &class, readid_flags, id);
Tejun Heo623a3122006-03-05 17:55:58 +09003654 if (rc)
Tejun Heofe309112007-05-15 03:28:15 +09003655 return rc;
Tejun Heo623a3122006-03-05 17:55:58 +09003656
3657 /* is the device still there? */
Tejun Heofe309112007-05-15 03:28:15 +09003658 if (!ata_dev_same_device(dev, class, id))
3659 return -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09003660
Tejun Heofe635c72006-05-15 20:57:35 +09003661 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heofe309112007-05-15 03:28:15 +09003662 return 0;
3663}
3664
3665/**
3666 * ata_dev_revalidate - Revalidate ATA device
3667 * @dev: device to revalidate
Tejun Heo422c9da2007-09-23 13:14:12 +09003668 * @new_class: new class code
Tejun Heofe309112007-05-15 03:28:15 +09003669 * @readid_flags: read ID flags
3670 *
3671 * Re-read IDENTIFY page, make sure @dev is still attached to the
3672 * port and reconfigure it according to the new IDENTIFY page.
3673 *
3674 * LOCKING:
3675 * Kernel thread context (may sleep)
3676 *
3677 * RETURNS:
3678 * 0 on success, negative errno otherwise
3679 */
Tejun Heo422c9da2007-09-23 13:14:12 +09003680int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3681 unsigned int readid_flags)
Tejun Heofe309112007-05-15 03:28:15 +09003682{
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003683 u64 n_sectors = dev->n_sectors;
Tejun Heofe309112007-05-15 03:28:15 +09003684 int rc;
3685
3686 if (!ata_dev_enabled(dev))
3687 return -ENODEV;
3688
Tejun Heo422c9da2007-09-23 13:14:12 +09003689 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3690 if (ata_class_enabled(new_class) &&
3691 new_class != ATA_DEV_ATA && new_class != ATA_DEV_ATAPI) {
3692 ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
3693 dev->class, new_class);
3694 rc = -ENODEV;
3695 goto fail;
3696 }
3697
Tejun Heofe309112007-05-15 03:28:15 +09003698 /* re-read ID */
3699 rc = ata_dev_reread_id(dev, readid_flags);
3700 if (rc)
3701 goto fail;
Tejun Heo623a3122006-03-05 17:55:58 +09003702
3703 /* configure device according to the new ID */
Tejun Heoefdaedc2006-11-01 18:38:52 +09003704 rc = ata_dev_configure(dev);
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003705 if (rc)
3706 goto fail;
3707
3708 /* verify n_sectors hasn't changed */
Tejun Heob54eebd2007-08-17 18:46:51 +09003709 if (dev->class == ATA_DEV_ATA && n_sectors &&
3710 dev->n_sectors != n_sectors) {
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003711 ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
3712 "%llu != %llu\n",
3713 (unsigned long long)n_sectors,
3714 (unsigned long long)dev->n_sectors);
Tejun Heo8270bec2007-08-16 03:02:22 +09003715
3716 /* restore original n_sectors */
3717 dev->n_sectors = n_sectors;
3718
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003719 rc = -ENODEV;
3720 goto fail;
3721 }
3722
3723 return 0;
Tejun Heo623a3122006-03-05 17:55:58 +09003724
3725 fail:
Tejun Heof15a1da2006-05-15 20:57:56 +09003726 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
Tejun Heo623a3122006-03-05 17:55:58 +09003727 return rc;
3728}
3729
Alan Cox6919a0a2006-10-27 19:08:46 -07003730struct ata_blacklist_entry {
3731 const char *model_num;
3732 const char *model_rev;
3733 unsigned long horkage;
3734};
3735
3736static const struct ata_blacklist_entry ata_device_blacklist [] = {
3737 /* Devices with DMA related problems under Linux */
3738 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
3739 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
3740 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
3741 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
3742 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
3743 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
3744 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
3745 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
3746 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
3747 { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
3748 { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
3749 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
3750 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
3751 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
3752 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
3753 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
3754 { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
3755 { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
3756 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
3757 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
3758 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
3759 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
3760 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
3761 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
3762 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
3763 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003764 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
3765 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003766 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
Dave Jones39f19882007-05-21 14:31:03 -04003767 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
Tejun Heo3af9a772007-09-23 13:19:54 +09003768 /* Odd clown on sil3726/4726 PMPs */
3769 { "Config Disk", NULL, ATA_HORKAGE_NODMA |
3770 ATA_HORKAGE_SKIP_PM },
Alan Cox6919a0a2006-10-27 19:08:46 -07003771
Albert Lee18d6e9d2007-04-02 11:34:15 +08003772 /* Weird ATAPI devices */
Tejun Heo40a1d532007-06-27 02:49:38 +09003773 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
Albert Lee18d6e9d2007-04-02 11:34:15 +08003774
Alan Cox6919a0a2006-10-27 19:08:46 -07003775 /* Devices we expect to fail diagnostics */
3776
3777 /* Devices where NCQ should be avoided */
3778 /* NCQ is slow */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003779 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
Tejun Heo459ad682007-12-07 12:46:23 +09003780 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
Tejun Heo09125ea2007-02-28 15:21:23 +09003781 /* http://thread.gmane.org/gmane.linux.ide/14907 */
3782 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
Paul Rolland7acfaf32007-03-26 21:43:44 -08003783 /* NCQ is broken */
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003784 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
Alan Cox0e3dbc02007-09-20 15:22:47 +01003785 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
Paolo Ornatida6f0ec2007-10-04 11:06:56 +09003786 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
Tejun Heoe41bd3e2007-12-09 19:45:39 +09003787 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003788
Robert Hancock36e337d2007-04-02 22:05:29 -06003789 /* Blacklist entries taken from Silicon Image 3124/3132
3790 Windows driver .inf file - also several Linux problem reports */
3791 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
3792 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
3793 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
Alan Cox6919a0a2006-10-27 19:08:46 -07003794
Tejun Heo16c55b02007-08-29 11:58:33 +09003795 /* devices which puke on READ_NATIVE_MAX */
3796 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
3797 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
3798 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
3799 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003800
Alan Cox93328e12007-09-29 04:06:48 -04003801 /* Devices which report 1 sector over size HPA */
3802 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
3803 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
Mikko Rapelib152fcd2008-02-19 01:41:25 +01003804 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
Alan Cox93328e12007-09-29 04:06:48 -04003805
Alan Cox6bbfd532007-11-05 22:58:58 +00003806 /* Devices which get the IVB wrong */
3807 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
3808 { "TSSTcorp CDDVDW SH-S202J", "SB00", ATA_HORKAGE_IVB, },
Peter Missele9f33402007-11-27 18:04:42 +01003809 { "TSSTcorp CDDVDW SH-S202J", "SB01", ATA_HORKAGE_IVB, },
3810 { "TSSTcorp CDDVDW SH-S202N", "SB00", ATA_HORKAGE_IVB, },
3811 { "TSSTcorp CDDVDW SH-S202N", "SB01", ATA_HORKAGE_IVB, },
Alan Cox6bbfd532007-11-05 22:58:58 +00003812
Alan Cox6919a0a2006-10-27 19:08:46 -07003813 /* End Marker */
3814 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003816
Adrian Bunk741b7762007-10-24 18:23:06 +02003817static int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003818{
3819 const char *p;
3820 int len;
3821
3822 /*
3823 * check for trailing wildcard: *\0
3824 */
3825 p = strchr(patt, wildchar);
3826 if (p && ((*(p + 1)) == 0))
3827 len = p - patt;
Andrew Paprocki317b50b2007-10-15 15:43:12 -04003828 else {
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003829 len = strlen(name);
Andrew Paprocki317b50b2007-10-15 15:43:12 -04003830 if (!len) {
3831 if (!*patt)
3832 return 0;
3833 return -1;
3834 }
3835 }
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003836
3837 return strncmp(patt, name, len);
3838}
3839
Tejun Heo75683fe2007-07-05 13:31:27 +09003840static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841{
Tejun Heo8bfa79f2007-01-02 20:19:40 +09003842 unsigned char model_num[ATA_ID_PROD_LEN + 1];
3843 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
Alan Cox6919a0a2006-10-27 19:08:46 -07003844 const struct ata_blacklist_entry *ad = ata_device_blacklist;
Albert Lee3a778272006-06-22 13:00:25 +08003845
Tejun Heo8bfa79f2007-01-02 20:19:40 +09003846 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
3847 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
Alan Cox6919a0a2006-10-27 19:08:46 -07003849 while (ad->model_num) {
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003850 if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
Alan Cox6919a0a2006-10-27 19:08:46 -07003851 if (ad->model_rev == NULL)
3852 return ad->horkage;
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003853 if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
Alan Cox6919a0a2006-10-27 19:08:46 -07003854 return ad->horkage;
Alan Coxf4b15fe2006-03-22 15:54:04 +00003855 }
Alan Cox6919a0a2006-10-27 19:08:46 -07003856 ad++;
Alan Coxf4b15fe2006-03-22 15:54:04 +00003857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 return 0;
3859}
3860
Alan Cox6919a0a2006-10-27 19:08:46 -07003861static int ata_dma_blacklisted(const struct ata_device *dev)
3862{
3863 /* We don't support polling DMA.
3864 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
3865 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
3866 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003867 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
Alan Cox6919a0a2006-10-27 19:08:46 -07003868 (dev->flags & ATA_DFLAG_CDB_INTR))
3869 return 1;
Tejun Heo75683fe2007-07-05 13:31:27 +09003870 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07003871}
3872
Tejun Heoa6d5a512006-03-06 04:31:57 +09003873/**
Alan Cox6bbfd532007-11-05 22:58:58 +00003874 * ata_is_40wire - check drive side detection
3875 * @dev: device
3876 *
3877 * Perform drive side detection decoding, allowing for device vendors
3878 * who can't follow the documentation.
3879 */
3880
3881static int ata_is_40wire(struct ata_device *dev)
3882{
3883 if (dev->horkage & ATA_HORKAGE_IVB)
3884 return ata_drive_40wire_relaxed(dev->id);
3885 return ata_drive_40wire(dev->id);
3886}
3887
3888/**
Alan Cox15a55512008-03-28 14:33:46 -07003889 * cable_is_40wire - 40/80/SATA decider
3890 * @ap: port to consider
3891 *
3892 * This function encapsulates the policy for speed management
3893 * in one place. At the moment we don't cache the result but
3894 * there is a good case for setting ap->cbl to the result when
3895 * we are called with unknown cables (and figuring out if it
3896 * impacts hotplug at all).
3897 *
3898 * Return 1 if the cable appears to be 40 wire.
3899 */
3900
3901static int cable_is_40wire(struct ata_port *ap)
3902{
3903 struct ata_link *link;
3904 struct ata_device *dev;
3905
3906 /* If the controller thinks we are 40 wire, we are */
3907 if (ap->cbl == ATA_CBL_PATA40)
3908 return 1;
3909 /* If the controller thinks we are 80 wire, we are */
3910 if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
3911 return 0;
3912 /* If the controller doesn't know we scan
3913
3914 - Note: We look for all 40 wire detects at this point.
3915 Any 80 wire detect is taken to be 80 wire cable
3916 because
3917 - In many setups only the one drive (slave if present)
3918 will give a valid detect
3919 - If you have a non detect capable drive you don't
3920 want it to colour the choice
3921 */
3922 ata_port_for_each_link(link, ap) {
3923 ata_link_for_each_dev(dev, link) {
3924 if (!ata_is_40wire(dev))
3925 return 0;
3926 }
3927 }
3928 return 1;
3929}
3930
3931/**
Tejun Heoa6d5a512006-03-06 04:31:57 +09003932 * ata_dev_xfermask - Compute supported xfermask of the given device
Tejun Heoa6d5a512006-03-06 04:31:57 +09003933 * @dev: Device to compute xfermask for
3934 *
Tejun Heoacf356b2006-03-24 14:07:50 +09003935 * Compute supported xfermask of @dev and store it in
3936 * dev->*_mask. This function is responsible for applying all
3937 * known limits including host controller limits, device
3938 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09003939 *
3940 * LOCKING:
3941 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09003942 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003943static void ata_dev_xfermask(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003945 struct ata_link *link = dev->link;
3946 struct ata_port *ap = link->ap;
Jeff Garzikcca39742006-08-24 03:19:22 -04003947 struct ata_host *host = ap->host;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003948 unsigned long xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
Tejun Heo37deecb2006-08-10 16:59:07 +09003950 /* controller modes available */
Tejun Heo565083e2006-04-02 17:54:47 +09003951 xfer_mask = ata_pack_xfermask(ap->pio_mask,
3952 ap->mwdma_mask, ap->udma_mask);
3953
Robert Hancock8343f882007-03-06 02:37:51 -08003954 /* drive modes available */
Tejun Heo37deecb2006-08-10 16:59:07 +09003955 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
3956 dev->mwdma_mask, dev->udma_mask);
3957 xfer_mask &= ata_id_xfermask(dev->id);
Tejun Heo565083e2006-04-02 17:54:47 +09003958
Alan Coxb352e572006-08-10 18:52:12 +01003959 /*
3960 * CFA Advanced TrueIDE timings are not allowed on a shared
3961 * cable
3962 */
3963 if (ata_dev_pair(dev)) {
3964 /* No PIO5 or PIO6 */
3965 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
3966 /* No MWDMA3 or MWDMA 4 */
3967 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
3968 }
3969
Tejun Heo37deecb2006-08-10 16:59:07 +09003970 if (ata_dma_blacklisted(dev)) {
3971 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Tejun Heof15a1da2006-05-15 20:57:56 +09003972 ata_dev_printk(dev, KERN_WARNING,
3973 "device is on DMA blacklist, disabling DMA\n");
Tejun Heo37deecb2006-08-10 16:59:07 +09003974 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09003975
Petr Vandrovec14d66ab2007-03-08 10:12:12 +01003976 if ((host->flags & ATA_HOST_SIMPLEX) &&
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003977 host->simplex_claimed && host->simplex_claimed != ap) {
Tejun Heo37deecb2006-08-10 16:59:07 +09003978 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
3979 ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
3980 "other device, disabling DMA\n");
Alan Cox5444a6f2006-03-27 18:58:20 +01003981 }
Tejun Heo565083e2006-04-02 17:54:47 +09003982
Jeff Garzike4246752007-03-09 09:56:46 -05003983 if (ap->flags & ATA_FLAG_NO_IORDY)
3984 xfer_mask &= ata_pio_mask_no_iordy(dev);
3985
Alan Cox5444a6f2006-03-27 18:58:20 +01003986 if (ap->ops->mode_filter)
Alan Coxa76b62ca2007-03-09 09:34:07 -05003987 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01003988
Robert Hancock8343f882007-03-06 02:37:51 -08003989 /* Apply cable rule here. Don't apply it early because when
3990 * we handle hot plug the cable type can itself change.
3991 * Check this last so that we know if the transfer rate was
3992 * solely limited by the cable.
3993 * Unknown or 80 wire cables reported host side are checked
3994 * drive side as well. Cases where we know a 40wire cable
3995 * is used safely for 80 are not checked here.
3996 */
3997 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
3998 /* UDMA/44 or higher would be available */
Alan Cox15a55512008-03-28 14:33:46 -07003999 if (cable_is_40wire(ap)) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004000 ata_dev_printk(dev, KERN_WARNING,
Robert Hancock8343f882007-03-06 02:37:51 -08004001 "limited to UDMA/33 due to 40-wire cable\n");
4002 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4003 }
4004
Tejun Heo565083e2006-04-02 17:54:47 +09004005 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4006 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007}
4008
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 * @dev: Device to which command will be sent
4012 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004013 * Issue SET FEATURES - XFER MODE command to device @dev
4014 * on port @ap.
4015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004017 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09004018 *
4019 * RETURNS:
4020 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 */
4022
Tejun Heo3373efd2006-05-15 20:57:53 +09004023static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024{
Tejun Heoa0123702005-12-13 14:49:31 +09004025 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09004026 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
4028 /* set up set-features taskfile */
4029 DPRINTK("set features - xfer mode\n");
4030
Tejun Heo464cf172007-05-27 15:10:40 +02004031 /* Some controllers and ATAPI devices show flaky interrupt
4032 * behavior after setting xfer mode. Use polling instead.
4033 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004034 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004035 tf.command = ATA_CMD_SET_FEATURES;
4036 tf.feature = SETFEATURES_XFER;
Tejun Heo464cf172007-05-27 15:10:40 +02004037 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
Tejun Heoa0123702005-12-13 14:49:31 +09004038 tf.protocol = ATA_PROT_NODATA;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004039 /* If we are using IORDY we must send the mode setting command */
Jeff Garzik11b7bec2007-11-23 21:12:14 -05004040 if (ata_pio_need_iordy(dev))
4041 tf.nsect = dev->xfer_mode;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004042 /* If the device has IORDY and the controller does not - turn it off */
4043 else if (ata_id_has_iordy(dev->id))
Jeff Garzik11b7bec2007-11-23 21:12:14 -05004044 tf.nsect = 0x01;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004045 else /* In the ancient relic department - skip all of this */
4046 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
Tejun Heo2b789102007-10-09 15:05:44 +09004048 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049
Tejun Heo83206a22006-03-24 15:25:31 +09004050 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4051 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053/**
Jeff Garzik218f3d32007-10-25 00:33:27 -04004054 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004055 * @dev: Device to which command will be sent
4056 * @enable: Whether to enable or disable the feature
Jeff Garzik218f3d32007-10-25 00:33:27 -04004057 * @feature: The sector count represents the feature to set
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004058 *
4059 * Issue SET FEATURES - SATA FEATURES command to device @dev
Jeff Garzik218f3d32007-10-25 00:33:27 -04004060 * on port @ap with sector count
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004061 *
4062 * LOCKING:
4063 * PCI/etc. bus probe sem.
4064 *
4065 * RETURNS:
4066 * 0 on success, AC_ERR_* mask otherwise.
4067 */
Jeff Garzik218f3d32007-10-25 00:33:27 -04004068static unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable,
4069 u8 feature)
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004070{
4071 struct ata_taskfile tf;
4072 unsigned int err_mask;
4073
4074 /* set up set-features taskfile */
4075 DPRINTK("set features - SATA features\n");
4076
4077 ata_tf_init(dev, &tf);
4078 tf.command = ATA_CMD_SET_FEATURES;
4079 tf.feature = enable;
4080 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4081 tf.protocol = ATA_PROT_NODATA;
Jeff Garzik218f3d32007-10-25 00:33:27 -04004082 tf.nsect = feature;
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004083
Tejun Heo2b789102007-10-09 15:05:44 +09004084 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004085
4086 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4087 return err_mask;
4088}
4089
4090/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04004091 * ata_dev_init_params - Issue INIT DEV PARAMS command
Albert Lee8bf62ece2005-05-12 15:29:42 -04004092 * @dev: Device to which command will be sent
Randy Dunlape2a7f772006-05-18 10:50:18 -07004093 * @heads: Number of heads (taskfile parameter)
4094 * @sectors: Number of sectors (taskfile parameter)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004095 *
4096 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09004097 * Kernel thread context (may sleep)
4098 *
4099 * RETURNS:
4100 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ece2005-05-12 15:29:42 -04004101 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004102static unsigned int ata_dev_init_params(struct ata_device *dev,
4103 u16 heads, u16 sectors)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004104{
Tejun Heoa0123702005-12-13 14:49:31 +09004105 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09004106 unsigned int err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004107
4108 /* Number of sectors per track 1-255. Number of heads 1-16 */
4109 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08004110 return AC_ERR_INVALID;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004111
4112 /* set up init dev params taskfile */
4113 DPRINTK("init dev params \n");
4114
Tejun Heo3373efd2006-05-15 20:57:53 +09004115 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004116 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4117 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4118 tf.protocol = ATA_PROT_NODATA;
4119 tf.nsect = sectors;
4120 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04004121
Tejun Heo2b789102007-10-09 15:05:44 +09004122 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Alan Cox18b24662007-08-08 14:28:49 +01004123 /* A clean abort indicates an original or just out of spec drive
4124 and we should continue as we issue the setup based on the
4125 drive reported working geometry */
4126 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4127 err_mask = 0;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004128
Tejun Heo6aff8f12006-02-15 18:24:09 +09004129 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4130 return err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004131}
4132
4133/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004134 * ata_sg_clean - Unmap DMA memory associated with command
4135 * @qc: Command containing DMA memory to be released
4136 *
4137 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 *
4139 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004140 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 */
Tejun Heo70e6ad02006-11-14 22:47:10 +09004142void ata_sg_clean(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143{
4144 struct ata_port *ap = qc->ap;
Tejun Heoff2aeb12007-12-05 16:43:11 +09004145 struct scatterlist *sg = qc->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 int dir = qc->dma_dir;
4147
Tejun Heoa46314742006-02-11 19:11:13 +09004148 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
James Bottomleydde20202008-02-19 11:36:56 +01004150 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
James Bottomleydde20202008-02-19 11:36:56 +01004152 if (qc->n_elem)
4153 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
4155 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heoff2aeb12007-12-05 16:43:11 +09004156 qc->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157}
4158
4159/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
4161 * @qc: Metadata associated with taskfile to check
4162 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004163 * Allow low-level driver to filter ATA PACKET commands, returning
4164 * a status indicating whether or not it is OK to use DMA for the
4165 * supplied PACKET command.
4166 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004168 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004169 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 * RETURNS: 0 when ATAPI DMA can be used
4171 * nonzero otherwise
4172 */
4173int ata_check_atapi_dma(struct ata_queued_cmd *qc)
4174{
4175 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
Tejun Heob9a41972007-06-27 02:48:43 +09004177 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4178 * few ATAPI devices choke on such DMA requests.
4179 */
4180 if (unlikely(qc->nbytes & 15))
4181 return 1;
Albert Lee6f23a312007-04-02 11:39:25 +08004182
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 if (ap->ops->check_atapi_dma)
Tejun Heob9a41972007-06-27 02:48:43 +09004184 return ap->ops->check_atapi_dma(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185
Tejun Heob9a41972007-06-27 02:48:43 +09004186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187}
Tejun Heob9a41972007-06-27 02:48:43 +09004188
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189/**
Tejun Heo31cc23b2007-09-23 13:14:12 +09004190 * ata_std_qc_defer - Check whether a qc needs to be deferred
4191 * @qc: ATA command in question
4192 *
4193 * Non-NCQ commands cannot run with any other command, NCQ or
4194 * not. As upper layer only knows the queue depth, we are
4195 * responsible for maintaining exclusion. This function checks
4196 * whether a new command @qc can be issued.
4197 *
4198 * LOCKING:
4199 * spin_lock_irqsave(host lock)
4200 *
4201 * RETURNS:
4202 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4203 */
4204int ata_std_qc_defer(struct ata_queued_cmd *qc)
4205{
4206 struct ata_link *link = qc->dev->link;
4207
4208 if (qc->tf.protocol == ATA_PROT_NCQ) {
4209 if (!ata_tag_valid(link->active_tag))
4210 return 0;
4211 } else {
4212 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4213 return 0;
4214 }
4215
4216 return ATA_DEFER_LINK;
4217}
4218
Brian Kinge46834c2006-03-17 17:04:03 -06004219void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4220
Jeff Garzik0cba6322005-05-30 19:49:12 -04004221/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004222 * ata_sg_init - Associate command with scatter-gather table.
4223 * @qc: Command to be associated
4224 * @sg: Scatter-gather table.
4225 * @n_elem: Number of elements in s/g table.
4226 *
4227 * Initialize the data-related elements of queued_cmd @qc
4228 * to point to a scatter-gather table @sg, containing @n_elem
4229 * elements.
4230 *
4231 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004232 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4235 unsigned int n_elem)
4236{
Tejun Heoff2aeb12007-12-05 16:43:11 +09004237 qc->sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 qc->n_elem = n_elem;
Tejun Heoff2aeb12007-12-05 16:43:11 +09004239 qc->cursg = qc->sg;
4240}
4241
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004243 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4244 * @qc: Command with scatter-gather table to be mapped.
4245 *
4246 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 *
4248 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004249 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 *
4251 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004252 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 *
4254 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255static int ata_sg_setup(struct ata_queued_cmd *qc)
4256{
4257 struct ata_port *ap = qc->ap;
James Bottomleydde20202008-02-19 11:36:56 +01004258 unsigned int n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
Tejun Heo44877b42007-02-21 01:06:51 +09004260 VPRINTK("ENTER, ata%u\n", ap->print_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
James Bottomleydde20202008-02-19 11:36:56 +01004262 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4263 if (n_elem < 1)
4264 return -1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004265
James Bottomleydde20202008-02-19 11:36:56 +01004266 DPRINTK("%d sg elements mapped\n", n_elem);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004267
James Bottomleydde20202008-02-19 11:36:56 +01004268 qc->n_elem = n_elem;
Tejun Heof92a2632007-12-05 16:43:10 +09004269 qc->flags |= ATA_QCFLAG_DMAMAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270
4271 return 0;
4272}
4273
4274/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004275 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04004276 * @buf: Buffer to swap
4277 * @buf_words: Number of 16-bit words in buffer.
4278 *
4279 * Swap halves of 16-bit words if needed to convert from
4280 * little-endian byte order to native cpu byte order, or
4281 * vice-versa.
4282 *
4283 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004284 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004285 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286void swap_buf_le16(u16 *buf, unsigned int buf_words)
4287{
4288#ifdef __BIG_ENDIAN
4289 unsigned int i;
4290
4291 for (i = 0; i < buf_words; i++)
4292 buf[i] = le16_to_cpu(buf[i]);
4293#endif /* __BIG_ENDIAN */
4294}
4295
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004296/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 * ata_qc_new - Request an available ATA command, for queueing
4298 * @ap: Port associated with device @dev
4299 * @dev: Device from whom we request an available command structure
4300 *
4301 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004302 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 */
4304
4305static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
4306{
4307 struct ata_queued_cmd *qc = NULL;
4308 unsigned int i;
4309
Tejun Heoe3180492006-05-15 20:58:09 +09004310 /* no command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09004311 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
Tejun Heoe3180492006-05-15 20:58:09 +09004312 return NULL;
4313
Tejun Heo2ab7db12006-05-15 20:58:02 +09004314 /* the last tag is reserved for internal command. */
4315 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
Tejun Heo6cec4a32006-05-15 21:03:41 +09004316 if (!test_and_set_bit(i, &ap->qc_allocated)) {
Tejun Heof69499f2006-05-15 20:58:03 +09004317 qc = __ata_qc_from_tag(ap, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 break;
4319 }
4320
4321 if (qc)
4322 qc->tag = i;
4323
4324 return qc;
4325}
4326
4327/**
4328 * ata_qc_new_init - Request an available ATA command, and initialize it
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 * @dev: Device from whom we request an available command structure
4330 *
4331 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004332 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 */
4334
Tejun Heo3373efd2006-05-15 20:57:53 +09004335struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004337 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 struct ata_queued_cmd *qc;
4339
4340 qc = ata_qc_new(ap);
4341 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 qc->scsicmd = NULL;
4343 qc->ap = ap;
4344 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05004346 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 }
4348
4349 return qc;
4350}
4351
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352/**
4353 * ata_qc_free - free unused ata_queued_cmd
4354 * @qc: Command to complete
4355 *
4356 * Designed to free unused ata_queued_cmd object
4357 * in case something prevents using it.
4358 *
4359 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004360 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 */
4362void ata_qc_free(struct ata_queued_cmd *qc)
4363{
Tejun Heo4ba946e2006-01-23 13:09:36 +09004364 struct ata_port *ap = qc->ap;
4365 unsigned int tag;
4366
Tejun Heoa46314742006-02-11 19:11:13 +09004367 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
Tejun Heo4ba946e2006-01-23 13:09:36 +09004369 qc->flags = 0;
4370 tag = qc->tag;
4371 if (likely(ata_tag_valid(tag))) {
Tejun Heo4ba946e2006-01-23 13:09:36 +09004372 qc->tag = ATA_TAG_POISON;
Tejun Heo6cec4a32006-05-15 21:03:41 +09004373 clear_bit(tag, &ap->qc_allocated);
Tejun Heo4ba946e2006-01-23 13:09:36 +09004374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375}
4376
Tejun Heo76014422006-02-11 15:13:49 +09004377void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378{
Tejun Heodedaf2b2006-05-15 21:03:43 +09004379 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004380 struct ata_link *link = qc->dev->link;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004381
Tejun Heoa46314742006-02-11 19:11:13 +09004382 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4383 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
4385 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4386 ata_sg_clean(qc);
4387
Tejun Heo7401abf2006-05-15 20:57:32 +09004388 /* command should be marked inactive atomically with qc completion */
Tejun Heoda917d62007-09-23 13:14:12 +09004389 if (qc->tf.protocol == ATA_PROT_NCQ) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004390 link->sactive &= ~(1 << qc->tag);
Tejun Heoda917d62007-09-23 13:14:12 +09004391 if (!link->sactive)
4392 ap->nr_active_links--;
4393 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004394 link->active_tag = ATA_TAG_POISON;
Tejun Heoda917d62007-09-23 13:14:12 +09004395 ap->nr_active_links--;
4396 }
4397
4398 /* clear exclusive status */
4399 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4400 ap->excl_link == link))
4401 ap->excl_link = NULL;
Tejun Heo7401abf2006-05-15 20:57:32 +09004402
Albert Lee3f3791d2005-08-16 14:25:38 +08004403 /* atapi: mark qc as inactive to prevent the interrupt handler
4404 * from completing the command twice later, before the error handler
4405 * is called. (when rc != 0 and atapi request sense is needed)
4406 */
4407 qc->flags &= ~ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004408 ap->qc_active &= ~(1 << qc->tag);
Albert Lee3f3791d2005-08-16 14:25:38 +08004409
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09004411 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412}
4413
Tejun Heo39599a52006-11-14 22:37:35 +09004414static void fill_result_tf(struct ata_queued_cmd *qc)
4415{
4416 struct ata_port *ap = qc->ap;
4417
Tejun Heo39599a52006-11-14 22:37:35 +09004418 qc->result_tf.flags = qc->tf.flags;
Mark Lord4742d542007-04-02 16:20:35 -04004419 ap->ops->tf_read(ap, &qc->result_tf);
Tejun Heo39599a52006-11-14 22:37:35 +09004420}
4421
Tejun Heo00115e02007-11-27 19:28:58 +09004422static void ata_verify_xfer(struct ata_queued_cmd *qc)
4423{
4424 struct ata_device *dev = qc->dev;
4425
4426 if (ata_tag_internal(qc->tag))
4427 return;
4428
4429 if (ata_is_nodata(qc->tf.protocol))
4430 return;
4431
4432 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
4433 return;
4434
4435 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
4436}
4437
Tejun Heof686bcb2006-05-15 20:58:05 +09004438/**
4439 * ata_qc_complete - Complete an active ATA command
4440 * @qc: Command to complete
4441 * @err_mask: ATA Status register contents
4442 *
4443 * Indicate to the mid and upper layers that an ATA
4444 * command has completed, with either an ok or not-ok status.
4445 *
4446 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004447 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +09004448 */
4449void ata_qc_complete(struct ata_queued_cmd *qc)
4450{
4451 struct ata_port *ap = qc->ap;
4452
4453 /* XXX: New EH and old EH use different mechanisms to
4454 * synchronize EH with regular execution path.
4455 *
4456 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4457 * Normal execution path is responsible for not accessing a
4458 * failed qc. libata core enforces the rule by returning NULL
4459 * from ata_qc_from_tag() for failed qcs.
4460 *
4461 * Old EH depends on ata_qc_complete() nullifying completion
4462 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
4463 * not synchronize with interrupt handler. Only PIO task is
4464 * taken care of.
4465 */
4466 if (ap->ops->error_handler) {
Tejun Heo4dbfa392007-10-25 18:22:44 +09004467 struct ata_device *dev = qc->dev;
4468 struct ata_eh_info *ehi = &dev->link->eh_info;
4469
Tejun Heob51e9e52006-06-29 01:29:30 +09004470 WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
Tejun Heof686bcb2006-05-15 20:58:05 +09004471
4472 if (unlikely(qc->err_mask))
4473 qc->flags |= ATA_QCFLAG_FAILED;
4474
4475 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4476 if (!ata_tag_internal(qc->tag)) {
4477 /* always fill result TF for failed qc */
Tejun Heo39599a52006-11-14 22:37:35 +09004478 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09004479 ata_qc_schedule_eh(qc);
4480 return;
4481 }
4482 }
4483
4484 /* read result TF if requested */
4485 if (qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09004486 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09004487
Tejun Heo4dbfa392007-10-25 18:22:44 +09004488 /* Some commands need post-processing after successful
4489 * completion.
4490 */
4491 switch (qc->tf.command) {
4492 case ATA_CMD_SET_FEATURES:
4493 if (qc->tf.feature != SETFEATURES_WC_ON &&
4494 qc->tf.feature != SETFEATURES_WC_OFF)
4495 break;
4496 /* fall through */
4497 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
4498 case ATA_CMD_SET_MULTI: /* multi_count changed */
4499 /* revalidate device */
4500 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
4501 ata_port_schedule_eh(ap);
4502 break;
Tejun Heo054a5fb2007-10-25 18:30:36 +09004503
4504 case ATA_CMD_SLEEP:
4505 dev->flags |= ATA_DFLAG_SLEEPING;
4506 break;
Tejun Heo4dbfa392007-10-25 18:22:44 +09004507 }
4508
Tejun Heo00115e02007-11-27 19:28:58 +09004509 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
4510 ata_verify_xfer(qc);
4511
Tejun Heof686bcb2006-05-15 20:58:05 +09004512 __ata_qc_complete(qc);
4513 } else {
4514 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4515 return;
4516
4517 /* read result TF if failed or requested */
4518 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09004519 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09004520
4521 __ata_qc_complete(qc);
4522 }
4523}
4524
Tejun Heodedaf2b2006-05-15 21:03:43 +09004525/**
4526 * ata_qc_complete_multiple - Complete multiple qcs successfully
4527 * @ap: port in question
4528 * @qc_active: new qc_active mask
4529 * @finish_qc: LLDD callback invoked before completing a qc
4530 *
4531 * Complete in-flight commands. This functions is meant to be
4532 * called from low-level driver's interrupt routine to complete
4533 * requests normally. ap->qc_active and @qc_active is compared
4534 * and commands are completed accordingly.
4535 *
4536 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004537 * spin_lock_irqsave(host lock)
Tejun Heodedaf2b2006-05-15 21:03:43 +09004538 *
4539 * RETURNS:
4540 * Number of completed commands on success, -errno otherwise.
4541 */
4542int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
4543 void (*finish_qc)(struct ata_queued_cmd *))
4544{
4545 int nr_done = 0;
4546 u32 done_mask;
4547 int i;
4548
4549 done_mask = ap->qc_active ^ qc_active;
4550
4551 if (unlikely(done_mask & qc_active)) {
4552 ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
4553 "(%08x->%08x)\n", ap->qc_active, qc_active);
4554 return -EINVAL;
4555 }
4556
4557 for (i = 0; i < ATA_MAX_QUEUE; i++) {
4558 struct ata_queued_cmd *qc;
4559
4560 if (!(done_mask & (1 << i)))
4561 continue;
4562
4563 if ((qc = ata_qc_from_tag(ap, i))) {
4564 if (finish_qc)
4565 finish_qc(qc);
4566 ata_qc_complete(qc);
4567 nr_done++;
4568 }
4569 }
4570
4571 return nr_done;
4572}
4573
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574/**
4575 * ata_qc_issue - issue taskfile to device
4576 * @qc: command to issue to device
4577 *
4578 * Prepare an ATA command to submission to device.
4579 * This includes mapping the data into a DMA-able
4580 * area, filling in the S/G table, and finally
4581 * writing the taskfile to hardware, starting the command.
4582 *
4583 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004584 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09004586void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587{
4588 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004589 struct ata_link *link = qc->dev->link;
Tejun Heo405e66b2007-11-27 19:28:53 +09004590 u8 prot = qc->tf.protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
Tejun Heodedaf2b2006-05-15 21:03:43 +09004592 /* Make sure only one non-NCQ command is outstanding. The
4593 * check is skipped for old EH because it reuses active qc to
4594 * request ATAPI sense.
4595 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004596 WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
Tejun Heodedaf2b2006-05-15 21:03:43 +09004597
Tejun Heo1973a022007-12-05 10:36:13 +09004598 if (ata_is_ncq(prot)) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004599 WARN_ON(link->sactive & (1 << qc->tag));
Tejun Heoda917d62007-09-23 13:14:12 +09004600
4601 if (!link->sactive)
4602 ap->nr_active_links++;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004603 link->sactive |= 1 << qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004604 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004605 WARN_ON(link->sactive);
Tejun Heoda917d62007-09-23 13:14:12 +09004606
4607 ap->nr_active_links++;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004608 link->active_tag = qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004609 }
4610
Tejun Heoe4a70e72006-03-31 20:36:47 +09004611 qc->flags |= ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004612 ap->qc_active |= 1 << qc->tag;
Tejun Heoe4a70e72006-03-31 20:36:47 +09004613
Tejun Heof92a2632007-12-05 16:43:10 +09004614 /* We guarantee to LLDs that they will have at least one
4615 * non-zero sg if the command is a data command.
4616 */
Tejun Heoff2aeb12007-12-05 16:43:11 +09004617 BUG_ON(ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes));
Tejun Heof92a2632007-12-05 16:43:10 +09004618
Tejun Heo405e66b2007-11-27 19:28:53 +09004619 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
Tejun Heof92a2632007-12-05 16:43:10 +09004620 (ap->flags & ATA_FLAG_PIO_DMA)))
Tejun Heo001102d2007-12-05 16:43:09 +09004621 if (ata_sg_setup(qc))
4622 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
Tejun Heocf480622008-01-24 00:05:14 +09004624 /* if device is sleeping, schedule reset and abort the link */
Tejun Heo054a5fb2007-10-25 18:30:36 +09004625 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
Tejun Heocf480622008-01-24 00:05:14 +09004626 link->eh_info.action |= ATA_EH_RESET;
Tejun Heo054a5fb2007-10-25 18:30:36 +09004627 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
4628 ata_link_abort(link);
4629 return;
4630 }
4631
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 ap->ops->qc_prep(qc);
4633
Tejun Heo8e0e6942006-03-31 20:41:11 +09004634 qc->err_mask |= ap->ops->qc_issue(qc);
4635 if (unlikely(qc->err_mask))
4636 goto err;
4637 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638
Tejun Heo8e436af2006-01-23 13:09:36 +09004639sg_err:
Tejun Heo8e0e6942006-03-31 20:41:11 +09004640 qc->err_mask |= AC_ERR_SYSTEM;
4641err:
4642 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643}
4644
4645/**
Tejun Heo34bf2172006-05-15 20:57:46 +09004646 * sata_scr_valid - test whether SCRs are accessible
Tejun Heo936fd732007-08-06 18:36:23 +09004647 * @link: ATA link to test SCR accessibility for
Tejun Heo34bf2172006-05-15 20:57:46 +09004648 *
Tejun Heo936fd732007-08-06 18:36:23 +09004649 * Test whether SCRs are accessible for @link.
Tejun Heo34bf2172006-05-15 20:57:46 +09004650 *
4651 * LOCKING:
4652 * None.
4653 *
4654 * RETURNS:
4655 * 1 if SCRs are accessible, 0 otherwise.
4656 */
Tejun Heo936fd732007-08-06 18:36:23 +09004657int sata_scr_valid(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09004658{
Tejun Heo936fd732007-08-06 18:36:23 +09004659 struct ata_port *ap = link->ap;
4660
Tejun Heoa16abc02007-05-21 18:33:47 +02004661 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
Tejun Heo34bf2172006-05-15 20:57:46 +09004662}
4663
4664/**
4665 * sata_scr_read - read SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09004666 * @link: ATA link to read SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09004667 * @reg: SCR to read
4668 * @val: Place to store read value
4669 *
Tejun Heo936fd732007-08-06 18:36:23 +09004670 * Read SCR register @reg of @link into *@val. This function is
Tejun Heo633273a2007-09-23 13:19:54 +09004671 * guaranteed to succeed if @link is ap->link, the cable type of
4672 * the port is SATA and the port implements ->scr_read.
Tejun Heo34bf2172006-05-15 20:57:46 +09004673 *
4674 * LOCKING:
Tejun Heo633273a2007-09-23 13:19:54 +09004675 * None if @link is ap->link. Kernel thread context otherwise.
Tejun Heo34bf2172006-05-15 20:57:46 +09004676 *
4677 * RETURNS:
4678 * 0 on success, negative errno on failure.
4679 */
Tejun Heo936fd732007-08-06 18:36:23 +09004680int sata_scr_read(struct ata_link *link, int reg, u32 *val)
Tejun Heo34bf2172006-05-15 20:57:46 +09004681{
Tejun Heo633273a2007-09-23 13:19:54 +09004682 if (ata_is_host_link(link)) {
4683 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09004684
Tejun Heo633273a2007-09-23 13:19:54 +09004685 if (sata_scr_valid(link))
4686 return ap->ops->scr_read(ap, reg, val);
4687 return -EOPNOTSUPP;
4688 }
4689
4690 return sata_pmp_scr_read(link, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09004691}
4692
4693/**
4694 * sata_scr_write - write SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09004695 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09004696 * @reg: SCR to write
4697 * @val: value to write
4698 *
Tejun Heo936fd732007-08-06 18:36:23 +09004699 * Write @val to SCR register @reg of @link. This function is
Tejun Heo633273a2007-09-23 13:19:54 +09004700 * guaranteed to succeed if @link is ap->link, the cable type of
4701 * the port is SATA and the port implements ->scr_read.
Tejun Heo34bf2172006-05-15 20:57:46 +09004702 *
4703 * LOCKING:
Tejun Heo633273a2007-09-23 13:19:54 +09004704 * None if @link is ap->link. Kernel thread context otherwise.
Tejun Heo34bf2172006-05-15 20:57:46 +09004705 *
4706 * RETURNS:
4707 * 0 on success, negative errno on failure.
4708 */
Tejun Heo936fd732007-08-06 18:36:23 +09004709int sata_scr_write(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09004710{
Tejun Heo633273a2007-09-23 13:19:54 +09004711 if (ata_is_host_link(link)) {
4712 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09004713
Tejun Heo633273a2007-09-23 13:19:54 +09004714 if (sata_scr_valid(link))
4715 return ap->ops->scr_write(ap, reg, val);
4716 return -EOPNOTSUPP;
4717 }
4718
4719 return sata_pmp_scr_write(link, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09004720}
4721
4722/**
4723 * sata_scr_write_flush - write SCR register of the specified port and flush
Tejun Heo936fd732007-08-06 18:36:23 +09004724 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09004725 * @reg: SCR to write
4726 * @val: value to write
4727 *
4728 * This function is identical to sata_scr_write() except that this
4729 * function performs flush after writing to the register.
4730 *
4731 * LOCKING:
Tejun Heo633273a2007-09-23 13:19:54 +09004732 * None if @link is ap->link. Kernel thread context otherwise.
Tejun Heo34bf2172006-05-15 20:57:46 +09004733 *
4734 * RETURNS:
4735 * 0 on success, negative errno on failure.
4736 */
Tejun Heo936fd732007-08-06 18:36:23 +09004737int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09004738{
Tejun Heo633273a2007-09-23 13:19:54 +09004739 if (ata_is_host_link(link)) {
4740 struct ata_port *ap = link->ap;
4741 int rc;
Tejun Heoda3dbb12007-07-16 14:29:40 +09004742
Tejun Heo633273a2007-09-23 13:19:54 +09004743 if (sata_scr_valid(link)) {
4744 rc = ap->ops->scr_write(ap, reg, val);
4745 if (rc == 0)
4746 rc = ap->ops->scr_read(ap, reg, &val);
4747 return rc;
4748 }
4749 return -EOPNOTSUPP;
Tejun Heo34bf2172006-05-15 20:57:46 +09004750 }
Tejun Heo633273a2007-09-23 13:19:54 +09004751
4752 return sata_pmp_scr_write(link, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09004753}
4754
4755/**
Tejun Heo936fd732007-08-06 18:36:23 +09004756 * ata_link_online - test whether the given link is online
4757 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09004758 *
Tejun Heo936fd732007-08-06 18:36:23 +09004759 * Test whether @link is online. Note that this function returns
4760 * 0 if online status of @link cannot be obtained, so
4761 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09004762 *
4763 * LOCKING:
4764 * None.
4765 *
4766 * RETURNS:
4767 * 1 if the port online status is available and online.
4768 */
Tejun Heo936fd732007-08-06 18:36:23 +09004769int ata_link_online(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09004770{
4771 u32 sstatus;
4772
Tejun Heo936fd732007-08-06 18:36:23 +09004773 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
4774 (sstatus & 0xf) == 0x3)
Tejun Heo34bf2172006-05-15 20:57:46 +09004775 return 1;
4776 return 0;
4777}
4778
4779/**
Tejun Heo936fd732007-08-06 18:36:23 +09004780 * ata_link_offline - test whether the given link is offline
4781 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09004782 *
Tejun Heo936fd732007-08-06 18:36:23 +09004783 * Test whether @link is offline. Note that this function
4784 * returns 0 if offline status of @link cannot be obtained, so
4785 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09004786 *
4787 * LOCKING:
4788 * None.
4789 *
4790 * RETURNS:
4791 * 1 if the port offline status is available and offline.
4792 */
Tejun Heo936fd732007-08-06 18:36:23 +09004793int ata_link_offline(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09004794{
4795 u32 sstatus;
4796
Tejun Heo936fd732007-08-06 18:36:23 +09004797 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
4798 (sstatus & 0xf) != 0x3)
Tejun Heo34bf2172006-05-15 20:57:46 +09004799 return 1;
4800 return 0;
4801}
Edward Falk0baab862005-06-02 18:17:13 -04004802
Tejun Heo77b08fb2006-06-24 20:30:19 +09004803int ata_flush_cache(struct ata_device *dev)
Jens Axboe9b847542006-01-06 09:28:07 +01004804{
Tejun Heo977e6b92006-06-24 20:30:19 +09004805 unsigned int err_mask;
Jens Axboe9b847542006-01-06 09:28:07 +01004806 u8 cmd;
4807
4808 if (!ata_try_flush_cache(dev))
4809 return 0;
4810
Tejun Heo6fc49ad2006-11-11 20:10:45 +09004811 if (dev->flags & ATA_DFLAG_FLUSH_EXT)
Jens Axboe9b847542006-01-06 09:28:07 +01004812 cmd = ATA_CMD_FLUSH_EXT;
4813 else
4814 cmd = ATA_CMD_FLUSH;
4815
Alan Cox4f343372007-08-08 14:30:31 +01004816 /* This is wrong. On a failed flush we get back the LBA of the lost
4817 sector and we should (assuming it wasn't aborted as unknown) issue
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004818 a further flush command to continue the writeback until it
Alan Cox4f343372007-08-08 14:30:31 +01004819 does not error */
Tejun Heo977e6b92006-06-24 20:30:19 +09004820 err_mask = ata_do_simple_cmd(dev, cmd);
4821 if (err_mask) {
4822 ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
4823 return -EIO;
4824 }
4825
4826 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +01004827}
4828
Tejun Heo6ffa01d2007-03-02 17:32:47 +09004829#ifdef CONFIG_PM
Jeff Garzikcca39742006-08-24 03:19:22 -04004830static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
4831 unsigned int action, unsigned int ehi_flags,
4832 int wait)
Tejun Heo500530f2006-07-03 16:07:27 +09004833{
4834 unsigned long flags;
4835 int i, rc;
4836
Jeff Garzikcca39742006-08-24 03:19:22 -04004837 for (i = 0; i < host->n_ports; i++) {
4838 struct ata_port *ap = host->ports[i];
Tejun Heoe3667eb2007-08-06 18:36:24 +09004839 struct ata_link *link;
Tejun Heo500530f2006-07-03 16:07:27 +09004840
4841 /* Previous resume operation might still be in
4842 * progress. Wait for PM_PENDING to clear.
4843 */
4844 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
4845 ata_port_wait_eh(ap);
4846 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
4847 }
4848
4849 /* request PM ops to EH */
4850 spin_lock_irqsave(ap->lock, flags);
4851
4852 ap->pm_mesg = mesg;
4853 if (wait) {
4854 rc = 0;
4855 ap->pm_result = &rc;
4856 }
4857
4858 ap->pflags |= ATA_PFLAG_PM_PENDING;
Tejun Heoe3667eb2007-08-06 18:36:24 +09004859 __ata_port_for_each_link(link, ap) {
4860 link->eh_info.action |= action;
4861 link->eh_info.flags |= ehi_flags;
4862 }
Tejun Heo500530f2006-07-03 16:07:27 +09004863
4864 ata_port_schedule_eh(ap);
4865
4866 spin_unlock_irqrestore(ap->lock, flags);
4867
4868 /* wait and check result */
4869 if (wait) {
4870 ata_port_wait_eh(ap);
4871 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
4872 if (rc)
4873 return rc;
4874 }
4875 }
4876
4877 return 0;
4878}
4879
4880/**
Jeff Garzikcca39742006-08-24 03:19:22 -04004881 * ata_host_suspend - suspend host
4882 * @host: host to suspend
Tejun Heo500530f2006-07-03 16:07:27 +09004883 * @mesg: PM message
4884 *
Jeff Garzikcca39742006-08-24 03:19:22 -04004885 * Suspend @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09004886 * function requests EH to perform PM operations and waits for EH
4887 * to finish.
4888 *
4889 * LOCKING:
4890 * Kernel thread context (may sleep).
4891 *
4892 * RETURNS:
4893 * 0 on success, -errno on failure.
4894 */
Jeff Garzikcca39742006-08-24 03:19:22 -04004895int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09004896{
Tejun Heo9666f402007-05-04 21:27:47 +02004897 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09004898
Kristen Carlson Accardica773292007-10-25 00:58:59 -04004899 /*
4900 * disable link pm on all ports before requesting
4901 * any pm activity
4902 */
4903 ata_lpm_enable(host);
4904
Jeff Garzikcca39742006-08-24 03:19:22 -04004905 rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
Jeff Garzik72ad6ec2008-02-25 17:31:10 -05004906 if (rc == 0)
4907 host->dev->power.power_state = mesg;
Tejun Heo500530f2006-07-03 16:07:27 +09004908 return rc;
4909}
4910
4911/**
Jeff Garzikcca39742006-08-24 03:19:22 -04004912 * ata_host_resume - resume host
4913 * @host: host to resume
Tejun Heo500530f2006-07-03 16:07:27 +09004914 *
Jeff Garzikcca39742006-08-24 03:19:22 -04004915 * Resume @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09004916 * function requests EH to perform PM operations and returns.
4917 * Note that all resume operations are performed parallely.
4918 *
4919 * LOCKING:
4920 * Kernel thread context (may sleep).
4921 */
Jeff Garzikcca39742006-08-24 03:19:22 -04004922void ata_host_resume(struct ata_host *host)
Tejun Heo500530f2006-07-03 16:07:27 +09004923{
Tejun Heocf480622008-01-24 00:05:14 +09004924 ata_host_request_pm(host, PMSG_ON, ATA_EH_RESET,
Jeff Garzikcca39742006-08-24 03:19:22 -04004925 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
Jeff Garzik72ad6ec2008-02-25 17:31:10 -05004926 host->dev->power.power_state = PMSG_ON;
Kristen Carlson Accardica773292007-10-25 00:58:59 -04004927
4928 /* reenable link pm */
4929 ata_lpm_disable(host);
Tejun Heo500530f2006-07-03 16:07:27 +09004930}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09004931#endif
Tejun Heo500530f2006-07-03 16:07:27 +09004932
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004933/**
4934 * ata_port_start - Set port up for dma.
4935 * @ap: Port to initialize
4936 *
4937 * Called just after data structures for each port are
4938 * initialized. Allocates space for PRD table.
4939 *
4940 * May be used as the port_start() entry in ata_port_operations.
4941 *
4942 * LOCKING:
4943 * Inherited from caller.
4944 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09004945int ata_port_start(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946{
Brian King2f1f6102006-03-23 17:30:15 -06004947 struct device *dev = ap->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
Tejun Heof0d36ef2007-01-20 16:00:28 +09004949 ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
4950 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 if (!ap->prd)
4952 return -ENOMEM;
4953
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 return 0;
4955}
4956
Edward Falk0baab862005-06-02 18:17:13 -04004957/**
Tejun Heo3ef3b432006-05-31 18:27:30 +09004958 * ata_dev_init - Initialize an ata_device structure
4959 * @dev: Device structure to initialize
4960 *
4961 * Initialize @dev in preparation for probing.
4962 *
4963 * LOCKING:
4964 * Inherited from caller.
4965 */
4966void ata_dev_init(struct ata_device *dev)
4967{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004968 struct ata_link *link = dev->link;
4969 struct ata_port *ap = link->ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09004970 unsigned long flags;
Tejun Heo3ef3b432006-05-31 18:27:30 +09004971
Tejun Heo5a04bf42006-05-31 18:27:38 +09004972 /* SATA spd limit is bound to the first device */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004973 link->sata_spd_limit = link->hw_sata_spd_limit;
4974 link->sata_spd = 0;
Tejun Heo5a04bf42006-05-31 18:27:38 +09004975
Tejun Heo72fa4b72006-05-31 18:27:32 +09004976 /* High bits of dev->flags are used to record warm plug
4977 * requests which occur asynchronously. Synchronize using
Jeff Garzikcca39742006-08-24 03:19:22 -04004978 * host lock.
Tejun Heo72fa4b72006-05-31 18:27:32 +09004979 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04004980 spin_lock_irqsave(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09004981 dev->flags &= ~ATA_DFLAG_INIT_MASK;
Tejun Heo3dcc3232007-09-03 12:20:11 +09004982 dev->horkage = 0;
Jeff Garzikba6a1302006-06-22 23:46:10 -04004983 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09004984
4985 memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
4986 sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
Tejun Heo3ef3b432006-05-31 18:27:30 +09004987 dev->pio_mask = UINT_MAX;
4988 dev->mwdma_mask = UINT_MAX;
4989 dev->udma_mask = UINT_MAX;
4990}
4991
4992/**
Tejun Heo4fb37a22007-08-06 18:36:23 +09004993 * ata_link_init - Initialize an ata_link structure
4994 * @ap: ATA port link is attached to
4995 * @link: Link structure to initialize
Tejun Heo89898052007-08-06 18:36:23 +09004996 * @pmp: Port multiplier port number
Tejun Heo4fb37a22007-08-06 18:36:23 +09004997 *
4998 * Initialize @link.
4999 *
5000 * LOCKING:
5001 * Kernel thread context (may sleep)
5002 */
Tejun Heofb7fd612007-09-23 13:14:12 +09005003void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
Tejun Heo4fb37a22007-08-06 18:36:23 +09005004{
5005 int i;
5006
5007 /* clear everything except for devices */
5008 memset(link, 0, offsetof(struct ata_link, device[0]));
5009
5010 link->ap = ap;
Tejun Heo89898052007-08-06 18:36:23 +09005011 link->pmp = pmp;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005012 link->active_tag = ATA_TAG_POISON;
5013 link->hw_sata_spd_limit = UINT_MAX;
5014
5015 /* can't use iterator, ap isn't initialized yet */
5016 for (i = 0; i < ATA_MAX_DEVICES; i++) {
5017 struct ata_device *dev = &link->device[i];
5018
5019 dev->link = link;
5020 dev->devno = dev - link->device;
5021 ata_dev_init(dev);
5022 }
5023}
5024
5025/**
5026 * sata_link_init_spd - Initialize link->sata_spd_limit
5027 * @link: Link to configure sata_spd_limit for
5028 *
5029 * Initialize @link->[hw_]sata_spd_limit to the currently
5030 * configured value.
5031 *
5032 * LOCKING:
5033 * Kernel thread context (may sleep).
5034 *
5035 * RETURNS:
5036 * 0 on success, -errno on failure.
5037 */
Tejun Heofb7fd612007-09-23 13:14:12 +09005038int sata_link_init_spd(struct ata_link *link)
Tejun Heo4fb37a22007-08-06 18:36:23 +09005039{
Tejun Heo33267322008-02-13 09:15:09 +09005040 u32 scontrol;
5041 u8 spd;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005042 int rc;
5043
5044 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
5045 if (rc)
5046 return rc;
5047
5048 spd = (scontrol >> 4) & 0xf;
5049 if (spd)
5050 link->hw_sata_spd_limit &= (1 << spd) - 1;
5051
Tejun Heo33267322008-02-13 09:15:09 +09005052 ata_force_spd_limit(link);
5053
Tejun Heo4fb37a22007-08-06 18:36:23 +09005054 link->sata_spd_limit = link->hw_sata_spd_limit;
5055
5056 return 0;
5057}
5058
5059/**
Tejun Heof3187192007-04-17 23:44:07 +09005060 * ata_port_alloc - allocate and initialize basic ATA port resources
5061 * @host: ATA host this allocated port belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 *
Tejun Heof3187192007-04-17 23:44:07 +09005063 * Allocate and initialize basic ATA port resources.
5064 *
5065 * RETURNS:
5066 * Allocate ATA port on success, NULL on failure.
Jeff Garzik0cba6322005-05-30 19:49:12 -04005067 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09005069 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 */
Tejun Heof3187192007-04-17 23:44:07 +09005071struct ata_port *ata_port_alloc(struct ata_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072{
Tejun Heof3187192007-04-17 23:44:07 +09005073 struct ata_port *ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074
Tejun Heof3187192007-04-17 23:44:07 +09005075 DPRINTK("ENTER\n");
5076
5077 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5078 if (!ap)
5079 return NULL;
5080
Tejun Heof4d6d002007-05-01 11:50:15 +02005081 ap->pflags |= ATA_PFLAG_INITIALIZING;
Jeff Garzikcca39742006-08-24 03:19:22 -04005082 ap->lock = &host->lock;
Tejun Heo198e0fe2006-04-02 18:51:52 +09005083 ap->flags = ATA_FLAG_DISABLED;
Tejun Heof3187192007-04-17 23:44:07 +09005084 ap->print_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 ap->ctl = ATA_DEVCTL_OBS;
Jeff Garzikcca39742006-08-24 03:19:22 -04005086 ap->host = host;
Tejun Heof3187192007-04-17 23:44:07 +09005087 ap->dev = host->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 ap->last_ctl = 0xFF;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005089
5090#if defined(ATA_VERBOSE_DEBUG)
5091 /* turn on all debugging levels */
5092 ap->msg_enable = 0x00FF;
5093#elif defined(ATA_DEBUG)
5094 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 +09005095#else
Borislav Petkov0dd4b212006-06-23 02:29:08 -04005096 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005097#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098
Jeff Garzik442eacc2007-12-19 04:25:10 -05005099 INIT_DELAYED_WORK(&ap->port_task, ata_pio_task);
David Howells65f27f32006-11-22 14:55:48 +00005100 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
5101 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005102 INIT_LIST_HEAD(&ap->eh_done_q);
Tejun Heoc6cf9e92006-05-31 18:27:27 +09005103 init_waitqueue_head(&ap->eh_wait_q);
Tejun Heo5ddf24c2007-07-16 14:29:41 +09005104 init_timer_deferrable(&ap->fastdrain_timer);
5105 ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
5106 ap->fastdrain_timer.data = (unsigned long)ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
Tejun Heo838df622006-05-15 20:57:44 +09005108 ap->cbl = ATA_CBL_NONE;
Tejun Heo838df622006-05-15 20:57:44 +09005109
Tejun Heo89898052007-08-06 18:36:23 +09005110 ata_link_init(ap, &ap->link, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111
5112#ifdef ATA_IRQ_TRAP
5113 ap->stats.unhandled_irq = 1;
5114 ap->stats.idle_irq = 1;
5115#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 return ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117}
5118
Tejun Heof0d36ef2007-01-20 16:00:28 +09005119static void ata_host_release(struct device *gendev, void *res)
5120{
5121 struct ata_host *host = dev_get_drvdata(gendev);
5122 int i;
5123
5124 for (i = 0; i < host->n_ports; i++) {
5125 struct ata_port *ap = host->ports[i];
5126
Tejun Heoecef7252007-04-17 23:44:06 +09005127 if (!ap)
5128 continue;
5129
Tejun Heo49114872007-04-17 23:44:06 +09005130 if (ap->scsi_host)
Tejun Heo1aa506e2007-03-09 19:36:12 +09005131 scsi_host_put(ap->scsi_host);
5132
Tejun Heo633273a2007-09-23 13:19:54 +09005133 kfree(ap->pmp_link);
Tejun Heo49114872007-04-17 23:44:06 +09005134 kfree(ap);
Tejun Heo1aa506e2007-03-09 19:36:12 +09005135 host->ports[i] = NULL;
5136 }
5137
Tejun Heo1aa56cc2007-02-27 22:33:21 +09005138 dev_set_drvdata(gendev, NULL);
Tejun Heof0d36ef2007-01-20 16:00:28 +09005139}
5140
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141/**
Tejun Heof3187192007-04-17 23:44:07 +09005142 * ata_host_alloc - allocate and init basic ATA host resources
5143 * @dev: generic device this host is associated with
5144 * @max_ports: maximum number of ATA ports associated with this host
5145 *
5146 * Allocate and initialize basic ATA host resources. LLD calls
5147 * this function to allocate a host, initializes it fully and
5148 * attaches it using ata_host_register().
5149 *
5150 * @max_ports ports are allocated and host->n_ports is
5151 * initialized to @max_ports. The caller is allowed to decrease
5152 * host->n_ports before calling ata_host_register(). The unused
5153 * ports will be automatically freed on registration.
5154 *
5155 * RETURNS:
5156 * Allocate ATA host on success, NULL on failure.
5157 *
5158 * LOCKING:
5159 * Inherited from calling layer (may sleep).
5160 */
5161struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5162{
5163 struct ata_host *host;
5164 size_t sz;
5165 int i;
5166
5167 DPRINTK("ENTER\n");
5168
5169 if (!devres_open_group(dev, NULL, GFP_KERNEL))
5170 return NULL;
5171
5172 /* alloc a container for our list of ATA ports (buses) */
5173 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
5174 /* alloc a container for our list of ATA ports (buses) */
5175 host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
5176 if (!host)
5177 goto err_out;
5178
5179 devres_add(dev, host);
5180 dev_set_drvdata(dev, host);
5181
5182 spin_lock_init(&host->lock);
5183 host->dev = dev;
5184 host->n_ports = max_ports;
5185
5186 /* allocate ports bound to this host */
5187 for (i = 0; i < max_ports; i++) {
5188 struct ata_port *ap;
5189
5190 ap = ata_port_alloc(host);
5191 if (!ap)
5192 goto err_out;
5193
5194 ap->port_no = i;
5195 host->ports[i] = ap;
5196 }
5197
5198 devres_remove_group(dev, NULL);
5199 return host;
5200
5201 err_out:
5202 devres_release_group(dev, NULL);
5203 return NULL;
5204}
5205
5206/**
Tejun Heof5cda252007-04-17 23:44:07 +09005207 * ata_host_alloc_pinfo - alloc host and init with port_info array
5208 * @dev: generic device this host is associated with
5209 * @ppi: array of ATA port_info to initialize host with
5210 * @n_ports: number of ATA ports attached to this host
5211 *
5212 * Allocate ATA host and initialize with info from @ppi. If NULL
5213 * terminated, @ppi may contain fewer entries than @n_ports. The
5214 * last entry will be used for the remaining ports.
5215 *
5216 * RETURNS:
5217 * Allocate ATA host on success, NULL on failure.
5218 *
5219 * LOCKING:
5220 * Inherited from calling layer (may sleep).
5221 */
5222struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5223 const struct ata_port_info * const * ppi,
5224 int n_ports)
5225{
5226 const struct ata_port_info *pi;
5227 struct ata_host *host;
5228 int i, j;
5229
5230 host = ata_host_alloc(dev, n_ports);
5231 if (!host)
5232 return NULL;
5233
5234 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5235 struct ata_port *ap = host->ports[i];
5236
5237 if (ppi[j])
5238 pi = ppi[j++];
5239
5240 ap->pio_mask = pi->pio_mask;
5241 ap->mwdma_mask = pi->mwdma_mask;
5242 ap->udma_mask = pi->udma_mask;
5243 ap->flags |= pi->flags;
Tejun Heo0c887582007-08-06 18:36:23 +09005244 ap->link.flags |= pi->link_flags;
Tejun Heof5cda252007-04-17 23:44:07 +09005245 ap->ops = pi->port_ops;
5246
5247 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5248 host->ops = pi->port_ops;
Tejun Heof5cda252007-04-17 23:44:07 +09005249 }
5250
5251 return host;
5252}
5253
Tejun Heo32ebbc02007-11-08 13:09:00 +09005254static void ata_host_stop(struct device *gendev, void *res)
5255{
5256 struct ata_host *host = dev_get_drvdata(gendev);
5257 int i;
5258
5259 WARN_ON(!(host->flags & ATA_HOST_STARTED));
5260
5261 for (i = 0; i < host->n_ports; i++) {
5262 struct ata_port *ap = host->ports[i];
5263
5264 if (ap->ops->port_stop)
5265 ap->ops->port_stop(ap);
5266 }
5267
5268 if (host->ops->host_stop)
5269 host->ops->host_stop(host);
5270}
5271
Tejun Heof5cda252007-04-17 23:44:07 +09005272/**
Tejun Heo029cfd62008-03-25 12:22:49 +09005273 * ata_finalize_port_ops - finalize ata_port_operations
5274 * @ops: ata_port_operations to finalize
5275 *
5276 * An ata_port_operations can inherit from another ops and that
5277 * ops can again inherit from another. This can go on as many
5278 * times as necessary as long as there is no loop in the
5279 * inheritance chain.
5280 *
5281 * Ops tables are finalized when the host is started. NULL or
5282 * unspecified entries are inherited from the closet ancestor
5283 * which has the method and the entry is populated with it.
5284 * After finalization, the ops table directly points to all the
5285 * methods and ->inherits is no longer necessary and cleared.
5286 *
5287 * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5288 *
5289 * LOCKING:
5290 * None.
5291 */
5292static void ata_finalize_port_ops(struct ata_port_operations *ops)
5293{
5294 static spinlock_t lock = SPIN_LOCK_UNLOCKED;
5295 const struct ata_port_operations *cur;
5296 void **begin = (void **)ops;
5297 void **end = (void **)&ops->inherits;
5298 void **pp;
5299
5300 if (!ops || !ops->inherits)
5301 return;
5302
5303 spin_lock(&lock);
5304
5305 for (cur = ops->inherits; cur; cur = cur->inherits) {
5306 void **inherit = (void **)cur;
5307
5308 for (pp = begin; pp < end; pp++, inherit++)
5309 if (!*pp)
5310 *pp = *inherit;
5311 }
5312
5313 for (pp = begin; pp < end; pp++)
5314 if (IS_ERR(*pp))
5315 *pp = NULL;
5316
5317 ops->inherits = NULL;
5318
5319 spin_unlock(&lock);
5320}
5321
5322/**
Tejun Heoecef7252007-04-17 23:44:06 +09005323 * ata_host_start - start and freeze ports of an ATA host
5324 * @host: ATA host to start ports for
5325 *
5326 * Start and then freeze ports of @host. Started status is
5327 * recorded in host->flags, so this function can be called
5328 * multiple times. Ports are guaranteed to get started only
Tejun Heof3187192007-04-17 23:44:07 +09005329 * once. If host->ops isn't initialized yet, its set to the
5330 * first non-dummy port ops.
Tejun Heoecef7252007-04-17 23:44:06 +09005331 *
5332 * LOCKING:
5333 * Inherited from calling layer (may sleep).
5334 *
5335 * RETURNS:
5336 * 0 if all ports are started successfully, -errno otherwise.
5337 */
5338int ata_host_start(struct ata_host *host)
5339{
Tejun Heo32ebbc02007-11-08 13:09:00 +09005340 int have_stop = 0;
5341 void *start_dr = NULL;
Tejun Heoecef7252007-04-17 23:44:06 +09005342 int i, rc;
5343
5344 if (host->flags & ATA_HOST_STARTED)
5345 return 0;
5346
Tejun Heo029cfd62008-03-25 12:22:49 +09005347 ata_finalize_port_ops(host->ops);
5348
Tejun Heoecef7252007-04-17 23:44:06 +09005349 for (i = 0; i < host->n_ports; i++) {
5350 struct ata_port *ap = host->ports[i];
5351
Tejun Heo029cfd62008-03-25 12:22:49 +09005352 ata_finalize_port_ops(ap->ops);
5353
Tejun Heof3187192007-04-17 23:44:07 +09005354 if (!host->ops && !ata_port_is_dummy(ap))
5355 host->ops = ap->ops;
5356
Tejun Heo32ebbc02007-11-08 13:09:00 +09005357 if (ap->ops->port_stop)
5358 have_stop = 1;
5359 }
5360
5361 if (host->ops->host_stop)
5362 have_stop = 1;
5363
5364 if (have_stop) {
5365 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
5366 if (!start_dr)
5367 return -ENOMEM;
5368 }
5369
5370 for (i = 0; i < host->n_ports; i++) {
5371 struct ata_port *ap = host->ports[i];
5372
Tejun Heoecef7252007-04-17 23:44:06 +09005373 if (ap->ops->port_start) {
5374 rc = ap->ops->port_start(ap);
5375 if (rc) {
Alan Cox0f9fe9b2007-11-30 15:23:16 +00005376 if (rc != -ENODEV)
Andrew Morton0f757742008-01-10 14:33:09 -08005377 dev_printk(KERN_ERR, host->dev,
5378 "failed to start port %d "
5379 "(errno=%d)\n", i, rc);
Tejun Heoecef7252007-04-17 23:44:06 +09005380 goto err_out;
5381 }
5382 }
Tejun Heoecef7252007-04-17 23:44:06 +09005383 ata_eh_freeze_port(ap);
5384 }
5385
Tejun Heo32ebbc02007-11-08 13:09:00 +09005386 if (start_dr)
5387 devres_add(host->dev, start_dr);
Tejun Heoecef7252007-04-17 23:44:06 +09005388 host->flags |= ATA_HOST_STARTED;
5389 return 0;
5390
5391 err_out:
5392 while (--i >= 0) {
5393 struct ata_port *ap = host->ports[i];
5394
5395 if (ap->ops->port_stop)
5396 ap->ops->port_stop(ap);
5397 }
Tejun Heo32ebbc02007-11-08 13:09:00 +09005398 devres_free(start_dr);
Tejun Heoecef7252007-04-17 23:44:06 +09005399 return rc;
5400}
5401
5402/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005403 * ata_sas_host_init - Initialize a host struct
5404 * @host: host to initialize
5405 * @dev: device host is attached to
5406 * @flags: host flags
5407 * @ops: port_ops
Brian Kingb03732f2006-08-07 14:27:10 -05005408 *
5409 * LOCKING:
5410 * PCI/etc. bus probe sem.
5411 *
5412 */
Tejun Heof3187192007-04-17 23:44:07 +09005413/* KILLME - the only user left is ipr */
Jeff Garzikcca39742006-08-24 03:19:22 -04005414void ata_host_init(struct ata_host *host, struct device *dev,
Tejun Heo029cfd62008-03-25 12:22:49 +09005415 unsigned long flags, struct ata_port_operations *ops)
Brian Kingb03732f2006-08-07 14:27:10 -05005416{
Jeff Garzikcca39742006-08-24 03:19:22 -04005417 spin_lock_init(&host->lock);
5418 host->dev = dev;
5419 host->flags = flags;
5420 host->ops = ops;
Brian Kingb03732f2006-08-07 14:27:10 -05005421}
5422
5423/**
Tejun Heof3187192007-04-17 23:44:07 +09005424 * ata_host_register - register initialized ATA host
5425 * @host: ATA host to register
5426 * @sht: template for SCSI host
Jeff Garzik0cba6322005-05-30 19:49:12 -04005427 *
Tejun Heof3187192007-04-17 23:44:07 +09005428 * Register initialized ATA host. @host is allocated using
5429 * ata_host_alloc() and fully initialized by LLD. This function
5430 * starts ports, registers @host with ATA and SCSI layers and
5431 * probe registered devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 *
5433 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09005434 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 *
5436 * RETURNS:
Tejun Heof3187192007-04-17 23:44:07 +09005437 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 */
Tejun Heof3187192007-04-17 23:44:07 +09005439int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440{
Tejun Heof3187192007-04-17 23:44:07 +09005441 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
Tejun Heof3187192007-04-17 23:44:07 +09005443 /* host must have been started */
5444 if (!(host->flags & ATA_HOST_STARTED)) {
5445 dev_printk(KERN_ERR, host->dev,
5446 "BUG: trying to register unstarted host\n");
5447 WARN_ON(1);
5448 return -EINVAL;
Alan Cox02f076a2006-09-26 17:35:32 +01005449 }
Tejun Heof0d36ef2007-01-20 16:00:28 +09005450
Tejun Heof3187192007-04-17 23:44:07 +09005451 /* Blow away unused ports. This happens when LLD can't
5452 * determine the exact number of ports to allocate at
5453 * allocation time.
5454 */
5455 for (i = host->n_ports; host->ports[i]; i++)
5456 kfree(host->ports[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457
Tejun Heof3187192007-04-17 23:44:07 +09005458 /* give ports names and add SCSI hosts */
5459 for (i = 0; i < host->n_ports; i++)
5460 host->ports[i]->print_id = ata_print_id++;
Tejun Heof0d36ef2007-01-20 16:00:28 +09005461
Tejun Heof3187192007-04-17 23:44:07 +09005462 rc = ata_scsi_add_hosts(host, sht);
Tejun Heoecef7252007-04-17 23:44:06 +09005463 if (rc)
Tejun Heof3187192007-04-17 23:44:07 +09005464 return rc;
Tejun Heoecef7252007-04-17 23:44:06 +09005465
Tejun Heofafbae82007-05-15 03:28:16 +09005466 /* associate with ACPI nodes */
5467 ata_acpi_associate(host);
5468
Tejun Heof3187192007-04-17 23:44:07 +09005469 /* set cable, sata_spd_limit and report */
Jeff Garzikcca39742006-08-24 03:19:22 -04005470 for (i = 0; i < host->n_ports; i++) {
5471 struct ata_port *ap = host->ports[i];
Tejun Heof3187192007-04-17 23:44:07 +09005472 unsigned long xfer_mask;
5473
5474 /* set SATA cable type if still unset */
5475 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
5476 ap->cbl = ATA_CBL_SATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477
Tejun Heo5a04bf42006-05-31 18:27:38 +09005478 /* init sata_spd_limit to the current value */
Tejun Heo4fb37a22007-08-06 18:36:23 +09005479 sata_link_init_spd(&ap->link);
Tejun Heo5a04bf42006-05-31 18:27:38 +09005480
Tejun Heocbcdd872007-08-18 13:14:55 +09005481 /* print per-port info to dmesg */
Tejun Heof3187192007-04-17 23:44:07 +09005482 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
5483 ap->udma_mask);
5484
Tejun Heoabf6e8e2007-10-09 14:57:25 +09005485 if (!ata_port_is_dummy(ap)) {
Tejun Heocbcdd872007-08-18 13:14:55 +09005486 ata_port_printk(ap, KERN_INFO,
5487 "%cATA max %s %s\n",
Tejun Heoa16abc02007-05-21 18:33:47 +02005488 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
Tejun Heof3187192007-04-17 23:44:07 +09005489 ata_mode_string(xfer_mask),
Tejun Heocbcdd872007-08-18 13:14:55 +09005490 ap->link.eh_info.desc);
Tejun Heoabf6e8e2007-10-09 14:57:25 +09005491 ata_ehi_clear_desc(&ap->link.eh_info);
5492 } else
Tejun Heof3187192007-04-17 23:44:07 +09005493 ata_port_printk(ap, KERN_INFO, "DUMMY\n");
5494 }
5495
5496 /* perform each probe synchronously */
5497 DPRINTK("probe begin\n");
5498 for (i = 0; i < host->n_ports; i++) {
5499 struct ata_port *ap = host->ports[i];
Tejun Heof3187192007-04-17 23:44:07 +09005500
5501 /* probe */
Tejun Heo52783c52006-05-31 18:28:22 +09005502 if (ap->ops->error_handler) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005503 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo3e706392006-05-31 18:28:11 +09005504 unsigned long flags;
5505
5506 ata_port_probe(ap);
5507
5508 /* kick EH for boot probing */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005509 spin_lock_irqsave(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09005510
Tejun Heob558edd2008-01-24 00:05:14 +09005511 ehi->probe_mask |= ATA_ALL_DEVICES;
Tejun Heocf480622008-01-24 00:05:14 +09005512 ehi->action |= ATA_EH_RESET;
Tejun Heo1cdaf532006-07-03 16:07:26 +09005513 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
Tejun Heo3e706392006-05-31 18:28:11 +09005514
Tejun Heof4d6d002007-05-01 11:50:15 +02005515 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
Tejun Heob51e9e52006-06-29 01:29:30 +09005516 ap->pflags |= ATA_PFLAG_LOADING;
Tejun Heo3e706392006-05-31 18:28:11 +09005517 ata_port_schedule_eh(ap);
5518
Jeff Garzikba6a1302006-06-22 23:46:10 -04005519 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09005520
5521 /* wait for EH to finish */
5522 ata_port_wait_eh(ap);
5523 } else {
Tejun Heo44877b42007-02-21 01:06:51 +09005524 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
Tejun Heo3e706392006-05-31 18:28:11 +09005525 rc = ata_bus_probe(ap);
Tejun Heo44877b42007-02-21 01:06:51 +09005526 DPRINTK("ata%u: bus probe end\n", ap->print_id);
Tejun Heo3e706392006-05-31 18:28:11 +09005527
5528 if (rc) {
5529 /* FIXME: do something useful here?
5530 * Current libata behavior will
5531 * tear down everything when
5532 * the module is removed
5533 * or the h/w is unplugged.
5534 */
5535 }
5536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 }
5538
5539 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005540 DPRINTK("host probe begin\n");
Jeff Garzikcca39742006-08-24 03:19:22 -04005541 for (i = 0; i < host->n_ports; i++) {
5542 struct ata_port *ap = host->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543
Tejun Heo1ae46312007-07-16 14:29:40 +09005544 ata_scsi_scan_host(ap, 1);
Kristen Carlson Accardica773292007-10-25 00:58:59 -04005545 ata_lpm_schedule(ap, ap->pm_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 }
5547
Tejun Heof3187192007-04-17 23:44:07 +09005548 return 0;
5549}
5550
5551/**
Tejun Heof5cda252007-04-17 23:44:07 +09005552 * ata_host_activate - start host, request IRQ and register it
5553 * @host: target ATA host
5554 * @irq: IRQ to request
5555 * @irq_handler: irq_handler used when requesting IRQ
5556 * @irq_flags: irq_flags used when requesting IRQ
5557 * @sht: scsi_host_template to use when registering the host
5558 *
5559 * After allocating an ATA host and initializing it, most libata
5560 * LLDs perform three steps to activate the host - start host,
5561 * request IRQ and register it. This helper takes necessasry
5562 * arguments and performs the three steps in one go.
5563 *
Paul Mundt3d46b2e2007-11-08 11:14:56 +09005564 * An invalid IRQ skips the IRQ registration and expects the host to
5565 * have set polling mode on the port. In this case, @irq_handler
5566 * should be NULL.
5567 *
Tejun Heof5cda252007-04-17 23:44:07 +09005568 * LOCKING:
5569 * Inherited from calling layer (may sleep).
5570 *
5571 * RETURNS:
5572 * 0 on success, -errno otherwise.
5573 */
5574int ata_host_activate(struct ata_host *host, int irq,
5575 irq_handler_t irq_handler, unsigned long irq_flags,
5576 struct scsi_host_template *sht)
5577{
Tejun Heocbcdd872007-08-18 13:14:55 +09005578 int i, rc;
Tejun Heof5cda252007-04-17 23:44:07 +09005579
5580 rc = ata_host_start(host);
5581 if (rc)
5582 return rc;
5583
Paul Mundt3d46b2e2007-11-08 11:14:56 +09005584 /* Special case for polling mode */
5585 if (!irq) {
5586 WARN_ON(irq_handler);
5587 return ata_host_register(host, sht);
5588 }
5589
Tejun Heof5cda252007-04-17 23:44:07 +09005590 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
5591 dev_driver_string(host->dev), host);
5592 if (rc)
5593 return rc;
5594
Tejun Heocbcdd872007-08-18 13:14:55 +09005595 for (i = 0; i < host->n_ports; i++)
5596 ata_port_desc(host->ports[i], "irq %d", irq);
Tejun Heo40318262007-07-03 01:38:47 +09005597
Tejun Heof5cda252007-04-17 23:44:07 +09005598 rc = ata_host_register(host, sht);
5599 /* if failed, just free the IRQ and leave ports alone */
5600 if (rc)
5601 devm_free_irq(host->dev, irq, host);
5602
5603 return rc;
5604}
5605
5606/**
Tejun Heo720ba122006-05-31 18:28:13 +09005607 * ata_port_detach - Detach ATA port in prepration of device removal
5608 * @ap: ATA port to be detached
5609 *
5610 * Detach all ATA devices and the associated SCSI devices of @ap;
5611 * then, remove the associated SCSI host. @ap is guaranteed to
5612 * be quiescent on return from this function.
5613 *
5614 * LOCKING:
5615 * Kernel thread context (may sleep).
5616 */
Adrian Bunk741b7762007-10-24 18:23:06 +02005617static void ata_port_detach(struct ata_port *ap)
Tejun Heo720ba122006-05-31 18:28:13 +09005618{
5619 unsigned long flags;
Tejun Heo41bda9c2007-08-06 18:36:24 +09005620 struct ata_link *link;
Tejun Heof58229f2007-08-06 18:36:23 +09005621 struct ata_device *dev;
Tejun Heo720ba122006-05-31 18:28:13 +09005622
5623 if (!ap->ops->error_handler)
Tejun Heoc3cf30a2006-08-05 03:59:11 +09005624 goto skip_eh;
Tejun Heo720ba122006-05-31 18:28:13 +09005625
5626 /* tell EH we're leaving & flush EH */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005627 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09005628 ap->pflags |= ATA_PFLAG_UNLOADING;
Jeff Garzikba6a1302006-06-22 23:46:10 -04005629 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005630
5631 ata_port_wait_eh(ap);
5632
Tejun Heo7f9ad9b2007-12-15 15:05:00 +09005633 /* EH is now guaranteed to see UNLOADING - EH context belongs
5634 * to us. Disable all existing devices.
Tejun Heo720ba122006-05-31 18:28:13 +09005635 */
Tejun Heo41bda9c2007-08-06 18:36:24 +09005636 ata_port_for_each_link(link, ap) {
5637 ata_link_for_each_dev(dev, link)
5638 ata_dev_disable(dev);
5639 }
Tejun Heo720ba122006-05-31 18:28:13 +09005640
Tejun Heo720ba122006-05-31 18:28:13 +09005641 /* Final freeze & EH. All in-flight commands are aborted. EH
5642 * will be skipped and retrials will be terminated with bad
5643 * target.
5644 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005645 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005646 ata_port_freeze(ap); /* won't be thawed */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005647 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005648
5649 ata_port_wait_eh(ap);
Oleg Nesterov45a66c12007-07-09 11:46:13 -07005650 cancel_rearming_delayed_work(&ap->hotplug_task);
Tejun Heo720ba122006-05-31 18:28:13 +09005651
Tejun Heoc3cf30a2006-08-05 03:59:11 +09005652 skip_eh:
Tejun Heo720ba122006-05-31 18:28:13 +09005653 /* remove the associated SCSI host */
Jeff Garzikcca39742006-08-24 03:19:22 -04005654 scsi_remove_host(ap->scsi_host);
Tejun Heo720ba122006-05-31 18:28:13 +09005655}
5656
5657/**
Tejun Heo0529c1592007-01-20 16:00:26 +09005658 * ata_host_detach - Detach all ports of an ATA host
5659 * @host: Host to detach
5660 *
5661 * Detach all ports of @host.
5662 *
5663 * LOCKING:
5664 * Kernel thread context (may sleep).
5665 */
5666void ata_host_detach(struct ata_host *host)
5667{
5668 int i;
5669
5670 for (i = 0; i < host->n_ports; i++)
5671 ata_port_detach(host->ports[i]);
Tejun Heo562f0c22007-12-15 15:05:01 +09005672
5673 /* the host is dead now, dissociate ACPI */
5674 ata_acpi_dissociate(host);
Tejun Heo0529c1592007-01-20 16:00:26 +09005675}
5676
Jeff Garzik374b1872005-08-30 05:42:52 -04005677#ifdef CONFIG_PCI
5678
Edward Falk0baab862005-06-02 18:17:13 -04005679/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 * ata_pci_remove_one - PCI layer callback for device removal
5681 * @pdev: PCI device that was removed
5682 *
Tejun Heob878ca52007-01-20 16:00:28 +09005683 * PCI layer indicates to libata via this hook that hot-unplug or
5684 * module unload event has occurred. Detach all ports. Resource
5685 * release is handled via devres.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 *
5687 * LOCKING:
5688 * Inherited from PCI layer (may sleep).
5689 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09005690void ata_pci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691{
Jeff Garzik28555682007-10-11 17:12:35 -04005692 struct device *dev = &pdev->dev;
Jeff Garzikcca39742006-08-24 03:19:22 -04005693 struct ata_host *host = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694
Tejun Heob878ca52007-01-20 16:00:28 +09005695 ata_host_detach(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696}
5697
5698/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005699int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700{
5701 unsigned long tmp = 0;
5702
5703 switch (bits->width) {
5704 case 1: {
5705 u8 tmp8 = 0;
5706 pci_read_config_byte(pdev, bits->reg, &tmp8);
5707 tmp = tmp8;
5708 break;
5709 }
5710 case 2: {
5711 u16 tmp16 = 0;
5712 pci_read_config_word(pdev, bits->reg, &tmp16);
5713 tmp = tmp16;
5714 break;
5715 }
5716 case 4: {
5717 u32 tmp32 = 0;
5718 pci_read_config_dword(pdev, bits->reg, &tmp32);
5719 tmp = tmp32;
5720 break;
5721 }
5722
5723 default:
5724 return -EINVAL;
5725 }
5726
5727 tmp &= bits->mask;
5728
5729 return (tmp == bits->val) ? 1 : 0;
5730}
Jens Axboe9b847542006-01-06 09:28:07 +01005731
Tejun Heo6ffa01d2007-03-02 17:32:47 +09005732#ifdef CONFIG_PM
Tejun Heo3c5100c2006-07-26 16:58:33 +09005733void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
Jens Axboe9b847542006-01-06 09:28:07 +01005734{
5735 pci_save_state(pdev);
Tejun Heo4c90d972007-02-20 18:14:48 +09005736 pci_disable_device(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09005737
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01005738 if (mesg.event & PM_EVENT_SLEEP)
Tejun Heo500530f2006-07-03 16:07:27 +09005739 pci_set_power_state(pdev, PCI_D3hot);
Jens Axboe9b847542006-01-06 09:28:07 +01005740}
5741
Tejun Heo553c4aa2006-12-26 19:39:50 +09005742int ata_pci_device_do_resume(struct pci_dev *pdev)
Jens Axboe9b847542006-01-06 09:28:07 +01005743{
Tejun Heo553c4aa2006-12-26 19:39:50 +09005744 int rc;
5745
Jens Axboe9b847542006-01-06 09:28:07 +01005746 pci_set_power_state(pdev, PCI_D0);
5747 pci_restore_state(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09005748
Tejun Heob878ca52007-01-20 16:00:28 +09005749 rc = pcim_enable_device(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09005750 if (rc) {
5751 dev_printk(KERN_ERR, &pdev->dev,
5752 "failed to enable device after resume (%d)\n", rc);
5753 return rc;
5754 }
5755
Jens Axboe9b847542006-01-06 09:28:07 +01005756 pci_set_master(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09005757 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09005758}
5759
Tejun Heo3c5100c2006-07-26 16:58:33 +09005760int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09005761{
Jeff Garzikcca39742006-08-24 03:19:22 -04005762 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo500530f2006-07-03 16:07:27 +09005763 int rc = 0;
5764
Jeff Garzikcca39742006-08-24 03:19:22 -04005765 rc = ata_host_suspend(host, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09005766 if (rc)
5767 return rc;
5768
Tejun Heo3c5100c2006-07-26 16:58:33 +09005769 ata_pci_device_do_suspend(pdev, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09005770
5771 return 0;
5772}
5773
5774int ata_pci_device_resume(struct pci_dev *pdev)
5775{
Jeff Garzikcca39742006-08-24 03:19:22 -04005776 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09005777 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09005778
Tejun Heo553c4aa2006-12-26 19:39:50 +09005779 rc = ata_pci_device_do_resume(pdev);
5780 if (rc == 0)
5781 ata_host_resume(host);
5782 return rc;
Jens Axboe9b847542006-01-06 09:28:07 +01005783}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09005784#endif /* CONFIG_PM */
5785
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786#endif /* CONFIG_PCI */
5787
Tejun Heo33267322008-02-13 09:15:09 +09005788static int __init ata_parse_force_one(char **cur,
5789 struct ata_force_ent *force_ent,
5790 const char **reason)
5791{
5792 /* FIXME: Currently, there's no way to tag init const data and
5793 * using __initdata causes build failure on some versions of
5794 * gcc. Once __initdataconst is implemented, add const to the
5795 * following structure.
5796 */
5797 static struct ata_force_param force_tbl[] __initdata = {
5798 { "40c", .cbl = ATA_CBL_PATA40 },
5799 { "80c", .cbl = ATA_CBL_PATA80 },
5800 { "short40c", .cbl = ATA_CBL_PATA40_SHORT },
5801 { "unk", .cbl = ATA_CBL_PATA_UNK },
5802 { "ign", .cbl = ATA_CBL_PATA_IGN },
5803 { "sata", .cbl = ATA_CBL_SATA },
5804 { "1.5Gbps", .spd_limit = 1 },
5805 { "3.0Gbps", .spd_limit = 2 },
5806 { "noncq", .horkage_on = ATA_HORKAGE_NONCQ },
5807 { "ncq", .horkage_off = ATA_HORKAGE_NONCQ },
5808 { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) },
5809 { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) },
5810 { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) },
5811 { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) },
5812 { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) },
5813 { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) },
5814 { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) },
5815 { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) },
5816 { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) },
5817 { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) },
5818 { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) },
5819 { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) },
5820 { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
5821 { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
5822 { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
5823 { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
5824 { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
5825 { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
5826 { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
5827 { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
5828 { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
5829 { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
5830 { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
5831 { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
5832 { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
5833 { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
5834 { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
5835 { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
5836 { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
5837 { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
5838 { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
5839 { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
5840 { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
5841 { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) },
5842 };
5843 char *start = *cur, *p = *cur;
5844 char *id, *val, *endp;
5845 const struct ata_force_param *match_fp = NULL;
5846 int nr_matches = 0, i;
5847
5848 /* find where this param ends and update *cur */
5849 while (*p != '\0' && *p != ',')
5850 p++;
5851
5852 if (*p == '\0')
5853 *cur = p;
5854 else
5855 *cur = p + 1;
5856
5857 *p = '\0';
5858
5859 /* parse */
5860 p = strchr(start, ':');
5861 if (!p) {
5862 val = strstrip(start);
5863 goto parse_val;
5864 }
5865 *p = '\0';
5866
5867 id = strstrip(start);
5868 val = strstrip(p + 1);
5869
5870 /* parse id */
5871 p = strchr(id, '.');
5872 if (p) {
5873 *p++ = '\0';
5874 force_ent->device = simple_strtoul(p, &endp, 10);
5875 if (p == endp || *endp != '\0') {
5876 *reason = "invalid device";
5877 return -EINVAL;
5878 }
5879 }
5880
5881 force_ent->port = simple_strtoul(id, &endp, 10);
5882 if (p == endp || *endp != '\0') {
5883 *reason = "invalid port/link";
5884 return -EINVAL;
5885 }
5886
5887 parse_val:
5888 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
5889 for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
5890 const struct ata_force_param *fp = &force_tbl[i];
5891
5892 if (strncasecmp(val, fp->name, strlen(val)))
5893 continue;
5894
5895 nr_matches++;
5896 match_fp = fp;
5897
5898 if (strcasecmp(val, fp->name) == 0) {
5899 nr_matches = 1;
5900 break;
5901 }
5902 }
5903
5904 if (!nr_matches) {
5905 *reason = "unknown value";
5906 return -EINVAL;
5907 }
5908 if (nr_matches > 1) {
5909 *reason = "ambigious value";
5910 return -EINVAL;
5911 }
5912
5913 force_ent->param = *match_fp;
5914
5915 return 0;
5916}
5917
5918static void __init ata_parse_force_param(void)
5919{
5920 int idx = 0, size = 1;
5921 int last_port = -1, last_device = -1;
5922 char *p, *cur, *next;
5923
5924 /* calculate maximum number of params and allocate force_tbl */
5925 for (p = ata_force_param_buf; *p; p++)
5926 if (*p == ',')
5927 size++;
5928
5929 ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
5930 if (!ata_force_tbl) {
5931 printk(KERN_WARNING "ata: failed to extend force table, "
5932 "libata.force ignored\n");
5933 return;
5934 }
5935
5936 /* parse and populate the table */
5937 for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
5938 const char *reason = "";
5939 struct ata_force_ent te = { .port = -1, .device = -1 };
5940
5941 next = cur;
5942 if (ata_parse_force_one(&next, &te, &reason)) {
5943 printk(KERN_WARNING "ata: failed to parse force "
5944 "parameter \"%s\" (%s)\n",
5945 cur, reason);
5946 continue;
5947 }
5948
5949 if (te.port == -1) {
5950 te.port = last_port;
5951 te.device = last_device;
5952 }
5953
5954 ata_force_tbl[idx++] = te;
5955
5956 last_port = te.port;
5957 last_device = te.device;
5958 }
5959
5960 ata_force_tbl_size = idx;
5961}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963static int __init ata_init(void)
5964{
Andrew Mortona8601e52006-06-25 01:36:52 -07005965 ata_probe_timeout *= HZ;
Tejun Heo33267322008-02-13 09:15:09 +09005966
5967 ata_parse_force_param();
5968
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 ata_wq = create_workqueue("ata");
5970 if (!ata_wq)
5971 return -ENOMEM;
5972
Tejun Heo453b07a2006-05-31 18:27:42 +09005973 ata_aux_wq = create_singlethread_workqueue("ata_aux");
5974 if (!ata_aux_wq) {
5975 destroy_workqueue(ata_wq);
5976 return -ENOMEM;
5977 }
5978
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5980 return 0;
5981}
5982
5983static void __exit ata_exit(void)
5984{
Tejun Heo33267322008-02-13 09:15:09 +09005985 kfree(ata_force_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 destroy_workqueue(ata_wq);
Tejun Heo453b07a2006-05-31 18:27:42 +09005987 destroy_workqueue(ata_aux_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988}
5989
Brian Kinga4625082006-11-13 16:32:36 -06005990subsys_initcall(ata_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991module_exit(ata_exit);
5992
Jeff Garzik67846b32005-10-05 02:58:32 -04005993static unsigned long ratelimit_time;
Ingo Molnar34af9462006-06-27 02:53:55 -07005994static DEFINE_SPINLOCK(ata_ratelimit_lock);
Jeff Garzik67846b32005-10-05 02:58:32 -04005995
5996int ata_ratelimit(void)
5997{
5998 int rc;
5999 unsigned long flags;
6000
6001 spin_lock_irqsave(&ata_ratelimit_lock, flags);
6002
6003 if (time_after(jiffies, ratelimit_time)) {
6004 rc = 1;
6005 ratelimit_time = jiffies + (HZ/5);
6006 } else
6007 rc = 0;
6008
6009 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
6010
6011 return rc;
6012}
6013
Tejun Heoc22daff2006-04-11 22:22:29 +09006014/**
6015 * ata_wait_register - wait until register value changes
6016 * @reg: IO-mapped register
6017 * @mask: Mask to apply to read register value
6018 * @val: Wait condition
6019 * @interval_msec: polling interval in milliseconds
6020 * @timeout_msec: timeout in milliseconds
6021 *
6022 * Waiting for some bits of register to change is a common
6023 * operation for ATA controllers. This function reads 32bit LE
6024 * IO-mapped register @reg and tests for the following condition.
6025 *
6026 * (*@reg & mask) != val
6027 *
6028 * If the condition is met, it returns; otherwise, the process is
6029 * repeated after @interval_msec until timeout.
6030 *
6031 * LOCKING:
6032 * Kernel thread context (may sleep)
6033 *
6034 * RETURNS:
6035 * The final register value.
6036 */
6037u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
6038 unsigned long interval_msec,
6039 unsigned long timeout_msec)
6040{
6041 unsigned long timeout;
6042 u32 tmp;
6043
6044 tmp = ioread32(reg);
6045
6046 /* Calculate timeout _after_ the first read to make sure
6047 * preceding writes reach the controller before starting to
6048 * eat away the timeout.
6049 */
6050 timeout = jiffies + (timeout_msec * HZ) / 1000;
6051
6052 while ((tmp & mask) == val && time_before(jiffies, timeout)) {
6053 msleep(interval_msec);
6054 tmp = ioread32(reg);
6055 }
6056
6057 return tmp;
6058}
6059
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060/*
Tejun Heodd5b06c2006-08-10 16:59:12 +09006061 * Dummy port_ops
6062 */
6063static void ata_dummy_noret(struct ata_port *ap) { }
6064static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
6065static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
6066
6067static u8 ata_dummy_check_status(struct ata_port *ap)
6068{
6069 return ATA_DRDY;
6070}
6071
6072static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6073{
6074 return AC_ERR_SYSTEM;
6075}
6076
Tejun Heo029cfd62008-03-25 12:22:49 +09006077struct ata_port_operations ata_dummy_port_ops = {
Tejun Heodd5b06c2006-08-10 16:59:12 +09006078 .check_status = ata_dummy_check_status,
6079 .check_altstatus = ata_dummy_check_status,
6080 .dev_select = ata_noop_dev_select,
6081 .qc_prep = ata_noop_qc_prep,
6082 .qc_issue = ata_dummy_qc_issue,
6083 .freeze = ata_dummy_noret,
6084 .thaw = ata_dummy_noret,
6085 .error_handler = ata_dummy_noret,
6086 .post_internal_cmd = ata_dummy_qc_noret,
6087 .irq_clear = ata_dummy_noret,
6088 .port_start = ata_dummy_ret0,
6089 .port_stop = ata_dummy_noret,
6090};
6091
Tejun Heo21b0ad42007-04-17 23:44:07 +09006092const struct ata_port_info ata_dummy_port_info = {
6093 .port_ops = &ata_dummy_port_ops,
6094};
6095
Tejun Heodd5b06c2006-08-10 16:59:12 +09006096/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 * libata is essentially a library of internal helper functions for
6098 * low-level ATA host controller drivers. As such, the API/ABI is
6099 * likely to change as new drivers are added and updated.
6100 * Do not depend on ABI/API stability.
6101 */
Tejun Heoe9c83912006-07-03 16:07:26 +09006102EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
6103EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
6104EXPORT_SYMBOL_GPL(sata_deb_timing_long);
Tejun Heo029cfd62008-03-25 12:22:49 +09006105EXPORT_SYMBOL_GPL(ata_base_port_ops);
6106EXPORT_SYMBOL_GPL(sata_port_ops);
6107EXPORT_SYMBOL_GPL(sata_pmp_port_ops);
Tejun Heodd5b06c2006-08-10 16:59:12 +09006108EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
Tejun Heo21b0ad42007-04-17 23:44:07 +09006109EXPORT_SYMBOL_GPL(ata_dummy_port_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110EXPORT_SYMBOL_GPL(ata_std_bios_param);
Jeff Garzikcca39742006-08-24 03:19:22 -04006111EXPORT_SYMBOL_GPL(ata_host_init);
Tejun Heof3187192007-04-17 23:44:07 +09006112EXPORT_SYMBOL_GPL(ata_host_alloc);
Tejun Heof5cda252007-04-17 23:44:07 +09006113EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
Tejun Heoecef7252007-04-17 23:44:06 +09006114EXPORT_SYMBOL_GPL(ata_host_start);
Tejun Heof3187192007-04-17 23:44:07 +09006115EXPORT_SYMBOL_GPL(ata_host_register);
Tejun Heof5cda252007-04-17 23:44:07 +09006116EXPORT_SYMBOL_GPL(ata_host_activate);
Tejun Heo0529c1592007-01-20 16:00:26 +09006117EXPORT_SYMBOL_GPL(ata_host_detach);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118EXPORT_SYMBOL_GPL(ata_sg_init);
Tejun Heof686bcb2006-05-15 20:58:05 +09006119EXPORT_SYMBOL_GPL(ata_qc_complete);
Tejun Heodedaf2b2006-05-15 21:03:43 +09006120EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121EXPORT_SYMBOL_GPL(ata_noop_dev_select);
Jeff Garzik43727fb2007-02-25 16:50:52 -05006122EXPORT_SYMBOL_GPL(sata_print_link_status);
Tejun Heo436d34b2008-04-02 17:28:46 +09006123EXPORT_SYMBOL_GPL(atapi_cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124EXPORT_SYMBOL_GPL(ata_tf_to_fis);
6125EXPORT_SYMBOL_GPL(ata_tf_from_fis);
Tejun Heo63573572007-11-27 19:43:39 +09006126EXPORT_SYMBOL_GPL(ata_pack_xfermask);
6127EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
6128EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
6129EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
6130EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
6131EXPORT_SYMBOL_GPL(ata_mode_string);
6132EXPORT_SYMBOL_GPL(ata_id_xfermask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133EXPORT_SYMBOL_GPL(ata_port_start);
Alan04351822007-03-06 02:37:52 -08006134EXPORT_SYMBOL_GPL(ata_do_set_mode);
Tejun Heo31cc23b2007-09-23 13:14:12 +09006135EXPORT_SYMBOL_GPL(ata_std_qc_defer);
Brian Kinge46834c2006-03-17 17:04:03 -06006136EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Tejun Heo358f9a72008-03-25 12:22:47 +09006137EXPORT_SYMBOL_GPL(ata_noop_irq_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138EXPORT_SYMBOL_GPL(ata_port_probe);
Alan10305f02007-02-20 18:01:59 +00006139EXPORT_SYMBOL_GPL(ata_dev_disable);
Tejun Heo3c567b72006-05-15 20:57:23 +09006140EXPORT_SYMBOL_GPL(sata_set_spd);
Tejun Heo936fd732007-08-06 18:36:23 +09006141EXPORT_SYMBOL_GPL(sata_link_debounce);
6142EXPORT_SYMBOL_GPL(sata_link_resume);
Tejun Heocc0680a2007-08-06 18:36:23 +09006143EXPORT_SYMBOL_GPL(sata_link_hardreset);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05006144EXPORT_SYMBOL_GPL(ata_dev_classify);
6145EXPORT_SYMBOL_GPL(ata_dev_pair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04006147EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heoc22daff2006-04-11 22:22:29 +09006148EXPORT_SYMBOL_GPL(ata_wait_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
6150EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
Tejun Heo83c47bc2006-05-31 18:28:07 +09006152EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09006153EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
Tejun Heo34bf2172006-05-15 20:57:46 +09006154EXPORT_SYMBOL_GPL(sata_scr_valid);
6155EXPORT_SYMBOL_GPL(sata_scr_read);
6156EXPORT_SYMBOL_GPL(sata_scr_write);
6157EXPORT_SYMBOL_GPL(sata_scr_write_flush);
Tejun Heo936fd732007-08-06 18:36:23 +09006158EXPORT_SYMBOL_GPL(ata_link_online);
6159EXPORT_SYMBOL_GPL(ata_link_offline);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006160#ifdef CONFIG_PM
Jeff Garzikcca39742006-08-24 03:19:22 -04006161EXPORT_SYMBOL_GPL(ata_host_suspend);
6162EXPORT_SYMBOL_GPL(ata_host_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006163#endif /* CONFIG_PM */
Tejun Heo6a62a042006-02-13 10:02:46 +09006164EXPORT_SYMBOL_GPL(ata_id_string);
6165EXPORT_SYMBOL_GPL(ata_id_c_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166EXPORT_SYMBOL_GPL(ata_scsi_simulate);
6167
Alan Cox1bc4ccf2006-01-09 17:18:14 +00006168EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Tejun Heo63573572007-11-27 19:43:39 +09006169EXPORT_SYMBOL_GPL(ata_timing_find_mode);
Alan Cox452503f2005-10-21 19:01:32 -04006170EXPORT_SYMBOL_GPL(ata_timing_compute);
6171EXPORT_SYMBOL_GPL(ata_timing_merge);
Tejun Heoa0f79b92007-12-18 16:33:05 +09006172EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
Alan Cox452503f2005-10-21 19:01:32 -04006173
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174#ifdef CONFIG_PCI
6175EXPORT_SYMBOL_GPL(pci_test_config_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006177#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09006178EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6179EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
Jens Axboe9b847542006-01-06 09:28:07 +01006180EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6181EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006182#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01006184
Tejun Heo31f88382007-09-23 13:19:54 +09006185EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch);
Tejun Heo3af9a772007-09-23 13:19:54 +09006186EXPORT_SYMBOL_GPL(sata_pmp_std_prereset);
6187EXPORT_SYMBOL_GPL(sata_pmp_std_hardreset);
6188EXPORT_SYMBOL_GPL(sata_pmp_std_postreset);
Tejun Heoa1efdab2008-03-25 12:22:50 +09006189EXPORT_SYMBOL_GPL(sata_pmp_error_handler);
Tejun Heo3af9a772007-09-23 13:19:54 +09006190
Tejun Heob64bbc32007-07-16 14:29:39 +09006191EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
6192EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
6193EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
Tejun Heocbcdd872007-08-18 13:14:55 +09006194EXPORT_SYMBOL_GPL(ata_port_desc);
6195#ifdef CONFIG_PCI
6196EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
6197#endif /* CONFIG_PCI */
Tejun Heo7b70fc02006-05-15 20:58:07 +09006198EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
Tejun Heodbd82612007-08-06 18:36:23 +09006199EXPORT_SYMBOL_GPL(ata_link_abort);
Tejun Heo7b70fc02006-05-15 20:58:07 +09006200EXPORT_SYMBOL_GPL(ata_port_abort);
Tejun Heoe3180492006-05-15 20:58:09 +09006201EXPORT_SYMBOL_GPL(ata_port_freeze);
Tejun Heo7d77b242007-09-23 13:14:13 +09006202EXPORT_SYMBOL_GPL(sata_async_notification);
Tejun Heoe3180492006-05-15 20:58:09 +09006203EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
6204EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
Tejun Heoece1d632006-04-02 18:51:53 +09006205EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
6206EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Tejun Heo022bdb02006-05-15 20:58:22 +09006207EXPORT_SYMBOL_GPL(ata_do_eh);
Tejun Heoa1efdab2008-03-25 12:22:50 +09006208EXPORT_SYMBOL_GPL(ata_std_error_handler);
Alan Coxbe0d18d2007-03-06 02:37:56 -08006209
6210EXPORT_SYMBOL_GPL(ata_cable_40wire);
6211EXPORT_SYMBOL_GPL(ata_cable_80wire);
6212EXPORT_SYMBOL_GPL(ata_cable_unknown);
Tejun Heoc88f90c2007-11-27 19:43:48 +09006213EXPORT_SYMBOL_GPL(ata_cable_ignore);
Alan Coxbe0d18d2007-03-06 02:37:56 -08006214EXPORT_SYMBOL_GPL(ata_cable_sata);