libata-core.c 118 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 *  libata-core.c - helper library for ATA
 *
 *  Maintained by:  Jeff Garzik <jgarzik@pobox.com>
 *    		    Please ALWAYS copy linux-ide@vger.kernel.org
 *		    on emails.
 *
 *  Copyright 2003-2004 Red Hat, Inc.  All rights reserved.
 *  Copyright 2003-2004 Jeff Garzik
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 *  libata documentation is available via 'make {ps|pdf}docs',
 *  as Documentation/DocBook/libata.*
 *
 *  Hardware documentation available from http://www.t13.org/ and
 *  http://www.sata-io.org/
 *
Linus Torvalds's avatar
Linus Torvalds committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 */

#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/mm.h>
#include <linux/highmem.h>
#include <linux/spinlock.h>
#include <linux/blkdev.h>
#include <linux/delay.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#include <linux/completion.h>
#include <linux/suspend.h>
#include <linux/workqueue.h>
51
#include <linux/jiffies.h>
52
#include <linux/scatterlist.h>
Linus Torvalds's avatar
Linus Torvalds committed
53
54
#include <scsi/scsi.h>
#include "scsi_priv.h"
55
#include <scsi/scsi_cmnd.h>
Linus Torvalds's avatar
Linus Torvalds committed
56
57
58
59
60
61
62
63
#include <scsi/scsi_host.h>
#include <linux/libata.h>
#include <asm/io.h>
#include <asm/semaphore.h>
#include <asm/byteorder.h>

#include "libata.h"

64
65
static unsigned int ata_dev_init_params(struct ata_port *ap,
					struct ata_device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
66
static void ata_set_mode(struct ata_port *ap);
67
68
static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
					 struct ata_device *dev);
69
static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
70
71
72
73

static unsigned int ata_unique_id = 1;
static struct workqueue_struct *ata_wq;

Jeff Garzik's avatar
Jeff Garzik committed
74
int atapi_enabled = 1;
75
76
77
module_param(atapi_enabled, int, 0444);
MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");

Jeff Garzik's avatar
Jeff Garzik committed
78
79
80
81
int libata_fua = 0;
module_param_named(fua, libata_fua, int, 0444);
MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");

Linus Torvalds's avatar
Linus Torvalds committed
82
83
84
85
86
MODULE_AUTHOR("Jeff Garzik");
MODULE_DESCRIPTION("Library module for ATA devices");
MODULE_LICENSE("GPL");
MODULE_VERSION(DRV_VERSION);

87

Linus Torvalds's avatar
Linus Torvalds committed
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
 *	ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
 *	@tf: Taskfile to convert
 *	@fis: Buffer into which data will output
 *	@pmp: Port multiplier port
 *
 *	Converts a standard ATA taskfile to a Serial ATA
 *	FIS structure (Register - Host to Device).
 *
 *	LOCKING:
 *	Inherited from caller.
 */

101
void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds's avatar
Linus Torvalds committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
{
	fis[0] = 0x27;	/* Register - Host to Device FIS */
	fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
					    bit 7 indicates Command FIS */
	fis[2] = tf->command;
	fis[3] = tf->feature;

	fis[4] = tf->lbal;
	fis[5] = tf->lbam;
	fis[6] = tf->lbah;
	fis[7] = tf->device;

	fis[8] = tf->hob_lbal;
	fis[9] = tf->hob_lbam;
	fis[10] = tf->hob_lbah;
	fis[11] = tf->hob_feature;

	fis[12] = tf->nsect;
	fis[13] = tf->hob_nsect;
	fis[14] = 0;
	fis[15] = tf->ctl;

	fis[16] = 0;
	fis[17] = 0;
	fis[18] = 0;
	fis[19] = 0;
}

/**
 *	ata_tf_from_fis - Convert SATA FIS to ATA taskfile
 *	@fis: Buffer from which data will be input
 *	@tf: Taskfile to output
 *
135
 *	Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds's avatar
Linus Torvalds committed
136
137
138
139
140
 *
 *	LOCKING:
 *	Inherited from caller.
 */

141
void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds's avatar
Linus Torvalds committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
{
	tf->command	= fis[2];	/* status */
	tf->feature	= fis[3];	/* error */

	tf->lbal	= fis[4];
	tf->lbam	= fis[5];
	tf->lbah	= fis[6];
	tf->device	= fis[7];

	tf->hob_lbal	= fis[8];
	tf->hob_lbam	= fis[9];
	tf->hob_lbah	= fis[10];

	tf->nsect	= fis[12];
	tf->hob_nsect	= fis[13];
}

159
160
161
162
163
164
static const u8 ata_rw_cmds[] = {
	/* pio multi */
	ATA_CMD_READ_MULTI,
	ATA_CMD_WRITE_MULTI,
	ATA_CMD_READ_MULTI_EXT,
	ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo's avatar
Tejun Heo committed
165
166
167
168
	0,
	0,
	0,
	ATA_CMD_WRITE_MULTI_FUA_EXT,
169
170
171
172
173
	/* pio */
	ATA_CMD_PIO_READ,
	ATA_CMD_PIO_WRITE,
	ATA_CMD_PIO_READ_EXT,
	ATA_CMD_PIO_WRITE_EXT,
Tejun Heo's avatar
Tejun Heo committed
174
175
176
177
	0,
	0,
	0,
	0,
178
179
180
181
	/* dma */
	ATA_CMD_READ,
	ATA_CMD_WRITE,
	ATA_CMD_READ_EXT,
Tejun Heo's avatar
Tejun Heo committed
182
183
184
185
186
	ATA_CMD_WRITE_EXT,
	0,
	0,
	0,
	ATA_CMD_WRITE_FUA_EXT
187
};
Linus Torvalds's avatar
Linus Torvalds committed
188
189

/**
190
191
 *	ata_rwcmd_protocol - set taskfile r/w commands and protocol
 *	@qc: command to examine and configure
Linus Torvalds's avatar
Linus Torvalds committed
192
 *
193
 *	Examine the device configuration and tf->flags to calculate
194
 *	the proper read/write commands and protocol to use.
Linus Torvalds's avatar
Linus Torvalds committed
195
196
197
198
 *
 *	LOCKING:
 *	caller.
 */
Tejun Heo's avatar
Tejun Heo committed
199
int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds's avatar
Linus Torvalds committed
200
{
201
202
	struct ata_taskfile *tf = &qc->tf;
	struct ata_device *dev = qc->dev;
Tejun Heo's avatar
Tejun Heo committed
203
	u8 cmd;
Linus Torvalds's avatar
Linus Torvalds committed
204

Tejun Heo's avatar
Tejun Heo committed
205
	int index, fua, lba48, write;
206

Tejun Heo's avatar
Tejun Heo committed
207
	fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
208
209
	lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
	write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds's avatar
Linus Torvalds committed
210

211
212
	if (dev->flags & ATA_DFLAG_PIO) {
		tf->protocol = ATA_PROT_PIO;
Tejun Heo's avatar
Tejun Heo committed
213
		index = dev->multi_count ? 0 : 8;
214
215
216
	} else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
		/* Unable to use DMA due to host limitation */
		tf->protocol = ATA_PROT_PIO;
217
		index = dev->multi_count ? 0 : 8;
218
219
	} else {
		tf->protocol = ATA_PROT_DMA;
Tejun Heo's avatar
Tejun Heo committed
220
		index = 16;
221
	}
Linus Torvalds's avatar
Linus Torvalds committed
222

Tejun Heo's avatar
Tejun Heo committed
223
224
225
226
227
228
	cmd = ata_rw_cmds[index + fua + lba48 + write];
	if (cmd) {
		tf->command = cmd;
		return 0;
	}
	return -1;
Linus Torvalds's avatar
Linus Torvalds committed
229
230
}

231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
 *	ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
 *	@pio_mask: pio_mask
 *	@mwdma_mask: mwdma_mask
 *	@udma_mask: udma_mask
 *
 *	Pack @pio_mask, @mwdma_mask and @udma_mask into a single
 *	unsigned int xfer_mask.
 *
 *	LOCKING:
 *	None.
 *
 *	RETURNS:
 *	Packed xfer_mask.
 */
static unsigned int ata_pack_xfermask(unsigned int pio_mask,
				      unsigned int mwdma_mask,
				      unsigned int udma_mask)
{
	return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
		((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
		((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
}

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
 *	ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
 *	@xfer_mask: xfer_mask to unpack
 *	@pio_mask: resulting pio_mask
 *	@mwdma_mask: resulting mwdma_mask
 *	@udma_mask: resulting udma_mask
 *
 *	Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
 *	Any NULL distination masks will be ignored.
 */
static void ata_unpack_xfermask(unsigned int xfer_mask,
				unsigned int *pio_mask,
				unsigned int *mwdma_mask,
				unsigned int *udma_mask)
{
	if (pio_mask)
		*pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
	if (mwdma_mask)
		*mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
	if (udma_mask)
		*udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
}

278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
static const struct ata_xfer_ent {
	unsigned int shift, bits;
	u8 base;
} ata_xfer_tbl[] = {
	{ ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
	{ ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
	{ ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
	{ -1, },
};

/**
 *	ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
 *	@xfer_mask: xfer_mask of interest
 *
 *	Return matching XFER_* value for @xfer_mask.  Only the highest
 *	bit of @xfer_mask is considered.
 *
 *	LOCKING:
 *	None.
 *
 *	RETURNS:
 *	Matching XFER_* value, 0 if no match found.
 */
static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
{
	int highbit = fls(xfer_mask) - 1;
	const struct ata_xfer_ent *ent;

	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
		if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
			return ent->base + highbit - ent->shift;
	return 0;
}

/**
 *	ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
 *	@xfer_mode: XFER_* of interest
 *
 *	Return matching xfer_mask for @xfer_mode.
 *
 *	LOCKING:
 *	None.
 *
 *	RETURNS:
 *	Matching xfer_mask, 0 if no match found.
 */
static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
{
	const struct ata_xfer_ent *ent;

	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
			return 1 << (ent->shift + xfer_mode - ent->base);
	return 0;
}

/**
 *	ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
 *	@xfer_mode: XFER_* of interest
 *
 *	Return matching xfer_shift for @xfer_mode.
 *
 *	LOCKING:
 *	None.
 *
 *	RETURNS:
 *	Matching xfer_shift, -1 if no match found.
 */
static int ata_xfer_mode2shift(unsigned int xfer_mode)
{
	const struct ata_xfer_ent *ent;

	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
			return ent->shift;
	return -1;
}

Linus Torvalds's avatar
Linus Torvalds committed
356
/**
357
358
 *	ata_mode_string - convert xfer_mask to string
 *	@xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds's avatar
Linus Torvalds committed
359
360
 *
 *	Determine string which represents the highest speed
361
 *	(highest bit in @modemask).
Linus Torvalds's avatar
Linus Torvalds committed
362
363
364
365
366
367
 *
 *	LOCKING:
 *	None.
 *
 *	RETURNS:
 *	Constant C string representing highest speed listed in
368
 *	@mode_mask, or the constant C string "<n/a>".
Linus Torvalds's avatar
Linus Torvalds committed
369
 */
370
static const char *ata_mode_string(unsigned int xfer_mask)
Linus Torvalds's avatar
Linus Torvalds committed
371
{
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
	static const char * const xfer_mode_str[] = {
		"PIO0",
		"PIO1",
		"PIO2",
		"PIO3",
		"PIO4",
		"MWDMA0",
		"MWDMA1",
		"MWDMA2",
		"UDMA/16",
		"UDMA/25",
		"UDMA/33",
		"UDMA/44",
		"UDMA/66",
		"UDMA/100",
		"UDMA/133",
		"UDMA7",
	};
390
	int highbit;
Linus Torvalds's avatar
Linus Torvalds committed
391

392
393
394
	highbit = fls(xfer_mask) - 1;
	if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
		return xfer_mode_str[highbit];
Linus Torvalds's avatar
Linus Torvalds committed
395
396
397
	return "<n/a>";
}

398
399
400
401
402
403
404
405
406
static void ata_dev_disable(struct ata_port *ap, struct ata_device *dev)
{
	if (ata_dev_present(dev)) {
		printk(KERN_WARNING "ata%u: dev %u disabled\n",
		       ap->id, dev->devno);
		dev->class++;
	}
}

Linus Torvalds's avatar
Linus Torvalds committed
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/**
 *	ata_pio_devchk - PATA device presence detection
 *	@ap: ATA channel to examine
 *	@device: Device to examine (starting at zero)
 *
 *	This technique was originally described in
 *	Hale Landis's ATADRVR (www.ata-atapi.com), and
 *	later found its way into the ATA/ATAPI spec.
 *
 *	Write a pattern to the ATA shadow registers,
 *	and if a device is present, it will respond by
 *	correctly storing and echoing back the
 *	ATA shadow register contents.
 *
 *	LOCKING:
 *	caller.
 */

static unsigned int ata_pio_devchk(struct ata_port *ap,
				   unsigned int device)
{
	struct ata_ioports *ioaddr = &ap->ioaddr;
	u8 nsect, lbal;

	ap->ops->dev_select(ap, device);

	outb(0x55, ioaddr->nsect_addr);
	outb(0xaa, ioaddr->lbal_addr);

	outb(0xaa, ioaddr->nsect_addr);
	outb(0x55, ioaddr->lbal_addr);

	outb(0x55, ioaddr->nsect_addr);
	outb(0xaa, ioaddr->lbal_addr);

	nsect = inb(ioaddr->nsect_addr);
	lbal = inb(ioaddr->lbal_addr);

	if ((nsect == 0x55) && (lbal == 0xaa))
		return 1;	/* we found a device */

	return 0;		/* nothing found */
}

/**
 *	ata_mmio_devchk - PATA device presence detection
 *	@ap: ATA channel to examine
 *	@device: Device to examine (starting at zero)
 *
 *	This technique was originally described in
 *	Hale Landis's ATADRVR (www.ata-atapi.com), and
 *	later found its way into the ATA/ATAPI spec.
 *
 *	Write a pattern to the ATA shadow registers,
 *	and if a device is present, it will respond by
 *	correctly storing and echoing back the
 *	ATA shadow register contents.
 *
 *	LOCKING:
 *	caller.
 */

static unsigned int ata_mmio_devchk(struct ata_port *ap,
				    unsigned int device)
{
	struct ata_ioports *ioaddr = &ap->ioaddr;
	u8 nsect, lbal;

	ap->ops->dev_select(ap, device);

	writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
	writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);

	writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
	writeb(0x55, (void __iomem *) ioaddr->lbal_addr);

	writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
	writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);

	nsect = readb((void __iomem *) ioaddr->nsect_addr);
	lbal = readb((void __iomem *) ioaddr->lbal_addr);

	if ((nsect == 0x55) && (lbal == 0xaa))
		return 1;	/* we found a device */

	return 0;		/* nothing found */
}

/**
 *	ata_devchk - PATA device presence detection
 *	@ap: ATA channel to examine
 *	@device: Device to examine (starting at zero)
 *
 *	Dispatch ATA device presence detection, depending
 *	on whether we are using PIO or MMIO to talk to the
 *	ATA shadow registers.
 *
 *	LOCKING:
 *	caller.
 */

static unsigned int ata_devchk(struct ata_port *ap,
				    unsigned int device)
{
	if (ap->flags & ATA_FLAG_MMIO)
		return ata_mmio_devchk(ap, device);
	return ata_pio_devchk(ap, device);
}

/**
 *	ata_dev_classify - determine device type based on ATA-spec signature
 *	@tf: ATA taskfile register set for device to be identified
 *
 *	Determine from taskfile register contents whether a device is
 *	ATA or ATAPI, as per "Signature and persistence" section
 *	of ATA/PI spec (volume 1, sect 5.14).
 *
 *	LOCKING:
 *	None.
 *
 *	RETURNS:
 *	Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
 *	the event of failure.
 */

532
unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds's avatar
Linus Torvalds committed
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
{
	/* Apple's open source Darwin code hints that some devices only
	 * put a proper signature into the LBA mid/high registers,
	 * So, we only check those.  It's sufficient for uniqueness.
	 */

	if (((tf->lbam == 0) && (tf->lbah == 0)) ||
	    ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
		DPRINTK("found ATA device by sig\n");
		return ATA_DEV_ATA;
	}

	if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
	    ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
		DPRINTK("found ATAPI device by sig\n");
		return ATA_DEV_ATAPI;
	}

	DPRINTK("unknown device\n");
	return ATA_DEV_UNKNOWN;
}

/**
 *	ata_dev_try_classify - Parse returned ATA device signature
 *	@ap: ATA channel to examine
 *	@device: Device to examine (starting at zero)
559
 *	@r_err: Value of error register on completion
Linus Torvalds's avatar
Linus Torvalds committed
560
561
562
563
564
565
566
567
568
569
570
571
 *
 *	After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
 *	an ATA/ATAPI-defined set of values is placed in the ATA
 *	shadow registers, indicating the results of device detection
 *	and diagnostics.
 *
 *	Select the ATA device, and read the values from the ATA shadow
 *	registers.  Then parse according to the Error register value,
 *	and the spec-defined values examined by ata_dev_classify().
 *
 *	LOCKING:
 *	caller.
572
573
574
 *
 *	RETURNS:
 *	Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds's avatar
Linus Torvalds committed
575
576
 */

577
578
static unsigned int
ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds's avatar
Linus Torvalds committed
579
580
581
582
583
584
585
586
587
588
{
	struct ata_taskfile tf;
	unsigned int class;
	u8 err;

	ap->ops->dev_select(ap, device);

	memset(&tf, 0, sizeof(tf));

	ap->ops->tf_read(ap, &tf);
589
	err = tf.feature;
590
591
	if (r_err)
		*r_err = err;
Linus Torvalds's avatar
Linus Torvalds committed
592
593
594
595
596
597
598

	/* see if device passed diags */
	if (err == 1)
		/* do nothing */ ;
	else if ((device == 0) && (err == 0x81))
		/* do nothing */ ;
	else
599
		return ATA_DEV_NONE;
Linus Torvalds's avatar
Linus Torvalds committed
600

601
	/* determine if device is ATA or ATAPI */
Linus Torvalds's avatar
Linus Torvalds committed
602
	class = ata_dev_classify(&tf);
603

Linus Torvalds's avatar
Linus Torvalds committed
604
	if (class == ATA_DEV_UNKNOWN)
605
		return ATA_DEV_NONE;
Linus Torvalds's avatar
Linus Torvalds committed
606
	if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
607
608
		return ATA_DEV_NONE;
	return class;
Linus Torvalds's avatar
Linus Torvalds committed
609
610
611
}

/**
612
 *	ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds's avatar
Linus Torvalds committed
613
614
615
616
617
618
619
620
621
622
623
624
625
 *	@id: IDENTIFY DEVICE results we will examine
 *	@s: string into which data is output
 *	@ofs: offset into identify device page
 *	@len: length of string to return. must be an even number.
 *
 *	The strings in the IDENTIFY DEVICE page are broken up into
 *	16-bit chunks.  Run through the string, and output each
 *	8-bit chunk linearly, regardless of platform.
 *
 *	LOCKING:
 *	caller.
 */

626
627
void ata_id_string(const u16 *id, unsigned char *s,
		   unsigned int ofs, unsigned int len)
Linus Torvalds's avatar
Linus Torvalds committed
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
{
	unsigned int c;

	while (len > 0) {
		c = id[ofs] >> 8;
		*s = c;
		s++;

		c = id[ofs] & 0xff;
		*s = c;
		s++;

		ofs++;
		len -= 2;
	}
}

645
/**
646
 *	ata_id_c_string - Convert IDENTIFY DEVICE page into C string
647
648
649
650
651
 *	@id: IDENTIFY DEVICE results we will examine
 *	@s: string into which data is output
 *	@ofs: offset into identify device page
 *	@len: length of string to return. must be an odd number.
 *
652
 *	This function is identical to ata_id_string except that it
653
654
655
656
657
658
 *	trims trailing spaces and terminates the resulting string with
 *	null.  @len must be actual maximum length (even number) + 1.
 *
 *	LOCKING:
 *	caller.
 */
659
660
void ata_id_c_string(const u16 *id, unsigned char *s,
		     unsigned int ofs, unsigned int len)
661
662
663
664
665
{
	unsigned char *p;

	WARN_ON(!(len & 1));

666
	ata_id_string(id, s, ofs, len - 1);
667
668
669
670
671
672

	p = s + strnlen(s, len - 1);
	while (p > s && p[-1] == ' ')
		p--;
	*p = '\0';
}
673

674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
static u64 ata_id_n_sectors(const u16 *id)
{
	if (ata_id_has_lba(id)) {
		if (ata_id_has_lba48(id))
			return ata_id_u64(id, 100);
		else
			return ata_id_u32(id, 60);
	} else {
		if (ata_id_current_chs_valid(id))
			return ata_id_u32(id, 57);
		else
			return id[1] * id[3] * id[6];
	}
}

689
690
691
692
693
694
695
696
697
698
699
700
/**
 *	ata_noop_dev_select - Select device 0/1 on ATA bus
 *	@ap: ATA channel to manipulate
 *	@device: ATA device (numbered from zero) to select
 *
 *	This function performs no actual function.
 *
 *	May be used as the dev_select() entry in ata_port_operations.
 *
 *	LOCKING:
 *	caller.
 */
Linus Torvalds's avatar
Linus Torvalds committed
701
702
703
704
void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
{
}

705

Linus Torvalds's avatar
Linus Torvalds committed
706
707
708
709
710
711
712
/**
 *	ata_std_dev_select - Select device 0/1 on ATA bus
 *	@ap: ATA channel to manipulate
 *	@device: ATA device (numbered from zero) to select
 *
 *	Use the method defined in the ATA specification to
 *	make either device 0, or device 1, active on the
713
714
715
 *	ATA channel.  Works with both PIO and MMIO.
 *
 *	May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds's avatar
Linus Torvalds committed
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
 *
 *	LOCKING:
 *	caller.
 */

void ata_std_dev_select (struct ata_port *ap, unsigned int device)
{
	u8 tmp;

	if (device == 0)
		tmp = ATA_DEVICE_OBS;
	else
		tmp = ATA_DEVICE_OBS | ATA_DEV1;

	if (ap->flags & ATA_FLAG_MMIO) {
		writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
	} else {
		outb(tmp, ap->ioaddr.device_addr);
	}
	ata_pause(ap);		/* needed; also flushes, for mmio */
}

/**
 *	ata_dev_select - Select device 0/1 on ATA bus
 *	@ap: ATA channel to manipulate
 *	@device: ATA device (numbered from zero) to select
 *	@wait: non-zero to wait for Status register BSY bit to clear
 *	@can_sleep: non-zero if context allows sleeping
 *
 *	Use the method defined in the ATA specification to
 *	make either device 0, or device 1, active on the
 *	ATA channel.
 *
 *	This is a high-level version of ata_std_dev_select(),
 *	which additionally provides the services of inserting
 *	the proper pauses and status polling, where needed.
 *
 *	LOCKING:
 *	caller.
 */

void ata_dev_select(struct ata_port *ap, unsigned int device,
			   unsigned int wait, unsigned int can_sleep)
{
	VPRINTK("ENTER, ata%u: device %u, wait %u\n",
		ap->id, device, wait);

	if (wait)
		ata_wait_idle(ap);

	ap->ops->dev_select(ap, device);

	if (wait) {
		if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
			msleep(150);
		ata_wait_idle(ap);
	}
}

/**
 *	ata_dump_id - IDENTIFY DEVICE info debugging output
777
 *	@id: IDENTIFY DEVICE page to dump
Linus Torvalds's avatar
Linus Torvalds committed
778
 *
779
780
 *	Dump selected 16-bit words from the given IDENTIFY DEVICE
 *	page.
Linus Torvalds's avatar
Linus Torvalds committed
781
782
783
784
785
 *
 *	LOCKING:
 *	caller.
 */

786
static inline void ata_dump_id(const u16 *id)
Linus Torvalds's avatar
Linus Torvalds committed
787
788
789
790
791
792
{
	DPRINTK("49==0x%04x  "
		"53==0x%04x  "
		"63==0x%04x  "
		"64==0x%04x  "
		"75==0x%04x  \n",
793
794
795
796
797
		id[49],
		id[53],
		id[63],
		id[64],
		id[75]);
Linus Torvalds's avatar
Linus Torvalds committed
798
799
800
801
802
	DPRINTK("80==0x%04x  "
		"81==0x%04x  "
		"82==0x%04x  "
		"83==0x%04x  "
		"84==0x%04x  \n",
803
804
805
806
807
		id[80],
		id[81],
		id[82],
		id[83],
		id[84]);
Linus Torvalds's avatar
Linus Torvalds committed
808
809
	DPRINTK("88==0x%04x  "
		"93==0x%04x\n",
810
811
		id[88],
		id[93]);
Linus Torvalds's avatar
Linus Torvalds committed
812
813
}

814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
/**
 *	ata_id_xfermask - Compute xfermask from the given IDENTIFY data
 *	@id: IDENTIFY data to compute xfer mask from
 *
 *	Compute the xfermask for this device. This is not as trivial
 *	as it seems if we must consider early devices correctly.
 *
 *	FIXME: pre IDE drive timing (do we care ?).
 *
 *	LOCKING:
 *	None.
 *
 *	RETURNS:
 *	Computed xfermask
 */
static unsigned int ata_id_xfermask(const u16 *id)
{
	unsigned int pio_mask, mwdma_mask, udma_mask;

	/* Usual case. Word 53 indicates word 64 is valid */
	if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
		pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
		pio_mask <<= 3;
		pio_mask |= 0x7;
	} else {
		/* If word 64 isn't valid then Word 51 high byte holds
		 * the PIO timing number for the maximum. Turn it into
		 * a mask.
		 */
		pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;

		/* But wait.. there's more. Design your standards by
		 * committee and you too can get a free iordy field to
		 * process. However its the speeds not the modes that
		 * are supported... Note drivers using the timing API
		 * will get this right anyway
		 */
	}

	mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
854
855
856
857

	udma_mask = 0;
	if (id[ATA_ID_FIELD_VALID] & (1 << 2))
		udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
858
859
860
861

	return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
}

862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
/**
 *	ata_port_queue_task - Queue port_task
 *	@ap: The ata_port to queue port_task for
 *
 *	Schedule @fn(@data) for execution after @delay jiffies using
 *	port_task.  There is one port_task per port and it's the
 *	user(low level driver)'s responsibility to make sure that only
 *	one task is active at any given time.
 *
 *	libata core layer takes care of synchronization between
 *	port_task and EH.  ata_port_queue_task() may be ignored for EH
 *	synchronization.
 *
 *	LOCKING:
 *	Inherited from caller.
 */
void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
			 unsigned long delay)
{
	int rc;

883
	if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
		return;

	PREPARE_WORK(&ap->port_task, fn, data);

	if (!delay)
		rc = queue_work(ata_wq, &ap->port_task);
	else
		rc = queue_delayed_work(ata_wq, &ap->port_task, delay);

	/* rc == 0 means that another user is using port task */
	WARN_ON(rc == 0);
}

/**
 *	ata_port_flush_task - Flush port_task
 *	@ap: The ata_port to flush port_task for
 *
 *	After this function completes, port_task is guranteed not to
 *	be running or scheduled.
 *
 *	LOCKING:
 *	Kernel thread context (may sleep)
 */
void ata_port_flush_task(struct ata_port *ap)
{
	unsigned long flags;

	DPRINTK("ENTER\n");

	spin_lock_irqsave(&ap->host_set->lock, flags);
914
	ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
	spin_unlock_irqrestore(&ap->host_set->lock, flags);

	DPRINTK("flush #1\n");
	flush_workqueue(ata_wq);

	/*
	 * At this point, if a task is running, it's guaranteed to see
	 * the FLUSH flag; thus, it will never queue pio tasks again.
	 * Cancel and flush.
	 */
	if (!cancel_delayed_work(&ap->port_task)) {
		DPRINTK("flush #2\n");
		flush_workqueue(ata_wq);
	}

	spin_lock_irqsave(&ap->host_set->lock, flags);
931
	ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
932
933
934
935
936
	spin_unlock_irqrestore(&ap->host_set->lock, flags);

	DPRINTK("EXIT\n");
}

937
void ata_qc_complete_internal(struct ata_queued_cmd *qc)
938
{
939
	struct completion *waiting = qc->private_data;
940

941
	qc->ap->ops->tf_read(qc->ap, &qc->tf);
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
	complete(waiting);
}

/**
 *	ata_exec_internal - execute libata internal command
 *	@ap: Port to which the command is sent
 *	@dev: Device to which the command is sent
 *	@tf: Taskfile registers for the command and the result
 *	@dma_dir: Data tranfer direction of the command
 *	@buf: Data buffer of the command
 *	@buflen: Length of data buffer
 *
 *	Executes libata internal command with timeout.  @tf contains
 *	command on entry and result on return.  Timeout and error
 *	conditions are reported via return value.  No recovery action
 *	is taken after a command times out.  It's caller's duty to
 *	clean up after timeout.
 *
 *	LOCKING:
 *	None.  Should be called with kernel context, might sleep.
 */

static unsigned
ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
		  struct ata_taskfile *tf,
		  int dma_dir, void *buf, unsigned int buflen)
{
	u8 command = tf->command;
	struct ata_queued_cmd *qc;
	DECLARE_COMPLETION(wait);
	unsigned long flags;
973
	unsigned int err_mask;
974
975
976
977
978
979
980
981
982
983
984
985
986

	spin_lock_irqsave(&ap->host_set->lock, flags);

	qc = ata_qc_new_init(ap, dev);
	BUG_ON(qc == NULL);

	qc->tf = *tf;
	qc->dma_dir = dma_dir;
	if (dma_dir != DMA_NONE) {
		ata_sg_init_one(qc, buf, buflen);
		qc->nsect = buflen / ATA_SECT_SIZE;
	}

987
	qc->private_data = &wait;
988
989
	qc->complete_fn = ata_qc_complete_internal;

990
991
	qc->err_mask = ata_qc_issue(qc);
	if (qc->err_mask)
992
		ata_qc_complete(qc);
993
994
995
996

	spin_unlock_irqrestore(&ap->host_set->lock, flags);

	if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
997
998
		ata_port_flush_task(ap);

999
1000
1001
1002
1003
1004
1005
1006
		spin_lock_irqsave(&ap->host_set->lock, flags);

		/* We're racing with irq here.  If we lose, the
		 * following test prevents us from completing the qc
		 * again.  If completion irq occurs after here but
		 * before the caller cleans up, it will result in a
		 * spurious interrupt.  We can live with that.
		 */
1007
		if (qc->flags & ATA_QCFLAG_ACTIVE) {
1008
			qc->err_mask = AC_ERR_TIMEOUT;
1009
1010
1011
1012
1013
1014
1015
1016
			ata_qc_complete(qc);
			printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
			       ap->id, command);
		}

		spin_unlock_irqrestore(&ap->host_set->lock, flags);
	}

1017
1018
1019
1020
1021
	*tf = qc->tf;
	err_mask = qc->err_mask;

	ata_qc_free(qc);

1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
	/* XXX - Some LLDDs (sata_mv) disable port on command failure.
	 * Until those drivers are fixed, we detect the condition
	 * here, fail the command with AC_ERR_SYSTEM and reenable the
	 * port.
	 *
	 * Note that this doesn't change any behavior as internal
	 * command failure results in disabling the device in the
	 * higher layer for LLDDs without new reset/EH callbacks.
	 *
	 * Kill the following code as soon as those drivers are fixed.
	 */
	if (ap->flags & ATA_FLAG_PORT_DISABLED) {
		err_mask |= AC_ERR_SYSTEM;
		ata_port_probe(ap);
	}

1038
	return err_mask;
1039
1040
}

1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
/**
 *	ata_pio_need_iordy	-	check if iordy needed
 *	@adev: ATA device
 *
 *	Check if the current speed of the device requires IORDY. Used
 *	by various controllers for chip configuration.
 */

unsigned int ata_pio_need_iordy(const struct ata_device *adev)
{
	int pio;
	int speed = adev->pio_mode - XFER_PIO_0;

	if (speed < 2)
		return 0;
	if (speed > 2)
		return 1;
1058

1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
	/* If we have no drive specific rule, then PIO 2 is non IORDY */

	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE */
		pio = adev->id[ATA_ID_EIDE_PIO];
		/* Is the speed faster than the drive allows non IORDY ? */
		if (pio) {
			/* This is cycle times not frequency - watch the logic! */
			if (pio > 240)	/* PIO2 is 240nS per cycle */
				return 1;
			return 0;
		}
	}
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
1074
/**
1075
1076
1077
1078
1079
 *	ata_dev_read_id - Read ID data from the specified device
 *	@ap: port on which target device resides
 *	@dev: target device
 *	@p_class: pointer to class of the target device (may be changed)
 *	@post_reset: is this read ID post-reset?
1080
 *	@p_id: read IDENTIFY page (newly allocated)
Linus Torvalds's avatar
Linus Torvalds committed
1081
 *
1082
1083
 *	Read ID data from the specified device.  ATA_CMD_ID_ATA is
 *	performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heo's avatar
Tejun Heo committed
1084
1085
 *	devices.  This function also issues ATA_CMD_INIT_DEV_PARAMS
 *	for pre-ATA4 drives.
Linus Torvalds's avatar
Linus Torvalds committed
1086
1087
 *
 *	LOCKING:
1088
1089
1090
1091
 *	Kernel thread context (may sleep)
 *
 *	RETURNS:
 *	0 on success, -errno otherwise.
Linus Torvalds's avatar
Linus Torvalds committed
1092
 */
1093
static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
1094
			   unsigned int *p_class, int post_reset, u16 **p_id)
Linus Torvalds's avatar
Linus Torvalds committed
1095
{
1096
	unsigned int class = *p_class;
1097
	struct ata_taskfile tf;
1098
	unsigned int err_mask = 0;
1099
	u16 *id;
1100
1101
	const char *reason;
	int rc;
Linus Torvalds's avatar
Linus Torvalds committed
1102

1103
	DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
Linus Torvalds's avatar
Linus Torvalds committed
1104

1105
	ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
Linus Torvalds's avatar
Linus Torvalds committed
1106

1107
1108
1109
1110
1111
1112
1113
	id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
	if (id == NULL) {
		rc = -ENOMEM;
		reason = "out of memory";
		goto err_out;
	}

1114
1115
 retry:
	ata_tf_init(ap, &tf, dev->devno);
1116

1117
1118
	switch (class) {
	case ATA_DEV_ATA:
1119
		tf.command = ATA_CMD_ID_ATA;
1120
1121
		break;
	case ATA_DEV_ATAPI:
1122
		tf.command = ATA_CMD_ID_ATAPI;
1123
1124
1125
1126
1127
		break;
	default:
		rc = -ENODEV;
		reason = "unsupported class";
		goto err_out;
Linus Torvalds's avatar
Linus Torvalds committed
1128
1129
	}

1130
	tf.protocol = ATA_PROT_PIO;
Linus Torvalds's avatar
Linus Torvalds committed
1131

1132
	err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1133
				     id, sizeof(id[0]) * ATA_ID_WORDS);
1134
	if (err_mask) {
1135
1136
		rc = -EIO;
		reason = "I/O error";
Linus Torvalds's avatar
Linus Torvalds committed
1137
1138
1139
		goto err_out;
	}

1140
	swap_buf_le16(id, ATA_ID_WORDS);
Linus Torvalds's avatar
Linus Torvalds committed
1141

1142
	/* sanity check */
1143
	if ((class == ATA_DEV_ATA) != (ata_id_is_ata(id) | ata_id_is_cfa(id))) {
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
		rc = -EINVAL;
		reason = "device reports illegal type";
		goto err_out;
	}

	if (post_reset && class == ATA_DEV_ATA) {
		/*
		 * The exact sequence expected by certain pre-ATA4 drives is:
		 * SRST RESET
		 * IDENTIFY
		 * INITIALIZE DEVICE PARAMETERS
		 * anything else..
		 * Some drives were very specific about that exact sequence.
		 */
		if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
			err_mask = ata_dev_init_params(ap, dev);
			if (err_mask) {
				rc = -EIO;
				reason = "INIT_DEV_PARAMS failed";
				goto err_out;
			}

			/* current CHS translation info (id[53-58]) might be
			 * changed. reread the identify device info.
			 */
			post_reset = 0;
			goto retry;
		}
	}

	*p_class = class;
1175
	*p_id = id;
1176
1177
1178
1179
1180
	return 0;

 err_out:
	printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
	       ap->id, dev->devno, reason);
1181
	kfree(id);
1182
1183
1184
	return rc;
}

1185
1186
1187
1188
1189
1190
static inline u8 ata_dev_knobble(const struct ata_port *ap,
				 struct ata_device *dev)
{
	return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
}

1191
/**
1192
1193
1194
 *	ata_dev_configure - Configure the specified ATA/ATAPI device
 *	@ap: Port on which target device resides
 *	@dev: Target device to configure
1195
 *	@print_info: Enable device info printout
1196
1197
1198
 *
 *	Configure @dev according to @dev->id.  Generic and low-level
 *	driver specific fixups are also applied.
1199
1200
 *
 *	LOCKING:
1201
1202
1203
1204
 *	Kernel thread context (may sleep)
 *
 *	RETURNS:
 *	0 on success, -errno otherwise
1205
 */
1206
1207
static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
			     int print_info)
1208
{
1209
	const u16 *id = dev->id;
1210
	unsigned int xfer_mask;
1211
1212
1213
1214
	int i, rc;

	if (!ata_dev_present(dev)) {
		DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1215
1216
			ap->id, dev->devno);
		return 0;
1217
1218
	}

1219
	DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
Linus Torvalds's avatar
Linus Torvalds committed
1220

1221
1222
1223
1224
1225
1226
1227
	/* print device capabilities */
	if (print_info)
		printk(KERN_DEBUG "ata%u: dev %u cfg 49:%04x 82:%04x 83:%04x "
		       "84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
		       ap->id, dev->devno, id[49], id[82], id[83],
		       id[84], id[85], id[86], id[87], id[88]);

1228
1229
1230
1231
1232
1233
1234
1235
1236
	/* initialize to-be-configured parameters */
	dev->flags = 0;
	dev->max_sectors = 0;
	dev->cdb_len = 0;
	dev->n_sectors = 0;
	dev->cylinders = 0;
	dev->heads = 0;
	dev->sectors = 0;

Linus Torvalds's avatar
Linus Torvalds committed
1237
1238
1239
1240
	/*
	 * common ATA, ATAPI feature tests
	 */

1241
	/* find max transfer mode; for printk only */
1242
	xfer_mask = ata_id_xfermask(id);
Linus Torvalds's avatar
Linus Torvalds committed
1243

1244
	ata_dump_id(id);
Linus Torvalds's avatar
Linus Torvalds committed
1245
1246
1247

	/* ATA-specific feature tests */
	if (dev->class == ATA_DEV_ATA) {
1248
		dev->n_sectors = ata_id_n_sectors(id);
1249

1250
		if (ata_id_has_lba(id)) {
1251
			const char *lba_desc;
1252

1253
1254
			lba_desc = "LBA";
			dev->flags |= ATA_DFLAG_LBA;
1255
			if (ata_id_has_lba48(id)) {
1256
				dev->flags |= ATA_DFLAG_LBA48;
1257
1258
				lba_desc = "LBA48";
			}
1259
1260

			/* print device info to dmesg */
1261
1262
1263
1264
			if (print_info)
				printk(KERN_INFO "ata%u: dev %u ATA-%d, "
				       "max %s, %Lu sectors: %s\n",
				       ap->id, dev->devno,
1265
				       ata_id_major_version(id),
1266
				       ata_mode_string(xfer_mask),
1267
1268
				       (unsigned long long)dev->n_sectors,
				       lba_desc);
1269
		} else {
1270
1271
1272
			/* CHS */

			/* Default translation */
1273
1274
1275
			dev->cylinders	= id[1];
			dev->heads	= id[3];
			dev->sectors	= id[6];
1276

1277
			if (ata_id_current_chs_valid(id)) {
1278
				/* Current CHS translation is valid. */
1279
1280
1281
				dev->cylinders = id[54];
				dev->heads     = id[55];
				dev->sectors   = id[56];
1282
1283
1284
			}

			/* print device info to dmesg */
1285
1286
1287
1288
			if (print_info)
				printk(KERN_INFO "ata%u: dev %u ATA-%d, "
				       "max %s, %Lu sectors: CHS %u/%u/%u\n",
				       ap->id, dev->devno,
1289
				       ata_id_major_version(id),
1290
				       ata_mode_string(xfer_mask),
1291
1292
				       (unsigned long long)dev->n_sectors,
				       dev->cylinders, dev->heads, dev->sectors);
Linus Torvalds's avatar
Linus Torvalds committed
1293
1294
		}

1295
		dev->cdb_len = 16;
Linus Torvalds's avatar
Linus Torvalds committed
1296
1297
1298
	}

	/* ATAPI-specific feature tests */
1299
	else if (dev->class == ATA_DEV_ATAPI) {
1300
		rc = atapi_cdb_len(id);
Linus Torvalds's avatar
Linus Torvalds committed
1301
1302
		if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
			printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1303
			rc = -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1304
1305
			goto err_out_nosup;
		}
1306
		dev->cdb_len = (unsigned int) rc;
Linus Torvalds's avatar
Linus Torvalds committed
1307
1308

		/* print device info to dmesg */
1309
1310
		if (print_info)
			printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1311
			       ap->id, dev->devno, ata_mode_string(xfer_mask));
Linus Torvalds's avatar
Linus Torvalds committed
1312
1313
	}

1314
1315
1316
1317
1318
1319
	ap->host->max_cmd_len = 0;
	for (i = 0; i < ATA_MAX_DEVICES; i++)
		ap->host->max_cmd_len = max_t(unsigned int,
					      ap->host->max_cmd_len,
					      ap->device[i].cdb_len);

1320
1321
	/* limit bridge transfers to udma5, 200 sectors */
	if (ata_dev_knobble(ap, dev)) {
1322
1323
1324
		if (print_info)
			printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
			       ap->id, dev->devno);
1325
		dev->udma_mask &= ATA_UDMA5;
1326
1327
1328
1329
1330
1331
		dev->max_sectors = ATA_MAX_SECTORS;
	}

	if (ap->ops->dev_config)
		ap->ops->dev_config(ap, dev);

Linus Torvalds's avatar
Linus Torvalds committed
1332
	DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1333
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1334
1335
1336

err_out_nosup:
	DPRINTK("EXIT, err\n");
1337
	return rc;
Linus Torvalds's avatar
Linus Torvalds committed
1338
1339
1340
1341
1342
1343
}

/**
 *	ata_bus_probe - Reset and probe ATA bus
 *	@ap: Bus to probe
 *
Jeff Garzik's avatar
Jeff Garzik committed
1344
1345
1346
1347
 *	Master ATA bus probing function.  Initiates a hardware-dependent
 *	bus reset, then attempts to identify any devices found on
 *	the bus.
 *
Linus Torvalds's avatar
Linus Torvalds committed
1348
 *	LOCKING:
Jeff Garzik's avatar
Jeff Garzik committed
1349
 *	PCI/etc. bus probe sem.
Linus Torvalds's avatar
Linus Torvalds committed
1350
1351
1352
1353
1354
1355
1356
 *
 *	RETURNS:
 *	Zero on success, non-zero on error.
 */

static int ata_bus_probe(struct ata_port *ap)
{
1357
1358
	unsigned int classes[ATA_MAX_DEVICES];
	unsigned int i, rc, found = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1359

1360
	ata_port_probe(ap);
1361

1362
1363
1364
	/* reset and determine device classes */
	for (i = 0; i < ATA_MAX_DEVICES; i++)
		classes[i] = ATA_DEV_UNKNOWN;
1365

1366
	if (ap->ops->probe_reset) {
1367
		rc = ap->ops->probe_reset(ap, classes);
1368
1369
1370
		if (rc) {
			printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
			return rc;
1371
		}
1372
	} else {
1373
1374
		ap->ops->phy_reset(ap);

1375
1376
		if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
			for (i = 0; i < ATA_MAX_DEVICES; i++)
1377
				classes[i] = ap->device[i].class;
1378

1379
1380
		ata_port_probe(ap);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1381

1382
1383
1384
1385
	for (i = 0; i < ATA_MAX_DEVICES; i++)
		if (classes[i] == ATA_DEV_UNKNOWN)
			classes[i] = ATA_DEV_NONE;

1386
	/* read IDENTIFY page and configure devices */
Linus Torvalds's avatar
Linus Torvalds committed
1387
	for (i = 0; i < ATA_MAX_DEVICES; i++) {
1388
1389
		struct ata_device *dev = &ap->device[i];

1390
1391
		dev->class = classes[i];

1392
1393
1394
1395
1396
1397
1398
1399
1400
		if (!ata_dev_present(dev))
			continue;

		WARN_ON(dev->id != NULL);
		if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
			dev->class = ATA_DEV_NONE;
			continue;
		}

1401
		if (ata_dev_configure(ap, dev, 1)) {
1402
			ata_dev_disable(ap, dev);
1403
			continue;
Linus Torvalds's avatar
Linus Torvalds committed
1404
		}
1405
1406

		found = 1;
Linus Torvalds's avatar
Linus Torvalds committed
1407
1408
	}

1409
	if (!found)
Linus Torvalds's avatar
Linus Torvalds committed
1410
1411
		goto err_out_disable;

1412
1413
1414
1415
1416
	if (ap->ops->set_mode)
		ap->ops->set_mode(ap);
	else
		ata_set_mode(ap);

Linus Torvalds's avatar
Linus Torvalds committed
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
	if (ap->flags & ATA_FLAG_PORT_DISABLED)
		goto err_out_disable;

	return 0;

err_out_disable:
	ap->ops->port_disable(ap);
	return -1;
}

/**
Jeff Garzik's avatar
Jeff Garzik committed
1428
1429
 *	ata_port_probe - Mark port as enabled
 *	@ap: Port for which we indicate enablement
Linus Torvalds's avatar
Linus Torvalds committed
1430
 *
Jeff Garzik's avatar
Jeff Garzik committed
1431
1432
1433
1434
1435
 *	Modify @ap data structure such that the system
 *	thinks that the entire port is enabled.
 *
 *	LOCKING: host_set lock, or some other form of
 *	serialization.
Linus Torvalds's avatar
Linus Torvalds committed
1436
1437
1438
1439
1440
1441
1442
 */

void ata_port_probe(struct ata_port *ap)
{
	ap->flags &= ~ATA_FLAG_PORT_DISABLED;
}

1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
/**
 *	sata_print_link_status - Print SATA link status
 *	@ap: SATA port to printk link status about
 *
 *	This function prints link speed and status of a SATA link.
 *
 *	LOCKING:
 *	None.
 */
static void sata_print_link_status(struct ata_port *ap)
{
	u32 sstatus, tmp;
	const char *speed;

	if (!ap->ops->scr_read)
		return;

	sstatus = scr_read(ap, SCR_STATUS);

	if (sata_dev_present(ap)) {
		tmp = (sstatus >> 4) & 0xf;
		if (tmp & (1 << 0))
			speed = "1.5";
		else if (tmp & (1 << 1))
			speed = "3.0";
		else
			speed = "<unknown>";
		printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
		       ap->id, speed, sstatus);
	} else {
		printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
		       ap->id, sstatus);
	}
}

Linus Torvalds's avatar
Linus Torvalds committed
1478
/**
Jeff Garzik's avatar
Jeff Garzik committed
1479
1480
 *	__sata_phy_reset - Wake/reset a low-level SATA PHY
 *	@ap: SATA port associated with target SATA PHY.
Linus Torvalds's avatar
Linus Torvalds committed
1481
 *
Jeff Garzik's avatar
Jeff Garzik committed
1482
1483
1484
 *	This function issues commands to standard SATA Sxxx
 *	PHY registers, to wake up the phy (and device), and
 *	clear any reset condition.
Linus Torvalds's avatar
Linus Torvalds committed
1485
1486
 *
 *	LOCKING:
Jeff Garzik's avatar
Jeff Garzik committed
1487
 *	PCI/etc. bus probe sem.
Linus Torvalds's avatar
Linus Torvalds committed
1488
1489
1490
1491
1492
1493
1494
1495
 *
 */
void __sata_phy_reset(struct ata_port *ap)
{
	u32 sstatus;
	unsigned long timeout = jiffies + (HZ * 5);

	if (ap->flags & ATA_FLAG_SATA_RESET) {
1496
1497
		/* issue phy wake/reset */
		scr_write_flush(ap, SCR_CONTROL, 0x301);
1498
1499
1500
		/* Couldn't find anything in SATA I/II specs, but
		 * AHCI-1.1 10.4.2 says at least 1 ms. */
		mdelay(1);
Linus Torvalds's avatar
Linus Torvalds committed
1501
	}
1502
	scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds's avatar
Linus Torvalds committed
1503
1504
1505
1506
1507
1508
1509
1510
1511

	/* wait for phy to become ready, if necessary */
	do {
		msleep(200);
		sstatus = scr_read(ap, SCR_STATUS);
		if ((sstatus & 0xf) != 1)
			break;
	} while (time_before(jiffies, timeout));

1512
1513
	/* print link status */
	sata_print_link_status(ap);
1514

1515
1516
	/* TODO: phy layer with polling, timeouts, etc. */
	if (sata_dev_present(ap))
Linus Torvalds's avatar
Linus Torvalds committed
1517
		ata_port_probe(ap);
1518
	else
Linus Torvalds's avatar
Linus Torvalds committed
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
		ata_port_disable(ap);

	if (ap->flags & ATA_FLAG_PORT_DISABLED)
		return;

	if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
		ata_port_disable(ap);
		return;
	}

	ap->cbl = ATA_CBL_SATA;
}

/**
Jeff Garzik's avatar
Jeff Garzik committed
1533
1534
 *	sata_phy_reset - Reset SATA bus.
 *	@ap: SATA port associated with target SATA PHY.
Linus Torvalds's avatar
Linus Torvalds committed
1535
 *
Jeff Garzik's avatar
Jeff Garzik committed
1536
1537
 *	This function resets the SATA bus, and then probes
 *	the bus for devices.
Linus Torvalds's avatar
Linus Torvalds committed
1538
1539
 *
 *	LOCKING:
Jeff Garzik's avatar
Jeff Garzik committed
1540
 *	PCI/etc. bus probe sem.
Linus Torvalds's avatar
Linus Torvalds committed
1541
1542
1543
1544
1545
1546