blob: 0a5103b707c686f33eb2f15c2fb8129a6fcdd5ff [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
Tejun Heod7bb4cc2006-05-31 18:27:46 +090062/* debounce timing parameters in msecs { interval, duration, timeout } */
Tejun Heoe9c83912006-07-03 16:07:26 +090063const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
64const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
65const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
Tejun Heod7bb4cc2006-05-31 18:27:46 +090066
Tejun Heo3373efd2006-05-15 20:57:53 +090067static unsigned int ata_dev_init_params(struct ata_device *dev,
68 u16 heads, u16 sectors);
69static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
70static void ata_dev_xfermask(struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72static unsigned int ata_unique_id = 1;
73static struct workqueue_struct *ata_wq;
74
Tejun Heo453b07a2006-05-31 18:27:42 +090075struct workqueue_struct *ata_aux_wq;
76
Jeff Garzik418dc1f2006-03-11 20:50:08 -050077int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -040078module_param(atapi_enabled, int, 0444);
79MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
80
Albert Lee95de7192006-04-04 10:57:18 +080081int atapi_dmadir = 0;
82module_param(atapi_dmadir, int, 0444);
83MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
84
Jeff Garzikc3c013a2006-02-27 22:31:19 -050085int libata_fua = 0;
86module_param_named(fua, libata_fua, int, 0444);
87MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
88
Andrew Mortona8601e52006-06-25 01:36:52 -070089static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
90module_param(ata_probe_timeout, int, 0444);
91MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093MODULE_AUTHOR("Jeff Garzik");
94MODULE_DESCRIPTION("Library module for ATA devices");
95MODULE_LICENSE("GPL");
96MODULE_VERSION(DRV_VERSION);
97
Edward Falk0baab862005-06-02 18:17:13 -040098
99/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
101 * @tf: Taskfile to convert
102 * @fis: Buffer into which data will output
103 * @pmp: Port multiplier port
104 *
105 * Converts a standard ATA taskfile to a Serial ATA
106 * FIS structure (Register - Host to Device).
107 *
108 * LOCKING:
109 * Inherited from caller.
110 */
111
Jeff Garzik057ace52005-10-22 14:27:05 -0400112void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 fis[0] = 0x27; /* Register - Host to Device FIS */
115 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
116 bit 7 indicates Command FIS */
117 fis[2] = tf->command;
118 fis[3] = tf->feature;
119
120 fis[4] = tf->lbal;
121 fis[5] = tf->lbam;
122 fis[6] = tf->lbah;
123 fis[7] = tf->device;
124
125 fis[8] = tf->hob_lbal;
126 fis[9] = tf->hob_lbam;
127 fis[10] = tf->hob_lbah;
128 fis[11] = tf->hob_feature;
129
130 fis[12] = tf->nsect;
131 fis[13] = tf->hob_nsect;
132 fis[14] = 0;
133 fis[15] = tf->ctl;
134
135 fis[16] = 0;
136 fis[17] = 0;
137 fis[18] = 0;
138 fis[19] = 0;
139}
140
141/**
142 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
143 * @fis: Buffer from which data will be input
144 * @tf: Taskfile to output
145 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500146 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 *
148 * LOCKING:
149 * Inherited from caller.
150 */
151
Jeff Garzik057ace52005-10-22 14:27:05 -0400152void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 tf->command = fis[2]; /* status */
155 tf->feature = fis[3]; /* error */
156
157 tf->lbal = fis[4];
158 tf->lbam = fis[5];
159 tf->lbah = fis[6];
160 tf->device = fis[7];
161
162 tf->hob_lbal = fis[8];
163 tf->hob_lbam = fis[9];
164 tf->hob_lbah = fis[10];
165
166 tf->nsect = fis[12];
167 tf->hob_nsect = fis[13];
168}
169
Albert Lee8cbd6df2005-10-12 15:06:27 +0800170static const u8 ata_rw_cmds[] = {
171 /* pio multi */
172 ATA_CMD_READ_MULTI,
173 ATA_CMD_WRITE_MULTI,
174 ATA_CMD_READ_MULTI_EXT,
175 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100176 0,
177 0,
178 0,
179 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800180 /* pio */
181 ATA_CMD_PIO_READ,
182 ATA_CMD_PIO_WRITE,
183 ATA_CMD_PIO_READ_EXT,
184 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100185 0,
186 0,
187 0,
188 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800189 /* dma */
190 ATA_CMD_READ,
191 ATA_CMD_WRITE,
192 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100193 ATA_CMD_WRITE_EXT,
194 0,
195 0,
196 0,
197 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800198};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800201 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
202 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500204 * Examine the device configuration and tf->flags to calculate
Albert Lee8cbd6df2005-10-12 15:06:27 +0800205 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 *
207 * LOCKING:
208 * caller.
209 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100210int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800212 struct ata_taskfile *tf = &qc->tf;
213 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100214 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100216 int index, fua, lba48, write;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500217
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100218 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800219 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
220 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Albert Lee8cbd6df2005-10-12 15:06:27 +0800222 if (dev->flags & ATA_DFLAG_PIO) {
223 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100224 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000225 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
226 /* Unable to use DMA due to host limitation */
227 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800228 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800229 } else {
230 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100231 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100234 cmd = ata_rw_cmds[index + fua + lba48 + write];
235 if (cmd) {
236 tf->command = cmd;
237 return 0;
238 }
239 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Tejun Heocb95d562006-03-06 04:31:56 +0900242/**
Tejun Heo35b649f2006-11-14 22:37:35 +0900243 * ata_tf_read_block - Read block address from ATA taskfile
244 * @tf: ATA taskfile of interest
245 * @dev: ATA device @tf belongs to
246 *
247 * LOCKING:
248 * None.
249 *
250 * Read block address from @tf. This function can handle all
251 * three address formats - LBA, LBA48 and CHS. tf->protocol and
252 * flags select the address format to use.
253 *
254 * RETURNS:
255 * Block address read from @tf.
256 */
257u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
258{
259 u64 block = 0;
260
261 if (tf->flags & ATA_TFLAG_LBA) {
262 if (tf->flags & ATA_TFLAG_LBA48) {
263 block |= (u64)tf->hob_lbah << 40;
264 block |= (u64)tf->hob_lbam << 32;
265 block |= tf->hob_lbal << 24;
266 } else
267 block |= (tf->device & 0xf) << 24;
268
269 block |= tf->lbah << 16;
270 block |= tf->lbam << 8;
271 block |= tf->lbal;
272 } else {
273 u32 cyl, head, sect;
274
275 cyl = tf->lbam | (tf->lbah << 8);
276 head = tf->device & 0xf;
277 sect = tf->lbal;
278
279 block = (cyl * dev->heads + head) * dev->sectors + sect;
280 }
281
282 return block;
283}
284
285/**
Tejun Heocb95d562006-03-06 04:31:56 +0900286 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
287 * @pio_mask: pio_mask
288 * @mwdma_mask: mwdma_mask
289 * @udma_mask: udma_mask
290 *
291 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
292 * unsigned int xfer_mask.
293 *
294 * LOCKING:
295 * None.
296 *
297 * RETURNS:
298 * Packed xfer_mask.
299 */
300static unsigned int ata_pack_xfermask(unsigned int pio_mask,
301 unsigned int mwdma_mask,
302 unsigned int udma_mask)
303{
304 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
305 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
306 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
307}
308
Tejun Heoc0489e42006-03-24 14:07:49 +0900309/**
310 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
311 * @xfer_mask: xfer_mask to unpack
312 * @pio_mask: resulting pio_mask
313 * @mwdma_mask: resulting mwdma_mask
314 * @udma_mask: resulting udma_mask
315 *
316 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
317 * Any NULL distination masks will be ignored.
318 */
319static void ata_unpack_xfermask(unsigned int xfer_mask,
320 unsigned int *pio_mask,
321 unsigned int *mwdma_mask,
322 unsigned int *udma_mask)
323{
324 if (pio_mask)
325 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
326 if (mwdma_mask)
327 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
328 if (udma_mask)
329 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
330}
331
Tejun Heocb95d562006-03-06 04:31:56 +0900332static const struct ata_xfer_ent {
Tejun Heobe9a50c82006-03-31 22:48:52 +0900333 int shift, bits;
Tejun Heocb95d562006-03-06 04:31:56 +0900334 u8 base;
335} ata_xfer_tbl[] = {
336 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
337 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
338 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
339 { -1, },
340};
341
342/**
343 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
344 * @xfer_mask: xfer_mask of interest
345 *
346 * Return matching XFER_* value for @xfer_mask. Only the highest
347 * bit of @xfer_mask is considered.
348 *
349 * LOCKING:
350 * None.
351 *
352 * RETURNS:
353 * Matching XFER_* value, 0 if no match found.
354 */
355static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
356{
357 int highbit = fls(xfer_mask) - 1;
358 const struct ata_xfer_ent *ent;
359
360 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
361 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
362 return ent->base + highbit - ent->shift;
363 return 0;
364}
365
366/**
367 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
368 * @xfer_mode: XFER_* of interest
369 *
370 * Return matching xfer_mask for @xfer_mode.
371 *
372 * LOCKING:
373 * None.
374 *
375 * RETURNS:
376 * Matching xfer_mask, 0 if no match found.
377 */
378static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
379{
380 const struct ata_xfer_ent *ent;
381
382 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
383 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
384 return 1 << (ent->shift + xfer_mode - ent->base);
385 return 0;
386}
387
388/**
389 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
390 * @xfer_mode: XFER_* of interest
391 *
392 * Return matching xfer_shift for @xfer_mode.
393 *
394 * LOCKING:
395 * None.
396 *
397 * RETURNS:
398 * Matching xfer_shift, -1 if no match found.
399 */
400static int ata_xfer_mode2shift(unsigned int xfer_mode)
401{
402 const struct ata_xfer_ent *ent;
403
404 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
405 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
406 return ent->shift;
407 return -1;
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900411 * ata_mode_string - convert xfer_mask to string
412 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 *
414 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900415 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 *
417 * LOCKING:
418 * None.
419 *
420 * RETURNS:
421 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900422 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 */
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900424static const char *ata_mode_string(unsigned int xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Tejun Heo75f554b2006-03-06 04:31:57 +0900426 static const char * const xfer_mode_str[] = {
427 "PIO0",
428 "PIO1",
429 "PIO2",
430 "PIO3",
431 "PIO4",
Alan Coxb352e572006-08-10 18:52:12 +0100432 "PIO5",
433 "PIO6",
Tejun Heo75f554b2006-03-06 04:31:57 +0900434 "MWDMA0",
435 "MWDMA1",
436 "MWDMA2",
Alan Coxb352e572006-08-10 18:52:12 +0100437 "MWDMA3",
438 "MWDMA4",
Tejun Heo75f554b2006-03-06 04:31:57 +0900439 "UDMA/16",
440 "UDMA/25",
441 "UDMA/33",
442 "UDMA/44",
443 "UDMA/66",
444 "UDMA/100",
445 "UDMA/133",
446 "UDMA7",
447 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900448 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900450 highbit = fls(xfer_mask) - 1;
451 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
452 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Tejun Heo4c360c82006-04-01 01:38:17 +0900456static const char *sata_spd_string(unsigned int spd)
457{
458 static const char * const spd_str[] = {
459 "1.5 Gbps",
460 "3.0 Gbps",
461 };
462
463 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
464 return "<unknown>";
465 return spd_str[spd - 1];
466}
467
Tejun Heo3373efd2006-05-15 20:57:53 +0900468void ata_dev_disable(struct ata_device *dev)
Tejun Heo0b8efb02006-03-24 15:25:31 +0900469{
Borislav Petkov0dd4b212006-06-23 02:29:08 -0400470 if (ata_dev_enabled(dev) && ata_msg_drv(dev->ap)) {
Tejun Heof15a1da2006-05-15 20:57:56 +0900471 ata_dev_printk(dev, KERN_WARNING, "disabled\n");
Tejun Heo0b8efb02006-03-24 15:25:31 +0900472 dev->class++;
473 }
474}
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476/**
477 * ata_pio_devchk - PATA device presence detection
478 * @ap: ATA channel to examine
479 * @device: Device to examine (starting at zero)
480 *
481 * This technique was originally described in
482 * Hale Landis's ATADRVR (www.ata-atapi.com), and
483 * later found its way into the ATA/ATAPI spec.
484 *
485 * Write a pattern to the ATA shadow registers,
486 * and if a device is present, it will respond by
487 * correctly storing and echoing back the
488 * ATA shadow register contents.
489 *
490 * LOCKING:
491 * caller.
492 */
493
494static unsigned int ata_pio_devchk(struct ata_port *ap,
495 unsigned int device)
496{
497 struct ata_ioports *ioaddr = &ap->ioaddr;
498 u8 nsect, lbal;
499
500 ap->ops->dev_select(ap, device);
501
502 outb(0x55, ioaddr->nsect_addr);
503 outb(0xaa, ioaddr->lbal_addr);
504
505 outb(0xaa, ioaddr->nsect_addr);
506 outb(0x55, ioaddr->lbal_addr);
507
508 outb(0x55, ioaddr->nsect_addr);
509 outb(0xaa, ioaddr->lbal_addr);
510
511 nsect = inb(ioaddr->nsect_addr);
512 lbal = inb(ioaddr->lbal_addr);
513
514 if ((nsect == 0x55) && (lbal == 0xaa))
515 return 1; /* we found a device */
516
517 return 0; /* nothing found */
518}
519
520/**
521 * ata_mmio_devchk - PATA device presence detection
522 * @ap: ATA channel to examine
523 * @device: Device to examine (starting at zero)
524 *
525 * This technique was originally described in
526 * Hale Landis's ATADRVR (www.ata-atapi.com), and
527 * later found its way into the ATA/ATAPI spec.
528 *
529 * Write a pattern to the ATA shadow registers,
530 * and if a device is present, it will respond by
531 * correctly storing and echoing back the
532 * ATA shadow register contents.
533 *
534 * LOCKING:
535 * caller.
536 */
537
538static unsigned int ata_mmio_devchk(struct ata_port *ap,
539 unsigned int device)
540{
541 struct ata_ioports *ioaddr = &ap->ioaddr;
542 u8 nsect, lbal;
543
544 ap->ops->dev_select(ap, device);
545
546 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
547 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
548
549 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
550 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
551
552 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
553 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
554
555 nsect = readb((void __iomem *) ioaddr->nsect_addr);
556 lbal = readb((void __iomem *) ioaddr->lbal_addr);
557
558 if ((nsect == 0x55) && (lbal == 0xaa))
559 return 1; /* we found a device */
560
561 return 0; /* nothing found */
562}
563
564/**
565 * ata_devchk - PATA device presence detection
566 * @ap: ATA channel to examine
567 * @device: Device to examine (starting at zero)
568 *
569 * Dispatch ATA device presence detection, depending
570 * on whether we are using PIO or MMIO to talk to the
571 * ATA shadow registers.
572 *
573 * LOCKING:
574 * caller.
575 */
576
577static unsigned int ata_devchk(struct ata_port *ap,
578 unsigned int device)
579{
580 if (ap->flags & ATA_FLAG_MMIO)
581 return ata_mmio_devchk(ap, device);
582 return ata_pio_devchk(ap, device);
583}
584
585/**
586 * ata_dev_classify - determine device type based on ATA-spec signature
587 * @tf: ATA taskfile register set for device to be identified
588 *
589 * Determine from taskfile register contents whether a device is
590 * ATA or ATAPI, as per "Signature and persistence" section
591 * of ATA/PI spec (volume 1, sect 5.14).
592 *
593 * LOCKING:
594 * None.
595 *
596 * RETURNS:
597 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
598 * the event of failure.
599 */
600
Jeff Garzik057ace52005-10-22 14:27:05 -0400601unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 /* Apple's open source Darwin code hints that some devices only
604 * put a proper signature into the LBA mid/high registers,
605 * So, we only check those. It's sufficient for uniqueness.
606 */
607
608 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
609 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
610 DPRINTK("found ATA device by sig\n");
611 return ATA_DEV_ATA;
612 }
613
614 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
615 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
616 DPRINTK("found ATAPI device by sig\n");
617 return ATA_DEV_ATAPI;
618 }
619
620 DPRINTK("unknown device\n");
621 return ATA_DEV_UNKNOWN;
622}
623
624/**
625 * ata_dev_try_classify - Parse returned ATA device signature
626 * @ap: ATA channel to examine
627 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900628 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 *
630 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
631 * an ATA/ATAPI-defined set of values is placed in the ATA
632 * shadow registers, indicating the results of device detection
633 * and diagnostics.
634 *
635 * Select the ATA device, and read the values from the ATA shadow
636 * registers. Then parse according to the Error register value,
637 * and the spec-defined values examined by ata_dev_classify().
638 *
639 * LOCKING:
640 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900641 *
642 * RETURNS:
643 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 */
645
Tejun Heob4dc7622006-01-24 17:05:22 +0900646static unsigned int
647ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct ata_taskfile tf;
650 unsigned int class;
651 u8 err;
652
653 ap->ops->dev_select(ap, device);
654
655 memset(&tf, 0, sizeof(tf));
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400658 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900659 if (r_err)
660 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Alan Cox93590852006-09-12 16:55:12 +0100662 /* see if device passed diags: if master then continue and warn later */
663 if (err == 0 && device == 0)
664 /* diagnostic fail : do nothing _YET_ */
665 ap->device[device].horkage |= ATA_HORKAGE_DIAGNOSTIC;
666 else if (err == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /* do nothing */ ;
668 else if ((device == 0) && (err == 0x81))
669 /* do nothing */ ;
670 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900671 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Tejun Heob4dc7622006-01-24 17:05:22 +0900673 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900677 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900679 return ATA_DEV_NONE;
680 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
683/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900684 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 * @id: IDENTIFY DEVICE results we will examine
686 * @s: string into which data is output
687 * @ofs: offset into identify device page
688 * @len: length of string to return. must be an even number.
689 *
690 * The strings in the IDENTIFY DEVICE page are broken up into
691 * 16-bit chunks. Run through the string, and output each
692 * 8-bit chunk linearly, regardless of platform.
693 *
694 * LOCKING:
695 * caller.
696 */
697
Tejun Heo6a62a042006-02-13 10:02:46 +0900698void ata_id_string(const u16 *id, unsigned char *s,
699 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 unsigned int c;
702
703 while (len > 0) {
704 c = id[ofs] >> 8;
705 *s = c;
706 s++;
707
708 c = id[ofs] & 0xff;
709 *s = c;
710 s++;
711
712 ofs++;
713 len -= 2;
714 }
715}
716
Tejun Heo0e949ff2006-02-12 22:47:04 +0900717/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900718 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900719 * @id: IDENTIFY DEVICE results we will examine
720 * @s: string into which data is output
721 * @ofs: offset into identify device page
722 * @len: length of string to return. must be an odd number.
723 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900724 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900725 * trims trailing spaces and terminates the resulting string with
726 * null. @len must be actual maximum length (even number) + 1.
727 *
728 * LOCKING:
729 * caller.
730 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900731void ata_id_c_string(const u16 *id, unsigned char *s,
732 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900733{
734 unsigned char *p;
735
736 WARN_ON(!(len & 1));
737
Tejun Heo6a62a042006-02-13 10:02:46 +0900738 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900739
740 p = s + strnlen(s, len - 1);
741 while (p > s && p[-1] == ' ')
742 p--;
743 *p = '\0';
744}
Edward Falk0baab862005-06-02 18:17:13 -0400745
Tejun Heo29407402006-02-12 22:47:04 +0900746static u64 ata_id_n_sectors(const u16 *id)
747{
748 if (ata_id_has_lba(id)) {
749 if (ata_id_has_lba48(id))
750 return ata_id_u64(id, 100);
751 else
752 return ata_id_u32(id, 60);
753 } else {
754 if (ata_id_current_chs_valid(id))
755 return ata_id_u32(id, 57);
756 else
757 return id[1] * id[3] * id[6];
758 }
759}
760
Edward Falk0baab862005-06-02 18:17:13 -0400761/**
762 * ata_noop_dev_select - Select device 0/1 on ATA bus
763 * @ap: ATA channel to manipulate
764 * @device: ATA device (numbered from zero) to select
765 *
766 * This function performs no actual function.
767 *
768 * May be used as the dev_select() entry in ata_port_operations.
769 *
770 * LOCKING:
771 * caller.
772 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
774{
775}
776
Edward Falk0baab862005-06-02 18:17:13 -0400777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778/**
779 * ata_std_dev_select - Select device 0/1 on ATA bus
780 * @ap: ATA channel to manipulate
781 * @device: ATA device (numbered from zero) to select
782 *
783 * Use the method defined in the ATA specification to
784 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400785 * ATA channel. Works with both PIO and MMIO.
786 *
787 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 *
789 * LOCKING:
790 * caller.
791 */
792
793void ata_std_dev_select (struct ata_port *ap, unsigned int device)
794{
795 u8 tmp;
796
797 if (device == 0)
798 tmp = ATA_DEVICE_OBS;
799 else
800 tmp = ATA_DEVICE_OBS | ATA_DEV1;
801
802 if (ap->flags & ATA_FLAG_MMIO) {
803 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
804 } else {
805 outb(tmp, ap->ioaddr.device_addr);
806 }
807 ata_pause(ap); /* needed; also flushes, for mmio */
808}
809
810/**
811 * ata_dev_select - Select device 0/1 on ATA bus
812 * @ap: ATA channel to manipulate
813 * @device: ATA device (numbered from zero) to select
814 * @wait: non-zero to wait for Status register BSY bit to clear
815 * @can_sleep: non-zero if context allows sleeping
816 *
817 * Use the method defined in the ATA specification to
818 * make either device 0, or device 1, active on the
819 * ATA channel.
820 *
821 * This is a high-level version of ata_std_dev_select(),
822 * which additionally provides the services of inserting
823 * the proper pauses and status polling, where needed.
824 *
825 * LOCKING:
826 * caller.
827 */
828
829void ata_dev_select(struct ata_port *ap, unsigned int device,
830 unsigned int wait, unsigned int can_sleep)
831{
Tejun Heo88574552006-06-25 20:00:35 +0900832 if (ata_msg_probe(ap))
Borislav Petkov0dd4b212006-06-23 02:29:08 -0400833 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, ata%u: "
Tejun Heo88574552006-06-25 20:00:35 +0900834 "device %u, wait %u\n", ap->id, device, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 if (wait)
837 ata_wait_idle(ap);
838
839 ap->ops->dev_select(ap, device);
840
841 if (wait) {
842 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
843 msleep(150);
844 ata_wait_idle(ap);
845 }
846}
847
848/**
849 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +0900850 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 *
Tejun Heo0bd33002006-02-12 22:47:05 +0900852 * Dump selected 16-bit words from the given IDENTIFY DEVICE
853 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 *
855 * LOCKING:
856 * caller.
857 */
858
Tejun Heo0bd33002006-02-12 22:47:05 +0900859static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 DPRINTK("49==0x%04x "
862 "53==0x%04x "
863 "63==0x%04x "
864 "64==0x%04x "
865 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900866 id[49],
867 id[53],
868 id[63],
869 id[64],
870 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 DPRINTK("80==0x%04x "
872 "81==0x%04x "
873 "82==0x%04x "
874 "83==0x%04x "
875 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900876 id[80],
877 id[81],
878 id[82],
879 id[83],
880 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 DPRINTK("88==0x%04x "
882 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900883 id[88],
884 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
Tejun Heocb95d562006-03-06 04:31:56 +0900887/**
888 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
889 * @id: IDENTIFY data to compute xfer mask from
890 *
891 * Compute the xfermask for this device. This is not as trivial
892 * as it seems if we must consider early devices correctly.
893 *
894 * FIXME: pre IDE drive timing (do we care ?).
895 *
896 * LOCKING:
897 * None.
898 *
899 * RETURNS:
900 * Computed xfermask
901 */
902static unsigned int ata_id_xfermask(const u16 *id)
903{
904 unsigned int pio_mask, mwdma_mask, udma_mask;
905
906 /* Usual case. Word 53 indicates word 64 is valid */
907 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
908 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
909 pio_mask <<= 3;
910 pio_mask |= 0x7;
911 } else {
912 /* If word 64 isn't valid then Word 51 high byte holds
913 * the PIO timing number for the maximum. Turn it into
914 * a mask.
915 */
Alan Cox46767aeb2006-09-29 18:26:47 +0100916 u8 mode = id[ATA_ID_OLD_PIO_MODES] & 0xFF;
917 if (mode < 5) /* Valid PIO range */
918 pio_mask = (2 << mode) - 1;
919 else
920 pio_mask = 1;
Tejun Heocb95d562006-03-06 04:31:56 +0900921
922 /* But wait.. there's more. Design your standards by
923 * committee and you too can get a free iordy field to
924 * process. However its the speeds not the modes that
925 * are supported... Note drivers using the timing API
926 * will get this right anyway
927 */
928 }
929
930 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +0900931
Alan Coxb352e572006-08-10 18:52:12 +0100932 if (ata_id_is_cfa(id)) {
933 /*
934 * Process compact flash extended modes
935 */
936 int pio = id[163] & 0x7;
937 int dma = (id[163] >> 3) & 7;
938
939 if (pio)
940 pio_mask |= (1 << 5);
941 if (pio > 1)
942 pio_mask |= (1 << 6);
943 if (dma)
944 mwdma_mask |= (1 << 3);
945 if (dma > 1)
946 mwdma_mask |= (1 << 4);
947 }
948
Tejun Heofb21f0d2006-03-12 12:34:35 +0900949 udma_mask = 0;
950 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
951 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +0900952
953 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
954}
955
Tejun Heo86e45b62006-03-05 15:29:09 +0900956/**
957 * ata_port_queue_task - Queue port_task
958 * @ap: The ata_port to queue port_task for
Randy Dunlape2a7f772006-05-18 10:50:18 -0700959 * @fn: workqueue function to be scheduled
960 * @data: data value to pass to workqueue function
961 * @delay: delay time for workqueue function
Tejun Heo86e45b62006-03-05 15:29:09 +0900962 *
963 * Schedule @fn(@data) for execution after @delay jiffies using
964 * port_task. There is one port_task per port and it's the
965 * user(low level driver)'s responsibility to make sure that only
966 * one task is active at any given time.
967 *
968 * libata core layer takes care of synchronization between
969 * port_task and EH. ata_port_queue_task() may be ignored for EH
970 * synchronization.
971 *
972 * LOCKING:
973 * Inherited from caller.
974 */
975void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
976 unsigned long delay)
977{
978 int rc;
979
Tejun Heob51e9e52006-06-29 01:29:30 +0900980 if (ap->pflags & ATA_PFLAG_FLUSH_PORT_TASK)
Tejun Heo86e45b62006-03-05 15:29:09 +0900981 return;
982
983 PREPARE_WORK(&ap->port_task, fn, data);
984
985 if (!delay)
986 rc = queue_work(ata_wq, &ap->port_task);
987 else
988 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
989
990 /* rc == 0 means that another user is using port task */
991 WARN_ON(rc == 0);
992}
993
994/**
995 * ata_port_flush_task - Flush port_task
996 * @ap: The ata_port to flush port_task for
997 *
998 * After this function completes, port_task is guranteed not to
999 * be running or scheduled.
1000 *
1001 * LOCKING:
1002 * Kernel thread context (may sleep)
1003 */
1004void ata_port_flush_task(struct ata_port *ap)
1005{
1006 unsigned long flags;
1007
1008 DPRINTK("ENTER\n");
1009
Jeff Garzikba6a1302006-06-22 23:46:10 -04001010 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09001011 ap->pflags |= ATA_PFLAG_FLUSH_PORT_TASK;
Jeff Garzikba6a1302006-06-22 23:46:10 -04001012 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo86e45b62006-03-05 15:29:09 +09001013
1014 DPRINTK("flush #1\n");
1015 flush_workqueue(ata_wq);
1016
1017 /*
1018 * At this point, if a task is running, it's guaranteed to see
1019 * the FLUSH flag; thus, it will never queue pio tasks again.
1020 * Cancel and flush.
1021 */
1022 if (!cancel_delayed_work(&ap->port_task)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001023 if (ata_msg_ctl(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001024 ata_port_printk(ap, KERN_DEBUG, "%s: flush #2\n",
1025 __FUNCTION__);
Tejun Heo86e45b62006-03-05 15:29:09 +09001026 flush_workqueue(ata_wq);
1027 }
1028
Jeff Garzikba6a1302006-06-22 23:46:10 -04001029 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09001030 ap->pflags &= ~ATA_PFLAG_FLUSH_PORT_TASK;
Jeff Garzikba6a1302006-06-22 23:46:10 -04001031 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo86e45b62006-03-05 15:29:09 +09001032
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001033 if (ata_msg_ctl(ap))
1034 ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __FUNCTION__);
Tejun Heo86e45b62006-03-05 15:29:09 +09001035}
1036
Tejun Heo77853bf2006-01-23 13:09:36 +09001037void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001038{
Tejun Heo77853bf2006-01-23 13:09:36 +09001039 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001040
Tejun Heoa2a7a662005-12-13 14:48:31 +09001041 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001042}
1043
1044/**
Tejun Heo24326972006-11-14 22:47:09 +09001045 * ata_exec_internal_sg - execute libata internal command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001046 * @dev: Device to which the command is sent
1047 * @tf: Taskfile registers for the command and the result
Tejun Heod69cf372006-04-02 18:51:53 +09001048 * @cdb: CDB for packet command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001049 * @dma_dir: Data tranfer direction of the command
Tejun Heo24326972006-11-14 22:47:09 +09001050 * @sg: sg list for the data buffer of the command
1051 * @n_elem: Number of sg entries
Tejun Heoa2a7a662005-12-13 14:48:31 +09001052 *
1053 * Executes libata internal command with timeout. @tf contains
1054 * command on entry and result on return. Timeout and error
1055 * conditions are reported via return value. No recovery action
1056 * is taken after a command times out. It's caller's duty to
1057 * clean up after timeout.
1058 *
1059 * LOCKING:
1060 * None. Should be called with kernel context, might sleep.
Tejun Heo551e8882006-06-12 14:09:49 +09001061 *
1062 * RETURNS:
1063 * Zero on success, AC_ERR_* mask on failure
Tejun Heoa2a7a662005-12-13 14:48:31 +09001064 */
Tejun Heo24326972006-11-14 22:47:09 +09001065unsigned ata_exec_internal_sg(struct ata_device *dev,
1066 struct ata_taskfile *tf, const u8 *cdb,
1067 int dma_dir, struct scatterlist *sg,
1068 unsigned int n_elem)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001069{
Tejun Heo3373efd2006-05-15 20:57:53 +09001070 struct ata_port *ap = dev->ap;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001071 u8 command = tf->command;
1072 struct ata_queued_cmd *qc;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001073 unsigned int tag, preempted_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001074 u32 preempted_sactive, preempted_qc_active;
Ingo Molnar60be6b92006-07-03 00:25:26 -07001075 DECLARE_COMPLETION_ONSTACK(wait);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001076 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001077 unsigned int err_mask;
Tejun Heod95a7172006-05-15 20:58:14 +09001078 int rc;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001079
Jeff Garzikba6a1302006-06-22 23:46:10 -04001080 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001081
Tejun Heoe3180492006-05-15 20:58:09 +09001082 /* no internal command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09001083 if (ap->pflags & ATA_PFLAG_FROZEN) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001084 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001085 return AC_ERR_SYSTEM;
1086 }
1087
Tejun Heo2ab7db12006-05-15 20:58:02 +09001088 /* initialize internal qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001089
Tejun Heo2ab7db12006-05-15 20:58:02 +09001090 /* XXX: Tag 0 is used for drivers with legacy EH as some
1091 * drivers choke if any other tag is given. This breaks
1092 * ata_tag_internal() test for those drivers. Don't use new
1093 * EH stuff without converting to it.
1094 */
1095 if (ap->ops->error_handler)
1096 tag = ATA_TAG_INTERNAL;
1097 else
1098 tag = 0;
1099
Tejun Heo6cec4a32006-05-15 21:03:41 +09001100 if (test_and_set_bit(tag, &ap->qc_allocated))
Tejun Heo2ab7db12006-05-15 20:58:02 +09001101 BUG();
Tejun Heof69499f2006-05-15 20:58:03 +09001102 qc = __ata_qc_from_tag(ap, tag);
Tejun Heo2ab7db12006-05-15 20:58:02 +09001103
1104 qc->tag = tag;
1105 qc->scsicmd = NULL;
1106 qc->ap = ap;
1107 qc->dev = dev;
1108 ata_qc_reinit(qc);
1109
1110 preempted_tag = ap->active_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001111 preempted_sactive = ap->sactive;
1112 preempted_qc_active = ap->qc_active;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001113 ap->active_tag = ATA_TAG_POISON;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001114 ap->sactive = 0;
1115 ap->qc_active = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001116
1117 /* prepare & issue qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001118 qc->tf = *tf;
Tejun Heod69cf372006-04-02 18:51:53 +09001119 if (cdb)
1120 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
Tejun Heoe61e0672006-05-15 20:57:40 +09001121 qc->flags |= ATA_QCFLAG_RESULT_TF;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001122 qc->dma_dir = dma_dir;
1123 if (dma_dir != DMA_NONE) {
Tejun Heo24326972006-11-14 22:47:09 +09001124 unsigned int i, buflen = 0;
1125
1126 for (i = 0; i < n_elem; i++)
1127 buflen += sg[i].length;
1128
1129 ata_sg_init(qc, sg, n_elem);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001130 qc->nsect = buflen / ATA_SECT_SIZE;
1131 }
1132
Tejun Heo77853bf2006-01-23 13:09:36 +09001133 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001134 qc->complete_fn = ata_qc_complete_internal;
1135
Tejun Heo8e0e6942006-03-31 20:41:11 +09001136 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001137
Jeff Garzikba6a1302006-06-22 23:46:10 -04001138 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001139
Andrew Mortona8601e52006-06-25 01:36:52 -07001140 rc = wait_for_completion_timeout(&wait, ata_probe_timeout);
Albert Lee41ade502006-03-14 11:19:04 +08001141
Tejun Heod95a7172006-05-15 20:58:14 +09001142 ata_port_flush_task(ap);
1143
1144 if (!rc) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001145 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001146
1147 /* We're racing with irq here. If we lose, the
1148 * following test prevents us from completing the qc
Tejun Heod95a7172006-05-15 20:58:14 +09001149 * twice. If we win, the port is frozen and will be
1150 * cleaned up by ->post_internal_cmd().
Tejun Heoa2a7a662005-12-13 14:48:31 +09001151 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001152 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heod95a7172006-05-15 20:58:14 +09001153 qc->err_mask |= AC_ERR_TIMEOUT;
1154
1155 if (ap->ops->error_handler)
1156 ata_port_freeze(ap);
1157 else
1158 ata_qc_complete(qc);
Tejun Heof15a1da2006-05-15 20:57:56 +09001159
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001160 if (ata_msg_warn(ap))
1161 ata_dev_printk(dev, KERN_WARNING,
Tejun Heo88574552006-06-25 20:00:35 +09001162 "qc timeout (cmd 0x%x)\n", command);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001163 }
1164
Jeff Garzikba6a1302006-06-22 23:46:10 -04001165 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001166 }
1167
Tejun Heod95a7172006-05-15 20:58:14 +09001168 /* do post_internal_cmd */
1169 if (ap->ops->post_internal_cmd)
1170 ap->ops->post_internal_cmd(qc);
1171
1172 if (qc->flags & ATA_QCFLAG_FAILED && !qc->err_mask) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001173 if (ata_msg_warn(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001174 ata_dev_printk(dev, KERN_WARNING,
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001175 "zero err_mask for failed "
Tejun Heo88574552006-06-25 20:00:35 +09001176 "internal command, assuming AC_ERR_OTHER\n");
Tejun Heod95a7172006-05-15 20:58:14 +09001177 qc->err_mask |= AC_ERR_OTHER;
1178 }
1179
Tejun Heo15869302006-05-15 20:57:33 +09001180 /* finish up */
Jeff Garzikba6a1302006-06-22 23:46:10 -04001181 spin_lock_irqsave(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001182
Tejun Heoe61e0672006-05-15 20:57:40 +09001183 *tf = qc->result_tf;
Tejun Heo77853bf2006-01-23 13:09:36 +09001184 err_mask = qc->err_mask;
1185
1186 ata_qc_free(qc);
Tejun Heo2ab7db12006-05-15 20:58:02 +09001187 ap->active_tag = preempted_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001188 ap->sactive = preempted_sactive;
1189 ap->qc_active = preempted_qc_active;
Tejun Heo77853bf2006-01-23 13:09:36 +09001190
Tejun Heo1f7dd3e2006-03-24 15:25:30 +09001191 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1192 * Until those drivers are fixed, we detect the condition
1193 * here, fail the command with AC_ERR_SYSTEM and reenable the
1194 * port.
1195 *
1196 * Note that this doesn't change any behavior as internal
1197 * command failure results in disabling the device in the
1198 * higher layer for LLDDs without new reset/EH callbacks.
1199 *
1200 * Kill the following code as soon as those drivers are fixed.
1201 */
Tejun Heo198e0fe2006-04-02 18:51:52 +09001202 if (ap->flags & ATA_FLAG_DISABLED) {
Tejun Heo1f7dd3e2006-03-24 15:25:30 +09001203 err_mask |= AC_ERR_SYSTEM;
1204 ata_port_probe(ap);
1205 }
1206
Jeff Garzikba6a1302006-06-22 23:46:10 -04001207 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001208
Tejun Heo77853bf2006-01-23 13:09:36 +09001209 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001210}
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212/**
Tejun Heo24326972006-11-14 22:47:09 +09001213 * ata_exec_internal_sg - execute libata internal command
1214 * @dev: Device to which the command is sent
1215 * @tf: Taskfile registers for the command and the result
1216 * @cdb: CDB for packet command
1217 * @dma_dir: Data tranfer direction of the command
1218 * @buf: Data buffer of the command
1219 * @buflen: Length of data buffer
1220 *
1221 * Wrapper around ata_exec_internal_sg() which takes simple
1222 * buffer instead of sg list.
1223 *
1224 * LOCKING:
1225 * None. Should be called with kernel context, might sleep.
1226 *
1227 * RETURNS:
1228 * Zero on success, AC_ERR_* mask on failure
1229 */
1230unsigned ata_exec_internal(struct ata_device *dev,
1231 struct ata_taskfile *tf, const u8 *cdb,
1232 int dma_dir, void *buf, unsigned int buflen)
1233{
1234 struct scatterlist sg;
1235
1236 sg_init_one(&sg, buf, buflen);
1237
1238 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, &sg, 1);
1239}
1240
1241/**
Tejun Heo977e6b92006-06-24 20:30:19 +09001242 * ata_do_simple_cmd - execute simple internal command
1243 * @dev: Device to which the command is sent
1244 * @cmd: Opcode to execute
1245 *
1246 * Execute a 'simple' command, that only consists of the opcode
1247 * 'cmd' itself, without filling any other registers
1248 *
1249 * LOCKING:
1250 * Kernel thread context (may sleep).
1251 *
1252 * RETURNS:
1253 * Zero on success, AC_ERR_* mask on failure
Tejun Heoe58eb582006-06-24 20:30:19 +09001254 */
Tejun Heo77b08fb2006-06-24 20:30:19 +09001255unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
Tejun Heoe58eb582006-06-24 20:30:19 +09001256{
1257 struct ata_taskfile tf;
Tejun Heoe58eb582006-06-24 20:30:19 +09001258
1259 ata_tf_init(dev, &tf);
1260
1261 tf.command = cmd;
1262 tf.flags |= ATA_TFLAG_DEVICE;
1263 tf.protocol = ATA_PROT_NODATA;
1264
Tejun Heo977e6b92006-06-24 20:30:19 +09001265 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Tejun Heoe58eb582006-06-24 20:30:19 +09001266}
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001269 * ata_pio_need_iordy - check if iordy needed
1270 * @adev: ATA device
1271 *
1272 * Check if the current speed of the device requires IORDY. Used
1273 * by various controllers for chip configuration.
1274 */
1275
1276unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1277{
1278 int pio;
1279 int speed = adev->pio_mode - XFER_PIO_0;
1280
1281 if (speed < 2)
1282 return 0;
1283 if (speed > 2)
1284 return 1;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001285
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001286 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1287
1288 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1289 pio = adev->id[ATA_ID_EIDE_PIO];
1290 /* Is the speed faster than the drive allows non IORDY ? */
1291 if (pio) {
1292 /* This is cycle times not frequency - watch the logic! */
1293 if (pio > 240) /* PIO2 is 240nS per cycle */
1294 return 1;
1295 return 0;
1296 }
1297 }
1298 return 0;
1299}
1300
1301/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001302 * ata_dev_read_id - Read ID data from the specified device
Tejun Heo49016ac2006-02-21 02:12:11 +09001303 * @dev: target device
1304 * @p_class: pointer to class of the target device (may be changed)
Tejun Heobff04642006-11-10 18:08:10 +09001305 * @flags: ATA_READID_* flags
Tejun Heofe635c72006-05-15 20:57:35 +09001306 * @id: buffer to read IDENTIFY data into
Tejun Heo49016ac2006-02-21 02:12:11 +09001307 *
1308 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1309 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09001310 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1311 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09001312 *
1313 * LOCKING:
1314 * Kernel thread context (may sleep)
1315 *
1316 * RETURNS:
1317 * 0 on success, -errno otherwise.
1318 */
Tejun Heoa9beec92006-05-31 18:27:44 +09001319int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
Tejun Heobff04642006-11-10 18:08:10 +09001320 unsigned int flags, u16 *id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001321{
Tejun Heo3373efd2006-05-15 20:57:53 +09001322 struct ata_port *ap = dev->ap;
Tejun Heo49016ac2006-02-21 02:12:11 +09001323 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09001324 struct ata_taskfile tf;
1325 unsigned int err_mask = 0;
1326 const char *reason;
1327 int rc;
1328
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001329 if (ata_msg_ctl(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001330 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER, host %u, dev %u\n",
1331 __FUNCTION__, ap->id, dev->devno);
Tejun Heo49016ac2006-02-21 02:12:11 +09001332
Tejun Heo49016ac2006-02-21 02:12:11 +09001333 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1334
1335 retry:
Tejun Heo3373efd2006-05-15 20:57:53 +09001336 ata_tf_init(dev, &tf);
Tejun Heo49016ac2006-02-21 02:12:11 +09001337
1338 switch (class) {
1339 case ATA_DEV_ATA:
1340 tf.command = ATA_CMD_ID_ATA;
1341 break;
1342 case ATA_DEV_ATAPI:
1343 tf.command = ATA_CMD_ID_ATAPI;
1344 break;
1345 default:
1346 rc = -ENODEV;
1347 reason = "unsupported class";
1348 goto err_out;
1349 }
1350
1351 tf.protocol = ATA_PROT_PIO;
1352
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001353 /* presence detection using polling IDENTIFY? */
1354 if (flags & ATA_READID_DETECT)
1355 tf.flags |= ATA_TFLAG_POLLING;
1356
Tejun Heo3373efd2006-05-15 20:57:53 +09001357 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
Tejun Heo49016ac2006-02-21 02:12:11 +09001358 id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heo49016ac2006-02-21 02:12:11 +09001359 if (err_mask) {
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001360 if ((flags & ATA_READID_DETECT) &&
1361 (err_mask & AC_ERR_NODEV_HINT)) {
1362 DPRINTK("ata%u.%d: NODEV after polling detection\n",
1363 ap->id, dev->devno);
1364 return -ENOENT;
1365 }
1366
Tejun Heo49016ac2006-02-21 02:12:11 +09001367 rc = -EIO;
1368 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09001369 goto err_out;
1370 }
1371
1372 swap_buf_le16(id, ATA_ID_WORDS);
1373
Tejun Heo49016ac2006-02-21 02:12:11 +09001374 /* sanity check */
Tejun Heoa4f57492006-09-12 20:35:49 -07001375 rc = -EINVAL;
1376 reason = "device reports illegal type";
1377
1378 if (class == ATA_DEV_ATA) {
1379 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1380 goto err_out;
1381 } else {
1382 if (ata_id_is_ata(id))
1383 goto err_out;
Tejun Heo49016ac2006-02-21 02:12:11 +09001384 }
1385
Tejun Heobff04642006-11-10 18:08:10 +09001386 if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
Tejun Heo49016ac2006-02-21 02:12:11 +09001387 /*
1388 * The exact sequence expected by certain pre-ATA4 drives is:
1389 * SRST RESET
1390 * IDENTIFY
1391 * INITIALIZE DEVICE PARAMETERS
1392 * anything else..
1393 * Some drives were very specific about that exact sequence.
1394 */
1395 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Tejun Heo3373efd2006-05-15 20:57:53 +09001396 err_mask = ata_dev_init_params(dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09001397 if (err_mask) {
1398 rc = -EIO;
1399 reason = "INIT_DEV_PARAMS failed";
1400 goto err_out;
1401 }
1402
1403 /* current CHS translation info (id[53-58]) might be
1404 * changed. reread the identify device info.
1405 */
Tejun Heobff04642006-11-10 18:08:10 +09001406 flags &= ~ATA_READID_POSTRESET;
Tejun Heo49016ac2006-02-21 02:12:11 +09001407 goto retry;
1408 }
1409 }
1410
1411 *p_class = class;
Tejun Heofe635c72006-05-15 20:57:35 +09001412
Tejun Heo49016ac2006-02-21 02:12:11 +09001413 return 0;
1414
1415 err_out:
Tejun Heo88574552006-06-25 20:00:35 +09001416 if (ata_msg_warn(ap))
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001417 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
Tejun Heo88574552006-06-25 20:00:35 +09001418 "(%s, err_mask=0x%x)\n", reason, err_mask);
Tejun Heo49016ac2006-02-21 02:12:11 +09001419 return rc;
1420}
1421
Tejun Heo3373efd2006-05-15 20:57:53 +09001422static inline u8 ata_dev_knobble(struct ata_device *dev)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001423{
Tejun Heo3373efd2006-05-15 20:57:53 +09001424 return ((dev->ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001425}
1426
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001427static void ata_dev_config_ncq(struct ata_device *dev,
1428 char *desc, size_t desc_sz)
1429{
1430 struct ata_port *ap = dev->ap;
1431 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
1432
1433 if (!ata_id_has_ncq(dev->id)) {
1434 desc[0] = '\0';
1435 return;
1436 }
Alan Cox6919a0a2006-10-27 19:08:46 -07001437 if (ata_device_blacklisted(dev) & ATA_HORKAGE_NONCQ) {
1438 snprintf(desc, desc_sz, "NCQ (not used)");
1439 return;
1440 }
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001441 if (ap->flags & ATA_FLAG_NCQ) {
Jeff Garzikcca39742006-08-24 03:19:22 -04001442 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001443 dev->flags |= ATA_DFLAG_NCQ;
1444 }
1445
1446 if (hdepth >= ddepth)
1447 snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
1448 else
1449 snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
1450}
1451
Brian Kinge6d902a2006-06-28 08:30:31 -05001452static void ata_set_port_max_cmd_len(struct ata_port *ap)
1453{
1454 int i;
1455
Jeff Garzikcca39742006-08-24 03:19:22 -04001456 if (ap->scsi_host) {
1457 unsigned int len = 0;
1458
Brian Kinge6d902a2006-06-28 08:30:31 -05001459 for (i = 0; i < ATA_MAX_DEVICES; i++)
Jeff Garzikcca39742006-08-24 03:19:22 -04001460 len = max(len, ap->device[i].cdb_len);
1461
1462 ap->scsi_host->max_cmd_len = len;
Brian Kinge6d902a2006-06-28 08:30:31 -05001463 }
1464}
1465
Tejun Heo49016ac2006-02-21 02:12:11 +09001466/**
Tejun Heoffeae412006-03-01 16:09:35 +09001467 * ata_dev_configure - Configure the specified ATA/ATAPI device
Tejun Heoffeae412006-03-01 16:09:35 +09001468 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 *
Tejun Heoffeae412006-03-01 16:09:35 +09001470 * Configure @dev according to @dev->id. Generic and low-level
1471 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 *
1473 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001474 * Kernel thread context (may sleep)
1475 *
1476 * RETURNS:
1477 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 */
Tejun Heoefdaedc2006-11-01 18:38:52 +09001479int ata_dev_configure(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Tejun Heo3373efd2006-05-15 20:57:53 +09001481 struct ata_port *ap = dev->ap;
Tejun Heoefdaedc2006-11-01 18:38:52 +09001482 int print_info = ap->eh_context.i.flags & ATA_EHI_PRINTINFO;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001483 const u16 *id = dev->id;
Tejun Heoff8854b2006-03-06 04:31:56 +09001484 unsigned int xfer_mask;
Alan Coxb352e572006-08-10 18:52:12 +01001485 char revbuf[7]; /* XYZ-99\0 */
Brian Kinge6d902a2006-06-28 08:30:31 -05001486 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001488 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
Tejun Heo88574552006-06-25 20:00:35 +09001489 ata_dev_printk(dev, KERN_INFO,
1490 "%s: ENTER/EXIT (host %u, dev %u) -- nodev\n",
1491 __FUNCTION__, ap->id, dev->devno);
Tejun Heoffeae412006-03-01 16:09:35 +09001492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
1494
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001495 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001496 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER, host %u, dev %u\n",
1497 __FUNCTION__, ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001499 /* print device capabilities */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001500 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001501 ata_dev_printk(dev, KERN_DEBUG,
1502 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
1503 "85:%04x 86:%04x 87:%04x 88:%04x\n",
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001504 __FUNCTION__,
Tejun Heof15a1da2006-05-15 20:57:56 +09001505 id[49], id[82], id[83], id[84],
1506 id[85], id[86], id[87], id[88]);
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001507
Tejun Heo208a9932006-03-05 17:55:58 +09001508 /* initialize to-be-configured parameters */
Tejun Heoea1dd4e2006-04-02 18:51:53 +09001509 dev->flags &= ~ATA_DFLAG_CFG_MASK;
Tejun Heo208a9932006-03-05 17:55:58 +09001510 dev->max_sectors = 0;
1511 dev->cdb_len = 0;
1512 dev->n_sectors = 0;
1513 dev->cylinders = 0;
1514 dev->heads = 0;
1515 dev->sectors = 0;
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 /*
1518 * common ATA, ATAPI feature tests
1519 */
1520
Tejun Heoff8854b2006-03-06 04:31:56 +09001521 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001522 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001524 if (ata_msg_probe(ap))
1525 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 /* ATA-specific feature tests */
1528 if (dev->class == ATA_DEV_ATA) {
Alan Coxb352e572006-08-10 18:52:12 +01001529 if (ata_id_is_cfa(id)) {
1530 if (id[162] & 1) /* CPRM may make this media unusable */
1531 ata_dev_printk(dev, KERN_WARNING, "ata%u: device %u supports DRM functions and may not be fully accessable.\n",
1532 ap->id, dev->devno);
1533 snprintf(revbuf, 7, "CFA");
1534 }
1535 else
1536 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
1537
Tejun Heo1148c3a2006-03-13 19:48:04 +09001538 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09001539
Tejun Heo1148c3a2006-03-13 19:48:04 +09001540 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001541 const char *lba_desc;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001542 char ncq_desc[20];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001543
Tejun Heo4c2d7212006-03-05 17:55:58 +09001544 lba_desc = "LBA";
1545 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001546 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001547 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001548 lba_desc = "LBA48";
Tejun Heo6fc49ad2006-11-11 20:10:45 +09001549
1550 if (dev->n_sectors >= (1UL << 28) &&
1551 ata_id_has_flush_ext(id))
1552 dev->flags |= ATA_DFLAG_FLUSH_EXT;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001553 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04001554
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001555 /* config NCQ */
1556 ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
1557
Albert Lee8bf62ec2005-05-12 15:29:42 -04001558 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02001559 if (ata_msg_drv(ap) && print_info)
Alan Coxb352e572006-08-10 18:52:12 +01001560 ata_dev_printk(dev, KERN_INFO, "%s, "
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001561 "max %s, %Lu sectors: %s %s\n",
Alan Coxb352e572006-08-10 18:52:12 +01001562 revbuf,
Tejun Heof15a1da2006-05-15 20:57:56 +09001563 ata_mode_string(xfer_mask),
1564 (unsigned long long)dev->n_sectors,
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001565 lba_desc, ncq_desc);
Tejun Heoffeae412006-03-01 16:09:35 +09001566 } else {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001567 /* CHS */
1568
1569 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001570 dev->cylinders = id[1];
1571 dev->heads = id[3];
1572 dev->sectors = id[6];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001573
Tejun Heo1148c3a2006-03-13 19:48:04 +09001574 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001575 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001576 dev->cylinders = id[54];
1577 dev->heads = id[55];
1578 dev->sectors = id[56];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001579 }
1580
1581 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02001582 if (ata_msg_drv(ap) && print_info)
Alan Coxb352e572006-08-10 18:52:12 +01001583 ata_dev_printk(dev, KERN_INFO, "%s, "
Tejun Heof15a1da2006-05-15 20:57:56 +09001584 "max %s, %Lu sectors: CHS %u/%u/%u\n",
Alan Coxb352e572006-08-10 18:52:12 +01001585 revbuf,
Tejun Heof15a1da2006-05-15 20:57:56 +09001586 ata_mode_string(xfer_mask),
1587 (unsigned long long)dev->n_sectors,
Tejun Heo88574552006-06-25 20:00:35 +09001588 dev->cylinders, dev->heads,
1589 dev->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
1591
Albert Lee07f6f7d2005-11-01 19:33:20 +08001592 if (dev->id[59] & 0x100) {
1593 dev->multi_count = dev->id[59] & 0xff;
Borislav Petkov5afc8142006-06-27 14:51:25 +02001594 if (ata_msg_drv(ap) && print_info)
Tejun Heo88574552006-06-25 20:00:35 +09001595 ata_dev_printk(dev, KERN_INFO,
1596 "ata%u: dev %u multi count %u\n",
1597 ap->id, dev->devno, dev->multi_count);
Albert Lee07f6f7d2005-11-01 19:33:20 +08001598 }
1599
Tejun Heo6e7846e2006-02-12 23:32:58 +09001600 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
1602
1603 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001604 else if (dev->class == ATA_DEV_ATAPI) {
Albert Lee08a556d2006-03-31 13:29:04 +08001605 char *cdb_intr_string = "";
1606
Tejun Heo1148c3a2006-03-13 19:48:04 +09001607 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001609 if (ata_msg_warn(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001610 ata_dev_printk(dev, KERN_WARNING,
1611 "unsupported CDB len\n");
Tejun Heoffeae412006-03-01 16:09:35 +09001612 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 goto err_out_nosup;
1614 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001615 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Albert Lee08a556d2006-03-31 13:29:04 +08001617 if (ata_id_cdb_intr(dev->id)) {
Albert Lee312f7da2005-09-27 17:38:03 +08001618 dev->flags |= ATA_DFLAG_CDB_INTR;
Albert Lee08a556d2006-03-31 13:29:04 +08001619 cdb_intr_string = ", CDB intr";
1620 }
Albert Lee312f7da2005-09-27 17:38:03 +08001621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02001623 if (ata_msg_drv(ap) && print_info)
Tejun Heo12436c32006-05-15 20:59:15 +09001624 ata_dev_printk(dev, KERN_INFO, "ATAPI, max %s%s\n",
1625 ata_mode_string(xfer_mask),
1626 cdb_intr_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
1628
Tejun Heo914ed352006-11-01 18:39:55 +09001629 /* determine max_sectors */
1630 dev->max_sectors = ATA_MAX_SECTORS;
1631 if (dev->flags & ATA_DFLAG_LBA48)
1632 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
1633
Alan Cox93590852006-09-12 16:55:12 +01001634 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
1635 /* Let the user know. We don't want to disallow opens for
1636 rescue purposes, or in case the vendor is just a blithering
1637 idiot */
1638 if (print_info) {
1639 ata_dev_printk(dev, KERN_WARNING,
1640"Drive reports diagnostics failure. This may indicate a drive\n");
1641 ata_dev_printk(dev, KERN_WARNING,
1642"fault or invalid emulation. Contact drive vendor for information.\n");
1643 }
1644 }
1645
Brian Kinge6d902a2006-06-28 08:30:31 -05001646 ata_set_port_max_cmd_len(ap);
Tejun Heo6e7846e2006-02-12 23:32:58 +09001647
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001648 /* limit bridge transfers to udma5, 200 sectors */
Tejun Heo3373efd2006-05-15 20:57:53 +09001649 if (ata_dev_knobble(dev)) {
Borislav Petkov5afc8142006-06-27 14:51:25 +02001650 if (ata_msg_drv(ap) && print_info)
Tejun Heof15a1da2006-05-15 20:57:56 +09001651 ata_dev_printk(dev, KERN_INFO,
1652 "applying bridge limits\n");
Tejun Heo5a529132006-03-24 14:07:50 +09001653 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001654 dev->max_sectors = ATA_MAX_SECTORS;
1655 }
1656
1657 if (ap->ops->dev_config)
1658 ap->ops->dev_config(ap, dev);
1659
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001660 if (ata_msg_probe(ap))
1661 ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n",
1662 __FUNCTION__, ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09001663 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665err_out_nosup:
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001666 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001667 ata_dev_printk(dev, KERN_DEBUG,
1668 "%s: EXIT, err\n", __FUNCTION__);
Tejun Heoffeae412006-03-01 16:09:35 +09001669 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
1672/**
1673 * ata_bus_probe - Reset and probe ATA bus
1674 * @ap: Bus to probe
1675 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001676 * Master ATA bus probing function. Initiates a hardware-dependent
1677 * bus reset, then attempts to identify any devices found on
1678 * the bus.
1679 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001681 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 *
1683 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09001684 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 */
1686
Brian King80289162006-08-07 14:27:31 -05001687int ata_bus_probe(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Tejun Heo28ca5c52006-03-01 16:09:36 +09001689 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo14d2bac2006-04-02 17:54:46 +09001690 int tries[ATA_MAX_DEVICES];
1691 int i, rc, down_xfermask;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001692 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Tejun Heo28ca5c52006-03-01 16:09:36 +09001694 ata_port_probe(ap);
1695
Tejun Heo14d2bac2006-04-02 17:54:46 +09001696 for (i = 0; i < ATA_MAX_DEVICES; i++)
1697 tries[i] = ATA_PROBE_MAX_TRIES;
1698
1699 retry:
1700 down_xfermask = 0;
1701
Tejun Heo20444702006-03-13 01:57:01 +09001702 /* reset and determine device classes */
Tejun Heo52783c52006-05-31 18:28:22 +09001703 ap->ops->phy_reset(ap);
Tejun Heo2061a472006-03-12 00:57:39 +09001704
Tejun Heo52783c52006-05-31 18:28:22 +09001705 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1706 dev = &ap->device[i];
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001707
Tejun Heo52783c52006-05-31 18:28:22 +09001708 if (!(ap->flags & ATA_FLAG_DISABLED) &&
1709 dev->class != ATA_DEV_UNKNOWN)
1710 classes[dev->devno] = dev->class;
1711 else
1712 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo20444702006-03-13 01:57:01 +09001713
Tejun Heo52783c52006-05-31 18:28:22 +09001714 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo28ca5c52006-03-01 16:09:36 +09001715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Tejun Heo52783c52006-05-31 18:28:22 +09001717 ata_port_probe(ap);
Tejun Heo20444702006-03-13 01:57:01 +09001718
Alan Coxb6079ca2006-05-22 16:52:06 +01001719 /* after the reset the device state is PIO 0 and the controller
1720 state is undefined. Record the mode */
1721
1722 for (i = 0; i < ATA_MAX_DEVICES; i++)
1723 ap->device[i].pio_mode = XFER_PIO_0;
1724
Tejun Heo28ca5c52006-03-01 16:09:36 +09001725 /* read IDENTIFY page and configure devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001727 dev = &ap->device[i];
Tejun Heo28ca5c52006-03-01 16:09:36 +09001728
Tejun Heoec573752006-04-11 22:26:29 +09001729 if (tries[i])
1730 dev->class = classes[i];
Tejun Heo14d2bac2006-04-02 17:54:46 +09001731
Tejun Heoe1211e32006-04-01 01:38:18 +09001732 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09001733 continue;
1734
Tejun Heobff04642006-11-10 18:08:10 +09001735 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
1736 dev->id);
Tejun Heo14d2bac2006-04-02 17:54:46 +09001737 if (rc)
1738 goto fail;
Tejun Heoffeae412006-03-01 16:09:35 +09001739
Tejun Heoefdaedc2006-11-01 18:38:52 +09001740 ap->eh_context.i.flags |= ATA_EHI_PRINTINFO;
1741 rc = ata_dev_configure(dev);
1742 ap->eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo14d2bac2006-04-02 17:54:46 +09001743 if (rc)
1744 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001747 /* configure transfer mode */
Tejun Heo3adcebb2006-05-15 20:57:37 +09001748 rc = ata_set_mode(ap, &dev);
Tejun Heo51713d32006-04-11 22:26:29 +09001749 if (rc) {
1750 down_xfermask = 1;
1751 goto fail;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001754 for (i = 0; i < ATA_MAX_DEVICES; i++)
1755 if (ata_dev_enabled(&ap->device[i]))
1756 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01001757
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001758 /* no device present, disable port */
1759 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 ap->ops->port_disable(ap);
Tejun Heo96072e62006-04-01 01:38:17 +09001761 return -ENODEV;
Tejun Heo14d2bac2006-04-02 17:54:46 +09001762
1763 fail:
1764 switch (rc) {
1765 case -EINVAL:
1766 case -ENODEV:
1767 tries[dev->devno] = 0;
1768 break;
1769 case -EIO:
Tejun Heo3c567b72006-05-15 20:57:23 +09001770 sata_down_spd_limit(ap);
Tejun Heo14d2bac2006-04-02 17:54:46 +09001771 /* fall through */
1772 default:
1773 tries[dev->devno]--;
1774 if (down_xfermask &&
Tejun Heo3373efd2006-05-15 20:57:53 +09001775 ata_down_xfermask_limit(dev, tries[dev->devno] == 1))
Tejun Heo14d2bac2006-04-02 17:54:46 +09001776 tries[dev->devno] = 0;
1777 }
1778
Tejun Heoec573752006-04-11 22:26:29 +09001779 if (!tries[dev->devno]) {
Tejun Heo3373efd2006-05-15 20:57:53 +09001780 ata_down_xfermask_limit(dev, 1);
1781 ata_dev_disable(dev);
Tejun Heoec573752006-04-11 22:26:29 +09001782 }
1783
Tejun Heo14d2bac2006-04-02 17:54:46 +09001784 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
1787/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001788 * ata_port_probe - Mark port as enabled
1789 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001791 * Modify @ap data structure such that the system
1792 * thinks that the entire port is enabled.
1793 *
Jeff Garzikcca39742006-08-24 03:19:22 -04001794 * LOCKING: host lock, or some other form of
Jeff Garzik0cba6322005-05-30 19:49:12 -04001795 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 */
1797
1798void ata_port_probe(struct ata_port *ap)
1799{
Tejun Heo198e0fe2006-04-02 18:51:52 +09001800 ap->flags &= ~ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802
1803/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001804 * sata_print_link_status - Print SATA link status
1805 * @ap: SATA port to printk link status about
1806 *
1807 * This function prints link speed and status of a SATA link.
1808 *
1809 * LOCKING:
1810 * None.
1811 */
1812static void sata_print_link_status(struct ata_port *ap)
1813{
Tejun Heo6d5f9732006-04-03 00:09:41 +09001814 u32 sstatus, scontrol, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09001815
Tejun Heo81952c52006-05-15 20:57:47 +09001816 if (sata_scr_read(ap, SCR_STATUS, &sstatus))
Tejun Heo3be680b2005-12-19 22:35:02 +09001817 return;
Tejun Heo81952c52006-05-15 20:57:47 +09001818 sata_scr_read(ap, SCR_CONTROL, &scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09001819
Tejun Heo81952c52006-05-15 20:57:47 +09001820 if (ata_port_online(ap)) {
Tejun Heo3be680b2005-12-19 22:35:02 +09001821 tmp = (sstatus >> 4) & 0xf;
Tejun Heof15a1da2006-05-15 20:57:56 +09001822 ata_port_printk(ap, KERN_INFO,
1823 "SATA link up %s (SStatus %X SControl %X)\n",
1824 sata_spd_string(tmp), sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09001825 } else {
Tejun Heof15a1da2006-05-15 20:57:56 +09001826 ata_port_printk(ap, KERN_INFO,
1827 "SATA link down (SStatus %X SControl %X)\n",
1828 sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09001829 }
1830}
1831
1832/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001833 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1834 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001836 * This function issues commands to standard SATA Sxxx
1837 * PHY registers, to wake up the phy (and device), and
1838 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 *
1840 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001841 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 *
1843 */
1844void __sata_phy_reset(struct ata_port *ap)
1845{
1846 u32 sstatus;
1847 unsigned long timeout = jiffies + (HZ * 5);
1848
1849 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001850 /* issue phy wake/reset */
Tejun Heo81952c52006-05-15 20:57:47 +09001851 sata_scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001852 /* Couldn't find anything in SATA I/II specs, but
1853 * AHCI-1.1 10.4.2 says at least 1 ms. */
1854 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
Tejun Heo81952c52006-05-15 20:57:47 +09001856 /* phy wake/clear reset */
1857 sata_scr_write_flush(ap, SCR_CONTROL, 0x300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 /* wait for phy to become ready, if necessary */
1860 do {
1861 msleep(200);
Tejun Heo81952c52006-05-15 20:57:47 +09001862 sata_scr_read(ap, SCR_STATUS, &sstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if ((sstatus & 0xf) != 1)
1864 break;
1865 } while (time_before(jiffies, timeout));
1866
Tejun Heo3be680b2005-12-19 22:35:02 +09001867 /* print link status */
1868 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001869
Tejun Heo3be680b2005-12-19 22:35:02 +09001870 /* TODO: phy layer with polling, timeouts, etc. */
Tejun Heo81952c52006-05-15 20:57:47 +09001871 if (!ata_port_offline(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001873 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Tejun Heo198e0fe2006-04-02 18:51:52 +09001876 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 return;
1878
1879 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1880 ata_port_disable(ap);
1881 return;
1882 }
1883
1884 ap->cbl = ATA_CBL_SATA;
1885}
1886
1887/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001888 * sata_phy_reset - Reset SATA bus.
1889 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001891 * This function resets the SATA bus, and then probes
1892 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 *
1894 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001895 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 *
1897 */
1898void sata_phy_reset(struct ata_port *ap)
1899{
1900 __sata_phy_reset(ap);
Tejun Heo198e0fe2006-04-02 18:51:52 +09001901 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 return;
1903 ata_bus_reset(ap);
1904}
1905
1906/**
Alan Coxebdfca62006-03-23 15:38:34 +00001907 * ata_dev_pair - return other device on cable
Alan Coxebdfca62006-03-23 15:38:34 +00001908 * @adev: device
1909 *
1910 * Obtain the other device on the same cable, or if none is
1911 * present NULL is returned
1912 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001913
Tejun Heo3373efd2006-05-15 20:57:53 +09001914struct ata_device *ata_dev_pair(struct ata_device *adev)
Alan Coxebdfca62006-03-23 15:38:34 +00001915{
Tejun Heo3373efd2006-05-15 20:57:53 +09001916 struct ata_port *ap = adev->ap;
Alan Coxebdfca62006-03-23 15:38:34 +00001917 struct ata_device *pair = &ap->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09001918 if (!ata_dev_enabled(pair))
Alan Coxebdfca62006-03-23 15:38:34 +00001919 return NULL;
1920 return pair;
1921}
1922
1923/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001924 * ata_port_disable - Disable port.
1925 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001927 * Modify @ap data structure such that the system
1928 * thinks that the entire port is disabled, and should
1929 * never attempt to probe or communicate with devices
1930 * on this port.
1931 *
Jeff Garzikcca39742006-08-24 03:19:22 -04001932 * LOCKING: host lock, or some other form of
Jeff Garzik780a87f2005-05-30 15:41:05 -04001933 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 */
1935
1936void ata_port_disable(struct ata_port *ap)
1937{
1938 ap->device[0].class = ATA_DEV_NONE;
1939 ap->device[1].class = ATA_DEV_NONE;
Tejun Heo198e0fe2006-04-02 18:51:52 +09001940 ap->flags |= ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
Tejun Heo1c3fae42006-04-02 20:53:28 +09001943/**
Tejun Heo3c567b72006-05-15 20:57:23 +09001944 * sata_down_spd_limit - adjust SATA spd limit downward
Tejun Heo1c3fae42006-04-02 20:53:28 +09001945 * @ap: Port to adjust SATA spd limit for
1946 *
1947 * Adjust SATA spd limit of @ap downward. Note that this
1948 * function only adjusts the limit. The change must be applied
Tejun Heo3c567b72006-05-15 20:57:23 +09001949 * using sata_set_spd().
Tejun Heo1c3fae42006-04-02 20:53:28 +09001950 *
1951 * LOCKING:
1952 * Inherited from caller.
1953 *
1954 * RETURNS:
1955 * 0 on success, negative errno on failure
1956 */
Tejun Heo3c567b72006-05-15 20:57:23 +09001957int sata_down_spd_limit(struct ata_port *ap)
Tejun Heo1c3fae42006-04-02 20:53:28 +09001958{
Tejun Heo81952c52006-05-15 20:57:47 +09001959 u32 sstatus, spd, mask;
1960 int rc, highbit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09001961
Tejun Heo81952c52006-05-15 20:57:47 +09001962 rc = sata_scr_read(ap, SCR_STATUS, &sstatus);
1963 if (rc)
1964 return rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09001965
1966 mask = ap->sata_spd_limit;
1967 if (mask <= 1)
1968 return -EINVAL;
1969 highbit = fls(mask) - 1;
1970 mask &= ~(1 << highbit);
1971
Tejun Heo81952c52006-05-15 20:57:47 +09001972 spd = (sstatus >> 4) & 0xf;
Tejun Heo1c3fae42006-04-02 20:53:28 +09001973 if (spd <= 1)
1974 return -EINVAL;
1975 spd--;
1976 mask &= (1 << spd) - 1;
1977 if (!mask)
1978 return -EINVAL;
1979
1980 ap->sata_spd_limit = mask;
1981
Tejun Heof15a1da2006-05-15 20:57:56 +09001982 ata_port_printk(ap, KERN_WARNING, "limiting SATA link speed to %s\n",
1983 sata_spd_string(fls(mask)));
Tejun Heo1c3fae42006-04-02 20:53:28 +09001984
1985 return 0;
1986}
1987
Tejun Heo3c567b72006-05-15 20:57:23 +09001988static int __sata_set_spd_needed(struct ata_port *ap, u32 *scontrol)
Tejun Heo1c3fae42006-04-02 20:53:28 +09001989{
1990 u32 spd, limit;
1991
1992 if (ap->sata_spd_limit == UINT_MAX)
1993 limit = 0;
1994 else
1995 limit = fls(ap->sata_spd_limit);
1996
1997 spd = (*scontrol >> 4) & 0xf;
1998 *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
1999
2000 return spd != limit;
2001}
2002
2003/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002004 * sata_set_spd_needed - is SATA spd configuration needed
Tejun Heo1c3fae42006-04-02 20:53:28 +09002005 * @ap: Port in question
2006 *
2007 * Test whether the spd limit in SControl matches
2008 * @ap->sata_spd_limit. This function is used to determine
2009 * whether hardreset is necessary to apply SATA spd
2010 * configuration.
2011 *
2012 * LOCKING:
2013 * Inherited from caller.
2014 *
2015 * RETURNS:
2016 * 1 if SATA spd configuration is needed, 0 otherwise.
2017 */
Tejun Heo3c567b72006-05-15 20:57:23 +09002018int sata_set_spd_needed(struct ata_port *ap)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002019{
2020 u32 scontrol;
2021
Tejun Heo81952c52006-05-15 20:57:47 +09002022 if (sata_scr_read(ap, SCR_CONTROL, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09002023 return 0;
2024
Tejun Heo3c567b72006-05-15 20:57:23 +09002025 return __sata_set_spd_needed(ap, &scontrol);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002026}
2027
2028/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002029 * sata_set_spd - set SATA spd according to spd limit
Tejun Heo1c3fae42006-04-02 20:53:28 +09002030 * @ap: Port to set SATA spd for
2031 *
2032 * Set SATA spd of @ap according to sata_spd_limit.
2033 *
2034 * LOCKING:
2035 * Inherited from caller.
2036 *
2037 * RETURNS:
2038 * 0 if spd doesn't need to be changed, 1 if spd has been
Tejun Heo81952c52006-05-15 20:57:47 +09002039 * changed. Negative errno if SCR registers are inaccessible.
Tejun Heo1c3fae42006-04-02 20:53:28 +09002040 */
Tejun Heo3c567b72006-05-15 20:57:23 +09002041int sata_set_spd(struct ata_port *ap)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002042{
2043 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09002044 int rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002045
Tejun Heo81952c52006-05-15 20:57:47 +09002046 if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
2047 return rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002048
Tejun Heo3c567b72006-05-15 20:57:23 +09002049 if (!__sata_set_spd_needed(ap, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09002050 return 0;
2051
Tejun Heo81952c52006-05-15 20:57:47 +09002052 if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
2053 return rc;
2054
Tejun Heo1c3fae42006-04-02 20:53:28 +09002055 return 1;
2056}
2057
Alan Cox452503f2005-10-21 19:01:32 -04002058/*
2059 * This mode timing computation functionality is ported over from
2060 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
2061 */
2062/*
Alan Coxb352e572006-08-10 18:52:12 +01002063 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
Alan Cox452503f2005-10-21 19:01:32 -04002064 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
Alan Coxb352e572006-08-10 18:52:12 +01002065 * for UDMA6, which is currently supported only by Maxtor drives.
2066 *
2067 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
Alan Cox452503f2005-10-21 19:01:32 -04002068 */
2069
2070static const struct ata_timing ata_timing[] = {
2071
2072 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
2073 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
2074 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
2075 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
2076
Alan Coxb352e572006-08-10 18:52:12 +01002077 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
2078 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04002079 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
2080 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
2081 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
2082
2083/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002084
Alan Cox452503f2005-10-21 19:01:32 -04002085 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
2086 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
2087 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002088
Alan Cox452503f2005-10-21 19:01:32 -04002089 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
2090 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
2091 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
2092
Alan Coxb352e572006-08-10 18:52:12 +01002093 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
2094 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04002095 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
2096 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
2097
2098 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
2099 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
2100 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
2101
2102/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
2103
2104 { 0xFF }
2105};
2106
2107#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
2108#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
2109
2110static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
2111{
2112 q->setup = EZ(t->setup * 1000, T);
2113 q->act8b = EZ(t->act8b * 1000, T);
2114 q->rec8b = EZ(t->rec8b * 1000, T);
2115 q->cyc8b = EZ(t->cyc8b * 1000, T);
2116 q->active = EZ(t->active * 1000, T);
2117 q->recover = EZ(t->recover * 1000, T);
2118 q->cycle = EZ(t->cycle * 1000, T);
2119 q->udma = EZ(t->udma * 1000, UT);
2120}
2121
2122void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
2123 struct ata_timing *m, unsigned int what)
2124{
2125 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
2126 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
2127 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
2128 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
2129 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
2130 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
2131 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
2132 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
2133}
2134
2135static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
2136{
2137 const struct ata_timing *t;
2138
2139 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04002140 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04002141 return NULL;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002142 return t;
Alan Cox452503f2005-10-21 19:01:32 -04002143}
2144
2145int ata_timing_compute(struct ata_device *adev, unsigned short speed,
2146 struct ata_timing *t, int T, int UT)
2147{
2148 const struct ata_timing *s;
2149 struct ata_timing p;
2150
2151 /*
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002152 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08002153 */
Alan Cox452503f2005-10-21 19:01:32 -04002154
2155 if (!(s = ata_timing_find_mode(speed)))
2156 return -EINVAL;
2157
Albert Lee75b1f2f2005-11-16 17:06:18 +08002158 memcpy(t, s, sizeof(*s));
2159
Alan Cox452503f2005-10-21 19:01:32 -04002160 /*
2161 * If the drive is an EIDE drive, it can tell us it needs extended
2162 * PIO/MW_DMA cycle timing.
2163 */
2164
2165 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
2166 memset(&p, 0, sizeof(p));
2167 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
2168 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
2169 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
2170 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
2171 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
2172 }
2173 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
2174 }
2175
2176 /*
2177 * Convert the timing to bus clock counts.
2178 */
2179
Albert Lee75b1f2f2005-11-16 17:06:18 +08002180 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04002181
2182 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002183 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
2184 * S.M.A.R.T * and some other commands. We have to ensure that the
2185 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04002186 */
2187
2188 if (speed > XFER_PIO_4) {
2189 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
2190 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
2191 }
2192
2193 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002194 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04002195 */
2196
2197 if (t->act8b + t->rec8b < t->cyc8b) {
2198 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
2199 t->rec8b = t->cyc8b - t->act8b;
2200 }
2201
2202 if (t->active + t->recover < t->cycle) {
2203 t->active += (t->cycle - (t->active + t->recover)) / 2;
2204 t->recover = t->cycle - t->active;
2205 }
2206
2207 return 0;
2208}
2209
Tejun Heocf176e12006-04-02 17:54:46 +09002210/**
2211 * ata_down_xfermask_limit - adjust dev xfer masks downward
Tejun Heocf176e12006-04-02 17:54:46 +09002212 * @dev: Device to adjust xfer masks
2213 * @force_pio0: Force PIO0
2214 *
2215 * Adjust xfer masks of @dev downward. Note that this function
2216 * does not apply the change. Invoking ata_set_mode() afterwards
2217 * will apply the limit.
2218 *
2219 * LOCKING:
2220 * Inherited from caller.
2221 *
2222 * RETURNS:
2223 * 0 on success, negative errno on failure
2224 */
Tejun Heo3373efd2006-05-15 20:57:53 +09002225int ata_down_xfermask_limit(struct ata_device *dev, int force_pio0)
Tejun Heocf176e12006-04-02 17:54:46 +09002226{
2227 unsigned long xfer_mask;
2228 int highbit;
2229
2230 xfer_mask = ata_pack_xfermask(dev->pio_mask, dev->mwdma_mask,
2231 dev->udma_mask);
2232
2233 if (!xfer_mask)
2234 goto fail;
2235 /* don't gear down to MWDMA from UDMA, go directly to PIO */
2236 if (xfer_mask & ATA_MASK_UDMA)
2237 xfer_mask &= ~ATA_MASK_MWDMA;
2238
2239 highbit = fls(xfer_mask) - 1;
2240 xfer_mask &= ~(1 << highbit);
2241 if (force_pio0)
2242 xfer_mask &= 1 << ATA_SHIFT_PIO;
2243 if (!xfer_mask)
2244 goto fail;
2245
2246 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
2247 &dev->udma_mask);
2248
Tejun Heof15a1da2006-05-15 20:57:56 +09002249 ata_dev_printk(dev, KERN_WARNING, "limiting speed to %s\n",
2250 ata_mode_string(xfer_mask));
Tejun Heocf176e12006-04-02 17:54:46 +09002251
2252 return 0;
2253
2254 fail:
2255 return -EINVAL;
2256}
2257
Tejun Heo3373efd2006-05-15 20:57:53 +09002258static int ata_dev_set_mode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
Tejun Heobaa1e782006-11-01 18:39:27 +09002260 struct ata_eh_context *ehc = &dev->ap->eh_context;
Tejun Heo83206a22006-03-24 15:25:31 +09002261 unsigned int err_mask;
2262 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Tejun Heoe8384602006-04-02 18:51:53 +09002264 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 if (dev->xfer_shift == ATA_SHIFT_PIO)
2266 dev->flags |= ATA_DFLAG_PIO;
2267
Tejun Heo3373efd2006-05-15 20:57:53 +09002268 err_mask = ata_dev_set_xfermode(dev);
Tejun Heo83206a22006-03-24 15:25:31 +09002269 if (err_mask) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002270 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
2271 "(err_mask=0x%x)\n", err_mask);
Tejun Heo83206a22006-03-24 15:25:31 +09002272 return -EIO;
2273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Tejun Heobaa1e782006-11-01 18:39:27 +09002275 ehc->i.flags |= ATA_EHI_POST_SETMODE;
Tejun Heo3373efd2006-05-15 20:57:53 +09002276 rc = ata_dev_revalidate(dev, 0);
Tejun Heobaa1e782006-11-01 18:39:27 +09002277 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
Tejun Heo5eb45c02006-04-02 18:51:52 +09002278 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09002279 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09002280
Tejun Heo23e71c32006-03-06 04:31:57 +09002281 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
2282 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Tejun Heof15a1da2006-05-15 20:57:56 +09002284 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
2285 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
Tejun Heo83206a22006-03-24 15:25:31 +09002286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289/**
2290 * ata_set_mode - Program timings and issue SET FEATURES - XFER
2291 * @ap: port on which timings will be programmed
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002292 * @r_failed_dev: out paramter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 *
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002294 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2295 * ata_set_mode() fails, pointer to the failing device is
2296 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04002297 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002299 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002300 *
2301 * RETURNS:
2302 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 */
Tejun Heo1ad8e7f2006-04-02 18:51:53 +09002304int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305{
Tejun Heoe8e06192006-04-01 01:38:18 +09002306 struct ata_device *dev;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002307 int i, rc = 0, used_dma = 0, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Tejun Heo3adcebb2006-05-15 20:57:37 +09002309 /* has private set_mode? */
2310 if (ap->ops->set_mode) {
2311 /* FIXME: make ->set_mode handle no device case and
2312 * return error code and failing device on failure.
2313 */
2314 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heo02670bf2006-07-03 16:07:26 +09002315 if (ata_dev_ready(&ap->device[i])) {
Tejun Heo3adcebb2006-05-15 20:57:37 +09002316 ap->ops->set_mode(ap);
2317 break;
2318 }
2319 }
2320 return 0;
2321 }
2322
Tejun Heoa6d5a512006-03-06 04:31:57 +09002323 /* step 1: calculate xfer_mask */
2324 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoacf356b2006-03-24 14:07:50 +09002325 unsigned int pio_mask, dma_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002326
Tejun Heoe8e06192006-04-01 01:38:18 +09002327 dev = &ap->device[i];
2328
Tejun Heoe1211e32006-04-01 01:38:18 +09002329 if (!ata_dev_enabled(dev))
Tejun Heoa6d5a512006-03-06 04:31:57 +09002330 continue;
2331
Tejun Heo3373efd2006-05-15 20:57:53 +09002332 ata_dev_xfermask(dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09002333
Tejun Heoacf356b2006-03-24 14:07:50 +09002334 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
2335 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
2336 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
2337 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01002338
Tejun Heo4f659772006-04-01 01:38:18 +09002339 found = 1;
Alan Cox5444a6f2006-03-27 18:58:20 +01002340 if (dev->dma_mode)
2341 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002342 }
Tejun Heo4f659772006-04-01 01:38:18 +09002343 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002344 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002345
2346 /* step 2: always set host PIO timings */
Tejun Heoe8e06192006-04-01 01:38:18 +09002347 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2348 dev = &ap->device[i];
2349 if (!ata_dev_enabled(dev))
2350 continue;
2351
2352 if (!dev->pio_mode) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002353 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
Tejun Heoe8e06192006-04-01 01:38:18 +09002354 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002355 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09002356 }
2357
2358 dev->xfer_mode = dev->pio_mode;
2359 dev->xfer_shift = ATA_SHIFT_PIO;
2360 if (ap->ops->set_piomode)
2361 ap->ops->set_piomode(ap, dev);
2362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Tejun Heoa6d5a512006-03-06 04:31:57 +09002364 /* step 3: set host DMA timings */
Tejun Heoe8e06192006-04-01 01:38:18 +09002365 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2366 dev = &ap->device[i];
2367
2368 if (!ata_dev_enabled(dev) || !dev->dma_mode)
2369 continue;
2370
2371 dev->xfer_mode = dev->dma_mode;
2372 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
2373 if (ap->ops->set_dmamode)
2374 ap->ops->set_dmamode(ap, dev);
2375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
2377 /* step 4: update devices' xfer mode */
Tejun Heo83206a22006-03-24 15:25:31 +09002378 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoe8e06192006-04-01 01:38:18 +09002379 dev = &ap->device[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Tejun Heo02670bf2006-07-03 16:07:26 +09002381 /* don't udpate suspended devices' xfer mode */
2382 if (!ata_dev_ready(dev))
Tejun Heo83206a22006-03-24 15:25:31 +09002383 continue;
2384
Tejun Heo3373efd2006-05-15 20:57:53 +09002385 rc = ata_dev_set_mode(dev);
Tejun Heo5bbc53f2006-04-01 01:38:17 +09002386 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002387 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09002388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Tejun Heoe8e06192006-04-01 01:38:18 +09002390 /* Record simplex status. If we selected DMA then the other
2391 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01002392 */
Jeff Garzikcca39742006-08-24 03:19:22 -04002393 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
2394 ap->host->simplex_claimed = 1;
Alan Cox5444a6f2006-03-27 18:58:20 +01002395
Tejun Heoe8e06192006-04-01 01:38:18 +09002396 /* step5: chip specific finalisation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 if (ap->ops->post_set_mode)
2398 ap->ops->post_set_mode(ap);
2399
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002400 out:
2401 if (rc)
2402 *r_failed_dev = dev;
2403 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
2405
2406/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002407 * ata_tf_to_host - issue ATA taskfile to host controller
2408 * @ap: port to which command is being issued
2409 * @tf: ATA taskfile register set
2410 *
2411 * Issues ATA taskfile register set to ATA host controller,
2412 * with proper synchronization with interrupt handler and
2413 * other threads.
2414 *
2415 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002416 * spin_lock_irqsave(host lock)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002417 */
2418
2419static inline void ata_tf_to_host(struct ata_port *ap,
2420 const struct ata_taskfile *tf)
2421{
2422 ap->ops->tf_load(ap, tf);
2423 ap->ops->exec_command(ap, tf);
2424}
2425
2426/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 * ata_busy_sleep - sleep until BSY clears, or timeout
2428 * @ap: port containing status register to be polled
2429 * @tmout_pat: impatience timeout
2430 * @tmout: overall timeout
2431 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002432 * Sleep until ATA Status register bit BSY clears,
2433 * or a timeout occurs.
2434 *
Tejun Heod1adc1b2006-10-09 18:32:15 +09002435 * LOCKING:
2436 * Kernel thread context (may sleep).
2437 *
2438 * RETURNS:
2439 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 */
Tejun Heod1adc1b2006-10-09 18:32:15 +09002441int ata_busy_sleep(struct ata_port *ap,
2442 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
2444 unsigned long timer_start, timeout;
2445 u8 status;
2446
2447 status = ata_busy_wait(ap, ATA_BUSY, 300);
2448 timer_start = jiffies;
2449 timeout = timer_start + tmout_pat;
Tejun Heod1adc1b2006-10-09 18:32:15 +09002450 while (status != 0xff && (status & ATA_BUSY) &&
2451 time_before(jiffies, timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 msleep(50);
2453 status = ata_busy_wait(ap, ATA_BUSY, 3);
2454 }
2455
Tejun Heod1adc1b2006-10-09 18:32:15 +09002456 if (status != 0xff && (status & ATA_BUSY))
Tejun Heof15a1da2006-05-15 20:57:56 +09002457 ata_port_printk(ap, KERN_WARNING,
Jeff Garzik35aa7a42006-09-28 06:50:24 -04002458 "port is slow to respond, please be patient "
2459 "(Status 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
2461 timeout = timer_start + tmout;
Tejun Heod1adc1b2006-10-09 18:32:15 +09002462 while (status != 0xff && (status & ATA_BUSY) &&
2463 time_before(jiffies, timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 msleep(50);
2465 status = ata_chk_status(ap);
2466 }
2467
Tejun Heod1adc1b2006-10-09 18:32:15 +09002468 if (status == 0xff)
2469 return -ENODEV;
2470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 if (status & ATA_BUSY) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002472 ata_port_printk(ap, KERN_ERR, "port failed to respond "
Jeff Garzik35aa7a42006-09-28 06:50:24 -04002473 "(%lu secs, Status 0x%x)\n",
2474 tmout / HZ, status);
Tejun Heod1adc1b2006-10-09 18:32:15 +09002475 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 }
2477
2478 return 0;
2479}
2480
2481static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
2482{
2483 struct ata_ioports *ioaddr = &ap->ioaddr;
2484 unsigned int dev0 = devmask & (1 << 0);
2485 unsigned int dev1 = devmask & (1 << 1);
2486 unsigned long timeout;
2487
2488 /* if device 0 was found in ata_devchk, wait for its
2489 * BSY bit to clear
2490 */
2491 if (dev0)
2492 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2493
2494 /* if device 1 was found in ata_devchk, wait for
2495 * register access, then wait for BSY to clear
2496 */
2497 timeout = jiffies + ATA_TMOUT_BOOT;
2498 while (dev1) {
2499 u8 nsect, lbal;
2500
2501 ap->ops->dev_select(ap, 1);
2502 if (ap->flags & ATA_FLAG_MMIO) {
2503 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2504 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2505 } else {
2506 nsect = inb(ioaddr->nsect_addr);
2507 lbal = inb(ioaddr->lbal_addr);
2508 }
2509 if ((nsect == 1) && (lbal == 1))
2510 break;
2511 if (time_after(jiffies, timeout)) {
2512 dev1 = 0;
2513 break;
2514 }
2515 msleep(50); /* give drive a breather */
2516 }
2517 if (dev1)
2518 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2519
2520 /* is all this really necessary? */
2521 ap->ops->dev_select(ap, 0);
2522 if (dev1)
2523 ap->ops->dev_select(ap, 1);
2524 if (dev0)
2525 ap->ops->dev_select(ap, 0);
2526}
2527
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528static unsigned int ata_bus_softreset(struct ata_port *ap,
2529 unsigned int devmask)
2530{
2531 struct ata_ioports *ioaddr = &ap->ioaddr;
2532
2533 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2534
2535 /* software reset. causes dev0 to be selected */
2536 if (ap->flags & ATA_FLAG_MMIO) {
2537 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2538 udelay(20); /* FIXME: flush */
2539 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2540 udelay(20); /* FIXME: flush */
2541 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2542 } else {
2543 outb(ap->ctl, ioaddr->ctl_addr);
2544 udelay(10);
2545 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2546 udelay(10);
2547 outb(ap->ctl, ioaddr->ctl_addr);
2548 }
2549
2550 /* spec mandates ">= 2ms" before checking status.
2551 * We wait 150ms, because that was the magic delay used for
2552 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2553 * between when the ATA command register is written, and then
2554 * status is checked. Because waiting for "a while" before
2555 * checking status is fine, post SRST, we perform this magic
2556 * delay here as well.
Alan Cox09c7ad72006-03-22 15:52:40 +00002557 *
2558 * Old drivers/ide uses the 2mS rule and then waits for ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 */
2560 msleep(150);
2561
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002562 /* Before we perform post reset processing we want to see if
Tejun Heo298a41c2006-03-25 02:58:13 +09002563 * the bus shows 0xFF because the odd clown forgets the D7
2564 * pulldown resistor.
2565 */
Tejun Heod1adc1b2006-10-09 18:32:15 +09002566 if (ata_check_status(ap) == 0xFF)
2567 return 0;
Alan Cox09c7ad72006-03-22 15:52:40 +00002568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 ata_bus_post_reset(ap, devmask);
2570
2571 return 0;
2572}
2573
2574/**
2575 * ata_bus_reset - reset host port and associated ATA channel
2576 * @ap: port to reset
2577 *
2578 * This is typically the first time we actually start issuing
2579 * commands to the ATA channel. We wait for BSY to clear, then
2580 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2581 * result. Determine what devices, if any, are on the channel
2582 * by looking at the device 0/1 error register. Look at the signature
2583 * stored in each device's taskfile registers, to determine if
2584 * the device is ATA or ATAPI.
2585 *
2586 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002587 * PCI/etc. bus probe sem.
Jeff Garzikcca39742006-08-24 03:19:22 -04002588 * Obtains host lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 *
2590 * SIDE EFFECTS:
Tejun Heo198e0fe2006-04-02 18:51:52 +09002591 * Sets ATA_FLAG_DISABLED if bus reset fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 */
2593
2594void ata_bus_reset(struct ata_port *ap)
2595{
2596 struct ata_ioports *ioaddr = &ap->ioaddr;
2597 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2598 u8 err;
Tejun Heoaec5c3c2006-03-25 01:33:34 +09002599 unsigned int dev0, dev1 = 0, devmask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
2601 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2602
2603 /* determine if device 0/1 are present */
2604 if (ap->flags & ATA_FLAG_SATA_RESET)
2605 dev0 = 1;
2606 else {
2607 dev0 = ata_devchk(ap, 0);
2608 if (slave_possible)
2609 dev1 = ata_devchk(ap, 1);
2610 }
2611
2612 if (dev0)
2613 devmask |= (1 << 0);
2614 if (dev1)
2615 devmask |= (1 << 1);
2616
2617 /* select device 0 again */
2618 ap->ops->dev_select(ap, 0);
2619
2620 /* issue bus reset */
2621 if (ap->flags & ATA_FLAG_SRST)
Tejun Heoaec5c3c2006-03-25 01:33:34 +09002622 if (ata_bus_softreset(ap, devmask))
2623 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
2625 /*
2626 * determine by signature whether we have ATA or ATAPI devices
2627 */
Tejun Heob4dc7622006-01-24 17:05:22 +09002628 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09002630 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
2632 /* re-enable interrupts */
2633 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2634 ata_irq_on(ap);
2635
2636 /* is double-select really necessary? */
2637 if (ap->device[1].class != ATA_DEV_NONE)
2638 ap->ops->dev_select(ap, 1);
2639 if (ap->device[0].class != ATA_DEV_NONE)
2640 ap->ops->dev_select(ap, 0);
2641
2642 /* if no devices were detected, disable this port */
2643 if ((ap->device[0].class == ATA_DEV_NONE) &&
2644 (ap->device[1].class == ATA_DEV_NONE))
2645 goto err_out;
2646
2647 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2648 /* set up device control for ATA_FLAG_SATA_RESET */
2649 if (ap->flags & ATA_FLAG_MMIO)
2650 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2651 else
2652 outb(ap->ctl, ioaddr->ctl_addr);
2653 }
2654
2655 DPRINTK("EXIT\n");
2656 return;
2657
2658err_out:
Tejun Heof15a1da2006-05-15 20:57:56 +09002659 ata_port_printk(ap, KERN_ERR, "disabling port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 ap->ops->port_disable(ap);
2661
2662 DPRINTK("EXIT\n");
2663}
2664
Tejun Heod7bb4cc2006-05-31 18:27:46 +09002665/**
2666 * sata_phy_debounce - debounce SATA phy status
2667 * @ap: ATA port to debounce SATA phy status for
2668 * @params: timing parameters { interval, duratinon, timeout } in msec
2669 *
2670 * Make sure SStatus of @ap reaches stable state, determined by
2671 * holding the same value where DET is not 1 for @duration polled
2672 * every @interval, before @timeout. Timeout constraints the
2673 * beginning of the stable state. Because, after hot unplugging,
2674 * DET gets stuck at 1 on some controllers, this functions waits
2675 * until timeout then returns 0 if DET is stable at 1.
2676 *
2677 * LOCKING:
2678 * Kernel thread context (may sleep)
2679 *
2680 * RETURNS:
2681 * 0 on success, -errno on failure.
2682 */
2683int sata_phy_debounce(struct ata_port *ap, const unsigned long *params)
Tejun Heo7a7921e2006-02-02 18:20:00 +09002684{
Tejun Heod7bb4cc2006-05-31 18:27:46 +09002685 unsigned long interval_msec = params[0];
2686 unsigned long duration = params[1] * HZ / 1000;
2687 unsigned long timeout = jiffies + params[2] * HZ / 1000;
2688 unsigned long last_jiffies;
2689 u32 last, cur;
2690 int rc;
2691
2692 if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
2693 return rc;
2694 cur &= 0xf;
2695
2696 last = cur;
2697 last_jiffies = jiffies;
2698
2699 while (1) {
2700 msleep(interval_msec);
2701 if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
2702 return rc;
2703 cur &= 0xf;
2704
2705 /* DET stable? */
2706 if (cur == last) {
2707 if (cur == 1 && time_before(jiffies, timeout))
2708 continue;
2709 if (time_after(jiffies, last_jiffies + duration))
2710 return 0;
2711 continue;
2712 }
2713
2714 /* unstable, start over */
2715 last = cur;
2716 last_jiffies = jiffies;
2717
2718 /* check timeout */
2719 if (time_after(jiffies, timeout))
2720 return -EBUSY;
2721 }
2722}
2723
2724/**
2725 * sata_phy_resume - resume SATA phy
2726 * @ap: ATA port to resume SATA phy for
2727 * @params: timing parameters { interval, duratinon, timeout } in msec
2728 *
2729 * Resume SATA phy of @ap and debounce it.
2730 *
2731 * LOCKING:
2732 * Kernel thread context (may sleep)
2733 *
2734 * RETURNS:
2735 * 0 on success, -errno on failure.
2736 */
2737int sata_phy_resume(struct ata_port *ap, const unsigned long *params)
2738{
2739 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09002740 int rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09002741
Tejun Heo81952c52006-05-15 20:57:47 +09002742 if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
2743 return rc;
2744
Tejun Heo852ee162006-04-01 01:38:18 +09002745 scontrol = (scontrol & 0x0f0) | 0x300;
Tejun Heo81952c52006-05-15 20:57:47 +09002746
2747 if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
2748 return rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09002749
Tejun Heod7bb4cc2006-05-31 18:27:46 +09002750 /* Some PHYs react badly if SStatus is pounded immediately
2751 * after resuming. Delay 200ms before debouncing.
2752 */
2753 msleep(200);
Tejun Heo7a7921e2006-02-02 18:20:00 +09002754
Tejun Heod7bb4cc2006-05-31 18:27:46 +09002755 return sata_phy_debounce(ap, params);
Tejun Heo7a7921e2006-02-02 18:20:00 +09002756}
2757
Tejun Heof5914a42006-05-31 18:27:48 +09002758static void ata_wait_spinup(struct ata_port *ap)
2759{
2760 struct ata_eh_context *ehc = &ap->eh_context;
2761 unsigned long end, secs;
2762 int rc;
2763
2764 /* first, debounce phy if SATA */
2765 if (ap->cbl == ATA_CBL_SATA) {
Tejun Heoe9c83912006-07-03 16:07:26 +09002766 rc = sata_phy_debounce(ap, sata_deb_timing_hotplug);
Tejun Heof5914a42006-05-31 18:27:48 +09002767
2768 /* if debounced successfully and offline, no need to wait */
2769 if ((rc == 0 || rc == -EOPNOTSUPP) && ata_port_offline(ap))
2770 return;
2771 }
2772
2773 /* okay, let's give the drive time to spin up */
2774 end = ehc->i.hotplug_timestamp + ATA_SPINUP_WAIT * HZ / 1000;
2775 secs = ((end - jiffies) + HZ - 1) / HZ;
2776
2777 if (time_after(jiffies, end))
2778 return;
2779
2780 if (secs > 5)
2781 ata_port_printk(ap, KERN_INFO, "waiting for device to spin up "
2782 "(%lu secs)\n", secs);
2783
2784 schedule_timeout_uninterruptible(end - jiffies);
2785}
2786
2787/**
2788 * ata_std_prereset - prepare for reset
2789 * @ap: ATA port to be reset
2790 *
2791 * @ap is about to be reset. Initialize it.
2792 *
2793 * LOCKING:
2794 * Kernel thread context (may sleep)
2795 *
2796 * RETURNS:
2797 * 0 on success, -errno otherwise.
2798 */
2799int ata_std_prereset(struct ata_port *ap)
2800{
2801 struct ata_eh_context *ehc = &ap->eh_context;
Tejun Heoe9c83912006-07-03 16:07:26 +09002802 const unsigned long *timing = sata_ehc_deb_timing(ehc);
Tejun Heof5914a42006-05-31 18:27:48 +09002803 int rc;
2804
Tejun Heo28324302006-07-03 16:07:26 +09002805 /* handle link resume & hotplug spinup */
2806 if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
2807 (ap->flags & ATA_FLAG_HRST_TO_RESUME))
2808 ehc->i.action |= ATA_EH_HARDRESET;
2809
2810 if ((ehc->i.flags & ATA_EHI_HOTPLUGGED) &&
2811 (ap->flags & ATA_FLAG_SKIP_D2H_BSY))
2812 ata_wait_spinup(ap);
Tejun Heof5914a42006-05-31 18:27:48 +09002813
2814 /* if we're about to do hardreset, nothing more to do */
2815 if (ehc->i.action & ATA_EH_HARDRESET)
2816 return 0;
2817
2818 /* if SATA, resume phy */
2819 if (ap->cbl == ATA_CBL_SATA) {
Tejun Heof5914a42006-05-31 18:27:48 +09002820 rc = sata_phy_resume(ap, timing);
2821 if (rc && rc != -EOPNOTSUPP) {
2822 /* phy resume failed */
2823 ata_port_printk(ap, KERN_WARNING, "failed to resume "
2824 "link for reset (errno=%d)\n", rc);
2825 return rc;
2826 }
2827 }
2828
2829 /* Wait for !BSY if the controller can wait for the first D2H
2830 * Reg FIS and we don't know that no device is attached.
2831 */
2832 if (!(ap->flags & ATA_FLAG_SKIP_D2H_BSY) && !ata_port_offline(ap))
2833 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2834
2835 return 0;
2836}
2837
Tejun Heoc2bd5802006-01-24 17:05:22 +09002838/**
2839 * ata_std_softreset - reset host port via ATA SRST
2840 * @ap: port to reset
Tejun Heoc2bd5802006-01-24 17:05:22 +09002841 * @classes: resulting classes of attached devices
2842 *
Tejun Heo52783c52006-05-31 18:28:22 +09002843 * Reset host port using ATA SRST.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002844 *
2845 * LOCKING:
2846 * Kernel thread context (may sleep)
2847 *
2848 * RETURNS:
2849 * 0 on success, -errno otherwise.
2850 */
Tejun Heo2bf2cb22006-04-11 22:16:45 +09002851int ata_std_softreset(struct ata_port *ap, unsigned int *classes)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002852{
2853 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2854 unsigned int devmask = 0, err_mask;
2855 u8 err;
2856
2857 DPRINTK("ENTER\n");
2858
Tejun Heo81952c52006-05-15 20:57:47 +09002859 if (ata_port_offline(ap)) {
Tejun Heo3a397462006-02-10 23:58:48 +09002860 classes[0] = ATA_DEV_NONE;
2861 goto out;
2862 }
2863
Tejun Heoc2bd5802006-01-24 17:05:22 +09002864 /* determine if device 0/1 are present */
2865 if (ata_devchk(ap, 0))
2866 devmask |= (1 << 0);
2867 if (slave_possible && ata_devchk(ap, 1))
2868 devmask |= (1 << 1);
2869
Tejun Heoc2bd5802006-01-24 17:05:22 +09002870 /* select device 0 again */
2871 ap->ops->dev_select(ap, 0);
2872
2873 /* issue bus reset */
2874 DPRINTK("about to softreset, devmask=%x\n", devmask);
2875 err_mask = ata_bus_softreset(ap, devmask);
2876 if (err_mask) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002877 ata_port_printk(ap, KERN_ERR, "SRST failed (err_mask=0x%x)\n",
2878 err_mask);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002879 return -EIO;
2880 }
2881
2882 /* determine by signature whether we have ATA or ATAPI devices */
2883 classes[0] = ata_dev_try_classify(ap, 0, &err);
2884 if (slave_possible && err != 0x81)
2885 classes[1] = ata_dev_try_classify(ap, 1, &err);
2886
Tejun Heo3a397462006-02-10 23:58:48 +09002887 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002888 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2889 return 0;
2890}
2891
2892/**
Tejun Heob6103f62006-11-01 17:59:53 +09002893 * sata_port_hardreset - reset port via SATA phy reset
Tejun Heoc2bd5802006-01-24 17:05:22 +09002894 * @ap: port to reset
Tejun Heob6103f62006-11-01 17:59:53 +09002895 * @timing: timing parameters { interval, duratinon, timeout } in msec
Tejun Heoc2bd5802006-01-24 17:05:22 +09002896 *
2897 * SATA phy-reset host port using DET bits of SControl register.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002898 *
2899 * LOCKING:
2900 * Kernel thread context (may sleep)
2901 *
2902 * RETURNS:
2903 * 0 on success, -errno otherwise.
2904 */
Tejun Heob6103f62006-11-01 17:59:53 +09002905int sata_port_hardreset(struct ata_port *ap, const unsigned long *timing)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002906{
Tejun Heo852ee162006-04-01 01:38:18 +09002907 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09002908 int rc;
Tejun Heo852ee162006-04-01 01:38:18 +09002909
Tejun Heoc2bd5802006-01-24 17:05:22 +09002910 DPRINTK("ENTER\n");
2911
Tejun Heo3c567b72006-05-15 20:57:23 +09002912 if (sata_set_spd_needed(ap)) {
Tejun Heo1c3fae42006-04-02 20:53:28 +09002913 /* SATA spec says nothing about how to reconfigure
2914 * spd. To be on the safe side, turn off phy during
2915 * reconfiguration. This works for at least ICH7 AHCI
2916 * and Sil3124.
2917 */
Tejun Heo81952c52006-05-15 20:57:47 +09002918 if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09002919 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09002920
Martin Hicksa34b6fc2006-07-05 15:06:13 -04002921 scontrol = (scontrol & 0x0f0) | 0x304;
Tejun Heo81952c52006-05-15 20:57:47 +09002922
2923 if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09002924 goto out;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002925
Tejun Heo3c567b72006-05-15 20:57:23 +09002926 sata_set_spd(ap);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002927 }
2928
2929 /* issue phy wake/reset */
Tejun Heo81952c52006-05-15 20:57:47 +09002930 if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09002931 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09002932
Tejun Heo852ee162006-04-01 01:38:18 +09002933 scontrol = (scontrol & 0x0f0) | 0x301;
Tejun Heo81952c52006-05-15 20:57:47 +09002934
2935 if ((rc = sata_scr_write_flush(ap, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09002936 goto out;
Tejun Heoc2bd5802006-01-24 17:05:22 +09002937
Tejun Heo1c3fae42006-04-02 20:53:28 +09002938 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
Tejun Heoc2bd5802006-01-24 17:05:22 +09002939 * 10.4.2 says at least 1 ms.
2940 */
2941 msleep(1);
2942
Tejun Heo1c3fae42006-04-02 20:53:28 +09002943 /* bring phy back */
Tejun Heob6103f62006-11-01 17:59:53 +09002944 rc = sata_phy_resume(ap, timing);
2945 out:
2946 DPRINTK("EXIT, rc=%d\n", rc);
2947 return rc;
2948}
2949
2950/**
2951 * sata_std_hardreset - reset host port via SATA phy reset
2952 * @ap: port to reset
2953 * @class: resulting class of attached device
2954 *
2955 * SATA phy-reset host port using DET bits of SControl register,
2956 * wait for !BSY and classify the attached device.
2957 *
2958 * LOCKING:
2959 * Kernel thread context (may sleep)
2960 *
2961 * RETURNS:
2962 * 0 on success, -errno otherwise.
2963 */
2964int sata_std_hardreset(struct ata_port *ap, unsigned int *class)
2965{
2966 const unsigned long *timing = sata_ehc_deb_timing(&ap->eh_context);
2967 int rc;
2968
2969 DPRINTK("ENTER\n");
2970
2971 /* do hardreset */
2972 rc = sata_port_hardreset(ap, timing);
2973 if (rc) {
2974 ata_port_printk(ap, KERN_ERR,
2975 "COMRESET failed (errno=%d)\n", rc);
2976 return rc;
2977 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002978
Tejun Heoc2bd5802006-01-24 17:05:22 +09002979 /* TODO: phy layer with polling, timeouts, etc. */
Tejun Heo81952c52006-05-15 20:57:47 +09002980 if (ata_port_offline(ap)) {
Tejun Heoc2bd5802006-01-24 17:05:22 +09002981 *class = ATA_DEV_NONE;
2982 DPRINTK("EXIT, link offline\n");
2983 return 0;
2984 }
2985
2986 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002987 ata_port_printk(ap, KERN_ERR,
2988 "COMRESET failed (device not ready)\n");
Tejun Heoc2bd5802006-01-24 17:05:22 +09002989 return -EIO;
2990 }
2991
Tejun Heo3a397462006-02-10 23:58:48 +09002992 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2993
Tejun Heoc2bd5802006-01-24 17:05:22 +09002994 *class = ata_dev_try_classify(ap, 0, NULL);
2995
2996 DPRINTK("EXIT, class=%u\n", *class);
2997 return 0;
2998}
2999
3000/**
3001 * ata_std_postreset - standard postreset callback
3002 * @ap: the target ata_port
3003 * @classes: classes of attached devices
3004 *
3005 * This function is invoked after a successful reset. Note that
3006 * the device might have been reset more than once using
3007 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003008 *
Tejun Heoc2bd5802006-01-24 17:05:22 +09003009 * LOCKING:
3010 * Kernel thread context (may sleep)
3011 */
3012void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
3013{
Tejun Heodc2b3512006-05-15 20:58:00 +09003014 u32 serror;
3015
Tejun Heoc2bd5802006-01-24 17:05:22 +09003016 DPRINTK("ENTER\n");
3017
Tejun Heoc2bd5802006-01-24 17:05:22 +09003018 /* print link status */
Tejun Heo81952c52006-05-15 20:57:47 +09003019 sata_print_link_status(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003020
Tejun Heodc2b3512006-05-15 20:58:00 +09003021 /* clear SError */
3022 if (sata_scr_read(ap, SCR_ERROR, &serror) == 0)
3023 sata_scr_write(ap, SCR_ERROR, serror);
3024
Tejun Heo3a397462006-02-10 23:58:48 +09003025 /* re-enable interrupts */
Tejun Heoe3180492006-05-15 20:58:09 +09003026 if (!ap->ops->error_handler) {
3027 /* FIXME: hack. create a hook instead */
3028 if (ap->ioaddr.ctl_addr)
3029 ata_irq_on(ap);
3030 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09003031
3032 /* is double-select really necessary? */
3033 if (classes[0] != ATA_DEV_NONE)
3034 ap->ops->dev_select(ap, 1);
3035 if (classes[1] != ATA_DEV_NONE)
3036 ap->ops->dev_select(ap, 0);
3037
Tejun Heo3a397462006-02-10 23:58:48 +09003038 /* bail out if no device is present */
3039 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
3040 DPRINTK("EXIT, no device\n");
3041 return;
3042 }
3043
3044 /* set up device control */
3045 if (ap->ioaddr.ctl_addr) {
3046 if (ap->flags & ATA_FLAG_MMIO)
3047 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
3048 else
3049 outb(ap->ctl, ap->ioaddr.ctl_addr);
3050 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09003051
3052 DPRINTK("EXIT\n");
3053}
3054
Tejun Heoa62c0fc2006-01-24 17:05:22 +09003055/**
Tejun Heo623a3122006-03-05 17:55:58 +09003056 * ata_dev_same_device - Determine whether new ID matches configured device
Tejun Heo623a3122006-03-05 17:55:58 +09003057 * @dev: device to compare against
3058 * @new_class: class of the new device
3059 * @new_id: IDENTIFY page of the new device
3060 *
3061 * Compare @new_class and @new_id against @dev and determine
3062 * whether @dev is the device indicated by @new_class and
3063 * @new_id.
3064 *
3065 * LOCKING:
3066 * None.
3067 *
3068 * RETURNS:
3069 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3070 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003071static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3072 const u16 *new_id)
Tejun Heo623a3122006-03-05 17:55:58 +09003073{
3074 const u16 *old_id = dev->id;
3075 unsigned char model[2][41], serial[2][21];
3076 u64 new_n_sectors;
3077
3078 if (dev->class != new_class) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003079 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
3080 dev->class, new_class);
Tejun Heo623a3122006-03-05 17:55:58 +09003081 return 0;
3082 }
3083
3084 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
3085 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
3086 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
3087 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
3088 new_n_sectors = ata_id_n_sectors(new_id);
3089
3090 if (strcmp(model[0], model[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003091 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
3092 "'%s' != '%s'\n", model[0], model[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003093 return 0;
3094 }
3095
3096 if (strcmp(serial[0], serial[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003097 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
3098 "'%s' != '%s'\n", serial[0], serial[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003099 return 0;
3100 }
3101
3102 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003103 ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
3104 "%llu != %llu\n",
3105 (unsigned long long)dev->n_sectors,
3106 (unsigned long long)new_n_sectors);
Tejun Heo623a3122006-03-05 17:55:58 +09003107 return 0;
3108 }
3109
3110 return 1;
3111}
3112
3113/**
3114 * ata_dev_revalidate - Revalidate ATA device
Tejun Heo623a3122006-03-05 17:55:58 +09003115 * @dev: device to revalidate
Tejun Heobff04642006-11-10 18:08:10 +09003116 * @readid_flags: read ID flags
Tejun Heo623a3122006-03-05 17:55:58 +09003117 *
3118 * Re-read IDENTIFY page and make sure @dev is still attached to
3119 * the port.
3120 *
3121 * LOCKING:
3122 * Kernel thread context (may sleep)
3123 *
3124 * RETURNS:
3125 * 0 on success, negative errno otherwise
3126 */
Tejun Heobff04642006-11-10 18:08:10 +09003127int ata_dev_revalidate(struct ata_device *dev, unsigned int readid_flags)
Tejun Heo623a3122006-03-05 17:55:58 +09003128{
Tejun Heo5eb45c02006-04-02 18:51:52 +09003129 unsigned int class = dev->class;
Tejun Heof15a1da2006-05-15 20:57:56 +09003130 u16 *id = (void *)dev->ap->sector_buf;
Tejun Heo623a3122006-03-05 17:55:58 +09003131 int rc;
3132
Tejun Heo5eb45c02006-04-02 18:51:52 +09003133 if (!ata_dev_enabled(dev)) {
3134 rc = -ENODEV;
3135 goto fail;
3136 }
Tejun Heo623a3122006-03-05 17:55:58 +09003137
Tejun Heofe635c72006-05-15 20:57:35 +09003138 /* read ID data */
Tejun Heobff04642006-11-10 18:08:10 +09003139 rc = ata_dev_read_id(dev, &class, readid_flags, id);
Tejun Heo623a3122006-03-05 17:55:58 +09003140 if (rc)
3141 goto fail;
3142
3143 /* is the device still there? */
Tejun Heo3373efd2006-05-15 20:57:53 +09003144 if (!ata_dev_same_device(dev, class, id)) {
Tejun Heo623a3122006-03-05 17:55:58 +09003145 rc = -ENODEV;
3146 goto fail;
3147 }
3148
Tejun Heofe635c72006-05-15 20:57:35 +09003149 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heo623a3122006-03-05 17:55:58 +09003150
3151 /* configure device according to the new ID */
Tejun Heoefdaedc2006-11-01 18:38:52 +09003152 rc = ata_dev_configure(dev);
Tejun Heo5eb45c02006-04-02 18:51:52 +09003153 if (rc == 0)
3154 return 0;
Tejun Heo623a3122006-03-05 17:55:58 +09003155
3156 fail:
Tejun Heof15a1da2006-05-15 20:57:56 +09003157 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
Tejun Heo623a3122006-03-05 17:55:58 +09003158 return rc;
3159}
3160
Alan Cox6919a0a2006-10-27 19:08:46 -07003161struct ata_blacklist_entry {
3162 const char *model_num;
3163 const char *model_rev;
3164 unsigned long horkage;
3165};
3166
3167static const struct ata_blacklist_entry ata_device_blacklist [] = {
3168 /* Devices with DMA related problems under Linux */
3169 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
3170 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
3171 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
3172 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
3173 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
3174 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
3175 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
3176 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
3177 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
3178 { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
3179 { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
3180 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
3181 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
3182 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
3183 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
3184 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
3185 { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
3186 { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
3187 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
3188 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
3189 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
3190 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
3191 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
3192 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
3193 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
3194 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
3195 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
3196 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
3197 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
3198 { "SAMSUNG CD-ROM SN-124","N001", ATA_HORKAGE_NODMA },
3199
3200 /* Devices we expect to fail diagnostics */
3201
3202 /* Devices where NCQ should be avoided */
3203 /* NCQ is slow */
3204 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
3205
3206 /* Devices with NCQ limits */
3207
3208 /* End Marker */
3209 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003211
Alan Coxf4b15fe2006-03-22 15:54:04 +00003212static int ata_strim(char *s, size_t len)
3213{
3214 len = strnlen(s, len);
3215
3216 /* ATAPI specifies that empty space is blank-filled; remove blanks */
3217 while ((len > 0) && (s[len - 1] == ' ')) {
3218 len--;
3219 s[len] = 0;
3220 }
3221 return len;
3222}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
Alan Cox6919a0a2006-10-27 19:08:46 -07003224unsigned long ata_device_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
Alan Coxf4b15fe2006-03-22 15:54:04 +00003226 unsigned char model_num[40];
3227 unsigned char model_rev[16];
3228 unsigned int nlen, rlen;
Alan Cox6919a0a2006-10-27 19:08:46 -07003229 const struct ata_blacklist_entry *ad = ata_device_blacklist;
Albert Lee3a778272006-06-22 13:00:25 +08003230
Alan Coxf4b15fe2006-03-22 15:54:04 +00003231 ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
3232 sizeof(model_num));
3233 ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS,
3234 sizeof(model_rev));
3235 nlen = ata_strim(model_num, sizeof(model_num));
3236 rlen = ata_strim(model_rev, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Alan Cox6919a0a2006-10-27 19:08:46 -07003238 while (ad->model_num) {
3239 if (!strncmp(ad->model_num, model_num, nlen)) {
3240 if (ad->model_rev == NULL)
3241 return ad->horkage;
3242 if (!strncmp(ad->model_rev, model_rev, rlen))
3243 return ad->horkage;
Alan Coxf4b15fe2006-03-22 15:54:04 +00003244 }
Alan Cox6919a0a2006-10-27 19:08:46 -07003245 ad++;
Alan Coxf4b15fe2006-03-22 15:54:04 +00003246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 return 0;
3248}
3249
Alan Cox6919a0a2006-10-27 19:08:46 -07003250static int ata_dma_blacklisted(const struct ata_device *dev)
3251{
3252 /* We don't support polling DMA.
3253 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
3254 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
3255 */
3256 if ((dev->ap->flags & ATA_FLAG_PIO_POLLING) &&
3257 (dev->flags & ATA_DFLAG_CDB_INTR))
3258 return 1;
3259 return (ata_device_blacklisted(dev) & ATA_HORKAGE_NODMA) ? 1 : 0;
3260}
3261
Tejun Heoa6d5a512006-03-06 04:31:57 +09003262/**
3263 * ata_dev_xfermask - Compute supported xfermask of the given device
Tejun Heoa6d5a512006-03-06 04:31:57 +09003264 * @dev: Device to compute xfermask for
3265 *
Tejun Heoacf356b2006-03-24 14:07:50 +09003266 * Compute supported xfermask of @dev and store it in
3267 * dev->*_mask. This function is responsible for applying all
3268 * known limits including host controller limits, device
3269 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09003270 *
3271 * LOCKING:
3272 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09003273 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003274static void ata_dev_xfermask(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275{
Tejun Heo3373efd2006-05-15 20:57:53 +09003276 struct ata_port *ap = dev->ap;
Jeff Garzikcca39742006-08-24 03:19:22 -04003277 struct ata_host *host = ap->host;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003278 unsigned long xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Tejun Heo37deecb2006-08-10 16:59:07 +09003280 /* controller modes available */
Tejun Heo565083e2006-04-02 17:54:47 +09003281 xfer_mask = ata_pack_xfermask(ap->pio_mask,
3282 ap->mwdma_mask, ap->udma_mask);
3283
3284 /* Apply cable rule here. Don't apply it early because when
3285 * we handle hot plug the cable type can itself change.
3286 */
3287 if (ap->cbl == ATA_CBL_PATA40)
3288 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
Alan Coxfc085152006-10-10 14:28:11 -07003289 /* Apply drive side cable rule. Unknown or 80 pin cables reported
3290 * host side are checked drive side as well. Cases where we know a
3291 * 40wire cable is used safely for 80 are not checked here.
3292 */
3293 if (ata_drive_40wire(dev->id) && (ap->cbl == ATA_CBL_PATA_UNK || ap->cbl == ATA_CBL_PATA80))
3294 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
3295
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
Tejun Heo37deecb2006-08-10 16:59:07 +09003297 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
3298 dev->mwdma_mask, dev->udma_mask);
3299 xfer_mask &= ata_id_xfermask(dev->id);
Tejun Heo565083e2006-04-02 17:54:47 +09003300
Alan Coxb352e572006-08-10 18:52:12 +01003301 /*
3302 * CFA Advanced TrueIDE timings are not allowed on a shared
3303 * cable
3304 */
3305 if (ata_dev_pair(dev)) {
3306 /* No PIO5 or PIO6 */
3307 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
3308 /* No MWDMA3 or MWDMA 4 */
3309 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
3310 }
3311
Tejun Heo37deecb2006-08-10 16:59:07 +09003312 if (ata_dma_blacklisted(dev)) {
3313 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Tejun Heof15a1da2006-05-15 20:57:56 +09003314 ata_dev_printk(dev, KERN_WARNING,
3315 "device is on DMA blacklist, disabling DMA\n");
Tejun Heo37deecb2006-08-10 16:59:07 +09003316 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09003317
Jeff Garzikcca39742006-08-24 03:19:22 -04003318 if ((host->flags & ATA_HOST_SIMPLEX) && host->simplex_claimed) {
Tejun Heo37deecb2006-08-10 16:59:07 +09003319 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
3320 ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
3321 "other device, disabling DMA\n");
Alan Cox5444a6f2006-03-27 18:58:20 +01003322 }
Tejun Heo565083e2006-04-02 17:54:47 +09003323
Alan Cox5444a6f2006-03-27 18:58:20 +01003324 if (ap->ops->mode_filter)
3325 xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask);
3326
Tejun Heo565083e2006-04-02 17:54:47 +09003327 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
3328 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329}
3330
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 * @dev: Device to which command will be sent
3334 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003335 * Issue SET FEATURES - XFER MODE command to device @dev
3336 * on port @ap.
3337 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003339 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09003340 *
3341 * RETURNS:
3342 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 */
3344
Tejun Heo3373efd2006-05-15 20:57:53 +09003345static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346{
Tejun Heoa0123702005-12-13 14:49:31 +09003347 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09003348 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
3350 /* set up set-features taskfile */
3351 DPRINTK("set features - xfer mode\n");
3352
Tejun Heo3373efd2006-05-15 20:57:53 +09003353 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09003354 tf.command = ATA_CMD_SET_FEATURES;
3355 tf.feature = SETFEATURES_XFER;
3356 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3357 tf.protocol = ATA_PROT_NODATA;
3358 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Tejun Heo3373efd2006-05-15 20:57:53 +09003360 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
Tejun Heo83206a22006-03-24 15:25:31 +09003362 DPRINTK("EXIT, err_mask=%x\n", err_mask);
3363 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364}
3365
3366/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04003367 * ata_dev_init_params - Issue INIT DEV PARAMS command
Albert Lee8bf62ec2005-05-12 15:29:42 -04003368 * @dev: Device to which command will be sent
Randy Dunlape2a7f772006-05-18 10:50:18 -07003369 * @heads: Number of heads (taskfile parameter)
3370 * @sectors: Number of sectors (taskfile parameter)
Albert Lee8bf62ec2005-05-12 15:29:42 -04003371 *
3372 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09003373 * Kernel thread context (may sleep)
3374 *
3375 * RETURNS:
3376 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ec2005-05-12 15:29:42 -04003377 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003378static unsigned int ata_dev_init_params(struct ata_device *dev,
3379 u16 heads, u16 sectors)
Albert Lee8bf62ec2005-05-12 15:29:42 -04003380{
Tejun Heoa0123702005-12-13 14:49:31 +09003381 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09003382 unsigned int err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04003383
3384 /* Number of sectors per track 1-255. Number of heads 1-16 */
3385 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08003386 return AC_ERR_INVALID;
Albert Lee8bf62ec2005-05-12 15:29:42 -04003387
3388 /* set up init dev params taskfile */
3389 DPRINTK("init dev params \n");
3390
Tejun Heo3373efd2006-05-15 20:57:53 +09003391 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09003392 tf.command = ATA_CMD_INIT_DEV_PARAMS;
3393 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3394 tf.protocol = ATA_PROT_NODATA;
3395 tf.nsect = sectors;
3396 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04003397
Tejun Heo3373efd2006-05-15 20:57:53 +09003398 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Albert Lee8bf62ec2005-05-12 15:29:42 -04003399
Tejun Heo6aff8f12006-02-15 18:24:09 +09003400 DPRINTK("EXIT, err_mask=%x\n", err_mask);
3401 return err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04003402}
3403
3404/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003405 * ata_sg_clean - Unmap DMA memory associated with command
3406 * @qc: Command containing DMA memory to be released
3407 *
3408 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 *
3410 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003411 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 */
3413
3414static void ata_sg_clean(struct ata_queued_cmd *qc)
3415{
3416 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003417 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003419 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
Tejun Heoa4631472006-02-11 19:11:13 +09003421 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
3422 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
3424 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05003425 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003427 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003429 /* if we padded the buffer out to 32-bit bound, and data
3430 * xfer direction is from-device, we must copy from the
3431 * pad buffer back into the supplied buffer
3432 */
3433 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
3434 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3435
3436 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05003437 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06003438 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003439 /* restore last sg */
3440 sg[qc->orig_n_elem - 1].length += qc->pad_len;
3441 if (pad_buf) {
3442 struct scatterlist *psg = &qc->pad_sgent;
3443 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3444 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003445 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003446 }
3447 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09003448 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06003449 dma_unmap_single(ap->dev,
Jeff Garzike1410f22005-11-14 14:06:26 -05003450 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
3451 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003452 /* restore sg */
3453 sg->length += qc->pad_len;
3454 if (pad_buf)
3455 memcpy(qc->buf_virt + sg->length - qc->pad_len,
3456 pad_buf, qc->pad_len);
3457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
3459 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003460 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461}
3462
3463/**
3464 * ata_fill_sg - Fill PCI IDE PRD table
3465 * @qc: Metadata associated with taskfile to be transferred
3466 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003467 * Fill PCI IDE PRD (scatter-gather) table with segments
3468 * associated with the current disk command.
3469 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003471 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 *
3473 */
3474static void ata_fill_sg(struct ata_queued_cmd *qc)
3475{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003477 struct scatterlist *sg;
3478 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
Tejun Heoa4631472006-02-11 19:11:13 +09003480 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05003481 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
3483 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003484 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 u32 addr, offset;
3486 u32 sg_len, len;
3487
3488 /* determine if physical DMA addr spans 64K boundary.
3489 * Note h/w doesn't support 64-bit, so we unconditionally
3490 * truncate dma_addr_t to u32.
3491 */
3492 addr = (u32) sg_dma_address(sg);
3493 sg_len = sg_dma_len(sg);
3494
3495 while (sg_len) {
3496 offset = addr & 0xffff;
3497 len = sg_len;
3498 if ((offset + sg_len) > 0x10000)
3499 len = 0x10000 - offset;
3500
3501 ap->prd[idx].addr = cpu_to_le32(addr);
3502 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
3503 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
3504
3505 idx++;
3506 sg_len -= len;
3507 addr += len;
3508 }
3509 }
3510
3511 if (idx)
3512 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
3513}
3514/**
3515 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
3516 * @qc: Metadata associated with taskfile to check
3517 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003518 * Allow low-level driver to filter ATA PACKET commands, returning
3519 * a status indicating whether or not it is OK to use DMA for the
3520 * supplied PACKET command.
3521 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003523 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04003524 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 * RETURNS: 0 when ATAPI DMA can be used
3526 * nonzero otherwise
3527 */
3528int ata_check_atapi_dma(struct ata_queued_cmd *qc)
3529{
3530 struct ata_port *ap = qc->ap;
3531 int rc = 0; /* Assume ATAPI DMA is OK by default */
3532
3533 if (ap->ops->check_atapi_dma)
3534 rc = ap->ops->check_atapi_dma(qc);
3535
3536 return rc;
3537}
3538/**
3539 * ata_qc_prep - Prepare taskfile for submission
3540 * @qc: Metadata associated with taskfile to be prepared
3541 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003542 * Prepare ATA taskfile for submission.
3543 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003545 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 */
3547void ata_qc_prep(struct ata_queued_cmd *qc)
3548{
3549 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
3550 return;
3551
3552 ata_fill_sg(qc);
3553}
3554
Brian Kinge46834c2006-03-17 17:04:03 -06003555void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
3556
Jeff Garzik0cba6322005-05-30 19:49:12 -04003557/**
3558 * ata_sg_init_one - Associate command with memory buffer
3559 * @qc: Command to be associated
3560 * @buf: Memory buffer
3561 * @buflen: Length of memory buffer, in bytes.
3562 *
3563 * Initialize the data-related elements of queued_cmd @qc
3564 * to point to a single memory buffer, @buf of byte length @buflen.
3565 *
3566 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003567 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04003568 */
3569
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
3571{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 qc->flags |= ATA_QCFLAG_SINGLE;
3573
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003574 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003576 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 qc->buf_virt = buf;
Brian King233277c2006-06-07 11:25:31 -05003578 qc->nbytes = buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Tejun Heo61c05962006-11-14 22:35:43 +09003580 sg_init_one(&qc->sgent, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581}
3582
Jeff Garzik0cba6322005-05-30 19:49:12 -04003583/**
3584 * ata_sg_init - Associate command with scatter-gather table.
3585 * @qc: Command to be associated
3586 * @sg: Scatter-gather table.
3587 * @n_elem: Number of elements in s/g table.
3588 *
3589 * Initialize the data-related elements of queued_cmd @qc
3590 * to point to a scatter-gather table @sg, containing @n_elem
3591 * elements.
3592 *
3593 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003594 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04003595 */
3596
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
3598 unsigned int n_elem)
3599{
3600 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003601 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003603 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604}
3605
3606/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003607 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
3608 * @qc: Command with memory buffer to be mapped.
3609 *
3610 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 *
3612 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003613 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 *
3615 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003616 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 */
3618
3619static int ata_sg_setup_one(struct ata_queued_cmd *qc)
3620{
3621 struct ata_port *ap = qc->ap;
3622 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003623 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003625 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003627 /* we must lengthen transfers to end on a 32-bit boundary */
3628 qc->pad_len = sg->length & 3;
3629 if (qc->pad_len) {
3630 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3631 struct scatterlist *psg = &qc->pad_sgent;
3632
Tejun Heoa4631472006-02-11 19:11:13 +09003633 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003634
3635 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3636
3637 if (qc->tf.flags & ATA_TFLAG_WRITE)
3638 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
3639 qc->pad_len);
3640
3641 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3642 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3643 /* trim sg */
3644 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003645 if (sg->length == 0)
3646 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003647
3648 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
3649 sg->length, qc->pad_len);
3650 }
3651
Tejun Heo2e242fa2006-02-20 23:48:38 +09003652 if (trim_sg) {
3653 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05003654 goto skip_map;
3655 }
3656
Brian King2f1f6102006-03-23 17:30:15 -06003657 dma_address = dma_map_single(ap->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04003658 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003659 if (dma_mapping_error(dma_address)) {
3660 /* restore sg */
3661 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
3665 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04003666 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
Tejun Heo2e242fa2006-02-20 23:48:38 +09003668skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
3670 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3671
3672 return 0;
3673}
3674
3675/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003676 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
3677 * @qc: Command with scatter-gather table to be mapped.
3678 *
3679 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 *
3681 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003682 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 *
3684 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003685 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 *
3687 */
3688
3689static int ata_sg_setup(struct ata_queued_cmd *qc)
3690{
3691 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003692 struct scatterlist *sg = qc->__sg;
3693 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05003694 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
3696 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa4631472006-02-11 19:11:13 +09003697 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003699 /* we must lengthen transfers to end on a 32-bit boundary */
3700 qc->pad_len = lsg->length & 3;
3701 if (qc->pad_len) {
3702 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3703 struct scatterlist *psg = &qc->pad_sgent;
3704 unsigned int offset;
3705
Tejun Heoa4631472006-02-11 19:11:13 +09003706 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003707
3708 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3709
3710 /*
3711 * psg->page/offset are used to copy to-be-written
3712 * data in this function or read data in ata_sg_clean.
3713 */
3714 offset = lsg->offset + lsg->length - qc->pad_len;
3715 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3716 psg->offset = offset_in_page(offset);
3717
3718 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3719 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3720 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003721 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003722 }
3723
3724 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3725 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3726 /* trim last sg */
3727 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05003728 if (lsg->length == 0)
3729 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003730
3731 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3732 qc->n_elem - 1, lsg->length, qc->pad_len);
3733 }
3734
Jeff Garzike1410f22005-11-14 14:06:26 -05003735 pre_n_elem = qc->n_elem;
3736 if (trim_sg && pre_n_elem)
3737 pre_n_elem--;
3738
3739 if (!pre_n_elem) {
3740 n_elem = 0;
3741 goto skip_map;
3742 }
3743
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 dir = qc->dma_dir;
Brian King2f1f6102006-03-23 17:30:15 -06003745 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003746 if (n_elem < 1) {
3747 /* restore last sg */
3748 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
3752 DPRINTK("%d sg elements mapped\n", n_elem);
3753
Jeff Garzike1410f22005-11-14 14:06:26 -05003754skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 qc->n_elem = n_elem;
3756
3757 return 0;
3758}
3759
3760/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003761 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003762 * @buf: Buffer to swap
3763 * @buf_words: Number of 16-bit words in buffer.
3764 *
3765 * Swap halves of 16-bit words if needed to convert from
3766 * little-endian byte order to native cpu byte order, or
3767 * vice-versa.
3768 *
3769 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003770 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003771 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772void swap_buf_le16(u16 *buf, unsigned int buf_words)
3773{
3774#ifdef __BIG_ENDIAN
3775 unsigned int i;
3776
3777 for (i = 0; i < buf_words; i++)
3778 buf[i] = le16_to_cpu(buf[i]);
3779#endif /* __BIG_ENDIAN */
3780}
3781
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003782/**
3783 * ata_mmio_data_xfer - Transfer data by MMIO
Jeff Garzikbf717b112006-06-13 20:27:03 -04003784 * @adev: device for this I/O
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003785 * @buf: data buffer
3786 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003787 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003788 *
3789 * Transfer data from/to the device data register by MMIO.
3790 *
3791 * LOCKING:
3792 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003793 */
3794
Tejun Heo88574552006-06-25 20:00:35 +09003795void ata_mmio_data_xfer(struct ata_device *adev, unsigned char *buf,
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003796 unsigned int buflen, int write_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797{
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003798 struct ata_port *ap = adev->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 unsigned int i;
3800 unsigned int words = buflen >> 1;
3801 u16 *buf16 = (u16 *) buf;
3802 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3803
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003804 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 if (write_data) {
3806 for (i = 0; i < words; i++)
3807 writew(le16_to_cpu(buf16[i]), mmio);
3808 } else {
3809 for (i = 0; i < words; i++)
3810 buf16[i] = cpu_to_le16(readw(mmio));
3811 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003812
3813 /* Transfer trailing 1 byte, if any. */
3814 if (unlikely(buflen & 0x01)) {
3815 u16 align_buf[1] = { 0 };
3816 unsigned char *trailing_buf = buf + buflen - 1;
3817
3818 if (write_data) {
3819 memcpy(align_buf, trailing_buf, 1);
3820 writew(le16_to_cpu(align_buf[0]), mmio);
3821 } else {
3822 align_buf[0] = cpu_to_le16(readw(mmio));
3823 memcpy(trailing_buf, align_buf, 1);
3824 }
3825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826}
3827
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003828/**
3829 * ata_pio_data_xfer - Transfer data by PIO
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003830 * @adev: device to target
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003831 * @buf: data buffer
3832 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003833 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003834 *
3835 * Transfer data from/to the device data register by PIO.
3836 *
3837 * LOCKING:
3838 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003839 */
3840
Tejun Heo88574552006-06-25 20:00:35 +09003841void ata_pio_data_xfer(struct ata_device *adev, unsigned char *buf,
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003842 unsigned int buflen, int write_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843{
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003844 struct ata_port *ap = adev->ap;
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003845 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003847 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003849 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003851 insw(ap->ioaddr.data_addr, buf, words);
3852
3853 /* Transfer trailing 1 byte, if any. */
3854 if (unlikely(buflen & 0x01)) {
3855 u16 align_buf[1] = { 0 };
3856 unsigned char *trailing_buf = buf + buflen - 1;
3857
3858 if (write_data) {
3859 memcpy(align_buf, trailing_buf, 1);
3860 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3861 } else {
3862 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3863 memcpy(trailing_buf, align_buf, 1);
3864 }
3865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866}
3867
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003868/**
Alan Cox75e99582006-05-24 14:14:41 +01003869 * ata_pio_data_xfer_noirq - Transfer data by PIO
3870 * @adev: device to target
3871 * @buf: data buffer
3872 * @buflen: buffer length
3873 * @write_data: read/write
3874 *
Tejun Heo88574552006-06-25 20:00:35 +09003875 * Transfer data from/to the device data register by PIO. Do the
Alan Cox75e99582006-05-24 14:14:41 +01003876 * transfer with interrupts disabled.
3877 *
3878 * LOCKING:
3879 * Inherited from caller.
3880 */
3881
3882void ata_pio_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
3883 unsigned int buflen, int write_data)
3884{
3885 unsigned long flags;
3886 local_irq_save(flags);
3887 ata_pio_data_xfer(adev, buf, buflen, write_data);
3888 local_irq_restore(flags);
3889}
3890
3891
3892/**
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003893 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3894 * @qc: Command on going
3895 *
3896 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3897 *
3898 * LOCKING:
3899 * Inherited from caller.
3900 */
3901
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902static void ata_pio_sector(struct ata_queued_cmd *qc)
3903{
3904 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003905 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 struct ata_port *ap = qc->ap;
3907 struct page *page;
3908 unsigned int offset;
3909 unsigned char *buf;
3910
3911 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003912 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
3914 page = sg[qc->cursg].page;
3915 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3916
3917 /* get the current page and offset */
3918 page = nth_page(page, (offset >> PAGE_SHIFT));
3919 offset %= PAGE_SIZE;
3920
Albert Lee7282aa42005-10-09 09:46:07 -04003921 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3922
Albert Lee91b8b312005-10-09 09:48:44 -04003923 if (PageHighMem(page)) {
3924 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003925
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003926 /* FIXME: use a bounce buffer */
Albert Lee91b8b312005-10-09 09:48:44 -04003927 local_irq_save(flags);
3928 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003929
Albert Lee91b8b312005-10-09 09:48:44 -04003930 /* do the actual data transfer */
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003931 ap->ops->data_xfer(qc->dev, buf + offset, ATA_SECT_SIZE, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04003932
3933 kunmap_atomic(buf, KM_IRQ0);
3934 local_irq_restore(flags);
3935 } else {
3936 buf = page_address(page);
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003937 ap->ops->data_xfer(qc->dev, buf + offset, ATA_SECT_SIZE, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04003938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
3940 qc->cursect++;
3941 qc->cursg_ofs++;
3942
Albert Lee32529e02005-05-26 03:49:42 -04003943 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 qc->cursg++;
3945 qc->cursg_ofs = 0;
3946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003949/**
Albert Lee07f6f7d2005-11-01 19:33:20 +08003950 * ata_pio_sectors - Transfer one or many 512-byte sectors.
3951 * @qc: Command on going
3952 *
Jeff Garzikc81e29b2006-05-24 01:49:12 -04003953 * Transfer one or many ATA_SECT_SIZE of data from/to the
Albert Lee07f6f7d2005-11-01 19:33:20 +08003954 * ATA device for the DRQ request.
3955 *
3956 * LOCKING:
3957 * Inherited from caller.
3958 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
Albert Lee07f6f7d2005-11-01 19:33:20 +08003960static void ata_pio_sectors(struct ata_queued_cmd *qc)
3961{
3962 if (is_multi_taskfile(&qc->tf)) {
3963 /* READ/WRITE MULTIPLE */
3964 unsigned int nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
Jeff Garzik587005d2006-02-11 18:17:32 -05003966 WARN_ON(qc->dev->multi_count == 0);
Albert Lee07f6f7d2005-11-01 19:33:20 +08003967
3968 nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
3969 while (nsect--)
3970 ata_pio_sector(qc);
3971 } else
3972 ata_pio_sector(qc);
3973}
3974
3975/**
Albert Leec71c1852005-10-04 06:03:45 -04003976 * atapi_send_cdb - Write CDB bytes to hardware
3977 * @ap: Port to which ATAPI device is attached.
3978 * @qc: Taskfile currently active
3979 *
3980 * When device has indicated its readiness to accept
3981 * a CDB, this function is called. Send the CDB.
3982 *
3983 * LOCKING:
3984 * caller.
3985 */
3986
3987static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3988{
3989 /* send SCSI cdb */
3990 DPRINTK("send cdb\n");
Jeff Garzikdb024d52006-02-13 00:23:57 -05003991 WARN_ON(qc->dev->cdb_len < 12);
Albert Leec71c1852005-10-04 06:03:45 -04003992
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003993 ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
Albert Leec71c1852005-10-04 06:03:45 -04003994 ata_altstatus(ap); /* flush */
3995
3996 switch (qc->tf.protocol) {
3997 case ATA_PROT_ATAPI:
3998 ap->hsm_task_state = HSM_ST;
3999 break;
4000 case ATA_PROT_ATAPI_NODATA:
4001 ap->hsm_task_state = HSM_ST_LAST;
4002 break;
4003 case ATA_PROT_ATAPI_DMA:
4004 ap->hsm_task_state = HSM_ST_LAST;
4005 /* initiate bmdma */
4006 ap->ops->bmdma_start(qc);
4007 break;
4008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009}
4010
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004011/**
4012 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
4013 * @qc: Command on going
4014 * @bytes: number of bytes
4015 *
4016 * Transfer Transfer data from/to the ATAPI device.
4017 *
4018 * LOCKING:
4019 * Inherited from caller.
4020 *
4021 */
4022
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
4024{
4025 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004026 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 struct ata_port *ap = qc->ap;
4028 struct page *page;
4029 unsigned char *buf;
4030 unsigned int offset, count;
4031
Albert Lee563a6e12005-08-12 14:17:50 +08004032 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08004033 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
4035next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08004036 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04004037 /*
Albert Lee563a6e12005-08-12 14:17:50 +08004038 * The end of qc->sg is reached and the device expects
4039 * more data to transfer. In order not to overrun qc->sg
4040 * and fulfill length specified in the byte count register,
4041 * - for read case, discard trailing data from the device
4042 * - for write case, padding zero data to the device
4043 */
4044 u16 pad_buf[1] = { 0 };
4045 unsigned int words = bytes >> 1;
4046 unsigned int i;
4047
4048 if (words) /* warning if bytes > 1 */
Tejun Heof15a1da2006-05-15 20:57:56 +09004049 ata_dev_printk(qc->dev, KERN_WARNING,
4050 "%u bytes trailing data\n", bytes);
Albert Lee563a6e12005-08-12 14:17:50 +08004051
4052 for (i = 0; i < words; i++)
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004053 ap->ops->data_xfer(qc->dev, (unsigned char*)pad_buf, 2, do_write);
Albert Lee563a6e12005-08-12 14:17:50 +08004054
Albert Lee14be71f2005-09-27 17:36:35 +08004055 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08004056 return;
4057 }
4058
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004059 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 page = sg->page;
4062 offset = sg->offset + qc->cursg_ofs;
4063
4064 /* get the current page and offset */
4065 page = nth_page(page, (offset >> PAGE_SHIFT));
4066 offset %= PAGE_SIZE;
4067
Albert Lee6952df02005-06-06 15:56:03 +08004068 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04004069 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
4071 /* don't cross page boundaries */
4072 count = min(count, (unsigned int)PAGE_SIZE - offset);
4073
Albert Lee7282aa42005-10-09 09:46:07 -04004074 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4075
Albert Lee91b8b312005-10-09 09:48:44 -04004076 if (PageHighMem(page)) {
4077 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04004078
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004079 /* FIXME: use bounce buffer */
Albert Lee91b8b312005-10-09 09:48:44 -04004080 local_irq_save(flags);
4081 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04004082
Albert Lee91b8b312005-10-09 09:48:44 -04004083 /* do the actual data transfer */
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004084 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004085
4086 kunmap_atomic(buf, KM_IRQ0);
4087 local_irq_restore(flags);
4088 } else {
4089 buf = page_address(page);
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004090 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
4093 bytes -= count;
4094 qc->curbytes += count;
4095 qc->cursg_ofs += count;
4096
Albert Lee32529e02005-05-26 03:49:42 -04004097 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 qc->cursg++;
4099 qc->cursg_ofs = 0;
4100 }
4101
Albert Lee563a6e12005-08-12 14:17:50 +08004102 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104}
4105
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004106/**
4107 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
4108 * @qc: Command on going
4109 *
4110 * Transfer Transfer data from/to the ATAPI device.
4111 *
4112 * LOCKING:
4113 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004114 */
4115
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116static void atapi_pio_bytes(struct ata_queued_cmd *qc)
4117{
4118 struct ata_port *ap = qc->ap;
4119 struct ata_device *dev = qc->dev;
4120 unsigned int ireason, bc_lo, bc_hi, bytes;
4121 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
4122
Albert Leeeec4c3f2006-05-18 17:51:10 +08004123 /* Abuse qc->result_tf for temp storage of intermediate TF
4124 * here to save some kernel stack usage.
4125 * For normal completion, qc->result_tf is not relevant. For
4126 * error, qc->result_tf is later overwritten by ata_qc_complete().
4127 * So, the correctness of qc->result_tf is not affected.
4128 */
4129 ap->ops->tf_read(ap, &qc->result_tf);
4130 ireason = qc->result_tf.nsect;
4131 bc_lo = qc->result_tf.lbam;
4132 bc_hi = qc->result_tf.lbah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 bytes = (bc_hi << 8) | bc_lo;
4134
4135 /* shall be cleared to zero, indicating xfer of data */
4136 if (ireason & (1 << 0))
4137 goto err_out;
4138
4139 /* make sure transfer direction matches expected */
4140 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
4141 if (do_write != i_write)
4142 goto err_out;
4143
Albert Lee312f7da2005-09-27 17:38:03 +08004144 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
4145
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 __atapi_pio_bytes(qc, bytes);
4147
4148 return;
4149
4150err_out:
Tejun Heof15a1da2006-05-15 20:57:56 +09004151 ata_dev_printk(dev, KERN_INFO, "ATAPI check failed\n");
Tejun Heo11a56d22006-01-23 13:09:36 +09004152 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08004153 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154}
4155
4156/**
Albert Leec234fb02006-03-25 17:58:38 +08004157 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
4158 * @ap: the target ata_port
4159 * @qc: qc on going
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 *
Albert Leec234fb02006-03-25 17:58:38 +08004161 * RETURNS:
4162 * 1 if ok in workqueue, 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 */
Albert Leec234fb02006-03-25 17:58:38 +08004164
4165static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166{
Albert Leec234fb02006-03-25 17:58:38 +08004167 if (qc->tf.flags & ATA_TFLAG_POLLING)
4168 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
Albert Leec234fb02006-03-25 17:58:38 +08004170 if (ap->hsm_task_state == HSM_ST_FIRST) {
4171 if (qc->tf.protocol == ATA_PROT_PIO &&
4172 (qc->tf.flags & ATA_TFLAG_WRITE))
4173 return 1;
4174
4175 if (is_atapi_taskfile(&qc->tf) &&
4176 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
4177 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 }
4179
Albert Leec234fb02006-03-25 17:58:38 +08004180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181}
4182
Albert Leec234fb02006-03-25 17:58:38 +08004183/**
Tejun Heoc17ea202006-05-15 20:59:29 +09004184 * ata_hsm_qc_complete - finish a qc running on standard HSM
4185 * @qc: Command to complete
4186 * @in_wq: 1 if called from workqueue, 0 otherwise
4187 *
4188 * Finish @qc which is running on standard HSM.
4189 *
4190 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004191 * If @in_wq is zero, spin_lock_irqsave(host lock).
Tejun Heoc17ea202006-05-15 20:59:29 +09004192 * Otherwise, none on entry and grabs host lock.
4193 */
4194static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
4195{
4196 struct ata_port *ap = qc->ap;
4197 unsigned long flags;
4198
4199 if (ap->ops->error_handler) {
4200 if (in_wq) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04004201 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09004202
Jeff Garzikcca39742006-08-24 03:19:22 -04004203 /* EH might have kicked in while host lock is
4204 * released.
Tejun Heoc17ea202006-05-15 20:59:29 +09004205 */
4206 qc = ata_qc_from_tag(ap, qc->tag);
4207 if (qc) {
4208 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
4209 ata_irq_on(ap);
4210 ata_qc_complete(qc);
4211 } else
4212 ata_port_freeze(ap);
4213 }
4214
Jeff Garzikba6a1302006-06-22 23:46:10 -04004215 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09004216 } else {
4217 if (likely(!(qc->err_mask & AC_ERR_HSM)))
4218 ata_qc_complete(qc);
4219 else
4220 ata_port_freeze(ap);
4221 }
4222 } else {
4223 if (in_wq) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04004224 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09004225 ata_irq_on(ap);
4226 ata_qc_complete(qc);
Jeff Garzikba6a1302006-06-22 23:46:10 -04004227 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09004228 } else
4229 ata_qc_complete(qc);
4230 }
Jeff Garzikc81e29b2006-05-24 01:49:12 -04004231
4232 ata_altstatus(ap); /* flush */
Tejun Heoc17ea202006-05-15 20:59:29 +09004233}
4234
4235/**
Albert Leebb5cb292006-03-25 17:48:02 +08004236 * ata_hsm_move - move the HSM to the next state.
4237 * @ap: the target ata_port
4238 * @qc: qc on going
4239 * @status: current device status
4240 * @in_wq: 1 if called from workqueue, 0 otherwise
4241 *
4242 * RETURNS:
4243 * 1 when poll next status needed, 0 otherwise.
4244 */
Tejun Heo9a1004d2006-05-31 18:27:52 +09004245int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
4246 u8 status, int in_wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247{
Albert Leebb5cb292006-03-25 17:48:02 +08004248 unsigned long flags = 0;
4249 int poll_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
Albert Lee6912ccd2006-03-25 17:45:49 +08004251 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
Albert Lee0565c262006-02-13 18:55:25 +08004252
Albert Leebb5cb292006-03-25 17:48:02 +08004253 /* Make sure ata_qc_issue_prot() does not throw things
4254 * like DMA polling into the workqueue. Notice that
4255 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
Albert Lee1c848982005-12-05 15:40:15 +08004256 */
Albert Leec234fb02006-03-25 17:58:38 +08004257 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
Albert Lee1c848982005-12-05 15:40:15 +08004258
Albert Leee2cec772006-03-25 17:43:49 +08004259fsm_start:
Albert Lee999bb6f2006-03-25 18:07:48 +08004260 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
4261 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262
Albert Leee2cec772006-03-25 17:43:49 +08004263 switch (ap->hsm_task_state) {
4264 case HSM_ST_FIRST:
Albert Leebb5cb292006-03-25 17:48:02 +08004265 /* Send first data block or PACKET CDB */
4266
4267 /* If polling, we will stay in the work queue after
4268 * sending the data. Otherwise, interrupt handler
4269 * takes over after sending the data.
4270 */
4271 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
4272
Albert Leee2cec772006-03-25 17:43:49 +08004273 /* check device status */
Albert Lee3655d1d2006-05-19 11:43:04 +08004274 if (unlikely((status & ATA_DRQ) == 0)) {
4275 /* handle BSY=0, DRQ=0 as error */
4276 if (likely(status & (ATA_ERR | ATA_DF)))
4277 /* device stops HSM for abort/error */
4278 qc->err_mask |= AC_ERR_DEV;
4279 else
4280 /* HSM violation. Let EH handle this */
4281 qc->err_mask |= AC_ERR_HSM;
4282
Albert Leee2cec772006-03-25 17:43:49 +08004283 ap->hsm_task_state = HSM_ST_ERR;
4284 goto fsm_start;
4285 }
4286
Albert Lee71601952006-03-25 18:11:12 +08004287 /* Device should not ask for data transfer (DRQ=1)
4288 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08004289 * We ignore DRQ here and stop the HSM by
4290 * changing hsm_task_state to HSM_ST_ERR and
4291 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08004292 */
4293 if (unlikely(status & (ATA_ERR | ATA_DF))) {
4294 printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
4295 ap->id, status);
Albert Lee3655d1d2006-05-19 11:43:04 +08004296 qc->err_mask |= AC_ERR_HSM;
Albert Leeeee6c322006-04-01 17:38:43 +08004297 ap->hsm_task_state = HSM_ST_ERR;
4298 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08004299 }
4300
Albert Leebb5cb292006-03-25 17:48:02 +08004301 /* Send the CDB (atapi) or the first data block (ata pio out).
4302 * During the state transition, interrupt handler shouldn't
4303 * be invoked before the data transfer is complete and
4304 * hsm_task_state is changed. Hence, the following locking.
4305 */
4306 if (in_wq)
Jeff Garzikba6a1302006-06-22 23:46:10 -04004307 spin_lock_irqsave(ap->lock, flags);
Albert Leee2cec772006-03-25 17:43:49 +08004308
Albert Leebb5cb292006-03-25 17:48:02 +08004309 if (qc->tf.protocol == ATA_PROT_PIO) {
4310 /* PIO data out protocol.
4311 * send first data block.
4312 */
4313
4314 /* ata_pio_sectors() might change the state
4315 * to HSM_ST_LAST. so, the state is changed here
4316 * before ata_pio_sectors().
4317 */
4318 ap->hsm_task_state = HSM_ST;
4319 ata_pio_sectors(qc);
4320 ata_altstatus(ap); /* flush */
4321 } else
4322 /* send CDB */
4323 atapi_send_cdb(ap, qc);
4324
4325 if (in_wq)
Jeff Garzikba6a1302006-06-22 23:46:10 -04004326 spin_unlock_irqrestore(ap->lock, flags);
Albert Leebb5cb292006-03-25 17:48:02 +08004327
4328 /* if polling, ata_pio_task() handles the rest.
4329 * otherwise, interrupt handler takes over from here.
4330 */
Albert Leee2cec772006-03-25 17:43:49 +08004331 break;
4332
4333 case HSM_ST:
4334 /* complete command or read/write the data register */
4335 if (qc->tf.protocol == ATA_PROT_ATAPI) {
4336 /* ATAPI PIO protocol */
4337 if ((status & ATA_DRQ) == 0) {
Albert Lee3655d1d2006-05-19 11:43:04 +08004338 /* No more data to transfer or device error.
4339 * Device error will be tagged in HSM_ST_LAST.
4340 */
Albert Leee2cec772006-03-25 17:43:49 +08004341 ap->hsm_task_state = HSM_ST_LAST;
4342 goto fsm_start;
4343 }
4344
Albert Lee71601952006-03-25 18:11:12 +08004345 /* Device should not ask for data transfer (DRQ=1)
4346 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08004347 * We ignore DRQ here and stop the HSM by
4348 * changing hsm_task_state to HSM_ST_ERR and
4349 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08004350 */
4351 if (unlikely(status & (ATA_ERR | ATA_DF))) {
4352 printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
4353 ap->id, status);
Albert Lee3655d1d2006-05-19 11:43:04 +08004354 qc->err_mask |= AC_ERR_HSM;
Albert Leeeee6c322006-04-01 17:38:43 +08004355 ap->hsm_task_state = HSM_ST_ERR;
4356 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08004357 }
4358
Albert Leee2cec772006-03-25 17:43:49 +08004359 atapi_pio_bytes(qc);
4360
4361 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
4362 /* bad ireason reported by device */
4363 goto fsm_start;
4364
4365 } else {
4366 /* ATA PIO protocol */
4367 if (unlikely((status & ATA_DRQ) == 0)) {
4368 /* handle BSY=0, DRQ=0 as error */
Albert Lee3655d1d2006-05-19 11:43:04 +08004369 if (likely(status & (ATA_ERR | ATA_DF)))
4370 /* device stops HSM for abort/error */
4371 qc->err_mask |= AC_ERR_DEV;
4372 else
Tejun Heo55a8e2c2006-11-10 18:08:10 +09004373 /* HSM violation. Let EH handle this.
4374 * Phantom devices also trigger this
4375 * condition. Mark hint.
4376 */
4377 qc->err_mask |= AC_ERR_HSM |
4378 AC_ERR_NODEV_HINT;
Albert Lee3655d1d2006-05-19 11:43:04 +08004379
Albert Leee2cec772006-03-25 17:43:49 +08004380 ap->hsm_task_state = HSM_ST_ERR;
4381 goto fsm_start;
4382 }
4383
Albert Leeeee6c322006-04-01 17:38:43 +08004384 /* For PIO reads, some devices may ask for
4385 * data transfer (DRQ=1) alone with ERR=1.
4386 * We respect DRQ here and transfer one
4387 * block of junk data before changing the
4388 * hsm_task_state to HSM_ST_ERR.
4389 *
4390 * For PIO writes, ERR=1 DRQ=1 doesn't make
4391 * sense since the data block has been
4392 * transferred to the device.
Albert Lee71601952006-03-25 18:11:12 +08004393 */
4394 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Albert Lee71601952006-03-25 18:11:12 +08004395 /* data might be corrputed */
4396 qc->err_mask |= AC_ERR_DEV;
Albert Leeeee6c322006-04-01 17:38:43 +08004397
4398 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
4399 ata_pio_sectors(qc);
4400 ata_altstatus(ap);
4401 status = ata_wait_idle(ap);
4402 }
4403
Albert Lee3655d1d2006-05-19 11:43:04 +08004404 if (status & (ATA_BUSY | ATA_DRQ))
4405 qc->err_mask |= AC_ERR_HSM;
4406
Albert Leeeee6c322006-04-01 17:38:43 +08004407 /* ata_pio_sectors() might change the
4408 * state to HSM_ST_LAST. so, the state
4409 * is changed after ata_pio_sectors().
4410 */
4411 ap->hsm_task_state = HSM_ST_ERR;
4412 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08004413 }
4414
Albert Leee2cec772006-03-25 17:43:49 +08004415 ata_pio_sectors(qc);
4416
4417 if (ap->hsm_task_state == HSM_ST_LAST &&
4418 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
4419 /* all data read */
4420 ata_altstatus(ap);
Albert Lee52a32202006-03-25 18:18:15 +08004421 status = ata_wait_idle(ap);
Albert Leee2cec772006-03-25 17:43:49 +08004422 goto fsm_start;
4423 }
4424 }
4425
4426 ata_altstatus(ap); /* flush */
Albert Leebb5cb292006-03-25 17:48:02 +08004427 poll_next = 1;
Albert Leee2cec772006-03-25 17:43:49 +08004428 break;
4429
4430 case HSM_ST_LAST:
Albert Lee6912ccd2006-03-25 17:45:49 +08004431 if (unlikely(!ata_ok(status))) {
4432 qc->err_mask |= __ac_err_mask(status);
Albert Leee2cec772006-03-25 17:43:49 +08004433 ap->hsm_task_state = HSM_ST_ERR;
4434 goto fsm_start;
4435 }
4436
4437 /* no more data to transfer */
Albert Lee4332a772006-04-03 17:43:24 +08004438 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
4439 ap->id, qc->dev->devno, status);
Albert Leee2cec772006-03-25 17:43:49 +08004440
Albert Lee6912ccd2006-03-25 17:45:49 +08004441 WARN_ON(qc->err_mask);
4442
Albert Leee2cec772006-03-25 17:43:49 +08004443 ap->hsm_task_state = HSM_ST_IDLE;
4444
4445 /* complete taskfile transaction */
Tejun Heoc17ea202006-05-15 20:59:29 +09004446 ata_hsm_qc_complete(qc, in_wq);
Albert Leebb5cb292006-03-25 17:48:02 +08004447
4448 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08004449 break;
4450
4451 case HSM_ST_ERR:
Albert Leee2cec772006-03-25 17:43:49 +08004452 /* make sure qc->err_mask is available to
4453 * know what's wrong and recover
4454 */
4455 WARN_ON(qc->err_mask == 0);
4456
4457 ap->hsm_task_state = HSM_ST_IDLE;
Albert Leebb5cb292006-03-25 17:48:02 +08004458
Albert Lee999bb6f2006-03-25 18:07:48 +08004459 /* complete taskfile transaction */
Tejun Heoc17ea202006-05-15 20:59:29 +09004460 ata_hsm_qc_complete(qc, in_wq);
Albert Leebb5cb292006-03-25 17:48:02 +08004461
4462 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08004463 break;
4464 default:
Albert Leebb5cb292006-03-25 17:48:02 +08004465 poll_next = 0;
Albert Lee6912ccd2006-03-25 17:45:49 +08004466 BUG();
Albert Leee2cec772006-03-25 17:43:49 +08004467 }
4468
Albert Leebb5cb292006-03-25 17:48:02 +08004469 return poll_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470}
4471
4472static void ata_pio_task(void *_data)
4473{
Tejun Heoc91af2c2006-04-02 18:51:53 +09004474 struct ata_queued_cmd *qc = _data;
4475 struct ata_port *ap = qc->ap;
Albert Leea1af3732006-03-25 17:50:15 +08004476 u8 status;
4477 int poll_next;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04004478
4479fsm_start:
Albert Leea1af3732006-03-25 17:50:15 +08004480 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
Albert Leea1af3732006-03-25 17:50:15 +08004482 /*
4483 * This is purely heuristic. This is a fast path.
4484 * Sometimes when we enter, BSY will be cleared in
4485 * a chk-status or two. If not, the drive is probably seeking
4486 * or something. Snooze for a couple msecs, then
4487 * chk-status again. If still busy, queue delayed work.
4488 */
4489 status = ata_busy_wait(ap, ATA_BUSY, 5);
4490 if (status & ATA_BUSY) {
4491 msleep(2);
4492 status = ata_busy_wait(ap, ATA_BUSY, 10);
4493 if (status & ATA_BUSY) {
Albert Lee31ce6da2006-04-03 18:31:44 +08004494 ata_port_queue_task(ap, ata_pio_task, qc, ATA_SHORT_PAUSE);
Albert Leea1af3732006-03-25 17:50:15 +08004495 return;
4496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 }
4498
Albert Leea1af3732006-03-25 17:50:15 +08004499 /* move the HSM */
4500 poll_next = ata_hsm_move(ap, qc, status, 1);
4501
4502 /* another command or interrupt handler
4503 * may be running at this point.
4504 */
4505 if (poll_next)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04004506 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507}
4508
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 * ata_qc_new - Request an available ATA command, for queueing
4511 * @ap: Port associated with device @dev
4512 * @dev: Device from whom we request an available command structure
4513 *
4514 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004515 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 */
4517
4518static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
4519{
4520 struct ata_queued_cmd *qc = NULL;
4521 unsigned int i;
4522
Tejun Heoe3180492006-05-15 20:58:09 +09004523 /* no command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09004524 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
Tejun Heoe3180492006-05-15 20:58:09 +09004525 return NULL;
4526
Tejun Heo2ab7db12006-05-15 20:58:02 +09004527 /* the last tag is reserved for internal command. */
4528 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
Tejun Heo6cec4a32006-05-15 21:03:41 +09004529 if (!test_and_set_bit(i, &ap->qc_allocated)) {
Tejun Heof69499f2006-05-15 20:58:03 +09004530 qc = __ata_qc_from_tag(ap, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 break;
4532 }
4533
4534 if (qc)
4535 qc->tag = i;
4536
4537 return qc;
4538}
4539
4540/**
4541 * ata_qc_new_init - Request an available ATA command, and initialize it
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 * @dev: Device from whom we request an available command structure
4543 *
4544 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004545 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 */
4547
Tejun Heo3373efd2006-05-15 20:57:53 +09004548struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549{
Tejun Heo3373efd2006-05-15 20:57:53 +09004550 struct ata_port *ap = dev->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 struct ata_queued_cmd *qc;
4552
4553 qc = ata_qc_new(ap);
4554 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 qc->scsicmd = NULL;
4556 qc->ap = ap;
4557 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05004559 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 }
4561
4562 return qc;
4563}
4564
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565/**
4566 * ata_qc_free - free unused ata_queued_cmd
4567 * @qc: Command to complete
4568 *
4569 * Designed to free unused ata_queued_cmd object
4570 * in case something prevents using it.
4571 *
4572 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004573 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 */
4575void ata_qc_free(struct ata_queued_cmd *qc)
4576{
Tejun Heo4ba946e2006-01-23 13:09:36 +09004577 struct ata_port *ap = qc->ap;
4578 unsigned int tag;
4579
Tejun Heoa4631472006-02-11 19:11:13 +09004580 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
Tejun Heo4ba946e2006-01-23 13:09:36 +09004582 qc->flags = 0;
4583 tag = qc->tag;
4584 if (likely(ata_tag_valid(tag))) {
Tejun Heo4ba946e2006-01-23 13:09:36 +09004585 qc->tag = ATA_TAG_POISON;
Tejun Heo6cec4a32006-05-15 21:03:41 +09004586 clear_bit(tag, &ap->qc_allocated);
Tejun Heo4ba946e2006-01-23 13:09:36 +09004587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588}
4589
Tejun Heo76014422006-02-11 15:13:49 +09004590void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591{
Tejun Heodedaf2b2006-05-15 21:03:43 +09004592 struct ata_port *ap = qc->ap;
4593
Tejun Heoa4631472006-02-11 19:11:13 +09004594 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4595 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596
4597 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4598 ata_sg_clean(qc);
4599
Tejun Heo7401abf2006-05-15 20:57:32 +09004600 /* command should be marked inactive atomically with qc completion */
Tejun Heodedaf2b2006-05-15 21:03:43 +09004601 if (qc->tf.protocol == ATA_PROT_NCQ)
4602 ap->sactive &= ~(1 << qc->tag);
4603 else
4604 ap->active_tag = ATA_TAG_POISON;
Tejun Heo7401abf2006-05-15 20:57:32 +09004605
Albert Lee3f3791d2005-08-16 14:25:38 +08004606 /* atapi: mark qc as inactive to prevent the interrupt handler
4607 * from completing the command twice later, before the error handler
4608 * is called. (when rc != 0 and atapi request sense is needed)
4609 */
4610 qc->flags &= ~ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004611 ap->qc_active &= ~(1 << qc->tag);
Albert Lee3f3791d2005-08-16 14:25:38 +08004612
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09004614 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615}
4616
Tejun Heo39599a52006-11-14 22:37:35 +09004617static void fill_result_tf(struct ata_queued_cmd *qc)
4618{
4619 struct ata_port *ap = qc->ap;
4620
4621 ap->ops->tf_read(ap, &qc->result_tf);
4622 qc->result_tf.flags = qc->tf.flags;
4623}
4624
Tejun Heof686bcb2006-05-15 20:58:05 +09004625/**
4626 * ata_qc_complete - Complete an active ATA command
4627 * @qc: Command to complete
4628 * @err_mask: ATA Status register contents
4629 *
4630 * Indicate to the mid and upper layers that an ATA
4631 * command has completed, with either an ok or not-ok status.
4632 *
4633 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004634 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +09004635 */
4636void ata_qc_complete(struct ata_queued_cmd *qc)
4637{
4638 struct ata_port *ap = qc->ap;
4639
4640 /* XXX: New EH and old EH use different mechanisms to
4641 * synchronize EH with regular execution path.
4642 *
4643 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4644 * Normal execution path is responsible for not accessing a
4645 * failed qc. libata core enforces the rule by returning NULL
4646 * from ata_qc_from_tag() for failed qcs.
4647 *
4648 * Old EH depends on ata_qc_complete() nullifying completion
4649 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
4650 * not synchronize with interrupt handler. Only PIO task is
4651 * taken care of.
4652 */
4653 if (ap->ops->error_handler) {
Tejun Heob51e9e52006-06-29 01:29:30 +09004654 WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
Tejun Heof686bcb2006-05-15 20:58:05 +09004655
4656 if (unlikely(qc->err_mask))
4657 qc->flags |= ATA_QCFLAG_FAILED;
4658
4659 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4660 if (!ata_tag_internal(qc->tag)) {
4661 /* always fill result TF for failed qc */
Tejun Heo39599a52006-11-14 22:37:35 +09004662 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09004663 ata_qc_schedule_eh(qc);
4664 return;
4665 }
4666 }
4667
4668 /* read result TF if requested */
4669 if (qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09004670 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09004671
4672 __ata_qc_complete(qc);
4673 } else {
4674 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4675 return;
4676
4677 /* read result TF if failed or requested */
4678 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09004679 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09004680
4681 __ata_qc_complete(qc);
4682 }
4683}
4684
Tejun Heodedaf2b2006-05-15 21:03:43 +09004685/**
4686 * ata_qc_complete_multiple - Complete multiple qcs successfully
4687 * @ap: port in question
4688 * @qc_active: new qc_active mask
4689 * @finish_qc: LLDD callback invoked before completing a qc
4690 *
4691 * Complete in-flight commands. This functions is meant to be
4692 * called from low-level driver's interrupt routine to complete
4693 * requests normally. ap->qc_active and @qc_active is compared
4694 * and commands are completed accordingly.
4695 *
4696 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004697 * spin_lock_irqsave(host lock)
Tejun Heodedaf2b2006-05-15 21:03:43 +09004698 *
4699 * RETURNS:
4700 * Number of completed commands on success, -errno otherwise.
4701 */
4702int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
4703 void (*finish_qc)(struct ata_queued_cmd *))
4704{
4705 int nr_done = 0;
4706 u32 done_mask;
4707 int i;
4708
4709 done_mask = ap->qc_active ^ qc_active;
4710
4711 if (unlikely(done_mask & qc_active)) {
4712 ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
4713 "(%08x->%08x)\n", ap->qc_active, qc_active);
4714 return -EINVAL;
4715 }
4716
4717 for (i = 0; i < ATA_MAX_QUEUE; i++) {
4718 struct ata_queued_cmd *qc;
4719
4720 if (!(done_mask & (1 << i)))
4721 continue;
4722
4723 if ((qc = ata_qc_from_tag(ap, i))) {
4724 if (finish_qc)
4725 finish_qc(qc);
4726 ata_qc_complete(qc);
4727 nr_done++;
4728 }
4729 }
4730
4731 return nr_done;
4732}
4733
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
4735{
4736 struct ata_port *ap = qc->ap;
4737
4738 switch (qc->tf.protocol) {
Tejun Heo3dc1d882006-05-15 21:03:45 +09004739 case ATA_PROT_NCQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 case ATA_PROT_DMA:
4741 case ATA_PROT_ATAPI_DMA:
4742 return 1;
4743
4744 case ATA_PROT_ATAPI:
4745 case ATA_PROT_PIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 if (ap->flags & ATA_FLAG_PIO_DMA)
4747 return 1;
4748
4749 /* fall through */
4750
4751 default:
4752 return 0;
4753 }
4754
4755 /* never reached */
4756}
4757
4758/**
4759 * ata_qc_issue - issue taskfile to device
4760 * @qc: command to issue to device
4761 *
4762 * Prepare an ATA command to submission to device.
4763 * This includes mapping the data into a DMA-able
4764 * area, filling in the S/G table, and finally
4765 * writing the taskfile to hardware, starting the command.
4766 *
4767 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004768 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09004770void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771{
4772 struct ata_port *ap = qc->ap;
4773
Tejun Heodedaf2b2006-05-15 21:03:43 +09004774 /* Make sure only one non-NCQ command is outstanding. The
4775 * check is skipped for old EH because it reuses active qc to
4776 * request ATAPI sense.
4777 */
4778 WARN_ON(ap->ops->error_handler && ata_tag_valid(ap->active_tag));
4779
4780 if (qc->tf.protocol == ATA_PROT_NCQ) {
4781 WARN_ON(ap->sactive & (1 << qc->tag));
4782 ap->sactive |= 1 << qc->tag;
4783 } else {
4784 WARN_ON(ap->sactive);
4785 ap->active_tag = qc->tag;
4786 }
4787
Tejun Heoe4a70e72006-03-31 20:36:47 +09004788 qc->flags |= ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004789 ap->qc_active |= 1 << qc->tag;
Tejun Heoe4a70e72006-03-31 20:36:47 +09004790
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 if (ata_should_dma_map(qc)) {
4792 if (qc->flags & ATA_QCFLAG_SG) {
4793 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004794 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
4796 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004797 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 }
4799 } else {
4800 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4801 }
4802
4803 ap->ops->qc_prep(qc);
4804
Tejun Heo8e0e6942006-03-31 20:41:11 +09004805 qc->err_mask |= ap->ops->qc_issue(qc);
4806 if (unlikely(qc->err_mask))
4807 goto err;
4808 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Tejun Heo8e436af2006-01-23 13:09:36 +09004810sg_err:
4811 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo8e0e6942006-03-31 20:41:11 +09004812 qc->err_mask |= AC_ERR_SYSTEM;
4813err:
4814 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815}
4816
4817/**
4818 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
4819 * @qc: command to issue to device
4820 *
4821 * Using various libata functions and hooks, this function
4822 * starts an ATA command. ATA commands are grouped into
4823 * classes called "protocols", and issuing each type of protocol
4824 * is slightly different.
4825 *
Edward Falk0baab862005-06-02 18:17:13 -04004826 * May be used as the qc_issue() entry in ata_port_operations.
4827 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004829 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 *
4831 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004832 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 */
4834
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004835unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836{
4837 struct ata_port *ap = qc->ap;
4838
Albert Leee50362e2005-09-27 17:39:50 +08004839 /* Use polling pio if the LLD doesn't handle
4840 * interrupt driven pio and atapi CDB interrupt.
4841 */
4842 if (ap->flags & ATA_FLAG_PIO_POLLING) {
4843 switch (qc->tf.protocol) {
4844 case ATA_PROT_PIO:
4845 case ATA_PROT_ATAPI:
4846 case ATA_PROT_ATAPI_NODATA:
4847 qc->tf.flags |= ATA_TFLAG_POLLING;
4848 break;
4849 case ATA_PROT_ATAPI_DMA:
4850 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
Albert Lee3a778272006-06-22 13:00:25 +08004851 /* see ata_dma_blacklisted() */
Albert Leee50362e2005-09-27 17:39:50 +08004852 BUG();
4853 break;
4854 default:
4855 break;
4856 }
4857 }
4858
Tejun Heo3d3cca32006-11-16 10:50:50 +09004859 /* Some controllers show flaky interrupt behavior after
4860 * setting xfer mode. Use polling instead.
4861 */
4862 if (unlikely(qc->tf.command == ATA_CMD_SET_FEATURES &&
4863 qc->tf.feature == SETFEATURES_XFER) &&
4864 (ap->flags & ATA_FLAG_SETXFER_POLLING))
4865 qc->tf.flags |= ATA_TFLAG_POLLING;
4866
Albert Lee312f7da2005-09-27 17:38:03 +08004867 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 ata_dev_select(ap, qc->dev->devno, 1, 0);
4869
Albert Lee312f7da2005-09-27 17:38:03 +08004870 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 switch (qc->tf.protocol) {
4872 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08004873 if (qc->tf.flags & ATA_TFLAG_POLLING)
4874 ata_qc_set_polling(qc);
4875
Jeff Garzike5338252005-10-30 21:37:17 -05004876 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08004877 ap->hsm_task_state = HSM_ST_LAST;
4878
4879 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee31ce6da2006-04-03 18:31:44 +08004880 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08004881
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 break;
4883
4884 case ATA_PROT_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05004885 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08004886
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4888 ap->ops->bmdma_setup(qc); /* set up bmdma */
4889 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08004890 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 break;
4892
Albert Lee312f7da2005-09-27 17:38:03 +08004893 case ATA_PROT_PIO:
4894 if (qc->tf.flags & ATA_TFLAG_POLLING)
4895 ata_qc_set_polling(qc);
4896
Jeff Garzike5338252005-10-30 21:37:17 -05004897 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08004898
Albert Lee54f00382005-09-30 19:14:19 +08004899 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4900 /* PIO data out protocol */
4901 ap->hsm_task_state = HSM_ST_FIRST;
Albert Lee31ce6da2006-04-03 18:31:44 +08004902 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee54f00382005-09-30 19:14:19 +08004903
4904 /* always send first data block using
Albert Leee27486d2005-11-01 19:24:49 +08004905 * the ata_pio_task() codepath.
Albert Lee54f00382005-09-30 19:14:19 +08004906 */
Albert Lee312f7da2005-09-27 17:38:03 +08004907 } else {
Albert Lee54f00382005-09-30 19:14:19 +08004908 /* PIO data in protocol */
4909 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08004910
Albert Lee54f00382005-09-30 19:14:19 +08004911 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee31ce6da2006-04-03 18:31:44 +08004912 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08004913
Albert Lee54f00382005-09-30 19:14:19 +08004914 /* if polling, ata_pio_task() handles the rest.
4915 * otherwise, interrupt handler takes over from here.
4916 */
Albert Lee312f7da2005-09-27 17:38:03 +08004917 }
4918
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 break;
4920
4921 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08004923 if (qc->tf.flags & ATA_TFLAG_POLLING)
4924 ata_qc_set_polling(qc);
4925
Jeff Garzike5338252005-10-30 21:37:17 -05004926 ata_tf_to_host(ap, &qc->tf);
Jeff Garzikf6ef65e2006-01-27 02:45:00 -05004927
Albert Lee312f7da2005-09-27 17:38:03 +08004928 ap->hsm_task_state = HSM_ST_FIRST;
4929
4930 /* send cdb by polling if no cdb interrupt */
4931 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
4932 (qc->tf.flags & ATA_TFLAG_POLLING))
Albert Lee31ce6da2006-04-03 18:31:44 +08004933 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 break;
4935
4936 case ATA_PROT_ATAPI_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05004937 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08004938
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4940 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08004941 ap->hsm_task_state = HSM_ST_FIRST;
4942
4943 /* send cdb by polling if no cdb interrupt */
4944 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Albert Lee31ce6da2006-04-03 18:31:44 +08004945 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 break;
4947
4948 default:
4949 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004950 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 }
4952
4953 return 0;
4954}
4955
4956/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 * ata_host_intr - Handle host interrupt for given (port, task)
4958 * @ap: Port on which interrupt arrived (possibly...)
4959 * @qc: Taskfile currently active in engine
4960 *
4961 * Handle host interrupt for given queued command. Currently,
4962 * only DMA interrupts are handled. All other commands are
4963 * handled via polling with interrupts disabled (nIEN bit).
4964 *
4965 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004966 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 *
4968 * RETURNS:
4969 * One if interrupt was handled, zero if not (shared irq).
4970 */
4971
4972inline unsigned int ata_host_intr (struct ata_port *ap,
4973 struct ata_queued_cmd *qc)
4974{
Tejun Heoea547632006-11-17 12:06:21 +09004975 struct ata_eh_info *ehi = &ap->eh_info;
Albert Lee312f7da2005-09-27 17:38:03 +08004976 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
Albert Lee312f7da2005-09-27 17:38:03 +08004978 VPRINTK("ata%u: protocol %d task_state %d\n",
4979 ap->id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
Albert Lee312f7da2005-09-27 17:38:03 +08004981 /* Check whether we are expecting interrupt in this state */
4982 switch (ap->hsm_task_state) {
4983 case HSM_ST_FIRST:
Albert Lee6912ccd2006-03-25 17:45:49 +08004984 /* Some pre-ATAPI-4 devices assert INTRQ
4985 * at this state when ready to receive CDB.
4986 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
Albert Lee312f7da2005-09-27 17:38:03 +08004988 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
4989 * The flag was turned on only for atapi devices.
4990 * No need to check is_atapi_taskfile(&qc->tf) again.
4991 */
4992 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 break;
Albert Lee312f7da2005-09-27 17:38:03 +08004995 case HSM_ST_LAST:
4996 if (qc->tf.protocol == ATA_PROT_DMA ||
4997 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
4998 /* check status of DMA engine */
4999 host_stat = ap->ops->bmdma_status(ap);
5000 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
Albert Lee312f7da2005-09-27 17:38:03 +08005002 /* if it's not our irq... */
5003 if (!(host_stat & ATA_DMA_INTR))
5004 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
Albert Lee312f7da2005-09-27 17:38:03 +08005006 /* before we do anything else, clear DMA-Start bit */
5007 ap->ops->bmdma_stop(qc);
Albert Leea4f16612005-12-26 16:40:53 +08005008
5009 if (unlikely(host_stat & ATA_DMA_ERR)) {
5010 /* error when transfering data to/from memory */
5011 qc->err_mask |= AC_ERR_HOST_BUS;
5012 ap->hsm_task_state = HSM_ST_ERR;
5013 }
Albert Lee312f7da2005-09-27 17:38:03 +08005014 }
5015 break;
5016 case HSM_ST:
5017 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 default:
5019 goto idle_irq;
5020 }
5021
Albert Lee312f7da2005-09-27 17:38:03 +08005022 /* check altstatus */
5023 status = ata_altstatus(ap);
5024 if (status & ATA_BUSY)
5025 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
Albert Lee312f7da2005-09-27 17:38:03 +08005027 /* check main status, clearing INTRQ */
5028 status = ata_chk_status(ap);
5029 if (unlikely(status & ATA_BUSY))
5030 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031
Albert Lee312f7da2005-09-27 17:38:03 +08005032 /* ack bmdma irq events */
5033 ap->ops->irq_clear(ap);
5034
Albert Leebb5cb292006-03-25 17:48:02 +08005035 ata_hsm_move(ap, qc, status, 0);
Tejun Heoea547632006-11-17 12:06:21 +09005036
5037 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
5038 qc->tf.protocol == ATA_PROT_ATAPI_DMA))
5039 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
5040
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 return 1; /* irq handled */
5042
5043idle_irq:
5044 ap->stats.idle_irq++;
5045
5046#ifdef ATA_IRQ_TRAP
5047 if ((ap->stats.idle_irq % 1000) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 ata_irq_ack(ap, 0); /* debug trap */
Tejun Heof15a1da2006-05-15 20:57:56 +09005049 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
Alan Cox23cfce82006-03-21 16:06:53 +00005050 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 }
5052#endif
5053 return 0; /* irq not handled */
5054}
5055
5056/**
5057 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04005058 * @irq: irq line (unused)
Jeff Garzikcca39742006-08-24 03:19:22 -04005059 * @dev_instance: pointer to our ata_host information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04005061 * Default interrupt handler for PCI IDE devices. Calls
5062 * ata_host_intr() for each port that is not disabled.
5063 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005065 * Obtains host lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 *
5067 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005068 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 */
5070
David Howells7d12e782006-10-05 14:55:46 +01005071irqreturn_t ata_interrupt (int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072{
Jeff Garzikcca39742006-08-24 03:19:22 -04005073 struct ata_host *host = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 unsigned int i;
5075 unsigned int handled = 0;
5076 unsigned long flags;
5077
5078 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
Jeff Garzikcca39742006-08-24 03:19:22 -04005079 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080
Jeff Garzikcca39742006-08-24 03:19:22 -04005081 for (i = 0; i < host->n_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 struct ata_port *ap;
5083
Jeff Garzikcca39742006-08-24 03:19:22 -04005084 ap = host->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09005085 if (ap &&
Jeff Garzik029f5462006-04-02 10:30:40 -04005086 !(ap->flags & ATA_FLAG_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 struct ata_queued_cmd *qc;
5088
5089 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08005090 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08005091 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 handled |= ata_host_intr(ap, qc);
5093 }
5094 }
5095
Jeff Garzikcca39742006-08-24 03:19:22 -04005096 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097
5098 return IRQ_RETVAL(handled);
5099}
5100
Tejun Heo34bf2172006-05-15 20:57:46 +09005101/**
5102 * sata_scr_valid - test whether SCRs are accessible
5103 * @ap: ATA port to test SCR accessibility for
5104 *
5105 * Test whether SCRs are accessible for @ap.
5106 *
5107 * LOCKING:
5108 * None.
5109 *
5110 * RETURNS:
5111 * 1 if SCRs are accessible, 0 otherwise.
5112 */
5113int sata_scr_valid(struct ata_port *ap)
5114{
5115 return ap->cbl == ATA_CBL_SATA && ap->ops->scr_read;
5116}
5117
5118/**
5119 * sata_scr_read - read SCR register of the specified port
5120 * @ap: ATA port to read SCR for
5121 * @reg: SCR to read
5122 * @val: Place to store read value
5123 *
5124 * Read SCR register @reg of @ap into *@val. This function is
5125 * guaranteed to succeed if the cable type of the port is SATA
5126 * and the port implements ->scr_read.
5127 *
5128 * LOCKING:
5129 * None.
5130 *
5131 * RETURNS:
5132 * 0 on success, negative errno on failure.
5133 */
5134int sata_scr_read(struct ata_port *ap, int reg, u32 *val)
5135{
5136 if (sata_scr_valid(ap)) {
5137 *val = ap->ops->scr_read(ap, reg);
5138 return 0;
5139 }
5140 return -EOPNOTSUPP;
5141}
5142
5143/**
5144 * sata_scr_write - write SCR register of the specified port
5145 * @ap: ATA port to write SCR for
5146 * @reg: SCR to write
5147 * @val: value to write
5148 *
5149 * Write @val to SCR register @reg of @ap. This function is
5150 * guaranteed to succeed if the cable type of the port is SATA
5151 * and the port implements ->scr_read.
5152 *
5153 * LOCKING:
5154 * None.
5155 *
5156 * RETURNS:
5157 * 0 on success, negative errno on failure.
5158 */
5159int sata_scr_write(struct ata_port *ap, int reg, u32 val)
5160{
5161 if (sata_scr_valid(ap)) {
5162 ap->ops->scr_write(ap, reg, val);
5163 return 0;
5164 }
5165 return -EOPNOTSUPP;
5166}
5167
5168/**
5169 * sata_scr_write_flush - write SCR register of the specified port and flush
5170 * @ap: ATA port to write SCR for
5171 * @reg: SCR to write
5172 * @val: value to write
5173 *
5174 * This function is identical to sata_scr_write() except that this
5175 * function performs flush after writing to the register.
5176 *
5177 * LOCKING:
5178 * None.
5179 *
5180 * RETURNS:
5181 * 0 on success, negative errno on failure.
5182 */
5183int sata_scr_write_flush(struct ata_port *ap, int reg, u32 val)
5184{
5185 if (sata_scr_valid(ap)) {
5186 ap->ops->scr_write(ap, reg, val);
5187 ap->ops->scr_read(ap, reg);
5188 return 0;
5189 }
5190 return -EOPNOTSUPP;
5191}
5192
5193/**
5194 * ata_port_online - test whether the given port is online
5195 * @ap: ATA port to test
5196 *
5197 * Test whether @ap is online. Note that this function returns 0
5198 * if online status of @ap cannot be obtained, so
5199 * ata_port_online(ap) != !ata_port_offline(ap).
5200 *
5201 * LOCKING:
5202 * None.
5203 *
5204 * RETURNS:
5205 * 1 if the port online status is available and online.
5206 */
5207int ata_port_online(struct ata_port *ap)
5208{
5209 u32 sstatus;
5210
5211 if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) == 0x3)
5212 return 1;
5213 return 0;
5214}
5215
5216/**
5217 * ata_port_offline - test whether the given port is offline
5218 * @ap: ATA port to test
5219 *
5220 * Test whether @ap is offline. Note that this function returns
5221 * 0 if offline status of @ap cannot be obtained, so
5222 * ata_port_online(ap) != !ata_port_offline(ap).
5223 *
5224 * LOCKING:
5225 * None.
5226 *
5227 * RETURNS:
5228 * 1 if the port offline status is available and offline.
5229 */
5230int ata_port_offline(struct ata_port *ap)
5231{
5232 u32 sstatus;
5233
5234 if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) != 0x3)
5235 return 1;
5236 return 0;
5237}
Edward Falk0baab862005-06-02 18:17:13 -04005238
Tejun Heo77b08fb2006-06-24 20:30:19 +09005239int ata_flush_cache(struct ata_device *dev)
Jens Axboe9b847542006-01-06 09:28:07 +01005240{
Tejun Heo977e6b92006-06-24 20:30:19 +09005241 unsigned int err_mask;
Jens Axboe9b847542006-01-06 09:28:07 +01005242 u8 cmd;
5243
5244 if (!ata_try_flush_cache(dev))
5245 return 0;
5246
Tejun Heo6fc49ad2006-11-11 20:10:45 +09005247 if (dev->flags & ATA_DFLAG_FLUSH_EXT)
Jens Axboe9b847542006-01-06 09:28:07 +01005248 cmd = ATA_CMD_FLUSH_EXT;
5249 else
5250 cmd = ATA_CMD_FLUSH;
5251
Tejun Heo977e6b92006-06-24 20:30:19 +09005252 err_mask = ata_do_simple_cmd(dev, cmd);
5253 if (err_mask) {
5254 ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
5255 return -EIO;
5256 }
5257
5258 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +01005259}
5260
Jeff Garzikcca39742006-08-24 03:19:22 -04005261static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
5262 unsigned int action, unsigned int ehi_flags,
5263 int wait)
Tejun Heo500530f2006-07-03 16:07:27 +09005264{
5265 unsigned long flags;
5266 int i, rc;
5267
Jeff Garzikcca39742006-08-24 03:19:22 -04005268 for (i = 0; i < host->n_ports; i++) {
5269 struct ata_port *ap = host->ports[i];
Tejun Heo500530f2006-07-03 16:07:27 +09005270
5271 /* Previous resume operation might still be in
5272 * progress. Wait for PM_PENDING to clear.
5273 */
5274 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5275 ata_port_wait_eh(ap);
5276 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5277 }
5278
5279 /* request PM ops to EH */
5280 spin_lock_irqsave(ap->lock, flags);
5281
5282 ap->pm_mesg = mesg;
5283 if (wait) {
5284 rc = 0;
5285 ap->pm_result = &rc;
5286 }
5287
5288 ap->pflags |= ATA_PFLAG_PM_PENDING;
5289 ap->eh_info.action |= action;
5290 ap->eh_info.flags |= ehi_flags;
5291
5292 ata_port_schedule_eh(ap);
5293
5294 spin_unlock_irqrestore(ap->lock, flags);
5295
5296 /* wait and check result */
5297 if (wait) {
5298 ata_port_wait_eh(ap);
5299 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5300 if (rc)
5301 return rc;
5302 }
5303 }
5304
5305 return 0;
5306}
5307
5308/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005309 * ata_host_suspend - suspend host
5310 * @host: host to suspend
Tejun Heo500530f2006-07-03 16:07:27 +09005311 * @mesg: PM message
5312 *
Jeff Garzikcca39742006-08-24 03:19:22 -04005313 * Suspend @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09005314 * function requests EH to perform PM operations and waits for EH
5315 * to finish.
5316 *
5317 * LOCKING:
5318 * Kernel thread context (may sleep).
5319 *
5320 * RETURNS:
5321 * 0 on success, -errno on failure.
5322 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005323int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09005324{
5325 int i, j, rc;
5326
Jeff Garzikcca39742006-08-24 03:19:22 -04005327 rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
Tejun Heo500530f2006-07-03 16:07:27 +09005328 if (rc)
5329 goto fail;
5330
5331 /* EH is quiescent now. Fail if we have any ready device.
5332 * This happens if hotplug occurs between completion of device
5333 * suspension and here.
5334 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005335 for (i = 0; i < host->n_ports; i++) {
5336 struct ata_port *ap = host->ports[i];
Tejun Heo500530f2006-07-03 16:07:27 +09005337
5338 for (j = 0; j < ATA_MAX_DEVICES; j++) {
5339 struct ata_device *dev = &ap->device[j];
5340
5341 if (ata_dev_ready(dev)) {
5342 ata_port_printk(ap, KERN_WARNING,
5343 "suspend failed, device %d "
5344 "still active\n", dev->devno);
5345 rc = -EBUSY;
5346 goto fail;
5347 }
5348 }
5349 }
5350
Jeff Garzikcca39742006-08-24 03:19:22 -04005351 host->dev->power.power_state = mesg;
Tejun Heo500530f2006-07-03 16:07:27 +09005352 return 0;
5353
5354 fail:
Jeff Garzikcca39742006-08-24 03:19:22 -04005355 ata_host_resume(host);
Tejun Heo500530f2006-07-03 16:07:27 +09005356 return rc;
5357}
5358
5359/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005360 * ata_host_resume - resume host
5361 * @host: host to resume
Tejun Heo500530f2006-07-03 16:07:27 +09005362 *
Jeff Garzikcca39742006-08-24 03:19:22 -04005363 * Resume @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09005364 * function requests EH to perform PM operations and returns.
5365 * Note that all resume operations are performed parallely.
5366 *
5367 * LOCKING:
5368 * Kernel thread context (may sleep).
5369 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005370void ata_host_resume(struct ata_host *host)
Tejun Heo500530f2006-07-03 16:07:27 +09005371{
Jeff Garzikcca39742006-08-24 03:19:22 -04005372 ata_host_request_pm(host, PMSG_ON, ATA_EH_SOFTRESET,
5373 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
5374 host->dev->power.power_state = PMSG_ON;
Tejun Heo500530f2006-07-03 16:07:27 +09005375}
5376
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005377/**
5378 * ata_port_start - Set port up for dma.
5379 * @ap: Port to initialize
5380 *
5381 * Called just after data structures for each port are
5382 * initialized. Allocates space for PRD table.
5383 *
5384 * May be used as the port_start() entry in ata_port_operations.
5385 *
5386 * LOCKING:
5387 * Inherited from caller.
5388 */
5389
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390int ata_port_start (struct ata_port *ap)
5391{
Brian King2f1f6102006-03-23 17:30:15 -06005392 struct device *dev = ap->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05005393 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394
5395 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
5396 if (!ap->prd)
5397 return -ENOMEM;
5398
Jeff Garzik6037d6b2005-11-04 22:08:00 -05005399 rc = ata_pad_alloc(ap, dev);
5400 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04005401 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05005402 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04005403 }
5404
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
5406
5407 return 0;
5408}
5409
Edward Falk0baab862005-06-02 18:17:13 -04005410
5411/**
5412 * ata_port_stop - Undo ata_port_start()
5413 * @ap: Port to shut down
5414 *
5415 * Frees the PRD table.
5416 *
5417 * May be used as the port_stop() entry in ata_port_operations.
5418 *
5419 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005420 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04005421 */
5422
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423void ata_port_stop (struct ata_port *ap)
5424{
Brian King2f1f6102006-03-23 17:30:15 -06005425 struct device *dev = ap->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426
5427 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05005428 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429}
5430
Jeff Garzikcca39742006-08-24 03:19:22 -04005431void ata_host_stop (struct ata_host *host)
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005432{
Jeff Garzikcca39742006-08-24 03:19:22 -04005433 if (host->mmio_base)
5434 iounmap(host->mmio_base);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005435}
5436
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437/**
Tejun Heo3ef3b432006-05-31 18:27:30 +09005438 * ata_dev_init - Initialize an ata_device structure
5439 * @dev: Device structure to initialize
5440 *
5441 * Initialize @dev in preparation for probing.
5442 *
5443 * LOCKING:
5444 * Inherited from caller.
5445 */
5446void ata_dev_init(struct ata_device *dev)
5447{
5448 struct ata_port *ap = dev->ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09005449 unsigned long flags;
Tejun Heo3ef3b432006-05-31 18:27:30 +09005450
Tejun Heo5a04bf42006-05-31 18:27:38 +09005451 /* SATA spd limit is bound to the first device */
5452 ap->sata_spd_limit = ap->hw_sata_spd_limit;
5453
Tejun Heo72fa4b72006-05-31 18:27:32 +09005454 /* High bits of dev->flags are used to record warm plug
5455 * requests which occur asynchronously. Synchronize using
Jeff Garzikcca39742006-08-24 03:19:22 -04005456 * host lock.
Tejun Heo72fa4b72006-05-31 18:27:32 +09005457 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005458 spin_lock_irqsave(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005459 dev->flags &= ~ATA_DFLAG_INIT_MASK;
Jeff Garzikba6a1302006-06-22 23:46:10 -04005460 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005461
5462 memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
5463 sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
Tejun Heo3ef3b432006-05-31 18:27:30 +09005464 dev->pio_mask = UINT_MAX;
5465 dev->mwdma_mask = UINT_MAX;
5466 dev->udma_mask = UINT_MAX;
5467}
5468
5469/**
Brian King155a8a92006-08-07 14:27:17 -05005470 * ata_port_init - Initialize an ata_port structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 * @ap: Structure to initialize
Jeff Garzikcca39742006-08-24 03:19:22 -04005472 * @host: Collection of hosts to which @ap belongs
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 * @ent: Probe information provided by low-level driver
5474 * @port_no: Port number associated with this ata_port
5475 *
Brian King155a8a92006-08-07 14:27:17 -05005476 * Initialize a new ata_port structure.
Jeff Garzik0cba6322005-05-30 19:49:12 -04005477 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005479 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005481void ata_port_init(struct ata_port *ap, struct ata_host *host,
Brian King155a8a92006-08-07 14:27:17 -05005482 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483{
5484 unsigned int i;
5485
Jeff Garzikcca39742006-08-24 03:19:22 -04005486 ap->lock = &host->lock;
Tejun Heo198e0fe2006-04-02 18:51:52 +09005487 ap->flags = ATA_FLAG_DISABLED;
Brian King155a8a92006-08-07 14:27:17 -05005488 ap->id = ata_unique_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 ap->ctl = ATA_DEVCTL_OBS;
Jeff Garzikcca39742006-08-24 03:19:22 -04005490 ap->host = host;
Brian King2f1f6102006-03-23 17:30:15 -06005491 ap->dev = ent->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 ap->port_no = port_no;
Tejun Heofea63e32006-09-16 03:04:15 +09005493 if (port_no == 1 && ent->pinfo2) {
5494 ap->pio_mask = ent->pinfo2->pio_mask;
5495 ap->mwdma_mask = ent->pinfo2->mwdma_mask;
5496 ap->udma_mask = ent->pinfo2->udma_mask;
5497 ap->flags |= ent->pinfo2->flags;
5498 ap->ops = ent->pinfo2->port_ops;
5499 } else {
5500 ap->pio_mask = ent->pio_mask;
5501 ap->mwdma_mask = ent->mwdma_mask;
5502 ap->udma_mask = ent->udma_mask;
5503 ap->flags |= ent->port_flags;
5504 ap->ops = ent->port_ops;
5505 }
Tejun Heo5a04bf42006-05-31 18:27:38 +09005506 ap->hw_sata_spd_limit = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 ap->active_tag = ATA_TAG_POISON;
5508 ap->last_ctl = 0xFF;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005509
5510#if defined(ATA_VERBOSE_DEBUG)
5511 /* turn on all debugging levels */
5512 ap->msg_enable = 0x00FF;
5513#elif defined(ATA_DEBUG)
5514 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 +09005515#else
Borislav Petkov0dd4b212006-06-23 02:29:08 -04005516 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005517#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518
Tejun Heo86e45b62006-03-05 15:29:09 +09005519 INIT_WORK(&ap->port_task, NULL, NULL);
Tejun Heo580b2102006-05-31 18:28:05 +09005520 INIT_WORK(&ap->hotplug_task, ata_scsi_hotplug, ap);
zhao, forrest3057ac32006-06-12 12:01:34 +08005521 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan, ap);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005522 INIT_LIST_HEAD(&ap->eh_done_q);
Tejun Heoc6cf9e92006-05-31 18:27:27 +09005523 init_waitqueue_head(&ap->eh_wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524
Tejun Heo838df622006-05-15 20:57:44 +09005525 /* set cable type */
5526 ap->cbl = ATA_CBL_NONE;
5527 if (ap->flags & ATA_FLAG_SATA)
5528 ap->cbl = ATA_CBL_SATA;
5529
Tejun Heoacf356b2006-03-24 14:07:50 +09005530 for (i = 0; i < ATA_MAX_DEVICES; i++) {
5531 struct ata_device *dev = &ap->device[i];
Tejun Heo38d87232006-05-15 20:57:51 +09005532 dev->ap = ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09005533 dev->devno = i;
Tejun Heo3ef3b432006-05-31 18:27:30 +09005534 ata_dev_init(dev);
Tejun Heoacf356b2006-03-24 14:07:50 +09005535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536
5537#ifdef ATA_IRQ_TRAP
5538 ap->stats.unhandled_irq = 1;
5539 ap->stats.idle_irq = 1;
5540#endif
5541
5542 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
5543}
5544
5545/**
Tejun Heo4608c162006-08-10 16:59:01 +09005546 * ata_port_init_shost - Initialize SCSI host associated with ATA port
5547 * @ap: ATA port to initialize SCSI host for
5548 * @shost: SCSI host associated with @ap
Brian King155a8a92006-08-07 14:27:17 -05005549 *
Tejun Heo4608c162006-08-10 16:59:01 +09005550 * Initialize SCSI host @shost associated with ATA port @ap.
Brian King155a8a92006-08-07 14:27:17 -05005551 *
5552 * LOCKING:
5553 * Inherited from caller.
5554 */
Tejun Heo4608c162006-08-10 16:59:01 +09005555static void ata_port_init_shost(struct ata_port *ap, struct Scsi_Host *shost)
Brian King155a8a92006-08-07 14:27:17 -05005556{
Jeff Garzikcca39742006-08-24 03:19:22 -04005557 ap->scsi_host = shost;
Brian King155a8a92006-08-07 14:27:17 -05005558
Tejun Heo4608c162006-08-10 16:59:01 +09005559 shost->unique_id = ap->id;
5560 shost->max_id = 16;
5561 shost->max_lun = 1;
5562 shost->max_channel = 1;
5563 shost->max_cmd_len = 12;
Brian King155a8a92006-08-07 14:27:17 -05005564}
5565
5566/**
Jeff Garzik996139f2006-08-10 16:59:03 +09005567 * ata_port_add - Attach low-level ATA driver to system
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 * @ent: Information provided by low-level driver
Jeff Garzikcca39742006-08-24 03:19:22 -04005569 * @host: Collections of ports to which we add
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 * @port_no: Port number associated with this host
5571 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04005572 * Attach low-level ATA driver to system.
5573 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005575 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 *
5577 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005578 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 */
Jeff Garzik996139f2006-08-10 16:59:03 +09005580static struct ata_port * ata_port_add(const struct ata_probe_ent *ent,
Jeff Garzikcca39742006-08-24 03:19:22 -04005581 struct ata_host *host,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 unsigned int port_no)
5583{
Jeff Garzik996139f2006-08-10 16:59:03 +09005584 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 struct ata_port *ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586
5587 DPRINTK("ENTER\n");
Tejun Heoaec5c3c2006-03-25 01:33:34 +09005588
Tejun Heo52783c52006-05-31 18:28:22 +09005589 if (!ent->port_ops->error_handler &&
Jeff Garzikcca39742006-08-24 03:19:22 -04005590 !(ent->port_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) {
Tejun Heoaec5c3c2006-03-25 01:33:34 +09005591 printk(KERN_ERR "ata%u: no reset mechanism available\n",
5592 port_no);
5593 return NULL;
5594 }
5595
Jeff Garzik996139f2006-08-10 16:59:03 +09005596 shost = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
5597 if (!shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 return NULL;
5599
Jeff Garzik996139f2006-08-10 16:59:03 +09005600 shost->transportt = &ata_scsi_transport_template;
Tejun Heo30afc842006-03-18 18:40:14 +09005601
Jeff Garzik996139f2006-08-10 16:59:03 +09005602 ap = ata_shost_to_port(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603
Jeff Garzikcca39742006-08-24 03:19:22 -04005604 ata_port_init(ap, host, ent, port_no);
Jeff Garzik996139f2006-08-10 16:59:03 +09005605 ata_port_init_shost(ap, shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 return ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608}
5609
5610/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005611 * ata_sas_host_init - Initialize a host struct
5612 * @host: host to initialize
5613 * @dev: device host is attached to
5614 * @flags: host flags
5615 * @ops: port_ops
Brian Kingb03732f2006-08-07 14:27:10 -05005616 *
5617 * LOCKING:
5618 * PCI/etc. bus probe sem.
5619 *
5620 */
5621
Jeff Garzikcca39742006-08-24 03:19:22 -04005622void ata_host_init(struct ata_host *host, struct device *dev,
5623 unsigned long flags, const struct ata_port_operations *ops)
Brian Kingb03732f2006-08-07 14:27:10 -05005624{
Jeff Garzikcca39742006-08-24 03:19:22 -04005625 spin_lock_init(&host->lock);
5626 host->dev = dev;
5627 host->flags = flags;
5628 host->ops = ops;
Brian Kingb03732f2006-08-07 14:27:10 -05005629}
5630
5631/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04005632 * ata_device_add - Register hardware device with ATA and SCSI layers
5633 * @ent: Probe information describing hardware device to be registered
5634 *
5635 * This function processes the information provided in the probe
5636 * information struct @ent, allocates the necessary ATA and SCSI
5637 * host information structures, initializes them, and registers
5638 * everything with requisite kernel subsystems.
5639 *
5640 * This function requests irqs, probes the ATA bus, and probes
5641 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 *
5643 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005644 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 *
5646 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005647 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 */
Jeff Garzik057ace52005-10-22 14:27:05 -04005649int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650{
Jeff Garzik6d0500d2006-08-10 16:59:05 +09005651 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 struct device *dev = ent->dev;
Jeff Garzikcca39742006-08-24 03:19:22 -04005653 struct ata_host *host;
Jeff Garzik39b07ce2006-06-11 23:59:44 -04005654 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655
5656 DPRINTK("ENTER\n");
Alan Cox02f076a2006-09-26 17:35:32 +01005657
5658 if (ent->irq == 0) {
5659 dev_printk(KERN_ERR, dev, "is not available: No interrupt assigned.\n");
5660 return 0;
5661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 /* alloc a container for our list of ATA ports (buses) */
Jeff Garzikcca39742006-08-24 03:19:22 -04005663 host = kzalloc(sizeof(struct ata_host) +
5664 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
5665 if (!host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667
Jeff Garzikcca39742006-08-24 03:19:22 -04005668 ata_host_init(host, dev, ent->_host_flags, ent->port_ops);
5669 host->n_ports = ent->n_ports;
5670 host->irq = ent->irq;
5671 host->irq2 = ent->irq2;
5672 host->mmio_base = ent->mmio_base;
5673 host->private_data = ent->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674
5675 /* register each port bound to this device */
Jeff Garzikcca39742006-08-24 03:19:22 -04005676 for (i = 0; i < host->n_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 struct ata_port *ap;
5678 unsigned long xfer_mode_mask;
Alan Cox2ec7df02006-08-10 16:59:10 +09005679 int irq_line = ent->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680
Jeff Garzikcca39742006-08-24 03:19:22 -04005681 ap = ata_port_add(ent, host, i);
Dave Jonesc38778c2006-09-26 23:52:50 -07005682 host->ports[i] = ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 if (!ap)
5684 goto err_out;
5685
Tejun Heodd5b06c2006-08-10 16:59:12 +09005686 /* dummy? */
5687 if (ent->dummy_port_mask & (1 << i)) {
5688 ata_port_printk(ap, KERN_INFO, "DUMMY\n");
5689 ap->ops = &ata_dummy_port_ops;
5690 continue;
5691 }
5692
5693 /* start port */
5694 rc = ap->ops->port_start(ap);
5695 if (rc) {
Jeff Garzikcca39742006-08-24 03:19:22 -04005696 host->ports[i] = NULL;
5697 scsi_host_put(ap->scsi_host);
Tejun Heodd5b06c2006-08-10 16:59:12 +09005698 goto err_out;
5699 }
5700
Alan Cox2ec7df02006-08-10 16:59:10 +09005701 /* Report the secondary IRQ for second channel legacy */
5702 if (i == 1 && ent->irq2)
5703 irq_line = ent->irq2;
5704
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
5706 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
5707 (ap->pio_mask << ATA_SHIFT_PIO);
5708
5709 /* print per-port info to dmesg */
Tejun Heof15a1da2006-05-15 20:57:56 +09005710 ata_port_printk(ap, KERN_INFO, "%cATA max %s cmd 0x%lX "
Alan Cox2ec7df02006-08-10 16:59:10 +09005711 "ctl 0x%lX bmdma 0x%lX irq %d\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09005712 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
5713 ata_mode_string(xfer_mode_mask),
5714 ap->ioaddr.cmd_addr,
5715 ap->ioaddr.ctl_addr,
5716 ap->ioaddr.bmdma_addr,
Alan Cox2ec7df02006-08-10 16:59:10 +09005717 irq_line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718
Tejun Heo0f0a3ad2006-11-17 12:24:22 +09005719 /* freeze port before requesting IRQ */
5720 ata_eh_freeze_port(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 }
5722
Alan Cox2ec7df02006-08-10 16:59:10 +09005723 /* obtain irq, that may be shared between channels */
Jeff Garzik39b07ce2006-06-11 23:59:44 -04005724 rc = request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
Jeff Garzikcca39742006-08-24 03:19:22 -04005725 DRV_NAME, host);
Jeff Garzik39b07ce2006-06-11 23:59:44 -04005726 if (rc) {
5727 dev_printk(KERN_ERR, dev, "irq %lu request failed: %d\n",
5728 ent->irq, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 goto err_out;
Jeff Garzik39b07ce2006-06-11 23:59:44 -04005730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731
Alan Cox2ec7df02006-08-10 16:59:10 +09005732 /* do we have a second IRQ for the other channel, eg legacy mode */
5733 if (ent->irq2) {
5734 /* We will get weird core code crashes later if this is true
5735 so trap it now */
5736 BUG_ON(ent->irq == ent->irq2);
5737
5738 rc = request_irq(ent->irq2, ent->port_ops->irq_handler, ent->irq_flags,
Jeff Garzikcca39742006-08-24 03:19:22 -04005739 DRV_NAME, host);
Alan Cox2ec7df02006-08-10 16:59:10 +09005740 if (rc) {
5741 dev_printk(KERN_ERR, dev, "irq %lu request failed: %d\n",
5742 ent->irq2, rc);
5743 goto err_out_free_irq;
5744 }
5745 }
5746
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 /* perform each probe synchronously */
5748 DPRINTK("probe begin\n");
Jeff Garzikcca39742006-08-24 03:19:22 -04005749 for (i = 0; i < host->n_ports; i++) {
5750 struct ata_port *ap = host->ports[i];
Tejun Heo5a04bf42006-05-31 18:27:38 +09005751 u32 scontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 int rc;
5753
Tejun Heo5a04bf42006-05-31 18:27:38 +09005754 /* init sata_spd_limit to the current value */
5755 if (sata_scr_read(ap, SCR_CONTROL, &scontrol) == 0) {
5756 int spd = (scontrol >> 4) & 0xf;
5757 ap->hw_sata_spd_limit &= (1 << spd) - 1;
5758 }
5759 ap->sata_spd_limit = ap->hw_sata_spd_limit;
5760
Jeff Garzikcca39742006-08-24 03:19:22 -04005761 rc = scsi_add_host(ap->scsi_host, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 if (rc) {
Tejun Heof15a1da2006-05-15 20:57:56 +09005763 ata_port_printk(ap, KERN_ERR, "scsi_add_host failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 /* FIXME: do something useful here */
5765 /* FIXME: handle unconditional calls to
5766 * scsi_scan_host and ata_host_remove, below,
5767 * at the very least
5768 */
5769 }
Tejun Heo3e706392006-05-31 18:28:11 +09005770
Tejun Heo52783c52006-05-31 18:28:22 +09005771 if (ap->ops->error_handler) {
Tejun Heo1cdaf532006-07-03 16:07:26 +09005772 struct ata_eh_info *ehi = &ap->eh_info;
Tejun Heo3e706392006-05-31 18:28:11 +09005773 unsigned long flags;
5774
5775 ata_port_probe(ap);
5776
5777 /* kick EH for boot probing */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005778 spin_lock_irqsave(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09005779
Tejun Heo1cdaf532006-07-03 16:07:26 +09005780 ehi->probe_mask = (1 << ATA_MAX_DEVICES) - 1;
5781 ehi->action |= ATA_EH_SOFTRESET;
5782 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
Tejun Heo3e706392006-05-31 18:28:11 +09005783
Tejun Heob51e9e52006-06-29 01:29:30 +09005784 ap->pflags |= ATA_PFLAG_LOADING;
Tejun Heo3e706392006-05-31 18:28:11 +09005785 ata_port_schedule_eh(ap);
5786
Jeff Garzikba6a1302006-06-22 23:46:10 -04005787 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09005788
5789 /* wait for EH to finish */
5790 ata_port_wait_eh(ap);
5791 } else {
5792 DPRINTK("ata%u: bus probe begin\n", ap->id);
5793 rc = ata_bus_probe(ap);
5794 DPRINTK("ata%u: bus probe end\n", ap->id);
5795
5796 if (rc) {
5797 /* FIXME: do something useful here?
5798 * Current libata behavior will
5799 * tear down everything when
5800 * the module is removed
5801 * or the h/w is unplugged.
5802 */
5803 }
5804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 }
5806
5807 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005808 DPRINTK("host probe begin\n");
Jeff Garzikcca39742006-08-24 03:19:22 -04005809 for (i = 0; i < host->n_ports; i++) {
5810 struct ata_port *ap = host->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811
Jeff Garzik644dd0c2005-10-03 15:55:19 -04005812 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 }
5814
Jeff Garzikcca39742006-08-24 03:19:22 -04005815 dev_set_drvdata(dev, host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816
5817 VPRINTK("EXIT, returning %u\n", ent->n_ports);
5818 return ent->n_ports; /* success */
5819
Alan Cox2ec7df02006-08-10 16:59:10 +09005820err_out_free_irq:
Jeff Garzikcca39742006-08-24 03:19:22 -04005821 free_irq(ent->irq, host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822err_out:
Jeff Garzikcca39742006-08-24 03:19:22 -04005823 for (i = 0; i < host->n_ports; i++) {
5824 struct ata_port *ap = host->ports[i];
Tejun Heo77f3f872006-08-05 03:59:19 +09005825 if (ap) {
5826 ap->ops->port_stop(ap);
Jeff Garzikcca39742006-08-24 03:19:22 -04005827 scsi_host_put(ap->scsi_host);
Tejun Heo77f3f872006-08-05 03:59:19 +09005828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 }
Jeff Garzik6d0500d2006-08-10 16:59:05 +09005830
Jeff Garzikcca39742006-08-24 03:19:22 -04005831 kfree(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 VPRINTK("EXIT, returning 0\n");
5833 return 0;
5834}
5835
5836/**
Tejun Heo720ba122006-05-31 18:28:13 +09005837 * ata_port_detach - Detach ATA port in prepration of device removal
5838 * @ap: ATA port to be detached
5839 *
5840 * Detach all ATA devices and the associated SCSI devices of @ap;
5841 * then, remove the associated SCSI host. @ap is guaranteed to
5842 * be quiescent on return from this function.
5843 *
5844 * LOCKING:
5845 * Kernel thread context (may sleep).
5846 */
5847void ata_port_detach(struct ata_port *ap)
5848{
5849 unsigned long flags;
5850 int i;
5851
5852 if (!ap->ops->error_handler)
Tejun Heoc3cf30a2006-08-05 03:59:11 +09005853 goto skip_eh;
Tejun Heo720ba122006-05-31 18:28:13 +09005854
5855 /* tell EH we're leaving & flush EH */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005856 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09005857 ap->pflags |= ATA_PFLAG_UNLOADING;
Jeff Garzikba6a1302006-06-22 23:46:10 -04005858 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005859
5860 ata_port_wait_eh(ap);
5861
5862 /* EH is now guaranteed to see UNLOADING, so no new device
5863 * will be attached. Disable all existing devices.
5864 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005865 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005866
5867 for (i = 0; i < ATA_MAX_DEVICES; i++)
5868 ata_dev_disable(&ap->device[i]);
5869
Jeff Garzikba6a1302006-06-22 23:46:10 -04005870 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005871
5872 /* Final freeze & EH. All in-flight commands are aborted. EH
5873 * will be skipped and retrials will be terminated with bad
5874 * target.
5875 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005876 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005877 ata_port_freeze(ap); /* won't be thawed */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005878 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005879
5880 ata_port_wait_eh(ap);
5881
5882 /* Flush hotplug task. The sequence is similar to
5883 * ata_port_flush_task().
5884 */
5885 flush_workqueue(ata_aux_wq);
5886 cancel_delayed_work(&ap->hotplug_task);
5887 flush_workqueue(ata_aux_wq);
5888
Tejun Heoc3cf30a2006-08-05 03:59:11 +09005889 skip_eh:
Tejun Heo720ba122006-05-31 18:28:13 +09005890 /* remove the associated SCSI host */
Jeff Garzikcca39742006-08-24 03:19:22 -04005891 scsi_remove_host(ap->scsi_host);
Tejun Heo720ba122006-05-31 18:28:13 +09005892}
5893
5894/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005895 * ata_host_remove - PCI layer callback for device removal
5896 * @host: ATA host set that was removed
Alan Cox17b14452005-09-15 15:44:00 +01005897 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05005898 * Unregister all objects associated with this host set. Free those
Alan Cox17b14452005-09-15 15:44:00 +01005899 * objects.
5900 *
5901 * LOCKING:
5902 * Inherited from calling layer (may sleep).
5903 */
5904
Jeff Garzikcca39742006-08-24 03:19:22 -04005905void ata_host_remove(struct ata_host *host)
Alan Cox17b14452005-09-15 15:44:00 +01005906{
Alan Cox17b14452005-09-15 15:44:00 +01005907 unsigned int i;
5908
Jeff Garzikcca39742006-08-24 03:19:22 -04005909 for (i = 0; i < host->n_ports; i++)
5910 ata_port_detach(host->ports[i]);
Alan Cox17b14452005-09-15 15:44:00 +01005911
Jeff Garzikcca39742006-08-24 03:19:22 -04005912 free_irq(host->irq, host);
5913 if (host->irq2)
5914 free_irq(host->irq2, host);
Alan Cox17b14452005-09-15 15:44:00 +01005915
Jeff Garzikcca39742006-08-24 03:19:22 -04005916 for (i = 0; i < host->n_ports; i++) {
5917 struct ata_port *ap = host->ports[i];
Alan Cox17b14452005-09-15 15:44:00 +01005918
Jeff Garzikcca39742006-08-24 03:19:22 -04005919 ata_scsi_release(ap->scsi_host);
Alan Cox17b14452005-09-15 15:44:00 +01005920
5921 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
5922 struct ata_ioports *ioaddr = &ap->ioaddr;
5923
Alan Cox2ec7df02006-08-10 16:59:10 +09005924 /* FIXME: Add -ac IDE pci mods to remove these special cases */
5925 if (ioaddr->cmd_addr == ATA_PRIMARY_CMD)
5926 release_region(ATA_PRIMARY_CMD, 8);
5927 else if (ioaddr->cmd_addr == ATA_SECONDARY_CMD)
5928 release_region(ATA_SECONDARY_CMD, 8);
Alan Cox17b14452005-09-15 15:44:00 +01005929 }
5930
Jeff Garzikcca39742006-08-24 03:19:22 -04005931 scsi_host_put(ap->scsi_host);
Alan Cox17b14452005-09-15 15:44:00 +01005932 }
5933
Jeff Garzikcca39742006-08-24 03:19:22 -04005934 if (host->ops->host_stop)
5935 host->ops->host_stop(host);
Alan Cox17b14452005-09-15 15:44:00 +01005936
Jeff Garzikcca39742006-08-24 03:19:22 -04005937 kfree(host);
Alan Cox17b14452005-09-15 15:44:00 +01005938}
5939
5940/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 * ata_scsi_release - SCSI layer callback hook for host unload
Jeff Garzik4f931372006-09-29 05:07:25 -04005942 * @shost: libata host to be unloaded
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 *
5944 * Performs all duties necessary to shut down a libata port...
5945 * Kill port kthread, disable port, and release resources.
5946 *
5947 * LOCKING:
5948 * Inherited from SCSI layer.
5949 *
5950 * RETURNS:
5951 * One.
5952 */
5953
Jeff Garzikcca39742006-08-24 03:19:22 -04005954int ata_scsi_release(struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955{
Jeff Garzikcca39742006-08-24 03:19:22 -04005956 struct ata_port *ap = ata_shost_to_port(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957
5958 DPRINTK("ENTER\n");
5959
5960 ap->ops->port_disable(ap);
Jeff Garzik6543bc02006-08-05 03:59:17 +09005961 ap->ops->port_stop(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962
5963 DPRINTK("EXIT\n");
5964 return 1;
5965}
5966
Brian Kingf6d950e2006-08-07 14:27:24 -05005967struct ata_probe_ent *
5968ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
5969{
5970 struct ata_probe_ent *probe_ent;
5971
5972 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
5973 if (!probe_ent) {
5974 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
5975 kobject_name(&(dev->kobj)));
5976 return NULL;
5977 }
5978
5979 INIT_LIST_HEAD(&probe_ent->node);
5980 probe_ent->dev = dev;
5981
5982 probe_ent->sht = port->sht;
Jeff Garzikcca39742006-08-24 03:19:22 -04005983 probe_ent->port_flags = port->flags;
Brian Kingf6d950e2006-08-07 14:27:24 -05005984 probe_ent->pio_mask = port->pio_mask;
5985 probe_ent->mwdma_mask = port->mwdma_mask;
5986 probe_ent->udma_mask = port->udma_mask;
5987 probe_ent->port_ops = port->port_ops;
Jeff Garzikd639ca92006-09-28 03:48:18 -04005988 probe_ent->private_data = port->private_data;
Brian Kingf6d950e2006-08-07 14:27:24 -05005989
5990 return probe_ent;
5991}
5992
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993/**
5994 * ata_std_ports - initialize ioaddr with standard port offsets.
5995 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04005996 *
5997 * Utility function which initializes data_addr, error_addr,
5998 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
5999 * device_addr, status_addr, and command_addr to standard offsets
6000 * relative to cmd_addr.
6001 *
6002 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 */
Edward Falk0baab862005-06-02 18:17:13 -04006004
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005void ata_std_ports(struct ata_ioports *ioaddr)
6006{
6007 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
6008 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
6009 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
6010 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
6011 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
6012 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
6013 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
6014 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
6015 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
6016 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
6017}
6018
Edward Falk0baab862005-06-02 18:17:13 -04006019
Jeff Garzik374b1872005-08-30 05:42:52 -04006020#ifdef CONFIG_PCI
6021
Jeff Garzikcca39742006-08-24 03:19:22 -04006022void ata_pci_host_stop (struct ata_host *host)
Jeff Garzik374b1872005-08-30 05:42:52 -04006023{
Jeff Garzikcca39742006-08-24 03:19:22 -04006024 struct pci_dev *pdev = to_pci_dev(host->dev);
Jeff Garzik374b1872005-08-30 05:42:52 -04006025
Jeff Garzikcca39742006-08-24 03:19:22 -04006026 pci_iounmap(pdev, host->mmio_base);
Jeff Garzik374b1872005-08-30 05:42:52 -04006027}
6028
Edward Falk0baab862005-06-02 18:17:13 -04006029/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 * ata_pci_remove_one - PCI layer callback for device removal
6031 * @pdev: PCI device that was removed
6032 *
6033 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04006034 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 * Handle this by unregistering all objects associated
6036 * with this PCI device. Free those objects. Then finally
6037 * release PCI resources and disable device.
6038 *
6039 * LOCKING:
6040 * Inherited from PCI layer (may sleep).
6041 */
6042
6043void ata_pci_remove_one (struct pci_dev *pdev)
6044{
6045 struct device *dev = pci_dev_to_dev(pdev);
Jeff Garzikcca39742006-08-24 03:19:22 -04006046 struct ata_host *host = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
Jeff Garzikcca39742006-08-24 03:19:22 -04006048 ata_host_remove(host);
Tejun Heof0eb62b2006-06-12 23:05:38 +09006049
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 pci_release_regions(pdev);
6051 pci_disable_device(pdev);
6052 dev_set_drvdata(dev, NULL);
6053}
6054
6055/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04006056int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057{
6058 unsigned long tmp = 0;
6059
6060 switch (bits->width) {
6061 case 1: {
6062 u8 tmp8 = 0;
6063 pci_read_config_byte(pdev, bits->reg, &tmp8);
6064 tmp = tmp8;
6065 break;
6066 }
6067 case 2: {
6068 u16 tmp16 = 0;
6069 pci_read_config_word(pdev, bits->reg, &tmp16);
6070 tmp = tmp16;
6071 break;
6072 }
6073 case 4: {
6074 u32 tmp32 = 0;
6075 pci_read_config_dword(pdev, bits->reg, &tmp32);
6076 tmp = tmp32;
6077 break;
6078 }
6079
6080 default:
6081 return -EINVAL;
6082 }
6083
6084 tmp &= bits->mask;
6085
6086 return (tmp == bits->val) ? 1 : 0;
6087}
Jens Axboe9b847542006-01-06 09:28:07 +01006088
Tejun Heo3c5100c2006-07-26 16:58:33 +09006089void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
Jens Axboe9b847542006-01-06 09:28:07 +01006090{
6091 pci_save_state(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006092
Tejun Heo3c5100c2006-07-26 16:58:33 +09006093 if (mesg.event == PM_EVENT_SUSPEND) {
Tejun Heo500530f2006-07-03 16:07:27 +09006094 pci_disable_device(pdev);
6095 pci_set_power_state(pdev, PCI_D3hot);
6096 }
Jens Axboe9b847542006-01-06 09:28:07 +01006097}
6098
Tejun Heo500530f2006-07-03 16:07:27 +09006099void ata_pci_device_do_resume(struct pci_dev *pdev)
Jens Axboe9b847542006-01-06 09:28:07 +01006100{
6101 pci_set_power_state(pdev, PCI_D0);
6102 pci_restore_state(pdev);
6103 pci_enable_device(pdev);
6104 pci_set_master(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006105}
6106
Tejun Heo3c5100c2006-07-26 16:58:33 +09006107int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006108{
Jeff Garzikcca39742006-08-24 03:19:22 -04006109 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo500530f2006-07-03 16:07:27 +09006110 int rc = 0;
6111
Jeff Garzikcca39742006-08-24 03:19:22 -04006112 rc = ata_host_suspend(host, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006113 if (rc)
6114 return rc;
6115
Tejun Heo3c5100c2006-07-26 16:58:33 +09006116 ata_pci_device_do_suspend(pdev, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006117
6118 return 0;
6119}
6120
6121int ata_pci_device_resume(struct pci_dev *pdev)
6122{
Jeff Garzikcca39742006-08-24 03:19:22 -04006123 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo500530f2006-07-03 16:07:27 +09006124
6125 ata_pci_device_do_resume(pdev);
Jeff Garzikcca39742006-08-24 03:19:22 -04006126 ata_host_resume(host);
Jens Axboe9b847542006-01-06 09:28:07 +01006127 return 0;
6128}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129#endif /* CONFIG_PCI */
6130
6131
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132static int __init ata_init(void)
6133{
Andrew Mortona8601e52006-06-25 01:36:52 -07006134 ata_probe_timeout *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 ata_wq = create_workqueue("ata");
6136 if (!ata_wq)
6137 return -ENOMEM;
6138
Tejun Heo453b07a2006-05-31 18:27:42 +09006139 ata_aux_wq = create_singlethread_workqueue("ata_aux");
6140 if (!ata_aux_wq) {
6141 destroy_workqueue(ata_wq);
6142 return -ENOMEM;
6143 }
6144
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6146 return 0;
6147}
6148
6149static void __exit ata_exit(void)
6150{
6151 destroy_workqueue(ata_wq);
Tejun Heo453b07a2006-05-31 18:27:42 +09006152 destroy_workqueue(ata_aux_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153}
6154
Brian Kinga4625082006-11-13 16:32:36 -06006155subsys_initcall(ata_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156module_exit(ata_exit);
6157
Jeff Garzik67846b32005-10-05 02:58:32 -04006158static unsigned long ratelimit_time;
Ingo Molnar34af9462006-06-27 02:53:55 -07006159static DEFINE_SPINLOCK(ata_ratelimit_lock);
Jeff Garzik67846b32005-10-05 02:58:32 -04006160
6161int ata_ratelimit(void)
6162{
6163 int rc;
6164 unsigned long flags;
6165
6166 spin_lock_irqsave(&ata_ratelimit_lock, flags);
6167
6168 if (time_after(jiffies, ratelimit_time)) {
6169 rc = 1;
6170 ratelimit_time = jiffies + (HZ/5);
6171 } else
6172 rc = 0;
6173
6174 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
6175
6176 return rc;
6177}
6178
Tejun Heoc22daff2006-04-11 22:22:29 +09006179/**
6180 * ata_wait_register - wait until register value changes
6181 * @reg: IO-mapped register
6182 * @mask: Mask to apply to read register value
6183 * @val: Wait condition
6184 * @interval_msec: polling interval in milliseconds
6185 * @timeout_msec: timeout in milliseconds
6186 *
6187 * Waiting for some bits of register to change is a common
6188 * operation for ATA controllers. This function reads 32bit LE
6189 * IO-mapped register @reg and tests for the following condition.
6190 *
6191 * (*@reg & mask) != val
6192 *
6193 * If the condition is met, it returns; otherwise, the process is
6194 * repeated after @interval_msec until timeout.
6195 *
6196 * LOCKING:
6197 * Kernel thread context (may sleep)
6198 *
6199 * RETURNS:
6200 * The final register value.
6201 */
6202u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
6203 unsigned long interval_msec,
6204 unsigned long timeout_msec)
6205{
6206 unsigned long timeout;
6207 u32 tmp;
6208
6209 tmp = ioread32(reg);
6210
6211 /* Calculate timeout _after_ the first read to make sure
6212 * preceding writes reach the controller before starting to
6213 * eat away the timeout.
6214 */
6215 timeout = jiffies + (timeout_msec * HZ) / 1000;
6216
6217 while ((tmp & mask) == val && time_before(jiffies, timeout)) {
6218 msleep(interval_msec);
6219 tmp = ioread32(reg);
6220 }
6221
6222 return tmp;
6223}
6224
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225/*
Tejun Heodd5b06c2006-08-10 16:59:12 +09006226 * Dummy port_ops
6227 */
6228static void ata_dummy_noret(struct ata_port *ap) { }
6229static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
6230static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
6231
6232static u8 ata_dummy_check_status(struct ata_port *ap)
6233{
6234 return ATA_DRDY;
6235}
6236
6237static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6238{
6239 return AC_ERR_SYSTEM;
6240}
6241
6242const struct ata_port_operations ata_dummy_port_ops = {
6243 .port_disable = ata_port_disable,
6244 .check_status = ata_dummy_check_status,
6245 .check_altstatus = ata_dummy_check_status,
6246 .dev_select = ata_noop_dev_select,
6247 .qc_prep = ata_noop_qc_prep,
6248 .qc_issue = ata_dummy_qc_issue,
6249 .freeze = ata_dummy_noret,
6250 .thaw = ata_dummy_noret,
6251 .error_handler = ata_dummy_noret,
6252 .post_internal_cmd = ata_dummy_qc_noret,
6253 .irq_clear = ata_dummy_noret,
6254 .port_start = ata_dummy_ret0,
6255 .port_stop = ata_dummy_noret,
6256};
6257
6258/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 * libata is essentially a library of internal helper functions for
6260 * low-level ATA host controller drivers. As such, the API/ABI is
6261 * likely to change as new drivers are added and updated.
6262 * Do not depend on ABI/API stability.
6263 */
6264
Tejun Heoe9c83912006-07-03 16:07:26 +09006265EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
6266EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
6267EXPORT_SYMBOL_GPL(sata_deb_timing_long);
Tejun Heodd5b06c2006-08-10 16:59:12 +09006268EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269EXPORT_SYMBOL_GPL(ata_std_bios_param);
6270EXPORT_SYMBOL_GPL(ata_std_ports);
Jeff Garzikcca39742006-08-24 03:19:22 -04006271EXPORT_SYMBOL_GPL(ata_host_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272EXPORT_SYMBOL_GPL(ata_device_add);
Tejun Heo720ba122006-05-31 18:28:13 +09006273EXPORT_SYMBOL_GPL(ata_port_detach);
Jeff Garzikcca39742006-08-24 03:19:22 -04006274EXPORT_SYMBOL_GPL(ata_host_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275EXPORT_SYMBOL_GPL(ata_sg_init);
6276EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo9a1004d2006-05-31 18:27:52 +09006277EXPORT_SYMBOL_GPL(ata_hsm_move);
Tejun Heof686bcb2006-05-15 20:58:05 +09006278EXPORT_SYMBOL_GPL(ata_qc_complete);
Tejun Heodedaf2b2006-05-15 21:03:43 +09006279EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281EXPORT_SYMBOL_GPL(ata_tf_load);
6282EXPORT_SYMBOL_GPL(ata_tf_read);
6283EXPORT_SYMBOL_GPL(ata_noop_dev_select);
6284EXPORT_SYMBOL_GPL(ata_std_dev_select);
6285EXPORT_SYMBOL_GPL(ata_tf_to_fis);
6286EXPORT_SYMBOL_GPL(ata_tf_from_fis);
6287EXPORT_SYMBOL_GPL(ata_check_status);
6288EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289EXPORT_SYMBOL_GPL(ata_exec_command);
6290EXPORT_SYMBOL_GPL(ata_port_start);
6291EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04006292EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293EXPORT_SYMBOL_GPL(ata_interrupt);
Alan Coxa6b2c5d2006-05-22 16:59:59 +01006294EXPORT_SYMBOL_GPL(ata_mmio_data_xfer);
6295EXPORT_SYMBOL_GPL(ata_pio_data_xfer);
Alan Cox75e99582006-05-24 14:14:41 +01006296EXPORT_SYMBOL_GPL(ata_pio_data_xfer_noirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297EXPORT_SYMBOL_GPL(ata_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06006298EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299EXPORT_SYMBOL_GPL(ata_bmdma_setup);
6300EXPORT_SYMBOL_GPL(ata_bmdma_start);
6301EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
6302EXPORT_SYMBOL_GPL(ata_bmdma_status);
6303EXPORT_SYMBOL_GPL(ata_bmdma_stop);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09006304EXPORT_SYMBOL_GPL(ata_bmdma_freeze);
6305EXPORT_SYMBOL_GPL(ata_bmdma_thaw);
6306EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh);
6307EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
6308EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309EXPORT_SYMBOL_GPL(ata_port_probe);
Tejun Heo3c567b72006-05-15 20:57:23 +09006310EXPORT_SYMBOL_GPL(sata_set_spd);
Tejun Heod7bb4cc2006-05-31 18:27:46 +09006311EXPORT_SYMBOL_GPL(sata_phy_debounce);
6312EXPORT_SYMBOL_GPL(sata_phy_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313EXPORT_SYMBOL_GPL(sata_phy_reset);
6314EXPORT_SYMBOL_GPL(__sata_phy_reset);
6315EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heof5914a42006-05-31 18:27:48 +09006316EXPORT_SYMBOL_GPL(ata_std_prereset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09006317EXPORT_SYMBOL_GPL(ata_std_softreset);
Tejun Heob6103f62006-11-01 17:59:53 +09006318EXPORT_SYMBOL_GPL(sata_port_hardreset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09006319EXPORT_SYMBOL_GPL(sata_std_hardreset);
6320EXPORT_SYMBOL_GPL(ata_std_postreset);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05006321EXPORT_SYMBOL_GPL(ata_dev_classify);
6322EXPORT_SYMBOL_GPL(ata_dev_pair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04006324EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heoc22daff2006-04-11 22:22:29 +09006325EXPORT_SYMBOL_GPL(ata_wait_register);
Tejun Heo6f8b9952006-01-24 17:05:21 +09006326EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heo86e45b62006-03-05 15:29:09 +09006327EXPORT_SYMBOL_GPL(ata_port_queue_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
6329EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
Tejun Heo83c47bc2006-05-31 18:28:07 +09006331EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09006332EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333EXPORT_SYMBOL_GPL(ata_scsi_release);
6334EXPORT_SYMBOL_GPL(ata_host_intr);
Tejun Heo34bf2172006-05-15 20:57:46 +09006335EXPORT_SYMBOL_GPL(sata_scr_valid);
6336EXPORT_SYMBOL_GPL(sata_scr_read);
6337EXPORT_SYMBOL_GPL(sata_scr_write);
6338EXPORT_SYMBOL_GPL(sata_scr_write_flush);
6339EXPORT_SYMBOL_GPL(ata_port_online);
6340EXPORT_SYMBOL_GPL(ata_port_offline);
Jeff Garzikcca39742006-08-24 03:19:22 -04006341EXPORT_SYMBOL_GPL(ata_host_suspend);
6342EXPORT_SYMBOL_GPL(ata_host_resume);
Tejun Heo6a62a042006-02-13 10:02:46 +09006343EXPORT_SYMBOL_GPL(ata_id_string);
6344EXPORT_SYMBOL_GPL(ata_id_c_string);
Alan Cox6919a0a2006-10-27 19:08:46 -07006345EXPORT_SYMBOL_GPL(ata_device_blacklisted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346EXPORT_SYMBOL_GPL(ata_scsi_simulate);
6347
Alan Cox1bc4ccf2006-01-09 17:18:14 +00006348EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04006349EXPORT_SYMBOL_GPL(ata_timing_compute);
6350EXPORT_SYMBOL_GPL(ata_timing_merge);
6351
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352#ifdef CONFIG_PCI
6353EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04006354EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
6356EXPORT_SYMBOL_GPL(ata_pci_init_one);
6357EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Tejun Heo500530f2006-07-03 16:07:27 +09006358EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6359EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
Jens Axboe9b847542006-01-06 09:28:07 +01006360EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6361EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Alan Cox67951ad2006-03-22 15:55:54 +00006362EXPORT_SYMBOL_GPL(ata_pci_default_filter);
6363EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01006365
Jens Axboe9b847542006-01-06 09:28:07 +01006366EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
6367EXPORT_SYMBOL_GPL(ata_scsi_device_resume);
Tejun Heoece1d632006-04-02 18:51:53 +09006368
Tejun Heoece1d632006-04-02 18:51:53 +09006369EXPORT_SYMBOL_GPL(ata_eng_timeout);
Tejun Heo7b70fc02006-05-15 20:58:07 +09006370EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
6371EXPORT_SYMBOL_GPL(ata_port_abort);
Tejun Heoe3180492006-05-15 20:58:09 +09006372EXPORT_SYMBOL_GPL(ata_port_freeze);
6373EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
6374EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
Tejun Heoece1d632006-04-02 18:51:53 +09006375EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
6376EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Tejun Heo022bdb02006-05-15 20:58:22 +09006377EXPORT_SYMBOL_GPL(ata_do_eh);