blob: a776a6d73c54de5619e67240554c82c0f8a3f558 [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{
53 drive->id->cyls = drive->cyl;
54 drive->id->heads = drive->head;
55 drive->id->sectors = drive->sect;
56 drive->id->cur_cyls = drive->cyl;
57 drive->id->cur_heads = drive->head;
58 drive->id->cur_sectors = drive->sect;
59}
60
61static void ide_disk_init_chs(ide_drive_t *drive)
62{
63 struct hd_driveid *id = drive->id;
64
65 /* Extract geometry if we did not already have one for the drive */
66 if (!drive->cyl || !drive->head || !drive->sect) {
67 drive->cyl = drive->bios_cyl = id->cyls;
68 drive->head = drive->bios_head = id->heads;
69 drive->sect = drive->bios_sect = id->sectors;
70 }
71
72 /* Handle logical geometry translation by the drive */
73 if ((id->field_valid & 1) && id->cur_cyls &&
74 id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
75 drive->cyl = id->cur_cyls;
76 drive->head = id->cur_heads;
77 drive->sect = id->cur_sectors;
78 }
79
80 /* Use physical geometry if what we have still makes no sense */
81 if (drive->head > 16 && id->heads && id->heads <= 16) {
82 drive->cyl = id->cyls;
83 drive->head = id->heads;
84 drive->sect = id->sectors;
85 }
86}
87
88static void ide_disk_init_mult_count(ide_drive_t *drive)
89{
90 struct hd_driveid *id = drive->id;
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (id->max_multsect) {
93#ifdef CONFIG_IDEDISK_MULTI_MODE
Bartlomiej Zolnierkiewiczdf1f8372008-10-10 22:39:18 +020094 if ((id->max_multsect / 2) > 1) {
95 id->multsect = id->max_multsect;
96 id->multsect_valid = 1;
97 } else {
98 id->multsect = 0;
99 id->multsect_valid = 0;
100 }
101 drive->mult_req = id->multsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#endif
Bartlomiej Zolnierkiewiczdf1f8372008-10-10 22:39:18 +0200103 if ((id->multsect_valid & 1) && id->multsect)
104 drive->special.b.set_multmode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106}
107
108/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * do_identify - identify a drive
110 * @drive: drive to identify
111 * @cmd: command used
112 *
113 * Called when we have issued a drive identify command to
114 * read and parse the results. This function is run with
115 * interrupts disabled.
116 */
117
118static inline void do_identify (ide_drive_t *drive, u8 cmd)
119{
120 ide_hwif_t *hwif = HWIF(drive);
121 int bswap = 1;
122 struct hd_driveid *id;
123
124 id = drive->id;
125 /* read 512 bytes of id info */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200126 hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 drive->id_read = 1;
129 local_irq_enable();
Bartlomiej Zolnierkiewicz7b9f25b2008-02-01 23:09:28 +0100130#ifdef DEBUG
131 printk(KERN_INFO "%s: dumping identify data\n", drive->name);
132 ide_dump_identify((u8 *)id);
133#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 ide_fix_driveid(id);
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /*
137 * WIN_IDENTIFY returns little-endian info,
138 * WIN_PIDENTIFY *usually* returns little-endian info.
139 */
140 if (cmd == WIN_PIDENTIFY) {
141 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
142 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
143 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
144 /* Vertos drives may still be weird */
145 bswap ^= 1;
146 }
147 ide_fixstring(id->model, sizeof(id->model), bswap);
148 ide_fixstring(id->fw_rev, sizeof(id->fw_rev), bswap);
149 ide_fixstring(id->serial_no, sizeof(id->serial_no), bswap);
150
Tejun Heo699b0522007-11-05 21:42:25 +0100151 /* we depend on this a lot! */
152 id->model[sizeof(id->model)-1] = '\0';
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (strstr(id->model, "E X A B Y T E N E S T"))
155 goto err_misc;
156
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200157 printk(KERN_INFO "%s: %s, ", drive->name, id->model);
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 drive->present = 1;
160 drive->dead = 0;
161
162 /*
163 * Check for an ATAPI device
164 */
165 if (cmd == WIN_PIDENTIFY) {
166 u8 type = (id->config >> 8) & 0x1f;
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200167
168 printk(KERN_CONT "ATAPI ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 switch (type) {
170 case ide_floppy:
171 if (!strstr(id->model, "CD-ROM")) {
172 if (!strstr(id->model, "oppy") &&
173 !strstr(id->model, "poyp") &&
174 !strstr(id->model, "ZIP"))
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200175 printk(KERN_CONT "cdrom or floppy?, assuming ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (drive->media != ide_cdrom) {
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200177 printk(KERN_CONT "FLOPPY");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 drive->removable = 1;
179 break;
180 }
181 }
182 /* Early cdrom models used zero */
183 type = ide_cdrom;
184 case ide_cdrom:
185 drive->removable = 1;
186#ifdef CONFIG_PPC
187 /* kludge for Apple PowerBook internal zip */
188 if (!strstr(id->model, "CD-ROM") &&
189 strstr(id->model, "ZIP")) {
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200190 printk(KERN_CONT "FLOPPY");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 type = ide_floppy;
192 break;
193 }
194#endif
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200195 printk(KERN_CONT "CD/DVD-ROM");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 break;
197 case ide_tape:
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200198 printk(KERN_CONT "TAPE");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 break;
200 case ide_optical:
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200201 printk(KERN_CONT "OPTICAL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 drive->removable = 1;
203 break;
204 default:
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200205 printk(KERN_CONT "UNKNOWN (type %d)", type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 break;
207 }
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200208 printk(KERN_CONT " drive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 drive->media = type;
210 /* an ATAPI device ignores DRDY */
211 drive->ready_stat = 0;
212 return;
213 }
214
215 /*
216 * Not an ATAPI device: looks like a "regular" hard disk
217 */
Richard Purdie98109332006-02-03 03:04:55 -0800218
219 /*
220 * 0x848a = CompactFlash device
221 * These are *not* removable in Linux definition of the term
222 */
223
224 if ((id->config != 0x848a) && (id->config & (1<<7)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 drive->removable = 1;
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 drive->media = ide_disk;
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200228
229 printk(KERN_CONT "%s DISK drive\n",
230 (id->config == 0x848a) ? "CFA" : "ATA");
Bartlomiej Zolnierkiewiczcd3dbc92008-01-25 22:17:13 +0100231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return;
233
234err_misc:
235 kfree(id);
236 drive->present = 0;
237 return;
238}
239
240/**
241 * actual_try_to_identify - send ata/atapi identify
242 * @drive: drive to identify
243 * @cmd: command to use
244 *
245 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
246 * and waits for a response. It also monitors irqs while this is
247 * happening, in hope of automatically determining which one is
248 * being used by the interface.
249 *
250 * Returns: 0 device was identified
251 * 1 device timed-out (no response to identify request)
252 * 2 device aborted the command (refused to identify itself)
253 */
254
255static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
256{
257 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200258 struct ide_io_ports *io_ports = &hwif->io_ports;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200259 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100260 int use_altstatus = 0, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 unsigned long timeout;
262 u8 s = 0, a = 0;
263
264 /* take a deep breath */
265 msleep(50);
266
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200267 if (io_ports->ctl_addr) {
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200268 a = tp_ops->read_altstatus(hwif);
269 s = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100270 if ((a ^ s) & ~INDEX_STAT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /* ancient Seagate drives, broken interfaces */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100272 printk(KERN_INFO "%s: probing with STATUS(0x%02x) "
273 "instead of ALTSTATUS(0x%02x)\n",
274 drive->name, s, a);
275 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /* use non-intrusive polling */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100277 use_altstatus = 1;
278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 /* set features register for atapi
281 * identify command to be sure of reply
282 */
Bartlomiej Zolnierkiewicz4e658372008-07-23 19:55:53 +0200283 if (cmd == WIN_PIDENTIFY) {
284 ide_task_t task;
285
286 memset(&task, 0, sizeof(task));
287 /* disable DMA & overlap */
288 task.tf_flags = IDE_TFLAG_OUT_FEATURE;
289
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200290 tp_ops->tf_load(drive, &task);
Bartlomiej Zolnierkiewicz4e658372008-07-23 19:55:53 +0200291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 /* ask drive for ID */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200294 tp_ops->exec_command(hwif, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
297 timeout += jiffies;
298 do {
299 if (time_after(jiffies, timeout)) {
300 /* drive timed-out */
301 return 1;
302 }
303 /* give drive a breather */
304 msleep(50);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200305 s = use_altstatus ? tp_ops->read_altstatus(hwif)
306 : tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100307 } while (s & BUSY_STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 /* wait for IRQ and DRQ_STAT */
310 msleep(50);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200311 s = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100312
313 if (OK_STAT(s, DRQ_STAT, BAD_R_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 unsigned long flags;
315
316 /* local CPU only; some systems need this */
317 local_irq_save(flags);
318 /* drive returned ID */
319 do_identify(drive, cmd);
320 /* drive responded with ID */
321 rc = 0;
322 /* clear drive IRQ */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200323 (void)tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 local_irq_restore(flags);
325 } 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{
344 ide_hwif_t *hwif = HWIF(drive);
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 Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100389static int ide_busy_sleep(ide_hwif_t *hwif)
390{
391 unsigned long timeout = jiffies + WAIT_WORSTCASE;
392 u8 stat;
393
394 do {
395 msleep(50);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200396 stat = hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100397 if ((stat & BUSY_STAT) == 0)
398 return 0;
399 } while (time_before(jiffies, timeout));
400
401 return 1;
402}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200404static u8 ide_read_device(ide_drive_t *drive)
405{
406 ide_task_t task;
407
408 memset(&task, 0, sizeof(task));
409 task.tf_flags = IDE_TFLAG_IN_DEVICE;
410
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200411 drive->hwif->tp_ops->tf_read(drive, &task);
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200412
413 return task.tf.device;
414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/**
417 * do_probe - probe an IDE device
418 * @drive: drive to probe
419 * @cmd: command to use
420 *
421 * do_probe() has the difficult job of finding a drive if it exists,
422 * without getting hung up if it doesn't exist, without trampling on
423 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
424 *
425 * If a drive is "known" to exist (from CMOS or kernel parameters),
426 * but does not respond right away, the probe will "hang in there"
427 * for the maximum wait time (about 30 seconds), otherwise it will
428 * exit much more quickly.
429 *
430 * Returns: 0 device was identified
431 * 1 device timed-out (no response to identify request)
432 * 2 device aborted the command (refused to identify itself)
433 * 3 bad status from device (possible for ATAPI drives)
434 * 4 probe was not attempted because failure was obvious
435 */
436
437static int do_probe (ide_drive_t *drive, u8 cmd)
438{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200440 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100441 int rc;
442 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 if (drive->present) {
445 /* avoid waiting for inappropriate probes */
446 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
447 return 4;
448 }
449#ifdef DEBUG
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200450 printk(KERN_INFO "probing for %s: present=%d, media=%d, probetype=%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 drive->name, drive->present, drive->media,
452 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
453#endif
454
455 /* needed for some systems
456 * (e.g. crw9624 as drive0 with disk as slave)
457 */
458 msleep(50);
459 SELECT_DRIVE(drive);
460 msleep(50);
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200461
462 if (ide_read_device(drive) != drive->select.all && !drive->present) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (drive->select.b.unit != 0) {
464 /* exit with drive0 selected */
465 SELECT_DRIVE(&hwif->drives[0]);
466 /* allow BUSY_STAT to assert & clear */
467 msleep(50);
468 }
469 /* no i/f present: mmm.. this should be a 4 -ml */
470 return 3;
471 }
472
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200473 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100474
475 if (OK_STAT(stat, READY_STAT, BUSY_STAT) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 drive->present || cmd == WIN_PIDENTIFY) {
477 /* send cmd and wait */
478 if ((rc = try_to_identify(drive, cmd))) {
479 /* failed: try again */
480 rc = try_to_identify(drive,cmd);
481 }
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100482
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200483 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100484
485 if (stat == (BUSY_STAT | READY_STAT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return 4;
487
Bartlomiej Zolnierkiewiczcc121752008-04-27 15:38:24 +0200488 if (rc == 1 && cmd == WIN_PIDENTIFY) {
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100489 printk(KERN_ERR "%s: no response (status = 0x%02x), "
490 "resetting drive\n", drive->name, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 msleep(50);
Bartlomiej Zolnierkiewicze81a3bd2008-07-15 21:21:48 +0200492 SELECT_DRIVE(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 msleep(50);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200494 tp_ops->exec_command(hwif, WIN_SRST);
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100495 (void)ide_busy_sleep(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 rc = try_to_identify(drive, cmd);
497 }
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100498
499 /* ensure drive IRQ is clear */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200500 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (rc == 1)
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100503 printk(KERN_ERR "%s: no response (status = 0x%02x)\n",
504 drive->name, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 } else {
506 /* not present or maybe ATAPI */
507 rc = 3;
508 }
509 if (drive->select.b.unit != 0) {
510 /* exit with drive0 selected */
511 SELECT_DRIVE(&hwif->drives[0]);
512 msleep(50);
513 /* ensure drive irq is clear */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200514 (void)tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516 return rc;
517}
518
519/*
520 *
521 */
522static void enable_nest (ide_drive_t *drive)
523{
524 ide_hwif_t *hwif = HWIF(drive);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200525 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100526 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200528 printk(KERN_INFO "%s: enabling %s -- ", hwif->name, drive->id->model);
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 SELECT_DRIVE(drive);
531 msleep(50);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200532 tp_ops->exec_command(hwif, EXABYTE_ENABLE_NEST);
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100533
534 if (ide_busy_sleep(hwif)) {
535 printk(KERN_CONT "failed (timeout)\n");
536 return;
537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 msleep(50);
540
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200541 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100542
543 if (!OK_STAT(stat, 0, BAD_STAT))
544 printk(KERN_CONT "failed (status = 0x%02x)\n", stat);
545 else
546 printk(KERN_CONT "success\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 /* if !(success||timed-out) */
549 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
550 /* look for ATAPI device */
551 (void) do_probe(drive, WIN_PIDENTIFY);
552 }
553}
554
555/**
556 * probe_for_drives - upper level drive probe
557 * @drive: drive to probe for
558 *
559 * probe_for_drive() tests for existence of a given drive using do_probe()
560 * and presents things to the user as needed.
561 *
562 * Returns: 0 no device was found
563 * 1 device was found (note: drive->present might
564 * still be 0)
565 */
566
567static inline u8 probe_for_drive (ide_drive_t *drive)
568{
569 /*
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 strcpy(drive->id->model, "UNKNOWN");
586
587 /* skip probing? */
588 if (!drive->noprobe)
589 {
590 /* if !(success||timed-out) */
591 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
592 /* look for ATAPI device */
593 (void) do_probe(drive, WIN_PIDENTIFY);
594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (!drive->present)
596 /* drive not found */
597 return 0;
Alan Cox78595572007-07-03 22:28:35 +0200598 if (strstr(drive->id->model, "E X A B Y T E N E S T"))
599 enable_nest(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 /* identification failed? */
602 if (!drive->id_read) {
603 if (drive->media == ide_disk) {
604 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
605 drive->name, drive->cyl,
606 drive->head, drive->sect);
607 } else if (drive->media == ide_cdrom) {
608 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
609 } else {
610 /* nuke it */
611 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
612 drive->present = 0;
613 }
614 }
615 /* drive was found */
616 }
617 if(!drive->present)
618 return 0;
619 /* The drive wasn't being helpful. Add generic info only */
620 if (drive->id_read == 0) {
621 generic_id(drive);
622 return 1;
623 }
624
625 if (drive->media == ide_disk) {
626 ide_disk_init_chs(drive);
627 ide_disk_init_mult_count(drive);
628 }
629
630 return drive->present;
631}
632
Pavel Machekfc410692008-07-23 19:56:02 +0200633static void hwif_release_dev(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
636
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800637 complete(&hwif->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200640static int ide_register_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Randy Dunlap349ae232006-10-03 01:14:23 -0700642 int ret;
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /* register with global device tree */
645 strlcpy(hwif->gendev.bus_id,hwif->name,BUS_ID_SIZE);
646 hwif->gendev.driver_data = hwif;
647 if (hwif->gendev.parent == NULL) {
Bartlomiej Zolnierkiewicz36501652008-02-01 23:09:31 +0100648 if (hwif->dev)
649 hwif->gendev.parent = hwif->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 else
651 /* Would like to do = &device_legacy */
652 hwif->gendev.parent = NULL;
653 }
654 hwif->gendev.release = hwif_release_dev;
Randy Dunlap349ae232006-10-03 01:14:23 -0700655 ret = device_register(&hwif->gendev);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200656 if (ret < 0) {
Randy Dunlap349ae232006-10-03 01:14:23 -0700657 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200658 __func__, ret);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200659 goto out;
660 }
661
Greg Kroah-Hartman716ad872008-05-16 17:55:12 -0700662 hwif->portdev = device_create_drvdata(ide_port_class, &hwif->gendev,
663 MKDEV(0, 0), hwif, hwif->name);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200664 if (IS_ERR(hwif->portdev)) {
665 ret = PTR_ERR(hwif->portdev);
666 device_unregister(&hwif->gendev);
667 }
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200668out:
669 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Bartlomiej Zolnierkiewiczc860a8f2008-02-01 23:09:34 +0100672/**
673 * ide_port_wait_ready - wait for port to become ready
674 * @hwif: IDE port
675 *
676 * This is needed on some PPCs and a bunch of BIOS-less embedded
677 * platforms. Typical cases are:
678 *
679 * - The firmware hard reset the disk before booting the kernel,
680 * the drive is still doing it's poweron-reset sequence, that
681 * can take up to 30 seconds.
682 *
683 * - The firmware does nothing (or no firmware), the device is
684 * still in POST state (same as above actually).
685 *
686 * - Some CD/DVD/Writer combo drives tend to drive the bus during
687 * their reset sequence even when they are non-selected slave
688 * devices, thus preventing discovery of the main HD.
689 *
690 * Doing this wait-for-non-busy should not harm any existing
691 * configuration and fix some issues like the above.
692 *
693 * BenH.
694 *
695 * Returns 0 on success, error code (< 0) otherwise.
696 */
697
698static int ide_port_wait_ready(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Jonas Stare82661052007-11-27 21:35:53 +0100700 int unit, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
703
704 /* Let HW settle down a bit from whatever init state we
705 * come from */
706 mdelay(2);
707
708 /* Wait for BSY bit to go away, spec timeout is 30 seconds,
709 * I know of at least one disk who takes 31 seconds, I use 35
710 * here to be safe
711 */
712 rc = ide_wait_not_busy(hwif, 35000);
713 if (rc)
714 return rc;
715
716 /* Now make sure both master & slave are ready */
Jonas Stare82661052007-11-27 21:35:53 +0100717 for (unit = 0; unit < MAX_DRIVES; unit++) {
718 ide_drive_t *drive = &hwif->drives[unit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Jonas Stare82661052007-11-27 21:35:53 +0100720 /* Ignore disks that we will not probe for later. */
721 if (!drive->noprobe || drive->present) {
722 SELECT_DRIVE(drive);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200723 hwif->tp_ops->set_irq(hwif, 1);
Jonas Stare82661052007-11-27 21:35:53 +0100724 mdelay(2);
725 rc = ide_wait_not_busy(hwif, 35000);
726 if (rc)
727 goto out;
728 } else
729 printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
730 drive->name);
731 }
732out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /* Exit function with master reselected (let's be sane) */
Jonas Stare82661052007-11-27 21:35:53 +0100734 if (unit)
735 SELECT_DRIVE(&hwif->drives[0]);
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return rc;
738}
739
740/**
741 * ide_undecoded_slave - look for bad CF adapters
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100742 * @drive1: drive
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 *
744 * Analyse the drives on the interface and attempt to decide if we
745 * have the same drive viewed twice. This occurs with crap CF adapters
746 * and PCMCIA sometimes.
747 */
748
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100749void ide_undecoded_slave(ide_drive_t *drive1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100751 ide_drive_t *drive0 = &drive1->hwif->drives[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100753 if ((drive1->dn & 1) == 0 || drive0->present == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return;
755
756 /* If the models don't match they are not the same product */
757 if (strcmp(drive0->id->model, drive1->id->model))
758 return;
759
760 /* Serial numbers do not match */
761 if (strncmp(drive0->id->serial_no, drive1->id->serial_no, 20))
762 return;
763
764 /* No serial number, thankfully very rare for CF */
765 if (drive0->id->serial_no[0] == 0)
766 return;
767
768 /* Appears to be an IDE flash adapter with decode bugs */
769 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
770
771 drive1->present = 0;
772}
773
774EXPORT_SYMBOL_GPL(ide_undecoded_slave);
775
Bartlomiej Zolnierkiewicz9d501522008-02-01 23:09:36 +0100776static int ide_probe_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 unsigned long flags;
779 unsigned int irqd;
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100780 int unit, rc = -ENODEV;
781
782 BUG_ON(hwif->present);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Bartlomiej Zolnierkiewicze53cd452008-04-26 22:25:16 +0200784 if (hwif->drives[0].noprobe && hwif->drives[1].noprobe)
Bartlomiej Zolnierkiewicz9d501522008-02-01 23:09:36 +0100785 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /*
788 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
789 * we'll install our IRQ driver much later...
790 */
791 irqd = hwif->irq;
792 if (irqd)
793 disable_irq(hwif->irq);
794
795 local_irq_set(flags);
796
Bartlomiej Zolnierkiewiczc860a8f2008-02-01 23:09:34 +0100797 if (ide_port_wait_ready(hwif) == -EBUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
799
800 /*
Bartlomiej Zolnierkiewiczf367bed2008-03-29 19:48:21 +0100801 * Second drive should only exist if first drive was found,
802 * but a lot of cdrom drives are configured as single slaves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 */
Bartlomiej Zolnierkiewiczf367bed2008-03-29 19:48:21 +0100804 for (unit = 0; unit < MAX_DRIVES; ++unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 ide_drive_t *drive = &hwif->drives[unit];
806 drive->dn = (hwif->channel ? 2 : 0) + unit;
807 (void) probe_for_drive(drive);
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100808 if (drive->present)
809 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
Bartlomiej Zolnierkiewicze460a592008-04-27 15:38:24 +0200811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 local_irq_restore(flags);
Bartlomiej Zolnierkiewicze460a592008-04-27 15:38:24 +0200813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /*
815 * Use cached IRQ number. It might be (and is...) changed by probe
816 * code above
817 */
818 if (irqd)
819 enable_irq(irqd);
820
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100821 return rc;
Bartlomiej Zolnierkiewicze84e7ea2008-02-01 23:09:36 +0100822}
823
824static void ide_port_tune_devices(ide_hwif_t *hwif)
825{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200826 const struct ide_port_ops *port_ops = hwif->port_ops;
Bartlomiej Zolnierkiewicze84e7ea2008-02-01 23:09:36 +0100827 int unit;
828
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100829 for (unit = 0; unit < MAX_DRIVES; unit++) {
830 ide_drive_t *drive = &hwif->drives[unit];
831
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200832 if (drive->present && port_ops && port_ops->quirkproc)
833 port_ops->quirkproc(drive);
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100834 }
Bartlomiej Zolnierkiewicz0380dad2007-06-08 15:14:29 +0200835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 for (unit = 0; unit < MAX_DRIVES; ++unit) {
837 ide_drive_t *drive = &hwif->drives[unit];
838
839 if (drive->present) {
Bartlomiej Zolnierkiewicz207daea2008-04-27 15:38:29 +0200840 ide_set_max_pio(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 drive->nice1 = 1;
843
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200844 if (hwif->dma_ops)
Bartlomiej Zolnierkiewicz8c0697c2007-10-16 22:29:58 +0200845 ide_set_dma(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847 }
Kumar Gala208a08f2006-03-24 03:18:21 -0800848
849 for (unit = 0; unit < MAX_DRIVES; ++unit) {
850 ide_drive_t *drive = &hwif->drives[unit];
851
Bartlomiej Zolnierkiewicz807b90d2008-02-02 19:56:40 +0100852 if (hwif->host_flags & IDE_HFLAG_NO_IO_32BIT)
Kumar Gala208a08f2006-03-24 03:18:21 -0800853 drive->no_io_32bit = 1;
854 else
855 drive->no_io_32bit = drive->id->dword_io ? 1 : 0;
856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859#if MAX_HWIFS > 1
860/*
861 * save_match() is used to simplify logic in init_irq() below.
862 *
863 * A loophole here is that we may not know about a particular
864 * hwif's irq until after that hwif is actually probed/initialized..
865 * This could be a problem for the case where an hwif is on a
866 * dual interface that requires serialization (eg. cmd640) and another
867 * hwif using one of the same irqs is initialized beforehand.
868 *
869 * This routine detects and reports such situations, but does not fix them.
870 */
871static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
872{
873 ide_hwif_t *m = *match;
874
875 if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
876 if (!new->hwgroup)
877 return;
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200878 printk(KERN_WARNING "%s: potential IRQ problem with %s and %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 hwif->name, new->name, m->name);
880 }
881 if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
882 *match = new;
883}
884#endif /* MAX_HWIFS > 1 */
885
886/*
887 * init request queue
888 */
889static int ide_init_queue(ide_drive_t *drive)
890{
Jens Axboe165125e2007-07-24 09:28:11 +0200891 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 ide_hwif_t *hwif = HWIF(drive);
893 int max_sectors = 256;
894 int max_sg_entries = PRD_ENTRIES;
895
896 /*
897 * Our default set up assumes the normal IDE case,
898 * that is 64K segmenting, standard PRD setup
899 * and LBA28. Some drivers then impose their own
900 * limits and LBA48 we could raise it but as yet
901 * do not.
902 */
Christoph Lameter19460892005-06-23 00:08:19 -0700903
Ravikiran G Thirumalai556e58f2005-08-04 12:53:26 -0700904 q = blk_init_queue_node(do_ide_request, &ide_lock, hwif_to_node(hwif));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (!q)
906 return 1;
907
908 q->queuedata = drive;
909 blk_queue_segment_boundary(q, 0xffff);
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (hwif->rqsize < max_sectors)
912 max_sectors = hwif->rqsize;
913 blk_queue_max_sectors(q, max_sectors);
914
915#ifdef CONFIG_PCI
916 /* When we have an IOMMU, we may have a problem where pci_map_sg()
917 * creates segments that don't completely match our boundary
918 * requirements and thus need to be broken up again. Because it
919 * doesn't align properly either, we may actually have to break up
920 * to more segments than what was we got in the first place, a max
921 * worst case is twice as many.
922 * This will be fixed once we teach pci_map_sg() about our boundary
923 * requirements, hopefully soon. *FIXME*
924 */
925 if (!PCI_DMA_BUS_IS_PHYS)
926 max_sg_entries >>= 1;
927#endif /* CONFIG_PCI */
928
929 blk_queue_max_hw_segments(q, max_sg_entries);
930 blk_queue_max_phys_segments(q, max_sg_entries);
931
932 /* assign drive queue */
933 drive->queue = q;
934
935 /* needs drive->queue to be set */
936 ide_toggle_bounce(drive, 1);
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return 0;
939}
940
Bartlomiej Zolnierkiewicz0947e0d2008-02-02 19:56:41 +0100941static void ide_add_drive_to_hwgroup(ide_drive_t *drive)
942{
943 ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
944
945 spin_lock_irq(&ide_lock);
946 if (!hwgroup->drive) {
947 /* first drive for hwgroup. */
948 drive->next = drive;
949 hwgroup->drive = drive;
950 hwgroup->hwif = HWIF(hwgroup->drive);
951 } else {
952 drive->next = hwgroup->drive->next;
953 hwgroup->drive->next = drive;
954 }
955 spin_unlock_irq(&ide_lock);
956}
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958/*
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100959 * For any present drive:
960 * - allocate the block device queue
961 * - link drive into the hwgroup
962 */
963static void ide_port_setup_devices(ide_hwif_t *hwif)
964{
965 int i;
966
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +0200967 mutex_lock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100968 for (i = 0; i < MAX_DRIVES; i++) {
969 ide_drive_t *drive = &hwif->drives[i];
970
971 if (!drive->present)
972 continue;
973
974 if (ide_init_queue(drive)) {
975 printk(KERN_ERR "ide: failed to init %s\n",
976 drive->name);
977 continue;
978 }
979
980 ide_add_drive_to_hwgroup(drive);
981 }
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +0200982 mutex_unlock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100983}
984
Bartlomiej Zolnierkiewiczaf1cbba2008-07-23 19:55:58 +0200985static ide_hwif_t *ide_ports[MAX_HWIFS];
986
Bartlomiej Zolnierkiewicz60591432008-07-23 19:55:58 +0200987void ide_remove_port_from_hwgroup(ide_hwif_t *hwif)
988{
989 ide_hwgroup_t *hwgroup = hwif->hwgroup;
990
Bartlomiej Zolnierkiewiczaf1cbba2008-07-23 19:55:58 +0200991 ide_ports[hwif->index] = NULL;
992
Bartlomiej Zolnierkiewicz60591432008-07-23 19:55:58 +0200993 spin_lock_irq(&ide_lock);
994 /*
995 * Remove us from the hwgroup, and free
996 * the hwgroup if we were the only member
997 */
998 if (hwif->next == hwif) {
999 BUG_ON(hwgroup->hwif != hwif);
1000 kfree(hwgroup);
1001 } else {
1002 /* There is another interface in hwgroup.
1003 * Unlink us, and set hwgroup->drive and ->hwif to
1004 * something sane.
1005 */
1006 ide_hwif_t *g = hwgroup->hwif;
1007
1008 while (g->next != hwif)
1009 g = g->next;
1010 g->next = hwif->next;
1011 if (hwgroup->hwif == hwif) {
1012 /* Chose a random hwif for hwgroup->hwif.
1013 * It's guaranteed that there are no drives
1014 * left in the hwgroup.
1015 */
1016 BUG_ON(hwgroup->drive != NULL);
1017 hwgroup->hwif = g;
1018 }
1019 BUG_ON(hwgroup->hwif == hwif);
1020 }
1021 spin_unlock_irq(&ide_lock);
1022}
1023
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +01001024/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 * This routine sets up the irq for an ide interface, and creates a new
1026 * hwgroup for the irq/hwif if none was previously assigned.
1027 *
1028 * Much of the code is for correctly detecting/handling irq sharing
1029 * and irq serialization situations. This is somewhat complex because
1030 * it handles static as well as dynamic (PCMCIA) IDE interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 */
1032static int init_irq (ide_hwif_t *hwif)
1033{
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +02001034 struct ide_io_ports *io_ports = &hwif->io_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 unsigned int index;
1036 ide_hwgroup_t *hwgroup;
1037 ide_hwif_t *match = NULL;
1038
1039
1040 BUG_ON(in_interrupt());
1041 BUG_ON(irqs_disabled());
Ravikiran G Thirumalai556e58f2005-08-04 12:53:26 -07001042 BUG_ON(hwif == NULL);
1043
Matthias Kaehlckeef298882007-07-09 23:17:55 +02001044 mutex_lock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 hwif->hwgroup = NULL;
1046#if MAX_HWIFS > 1
1047 /*
1048 * Group up with any other hwifs that share our irq(s).
1049 */
1050 for (index = 0; index < MAX_HWIFS; index++) {
Bartlomiej Zolnierkiewiczaf1cbba2008-07-23 19:55:58 +02001051 ide_hwif_t *h = ide_ports[index];
1052
1053 if (h && h->hwgroup) { /* scan only initialized ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (hwif->irq == h->irq) {
1055 hwif->sharing_irq = h->sharing_irq = 1;
1056 if (hwif->chipset != ide_pci ||
1057 h->chipset != ide_pci) {
1058 save_match(hwif, h, &match);
1059 }
1060 }
1061 if (hwif->serialized) {
1062 if (hwif->mate && hwif->mate->irq == h->irq)
1063 save_match(hwif, h, &match);
1064 }
1065 if (h->serialized) {
1066 if (h->mate && hwif->irq == h->mate->irq)
1067 save_match(hwif, h, &match);
1068 }
1069 }
1070 }
1071#endif /* MAX_HWIFS > 1 */
1072 /*
1073 * If we are still without a hwgroup, then form a new one
1074 */
1075 if (match) {
1076 hwgroup = match->hwgroup;
1077 hwif->hwgroup = hwgroup;
1078 /*
1079 * Link us into the hwgroup.
1080 * This must be done early, do ensure that unexpected_intr
1081 * can find the hwif and prevent irq storms.
1082 * No drives are attached to the new hwif, choose_drive
1083 * can't do anything stupid (yet).
1084 * Add ourself as the 2nd entry to the hwgroup->hwif
1085 * linked list, the first entry is the hwif that owns
1086 * hwgroup->handler - do not change that.
1087 */
1088 spin_lock_irq(&ide_lock);
1089 hwif->next = hwgroup->hwif->next;
1090 hwgroup->hwif->next = hwif;
Bartlomiej Zolnierkiewiczcae5c822008-02-01 23:09:36 +01001091 BUG_ON(hwif->next == hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 spin_unlock_irq(&ide_lock);
1093 } else {
Bartlomiej Zolnierkiewicz422278e2008-02-01 23:09:35 +01001094 hwgroup = kmalloc_node(sizeof(*hwgroup), GFP_KERNEL|__GFP_ZERO,
1095 hwif_to_node(hwif));
1096 if (hwgroup == NULL)
1097 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 hwif->hwgroup = hwgroup;
Bartlomiej Zolnierkiewicz422278e2008-02-01 23:09:35 +01001100 hwgroup->hwif = hwif->next = hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 init_timer(&hwgroup->timer);
1103 hwgroup->timer.function = &ide_timer_expiry;
1104 hwgroup->timer.data = (unsigned long) hwgroup;
1105 }
1106
Bartlomiej Zolnierkiewiczaf1cbba2008-07-23 19:55:58 +02001107 ide_ports[hwif->index] = hwif;
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 /*
1110 * Allocate the irq, if not already obtained for another hwif
1111 */
1112 if (!match || match->irq != hwif->irq) {
Bartlomiej Zolnierkiewicz7b5da4b2008-01-25 22:17:08 +01001113 int sa = 0;
Adrian Bunk7b892802008-02-06 01:36:29 -08001114#if defined(__mc68000__)
Thomas Gleixner362537b2006-07-01 19:29:34 -07001115 sa = IRQF_SHARED;
Bartlomiej Zolnierkiewicze1771e22008-02-11 00:32:15 +01001116#endif /* __mc68000__ */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Bartlomiej Zolnierkiewicz7b5da4b2008-01-25 22:17:08 +01001118 if (IDE_CHIPSET_IS_PCI(hwif->chipset))
Thomas Gleixner362537b2006-07-01 19:29:34 -07001119 sa = IRQF_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +02001121 if (io_ports->ctl_addr)
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001122 hwif->tp_ops->set_irq(hwif, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
1125 goto out_unlink;
1126 }
1127
Bartlomiej Zolnierkiewicz8a0e7e12008-02-02 19:56:41 +01001128 if (!hwif->rqsize) {
1129 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
1130 (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
1131 hwif->rqsize = 256;
1132 else
1133 hwif->rqsize = 65536;
1134 }
1135
Adrian Bunk7b892802008-02-06 01:36:29 -08001136#if !defined(__mc68000__)
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +02001137 printk(KERN_INFO "%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +02001138 io_ports->data_addr, io_ports->status_addr,
1139 io_ports->ctl_addr, hwif->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140#else
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +02001141 printk(KERN_INFO "%s at 0x%08lx on irq %d", hwif->name,
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +02001142 io_ports->data_addr, hwif->irq);
Adrian Bunk7b892802008-02-06 01:36:29 -08001143#endif /* __mc68000__ */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (match)
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +02001145 printk(KERN_CONT " (%sed with %s)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 hwif->sharing_irq ? "shar" : "serializ", match->name);
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +02001147 printk(KERN_CONT "\n");
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +01001148
Matthias Kaehlckeef298882007-07-09 23:17:55 +02001149 mutex_unlock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return 0;
1151out_unlink:
Bartlomiej Zolnierkiewiczfbd13082008-02-01 23:09:36 +01001152 ide_remove_port_from_hwgroup(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153out_up:
Matthias Kaehlckeef298882007-07-09 23:17:55 +02001154 mutex_unlock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return 1;
1156}
1157
1158static int ata_lock(dev_t dev, void *data)
1159{
1160 /* FIXME: we want to pin hwif down */
1161 return 0;
1162}
1163
1164static struct kobject *ata_probe(dev_t dev, int *part, void *data)
1165{
1166 ide_hwif_t *hwif = data;
1167 int unit = *part >> PARTN_BITS;
1168 ide_drive_t *drive = &hwif->drives[unit];
1169 if (!drive->present)
1170 return NULL;
1171
1172 if (drive->media == ide_disk)
1173 request_module("ide-disk");
1174 if (drive->scsi)
1175 request_module("ide-scsi");
1176 if (drive->media == ide_cdrom || drive->media == ide_optical)
1177 request_module("ide-cd");
1178 if (drive->media == ide_tape)
1179 request_module("ide-tape");
1180 if (drive->media == ide_floppy)
1181 request_module("ide-floppy");
1182
1183 return NULL;
1184}
1185
1186static struct kobject *exact_match(dev_t dev, int *part, void *data)
1187{
1188 struct gendisk *p = data;
1189 *part &= (1 << PARTN_BITS) - 1;
Tejun Heoed9e1982008-08-25 19:56:05 +09001190 return &disk_to_dev(p)->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
1193static int exact_lock(dev_t dev, void *data)
1194{
1195 struct gendisk *p = data;
1196
1197 if (!get_disk(p))
1198 return -1;
1199 return 0;
1200}
1201
1202void ide_register_region(struct gendisk *disk)
1203{
1204 blk_register_region(MKDEV(disk->major, disk->first_minor),
1205 disk->minors, NULL, exact_match, exact_lock, disk);
1206}
1207
1208EXPORT_SYMBOL_GPL(ide_register_region);
1209
1210void ide_unregister_region(struct gendisk *disk)
1211{
1212 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
1213 disk->minors);
1214}
1215
1216EXPORT_SYMBOL_GPL(ide_unregister_region);
1217
1218void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
1219{
1220 ide_hwif_t *hwif = drive->hwif;
1221 unsigned int unit = (drive->select.all >> 4) & 1;
1222
1223 disk->major = hwif->major;
1224 disk->first_minor = unit << PARTN_BITS;
1225 sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
1226 disk->queue = drive->queue;
1227}
1228
1229EXPORT_SYMBOL_GPL(ide_init_disk);
1230
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001231static void ide_remove_drive_from_hwgroup(ide_drive_t *drive)
1232{
1233 ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
1234
1235 if (drive == drive->next) {
1236 /* special case: last drive from hwgroup. */
1237 BUG_ON(hwgroup->drive != drive);
1238 hwgroup->drive = NULL;
1239 } else {
1240 ide_drive_t *walk;
1241
1242 walk = hwgroup->drive;
1243 while (walk->next != drive)
1244 walk = walk->next;
1245 walk->next = drive->next;
1246 if (hwgroup->drive == drive) {
1247 hwgroup->drive = drive->next;
1248 hwgroup->hwif = hwgroup->drive->hwif;
1249 }
1250 }
1251 BUG_ON(hwgroup->drive == drive);
1252}
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254static void drive_release_dev (struct device *dev)
1255{
1256 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1257
Bartlomiej Zolnierkiewicz5b0c4b32008-04-18 00:46:22 +02001258 ide_proc_unregister_device(drive);
1259
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001260 spin_lock_irq(&ide_lock);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001261 ide_remove_drive_from_hwgroup(drive);
Jesper Juhl6044ec82005-11-07 01:01:32 -08001262 kfree(drive->id);
1263 drive->id = NULL;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001264 drive->present = 0;
1265 /* Messed up locking ... */
1266 spin_unlock_irq(&ide_lock);
1267 blk_cleanup_queue(drive->queue);
1268 spin_lock_irq(&ide_lock);
1269 drive->queue = NULL;
1270 spin_unlock_irq(&ide_lock);
1271
Aleksey Makarovf36d4022006-01-09 15:59:27 -08001272 complete(&drive->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275static int hwif_init(ide_hwif_t *hwif)
1276{
1277 int old_irq;
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (!hwif->irq) {
Bartlomiej Zolnierkiewicza861beb2008-07-08 19:27:22 +02001280 hwif->irq = __ide_default_irq(hwif->io_ports.data_addr);
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +02001281 if (!hwif->irq) {
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +02001282 printk(KERN_ERR "%s: disabled, no IRQ\n", hwif->name);
Bartlomiej Zolnierkiewicz48535652008-02-01 23:09:34 +01001283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
1285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (register_blkdev(hwif->major, hwif->name))
1288 return 0;
1289
1290 if (!hwif->sg_max_nents)
1291 hwif->sg_max_nents = PRD_ENTRIES;
1292
Jens Axboe45711f12007-10-22 21:19:53 +02001293 hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 GFP_KERNEL);
1295 if (!hwif->sg_table) {
1296 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1297 goto out;
1298 }
Jens Axboe45711f12007-10-22 21:19:53 +02001299
1300 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 if (init_irq(hwif) == 0)
1303 goto done;
1304
1305 old_irq = hwif->irq;
1306 /*
1307 * It failed to initialise. Find the default IRQ for
1308 * this port and try that.
1309 */
Bartlomiej Zolnierkiewicza861beb2008-07-08 19:27:22 +02001310 hwif->irq = __ide_default_irq(hwif->io_ports.data_addr);
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +02001311 if (!hwif->irq) {
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +02001312 printk(KERN_ERR "%s: disabled, unable to get IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 hwif->name, old_irq);
1314 goto out;
1315 }
1316 if (init_irq(hwif)) {
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +02001317 printk(KERN_ERR "%s: probed IRQ %d and default IRQ %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 hwif->name, old_irq, hwif->irq);
1319 goto out;
1320 }
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +02001321 printk(KERN_WARNING "%s: probed IRQ %d failed, using default\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 hwif->name, hwif->irq);
1323
1324done:
Bartlomiej Zolnierkiewicz3a4e7c92008-02-02 19:56:40 +01001325 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1326 THIS_MODULE, ata_probe, ata_lock, hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return 1;
1328
1329out:
1330 unregister_blkdev(hwif->major, hwif->name);
1331 return 0;
1332}
1333
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001334static void hwif_register_devices(ide_hwif_t *hwif)
1335{
1336 unsigned int i;
1337
1338 for (i = 0; i < MAX_DRIVES; i++) {
1339 ide_drive_t *drive = &hwif->drives[i];
Bartlomiej Zolnierkiewiczc5d70cc2008-02-02 19:56:41 +01001340 struct device *dev = &drive->gendev;
1341 int ret;
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001342
Bartlomiej Zolnierkiewiczc5d70cc2008-02-02 19:56:41 +01001343 if (!drive->present)
1344 continue;
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001345
Bartlomiej Zolnierkiewiczc5d70cc2008-02-02 19:56:41 +01001346 ide_add_generic_settings(drive);
1347
1348 snprintf(dev->bus_id, BUS_ID_SIZE, "%u.%u", hwif->index, i);
1349 dev->parent = &hwif->gendev;
1350 dev->bus = &ide_bus_type;
1351 dev->driver_data = drive;
1352 dev->release = drive_release_dev;
1353
1354 ret = device_register(dev);
1355 if (ret < 0)
1356 printk(KERN_WARNING "IDE: %s: device_register error: "
1357 "%d\n", __func__, ret);
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001358 }
1359}
1360
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001361static void ide_port_init_devices(ide_hwif_t *hwif)
1362{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001363 const struct ide_port_ops *port_ops = hwif->port_ops;
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001364 int i;
1365
1366 for (i = 0; i < MAX_DRIVES; i++) {
1367 ide_drive_t *drive = &hwif->drives[i];
1368
1369 if (hwif->host_flags & IDE_HFLAG_IO_32BIT)
1370 drive->io_32bit = 1;
1371 if (hwif->host_flags & IDE_HFLAG_UNMASK_IRQS)
1372 drive->unmask = 1;
Bartlomiej Zolnierkiewicz807b90d2008-02-02 19:56:40 +01001373 if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS)
1374 drive->no_unmask = 1;
Bartlomiej Zolnierkiewicz1f2cf8b2008-02-02 19:56:40 +01001375
Bartlomiej Zolnierkiewicze6d95bd2008-07-16 20:33:42 +02001376 if (port_ops && port_ops->init_dev)
1377 port_ops->init_dev(drive);
1378 }
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001379}
1380
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001381static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
1382 const struct ide_port_info *d)
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001383{
Adrian Bunkb7691642008-06-10 20:56:36 +02001384 hwif->channel = port;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001385
1386 if (d->chipset)
1387 hwif->chipset = d->chipset;
1388
1389 if (d->init_iops)
1390 d->init_iops(hwif);
1391
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001392 if ((!hwif->irq && (d->host_flags & IDE_HFLAG_LEGACY_IRQS)) ||
1393 (d->host_flags & IDE_HFLAG_FORCE_LEGACY_IRQS))
1394 hwif->irq = port ? 15 : 14;
1395
Bartlomiej Zolnierkiewicz23f8e4b2008-05-01 14:08:51 +02001396 /* ->host_flags may be set by ->init_iops (or even earlier...) */
1397 hwif->host_flags |= d->host_flags;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001398 hwif->pio_mask = d->pio_mask;
1399
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001400 if (d->tp_ops)
1401 hwif->tp_ops = d->tp_ops;
1402
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001403 /* ->set_pio_mode for DTC2278 is currently limited to port 0 */
1404 if (hwif->chipset != ide_dtc2278 || hwif->channel == 0)
1405 hwif->port_ops = d->port_ops;
1406
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001407 hwif->swdma_mask = d->swdma_mask;
1408 hwif->mwdma_mask = d->mwdma_mask;
1409 hwif->ultra_mask = d->udma_mask;
1410
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001411 if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
1412 int rc;
1413
1414 if (d->init_dma)
1415 rc = d->init_dma(hwif, d);
1416 else
1417 rc = ide_hwif_setup_dma(hwif, d);
1418
1419 if (rc < 0) {
1420 printk(KERN_INFO "%s: DMA disabled\n", hwif->name);
Bartlomiej Zolnierkiewiczebb00fb2008-07-23 19:55:51 +02001421 hwif->dma_base = 0;
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001422 hwif->swdma_mask = 0;
1423 hwif->mwdma_mask = 0;
1424 hwif->ultra_mask = 0;
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +02001425 } else if (d->dma_ops)
1426 hwif->dma_ops = d->dma_ops;
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001427 }
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001428
Bartlomiej Zolnierkiewicz1024c5f2008-05-04 17:03:41 +02001429 if ((d->host_flags & IDE_HFLAG_SERIALIZE) ||
1430 ((d->host_flags & IDE_HFLAG_SERIALIZE_DMA) && hwif->dma_base)) {
1431 if (hwif->mate)
1432 hwif->mate->serialized = hwif->serialized = 1;
1433 }
1434
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001435 if (d->host_flags & IDE_HFLAG_RQSIZE_256)
1436 hwif->rqsize = 256;
1437
1438 /* call chipset specific routine for each enabled port */
1439 if (d->init_hwif)
1440 d->init_hwif(hwif);
Bartlomiej Zolnierkiewiczc7f6f212008-04-18 00:46:22 +02001441}
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001442
Bartlomiej Zolnierkiewiczc7f6f212008-04-18 00:46:22 +02001443static void ide_port_cable_detect(ide_hwif_t *hwif)
1444{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001445 const struct ide_port_ops *port_ops = hwif->port_ops;
1446
1447 if (port_ops && port_ops->cable_detect && (hwif->ultra_mask & 0x78)) {
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001448 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001449 hwif->cbl = port_ops->cable_detect(hwif);
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001450 }
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001451}
1452
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +02001453static ssize_t store_delete_devices(struct device *portdev,
1454 struct device_attribute *attr,
1455 const char *buf, size_t n)
1456{
1457 ide_hwif_t *hwif = dev_get_drvdata(portdev);
1458
1459 if (strncmp(buf, "1", n))
1460 return -EINVAL;
1461
1462 ide_port_unregister_devices(hwif);
1463
1464 return n;
1465};
1466
1467static DEVICE_ATTR(delete_devices, S_IWUSR, NULL, store_delete_devices);
1468
1469static ssize_t store_scan(struct device *portdev,
1470 struct device_attribute *attr,
1471 const char *buf, size_t n)
1472{
1473 ide_hwif_t *hwif = dev_get_drvdata(portdev);
1474
1475 if (strncmp(buf, "1", n))
1476 return -EINVAL;
1477
1478 ide_port_unregister_devices(hwif);
1479 ide_port_scan(hwif);
1480
1481 return n;
1482};
1483
1484static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
1485
1486static struct device_attribute *ide_port_attrs[] = {
1487 &dev_attr_delete_devices,
1488 &dev_attr_scan,
1489 NULL
1490};
1491
1492static int ide_sysfs_register_port(ide_hwif_t *hwif)
1493{
Bartlomiej Zolnierkiewiczca09a232008-10-05 18:23:28 +02001494 int i, uninitialized_var(rc);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +02001495
1496 for (i = 0; ide_port_attrs[i]; i++) {
1497 rc = device_create_file(hwif->portdev, ide_port_attrs[i]);
1498 if (rc)
1499 break;
1500 }
1501
1502 return rc;
1503}
1504
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001505static unsigned int ide_indexes;
1506
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001507/**
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001508 * ide_find_port_slot - find free port slot
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001509 * @d: IDE port info
1510 *
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001511 * Return the new port slot index or -ENOENT if we are out of free slots.
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001512 */
1513
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001514static int ide_find_port_slot(const struct ide_port_info *d)
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001515{
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001516 int idx = -ENOENT;
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001517 u8 bootable = (d && (d->host_flags & IDE_HFLAG_NON_BOOTABLE)) ? 0 : 1;
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001518 u8 i = (d && (d->host_flags & IDE_HFLAG_QD_2ND_PORT)) ? 1 : 0;;
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001519
1520 /*
1521 * Claim an unassigned slot.
1522 *
1523 * Give preference to claiming other slots before claiming ide0/ide1,
1524 * just in case there's another interface yet-to-be-scanned
1525 * which uses ports 0x1f0/0x170 (the ide0/ide1 defaults).
1526 *
1527 * Unless there is a bootable card that does not use the standard
1528 * ports 0x1f0/0x170 (the ide0/ide1 defaults).
1529 */
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001530 mutex_lock(&ide_cfg_mtx);
1531 if (MAX_HWIFS == 1) {
1532 if (ide_indexes == 0 && i == 0)
1533 idx = 1;
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001534 } else {
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001535 if (bootable) {
1536 if ((ide_indexes | i) != (1 << MAX_HWIFS) - 1)
1537 idx = ffz(ide_indexes | i);
1538 } else {
1539 if ((ide_indexes | 3) != (1 << MAX_HWIFS) - 1)
1540 idx = ffz(ide_indexes | 3);
1541 else if ((ide_indexes & 3) != 3)
1542 idx = ffz(ide_indexes);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001543 }
1544 }
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001545 if (idx >= 0)
1546 ide_indexes |= (1 << idx);
1547 mutex_unlock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001548
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001549 return idx;
1550}
Bartlomiej Zolnierkiewiczeb3aff52008-07-16 20:33:42 +02001551
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001552static void ide_free_port_slot(int idx)
1553{
1554 mutex_lock(&ide_cfg_mtx);
1555 ide_indexes &= ~(1 << idx);
1556 mutex_unlock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001557}
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001558
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001559struct ide_host *ide_host_alloc_all(const struct ide_port_info *d,
1560 hw_regs_t **hws)
1561{
1562 struct ide_host *host;
1563 int i;
1564
1565 host = kzalloc(sizeof(*host), GFP_KERNEL);
1566 if (host == NULL)
1567 return NULL;
1568
1569 for (i = 0; i < MAX_HWIFS; i++) {
1570 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001571 int idx;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001572
1573 if (hws[i] == NULL)
1574 continue;
1575
Bartlomiej Zolnierkiewicz18de1012008-07-23 19:55:58 +02001576 hwif = kzalloc(sizeof(*hwif), GFP_KERNEL);
1577 if (hwif == NULL)
1578 continue;
1579
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001580 idx = ide_find_port_slot(d);
1581 if (idx < 0) {
1582 printk(KERN_ERR "%s: no free slot for interface\n",
1583 d ? d->name : "ide");
Bartlomiej Zolnierkiewicz18de1012008-07-23 19:55:58 +02001584 kfree(hwif);
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001585 continue;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001586 }
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001587
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001588 ide_init_port_data(hwif, idx);
1589
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +02001590 hwif->host = host;
1591
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001592 host->ports[i] = hwif;
1593 host->n_ports++;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001594 }
1595
1596 if (host->n_ports == 0) {
1597 kfree(host);
1598 return NULL;
1599 }
1600
Bartlomiej Zolnierkiewicz6cdf6eb2008-07-24 22:53:14 +02001601 if (hws[0])
1602 host->dev[0] = hws[0]->dev;
1603
Bartlomiej Zolnierkiewiczef0b0422008-07-24 22:53:19 +02001604 if (d)
1605 host->host_flags = d->host_flags;
1606
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001607 return host;
1608}
1609EXPORT_SYMBOL_GPL(ide_host_alloc_all);
1610
1611struct ide_host *ide_host_alloc(const struct ide_port_info *d, hw_regs_t **hws)
1612{
1613 hw_regs_t *hws_all[MAX_HWIFS];
1614 int i;
1615
1616 for (i = 0; i < MAX_HWIFS; i++)
1617 hws_all[i] = (i < 4) ? hws[i] : NULL;
1618
1619 return ide_host_alloc_all(d, hws_all);
1620}
1621EXPORT_SYMBOL_GPL(ide_host_alloc);
1622
1623int ide_host_register(struct ide_host *host, const struct ide_port_info *d,
1624 hw_regs_t **hws)
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001625{
1626 ide_hwif_t *hwif, *mate = NULL;
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001627 int i, j = 0;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001628
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001629 for (i = 0; i < MAX_HWIFS; i++) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001630 hwif = host->ports[i];
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001631
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001632 if (hwif == NULL) {
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001633 mate = NULL;
1634 continue;
1635 }
1636
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +02001637 ide_init_port_hw(hwif, hws[i]);
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +02001638 ide_port_apply_params(hwif);
1639
1640 if (d == NULL) {
1641 mate = NULL;
1642 continue;
1643 }
1644
Adrian Bunkb7691642008-06-10 20:56:36 +02001645 if ((i & 1) && mate) {
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001646 hwif->mate = mate;
1647 mate->mate = hwif;
1648 }
1649
1650 mate = (i & 1) ? NULL : hwif;
1651
1652 ide_init_port(hwif, i & 1, d);
Bartlomiej Zolnierkiewiczc7f6f212008-04-18 00:46:22 +02001653 ide_port_cable_detect(hwif);
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001654 ide_port_init_devices(hwif);
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001655 }
1656
1657 for (i = 0; i < MAX_HWIFS; i++) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001658 hwif = host->ports[i];
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001659
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001660 if (hwif == NULL)
1661 continue;
Bartlomiej Zolnierkiewicz139ddfc2008-02-01 23:09:36 +01001662
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001663 if (ide_probe_port(hwif) == 0)
1664 hwif->present = 1;
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +01001665
1666 if (hwif->chipset != ide_4drives || !hwif->mate ||
1667 !hwif->mate->present)
1668 ide_register_port(hwif);
1669
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001670 if (hwif->present)
1671 ide_port_tune_devices(hwif);
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001672 }
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001673
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001674 for (i = 0; i < MAX_HWIFS; i++) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001675 hwif = host->ports[i];
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001676
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001677 if (hwif == NULL)
1678 continue;
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001679
1680 if (hwif_init(hwif) == 0) {
1681 printk(KERN_INFO "%s: failed to initialize IDE "
1682 "interface\n", hwif->name);
Bartlomiej Zolnierkiewicz48535652008-02-01 23:09:34 +01001683 hwif->present = 0;
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001684 continue;
1685 }
Bartlomiej Zolnierkiewiczdecdc3f2008-02-02 19:56:41 +01001686
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001687 j++;
1688
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001689 if (hwif->present)
1690 ide_port_setup_devices(hwif);
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +02001691
Bartlomiej Zolnierkiewiczdecdc3f2008-02-02 19:56:41 +01001692 ide_acpi_init(hwif);
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001693
1694 if (hwif->present)
1695 ide_acpi_port_init_devices(hwif);
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001696 }
1697
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001698 for (i = 0; i < MAX_HWIFS; i++) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001699 hwif = host->ports[i];
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001700
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001701 if (hwif == NULL)
1702 continue;
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001703
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001704 if (hwif->chipset == ide_unknown)
1705 hwif->chipset = ide_generic;
1706
1707 if (hwif->present)
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001708 hwif_register_devices(hwif);
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001709 }
1710
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001711 for (i = 0; i < MAX_HWIFS; i++) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001712 hwif = host->ports[i];
Bartlomiej Zolnierkiewicz327617e2008-02-02 19:56:43 +01001713
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001714 if (hwif == NULL)
1715 continue;
Bartlomiej Zolnierkiewicz327617e2008-02-02 19:56:43 +01001716
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001717 ide_sysfs_register_port(hwif);
1718 ide_proc_register_port(hwif);
1719
1720 if (hwif->present)
Bartlomiej Zolnierkiewiczd9270a32008-02-02 19:56:43 +01001721 ide_proc_port_register_devices(hwif);
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001722 }
1723
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001724 return j ? 0 : -1;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001725}
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001726EXPORT_SYMBOL_GPL(ide_host_register);
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001727
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001728int ide_host_add(const struct ide_port_info *d, hw_regs_t **hws,
1729 struct ide_host **hostp)
1730{
1731 struct ide_host *host;
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001732 int rc;
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001733
1734 host = ide_host_alloc(d, hws);
1735 if (host == NULL)
1736 return -ENOMEM;
1737
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001738 rc = ide_host_register(host, d, hws);
1739 if (rc) {
1740 ide_host_free(host);
1741 return rc;
1742 }
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001743
1744 if (hostp)
1745 *hostp = host;
1746
1747 return 0;
1748}
1749EXPORT_SYMBOL_GPL(ide_host_add);
1750
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001751void ide_host_free(struct ide_host *host)
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001752{
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001753 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001754 int i;
1755
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +02001756 for (i = 0; i < MAX_HWIFS; i++) {
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001757 hwif = host->ports[i];
1758
1759 if (hwif == NULL)
1760 continue;
1761
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001762 ide_free_port_slot(hwif->index);
Bartlomiej Zolnierkiewicz18de1012008-07-23 19:55:58 +02001763 kfree(hwif);
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +02001764 }
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001765
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001766 kfree(host);
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001767}
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001768EXPORT_SYMBOL_GPL(ide_host_free);
1769
1770void ide_host_remove(struct ide_host *host)
1771{
1772 int i;
1773
1774 for (i = 0; i < MAX_HWIFS; i++) {
1775 if (host->ports[i])
1776 ide_unregister(host->ports[i]);
1777 }
1778
1779 ide_host_free(host);
1780}
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001781EXPORT_SYMBOL_GPL(ide_host_remove);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001782
1783void ide_port_scan(ide_hwif_t *hwif)
1784{
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +02001785 ide_port_apply_params(hwif);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001786 ide_port_cable_detect(hwif);
1787 ide_port_init_devices(hwif);
1788
1789 if (ide_probe_port(hwif) < 0)
1790 return;
1791
1792 hwif->present = 1;
1793
1794 ide_port_tune_devices(hwif);
1795 ide_acpi_port_init_devices(hwif);
1796 ide_port_setup_devices(hwif);
1797 hwif_register_devices(hwif);
1798 ide_proc_port_register_devices(hwif);
1799}
1800EXPORT_SYMBOL_GPL(ide_port_scan);
Bartlomiej Zolnierkiewicz3b36f662008-04-26 22:25:16 +02001801
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001802static void ide_legacy_init_one(hw_regs_t **hws, hw_regs_t *hw,
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +02001803 u8 port_no, const struct ide_port_info *d,
Bartlomiej Zolnierkiewiczd9b819a2008-04-26 22:25:18 +02001804 unsigned long config)
1805{
Bartlomiej Zolnierkiewiczd9b819a2008-04-26 22:25:18 +02001806 unsigned long base, ctl;
1807 int irq;
1808
1809 if (port_no == 0) {
1810 base = 0x1f0;
1811 ctl = 0x3f6;
1812 irq = 14;
1813 } else {
1814 base = 0x170;
1815 ctl = 0x376;
1816 irq = 15;
1817 }
1818
Bartlomiej Zolnierkiewiczd92f1a22008-04-26 22:25:18 +02001819 if (!request_region(base, 8, d->name)) {
1820 printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
1821 d->name, base, base + 7);
1822 return;
1823 }
1824
1825 if (!request_region(ctl, 1, d->name)) {
1826 printk(KERN_ERR "%s: I/O resource 0x%lX not free.\n",
1827 d->name, ctl);
1828 release_region(base, 8);
1829 return;
1830 }
1831
Bartlomiej Zolnierkiewiczd9b819a2008-04-26 22:25:18 +02001832 ide_std_init_ports(hw, base, ctl);
1833 hw->irq = irq;
Bartlomiej Zolnierkiewiczd427e832008-06-10 20:56:37 +02001834 hw->chipset = d->chipset;
Bartlomiej Zolnierkiewiczd6276b52008-07-23 19:55:56 +02001835 hw->config = config;
Bartlomiej Zolnierkiewiczd9b819a2008-04-26 22:25:18 +02001836
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001837 hws[port_no] = hw;
Bartlomiej Zolnierkiewiczd9b819a2008-04-26 22:25:18 +02001838}
1839
Bartlomiej Zolnierkiewicz0bfeee72008-04-26 22:25:16 +02001840int ide_legacy_device_add(const struct ide_port_info *d, unsigned long config)
Bartlomiej Zolnierkiewicz3b36f662008-04-26 22:25:16 +02001841{
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +02001842 hw_regs_t hw[2], *hws[] = { NULL, NULL, NULL, NULL };
Bartlomiej Zolnierkiewicz3b36f662008-04-26 22:25:16 +02001843
1844 memset(&hw, 0, sizeof(hw));
1845
Bartlomiej Zolnierkiewiczd9b819a2008-04-26 22:25:18 +02001846 if ((d->host_flags & IDE_HFLAG_QD_2ND_PORT) == 0)
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001847 ide_legacy_init_one(hws, &hw[0], 0, d, config);
1848 ide_legacy_init_one(hws, &hw[1], 1, d, config);
Bartlomiej Zolnierkiewicz3b36f662008-04-26 22:25:16 +02001849
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001850 if (hws[0] == NULL && hws[1] == NULL &&
Bartlomiej Zolnierkiewiczd9b819a2008-04-26 22:25:18 +02001851 (d->host_flags & IDE_HFLAG_SINGLE))
Bartlomiej Zolnierkiewicz0bfeee72008-04-26 22:25:16 +02001852 return -ENOENT;
1853
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001854 return ide_host_add(d, hws, NULL);
Bartlomiej Zolnierkiewicz3b36f662008-04-26 22:25:16 +02001855}
1856EXPORT_SYMBOL_GPL(ide_legacy_device_add);