blob: fd4563daacd14e57f69979a357b874d5330edff4 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/kernel.h>
36#include <linux/module.h>
37#include <linux/pci.h>
38#include <linux/init.h>
39#include <linux/list.h>
40#include <linux/mm.h>
41#include <linux/highmem.h>
42#include <linux/spinlock.h>
43#include <linux/blkdev.h>
44#include <linux/delay.h>
45#include <linux/timer.h>
46#include <linux/interrupt.h>
47#include <linux/completion.h>
48#include <linux/suspend.h>
49#include <linux/workqueue.h>
Jeff Garzik67846b32005-10-05 02:58:32 -040050#include <linux/jiffies.h>
David Hardeman378f0582005-09-17 17:55:31 +100051#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <scsi/scsi.h>
Jeff Garzik193515d2005-11-07 00:59:37 -050053#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <scsi/scsi_host.h>
55#include <linux/libata.h>
56#include <asm/io.h>
57#include <asm/semaphore.h>
58#include <asm/byteorder.h>
59
60#include "libata.h"
61
Jeff Garzikfda0efc2007-01-31 07:43:15 -050062
Tejun Heod7bb4cc2006-05-31 18:27:46 +090063/* debounce timing parameters in msecs { interval, duration, timeout } */
Tejun Heoe9c83912006-07-03 16:07:26 +090064const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
65const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
66const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
Tejun Heod7bb4cc2006-05-31 18:27:46 +090067
Tejun Heo3373efd2006-05-15 20:57:53 +090068static unsigned int ata_dev_init_params(struct ata_device *dev,
69 u16 heads, u16 sectors);
70static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -040071static unsigned int ata_dev_set_AN(struct ata_device *dev, u8 enable);
Tejun Heo3373efd2006-05-15 20:57:53 +090072static void ata_dev_xfermask(struct ata_device *dev);
Tejun Heo75683fe2007-07-05 13:31:27 +090073static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Tejun Heof3187192007-04-17 23:44:07 +090075unsigned int ata_print_id = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static struct workqueue_struct *ata_wq;
77
Tejun Heo453b07a2006-05-31 18:27:42 +090078struct workqueue_struct *ata_aux_wq;
79
Jeff Garzik418dc1f2006-03-11 20:50:08 -050080int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -040081module_param(atapi_enabled, int, 0444);
82MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
83
Albert Lee95de7192006-04-04 10:57:18 +080084int atapi_dmadir = 0;
85module_param(atapi_dmadir, int, 0444);
86MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
87
Mark Lordbaf4fdf2007-08-08 01:08:45 +090088int atapi_passthru16 = 1;
89module_param(atapi_passthru16, int, 0444);
90MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices; on by default (0=off, 1=on)");
91
Jeff Garzikc3c013a2006-02-27 22:31:19 -050092int libata_fua = 0;
93module_param_named(fua, libata_fua, int, 0444);
94MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
95
Alan Cox1e999732007-04-11 00:23:13 +010096static int ata_ignore_hpa = 0;
97module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
98MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
99
Andrew Mortona8601e52006-06-25 01:36:52 -0700100static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
101module_param(ata_probe_timeout, int, 0444);
102MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
103
Jeff Garzikd7d0dad2007-03-28 01:57:37 -0400104int libata_noacpi = 1;
105module_param_named(noacpi, libata_noacpi, int, 0444);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700106MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in suspend/resume when set");
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108MODULE_AUTHOR("Jeff Garzik");
109MODULE_DESCRIPTION("Library module for ATA devices");
110MODULE_LICENSE("GPL");
111MODULE_VERSION(DRV_VERSION);
112
Edward Falk0baab862005-06-02 18:17:13 -0400113
114/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
116 * @tf: Taskfile to convert
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * @pmp: Port multiplier port
Tejun Heo99771262007-07-16 14:29:38 +0900118 * @is_cmd: This FIS is for command
119 * @fis: Buffer into which data will output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 *
121 * Converts a standard ATA taskfile to a Serial ATA
122 * FIS structure (Register - Host to Device).
123 *
124 * LOCKING:
125 * Inherited from caller.
126 */
Tejun Heo99771262007-07-16 14:29:38 +0900127void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Tejun Heo99771262007-07-16 14:29:38 +0900129 fis[0] = 0x27; /* Register - Host to Device FIS */
130 fis[1] = pmp & 0xf; /* Port multiplier number*/
131 if (is_cmd)
132 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 fis[2] = tf->command;
135 fis[3] = tf->feature;
136
137 fis[4] = tf->lbal;
138 fis[5] = tf->lbam;
139 fis[6] = tf->lbah;
140 fis[7] = tf->device;
141
142 fis[8] = tf->hob_lbal;
143 fis[9] = tf->hob_lbam;
144 fis[10] = tf->hob_lbah;
145 fis[11] = tf->hob_feature;
146
147 fis[12] = tf->nsect;
148 fis[13] = tf->hob_nsect;
149 fis[14] = 0;
150 fis[15] = tf->ctl;
151
152 fis[16] = 0;
153 fis[17] = 0;
154 fis[18] = 0;
155 fis[19] = 0;
156}
157
158/**
159 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
160 * @fis: Buffer from which data will be input
161 * @tf: Taskfile to output
162 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500163 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 *
165 * LOCKING:
166 * Inherited from caller.
167 */
168
Jeff Garzik057ace52005-10-22 14:27:05 -0400169void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 tf->command = fis[2]; /* status */
172 tf->feature = fis[3]; /* error */
173
174 tf->lbal = fis[4];
175 tf->lbam = fis[5];
176 tf->lbah = fis[6];
177 tf->device = fis[7];
178
179 tf->hob_lbal = fis[8];
180 tf->hob_lbam = fis[9];
181 tf->hob_lbah = fis[10];
182
183 tf->nsect = fis[12];
184 tf->hob_nsect = fis[13];
185}
186
Albert Lee8cbd6df2005-10-12 15:06:27 +0800187static const u8 ata_rw_cmds[] = {
188 /* pio multi */
189 ATA_CMD_READ_MULTI,
190 ATA_CMD_WRITE_MULTI,
191 ATA_CMD_READ_MULTI_EXT,
192 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100193 0,
194 0,
195 0,
196 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800197 /* pio */
198 ATA_CMD_PIO_READ,
199 ATA_CMD_PIO_WRITE,
200 ATA_CMD_PIO_READ_EXT,
201 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100202 0,
203 0,
204 0,
205 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800206 /* dma */
207 ATA_CMD_READ,
208 ATA_CMD_WRITE,
209 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100210 ATA_CMD_WRITE_EXT,
211 0,
212 0,
213 0,
214 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800215};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800218 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
Tejun Heobd056d72006-11-14 22:47:10 +0900219 * @tf: command to examine and configure
220 * @dev: device tf belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500222 * Examine the device configuration and tf->flags to calculate
Albert Lee8cbd6df2005-10-12 15:06:27 +0800223 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 *
225 * LOCKING:
226 * caller.
227 */
Tejun Heobd056d72006-11-14 22:47:10 +0900228static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100230 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100232 int index, fua, lba48, write;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500233
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100234 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800235 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
236 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Albert Lee8cbd6df2005-10-12 15:06:27 +0800238 if (dev->flags & ATA_DFLAG_PIO) {
239 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100240 index = dev->multi_count ? 0 : 8;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900241 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
Alan Cox8d238e02006-01-17 20:50:31 +0000242 /* Unable to use DMA due to host limitation */
243 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800244 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800245 } else {
246 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100247 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100250 cmd = ata_rw_cmds[index + fua + lba48 + write];
251 if (cmd) {
252 tf->command = cmd;
253 return 0;
254 }
255 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Tejun Heocb95d562006-03-06 04:31:56 +0900258/**
Tejun Heo35b649f2006-11-14 22:37:35 +0900259 * ata_tf_read_block - Read block address from ATA taskfile
260 * @tf: ATA taskfile of interest
261 * @dev: ATA device @tf belongs to
262 *
263 * LOCKING:
264 * None.
265 *
266 * Read block address from @tf. This function can handle all
267 * three address formats - LBA, LBA48 and CHS. tf->protocol and
268 * flags select the address format to use.
269 *
270 * RETURNS:
271 * Block address read from @tf.
272 */
273u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
274{
275 u64 block = 0;
276
277 if (tf->flags & ATA_TFLAG_LBA) {
278 if (tf->flags & ATA_TFLAG_LBA48) {
279 block |= (u64)tf->hob_lbah << 40;
280 block |= (u64)tf->hob_lbam << 32;
281 block |= tf->hob_lbal << 24;
282 } else
283 block |= (tf->device & 0xf) << 24;
284
285 block |= tf->lbah << 16;
286 block |= tf->lbam << 8;
287 block |= tf->lbal;
288 } else {
289 u32 cyl, head, sect;
290
291 cyl = tf->lbam | (tf->lbah << 8);
292 head = tf->device & 0xf;
293 sect = tf->lbal;
294
295 block = (cyl * dev->heads + head) * dev->sectors + sect;
296 }
297
298 return block;
299}
300
301/**
Tejun Heobd056d72006-11-14 22:47:10 +0900302 * ata_build_rw_tf - Build ATA taskfile for given read/write request
303 * @tf: Target ATA taskfile
304 * @dev: ATA device @tf belongs to
305 * @block: Block address
306 * @n_block: Number of blocks
307 * @tf_flags: RW/FUA etc...
308 * @tag: tag
309 *
310 * LOCKING:
311 * None.
312 *
313 * Build ATA taskfile @tf for read/write request described by
314 * @block, @n_block, @tf_flags and @tag on @dev.
315 *
316 * RETURNS:
317 *
318 * 0 on success, -ERANGE if the request is too large for @dev,
319 * -EINVAL if the request is invalid.
320 */
321int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
322 u64 block, u32 n_block, unsigned int tf_flags,
323 unsigned int tag)
324{
325 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
326 tf->flags |= tf_flags;
327
Tejun Heo6d1245b2007-02-20 23:20:27 +0900328 if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
Tejun Heobd056d72006-11-14 22:47:10 +0900329 /* yay, NCQ */
330 if (!lba_48_ok(block, n_block))
331 return -ERANGE;
332
333 tf->protocol = ATA_PROT_NCQ;
334 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
335
336 if (tf->flags & ATA_TFLAG_WRITE)
337 tf->command = ATA_CMD_FPDMA_WRITE;
338 else
339 tf->command = ATA_CMD_FPDMA_READ;
340
341 tf->nsect = tag << 3;
342 tf->hob_feature = (n_block >> 8) & 0xff;
343 tf->feature = n_block & 0xff;
344
345 tf->hob_lbah = (block >> 40) & 0xff;
346 tf->hob_lbam = (block >> 32) & 0xff;
347 tf->hob_lbal = (block >> 24) & 0xff;
348 tf->lbah = (block >> 16) & 0xff;
349 tf->lbam = (block >> 8) & 0xff;
350 tf->lbal = block & 0xff;
351
352 tf->device = 1 << 6;
353 if (tf->flags & ATA_TFLAG_FUA)
354 tf->device |= 1 << 7;
355 } else if (dev->flags & ATA_DFLAG_LBA) {
356 tf->flags |= ATA_TFLAG_LBA;
357
358 if (lba_28_ok(block, n_block)) {
359 /* use LBA28 */
360 tf->device |= (block >> 24) & 0xf;
361 } else if (lba_48_ok(block, n_block)) {
362 if (!(dev->flags & ATA_DFLAG_LBA48))
363 return -ERANGE;
364
365 /* use LBA48 */
366 tf->flags |= ATA_TFLAG_LBA48;
367
368 tf->hob_nsect = (n_block >> 8) & 0xff;
369
370 tf->hob_lbah = (block >> 40) & 0xff;
371 tf->hob_lbam = (block >> 32) & 0xff;
372 tf->hob_lbal = (block >> 24) & 0xff;
373 } else
374 /* request too large even for LBA48 */
375 return -ERANGE;
376
377 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
378 return -EINVAL;
379
380 tf->nsect = n_block & 0xff;
381
382 tf->lbah = (block >> 16) & 0xff;
383 tf->lbam = (block >> 8) & 0xff;
384 tf->lbal = block & 0xff;
385
386 tf->device |= ATA_LBA;
387 } else {
388 /* CHS */
389 u32 sect, head, cyl, track;
390
391 /* The request -may- be too large for CHS addressing. */
392 if (!lba_28_ok(block, n_block))
393 return -ERANGE;
394
395 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
396 return -EINVAL;
397
398 /* Convert LBA to CHS */
399 track = (u32)block / dev->sectors;
400 cyl = track / dev->heads;
401 head = track % dev->heads;
402 sect = (u32)block % dev->sectors + 1;
403
404 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
405 (u32)block, track, cyl, head, sect);
406
407 /* Check whether the converted CHS can fit.
408 Cylinder: 0-65535
409 Head: 0-15
410 Sector: 1-255*/
411 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
412 return -ERANGE;
413
414 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
415 tf->lbal = sect;
416 tf->lbam = cyl;
417 tf->lbah = cyl >> 8;
418 tf->device |= head;
419 }
420
421 return 0;
422}
423
424/**
Tejun Heocb95d562006-03-06 04:31:56 +0900425 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
426 * @pio_mask: pio_mask
427 * @mwdma_mask: mwdma_mask
428 * @udma_mask: udma_mask
429 *
430 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
431 * unsigned int xfer_mask.
432 *
433 * LOCKING:
434 * None.
435 *
436 * RETURNS:
437 * Packed xfer_mask.
438 */
439static unsigned int ata_pack_xfermask(unsigned int pio_mask,
440 unsigned int mwdma_mask,
441 unsigned int udma_mask)
442{
443 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
444 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
445 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
446}
447
Tejun Heoc0489e42006-03-24 14:07:49 +0900448/**
449 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
450 * @xfer_mask: xfer_mask to unpack
451 * @pio_mask: resulting pio_mask
452 * @mwdma_mask: resulting mwdma_mask
453 * @udma_mask: resulting udma_mask
454 *
455 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
456 * Any NULL distination masks will be ignored.
457 */
458static void ata_unpack_xfermask(unsigned int xfer_mask,
459 unsigned int *pio_mask,
460 unsigned int *mwdma_mask,
461 unsigned int *udma_mask)
462{
463 if (pio_mask)
464 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
465 if (mwdma_mask)
466 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
467 if (udma_mask)
468 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
469}
470
Tejun Heocb95d562006-03-06 04:31:56 +0900471static const struct ata_xfer_ent {
Tejun Heobe9a50c2006-03-31 22:48:52 +0900472 int shift, bits;
Tejun Heocb95d562006-03-06 04:31:56 +0900473 u8 base;
474} ata_xfer_tbl[] = {
475 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
476 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
477 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
478 { -1, },
479};
480
481/**
482 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
483 * @xfer_mask: xfer_mask of interest
484 *
485 * Return matching XFER_* value for @xfer_mask. Only the highest
486 * bit of @xfer_mask is considered.
487 *
488 * LOCKING:
489 * None.
490 *
491 * RETURNS:
492 * Matching XFER_* value, 0 if no match found.
493 */
494static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
495{
496 int highbit = fls(xfer_mask) - 1;
497 const struct ata_xfer_ent *ent;
498
499 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
500 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
501 return ent->base + highbit - ent->shift;
502 return 0;
503}
504
505/**
506 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
507 * @xfer_mode: XFER_* of interest
508 *
509 * Return matching xfer_mask for @xfer_mode.
510 *
511 * LOCKING:
512 * None.
513 *
514 * RETURNS:
515 * Matching xfer_mask, 0 if no match found.
516 */
517static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
518{
519 const struct ata_xfer_ent *ent;
520
521 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
522 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
523 return 1 << (ent->shift + xfer_mode - ent->base);
524 return 0;
525}
526
527/**
528 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
529 * @xfer_mode: XFER_* of interest
530 *
531 * Return matching xfer_shift for @xfer_mode.
532 *
533 * LOCKING:
534 * None.
535 *
536 * RETURNS:
537 * Matching xfer_shift, -1 if no match found.
538 */
539static int ata_xfer_mode2shift(unsigned int xfer_mode)
540{
541 const struct ata_xfer_ent *ent;
542
543 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
544 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
545 return ent->shift;
546 return -1;
547}
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900550 * ata_mode_string - convert xfer_mask to string
551 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 *
553 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900554 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 *
556 * LOCKING:
557 * None.
558 *
559 * RETURNS:
560 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900561 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 */
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900563static const char *ata_mode_string(unsigned int xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Tejun Heo75f554b2006-03-06 04:31:57 +0900565 static const char * const xfer_mode_str[] = {
566 "PIO0",
567 "PIO1",
568 "PIO2",
569 "PIO3",
570 "PIO4",
Alan Coxb352e572006-08-10 18:52:12 +0100571 "PIO5",
572 "PIO6",
Tejun Heo75f554b2006-03-06 04:31:57 +0900573 "MWDMA0",
574 "MWDMA1",
575 "MWDMA2",
Alan Coxb352e572006-08-10 18:52:12 +0100576 "MWDMA3",
577 "MWDMA4",
Tejun Heo75f554b2006-03-06 04:31:57 +0900578 "UDMA/16",
579 "UDMA/25",
580 "UDMA/33",
581 "UDMA/44",
582 "UDMA/66",
583 "UDMA/100",
584 "UDMA/133",
585 "UDMA7",
586 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900587 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900589 highbit = fls(xfer_mask) - 1;
590 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
591 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
Tejun Heo4c360c82006-04-01 01:38:17 +0900595static const char *sata_spd_string(unsigned int spd)
596{
597 static const char * const spd_str[] = {
598 "1.5 Gbps",
599 "3.0 Gbps",
600 };
601
602 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
603 return "<unknown>";
604 return spd_str[spd - 1];
605}
606
Tejun Heo3373efd2006-05-15 20:57:53 +0900607void ata_dev_disable(struct ata_device *dev)
Tejun Heo0b8efb02006-03-24 15:25:31 +0900608{
Tejun Heo09d7f9b2007-06-25 23:34:02 +0900609 if (ata_dev_enabled(dev)) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900610 if (ata_msg_drv(dev->link->ap))
Tejun Heo09d7f9b2007-06-25 23:34:02 +0900611 ata_dev_printk(dev, KERN_WARNING, "disabled\n");
Tejun Heo4ae72a12007-02-02 16:22:30 +0900612 ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 |
613 ATA_DNXFER_QUIET);
Tejun Heo0b8efb02006-03-24 15:25:31 +0900614 dev->class++;
615 }
616}
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 * ata_devchk - PATA device presence detection
620 * @ap: ATA channel to examine
621 * @device: Device to examine (starting at zero)
622 *
Tejun Heo0d5ff562007-02-01 15:06:36 +0900623 * This technique was originally described in
624 * Hale Landis's ATADRVR (www.ata-atapi.com), and
625 * later found its way into the ATA/ATAPI spec.
626 *
627 * Write a pattern to the ATA shadow registers,
628 * and if a device is present, it will respond by
629 * correctly storing and echoing back the
630 * ATA shadow register contents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 *
632 * LOCKING:
633 * caller.
634 */
635
Tejun Heo0d5ff562007-02-01 15:06:36 +0900636static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900638 struct ata_ioports *ioaddr = &ap->ioaddr;
639 u8 nsect, lbal;
640
641 ap->ops->dev_select(ap, device);
642
643 iowrite8(0x55, ioaddr->nsect_addr);
644 iowrite8(0xaa, ioaddr->lbal_addr);
645
646 iowrite8(0xaa, ioaddr->nsect_addr);
647 iowrite8(0x55, ioaddr->lbal_addr);
648
649 iowrite8(0x55, ioaddr->nsect_addr);
650 iowrite8(0xaa, ioaddr->lbal_addr);
651
652 nsect = ioread8(ioaddr->nsect_addr);
653 lbal = ioread8(ioaddr->lbal_addr);
654
655 if ((nsect == 0x55) && (lbal == 0xaa))
656 return 1; /* we found a device */
657
658 return 0; /* nothing found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661/**
662 * ata_dev_classify - determine device type based on ATA-spec signature
663 * @tf: ATA taskfile register set for device to be identified
664 *
665 * Determine from taskfile register contents whether a device is
666 * ATA or ATAPI, as per "Signature and persistence" section
667 * of ATA/PI spec (volume 1, sect 5.14).
668 *
669 * LOCKING:
670 * None.
671 *
672 * RETURNS:
673 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
674 * the event of failure.
675 */
676
Jeff Garzik057ace52005-10-22 14:27:05 -0400677unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 /* Apple's open source Darwin code hints that some devices only
680 * put a proper signature into the LBA mid/high registers,
681 * So, we only check those. It's sufficient for uniqueness.
682 */
683
684 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
685 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
686 DPRINTK("found ATA device by sig\n");
687 return ATA_DEV_ATA;
688 }
689
690 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
691 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
692 DPRINTK("found ATAPI device by sig\n");
693 return ATA_DEV_ATAPI;
694 }
695
696 DPRINTK("unknown device\n");
697 return ATA_DEV_UNKNOWN;
698}
699
700/**
701 * ata_dev_try_classify - Parse returned ATA device signature
Tejun Heo3f198592007-09-02 23:23:57 +0900702 * @dev: ATA device to classify (starting at zero)
703 * @present: device seems present
Tejun Heob4dc7622006-01-24 17:05:22 +0900704 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 *
706 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
707 * an ATA/ATAPI-defined set of values is placed in the ATA
708 * shadow registers, indicating the results of device detection
709 * and diagnostics.
710 *
711 * Select the ATA device, and read the values from the ATA shadow
712 * registers. Then parse according to the Error register value,
713 * and the spec-defined values examined by ata_dev_classify().
714 *
715 * LOCKING:
716 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900717 *
718 * RETURNS:
719 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
Tejun Heo3f198592007-09-02 23:23:57 +0900721unsigned int ata_dev_try_classify(struct ata_device *dev, int present,
722 u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Tejun Heo3f198592007-09-02 23:23:57 +0900724 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 struct ata_taskfile tf;
726 unsigned int class;
727 u8 err;
728
Tejun Heo3f198592007-09-02 23:23:57 +0900729 ap->ops->dev_select(ap, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 memset(&tf, 0, sizeof(tf));
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400734 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900735 if (r_err)
736 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Alan Cox93590852006-09-12 16:55:12 +0100738 /* see if device passed diags: if master then continue and warn later */
Tejun Heo3f198592007-09-02 23:23:57 +0900739 if (err == 0 && dev->devno == 0)
Alan Cox93590852006-09-12 16:55:12 +0100740 /* diagnostic fail : do nothing _YET_ */
Tejun Heo3f198592007-09-02 23:23:57 +0900741 dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
Alan Cox93590852006-09-12 16:55:12 +0100742 else if (err == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 /* do nothing */ ;
Tejun Heo3f198592007-09-02 23:23:57 +0900744 else if ((dev->devno == 0) && (err == 0x81))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /* do nothing */ ;
746 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900747 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Tejun Heob4dc7622006-01-24 17:05:22 +0900749 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900751
Tejun Heod7fbee02007-09-02 23:24:48 +0900752 if (class == ATA_DEV_UNKNOWN) {
753 /* If the device failed diagnostic, it's likely to
754 * have reported incorrect device signature too.
755 * Assume ATA device if the device seems present but
756 * device signature is invalid with diagnostic
757 * failure.
758 */
759 if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
760 class = ATA_DEV_ATA;
761 else
762 class = ATA_DEV_NONE;
763 } else if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
764 class = ATA_DEV_NONE;
765
Tejun Heob4dc7622006-01-24 17:05:22 +0900766 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
769/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900770 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 * @id: IDENTIFY DEVICE results we will examine
772 * @s: string into which data is output
773 * @ofs: offset into identify device page
774 * @len: length of string to return. must be an even number.
775 *
776 * The strings in the IDENTIFY DEVICE page are broken up into
777 * 16-bit chunks. Run through the string, and output each
778 * 8-bit chunk linearly, regardless of platform.
779 *
780 * LOCKING:
781 * caller.
782 */
783
Tejun Heo6a62a042006-02-13 10:02:46 +0900784void ata_id_string(const u16 *id, unsigned char *s,
785 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 unsigned int c;
788
789 while (len > 0) {
790 c = id[ofs] >> 8;
791 *s = c;
792 s++;
793
794 c = id[ofs] & 0xff;
795 *s = c;
796 s++;
797
798 ofs++;
799 len -= 2;
800 }
801}
802
Tejun Heo0e949ff2006-02-12 22:47:04 +0900803/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900804 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900805 * @id: IDENTIFY DEVICE results we will examine
806 * @s: string into which data is output
807 * @ofs: offset into identify device page
808 * @len: length of string to return. must be an odd number.
809 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900810 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900811 * trims trailing spaces and terminates the resulting string with
812 * null. @len must be actual maximum length (even number) + 1.
813 *
814 * LOCKING:
815 * caller.
816 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900817void ata_id_c_string(const u16 *id, unsigned char *s,
818 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900819{
820 unsigned char *p;
821
822 WARN_ON(!(len & 1));
823
Tejun Heo6a62a042006-02-13 10:02:46 +0900824 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900825
826 p = s + strnlen(s, len - 1);
827 while (p > s && p[-1] == ' ')
828 p--;
829 *p = '\0';
830}
Edward Falk0baab862005-06-02 18:17:13 -0400831
Tejun Heodb6f8752007-09-03 12:31:58 +0900832static u64 ata_id_n_sectors(const u16 *id)
833{
834 if (ata_id_has_lba(id)) {
835 if (ata_id_has_lba48(id))
836 return ata_id_u64(id, 100);
837 else
838 return ata_id_u32(id, 60);
839 } else {
840 if (ata_id_current_chs_valid(id))
841 return ata_id_u32(id, 57);
842 else
843 return id[1] * id[3] * id[6];
844 }
845}
846
Alan Cox1e999732007-04-11 00:23:13 +0100847static u64 ata_tf_to_lba48(struct ata_taskfile *tf)
848{
849 u64 sectors = 0;
850
851 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
852 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
853 sectors |= (tf->hob_lbal & 0xff) << 24;
854 sectors |= (tf->lbah & 0xff) << 16;
855 sectors |= (tf->lbam & 0xff) << 8;
856 sectors |= (tf->lbal & 0xff);
857
858 return ++sectors;
859}
860
861static u64 ata_tf_to_lba(struct ata_taskfile *tf)
862{
863 u64 sectors = 0;
864
865 sectors |= (tf->device & 0x0f) << 24;
866 sectors |= (tf->lbah & 0xff) << 16;
867 sectors |= (tf->lbam & 0xff) << 8;
868 sectors |= (tf->lbal & 0xff);
869
870 return ++sectors;
871}
872
873/**
Tejun Heoc728a912007-09-03 12:32:30 +0900874 * ata_read_native_max_address - Read native max address
875 * @dev: target device
876 * @max_sectors: out parameter for the result native max address
Alan Cox1e999732007-04-11 00:23:13 +0100877 *
Tejun Heoc728a912007-09-03 12:32:30 +0900878 * Perform an LBA48 or LBA28 native size query upon the device in
879 * question.
880 *
881 * RETURNS:
882 * 0 on success, -EACCES if command is aborted by the drive.
883 * -EIO on other errors.
Alan Cox1e999732007-04-11 00:23:13 +0100884 */
Tejun Heoc728a912007-09-03 12:32:30 +0900885static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
Alan Cox1e999732007-04-11 00:23:13 +0100886{
Tejun Heoc728a912007-09-03 12:32:30 +0900887 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +0100888 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +0900889 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +0100890
891 ata_tf_init(dev, &tf);
892
Tejun Heoc728a912007-09-03 12:32:30 +0900893 /* always clear all address registers */
Alan Cox1e999732007-04-11 00:23:13 +0100894 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +0900895
896 if (lba48) {
897 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
898 tf.flags |= ATA_TFLAG_LBA48;
899 } else
900 tf.command = ATA_CMD_READ_NATIVE_MAX;
901
Alan Cox1e999732007-04-11 00:23:13 +0100902 tf.protocol |= ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +0900903 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +0100904
Tejun Heoc728a912007-09-03 12:32:30 +0900905 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
906 if (err_mask) {
907 ata_dev_printk(dev, KERN_WARNING, "failed to read native "
908 "max address (err_mask=0x%x)\n", err_mask);
909 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
910 return -EACCES;
911 return -EIO;
912 }
Alan Cox1e999732007-04-11 00:23:13 +0100913
Tejun Heoc728a912007-09-03 12:32:30 +0900914 if (lba48)
915 *max_sectors = ata_tf_to_lba48(&tf);
916 else
917 *max_sectors = ata_tf_to_lba(&tf);
918
919 return 0;
Alan Cox1e999732007-04-11 00:23:13 +0100920}
921
922/**
Tejun Heoc728a912007-09-03 12:32:30 +0900923 * ata_set_max_sectors - Set max sectors
924 * @dev: target device
Randy Dunlap6b38d1d2007-05-01 17:35:55 -0700925 * @new_sectors: new max sectors value to set for the device
Alan Cox1e999732007-04-11 00:23:13 +0100926 *
Tejun Heoc728a912007-09-03 12:32:30 +0900927 * Set max sectors of @dev to @new_sectors.
928 *
929 * RETURNS:
930 * 0 on success, -EACCES if command is aborted or denied (due to
931 * previous non-volatile SET_MAX) by the drive. -EIO on other
932 * errors.
Alan Cox1e999732007-04-11 00:23:13 +0100933 */
Tejun Heo05027ad2007-09-03 12:32:57 +0900934static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
Alan Cox1e999732007-04-11 00:23:13 +0100935{
Tejun Heoc728a912007-09-03 12:32:30 +0900936 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +0100937 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +0900938 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +0100939
940 new_sectors--;
941
942 ata_tf_init(dev, &tf);
943
Alan Cox1e999732007-04-11 00:23:13 +0100944 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +0900945
946 if (lba48) {
947 tf.command = ATA_CMD_SET_MAX_EXT;
948 tf.flags |= ATA_TFLAG_LBA48;
949
950 tf.hob_lbal = (new_sectors >> 24) & 0xff;
951 tf.hob_lbam = (new_sectors >> 32) & 0xff;
952 tf.hob_lbah = (new_sectors >> 40) & 0xff;
Tejun Heo1e582ba2007-09-21 20:07:14 +0900953 } else {
Tejun Heoc728a912007-09-03 12:32:30 +0900954 tf.command = ATA_CMD_SET_MAX;
955
Tejun Heo1e582ba2007-09-21 20:07:14 +0900956 tf.device |= (new_sectors >> 24) & 0xf;
957 }
958
Alan Cox1e999732007-04-11 00:23:13 +0100959 tf.protocol |= ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +0900960 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +0100961
962 tf.lbal = (new_sectors >> 0) & 0xff;
963 tf.lbam = (new_sectors >> 8) & 0xff;
964 tf.lbah = (new_sectors >> 16) & 0xff;
Alan Cox1e999732007-04-11 00:23:13 +0100965
Tejun Heoc728a912007-09-03 12:32:30 +0900966 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
967 if (err_mask) {
968 ata_dev_printk(dev, KERN_WARNING, "failed to set "
969 "max address (err_mask=0x%x)\n", err_mask);
970 if (err_mask == AC_ERR_DEV &&
971 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
972 return -EACCES;
973 return -EIO;
974 }
Alan Cox1e999732007-04-11 00:23:13 +0100975
Tejun Heoc728a912007-09-03 12:32:30 +0900976 return 0;
Alan Cox1e999732007-04-11 00:23:13 +0100977}
978
979/**
980 * ata_hpa_resize - Resize a device with an HPA set
981 * @dev: Device to resize
982 *
983 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
984 * it if required to the full size of the media. The caller must check
985 * the drive has the HPA feature set enabled.
Tejun Heo05027ad2007-09-03 12:32:57 +0900986 *
987 * RETURNS:
988 * 0 on success, -errno on failure.
Alan Cox1e999732007-04-11 00:23:13 +0100989 */
Tejun Heo05027ad2007-09-03 12:32:57 +0900990static int ata_hpa_resize(struct ata_device *dev)
Alan Cox1e999732007-04-11 00:23:13 +0100991{
Tejun Heo05027ad2007-09-03 12:32:57 +0900992 struct ata_eh_context *ehc = &dev->link->eh_context;
993 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
994 u64 sectors = ata_id_n_sectors(dev->id);
995 u64 native_sectors;
Tejun Heoc728a912007-09-03 12:32:30 +0900996 int rc;
Jeff Garzika617c092007-05-21 20:14:23 -0400997
Tejun Heo05027ad2007-09-03 12:32:57 +0900998 /* do we need to do it? */
999 if (dev->class != ATA_DEV_ATA ||
1000 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1001 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
Tejun Heoc728a912007-09-03 12:32:30 +09001002 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001003
Tejun Heo05027ad2007-09-03 12:32:57 +09001004 /* read native max address */
1005 rc = ata_read_native_max_address(dev, &native_sectors);
1006 if (rc) {
1007 /* If HPA isn't going to be unlocked, skip HPA
1008 * resizing from the next try.
1009 */
1010 if (!ata_ignore_hpa) {
1011 ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
1012 "broken, will skip HPA handling\n");
1013 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
Alan Cox1e999732007-04-11 00:23:13 +01001014
Tejun Heo05027ad2007-09-03 12:32:57 +09001015 /* we can continue if device aborted the command */
1016 if (rc == -EACCES)
1017 rc = 0;
Alan Cox1e999732007-04-11 00:23:13 +01001018 }
Tejun Heo37301a52007-06-25 20:45:54 +09001019
Tejun Heo05027ad2007-09-03 12:32:57 +09001020 return rc;
1021 }
1022
1023 /* nothing to do? */
1024 if (native_sectors <= sectors || !ata_ignore_hpa) {
1025 if (!print_info || native_sectors == sectors)
1026 return 0;
1027
1028 if (native_sectors > sectors)
1029 ata_dev_printk(dev, KERN_INFO,
1030 "HPA detected: current %llu, native %llu\n",
1031 (unsigned long long)sectors,
1032 (unsigned long long)native_sectors);
1033 else if (native_sectors < sectors)
1034 ata_dev_printk(dev, KERN_WARNING,
1035 "native sectors (%llu) is smaller than "
1036 "sectors (%llu)\n",
1037 (unsigned long long)native_sectors,
1038 (unsigned long long)sectors);
1039 return 0;
1040 }
1041
1042 /* let's unlock HPA */
1043 rc = ata_set_max_sectors(dev, native_sectors);
1044 if (rc == -EACCES) {
1045 /* if device aborted the command, skip HPA resizing */
1046 ata_dev_printk(dev, KERN_WARNING, "device aborted resize "
1047 "(%llu -> %llu), skipping HPA handling\n",
1048 (unsigned long long)sectors,
1049 (unsigned long long)native_sectors);
1050 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1051 return 0;
1052 } else if (rc)
1053 return rc;
1054
1055 /* re-read IDENTIFY data */
1056 rc = ata_dev_reread_id(dev, 0);
1057 if (rc) {
1058 ata_dev_printk(dev, KERN_ERR, "failed to re-read IDENTIFY "
1059 "data after HPA resizing\n");
1060 return rc;
1061 }
1062
1063 if (print_info) {
1064 u64 new_sectors = ata_id_n_sectors(dev->id);
1065 ata_dev_printk(dev, KERN_INFO,
1066 "HPA unlocked: %llu -> %llu, native %llu\n",
1067 (unsigned long long)sectors,
1068 (unsigned long long)new_sectors,
1069 (unsigned long long)native_sectors);
1070 }
1071
1072 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001073}
1074
Edward Falk0baab862005-06-02 18:17:13 -04001075/**
Alan10305f02007-02-20 18:01:59 +00001076 * ata_id_to_dma_mode - Identify DMA mode from id block
1077 * @dev: device to identify
Randy Dunlapcc261262007-03-16 19:55:47 -07001078 * @unknown: mode to assume if we cannot tell
Alan10305f02007-02-20 18:01:59 +00001079 *
1080 * Set up the timing values for the device based upon the identify
1081 * reported values for the DMA mode. This function is used by drivers
1082 * which rely upon firmware configured modes, but wish to report the
1083 * mode correctly when possible.
1084 *
1085 * In addition we emit similarly formatted messages to the default
1086 * ata_dev_set_mode handler, in order to provide consistency of
1087 * presentation.
1088 */
1089
1090void ata_id_to_dma_mode(struct ata_device *dev, u8 unknown)
1091{
1092 unsigned int mask;
1093 u8 mode;
1094
1095 /* Pack the DMA modes */
1096 mask = ((dev->id[63] >> 8) << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA;
1097 if (dev->id[53] & 0x04)
1098 mask |= ((dev->id[88] >> 8) << ATA_SHIFT_UDMA) & ATA_MASK_UDMA;
1099
1100 /* Select the mode in use */
1101 mode = ata_xfer_mask2mode(mask);
1102
1103 if (mode != 0) {
1104 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
1105 ata_mode_string(mask));
1106 } else {
1107 /* SWDMA perhaps ? */
1108 mode = unknown;
1109 ata_dev_printk(dev, KERN_INFO, "configured for DMA\n");
1110 }
1111
1112 /* Configure the device reporting */
1113 dev->xfer_mode = mode;
1114 dev->xfer_shift = ata_xfer_mode2shift(mode);
1115}
1116
1117/**
Edward Falk0baab862005-06-02 18:17:13 -04001118 * ata_noop_dev_select - Select device 0/1 on ATA bus
1119 * @ap: ATA channel to manipulate
1120 * @device: ATA device (numbered from zero) to select
1121 *
1122 * This function performs no actual function.
1123 *
1124 * May be used as the dev_select() entry in ata_port_operations.
1125 *
1126 * LOCKING:
1127 * caller.
1128 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
1130{
1131}
1132
Edward Falk0baab862005-06-02 18:17:13 -04001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134/**
1135 * ata_std_dev_select - Select device 0/1 on ATA bus
1136 * @ap: ATA channel to manipulate
1137 * @device: ATA device (numbered from zero) to select
1138 *
1139 * Use the method defined in the ATA specification to
1140 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -04001141 * ATA channel. Works with both PIO and MMIO.
1142 *
1143 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 *
1145 * LOCKING:
1146 * caller.
1147 */
1148
1149void ata_std_dev_select (struct ata_port *ap, unsigned int device)
1150{
1151 u8 tmp;
1152
1153 if (device == 0)
1154 tmp = ATA_DEVICE_OBS;
1155 else
1156 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1157
Tejun Heo0d5ff562007-02-01 15:06:36 +09001158 iowrite8(tmp, ap->ioaddr.device_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 ata_pause(ap); /* needed; also flushes, for mmio */
1160}
1161
1162/**
1163 * ata_dev_select - Select device 0/1 on ATA bus
1164 * @ap: ATA channel to manipulate
1165 * @device: ATA device (numbered from zero) to select
1166 * @wait: non-zero to wait for Status register BSY bit to clear
1167 * @can_sleep: non-zero if context allows sleeping
1168 *
1169 * Use the method defined in the ATA specification to
1170 * make either device 0, or device 1, active on the
1171 * ATA channel.
1172 *
1173 * This is a high-level version of ata_std_dev_select(),
1174 * which additionally provides the services of inserting
1175 * the proper pauses and status polling, where needed.
1176 *
1177 * LOCKING:
1178 * caller.
1179 */
1180
1181void ata_dev_select(struct ata_port *ap, unsigned int device,
1182 unsigned int wait, unsigned int can_sleep)
1183{
Tejun Heo88574552006-06-25 20:00:35 +09001184 if (ata_msg_probe(ap))
Tejun Heo44877b42007-02-21 01:06:51 +09001185 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, "
1186 "device %u, wait %u\n", device, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 if (wait)
1189 ata_wait_idle(ap);
1190
1191 ap->ops->dev_select(ap, device);
1192
1193 if (wait) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001194 if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 msleep(150);
1196 ata_wait_idle(ap);
1197 }
1198}
1199
1200/**
1201 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +09001202 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 *
Tejun Heo0bd33002006-02-12 22:47:05 +09001204 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1205 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 *
1207 * LOCKING:
1208 * caller.
1209 */
1210
Tejun Heo0bd33002006-02-12 22:47:05 +09001211static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
1213 DPRINTK("49==0x%04x "
1214 "53==0x%04x "
1215 "63==0x%04x "
1216 "64==0x%04x "
1217 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001218 id[49],
1219 id[53],
1220 id[63],
1221 id[64],
1222 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 DPRINTK("80==0x%04x "
1224 "81==0x%04x "
1225 "82==0x%04x "
1226 "83==0x%04x "
1227 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001228 id[80],
1229 id[81],
1230 id[82],
1231 id[83],
1232 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 DPRINTK("88==0x%04x "
1234 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001235 id[88],
1236 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
Tejun Heocb95d562006-03-06 04:31:56 +09001239/**
1240 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1241 * @id: IDENTIFY data to compute xfer mask from
1242 *
1243 * Compute the xfermask for this device. This is not as trivial
1244 * as it seems if we must consider early devices correctly.
1245 *
1246 * FIXME: pre IDE drive timing (do we care ?).
1247 *
1248 * LOCKING:
1249 * None.
1250 *
1251 * RETURNS:
1252 * Computed xfermask
1253 */
1254static unsigned int ata_id_xfermask(const u16 *id)
1255{
1256 unsigned int pio_mask, mwdma_mask, udma_mask;
1257
1258 /* Usual case. Word 53 indicates word 64 is valid */
1259 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1260 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1261 pio_mask <<= 3;
1262 pio_mask |= 0x7;
1263 } else {
1264 /* If word 64 isn't valid then Word 51 high byte holds
1265 * the PIO timing number for the maximum. Turn it into
1266 * a mask.
1267 */
Lennert Buytenhek7a0f1c82007-01-29 13:28:47 +01001268 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
Alan Cox46767aeb2006-09-29 18:26:47 +01001269 if (mode < 5) /* Valid PIO range */
1270 pio_mask = (2 << mode) - 1;
1271 else
1272 pio_mask = 1;
Tejun Heocb95d562006-03-06 04:31:56 +09001273
1274 /* But wait.. there's more. Design your standards by
1275 * committee and you too can get a free iordy field to
1276 * process. However its the speeds not the modes that
1277 * are supported... Note drivers using the timing API
1278 * will get this right anyway
1279 */
1280 }
1281
1282 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +09001283
Alan Coxb352e572006-08-10 18:52:12 +01001284 if (ata_id_is_cfa(id)) {
1285 /*
1286 * Process compact flash extended modes
1287 */
1288 int pio = id[163] & 0x7;
1289 int dma = (id[163] >> 3) & 7;
1290
1291 if (pio)
1292 pio_mask |= (1 << 5);
1293 if (pio > 1)
1294 pio_mask |= (1 << 6);
1295 if (dma)
1296 mwdma_mask |= (1 << 3);
1297 if (dma > 1)
1298 mwdma_mask |= (1 << 4);
1299 }
1300
Tejun Heofb21f0d2006-03-12 12:34:35 +09001301 udma_mask = 0;
1302 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1303 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +09001304
1305 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1306}
1307
Tejun Heo86e45b62006-03-05 15:29:09 +09001308/**
1309 * ata_port_queue_task - Queue port_task
1310 * @ap: The ata_port to queue port_task for
Randy Dunlape2a7f772006-05-18 10:50:18 -07001311 * @fn: workqueue function to be scheduled
David Howells65f27f32006-11-22 14:55:48 +00001312 * @data: data for @fn to use
Randy Dunlape2a7f772006-05-18 10:50:18 -07001313 * @delay: delay time for workqueue function
Tejun Heo86e45b62006-03-05 15:29:09 +09001314 *
1315 * Schedule @fn(@data) for execution after @delay jiffies using
1316 * port_task. There is one port_task per port and it's the
1317 * user(low level driver)'s responsibility to make sure that only
1318 * one task is active at any given time.
1319 *
1320 * libata core layer takes care of synchronization between
1321 * port_task and EH. ata_port_queue_task() may be ignored for EH
1322 * synchronization.
1323 *
1324 * LOCKING:
1325 * Inherited from caller.
1326 */
David Howells65f27f32006-11-22 14:55:48 +00001327void ata_port_queue_task(struct ata_port *ap, work_func_t fn, void *data,
Tejun Heo86e45b62006-03-05 15:29:09 +09001328 unsigned long delay)
1329{
David Howells65f27f32006-11-22 14:55:48 +00001330 PREPARE_DELAYED_WORK(&ap->port_task, fn);
1331 ap->port_task_data = data;
Tejun Heo86e45b62006-03-05 15:29:09 +09001332
Oleg Nesterov45a66c12007-07-09 11:46:13 -07001333 /* may fail if ata_port_flush_task() in progress */
1334 queue_delayed_work(ata_wq, &ap->port_task, delay);
Tejun Heo86e45b62006-03-05 15:29:09 +09001335}
1336
1337/**
1338 * ata_port_flush_task - Flush port_task
1339 * @ap: The ata_port to flush port_task for
1340 *
1341 * After this function completes, port_task is guranteed not to
1342 * be running or scheduled.
1343 *
1344 * LOCKING:
1345 * Kernel thread context (may sleep)
1346 */
1347void ata_port_flush_task(struct ata_port *ap)
1348{
Tejun Heo86e45b62006-03-05 15:29:09 +09001349 DPRINTK("ENTER\n");
1350
Oleg Nesterov45a66c12007-07-09 11:46:13 -07001351 cancel_rearming_delayed_work(&ap->port_task);
Tejun Heo86e45b62006-03-05 15:29:09 +09001352
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001353 if (ata_msg_ctl(ap))
1354 ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __FUNCTION__);
Tejun Heo86e45b62006-03-05 15:29:09 +09001355}
1356
Adrian Bunk7102d232007-01-04 00:09:36 +01001357static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001358{
Tejun Heo77853bf2006-01-23 13:09:36 +09001359 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001360
Tejun Heoa2a7a662005-12-13 14:48:31 +09001361 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001362}
1363
1364/**
Tejun Heo24326972006-11-14 22:47:09 +09001365 * ata_exec_internal_sg - execute libata internal command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001366 * @dev: Device to which the command is sent
1367 * @tf: Taskfile registers for the command and the result
Tejun Heod69cf372006-04-02 18:51:53 +09001368 * @cdb: CDB for packet command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001369 * @dma_dir: Data tranfer direction of the command
Tejun Heo24326972006-11-14 22:47:09 +09001370 * @sg: sg list for the data buffer of the command
1371 * @n_elem: Number of sg entries
Tejun Heoa2a7a662005-12-13 14:48:31 +09001372 *
1373 * Executes libata internal command with timeout. @tf contains
1374 * command on entry and result on return. Timeout and error
1375 * conditions are reported via return value. No recovery action
1376 * is taken after a command times out. It's caller's duty to
1377 * clean up after timeout.
1378 *
1379 * LOCKING:
1380 * None. Should be called with kernel context, might sleep.
Tejun Heo551e8882006-06-12 14:09:49 +09001381 *
1382 * RETURNS:
1383 * Zero on success, AC_ERR_* mask on failure
Tejun Heoa2a7a662005-12-13 14:48:31 +09001384 */
Tejun Heo24326972006-11-14 22:47:09 +09001385unsigned ata_exec_internal_sg(struct ata_device *dev,
1386 struct ata_taskfile *tf, const u8 *cdb,
1387 int dma_dir, struct scatterlist *sg,
1388 unsigned int n_elem)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001389{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001390 struct ata_link *link = dev->link;
1391 struct ata_port *ap = link->ap;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001392 u8 command = tf->command;
1393 struct ata_queued_cmd *qc;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001394 unsigned int tag, preempted_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001395 u32 preempted_sactive, preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001396 int preempted_nr_active_links;
Ingo Molnar60be6b92006-07-03 00:25:26 -07001397 DECLARE_COMPLETION_ONSTACK(wait);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001398 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001399 unsigned int err_mask;
Tejun Heod95a7172006-05-15 20:58:14 +09001400 int rc;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001401
Jeff Garzikba6a1302006-06-22 23:46:10 -04001402 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001403
Tejun Heoe3180492006-05-15 20:58:09 +09001404 /* no internal command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09001405 if (ap->pflags & ATA_PFLAG_FROZEN) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001406 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001407 return AC_ERR_SYSTEM;
1408 }
1409
Tejun Heo2ab7db12006-05-15 20:58:02 +09001410 /* initialize internal qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001411
Tejun Heo2ab7db12006-05-15 20:58:02 +09001412 /* XXX: Tag 0 is used for drivers with legacy EH as some
1413 * drivers choke if any other tag is given. This breaks
1414 * ata_tag_internal() test for those drivers. Don't use new
1415 * EH stuff without converting to it.
1416 */
1417 if (ap->ops->error_handler)
1418 tag = ATA_TAG_INTERNAL;
1419 else
1420 tag = 0;
1421
Tejun Heo6cec4a32006-05-15 21:03:41 +09001422 if (test_and_set_bit(tag, &ap->qc_allocated))
Tejun Heo2ab7db12006-05-15 20:58:02 +09001423 BUG();
Tejun Heof69499f2006-05-15 20:58:03 +09001424 qc = __ata_qc_from_tag(ap, tag);
Tejun Heo2ab7db12006-05-15 20:58:02 +09001425
1426 qc->tag = tag;
1427 qc->scsicmd = NULL;
1428 qc->ap = ap;
1429 qc->dev = dev;
1430 ata_qc_reinit(qc);
1431
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001432 preempted_tag = link->active_tag;
1433 preempted_sactive = link->sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001434 preempted_qc_active = ap->qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001435 preempted_nr_active_links = ap->nr_active_links;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001436 link->active_tag = ATA_TAG_POISON;
1437 link->sactive = 0;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001438 ap->qc_active = 0;
Tejun Heoda917d62007-09-23 13:14:12 +09001439 ap->nr_active_links = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001440
1441 /* prepare & issue qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001442 qc->tf = *tf;
Tejun Heod69cf372006-04-02 18:51:53 +09001443 if (cdb)
1444 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
Tejun Heoe61e0672006-05-15 20:57:40 +09001445 qc->flags |= ATA_QCFLAG_RESULT_TF;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001446 qc->dma_dir = dma_dir;
1447 if (dma_dir != DMA_NONE) {
Tejun Heo24326972006-11-14 22:47:09 +09001448 unsigned int i, buflen = 0;
1449
1450 for (i = 0; i < n_elem; i++)
1451 buflen += sg[i].length;
1452
1453 ata_sg_init(qc, sg, n_elem);
Brian King49c80422007-01-30 11:32:26 -06001454 qc->nbytes = buflen;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001455 }
1456
Tejun Heo77853bf2006-01-23 13:09:36 +09001457 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001458 qc->complete_fn = ata_qc_complete_internal;
1459
Tejun Heo8e0e6942006-03-31 20:41:11 +09001460 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001461
Jeff Garzikba6a1302006-06-22 23:46:10 -04001462 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001463
Andrew Mortona8601e52006-06-25 01:36:52 -07001464 rc = wait_for_completion_timeout(&wait, ata_probe_timeout);
Albert Lee41ade502006-03-14 11:19:04 +08001465
Tejun Heod95a7172006-05-15 20:58:14 +09001466 ata_port_flush_task(ap);
1467
1468 if (!rc) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001469 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001470
1471 /* We're racing with irq here. If we lose, the
1472 * following test prevents us from completing the qc
Tejun Heod95a7172006-05-15 20:58:14 +09001473 * twice. If we win, the port is frozen and will be
1474 * cleaned up by ->post_internal_cmd().
Tejun Heoa2a7a662005-12-13 14:48:31 +09001475 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001476 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heod95a7172006-05-15 20:58:14 +09001477 qc->err_mask |= AC_ERR_TIMEOUT;
1478
1479 if (ap->ops->error_handler)
1480 ata_port_freeze(ap);
1481 else
1482 ata_qc_complete(qc);
Tejun Heof15a1da2006-05-15 20:57:56 +09001483
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001484 if (ata_msg_warn(ap))
1485 ata_dev_printk(dev, KERN_WARNING,
Tejun Heo88574552006-06-25 20:00:35 +09001486 "qc timeout (cmd 0x%x)\n", command);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001487 }
1488
Jeff Garzikba6a1302006-06-22 23:46:10 -04001489 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001490 }
1491
Tejun Heod95a7172006-05-15 20:58:14 +09001492 /* do post_internal_cmd */
1493 if (ap->ops->post_internal_cmd)
1494 ap->ops->post_internal_cmd(qc);
1495
Tejun Heoa51d6442007-03-20 15:24:11 +09001496 /* perform minimal error analysis */
1497 if (qc->flags & ATA_QCFLAG_FAILED) {
1498 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1499 qc->err_mask |= AC_ERR_DEV;
1500
1501 if (!qc->err_mask)
1502 qc->err_mask |= AC_ERR_OTHER;
1503
1504 if (qc->err_mask & ~AC_ERR_OTHER)
1505 qc->err_mask &= ~AC_ERR_OTHER;
Tejun Heod95a7172006-05-15 20:58:14 +09001506 }
1507
Tejun Heo15869302006-05-15 20:57:33 +09001508 /* finish up */
Jeff Garzikba6a1302006-06-22 23:46:10 -04001509 spin_lock_irqsave(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001510
Tejun Heoe61e0672006-05-15 20:57:40 +09001511 *tf = qc->result_tf;
Tejun Heo77853bf2006-01-23 13:09:36 +09001512 err_mask = qc->err_mask;
1513
1514 ata_qc_free(qc);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001515 link->active_tag = preempted_tag;
1516 link->sactive = preempted_sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001517 ap->qc_active = preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001518 ap->nr_active_links = preempted_nr_active_links;
Tejun Heo77853bf2006-01-23 13:09:36 +09001519
Tejun Heo1f7dd3e92006-03-24 15:25:30 +09001520 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1521 * Until those drivers are fixed, we detect the condition
1522 * here, fail the command with AC_ERR_SYSTEM and reenable the
1523 * port.
1524 *
1525 * Note that this doesn't change any behavior as internal
1526 * command failure results in disabling the device in the
1527 * higher layer for LLDDs without new reset/EH callbacks.
1528 *
1529 * Kill the following code as soon as those drivers are fixed.
1530 */
Tejun Heo198e0fe2006-04-02 18:51:52 +09001531 if (ap->flags & ATA_FLAG_DISABLED) {
Tejun Heo1f7dd3e92006-03-24 15:25:30 +09001532 err_mask |= AC_ERR_SYSTEM;
1533 ata_port_probe(ap);
1534 }
1535
Jeff Garzikba6a1302006-06-22 23:46:10 -04001536 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001537
Tejun Heo77853bf2006-01-23 13:09:36 +09001538 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001539}
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541/**
Tejun Heo33480a02006-12-12 02:15:31 +09001542 * ata_exec_internal - execute libata internal command
Tejun Heo24326972006-11-14 22:47:09 +09001543 * @dev: Device to which the command is sent
1544 * @tf: Taskfile registers for the command and the result
1545 * @cdb: CDB for packet command
1546 * @dma_dir: Data tranfer direction of the command
1547 * @buf: Data buffer of the command
1548 * @buflen: Length of data buffer
1549 *
1550 * Wrapper around ata_exec_internal_sg() which takes simple
1551 * buffer instead of sg list.
1552 *
1553 * LOCKING:
1554 * None. Should be called with kernel context, might sleep.
1555 *
1556 * RETURNS:
1557 * Zero on success, AC_ERR_* mask on failure
1558 */
1559unsigned ata_exec_internal(struct ata_device *dev,
1560 struct ata_taskfile *tf, const u8 *cdb,
1561 int dma_dir, void *buf, unsigned int buflen)
1562{
Tejun Heo33480a02006-12-12 02:15:31 +09001563 struct scatterlist *psg = NULL, sg;
1564 unsigned int n_elem = 0;
Tejun Heo24326972006-11-14 22:47:09 +09001565
Tejun Heo33480a02006-12-12 02:15:31 +09001566 if (dma_dir != DMA_NONE) {
1567 WARN_ON(!buf);
1568 sg_init_one(&sg, buf, buflen);
1569 psg = &sg;
1570 n_elem++;
1571 }
Tejun Heo24326972006-11-14 22:47:09 +09001572
Tejun Heo33480a02006-12-12 02:15:31 +09001573 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem);
Tejun Heo24326972006-11-14 22:47:09 +09001574}
1575
1576/**
Tejun Heo977e6b92006-06-24 20:30:19 +09001577 * ata_do_simple_cmd - execute simple internal command
1578 * @dev: Device to which the command is sent
1579 * @cmd: Opcode to execute
1580 *
1581 * Execute a 'simple' command, that only consists of the opcode
1582 * 'cmd' itself, without filling any other registers
1583 *
1584 * LOCKING:
1585 * Kernel thread context (may sleep).
1586 *
1587 * RETURNS:
1588 * Zero on success, AC_ERR_* mask on failure
Tejun Heoe58eb582006-06-24 20:30:19 +09001589 */
Tejun Heo77b08fb2006-06-24 20:30:19 +09001590unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
Tejun Heoe58eb582006-06-24 20:30:19 +09001591{
1592 struct ata_taskfile tf;
Tejun Heoe58eb582006-06-24 20:30:19 +09001593
1594 ata_tf_init(dev, &tf);
1595
1596 tf.command = cmd;
1597 tf.flags |= ATA_TFLAG_DEVICE;
1598 tf.protocol = ATA_PROT_NODATA;
1599
Tejun Heo977e6b92006-06-24 20:30:19 +09001600 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Tejun Heoe58eb582006-06-24 20:30:19 +09001601}
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001604 * ata_pio_need_iordy - check if iordy needed
1605 * @adev: ATA device
1606 *
1607 * Check if the current speed of the device requires IORDY. Used
1608 * by various controllers for chip configuration.
1609 */
Jeff Garzika617c092007-05-21 20:14:23 -04001610
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001611unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1612{
Alan Cox432729f2007-03-08 23:22:59 +00001613 /* Controller doesn't support IORDY. Probably a pointless check
1614 as the caller should know this */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001615 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001616 return 0;
Alan Cox432729f2007-03-08 23:22:59 +00001617 /* PIO3 and higher it is mandatory */
1618 if (adev->pio_mode > XFER_PIO_2)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001619 return 1;
Alan Cox432729f2007-03-08 23:22:59 +00001620 /* We turn it on when possible */
1621 if (ata_id_has_iordy(adev->id))
1622 return 1;
1623 return 0;
1624}
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001625
Alan Cox432729f2007-03-08 23:22:59 +00001626/**
1627 * ata_pio_mask_no_iordy - Return the non IORDY mask
1628 * @adev: ATA device
1629 *
1630 * Compute the highest mode possible if we are not using iordy. Return
1631 * -1 if no iordy mode is available.
1632 */
Jeff Garzika617c092007-05-21 20:14:23 -04001633
Alan Cox432729f2007-03-08 23:22:59 +00001634static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1635{
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001636 /* If we have no drive specific rule, then PIO 2 is non IORDY */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001637 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
Alan Cox432729f2007-03-08 23:22:59 +00001638 u16 pio = adev->id[ATA_ID_EIDE_PIO];
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001639 /* Is the speed faster than the drive allows non IORDY ? */
1640 if (pio) {
1641 /* This is cycle times not frequency - watch the logic! */
1642 if (pio > 240) /* PIO2 is 240nS per cycle */
Alan Cox432729f2007-03-08 23:22:59 +00001643 return 3 << ATA_SHIFT_PIO;
1644 return 7 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001645 }
1646 }
Alan Cox432729f2007-03-08 23:22:59 +00001647 return 3 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001648}
1649
1650/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001651 * ata_dev_read_id - Read ID data from the specified device
Tejun Heo49016ac2006-02-21 02:12:11 +09001652 * @dev: target device
1653 * @p_class: pointer to class of the target device (may be changed)
Tejun Heobff04642006-11-10 18:08:10 +09001654 * @flags: ATA_READID_* flags
Tejun Heofe635c72006-05-15 20:57:35 +09001655 * @id: buffer to read IDENTIFY data into
Tejun Heo49016ac2006-02-21 02:12:11 +09001656 *
1657 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1658 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09001659 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1660 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09001661 *
Alan Cox50a99012007-08-08 14:27:00 +01001662 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
1663 * now we abort if we hit that case.
1664 *
Tejun Heo49016ac2006-02-21 02:12:11 +09001665 * LOCKING:
1666 * Kernel thread context (may sleep)
1667 *
1668 * RETURNS:
1669 * 0 on success, -errno otherwise.
1670 */
Tejun Heoa9beec92006-05-31 18:27:44 +09001671int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
Tejun Heobff04642006-11-10 18:08:10 +09001672 unsigned int flags, u16 *id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001673{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001674 struct ata_port *ap = dev->link->ap;
Tejun Heo49016ac2006-02-21 02:12:11 +09001675 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09001676 struct ata_taskfile tf;
1677 unsigned int err_mask = 0;
1678 const char *reason;
Tejun Heo54936f82007-05-11 14:35:29 +02001679 int may_fallback = 1, tried_spinup = 0;
Tejun Heo49016ac2006-02-21 02:12:11 +09001680 int rc;
1681
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001682 if (ata_msg_ctl(ap))
Tejun Heo44877b42007-02-21 01:06:51 +09001683 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
Tejun Heo49016ac2006-02-21 02:12:11 +09001684
Tejun Heo49016ac2006-02-21 02:12:11 +09001685 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
Tejun Heo49016ac2006-02-21 02:12:11 +09001686 retry:
Tejun Heo3373efd2006-05-15 20:57:53 +09001687 ata_tf_init(dev, &tf);
Tejun Heo49016ac2006-02-21 02:12:11 +09001688
1689 switch (class) {
1690 case ATA_DEV_ATA:
1691 tf.command = ATA_CMD_ID_ATA;
1692 break;
1693 case ATA_DEV_ATAPI:
1694 tf.command = ATA_CMD_ID_ATAPI;
1695 break;
1696 default:
1697 rc = -ENODEV;
1698 reason = "unsupported class";
1699 goto err_out;
1700 }
1701
1702 tf.protocol = ATA_PROT_PIO;
Tejun Heo81afe892007-02-07 12:37:41 -08001703
1704 /* Some devices choke if TF registers contain garbage. Make
1705 * sure those are properly initialized.
1706 */
1707 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1708
1709 /* Device presence detection is unreliable on some
1710 * controllers. Always poll IDENTIFY if available.
1711 */
1712 tf.flags |= ATA_TFLAG_POLLING;
Tejun Heo49016ac2006-02-21 02:12:11 +09001713
Tejun Heo3373efd2006-05-15 20:57:53 +09001714 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
Tejun Heo49016ac2006-02-21 02:12:11 +09001715 id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heo49016ac2006-02-21 02:12:11 +09001716 if (err_mask) {
Tejun Heo800b3992006-12-03 21:34:13 +09001717 if (err_mask & AC_ERR_NODEV_HINT) {
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001718 DPRINTK("ata%u.%d: NODEV after polling detection\n",
Tejun Heo44877b42007-02-21 01:06:51 +09001719 ap->print_id, dev->devno);
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001720 return -ENOENT;
1721 }
1722
Tejun Heo54936f82007-05-11 14:35:29 +02001723 /* Device or controller might have reported the wrong
1724 * device class. Give a shot at the other IDENTIFY if
1725 * the current one is aborted by the device.
1726 */
1727 if (may_fallback &&
1728 (err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1729 may_fallback = 0;
1730
1731 if (class == ATA_DEV_ATA)
1732 class = ATA_DEV_ATAPI;
1733 else
1734 class = ATA_DEV_ATA;
1735 goto retry;
1736 }
1737
Tejun Heo49016ac2006-02-21 02:12:11 +09001738 rc = -EIO;
1739 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09001740 goto err_out;
1741 }
1742
Tejun Heo54936f82007-05-11 14:35:29 +02001743 /* Falling back doesn't make sense if ID data was read
1744 * successfully at least once.
1745 */
1746 may_fallback = 0;
1747
Tejun Heo49016ac2006-02-21 02:12:11 +09001748 swap_buf_le16(id, ATA_ID_WORDS);
1749
Tejun Heo49016ac2006-02-21 02:12:11 +09001750 /* sanity check */
Tejun Heoa4f57492006-09-12 20:35:49 -07001751 rc = -EINVAL;
Alan Cox60700682007-06-07 16:13:55 +01001752 reason = "device reports invalid type";
Tejun Heoa4f57492006-09-12 20:35:49 -07001753
1754 if (class == ATA_DEV_ATA) {
1755 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1756 goto err_out;
1757 } else {
1758 if (ata_id_is_ata(id))
1759 goto err_out;
Tejun Heo49016ac2006-02-21 02:12:11 +09001760 }
1761
Mark Lord169439c2007-04-17 18:26:07 -04001762 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1763 tried_spinup = 1;
1764 /*
1765 * Drive powered-up in standby mode, and requires a specific
1766 * SET_FEATURES spin-up subcommand before it will accept
1767 * anything other than the original IDENTIFY command.
1768 */
1769 ata_tf_init(dev, &tf);
1770 tf.command = ATA_CMD_SET_FEATURES;
1771 tf.feature = SETFEATURES_SPINUP;
1772 tf.protocol = ATA_PROT_NODATA;
1773 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1774 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Ryan Powerfb0582f92007-08-10 13:59:35 -07001775 if (err_mask && id[2] != 0x738c) {
Mark Lord169439c2007-04-17 18:26:07 -04001776 rc = -EIO;
1777 reason = "SPINUP failed";
1778 goto err_out;
1779 }
1780 /*
1781 * If the drive initially returned incomplete IDENTIFY info,
1782 * we now must reissue the IDENTIFY command.
1783 */
1784 if (id[2] == 0x37c8)
1785 goto retry;
1786 }
1787
Tejun Heobff04642006-11-10 18:08:10 +09001788 if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
Tejun Heo49016ac2006-02-21 02:12:11 +09001789 /*
1790 * The exact sequence expected by certain pre-ATA4 drives is:
1791 * SRST RESET
Alan Cox50a99012007-08-08 14:27:00 +01001792 * IDENTIFY (optional in early ATA)
1793 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
Tejun Heo49016ac2006-02-21 02:12:11 +09001794 * anything else..
1795 * Some drives were very specific about that exact sequence.
Alan Cox50a99012007-08-08 14:27:00 +01001796 *
1797 * Note that ATA4 says lba is mandatory so the second check
1798 * shoud never trigger.
Tejun Heo49016ac2006-02-21 02:12:11 +09001799 */
1800 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Tejun Heo3373efd2006-05-15 20:57:53 +09001801 err_mask = ata_dev_init_params(dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09001802 if (err_mask) {
1803 rc = -EIO;
1804 reason = "INIT_DEV_PARAMS failed";
1805 goto err_out;
1806 }
1807
1808 /* current CHS translation info (id[53-58]) might be
1809 * changed. reread the identify device info.
1810 */
Tejun Heobff04642006-11-10 18:08:10 +09001811 flags &= ~ATA_READID_POSTRESET;
Tejun Heo49016ac2006-02-21 02:12:11 +09001812 goto retry;
1813 }
1814 }
1815
1816 *p_class = class;
Tejun Heofe635c72006-05-15 20:57:35 +09001817
Tejun Heo49016ac2006-02-21 02:12:11 +09001818 return 0;
1819
1820 err_out:
Tejun Heo88574552006-06-25 20:00:35 +09001821 if (ata_msg_warn(ap))
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001822 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
Tejun Heo88574552006-06-25 20:00:35 +09001823 "(%s, err_mask=0x%x)\n", reason, err_mask);
Tejun Heo49016ac2006-02-21 02:12:11 +09001824 return rc;
1825}
1826
Tejun Heo3373efd2006-05-15 20:57:53 +09001827static inline u8 ata_dev_knobble(struct ata_device *dev)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001828{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001829 struct ata_port *ap = dev->link->ap;
1830 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001831}
1832
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001833static void ata_dev_config_ncq(struct ata_device *dev,
1834 char *desc, size_t desc_sz)
1835{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001836 struct ata_port *ap = dev->link->ap;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001837 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
1838
1839 if (!ata_id_has_ncq(dev->id)) {
1840 desc[0] = '\0';
1841 return;
1842 }
Tejun Heo75683fe2007-07-05 13:31:27 +09001843 if (dev->horkage & ATA_HORKAGE_NONCQ) {
Alan Cox6919a0a2006-10-27 19:08:46 -07001844 snprintf(desc, desc_sz, "NCQ (not used)");
1845 return;
1846 }
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001847 if (ap->flags & ATA_FLAG_NCQ) {
Jeff Garzikcca39742006-08-24 03:19:22 -04001848 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001849 dev->flags |= ATA_DFLAG_NCQ;
1850 }
1851
1852 if (hdepth >= ddepth)
1853 snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
1854 else
1855 snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
1856}
1857
Tejun Heo49016ac2006-02-21 02:12:11 +09001858/**
Tejun Heoffeae412006-03-01 16:09:35 +09001859 * ata_dev_configure - Configure the specified ATA/ATAPI device
Tejun Heoffeae412006-03-01 16:09:35 +09001860 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 *
Tejun Heoffeae412006-03-01 16:09:35 +09001862 * Configure @dev according to @dev->id. Generic and low-level
1863 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 *
1865 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001866 * Kernel thread context (may sleep)
1867 *
1868 * RETURNS:
1869 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 */
Tejun Heoefdaedc2006-11-01 18:38:52 +09001871int ata_dev_configure(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001873 struct ata_port *ap = dev->link->ap;
1874 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo67465442007-05-15 03:28:16 +09001875 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001876 const u16 *id = dev->id;
Tejun Heoff8854b2006-03-06 04:31:56 +09001877 unsigned int xfer_mask;
Alan Coxb352e572006-08-10 18:52:12 +01001878 char revbuf[7]; /* XYZ-99\0 */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001879 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
1880 char modelbuf[ATA_ID_PROD_LEN+1];
Brian Kinge6d902a2006-06-28 08:30:31 -05001881 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001883 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001884 ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
1885 __FUNCTION__);
Tejun Heoffeae412006-03-01 16:09:35 +09001886 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
1888
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001889 if (ata_msg_probe(ap))
Tejun Heo44877b42007-02-21 01:06:51 +09001890 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Tejun Heo75683fe2007-07-05 13:31:27 +09001892 /* set horkage */
1893 dev->horkage |= ata_dev_blacklisted(dev);
1894
Tejun Heo67465442007-05-15 03:28:16 +09001895 /* let ACPI work its magic */
1896 rc = ata_acpi_on_devcfg(dev);
1897 if (rc)
1898 return rc;
Kristen Carlson Accardi08573a82006-11-10 16:14:47 -08001899
Tejun Heo05027ad2007-09-03 12:32:57 +09001900 /* massage HPA, do it early as it might change IDENTIFY data */
1901 rc = ata_hpa_resize(dev);
1902 if (rc)
1903 return rc;
1904
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001905 /* print device capabilities */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001906 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001907 ata_dev_printk(dev, KERN_DEBUG,
1908 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
1909 "85:%04x 86:%04x 87:%04x 88:%04x\n",
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001910 __FUNCTION__,
Tejun Heof15a1da2006-05-15 20:57:56 +09001911 id[49], id[82], id[83], id[84],
1912 id[85], id[86], id[87], id[88]);
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001913
Tejun Heo208a9932006-03-05 17:55:58 +09001914 /* initialize to-be-configured parameters */
Tejun Heoea1dd4e2006-04-02 18:51:53 +09001915 dev->flags &= ~ATA_DFLAG_CFG_MASK;
Tejun Heo208a9932006-03-05 17:55:58 +09001916 dev->max_sectors = 0;
1917 dev->cdb_len = 0;
1918 dev->n_sectors = 0;
1919 dev->cylinders = 0;
1920 dev->heads = 0;
1921 dev->sectors = 0;
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 /*
1924 * common ATA, ATAPI feature tests
1925 */
1926
Tejun Heoff8854b2006-03-06 04:31:56 +09001927 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001928 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001930 if (ata_msg_probe(ap))
1931 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Albert Leeef143d52007-06-05 13:01:33 +08001933 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
1934 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
1935 sizeof(fwrevbuf));
1936
1937 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
1938 sizeof(modelbuf));
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 /* ATA-specific feature tests */
1941 if (dev->class == ATA_DEV_ATA) {
Alan Coxb352e572006-08-10 18:52:12 +01001942 if (ata_id_is_cfa(id)) {
1943 if (id[162] & 1) /* CPRM may make this media unusable */
Tejun Heo44877b42007-02-21 01:06:51 +09001944 ata_dev_printk(dev, KERN_WARNING,
1945 "supports DRM functions and may "
1946 "not be fully accessable.\n");
Alan Coxb352e572006-08-10 18:52:12 +01001947 snprintf(revbuf, 7, "CFA");
1948 }
1949 else
1950 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
1951
Tejun Heo1148c3a2006-03-13 19:48:04 +09001952 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09001953
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001954 if (dev->id[59] & 0x100)
1955 dev->multi_count = dev->id[59] & 0xff;
1956
Tejun Heo1148c3a2006-03-13 19:48:04 +09001957 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001958 const char *lba_desc;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001959 char ncq_desc[20];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001960
Tejun Heo4c2d7212006-03-05 17:55:58 +09001961 lba_desc = "LBA";
1962 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001963 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001964 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001965 lba_desc = "LBA48";
Tejun Heo6fc49ad2006-11-11 20:10:45 +09001966
1967 if (dev->n_sectors >= (1UL << 28) &&
1968 ata_id_has_flush_ext(id))
1969 dev->flags |= ATA_DFLAG_FLUSH_EXT;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001970 }
Albert Lee8bf62ece2005-05-12 15:29:42 -04001971
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001972 /* config NCQ */
1973 ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
1974
Albert Lee8bf62ece2005-05-12 15:29:42 -04001975 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001976 if (ata_msg_drv(ap) && print_info) {
1977 ata_dev_printk(dev, KERN_INFO,
1978 "%s: %s, %s, max %s\n",
1979 revbuf, modelbuf, fwrevbuf,
1980 ata_mode_string(xfer_mask));
1981 ata_dev_printk(dev, KERN_INFO,
1982 "%Lu sectors, multi %u: %s %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09001983 (unsigned long long)dev->n_sectors,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001984 dev->multi_count, lba_desc, ncq_desc);
1985 }
Tejun Heoffeae412006-03-01 16:09:35 +09001986 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001987 /* CHS */
1988
1989 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001990 dev->cylinders = id[1];
1991 dev->heads = id[3];
1992 dev->sectors = id[6];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001993
Tejun Heo1148c3a2006-03-13 19:48:04 +09001994 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001995 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001996 dev->cylinders = id[54];
1997 dev->heads = id[55];
1998 dev->sectors = id[56];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001999 }
2000
2001 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002002 if (ata_msg_drv(ap) && print_info) {
Tejun Heo88574552006-06-25 20:00:35 +09002003 ata_dev_printk(dev, KERN_INFO,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002004 "%s: %s, %s, max %s\n",
2005 revbuf, modelbuf, fwrevbuf,
2006 ata_mode_string(xfer_mask));
Jeff Garzika84471f2007-02-26 05:51:33 -05002007 ata_dev_printk(dev, KERN_INFO,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002008 "%Lu sectors, multi %u, CHS %u/%u/%u\n",
2009 (unsigned long long)dev->n_sectors,
2010 dev->multi_count, dev->cylinders,
2011 dev->heads, dev->sectors);
2012 }
Albert Lee07f6f7d2005-11-01 19:33:20 +08002013 }
2014
Tejun Heo6e7846e2006-02-12 23:32:58 +09002015 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
2017
2018 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002019 else if (dev->class == ATA_DEV_ATAPI) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002020 const char *cdb_intr_string = "";
2021 const char *atapi_an_string = "";
Tejun Heo7d77b242007-09-23 13:14:13 +09002022 u32 sntf;
Albert Lee08a556d2006-03-31 13:29:04 +08002023
Tejun Heo1148c3a2006-03-13 19:48:04 +09002024 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002026 if (ata_msg_warn(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002027 ata_dev_printk(dev, KERN_WARNING,
2028 "unsupported CDB len\n");
Tejun Heoffeae412006-03-01 16:09:35 +09002029 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 goto err_out_nosup;
2031 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09002032 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Tejun Heo7d77b242007-09-23 13:14:13 +09002034 /* Enable ATAPI AN if both the host and device have
2035 * the support. If PMP is attached, SNTF is required
2036 * to enable ATAPI AN to discern between PHY status
2037 * changed notifications and ATAPI ANs.
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002038 */
Tejun Heo7d77b242007-09-23 13:14:13 +09002039 if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2040 (!ap->nr_pmp_links ||
2041 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002042 unsigned int err_mask;
2043
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002044 /* issue SET feature command to turn this on */
Tejun Heo854c73a2007-09-23 13:14:11 +09002045 err_mask = ata_dev_set_AN(dev, SETFEATURES_SATA_ENABLE);
2046 if (err_mask)
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002047 ata_dev_printk(dev, KERN_ERR,
Tejun Heo854c73a2007-09-23 13:14:11 +09002048 "failed to enable ATAPI AN "
2049 "(err_mask=0x%x)\n", err_mask);
2050 else {
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002051 dev->flags |= ATA_DFLAG_AN;
Tejun Heo854c73a2007-09-23 13:14:11 +09002052 atapi_an_string = ", ATAPI AN";
2053 }
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002054 }
2055
Albert Lee08a556d2006-03-31 13:29:04 +08002056 if (ata_id_cdb_intr(dev->id)) {
Albert Lee312f7da2005-09-27 17:38:03 +08002057 dev->flags |= ATA_DFLAG_CDB_INTR;
Albert Lee08a556d2006-03-31 13:29:04 +08002058 cdb_intr_string = ", CDB intr";
2059 }
Albert Lee312f7da2005-09-27 17:38:03 +08002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02002062 if (ata_msg_drv(ap) && print_info)
Albert Leeef143d52007-06-05 13:01:33 +08002063 ata_dev_printk(dev, KERN_INFO,
Tejun Heo854c73a2007-09-23 13:14:11 +09002064 "ATAPI: %s, %s, max %s%s%s\n",
Albert Leeef143d52007-06-05 13:01:33 +08002065 modelbuf, fwrevbuf,
Tejun Heo12436c32006-05-15 20:59:15 +09002066 ata_mode_string(xfer_mask),
Tejun Heo854c73a2007-09-23 13:14:11 +09002067 cdb_intr_string, atapi_an_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
2069
Tejun Heo914ed352006-11-01 18:39:55 +09002070 /* determine max_sectors */
2071 dev->max_sectors = ATA_MAX_SECTORS;
2072 if (dev->flags & ATA_DFLAG_LBA48)
2073 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2074
Alan Cox93590852006-09-12 16:55:12 +01002075 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2076 /* Let the user know. We don't want to disallow opens for
2077 rescue purposes, or in case the vendor is just a blithering
2078 idiot */
2079 if (print_info) {
2080 ata_dev_printk(dev, KERN_WARNING,
2081"Drive reports diagnostics failure. This may indicate a drive\n");
2082 ata_dev_printk(dev, KERN_WARNING,
2083"fault or invalid emulation. Contact drive vendor for information.\n");
2084 }
2085 }
2086
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002087 /* limit bridge transfers to udma5, 200 sectors */
Tejun Heo3373efd2006-05-15 20:57:53 +09002088 if (ata_dev_knobble(dev)) {
Borislav Petkov5afc8142006-06-27 14:51:25 +02002089 if (ata_msg_drv(ap) && print_info)
Tejun Heof15a1da2006-05-15 20:57:56 +09002090 ata_dev_printk(dev, KERN_INFO,
2091 "applying bridge limits\n");
Tejun Heo5a529132006-03-24 14:07:50 +09002092 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002093 dev->max_sectors = ATA_MAX_SECTORS;
2094 }
2095
Tejun Heo75683fe2007-07-05 13:31:27 +09002096 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
Tejun Heo03ec52d2007-04-12 13:38:11 +09002097 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2098 dev->max_sectors);
Albert Lee18d6e9d2007-04-02 11:34:15 +08002099
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002100 if (ap->ops->dev_config)
Alancd0d3bb2007-03-02 00:56:15 +00002101 ap->ops->dev_config(dev);
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002102
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002103 if (ata_msg_probe(ap))
2104 ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n",
2105 __FUNCTION__, ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09002106 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108err_out_nosup:
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002109 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002110 ata_dev_printk(dev, KERN_DEBUG,
2111 "%s: EXIT, err\n", __FUNCTION__);
Tejun Heoffeae412006-03-01 16:09:35 +09002112 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
2115/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002116 * ata_cable_40wire - return 40 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002117 * @ap: port
2118 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002119 * Helper method for drivers which want to hardwire 40 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002120 * detection.
2121 */
2122
2123int ata_cable_40wire(struct ata_port *ap)
2124{
2125 return ATA_CBL_PATA40;
2126}
2127
2128/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002129 * ata_cable_80wire - return 80 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002130 * @ap: port
2131 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002132 * Helper method for drivers which want to hardwire 80 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002133 * detection.
2134 */
2135
2136int ata_cable_80wire(struct ata_port *ap)
2137{
2138 return ATA_CBL_PATA80;
2139}
2140
2141/**
2142 * ata_cable_unknown - return unknown PATA cable.
2143 * @ap: port
2144 *
2145 * Helper method for drivers which have no PATA cable detection.
2146 */
2147
2148int ata_cable_unknown(struct ata_port *ap)
2149{
2150 return ATA_CBL_PATA_UNK;
2151}
2152
2153/**
2154 * ata_cable_sata - return SATA cable type
2155 * @ap: port
2156 *
2157 * Helper method for drivers which have SATA cables
2158 */
2159
2160int ata_cable_sata(struct ata_port *ap)
2161{
2162 return ATA_CBL_SATA;
2163}
2164
2165/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 * ata_bus_probe - Reset and probe ATA bus
2167 * @ap: Bus to probe
2168 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002169 * Master ATA bus probing function. Initiates a hardware-dependent
2170 * bus reset, then attempts to identify any devices found on
2171 * the bus.
2172 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002174 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 *
2176 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09002177 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 */
2179
Brian King80289162006-08-07 14:27:31 -05002180int ata_bus_probe(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
Tejun Heo28ca5c52006-03-01 16:09:36 +09002182 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002183 int tries[ATA_MAX_DEVICES];
Tejun Heof58229f2007-08-06 18:36:23 +09002184 int rc;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002185 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Tejun Heo28ca5c52006-03-01 16:09:36 +09002187 ata_port_probe(ap);
2188
Tejun Heof58229f2007-08-06 18:36:23 +09002189 ata_link_for_each_dev(dev, &ap->link)
2190 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002191
2192 retry:
Tejun Heo20444702006-03-13 01:57:01 +09002193 /* reset and determine device classes */
Tejun Heo52783c52006-05-31 18:28:22 +09002194 ap->ops->phy_reset(ap);
Tejun Heo2061a472006-03-12 00:57:39 +09002195
Tejun Heof58229f2007-08-06 18:36:23 +09002196 ata_link_for_each_dev(dev, &ap->link) {
Tejun Heo52783c52006-05-31 18:28:22 +09002197 if (!(ap->flags & ATA_FLAG_DISABLED) &&
2198 dev->class != ATA_DEV_UNKNOWN)
2199 classes[dev->devno] = dev->class;
2200 else
2201 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo20444702006-03-13 01:57:01 +09002202
Tejun Heo52783c52006-05-31 18:28:22 +09002203 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo28ca5c52006-03-01 16:09:36 +09002204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Tejun Heo52783c52006-05-31 18:28:22 +09002206 ata_port_probe(ap);
Tejun Heo20444702006-03-13 01:57:01 +09002207
Alan Coxb6079ca2006-05-22 16:52:06 +01002208 /* after the reset the device state is PIO 0 and the controller
2209 state is undefined. Record the mode */
2210
Tejun Heof58229f2007-08-06 18:36:23 +09002211 ata_link_for_each_dev(dev, &ap->link)
2212 dev->pio_mode = XFER_PIO_0;
Alan Coxb6079ca2006-05-22 16:52:06 +01002213
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002214 /* read IDENTIFY page and configure devices. We have to do the identify
2215 specific sequence bass-ackwards so that PDIAG- is released by
2216 the slave device */
2217
Tejun Heof58229f2007-08-06 18:36:23 +09002218 ata_link_for_each_dev(dev, &ap->link) {
2219 if (tries[dev->devno])
2220 dev->class = classes[dev->devno];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002221
Tejun Heoe1211e32006-04-01 01:38:18 +09002222 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09002223 continue;
2224
Tejun Heobff04642006-11-10 18:08:10 +09002225 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2226 dev->id);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002227 if (rc)
2228 goto fail;
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002229 }
2230
Alan Coxbe0d18d2007-03-06 02:37:56 -08002231 /* Now ask for the cable type as PDIAG- should have been released */
2232 if (ap->ops->cable_detect)
2233 ap->cbl = ap->ops->cable_detect(ap);
2234
Alan Cox614fe292007-08-22 23:22:45 +01002235 /* We may have SATA bridge glue hiding here irrespective of the
2236 reported cable types and sensed types */
2237 ata_link_for_each_dev(dev, &ap->link) {
2238 if (!ata_dev_enabled(dev))
2239 continue;
2240 /* SATA drives indicate we have a bridge. We don't know which
2241 end of the link the bridge is which is a problem */
2242 if (ata_id_is_sata(dev->id))
2243 ap->cbl = ATA_CBL_SATA;
2244 }
2245
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002246 /* After the identify sequence we can now set up the devices. We do
2247 this in the normal order so that the user doesn't get confused */
2248
Tejun Heof58229f2007-08-06 18:36:23 +09002249 ata_link_for_each_dev(dev, &ap->link) {
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002250 if (!ata_dev_enabled(dev))
2251 continue;
Tejun Heoffeae412006-03-01 16:09:35 +09002252
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002253 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
Tejun Heoefdaedc2006-11-01 18:38:52 +09002254 rc = ata_dev_configure(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002255 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002256 if (rc)
2257 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
2259
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002260 /* configure transfer mode */
Tejun Heo02607312007-08-06 18:36:23 +09002261 rc = ata_set_mode(&ap->link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002262 if (rc)
Tejun Heo51713d32006-04-11 22:26:29 +09002263 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Tejun Heof58229f2007-08-06 18:36:23 +09002265 ata_link_for_each_dev(dev, &ap->link)
2266 if (ata_dev_enabled(dev))
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002267 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01002268
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002269 /* no device present, disable port */
2270 ata_port_disable(ap);
Tejun Heo96072e62006-04-01 01:38:17 +09002271 return -ENODEV;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002272
2273 fail:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002274 tries[dev->devno]--;
2275
Tejun Heo14d2bac2006-04-02 17:54:46 +09002276 switch (rc) {
2277 case -EINVAL:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002278 /* eeek, something went very wrong, give up */
Tejun Heo14d2bac2006-04-02 17:54:46 +09002279 tries[dev->devno] = 0;
2280 break;
Tejun Heo4ae72a12007-02-02 16:22:30 +09002281
2282 case -ENODEV:
2283 /* give it just one more chance */
2284 tries[dev->devno] = min(tries[dev->devno], 1);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002285 case -EIO:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002286 if (tries[dev->devno] == 1) {
2287 /* This is the last chance, better to slow
2288 * down than lose it.
2289 */
Tejun Heo936fd732007-08-06 18:36:23 +09002290 sata_down_spd_limit(&ap->link);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002291 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2292 }
Tejun Heo14d2bac2006-04-02 17:54:46 +09002293 }
2294
Tejun Heo4ae72a12007-02-02 16:22:30 +09002295 if (!tries[dev->devno])
Tejun Heo3373efd2006-05-15 20:57:53 +09002296 ata_dev_disable(dev);
Tejun Heoec573752006-04-11 22:26:29 +09002297
Tejun Heo14d2bac2006-04-02 17:54:46 +09002298 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299}
2300
2301/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002302 * ata_port_probe - Mark port as enabled
2303 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002305 * Modify @ap data structure such that the system
2306 * thinks that the entire port is enabled.
2307 *
Jeff Garzikcca39742006-08-24 03:19:22 -04002308 * LOCKING: host lock, or some other form of
Jeff Garzik0cba6322005-05-30 19:49:12 -04002309 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 */
2311
2312void ata_port_probe(struct ata_port *ap)
2313{
Tejun Heo198e0fe2006-04-02 18:51:52 +09002314 ap->flags &= ~ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315}
2316
2317/**
Tejun Heo3be680b2005-12-19 22:35:02 +09002318 * sata_print_link_status - Print SATA link status
Tejun Heo936fd732007-08-06 18:36:23 +09002319 * @link: SATA link to printk link status about
Tejun Heo3be680b2005-12-19 22:35:02 +09002320 *
2321 * This function prints link speed and status of a SATA link.
2322 *
2323 * LOCKING:
2324 * None.
2325 */
Tejun Heo936fd732007-08-06 18:36:23 +09002326void sata_print_link_status(struct ata_link *link)
Tejun Heo3be680b2005-12-19 22:35:02 +09002327{
Tejun Heo6d5f9732006-04-03 00:09:41 +09002328 u32 sstatus, scontrol, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09002329
Tejun Heo936fd732007-08-06 18:36:23 +09002330 if (sata_scr_read(link, SCR_STATUS, &sstatus))
Tejun Heo3be680b2005-12-19 22:35:02 +09002331 return;
Tejun Heo936fd732007-08-06 18:36:23 +09002332 sata_scr_read(link, SCR_CONTROL, &scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002333
Tejun Heo936fd732007-08-06 18:36:23 +09002334 if (ata_link_online(link)) {
Tejun Heo3be680b2005-12-19 22:35:02 +09002335 tmp = (sstatus >> 4) & 0xf;
Tejun Heo936fd732007-08-06 18:36:23 +09002336 ata_link_printk(link, KERN_INFO,
Tejun Heof15a1da2006-05-15 20:57:56 +09002337 "SATA link up %s (SStatus %X SControl %X)\n",
2338 sata_spd_string(tmp), sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002339 } else {
Tejun Heo936fd732007-08-06 18:36:23 +09002340 ata_link_printk(link, KERN_INFO,
Tejun Heof15a1da2006-05-15 20:57:56 +09002341 "SATA link down (SStatus %X SControl %X)\n",
2342 sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002343 }
2344}
2345
2346/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002347 * __sata_phy_reset - Wake/reset a low-level SATA PHY
2348 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002350 * This function issues commands to standard SATA Sxxx
2351 * PHY registers, to wake up the phy (and device), and
2352 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 *
2354 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002355 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 *
2357 */
2358void __sata_phy_reset(struct ata_port *ap)
2359{
Tejun Heo936fd732007-08-06 18:36:23 +09002360 struct ata_link *link = &ap->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 unsigned long timeout = jiffies + (HZ * 5);
Tejun Heo936fd732007-08-06 18:36:23 +09002362 u32 sstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
2364 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca89e2005-03-28 15:10:27 -05002365 /* issue phy wake/reset */
Tejun Heo936fd732007-08-06 18:36:23 +09002366 sata_scr_write_flush(link, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09002367 /* Couldn't find anything in SATA I/II specs, but
2368 * AHCI-1.1 10.4.2 says at least 1 ms. */
2369 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 }
Tejun Heo81952c52006-05-15 20:57:47 +09002371 /* phy wake/clear reset */
Tejun Heo936fd732007-08-06 18:36:23 +09002372 sata_scr_write_flush(link, SCR_CONTROL, 0x300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
2374 /* wait for phy to become ready, if necessary */
2375 do {
2376 msleep(200);
Tejun Heo936fd732007-08-06 18:36:23 +09002377 sata_scr_read(link, SCR_STATUS, &sstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if ((sstatus & 0xf) != 1)
2379 break;
2380 } while (time_before(jiffies, timeout));
2381
Tejun Heo3be680b2005-12-19 22:35:02 +09002382 /* print link status */
Tejun Heo936fd732007-08-06 18:36:23 +09002383 sata_print_link_status(link);
Jeff Garzik656563e2005-11-20 03:36:45 -05002384
Tejun Heo3be680b2005-12-19 22:35:02 +09002385 /* TODO: phy layer with polling, timeouts, etc. */
Tejun Heo936fd732007-08-06 18:36:23 +09002386 if (!ata_link_offline(link))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09002388 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Tejun Heo198e0fe2006-04-02 18:51:52 +09002391 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 return;
2393
2394 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2395 ata_port_disable(ap);
2396 return;
2397 }
2398
2399 ap->cbl = ATA_CBL_SATA;
2400}
2401
2402/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002403 * sata_phy_reset - Reset SATA bus.
2404 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002406 * This function resets the SATA bus, and then probes
2407 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 *
2409 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002410 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 *
2412 */
2413void sata_phy_reset(struct ata_port *ap)
2414{
2415 __sata_phy_reset(ap);
Tejun Heo198e0fe2006-04-02 18:51:52 +09002416 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return;
2418 ata_bus_reset(ap);
2419}
2420
2421/**
Alan Coxebdfca62006-03-23 15:38:34 +00002422 * ata_dev_pair - return other device on cable
Alan Coxebdfca62006-03-23 15:38:34 +00002423 * @adev: device
2424 *
2425 * Obtain the other device on the same cable, or if none is
2426 * present NULL is returned
2427 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002428
Tejun Heo3373efd2006-05-15 20:57:53 +09002429struct ata_device *ata_dev_pair(struct ata_device *adev)
Alan Coxebdfca62006-03-23 15:38:34 +00002430{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002431 struct ata_link *link = adev->link;
2432 struct ata_device *pair = &link->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09002433 if (!ata_dev_enabled(pair))
Alan Coxebdfca62006-03-23 15:38:34 +00002434 return NULL;
2435 return pair;
2436}
2437
2438/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002439 * ata_port_disable - Disable port.
2440 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002442 * Modify @ap data structure such that the system
2443 * thinks that the entire port is disabled, and should
2444 * never attempt to probe or communicate with devices
2445 * on this port.
2446 *
Jeff Garzikcca39742006-08-24 03:19:22 -04002447 * LOCKING: host lock, or some other form of
Jeff Garzik780a87f2005-05-30 15:41:05 -04002448 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 */
2450
2451void ata_port_disable(struct ata_port *ap)
2452{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002453 ap->link.device[0].class = ATA_DEV_NONE;
2454 ap->link.device[1].class = ATA_DEV_NONE;
Tejun Heo198e0fe2006-04-02 18:51:52 +09002455 ap->flags |= ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
2457
Tejun Heo1c3fae42006-04-02 20:53:28 +09002458/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002459 * sata_down_spd_limit - adjust SATA spd limit downward
Tejun Heo936fd732007-08-06 18:36:23 +09002460 * @link: Link to adjust SATA spd limit for
Tejun Heo1c3fae42006-04-02 20:53:28 +09002461 *
Tejun Heo936fd732007-08-06 18:36:23 +09002462 * Adjust SATA spd limit of @link downward. Note that this
Tejun Heo1c3fae42006-04-02 20:53:28 +09002463 * function only adjusts the limit. The change must be applied
Tejun Heo3c567b72006-05-15 20:57:23 +09002464 * using sata_set_spd().
Tejun Heo1c3fae42006-04-02 20:53:28 +09002465 *
2466 * LOCKING:
2467 * Inherited from caller.
2468 *
2469 * RETURNS:
2470 * 0 on success, negative errno on failure
2471 */
Tejun Heo936fd732007-08-06 18:36:23 +09002472int sata_down_spd_limit(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002473{
Tejun Heo81952c52006-05-15 20:57:47 +09002474 u32 sstatus, spd, mask;
2475 int rc, highbit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002476
Tejun Heo936fd732007-08-06 18:36:23 +09002477 if (!sata_scr_valid(link))
Tejun Heo008a7892007-07-16 14:29:40 +09002478 return -EOPNOTSUPP;
2479
2480 /* If SCR can be read, use it to determine the current SPD.
Tejun Heo936fd732007-08-06 18:36:23 +09002481 * If not, use cached value in link->sata_spd.
Tejun Heo008a7892007-07-16 14:29:40 +09002482 */
Tejun Heo936fd732007-08-06 18:36:23 +09002483 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
Tejun Heo008a7892007-07-16 14:29:40 +09002484 if (rc == 0)
2485 spd = (sstatus >> 4) & 0xf;
2486 else
Tejun Heo936fd732007-08-06 18:36:23 +09002487 spd = link->sata_spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002488
Tejun Heo936fd732007-08-06 18:36:23 +09002489 mask = link->sata_spd_limit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002490 if (mask <= 1)
2491 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09002492
2493 /* unconditionally mask off the highest bit */
Tejun Heo1c3fae42006-04-02 20:53:28 +09002494 highbit = fls(mask) - 1;
2495 mask &= ~(1 << highbit);
2496
Tejun Heo008a7892007-07-16 14:29:40 +09002497 /* Mask off all speeds higher than or equal to the current
2498 * one. Force 1.5Gbps if current SPD is not available.
2499 */
2500 if (spd > 1)
2501 mask &= (1 << (spd - 1)) - 1;
2502 else
2503 mask &= 1;
2504
2505 /* were we already at the bottom? */
Tejun Heo1c3fae42006-04-02 20:53:28 +09002506 if (!mask)
2507 return -EINVAL;
2508
Tejun Heo936fd732007-08-06 18:36:23 +09002509 link->sata_spd_limit = mask;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002510
Tejun Heo936fd732007-08-06 18:36:23 +09002511 ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09002512 sata_spd_string(fls(mask)));
Tejun Heo1c3fae42006-04-02 20:53:28 +09002513
2514 return 0;
2515}
2516
Tejun Heo936fd732007-08-06 18:36:23 +09002517static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002518{
2519 u32 spd, limit;
2520
Tejun Heo936fd732007-08-06 18:36:23 +09002521 if (link->sata_spd_limit == UINT_MAX)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002522 limit = 0;
2523 else
Tejun Heo936fd732007-08-06 18:36:23 +09002524 limit = fls(link->sata_spd_limit);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002525
2526 spd = (*scontrol >> 4) & 0xf;
2527 *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
2528
2529 return spd != limit;
2530}
2531
2532/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002533 * sata_set_spd_needed - is SATA spd configuration needed
Tejun Heo936fd732007-08-06 18:36:23 +09002534 * @link: Link in question
Tejun Heo1c3fae42006-04-02 20:53:28 +09002535 *
2536 * Test whether the spd limit in SControl matches
Tejun Heo936fd732007-08-06 18:36:23 +09002537 * @link->sata_spd_limit. This function is used to determine
Tejun Heo1c3fae42006-04-02 20:53:28 +09002538 * whether hardreset is necessary to apply SATA spd
2539 * configuration.
2540 *
2541 * LOCKING:
2542 * Inherited from caller.
2543 *
2544 * RETURNS:
2545 * 1 if SATA spd configuration is needed, 0 otherwise.
2546 */
Tejun Heo936fd732007-08-06 18:36:23 +09002547int sata_set_spd_needed(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002548{
2549 u32 scontrol;
2550
Tejun Heo936fd732007-08-06 18:36:23 +09002551 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09002552 return 0;
2553
Tejun Heo936fd732007-08-06 18:36:23 +09002554 return __sata_set_spd_needed(link, &scontrol);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002555}
2556
2557/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002558 * sata_set_spd - set SATA spd according to spd limit
Tejun Heo936fd732007-08-06 18:36:23 +09002559 * @link: Link to set SATA spd for
Tejun Heo1c3fae42006-04-02 20:53:28 +09002560 *
Tejun Heo936fd732007-08-06 18:36:23 +09002561 * Set SATA spd of @link according to sata_spd_limit.
Tejun Heo1c3fae42006-04-02 20:53:28 +09002562 *
2563 * LOCKING:
2564 * Inherited from caller.
2565 *
2566 * RETURNS:
2567 * 0 if spd doesn't need to be changed, 1 if spd has been
Tejun Heo81952c52006-05-15 20:57:47 +09002568 * changed. Negative errno if SCR registers are inaccessible.
Tejun Heo1c3fae42006-04-02 20:53:28 +09002569 */
Tejun Heo936fd732007-08-06 18:36:23 +09002570int sata_set_spd(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002571{
2572 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09002573 int rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002574
Tejun Heo936fd732007-08-06 18:36:23 +09002575 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09002576 return rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002577
Tejun Heo936fd732007-08-06 18:36:23 +09002578 if (!__sata_set_spd_needed(link, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09002579 return 0;
2580
Tejun Heo936fd732007-08-06 18:36:23 +09002581 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09002582 return rc;
2583
Tejun Heo1c3fae42006-04-02 20:53:28 +09002584 return 1;
2585}
2586
Alan Cox452503f2005-10-21 19:01:32 -04002587/*
2588 * This mode timing computation functionality is ported over from
2589 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
2590 */
2591/*
Alan Coxb352e572006-08-10 18:52:12 +01002592 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
Alan Cox452503f2005-10-21 19:01:32 -04002593 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
Alan Coxb352e572006-08-10 18:52:12 +01002594 * for UDMA6, which is currently supported only by Maxtor drives.
2595 *
2596 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
Alan Cox452503f2005-10-21 19:01:32 -04002597 */
2598
2599static const struct ata_timing ata_timing[] = {
2600
2601 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
2602 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
2603 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
2604 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
2605
Alan Coxb352e572006-08-10 18:52:12 +01002606 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
2607 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04002608 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
2609 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
2610 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
2611
2612/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002613
Alan Cox452503f2005-10-21 19:01:32 -04002614 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
2615 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
2616 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002617
Alan Cox452503f2005-10-21 19:01:32 -04002618 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
2619 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
2620 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
2621
Alan Coxb352e572006-08-10 18:52:12 +01002622 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
2623 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04002624 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
2625 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
2626
2627 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
2628 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
2629 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
2630
2631/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
2632
2633 { 0xFF }
2634};
2635
2636#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
2637#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
2638
2639static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
2640{
2641 q->setup = EZ(t->setup * 1000, T);
2642 q->act8b = EZ(t->act8b * 1000, T);
2643 q->rec8b = EZ(t->rec8b * 1000, T);
2644 q->cyc8b = EZ(t->cyc8b * 1000, T);
2645 q->active = EZ(t->active * 1000, T);
2646 q->recover = EZ(t->recover * 1000, T);
2647 q->cycle = EZ(t->cycle * 1000, T);
2648 q->udma = EZ(t->udma * 1000, UT);
2649}
2650
2651void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
2652 struct ata_timing *m, unsigned int what)
2653{
2654 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
2655 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
2656 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
2657 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
2658 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
2659 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
2660 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
2661 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
2662}
2663
2664static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
2665{
2666 const struct ata_timing *t;
2667
2668 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04002669 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04002670 return NULL;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002671 return t;
Alan Cox452503f2005-10-21 19:01:32 -04002672}
2673
2674int ata_timing_compute(struct ata_device *adev, unsigned short speed,
2675 struct ata_timing *t, int T, int UT)
2676{
2677 const struct ata_timing *s;
2678 struct ata_timing p;
2679
2680 /*
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002681 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08002682 */
Alan Cox452503f2005-10-21 19:01:32 -04002683
2684 if (!(s = ata_timing_find_mode(speed)))
2685 return -EINVAL;
2686
Albert Lee75b1f2f2005-11-16 17:06:18 +08002687 memcpy(t, s, sizeof(*s));
2688
Alan Cox452503f2005-10-21 19:01:32 -04002689 /*
2690 * If the drive is an EIDE drive, it can tell us it needs extended
2691 * PIO/MW_DMA cycle timing.
2692 */
2693
2694 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
2695 memset(&p, 0, sizeof(p));
2696 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
2697 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
2698 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
2699 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
2700 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
2701 }
2702 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
2703 }
2704
2705 /*
2706 * Convert the timing to bus clock counts.
2707 */
2708
Albert Lee75b1f2f2005-11-16 17:06:18 +08002709 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04002710
2711 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002712 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
2713 * S.M.A.R.T * and some other commands. We have to ensure that the
2714 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04002715 */
2716
Alanfd3367af2006-12-07 12:41:18 +00002717 if (speed > XFER_PIO_6) {
Alan Cox452503f2005-10-21 19:01:32 -04002718 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
2719 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
2720 }
2721
2722 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002723 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04002724 */
2725
2726 if (t->act8b + t->rec8b < t->cyc8b) {
2727 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
2728 t->rec8b = t->cyc8b - t->act8b;
2729 }
2730
2731 if (t->active + t->recover < t->cycle) {
2732 t->active += (t->cycle - (t->active + t->recover)) / 2;
2733 t->recover = t->cycle - t->active;
2734 }
Jeff Garzika617c092007-05-21 20:14:23 -04002735
Alan Cox4f701d12007-04-23 11:55:36 +01002736 /* In a few cases quantisation may produce enough errors to
2737 leave t->cycle too low for the sum of active and recovery
2738 if so we must correct this */
2739 if (t->active + t->recover > t->cycle)
2740 t->cycle = t->active + t->recover;
Alan Cox452503f2005-10-21 19:01:32 -04002741
2742 return 0;
2743}
2744
Tejun Heocf176e12006-04-02 17:54:46 +09002745/**
2746 * ata_down_xfermask_limit - adjust dev xfer masks downward
Tejun Heocf176e12006-04-02 17:54:46 +09002747 * @dev: Device to adjust xfer masks
Tejun Heo458337d2007-02-02 16:22:30 +09002748 * @sel: ATA_DNXFER_* selector
Tejun Heocf176e12006-04-02 17:54:46 +09002749 *
2750 * Adjust xfer masks of @dev downward. Note that this function
2751 * does not apply the change. Invoking ata_set_mode() afterwards
2752 * will apply the limit.
2753 *
2754 * LOCKING:
2755 * Inherited from caller.
2756 *
2757 * RETURNS:
2758 * 0 on success, negative errno on failure
2759 */
Tejun Heo458337d2007-02-02 16:22:30 +09002760int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
Tejun Heocf176e12006-04-02 17:54:46 +09002761{
Tejun Heo458337d2007-02-02 16:22:30 +09002762 char buf[32];
2763 unsigned int orig_mask, xfer_mask;
2764 unsigned int pio_mask, mwdma_mask, udma_mask;
2765 int quiet, highbit;
Tejun Heocf176e12006-04-02 17:54:46 +09002766
Tejun Heo458337d2007-02-02 16:22:30 +09002767 quiet = !!(sel & ATA_DNXFER_QUIET);
2768 sel &= ~ATA_DNXFER_QUIET;
Tejun Heocf176e12006-04-02 17:54:46 +09002769
Tejun Heo458337d2007-02-02 16:22:30 +09002770 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
2771 dev->mwdma_mask,
2772 dev->udma_mask);
2773 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
Tejun Heocf176e12006-04-02 17:54:46 +09002774
Tejun Heo458337d2007-02-02 16:22:30 +09002775 switch (sel) {
2776 case ATA_DNXFER_PIO:
2777 highbit = fls(pio_mask) - 1;
2778 pio_mask &= ~(1 << highbit);
2779 break;
2780
2781 case ATA_DNXFER_DMA:
2782 if (udma_mask) {
2783 highbit = fls(udma_mask) - 1;
2784 udma_mask &= ~(1 << highbit);
2785 if (!udma_mask)
2786 return -ENOENT;
2787 } else if (mwdma_mask) {
2788 highbit = fls(mwdma_mask) - 1;
2789 mwdma_mask &= ~(1 << highbit);
2790 if (!mwdma_mask)
2791 return -ENOENT;
2792 }
2793 break;
2794
2795 case ATA_DNXFER_40C:
2796 udma_mask &= ATA_UDMA_MASK_40C;
2797 break;
2798
2799 case ATA_DNXFER_FORCE_PIO0:
2800 pio_mask &= 1;
2801 case ATA_DNXFER_FORCE_PIO:
2802 mwdma_mask = 0;
2803 udma_mask = 0;
2804 break;
2805
Tejun Heo458337d2007-02-02 16:22:30 +09002806 default:
2807 BUG();
2808 }
2809
2810 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
2811
2812 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
2813 return -ENOENT;
2814
2815 if (!quiet) {
2816 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
2817 snprintf(buf, sizeof(buf), "%s:%s",
2818 ata_mode_string(xfer_mask),
2819 ata_mode_string(xfer_mask & ATA_MASK_PIO));
2820 else
2821 snprintf(buf, sizeof(buf), "%s",
2822 ata_mode_string(xfer_mask));
2823
2824 ata_dev_printk(dev, KERN_WARNING,
2825 "limiting speed to %s\n", buf);
2826 }
Tejun Heocf176e12006-04-02 17:54:46 +09002827
2828 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
2829 &dev->udma_mask);
2830
Tejun Heocf176e12006-04-02 17:54:46 +09002831 return 0;
Tejun Heocf176e12006-04-02 17:54:46 +09002832}
2833
Tejun Heo3373efd2006-05-15 20:57:53 +09002834static int ata_dev_set_mode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002836 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo83206a22006-03-24 15:25:31 +09002837 unsigned int err_mask;
2838 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Tejun Heoe8384602006-04-02 18:51:53 +09002840 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 if (dev->xfer_shift == ATA_SHIFT_PIO)
2842 dev->flags |= ATA_DFLAG_PIO;
2843
Tejun Heo3373efd2006-05-15 20:57:53 +09002844 err_mask = ata_dev_set_xfermode(dev);
Alan11750a42007-02-05 16:28:30 +00002845 /* Old CFA may refuse this command, which is just fine */
2846 if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
2847 err_mask &= ~AC_ERR_DEV;
Alan Cox0bc2a792007-07-31 14:01:48 +01002848 /* Some very old devices and some bad newer ones fail any kind of
2849 SET_XFERMODE request but support PIO0-2 timings and no IORDY */
2850 if (dev->xfer_shift == ATA_SHIFT_PIO && !ata_id_has_iordy(dev->id) &&
2851 dev->pio_mode <= XFER_PIO_2)
2852 err_mask &= ~AC_ERR_DEV;
Tejun Heo83206a22006-03-24 15:25:31 +09002853 if (err_mask) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002854 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
2855 "(err_mask=0x%x)\n", err_mask);
Tejun Heo83206a22006-03-24 15:25:31 +09002856 return -EIO;
2857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Tejun Heobaa1e782006-11-01 18:39:27 +09002859 ehc->i.flags |= ATA_EHI_POST_SETMODE;
Tejun Heo422c9da2007-09-23 13:14:12 +09002860 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
Tejun Heobaa1e782006-11-01 18:39:27 +09002861 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
Tejun Heo5eb45c02006-04-02 18:51:52 +09002862 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09002863 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09002864
Tejun Heo23e71c32006-03-06 04:31:57 +09002865 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
2866 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
Tejun Heof15a1da2006-05-15 20:57:56 +09002868 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
2869 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
Tejun Heo83206a22006-03-24 15:25:31 +09002870 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871}
2872
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873/**
Alan04351822007-03-06 02:37:52 -08002874 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
Tejun Heo02607312007-08-06 18:36:23 +09002875 * @link: link on which timings will be programmed
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002876 * @r_failed_dev: out paramter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 *
Alan04351822007-03-06 02:37:52 -08002878 * Standard implementation of the function used to tune and set
2879 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2880 * ata_dev_set_mode() fails, pointer to the failing device is
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002881 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04002882 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002884 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002885 *
2886 * RETURNS:
2887 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 */
Alan04351822007-03-06 02:37:52 -08002889
Tejun Heo02607312007-08-06 18:36:23 +09002890int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891{
Tejun Heo02607312007-08-06 18:36:23 +09002892 struct ata_port *ap = link->ap;
Tejun Heoe8e06192006-04-01 01:38:18 +09002893 struct ata_device *dev;
Tejun Heof58229f2007-08-06 18:36:23 +09002894 int rc = 0, used_dma = 0, found = 0;
Tejun Heo3adcebb2006-05-15 20:57:37 +09002895
Tejun Heoa6d5a512006-03-06 04:31:57 +09002896 /* step 1: calculate xfer_mask */
Tejun Heof58229f2007-08-06 18:36:23 +09002897 ata_link_for_each_dev(dev, link) {
Tejun Heoacf356b2006-03-24 14:07:50 +09002898 unsigned int pio_mask, dma_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002899
Tejun Heoe1211e32006-04-01 01:38:18 +09002900 if (!ata_dev_enabled(dev))
Tejun Heoa6d5a512006-03-06 04:31:57 +09002901 continue;
2902
Tejun Heo3373efd2006-05-15 20:57:53 +09002903 ata_dev_xfermask(dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09002904
Tejun Heoacf356b2006-03-24 14:07:50 +09002905 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
2906 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
2907 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
2908 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01002909
Tejun Heo4f659772006-04-01 01:38:18 +09002910 found = 1;
Alan Cox5444a6f2006-03-27 18:58:20 +01002911 if (dev->dma_mode)
2912 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002913 }
Tejun Heo4f659772006-04-01 01:38:18 +09002914 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002915 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002916
2917 /* step 2: always set host PIO timings */
Tejun Heof58229f2007-08-06 18:36:23 +09002918 ata_link_for_each_dev(dev, link) {
Tejun Heoe8e06192006-04-01 01:38:18 +09002919 if (!ata_dev_enabled(dev))
2920 continue;
2921
2922 if (!dev->pio_mode) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002923 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
Tejun Heoe8e06192006-04-01 01:38:18 +09002924 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002925 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09002926 }
2927
2928 dev->xfer_mode = dev->pio_mode;
2929 dev->xfer_shift = ATA_SHIFT_PIO;
2930 if (ap->ops->set_piomode)
2931 ap->ops->set_piomode(ap, dev);
2932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Tejun Heoa6d5a512006-03-06 04:31:57 +09002934 /* step 3: set host DMA timings */
Tejun Heof58229f2007-08-06 18:36:23 +09002935 ata_link_for_each_dev(dev, link) {
Tejun Heoe8e06192006-04-01 01:38:18 +09002936 if (!ata_dev_enabled(dev) || !dev->dma_mode)
2937 continue;
2938
2939 dev->xfer_mode = dev->dma_mode;
2940 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
2941 if (ap->ops->set_dmamode)
2942 ap->ops->set_dmamode(ap, dev);
2943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
2945 /* step 4: update devices' xfer mode */
Tejun Heof58229f2007-08-06 18:36:23 +09002946 ata_link_for_each_dev(dev, link) {
Alan18d90de2007-01-24 11:42:38 +00002947 /* don't update suspended devices' xfer mode */
Tejun Heo9666f402007-05-04 21:27:47 +02002948 if (!ata_dev_enabled(dev))
Tejun Heo83206a22006-03-24 15:25:31 +09002949 continue;
2950
Tejun Heo3373efd2006-05-15 20:57:53 +09002951 rc = ata_dev_set_mode(dev);
Tejun Heo5bbc53f2006-04-01 01:38:17 +09002952 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002953 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09002954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Tejun Heoe8e06192006-04-01 01:38:18 +09002956 /* Record simplex status. If we selected DMA then the other
2957 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01002958 */
Jeff Garzikcca39742006-08-24 03:19:22 -04002959 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
Alan032af1c2007-03-01 17:36:46 +00002960 ap->host->simplex_claimed = ap;
Alan Cox5444a6f2006-03-27 18:58:20 +01002961
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002962 out:
2963 if (rc)
2964 *r_failed_dev = dev;
2965 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966}
2967
2968/**
Alan04351822007-03-06 02:37:52 -08002969 * ata_set_mode - Program timings and issue SET FEATURES - XFER
Tejun Heo02607312007-08-06 18:36:23 +09002970 * @link: link on which timings will be programmed
Alan04351822007-03-06 02:37:52 -08002971 * @r_failed_dev: out paramter for failed device
2972 *
2973 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2974 * ata_set_mode() fails, pointer to the failing device is
2975 * returned in @r_failed_dev.
2976 *
2977 * LOCKING:
2978 * PCI/etc. bus probe sem.
2979 *
2980 * RETURNS:
2981 * 0 on success, negative errno otherwise
2982 */
Tejun Heo02607312007-08-06 18:36:23 +09002983int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
Alan04351822007-03-06 02:37:52 -08002984{
Tejun Heo02607312007-08-06 18:36:23 +09002985 struct ata_port *ap = link->ap;
2986
Alan04351822007-03-06 02:37:52 -08002987 /* has private set_mode? */
2988 if (ap->ops->set_mode)
Tejun Heo02607312007-08-06 18:36:23 +09002989 return ap->ops->set_mode(link, r_failed_dev);
2990 return ata_do_set_mode(link, r_failed_dev);
Alan04351822007-03-06 02:37:52 -08002991}
2992
2993/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002994 * ata_tf_to_host - issue ATA taskfile to host controller
2995 * @ap: port to which command is being issued
2996 * @tf: ATA taskfile register set
2997 *
2998 * Issues ATA taskfile register set to ATA host controller,
2999 * with proper synchronization with interrupt handler and
3000 * other threads.
3001 *
3002 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003003 * spin_lock_irqsave(host lock)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05003004 */
3005
3006static inline void ata_tf_to_host(struct ata_port *ap,
3007 const struct ata_taskfile *tf)
3008{
3009 ap->ops->tf_load(ap, tf);
3010 ap->ops->exec_command(ap, tf);
3011}
3012
3013/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 * ata_busy_sleep - sleep until BSY clears, or timeout
3015 * @ap: port containing status register to be polled
3016 * @tmout_pat: impatience timeout
3017 * @tmout: overall timeout
3018 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003019 * Sleep until ATA Status register bit BSY clears,
3020 * or a timeout occurs.
3021 *
Tejun Heod1adc1b2006-10-09 18:32:15 +09003022 * LOCKING:
3023 * Kernel thread context (may sleep).
3024 *
3025 * RETURNS:
3026 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 */
Tejun Heod1adc1b2006-10-09 18:32:15 +09003028int ata_busy_sleep(struct ata_port *ap,
3029 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
3031 unsigned long timer_start, timeout;
3032 u8 status;
3033
3034 status = ata_busy_wait(ap, ATA_BUSY, 300);
3035 timer_start = jiffies;
3036 timeout = timer_start + tmout_pat;
Tejun Heod1adc1b2006-10-09 18:32:15 +09003037 while (status != 0xff && (status & ATA_BUSY) &&
3038 time_before(jiffies, timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 msleep(50);
3040 status = ata_busy_wait(ap, ATA_BUSY, 3);
3041 }
3042
Tejun Heod1adc1b2006-10-09 18:32:15 +09003043 if (status != 0xff && (status & ATA_BUSY))
Tejun Heof15a1da2006-05-15 20:57:56 +09003044 ata_port_printk(ap, KERN_WARNING,
Jeff Garzik35aa7a42006-09-28 06:50:24 -04003045 "port is slow to respond, please be patient "
3046 "(Status 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
3048 timeout = timer_start + tmout;
Tejun Heod1adc1b2006-10-09 18:32:15 +09003049 while (status != 0xff && (status & ATA_BUSY) &&
3050 time_before(jiffies, timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 msleep(50);
3052 status = ata_chk_status(ap);
3053 }
3054
Tejun Heod1adc1b2006-10-09 18:32:15 +09003055 if (status == 0xff)
3056 return -ENODEV;
3057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 if (status & ATA_BUSY) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003059 ata_port_printk(ap, KERN_ERR, "port failed to respond "
Jeff Garzik35aa7a42006-09-28 06:50:24 -04003060 "(%lu secs, Status 0x%x)\n",
3061 tmout / HZ, status);
Tejun Heod1adc1b2006-10-09 18:32:15 +09003062 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 }
3064
3065 return 0;
3066}
3067
Tejun Heod4b2bab2007-02-02 16:50:52 +09003068/**
3069 * ata_wait_ready - sleep until BSY clears, or timeout
3070 * @ap: port containing status register to be polled
3071 * @deadline: deadline jiffies for the operation
3072 *
3073 * Sleep until ATA Status register bit BSY clears, or timeout
3074 * occurs.
3075 *
3076 * LOCKING:
3077 * Kernel thread context (may sleep).
3078 *
3079 * RETURNS:
3080 * 0 on success, -errno otherwise.
3081 */
3082int ata_wait_ready(struct ata_port *ap, unsigned long deadline)
3083{
3084 unsigned long start = jiffies;
3085 int warned = 0;
3086
3087 while (1) {
3088 u8 status = ata_chk_status(ap);
3089 unsigned long now = jiffies;
3090
3091 if (!(status & ATA_BUSY))
3092 return 0;
Tejun Heo936fd732007-08-06 18:36:23 +09003093 if (!ata_link_online(&ap->link) && status == 0xff)
Tejun Heod4b2bab2007-02-02 16:50:52 +09003094 return -ENODEV;
3095 if (time_after(now, deadline))
3096 return -EBUSY;
3097
3098 if (!warned && time_after(now, start + 5 * HZ) &&
3099 (deadline - now > 3 * HZ)) {
3100 ata_port_printk(ap, KERN_WARNING,
3101 "port is slow to respond, please be patient "
3102 "(Status 0x%x)\n", status);
3103 warned = 1;
3104 }
3105
3106 msleep(50);
3107 }
3108}
3109
3110static int ata_bus_post_reset(struct ata_port *ap, unsigned int devmask,
3111 unsigned long deadline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112{
3113 struct ata_ioports *ioaddr = &ap->ioaddr;
3114 unsigned int dev0 = devmask & (1 << 0);
3115 unsigned int dev1 = devmask & (1 << 1);
Tejun Heo9b893912007-02-02 16:50:52 +09003116 int rc, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
3118 /* if device 0 was found in ata_devchk, wait for its
3119 * BSY bit to clear
3120 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003121 if (dev0) {
3122 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003123 if (rc) {
3124 if (rc != -ENODEV)
3125 return rc;
3126 ret = rc;
3127 }
Tejun Heod4b2bab2007-02-02 16:50:52 +09003128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Tejun Heoe141d992007-06-10 14:26:20 +09003130 /* if device 1 was found in ata_devchk, wait for register
3131 * access briefly, then wait for BSY to clear.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 */
Tejun Heoe141d992007-06-10 14:26:20 +09003133 if (dev1) {
3134 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
3136 ap->ops->dev_select(ap, 1);
Tejun Heoe141d992007-06-10 14:26:20 +09003137
3138 /* Wait for register access. Some ATAPI devices fail
3139 * to set nsect/lbal after reset, so don't waste too
3140 * much time on it. We're gonna wait for !BSY anyway.
3141 */
3142 for (i = 0; i < 2; i++) {
3143 u8 nsect, lbal;
3144
3145 nsect = ioread8(ioaddr->nsect_addr);
3146 lbal = ioread8(ioaddr->lbal_addr);
3147 if ((nsect == 1) && (lbal == 1))
3148 break;
3149 msleep(50); /* give drive a breather */
3150 }
3151
Tejun Heod4b2bab2007-02-02 16:50:52 +09003152 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003153 if (rc) {
3154 if (rc != -ENODEV)
3155 return rc;
3156 ret = rc;
3157 }
Tejun Heod4b2bab2007-02-02 16:50:52 +09003158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
3160 /* is all this really necessary? */
3161 ap->ops->dev_select(ap, 0);
3162 if (dev1)
3163 ap->ops->dev_select(ap, 1);
3164 if (dev0)
3165 ap->ops->dev_select(ap, 0);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003166
Tejun Heo9b893912007-02-02 16:50:52 +09003167 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168}
3169
Tejun Heod4b2bab2007-02-02 16:50:52 +09003170static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
3171 unsigned long deadline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
3173 struct ata_ioports *ioaddr = &ap->ioaddr;
3174
Tejun Heo44877b42007-02-21 01:06:51 +09003175 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
3177 /* software reset. causes dev0 to be selected */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003178 iowrite8(ap->ctl, ioaddr->ctl_addr);
3179 udelay(20); /* FIXME: flush */
3180 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
3181 udelay(20); /* FIXME: flush */
3182 iowrite8(ap->ctl, ioaddr->ctl_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
3184 /* spec mandates ">= 2ms" before checking status.
3185 * We wait 150ms, because that was the magic delay used for
3186 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
3187 * between when the ATA command register is written, and then
3188 * status is checked. Because waiting for "a while" before
3189 * checking status is fine, post SRST, we perform this magic
3190 * delay here as well.
Alan Cox09c7ad72006-03-22 15:52:40 +00003191 *
3192 * Old drivers/ide uses the 2mS rule and then waits for ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 */
3194 msleep(150);
3195
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003196 /* Before we perform post reset processing we want to see if
Tejun Heo298a41c2006-03-25 02:58:13 +09003197 * the bus shows 0xFF because the odd clown forgets the D7
3198 * pulldown resistor.
3199 */
Tejun Heod1adc1b2006-10-09 18:32:15 +09003200 if (ata_check_status(ap) == 0xFF)
Tejun Heo9b893912007-02-02 16:50:52 +09003201 return -ENODEV;
Alan Cox09c7ad72006-03-22 15:52:40 +00003202
Tejun Heod4b2bab2007-02-02 16:50:52 +09003203 return ata_bus_post_reset(ap, devmask, deadline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204}
3205
3206/**
3207 * ata_bus_reset - reset host port and associated ATA channel
3208 * @ap: port to reset
3209 *
3210 * This is typically the first time we actually start issuing
3211 * commands to the ATA channel. We wait for BSY to clear, then
3212 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
3213 * result. Determine what devices, if any, are on the channel
3214 * by looking at the device 0/1 error register. Look at the signature
3215 * stored in each device's taskfile registers, to determine if
3216 * the device is ATA or ATAPI.
3217 *
3218 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003219 * PCI/etc. bus probe sem.
Jeff Garzikcca39742006-08-24 03:19:22 -04003220 * Obtains host lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 *
3222 * SIDE EFFECTS:
Tejun Heo198e0fe2006-04-02 18:51:52 +09003223 * Sets ATA_FLAG_DISABLED if bus reset fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 */
3225
3226void ata_bus_reset(struct ata_port *ap)
3227{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003228 struct ata_device *device = ap->link.device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 struct ata_ioports *ioaddr = &ap->ioaddr;
3230 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
3231 u8 err;
Tejun Heoaec5c3c2006-03-25 01:33:34 +09003232 unsigned int dev0, dev1 = 0, devmask = 0;
Tejun Heo9b893912007-02-02 16:50:52 +09003233 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Tejun Heo44877b42007-02-21 01:06:51 +09003235 DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
3237 /* determine if device 0/1 are present */
3238 if (ap->flags & ATA_FLAG_SATA_RESET)
3239 dev0 = 1;
3240 else {
3241 dev0 = ata_devchk(ap, 0);
3242 if (slave_possible)
3243 dev1 = ata_devchk(ap, 1);
3244 }
3245
3246 if (dev0)
3247 devmask |= (1 << 0);
3248 if (dev1)
3249 devmask |= (1 << 1);
3250
3251 /* select device 0 again */
3252 ap->ops->dev_select(ap, 0);
3253
3254 /* issue bus reset */
Tejun Heo9b893912007-02-02 16:50:52 +09003255 if (ap->flags & ATA_FLAG_SRST) {
3256 rc = ata_bus_softreset(ap, devmask, jiffies + 40 * HZ);
3257 if (rc && rc != -ENODEV)
Tejun Heoaec5c3c2006-03-25 01:33:34 +09003258 goto err_out;
Tejun Heo9b893912007-02-02 16:50:52 +09003259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
3261 /*
3262 * determine by signature whether we have ATA or ATAPI devices
3263 */
Tejun Heo3f198592007-09-02 23:23:57 +09003264 device[0].class = ata_dev_try_classify(&device[0], dev0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 if ((slave_possible) && (err != 0x81))
Tejun Heo3f198592007-09-02 23:23:57 +09003266 device[1].class = ata_dev_try_classify(&device[1], dev1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 /* is double-select really necessary? */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003269 if (device[1].class != ATA_DEV_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 ap->ops->dev_select(ap, 1);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003271 if (device[0].class != ATA_DEV_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 ap->ops->dev_select(ap, 0);
3273
3274 /* if no devices were detected, disable this port */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003275 if ((device[0].class == ATA_DEV_NONE) &&
3276 (device[1].class == ATA_DEV_NONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 goto err_out;
3278
3279 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
3280 /* set up device control for ATA_FLAG_SATA_RESET */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003281 iowrite8(ap->ctl, ioaddr->ctl_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 }
3283
3284 DPRINTK("EXIT\n");
3285 return;
3286
3287err_out:
Tejun Heof15a1da2006-05-15 20:57:56 +09003288 ata_port_printk(ap, KERN_ERR, "disabling port\n");
Jeff Garzikac8869d2007-08-16 03:17:03 -04003289 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
3291 DPRINTK("EXIT\n");
3292}
3293
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003294/**
Tejun Heo936fd732007-08-06 18:36:23 +09003295 * sata_link_debounce - debounce SATA phy status
3296 * @link: ATA link to debounce SATA phy status for
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003297 * @params: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003298 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003299 *
Tejun Heo936fd732007-08-06 18:36:23 +09003300* Make sure SStatus of @link reaches stable state, determined by
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003301 * holding the same value where DET is not 1 for @duration polled
3302 * every @interval, before @timeout. Timeout constraints the
Tejun Heod4b2bab2007-02-02 16:50:52 +09003303 * beginning of the stable state. Because DET gets stuck at 1 on
3304 * some controllers after hot unplugging, this functions waits
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003305 * until timeout then returns 0 if DET is stable at 1.
3306 *
Tejun Heod4b2bab2007-02-02 16:50:52 +09003307 * @timeout is further limited by @deadline. The sooner of the
3308 * two is used.
3309 *
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003310 * LOCKING:
3311 * Kernel thread context (may sleep)
3312 *
3313 * RETURNS:
3314 * 0 on success, -errno on failure.
3315 */
Tejun Heo936fd732007-08-06 18:36:23 +09003316int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3317 unsigned long deadline)
Tejun Heo7a7921e2006-02-02 18:20:00 +09003318{
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003319 unsigned long interval_msec = params[0];
Tejun Heod4b2bab2007-02-02 16:50:52 +09003320 unsigned long duration = msecs_to_jiffies(params[1]);
3321 unsigned long last_jiffies, t;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003322 u32 last, cur;
3323 int rc;
3324
Tejun Heod4b2bab2007-02-02 16:50:52 +09003325 t = jiffies + msecs_to_jiffies(params[2]);
3326 if (time_before(t, deadline))
3327 deadline = t;
3328
Tejun Heo936fd732007-08-06 18:36:23 +09003329 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003330 return rc;
3331 cur &= 0xf;
3332
3333 last = cur;
3334 last_jiffies = jiffies;
3335
3336 while (1) {
3337 msleep(interval_msec);
Tejun Heo936fd732007-08-06 18:36:23 +09003338 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003339 return rc;
3340 cur &= 0xf;
3341
3342 /* DET stable? */
3343 if (cur == last) {
Tejun Heod4b2bab2007-02-02 16:50:52 +09003344 if (cur == 1 && time_before(jiffies, deadline))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003345 continue;
3346 if (time_after(jiffies, last_jiffies + duration))
3347 return 0;
3348 continue;
3349 }
3350
3351 /* unstable, start over */
3352 last = cur;
3353 last_jiffies = jiffies;
3354
Tejun Heof1545152007-07-16 14:29:40 +09003355 /* Check deadline. If debouncing failed, return
3356 * -EPIPE to tell upper layer to lower link speed.
3357 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003358 if (time_after(jiffies, deadline))
Tejun Heof1545152007-07-16 14:29:40 +09003359 return -EPIPE;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003360 }
3361}
3362
3363/**
Tejun Heo936fd732007-08-06 18:36:23 +09003364 * sata_link_resume - resume SATA link
3365 * @link: ATA link to resume SATA
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003366 * @params: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003367 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003368 *
Tejun Heo936fd732007-08-06 18:36:23 +09003369 * Resume SATA phy @link and debounce it.
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003370 *
3371 * LOCKING:
3372 * Kernel thread context (may sleep)
3373 *
3374 * RETURNS:
3375 * 0 on success, -errno on failure.
3376 */
Tejun Heo936fd732007-08-06 18:36:23 +09003377int sata_link_resume(struct ata_link *link, const unsigned long *params,
3378 unsigned long deadline)
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003379{
3380 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003381 int rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003382
Tejun Heo936fd732007-08-06 18:36:23 +09003383 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003384 return rc;
3385
Tejun Heo852ee162006-04-01 01:38:18 +09003386 scontrol = (scontrol & 0x0f0) | 0x300;
Tejun Heo81952c52006-05-15 20:57:47 +09003387
Tejun Heo936fd732007-08-06 18:36:23 +09003388 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003389 return rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003390
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003391 /* Some PHYs react badly if SStatus is pounded immediately
3392 * after resuming. Delay 200ms before debouncing.
3393 */
3394 msleep(200);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003395
Tejun Heo936fd732007-08-06 18:36:23 +09003396 return sata_link_debounce(link, params, deadline);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003397}
3398
Tejun Heof5914a42006-05-31 18:27:48 +09003399/**
3400 * ata_std_prereset - prepare for reset
Tejun Heocc0680a2007-08-06 18:36:23 +09003401 * @link: ATA link to be reset
Tejun Heod4b2bab2007-02-02 16:50:52 +09003402 * @deadline: deadline jiffies for the operation
Tejun Heof5914a42006-05-31 18:27:48 +09003403 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003404 * @link is about to be reset. Initialize it. Failure from
Tejun Heob8cffc62007-02-02 16:50:52 +09003405 * prereset makes libata abort whole reset sequence and give up
3406 * that port, so prereset should be best-effort. It does its
3407 * best to prepare for reset sequence but if things go wrong, it
3408 * should just whine, not fail.
Tejun Heof5914a42006-05-31 18:27:48 +09003409 *
3410 * LOCKING:
3411 * Kernel thread context (may sleep)
3412 *
3413 * RETURNS:
3414 * 0 on success, -errno otherwise.
3415 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003416int ata_std_prereset(struct ata_link *link, unsigned long deadline)
Tejun Heof5914a42006-05-31 18:27:48 +09003417{
Tejun Heocc0680a2007-08-06 18:36:23 +09003418 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09003419 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heoe9c83912006-07-03 16:07:26 +09003420 const unsigned long *timing = sata_ehc_deb_timing(ehc);
Tejun Heof5914a42006-05-31 18:27:48 +09003421 int rc;
3422
Tejun Heo31daabd2007-02-02 16:50:52 +09003423 /* handle link resume */
Tejun Heo28324302006-07-03 16:07:26 +09003424 if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
Tejun Heo0c887582007-08-06 18:36:23 +09003425 (link->flags & ATA_LFLAG_HRST_TO_RESUME))
Tejun Heo28324302006-07-03 16:07:26 +09003426 ehc->i.action |= ATA_EH_HARDRESET;
3427
Tejun Heof5914a42006-05-31 18:27:48 +09003428 /* if we're about to do hardreset, nothing more to do */
3429 if (ehc->i.action & ATA_EH_HARDRESET)
3430 return 0;
3431
Tejun Heo936fd732007-08-06 18:36:23 +09003432 /* if SATA, resume link */
Tejun Heoa16abc02007-05-21 18:33:47 +02003433 if (ap->flags & ATA_FLAG_SATA) {
Tejun Heo936fd732007-08-06 18:36:23 +09003434 rc = sata_link_resume(link, timing, deadline);
Tejun Heob8cffc62007-02-02 16:50:52 +09003435 /* whine about phy resume failure but proceed */
3436 if (rc && rc != -EOPNOTSUPP)
Tejun Heocc0680a2007-08-06 18:36:23 +09003437 ata_link_printk(link, KERN_WARNING, "failed to resume "
Tejun Heof5914a42006-05-31 18:27:48 +09003438 "link for reset (errno=%d)\n", rc);
Tejun Heof5914a42006-05-31 18:27:48 +09003439 }
3440
3441 /* Wait for !BSY if the controller can wait for the first D2H
3442 * Reg FIS and we don't know that no device is attached.
3443 */
Tejun Heo0c887582007-08-06 18:36:23 +09003444 if (!(link->flags & ATA_LFLAG_SKIP_D2H_BSY) && !ata_link_offline(link)) {
Tejun Heob8cffc62007-02-02 16:50:52 +09003445 rc = ata_wait_ready(ap, deadline);
Tejun Heo6dffaf62007-05-23 11:58:52 +02003446 if (rc && rc != -ENODEV) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003447 ata_link_printk(link, KERN_WARNING, "device not ready "
Tejun Heob8cffc62007-02-02 16:50:52 +09003448 "(errno=%d), forcing hardreset\n", rc);
3449 ehc->i.action |= ATA_EH_HARDRESET;
3450 }
3451 }
Tejun Heof5914a42006-05-31 18:27:48 +09003452
3453 return 0;
3454}
3455
Tejun Heoc2bd5802006-01-24 17:05:22 +09003456/**
3457 * ata_std_softreset - reset host port via ATA SRST
Tejun Heocc0680a2007-08-06 18:36:23 +09003458 * @link: ATA link to reset
Tejun Heoc2bd5802006-01-24 17:05:22 +09003459 * @classes: resulting classes of attached devices
Tejun Heod4b2bab2007-02-02 16:50:52 +09003460 * @deadline: deadline jiffies for the operation
Tejun Heoc2bd5802006-01-24 17:05:22 +09003461 *
Tejun Heo52783c52006-05-31 18:28:22 +09003462 * Reset host port using ATA SRST.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003463 *
3464 * LOCKING:
3465 * Kernel thread context (may sleep)
3466 *
3467 * RETURNS:
3468 * 0 on success, -errno otherwise.
3469 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003470int ata_std_softreset(struct ata_link *link, unsigned int *classes,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003471 unsigned long deadline)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003472{
Tejun Heocc0680a2007-08-06 18:36:23 +09003473 struct ata_port *ap = link->ap;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003474 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
Tejun Heod4b2bab2007-02-02 16:50:52 +09003475 unsigned int devmask = 0;
3476 int rc;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003477 u8 err;
3478
3479 DPRINTK("ENTER\n");
3480
Tejun Heo936fd732007-08-06 18:36:23 +09003481 if (ata_link_offline(link)) {
Tejun Heo3a397462006-02-10 23:58:48 +09003482 classes[0] = ATA_DEV_NONE;
3483 goto out;
3484 }
3485
Tejun Heoc2bd5802006-01-24 17:05:22 +09003486 /* determine if device 0/1 are present */
3487 if (ata_devchk(ap, 0))
3488 devmask |= (1 << 0);
3489 if (slave_possible && ata_devchk(ap, 1))
3490 devmask |= (1 << 1);
3491
Tejun Heoc2bd5802006-01-24 17:05:22 +09003492 /* select device 0 again */
3493 ap->ops->dev_select(ap, 0);
3494
3495 /* issue bus reset */
3496 DPRINTK("about to softreset, devmask=%x\n", devmask);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003497 rc = ata_bus_softreset(ap, devmask, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003498 /* if link is occupied, -ENODEV too is an error */
Tejun Heo936fd732007-08-06 18:36:23 +09003499 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003500 ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003501 return rc;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003502 }
3503
3504 /* determine by signature whether we have ATA or ATAPI devices */
Tejun Heo3f198592007-09-02 23:23:57 +09003505 classes[0] = ata_dev_try_classify(&link->device[0],
3506 devmask & (1 << 0), &err);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003507 if (slave_possible && err != 0x81)
Tejun Heo3f198592007-09-02 23:23:57 +09003508 classes[1] = ata_dev_try_classify(&link->device[1],
3509 devmask & (1 << 1), &err);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003510
Tejun Heo3a397462006-02-10 23:58:48 +09003511 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09003512 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
3513 return 0;
3514}
3515
3516/**
Tejun Heocc0680a2007-08-06 18:36:23 +09003517 * sata_link_hardreset - reset link via SATA phy reset
3518 * @link: link to reset
Tejun Heob6103f62006-11-01 17:59:53 +09003519 * @timing: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003520 * @deadline: deadline jiffies for the operation
Tejun Heoc2bd5802006-01-24 17:05:22 +09003521 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003522 * SATA phy-reset @link using DET bits of SControl register.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003523 *
3524 * LOCKING:
3525 * Kernel thread context (may sleep)
3526 *
3527 * RETURNS:
3528 * 0 on success, -errno otherwise.
3529 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003530int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003531 unsigned long deadline)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003532{
Tejun Heo852ee162006-04-01 01:38:18 +09003533 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003534 int rc;
Tejun Heo852ee162006-04-01 01:38:18 +09003535
Tejun Heoc2bd5802006-01-24 17:05:22 +09003536 DPRINTK("ENTER\n");
3537
Tejun Heo936fd732007-08-06 18:36:23 +09003538 if (sata_set_spd_needed(link)) {
Tejun Heo1c3fae42006-04-02 20:53:28 +09003539 /* SATA spec says nothing about how to reconfigure
3540 * spd. To be on the safe side, turn off phy during
3541 * reconfiguration. This works for at least ICH7 AHCI
3542 * and Sil3124.
3543 */
Tejun Heo936fd732007-08-06 18:36:23 +09003544 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003545 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09003546
Martin Hicksa34b6fc2006-07-05 15:06:13 -04003547 scontrol = (scontrol & 0x0f0) | 0x304;
Tejun Heo81952c52006-05-15 20:57:47 +09003548
Tejun Heo936fd732007-08-06 18:36:23 +09003549 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003550 goto out;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003551
Tejun Heo936fd732007-08-06 18:36:23 +09003552 sata_set_spd(link);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003553 }
3554
3555 /* issue phy wake/reset */
Tejun Heo936fd732007-08-06 18:36:23 +09003556 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003557 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09003558
Tejun Heo852ee162006-04-01 01:38:18 +09003559 scontrol = (scontrol & 0x0f0) | 0x301;
Tejun Heo81952c52006-05-15 20:57:47 +09003560
Tejun Heo936fd732007-08-06 18:36:23 +09003561 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003562 goto out;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003563
Tejun Heo1c3fae42006-04-02 20:53:28 +09003564 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
Tejun Heoc2bd5802006-01-24 17:05:22 +09003565 * 10.4.2 says at least 1 ms.
3566 */
3567 msleep(1);
3568
Tejun Heo936fd732007-08-06 18:36:23 +09003569 /* bring link back */
3570 rc = sata_link_resume(link, timing, deadline);
Tejun Heob6103f62006-11-01 17:59:53 +09003571 out:
3572 DPRINTK("EXIT, rc=%d\n", rc);
3573 return rc;
3574}
3575
3576/**
3577 * sata_std_hardreset - reset host port via SATA phy reset
Tejun Heocc0680a2007-08-06 18:36:23 +09003578 * @link: link to reset
Tejun Heob6103f62006-11-01 17:59:53 +09003579 * @class: resulting class of attached device
Tejun Heod4b2bab2007-02-02 16:50:52 +09003580 * @deadline: deadline jiffies for the operation
Tejun Heob6103f62006-11-01 17:59:53 +09003581 *
3582 * SATA phy-reset host port using DET bits of SControl register,
3583 * wait for !BSY and classify the attached device.
3584 *
3585 * LOCKING:
3586 * Kernel thread context (may sleep)
3587 *
3588 * RETURNS:
3589 * 0 on success, -errno otherwise.
3590 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003591int sata_std_hardreset(struct ata_link *link, unsigned int *class,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003592 unsigned long deadline)
Tejun Heob6103f62006-11-01 17:59:53 +09003593{
Tejun Heocc0680a2007-08-06 18:36:23 +09003594 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09003595 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
Tejun Heob6103f62006-11-01 17:59:53 +09003596 int rc;
3597
3598 DPRINTK("ENTER\n");
3599
3600 /* do hardreset */
Tejun Heocc0680a2007-08-06 18:36:23 +09003601 rc = sata_link_hardreset(link, timing, deadline);
Tejun Heob6103f62006-11-01 17:59:53 +09003602 if (rc) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003603 ata_link_printk(link, KERN_ERR,
Tejun Heob6103f62006-11-01 17:59:53 +09003604 "COMRESET failed (errno=%d)\n", rc);
3605 return rc;
3606 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09003607
Tejun Heoc2bd5802006-01-24 17:05:22 +09003608 /* TODO: phy layer with polling, timeouts, etc. */
Tejun Heo936fd732007-08-06 18:36:23 +09003609 if (ata_link_offline(link)) {
Tejun Heoc2bd5802006-01-24 17:05:22 +09003610 *class = ATA_DEV_NONE;
3611 DPRINTK("EXIT, link offline\n");
3612 return 0;
3613 }
3614
Tejun Heo34fee222007-02-02 15:29:27 +09003615 /* wait a while before checking status, see SRST for more info */
3616 msleep(150);
3617
Tejun Heod4b2bab2007-02-02 16:50:52 +09003618 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003619 /* link occupied, -ENODEV too is an error */
3620 if (rc) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003621 ata_link_printk(link, KERN_ERR,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003622 "COMRESET failed (errno=%d)\n", rc);
3623 return rc;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003624 }
3625
Tejun Heo3a397462006-02-10 23:58:48 +09003626 ap->ops->dev_select(ap, 0); /* probably unnecessary */
3627
Tejun Heo3f198592007-09-02 23:23:57 +09003628 *class = ata_dev_try_classify(link->device, 1, NULL);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003629
3630 DPRINTK("EXIT, class=%u\n", *class);
3631 return 0;
3632}
3633
3634/**
3635 * ata_std_postreset - standard postreset callback
Tejun Heocc0680a2007-08-06 18:36:23 +09003636 * @link: the target ata_link
Tejun Heoc2bd5802006-01-24 17:05:22 +09003637 * @classes: classes of attached devices
3638 *
3639 * This function is invoked after a successful reset. Note that
3640 * the device might have been reset more than once using
3641 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003642 *
Tejun Heoc2bd5802006-01-24 17:05:22 +09003643 * LOCKING:
3644 * Kernel thread context (may sleep)
3645 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003646void ata_std_postreset(struct ata_link *link, unsigned int *classes)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003647{
Tejun Heocc0680a2007-08-06 18:36:23 +09003648 struct ata_port *ap = link->ap;
Tejun Heodc2b3512006-05-15 20:58:00 +09003649 u32 serror;
3650
Tejun Heoc2bd5802006-01-24 17:05:22 +09003651 DPRINTK("ENTER\n");
3652
Tejun Heoc2bd5802006-01-24 17:05:22 +09003653 /* print link status */
Tejun Heo936fd732007-08-06 18:36:23 +09003654 sata_print_link_status(link);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003655
Tejun Heodc2b3512006-05-15 20:58:00 +09003656 /* clear SError */
Tejun Heo936fd732007-08-06 18:36:23 +09003657 if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
3658 sata_scr_write(link, SCR_ERROR, serror);
Tejun Heodc2b3512006-05-15 20:58:00 +09003659
Tejun Heoc2bd5802006-01-24 17:05:22 +09003660 /* is double-select really necessary? */
3661 if (classes[0] != ATA_DEV_NONE)
3662 ap->ops->dev_select(ap, 1);
3663 if (classes[1] != ATA_DEV_NONE)
3664 ap->ops->dev_select(ap, 0);
3665
Tejun Heo3a397462006-02-10 23:58:48 +09003666 /* bail out if no device is present */
3667 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
3668 DPRINTK("EXIT, no device\n");
3669 return;
3670 }
3671
3672 /* set up device control */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003673 if (ap->ioaddr.ctl_addr)
3674 iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003675
3676 DPRINTK("EXIT\n");
3677}
3678
Tejun Heoa62c0fc2006-01-24 17:05:22 +09003679/**
Tejun Heo623a3122006-03-05 17:55:58 +09003680 * ata_dev_same_device - Determine whether new ID matches configured device
Tejun Heo623a3122006-03-05 17:55:58 +09003681 * @dev: device to compare against
3682 * @new_class: class of the new device
3683 * @new_id: IDENTIFY page of the new device
3684 *
3685 * Compare @new_class and @new_id against @dev and determine
3686 * whether @dev is the device indicated by @new_class and
3687 * @new_id.
3688 *
3689 * LOCKING:
3690 * None.
3691 *
3692 * RETURNS:
3693 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3694 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003695static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3696 const u16 *new_id)
Tejun Heo623a3122006-03-05 17:55:58 +09003697{
3698 const u16 *old_id = dev->id;
Tejun Heoa0cf7332007-01-02 20:18:49 +09003699 unsigned char model[2][ATA_ID_PROD_LEN + 1];
3700 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
Tejun Heo623a3122006-03-05 17:55:58 +09003701
3702 if (dev->class != new_class) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003703 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
3704 dev->class, new_class);
Tejun Heo623a3122006-03-05 17:55:58 +09003705 return 0;
3706 }
3707
Tejun Heoa0cf7332007-01-02 20:18:49 +09003708 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3709 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3710 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3711 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
Tejun Heo623a3122006-03-05 17:55:58 +09003712
3713 if (strcmp(model[0], model[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003714 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
3715 "'%s' != '%s'\n", model[0], model[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003716 return 0;
3717 }
3718
3719 if (strcmp(serial[0], serial[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003720 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
3721 "'%s' != '%s'\n", serial[0], serial[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003722 return 0;
3723 }
3724
Tejun Heo623a3122006-03-05 17:55:58 +09003725 return 1;
3726}
3727
3728/**
Tejun Heofe309112007-05-15 03:28:15 +09003729 * ata_dev_reread_id - Re-read IDENTIFY data
Henrik Kretzschmar3fae4502007-06-19 10:10:50 +02003730 * @dev: target ATA device
Tejun Heobff04642006-11-10 18:08:10 +09003731 * @readid_flags: read ID flags
Tejun Heo623a3122006-03-05 17:55:58 +09003732 *
3733 * Re-read IDENTIFY page and make sure @dev is still attached to
3734 * the port.
3735 *
3736 * LOCKING:
3737 * Kernel thread context (may sleep)
3738 *
3739 * RETURNS:
3740 * 0 on success, negative errno otherwise
3741 */
Tejun Heofe309112007-05-15 03:28:15 +09003742int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
Tejun Heo623a3122006-03-05 17:55:58 +09003743{
Tejun Heo5eb45c02006-04-02 18:51:52 +09003744 unsigned int class = dev->class;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003745 u16 *id = (void *)dev->link->ap->sector_buf;
Tejun Heo623a3122006-03-05 17:55:58 +09003746 int rc;
3747
Tejun Heofe635c72006-05-15 20:57:35 +09003748 /* read ID data */
Tejun Heobff04642006-11-10 18:08:10 +09003749 rc = ata_dev_read_id(dev, &class, readid_flags, id);
Tejun Heo623a3122006-03-05 17:55:58 +09003750 if (rc)
Tejun Heofe309112007-05-15 03:28:15 +09003751 return rc;
Tejun Heo623a3122006-03-05 17:55:58 +09003752
3753 /* is the device still there? */
Tejun Heofe309112007-05-15 03:28:15 +09003754 if (!ata_dev_same_device(dev, class, id))
3755 return -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09003756
Tejun Heofe635c72006-05-15 20:57:35 +09003757 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heofe309112007-05-15 03:28:15 +09003758 return 0;
3759}
3760
3761/**
3762 * ata_dev_revalidate - Revalidate ATA device
3763 * @dev: device to revalidate
Tejun Heo422c9da2007-09-23 13:14:12 +09003764 * @new_class: new class code
Tejun Heofe309112007-05-15 03:28:15 +09003765 * @readid_flags: read ID flags
3766 *
3767 * Re-read IDENTIFY page, make sure @dev is still attached to the
3768 * port and reconfigure it according to the new IDENTIFY page.
3769 *
3770 * LOCKING:
3771 * Kernel thread context (may sleep)
3772 *
3773 * RETURNS:
3774 * 0 on success, negative errno otherwise
3775 */
Tejun Heo422c9da2007-09-23 13:14:12 +09003776int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3777 unsigned int readid_flags)
Tejun Heofe309112007-05-15 03:28:15 +09003778{
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003779 u64 n_sectors = dev->n_sectors;
Tejun Heofe309112007-05-15 03:28:15 +09003780 int rc;
3781
3782 if (!ata_dev_enabled(dev))
3783 return -ENODEV;
3784
Tejun Heo422c9da2007-09-23 13:14:12 +09003785 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3786 if (ata_class_enabled(new_class) &&
3787 new_class != ATA_DEV_ATA && new_class != ATA_DEV_ATAPI) {
3788 ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
3789 dev->class, new_class);
3790 rc = -ENODEV;
3791 goto fail;
3792 }
3793
Tejun Heofe309112007-05-15 03:28:15 +09003794 /* re-read ID */
3795 rc = ata_dev_reread_id(dev, readid_flags);
3796 if (rc)
3797 goto fail;
Tejun Heo623a3122006-03-05 17:55:58 +09003798
3799 /* configure device according to the new ID */
Tejun Heoefdaedc2006-11-01 18:38:52 +09003800 rc = ata_dev_configure(dev);
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003801 if (rc)
3802 goto fail;
3803
3804 /* verify n_sectors hasn't changed */
Tejun Heob54eebd2007-08-17 18:46:51 +09003805 if (dev->class == ATA_DEV_ATA && n_sectors &&
3806 dev->n_sectors != n_sectors) {
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003807 ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
3808 "%llu != %llu\n",
3809 (unsigned long long)n_sectors,
3810 (unsigned long long)dev->n_sectors);
Tejun Heo8270bec2007-08-16 03:02:22 +09003811
3812 /* restore original n_sectors */
3813 dev->n_sectors = n_sectors;
3814
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003815 rc = -ENODEV;
3816 goto fail;
3817 }
3818
3819 return 0;
Tejun Heo623a3122006-03-05 17:55:58 +09003820
3821 fail:
Tejun Heof15a1da2006-05-15 20:57:56 +09003822 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
Tejun Heo623a3122006-03-05 17:55:58 +09003823 return rc;
3824}
3825
Alan Cox6919a0a2006-10-27 19:08:46 -07003826struct ata_blacklist_entry {
3827 const char *model_num;
3828 const char *model_rev;
3829 unsigned long horkage;
3830};
3831
3832static const struct ata_blacklist_entry ata_device_blacklist [] = {
3833 /* Devices with DMA related problems under Linux */
3834 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
3835 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
3836 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
3837 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
3838 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
3839 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
3840 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
3841 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
3842 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
3843 { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
3844 { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
3845 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
3846 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
3847 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
3848 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
3849 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
3850 { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
3851 { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
3852 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
3853 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
3854 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
3855 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
3856 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
3857 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
3858 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
3859 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003860 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
3861 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
3862 { "SAMSUNG CD-ROM SN-124","N001", ATA_HORKAGE_NODMA },
Dave Jones39f19882007-05-21 14:31:03 -04003863 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
Tejun Heo5acd50f2007-06-10 14:52:36 +09003864 { "IOMEGA ZIP 250 ATAPI", NULL, ATA_HORKAGE_NODMA }, /* temporary fix */
Tejun Heo39ce7122007-07-05 12:31:31 +09003865 { "IOMEGA ZIP 250 ATAPI Floppy",
3866 NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003867
Albert Lee18d6e9d2007-04-02 11:34:15 +08003868 /* Weird ATAPI devices */
Tejun Heo40a1d532007-06-27 02:49:38 +09003869 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
Albert Lee18d6e9d2007-04-02 11:34:15 +08003870
Alan Cox6919a0a2006-10-27 19:08:46 -07003871 /* Devices we expect to fail diagnostics */
3872
3873 /* Devices where NCQ should be avoided */
3874 /* NCQ is slow */
3875 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
Tejun Heo09125ea2007-02-28 15:21:23 +09003876 /* http://thread.gmane.org/gmane.linux.ide/14907 */
3877 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
Paul Rolland7acfaf32007-03-26 21:43:44 -08003878 /* NCQ is broken */
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003879 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
Alan Cox0e3dbc02007-09-20 15:22:47 +01003880 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
Prarit Bhargava2f8d90a2007-07-10 18:13:28 -04003881 { "HITACHI HDS7250SASUN500G 0621KTAWSD", "K2AOAJ0AHITACHI",
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003882 ATA_HORKAGE_NONCQ },
3883
Robert Hancock36e337d2007-04-02 22:05:29 -06003884 /* Blacklist entries taken from Silicon Image 3124/3132
3885 Windows driver .inf file - also several Linux problem reports */
3886 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
3887 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
3888 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
Tejun Heobd9c5a32007-06-08 22:20:59 +09003889 /* Drives which do spurious command completion */
3890 { "HTS541680J9SA00", "SB2IC7EP", ATA_HORKAGE_NONCQ, },
Tejun Heo2f8fceb2007-06-18 14:38:41 +09003891 { "HTS541612J9SA00", "SBDIC7JP", ATA_HORKAGE_NONCQ, },
Tejun Heoe14cbfa2007-06-25 11:28:59 +09003892 { "Hitachi HTS541616J9SA00", "SB4OC70P", ATA_HORKAGE_NONCQ, },
Tejun Heo2f8fceb2007-06-18 14:38:41 +09003893 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
Tejun Heoa520f262007-07-10 16:16:18 +09003894 { "FUJITSU MHV2080BH", "00840028", ATA_HORKAGE_NONCQ, },
Tejun Heo3fb65892007-07-20 12:49:38 +09003895 { "ST9160821AS", "3.CLF", ATA_HORKAGE_NONCQ, },
Alan Cox0e3dbc02007-09-20 15:22:47 +01003896 { "ST3160812AS", "3.AD", ATA_HORKAGE_NONCQ, },
Tejun Heo5d6aca82007-07-28 16:25:25 +09003897 { "SAMSUNG HD401LJ", "ZZ100-15", ATA_HORKAGE_NONCQ, },
Alan Cox6919a0a2006-10-27 19:08:46 -07003898
Tejun Heo16c55b02007-08-29 11:58:33 +09003899 /* devices which puke on READ_NATIVE_MAX */
3900 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
3901 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
3902 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
3903 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003904
3905 /* End Marker */
3906 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003908
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003909int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
3910{
3911 const char *p;
3912 int len;
3913
3914 /*
3915 * check for trailing wildcard: *\0
3916 */
3917 p = strchr(patt, wildchar);
3918 if (p && ((*(p + 1)) == 0))
3919 len = p - patt;
3920 else
3921 len = strlen(name);
3922
3923 return strncmp(patt, name, len);
3924}
3925
Tejun Heo75683fe2007-07-05 13:31:27 +09003926static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927{
Tejun Heo8bfa79f2007-01-02 20:19:40 +09003928 unsigned char model_num[ATA_ID_PROD_LEN + 1];
3929 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
Alan Cox6919a0a2006-10-27 19:08:46 -07003930 const struct ata_blacklist_entry *ad = ata_device_blacklist;
Albert Lee3a778272006-06-22 13:00:25 +08003931
Tejun Heo8bfa79f2007-01-02 20:19:40 +09003932 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
3933 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934
Alan Cox6919a0a2006-10-27 19:08:46 -07003935 while (ad->model_num) {
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003936 if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
Alan Cox6919a0a2006-10-27 19:08:46 -07003937 if (ad->model_rev == NULL)
3938 return ad->horkage;
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003939 if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
Alan Cox6919a0a2006-10-27 19:08:46 -07003940 return ad->horkage;
Alan Coxf4b15fe2006-03-22 15:54:04 +00003941 }
Alan Cox6919a0a2006-10-27 19:08:46 -07003942 ad++;
Alan Coxf4b15fe2006-03-22 15:54:04 +00003943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 return 0;
3945}
3946
Alan Cox6919a0a2006-10-27 19:08:46 -07003947static int ata_dma_blacklisted(const struct ata_device *dev)
3948{
3949 /* We don't support polling DMA.
3950 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
3951 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
3952 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003953 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
Alan Cox6919a0a2006-10-27 19:08:46 -07003954 (dev->flags & ATA_DFLAG_CDB_INTR))
3955 return 1;
Tejun Heo75683fe2007-07-05 13:31:27 +09003956 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07003957}
3958
Tejun Heoa6d5a512006-03-06 04:31:57 +09003959/**
3960 * ata_dev_xfermask - Compute supported xfermask of the given device
Tejun Heoa6d5a512006-03-06 04:31:57 +09003961 * @dev: Device to compute xfermask for
3962 *
Tejun Heoacf356b2006-03-24 14:07:50 +09003963 * Compute supported xfermask of @dev and store it in
3964 * dev->*_mask. This function is responsible for applying all
3965 * known limits including host controller limits, device
3966 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09003967 *
3968 * LOCKING:
3969 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09003970 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003971static void ata_dev_xfermask(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003973 struct ata_link *link = dev->link;
3974 struct ata_port *ap = link->ap;
Jeff Garzikcca39742006-08-24 03:19:22 -04003975 struct ata_host *host = ap->host;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003976 unsigned long xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977
Tejun Heo37deecb2006-08-10 16:59:07 +09003978 /* controller modes available */
Tejun Heo565083e2006-04-02 17:54:47 +09003979 xfer_mask = ata_pack_xfermask(ap->pio_mask,
3980 ap->mwdma_mask, ap->udma_mask);
3981
Robert Hancock8343f882007-03-06 02:37:51 -08003982 /* drive modes available */
Tejun Heo37deecb2006-08-10 16:59:07 +09003983 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
3984 dev->mwdma_mask, dev->udma_mask);
3985 xfer_mask &= ata_id_xfermask(dev->id);
Tejun Heo565083e2006-04-02 17:54:47 +09003986
Alan Coxb352e572006-08-10 18:52:12 +01003987 /*
3988 * CFA Advanced TrueIDE timings are not allowed on a shared
3989 * cable
3990 */
3991 if (ata_dev_pair(dev)) {
3992 /* No PIO5 or PIO6 */
3993 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
3994 /* No MWDMA3 or MWDMA 4 */
3995 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
3996 }
3997
Tejun Heo37deecb2006-08-10 16:59:07 +09003998 if (ata_dma_blacklisted(dev)) {
3999 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Tejun Heof15a1da2006-05-15 20:57:56 +09004000 ata_dev_printk(dev, KERN_WARNING,
4001 "device is on DMA blacklist, disabling DMA\n");
Tejun Heo37deecb2006-08-10 16:59:07 +09004002 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09004003
Petr Vandrovec14d66ab2007-03-08 10:12:12 +01004004 if ((host->flags & ATA_HOST_SIMPLEX) &&
4005 host->simplex_claimed && host->simplex_claimed != ap) {
Tejun Heo37deecb2006-08-10 16:59:07 +09004006 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4007 ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
4008 "other device, disabling DMA\n");
Alan Cox5444a6f2006-03-27 18:58:20 +01004009 }
Tejun Heo565083e2006-04-02 17:54:47 +09004010
Jeff Garzike4246752007-03-09 09:56:46 -05004011 if (ap->flags & ATA_FLAG_NO_IORDY)
4012 xfer_mask &= ata_pio_mask_no_iordy(dev);
4013
Alan Cox5444a6f2006-03-27 18:58:20 +01004014 if (ap->ops->mode_filter)
Alan Coxa76b62c2007-03-09 09:34:07 -05004015 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01004016
Robert Hancock8343f882007-03-06 02:37:51 -08004017 /* Apply cable rule here. Don't apply it early because when
4018 * we handle hot plug the cable type can itself change.
4019 * Check this last so that we know if the transfer rate was
4020 * solely limited by the cable.
4021 * Unknown or 80 wire cables reported host side are checked
4022 * drive side as well. Cases where we know a 40wire cable
4023 * is used safely for 80 are not checked here.
4024 */
4025 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4026 /* UDMA/44 or higher would be available */
4027 if((ap->cbl == ATA_CBL_PATA40) ||
4028 (ata_drive_40wire(dev->id) &&
4029 (ap->cbl == ATA_CBL_PATA_UNK ||
4030 ap->cbl == ATA_CBL_PATA80))) {
4031 ata_dev_printk(dev, KERN_WARNING,
4032 "limited to UDMA/33 due to 40-wire cable\n");
4033 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4034 }
4035
Tejun Heo565083e2006-04-02 17:54:47 +09004036 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4037 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038}
4039
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 * @dev: Device to which command will be sent
4043 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004044 * Issue SET FEATURES - XFER MODE command to device @dev
4045 * on port @ap.
4046 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004048 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09004049 *
4050 * RETURNS:
4051 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 */
4053
Tejun Heo3373efd2006-05-15 20:57:53 +09004054static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055{
Tejun Heoa0123702005-12-13 14:49:31 +09004056 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09004057 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058
4059 /* set up set-features taskfile */
4060 DPRINTK("set features - xfer mode\n");
4061
Tejun Heo464cf172007-05-27 15:10:40 +02004062 /* Some controllers and ATAPI devices show flaky interrupt
4063 * behavior after setting xfer mode. Use polling instead.
4064 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004065 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004066 tf.command = ATA_CMD_SET_FEATURES;
4067 tf.feature = SETFEATURES_XFER;
Tejun Heo464cf172007-05-27 15:10:40 +02004068 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
Tejun Heoa0123702005-12-13 14:49:31 +09004069 tf.protocol = ATA_PROT_NODATA;
4070 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071
Tejun Heo3373efd2006-05-15 20:57:53 +09004072 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073
Tejun Heo83206a22006-03-24 15:25:31 +09004074 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4075 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076}
4077
4078/**
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004079 * ata_dev_set_AN - Issue SET FEATURES - SATA FEATURES
4080 * @dev: Device to which command will be sent
4081 * @enable: Whether to enable or disable the feature
4082 *
4083 * Issue SET FEATURES - SATA FEATURES command to device @dev
4084 * on port @ap with sector count set to indicate Asynchronous
4085 * Notification feature
4086 *
4087 * LOCKING:
4088 * PCI/etc. bus probe sem.
4089 *
4090 * RETURNS:
4091 * 0 on success, AC_ERR_* mask otherwise.
4092 */
4093static unsigned int ata_dev_set_AN(struct ata_device *dev, u8 enable)
4094{
4095 struct ata_taskfile tf;
4096 unsigned int err_mask;
4097
4098 /* set up set-features taskfile */
4099 DPRINTK("set features - SATA features\n");
4100
4101 ata_tf_init(dev, &tf);
4102 tf.command = ATA_CMD_SET_FEATURES;
4103 tf.feature = enable;
4104 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4105 tf.protocol = ATA_PROT_NODATA;
4106 tf.nsect = SATA_AN;
4107
4108 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
4109
4110 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4111 return err_mask;
4112}
4113
4114/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04004115 * ata_dev_init_params - Issue INIT DEV PARAMS command
Albert Lee8bf62ece2005-05-12 15:29:42 -04004116 * @dev: Device to which command will be sent
Randy Dunlape2a7f772006-05-18 10:50:18 -07004117 * @heads: Number of heads (taskfile parameter)
4118 * @sectors: Number of sectors (taskfile parameter)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004119 *
4120 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09004121 * Kernel thread context (may sleep)
4122 *
4123 * RETURNS:
4124 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ece2005-05-12 15:29:42 -04004125 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004126static unsigned int ata_dev_init_params(struct ata_device *dev,
4127 u16 heads, u16 sectors)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004128{
Tejun Heoa0123702005-12-13 14:49:31 +09004129 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09004130 unsigned int err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004131
4132 /* Number of sectors per track 1-255. Number of heads 1-16 */
4133 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08004134 return AC_ERR_INVALID;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004135
4136 /* set up init dev params taskfile */
4137 DPRINTK("init dev params \n");
4138
Tejun Heo3373efd2006-05-15 20:57:53 +09004139 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004140 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4141 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4142 tf.protocol = ATA_PROT_NODATA;
4143 tf.nsect = sectors;
4144 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04004145
Tejun Heo3373efd2006-05-15 20:57:53 +09004146 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Alan Cox18b24662007-08-08 14:28:49 +01004147 /* A clean abort indicates an original or just out of spec drive
4148 and we should continue as we issue the setup based on the
4149 drive reported working geometry */
4150 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4151 err_mask = 0;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004152
Tejun Heo6aff8f12006-02-15 18:24:09 +09004153 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4154 return err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004155}
4156
4157/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004158 * ata_sg_clean - Unmap DMA memory associated with command
4159 * @qc: Command containing DMA memory to be released
4160 *
4161 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 *
4163 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004164 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 */
Tejun Heo70e6ad02006-11-14 22:47:10 +09004166void ata_sg_clean(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167{
4168 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004169 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004171 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
Tejun Heoa46314742006-02-11 19:11:13 +09004173 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
4174 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
4176 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05004177 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05004179 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004181 /* if we padded the buffer out to 32-bit bound, and data
4182 * xfer direction is from-device, we must copy from the
4183 * pad buffer back into the supplied buffer
4184 */
4185 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
4186 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4187
4188 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05004189 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06004190 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004191 /* restore last sg */
4192 sg[qc->orig_n_elem - 1].length += qc->pad_len;
4193 if (pad_buf) {
4194 struct scatterlist *psg = &qc->pad_sgent;
4195 void *addr = kmap_atomic(psg->page, KM_IRQ0);
4196 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05004197 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004198 }
4199 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09004200 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06004201 dma_unmap_single(ap->dev,
Jeff Garzike1410f22005-11-14 14:06:26 -05004202 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
4203 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004204 /* restore sg */
4205 sg->length += qc->pad_len;
4206 if (pad_buf)
4207 memcpy(qc->buf_virt + sg->length - qc->pad_len,
4208 pad_buf, qc->pad_len);
4209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
4211 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004212 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213}
4214
4215/**
4216 * ata_fill_sg - Fill PCI IDE PRD table
4217 * @qc: Metadata associated with taskfile to be transferred
4218 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004219 * Fill PCI IDE PRD (scatter-gather) table with segments
4220 * associated with the current disk command.
4221 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004223 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 *
4225 */
4226static void ata_fill_sg(struct ata_queued_cmd *qc)
4227{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004229 struct scatterlist *sg;
4230 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231
Tejun Heoa46314742006-02-11 19:11:13 +09004232 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05004233 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
4235 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004236 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 u32 addr, offset;
4238 u32 sg_len, len;
4239
4240 /* determine if physical DMA addr spans 64K boundary.
4241 * Note h/w doesn't support 64-bit, so we unconditionally
4242 * truncate dma_addr_t to u32.
4243 */
4244 addr = (u32) sg_dma_address(sg);
4245 sg_len = sg_dma_len(sg);
4246
4247 while (sg_len) {
4248 offset = addr & 0xffff;
4249 len = sg_len;
4250 if ((offset + sg_len) > 0x10000)
4251 len = 0x10000 - offset;
4252
4253 ap->prd[idx].addr = cpu_to_le32(addr);
4254 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
4255 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
4256
4257 idx++;
4258 sg_len -= len;
4259 addr += len;
4260 }
4261 }
4262
4263 if (idx)
4264 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4265}
Tejun Heob9a41972007-06-27 02:48:43 +09004266
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267/**
Alan Coxd26fc952007-07-06 19:13:52 -04004268 * ata_fill_sg_dumb - Fill PCI IDE PRD table
4269 * @qc: Metadata associated with taskfile to be transferred
4270 *
4271 * Fill PCI IDE PRD (scatter-gather) table with segments
4272 * associated with the current disk command. Perform the fill
4273 * so that we avoid writing any length 64K records for
4274 * controllers that don't follow the spec.
4275 *
4276 * LOCKING:
4277 * spin_lock_irqsave(host lock)
4278 *
4279 */
4280static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
4281{
4282 struct ata_port *ap = qc->ap;
4283 struct scatterlist *sg;
4284 unsigned int idx;
4285
4286 WARN_ON(qc->__sg == NULL);
4287 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
4288
4289 idx = 0;
4290 ata_for_each_sg(sg, qc) {
4291 u32 addr, offset;
4292 u32 sg_len, len, blen;
4293
4294 /* determine if physical DMA addr spans 64K boundary.
4295 * Note h/w doesn't support 64-bit, so we unconditionally
4296 * truncate dma_addr_t to u32.
4297 */
4298 addr = (u32) sg_dma_address(sg);
4299 sg_len = sg_dma_len(sg);
4300
4301 while (sg_len) {
4302 offset = addr & 0xffff;
4303 len = sg_len;
4304 if ((offset + sg_len) > 0x10000)
4305 len = 0x10000 - offset;
4306
4307 blen = len & 0xffff;
4308 ap->prd[idx].addr = cpu_to_le32(addr);
4309 if (blen == 0) {
4310 /* Some PATA chipsets like the CS5530 can't
4311 cope with 0x0000 meaning 64K as the spec says */
4312 ap->prd[idx].flags_len = cpu_to_le32(0x8000);
4313 blen = 0x8000;
4314 ap->prd[++idx].addr = cpu_to_le32(addr + 0x8000);
4315 }
4316 ap->prd[idx].flags_len = cpu_to_le32(blen);
4317 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
4318
4319 idx++;
4320 sg_len -= len;
4321 addr += len;
4322 }
4323 }
4324
4325 if (idx)
4326 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4327}
4328
4329/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
4331 * @qc: Metadata associated with taskfile to check
4332 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004333 * Allow low-level driver to filter ATA PACKET commands, returning
4334 * a status indicating whether or not it is OK to use DMA for the
4335 * supplied PACKET command.
4336 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004338 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004339 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 * RETURNS: 0 when ATAPI DMA can be used
4341 * nonzero otherwise
4342 */
4343int ata_check_atapi_dma(struct ata_queued_cmd *qc)
4344{
4345 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Tejun Heob9a41972007-06-27 02:48:43 +09004347 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4348 * few ATAPI devices choke on such DMA requests.
4349 */
4350 if (unlikely(qc->nbytes & 15))
4351 return 1;
Albert Lee6f23a312007-04-02 11:39:25 +08004352
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 if (ap->ops->check_atapi_dma)
Tejun Heob9a41972007-06-27 02:48:43 +09004354 return ap->ops->check_atapi_dma(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355
Tejun Heob9a41972007-06-27 02:48:43 +09004356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357}
Tejun Heob9a41972007-06-27 02:48:43 +09004358
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359/**
Tejun Heo31cc23b2007-09-23 13:14:12 +09004360 * ata_std_qc_defer - Check whether a qc needs to be deferred
4361 * @qc: ATA command in question
4362 *
4363 * Non-NCQ commands cannot run with any other command, NCQ or
4364 * not. As upper layer only knows the queue depth, we are
4365 * responsible for maintaining exclusion. This function checks
4366 * whether a new command @qc can be issued.
4367 *
4368 * LOCKING:
4369 * spin_lock_irqsave(host lock)
4370 *
4371 * RETURNS:
4372 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4373 */
4374int ata_std_qc_defer(struct ata_queued_cmd *qc)
4375{
4376 struct ata_link *link = qc->dev->link;
4377
4378 if (qc->tf.protocol == ATA_PROT_NCQ) {
4379 if (!ata_tag_valid(link->active_tag))
4380 return 0;
4381 } else {
4382 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4383 return 0;
4384 }
4385
4386 return ATA_DEFER_LINK;
4387}
4388
4389/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 * ata_qc_prep - Prepare taskfile for submission
4391 * @qc: Metadata associated with taskfile to be prepared
4392 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004393 * Prepare ATA taskfile for submission.
4394 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004396 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 */
4398void ata_qc_prep(struct ata_queued_cmd *qc)
4399{
4400 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
4401 return;
4402
4403 ata_fill_sg(qc);
4404}
4405
Alan Coxd26fc952007-07-06 19:13:52 -04004406/**
4407 * ata_dumb_qc_prep - Prepare taskfile for submission
4408 * @qc: Metadata associated with taskfile to be prepared
4409 *
4410 * Prepare ATA taskfile for submission.
4411 *
4412 * LOCKING:
4413 * spin_lock_irqsave(host lock)
4414 */
4415void ata_dumb_qc_prep(struct ata_queued_cmd *qc)
4416{
4417 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
4418 return;
4419
4420 ata_fill_sg_dumb(qc);
4421}
4422
Brian Kinge46834c2006-03-17 17:04:03 -06004423void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4424
Jeff Garzik0cba6322005-05-30 19:49:12 -04004425/**
4426 * ata_sg_init_one - Associate command with memory buffer
4427 * @qc: Command to be associated
4428 * @buf: Memory buffer
4429 * @buflen: Length of memory buffer, in bytes.
4430 *
4431 * Initialize the data-related elements of queued_cmd @qc
4432 * to point to a single memory buffer, @buf of byte length @buflen.
4433 *
4434 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004435 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004436 */
4437
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
4439{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 qc->flags |= ATA_QCFLAG_SINGLE;
4441
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004442 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004444 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 qc->buf_virt = buf;
Brian King233277c2006-06-07 11:25:31 -05004446 qc->nbytes = buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Tejun Heo61c05962006-11-14 22:35:43 +09004448 sg_init_one(&qc->sgent, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449}
4450
Jeff Garzik0cba6322005-05-30 19:49:12 -04004451/**
4452 * ata_sg_init - Associate command with scatter-gather table.
4453 * @qc: Command to be associated
4454 * @sg: Scatter-gather table.
4455 * @n_elem: Number of elements in s/g table.
4456 *
4457 * Initialize the data-related elements of queued_cmd @qc
4458 * to point to a scatter-gather table @sg, containing @n_elem
4459 * elements.
4460 *
4461 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004462 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004463 */
4464
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4466 unsigned int n_elem)
4467{
4468 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004469 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004471 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472}
4473
4474/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004475 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
4476 * @qc: Command with memory buffer to be mapped.
4477 *
4478 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 *
4480 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004481 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 *
4483 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004484 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 */
4486
4487static int ata_sg_setup_one(struct ata_queued_cmd *qc)
4488{
4489 struct ata_port *ap = qc->ap;
4490 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004491 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09004493 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004495 /* we must lengthen transfers to end on a 32-bit boundary */
4496 qc->pad_len = sg->length & 3;
4497 if (qc->pad_len) {
4498 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4499 struct scatterlist *psg = &qc->pad_sgent;
4500
Tejun Heoa46314742006-02-11 19:11:13 +09004501 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004502
4503 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
4504
4505 if (qc->tf.flags & ATA_TFLAG_WRITE)
4506 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
4507 qc->pad_len);
4508
4509 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
4510 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
4511 /* trim sg */
4512 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09004513 if (sg->length == 0)
4514 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004515
4516 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
4517 sg->length, qc->pad_len);
4518 }
4519
Tejun Heo2e242fa2006-02-20 23:48:38 +09004520 if (trim_sg) {
4521 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05004522 goto skip_map;
4523 }
4524
Brian King2f1f6102006-03-23 17:30:15 -06004525 dma_address = dma_map_single(ap->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04004526 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05004527 if (dma_mapping_error(dma_address)) {
4528 /* restore sg */
4529 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05004531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
4533 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04004534 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
Tejun Heo2e242fa2006-02-20 23:48:38 +09004536skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
4538 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4539
4540 return 0;
4541}
4542
4543/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004544 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4545 * @qc: Command with scatter-gather table to be mapped.
4546 *
4547 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 *
4549 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004550 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 *
4552 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004553 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 *
4555 */
4556
4557static int ata_sg_setup(struct ata_queued_cmd *qc)
4558{
4559 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004560 struct scatterlist *sg = qc->__sg;
4561 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05004562 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563
Tejun Heo44877b42007-02-21 01:06:51 +09004564 VPRINTK("ENTER, ata%u\n", ap->print_id);
Tejun Heoa46314742006-02-11 19:11:13 +09004565 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004567 /* we must lengthen transfers to end on a 32-bit boundary */
4568 qc->pad_len = lsg->length & 3;
4569 if (qc->pad_len) {
4570 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4571 struct scatterlist *psg = &qc->pad_sgent;
4572 unsigned int offset;
4573
Tejun Heoa46314742006-02-11 19:11:13 +09004574 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004575
4576 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
4577
4578 /*
4579 * psg->page/offset are used to copy to-be-written
4580 * data in this function or read data in ata_sg_clean.
4581 */
4582 offset = lsg->offset + lsg->length - qc->pad_len;
4583 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
4584 psg->offset = offset_in_page(offset);
4585
4586 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4587 void *addr = kmap_atomic(psg->page, KM_IRQ0);
4588 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05004589 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004590 }
4591
4592 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
4593 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
4594 /* trim last sg */
4595 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05004596 if (lsg->length == 0)
4597 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004598
4599 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
4600 qc->n_elem - 1, lsg->length, qc->pad_len);
4601 }
4602
Jeff Garzike1410f22005-11-14 14:06:26 -05004603 pre_n_elem = qc->n_elem;
4604 if (trim_sg && pre_n_elem)
4605 pre_n_elem--;
4606
4607 if (!pre_n_elem) {
4608 n_elem = 0;
4609 goto skip_map;
4610 }
4611
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 dir = qc->dma_dir;
Brian King2f1f6102006-03-23 17:30:15 -06004613 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05004614 if (n_elem < 1) {
4615 /* restore last sg */
4616 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05004618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619
4620 DPRINTK("%d sg elements mapped\n", n_elem);
4621
Jeff Garzike1410f22005-11-14 14:06:26 -05004622skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 qc->n_elem = n_elem;
4624
4625 return 0;
4626}
4627
4628/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004629 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04004630 * @buf: Buffer to swap
4631 * @buf_words: Number of 16-bit words in buffer.
4632 *
4633 * Swap halves of 16-bit words if needed to convert from
4634 * little-endian byte order to native cpu byte order, or
4635 * vice-versa.
4636 *
4637 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004638 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004639 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640void swap_buf_le16(u16 *buf, unsigned int buf_words)
4641{
4642#ifdef __BIG_ENDIAN
4643 unsigned int i;
4644
4645 for (i = 0; i < buf_words; i++)
4646 buf[i] = le16_to_cpu(buf[i]);
4647#endif /* __BIG_ENDIAN */
4648}
4649
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004650/**
Tejun Heo0d5ff562007-02-01 15:06:36 +09004651 * ata_data_xfer - Transfer data by PIO
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004652 * @adev: device to target
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004653 * @buf: data buffer
4654 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04004655 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004656 *
4657 * Transfer data from/to the device data register by PIO.
4658 *
4659 * LOCKING:
4660 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004661 */
Tejun Heo0d5ff562007-02-01 15:06:36 +09004662void ata_data_xfer(struct ata_device *adev, unsigned char *buf,
4663 unsigned int buflen, int write_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004665 struct ata_port *ap = adev->link->ap;
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004666 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004668 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 if (write_data)
Tejun Heo0d5ff562007-02-01 15:06:36 +09004670 iowrite16_rep(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 else
Tejun Heo0d5ff562007-02-01 15:06:36 +09004672 ioread16_rep(ap->ioaddr.data_addr, buf, words);
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004673
4674 /* Transfer trailing 1 byte, if any. */
4675 if (unlikely(buflen & 0x01)) {
4676 u16 align_buf[1] = { 0 };
4677 unsigned char *trailing_buf = buf + buflen - 1;
4678
4679 if (write_data) {
4680 memcpy(align_buf, trailing_buf, 1);
Tejun Heo0d5ff562007-02-01 15:06:36 +09004681 iowrite16(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004682 } else {
Tejun Heo0d5ff562007-02-01 15:06:36 +09004683 align_buf[0] = cpu_to_le16(ioread16(ap->ioaddr.data_addr));
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004684 memcpy(trailing_buf, align_buf, 1);
4685 }
4686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687}
4688
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004689/**
Tejun Heo0d5ff562007-02-01 15:06:36 +09004690 * ata_data_xfer_noirq - Transfer data by PIO
Alan Cox75e99582006-05-24 14:14:41 +01004691 * @adev: device to target
4692 * @buf: data buffer
4693 * @buflen: buffer length
4694 * @write_data: read/write
4695 *
Tejun Heo88574552006-06-25 20:00:35 +09004696 * Transfer data from/to the device data register by PIO. Do the
Alan Cox75e99582006-05-24 14:14:41 +01004697 * transfer with interrupts disabled.
4698 *
4699 * LOCKING:
4700 * Inherited from caller.
4701 */
Tejun Heo0d5ff562007-02-01 15:06:36 +09004702void ata_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
4703 unsigned int buflen, int write_data)
Alan Cox75e99582006-05-24 14:14:41 +01004704{
4705 unsigned long flags;
4706 local_irq_save(flags);
Tejun Heo0d5ff562007-02-01 15:06:36 +09004707 ata_data_xfer(adev, buf, buflen, write_data);
Alan Cox75e99582006-05-24 14:14:41 +01004708 local_irq_restore(flags);
4709}
4710
4711
4712/**
Mark Lord5a5dbd12007-03-16 10:22:26 -04004713 * ata_pio_sector - Transfer a sector of data.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004714 * @qc: Command on going
4715 *
Mark Lord5a5dbd12007-03-16 10:22:26 -04004716 * Transfer qc->sect_size bytes of data from/to the ATA device.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004717 *
4718 * LOCKING:
4719 * Inherited from caller.
4720 */
4721
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722static void ata_pio_sector(struct ata_queued_cmd *qc)
4723{
4724 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004725 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 struct ata_port *ap = qc->ap;
4727 struct page *page;
4728 unsigned int offset;
4729 unsigned char *buf;
4730
Mark Lord5a5dbd12007-03-16 10:22:26 -04004731 if (qc->curbytes == qc->nbytes - qc->sect_size)
Albert Lee14be71f2005-09-27 17:36:35 +08004732 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
4734 page = sg[qc->cursg].page;
Tejun Heo726f0782007-01-03 17:30:39 +09004735 offset = sg[qc->cursg].offset + qc->cursg_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
4737 /* get the current page and offset */
4738 page = nth_page(page, (offset >> PAGE_SHIFT));
4739 offset %= PAGE_SIZE;
4740
Albert Lee7282aa42005-10-09 09:46:07 -04004741 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4742
Albert Lee91b8b312005-10-09 09:48:44 -04004743 if (PageHighMem(page)) {
4744 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04004745
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004746 /* FIXME: use a bounce buffer */
Albert Lee91b8b312005-10-09 09:48:44 -04004747 local_irq_save(flags);
4748 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04004749
Albert Lee91b8b312005-10-09 09:48:44 -04004750 /* do the actual data transfer */
Mark Lord5a5dbd12007-03-16 10:22:26 -04004751 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004752
4753 kunmap_atomic(buf, KM_IRQ0);
4754 local_irq_restore(flags);
4755 } else {
4756 buf = page_address(page);
Mark Lord5a5dbd12007-03-16 10:22:26 -04004757 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759
Mark Lord5a5dbd12007-03-16 10:22:26 -04004760 qc->curbytes += qc->sect_size;
4761 qc->cursg_ofs += qc->sect_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
Tejun Heo726f0782007-01-03 17:30:39 +09004763 if (qc->cursg_ofs == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 qc->cursg++;
4765 qc->cursg_ofs = 0;
4766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004769/**
Mark Lord5a5dbd12007-03-16 10:22:26 -04004770 * ata_pio_sectors - Transfer one or many sectors.
Albert Lee07f6f7d2005-11-01 19:33:20 +08004771 * @qc: Command on going
4772 *
Mark Lord5a5dbd12007-03-16 10:22:26 -04004773 * Transfer one or many sectors of data from/to the
Albert Lee07f6f7d2005-11-01 19:33:20 +08004774 * ATA device for the DRQ request.
4775 *
4776 * LOCKING:
4777 * Inherited from caller.
4778 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779
Albert Lee07f6f7d2005-11-01 19:33:20 +08004780static void ata_pio_sectors(struct ata_queued_cmd *qc)
4781{
4782 if (is_multi_taskfile(&qc->tf)) {
4783 /* READ/WRITE MULTIPLE */
4784 unsigned int nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
Jeff Garzik587005d2006-02-11 18:17:32 -05004786 WARN_ON(qc->dev->multi_count == 0);
Albert Lee07f6f7d2005-11-01 19:33:20 +08004787
Mark Lord5a5dbd12007-03-16 10:22:26 -04004788 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
Tejun Heo726f0782007-01-03 17:30:39 +09004789 qc->dev->multi_count);
Albert Lee07f6f7d2005-11-01 19:33:20 +08004790 while (nsect--)
4791 ata_pio_sector(qc);
4792 } else
4793 ata_pio_sector(qc);
Albert Lee4cc980b2007-08-15 03:19:45 -04004794
4795 ata_altstatus(qc->ap); /* flush */
Albert Lee07f6f7d2005-11-01 19:33:20 +08004796}
4797
4798/**
Albert Leec71c1852005-10-04 06:03:45 -04004799 * atapi_send_cdb - Write CDB bytes to hardware
4800 * @ap: Port to which ATAPI device is attached.
4801 * @qc: Taskfile currently active
4802 *
4803 * When device has indicated its readiness to accept
4804 * a CDB, this function is called. Send the CDB.
4805 *
4806 * LOCKING:
4807 * caller.
4808 */
4809
4810static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
4811{
4812 /* send SCSI cdb */
4813 DPRINTK("send cdb\n");
Jeff Garzikdb024d52006-02-13 00:23:57 -05004814 WARN_ON(qc->dev->cdb_len < 12);
Albert Leec71c1852005-10-04 06:03:45 -04004815
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004816 ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
Albert Leec71c1852005-10-04 06:03:45 -04004817 ata_altstatus(ap); /* flush */
4818
4819 switch (qc->tf.protocol) {
4820 case ATA_PROT_ATAPI:
4821 ap->hsm_task_state = HSM_ST;
4822 break;
4823 case ATA_PROT_ATAPI_NODATA:
4824 ap->hsm_task_state = HSM_ST_LAST;
4825 break;
4826 case ATA_PROT_ATAPI_DMA:
4827 ap->hsm_task_state = HSM_ST_LAST;
4828 /* initiate bmdma */
4829 ap->ops->bmdma_start(qc);
4830 break;
4831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832}
4833
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004834/**
4835 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
4836 * @qc: Command on going
4837 * @bytes: number of bytes
4838 *
4839 * Transfer Transfer data from/to the ATAPI device.
4840 *
4841 * LOCKING:
4842 * Inherited from caller.
4843 *
4844 */
4845
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
4847{
4848 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004849 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 struct ata_port *ap = qc->ap;
4851 struct page *page;
4852 unsigned char *buf;
4853 unsigned int offset, count;
4854
Albert Lee563a6e12005-08-12 14:17:50 +08004855 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08004856 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857
4858next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08004859 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04004860 /*
Albert Lee563a6e12005-08-12 14:17:50 +08004861 * The end of qc->sg is reached and the device expects
4862 * more data to transfer. In order not to overrun qc->sg
4863 * and fulfill length specified in the byte count register,
4864 * - for read case, discard trailing data from the device
4865 * - for write case, padding zero data to the device
4866 */
4867 u16 pad_buf[1] = { 0 };
4868 unsigned int words = bytes >> 1;
4869 unsigned int i;
4870
4871 if (words) /* warning if bytes > 1 */
Tejun Heof15a1da2006-05-15 20:57:56 +09004872 ata_dev_printk(qc->dev, KERN_WARNING,
4873 "%u bytes trailing data\n", bytes);
Albert Lee563a6e12005-08-12 14:17:50 +08004874
4875 for (i = 0; i < words; i++)
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004876 ap->ops->data_xfer(qc->dev, (unsigned char*)pad_buf, 2, do_write);
Albert Lee563a6e12005-08-12 14:17:50 +08004877
Albert Lee14be71f2005-09-27 17:36:35 +08004878 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08004879 return;
4880 }
4881
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004882 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 page = sg->page;
4885 offset = sg->offset + qc->cursg_ofs;
4886
4887 /* get the current page and offset */
4888 page = nth_page(page, (offset >> PAGE_SHIFT));
4889 offset %= PAGE_SIZE;
4890
Albert Lee6952df02005-06-06 15:56:03 +08004891 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04004892 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
4894 /* don't cross page boundaries */
4895 count = min(count, (unsigned int)PAGE_SIZE - offset);
4896
Albert Lee7282aa42005-10-09 09:46:07 -04004897 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4898
Albert Lee91b8b312005-10-09 09:48:44 -04004899 if (PageHighMem(page)) {
4900 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04004901
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004902 /* FIXME: use bounce buffer */
Albert Lee91b8b312005-10-09 09:48:44 -04004903 local_irq_save(flags);
4904 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04004905
Albert Lee91b8b312005-10-09 09:48:44 -04004906 /* do the actual data transfer */
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004907 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004908
4909 kunmap_atomic(buf, KM_IRQ0);
4910 local_irq_restore(flags);
4911 } else {
4912 buf = page_address(page);
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004913 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
4916 bytes -= count;
4917 qc->curbytes += count;
4918 qc->cursg_ofs += count;
4919
Albert Lee32529e02005-05-26 03:49:42 -04004920 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 qc->cursg++;
4922 qc->cursg_ofs = 0;
4923 }
4924
Albert Lee563a6e12005-08-12 14:17:50 +08004925 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927}
4928
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004929/**
4930 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
4931 * @qc: Command on going
4932 *
4933 * Transfer Transfer data from/to the ATAPI device.
4934 *
4935 * LOCKING:
4936 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004937 */
4938
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939static void atapi_pio_bytes(struct ata_queued_cmd *qc)
4940{
4941 struct ata_port *ap = qc->ap;
4942 struct ata_device *dev = qc->dev;
4943 unsigned int ireason, bc_lo, bc_hi, bytes;
4944 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
4945
Albert Leeeec4c3f2006-05-18 17:51:10 +08004946 /* Abuse qc->result_tf for temp storage of intermediate TF
4947 * here to save some kernel stack usage.
4948 * For normal completion, qc->result_tf is not relevant. For
4949 * error, qc->result_tf is later overwritten by ata_qc_complete().
4950 * So, the correctness of qc->result_tf is not affected.
4951 */
4952 ap->ops->tf_read(ap, &qc->result_tf);
4953 ireason = qc->result_tf.nsect;
4954 bc_lo = qc->result_tf.lbam;
4955 bc_hi = qc->result_tf.lbah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 bytes = (bc_hi << 8) | bc_lo;
4957
4958 /* shall be cleared to zero, indicating xfer of data */
4959 if (ireason & (1 << 0))
4960 goto err_out;
4961
4962 /* make sure transfer direction matches expected */
4963 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
4964 if (do_write != i_write)
4965 goto err_out;
4966
Tejun Heo44877b42007-02-21 01:06:51 +09004967 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
Albert Lee312f7da2005-09-27 17:38:03 +08004968
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 __atapi_pio_bytes(qc, bytes);
Albert Lee4cc980b2007-08-15 03:19:45 -04004970 ata_altstatus(ap); /* flush */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
4972 return;
4973
4974err_out:
Tejun Heof15a1da2006-05-15 20:57:56 +09004975 ata_dev_printk(dev, KERN_INFO, "ATAPI check failed\n");
Tejun Heo11a56d22006-01-23 13:09:36 +09004976 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08004977 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978}
4979
4980/**
Albert Leec234fb02006-03-25 17:58:38 +08004981 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
4982 * @ap: the target ata_port
4983 * @qc: qc on going
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 *
Albert Leec234fb02006-03-25 17:58:38 +08004985 * RETURNS:
4986 * 1 if ok in workqueue, 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 */
Albert Leec234fb02006-03-25 17:58:38 +08004988
4989static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990{
Albert Leec234fb02006-03-25 17:58:38 +08004991 if (qc->tf.flags & ATA_TFLAG_POLLING)
4992 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993
Albert Leec234fb02006-03-25 17:58:38 +08004994 if (ap->hsm_task_state == HSM_ST_FIRST) {
4995 if (qc->tf.protocol == ATA_PROT_PIO &&
4996 (qc->tf.flags & ATA_TFLAG_WRITE))
4997 return 1;
4998
4999 if (is_atapi_taskfile(&qc->tf) &&
5000 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
5001 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 }
5003
Albert Leec234fb02006-03-25 17:58:38 +08005004 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005}
5006
Albert Leec234fb02006-03-25 17:58:38 +08005007/**
Tejun Heoc17ea202006-05-15 20:59:29 +09005008 * ata_hsm_qc_complete - finish a qc running on standard HSM
5009 * @qc: Command to complete
5010 * @in_wq: 1 if called from workqueue, 0 otherwise
5011 *
5012 * Finish @qc which is running on standard HSM.
5013 *
5014 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005015 * If @in_wq is zero, spin_lock_irqsave(host lock).
Tejun Heoc17ea202006-05-15 20:59:29 +09005016 * Otherwise, none on entry and grabs host lock.
5017 */
5018static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
5019{
5020 struct ata_port *ap = qc->ap;
5021 unsigned long flags;
5022
5023 if (ap->ops->error_handler) {
5024 if (in_wq) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04005025 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09005026
Jeff Garzikcca39742006-08-24 03:19:22 -04005027 /* EH might have kicked in while host lock is
5028 * released.
Tejun Heoc17ea202006-05-15 20:59:29 +09005029 */
5030 qc = ata_qc_from_tag(ap, qc->tag);
5031 if (qc) {
5032 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
Akira Iguchi83625002007-01-26 16:27:32 +09005033 ap->ops->irq_on(ap);
Tejun Heoc17ea202006-05-15 20:59:29 +09005034 ata_qc_complete(qc);
5035 } else
5036 ata_port_freeze(ap);
5037 }
5038
Jeff Garzikba6a1302006-06-22 23:46:10 -04005039 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09005040 } else {
5041 if (likely(!(qc->err_mask & AC_ERR_HSM)))
5042 ata_qc_complete(qc);
5043 else
5044 ata_port_freeze(ap);
5045 }
5046 } else {
5047 if (in_wq) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04005048 spin_lock_irqsave(ap->lock, flags);
Akira Iguchi83625002007-01-26 16:27:32 +09005049 ap->ops->irq_on(ap);
Tejun Heoc17ea202006-05-15 20:59:29 +09005050 ata_qc_complete(qc);
Jeff Garzikba6a1302006-06-22 23:46:10 -04005051 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09005052 } else
5053 ata_qc_complete(qc);
5054 }
5055}
5056
5057/**
Albert Leebb5cb292006-03-25 17:48:02 +08005058 * ata_hsm_move - move the HSM to the next state.
5059 * @ap: the target ata_port
5060 * @qc: qc on going
5061 * @status: current device status
5062 * @in_wq: 1 if called from workqueue, 0 otherwise
5063 *
5064 * RETURNS:
5065 * 1 when poll next status needed, 0 otherwise.
5066 */
Tejun Heo9a1004d2006-05-31 18:27:52 +09005067int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
5068 u8 status, int in_wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069{
Albert Leebb5cb292006-03-25 17:48:02 +08005070 unsigned long flags = 0;
5071 int poll_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072
Albert Lee6912ccd2006-03-25 17:45:49 +08005073 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
Albert Lee0565c262006-02-13 18:55:25 +08005074
Albert Leebb5cb292006-03-25 17:48:02 +08005075 /* Make sure ata_qc_issue_prot() does not throw things
5076 * like DMA polling into the workqueue. Notice that
5077 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
Albert Lee1c848982005-12-05 15:40:15 +08005078 */
Albert Leec234fb02006-03-25 17:58:38 +08005079 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
Albert Lee1c848982005-12-05 15:40:15 +08005080
Albert Leee2cec772006-03-25 17:43:49 +08005081fsm_start:
Albert Lee999bb6f2006-03-25 18:07:48 +08005082 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
Tejun Heo44877b42007-02-21 01:06:51 +09005083 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084
Albert Leee2cec772006-03-25 17:43:49 +08005085 switch (ap->hsm_task_state) {
5086 case HSM_ST_FIRST:
Albert Leebb5cb292006-03-25 17:48:02 +08005087 /* Send first data block or PACKET CDB */
5088
5089 /* If polling, we will stay in the work queue after
5090 * sending the data. Otherwise, interrupt handler
5091 * takes over after sending the data.
5092 */
5093 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
5094
Albert Leee2cec772006-03-25 17:43:49 +08005095 /* check device status */
Albert Lee3655d1d2006-05-19 11:43:04 +08005096 if (unlikely((status & ATA_DRQ) == 0)) {
5097 /* handle BSY=0, DRQ=0 as error */
5098 if (likely(status & (ATA_ERR | ATA_DF)))
5099 /* device stops HSM for abort/error */
5100 qc->err_mask |= AC_ERR_DEV;
5101 else
5102 /* HSM violation. Let EH handle this */
5103 qc->err_mask |= AC_ERR_HSM;
5104
Albert Leee2cec772006-03-25 17:43:49 +08005105 ap->hsm_task_state = HSM_ST_ERR;
5106 goto fsm_start;
5107 }
5108
Albert Lee71601952006-03-25 18:11:12 +08005109 /* Device should not ask for data transfer (DRQ=1)
5110 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08005111 * We ignore DRQ here and stop the HSM by
5112 * changing hsm_task_state to HSM_ST_ERR and
5113 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08005114 */
5115 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Tejun Heo44877b42007-02-21 01:06:51 +09005116 ata_port_printk(ap, KERN_WARNING, "DRQ=1 with device "
5117 "error, dev_stat 0x%X\n", status);
Albert Lee3655d1d2006-05-19 11:43:04 +08005118 qc->err_mask |= AC_ERR_HSM;
Albert Leeeee6c322006-04-01 17:38:43 +08005119 ap->hsm_task_state = HSM_ST_ERR;
5120 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08005121 }
5122
Albert Leebb5cb292006-03-25 17:48:02 +08005123 /* Send the CDB (atapi) or the first data block (ata pio out).
5124 * During the state transition, interrupt handler shouldn't
5125 * be invoked before the data transfer is complete and
5126 * hsm_task_state is changed. Hence, the following locking.
5127 */
5128 if (in_wq)
Jeff Garzikba6a1302006-06-22 23:46:10 -04005129 spin_lock_irqsave(ap->lock, flags);
Albert Leee2cec772006-03-25 17:43:49 +08005130
Albert Leebb5cb292006-03-25 17:48:02 +08005131 if (qc->tf.protocol == ATA_PROT_PIO) {
5132 /* PIO data out protocol.
5133 * send first data block.
5134 */
5135
5136 /* ata_pio_sectors() might change the state
5137 * to HSM_ST_LAST. so, the state is changed here
5138 * before ata_pio_sectors().
5139 */
5140 ap->hsm_task_state = HSM_ST;
5141 ata_pio_sectors(qc);
Albert Leebb5cb292006-03-25 17:48:02 +08005142 } else
5143 /* send CDB */
5144 atapi_send_cdb(ap, qc);
5145
5146 if (in_wq)
Jeff Garzikba6a1302006-06-22 23:46:10 -04005147 spin_unlock_irqrestore(ap->lock, flags);
Albert Leebb5cb292006-03-25 17:48:02 +08005148
5149 /* if polling, ata_pio_task() handles the rest.
5150 * otherwise, interrupt handler takes over from here.
5151 */
Albert Leee2cec772006-03-25 17:43:49 +08005152 break;
5153
5154 case HSM_ST:
5155 /* complete command or read/write the data register */
5156 if (qc->tf.protocol == ATA_PROT_ATAPI) {
5157 /* ATAPI PIO protocol */
5158 if ((status & ATA_DRQ) == 0) {
Albert Lee3655d1d2006-05-19 11:43:04 +08005159 /* No more data to transfer or device error.
5160 * Device error will be tagged in HSM_ST_LAST.
5161 */
Albert Leee2cec772006-03-25 17:43:49 +08005162 ap->hsm_task_state = HSM_ST_LAST;
5163 goto fsm_start;
5164 }
5165
Albert Lee71601952006-03-25 18:11:12 +08005166 /* Device should not ask for data transfer (DRQ=1)
5167 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08005168 * We ignore DRQ here and stop the HSM by
5169 * changing hsm_task_state to HSM_ST_ERR and
5170 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08005171 */
5172 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Tejun Heo44877b42007-02-21 01:06:51 +09005173 ata_port_printk(ap, KERN_WARNING, "DRQ=1 with "
5174 "device error, dev_stat 0x%X\n",
5175 status);
Albert Lee3655d1d2006-05-19 11:43:04 +08005176 qc->err_mask |= AC_ERR_HSM;
Albert Leeeee6c322006-04-01 17:38:43 +08005177 ap->hsm_task_state = HSM_ST_ERR;
5178 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08005179 }
5180
Albert Leee2cec772006-03-25 17:43:49 +08005181 atapi_pio_bytes(qc);
5182
5183 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
5184 /* bad ireason reported by device */
5185 goto fsm_start;
5186
5187 } else {
5188 /* ATA PIO protocol */
5189 if (unlikely((status & ATA_DRQ) == 0)) {
5190 /* handle BSY=0, DRQ=0 as error */
Albert Lee3655d1d2006-05-19 11:43:04 +08005191 if (likely(status & (ATA_ERR | ATA_DF)))
5192 /* device stops HSM for abort/error */
5193 qc->err_mask |= AC_ERR_DEV;
5194 else
Tejun Heo55a8e2c2006-11-10 18:08:10 +09005195 /* HSM violation. Let EH handle this.
5196 * Phantom devices also trigger this
5197 * condition. Mark hint.
5198 */
5199 qc->err_mask |= AC_ERR_HSM |
5200 AC_ERR_NODEV_HINT;
Albert Lee3655d1d2006-05-19 11:43:04 +08005201
Albert Leee2cec772006-03-25 17:43:49 +08005202 ap->hsm_task_state = HSM_ST_ERR;
5203 goto fsm_start;
5204 }
5205
Albert Leeeee6c322006-04-01 17:38:43 +08005206 /* For PIO reads, some devices may ask for
5207 * data transfer (DRQ=1) alone with ERR=1.
5208 * We respect DRQ here and transfer one
5209 * block of junk data before changing the
5210 * hsm_task_state to HSM_ST_ERR.
5211 *
5212 * For PIO writes, ERR=1 DRQ=1 doesn't make
5213 * sense since the data block has been
5214 * transferred to the device.
Albert Lee71601952006-03-25 18:11:12 +08005215 */
5216 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Albert Lee71601952006-03-25 18:11:12 +08005217 /* data might be corrputed */
5218 qc->err_mask |= AC_ERR_DEV;
Albert Leeeee6c322006-04-01 17:38:43 +08005219
5220 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
5221 ata_pio_sectors(qc);
Albert Leeeee6c322006-04-01 17:38:43 +08005222 status = ata_wait_idle(ap);
5223 }
5224
Albert Lee3655d1d2006-05-19 11:43:04 +08005225 if (status & (ATA_BUSY | ATA_DRQ))
5226 qc->err_mask |= AC_ERR_HSM;
5227
Albert Leeeee6c322006-04-01 17:38:43 +08005228 /* ata_pio_sectors() might change the
5229 * state to HSM_ST_LAST. so, the state
5230 * is changed after ata_pio_sectors().
5231 */
5232 ap->hsm_task_state = HSM_ST_ERR;
5233 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08005234 }
5235
Albert Leee2cec772006-03-25 17:43:49 +08005236 ata_pio_sectors(qc);
5237
5238 if (ap->hsm_task_state == HSM_ST_LAST &&
5239 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
5240 /* all data read */
Albert Lee52a32202006-03-25 18:18:15 +08005241 status = ata_wait_idle(ap);
Albert Leee2cec772006-03-25 17:43:49 +08005242 goto fsm_start;
5243 }
5244 }
5245
Albert Leebb5cb292006-03-25 17:48:02 +08005246 poll_next = 1;
Albert Leee2cec772006-03-25 17:43:49 +08005247 break;
5248
5249 case HSM_ST_LAST:
Albert Lee6912ccd2006-03-25 17:45:49 +08005250 if (unlikely(!ata_ok(status))) {
5251 qc->err_mask |= __ac_err_mask(status);
Albert Leee2cec772006-03-25 17:43:49 +08005252 ap->hsm_task_state = HSM_ST_ERR;
5253 goto fsm_start;
5254 }
5255
5256 /* no more data to transfer */
Albert Lee4332a772006-04-03 17:43:24 +08005257 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
Tejun Heo44877b42007-02-21 01:06:51 +09005258 ap->print_id, qc->dev->devno, status);
Albert Leee2cec772006-03-25 17:43:49 +08005259
Albert Lee6912ccd2006-03-25 17:45:49 +08005260 WARN_ON(qc->err_mask);
5261
Albert Leee2cec772006-03-25 17:43:49 +08005262 ap->hsm_task_state = HSM_ST_IDLE;
5263
5264 /* complete taskfile transaction */
Tejun Heoc17ea202006-05-15 20:59:29 +09005265 ata_hsm_qc_complete(qc, in_wq);
Albert Leebb5cb292006-03-25 17:48:02 +08005266
5267 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08005268 break;
5269
5270 case HSM_ST_ERR:
Albert Leee2cec772006-03-25 17:43:49 +08005271 /* make sure qc->err_mask is available to
5272 * know what's wrong and recover
5273 */
5274 WARN_ON(qc->err_mask == 0);
5275
5276 ap->hsm_task_state = HSM_ST_IDLE;
Albert Leebb5cb292006-03-25 17:48:02 +08005277
Albert Lee999bb6f2006-03-25 18:07:48 +08005278 /* complete taskfile transaction */
Tejun Heoc17ea202006-05-15 20:59:29 +09005279 ata_hsm_qc_complete(qc, in_wq);
Albert Leebb5cb292006-03-25 17:48:02 +08005280
5281 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08005282 break;
5283 default:
Albert Leebb5cb292006-03-25 17:48:02 +08005284 poll_next = 0;
Albert Lee6912ccd2006-03-25 17:45:49 +08005285 BUG();
Albert Leee2cec772006-03-25 17:43:49 +08005286 }
5287
Albert Leebb5cb292006-03-25 17:48:02 +08005288 return poll_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289}
5290
David Howells65f27f32006-11-22 14:55:48 +00005291static void ata_pio_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292{
David Howells65f27f32006-11-22 14:55:48 +00005293 struct ata_port *ap =
5294 container_of(work, struct ata_port, port_task.work);
5295 struct ata_queued_cmd *qc = ap->port_task_data;
Albert Leea1af3732006-03-25 17:50:15 +08005296 u8 status;
5297 int poll_next;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04005298
5299fsm_start:
Albert Leea1af3732006-03-25 17:50:15 +08005300 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301
Albert Leea1af3732006-03-25 17:50:15 +08005302 /*
5303 * This is purely heuristic. This is a fast path.
5304 * Sometimes when we enter, BSY will be cleared in
5305 * a chk-status or two. If not, the drive is probably seeking
5306 * or something. Snooze for a couple msecs, then
5307 * chk-status again. If still busy, queue delayed work.
5308 */
5309 status = ata_busy_wait(ap, ATA_BUSY, 5);
5310 if (status & ATA_BUSY) {
5311 msleep(2);
5312 status = ata_busy_wait(ap, ATA_BUSY, 10);
5313 if (status & ATA_BUSY) {
Albert Lee31ce6da2006-04-03 18:31:44 +08005314 ata_port_queue_task(ap, ata_pio_task, qc, ATA_SHORT_PAUSE);
Albert Leea1af3732006-03-25 17:50:15 +08005315 return;
5316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 }
5318
Albert Leea1af3732006-03-25 17:50:15 +08005319 /* move the HSM */
5320 poll_next = ata_hsm_move(ap, qc, status, 1);
5321
5322 /* another command or interrupt handler
5323 * may be running at this point.
5324 */
5325 if (poll_next)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04005326 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327}
5328
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 * ata_qc_new - Request an available ATA command, for queueing
5331 * @ap: Port associated with device @dev
5332 * @dev: Device from whom we request an available command structure
5333 *
5334 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005335 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 */
5337
5338static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
5339{
5340 struct ata_queued_cmd *qc = NULL;
5341 unsigned int i;
5342
Tejun Heoe3180492006-05-15 20:58:09 +09005343 /* no command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09005344 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
Tejun Heoe3180492006-05-15 20:58:09 +09005345 return NULL;
5346
Tejun Heo2ab7db12006-05-15 20:58:02 +09005347 /* the last tag is reserved for internal command. */
5348 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
Tejun Heo6cec4a32006-05-15 21:03:41 +09005349 if (!test_and_set_bit(i, &ap->qc_allocated)) {
Tejun Heof69499f2006-05-15 20:58:03 +09005350 qc = __ata_qc_from_tag(ap, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 break;
5352 }
5353
5354 if (qc)
5355 qc->tag = i;
5356
5357 return qc;
5358}
5359
5360/**
5361 * ata_qc_new_init - Request an available ATA command, and initialize it
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 * @dev: Device from whom we request an available command structure
5363 *
5364 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005365 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 */
5367
Tejun Heo3373efd2006-05-15 20:57:53 +09005368struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005370 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 struct ata_queued_cmd *qc;
5372
5373 qc = ata_qc_new(ap);
5374 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 qc->scsicmd = NULL;
5376 qc->ap = ap;
5377 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05005379 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 }
5381
5382 return qc;
5383}
5384
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385/**
5386 * ata_qc_free - free unused ata_queued_cmd
5387 * @qc: Command to complete
5388 *
5389 * Designed to free unused ata_queued_cmd object
5390 * in case something prevents using it.
5391 *
5392 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005393 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 */
5395void ata_qc_free(struct ata_queued_cmd *qc)
5396{
Tejun Heo4ba946e2006-01-23 13:09:36 +09005397 struct ata_port *ap = qc->ap;
5398 unsigned int tag;
5399
Tejun Heoa46314742006-02-11 19:11:13 +09005400 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401
Tejun Heo4ba946e2006-01-23 13:09:36 +09005402 qc->flags = 0;
5403 tag = qc->tag;
5404 if (likely(ata_tag_valid(tag))) {
Tejun Heo4ba946e2006-01-23 13:09:36 +09005405 qc->tag = ATA_TAG_POISON;
Tejun Heo6cec4a32006-05-15 21:03:41 +09005406 clear_bit(tag, &ap->qc_allocated);
Tejun Heo4ba946e2006-01-23 13:09:36 +09005407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408}
5409
Tejun Heo76014422006-02-11 15:13:49 +09005410void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411{
Tejun Heodedaf2b2006-05-15 21:03:43 +09005412 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005413 struct ata_link *link = qc->dev->link;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005414
Tejun Heoa46314742006-02-11 19:11:13 +09005415 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5416 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417
5418 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
5419 ata_sg_clean(qc);
5420
Tejun Heo7401abf2006-05-15 20:57:32 +09005421 /* command should be marked inactive atomically with qc completion */
Tejun Heoda917d62007-09-23 13:14:12 +09005422 if (qc->tf.protocol == ATA_PROT_NCQ) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005423 link->sactive &= ~(1 << qc->tag);
Tejun Heoda917d62007-09-23 13:14:12 +09005424 if (!link->sactive)
5425 ap->nr_active_links--;
5426 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005427 link->active_tag = ATA_TAG_POISON;
Tejun Heoda917d62007-09-23 13:14:12 +09005428 ap->nr_active_links--;
5429 }
5430
5431 /* clear exclusive status */
5432 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
5433 ap->excl_link == link))
5434 ap->excl_link = NULL;
Tejun Heo7401abf2006-05-15 20:57:32 +09005435
Albert Lee3f3791d2005-08-16 14:25:38 +08005436 /* atapi: mark qc as inactive to prevent the interrupt handler
5437 * from completing the command twice later, before the error handler
5438 * is called. (when rc != 0 and atapi request sense is needed)
5439 */
5440 qc->flags &= ~ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005441 ap->qc_active &= ~(1 << qc->tag);
Albert Lee3f3791d2005-08-16 14:25:38 +08005442
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09005444 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445}
5446
Tejun Heo39599a52006-11-14 22:37:35 +09005447static void fill_result_tf(struct ata_queued_cmd *qc)
5448{
5449 struct ata_port *ap = qc->ap;
5450
Tejun Heo39599a52006-11-14 22:37:35 +09005451 qc->result_tf.flags = qc->tf.flags;
Mark Lord4742d542007-04-02 16:20:35 -04005452 ap->ops->tf_read(ap, &qc->result_tf);
Tejun Heo39599a52006-11-14 22:37:35 +09005453}
5454
Tejun Heof686bcb2006-05-15 20:58:05 +09005455/**
5456 * ata_qc_complete - Complete an active ATA command
5457 * @qc: Command to complete
5458 * @err_mask: ATA Status register contents
5459 *
5460 * Indicate to the mid and upper layers that an ATA
5461 * command has completed, with either an ok or not-ok status.
5462 *
5463 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005464 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +09005465 */
5466void ata_qc_complete(struct ata_queued_cmd *qc)
5467{
5468 struct ata_port *ap = qc->ap;
5469
5470 /* XXX: New EH and old EH use different mechanisms to
5471 * synchronize EH with regular execution path.
5472 *
5473 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5474 * Normal execution path is responsible for not accessing a
5475 * failed qc. libata core enforces the rule by returning NULL
5476 * from ata_qc_from_tag() for failed qcs.
5477 *
5478 * Old EH depends on ata_qc_complete() nullifying completion
5479 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
5480 * not synchronize with interrupt handler. Only PIO task is
5481 * taken care of.
5482 */
5483 if (ap->ops->error_handler) {
Tejun Heob51e9e52006-06-29 01:29:30 +09005484 WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
Tejun Heof686bcb2006-05-15 20:58:05 +09005485
5486 if (unlikely(qc->err_mask))
5487 qc->flags |= ATA_QCFLAG_FAILED;
5488
5489 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
5490 if (!ata_tag_internal(qc->tag)) {
5491 /* always fill result TF for failed qc */
Tejun Heo39599a52006-11-14 22:37:35 +09005492 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005493 ata_qc_schedule_eh(qc);
5494 return;
5495 }
5496 }
5497
5498 /* read result TF if requested */
5499 if (qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005500 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005501
5502 __ata_qc_complete(qc);
5503 } else {
5504 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5505 return;
5506
5507 /* read result TF if failed or requested */
5508 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005509 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005510
5511 __ata_qc_complete(qc);
5512 }
5513}
5514
Tejun Heodedaf2b2006-05-15 21:03:43 +09005515/**
5516 * ata_qc_complete_multiple - Complete multiple qcs successfully
5517 * @ap: port in question
5518 * @qc_active: new qc_active mask
5519 * @finish_qc: LLDD callback invoked before completing a qc
5520 *
5521 * Complete in-flight commands. This functions is meant to be
5522 * called from low-level driver's interrupt routine to complete
5523 * requests normally. ap->qc_active and @qc_active is compared
5524 * and commands are completed accordingly.
5525 *
5526 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005527 * spin_lock_irqsave(host lock)
Tejun Heodedaf2b2006-05-15 21:03:43 +09005528 *
5529 * RETURNS:
5530 * Number of completed commands on success, -errno otherwise.
5531 */
5532int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
5533 void (*finish_qc)(struct ata_queued_cmd *))
5534{
5535 int nr_done = 0;
5536 u32 done_mask;
5537 int i;
5538
5539 done_mask = ap->qc_active ^ qc_active;
5540
5541 if (unlikely(done_mask & qc_active)) {
5542 ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
5543 "(%08x->%08x)\n", ap->qc_active, qc_active);
5544 return -EINVAL;
5545 }
5546
5547 for (i = 0; i < ATA_MAX_QUEUE; i++) {
5548 struct ata_queued_cmd *qc;
5549
5550 if (!(done_mask & (1 << i)))
5551 continue;
5552
5553 if ((qc = ata_qc_from_tag(ap, i))) {
5554 if (finish_qc)
5555 finish_qc(qc);
5556 ata_qc_complete(qc);
5557 nr_done++;
5558 }
5559 }
5560
5561 return nr_done;
5562}
5563
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
5565{
5566 struct ata_port *ap = qc->ap;
5567
5568 switch (qc->tf.protocol) {
Tejun Heo3dc1d882006-05-15 21:03:45 +09005569 case ATA_PROT_NCQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 case ATA_PROT_DMA:
5571 case ATA_PROT_ATAPI_DMA:
5572 return 1;
5573
5574 case ATA_PROT_ATAPI:
5575 case ATA_PROT_PIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 if (ap->flags & ATA_FLAG_PIO_DMA)
5577 return 1;
5578
5579 /* fall through */
5580
5581 default:
5582 return 0;
5583 }
5584
5585 /* never reached */
5586}
5587
5588/**
5589 * ata_qc_issue - issue taskfile to device
5590 * @qc: command to issue to device
5591 *
5592 * Prepare an ATA command to submission to device.
5593 * This includes mapping the data into a DMA-able
5594 * area, filling in the S/G table, and finally
5595 * writing the taskfile to hardware, starting the command.
5596 *
5597 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005598 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09005600void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601{
5602 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005603 struct ata_link *link = qc->dev->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604
Tejun Heodedaf2b2006-05-15 21:03:43 +09005605 /* Make sure only one non-NCQ command is outstanding. The
5606 * check is skipped for old EH because it reuses active qc to
5607 * request ATAPI sense.
5608 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005609 WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
Tejun Heodedaf2b2006-05-15 21:03:43 +09005610
5611 if (qc->tf.protocol == ATA_PROT_NCQ) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005612 WARN_ON(link->sactive & (1 << qc->tag));
Tejun Heoda917d62007-09-23 13:14:12 +09005613
5614 if (!link->sactive)
5615 ap->nr_active_links++;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005616 link->sactive |= 1 << qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005617 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005618 WARN_ON(link->sactive);
Tejun Heoda917d62007-09-23 13:14:12 +09005619
5620 ap->nr_active_links++;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005621 link->active_tag = qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005622 }
5623
Tejun Heoe4a70e72006-03-31 20:36:47 +09005624 qc->flags |= ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005625 ap->qc_active |= 1 << qc->tag;
Tejun Heoe4a70e72006-03-31 20:36:47 +09005626
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 if (ata_should_dma_map(qc)) {
5628 if (qc->flags & ATA_QCFLAG_SG) {
5629 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09005630 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
5632 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09005633 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 }
5635 } else {
5636 qc->flags &= ~ATA_QCFLAG_DMAMAP;
5637 }
5638
5639 ap->ops->qc_prep(qc);
5640
Tejun Heo8e0e6942006-03-31 20:41:11 +09005641 qc->err_mask |= ap->ops->qc_issue(qc);
5642 if (unlikely(qc->err_mask))
5643 goto err;
5644 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645
Tejun Heo8e436af2006-01-23 13:09:36 +09005646sg_err:
5647 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo8e0e6942006-03-31 20:41:11 +09005648 qc->err_mask |= AC_ERR_SYSTEM;
5649err:
5650 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651}
5652
5653/**
5654 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
5655 * @qc: command to issue to device
5656 *
5657 * Using various libata functions and hooks, this function
5658 * starts an ATA command. ATA commands are grouped into
5659 * classes called "protocols", and issuing each type of protocol
5660 * is slightly different.
5661 *
Edward Falk0baab862005-06-02 18:17:13 -04005662 * May be used as the qc_issue() entry in ata_port_operations.
5663 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005665 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 *
5667 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09005668 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 */
5670
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09005671unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672{
5673 struct ata_port *ap = qc->ap;
5674
Albert Leee50362e2005-09-27 17:39:50 +08005675 /* Use polling pio if the LLD doesn't handle
5676 * interrupt driven pio and atapi CDB interrupt.
5677 */
5678 if (ap->flags & ATA_FLAG_PIO_POLLING) {
5679 switch (qc->tf.protocol) {
5680 case ATA_PROT_PIO:
Albert Leee3472cb2006-12-07 11:37:58 +08005681 case ATA_PROT_NODATA:
Albert Leee50362e2005-09-27 17:39:50 +08005682 case ATA_PROT_ATAPI:
5683 case ATA_PROT_ATAPI_NODATA:
5684 qc->tf.flags |= ATA_TFLAG_POLLING;
5685 break;
5686 case ATA_PROT_ATAPI_DMA:
5687 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
Albert Lee3a778272006-06-22 13:00:25 +08005688 /* see ata_dma_blacklisted() */
Albert Leee50362e2005-09-27 17:39:50 +08005689 BUG();
5690 break;
5691 default:
5692 break;
5693 }
5694 }
5695
Albert Lee312f7da2005-09-27 17:38:03 +08005696 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 ata_dev_select(ap, qc->dev->devno, 1, 0);
5698
Albert Lee312f7da2005-09-27 17:38:03 +08005699 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 switch (qc->tf.protocol) {
5701 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08005702 if (qc->tf.flags & ATA_TFLAG_POLLING)
5703 ata_qc_set_polling(qc);
5704
Jeff Garzike5338252005-10-30 21:37:17 -05005705 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08005706 ap->hsm_task_state = HSM_ST_LAST;
5707
5708 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee31ce6da2006-04-03 18:31:44 +08005709 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08005710
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 break;
5712
5713 case ATA_PROT_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05005714 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08005715
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
5717 ap->ops->bmdma_setup(qc); /* set up bmdma */
5718 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08005719 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 break;
5721
Albert Lee312f7da2005-09-27 17:38:03 +08005722 case ATA_PROT_PIO:
5723 if (qc->tf.flags & ATA_TFLAG_POLLING)
5724 ata_qc_set_polling(qc);
5725
Jeff Garzike5338252005-10-30 21:37:17 -05005726 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08005727
Albert Lee54f00382005-09-30 19:14:19 +08005728 if (qc->tf.flags & ATA_TFLAG_WRITE) {
5729 /* PIO data out protocol */
5730 ap->hsm_task_state = HSM_ST_FIRST;
Albert Lee31ce6da2006-04-03 18:31:44 +08005731 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee54f00382005-09-30 19:14:19 +08005732
5733 /* always send first data block using
Albert Leee27486d2005-11-01 19:24:49 +08005734 * the ata_pio_task() codepath.
Albert Lee54f00382005-09-30 19:14:19 +08005735 */
Albert Lee312f7da2005-09-27 17:38:03 +08005736 } else {
Albert Lee54f00382005-09-30 19:14:19 +08005737 /* PIO data in protocol */
5738 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08005739
Albert Lee54f00382005-09-30 19:14:19 +08005740 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee31ce6da2006-04-03 18:31:44 +08005741 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08005742
Albert Lee54f00382005-09-30 19:14:19 +08005743 /* if polling, ata_pio_task() handles the rest.
5744 * otherwise, interrupt handler takes over from here.
5745 */
Albert Lee312f7da2005-09-27 17:38:03 +08005746 }
5747
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 break;
5749
5750 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08005752 if (qc->tf.flags & ATA_TFLAG_POLLING)
5753 ata_qc_set_polling(qc);
5754
Jeff Garzike5338252005-10-30 21:37:17 -05005755 ata_tf_to_host(ap, &qc->tf);
Jeff Garzikf6ef65e2006-01-27 02:45:00 -05005756
Albert Lee312f7da2005-09-27 17:38:03 +08005757 ap->hsm_task_state = HSM_ST_FIRST;
5758
5759 /* send cdb by polling if no cdb interrupt */
5760 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
5761 (qc->tf.flags & ATA_TFLAG_POLLING))
Albert Lee31ce6da2006-04-03 18:31:44 +08005762 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 break;
5764
5765 case ATA_PROT_ATAPI_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05005766 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08005767
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
5769 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08005770 ap->hsm_task_state = HSM_ST_FIRST;
5771
5772 /* send cdb by polling if no cdb interrupt */
5773 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Albert Lee31ce6da2006-04-03 18:31:44 +08005774 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 break;
5776
5777 default:
5778 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09005779 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 }
5781
5782 return 0;
5783}
5784
5785/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 * ata_host_intr - Handle host interrupt for given (port, task)
5787 * @ap: Port on which interrupt arrived (possibly...)
5788 * @qc: Taskfile currently active in engine
5789 *
5790 * Handle host interrupt for given queued command. Currently,
5791 * only DMA interrupts are handled. All other commands are
5792 * handled via polling with interrupts disabled (nIEN bit).
5793 *
5794 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005795 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 *
5797 * RETURNS:
5798 * One if interrupt was handled, zero if not (shared irq).
5799 */
5800
5801inline unsigned int ata_host_intr (struct ata_port *ap,
5802 struct ata_queued_cmd *qc)
5803{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005804 struct ata_eh_info *ehi = &ap->link.eh_info;
Albert Lee312f7da2005-09-27 17:38:03 +08005805 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806
Albert Lee312f7da2005-09-27 17:38:03 +08005807 VPRINTK("ata%u: protocol %d task_state %d\n",
Tejun Heo44877b42007-02-21 01:06:51 +09005808 ap->print_id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809
Albert Lee312f7da2005-09-27 17:38:03 +08005810 /* Check whether we are expecting interrupt in this state */
5811 switch (ap->hsm_task_state) {
5812 case HSM_ST_FIRST:
Albert Lee6912ccd2006-03-25 17:45:49 +08005813 /* Some pre-ATAPI-4 devices assert INTRQ
5814 * at this state when ready to receive CDB.
5815 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816
Albert Lee312f7da2005-09-27 17:38:03 +08005817 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
5818 * The flag was turned on only for atapi devices.
5819 * No need to check is_atapi_taskfile(&qc->tf) again.
5820 */
5821 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 break;
Albert Lee312f7da2005-09-27 17:38:03 +08005824 case HSM_ST_LAST:
5825 if (qc->tf.protocol == ATA_PROT_DMA ||
5826 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
5827 /* check status of DMA engine */
5828 host_stat = ap->ops->bmdma_status(ap);
Tejun Heo44877b42007-02-21 01:06:51 +09005829 VPRINTK("ata%u: host_stat 0x%X\n",
5830 ap->print_id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831
Albert Lee312f7da2005-09-27 17:38:03 +08005832 /* if it's not our irq... */
5833 if (!(host_stat & ATA_DMA_INTR))
5834 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835
Albert Lee312f7da2005-09-27 17:38:03 +08005836 /* before we do anything else, clear DMA-Start bit */
5837 ap->ops->bmdma_stop(qc);
Albert Leea4f16612005-12-26 16:40:53 +08005838
5839 if (unlikely(host_stat & ATA_DMA_ERR)) {
5840 /* error when transfering data to/from memory */
5841 qc->err_mask |= AC_ERR_HOST_BUS;
5842 ap->hsm_task_state = HSM_ST_ERR;
5843 }
Albert Lee312f7da2005-09-27 17:38:03 +08005844 }
5845 break;
5846 case HSM_ST:
5847 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 default:
5849 goto idle_irq;
5850 }
5851
Albert Lee312f7da2005-09-27 17:38:03 +08005852 /* check altstatus */
5853 status = ata_altstatus(ap);
5854 if (status & ATA_BUSY)
5855 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856
Albert Lee312f7da2005-09-27 17:38:03 +08005857 /* check main status, clearing INTRQ */
5858 status = ata_chk_status(ap);
5859 if (unlikely(status & ATA_BUSY))
5860 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861
Albert Lee312f7da2005-09-27 17:38:03 +08005862 /* ack bmdma irq events */
5863 ap->ops->irq_clear(ap);
5864
Albert Leebb5cb292006-03-25 17:48:02 +08005865 ata_hsm_move(ap, qc, status, 0);
Tejun Heoea547632006-11-17 12:06:21 +09005866
5867 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
5868 qc->tf.protocol == ATA_PROT_ATAPI_DMA))
5869 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
5870
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 return 1; /* irq handled */
5872
5873idle_irq:
5874 ap->stats.idle_irq++;
5875
5876#ifdef ATA_IRQ_TRAP
5877 if ((ap->stats.idle_irq % 1000) == 0) {
Jeff Garzik6d32d302007-08-15 05:38:46 -04005878 ata_chk_status(ap);
5879 ap->ops->irq_clear(ap);
Tejun Heof15a1da2006-05-15 20:57:56 +09005880 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
Alan Cox23cfce82006-03-21 16:06:53 +00005881 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 }
5883#endif
5884 return 0; /* irq not handled */
5885}
5886
5887/**
5888 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04005889 * @irq: irq line (unused)
Jeff Garzikcca39742006-08-24 03:19:22 -04005890 * @dev_instance: pointer to our ata_host information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04005892 * Default interrupt handler for PCI IDE devices. Calls
5893 * ata_host_intr() for each port that is not disabled.
5894 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005896 * Obtains host lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 *
5898 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005899 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 */
5901
David Howells7d12e782006-10-05 14:55:46 +01005902irqreturn_t ata_interrupt (int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903{
Jeff Garzikcca39742006-08-24 03:19:22 -04005904 struct ata_host *host = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 unsigned int i;
5906 unsigned int handled = 0;
5907 unsigned long flags;
5908
5909 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
Jeff Garzikcca39742006-08-24 03:19:22 -04005910 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911
Jeff Garzikcca39742006-08-24 03:19:22 -04005912 for (i = 0; i < host->n_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 struct ata_port *ap;
5914
Jeff Garzikcca39742006-08-24 03:19:22 -04005915 ap = host->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09005916 if (ap &&
Jeff Garzik029f5462006-04-02 10:30:40 -04005917 !(ap->flags & ATA_FLAG_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 struct ata_queued_cmd *qc;
5919
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005920 qc = ata_qc_from_tag(ap, ap->link.active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08005921 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08005922 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 handled |= ata_host_intr(ap, qc);
5924 }
5925 }
5926
Jeff Garzikcca39742006-08-24 03:19:22 -04005927 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928
5929 return IRQ_RETVAL(handled);
5930}
5931
Tejun Heo34bf2172006-05-15 20:57:46 +09005932/**
5933 * sata_scr_valid - test whether SCRs are accessible
Tejun Heo936fd732007-08-06 18:36:23 +09005934 * @link: ATA link to test SCR accessibility for
Tejun Heo34bf2172006-05-15 20:57:46 +09005935 *
Tejun Heo936fd732007-08-06 18:36:23 +09005936 * Test whether SCRs are accessible for @link.
Tejun Heo34bf2172006-05-15 20:57:46 +09005937 *
5938 * LOCKING:
5939 * None.
5940 *
5941 * RETURNS:
5942 * 1 if SCRs are accessible, 0 otherwise.
5943 */
Tejun Heo936fd732007-08-06 18:36:23 +09005944int sata_scr_valid(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09005945{
Tejun Heo936fd732007-08-06 18:36:23 +09005946 struct ata_port *ap = link->ap;
5947
Tejun Heoa16abc02007-05-21 18:33:47 +02005948 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
Tejun Heo34bf2172006-05-15 20:57:46 +09005949}
5950
5951/**
5952 * sata_scr_read - read SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09005953 * @link: ATA link to read SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005954 * @reg: SCR to read
5955 * @val: Place to store read value
5956 *
Tejun Heo936fd732007-08-06 18:36:23 +09005957 * Read SCR register @reg of @link into *@val. This function is
Tejun Heo34bf2172006-05-15 20:57:46 +09005958 * guaranteed to succeed if the cable type of the port is SATA
5959 * and the port implements ->scr_read.
5960 *
5961 * LOCKING:
5962 * None.
5963 *
5964 * RETURNS:
5965 * 0 on success, negative errno on failure.
5966 */
Tejun Heo936fd732007-08-06 18:36:23 +09005967int sata_scr_read(struct ata_link *link, int reg, u32 *val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005968{
Tejun Heo936fd732007-08-06 18:36:23 +09005969 struct ata_port *ap = link->ap;
5970
5971 if (sata_scr_valid(link))
Tejun Heoda3dbb12007-07-16 14:29:40 +09005972 return ap->ops->scr_read(ap, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005973 return -EOPNOTSUPP;
5974}
5975
5976/**
5977 * sata_scr_write - write SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09005978 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005979 * @reg: SCR to write
5980 * @val: value to write
5981 *
Tejun Heo936fd732007-08-06 18:36:23 +09005982 * Write @val to SCR register @reg of @link. This function is
Tejun Heo34bf2172006-05-15 20:57:46 +09005983 * guaranteed to succeed if the cable type of the port is SATA
5984 * and the port implements ->scr_read.
5985 *
5986 * LOCKING:
5987 * None.
5988 *
5989 * RETURNS:
5990 * 0 on success, negative errno on failure.
5991 */
Tejun Heo936fd732007-08-06 18:36:23 +09005992int sata_scr_write(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005993{
Tejun Heo936fd732007-08-06 18:36:23 +09005994 struct ata_port *ap = link->ap;
5995
5996 if (sata_scr_valid(link))
Tejun Heoda3dbb12007-07-16 14:29:40 +09005997 return ap->ops->scr_write(ap, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005998 return -EOPNOTSUPP;
5999}
6000
6001/**
6002 * sata_scr_write_flush - write SCR register of the specified port and flush
Tejun Heo936fd732007-08-06 18:36:23 +09006003 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09006004 * @reg: SCR to write
6005 * @val: value to write
6006 *
6007 * This function is identical to sata_scr_write() except that this
6008 * function performs flush after writing to the register.
6009 *
6010 * LOCKING:
6011 * None.
6012 *
6013 * RETURNS:
6014 * 0 on success, negative errno on failure.
6015 */
Tejun Heo936fd732007-08-06 18:36:23 +09006016int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09006017{
Tejun Heo936fd732007-08-06 18:36:23 +09006018 struct ata_port *ap = link->ap;
Tejun Heoda3dbb12007-07-16 14:29:40 +09006019 int rc;
6020
Tejun Heo936fd732007-08-06 18:36:23 +09006021 if (sata_scr_valid(link)) {
Tejun Heoda3dbb12007-07-16 14:29:40 +09006022 rc = ap->ops->scr_write(ap, reg, val);
6023 if (rc == 0)
6024 rc = ap->ops->scr_read(ap, reg, &val);
6025 return rc;
Tejun Heo34bf2172006-05-15 20:57:46 +09006026 }
6027 return -EOPNOTSUPP;
6028}
6029
6030/**
Tejun Heo936fd732007-08-06 18:36:23 +09006031 * ata_link_online - test whether the given link is online
6032 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09006033 *
Tejun Heo936fd732007-08-06 18:36:23 +09006034 * Test whether @link is online. Note that this function returns
6035 * 0 if online status of @link cannot be obtained, so
6036 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09006037 *
6038 * LOCKING:
6039 * None.
6040 *
6041 * RETURNS:
6042 * 1 if the port online status is available and online.
6043 */
Tejun Heo936fd732007-08-06 18:36:23 +09006044int ata_link_online(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09006045{
6046 u32 sstatus;
6047
Tejun Heo936fd732007-08-06 18:36:23 +09006048 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
6049 (sstatus & 0xf) == 0x3)
Tejun Heo34bf2172006-05-15 20:57:46 +09006050 return 1;
6051 return 0;
6052}
6053
6054/**
Tejun Heo936fd732007-08-06 18:36:23 +09006055 * ata_link_offline - test whether the given link is offline
6056 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09006057 *
Tejun Heo936fd732007-08-06 18:36:23 +09006058 * Test whether @link is offline. Note that this function
6059 * returns 0 if offline status of @link cannot be obtained, so
6060 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09006061 *
6062 * LOCKING:
6063 * None.
6064 *
6065 * RETURNS:
6066 * 1 if the port offline status is available and offline.
6067 */
Tejun Heo936fd732007-08-06 18:36:23 +09006068int ata_link_offline(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09006069{
6070 u32 sstatus;
6071
Tejun Heo936fd732007-08-06 18:36:23 +09006072 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
6073 (sstatus & 0xf) != 0x3)
Tejun Heo34bf2172006-05-15 20:57:46 +09006074 return 1;
6075 return 0;
6076}
Edward Falk0baab862005-06-02 18:17:13 -04006077
Tejun Heo77b08fb2006-06-24 20:30:19 +09006078int ata_flush_cache(struct ata_device *dev)
Jens Axboe9b847542006-01-06 09:28:07 +01006079{
Tejun Heo977e6b92006-06-24 20:30:19 +09006080 unsigned int err_mask;
Jens Axboe9b847542006-01-06 09:28:07 +01006081 u8 cmd;
6082
6083 if (!ata_try_flush_cache(dev))
6084 return 0;
6085
Tejun Heo6fc49ad2006-11-11 20:10:45 +09006086 if (dev->flags & ATA_DFLAG_FLUSH_EXT)
Jens Axboe9b847542006-01-06 09:28:07 +01006087 cmd = ATA_CMD_FLUSH_EXT;
6088 else
6089 cmd = ATA_CMD_FLUSH;
6090
Alan Cox4f343372007-08-08 14:30:31 +01006091 /* This is wrong. On a failed flush we get back the LBA of the lost
6092 sector and we should (assuming it wasn't aborted as unknown) issue
6093 a further flush command to continue the writeback until it
6094 does not error */
Tejun Heo977e6b92006-06-24 20:30:19 +09006095 err_mask = ata_do_simple_cmd(dev, cmd);
6096 if (err_mask) {
6097 ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
6098 return -EIO;
6099 }
6100
6101 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +01006102}
6103
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006104#ifdef CONFIG_PM
Jeff Garzikcca39742006-08-24 03:19:22 -04006105static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
6106 unsigned int action, unsigned int ehi_flags,
6107 int wait)
Tejun Heo500530f2006-07-03 16:07:27 +09006108{
6109 unsigned long flags;
6110 int i, rc;
6111
Jeff Garzikcca39742006-08-24 03:19:22 -04006112 for (i = 0; i < host->n_ports; i++) {
6113 struct ata_port *ap = host->ports[i];
Tejun Heoe3667eb2007-08-06 18:36:24 +09006114 struct ata_link *link;
Tejun Heo500530f2006-07-03 16:07:27 +09006115
6116 /* Previous resume operation might still be in
6117 * progress. Wait for PM_PENDING to clear.
6118 */
6119 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
6120 ata_port_wait_eh(ap);
6121 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
6122 }
6123
6124 /* request PM ops to EH */
6125 spin_lock_irqsave(ap->lock, flags);
6126
6127 ap->pm_mesg = mesg;
6128 if (wait) {
6129 rc = 0;
6130 ap->pm_result = &rc;
6131 }
6132
6133 ap->pflags |= ATA_PFLAG_PM_PENDING;
Tejun Heoe3667eb2007-08-06 18:36:24 +09006134 __ata_port_for_each_link(link, ap) {
6135 link->eh_info.action |= action;
6136 link->eh_info.flags |= ehi_flags;
6137 }
Tejun Heo500530f2006-07-03 16:07:27 +09006138
6139 ata_port_schedule_eh(ap);
6140
6141 spin_unlock_irqrestore(ap->lock, flags);
6142
6143 /* wait and check result */
6144 if (wait) {
6145 ata_port_wait_eh(ap);
6146 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
6147 if (rc)
6148 return rc;
6149 }
6150 }
6151
6152 return 0;
6153}
6154
6155/**
Jeff Garzikcca39742006-08-24 03:19:22 -04006156 * ata_host_suspend - suspend host
6157 * @host: host to suspend
Tejun Heo500530f2006-07-03 16:07:27 +09006158 * @mesg: PM message
6159 *
Jeff Garzikcca39742006-08-24 03:19:22 -04006160 * Suspend @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09006161 * function requests EH to perform PM operations and waits for EH
6162 * to finish.
6163 *
6164 * LOCKING:
6165 * Kernel thread context (may sleep).
6166 *
6167 * RETURNS:
6168 * 0 on success, -errno on failure.
6169 */
Jeff Garzikcca39742006-08-24 03:19:22 -04006170int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006171{
Tejun Heo9666f402007-05-04 21:27:47 +02006172 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09006173
Jeff Garzikcca39742006-08-24 03:19:22 -04006174 rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
Tejun Heo9666f402007-05-04 21:27:47 +02006175 if (rc == 0)
6176 host->dev->power.power_state = mesg;
Tejun Heo500530f2006-07-03 16:07:27 +09006177 return rc;
6178}
6179
6180/**
Jeff Garzikcca39742006-08-24 03:19:22 -04006181 * ata_host_resume - resume host
6182 * @host: host to resume
Tejun Heo500530f2006-07-03 16:07:27 +09006183 *
Jeff Garzikcca39742006-08-24 03:19:22 -04006184 * Resume @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09006185 * function requests EH to perform PM operations and returns.
6186 * Note that all resume operations are performed parallely.
6187 *
6188 * LOCKING:
6189 * Kernel thread context (may sleep).
6190 */
Jeff Garzikcca39742006-08-24 03:19:22 -04006191void ata_host_resume(struct ata_host *host)
Tejun Heo500530f2006-07-03 16:07:27 +09006192{
Jeff Garzikcca39742006-08-24 03:19:22 -04006193 ata_host_request_pm(host, PMSG_ON, ATA_EH_SOFTRESET,
6194 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
6195 host->dev->power.power_state = PMSG_ON;
Tejun Heo500530f2006-07-03 16:07:27 +09006196}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006197#endif
Tejun Heo500530f2006-07-03 16:07:27 +09006198
Randy Dunlapc893a3a2006-01-28 13:15:32 -05006199/**
6200 * ata_port_start - Set port up for dma.
6201 * @ap: Port to initialize
6202 *
6203 * Called just after data structures for each port are
6204 * initialized. Allocates space for PRD table.
6205 *
6206 * May be used as the port_start() entry in ata_port_operations.
6207 *
6208 * LOCKING:
6209 * Inherited from caller.
6210 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09006211int ata_port_start(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212{
Brian King2f1f6102006-03-23 17:30:15 -06006213 struct device *dev = ap->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05006214 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215
Tejun Heof0d36ef2007-01-20 16:00:28 +09006216 ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
6217 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 if (!ap->prd)
6219 return -ENOMEM;
6220
Jeff Garzik6037d6b2005-11-04 22:08:00 -05006221 rc = ata_pad_alloc(ap, dev);
Tejun Heof0d36ef2007-01-20 16:00:28 +09006222 if (rc)
Jeff Garzik6037d6b2005-11-04 22:08:00 -05006223 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04006224
Tejun Heof0d36ef2007-01-20 16:00:28 +09006225 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd,
6226 (unsigned long long)ap->prd_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 return 0;
6228}
6229
Edward Falk0baab862005-06-02 18:17:13 -04006230/**
Tejun Heo3ef3b432006-05-31 18:27:30 +09006231 * ata_dev_init - Initialize an ata_device structure
6232 * @dev: Device structure to initialize
6233 *
6234 * Initialize @dev in preparation for probing.
6235 *
6236 * LOCKING:
6237 * Inherited from caller.
6238 */
6239void ata_dev_init(struct ata_device *dev)
6240{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006241 struct ata_link *link = dev->link;
6242 struct ata_port *ap = link->ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09006243 unsigned long flags;
Tejun Heo3ef3b432006-05-31 18:27:30 +09006244
Tejun Heo5a04bf42006-05-31 18:27:38 +09006245 /* SATA spd limit is bound to the first device */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006246 link->sata_spd_limit = link->hw_sata_spd_limit;
6247 link->sata_spd = 0;
Tejun Heo5a04bf42006-05-31 18:27:38 +09006248
Tejun Heo72fa4b72006-05-31 18:27:32 +09006249 /* High bits of dev->flags are used to record warm plug
6250 * requests which occur asynchronously. Synchronize using
Jeff Garzikcca39742006-08-24 03:19:22 -04006251 * host lock.
Tejun Heo72fa4b72006-05-31 18:27:32 +09006252 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006253 spin_lock_irqsave(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09006254 dev->flags &= ~ATA_DFLAG_INIT_MASK;
Tejun Heo3dcc3232007-09-03 12:20:11 +09006255 dev->horkage = 0;
Jeff Garzikba6a1302006-06-22 23:46:10 -04006256 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09006257
6258 memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
6259 sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
Tejun Heo3ef3b432006-05-31 18:27:30 +09006260 dev->pio_mask = UINT_MAX;
6261 dev->mwdma_mask = UINT_MAX;
6262 dev->udma_mask = UINT_MAX;
6263}
6264
6265/**
Tejun Heo4fb37a22007-08-06 18:36:23 +09006266 * ata_link_init - Initialize an ata_link structure
6267 * @ap: ATA port link is attached to
6268 * @link: Link structure to initialize
Tejun Heo89898052007-08-06 18:36:23 +09006269 * @pmp: Port multiplier port number
Tejun Heo4fb37a22007-08-06 18:36:23 +09006270 *
6271 * Initialize @link.
6272 *
6273 * LOCKING:
6274 * Kernel thread context (may sleep)
6275 */
Tejun Heofb7fd612007-09-23 13:14:12 +09006276void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
Tejun Heo4fb37a22007-08-06 18:36:23 +09006277{
6278 int i;
6279
6280 /* clear everything except for devices */
6281 memset(link, 0, offsetof(struct ata_link, device[0]));
6282
6283 link->ap = ap;
Tejun Heo89898052007-08-06 18:36:23 +09006284 link->pmp = pmp;
Tejun Heo4fb37a22007-08-06 18:36:23 +09006285 link->active_tag = ATA_TAG_POISON;
6286 link->hw_sata_spd_limit = UINT_MAX;
6287
6288 /* can't use iterator, ap isn't initialized yet */
6289 for (i = 0; i < ATA_MAX_DEVICES; i++) {
6290 struct ata_device *dev = &link->device[i];
6291
6292 dev->link = link;
6293 dev->devno = dev - link->device;
6294 ata_dev_init(dev);
6295 }
6296}
6297
6298/**
6299 * sata_link_init_spd - Initialize link->sata_spd_limit
6300 * @link: Link to configure sata_spd_limit for
6301 *
6302 * Initialize @link->[hw_]sata_spd_limit to the currently
6303 * configured value.
6304 *
6305 * LOCKING:
6306 * Kernel thread context (may sleep).
6307 *
6308 * RETURNS:
6309 * 0 on success, -errno on failure.
6310 */
Tejun Heofb7fd612007-09-23 13:14:12 +09006311int sata_link_init_spd(struct ata_link *link)
Tejun Heo4fb37a22007-08-06 18:36:23 +09006312{
6313 u32 scontrol, spd;
6314 int rc;
6315
6316 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
6317 if (rc)
6318 return rc;
6319
6320 spd = (scontrol >> 4) & 0xf;
6321 if (spd)
6322 link->hw_sata_spd_limit &= (1 << spd) - 1;
6323
6324 link->sata_spd_limit = link->hw_sata_spd_limit;
6325
6326 return 0;
6327}
6328
6329/**
Tejun Heof3187192007-04-17 23:44:07 +09006330 * ata_port_alloc - allocate and initialize basic ATA port resources
6331 * @host: ATA host this allocated port belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332 *
Tejun Heof3187192007-04-17 23:44:07 +09006333 * Allocate and initialize basic ATA port resources.
6334 *
6335 * RETURNS:
6336 * Allocate ATA port on success, NULL on failure.
Jeff Garzik0cba6322005-05-30 19:49:12 -04006337 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09006339 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340 */
Tejun Heof3187192007-04-17 23:44:07 +09006341struct ata_port *ata_port_alloc(struct ata_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342{
Tejun Heof3187192007-04-17 23:44:07 +09006343 struct ata_port *ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344
Tejun Heof3187192007-04-17 23:44:07 +09006345 DPRINTK("ENTER\n");
6346
6347 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
6348 if (!ap)
6349 return NULL;
6350
Tejun Heof4d6d002007-05-01 11:50:15 +02006351 ap->pflags |= ATA_PFLAG_INITIALIZING;
Jeff Garzikcca39742006-08-24 03:19:22 -04006352 ap->lock = &host->lock;
Tejun Heo198e0fe2006-04-02 18:51:52 +09006353 ap->flags = ATA_FLAG_DISABLED;
Tejun Heof3187192007-04-17 23:44:07 +09006354 ap->print_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 ap->ctl = ATA_DEVCTL_OBS;
Jeff Garzikcca39742006-08-24 03:19:22 -04006356 ap->host = host;
Tejun Heof3187192007-04-17 23:44:07 +09006357 ap->dev = host->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358 ap->last_ctl = 0xFF;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04006359
6360#if defined(ATA_VERBOSE_DEBUG)
6361 /* turn on all debugging levels */
6362 ap->msg_enable = 0x00FF;
6363#elif defined(ATA_DEBUG)
6364 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 +09006365#else
Borislav Petkov0dd4b212006-06-23 02:29:08 -04006366 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04006367#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368
David Howells65f27f32006-11-22 14:55:48 +00006369 INIT_DELAYED_WORK(&ap->port_task, NULL);
6370 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
6371 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09006372 INIT_LIST_HEAD(&ap->eh_done_q);
Tejun Heoc6cf9e92006-05-31 18:27:27 +09006373 init_waitqueue_head(&ap->eh_wait_q);
Tejun Heo5ddf24c2007-07-16 14:29:41 +09006374 init_timer_deferrable(&ap->fastdrain_timer);
6375 ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
6376 ap->fastdrain_timer.data = (unsigned long)ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
Tejun Heo838df622006-05-15 20:57:44 +09006378 ap->cbl = ATA_CBL_NONE;
Tejun Heo838df622006-05-15 20:57:44 +09006379
Tejun Heo89898052007-08-06 18:36:23 +09006380 ata_link_init(ap, &ap->link, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381
6382#ifdef ATA_IRQ_TRAP
6383 ap->stats.unhandled_irq = 1;
6384 ap->stats.idle_irq = 1;
6385#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 return ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387}
6388
Tejun Heof0d36ef2007-01-20 16:00:28 +09006389static void ata_host_release(struct device *gendev, void *res)
6390{
6391 struct ata_host *host = dev_get_drvdata(gendev);
6392 int i;
6393
6394 for (i = 0; i < host->n_ports; i++) {
6395 struct ata_port *ap = host->ports[i];
6396
Tejun Heoecef7252007-04-17 23:44:06 +09006397 if (!ap)
6398 continue;
6399
6400 if ((host->flags & ATA_HOST_STARTED) && ap->ops->port_stop)
Tejun Heof0d36ef2007-01-20 16:00:28 +09006401 ap->ops->port_stop(ap);
Tejun Heof0d36ef2007-01-20 16:00:28 +09006402 }
6403
Tejun Heoecef7252007-04-17 23:44:06 +09006404 if ((host->flags & ATA_HOST_STARTED) && host->ops->host_stop)
Tejun Heof0d36ef2007-01-20 16:00:28 +09006405 host->ops->host_stop(host);
Tejun Heo1aa56cc2007-02-27 22:33:21 +09006406
Tejun Heo1aa506e2007-03-09 19:36:12 +09006407 for (i = 0; i < host->n_ports; i++) {
6408 struct ata_port *ap = host->ports[i];
6409
Tejun Heo49114872007-04-17 23:44:06 +09006410 if (!ap)
6411 continue;
6412
6413 if (ap->scsi_host)
Tejun Heo1aa506e2007-03-09 19:36:12 +09006414 scsi_host_put(ap->scsi_host);
6415
Tejun Heo49114872007-04-17 23:44:06 +09006416 kfree(ap);
Tejun Heo1aa506e2007-03-09 19:36:12 +09006417 host->ports[i] = NULL;
6418 }
6419
Tejun Heo1aa56cc2007-02-27 22:33:21 +09006420 dev_set_drvdata(gendev, NULL);
Tejun Heof0d36ef2007-01-20 16:00:28 +09006421}
6422
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423/**
Tejun Heof3187192007-04-17 23:44:07 +09006424 * ata_host_alloc - allocate and init basic ATA host resources
6425 * @dev: generic device this host is associated with
6426 * @max_ports: maximum number of ATA ports associated with this host
6427 *
6428 * Allocate and initialize basic ATA host resources. LLD calls
6429 * this function to allocate a host, initializes it fully and
6430 * attaches it using ata_host_register().
6431 *
6432 * @max_ports ports are allocated and host->n_ports is
6433 * initialized to @max_ports. The caller is allowed to decrease
6434 * host->n_ports before calling ata_host_register(). The unused
6435 * ports will be automatically freed on registration.
6436 *
6437 * RETURNS:
6438 * Allocate ATA host on success, NULL on failure.
6439 *
6440 * LOCKING:
6441 * Inherited from calling layer (may sleep).
6442 */
6443struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
6444{
6445 struct ata_host *host;
6446 size_t sz;
6447 int i;
6448
6449 DPRINTK("ENTER\n");
6450
6451 if (!devres_open_group(dev, NULL, GFP_KERNEL))
6452 return NULL;
6453
6454 /* alloc a container for our list of ATA ports (buses) */
6455 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
6456 /* alloc a container for our list of ATA ports (buses) */
6457 host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
6458 if (!host)
6459 goto err_out;
6460
6461 devres_add(dev, host);
6462 dev_set_drvdata(dev, host);
6463
6464 spin_lock_init(&host->lock);
6465 host->dev = dev;
6466 host->n_ports = max_ports;
6467
6468 /* allocate ports bound to this host */
6469 for (i = 0; i < max_ports; i++) {
6470 struct ata_port *ap;
6471
6472 ap = ata_port_alloc(host);
6473 if (!ap)
6474 goto err_out;
6475
6476 ap->port_no = i;
6477 host->ports[i] = ap;
6478 }
6479
6480 devres_remove_group(dev, NULL);
6481 return host;
6482
6483 err_out:
6484 devres_release_group(dev, NULL);
6485 return NULL;
6486}
6487
6488/**
Tejun Heof5cda252007-04-17 23:44:07 +09006489 * ata_host_alloc_pinfo - alloc host and init with port_info array
6490 * @dev: generic device this host is associated with
6491 * @ppi: array of ATA port_info to initialize host with
6492 * @n_ports: number of ATA ports attached to this host
6493 *
6494 * Allocate ATA host and initialize with info from @ppi. If NULL
6495 * terminated, @ppi may contain fewer entries than @n_ports. The
6496 * last entry will be used for the remaining ports.
6497 *
6498 * RETURNS:
6499 * Allocate ATA host on success, NULL on failure.
6500 *
6501 * LOCKING:
6502 * Inherited from calling layer (may sleep).
6503 */
6504struct ata_host *ata_host_alloc_pinfo(struct device *dev,
6505 const struct ata_port_info * const * ppi,
6506 int n_ports)
6507{
6508 const struct ata_port_info *pi;
6509 struct ata_host *host;
6510 int i, j;
6511
6512 host = ata_host_alloc(dev, n_ports);
6513 if (!host)
6514 return NULL;
6515
6516 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
6517 struct ata_port *ap = host->ports[i];
6518
6519 if (ppi[j])
6520 pi = ppi[j++];
6521
6522 ap->pio_mask = pi->pio_mask;
6523 ap->mwdma_mask = pi->mwdma_mask;
6524 ap->udma_mask = pi->udma_mask;
6525 ap->flags |= pi->flags;
Tejun Heo0c887582007-08-06 18:36:23 +09006526 ap->link.flags |= pi->link_flags;
Tejun Heof5cda252007-04-17 23:44:07 +09006527 ap->ops = pi->port_ops;
6528
6529 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
6530 host->ops = pi->port_ops;
6531 if (!host->private_data && pi->private_data)
6532 host->private_data = pi->private_data;
6533 }
6534
6535 return host;
6536}
6537
6538/**
Tejun Heoecef7252007-04-17 23:44:06 +09006539 * ata_host_start - start and freeze ports of an ATA host
6540 * @host: ATA host to start ports for
6541 *
6542 * Start and then freeze ports of @host. Started status is
6543 * recorded in host->flags, so this function can be called
6544 * multiple times. Ports are guaranteed to get started only
Tejun Heof3187192007-04-17 23:44:07 +09006545 * once. If host->ops isn't initialized yet, its set to the
6546 * first non-dummy port ops.
Tejun Heoecef7252007-04-17 23:44:06 +09006547 *
6548 * LOCKING:
6549 * Inherited from calling layer (may sleep).
6550 *
6551 * RETURNS:
6552 * 0 if all ports are started successfully, -errno otherwise.
6553 */
6554int ata_host_start(struct ata_host *host)
6555{
6556 int i, rc;
6557
6558 if (host->flags & ATA_HOST_STARTED)
6559 return 0;
6560
6561 for (i = 0; i < host->n_ports; i++) {
6562 struct ata_port *ap = host->ports[i];
6563
Tejun Heof3187192007-04-17 23:44:07 +09006564 if (!host->ops && !ata_port_is_dummy(ap))
6565 host->ops = ap->ops;
6566
Tejun Heoecef7252007-04-17 23:44:06 +09006567 if (ap->ops->port_start) {
6568 rc = ap->ops->port_start(ap);
6569 if (rc) {
6570 ata_port_printk(ap, KERN_ERR, "failed to "
6571 "start port (errno=%d)\n", rc);
6572 goto err_out;
6573 }
6574 }
6575
6576 ata_eh_freeze_port(ap);
6577 }
6578
6579 host->flags |= ATA_HOST_STARTED;
6580 return 0;
6581
6582 err_out:
6583 while (--i >= 0) {
6584 struct ata_port *ap = host->ports[i];
6585
6586 if (ap->ops->port_stop)
6587 ap->ops->port_stop(ap);
6588 }
6589 return rc;
6590}
6591
6592/**
Jeff Garzikcca39742006-08-24 03:19:22 -04006593 * ata_sas_host_init - Initialize a host struct
6594 * @host: host to initialize
6595 * @dev: device host is attached to
6596 * @flags: host flags
6597 * @ops: port_ops
Brian Kingb03732f2006-08-07 14:27:10 -05006598 *
6599 * LOCKING:
6600 * PCI/etc. bus probe sem.
6601 *
6602 */
Tejun Heof3187192007-04-17 23:44:07 +09006603/* KILLME - the only user left is ipr */
Jeff Garzikcca39742006-08-24 03:19:22 -04006604void ata_host_init(struct ata_host *host, struct device *dev,
6605 unsigned long flags, const struct ata_port_operations *ops)
Brian Kingb03732f2006-08-07 14:27:10 -05006606{
Jeff Garzikcca39742006-08-24 03:19:22 -04006607 spin_lock_init(&host->lock);
6608 host->dev = dev;
6609 host->flags = flags;
6610 host->ops = ops;
Brian Kingb03732f2006-08-07 14:27:10 -05006611}
6612
6613/**
Tejun Heof3187192007-04-17 23:44:07 +09006614 * ata_host_register - register initialized ATA host
6615 * @host: ATA host to register
6616 * @sht: template for SCSI host
Jeff Garzik0cba6322005-05-30 19:49:12 -04006617 *
Tejun Heof3187192007-04-17 23:44:07 +09006618 * Register initialized ATA host. @host is allocated using
6619 * ata_host_alloc() and fully initialized by LLD. This function
6620 * starts ports, registers @host with ATA and SCSI layers and
6621 * probe registered devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622 *
6623 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09006624 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 *
6626 * RETURNS:
Tejun Heof3187192007-04-17 23:44:07 +09006627 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628 */
Tejun Heof3187192007-04-17 23:44:07 +09006629int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630{
Tejun Heof3187192007-04-17 23:44:07 +09006631 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632
Tejun Heof3187192007-04-17 23:44:07 +09006633 /* host must have been started */
6634 if (!(host->flags & ATA_HOST_STARTED)) {
6635 dev_printk(KERN_ERR, host->dev,
6636 "BUG: trying to register unstarted host\n");
6637 WARN_ON(1);
6638 return -EINVAL;
Alan Cox02f076a2006-09-26 17:35:32 +01006639 }
Tejun Heof0d36ef2007-01-20 16:00:28 +09006640
Tejun Heof3187192007-04-17 23:44:07 +09006641 /* Blow away unused ports. This happens when LLD can't
6642 * determine the exact number of ports to allocate at
6643 * allocation time.
6644 */
6645 for (i = host->n_ports; host->ports[i]; i++)
6646 kfree(host->ports[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647
Tejun Heof3187192007-04-17 23:44:07 +09006648 /* give ports names and add SCSI hosts */
6649 for (i = 0; i < host->n_ports; i++)
6650 host->ports[i]->print_id = ata_print_id++;
Tejun Heof0d36ef2007-01-20 16:00:28 +09006651
Tejun Heof3187192007-04-17 23:44:07 +09006652 rc = ata_scsi_add_hosts(host, sht);
Tejun Heoecef7252007-04-17 23:44:06 +09006653 if (rc)
Tejun Heof3187192007-04-17 23:44:07 +09006654 return rc;
Tejun Heoecef7252007-04-17 23:44:06 +09006655
Tejun Heofafbae82007-05-15 03:28:16 +09006656 /* associate with ACPI nodes */
6657 ata_acpi_associate(host);
6658
Tejun Heof3187192007-04-17 23:44:07 +09006659 /* set cable, sata_spd_limit and report */
Jeff Garzikcca39742006-08-24 03:19:22 -04006660 for (i = 0; i < host->n_ports; i++) {
6661 struct ata_port *ap = host->ports[i];
Tejun Heof3187192007-04-17 23:44:07 +09006662 unsigned long xfer_mask;
6663
6664 /* set SATA cable type if still unset */
6665 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6666 ap->cbl = ATA_CBL_SATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667
Tejun Heo5a04bf42006-05-31 18:27:38 +09006668 /* init sata_spd_limit to the current value */
Tejun Heo4fb37a22007-08-06 18:36:23 +09006669 sata_link_init_spd(&ap->link);
Tejun Heo5a04bf42006-05-31 18:27:38 +09006670
Tejun Heocbcdd872007-08-18 13:14:55 +09006671 /* print per-port info to dmesg */
Tejun Heof3187192007-04-17 23:44:07 +09006672 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6673 ap->udma_mask);
6674
Tejun Heof3187192007-04-17 23:44:07 +09006675 if (!ata_port_is_dummy(ap))
Tejun Heocbcdd872007-08-18 13:14:55 +09006676 ata_port_printk(ap, KERN_INFO,
6677 "%cATA max %s %s\n",
Tejun Heoa16abc02007-05-21 18:33:47 +02006678 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
Tejun Heof3187192007-04-17 23:44:07 +09006679 ata_mode_string(xfer_mask),
Tejun Heocbcdd872007-08-18 13:14:55 +09006680 ap->link.eh_info.desc);
Tejun Heof3187192007-04-17 23:44:07 +09006681 else
6682 ata_port_printk(ap, KERN_INFO, "DUMMY\n");
6683 }
6684
6685 /* perform each probe synchronously */
6686 DPRINTK("probe begin\n");
6687 for (i = 0; i < host->n_ports; i++) {
6688 struct ata_port *ap = host->ports[i];
6689 int rc;
6690
6691 /* probe */
Tejun Heo52783c52006-05-31 18:28:22 +09006692 if (ap->ops->error_handler) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006693 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo3e706392006-05-31 18:28:11 +09006694 unsigned long flags;
6695
6696 ata_port_probe(ap);
6697
6698 /* kick EH for boot probing */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006699 spin_lock_irqsave(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09006700
Tejun Heof58229f2007-08-06 18:36:23 +09006701 ehi->probe_mask =
6702 (1 << ata_link_max_devices(&ap->link)) - 1;
Tejun Heo1cdaf532006-07-03 16:07:26 +09006703 ehi->action |= ATA_EH_SOFTRESET;
6704 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
Tejun Heo3e706392006-05-31 18:28:11 +09006705
Tejun Heof4d6d002007-05-01 11:50:15 +02006706 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
Tejun Heob51e9e52006-06-29 01:29:30 +09006707 ap->pflags |= ATA_PFLAG_LOADING;
Tejun Heo3e706392006-05-31 18:28:11 +09006708 ata_port_schedule_eh(ap);
6709
Jeff Garzikba6a1302006-06-22 23:46:10 -04006710 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09006711
6712 /* wait for EH to finish */
6713 ata_port_wait_eh(ap);
6714 } else {
Tejun Heo44877b42007-02-21 01:06:51 +09006715 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
Tejun Heo3e706392006-05-31 18:28:11 +09006716 rc = ata_bus_probe(ap);
Tejun Heo44877b42007-02-21 01:06:51 +09006717 DPRINTK("ata%u: bus probe end\n", ap->print_id);
Tejun Heo3e706392006-05-31 18:28:11 +09006718
6719 if (rc) {
6720 /* FIXME: do something useful here?
6721 * Current libata behavior will
6722 * tear down everything when
6723 * the module is removed
6724 * or the h/w is unplugged.
6725 */
6726 }
6727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 }
6729
6730 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05006731 DPRINTK("host probe begin\n");
Jeff Garzikcca39742006-08-24 03:19:22 -04006732 for (i = 0; i < host->n_ports; i++) {
6733 struct ata_port *ap = host->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734
Tejun Heo1ae46312007-07-16 14:29:40 +09006735 ata_scsi_scan_host(ap, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736 }
6737
Tejun Heof3187192007-04-17 23:44:07 +09006738 return 0;
6739}
6740
6741/**
Tejun Heof5cda252007-04-17 23:44:07 +09006742 * ata_host_activate - start host, request IRQ and register it
6743 * @host: target ATA host
6744 * @irq: IRQ to request
6745 * @irq_handler: irq_handler used when requesting IRQ
6746 * @irq_flags: irq_flags used when requesting IRQ
6747 * @sht: scsi_host_template to use when registering the host
6748 *
6749 * After allocating an ATA host and initializing it, most libata
6750 * LLDs perform three steps to activate the host - start host,
6751 * request IRQ and register it. This helper takes necessasry
6752 * arguments and performs the three steps in one go.
6753 *
6754 * LOCKING:
6755 * Inherited from calling layer (may sleep).
6756 *
6757 * RETURNS:
6758 * 0 on success, -errno otherwise.
6759 */
6760int ata_host_activate(struct ata_host *host, int irq,
6761 irq_handler_t irq_handler, unsigned long irq_flags,
6762 struct scsi_host_template *sht)
6763{
Tejun Heocbcdd872007-08-18 13:14:55 +09006764 int i, rc;
Tejun Heof5cda252007-04-17 23:44:07 +09006765
6766 rc = ata_host_start(host);
6767 if (rc)
6768 return rc;
6769
6770 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6771 dev_driver_string(host->dev), host);
6772 if (rc)
6773 return rc;
6774
Tejun Heocbcdd872007-08-18 13:14:55 +09006775 for (i = 0; i < host->n_ports; i++)
6776 ata_port_desc(host->ports[i], "irq %d", irq);
Tejun Heo40318262007-07-03 01:38:47 +09006777
Tejun Heof5cda252007-04-17 23:44:07 +09006778 rc = ata_host_register(host, sht);
6779 /* if failed, just free the IRQ and leave ports alone */
6780 if (rc)
6781 devm_free_irq(host->dev, irq, host);
6782
6783 return rc;
6784}
6785
6786/**
Tejun Heo720ba122006-05-31 18:28:13 +09006787 * ata_port_detach - Detach ATA port in prepration of device removal
6788 * @ap: ATA port to be detached
6789 *
6790 * Detach all ATA devices and the associated SCSI devices of @ap;
6791 * then, remove the associated SCSI host. @ap is guaranteed to
6792 * be quiescent on return from this function.
6793 *
6794 * LOCKING:
6795 * Kernel thread context (may sleep).
6796 */
6797void ata_port_detach(struct ata_port *ap)
6798{
6799 unsigned long flags;
Tejun Heo41bda9c2007-08-06 18:36:24 +09006800 struct ata_link *link;
Tejun Heof58229f2007-08-06 18:36:23 +09006801 struct ata_device *dev;
Tejun Heo720ba122006-05-31 18:28:13 +09006802
6803 if (!ap->ops->error_handler)
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006804 goto skip_eh;
Tejun Heo720ba122006-05-31 18:28:13 +09006805
6806 /* tell EH we're leaving & flush EH */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006807 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09006808 ap->pflags |= ATA_PFLAG_UNLOADING;
Jeff Garzikba6a1302006-06-22 23:46:10 -04006809 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006810
6811 ata_port_wait_eh(ap);
6812
6813 /* EH is now guaranteed to see UNLOADING, so no new device
6814 * will be attached. Disable all existing devices.
6815 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006816 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006817
Tejun Heo41bda9c2007-08-06 18:36:24 +09006818 ata_port_for_each_link(link, ap) {
6819 ata_link_for_each_dev(dev, link)
6820 ata_dev_disable(dev);
6821 }
Tejun Heo720ba122006-05-31 18:28:13 +09006822
Jeff Garzikba6a1302006-06-22 23:46:10 -04006823 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006824
6825 /* Final freeze & EH. All in-flight commands are aborted. EH
6826 * will be skipped and retrials will be terminated with bad
6827 * target.
6828 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006829 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006830 ata_port_freeze(ap); /* won't be thawed */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006831 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006832
6833 ata_port_wait_eh(ap);
Oleg Nesterov45a66c12007-07-09 11:46:13 -07006834 cancel_rearming_delayed_work(&ap->hotplug_task);
Tejun Heo720ba122006-05-31 18:28:13 +09006835
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006836 skip_eh:
Tejun Heo720ba122006-05-31 18:28:13 +09006837 /* remove the associated SCSI host */
Jeff Garzikcca39742006-08-24 03:19:22 -04006838 scsi_remove_host(ap->scsi_host);
Tejun Heo720ba122006-05-31 18:28:13 +09006839}
6840
6841/**
Tejun Heo0529c152007-01-20 16:00:26 +09006842 * ata_host_detach - Detach all ports of an ATA host
6843 * @host: Host to detach
6844 *
6845 * Detach all ports of @host.
6846 *
6847 * LOCKING:
6848 * Kernel thread context (may sleep).
6849 */
6850void ata_host_detach(struct ata_host *host)
6851{
6852 int i;
6853
6854 for (i = 0; i < host->n_ports; i++)
6855 ata_port_detach(host->ports[i]);
6856}
6857
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858/**
6859 * ata_std_ports - initialize ioaddr with standard port offsets.
6860 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04006861 *
6862 * Utility function which initializes data_addr, error_addr,
6863 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
6864 * device_addr, status_addr, and command_addr to standard offsets
6865 * relative to cmd_addr.
6866 *
6867 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868 */
Edward Falk0baab862005-06-02 18:17:13 -04006869
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870void ata_std_ports(struct ata_ioports *ioaddr)
6871{
6872 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
6873 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
6874 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
6875 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
6876 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
6877 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
6878 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
6879 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
6880 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
6881 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
6882}
6883
Edward Falk0baab862005-06-02 18:17:13 -04006884
Jeff Garzik374b1872005-08-30 05:42:52 -04006885#ifdef CONFIG_PCI
6886
Edward Falk0baab862005-06-02 18:17:13 -04006887/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888 * ata_pci_remove_one - PCI layer callback for device removal
6889 * @pdev: PCI device that was removed
6890 *
Tejun Heob878ca52007-01-20 16:00:28 +09006891 * PCI layer indicates to libata via this hook that hot-unplug or
6892 * module unload event has occurred. Detach all ports. Resource
6893 * release is handled via devres.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894 *
6895 * LOCKING:
6896 * Inherited from PCI layer (may sleep).
6897 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09006898void ata_pci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899{
6900 struct device *dev = pci_dev_to_dev(pdev);
Jeff Garzikcca39742006-08-24 03:19:22 -04006901 struct ata_host *host = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902
Tejun Heob878ca52007-01-20 16:00:28 +09006903 ata_host_detach(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904}
6905
6906/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04006907int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908{
6909 unsigned long tmp = 0;
6910
6911 switch (bits->width) {
6912 case 1: {
6913 u8 tmp8 = 0;
6914 pci_read_config_byte(pdev, bits->reg, &tmp8);
6915 tmp = tmp8;
6916 break;
6917 }
6918 case 2: {
6919 u16 tmp16 = 0;
6920 pci_read_config_word(pdev, bits->reg, &tmp16);
6921 tmp = tmp16;
6922 break;
6923 }
6924 case 4: {
6925 u32 tmp32 = 0;
6926 pci_read_config_dword(pdev, bits->reg, &tmp32);
6927 tmp = tmp32;
6928 break;
6929 }
6930
6931 default:
6932 return -EINVAL;
6933 }
6934
6935 tmp &= bits->mask;
6936
6937 return (tmp == bits->val) ? 1 : 0;
6938}
Jens Axboe9b847542006-01-06 09:28:07 +01006939
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006940#ifdef CONFIG_PM
Tejun Heo3c5100c2006-07-26 16:58:33 +09006941void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
Jens Axboe9b847542006-01-06 09:28:07 +01006942{
6943 pci_save_state(pdev);
Tejun Heo4c90d972007-02-20 18:14:48 +09006944 pci_disable_device(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006945
Tejun Heo4c90d972007-02-20 18:14:48 +09006946 if (mesg.event == PM_EVENT_SUSPEND)
Tejun Heo500530f2006-07-03 16:07:27 +09006947 pci_set_power_state(pdev, PCI_D3hot);
Jens Axboe9b847542006-01-06 09:28:07 +01006948}
6949
Tejun Heo553c4aa2006-12-26 19:39:50 +09006950int ata_pci_device_do_resume(struct pci_dev *pdev)
Jens Axboe9b847542006-01-06 09:28:07 +01006951{
Tejun Heo553c4aa2006-12-26 19:39:50 +09006952 int rc;
6953
Jens Axboe9b847542006-01-06 09:28:07 +01006954 pci_set_power_state(pdev, PCI_D0);
6955 pci_restore_state(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006956
Tejun Heob878ca52007-01-20 16:00:28 +09006957 rc = pcim_enable_device(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006958 if (rc) {
6959 dev_printk(KERN_ERR, &pdev->dev,
6960 "failed to enable device after resume (%d)\n", rc);
6961 return rc;
6962 }
6963
Jens Axboe9b847542006-01-06 09:28:07 +01006964 pci_set_master(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006965 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09006966}
6967
Tejun Heo3c5100c2006-07-26 16:58:33 +09006968int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006969{
Jeff Garzikcca39742006-08-24 03:19:22 -04006970 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo500530f2006-07-03 16:07:27 +09006971 int rc = 0;
6972
Jeff Garzikcca39742006-08-24 03:19:22 -04006973 rc = ata_host_suspend(host, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006974 if (rc)
6975 return rc;
6976
Tejun Heo3c5100c2006-07-26 16:58:33 +09006977 ata_pci_device_do_suspend(pdev, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006978
6979 return 0;
6980}
6981
6982int ata_pci_device_resume(struct pci_dev *pdev)
6983{
Jeff Garzikcca39742006-08-24 03:19:22 -04006984 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006985 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09006986
Tejun Heo553c4aa2006-12-26 19:39:50 +09006987 rc = ata_pci_device_do_resume(pdev);
6988 if (rc == 0)
6989 ata_host_resume(host);
6990 return rc;
Jens Axboe9b847542006-01-06 09:28:07 +01006991}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006992#endif /* CONFIG_PM */
6993
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994#endif /* CONFIG_PCI */
6995
6996
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997static int __init ata_init(void)
6998{
Andrew Mortona8601e52006-06-25 01:36:52 -07006999 ata_probe_timeout *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000 ata_wq = create_workqueue("ata");
7001 if (!ata_wq)
7002 return -ENOMEM;
7003
Tejun Heo453b07a2006-05-31 18:27:42 +09007004 ata_aux_wq = create_singlethread_workqueue("ata_aux");
7005 if (!ata_aux_wq) {
7006 destroy_workqueue(ata_wq);
7007 return -ENOMEM;
7008 }
7009
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
7011 return 0;
7012}
7013
7014static void __exit ata_exit(void)
7015{
7016 destroy_workqueue(ata_wq);
Tejun Heo453b07a2006-05-31 18:27:42 +09007017 destroy_workqueue(ata_aux_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018}
7019
Brian Kinga4625082006-11-13 16:32:36 -06007020subsys_initcall(ata_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021module_exit(ata_exit);
7022
Jeff Garzik67846b32005-10-05 02:58:32 -04007023static unsigned long ratelimit_time;
Ingo Molnar34af9462006-06-27 02:53:55 -07007024static DEFINE_SPINLOCK(ata_ratelimit_lock);
Jeff Garzik67846b32005-10-05 02:58:32 -04007025
7026int ata_ratelimit(void)
7027{
7028 int rc;
7029 unsigned long flags;
7030
7031 spin_lock_irqsave(&ata_ratelimit_lock, flags);
7032
7033 if (time_after(jiffies, ratelimit_time)) {
7034 rc = 1;
7035 ratelimit_time = jiffies + (HZ/5);
7036 } else
7037 rc = 0;
7038
7039 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
7040
7041 return rc;
7042}
7043
Tejun Heoc22daff2006-04-11 22:22:29 +09007044/**
7045 * ata_wait_register - wait until register value changes
7046 * @reg: IO-mapped register
7047 * @mask: Mask to apply to read register value
7048 * @val: Wait condition
7049 * @interval_msec: polling interval in milliseconds
7050 * @timeout_msec: timeout in milliseconds
7051 *
7052 * Waiting for some bits of register to change is a common
7053 * operation for ATA controllers. This function reads 32bit LE
7054 * IO-mapped register @reg and tests for the following condition.
7055 *
7056 * (*@reg & mask) != val
7057 *
7058 * If the condition is met, it returns; otherwise, the process is
7059 * repeated after @interval_msec until timeout.
7060 *
7061 * LOCKING:
7062 * Kernel thread context (may sleep)
7063 *
7064 * RETURNS:
7065 * The final register value.
7066 */
7067u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
7068 unsigned long interval_msec,
7069 unsigned long timeout_msec)
7070{
7071 unsigned long timeout;
7072 u32 tmp;
7073
7074 tmp = ioread32(reg);
7075
7076 /* Calculate timeout _after_ the first read to make sure
7077 * preceding writes reach the controller before starting to
7078 * eat away the timeout.
7079 */
7080 timeout = jiffies + (timeout_msec * HZ) / 1000;
7081
7082 while ((tmp & mask) == val && time_before(jiffies, timeout)) {
7083 msleep(interval_msec);
7084 tmp = ioread32(reg);
7085 }
7086
7087 return tmp;
7088}
7089
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090/*
Tejun Heodd5b06c2006-08-10 16:59:12 +09007091 * Dummy port_ops
7092 */
7093static void ata_dummy_noret(struct ata_port *ap) { }
7094static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
7095static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
7096
7097static u8 ata_dummy_check_status(struct ata_port *ap)
7098{
7099 return ATA_DRDY;
7100}
7101
7102static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
7103{
7104 return AC_ERR_SYSTEM;
7105}
7106
7107const struct ata_port_operations ata_dummy_port_ops = {
Tejun Heodd5b06c2006-08-10 16:59:12 +09007108 .check_status = ata_dummy_check_status,
7109 .check_altstatus = ata_dummy_check_status,
7110 .dev_select = ata_noop_dev_select,
7111 .qc_prep = ata_noop_qc_prep,
7112 .qc_issue = ata_dummy_qc_issue,
7113 .freeze = ata_dummy_noret,
7114 .thaw = ata_dummy_noret,
7115 .error_handler = ata_dummy_noret,
7116 .post_internal_cmd = ata_dummy_qc_noret,
7117 .irq_clear = ata_dummy_noret,
7118 .port_start = ata_dummy_ret0,
7119 .port_stop = ata_dummy_noret,
7120};
7121
Tejun Heo21b0ad42007-04-17 23:44:07 +09007122const struct ata_port_info ata_dummy_port_info = {
7123 .port_ops = &ata_dummy_port_ops,
7124};
7125
Tejun Heodd5b06c2006-08-10 16:59:12 +09007126/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127 * libata is essentially a library of internal helper functions for
7128 * low-level ATA host controller drivers. As such, the API/ABI is
7129 * likely to change as new drivers are added and updated.
7130 * Do not depend on ABI/API stability.
7131 */
7132
Tejun Heoe9c83912006-07-03 16:07:26 +09007133EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
7134EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
7135EXPORT_SYMBOL_GPL(sata_deb_timing_long);
Tejun Heodd5b06c2006-08-10 16:59:12 +09007136EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
Tejun Heo21b0ad42007-04-17 23:44:07 +09007137EXPORT_SYMBOL_GPL(ata_dummy_port_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138EXPORT_SYMBOL_GPL(ata_std_bios_param);
7139EXPORT_SYMBOL_GPL(ata_std_ports);
Jeff Garzikcca39742006-08-24 03:19:22 -04007140EXPORT_SYMBOL_GPL(ata_host_init);
Tejun Heof3187192007-04-17 23:44:07 +09007141EXPORT_SYMBOL_GPL(ata_host_alloc);
Tejun Heof5cda252007-04-17 23:44:07 +09007142EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
Tejun Heoecef7252007-04-17 23:44:06 +09007143EXPORT_SYMBOL_GPL(ata_host_start);
Tejun Heof3187192007-04-17 23:44:07 +09007144EXPORT_SYMBOL_GPL(ata_host_register);
Tejun Heof5cda252007-04-17 23:44:07 +09007145EXPORT_SYMBOL_GPL(ata_host_activate);
Tejun Heo0529c152007-01-20 16:00:26 +09007146EXPORT_SYMBOL_GPL(ata_host_detach);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147EXPORT_SYMBOL_GPL(ata_sg_init);
7148EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo9a1004d2006-05-31 18:27:52 +09007149EXPORT_SYMBOL_GPL(ata_hsm_move);
Tejun Heof686bcb2006-05-15 20:58:05 +09007150EXPORT_SYMBOL_GPL(ata_qc_complete);
Tejun Heodedaf2b2006-05-15 21:03:43 +09007151EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153EXPORT_SYMBOL_GPL(ata_tf_load);
7154EXPORT_SYMBOL_GPL(ata_tf_read);
7155EXPORT_SYMBOL_GPL(ata_noop_dev_select);
7156EXPORT_SYMBOL_GPL(ata_std_dev_select);
Jeff Garzik43727fb2007-02-25 16:50:52 -05007157EXPORT_SYMBOL_GPL(sata_print_link_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158EXPORT_SYMBOL_GPL(ata_tf_to_fis);
7159EXPORT_SYMBOL_GPL(ata_tf_from_fis);
7160EXPORT_SYMBOL_GPL(ata_check_status);
7161EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162EXPORT_SYMBOL_GPL(ata_exec_command);
7163EXPORT_SYMBOL_GPL(ata_port_start);
Alan Coxd92e74d2007-06-07 16:19:15 +01007164EXPORT_SYMBOL_GPL(ata_sff_port_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165EXPORT_SYMBOL_GPL(ata_interrupt);
Alan04351822007-03-06 02:37:52 -08007166EXPORT_SYMBOL_GPL(ata_do_set_mode);
Tejun Heo0d5ff562007-02-01 15:06:36 +09007167EXPORT_SYMBOL_GPL(ata_data_xfer);
7168EXPORT_SYMBOL_GPL(ata_data_xfer_noirq);
Tejun Heo31cc23b2007-09-23 13:14:12 +09007169EXPORT_SYMBOL_GPL(ata_std_qc_defer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170EXPORT_SYMBOL_GPL(ata_qc_prep);
Alan Coxd26fc952007-07-06 19:13:52 -04007171EXPORT_SYMBOL_GPL(ata_dumb_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06007172EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173EXPORT_SYMBOL_GPL(ata_bmdma_setup);
7174EXPORT_SYMBOL_GPL(ata_bmdma_start);
7175EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
7176EXPORT_SYMBOL_GPL(ata_bmdma_status);
7177EXPORT_SYMBOL_GPL(ata_bmdma_stop);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09007178EXPORT_SYMBOL_GPL(ata_bmdma_freeze);
7179EXPORT_SYMBOL_GPL(ata_bmdma_thaw);
7180EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh);
7181EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
7182EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183EXPORT_SYMBOL_GPL(ata_port_probe);
Alan10305f02007-02-20 18:01:59 +00007184EXPORT_SYMBOL_GPL(ata_dev_disable);
Tejun Heo3c567b72006-05-15 20:57:23 +09007185EXPORT_SYMBOL_GPL(sata_set_spd);
Tejun Heo936fd732007-08-06 18:36:23 +09007186EXPORT_SYMBOL_GPL(sata_link_debounce);
7187EXPORT_SYMBOL_GPL(sata_link_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188EXPORT_SYMBOL_GPL(sata_phy_reset);
7189EXPORT_SYMBOL_GPL(__sata_phy_reset);
7190EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heof5914a42006-05-31 18:27:48 +09007191EXPORT_SYMBOL_GPL(ata_std_prereset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09007192EXPORT_SYMBOL_GPL(ata_std_softreset);
Tejun Heocc0680a2007-08-06 18:36:23 +09007193EXPORT_SYMBOL_GPL(sata_link_hardreset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09007194EXPORT_SYMBOL_GPL(sata_std_hardreset);
7195EXPORT_SYMBOL_GPL(ata_std_postreset);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05007196EXPORT_SYMBOL_GPL(ata_dev_classify);
7197EXPORT_SYMBOL_GPL(ata_dev_pair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04007199EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heoc22daff2006-04-11 22:22:29 +09007200EXPORT_SYMBOL_GPL(ata_wait_register);
Tejun Heo6f8b9952006-01-24 17:05:21 +09007201EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heod4b2bab2007-02-02 16:50:52 +09007202EXPORT_SYMBOL_GPL(ata_wait_ready);
Tejun Heo86e45b62006-03-05 15:29:09 +09007203EXPORT_SYMBOL_GPL(ata_port_queue_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
7205EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
Tejun Heo83c47bc2006-05-31 18:28:07 +09007207EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09007208EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209EXPORT_SYMBOL_GPL(ata_host_intr);
Tejun Heo34bf2172006-05-15 20:57:46 +09007210EXPORT_SYMBOL_GPL(sata_scr_valid);
7211EXPORT_SYMBOL_GPL(sata_scr_read);
7212EXPORT_SYMBOL_GPL(sata_scr_write);
7213EXPORT_SYMBOL_GPL(sata_scr_write_flush);
Tejun Heo936fd732007-08-06 18:36:23 +09007214EXPORT_SYMBOL_GPL(ata_link_online);
7215EXPORT_SYMBOL_GPL(ata_link_offline);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007216#ifdef CONFIG_PM
Jeff Garzikcca39742006-08-24 03:19:22 -04007217EXPORT_SYMBOL_GPL(ata_host_suspend);
7218EXPORT_SYMBOL_GPL(ata_host_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007219#endif /* CONFIG_PM */
Tejun Heo6a62a042006-02-13 10:02:46 +09007220EXPORT_SYMBOL_GPL(ata_id_string);
7221EXPORT_SYMBOL_GPL(ata_id_c_string);
Alan10305f02007-02-20 18:01:59 +00007222EXPORT_SYMBOL_GPL(ata_id_to_dma_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223EXPORT_SYMBOL_GPL(ata_scsi_simulate);
7224
Alan Cox1bc4ccf2006-01-09 17:18:14 +00007225EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04007226EXPORT_SYMBOL_GPL(ata_timing_compute);
7227EXPORT_SYMBOL_GPL(ata_timing_merge);
7228
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229#ifdef CONFIG_PCI
7230EXPORT_SYMBOL_GPL(pci_test_config_bits);
Tejun Heod583bc12007-07-04 18:02:07 +09007231EXPORT_SYMBOL_GPL(ata_pci_init_sff_host);
Tejun Heo1626aeb2007-05-04 12:43:58 +02007232EXPORT_SYMBOL_GPL(ata_pci_init_bmdma);
Tejun Heod583bc12007-07-04 18:02:07 +09007233EXPORT_SYMBOL_GPL(ata_pci_prepare_sff_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234EXPORT_SYMBOL_GPL(ata_pci_init_one);
7235EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007236#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09007237EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
7238EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
Jens Axboe9b847542006-01-06 09:28:07 +01007239EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
7240EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007241#endif /* CONFIG_PM */
Alan Cox67951ad2006-03-22 15:55:54 +00007242EXPORT_SYMBOL_GPL(ata_pci_default_filter);
7243EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01007245
Tejun Heob64bbc32007-07-16 14:29:39 +09007246EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
7247EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
7248EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
Tejun Heocbcdd872007-08-18 13:14:55 +09007249EXPORT_SYMBOL_GPL(ata_port_desc);
7250#ifdef CONFIG_PCI
7251EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
7252#endif /* CONFIG_PCI */
Tejun Heoece1d632006-04-02 18:51:53 +09007253EXPORT_SYMBOL_GPL(ata_eng_timeout);
Tejun Heo7b70fc02006-05-15 20:58:07 +09007254EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
Tejun Heodbd82612007-08-06 18:36:23 +09007255EXPORT_SYMBOL_GPL(ata_link_abort);
Tejun Heo7b70fc02006-05-15 20:58:07 +09007256EXPORT_SYMBOL_GPL(ata_port_abort);
Tejun Heoe3180492006-05-15 20:58:09 +09007257EXPORT_SYMBOL_GPL(ata_port_freeze);
Tejun Heo7d77b242007-09-23 13:14:13 +09007258EXPORT_SYMBOL_GPL(sata_async_notification);
Tejun Heoe3180492006-05-15 20:58:09 +09007259EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
7260EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
Tejun Heoece1d632006-04-02 18:51:53 +09007261EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
7262EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Tejun Heo022bdb02006-05-15 20:58:22 +09007263EXPORT_SYMBOL_GPL(ata_do_eh);
Akira Iguchi83625002007-01-26 16:27:32 +09007264EXPORT_SYMBOL_GPL(ata_irq_on);
Akira Iguchia619f981b2007-01-26 16:28:18 +09007265EXPORT_SYMBOL_GPL(ata_dev_try_classify);
Alan Coxbe0d18d2007-03-06 02:37:56 -08007266
7267EXPORT_SYMBOL_GPL(ata_cable_40wire);
7268EXPORT_SYMBOL_GPL(ata_cable_80wire);
7269EXPORT_SYMBOL_GPL(ata_cable_unknown);
7270EXPORT_SYMBOL_GPL(ata_cable_sata);