blob: ce0818a993f68a42bf445bc79dea4eaa0ccc94da [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01002 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
3 * Copyright (C) 2005, 2007 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
6/*
7 * Mostly written by Mark Lord <mlord@pobox.com>
8 * and Gadi Oxman <gadio@netvision.net.il>
9 * and Andre Hedrick <andre@linux-ide.org>
10 *
11 * See linux/MAINTAINERS for address of current maintainer.
12 *
13 * This is the IDE probe module, as evolved from hd.c and ide.c.
14 *
Bartlomiej Zolnierkiewiczbbe4d6d2007-12-12 23:32:00 +010015 * -- increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
16 * by Andrea Arcangeli
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/types.h>
21#include <linux/string.h>
22#include <linux/kernel.h>
23#include <linux/timer.h>
24#include <linux/mm.h>
25#include <linux/interrupt.h>
26#include <linux/major.h>
27#include <linux/errno.h>
28#include <linux/genhd.h>
29#include <linux/slab.h>
30#include <linux/delay.h>
31#include <linux/ide.h>
32#include <linux/spinlock.h>
33#include <linux/kmod.h>
34#include <linux/pci.h>
FUJITA Tomonoridc817852007-10-23 09:29:58 +020035#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <asm/byteorder.h>
38#include <asm/irq.h>
39#include <asm/uaccess.h>
40#include <asm/io.h>
41
42/**
43 * generic_id - add a generic drive id
44 * @drive: drive to make an ID block for
45 *
46 * Add a fake id field to the drive we are passed. This allows
47 * use to skip a ton of NULL checks (which people always miss)
48 * and make drive properties unconditional outside of this file
49 */
50
51static void generic_id(ide_drive_t *drive)
52{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +020053 u16 *id = drive->id;
54
55 id[ATA_ID_CUR_CYLS] = id[ATA_ID_CYLS] = drive->cyl;
56 id[ATA_ID_CUR_HEADS] = id[ATA_ID_HEADS] = drive->head;
57 id[ATA_ID_CUR_SECTORS] = id[ATA_ID_SECTORS] = drive->sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
60static void ide_disk_init_chs(ide_drive_t *drive)
61{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +020062 u16 *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 /* Extract geometry if we did not already have one for the drive */
65 if (!drive->cyl || !drive->head || !drive->sect) {
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +020066 drive->cyl = drive->bios_cyl = id[ATA_ID_CYLS];
67 drive->head = drive->bios_head = id[ATA_ID_HEADS];
68 drive->sect = drive->bios_sect = id[ATA_ID_SECTORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
70
71 /* Handle logical geometry translation by the drive */
Bartlomiej Zolnierkiewiczdd8f46f2008-10-10 22:39:19 +020072 if (ata_id_current_chs_valid(id)) {
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +020073 drive->cyl = id[ATA_ID_CUR_CYLS];
74 drive->head = id[ATA_ID_CUR_HEADS];
75 drive->sect = id[ATA_ID_CUR_SECTORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77
78 /* Use physical geometry if what we have still makes no sense */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +020079 if (drive->head > 16 && id[ATA_ID_HEADS] && id[ATA_ID_HEADS] <= 16) {
80 drive->cyl = id[ATA_ID_CYLS];
81 drive->head = id[ATA_ID_HEADS];
82 drive->sect = id[ATA_ID_SECTORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84}
85
86static void ide_disk_init_mult_count(ide_drive_t *drive)
87{
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +020088 u16 *id = drive->id;
89 u8 max_multsect = id[ATA_ID_MAX_MULTSECT] & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +020091 if (max_multsect) {
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +020092 if ((max_multsect / 2) > 1)
93 id[ATA_ID_MULTSECT] = max_multsect | 0x100;
94 else
95 id[ATA_ID_MULTSECT] &= ~0x1ff;
96
97 drive->mult_req = id[ATA_ID_MULTSECT] & 0xff;
Bartlomiej Zolnierkiewicz7c51c172008-10-10 22:39:26 +020098
99 if (drive->mult_req)
Bartlomiej Zolnierkiewiczdf1f8372008-10-10 22:39:18 +0200100 drive->special.b.set_multmode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102}
103
Bartlomiej Zolnierkiewicz24630dc2009-01-02 16:12:47 +0100104static void ide_classify_ata_dev(ide_drive_t *drive)
105{
106 u16 *id = drive->id;
107 char *m = (char *)&id[ATA_ID_PROD];
108 int is_cfa = ata_id_is_cfa(id);
109
110 /* CF devices are *not* removable in Linux definition of the term */
111 if (is_cfa == 0 && (id[ATA_ID_CONFIG] & (1 << 7)))
112 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
113
114 drive->media = ide_disk;
115
116 if (!ata_id_has_unload(drive->id))
117 drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
118
119 printk(KERN_INFO "%s: %s, %s DISK drive\n", drive->name, m,
120 is_cfa ? "CFA" : "ATA");
121}
122
123static void ide_classify_atapi_dev(ide_drive_t *drive)
124{
125 u16 *id = drive->id;
126 char *m = (char *)&id[ATA_ID_PROD];
127 u8 type = (id[ATA_ID_CONFIG] >> 8) & 0x1f;
128
129 printk(KERN_INFO "%s: %s, ATAPI ", drive->name, m);
130 switch (type) {
131 case ide_floppy:
132 if (!strstr(m, "CD-ROM")) {
133 if (!strstr(m, "oppy") &&
134 !strstr(m, "poyp") &&
135 !strstr(m, "ZIP"))
136 printk(KERN_CONT "cdrom or floppy?, assuming ");
137 if (drive->media != ide_cdrom) {
138 printk(KERN_CONT "FLOPPY");
139 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
140 break;
141 }
142 }
143 /* Early cdrom models used zero */
144 type = ide_cdrom;
145 case ide_cdrom:
146 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
147#ifdef CONFIG_PPC
148 /* kludge for Apple PowerBook internal zip */
149 if (!strstr(m, "CD-ROM") && strstr(m, "ZIP")) {
150 printk(KERN_CONT "FLOPPY");
151 type = ide_floppy;
152 break;
153 }
154#endif
155 printk(KERN_CONT "CD/DVD-ROM");
156 break;
157 case ide_tape:
158 printk(KERN_CONT "TAPE");
159 break;
160 case ide_optical:
161 printk(KERN_CONT "OPTICAL");
162 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
163 break;
164 default:
165 printk(KERN_CONT "UNKNOWN (type %d)", type);
166 break;
167 }
168
169 printk(KERN_CONT " drive\n");
170 drive->media = type;
171 /* an ATAPI device ignores DRDY */
172 drive->ready_stat = 0;
173 if (ata_id_cdb_intr(id))
174 drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
175 drive->dev_flags |= IDE_DFLAG_DOORLOCKING;
176 /* we don't do head unloading on ATAPI devices */
177 drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * do_identify - identify a drive
182 * @drive: drive to identify
183 * @cmd: command used
184 *
185 * Called when we have issued a drive identify command to
186 * read and parse the results. This function is run with
187 * interrupts disabled.
188 */
Bartlomiej Zolnierkiewicz047140a2008-12-29 20:27:36 +0100189
190static void do_identify(ide_drive_t *drive, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100192 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200193 u16 *id = drive->id;
194 char *m = (char *)&id[ATA_ID_PROD];
Bartlomiej Zolnierkiewicz94b9efd2008-12-29 20:27:38 +0100195 unsigned long flags;
Bartlomiej Zolnierkiewicz24630dc2009-01-02 16:12:47 +0100196 int bswap = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Bartlomiej Zolnierkiewicz94b9efd2008-12-29 20:27:38 +0100198 /* local CPU only; some systems need this */
199 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /* read 512 bytes of id info */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200201 hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
Bartlomiej Zolnierkiewicz94b9efd2008-12-29 20:27:38 +0100202 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200204 drive->dev_flags |= IDE_DFLAG_ID_READ;
Bartlomiej Zolnierkiewicz7b9f25b2008-02-01 23:09:28 +0100205#ifdef DEBUG
206 printk(KERN_INFO "%s: dumping identify data\n", drive->name);
207 ide_dump_identify((u8 *)id);
208#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 ide_fix_driveid(id);
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /*
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200212 * ATA_CMD_ID_ATA returns little-endian info,
213 * ATA_CMD_ID_ATAPI *usually* returns little-endian info.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200215 if (cmd == ATA_CMD_ID_ATAPI) {
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200216 if ((m[0] == 'N' && m[1] == 'E') || /* NEC */
217 (m[0] == 'F' && m[1] == 'X') || /* Mitsumi */
218 (m[0] == 'P' && m[1] == 'i')) /* Pioneer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 /* Vertos drives may still be weird */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200220 bswap ^= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200222
223 ide_fixstring(m, ATA_ID_PROD_LEN, bswap);
224 ide_fixstring((char *)&id[ATA_ID_FW_REV], ATA_ID_FW_REV_LEN, bswap);
225 ide_fixstring((char *)&id[ATA_ID_SERNO], ATA_ID_SERNO_LEN, bswap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Tejun Heo699b0522007-11-05 21:42:25 +0100227 /* we depend on this a lot! */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200228 m[ATA_ID_PROD_LEN - 1] = '\0';
Tejun Heo699b0522007-11-05 21:42:25 +0100229
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200230 if (strstr(m, "E X A B Y T E N E S T"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 goto err_misc;
232
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200233 drive->dev_flags |= IDE_DFLAG_PRESENT;
234 drive->dev_flags &= ~IDE_DFLAG_DEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /*
237 * Check for an ATAPI device
238 */
Bartlomiej Zolnierkiewicz24630dc2009-01-02 16:12:47 +0100239 if (cmd == ATA_CMD_ID_ATAPI)
240 ide_classify_atapi_dev(drive);
241 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 /*
243 * Not an ATAPI device: looks like a "regular" hard disk
244 */
Bartlomiej Zolnierkiewicz24630dc2009-01-02 16:12:47 +0100245 ide_classify_ata_dev(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247err_misc:
248 kfree(id);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200249 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252/**
253 * actual_try_to_identify - send ata/atapi identify
254 * @drive: drive to identify
255 * @cmd: command to use
256 *
257 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
258 * and waits for a response. It also monitors irqs while this is
259 * happening, in hope of automatically determining which one is
260 * being used by the interface.
261 *
262 * Returns: 0 device was identified
263 * 1 device timed-out (no response to identify request)
264 * 2 device aborted the command (refused to identify itself)
265 */
266
267static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
268{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100269 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200270 struct ide_io_ports *io_ports = &hwif->io_ports;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200271 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100272 int use_altstatus = 0, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 unsigned long timeout;
274 u8 s = 0, a = 0;
275
276 /* take a deep breath */
277 msleep(50);
278
Bartlomiej Zolnierkiewicz66364872008-12-02 20:40:03 +0100279 if (io_ports->ctl_addr &&
280 (hwif->host_flags & IDE_HFLAG_BROKEN_ALTSTATUS) == 0) {
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200281 a = tp_ops->read_altstatus(hwif);
282 s = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200283 if ((a ^ s) & ~ATA_IDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /* ancient Seagate drives, broken interfaces */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100285 printk(KERN_INFO "%s: probing with STATUS(0x%02x) "
286 "instead of ALTSTATUS(0x%02x)\n",
287 drive->name, s, a);
288 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 /* use non-intrusive polling */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100290 use_altstatus = 1;
291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 /* set features register for atapi
294 * identify command to be sure of reply
295 */
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200296 if (cmd == ATA_CMD_ID_ATAPI) {
Bartlomiej Zolnierkiewicz4e658372008-07-23 19:55:53 +0200297 ide_task_t task;
298
299 memset(&task, 0, sizeof(task));
300 /* disable DMA & overlap */
301 task.tf_flags = IDE_TFLAG_OUT_FEATURE;
302
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200303 tp_ops->tf_load(drive, &task);
Bartlomiej Zolnierkiewicz4e658372008-07-23 19:55:53 +0200304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /* ask drive for ID */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200307 tp_ops->exec_command(hwif, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200309 timeout = ((cmd == ATA_CMD_ID_ATA) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200310
311 if (ide_busy_sleep(hwif, timeout, use_altstatus))
312 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200314 /* wait for IRQ and ATA_DRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 msleep(50);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200316 s = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100317
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200318 if (OK_STAT(s, ATA_DRQ, BAD_R_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* drive returned ID */
320 do_identify(drive, cmd);
321 /* drive responded with ID */
322 rc = 0;
323 /* clear drive IRQ */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200324 (void)tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 } else {
326 /* drive refused ID */
327 rc = 2;
328 }
329 return rc;
330}
331
332/**
333 * try_to_identify - try to identify a drive
334 * @drive: drive to probe
335 * @cmd: command to use
336 *
337 * Issue the identify command and then do IRQ probing to
338 * complete the identification when needed by finding the
339 * IRQ the drive is attached to
340 */
341
342static int try_to_identify (ide_drive_t *drive, u8 cmd)
343{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100344 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200345 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 int retval;
347 int autoprobe = 0;
348 unsigned long cookie = 0;
349
350 /*
351 * Disable device irq unless we need to
352 * probe for it. Otherwise we'll get spurious
353 * interrupts during the identify-phase that
354 * the irq handler isn't expecting.
355 */
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200356 if (hwif->io_ports.ctl_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (!hwif->irq) {
358 autoprobe = 1;
359 cookie = probe_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200361 tp_ops->set_irq(hwif, autoprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
364 retval = actual_try_to_identify(drive, cmd);
365
366 if (autoprobe) {
367 int irq;
Bartlomiej Zolnierkiewicz81ca6912008-01-26 20:13:08 +0100368
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200369 tp_ops->set_irq(hwif, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /* clear drive IRQ */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200371 (void)tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 udelay(5);
373 irq = probe_irq_off(cookie);
374 if (!hwif->irq) {
375 if (irq > 0) {
376 hwif->irq = irq;
377 } else {
378 /* Mmmm.. multiple IRQs..
379 * don't know which was ours
380 */
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200381 printk(KERN_ERR "%s: IRQ probe failed (0x%lx)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 drive->name, cookie);
383 }
384 }
385 }
386 return retval;
387}
388
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200389int ide_busy_sleep(ide_hwif_t *hwif, unsigned long timeout, int altstatus)
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100390{
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100391 u8 stat;
392
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200393 timeout += jiffies;
394
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100395 do {
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200396 msleep(50); /* give drive a breather */
397 stat = altstatus ? hwif->tp_ops->read_altstatus(hwif)
398 : hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200399 if ((stat & ATA_BUSY) == 0)
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100400 return 0;
401 } while (time_before(jiffies, timeout));
402
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200403 return 1; /* drive timed-out */
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100404}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200406static u8 ide_read_device(ide_drive_t *drive)
407{
408 ide_task_t task;
409
410 memset(&task, 0, sizeof(task));
411 task.tf_flags = IDE_TFLAG_IN_DEVICE;
412
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200413 drive->hwif->tp_ops->tf_read(drive, &task);
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200414
415 return task.tf.device;
416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/**
419 * do_probe - probe an IDE device
420 * @drive: drive to probe
421 * @cmd: command to use
422 *
423 * do_probe() has the difficult job of finding a drive if it exists,
424 * without getting hung up if it doesn't exist, without trampling on
425 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
426 *
427 * If a drive is "known" to exist (from CMOS or kernel parameters),
428 * but does not respond right away, the probe will "hang in there"
429 * for the maximum wait time (about 30 seconds), otherwise it will
430 * exit much more quickly.
431 *
432 * Returns: 0 device was identified
433 * 1 device timed-out (no response to identify request)
434 * 2 device aborted the command (refused to identify itself)
435 * 3 bad status from device (possible for ATAPI drives)
436 * 4 probe was not attempted because failure was obvious
437 */
438
439static int do_probe (ide_drive_t *drive, u8 cmd)
440{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100441 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200442 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100443 int rc;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200444 u8 present = !!(drive->dev_flags & IDE_DFLAG_PRESENT), stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200446 /* avoid waiting for inappropriate probes */
447 if (present && drive->media != ide_disk && cmd == ATA_CMD_ID_ATA)
448 return 4;
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450#ifdef DEBUG
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200451 printk(KERN_INFO "probing for %s: present=%d, media=%d, probetype=%s\n",
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200452 drive->name, present, drive->media,
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200453 (cmd == ATA_CMD_ID_ATA) ? "ATA" : "ATAPI");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454#endif
455
456 /* needed for some systems
457 * (e.g. crw9624 as drive0 with disk as slave)
458 */
459 msleep(50);
460 SELECT_DRIVE(drive);
461 msleep(50);
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200462
Bartlomiej Zolnierkiewicz7f612f22008-10-13 21:39:40 +0200463 if (ide_read_device(drive) != drive->select && present == 0) {
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +0200464 if (drive->dn & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /* exit with drive0 selected */
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100466 SELECT_DRIVE(hwif->devices[0]);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200467 /* allow ATA_BUSY to assert & clear */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 msleep(50);
469 }
470 /* no i/f present: mmm.. this should be a 4 -ml */
471 return 3;
472 }
473
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200474 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100475
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200476 if (OK_STAT(stat, ATA_DRDY, ATA_BUSY) ||
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200477 present || cmd == ATA_CMD_ID_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 /* send cmd and wait */
479 if ((rc = try_to_identify(drive, cmd))) {
480 /* failed: try again */
481 rc = try_to_identify(drive,cmd);
482 }
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100483
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200484 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100485
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200486 if (stat == (ATA_BUSY | ATA_DRDY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return 4;
488
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200489 if (rc == 1 && cmd == ATA_CMD_ID_ATAPI) {
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100490 printk(KERN_ERR "%s: no response (status = 0x%02x), "
491 "resetting drive\n", drive->name, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 msleep(50);
Bartlomiej Zolnierkiewicze81a3bd2008-07-15 21:21:48 +0200493 SELECT_DRIVE(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 msleep(50);
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200495 tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200496 (void)ide_busy_sleep(hwif, WAIT_WORSTCASE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 rc = try_to_identify(drive, cmd);
498 }
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100499
500 /* ensure drive IRQ is clear */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200501 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (rc == 1)
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100504 printk(KERN_ERR "%s: no response (status = 0x%02x)\n",
505 drive->name, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 } else {
507 /* not present or maybe ATAPI */
508 rc = 3;
509 }
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +0200510 if (drive->dn & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* exit with drive0 selected */
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100512 SELECT_DRIVE(hwif->devices[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 msleep(50);
514 /* ensure drive irq is clear */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200515 (void)tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517 return rc;
518}
519
520/*
521 *
522 */
523static void enable_nest (ide_drive_t *drive)
524{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100525 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200526 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100527 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200529 printk(KERN_INFO "%s: enabling %s -- ",
530 hwif->name, (char *)&drive->id[ATA_ID_PROD]);
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 SELECT_DRIVE(drive);
533 msleep(50);
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200534 tp_ops->exec_command(hwif, ATA_EXABYTE_ENABLE_NEST);
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100535
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200536 if (ide_busy_sleep(hwif, WAIT_WORSTCASE, 0)) {
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100537 printk(KERN_CONT "failed (timeout)\n");
538 return;
539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 msleep(50);
542
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200543 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100544
545 if (!OK_STAT(stat, 0, BAD_STAT))
546 printk(KERN_CONT "failed (status = 0x%02x)\n", stat);
547 else
548 printk(KERN_CONT "success\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
551/**
552 * probe_for_drives - upper level drive probe
553 * @drive: drive to probe for
554 *
555 * probe_for_drive() tests for existence of a given drive using do_probe()
556 * and presents things to the user as needed.
557 *
558 * Returns: 0 no device was found
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200559 * 1 device was found
560 * (note: IDE_DFLAG_PRESENT might still be not set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 */
Bartlomiej Zolnierkiewicz047140a2008-12-29 20:27:36 +0100562
563static u8 probe_for_drive(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200565 char *m;
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /*
568 * In order to keep things simple we have an id
569 * block for all drives at all times. If the device
570 * is pre ATA or refuses ATA/ATAPI identify we
571 * will add faked data to this.
572 *
573 * Also note that 0 everywhere means "can't do X"
574 */
575
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200576 drive->dev_flags &= ~IDE_DFLAG_ID_READ;
577
Bartlomiej Zolnierkiewicz151a6702008-10-10 22:39:28 +0200578 drive->id = kzalloc(SECTOR_SIZE, GFP_KERNEL);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200579 if (drive->id == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 printk(KERN_ERR "ide: out of memory for id data.\n");
581 return 0;
582 }
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200583
584 m = (char *)&drive->id[ATA_ID_PROD];
585 strcpy(m, "UNKNOWN");
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* skip probing? */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200588 if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0) {
Bartlomiej Zolnierkiewiczc36a7e92008-10-10 22:39:23 +0200589retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 /* if !(success||timed-out) */
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200591 if (do_probe(drive, ATA_CMD_ID_ATA) >= 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /* look for ATAPI device */
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200593 (void)do_probe(drive, ATA_CMD_ID_ATAPI);
Bartlomiej Zolnierkiewiczc36a7e92008-10-10 22:39:23 +0200594
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200595 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /* drive not found */
597 return 0;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200598
Bartlomiej Zolnierkiewiczc36a7e92008-10-10 22:39:23 +0200599 if (strstr(m, "E X A B Y T E N E S T")) {
Alan Cox78595572007-07-03 22:28:35 +0200600 enable_nest(drive);
Bartlomiej Zolnierkiewiczc36a7e92008-10-10 22:39:23 +0200601 goto retry;
602 }
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /* identification failed? */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200605 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (drive->media == ide_disk) {
607 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
608 drive->name, drive->cyl,
609 drive->head, drive->sect);
610 } else if (drive->media == ide_cdrom) {
611 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
612 } else {
613 /* nuke it */
614 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200615 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617 }
618 /* drive was found */
619 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200620
621 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return 0;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /* The drive wasn't being helpful. Add generic info only */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200625 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 generic_id(drive);
627 return 1;
628 }
629
630 if (drive->media == ide_disk) {
631 ide_disk_init_chs(drive);
632 ide_disk_init_mult_count(drive);
633 }
634
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200635 return !!(drive->dev_flags & IDE_DFLAG_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
Pavel Machekfc410692008-07-23 19:56:02 +0200638static void hwif_release_dev(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
641
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800642 complete(&hwif->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200645static int ide_register_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Randy Dunlap349ae232006-10-03 01:14:23 -0700647 int ret;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /* register with global device tree */
Kay Sieversdc09c782008-12-29 20:27:36 +0100650 dev_set_name(&hwif->gendev, hwif->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 hwif->gendev.driver_data = hwif;
Andreas Schwabbb54aff2009-01-19 13:46:56 +0100652 if (hwif->gendev.parent == NULL)
653 hwif->gendev.parent = hwif->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 hwif->gendev.release = hwif_release_dev;
Bartlomiej Zolnierkiewicz96d40942009-01-02 16:12:47 +0100655
Randy Dunlap349ae232006-10-03 01:14:23 -0700656 ret = device_register(&hwif->gendev);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200657 if (ret < 0) {
Randy Dunlap349ae232006-10-03 01:14:23 -0700658 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200659 __func__, ret);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200660 goto out;
661 }
662
Greg Kroah-Hartman3ee074b2008-07-21 20:03:34 -0700663 hwif->portdev = device_create(ide_port_class, &hwif->gendev,
664 MKDEV(0, 0), hwif, hwif->name);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200665 if (IS_ERR(hwif->portdev)) {
666 ret = PTR_ERR(hwif->portdev);
667 device_unregister(&hwif->gendev);
668 }
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200669out:
670 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
Bartlomiej Zolnierkiewiczc860a8f2008-02-01 23:09:34 +0100673/**
674 * ide_port_wait_ready - wait for port to become ready
675 * @hwif: IDE port
676 *
677 * This is needed on some PPCs and a bunch of BIOS-less embedded
678 * platforms. Typical cases are:
679 *
680 * - The firmware hard reset the disk before booting the kernel,
681 * the drive is still doing it's poweron-reset sequence, that
682 * can take up to 30 seconds.
683 *
684 * - The firmware does nothing (or no firmware), the device is
685 * still in POST state (same as above actually).
686 *
687 * - Some CD/DVD/Writer combo drives tend to drive the bus during
688 * their reset sequence even when they are non-selected slave
689 * devices, thus preventing discovery of the main HD.
690 *
691 * Doing this wait-for-non-busy should not harm any existing
692 * configuration and fix some issues like the above.
693 *
694 * BenH.
695 *
696 * Returns 0 on success, error code (< 0) otherwise.
697 */
698
699static int ide_port_wait_ready(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100701 ide_drive_t *drive;
702 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
705
706 /* Let HW settle down a bit from whatever init state we
707 * come from */
708 mdelay(2);
709
710 /* Wait for BSY bit to go away, spec timeout is 30 seconds,
711 * I know of at least one disk who takes 31 seconds, I use 35
712 * here to be safe
713 */
714 rc = ide_wait_not_busy(hwif, 35000);
715 if (rc)
716 return rc;
717
718 /* Now make sure both master & slave are ready */
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100719 ide_port_for_each_dev(i, drive, hwif) {
Jonas Stare82661052007-11-27 21:35:53 +0100720 /* Ignore disks that we will not probe for later. */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200721 if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0 ||
722 (drive->dev_flags & IDE_DFLAG_PRESENT)) {
Jonas Stare82661052007-11-27 21:35:53 +0100723 SELECT_DRIVE(drive);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200724 hwif->tp_ops->set_irq(hwif, 1);
Jonas Stare82661052007-11-27 21:35:53 +0100725 mdelay(2);
726 rc = ide_wait_not_busy(hwif, 35000);
727 if (rc)
728 goto out;
729 } else
730 printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
731 drive->name);
732 }
733out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /* Exit function with master reselected (let's be sane) */
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100735 if (i)
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100736 SELECT_DRIVE(hwif->devices[0]);
Jonas Stare82661052007-11-27 21:35:53 +0100737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return rc;
739}
740
741/**
742 * ide_undecoded_slave - look for bad CF adapters
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200743 * @dev1: slave device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 *
745 * Analyse the drives on the interface and attempt to decide if we
746 * have the same drive viewed twice. This occurs with crap CF adapters
747 * and PCMCIA sometimes.
748 */
749
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200750void ide_undecoded_slave(ide_drive_t *dev1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100752 ide_drive_t *dev0 = dev1->hwif->devices[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200754 if ((dev1->dn & 1) == 0 || (dev0->dev_flags & IDE_DFLAG_PRESENT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return;
756
757 /* If the models don't match they are not the same product */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200758 if (strcmp((char *)&dev0->id[ATA_ID_PROD],
759 (char *)&dev1->id[ATA_ID_PROD]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return;
761
762 /* Serial numbers do not match */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200763 if (strncmp((char *)&dev0->id[ATA_ID_SERNO],
764 (char *)&dev1->id[ATA_ID_SERNO], ATA_ID_SERNO_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return;
766
767 /* No serial number, thankfully very rare for CF */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200768 if (*(char *)&dev0->id[ATA_ID_SERNO] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return;
770
771 /* Appears to be an IDE flash adapter with decode bugs */
772 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
773
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200774 dev1->dev_flags &= ~IDE_DFLAG_PRESENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777EXPORT_SYMBOL_GPL(ide_undecoded_slave);
778
Bartlomiej Zolnierkiewicz9d501522008-02-01 23:09:36 +0100779static int ide_probe_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100781 ide_drive_t *drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 unsigned long flags;
783 unsigned int irqd;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100784 int i, rc = -ENODEV;
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100785
786 BUG_ON(hwif->present);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100788 if ((hwif->devices[0]->dev_flags & IDE_DFLAG_NOPROBE) &&
789 (hwif->devices[1]->dev_flags & IDE_DFLAG_NOPROBE))
Bartlomiej Zolnierkiewicz9d501522008-02-01 23:09:36 +0100790 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 /*
793 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
794 * we'll install our IRQ driver much later...
795 */
796 irqd = hwif->irq;
797 if (irqd)
798 disable_irq(hwif->irq);
799
Bartlomiej Zolnierkiewicz9b896032009-01-14 19:19:02 +0100800 local_save_flags(flags);
Bartlomiej Zolnierkiewicz54cc1422009-01-06 17:20:52 +0100801 local_irq_enable_in_hardirq();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Bartlomiej Zolnierkiewiczc860a8f2008-02-01 23:09:34 +0100803 if (ide_port_wait_ready(hwif) == -EBUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
805
806 /*
Bartlomiej Zolnierkiewiczf367bed2008-03-29 19:48:21 +0100807 * Second drive should only exist if first drive was found,
808 * but a lot of cdrom drives are configured as single slaves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 */
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100810 ide_port_for_each_dev(i, drive, hwif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 (void) probe_for_drive(drive);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200812 if (drive->dev_flags & IDE_DFLAG_PRESENT)
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100813 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
Bartlomiej Zolnierkiewicze460a592008-04-27 15:38:24 +0200815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 local_irq_restore(flags);
Bartlomiej Zolnierkiewicze460a592008-04-27 15:38:24 +0200817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /*
819 * Use cached IRQ number. It might be (and is...) changed by probe
820 * code above
821 */
822 if (irqd)
823 enable_irq(irqd);
824
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100825 return rc;
Bartlomiej Zolnierkiewicze84e7ea2008-02-01 23:09:36 +0100826}
827
828static void ide_port_tune_devices(ide_hwif_t *hwif)
829{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200830 const struct ide_port_ops *port_ops = hwif->port_ops;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100831 ide_drive_t *drive;
832 int i;
Bartlomiej Zolnierkiewicze84e7ea2008-02-01 23:09:36 +0100833
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100834 ide_port_for_each_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200835 if (drive->dev_flags & IDE_DFLAG_PRESENT) {
836 if (port_ops && port_ops->quirkproc)
837 port_ops->quirkproc(drive);
838 }
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100839 }
Bartlomiej Zolnierkiewicz0380dad2007-06-08 15:14:29 +0200840
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100841 ide_port_for_each_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200842 if (drive->dev_flags & IDE_DFLAG_PRESENT) {
Bartlomiej Zolnierkiewicz207daea2008-04-27 15:38:29 +0200843 ide_set_max_pio(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200845 drive->dev_flags |= IDE_DFLAG_NICE1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200847 if (hwif->dma_ops)
Bartlomiej Zolnierkiewicz8c0697c2007-10-16 22:29:58 +0200848 ide_set_dma(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850 }
Kumar Gala208a08f2006-03-24 03:18:21 -0800851
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100852 ide_port_for_each_dev(i, drive, hwif) {
Mario Schwalbe744a82b2009-01-06 17:20:57 +0100853 if (hwif->host_flags & IDE_HFLAG_NO_IO_32BIT)
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200854 drive->dev_flags |= IDE_DFLAG_NO_IO_32BIT;
Kumar Gala208a08f2006-03-24 03:18:21 -0800855 else
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200856 drive->dev_flags &= ~IDE_DFLAG_NO_IO_32BIT;
Kumar Gala208a08f2006-03-24 03:18:21 -0800857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 * init request queue
862 */
863static int ide_init_queue(ide_drive_t *drive)
864{
Jens Axboe165125e2007-07-24 09:28:11 +0200865 struct request_queue *q;
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100866 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 int max_sectors = 256;
868 int max_sg_entries = PRD_ENTRIES;
869
870 /*
871 * Our default set up assumes the normal IDE case,
872 * that is 64K segmenting, standard PRD setup
873 * and LBA28. Some drivers then impose their own
874 * limits and LBA48 we could raise it but as yet
875 * do not.
876 */
Christoph Lameter19460892005-06-23 00:08:19 -0700877
Bartlomiej Zolnierkiewicz201bffa2009-01-02 16:12:50 +0100878 q = blk_init_queue_node(do_ide_request, NULL, hwif_to_node(hwif));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (!q)
880 return 1;
881
882 q->queuedata = drive;
883 blk_queue_segment_boundary(q, 0xffff);
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (hwif->rqsize < max_sectors)
886 max_sectors = hwif->rqsize;
887 blk_queue_max_sectors(q, max_sectors);
888
889#ifdef CONFIG_PCI
890 /* When we have an IOMMU, we may have a problem where pci_map_sg()
891 * creates segments that don't completely match our boundary
892 * requirements and thus need to be broken up again. Because it
893 * doesn't align properly either, we may actually have to break up
894 * to more segments than what was we got in the first place, a max
895 * worst case is twice as many.
896 * This will be fixed once we teach pci_map_sg() about our boundary
897 * requirements, hopefully soon. *FIXME*
898 */
899 if (!PCI_DMA_BUS_IS_PHYS)
900 max_sg_entries >>= 1;
901#endif /* CONFIG_PCI */
902
903 blk_queue_max_hw_segments(q, max_sg_entries);
904 blk_queue_max_phys_segments(q, max_sg_entries);
905
906 /* assign drive queue */
907 drive->queue = q;
908
909 /* needs drive->queue to be set */
910 ide_toggle_bounce(drive, 1);
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return 0;
913}
914
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +0100915static DEFINE_MUTEX(ide_cfg_mtx);
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917/*
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100918 * For any present drive:
919 * - allocate the block device queue
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100920 */
Elias Oltmannse415e492008-10-13 21:39:45 +0200921static int ide_port_setup_devices(ide_hwif_t *hwif)
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100922{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100923 ide_drive_t *drive;
Elias Oltmannse415e492008-10-13 21:39:45 +0200924 int i, j = 0;
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100925
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +0200926 mutex_lock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100927 ide_port_for_each_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200928 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100929 continue;
930
931 if (ide_init_queue(drive)) {
932 printk(KERN_ERR "ide: failed to init %s\n",
933 drive->name);
Elias Oltmannse415e492008-10-13 21:39:45 +0200934 kfree(drive->id);
935 drive->id = NULL;
936 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100937 continue;
938 }
939
Elias Oltmannse415e492008-10-13 21:39:45 +0200940 j++;
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100941 }
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +0200942 mutex_unlock(&ide_cfg_mtx);
Elias Oltmannse415e492008-10-13 21:39:45 +0200943
944 return j;
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100945}
946
947/*
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100948 * This routine sets up the IRQ for an IDE interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 */
950static int init_irq (ide_hwif_t *hwif)
951{
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200952 struct ide_io_ports *io_ports = &hwif->io_ports;
Bartlomiej Zolnierkiewiczae86afa2009-01-06 17:20:48 +0100953 int sa = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Matthias Kaehlckeef298882007-07-09 23:17:55 +0200955 mutex_lock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100956 spin_lock_init(&hwif->lock);
Bartlomiej Zolnierkiewicz75d21ff2008-10-13 21:39:33 +0200957
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100958 init_timer(&hwif->timer);
959 hwif->timer.function = &ide_timer_expiry;
960 hwif->timer.data = (unsigned long)hwif;
Bartlomiej Zolnierkiewiczaf1cbba2008-07-23 19:55:58 +0200961
Adrian Bunk7b892802008-02-06 01:36:29 -0800962#if defined(__mc68000__)
Bartlomiej Zolnierkiewiczae86afa2009-01-06 17:20:48 +0100963 sa = IRQF_SHARED;
Bartlomiej Zolnierkiewicze1771e22008-02-11 00:32:15 +0100964#endif /* __mc68000__ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Bartlomiej Zolnierkiewiczae86afa2009-01-06 17:20:48 +0100966 if (hwif->chipset == ide_pci)
967 sa = IRQF_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Bartlomiej Zolnierkiewiczae86afa2009-01-06 17:20:48 +0100969 if (io_ports->ctl_addr)
970 hwif->tp_ops->set_irq(hwif, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Bartlomiej Zolnierkiewiczae86afa2009-01-06 17:20:48 +0100972 if (request_irq(hwif->irq, &ide_intr, sa, hwif->name, hwif))
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100973 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Bartlomiej Zolnierkiewicz8a0e7e12008-02-02 19:56:41 +0100975 if (!hwif->rqsize) {
976 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
977 (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
978 hwif->rqsize = 256;
979 else
980 hwif->rqsize = 65536;
981 }
982
Adrian Bunk7b892802008-02-06 01:36:29 -0800983#if !defined(__mc68000__)
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200984 printk(KERN_INFO "%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200985 io_ports->data_addr, io_ports->status_addr,
986 io_ports->ctl_addr, hwif->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987#else
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200988 printk(KERN_INFO "%s at 0x%08lx on irq %d", hwif->name,
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200989 io_ports->data_addr, hwif->irq);
Adrian Bunk7b892802008-02-06 01:36:29 -0800990#endif /* __mc68000__ */
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100991 if (hwif->host->host_flags & IDE_HFLAG_SERIALIZE)
992 printk(KERN_CONT " (serialized)");
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200993 printk(KERN_CONT "\n");
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100994
Matthias Kaehlckeef298882007-07-09 23:17:55 +0200995 mutex_unlock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997out_up:
Matthias Kaehlckeef298882007-07-09 23:17:55 +0200998 mutex_unlock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return 1;
1000}
1001
1002static int ata_lock(dev_t dev, void *data)
1003{
1004 /* FIXME: we want to pin hwif down */
1005 return 0;
1006}
1007
1008static struct kobject *ata_probe(dev_t dev, int *part, void *data)
1009{
1010 ide_hwif_t *hwif = data;
1011 int unit = *part >> PARTN_BITS;
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001012 ide_drive_t *drive = hwif->devices[unit];
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001013
1014 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return NULL;
1016
1017 if (drive->media == ide_disk)
1018 request_module("ide-disk");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (drive->media == ide_cdrom || drive->media == ide_optical)
1020 request_module("ide-cd");
1021 if (drive->media == ide_tape)
1022 request_module("ide-tape");
1023 if (drive->media == ide_floppy)
1024 request_module("ide-floppy");
1025
1026 return NULL;
1027}
1028
1029static struct kobject *exact_match(dev_t dev, int *part, void *data)
1030{
1031 struct gendisk *p = data;
1032 *part &= (1 << PARTN_BITS) - 1;
Tejun Heoed9e1982008-08-25 19:56:05 +09001033 return &disk_to_dev(p)->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
1036static int exact_lock(dev_t dev, void *data)
1037{
1038 struct gendisk *p = data;
1039
1040 if (!get_disk(p))
1041 return -1;
1042 return 0;
1043}
1044
1045void ide_register_region(struct gendisk *disk)
1046{
1047 blk_register_region(MKDEV(disk->major, disk->first_minor),
1048 disk->minors, NULL, exact_match, exact_lock, disk);
1049}
1050
1051EXPORT_SYMBOL_GPL(ide_register_region);
1052
1053void ide_unregister_region(struct gendisk *disk)
1054{
1055 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
1056 disk->minors);
1057}
1058
1059EXPORT_SYMBOL_GPL(ide_unregister_region);
1060
1061void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
1062{
1063 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz7f612f22008-10-13 21:39:40 +02001064 unsigned int unit = drive->dn & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 disk->major = hwif->major;
1067 disk->first_minor = unit << PARTN_BITS;
1068 sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
1069 disk->queue = drive->queue;
1070}
1071
1072EXPORT_SYMBOL_GPL(ide_init_disk);
1073
1074static void drive_release_dev (struct device *dev)
1075{
1076 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +01001077 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Bartlomiej Zolnierkiewicz5b0c4b32008-04-18 00:46:22 +02001079 ide_proc_unregister_device(drive);
1080
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +01001081 spin_lock_irq(&hwif->lock);
Jesper Juhl6044ec82005-11-07 01:01:32 -08001082 kfree(drive->id);
1083 drive->id = NULL;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001084 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001085 /* Messed up locking ... */
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +01001086 spin_unlock_irq(&hwif->lock);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001087 blk_cleanup_queue(drive->queue);
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +01001088 spin_lock_irq(&hwif->lock);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001089 drive->queue = NULL;
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +01001090 spin_unlock_irq(&hwif->lock);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001091
Aleksey Makarovf36d4022006-01-09 15:59:27 -08001092 complete(&drive->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095static int hwif_init(ide_hwif_t *hwif)
1096{
1097 int old_irq;
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (!hwif->irq) {
Bartlomiej Zolnierkiewicza861beb2008-07-08 19:27:22 +02001100 hwif->irq = __ide_default_irq(hwif->io_ports.data_addr);
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +02001101 if (!hwif->irq) {
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +02001102 printk(KERN_ERR "%s: disabled, no IRQ\n", hwif->name);
Bartlomiej Zolnierkiewicz48535652008-02-01 23:09:34 +01001103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (register_blkdev(hwif->major, hwif->name))
1108 return 0;
1109
1110 if (!hwif->sg_max_nents)
1111 hwif->sg_max_nents = PRD_ENTRIES;
1112
Jens Axboe45711f12007-10-22 21:19:53 +02001113 hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 GFP_KERNEL);
1115 if (!hwif->sg_table) {
1116 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1117 goto out;
1118 }
Jens Axboe45711f12007-10-22 21:19:53 +02001119
1120 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 if (init_irq(hwif) == 0)
1123 goto done;
1124
1125 old_irq = hwif->irq;
1126 /*
1127 * It failed to initialise. Find the default IRQ for
1128 * this port and try that.
1129 */
Bartlomiej Zolnierkiewicza861beb2008-07-08 19:27:22 +02001130 hwif->irq = __ide_default_irq(hwif->io_ports.data_addr);
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +02001131 if (!hwif->irq) {
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +02001132 printk(KERN_ERR "%s: disabled, unable to get IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 hwif->name, old_irq);
1134 goto out;
1135 }
1136 if (init_irq(hwif)) {
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +02001137 printk(KERN_ERR "%s: probed IRQ %d and default IRQ %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 hwif->name, old_irq, hwif->irq);
1139 goto out;
1140 }
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +02001141 printk(KERN_WARNING "%s: probed IRQ %d failed, using default\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 hwif->name, hwif->irq);
1143
1144done:
Bartlomiej Zolnierkiewicz3a4e7c92008-02-02 19:56:40 +01001145 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1146 THIS_MODULE, ata_probe, ata_lock, hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return 1;
1148
1149out:
1150 unregister_blkdev(hwif->major, hwif->name);
1151 return 0;
1152}
1153
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001154static void hwif_register_devices(ide_hwif_t *hwif)
1155{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001156 ide_drive_t *drive;
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001157 unsigned int i;
1158
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001159 ide_port_for_each_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewiczc5d70cc2008-02-02 19:56:41 +01001160 struct device *dev = &drive->gendev;
1161 int ret;
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001162
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001163 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Bartlomiej Zolnierkiewiczc5d70cc2008-02-02 19:56:41 +01001164 continue;
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001165
Kay Sieversdc09c782008-12-29 20:27:36 +01001166 dev_set_name(dev, "%u.%u", hwif->index, i);
Bartlomiej Zolnierkiewiczc5d70cc2008-02-02 19:56:41 +01001167 dev->parent = &hwif->gendev;
1168 dev->bus = &ide_bus_type;
1169 dev->driver_data = drive;
1170 dev->release = drive_release_dev;
1171
1172 ret = device_register(dev);
1173 if (ret < 0)
1174 printk(KERN_WARNING "IDE: %s: device_register error: "
1175 "%d\n", __func__, ret);
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001176 }
1177}
1178
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001179static void ide_port_init_devices(ide_hwif_t *hwif)
1180{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001181 const struct ide_port_ops *port_ops = hwif->port_ops;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001182 ide_drive_t *drive;
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001183 int i;
1184
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001185 ide_port_for_each_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +02001186 drive->dn = i + hwif->channel * 2;
1187
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001188 if (hwif->host_flags & IDE_HFLAG_IO_32BIT)
1189 drive->io_32bit = 1;
1190 if (hwif->host_flags & IDE_HFLAG_UNMASK_IRQS)
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001191 drive->dev_flags |= IDE_DFLAG_UNMASK;
Bartlomiej Zolnierkiewicz807b90d2008-02-02 19:56:40 +01001192 if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS)
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001193 drive->dev_flags |= IDE_DFLAG_NO_UNMASK;
Bartlomiej Zolnierkiewicz1f2cf8b2008-02-02 19:56:40 +01001194
Bartlomiej Zolnierkiewicze6d95bd2008-07-16 20:33:42 +02001195 if (port_ops && port_ops->init_dev)
1196 port_ops->init_dev(drive);
1197 }
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001198}
1199
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001200static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
1201 const struct ide_port_info *d)
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001202{
Adrian Bunkb7691642008-06-10 20:56:36 +02001203 hwif->channel = port;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001204
1205 if (d->chipset)
1206 hwif->chipset = d->chipset;
1207
1208 if (d->init_iops)
1209 d->init_iops(hwif);
1210
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001211 if ((!hwif->irq && (d->host_flags & IDE_HFLAG_LEGACY_IRQS)) ||
1212 (d->host_flags & IDE_HFLAG_FORCE_LEGACY_IRQS))
1213 hwif->irq = port ? 15 : 14;
1214
Bartlomiej Zolnierkiewicz23f8e4b2008-05-01 14:08:51 +02001215 /* ->host_flags may be set by ->init_iops (or even earlier...) */
1216 hwif->host_flags |= d->host_flags;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001217 hwif->pio_mask = d->pio_mask;
1218
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001219 if (d->tp_ops)
1220 hwif->tp_ops = d->tp_ops;
1221
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001222 /* ->set_pio_mode for DTC2278 is currently limited to port 0 */
1223 if (hwif->chipset != ide_dtc2278 || hwif->channel == 0)
1224 hwif->port_ops = d->port_ops;
1225
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001226 hwif->swdma_mask = d->swdma_mask;
1227 hwif->mwdma_mask = d->mwdma_mask;
1228 hwif->ultra_mask = d->udma_mask;
1229
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001230 if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
1231 int rc;
1232
Sergei Shtylyov592b5312009-01-06 17:21:02 +01001233 hwif->dma_ops = d->dma_ops;
1234
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001235 if (d->init_dma)
1236 rc = d->init_dma(hwif, d);
1237 else
1238 rc = ide_hwif_setup_dma(hwif, d);
1239
1240 if (rc < 0) {
1241 printk(KERN_INFO "%s: DMA disabled\n", hwif->name);
Sergei Shtylyov592b5312009-01-06 17:21:02 +01001242
1243 hwif->dma_ops = NULL;
Bartlomiej Zolnierkiewiczebb00fb2008-07-23 19:55:51 +02001244 hwif->dma_base = 0;
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001245 hwif->swdma_mask = 0;
1246 hwif->mwdma_mask = 0;
1247 hwif->ultra_mask = 0;
Sergei Shtylyov592b5312009-01-06 17:21:02 +01001248 }
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001249 }
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001250
Bartlomiej Zolnierkiewicz1024c5f2008-05-04 17:03:41 +02001251 if ((d->host_flags & IDE_HFLAG_SERIALIZE) ||
Bartlomiej Zolnierkiewicz702c0262008-12-29 20:27:36 +01001252 ((d->host_flags & IDE_HFLAG_SERIALIZE_DMA) && hwif->dma_base))
1253 hwif->host->host_flags |= IDE_HFLAG_SERIALIZE;
Bartlomiej Zolnierkiewicz1024c5f2008-05-04 17:03:41 +02001254
Bartlomiej Zolnierkiewicz6b492492008-12-29 20:27:34 +01001255 if (d->max_sectors)
1256 hwif->rqsize = d->max_sectors;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001257
1258 /* call chipset specific routine for each enabled port */
1259 if (d->init_hwif)
1260 d->init_hwif(hwif);
Bartlomiej Zolnierkiewiczc7f6f212008-04-18 00:46:22 +02001261}
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001262
Bartlomiej Zolnierkiewiczc7f6f212008-04-18 00:46:22 +02001263static void ide_port_cable_detect(ide_hwif_t *hwif)
1264{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001265 const struct ide_port_ops *port_ops = hwif->port_ops;
1266
1267 if (port_ops && port_ops->cable_detect && (hwif->ultra_mask & 0x78)) {
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001268 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001269 hwif->cbl = port_ops->cable_detect(hwif);
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001270 }
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001271}
1272
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001273static const u8 ide_hwif_to_major[] =
1274 { IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR, IDE4_MAJOR,
1275 IDE5_MAJOR, IDE6_MAJOR, IDE7_MAJOR, IDE8_MAJOR, IDE9_MAJOR };
1276
1277static void ide_port_init_devices_data(ide_hwif_t *hwif)
1278{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001279 ide_drive_t *drive;
1280 int i;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001281
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001282 ide_port_for_each_dev(i, drive, hwif) {
1283 u8 j = (hwif->index * MAX_DRIVES) + i;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001284
1285 memset(drive, 0, sizeof(*drive));
1286
1287 drive->media = ide_disk;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001288 drive->select = (i << 4) | ATA_DEVICE_OBS;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001289 drive->hwif = hwif;
1290 drive->ready_stat = ATA_DRDY;
1291 drive->bad_wstat = BAD_W_STAT;
1292 drive->special.b.recalibrate = 1;
1293 drive->special.b.set_geometry = 1;
1294 drive->name[0] = 'h';
1295 drive->name[1] = 'd';
1296 drive->name[2] = 'a' + j;
1297 drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
1298
1299 INIT_LIST_HEAD(&drive->list);
1300 init_completion(&drive->gendev_rel_comp);
1301 }
1302}
1303
1304static void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
1305{
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001306 /* fill in any non-zero initial values */
1307 hwif->index = index;
1308 hwif->major = ide_hwif_to_major[index];
1309
1310 hwif->name[0] = 'i';
1311 hwif->name[1] = 'd';
1312 hwif->name[2] = 'e';
1313 hwif->name[3] = '0' + index;
1314
1315 init_completion(&hwif->gendev_rel_comp);
1316
1317 hwif->tp_ops = &default_tp_ops;
1318
1319 ide_port_init_devices_data(hwif);
1320}
1321
1322static void ide_init_port_hw(ide_hwif_t *hwif, hw_regs_t *hw)
1323{
1324 memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports));
1325 hwif->irq = hw->irq;
1326 hwif->chipset = hw->chipset;
1327 hwif->dev = hw->dev;
1328 hwif->gendev.parent = hw->parent ? hw->parent : hw->dev;
1329 hwif->ack_intr = hw->ack_intr;
1330 hwif->config_data = hw->config;
1331}
1332
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001333static unsigned int ide_indexes;
1334
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001335/**
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001336 * ide_find_port_slot - find free port slot
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001337 * @d: IDE port info
1338 *
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001339 * Return the new port slot index or -ENOENT if we are out of free slots.
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001340 */
1341
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001342static int ide_find_port_slot(const struct ide_port_info *d)
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001343{
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001344 int idx = -ENOENT;
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001345 u8 bootable = (d && (d->host_flags & IDE_HFLAG_NON_BOOTABLE)) ? 0 : 1;
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001346 u8 i = (d && (d->host_flags & IDE_HFLAG_QD_2ND_PORT)) ? 1 : 0;;
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001347
1348 /*
1349 * Claim an unassigned slot.
1350 *
1351 * Give preference to claiming other slots before claiming ide0/ide1,
1352 * just in case there's another interface yet-to-be-scanned
1353 * which uses ports 0x1f0/0x170 (the ide0/ide1 defaults).
1354 *
1355 * Unless there is a bootable card that does not use the standard
1356 * ports 0x1f0/0x170 (the ide0/ide1 defaults).
1357 */
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001358 mutex_lock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewicz75d21ff2008-10-13 21:39:33 +02001359 if (bootable) {
1360 if ((ide_indexes | i) != (1 << MAX_HWIFS) - 1)
1361 idx = ffz(ide_indexes | i);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001362 } else {
Bartlomiej Zolnierkiewicz75d21ff2008-10-13 21:39:33 +02001363 if ((ide_indexes | 3) != (1 << MAX_HWIFS) - 1)
1364 idx = ffz(ide_indexes | 3);
1365 else if ((ide_indexes & 3) != 3)
1366 idx = ffz(ide_indexes);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001367 }
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001368 if (idx >= 0)
1369 ide_indexes |= (1 << idx);
1370 mutex_unlock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001371
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001372 return idx;
1373}
Bartlomiej Zolnierkiewiczeb3aff52008-07-16 20:33:42 +02001374
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001375static void ide_free_port_slot(int idx)
1376{
1377 mutex_lock(&ide_cfg_mtx);
1378 ide_indexes &= ~(1 << idx);
1379 mutex_unlock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001380}
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001381
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001382static void ide_port_free_devices(ide_hwif_t *hwif)
1383{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001384 ide_drive_t *drive;
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001385 int i;
1386
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001387 ide_port_for_each_dev(i, drive, hwif)
1388 kfree(drive);
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001389}
1390
1391static int ide_port_alloc_devices(ide_hwif_t *hwif, int node)
1392{
1393 int i;
1394
1395 for (i = 0; i < MAX_DRIVES; i++) {
1396 ide_drive_t *drive;
1397
1398 drive = kzalloc_node(sizeof(*drive), GFP_KERNEL, node);
1399 if (drive == NULL)
1400 goto out_nomem;
1401
1402 hwif->devices[i] = drive;
1403 }
1404 return 0;
1405
1406out_nomem:
1407 ide_port_free_devices(hwif);
1408 return -ENOMEM;
1409}
1410
Bartlomiej Zolnierkiewicza36223b2008-10-13 21:39:43 +02001411struct ide_host *ide_host_alloc(const struct ide_port_info *d, hw_regs_t **hws)
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001412{
1413 struct ide_host *host;
Bartlomiej Zolnierkiewicza32296f2009-01-06 17:20:56 +01001414 struct device *dev = hws[0] ? hws[0]->dev : NULL;
1415 int node = dev ? dev_to_node(dev) : -1;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001416 int i;
1417
Bartlomiej Zolnierkiewicza32296f2009-01-06 17:20:56 +01001418 host = kzalloc_node(sizeof(*host), GFP_KERNEL, node);
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001419 if (host == NULL)
1420 return NULL;
1421
Bartlomiej Zolnierkiewicza36223b2008-10-13 21:39:43 +02001422 for (i = 0; i < MAX_HOST_PORTS; i++) {
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001423 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001424 int idx;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001425
1426 if (hws[i] == NULL)
1427 continue;
1428
Bartlomiej Zolnierkiewicza32296f2009-01-06 17:20:56 +01001429 hwif = kzalloc_node(sizeof(*hwif), GFP_KERNEL, node);
Bartlomiej Zolnierkiewicz18de1012008-07-23 19:55:58 +02001430 if (hwif == NULL)
1431 continue;
1432
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001433 if (ide_port_alloc_devices(hwif, node) < 0) {
1434 kfree(hwif);
1435 continue;
1436 }
1437
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001438 idx = ide_find_port_slot(d);
1439 if (idx < 0) {
1440 printk(KERN_ERR "%s: no free slot for interface\n",
1441 d ? d->name : "ide");
Bartlomiej Zolnierkiewicz18de1012008-07-23 19:55:58 +02001442 kfree(hwif);
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001443 continue;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001444 }
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001445
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001446 ide_init_port_data(hwif, idx);
1447
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +02001448 hwif->host = host;
1449
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001450 host->ports[i] = hwif;
1451 host->n_ports++;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001452 }
1453
1454 if (host->n_ports == 0) {
1455 kfree(host);
1456 return NULL;
1457 }
1458
Bartlomiej Zolnierkiewicza32296f2009-01-06 17:20:56 +01001459 host->dev[0] = dev;
Bartlomiej Zolnierkiewicz6cdf6eb2008-07-24 22:53:14 +02001460
Bartlomiej Zolnierkiewiczfeb22b72008-10-10 22:39:32 +02001461 if (d) {
1462 host->init_chipset = d->init_chipset;
Bartlomiej Zolnierkiewiczef0b0422008-07-24 22:53:19 +02001463 host->host_flags = d->host_flags;
Bartlomiej Zolnierkiewiczfeb22b72008-10-10 22:39:32 +02001464 }
Bartlomiej Zolnierkiewiczef0b0422008-07-24 22:53:19 +02001465
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001466 return host;
1467}
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001468EXPORT_SYMBOL_GPL(ide_host_alloc);
1469
Bartlomiej Zolnierkiewicz9a100f42009-02-02 20:12:21 +01001470static void ide_port_free(ide_hwif_t *hwif)
1471{
1472 ide_port_free_devices(hwif);
1473 ide_free_port_slot(hwif->index);
1474 kfree(hwif);
1475}
1476
1477static void ide_disable_port(ide_hwif_t *hwif)
1478{
1479 struct ide_host *host = hwif->host;
1480 int i;
1481
1482 printk(KERN_INFO "%s: disabling port\n", hwif->name);
1483
1484 for (i = 0; i < MAX_HOST_PORTS; i++) {
1485 if (host->ports[i] == hwif) {
1486 host->ports[i] = NULL;
1487 host->n_ports--;
1488 }
1489 }
1490
1491 ide_port_free(hwif);
1492}
1493
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001494int ide_host_register(struct ide_host *host, const struct ide_port_info *d,
1495 hw_regs_t **hws)
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001496{
1497 ide_hwif_t *hwif, *mate = NULL;
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001498 int i, j = 0;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001499
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001500 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001501 if (hwif == NULL) {
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001502 mate = NULL;
1503 continue;
1504 }
1505
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +02001506 ide_init_port_hw(hwif, hws[i]);
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +02001507 ide_port_apply_params(hwif);
1508
1509 if (d == NULL) {
1510 mate = NULL;
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +02001511 } else {
1512 if ((i & 1) && mate) {
1513 hwif->mate = mate;
1514 mate->mate = hwif;
1515 }
1516
1517 mate = (i & 1) ? NULL : hwif;
1518
1519 ide_init_port(hwif, i & 1, d);
1520 ide_port_cable_detect(hwif);
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +02001521 }
1522
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001523 ide_port_init_devices(hwif);
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001524 }
1525
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001526 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001527 if (hwif == NULL)
1528 continue;
Bartlomiej Zolnierkiewicz139ddfc2008-02-01 23:09:36 +01001529
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001530 if (ide_probe_port(hwif) == 0)
1531 hwif->present = 1;
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +01001532
1533 if (hwif->chipset != ide_4drives || !hwif->mate ||
Bartlomiej Zolnierkiewicz9a100f42009-02-02 20:12:21 +01001534 !hwif->mate->present) {
1535 if (ide_register_port(hwif)) {
1536 ide_disable_port(hwif);
1537 continue;
1538 }
1539 }
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +01001540
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001541 if (hwif->present)
1542 ide_port_tune_devices(hwif);
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001543 }
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001544
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001545 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001546 if (hwif == NULL)
1547 continue;
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001548
1549 if (hwif_init(hwif) == 0) {
1550 printk(KERN_INFO "%s: failed to initialize IDE "
1551 "interface\n", hwif->name);
Ian Campbell51d6ac72009-02-02 20:12:22 +01001552 device_unregister(&hwif->gendev);
1553 ide_disable_port(hwif);
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001554 continue;
1555 }
Bartlomiej Zolnierkiewiczdecdc3f2008-02-02 19:56:41 +01001556
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001557 if (hwif->present)
Elias Oltmannse415e492008-10-13 21:39:45 +02001558 if (ide_port_setup_devices(hwif) == 0) {
1559 hwif->present = 0;
1560 continue;
1561 }
1562
1563 j++;
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +02001564
Bartlomiej Zolnierkiewiczdecdc3f2008-02-02 19:56:41 +01001565 ide_acpi_init(hwif);
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001566
1567 if (hwif->present)
1568 ide_acpi_port_init_devices(hwif);
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001569 }
1570
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001571 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001572 if (hwif == NULL)
1573 continue;
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001574
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001575 if (hwif->present)
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001576 hwif_register_devices(hwif);
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001577 }
1578
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001579 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001580 if (hwif == NULL)
1581 continue;
Bartlomiej Zolnierkiewicz327617e2008-02-02 19:56:43 +01001582
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001583 ide_sysfs_register_port(hwif);
1584 ide_proc_register_port(hwif);
1585
1586 if (hwif->present)
Bartlomiej Zolnierkiewiczd9270a32008-02-02 19:56:43 +01001587 ide_proc_port_register_devices(hwif);
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001588 }
1589
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001590 return j ? 0 : -1;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001591}
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001592EXPORT_SYMBOL_GPL(ide_host_register);
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001593
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001594int ide_host_add(const struct ide_port_info *d, hw_regs_t **hws,
1595 struct ide_host **hostp)
1596{
1597 struct ide_host *host;
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001598 int rc;
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001599
1600 host = ide_host_alloc(d, hws);
1601 if (host == NULL)
1602 return -ENOMEM;
1603
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001604 rc = ide_host_register(host, d, hws);
1605 if (rc) {
1606 ide_host_free(host);
1607 return rc;
1608 }
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001609
1610 if (hostp)
1611 *hostp = host;
1612
1613 return 0;
1614}
1615EXPORT_SYMBOL_GPL(ide_host_add);
1616
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001617static void __ide_port_unregister_devices(ide_hwif_t *hwif)
1618{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001619 ide_drive_t *drive;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001620 int i;
1621
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001622 ide_port_for_each_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001623 if (drive->dev_flags & IDE_DFLAG_PRESENT) {
1624 device_unregister(&drive->gendev);
1625 wait_for_completion(&drive->gendev_rel_comp);
1626 }
1627 }
1628}
1629
1630void ide_port_unregister_devices(ide_hwif_t *hwif)
1631{
1632 mutex_lock(&ide_cfg_mtx);
1633 __ide_port_unregister_devices(hwif);
1634 hwif->present = 0;
1635 ide_port_init_devices_data(hwif);
1636 mutex_unlock(&ide_cfg_mtx);
1637}
1638EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
1639
1640/**
1641 * ide_unregister - free an IDE interface
1642 * @hwif: IDE interface
1643 *
1644 * Perform the final unregister of an IDE interface.
1645 *
1646 * Locking:
1647 * The caller must not hold the IDE locks.
1648 *
1649 * It is up to the caller to be sure there is no pending I/O here,
1650 * and that the interface will not be reopened (present/vanishing
1651 * locking isn't yet done BTW).
1652 */
1653
1654static void ide_unregister(ide_hwif_t *hwif)
1655{
1656 BUG_ON(in_interrupt());
1657 BUG_ON(irqs_disabled());
1658
1659 mutex_lock(&ide_cfg_mtx);
1660
1661 if (hwif->present) {
1662 __ide_port_unregister_devices(hwif);
1663 hwif->present = 0;
1664 }
1665
1666 ide_proc_unregister_port(hwif);
1667
1668 free_irq(hwif->irq, hwif);
1669
1670 device_unregister(hwif->portdev);
1671 device_unregister(&hwif->gendev);
1672 wait_for_completion(&hwif->gendev_rel_comp);
1673
1674 /*
1675 * Remove us from the kernel's knowledge
1676 */
1677 blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
1678 kfree(hwif->sg_table);
1679 unregister_blkdev(hwif->major, hwif->name);
1680
1681 ide_release_dma_engine(hwif);
1682
1683 mutex_unlock(&ide_cfg_mtx);
1684}
1685
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001686void ide_host_free(struct ide_host *host)
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001687{
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001688 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001689 int i;
1690
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001691 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicz9a100f42009-02-02 20:12:21 +01001692 if (hwif)
1693 ide_port_free(hwif);
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +02001694 }
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001695
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001696 kfree(host);
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001697}
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001698EXPORT_SYMBOL_GPL(ide_host_free);
1699
1700void ide_host_remove(struct ide_host *host)
1701{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001702 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001703 int i;
1704
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001705 ide_host_for_each_port(i, hwif, host) {
1706 if (hwif)
1707 ide_unregister(hwif);
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001708 }
1709
1710 ide_host_free(host);
1711}
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001712EXPORT_SYMBOL_GPL(ide_host_remove);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001713
1714void ide_port_scan(ide_hwif_t *hwif)
1715{
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +02001716 ide_port_apply_params(hwif);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001717 ide_port_cable_detect(hwif);
1718 ide_port_init_devices(hwif);
1719
1720 if (ide_probe_port(hwif) < 0)
1721 return;
1722
1723 hwif->present = 1;
1724
1725 ide_port_tune_devices(hwif);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001726 ide_port_setup_devices(hwif);
Bartlomiej Zolnierkiewicze630fcb2009-01-06 17:21:00 +01001727 ide_acpi_port_init_devices(hwif);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001728 hwif_register_devices(hwif);
1729 ide_proc_port_register_devices(hwif);
1730}
1731EXPORT_SYMBOL_GPL(ide_port_scan);