blob: 2f105f4bbdecf2670860e996aacd1b675929e304 [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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#ifdef CONFIG_IDEDISK_MULTI_MODE
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +020093 if ((max_multsect / 2) > 1)
94 id[ATA_ID_MULTSECT] = max_multsect | 0x100;
95 else
96 id[ATA_ID_MULTSECT] &= ~0x1ff;
97
98 drive->mult_req = id[ATA_ID_MULTSECT] & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#endif
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +0200100 if ((id[ATA_ID_MULTSECT] & 0x100) &&
101 (id[ATA_ID_MULTSECT] & 0xff))
Bartlomiej Zolnierkiewiczdf1f8372008-10-10 22:39:18 +0200102 drive->special.b.set_multmode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104}
105
106/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * do_identify - identify a drive
108 * @drive: drive to identify
109 * @cmd: command used
110 *
111 * Called when we have issued a drive identify command to
112 * read and parse the results. This function is run with
113 * interrupts disabled.
114 */
115
116static inline void do_identify (ide_drive_t *drive, u8 cmd)
117{
118 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200119 u16 *id = drive->id;
120 char *m = (char *)&id[ATA_ID_PROD];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 int bswap = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 /* read 512 bytes of id info */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200124 hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 drive->id_read = 1;
127 local_irq_enable();
Bartlomiej Zolnierkiewicz7b9f25b2008-02-01 23:09:28 +0100128#ifdef DEBUG
129 printk(KERN_INFO "%s: dumping identify data\n", drive->name);
130 ide_dump_identify((u8 *)id);
131#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 ide_fix_driveid(id);
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /*
135 * WIN_IDENTIFY returns little-endian info,
136 * WIN_PIDENTIFY *usually* returns little-endian info.
137 */
138 if (cmd == WIN_PIDENTIFY) {
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200139 if ((m[0] == 'N' && m[1] == 'E') || /* NEC */
140 (m[0] == 'F' && m[1] == 'X') || /* Mitsumi */
141 (m[0] == 'P' && m[1] == 'i')) /* Pioneer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /* Vertos drives may still be weird */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200143 bswap ^= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200145
146 ide_fixstring(m, ATA_ID_PROD_LEN, bswap);
147 ide_fixstring((char *)&id[ATA_ID_FW_REV], ATA_ID_FW_REV_LEN, bswap);
148 ide_fixstring((char *)&id[ATA_ID_SERNO], ATA_ID_SERNO_LEN, bswap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Tejun Heo699b0522007-11-05 21:42:25 +0100150 /* we depend on this a lot! */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200151 m[ATA_ID_PROD_LEN - 1] = '\0';
Tejun Heo699b0522007-11-05 21:42:25 +0100152
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200153 if (strstr(m, "E X A B Y T E N E S T"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 goto err_misc;
155
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200156 printk(KERN_INFO "%s: %s, ", drive->name, m);
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 drive->present = 1;
159 drive->dead = 0;
160
161 /*
162 * Check for an ATAPI device
163 */
164 if (cmd == WIN_PIDENTIFY) {
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200165 u8 type = (id[ATA_ID_CONFIG] >> 8) & 0x1f;
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200166
167 printk(KERN_CONT "ATAPI ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 switch (type) {
169 case ide_floppy:
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200170 if (!strstr(m, "CD-ROM")) {
171 if (!strstr(m, "oppy") &&
172 !strstr(m, "poyp") &&
173 !strstr(m, "ZIP"))
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200174 printk(KERN_CONT "cdrom or floppy?, assuming ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (drive->media != ide_cdrom) {
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200176 printk(KERN_CONT "FLOPPY");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 drive->removable = 1;
178 break;
179 }
180 }
181 /* Early cdrom models used zero */
182 type = ide_cdrom;
183 case ide_cdrom:
184 drive->removable = 1;
185#ifdef CONFIG_PPC
186 /* kludge for Apple PowerBook internal zip */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200187 if (!strstr(m, "CD-ROM") && strstr(m, "ZIP")) {
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200188 printk(KERN_CONT "FLOPPY");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 type = ide_floppy;
190 break;
191 }
192#endif
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200193 printk(KERN_CONT "CD/DVD-ROM");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 break;
195 case ide_tape:
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200196 printk(KERN_CONT "TAPE");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 break;
198 case ide_optical:
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200199 printk(KERN_CONT "OPTICAL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 drive->removable = 1;
201 break;
202 default:
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200203 printk(KERN_CONT "UNKNOWN (type %d)", type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 break;
205 }
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200206 printk(KERN_CONT " drive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 drive->media = type;
208 /* an ATAPI device ignores DRDY */
209 drive->ready_stat = 0;
210 return;
211 }
212
213 /*
214 * Not an ATAPI device: looks like a "regular" hard disk
215 */
Richard Purdie98109332006-02-03 03:04:55 -0800216
217 /*
218 * 0x848a = CompactFlash device
219 * These are *not* removable in Linux definition of the term
220 */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200221 if (id[ATA_ID_CONFIG] != 0x848a && (id[ATA_ID_CONFIG] & (1 << 7)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 drive->removable = 1;
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 drive->media = ide_disk;
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200225
226 printk(KERN_CONT "%s DISK drive\n",
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200227 (id[ATA_ID_CONFIG] == 0x848a) ? "CFA" : "ATA");
Bartlomiej Zolnierkiewiczcd3dbc92008-01-25 22:17:13 +0100228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return;
230
231err_misc:
232 kfree(id);
233 drive->present = 0;
234 return;
235}
236
237/**
238 * actual_try_to_identify - send ata/atapi identify
239 * @drive: drive to identify
240 * @cmd: command to use
241 *
242 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
243 * and waits for a response. It also monitors irqs while this is
244 * happening, in hope of automatically determining which one is
245 * being used by the interface.
246 *
247 * Returns: 0 device was identified
248 * 1 device timed-out (no response to identify request)
249 * 2 device aborted the command (refused to identify itself)
250 */
251
252static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
253{
254 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200255 struct ide_io_ports *io_ports = &hwif->io_ports;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200256 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100257 int use_altstatus = 0, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 unsigned long timeout;
259 u8 s = 0, a = 0;
260
261 /* take a deep breath */
262 msleep(50);
263
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200264 if (io_ports->ctl_addr) {
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200265 a = tp_ops->read_altstatus(hwif);
266 s = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100267 if ((a ^ s) & ~INDEX_STAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /* ancient Seagate drives, broken interfaces */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100269 printk(KERN_INFO "%s: probing with STATUS(0x%02x) "
270 "instead of ALTSTATUS(0x%02x)\n",
271 drive->name, s, a);
272 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* use non-intrusive polling */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100274 use_altstatus = 1;
275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 /* set features register for atapi
278 * identify command to be sure of reply
279 */
Bartlomiej Zolnierkiewicz4e658372008-07-23 19:55:53 +0200280 if (cmd == WIN_PIDENTIFY) {
281 ide_task_t task;
282
283 memset(&task, 0, sizeof(task));
284 /* disable DMA & overlap */
285 task.tf_flags = IDE_TFLAG_OUT_FEATURE;
286
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200287 tp_ops->tf_load(drive, &task);
Bartlomiej Zolnierkiewicz4e658372008-07-23 19:55:53 +0200288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 /* ask drive for ID */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200291 tp_ops->exec_command(hwif, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
294 timeout += jiffies;
295 do {
296 if (time_after(jiffies, timeout)) {
297 /* drive timed-out */
298 return 1;
299 }
300 /* give drive a breather */
301 msleep(50);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200302 s = use_altstatus ? tp_ops->read_altstatus(hwif)
303 : tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100304 } while (s & BUSY_STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /* wait for IRQ and DRQ_STAT */
307 msleep(50);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200308 s = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100309
310 if (OK_STAT(s, DRQ_STAT, BAD_R_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 unsigned long flags;
312
313 /* local CPU only; some systems need this */
314 local_irq_save(flags);
315 /* drive returned ID */
316 do_identify(drive, cmd);
317 /* drive responded with ID */
318 rc = 0;
319 /* clear drive IRQ */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200320 (void)tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 local_irq_restore(flags);
322 } else {
323 /* drive refused ID */
324 rc = 2;
325 }
326 return rc;
327}
328
329/**
330 * try_to_identify - try to identify a drive
331 * @drive: drive to probe
332 * @cmd: command to use
333 *
334 * Issue the identify command and then do IRQ probing to
335 * complete the identification when needed by finding the
336 * IRQ the drive is attached to
337 */
338
339static int try_to_identify (ide_drive_t *drive, u8 cmd)
340{
341 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200342 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 int retval;
344 int autoprobe = 0;
345 unsigned long cookie = 0;
346
347 /*
348 * Disable device irq unless we need to
349 * probe for it. Otherwise we'll get spurious
350 * interrupts during the identify-phase that
351 * the irq handler isn't expecting.
352 */
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200353 if (hwif->io_ports.ctl_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (!hwif->irq) {
355 autoprobe = 1;
356 cookie = probe_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200358 tp_ops->set_irq(hwif, autoprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360
361 retval = actual_try_to_identify(drive, cmd);
362
363 if (autoprobe) {
364 int irq;
Bartlomiej Zolnierkiewicz81ca6912008-01-26 20:13:08 +0100365
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200366 tp_ops->set_irq(hwif, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 /* clear drive IRQ */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200368 (void)tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 udelay(5);
370 irq = probe_irq_off(cookie);
371 if (!hwif->irq) {
372 if (irq > 0) {
373 hwif->irq = irq;
374 } else {
375 /* Mmmm.. multiple IRQs..
376 * don't know which was ours
377 */
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200378 printk(KERN_ERR "%s: IRQ probe failed (0x%lx)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 drive->name, cookie);
380 }
381 }
382 }
383 return retval;
384}
385
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100386static int ide_busy_sleep(ide_hwif_t *hwif)
387{
388 unsigned long timeout = jiffies + WAIT_WORSTCASE;
389 u8 stat;
390
391 do {
392 msleep(50);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200393 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100394 if ((stat & BUSY_STAT) == 0)
395 return 0;
396 } while (time_before(jiffies, timeout));
397
398 return 1;
399}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200401static u8 ide_read_device(ide_drive_t *drive)
402{
403 ide_task_t task;
404
405 memset(&task, 0, sizeof(task));
406 task.tf_flags = IDE_TFLAG_IN_DEVICE;
407
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200408 drive->hwif->tp_ops->tf_read(drive, &task);
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200409
410 return task.tf.device;
411}
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413/**
414 * do_probe - probe an IDE device
415 * @drive: drive to probe
416 * @cmd: command to use
417 *
418 * do_probe() has the difficult job of finding a drive if it exists,
419 * without getting hung up if it doesn't exist, without trampling on
420 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
421 *
422 * If a drive is "known" to exist (from CMOS or kernel parameters),
423 * but does not respond right away, the probe will "hang in there"
424 * for the maximum wait time (about 30 seconds), otherwise it will
425 * exit much more quickly.
426 *
427 * Returns: 0 device was identified
428 * 1 device timed-out (no response to identify request)
429 * 2 device aborted the command (refused to identify itself)
430 * 3 bad status from device (possible for ATAPI drives)
431 * 4 probe was not attempted because failure was obvious
432 */
433
434static int do_probe (ide_drive_t *drive, u8 cmd)
435{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200437 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100438 int rc;
439 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (drive->present) {
442 /* avoid waiting for inappropriate probes */
443 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
444 return 4;
445 }
446#ifdef DEBUG
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200447 printk(KERN_INFO "probing for %s: present=%d, media=%d, probetype=%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 drive->name, drive->present, drive->media,
449 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
450#endif
451
452 /* needed for some systems
453 * (e.g. crw9624 as drive0 with disk as slave)
454 */
455 msleep(50);
456 SELECT_DRIVE(drive);
457 msleep(50);
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200458
459 if (ide_read_device(drive) != drive->select.all && !drive->present) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (drive->select.b.unit != 0) {
461 /* exit with drive0 selected */
462 SELECT_DRIVE(&hwif->drives[0]);
463 /* allow BUSY_STAT to assert & clear */
464 msleep(50);
465 }
466 /* no i/f present: mmm.. this should be a 4 -ml */
467 return 3;
468 }
469
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200470 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100471
472 if (OK_STAT(stat, READY_STAT, BUSY_STAT) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 drive->present || cmd == WIN_PIDENTIFY) {
474 /* send cmd and wait */
475 if ((rc = try_to_identify(drive, cmd))) {
476 /* failed: try again */
477 rc = try_to_identify(drive,cmd);
478 }
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100479
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200480 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100481
482 if (stat == (BUSY_STAT | READY_STAT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return 4;
484
Bartlomiej Zolnierkiewiczcc121752008-04-27 15:38:24 +0200485 if (rc == 1 && cmd == WIN_PIDENTIFY) {
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100486 printk(KERN_ERR "%s: no response (status = 0x%02x), "
487 "resetting drive\n", drive->name, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 msleep(50);
Bartlomiej Zolnierkiewicze81a3bd2008-07-15 21:21:48 +0200489 SELECT_DRIVE(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 msleep(50);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200491 tp_ops->exec_command(hwif, WIN_SRST);
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100492 (void)ide_busy_sleep(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 rc = try_to_identify(drive, cmd);
494 }
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100495
496 /* ensure drive IRQ is clear */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200497 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (rc == 1)
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100500 printk(KERN_ERR "%s: no response (status = 0x%02x)\n",
501 drive->name, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 } else {
503 /* not present or maybe ATAPI */
504 rc = 3;
505 }
506 if (drive->select.b.unit != 0) {
507 /* exit with drive0 selected */
508 SELECT_DRIVE(&hwif->drives[0]);
509 msleep(50);
510 /* ensure drive irq is clear */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200511 (void)tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 return rc;
514}
515
516/*
517 *
518 */
519static void enable_nest (ide_drive_t *drive)
520{
521 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200522 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100523 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200525 printk(KERN_INFO "%s: enabling %s -- ",
526 hwif->name, (char *)&drive->id[ATA_ID_PROD]);
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 SELECT_DRIVE(drive);
529 msleep(50);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200530 tp_ops->exec_command(hwif, EXABYTE_ENABLE_NEST);
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100531
532 if (ide_busy_sleep(hwif)) {
533 printk(KERN_CONT "failed (timeout)\n");
534 return;
535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 msleep(50);
538
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200539 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100540
541 if (!OK_STAT(stat, 0, BAD_STAT))
542 printk(KERN_CONT "failed (status = 0x%02x)\n", stat);
543 else
544 printk(KERN_CONT "success\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 /* if !(success||timed-out) */
547 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
548 /* look for ATAPI device */
549 (void) do_probe(drive, WIN_PIDENTIFY);
550 }
551}
552
553/**
554 * probe_for_drives - upper level drive probe
555 * @drive: drive to probe for
556 *
557 * probe_for_drive() tests for existence of a given drive using do_probe()
558 * and presents things to the user as needed.
559 *
560 * Returns: 0 no device was found
561 * 1 device was found (note: drive->present might
562 * still be 0)
563 */
564
565static inline u8 probe_for_drive (ide_drive_t *drive)
566{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200567 char *m;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /*
570 * In order to keep things simple we have an id
571 * block for all drives at all times. If the device
572 * is pre ATA or refuses ATA/ATAPI identify we
573 * will add faked data to this.
574 *
575 * Also note that 0 everywhere means "can't do X"
576 */
577
Deepak Saxenaf5e3c2f2005-11-07 01:01:25 -0800578 drive->id = kzalloc(SECTOR_WORDS *4, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 drive->id_read = 0;
580 if(drive->id == NULL)
581 {
582 printk(KERN_ERR "ide: out of memory for id data.\n");
583 return 0;
584 }
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200585
586 m = (char *)&drive->id[ATA_ID_PROD];
587 strcpy(m, "UNKNOWN");
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* skip probing? */
590 if (!drive->noprobe)
591 {
592 /* if !(success||timed-out) */
593 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
594 /* look for ATAPI device */
595 (void) do_probe(drive, WIN_PIDENTIFY);
596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (!drive->present)
598 /* drive not found */
599 return 0;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200600
601 if (strstr(m, "E X A B Y T E N E S T"))
Alan Cox78595572007-07-03 22:28:35 +0200602 enable_nest(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 /* identification failed? */
605 if (!drive->id_read) {
606 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);
615 drive->present = 0;
616 }
617 }
618 /* drive was found */
619 }
620 if(!drive->present)
621 return 0;
622 /* The drive wasn't being helpful. Add generic info only */
623 if (drive->id_read == 0) {
624 generic_id(drive);
625 return 1;
626 }
627
628 if (drive->media == ide_disk) {
629 ide_disk_init_chs(drive);
630 ide_disk_init_mult_count(drive);
631 }
632
633 return drive->present;
634}
635
Pavel Machekfc410692008-07-23 19:56:02 +0200636static void hwif_release_dev(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
639
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800640 complete(&hwif->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200643static int ide_register_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Randy Dunlap349ae232006-10-03 01:14:23 -0700645 int ret;
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /* register with global device tree */
648 strlcpy(hwif->gendev.bus_id,hwif->name,BUS_ID_SIZE);
649 hwif->gendev.driver_data = hwif;
650 if (hwif->gendev.parent == NULL) {
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100651 if (hwif->dev)
652 hwif->gendev.parent = hwif->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 else
654 /* Would like to do = &device_legacy */
655 hwif->gendev.parent = NULL;
656 }
657 hwif->gendev.release = hwif_release_dev;
Randy Dunlap349ae232006-10-03 01:14:23 -0700658 ret = device_register(&hwif->gendev);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200659 if (ret < 0) {
Randy Dunlap349ae232006-10-03 01:14:23 -0700660 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200661 __func__, ret);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200662 goto out;
663 }
664
Greg Kroah-Hartman716ad872008-05-16 17:55:12 -0700665 hwif->portdev = device_create_drvdata(ide_port_class, &hwif->gendev,
666 MKDEV(0, 0), hwif, hwif->name);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200667 if (IS_ERR(hwif->portdev)) {
668 ret = PTR_ERR(hwif->portdev);
669 device_unregister(&hwif->gendev);
670 }
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200671out:
672 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Bartlomiej Zolnierkiewiczc860a8f2008-02-01 23:09:34 +0100675/**
676 * ide_port_wait_ready - wait for port to become ready
677 * @hwif: IDE port
678 *
679 * This is needed on some PPCs and a bunch of BIOS-less embedded
680 * platforms. Typical cases are:
681 *
682 * - The firmware hard reset the disk before booting the kernel,
683 * the drive is still doing it's poweron-reset sequence, that
684 * can take up to 30 seconds.
685 *
686 * - The firmware does nothing (or no firmware), the device is
687 * still in POST state (same as above actually).
688 *
689 * - Some CD/DVD/Writer combo drives tend to drive the bus during
690 * their reset sequence even when they are non-selected slave
691 * devices, thus preventing discovery of the main HD.
692 *
693 * Doing this wait-for-non-busy should not harm any existing
694 * configuration and fix some issues like the above.
695 *
696 * BenH.
697 *
698 * Returns 0 on success, error code (< 0) otherwise.
699 */
700
701static int ide_port_wait_ready(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Jonas Stare82661052007-11-27 21:35:53 +0100703 int unit, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
706
707 /* Let HW settle down a bit from whatever init state we
708 * come from */
709 mdelay(2);
710
711 /* Wait for BSY bit to go away, spec timeout is 30 seconds,
712 * I know of at least one disk who takes 31 seconds, I use 35
713 * here to be safe
714 */
715 rc = ide_wait_not_busy(hwif, 35000);
716 if (rc)
717 return rc;
718
719 /* Now make sure both master & slave are ready */
Jonas Stare82661052007-11-27 21:35:53 +0100720 for (unit = 0; unit < MAX_DRIVES; unit++) {
721 ide_drive_t *drive = &hwif->drives[unit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Jonas Stare82661052007-11-27 21:35:53 +0100723 /* Ignore disks that we will not probe for later. */
724 if (!drive->noprobe || drive->present) {
725 SELECT_DRIVE(drive);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200726 hwif->tp_ops->set_irq(hwif, 1);
Jonas Stare82661052007-11-27 21:35:53 +0100727 mdelay(2);
728 rc = ide_wait_not_busy(hwif, 35000);
729 if (rc)
730 goto out;
731 } else
732 printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
733 drive->name);
734 }
735out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /* Exit function with master reselected (let's be sane) */
Jonas Stare82661052007-11-27 21:35:53 +0100737 if (unit)
738 SELECT_DRIVE(&hwif->drives[0]);
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return rc;
741}
742
743/**
744 * ide_undecoded_slave - look for bad CF adapters
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200745 * @dev1: slave device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 *
747 * Analyse the drives on the interface and attempt to decide if we
748 * have the same drive viewed twice. This occurs with crap CF adapters
749 * and PCMCIA sometimes.
750 */
751
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200752void ide_undecoded_slave(ide_drive_t *dev1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200754 ide_drive_t *dev0 = &dev1->hwif->drives[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200756 if ((dev1->dn & 1) == 0 || dev0->present == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return;
758
759 /* If the models don't match they are not the same product */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200760 if (strcmp((char *)&dev0->id[ATA_ID_PROD],
761 (char *)&dev1->id[ATA_ID_PROD]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return;
763
764 /* Serial numbers do not match */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200765 if (strncmp((char *)&dev0->id[ATA_ID_SERNO],
766 (char *)&dev1->id[ATA_ID_SERNO], ATA_ID_SERNO_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return;
768
769 /* No serial number, thankfully very rare for CF */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200770 if (*(char *)&dev0->id[ATA_ID_SERNO] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return;
772
773 /* Appears to be an IDE flash adapter with decode bugs */
774 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
775
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200776 dev1->present = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
779EXPORT_SYMBOL_GPL(ide_undecoded_slave);
780
Bartlomiej Zolnierkiewicz9d501522008-02-01 23:09:36 +0100781static int ide_probe_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 unsigned long flags;
784 unsigned int irqd;
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100785 int unit, rc = -ENODEV;
786
787 BUG_ON(hwif->present);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Bartlomiej Zolnierkiewicze53cd452008-04-26 22:25:16 +0200789 if (hwif->drives[0].noprobe && hwif->drives[1].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
800 local_irq_set(flags);
801
Bartlomiej Zolnierkiewiczc860a8f2008-02-01 23:09:34 +0100802 if (ide_port_wait_ready(hwif) == -EBUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
804
805 /*
Bartlomiej Zolnierkiewiczf367bed2008-03-29 19:48:21 +0100806 * Second drive should only exist if first drive was found,
807 * but a lot of cdrom drives are configured as single slaves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 */
Bartlomiej Zolnierkiewiczf367bed2008-03-29 19:48:21 +0100809 for (unit = 0; unit < MAX_DRIVES; ++unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 ide_drive_t *drive = &hwif->drives[unit];
811 drive->dn = (hwif->channel ? 2 : 0) + unit;
812 (void) probe_for_drive(drive);
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100813 if (drive->present)
814 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
Bartlomiej Zolnierkiewicze460a592008-04-27 15:38:24 +0200816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 local_irq_restore(flags);
Bartlomiej Zolnierkiewicze460a592008-04-27 15:38:24 +0200818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /*
820 * Use cached IRQ number. It might be (and is...) changed by probe
821 * code above
822 */
823 if (irqd)
824 enable_irq(irqd);
825
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100826 return rc;
Bartlomiej Zolnierkiewicze84e7ea2008-02-01 23:09:36 +0100827}
828
829static void ide_port_tune_devices(ide_hwif_t *hwif)
830{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200831 const struct ide_port_ops *port_ops = hwif->port_ops;
Bartlomiej Zolnierkiewicze84e7ea2008-02-01 23:09:36 +0100832 int unit;
833
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100834 for (unit = 0; unit < MAX_DRIVES; unit++) {
835 ide_drive_t *drive = &hwif->drives[unit];
836
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200837 if (drive->present && port_ops && port_ops->quirkproc)
838 port_ops->quirkproc(drive);
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100839 }
Bartlomiej Zolnierkiewicz0380dad2007-06-08 15:14:29 +0200840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 for (unit = 0; unit < MAX_DRIVES; ++unit) {
842 ide_drive_t *drive = &hwif->drives[unit];
843
844 if (drive->present) {
Bartlomiej Zolnierkiewicz207daea2008-04-27 15:38:29 +0200845 ide_set_max_pio(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 drive->nice1 = 1;
848
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200849 if (hwif->dma_ops)
Bartlomiej Zolnierkiewicz8c0697c2007-10-16 22:29:58 +0200850 ide_set_dma(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852 }
Kumar Gala208a08f2006-03-24 03:18:21 -0800853
854 for (unit = 0; unit < MAX_DRIVES; ++unit) {
855 ide_drive_t *drive = &hwif->drives[unit];
856
Bartlomiej Zolnierkiewicz807b90d2008-02-02 19:56:40 +0100857 if (hwif->host_flags & IDE_HFLAG_NO_IO_32BIT)
Kumar Gala208a08f2006-03-24 03:18:21 -0800858 drive->no_io_32bit = 1;
859 else
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200860 drive->no_io_32bit = drive->id[ATA_ID_DWORD_IO] ? 1 : 0;
Kumar Gala208a08f2006-03-24 03:18:21 -0800861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864#if MAX_HWIFS > 1
865/*
866 * save_match() is used to simplify logic in init_irq() below.
867 *
868 * A loophole here is that we may not know about a particular
869 * hwif's irq until after that hwif is actually probed/initialized..
870 * This could be a problem for the case where an hwif is on a
871 * dual interface that requires serialization (eg. cmd640) and another
872 * hwif using one of the same irqs is initialized beforehand.
873 *
874 * This routine detects and reports such situations, but does not fix them.
875 */
876static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
877{
878 ide_hwif_t *m = *match;
879
880 if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
881 if (!new->hwgroup)
882 return;
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200883 printk(KERN_WARNING "%s: potential IRQ problem with %s and %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 hwif->name, new->name, m->name);
885 }
886 if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
887 *match = new;
888}
889#endif /* MAX_HWIFS > 1 */
890
891/*
892 * init request queue
893 */
894static int ide_init_queue(ide_drive_t *drive)
895{
Jens Axboe165125e2007-07-24 09:28:11 +0200896 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 ide_hwif_t *hwif = HWIF(drive);
898 int max_sectors = 256;
899 int max_sg_entries = PRD_ENTRIES;
900
901 /*
902 * Our default set up assumes the normal IDE case,
903 * that is 64K segmenting, standard PRD setup
904 * and LBA28. Some drivers then impose their own
905 * limits and LBA48 we could raise it but as yet
906 * do not.
907 */
Christoph Lameter19460892005-06-23 00:08:19 -0700908
Ravikiran G Thirumalai556e58f2005-08-04 12:53:26 -0700909 q = blk_init_queue_node(do_ide_request, &ide_lock, hwif_to_node(hwif));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (!q)
911 return 1;
912
913 q->queuedata = drive;
914 blk_queue_segment_boundary(q, 0xffff);
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (hwif->rqsize < max_sectors)
917 max_sectors = hwif->rqsize;
918 blk_queue_max_sectors(q, max_sectors);
919
920#ifdef CONFIG_PCI
921 /* When we have an IOMMU, we may have a problem where pci_map_sg()
922 * creates segments that don't completely match our boundary
923 * requirements and thus need to be broken up again. Because it
924 * doesn't align properly either, we may actually have to break up
925 * to more segments than what was we got in the first place, a max
926 * worst case is twice as many.
927 * This will be fixed once we teach pci_map_sg() about our boundary
928 * requirements, hopefully soon. *FIXME*
929 */
930 if (!PCI_DMA_BUS_IS_PHYS)
931 max_sg_entries >>= 1;
932#endif /* CONFIG_PCI */
933
934 blk_queue_max_hw_segments(q, max_sg_entries);
935 blk_queue_max_phys_segments(q, max_sg_entries);
936
937 /* assign drive queue */
938 drive->queue = q;
939
940 /* needs drive->queue to be set */
941 ide_toggle_bounce(drive, 1);
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return 0;
944}
945
Bartlomiej Zolnierkiewicz0947e0d2008-02-02 19:56:41 +0100946static void ide_add_drive_to_hwgroup(ide_drive_t *drive)
947{
948 ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
949
950 spin_lock_irq(&ide_lock);
951 if (!hwgroup->drive) {
952 /* first drive for hwgroup. */
953 drive->next = drive;
954 hwgroup->drive = drive;
955 hwgroup->hwif = HWIF(hwgroup->drive);
956 } else {
957 drive->next = hwgroup->drive->next;
958 hwgroup->drive->next = drive;
959 }
960 spin_unlock_irq(&ide_lock);
961}
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963/*
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100964 * For any present drive:
965 * - allocate the block device queue
966 * - link drive into the hwgroup
967 */
968static void ide_port_setup_devices(ide_hwif_t *hwif)
969{
970 int i;
971
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +0200972 mutex_lock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100973 for (i = 0; i < MAX_DRIVES; i++) {
974 ide_drive_t *drive = &hwif->drives[i];
975
976 if (!drive->present)
977 continue;
978
979 if (ide_init_queue(drive)) {
980 printk(KERN_ERR "ide: failed to init %s\n",
981 drive->name);
982 continue;
983 }
984
985 ide_add_drive_to_hwgroup(drive);
986 }
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +0200987 mutex_unlock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100988}
989
Bartlomiej Zolnierkiewiczaf1cbba2008-07-23 19:55:58 +0200990static ide_hwif_t *ide_ports[MAX_HWIFS];
991
Bartlomiej Zolnierkiewicz60591432008-07-23 19:55:58 +0200992void ide_remove_port_from_hwgroup(ide_hwif_t *hwif)
993{
994 ide_hwgroup_t *hwgroup = hwif->hwgroup;
995
Bartlomiej Zolnierkiewiczaf1cbba2008-07-23 19:55:58 +0200996 ide_ports[hwif->index] = NULL;
997
Bartlomiej Zolnierkiewicz60591432008-07-23 19:55:58 +0200998 spin_lock_irq(&ide_lock);
999 /*
1000 * Remove us from the hwgroup, and free
1001 * the hwgroup if we were the only member
1002 */
1003 if (hwif->next == hwif) {
1004 BUG_ON(hwgroup->hwif != hwif);
1005 kfree(hwgroup);
1006 } else {
1007 /* There is another interface in hwgroup.
1008 * Unlink us, and set hwgroup->drive and ->hwif to
1009 * something sane.
1010 */
1011 ide_hwif_t *g = hwgroup->hwif;
1012
1013 while (g->next != hwif)
1014 g = g->next;
1015 g->next = hwif->next;
1016 if (hwgroup->hwif == hwif) {
1017 /* Chose a random hwif for hwgroup->hwif.
1018 * It's guaranteed that there are no drives
1019 * left in the hwgroup.
1020 */
1021 BUG_ON(hwgroup->drive != NULL);
1022 hwgroup->hwif = g;
1023 }
1024 BUG_ON(hwgroup->hwif == hwif);
1025 }
1026 spin_unlock_irq(&ide_lock);
1027}
1028
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +01001029/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 * This routine sets up the irq for an ide interface, and creates a new
1031 * hwgroup for the irq/hwif if none was previously assigned.
1032 *
1033 * Much of the code is for correctly detecting/handling irq sharing
1034 * and irq serialization situations. This is somewhat complex because
1035 * it handles static as well as dynamic (PCMCIA) IDE interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 */
1037static int init_irq (ide_hwif_t *hwif)
1038{
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +02001039 struct ide_io_ports *io_ports = &hwif->io_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 unsigned int index;
1041 ide_hwgroup_t *hwgroup;
1042 ide_hwif_t *match = NULL;
1043
1044
1045 BUG_ON(in_interrupt());
1046 BUG_ON(irqs_disabled());
Ravikiran G Thirumalai556e58f2005-08-04 12:53:26 -07001047 BUG_ON(hwif == NULL);
1048
Matthias Kaehlckeef298882007-07-09 23:17:55 +02001049 mutex_lock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 hwif->hwgroup = NULL;
1051#if MAX_HWIFS > 1
1052 /*
1053 * Group up with any other hwifs that share our irq(s).
1054 */
1055 for (index = 0; index < MAX_HWIFS; index++) {
Bartlomiej Zolnierkiewiczaf1cbba2008-07-23 19:55:58 +02001056 ide_hwif_t *h = ide_ports[index];
1057
1058 if (h && h->hwgroup) { /* scan only initialized ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (hwif->irq == h->irq) {
1060 hwif->sharing_irq = h->sharing_irq = 1;
1061 if (hwif->chipset != ide_pci ||
1062 h->chipset != ide_pci) {
1063 save_match(hwif, h, &match);
1064 }
1065 }
1066 if (hwif->serialized) {
1067 if (hwif->mate && hwif->mate->irq == h->irq)
1068 save_match(hwif, h, &match);
1069 }
1070 if (h->serialized) {
1071 if (h->mate && hwif->irq == h->mate->irq)
1072 save_match(hwif, h, &match);
1073 }
1074 }
1075 }
1076#endif /* MAX_HWIFS > 1 */
1077 /*
1078 * If we are still without a hwgroup, then form a new one
1079 */
1080 if (match) {
1081 hwgroup = match->hwgroup;
1082 hwif->hwgroup = hwgroup;
1083 /*
1084 * Link us into the hwgroup.
1085 * This must be done early, do ensure that unexpected_intr
1086 * can find the hwif and prevent irq storms.
1087 * No drives are attached to the new hwif, choose_drive
1088 * can't do anything stupid (yet).
1089 * Add ourself as the 2nd entry to the hwgroup->hwif
1090 * linked list, the first entry is the hwif that owns
1091 * hwgroup->handler - do not change that.
1092 */
1093 spin_lock_irq(&ide_lock);
1094 hwif->next = hwgroup->hwif->next;
1095 hwgroup->hwif->next = hwif;
Bartlomiej Zolnierkiewiczcae5c822008-02-01 23:09:36 +01001096 BUG_ON(hwif->next == hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 spin_unlock_irq(&ide_lock);
1098 } else {
Bartlomiej Zolnierkiewicz422278e2008-02-01 23:09:35 +01001099 hwgroup = kmalloc_node(sizeof(*hwgroup), GFP_KERNEL|__GFP_ZERO,
1100 hwif_to_node(hwif));
1101 if (hwgroup == NULL)
1102 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 hwif->hwgroup = hwgroup;
Bartlomiej Zolnierkiewicz422278e2008-02-01 23:09:35 +01001105 hwgroup->hwif = hwif->next = hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 init_timer(&hwgroup->timer);
1108 hwgroup->timer.function = &ide_timer_expiry;
1109 hwgroup->timer.data = (unsigned long) hwgroup;
1110 }
1111
Bartlomiej Zolnierkiewiczaf1cbba2008-07-23 19:55:58 +02001112 ide_ports[hwif->index] = hwif;
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 /*
1115 * Allocate the irq, if not already obtained for another hwif
1116 */
1117 if (!match || match->irq != hwif->irq) {
Bartlomiej Zolnierkiewicz7b5da4b2008-01-25 22:17:08 +01001118 int sa = 0;
Adrian Bunk7b892802008-02-06 01:36:29 -08001119#if defined(__mc68000__)
Thomas Gleixner362537b2006-07-01 19:29:34 -07001120 sa = IRQF_SHARED;
Bartlomiej Zolnierkiewicze1771e22008-02-11 00:32:15 +01001121#endif /* __mc68000__ */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Bartlomiej Zolnierkiewicz7b5da4b2008-01-25 22:17:08 +01001123 if (IDE_CHIPSET_IS_PCI(hwif->chipset))
Thomas Gleixner362537b2006-07-01 19:29:34 -07001124 sa = IRQF_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +02001126 if (io_ports->ctl_addr)
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001127 hwif->tp_ops->set_irq(hwif, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
1130 goto out_unlink;
1131 }
1132
Bartlomiej Zolnierkiewicz8a0e7e12008-02-02 19:56:41 +01001133 if (!hwif->rqsize) {
1134 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
1135 (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
1136 hwif->rqsize = 256;
1137 else
1138 hwif->rqsize = 65536;
1139 }
1140
Adrian Bunk7b892802008-02-06 01:36:29 -08001141#if !defined(__mc68000__)
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +02001142 printk(KERN_INFO "%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +02001143 io_ports->data_addr, io_ports->status_addr,
1144 io_ports->ctl_addr, hwif->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145#else
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +02001146 printk(KERN_INFO "%s at 0x%08lx on irq %d", hwif->name,
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +02001147 io_ports->data_addr, hwif->irq);
Adrian Bunk7b892802008-02-06 01:36:29 -08001148#endif /* __mc68000__ */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (match)
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +02001150 printk(KERN_CONT " (%sed with %s)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 hwif->sharing_irq ? "shar" : "serializ", match->name);
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +02001152 printk(KERN_CONT "\n");
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +01001153
Matthias Kaehlckeef298882007-07-09 23:17:55 +02001154 mutex_unlock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return 0;
1156out_unlink:
Bartlomiej Zolnierkiewiczfbd13082008-02-01 23:09:36 +01001157 ide_remove_port_from_hwgroup(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158out_up:
Matthias Kaehlckeef298882007-07-09 23:17:55 +02001159 mutex_unlock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return 1;
1161}
1162
1163static int ata_lock(dev_t dev, void *data)
1164{
1165 /* FIXME: we want to pin hwif down */
1166 return 0;
1167}
1168
1169static struct kobject *ata_probe(dev_t dev, int *part, void *data)
1170{
1171 ide_hwif_t *hwif = data;
1172 int unit = *part >> PARTN_BITS;
1173 ide_drive_t *drive = &hwif->drives[unit];
1174 if (!drive->present)
1175 return NULL;
1176
1177 if (drive->media == ide_disk)
1178 request_module("ide-disk");
1179 if (drive->scsi)
1180 request_module("ide-scsi");
1181 if (drive->media == ide_cdrom || drive->media == ide_optical)
1182 request_module("ide-cd");
1183 if (drive->media == ide_tape)
1184 request_module("ide-tape");
1185 if (drive->media == ide_floppy)
1186 request_module("ide-floppy");
1187
1188 return NULL;
1189}
1190
1191static struct kobject *exact_match(dev_t dev, int *part, void *data)
1192{
1193 struct gendisk *p = data;
1194 *part &= (1 << PARTN_BITS) - 1;
Tejun Heoed9e1982008-08-25 19:56:05 +09001195 return &disk_to_dev(p)->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
1198static int exact_lock(dev_t dev, void *data)
1199{
1200 struct gendisk *p = data;
1201
1202 if (!get_disk(p))
1203 return -1;
1204 return 0;
1205}
1206
1207void ide_register_region(struct gendisk *disk)
1208{
1209 blk_register_region(MKDEV(disk->major, disk->first_minor),
1210 disk->minors, NULL, exact_match, exact_lock, disk);
1211}
1212
1213EXPORT_SYMBOL_GPL(ide_register_region);
1214
1215void ide_unregister_region(struct gendisk *disk)
1216{
1217 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
1218 disk->minors);
1219}
1220
1221EXPORT_SYMBOL_GPL(ide_unregister_region);
1222
1223void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
1224{
1225 ide_hwif_t *hwif = drive->hwif;
1226 unsigned int unit = (drive->select.all >> 4) & 1;
1227
1228 disk->major = hwif->major;
1229 disk->first_minor = unit << PARTN_BITS;
1230 sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
1231 disk->queue = drive->queue;
1232}
1233
1234EXPORT_SYMBOL_GPL(ide_init_disk);
1235
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001236static void ide_remove_drive_from_hwgroup(ide_drive_t *drive)
1237{
1238 ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
1239
1240 if (drive == drive->next) {
1241 /* special case: last drive from hwgroup. */
1242 BUG_ON(hwgroup->drive != drive);
1243 hwgroup->drive = NULL;
1244 } else {
1245 ide_drive_t *walk;
1246
1247 walk = hwgroup->drive;
1248 while (walk->next != drive)
1249 walk = walk->next;
1250 walk->next = drive->next;
1251 if (hwgroup->drive == drive) {
1252 hwgroup->drive = drive->next;
1253 hwgroup->hwif = hwgroup->drive->hwif;
1254 }
1255 }
1256 BUG_ON(hwgroup->drive == drive);
1257}
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259static void drive_release_dev (struct device *dev)
1260{
1261 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1262
Bartlomiej Zolnierkiewicz5b0c4b32008-04-18 00:46:22 +02001263 ide_proc_unregister_device(drive);
1264
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001265 spin_lock_irq(&ide_lock);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001266 ide_remove_drive_from_hwgroup(drive);
Jesper Juhl6044ec82005-11-07 01:01:32 -08001267 kfree(drive->id);
1268 drive->id = NULL;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001269 drive->present = 0;
1270 /* Messed up locking ... */
1271 spin_unlock_irq(&ide_lock);
1272 blk_cleanup_queue(drive->queue);
1273 spin_lock_irq(&ide_lock);
1274 drive->queue = NULL;
1275 spin_unlock_irq(&ide_lock);
1276
Aleksey Makarovf36d4022006-01-09 15:59:27 -08001277 complete(&drive->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280static int hwif_init(ide_hwif_t *hwif)
1281{
1282 int old_irq;
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (!hwif->irq) {
Bartlomiej Zolnierkiewicza861beb2008-07-08 19:27:22 +02001285 hwif->irq = __ide_default_irq(hwif->io_ports.data_addr);
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +02001286 if (!hwif->irq) {
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +02001287 printk(KERN_ERR "%s: disabled, no IRQ\n", hwif->name);
Bartlomiej Zolnierkiewicz48535652008-02-01 23:09:34 +01001288 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (register_blkdev(hwif->major, hwif->name))
1293 return 0;
1294
1295 if (!hwif->sg_max_nents)
1296 hwif->sg_max_nents = PRD_ENTRIES;
1297
Jens Axboe45711f12007-10-22 21:19:53 +02001298 hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 GFP_KERNEL);
1300 if (!hwif->sg_table) {
1301 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1302 goto out;
1303 }
Jens Axboe45711f12007-10-22 21:19:53 +02001304
1305 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 if (init_irq(hwif) == 0)
1308 goto done;
1309
1310 old_irq = hwif->irq;
1311 /*
1312 * It failed to initialise. Find the default IRQ for
1313 * this port and try that.
1314 */
Bartlomiej Zolnierkiewicza861beb2008-07-08 19:27:22 +02001315 hwif->irq = __ide_default_irq(hwif->io_ports.data_addr);
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +02001316 if (!hwif->irq) {
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +02001317 printk(KERN_ERR "%s: disabled, unable to get IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 hwif->name, old_irq);
1319 goto out;
1320 }
1321 if (init_irq(hwif)) {
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +02001322 printk(KERN_ERR "%s: probed IRQ %d and default IRQ %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 hwif->name, old_irq, hwif->irq);
1324 goto out;
1325 }
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +02001326 printk(KERN_WARNING "%s: probed IRQ %d failed, using default\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 hwif->name, hwif->irq);
1328
1329done:
Bartlomiej Zolnierkiewicz3a4e7c92008-02-02 19:56:40 +01001330 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1331 THIS_MODULE, ata_probe, ata_lock, hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return 1;
1333
1334out:
1335 unregister_blkdev(hwif->major, hwif->name);
1336 return 0;
1337}
1338
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001339static void hwif_register_devices(ide_hwif_t *hwif)
1340{
1341 unsigned int i;
1342
1343 for (i = 0; i < MAX_DRIVES; i++) {
1344 ide_drive_t *drive = &hwif->drives[i];
Bartlomiej Zolnierkiewiczc5d70cc2008-02-02 19:56:41 +01001345 struct device *dev = &drive->gendev;
1346 int ret;
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001347
Bartlomiej Zolnierkiewiczc5d70cc2008-02-02 19:56:41 +01001348 if (!drive->present)
1349 continue;
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001350
Bartlomiej Zolnierkiewiczc5d70cc2008-02-02 19:56:41 +01001351 ide_add_generic_settings(drive);
1352
1353 snprintf(dev->bus_id, BUS_ID_SIZE, "%u.%u", hwif->index, i);
1354 dev->parent = &hwif->gendev;
1355 dev->bus = &ide_bus_type;
1356 dev->driver_data = drive;
1357 dev->release = drive_release_dev;
1358
1359 ret = device_register(dev);
1360 if (ret < 0)
1361 printk(KERN_WARNING "IDE: %s: device_register error: "
1362 "%d\n", __func__, ret);
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001363 }
1364}
1365
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001366static void ide_port_init_devices(ide_hwif_t *hwif)
1367{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001368 const struct ide_port_ops *port_ops = hwif->port_ops;
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001369 int i;
1370
1371 for (i = 0; i < MAX_DRIVES; i++) {
1372 ide_drive_t *drive = &hwif->drives[i];
1373
1374 if (hwif->host_flags & IDE_HFLAG_IO_32BIT)
1375 drive->io_32bit = 1;
1376 if (hwif->host_flags & IDE_HFLAG_UNMASK_IRQS)
1377 drive->unmask = 1;
Bartlomiej Zolnierkiewicz807b90d2008-02-02 19:56:40 +01001378 if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS)
1379 drive->no_unmask = 1;
Bartlomiej Zolnierkiewicz1f2cf8b2008-02-02 19:56:40 +01001380
Bartlomiej Zolnierkiewicze6d95bd2008-07-16 20:33:42 +02001381 if (port_ops && port_ops->init_dev)
1382 port_ops->init_dev(drive);
1383 }
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001384}
1385
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001386static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
1387 const struct ide_port_info *d)
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001388{
Adrian Bunkb7691642008-06-10 20:56:36 +02001389 hwif->channel = port;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001390
1391 if (d->chipset)
1392 hwif->chipset = d->chipset;
1393
1394 if (d->init_iops)
1395 d->init_iops(hwif);
1396
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001397 if ((!hwif->irq && (d->host_flags & IDE_HFLAG_LEGACY_IRQS)) ||
1398 (d->host_flags & IDE_HFLAG_FORCE_LEGACY_IRQS))
1399 hwif->irq = port ? 15 : 14;
1400
Bartlomiej Zolnierkiewicz23f8e4b2008-05-01 14:08:51 +02001401 /* ->host_flags may be set by ->init_iops (or even earlier...) */
1402 hwif->host_flags |= d->host_flags;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001403 hwif->pio_mask = d->pio_mask;
1404
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001405 if (d->tp_ops)
1406 hwif->tp_ops = d->tp_ops;
1407
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001408 /* ->set_pio_mode for DTC2278 is currently limited to port 0 */
1409 if (hwif->chipset != ide_dtc2278 || hwif->channel == 0)
1410 hwif->port_ops = d->port_ops;
1411
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001412 hwif->swdma_mask = d->swdma_mask;
1413 hwif->mwdma_mask = d->mwdma_mask;
1414 hwif->ultra_mask = d->udma_mask;
1415
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001416 if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
1417 int rc;
1418
1419 if (d->init_dma)
1420 rc = d->init_dma(hwif, d);
1421 else
1422 rc = ide_hwif_setup_dma(hwif, d);
1423
1424 if (rc < 0) {
1425 printk(KERN_INFO "%s: DMA disabled\n", hwif->name);
Bartlomiej Zolnierkiewiczebb00fb2008-07-23 19:55:51 +02001426 hwif->dma_base = 0;
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001427 hwif->swdma_mask = 0;
1428 hwif->mwdma_mask = 0;
1429 hwif->ultra_mask = 0;
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +02001430 } else if (d->dma_ops)
1431 hwif->dma_ops = d->dma_ops;
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001432 }
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001433
Bartlomiej Zolnierkiewicz1024c5f2008-05-04 17:03:41 +02001434 if ((d->host_flags & IDE_HFLAG_SERIALIZE) ||
1435 ((d->host_flags & IDE_HFLAG_SERIALIZE_DMA) && hwif->dma_base)) {
1436 if (hwif->mate)
1437 hwif->mate->serialized = hwif->serialized = 1;
1438 }
1439
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001440 if (d->host_flags & IDE_HFLAG_RQSIZE_256)
1441 hwif->rqsize = 256;
1442
1443 /* call chipset specific routine for each enabled port */
1444 if (d->init_hwif)
1445 d->init_hwif(hwif);
Bartlomiej Zolnierkiewiczc7f6f212008-04-18 00:46:22 +02001446}
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001447
Bartlomiej Zolnierkiewiczc7f6f212008-04-18 00:46:22 +02001448static void ide_port_cable_detect(ide_hwif_t *hwif)
1449{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001450 const struct ide_port_ops *port_ops = hwif->port_ops;
1451
1452 if (port_ops && port_ops->cable_detect && (hwif->ultra_mask & 0x78)) {
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001453 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001454 hwif->cbl = port_ops->cable_detect(hwif);
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001455 }
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001456}
1457
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +02001458static ssize_t store_delete_devices(struct device *portdev,
1459 struct device_attribute *attr,
1460 const char *buf, size_t n)
1461{
1462 ide_hwif_t *hwif = dev_get_drvdata(portdev);
1463
1464 if (strncmp(buf, "1", n))
1465 return -EINVAL;
1466
1467 ide_port_unregister_devices(hwif);
1468
1469 return n;
1470};
1471
1472static DEVICE_ATTR(delete_devices, S_IWUSR, NULL, store_delete_devices);
1473
1474static ssize_t store_scan(struct device *portdev,
1475 struct device_attribute *attr,
1476 const char *buf, size_t n)
1477{
1478 ide_hwif_t *hwif = dev_get_drvdata(portdev);
1479
1480 if (strncmp(buf, "1", n))
1481 return -EINVAL;
1482
1483 ide_port_unregister_devices(hwif);
1484 ide_port_scan(hwif);
1485
1486 return n;
1487};
1488
1489static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
1490
1491static struct device_attribute *ide_port_attrs[] = {
1492 &dev_attr_delete_devices,
1493 &dev_attr_scan,
1494 NULL
1495};
1496
1497static int ide_sysfs_register_port(ide_hwif_t *hwif)
1498{
Bartlomiej Zolnierkiewiczca09a232008-10-05 18:23:28 +02001499 int i, uninitialized_var(rc);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +02001500
1501 for (i = 0; ide_port_attrs[i]; i++) {
1502 rc = device_create_file(hwif->portdev, ide_port_attrs[i]);
1503 if (rc)
1504 break;
1505 }
1506
1507 return rc;
1508}
1509
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001510static unsigned int ide_indexes;
1511
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001512/**
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001513 * ide_find_port_slot - find free port slot
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001514 * @d: IDE port info
1515 *
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001516 * Return the new port slot index or -ENOENT if we are out of free slots.
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001517 */
1518
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001519static int ide_find_port_slot(const struct ide_port_info *d)
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001520{
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001521 int idx = -ENOENT;
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001522 u8 bootable = (d && (d->host_flags & IDE_HFLAG_NON_BOOTABLE)) ? 0 : 1;
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001523 u8 i = (d && (d->host_flags & IDE_HFLAG_QD_2ND_PORT)) ? 1 : 0;;
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001524
1525 /*
1526 * Claim an unassigned slot.
1527 *
1528 * Give preference to claiming other slots before claiming ide0/ide1,
1529 * just in case there's another interface yet-to-be-scanned
1530 * which uses ports 0x1f0/0x170 (the ide0/ide1 defaults).
1531 *
1532 * Unless there is a bootable card that does not use the standard
1533 * ports 0x1f0/0x170 (the ide0/ide1 defaults).
1534 */
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001535 mutex_lock(&ide_cfg_mtx);
1536 if (MAX_HWIFS == 1) {
1537 if (ide_indexes == 0 && i == 0)
1538 idx = 1;
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001539 } else {
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001540 if (bootable) {
1541 if ((ide_indexes | i) != (1 << MAX_HWIFS) - 1)
1542 idx = ffz(ide_indexes | i);
1543 } else {
1544 if ((ide_indexes | 3) != (1 << MAX_HWIFS) - 1)
1545 idx = ffz(ide_indexes | 3);
1546 else if ((ide_indexes & 3) != 3)
1547 idx = ffz(ide_indexes);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001548 }
1549 }
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001550 if (idx >= 0)
1551 ide_indexes |= (1 << idx);
1552 mutex_unlock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001553
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001554 return idx;
1555}
Bartlomiej Zolnierkiewiczeb3aff52008-07-16 20:33:42 +02001556
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001557static void ide_free_port_slot(int idx)
1558{
1559 mutex_lock(&ide_cfg_mtx);
1560 ide_indexes &= ~(1 << idx);
1561 mutex_unlock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001562}
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001563
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001564struct ide_host *ide_host_alloc_all(const struct ide_port_info *d,
1565 hw_regs_t **hws)
1566{
1567 struct ide_host *host;
1568 int i;
1569
1570 host = kzalloc(sizeof(*host), GFP_KERNEL);
1571 if (host == NULL)
1572 return NULL;
1573
1574 for (i = 0; i < MAX_HWIFS; i++) {
1575 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001576 int idx;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001577
1578 if (hws[i] == NULL)
1579 continue;
1580
Bartlomiej Zolnierkiewicz18de1012008-07-23 19:55:58 +02001581 hwif = kzalloc(sizeof(*hwif), GFP_KERNEL);
1582 if (hwif == NULL)
1583 continue;
1584
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001585 idx = ide_find_port_slot(d);
1586 if (idx < 0) {
1587 printk(KERN_ERR "%s: no free slot for interface\n",
1588 d ? d->name : "ide");
Bartlomiej Zolnierkiewicz18de1012008-07-23 19:55:58 +02001589 kfree(hwif);
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001590 continue;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001591 }
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001592
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001593 ide_init_port_data(hwif, idx);
1594
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +02001595 hwif->host = host;
1596
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001597 host->ports[i] = hwif;
1598 host->n_ports++;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001599 }
1600
1601 if (host->n_ports == 0) {
1602 kfree(host);
1603 return NULL;
1604 }
1605
Bartlomiej Zolnierkiewicz6cdf6eb2008-07-24 22:53:14 +02001606 if (hws[0])
1607 host->dev[0] = hws[0]->dev;
1608
Bartlomiej Zolnierkiewiczef0b0422008-07-24 22:53:19 +02001609 if (d)
1610 host->host_flags = d->host_flags;
1611
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001612 return host;
1613}
1614EXPORT_SYMBOL_GPL(ide_host_alloc_all);
1615
1616struct ide_host *ide_host_alloc(const struct ide_port_info *d, hw_regs_t **hws)
1617{
1618 hw_regs_t *hws_all[MAX_HWIFS];
1619 int i;
1620
1621 for (i = 0; i < MAX_HWIFS; i++)
1622 hws_all[i] = (i < 4) ? hws[i] : NULL;
1623
1624 return ide_host_alloc_all(d, hws_all);
1625}
1626EXPORT_SYMBOL_GPL(ide_host_alloc);
1627
1628int ide_host_register(struct ide_host *host, const struct ide_port_info *d,
1629 hw_regs_t **hws)
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001630{
1631 ide_hwif_t *hwif, *mate = NULL;
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001632 int i, j = 0;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001633
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001634 for (i = 0; i < MAX_HWIFS; i++) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001635 hwif = host->ports[i];
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001636
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001637 if (hwif == NULL) {
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001638 mate = NULL;
1639 continue;
1640 }
1641
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +02001642 ide_init_port_hw(hwif, hws[i]);
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +02001643 ide_port_apply_params(hwif);
1644
1645 if (d == NULL) {
1646 mate = NULL;
1647 continue;
1648 }
1649
Adrian Bunkb7691642008-06-10 20:56:36 +02001650 if ((i & 1) && mate) {
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001651 hwif->mate = mate;
1652 mate->mate = hwif;
1653 }
1654
1655 mate = (i & 1) ? NULL : hwif;
1656
1657 ide_init_port(hwif, i & 1, d);
Bartlomiej Zolnierkiewiczc7f6f212008-04-18 00:46:22 +02001658 ide_port_cable_detect(hwif);
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001659 ide_port_init_devices(hwif);
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001660 }
1661
1662 for (i = 0; i < MAX_HWIFS; i++) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001663 hwif = host->ports[i];
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001664
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001665 if (hwif == NULL)
1666 continue;
Bartlomiej Zolnierkiewicz139ddfc2008-02-01 23:09:36 +01001667
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001668 if (ide_probe_port(hwif) == 0)
1669 hwif->present = 1;
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +01001670
1671 if (hwif->chipset != ide_4drives || !hwif->mate ||
1672 !hwif->mate->present)
1673 ide_register_port(hwif);
1674
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001675 if (hwif->present)
1676 ide_port_tune_devices(hwif);
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001677 }
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001678
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001679 for (i = 0; i < MAX_HWIFS; i++) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001680 hwif = host->ports[i];
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001681
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001682 if (hwif == NULL)
1683 continue;
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001684
1685 if (hwif_init(hwif) == 0) {
1686 printk(KERN_INFO "%s: failed to initialize IDE "
1687 "interface\n", hwif->name);
Bartlomiej Zolnierkiewicz48535652008-02-01 23:09:34 +01001688 hwif->present = 0;
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001689 continue;
1690 }
Bartlomiej Zolnierkiewiczdecdc3f2008-02-02 19:56:41 +01001691
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001692 j++;
1693
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001694 if (hwif->present)
1695 ide_port_setup_devices(hwif);
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +02001696
Bartlomiej Zolnierkiewiczdecdc3f2008-02-02 19:56:41 +01001697 ide_acpi_init(hwif);
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001698
1699 if (hwif->present)
1700 ide_acpi_port_init_devices(hwif);
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001701 }
1702
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001703 for (i = 0; i < MAX_HWIFS; i++) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001704 hwif = host->ports[i];
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001705
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001706 if (hwif == NULL)
1707 continue;
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001708
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001709 if (hwif->chipset == ide_unknown)
1710 hwif->chipset = ide_generic;
1711
1712 if (hwif->present)
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001713 hwif_register_devices(hwif);
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001714 }
1715
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001716 for (i = 0; i < MAX_HWIFS; i++) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001717 hwif = host->ports[i];
Bartlomiej Zolnierkiewicz327617e2008-02-02 19:56:43 +01001718
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001719 if (hwif == NULL)
1720 continue;
Bartlomiej Zolnierkiewicz327617e2008-02-02 19:56:43 +01001721
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001722 ide_sysfs_register_port(hwif);
1723 ide_proc_register_port(hwif);
1724
1725 if (hwif->present)
Bartlomiej Zolnierkiewiczd9270a32008-02-02 19:56:43 +01001726 ide_proc_port_register_devices(hwif);
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001727 }
1728
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001729 return j ? 0 : -1;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001730}
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001731EXPORT_SYMBOL_GPL(ide_host_register);
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001732
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001733int ide_host_add(const struct ide_port_info *d, hw_regs_t **hws,
1734 struct ide_host **hostp)
1735{
1736 struct ide_host *host;
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001737 int rc;
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001738
1739 host = ide_host_alloc(d, hws);
1740 if (host == NULL)
1741 return -ENOMEM;
1742
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001743 rc = ide_host_register(host, d, hws);
1744 if (rc) {
1745 ide_host_free(host);
1746 return rc;
1747 }
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001748
1749 if (hostp)
1750 *hostp = host;
1751
1752 return 0;
1753}
1754EXPORT_SYMBOL_GPL(ide_host_add);
1755
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001756void ide_host_free(struct ide_host *host)
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001757{
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001758 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001759 int i;
1760
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +02001761 for (i = 0; i < MAX_HWIFS; i++) {
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001762 hwif = host->ports[i];
1763
1764 if (hwif == NULL)
1765 continue;
1766
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001767 ide_free_port_slot(hwif->index);
Bartlomiej Zolnierkiewicz18de1012008-07-23 19:55:58 +02001768 kfree(hwif);
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +02001769 }
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001770
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001771 kfree(host);
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001772}
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001773EXPORT_SYMBOL_GPL(ide_host_free);
1774
1775void ide_host_remove(struct ide_host *host)
1776{
1777 int i;
1778
1779 for (i = 0; i < MAX_HWIFS; i++) {
1780 if (host->ports[i])
1781 ide_unregister(host->ports[i]);
1782 }
1783
1784 ide_host_free(host);
1785}
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001786EXPORT_SYMBOL_GPL(ide_host_remove);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001787
1788void ide_port_scan(ide_hwif_t *hwif)
1789{
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +02001790 ide_port_apply_params(hwif);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001791 ide_port_cable_detect(hwif);
1792 ide_port_init_devices(hwif);
1793
1794 if (ide_probe_port(hwif) < 0)
1795 return;
1796
1797 hwif->present = 1;
1798
1799 ide_port_tune_devices(hwif);
1800 ide_acpi_port_init_devices(hwif);
1801 ide_port_setup_devices(hwif);
1802 hwif_register_devices(hwif);
1803 ide_proc_port_register_devices(hwif);
1804}
1805EXPORT_SYMBOL_GPL(ide_port_scan);
Bartlomiej Zolnierkiewicz3b36f662008-04-26 22:25:16 +02001806
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001807static void ide_legacy_init_one(hw_regs_t **hws, hw_regs_t *hw,
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +02001808 u8 port_no, const struct ide_port_info *d,
Bartlomiej Zolnierkiewiczd9b819a2008-04-26 22:25:18 +02001809 unsigned long config)
1810{
Bartlomiej Zolnierkiewiczd9b819a2008-04-26 22:25:18 +02001811 unsigned long base, ctl;
1812 int irq;
1813
1814 if (port_no == 0) {
1815 base = 0x1f0;
1816 ctl = 0x3f6;
1817 irq = 14;
1818 } else {
1819 base = 0x170;
1820 ctl = 0x376;
1821 irq = 15;
1822 }
1823
Bartlomiej Zolnierkiewiczd92f1a22008-04-26 22:25:18 +02001824 if (!request_region(base, 8, d->name)) {
1825 printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
1826 d->name, base, base + 7);
1827 return;
1828 }
1829
1830 if (!request_region(ctl, 1, d->name)) {
1831 printk(KERN_ERR "%s: I/O resource 0x%lX not free.\n",
1832 d->name, ctl);
1833 release_region(base, 8);
1834 return;
1835 }
1836
Bartlomiej Zolnierkiewiczd9b819a2008-04-26 22:25:18 +02001837 ide_std_init_ports(hw, base, ctl);
1838 hw->irq = irq;
Bartlomiej Zolnierkiewiczd427e832008-06-10 20:56:37 +02001839 hw->chipset = d->chipset;
Bartlomiej Zolnierkiewiczd6276b52008-07-23 19:55:56 +02001840 hw->config = config;
Bartlomiej Zolnierkiewiczd9b819a2008-04-26 22:25:18 +02001841
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001842 hws[port_no] = hw;
Bartlomiej Zolnierkiewiczd9b819a2008-04-26 22:25:18 +02001843}
1844
Bartlomiej Zolnierkiewicz0bfeee72008-04-26 22:25:16 +02001845int ide_legacy_device_add(const struct ide_port_info *d, unsigned long config)
Bartlomiej Zolnierkiewicz3b36f662008-04-26 22:25:16 +02001846{
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +02001847 hw_regs_t hw[2], *hws[] = { NULL, NULL, NULL, NULL };
Bartlomiej Zolnierkiewicz3b36f662008-04-26 22:25:16 +02001848
1849 memset(&hw, 0, sizeof(hw));
1850
Bartlomiej Zolnierkiewiczd9b819a2008-04-26 22:25:18 +02001851 if ((d->host_flags & IDE_HFLAG_QD_2ND_PORT) == 0)
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001852 ide_legacy_init_one(hws, &hw[0], 0, d, config);
1853 ide_legacy_init_one(hws, &hw[1], 1, d, config);
Bartlomiej Zolnierkiewicz3b36f662008-04-26 22:25:16 +02001854
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001855 if (hws[0] == NULL && hws[1] == NULL &&
Bartlomiej Zolnierkiewiczd9b819a2008-04-26 22:25:18 +02001856 (d->host_flags & IDE_HFLAG_SINGLE))
Bartlomiej Zolnierkiewicz0bfeee72008-04-26 22:25:16 +02001857 return -ENOENT;
1858
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001859 return ide_host_add(d, hws, NULL);
Bartlomiej Zolnierkiewicz3b36f662008-04-26 22:25:16 +02001860}
1861EXPORT_SYMBOL_GPL(ide_legacy_device_add);