]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/ide/ide-probe.c
ide: cleanup ide_fix_driveid()
[mirror_ubuntu-artful-kernel.git] / drivers / ide / ide-probe.c
CommitLineData
1da177e4 1/*
59bca8cc
BZ
2 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
3 * Copyright (C) 2005, 2007 Bartlomiej Zolnierkiewicz
1da177e4
LT
4 */
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 *
bbe4d6d8
BZ
15 * -- increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
16 * by Andrea Arcangeli
1da177e4
LT
17 */
18
1da177e4
LT
19#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>
dc81785d 35#include <linux/scatterlist.h>
1da177e4
LT
36
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
1da177e4
LT
92 if (id->max_multsect) {
93#ifdef CONFIG_IDEDISK_MULTI_MODE
df1f8378
BZ
94 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;
1da177e4 102#endif
df1f8378
BZ
103 if ((id->multsect_valid & 1) && id->multsect)
104 drive->special.b.set_multmode = 1;
1da177e4
LT
105 }
106}
107
1da177e4
LT
108/**
109 * 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 */
374e042c 126 hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
1da177e4
LT
127
128 drive->id_read = 1;
129 local_irq_enable();
7b9f25b5
BZ
130#ifdef DEBUG
131 printk(KERN_INFO "%s: dumping identify data\n", drive->name);
132 ide_dump_identify((u8 *)id);
133#endif
1da177e4
LT
134 ide_fix_driveid(id);
135
1da177e4
LT
136 /*
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
699b052a
TH
151 /* we depend on this a lot! */
152 id->model[sizeof(id->model)-1] = '\0';
153
1da177e4
LT
154 if (strstr(id->model, "E X A B Y T E N E S T"))
155 goto err_misc;
156
1b8ebad8
BZ
157 printk(KERN_INFO "%s: %s, ", drive->name, id->model);
158
1da177e4
LT
159 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;
1b8ebad8
BZ
167
168 printk(KERN_CONT "ATAPI ");
1da177e4
LT
169 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"))
1b8ebad8 175 printk(KERN_CONT "cdrom or floppy?, assuming ");
1da177e4 176 if (drive->media != ide_cdrom) {
1b8ebad8 177 printk(KERN_CONT "FLOPPY");
1da177e4
LT
178 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")) {
1b8ebad8 190 printk(KERN_CONT "FLOPPY");
1da177e4
LT
191 type = ide_floppy;
192 break;
193 }
194#endif
1b8ebad8 195 printk(KERN_CONT "CD/DVD-ROM");
1da177e4
LT
196 break;
197 case ide_tape:
1b8ebad8 198 printk(KERN_CONT "TAPE");
1da177e4
LT
199 break;
200 case ide_optical:
1b8ebad8 201 printk(KERN_CONT "OPTICAL");
1da177e4
LT
202 drive->removable = 1;
203 break;
204 default:
1b8ebad8 205 printk(KERN_CONT "UNKNOWN (type %d)", type);
1da177e4
LT
206 break;
207 }
1b8ebad8 208 printk(KERN_CONT " drive\n");
1da177e4
LT
209 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 */
98109337
RP
218
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)))
1da177e4
LT
225 drive->removable = 1;
226
1da177e4 227 drive->media = ide_disk;
1b8ebad8
BZ
228
229 printk(KERN_CONT "%s DISK drive\n",
230 (id->config == 0x848a) ? "CFA" : "ATA");
cd3dbc99 231
1da177e4
LT
232 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);
4c3032d8 258 struct ide_io_ports *io_ports = &hwif->io_ports;
374e042c 259 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
c47137a9 260 int use_altstatus = 0, rc;
1da177e4
LT
261 unsigned long timeout;
262 u8 s = 0, a = 0;
263
264 /* take a deep breath */
265 msleep(50);
266
4c3032d8 267 if (io_ports->ctl_addr) {
374e042c
BZ
268 a = tp_ops->read_altstatus(hwif);
269 s = tp_ops->read_status(hwif);
c47137a9 270 if ((a ^ s) & ~INDEX_STAT)
1da177e4 271 /* ancient Seagate drives, broken interfaces */
c47137a9
BZ
272 printk(KERN_INFO "%s: probing with STATUS(0x%02x) "
273 "instead of ALTSTATUS(0x%02x)\n",
274 drive->name, s, a);
275 else
1da177e4 276 /* use non-intrusive polling */
c47137a9
BZ
277 use_altstatus = 1;
278 }
1da177e4
LT
279
280 /* set features register for atapi
281 * identify command to be sure of reply
282 */
4e65837b
BZ
283 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
374e042c 290 tp_ops->tf_load(drive, &task);
4e65837b 291 }
1da177e4
LT
292
293 /* ask drive for ID */
374e042c 294 tp_ops->exec_command(hwif, cmd);
1da177e4
LT
295
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);
374e042c
BZ
305 s = use_altstatus ? tp_ops->read_altstatus(hwif)
306 : tp_ops->read_status(hwif);
c47137a9 307 } while (s & BUSY_STAT);
1da177e4
LT
308
309 /* wait for IRQ and DRQ_STAT */
310 msleep(50);
374e042c 311 s = tp_ops->read_status(hwif);
c47137a9
BZ
312
313 if (OK_STAT(s, DRQ_STAT, BAD_R_STAT)) {
1da177e4
LT
314 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 */
374e042c 323 (void)tp_ops->read_status(hwif);
1da177e4
LT
324 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);
374e042c 345 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
1da177e4
LT
346 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 */
4c3032d8 356 if (hwif->io_ports.ctl_addr) {
1da177e4
LT
357 if (!hwif->irq) {
358 autoprobe = 1;
359 cookie = probe_irq_on();
1da177e4 360 }
374e042c 361 tp_ops->set_irq(hwif, autoprobe);
1da177e4
LT
362 }
363
364 retval = actual_try_to_identify(drive, cmd);
365
366 if (autoprobe) {
367 int irq;
81ca6919 368
374e042c 369 tp_ops->set_irq(hwif, 0);
1da177e4 370 /* clear drive IRQ */
374e042c 371 (void)tp_ops->read_status(hwif);
1da177e4
LT
372 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 */
1b8ebad8 381 printk(KERN_ERR "%s: IRQ probe failed (0x%lx)\n",
1da177e4
LT
382 drive->name, cookie);
383 }
384 }
385 }
386 return retval;
387}
388
3a5015cc
BZ
389static 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);
374e042c 396 stat = hwif->tp_ops->read_status(hwif);
3a5015cc
BZ
397 if ((stat & BUSY_STAT) == 0)
398 return 0;
399 } while (time_before(jiffies, timeout));
400
401 return 1;
402}
1da177e4 403
1f2efb82
BZ
404static 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
374e042c 411 drive->hwif->tp_ops->tf_read(drive, &task);
1f2efb82
BZ
412
413 return task.tf.device;
414}
415
1da177e4
LT
416/**
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{
1da177e4 439 ide_hwif_t *hwif = HWIF(drive);
374e042c 440 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
57b55275
BZ
441 int rc;
442 u8 stat;
1da177e4
LT
443
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
1b8ebad8 450 printk(KERN_INFO "probing for %s: present=%d, media=%d, probetype=%s\n",
1da177e4
LT
451 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);
1f2efb82
BZ
461
462 if (ide_read_device(drive) != drive->select.all && !drive->present) {
1da177e4
LT
463 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
374e042c 473 stat = tp_ops->read_status(hwif);
c47137a9
BZ
474
475 if (OK_STAT(stat, READY_STAT, BUSY_STAT) ||
1da177e4
LT
476 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 }
57b55275 482
374e042c 483 stat = tp_ops->read_status(hwif);
57b55275
BZ
484
485 if (stat == (BUSY_STAT | READY_STAT))
1da177e4
LT
486 return 4;
487
cc12175f 488 if (rc == 1 && cmd == WIN_PIDENTIFY) {
57b55275
BZ
489 printk(KERN_ERR "%s: no response (status = 0x%02x), "
490 "resetting drive\n", drive->name, stat);
1da177e4 491 msleep(50);
e81a3bde 492 SELECT_DRIVE(drive);
1da177e4 493 msleep(50);
374e042c 494 tp_ops->exec_command(hwif, WIN_SRST);
3a5015cc 495 (void)ide_busy_sleep(hwif);
1da177e4
LT
496 rc = try_to_identify(drive, cmd);
497 }
57b55275
BZ
498
499 /* ensure drive IRQ is clear */
374e042c 500 stat = tp_ops->read_status(hwif);
57b55275 501
1da177e4 502 if (rc == 1)
57b55275
BZ
503 printk(KERN_ERR "%s: no response (status = 0x%02x)\n",
504 drive->name, stat);
1da177e4
LT
505 } 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 */
374e042c 514 (void)tp_ops->read_status(hwif);
1da177e4
LT
515 }
516 return rc;
517}
518
519/*
520 *
521 */
522static void enable_nest (ide_drive_t *drive)
523{
524 ide_hwif_t *hwif = HWIF(drive);
374e042c 525 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
57b55275 526 u8 stat;
1da177e4 527
1b8ebad8
BZ
528 printk(KERN_INFO "%s: enabling %s -- ", hwif->name, drive->id->model);
529
1da177e4
LT
530 SELECT_DRIVE(drive);
531 msleep(50);
374e042c 532 tp_ops->exec_command(hwif, EXABYTE_ENABLE_NEST);
3a5015cc
BZ
533
534 if (ide_busy_sleep(hwif)) {
535 printk(KERN_CONT "failed (timeout)\n");
536 return;
537 }
1da177e4
LT
538
539 msleep(50);
540
374e042c 541 stat = tp_ops->read_status(hwif);
57b55275
BZ
542
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");
1da177e4
LT
547
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
f5e3c2fa 578 drive->id = kzalloc(SECTOR_WORDS *4, GFP_KERNEL);
1da177e4
LT
579 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 }
1da177e4
LT
585 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 }
1da177e4
LT
595 if (!drive->present)
596 /* drive not found */
597 return 0;
78595575
AC
598 if (strstr(drive->id->model, "E X A B Y T E N E S T"))
599 enable_nest(drive);
1da177e4
LT
600
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
fc410698 633static void hwif_release_dev(struct device *dev)
1da177e4
LT
634{
635 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
636
f36d4024 637 complete(&hwif->gendev_rel_comp);
1da177e4
LT
638}
639
f74c9141 640static int ide_register_port(ide_hwif_t *hwif)
1da177e4 641{
349ae23f
RD
642 int ret;
643
1da177e4
LT
644 /* 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) {
36501650
BZ
648 if (hwif->dev)
649 hwif->gendev.parent = hwif->dev;
1da177e4
LT
650 else
651 /* Would like to do = &device_legacy */
652 hwif->gendev.parent = NULL;
653 }
654 hwif->gendev.release = hwif_release_dev;
349ae23f 655 ret = device_register(&hwif->gendev);
f74c9141 656 if (ret < 0) {
349ae23f 657 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
eb63963a 658 __func__, ret);
f74c9141
BZ
659 goto out;
660 }
661
716ad875
GKH
662 hwif->portdev = device_create_drvdata(ide_port_class, &hwif->gendev,
663 MKDEV(0, 0), hwif, hwif->name);
f74c9141
BZ
664 if (IS_ERR(hwif->portdev)) {
665 ret = PTR_ERR(hwif->portdev);
666 device_unregister(&hwif->gendev);
667 }
f74c9141
BZ
668out:
669 return ret;
1da177e4
LT
670}
671
c860a8f2
BZ
672/**
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)
1da177e4 699{
8266105b 700 int unit, rc;
1da177e4
LT
701
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 */
8266105b
JS
717 for (unit = 0; unit < MAX_DRIVES; unit++) {
718 ide_drive_t *drive = &hwif->drives[unit];
719
720 /* Ignore disks that we will not probe for later. */
721 if (!drive->noprobe || drive->present) {
722 SELECT_DRIVE(drive);
374e042c 723 hwif->tp_ops->set_irq(hwif, 1);
8266105b
JS
724 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:
1da177e4 733 /* Exit function with master reselected (let's be sane) */
8266105b
JS
734 if (unit)
735 SELECT_DRIVE(&hwif->drives[0]);
736
1da177e4
LT
737 return rc;
738}
739
740/**
741 * ide_undecoded_slave - look for bad CF adapters
f01393e4 742 * @drive1: drive
1da177e4
LT
743 *
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
f01393e4 749void ide_undecoded_slave(ide_drive_t *drive1)
1da177e4 750{
f01393e4 751 ide_drive_t *drive0 = &drive1->hwif->drives[0];
1da177e4 752
f01393e4 753 if ((drive1->dn & 1) == 0 || drive0->present == 0)
1da177e4
LT
754 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
9d501529 776static int ide_probe_port(ide_hwif_t *hwif)
1da177e4 777{
1da177e4
LT
778 unsigned long flags;
779 unsigned int irqd;
a14dc574
BZ
780 int unit, rc = -ENODEV;
781
782 BUG_ON(hwif->present);
1da177e4 783
e53cd458 784 if (hwif->drives[0].noprobe && hwif->drives[1].noprobe)
9d501529 785 return -EACCES;
1da177e4 786
1da177e4
LT
787 /*
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
c860a8f2 797 if (ide_port_wait_ready(hwif) == -EBUSY)
1da177e4
LT
798 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
799
800 /*
f367bed0
BZ
801 * Second drive should only exist if first drive was found,
802 * but a lot of cdrom drives are configured as single slaves.
1da177e4 803 */
f367bed0 804 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1da177e4
LT
805 ide_drive_t *drive = &hwif->drives[unit];
806 drive->dn = (hwif->channel ? 2 : 0) + unit;
807 (void) probe_for_drive(drive);
a14dc574
BZ
808 if (drive->present)
809 rc = 0;
1da177e4 810 }
e460a597 811
1da177e4 812 local_irq_restore(flags);
e460a597 813
1da177e4
LT
814 /*
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
a14dc574 821 return rc;
e84e7ea7
BZ
822}
823
824static void ide_port_tune_devices(ide_hwif_t *hwif)
825{
ac95beed 826 const struct ide_port_ops *port_ops = hwif->port_ops;
e84e7ea7
BZ
827 int unit;
828
f01393e4
BZ
829 for (unit = 0; unit < MAX_DRIVES; unit++) {
830 ide_drive_t *drive = &hwif->drives[unit];
831
ac95beed
BZ
832 if (drive->present && port_ops && port_ops->quirkproc)
833 port_ops->quirkproc(drive);
f01393e4 834 }
0380dad4 835
1da177e4
LT
836 for (unit = 0; unit < MAX_DRIVES; ++unit) {
837 ide_drive_t *drive = &hwif->drives[unit];
838
839 if (drive->present) {
207daeaa 840 ide_set_max_pio(drive);
1da177e4 841
1da177e4
LT
842 drive->nice1 = 1;
843
5e37bdc0 844 if (hwif->dma_ops)
8c0697cc 845 ide_set_dma(drive);
1da177e4
LT
846 }
847 }
208a08f7
KG
848
849 for (unit = 0; unit < MAX_DRIVES; ++unit) {
850 ide_drive_t *drive = &hwif->drives[unit];
851
807b90d0 852 if (hwif->host_flags & IDE_HFLAG_NO_IO_32BIT)
208a08f7
KG
853 drive->no_io_32bit = 1;
854 else
855 drive->no_io_32bit = drive->id->dword_io ? 1 : 0;
856 }
1da177e4
LT
857}
858
1da177e4
LT
859#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;
1b8ebad8 878 printk(KERN_WARNING "%s: potential IRQ problem with %s and %s\n",
1da177e4
LT
879 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{
165125e1 891 struct request_queue *q;
1da177e4
LT
892 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 */
1946089a 903
556e58fe 904 q = blk_init_queue_node(do_ide_request, &ide_lock, hwif_to_node(hwif));
1da177e4
LT
905 if (!q)
906 return 1;
907
908 q->queuedata = drive;
909 blk_queue_segment_boundary(q, 0xffff);
910
1da177e4
LT
911 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
1da177e4
LT
938 return 0;
939}
940
0947e0dc
BZ
941static 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
d5bc6592
BZ
958/*
959 * 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
26042d05 967 mutex_lock(&ide_cfg_mtx);
d5bc6592
BZ
968 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 }
26042d05 982 mutex_unlock(&ide_cfg_mtx);
d5bc6592
BZ
983}
984
af1cbba3
BZ
985static ide_hwif_t *ide_ports[MAX_HWIFS];
986
6059143a
BZ
987void ide_remove_port_from_hwgroup(ide_hwif_t *hwif)
988{
989 ide_hwgroup_t *hwgroup = hwif->hwgroup;
990
af1cbba3
BZ
991 ide_ports[hwif->index] = NULL;
992
6059143a
BZ
993 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
1da177e4
LT
1024/*
1025 * 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.
1da177e4
LT
1031 */
1032static int init_irq (ide_hwif_t *hwif)
1033{
4c3032d8 1034 struct ide_io_ports *io_ports = &hwif->io_ports;
1da177e4
LT
1035 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());
556e58fe
RT
1042 BUG_ON(hwif == NULL);
1043
ef29888e 1044 mutex_lock(&ide_cfg_mtx);
1da177e4
LT
1045 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++) {
af1cbba3
BZ
1051 ide_hwif_t *h = ide_ports[index];
1052
1053 if (h && h->hwgroup) { /* scan only initialized ports */
1da177e4
LT
1054 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;
cae5c820 1091 BUG_ON(hwif->next == hwif);
1da177e4
LT
1092 spin_unlock_irq(&ide_lock);
1093 } else {
422278ef
BZ
1094 hwgroup = kmalloc_node(sizeof(*hwgroup), GFP_KERNEL|__GFP_ZERO,
1095 hwif_to_node(hwif));
1096 if (hwgroup == NULL)
1097 goto out_up;
1da177e4
LT
1098
1099 hwif->hwgroup = hwgroup;
422278ef 1100 hwgroup->hwif = hwif->next = hwif;
1da177e4 1101
1da177e4
LT
1102 init_timer(&hwgroup->timer);
1103 hwgroup->timer.function = &ide_timer_expiry;
1104 hwgroup->timer.data = (unsigned long) hwgroup;
1105 }
1106
af1cbba3
BZ
1107 ide_ports[hwif->index] = hwif;
1108
1da177e4
LT
1109 /*
1110 * Allocate the irq, if not already obtained for another hwif
1111 */
1112 if (!match || match->irq != hwif->irq) {
7b5da4be 1113 int sa = 0;
7b892806 1114#if defined(__mc68000__)
362537b9 1115 sa = IRQF_SHARED;
e1771e20 1116#endif /* __mc68000__ */
1da177e4 1117
7b5da4be 1118 if (IDE_CHIPSET_IS_PCI(hwif->chipset))
362537b9 1119 sa = IRQF_SHARED;
1da177e4 1120
4c3032d8 1121 if (io_ports->ctl_addr)
374e042c 1122 hwif->tp_ops->set_irq(hwif, 1);
1da177e4
LT
1123
1124 if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
1125 goto out_unlink;
1126 }
1127
8a0e7e14
BZ
1128 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
7b892806 1136#if !defined(__mc68000__)
1b8ebad8 1137 printk(KERN_INFO "%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
4c3032d8
BZ
1138 io_ports->data_addr, io_ports->status_addr,
1139 io_ports->ctl_addr, hwif->irq);
1da177e4 1140#else
1b8ebad8 1141 printk(KERN_INFO "%s at 0x%08lx on irq %d", hwif->name,
4c3032d8 1142 io_ports->data_addr, hwif->irq);
7b892806 1143#endif /* __mc68000__ */
1da177e4 1144 if (match)
1b8ebad8 1145 printk(KERN_CONT " (%sed with %s)",
1da177e4 1146 hwif->sharing_irq ? "shar" : "serializ", match->name);
1b8ebad8 1147 printk(KERN_CONT "\n");
d5bc6592 1148
ef29888e 1149 mutex_unlock(&ide_cfg_mtx);
1da177e4
LT
1150 return 0;
1151out_unlink:
fbd13088 1152 ide_remove_port_from_hwgroup(hwif);
1da177e4 1153out_up:
ef29888e 1154 mutex_unlock(&ide_cfg_mtx);
1da177e4
LT
1155 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;
ed9e1982 1190 return &disk_to_dev(p)->kobj;
1da177e4
LT
1191}
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
8604affd
BZ
1231static 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
1da177e4
LT
1254static void drive_release_dev (struct device *dev)
1255{
1256 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1257
5b0c4b30
BZ
1258 ide_proc_unregister_device(drive);
1259
8604affd 1260 spin_lock_irq(&ide_lock);
8604affd 1261 ide_remove_drive_from_hwgroup(drive);
6044ec88
JJ
1262 kfree(drive->id);
1263 drive->id = NULL;
8604affd
BZ
1264 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
f36d4024 1272 complete(&drive->gendev_rel_comp);
1da177e4
LT
1273}
1274
1da177e4
LT
1275static int hwif_init(ide_hwif_t *hwif)
1276{
1277 int old_irq;
1278
1da177e4 1279 if (!hwif->irq) {
a861beb1 1280 hwif->irq = __ide_default_irq(hwif->io_ports.data_addr);
4c3032d8 1281 if (!hwif->irq) {
1b8ebad8 1282 printk(KERN_ERR "%s: disabled, no IRQ\n", hwif->name);
48535651 1283 return 0;
1da177e4
LT
1284 }
1285 }
1da177e4 1286
1da177e4
LT
1287 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
45711f1a 1293 hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
1da177e4
LT
1294 GFP_KERNEL);
1295 if (!hwif->sg_table) {
1296 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1297 goto out;
1298 }
45711f1a
JA
1299
1300 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
1da177e4
LT
1301
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 */
a861beb1 1310 hwif->irq = __ide_default_irq(hwif->io_ports.data_addr);
4c3032d8 1311 if (!hwif->irq) {
1b8ebad8 1312 printk(KERN_ERR "%s: disabled, unable to get IRQ %d\n",
1da177e4
LT
1313 hwif->name, old_irq);
1314 goto out;
1315 }
1316 if (init_irq(hwif)) {
1b8ebad8 1317 printk(KERN_ERR "%s: probed IRQ %d and default IRQ %d failed\n",
1da177e4
LT
1318 hwif->name, old_irq, hwif->irq);
1319 goto out;
1320 }
1b8ebad8 1321 printk(KERN_WARNING "%s: probed IRQ %d failed, using default\n",
1da177e4
LT
1322 hwif->name, hwif->irq);
1323
1324done:
3a4e7c96
BZ
1325 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1326 THIS_MODULE, ata_probe, ata_lock, hwif);
1da177e4
LT
1327 return 1;
1328
1329out:
1330 unregister_blkdev(hwif->major, hwif->name);
1331 return 0;
1332}
1333
9601a607
BZ
1334static 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];
c5d70cc7
BZ
1340 struct device *dev = &drive->gendev;
1341 int ret;
9601a607 1342
c5d70cc7
BZ
1343 if (!drive->present)
1344 continue;
9601a607 1345
c5d70cc7
BZ
1346 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);
9601a607
BZ
1358 }
1359}
1360
7704ca2a
BZ
1361static void ide_port_init_devices(ide_hwif_t *hwif)
1362{
ac95beed 1363 const struct ide_port_ops *port_ops = hwif->port_ops;
7704ca2a
BZ
1364 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;
807b90d0
BZ
1373 if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS)
1374 drive->no_unmask = 1;
1f2cf8b0 1375
e6d95bd1
BZ
1376 if (port_ops && port_ops->init_dev)
1377 port_ops->init_dev(drive);
1378 }
7704ca2a
BZ
1379}
1380
c413b9b9
BZ
1381static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
1382 const struct ide_port_info *d)
8447d9d5 1383{
b7691646 1384 hwif->channel = port;
c413b9b9
BZ
1385
1386 if (d->chipset)
1387 hwif->chipset = d->chipset;
1388
1389 if (d->init_iops)
1390 d->init_iops(hwif);
1391
c413b9b9
BZ
1392 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
23f8e4bf
BZ
1396 /* ->host_flags may be set by ->init_iops (or even earlier...) */
1397 hwif->host_flags |= d->host_flags;
c413b9b9
BZ
1398 hwif->pio_mask = d->pio_mask;
1399
374e042c
BZ
1400 if (d->tp_ops)
1401 hwif->tp_ops = d->tp_ops;
1402
ac95beed
BZ
1403 /* ->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
c413b9b9
BZ
1407 hwif->swdma_mask = d->swdma_mask;
1408 hwif->mwdma_mask = d->mwdma_mask;
1409 hwif->ultra_mask = d->udma_mask;
1410
b123f56e
BZ
1411 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);
ebb00fb5 1421 hwif->dma_base = 0;
b123f56e
BZ
1422 hwif->swdma_mask = 0;
1423 hwif->mwdma_mask = 0;
1424 hwif->ultra_mask = 0;
5e37bdc0
BZ
1425 } else if (d->dma_ops)
1426 hwif->dma_ops = d->dma_ops;
b123f56e 1427 }
c413b9b9 1428
1024c5f4
BZ
1429 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
c413b9b9
BZ
1435 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);
c7f6f21a 1441}
bfa14b42 1442
c7f6f21a
BZ
1443static void ide_port_cable_detect(ide_hwif_t *hwif)
1444{
ac95beed
BZ
1445 const struct ide_port_ops *port_ops = hwif->port_ops;
1446
1447 if (port_ops && port_ops->cable_detect && (hwif->ultra_mask & 0x78)) {
bfa14b42 1448 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
ac95beed 1449 hwif->cbl = port_ops->cable_detect(hwif);
bfa14b42 1450 }
c413b9b9
BZ
1451}
1452
f74c9141
BZ
1453static 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{
ca09a237 1494 int i, uninitialized_var(rc);
f74c9141
BZ
1495
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
8cdf3100
BZ
1505static unsigned int ide_indexes;
1506
fe80b937 1507/**
8cdf3100 1508 * ide_find_port_slot - find free port slot
fe80b937
BZ
1509 * @d: IDE port info
1510 *
8cdf3100 1511 * Return the new port slot index or -ENOENT if we are out of free slots.
fe80b937
BZ
1512 */
1513
8cdf3100 1514static int ide_find_port_slot(const struct ide_port_info *d)
fe80b937 1515{
8cdf3100 1516 int idx = -ENOENT;
fe80b937 1517 u8 bootable = (d && (d->host_flags & IDE_HFLAG_NON_BOOTABLE)) ? 0 : 1;
8cdf3100 1518 u8 i = (d && (d->host_flags & IDE_HFLAG_QD_2ND_PORT)) ? 1 : 0;;
fe80b937
BZ
1519
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 */
8cdf3100
BZ
1530 mutex_lock(&ide_cfg_mtx);
1531 if (MAX_HWIFS == 1) {
1532 if (ide_indexes == 0 && i == 0)
1533 idx = 1;
fe80b937 1534 } else {
8cdf3100
BZ
1535 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);
fe80b937
BZ
1543 }
1544 }
8cdf3100
BZ
1545 if (idx >= 0)
1546 ide_indexes |= (1 << idx);
1547 mutex_unlock(&ide_cfg_mtx);
fe80b937 1548
8cdf3100
BZ
1549 return idx;
1550}
256c5f8e 1551
8cdf3100
BZ
1552static 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);
fe80b937 1557}
fe80b937 1558
48c3c107
BZ
1559struct 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;
8cdf3100 1571 int idx;
48c3c107
BZ
1572
1573 if (hws[i] == NULL)
1574 continue;
1575
18de1017
BZ
1576 hwif = kzalloc(sizeof(*hwif), GFP_KERNEL);
1577 if (hwif == NULL)
1578 continue;
1579
8cdf3100
BZ
1580 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");
18de1017 1584 kfree(hwif);
8cdf3100 1585 continue;
48c3c107 1586 }
8cdf3100 1587
8cdf3100
BZ
1588 ide_init_port_data(hwif, idx);
1589
08da591e
BZ
1590 hwif->host = host;
1591
8cdf3100
BZ
1592 host->ports[i] = hwif;
1593 host->n_ports++;
48c3c107
BZ
1594 }
1595
1596 if (host->n_ports == 0) {
1597 kfree(host);
1598 return NULL;
1599 }
1600
6cdf6eb3
BZ
1601 if (hws[0])
1602 host->dev[0] = hws[0]->dev;
1603
ef0b0427
BZ
1604 if (d)
1605 host->host_flags = d->host_flags;
1606
48c3c107
BZ
1607 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)
c413b9b9
BZ
1625{
1626 ide_hwif_t *hwif, *mate = NULL;
e0d00207 1627 int i, j = 0;
8447d9d5 1628
c413b9b9 1629 for (i = 0; i < MAX_HWIFS; i++) {
e0d00207 1630 hwif = host->ports[i];
48c3c107 1631
e0d00207 1632 if (hwif == NULL) {
c413b9b9
BZ
1633 mate = NULL;
1634 continue;
1635 }
1636
c97c6aca 1637 ide_init_port_hw(hwif, hws[i]);
9fd91d95
BZ
1638 ide_port_apply_params(hwif);
1639
1640 if (d == NULL) {
1641 mate = NULL;
1642 continue;
1643 }
1644
b7691646 1645 if ((i & 1) && mate) {
c413b9b9
BZ
1646 hwif->mate = mate;
1647 mate->mate = hwif;
1648 }
1649
1650 mate = (i & 1) ? NULL : hwif;
1651
1652 ide_init_port(hwif, i & 1, d);
c7f6f21a 1653 ide_port_cable_detect(hwif);
7704ca2a 1654 ide_port_init_devices(hwif);
c413b9b9
BZ
1655 }
1656
151575e4 1657 for (i = 0; i < MAX_HWIFS; i++) {
e0d00207 1658 hwif = host->ports[i];
ba6560aa 1659
e0d00207
BZ
1660 if (hwif == NULL)
1661 continue;
139ddfca 1662
eb716beb
BZ
1663 if (ide_probe_port(hwif) == 0)
1664 hwif->present = 1;
a14dc574
BZ
1665
1666 if (hwif->chipset != ide_4drives || !hwif->mate ||
1667 !hwif->mate->present)
1668 ide_register_port(hwif);
1669
eb716beb
BZ
1670 if (hwif->present)
1671 ide_port_tune_devices(hwif);
2e13093a 1672 }
ba6560aa 1673
151575e4 1674 for (i = 0; i < MAX_HWIFS; i++) {
e0d00207 1675 hwif = host->ports[i];
2e13093a 1676
e0d00207
BZ
1677 if (hwif == NULL)
1678 continue;
ba6560aa
BZ
1679
1680 if (hwif_init(hwif) == 0) {
1681 printk(KERN_INFO "%s: failed to initialize IDE "
1682 "interface\n", hwif->name);
48535651 1683 hwif->present = 0;
ba6560aa
BZ
1684 continue;
1685 }
decdc3f0 1686
e0d00207
BZ
1687 j++;
1688
eb716beb
BZ
1689 if (hwif->present)
1690 ide_port_setup_devices(hwif);
26042d05 1691
decdc3f0 1692 ide_acpi_init(hwif);
eb716beb
BZ
1693
1694 if (hwif->present)
1695 ide_acpi_port_init_devices(hwif);
2e13093a
BZ
1696 }
1697
151575e4 1698 for (i = 0; i < MAX_HWIFS; i++) {
e0d00207 1699 hwif = host->ports[i];
2e13093a 1700
e0d00207
BZ
1701 if (hwif == NULL)
1702 continue;
ba6560aa 1703
eb716beb
BZ
1704 if (hwif->chipset == ide_unknown)
1705 hwif->chipset = ide_generic;
1706
1707 if (hwif->present)
ba6560aa 1708 hwif_register_devices(hwif);
8447d9d5
BZ
1709 }
1710
151575e4 1711 for (i = 0; i < MAX_HWIFS; i++) {
e0d00207 1712 hwif = host->ports[i];
327617e1 1713
e0d00207
BZ
1714 if (hwif == NULL)
1715 continue;
327617e1 1716
eb716beb
BZ
1717 ide_sysfs_register_port(hwif);
1718 ide_proc_register_port(hwif);
1719
1720 if (hwif->present)
d9270a3f 1721 ide_proc_port_register_devices(hwif);
8447d9d5
BZ
1722 }
1723
e0d00207 1724 return j ? 0 : -1;
8447d9d5 1725}
48c3c107 1726EXPORT_SYMBOL_GPL(ide_host_register);
151575e4 1727
6f904d01
BZ
1728int ide_host_add(const struct ide_port_info *d, hw_regs_t **hws,
1729 struct ide_host **hostp)
1730{
1731 struct ide_host *host;
8a69580e 1732 int rc;
6f904d01
BZ
1733
1734 host = ide_host_alloc(d, hws);
1735 if (host == NULL)
1736 return -ENOMEM;
1737
8a69580e
BZ
1738 rc = ide_host_register(host, d, hws);
1739 if (rc) {
1740 ide_host_free(host);
1741 return rc;
1742 }
6f904d01
BZ
1743
1744 if (hostp)
1745 *hostp = host;
1746
1747 return 0;
1748}
1749EXPORT_SYMBOL_GPL(ide_host_add);
1750
8a69580e 1751void ide_host_free(struct ide_host *host)
151575e4 1752{
8cdf3100 1753 ide_hwif_t *hwif;
151575e4 1754 int i;
8447d9d5 1755
c97c6aca 1756 for (i = 0; i < MAX_HWIFS; i++) {
8cdf3100
BZ
1757 hwif = host->ports[i];
1758
1759 if (hwif == NULL)
1760 continue;
1761
8cdf3100 1762 ide_free_port_slot(hwif->index);
18de1017 1763 kfree(hwif);
c97c6aca 1764 }
151575e4 1765
48c3c107 1766 kfree(host);
151575e4 1767}
8a69580e
BZ
1768EXPORT_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}
48c3c107 1781EXPORT_SYMBOL_GPL(ide_host_remove);
2dde7861
BZ
1782
1783void ide_port_scan(ide_hwif_t *hwif)
1784{
9fd91d95 1785 ide_port_apply_params(hwif);
2dde7861
BZ
1786 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);
3b36f66b 1801
48c3c107 1802static void ide_legacy_init_one(hw_regs_t **hws, hw_regs_t *hw,
c97c6aca 1803 u8 port_no, const struct ide_port_info *d,
d9b819a0 1804 unsigned long config)
3b36f66b 1805{
d9b819a0
BZ
1806 unsigned long base, ctl;
1807 int irq;
3b36f66b 1808
d9b819a0
BZ
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 }
3b36f66b 1818
d92f1a28
BZ
1819 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
d9b819a0
BZ
1832 ide_std_init_ports(hw, base, ctl);
1833 hw->irq = irq;
d427e836 1834 hw->chipset = d->chipset;
d6276b5f 1835 hw->config = config;
3b36f66b 1836
48c3c107 1837 hws[port_no] = hw;
d9b819a0 1838}
3b36f66b 1839
d9b819a0
BZ
1840int ide_legacy_device_add(const struct ide_port_info *d, unsigned long config)
1841{
c97c6aca 1842 hw_regs_t hw[2], *hws[] = { NULL, NULL, NULL, NULL };
0bfeee7d 1843
d9b819a0
BZ
1844 memset(&hw, 0, sizeof(hw));
1845
1846 if ((d->host_flags & IDE_HFLAG_QD_2ND_PORT) == 0)
48c3c107
BZ
1847 ide_legacy_init_one(hws, &hw[0], 0, d, config);
1848 ide_legacy_init_one(hws, &hw[1], 1, d, config);
d9b819a0 1849
48c3c107 1850 if (hws[0] == NULL && hws[1] == NULL &&
d9b819a0
BZ
1851 (d->host_flags & IDE_HFLAG_SINGLE))
1852 return -ENOENT;
3b36f66b 1853
6f904d01 1854 return ide_host_add(d, hws, NULL);
3b36f66b
BZ
1855}
1856EXPORT_SYMBOL_GPL(ide_legacy_device_add);