blob: c63c0780758344680e10ab0c397c6e41470a2992 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01002 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
3 * Copyright (C) 2005, 2007 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
6/*
7 * Mostly written by Mark Lord <mlord@pobox.com>
8 * and Gadi Oxman <gadio@netvision.net.il>
9 * and Andre Hedrick <andre@linux-ide.org>
10 *
11 * See linux/MAINTAINERS for address of current maintainer.
12 *
13 * This is the IDE probe module, as evolved from hd.c and ide.c.
14 *
Bartlomiej Zolnierkiewiczbbe4d6d2007-12-12 23:32:00 +010015 * -- increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
16 * by Andrea Arcangeli
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/types.h>
21#include <linux/string.h>
22#include <linux/kernel.h>
23#include <linux/timer.h>
24#include <linux/mm.h>
25#include <linux/interrupt.h>
26#include <linux/major.h>
27#include <linux/errno.h>
28#include <linux/genhd.h>
29#include <linux/slab.h>
30#include <linux/delay.h>
31#include <linux/ide.h>
32#include <linux/spinlock.h>
33#include <linux/kmod.h>
34#include <linux/pci.h>
FUJITA Tomonoridc817852007-10-23 09:29:58 +020035#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <asm/byteorder.h>
38#include <asm/irq.h>
39#include <asm/uaccess.h>
40#include <asm/io.h>
41
42/**
43 * generic_id - add a generic drive id
44 * @drive: drive to make an ID block for
45 *
46 * Add a fake id field to the drive we are passed. This allows
47 * use to skip a ton of NULL checks (which people always miss)
48 * and make drive properties unconditional outside of this file
49 */
50
51static void generic_id(ide_drive_t *drive)
52{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +020053 u16 *id = drive->id;
54
55 id[ATA_ID_CUR_CYLS] = id[ATA_ID_CYLS] = drive->cyl;
56 id[ATA_ID_CUR_HEADS] = id[ATA_ID_HEADS] = drive->head;
57 id[ATA_ID_CUR_SECTORS] = id[ATA_ID_SECTORS] = drive->sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
60static void ide_disk_init_chs(ide_drive_t *drive)
61{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +020062 u16 *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 /* Extract geometry if we did not already have one for the drive */
65 if (!drive->cyl || !drive->head || !drive->sect) {
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +020066 drive->cyl = drive->bios_cyl = id[ATA_ID_CYLS];
67 drive->head = drive->bios_head = id[ATA_ID_HEADS];
68 drive->sect = drive->bios_sect = id[ATA_ID_SECTORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
70
71 /* Handle logical geometry translation by the drive */
Bartlomiej Zolnierkiewiczdd8f46f2008-10-10 22:39:19 +020072 if (ata_id_current_chs_valid(id)) {
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +020073 drive->cyl = id[ATA_ID_CUR_CYLS];
74 drive->head = id[ATA_ID_CUR_HEADS];
75 drive->sect = id[ATA_ID_CUR_SECTORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77
78 /* Use physical geometry if what we have still makes no sense */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +020079 if (drive->head > 16 && id[ATA_ID_HEADS] && id[ATA_ID_HEADS] <= 16) {
80 drive->cyl = id[ATA_ID_CYLS];
81 drive->head = id[ATA_ID_HEADS];
82 drive->sect = id[ATA_ID_SECTORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84}
85
86static void ide_disk_init_mult_count(ide_drive_t *drive)
87{
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +020088 u16 *id = drive->id;
89 u8 max_multsect = id[ATA_ID_MAX_MULTSECT] & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +020091 if (max_multsect) {
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +020092 if ((max_multsect / 2) > 1)
93 id[ATA_ID_MULTSECT] = max_multsect | 0x100;
94 else
95 id[ATA_ID_MULTSECT] &= ~0x1ff;
96
97 drive->mult_req = id[ATA_ID_MULTSECT] & 0xff;
Bartlomiej Zolnierkiewicz7c51c172008-10-10 22:39:26 +020098
99 if (drive->mult_req)
Bartlomiej Zolnierkiewiczdf1f8372008-10-10 22:39:18 +0200100 drive->special.b.set_multmode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102}
103
Bartlomiej Zolnierkiewicz24630dc2009-01-02 16:12:47 +0100104static void ide_classify_ata_dev(ide_drive_t *drive)
105{
106 u16 *id = drive->id;
107 char *m = (char *)&id[ATA_ID_PROD];
108 int is_cfa = ata_id_is_cfa(id);
109
110 /* CF devices are *not* removable in Linux definition of the term */
111 if (is_cfa == 0 && (id[ATA_ID_CONFIG] & (1 << 7)))
112 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
113
114 drive->media = ide_disk;
115
116 if (!ata_id_has_unload(drive->id))
117 drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
118
119 printk(KERN_INFO "%s: %s, %s DISK drive\n", drive->name, m,
120 is_cfa ? "CFA" : "ATA");
121}
122
123static void ide_classify_atapi_dev(ide_drive_t *drive)
124{
125 u16 *id = drive->id;
126 char *m = (char *)&id[ATA_ID_PROD];
127 u8 type = (id[ATA_ID_CONFIG] >> 8) & 0x1f;
128
129 printk(KERN_INFO "%s: %s, ATAPI ", drive->name, m);
130 switch (type) {
131 case ide_floppy:
132 if (!strstr(m, "CD-ROM")) {
133 if (!strstr(m, "oppy") &&
134 !strstr(m, "poyp") &&
135 !strstr(m, "ZIP"))
136 printk(KERN_CONT "cdrom or floppy?, assuming ");
137 if (drive->media != ide_cdrom) {
138 printk(KERN_CONT "FLOPPY");
139 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
140 break;
141 }
142 }
143 /* Early cdrom models used zero */
144 type = ide_cdrom;
145 case ide_cdrom:
146 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
147#ifdef CONFIG_PPC
148 /* kludge for Apple PowerBook internal zip */
149 if (!strstr(m, "CD-ROM") && strstr(m, "ZIP")) {
150 printk(KERN_CONT "FLOPPY");
151 type = ide_floppy;
152 break;
153 }
154#endif
155 printk(KERN_CONT "CD/DVD-ROM");
156 break;
157 case ide_tape:
158 printk(KERN_CONT "TAPE");
159 break;
160 case ide_optical:
161 printk(KERN_CONT "OPTICAL");
162 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
163 break;
164 default:
165 printk(KERN_CONT "UNKNOWN (type %d)", type);
166 break;
167 }
168
169 printk(KERN_CONT " drive\n");
170 drive->media = type;
171 /* an ATAPI device ignores DRDY */
172 drive->ready_stat = 0;
173 if (ata_id_cdb_intr(id))
174 drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
175 drive->dev_flags |= IDE_DFLAG_DOORLOCKING;
176 /* we don't do head unloading on ATAPI devices */
177 drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * do_identify - identify a drive
182 * @drive: drive to identify
183 * @cmd: command used
184 *
185 * Called when we have issued a drive identify command to
186 * read and parse the results. This function is run with
187 * interrupts disabled.
188 */
Bartlomiej Zolnierkiewicz047140a2008-12-29 20:27:36 +0100189
190static void do_identify(ide_drive_t *drive, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100192 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200193 u16 *id = drive->id;
194 char *m = (char *)&id[ATA_ID_PROD];
Bartlomiej Zolnierkiewicz94b9efd2008-12-29 20:27:38 +0100195 unsigned long flags;
Bartlomiej Zolnierkiewicz24630dc2009-01-02 16:12:47 +0100196 int bswap = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Bartlomiej Zolnierkiewicz94b9efd2008-12-29 20:27:38 +0100198 /* local CPU only; some systems need this */
199 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /* read 512 bytes of id info */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200201 hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
Bartlomiej Zolnierkiewicz94b9efd2008-12-29 20:27:38 +0100202 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200204 drive->dev_flags |= IDE_DFLAG_ID_READ;
Bartlomiej Zolnierkiewicz7b9f25b2008-02-01 23:09:28 +0100205#ifdef DEBUG
206 printk(KERN_INFO "%s: dumping identify data\n", drive->name);
207 ide_dump_identify((u8 *)id);
208#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 ide_fix_driveid(id);
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /*
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200212 * ATA_CMD_ID_ATA returns little-endian info,
213 * ATA_CMD_ID_ATAPI *usually* returns little-endian info.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200215 if (cmd == ATA_CMD_ID_ATAPI) {
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200216 if ((m[0] == 'N' && m[1] == 'E') || /* NEC */
217 (m[0] == 'F' && m[1] == 'X') || /* Mitsumi */
218 (m[0] == 'P' && m[1] == 'i')) /* Pioneer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 /* Vertos drives may still be weird */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200220 bswap ^= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200222
223 ide_fixstring(m, ATA_ID_PROD_LEN, bswap);
224 ide_fixstring((char *)&id[ATA_ID_FW_REV], ATA_ID_FW_REV_LEN, bswap);
225 ide_fixstring((char *)&id[ATA_ID_SERNO], ATA_ID_SERNO_LEN, bswap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Tejun Heo699b0522007-11-05 21:42:25 +0100227 /* we depend on this a lot! */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200228 m[ATA_ID_PROD_LEN - 1] = '\0';
Tejun Heo699b0522007-11-05 21:42:25 +0100229
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200230 if (strstr(m, "E X A B Y T E N E S T"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 goto err_misc;
232
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200233 drive->dev_flags |= IDE_DFLAG_PRESENT;
234 drive->dev_flags &= ~IDE_DFLAG_DEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /*
237 * Check for an ATAPI device
238 */
Bartlomiej Zolnierkiewicz24630dc2009-01-02 16:12:47 +0100239 if (cmd == ATA_CMD_ID_ATAPI)
240 ide_classify_atapi_dev(drive);
241 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 /*
243 * Not an ATAPI device: looks like a "regular" hard disk
244 */
Bartlomiej Zolnierkiewicz24630dc2009-01-02 16:12:47 +0100245 ide_classify_ata_dev(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247err_misc:
248 kfree(id);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200249 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252/**
253 * actual_try_to_identify - send ata/atapi identify
254 * @drive: drive to identify
255 * @cmd: command to use
256 *
257 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
Bartlomiej Zolnierkiewiczf77e03c2009-03-24 23:22:53 +0100258 * and waits for a response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 *
260 * Returns: 0 device was identified
261 * 1 device timed-out (no response to identify request)
262 * 2 device aborted the command (refused to identify itself)
263 */
264
265static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
266{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100267 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200268 struct ide_io_ports *io_ports = &hwif->io_ports;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200269 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100270 int use_altstatus = 0, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 unsigned long timeout;
272 u8 s = 0, a = 0;
273
274 /* take a deep breath */
275 msleep(50);
276
Bartlomiej Zolnierkiewicz66364872008-12-02 20:40:03 +0100277 if (io_ports->ctl_addr &&
278 (hwif->host_flags & IDE_HFLAG_BROKEN_ALTSTATUS) == 0) {
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200279 a = tp_ops->read_altstatus(hwif);
280 s = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200281 if ((a ^ s) & ~ATA_IDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 /* ancient Seagate drives, broken interfaces */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100283 printk(KERN_INFO "%s: probing with STATUS(0x%02x) "
284 "instead of ALTSTATUS(0x%02x)\n",
285 drive->name, s, a);
286 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /* use non-intrusive polling */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100288 use_altstatus = 1;
289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 /* set features register for atapi
292 * identify command to be sure of reply
293 */
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200294 if (cmd == ATA_CMD_ID_ATAPI) {
Bartlomiej Zolnierkiewicz4e658372008-07-23 19:55:53 +0200295 ide_task_t task;
296
297 memset(&task, 0, sizeof(task));
298 /* disable DMA & overlap */
299 task.tf_flags = IDE_TFLAG_OUT_FEATURE;
300
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200301 tp_ops->tf_load(drive, &task);
Bartlomiej Zolnierkiewicz4e658372008-07-23 19:55:53 +0200302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 /* ask drive for ID */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200305 tp_ops->exec_command(hwif, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200307 timeout = ((cmd == ATA_CMD_ID_ATA) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200308
309 if (ide_busy_sleep(hwif, timeout, use_altstatus))
310 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200312 /* wait for IRQ and ATA_DRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 msleep(50);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200314 s = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100315
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200316 if (OK_STAT(s, ATA_DRQ, BAD_R_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /* drive returned ID */
318 do_identify(drive, cmd);
319 /* drive responded with ID */
320 rc = 0;
321 /* clear drive IRQ */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200322 (void)tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 } else {
324 /* drive refused ID */
325 rc = 2;
326 }
327 return rc;
328}
329
330/**
331 * try_to_identify - try to identify a drive
332 * @drive: drive to probe
333 * @cmd: command to use
334 *
Bartlomiej Zolnierkiewiczf77e03c2009-03-24 23:22:53 +0100335 * Issue the identify command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 */
337
338static int try_to_identify (ide_drive_t *drive, u8 cmd)
339{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100340 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200341 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /*
Bartlomiej Zolnierkiewiczf77e03c2009-03-24 23:22:53 +0100344 * Disable device IRQ. Otherwise we'll get spurious interrupts
345 * during the identify phase that the IRQ handler isn't expecting.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
Bartlomiej Zolnierkiewiczf77e03c2009-03-24 23:22:53 +0100347 if (hwif->io_ports.ctl_addr)
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200348 tp_ops->set_irq(hwif, 0);
Bartlomiej Zolnierkiewiczf77e03c2009-03-24 23:22:53 +0100349
350 return actual_try_to_identify(drive, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200353int ide_busy_sleep(ide_hwif_t *hwif, unsigned long timeout, int altstatus)
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100354{
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100355 u8 stat;
356
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200357 timeout += jiffies;
358
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100359 do {
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200360 msleep(50); /* give drive a breather */
361 stat = altstatus ? hwif->tp_ops->read_altstatus(hwif)
362 : hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200363 if ((stat & ATA_BUSY) == 0)
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100364 return 0;
365 } while (time_before(jiffies, timeout));
366
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200367 return 1; /* drive timed-out */
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100368}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200370static u8 ide_read_device(ide_drive_t *drive)
371{
372 ide_task_t task;
373
374 memset(&task, 0, sizeof(task));
375 task.tf_flags = IDE_TFLAG_IN_DEVICE;
376
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200377 drive->hwif->tp_ops->tf_read(drive, &task);
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200378
379 return task.tf.device;
380}
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/**
383 * do_probe - probe an IDE device
384 * @drive: drive to probe
385 * @cmd: command to use
386 *
387 * do_probe() has the difficult job of finding a drive if it exists,
388 * without getting hung up if it doesn't exist, without trampling on
389 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
390 *
391 * If a drive is "known" to exist (from CMOS or kernel parameters),
392 * but does not respond right away, the probe will "hang in there"
393 * for the maximum wait time (about 30 seconds), otherwise it will
394 * exit much more quickly.
395 *
396 * Returns: 0 device was identified
397 * 1 device timed-out (no response to identify request)
398 * 2 device aborted the command (refused to identify itself)
399 * 3 bad status from device (possible for ATAPI drives)
400 * 4 probe was not attempted because failure was obvious
401 */
402
403static int do_probe (ide_drive_t *drive, u8 cmd)
404{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100405 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200406 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100407 int rc;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200408 u8 present = !!(drive->dev_flags & IDE_DFLAG_PRESENT), stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200410 /* avoid waiting for inappropriate probes */
411 if (present && drive->media != ide_disk && cmd == ATA_CMD_ID_ATA)
412 return 4;
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414#ifdef DEBUG
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200415 printk(KERN_INFO "probing for %s: present=%d, media=%d, probetype=%s\n",
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200416 drive->name, present, drive->media,
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200417 (cmd == ATA_CMD_ID_ATA) ? "ATA" : "ATAPI");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418#endif
419
420 /* needed for some systems
421 * (e.g. crw9624 as drive0 with disk as slave)
422 */
423 msleep(50);
424 SELECT_DRIVE(drive);
425 msleep(50);
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200426
Bartlomiej Zolnierkiewicz7f612f22008-10-13 21:39:40 +0200427 if (ide_read_device(drive) != drive->select && present == 0) {
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +0200428 if (drive->dn & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /* exit with drive0 selected */
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100430 SELECT_DRIVE(hwif->devices[0]);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200431 /* allow ATA_BUSY to assert & clear */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 msleep(50);
433 }
434 /* no i/f present: mmm.. this should be a 4 -ml */
435 return 3;
436 }
437
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200438 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100439
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200440 if (OK_STAT(stat, ATA_DRDY, ATA_BUSY) ||
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200441 present || cmd == ATA_CMD_ID_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /* send cmd and wait */
443 if ((rc = try_to_identify(drive, cmd))) {
444 /* failed: try again */
445 rc = try_to_identify(drive,cmd);
446 }
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100447
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200448 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100449
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200450 if (stat == (ATA_BUSY | ATA_DRDY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return 4;
452
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200453 if (rc == 1 && cmd == ATA_CMD_ID_ATAPI) {
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100454 printk(KERN_ERR "%s: no response (status = 0x%02x), "
455 "resetting drive\n", drive->name, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 msleep(50);
Bartlomiej Zolnierkiewicze81a3bd2008-07-15 21:21:48 +0200457 SELECT_DRIVE(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 msleep(50);
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200459 tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200460 (void)ide_busy_sleep(hwif, WAIT_WORSTCASE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 rc = try_to_identify(drive, cmd);
462 }
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100463
464 /* ensure drive IRQ is clear */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200465 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (rc == 1)
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100468 printk(KERN_ERR "%s: no response (status = 0x%02x)\n",
469 drive->name, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 } else {
471 /* not present or maybe ATAPI */
472 rc = 3;
473 }
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +0200474 if (drive->dn & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 /* exit with drive0 selected */
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100476 SELECT_DRIVE(hwif->devices[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 msleep(50);
478 /* ensure drive irq is clear */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200479 (void)tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481 return rc;
482}
483
484/*
485 *
486 */
487static void enable_nest (ide_drive_t *drive)
488{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100489 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200490 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100491 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200493 printk(KERN_INFO "%s: enabling %s -- ",
494 hwif->name, (char *)&drive->id[ATA_ID_PROD]);
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 SELECT_DRIVE(drive);
497 msleep(50);
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200498 tp_ops->exec_command(hwif, ATA_EXABYTE_ENABLE_NEST);
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100499
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200500 if (ide_busy_sleep(hwif, WAIT_WORSTCASE, 0)) {
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100501 printk(KERN_CONT "failed (timeout)\n");
502 return;
503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 msleep(50);
506
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200507 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100508
509 if (!OK_STAT(stat, 0, BAD_STAT))
510 printk(KERN_CONT "failed (status = 0x%02x)\n", stat);
511 else
512 printk(KERN_CONT "success\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
515/**
516 * probe_for_drives - upper level drive probe
517 * @drive: drive to probe for
518 *
519 * probe_for_drive() tests for existence of a given drive using do_probe()
520 * and presents things to the user as needed.
521 *
522 * Returns: 0 no device was found
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200523 * 1 device was found
524 * (note: IDE_DFLAG_PRESENT might still be not set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 */
Bartlomiej Zolnierkiewicz047140a2008-12-29 20:27:36 +0100526
527static u8 probe_for_drive(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200529 char *m;
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /*
532 * In order to keep things simple we have an id
533 * block for all drives at all times. If the device
534 * is pre ATA or refuses ATA/ATAPI identify we
535 * will add faked data to this.
536 *
537 * Also note that 0 everywhere means "can't do X"
538 */
539
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200540 drive->dev_flags &= ~IDE_DFLAG_ID_READ;
541
Bartlomiej Zolnierkiewicz151a6702008-10-10 22:39:28 +0200542 drive->id = kzalloc(SECTOR_SIZE, GFP_KERNEL);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200543 if (drive->id == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 printk(KERN_ERR "ide: out of memory for id data.\n");
545 return 0;
546 }
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200547
548 m = (char *)&drive->id[ATA_ID_PROD];
549 strcpy(m, "UNKNOWN");
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* skip probing? */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200552 if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0) {
Bartlomiej Zolnierkiewiczc36a7e92008-10-10 22:39:23 +0200553retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /* if !(success||timed-out) */
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200555 if (do_probe(drive, ATA_CMD_ID_ATA) >= 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 /* look for ATAPI device */
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200557 (void)do_probe(drive, ATA_CMD_ID_ATAPI);
Bartlomiej Zolnierkiewiczc36a7e92008-10-10 22:39:23 +0200558
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200559 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /* drive not found */
561 return 0;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200562
Bartlomiej Zolnierkiewiczc36a7e92008-10-10 22:39:23 +0200563 if (strstr(m, "E X A B Y T E N E S T")) {
Alan Cox78595572007-07-03 22:28:35 +0200564 enable_nest(drive);
Bartlomiej Zolnierkiewiczc36a7e92008-10-10 22:39:23 +0200565 goto retry;
566 }
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* identification failed? */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200569 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (drive->media == ide_disk) {
571 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
572 drive->name, drive->cyl,
573 drive->head, drive->sect);
574 } else if (drive->media == ide_cdrom) {
575 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
576 } else {
577 /* nuke it */
578 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200579 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581 }
582 /* drive was found */
583 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200584
585 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return 0;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /* The drive wasn't being helpful. Add generic info only */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200589 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 generic_id(drive);
591 return 1;
592 }
593
594 if (drive->media == ide_disk) {
595 ide_disk_init_chs(drive);
596 ide_disk_init_mult_count(drive);
597 }
598
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200599 return !!(drive->dev_flags & IDE_DFLAG_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
Pavel Machekfc410692008-07-23 19:56:02 +0200602static void hwif_release_dev(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
605
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800606 complete(&hwif->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200609static int ide_register_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Randy Dunlap349ae232006-10-03 01:14:23 -0700611 int ret;
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* register with global device tree */
Kay Sieversdc09c782008-12-29 20:27:36 +0100614 dev_set_name(&hwif->gendev, hwif->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 hwif->gendev.driver_data = hwif;
Andreas Schwabbb54aff2009-01-19 13:46:56 +0100616 if (hwif->gendev.parent == NULL)
617 hwif->gendev.parent = hwif->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 hwif->gendev.release = hwif_release_dev;
Bartlomiej Zolnierkiewicz96d40942009-01-02 16:12:47 +0100619
Randy Dunlap349ae232006-10-03 01:14:23 -0700620 ret = device_register(&hwif->gendev);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200621 if (ret < 0) {
Randy Dunlap349ae232006-10-03 01:14:23 -0700622 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200623 __func__, ret);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200624 goto out;
625 }
626
Greg Kroah-Hartman3ee074b2008-07-21 20:03:34 -0700627 hwif->portdev = device_create(ide_port_class, &hwif->gendev,
628 MKDEV(0, 0), hwif, hwif->name);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200629 if (IS_ERR(hwif->portdev)) {
630 ret = PTR_ERR(hwif->portdev);
631 device_unregister(&hwif->gendev);
632 }
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200633out:
634 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Bartlomiej Zolnierkiewiczc860a8f2008-02-01 23:09:34 +0100637/**
638 * ide_port_wait_ready - wait for port to become ready
639 * @hwif: IDE port
640 *
641 * This is needed on some PPCs and a bunch of BIOS-less embedded
642 * platforms. Typical cases are:
643 *
644 * - The firmware hard reset the disk before booting the kernel,
645 * the drive is still doing it's poweron-reset sequence, that
646 * can take up to 30 seconds.
647 *
648 * - The firmware does nothing (or no firmware), the device is
649 * still in POST state (same as above actually).
650 *
651 * - Some CD/DVD/Writer combo drives tend to drive the bus during
652 * their reset sequence even when they are non-selected slave
653 * devices, thus preventing discovery of the main HD.
654 *
655 * Doing this wait-for-non-busy should not harm any existing
656 * configuration and fix some issues like the above.
657 *
658 * BenH.
659 *
660 * Returns 0 on success, error code (< 0) otherwise.
661 */
662
663static int ide_port_wait_ready(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100665 ide_drive_t *drive;
666 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
669
670 /* Let HW settle down a bit from whatever init state we
671 * come from */
672 mdelay(2);
673
674 /* Wait for BSY bit to go away, spec timeout is 30 seconds,
675 * I know of at least one disk who takes 31 seconds, I use 35
676 * here to be safe
677 */
678 rc = ide_wait_not_busy(hwif, 35000);
679 if (rc)
680 return rc;
681
682 /* Now make sure both master & slave are ready */
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100683 ide_port_for_each_dev(i, drive, hwif) {
Jonas Stare82661052007-11-27 21:35:53 +0100684 /* Ignore disks that we will not probe for later. */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200685 if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0 ||
686 (drive->dev_flags & IDE_DFLAG_PRESENT)) {
Jonas Stare82661052007-11-27 21:35:53 +0100687 SELECT_DRIVE(drive);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200688 hwif->tp_ops->set_irq(hwif, 1);
Jonas Stare82661052007-11-27 21:35:53 +0100689 mdelay(2);
690 rc = ide_wait_not_busy(hwif, 35000);
691 if (rc)
692 goto out;
693 } else
694 printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
695 drive->name);
696 }
697out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /* Exit function with master reselected (let's be sane) */
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100699 if (i)
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100700 SELECT_DRIVE(hwif->devices[0]);
Jonas Stare82661052007-11-27 21:35:53 +0100701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return rc;
703}
704
705/**
706 * ide_undecoded_slave - look for bad CF adapters
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200707 * @dev1: slave device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 *
709 * Analyse the drives on the interface and attempt to decide if we
710 * have the same drive viewed twice. This occurs with crap CF adapters
711 * and PCMCIA sometimes.
712 */
713
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200714void ide_undecoded_slave(ide_drive_t *dev1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100716 ide_drive_t *dev0 = dev1->hwif->devices[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200718 if ((dev1->dn & 1) == 0 || (dev0->dev_flags & IDE_DFLAG_PRESENT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return;
720
721 /* If the models don't match they are not the same product */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200722 if (strcmp((char *)&dev0->id[ATA_ID_PROD],
723 (char *)&dev1->id[ATA_ID_PROD]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return;
725
726 /* Serial numbers do not match */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200727 if (strncmp((char *)&dev0->id[ATA_ID_SERNO],
728 (char *)&dev1->id[ATA_ID_SERNO], ATA_ID_SERNO_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return;
730
731 /* No serial number, thankfully very rare for CF */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200732 if (*(char *)&dev0->id[ATA_ID_SERNO] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return;
734
735 /* Appears to be an IDE flash adapter with decode bugs */
736 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
737
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200738 dev1->dev_flags &= ~IDE_DFLAG_PRESENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
741EXPORT_SYMBOL_GPL(ide_undecoded_slave);
742
Bartlomiej Zolnierkiewicz9d501522008-02-01 23:09:36 +0100743static int ide_probe_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100745 ide_drive_t *drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 unsigned int irqd;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100747 int i, rc = -ENODEV;
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100748
749 BUG_ON(hwif->present);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100751 if ((hwif->devices[0]->dev_flags & IDE_DFLAG_NOPROBE) &&
752 (hwif->devices[1]->dev_flags & IDE_DFLAG_NOPROBE))
Bartlomiej Zolnierkiewicz9d501522008-02-01 23:09:36 +0100753 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 /*
756 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
757 * we'll install our IRQ driver much later...
758 */
759 irqd = hwif->irq;
760 if (irqd)
761 disable_irq(hwif->irq);
762
Bartlomiej Zolnierkiewiczc860a8f2008-02-01 23:09:34 +0100763 if (ide_port_wait_ready(hwif) == -EBUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
765
766 /*
Bartlomiej Zolnierkiewiczf367bed2008-03-29 19:48:21 +0100767 * Second drive should only exist if first drive was found,
768 * but a lot of cdrom drives are configured as single slaves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 */
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100770 ide_port_for_each_dev(i, drive, hwif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 (void) probe_for_drive(drive);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200772 if (drive->dev_flags & IDE_DFLAG_PRESENT)
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100773 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
Bartlomiej Zolnierkiewicze460a592008-04-27 15:38:24 +0200775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /*
777 * Use cached IRQ number. It might be (and is...) changed by probe
778 * code above
779 */
780 if (irqd)
781 enable_irq(irqd);
782
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100783 return rc;
Bartlomiej Zolnierkiewicze84e7ea2008-02-01 23:09:36 +0100784}
785
786static void ide_port_tune_devices(ide_hwif_t *hwif)
787{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200788 const struct ide_port_ops *port_ops = hwif->port_ops;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100789 ide_drive_t *drive;
790 int i;
Bartlomiej Zolnierkiewicze84e7ea2008-02-01 23:09:36 +0100791
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100792 ide_port_for_each_present_dev(i, drive, hwif) {
793 if (port_ops && port_ops->quirkproc)
794 port_ops->quirkproc(drive);
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100795 }
Bartlomiej Zolnierkiewicz0380dad2007-06-08 15:14:29 +0200796
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100797 ide_port_for_each_present_dev(i, drive, hwif) {
798 ide_set_max_pio(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100800 drive->dev_flags |= IDE_DFLAG_NICE1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100802 if (hwif->dma_ops)
803 ide_set_dma(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805}
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 * init request queue
809 */
810static int ide_init_queue(ide_drive_t *drive)
811{
Jens Axboe165125e2007-07-24 09:28:11 +0200812 struct request_queue *q;
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100813 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 int max_sectors = 256;
815 int max_sg_entries = PRD_ENTRIES;
816
817 /*
818 * Our default set up assumes the normal IDE case,
819 * that is 64K segmenting, standard PRD setup
820 * and LBA28. Some drivers then impose their own
821 * limits and LBA48 we could raise it but as yet
822 * do not.
823 */
Christoph Lameter19460892005-06-23 00:08:19 -0700824
Bartlomiej Zolnierkiewicz201bffa2009-01-02 16:12:50 +0100825 q = blk_init_queue_node(do_ide_request, NULL, hwif_to_node(hwif));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (!q)
827 return 1;
828
829 q->queuedata = drive;
830 blk_queue_segment_boundary(q, 0xffff);
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (hwif->rqsize < max_sectors)
833 max_sectors = hwif->rqsize;
834 blk_queue_max_sectors(q, max_sectors);
835
836#ifdef CONFIG_PCI
837 /* When we have an IOMMU, we may have a problem where pci_map_sg()
838 * creates segments that don't completely match our boundary
839 * requirements and thus need to be broken up again. Because it
840 * doesn't align properly either, we may actually have to break up
841 * to more segments than what was we got in the first place, a max
842 * worst case is twice as many.
843 * This will be fixed once we teach pci_map_sg() about our boundary
844 * requirements, hopefully soon. *FIXME*
845 */
846 if (!PCI_DMA_BUS_IS_PHYS)
847 max_sg_entries >>= 1;
848#endif /* CONFIG_PCI */
849
850 blk_queue_max_hw_segments(q, max_sg_entries);
851 blk_queue_max_phys_segments(q, max_sg_entries);
852
853 /* assign drive queue */
854 drive->queue = q;
855
856 /* needs drive->queue to be set */
857 ide_toggle_bounce(drive, 1);
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return 0;
860}
861
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +0100862static DEFINE_MUTEX(ide_cfg_mtx);
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864/*
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100865 * For any present drive:
866 * - allocate the block device queue
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100867 */
Elias Oltmannse415e492008-10-13 21:39:45 +0200868static int ide_port_setup_devices(ide_hwif_t *hwif)
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100869{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100870 ide_drive_t *drive;
Elias Oltmannse415e492008-10-13 21:39:45 +0200871 int i, j = 0;
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100872
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +0200873 mutex_lock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100874 ide_port_for_each_present_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100875 if (ide_init_queue(drive)) {
876 printk(KERN_ERR "ide: failed to init %s\n",
877 drive->name);
Elias Oltmannse415e492008-10-13 21:39:45 +0200878 kfree(drive->id);
879 drive->id = NULL;
880 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100881 continue;
882 }
883
Elias Oltmannse415e492008-10-13 21:39:45 +0200884 j++;
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100885 }
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +0200886 mutex_unlock(&ide_cfg_mtx);
Elias Oltmannse415e492008-10-13 21:39:45 +0200887
888 return j;
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100889}
890
891/*
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100892 * This routine sets up the IRQ for an IDE interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 */
894static int init_irq (ide_hwif_t *hwif)
895{
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200896 struct ide_io_ports *io_ports = &hwif->io_ports;
Stanislaw Gruszka849d7132009-03-05 16:10:57 +0100897 irq_handler_t irq_handler;
Bartlomiej Zolnierkiewiczae86afa2009-01-06 17:20:48 +0100898 int sa = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Stanislaw Gruszka849d7132009-03-05 16:10:57 +0100900 irq_handler = hwif->host->irq_handler;
901 if (irq_handler == NULL)
902 irq_handler = ide_intr;
903
Adrian Bunk7b892802008-02-06 01:36:29 -0800904#if defined(__mc68000__)
Bartlomiej Zolnierkiewiczae86afa2009-01-06 17:20:48 +0100905 sa = IRQF_SHARED;
Bartlomiej Zolnierkiewicze1771e22008-02-11 00:32:15 +0100906#endif /* __mc68000__ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Bartlomiej Zolnierkiewiczae86afa2009-01-06 17:20:48 +0100908 if (hwif->chipset == ide_pci)
909 sa = IRQF_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Bartlomiej Zolnierkiewiczae86afa2009-01-06 17:20:48 +0100911 if (io_ports->ctl_addr)
912 hwif->tp_ops->set_irq(hwif, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Stanislaw Gruszka849d7132009-03-05 16:10:57 +0100914 if (request_irq(hwif->irq, irq_handler, sa, hwif->name, hwif))
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100915 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Bartlomiej Zolnierkiewicz8a0e7e12008-02-02 19:56:41 +0100917 if (!hwif->rqsize) {
918 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
919 (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
920 hwif->rqsize = 256;
921 else
922 hwif->rqsize = 65536;
923 }
924
Adrian Bunk7b892802008-02-06 01:36:29 -0800925#if !defined(__mc68000__)
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200926 printk(KERN_INFO "%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200927 io_ports->data_addr, io_ports->status_addr,
928 io_ports->ctl_addr, hwif->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929#else
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200930 printk(KERN_INFO "%s at 0x%08lx on irq %d", hwif->name,
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200931 io_ports->data_addr, hwif->irq);
Adrian Bunk7b892802008-02-06 01:36:29 -0800932#endif /* __mc68000__ */
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100933 if (hwif->host->host_flags & IDE_HFLAG_SERIALIZE)
934 printk(KERN_CONT " (serialized)");
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200935 printk(KERN_CONT "\n");
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938out_up:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return 1;
940}
941
942static int ata_lock(dev_t dev, void *data)
943{
944 /* FIXME: we want to pin hwif down */
945 return 0;
946}
947
948static struct kobject *ata_probe(dev_t dev, int *part, void *data)
949{
950 ide_hwif_t *hwif = data;
951 int unit = *part >> PARTN_BITS;
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100952 ide_drive_t *drive = hwif->devices[unit];
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200953
954 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return NULL;
956
957 if (drive->media == ide_disk)
958 request_module("ide-disk");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (drive->media == ide_cdrom || drive->media == ide_optical)
960 request_module("ide-cd");
961 if (drive->media == ide_tape)
962 request_module("ide-tape");
963 if (drive->media == ide_floppy)
964 request_module("ide-floppy");
965
966 return NULL;
967}
968
969static struct kobject *exact_match(dev_t dev, int *part, void *data)
970{
971 struct gendisk *p = data;
972 *part &= (1 << PARTN_BITS) - 1;
Tejun Heoed9e1982008-08-25 19:56:05 +0900973 return &disk_to_dev(p)->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
976static int exact_lock(dev_t dev, void *data)
977{
978 struct gendisk *p = data;
979
980 if (!get_disk(p))
981 return -1;
982 return 0;
983}
984
985void ide_register_region(struct gendisk *disk)
986{
987 blk_register_region(MKDEV(disk->major, disk->first_minor),
988 disk->minors, NULL, exact_match, exact_lock, disk);
989}
990
991EXPORT_SYMBOL_GPL(ide_register_region);
992
993void ide_unregister_region(struct gendisk *disk)
994{
995 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
996 disk->minors);
997}
998
999EXPORT_SYMBOL_GPL(ide_unregister_region);
1000
1001void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
1002{
1003 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz7f612f22008-10-13 21:39:40 +02001004 unsigned int unit = drive->dn & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 disk->major = hwif->major;
1007 disk->first_minor = unit << PARTN_BITS;
1008 sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
1009 disk->queue = drive->queue;
1010}
1011
1012EXPORT_SYMBOL_GPL(ide_init_disk);
1013
1014static void drive_release_dev (struct device *dev)
1015{
1016 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +01001017 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Bartlomiej Zolnierkiewicz5b0c4b32008-04-18 00:46:22 +02001019 ide_proc_unregister_device(drive);
1020
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +01001021 spin_lock_irq(&hwif->lock);
Jesper Juhl6044ec82005-11-07 01:01:32 -08001022 kfree(drive->id);
1023 drive->id = NULL;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001024 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001025 /* Messed up locking ... */
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +01001026 spin_unlock_irq(&hwif->lock);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001027 blk_cleanup_queue(drive->queue);
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +01001028 spin_lock_irq(&hwif->lock);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001029 drive->queue = NULL;
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +01001030 spin_unlock_irq(&hwif->lock);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001031
Aleksey Makarovf36d4022006-01-09 15:59:27 -08001032 complete(&drive->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035static int hwif_init(ide_hwif_t *hwif)
1036{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (!hwif->irq) {
Bartlomiej Zolnierkiewicz8b07ed22009-03-24 23:22:52 +01001038 printk(KERN_ERR "%s: disabled, no IRQ\n", hwif->name);
1039 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (register_blkdev(hwif->major, hwif->name))
1043 return 0;
1044
1045 if (!hwif->sg_max_nents)
1046 hwif->sg_max_nents = PRD_ENTRIES;
1047
Jens Axboe45711f12007-10-22 21:19:53 +02001048 hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 GFP_KERNEL);
1050 if (!hwif->sg_table) {
1051 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1052 goto out;
1053 }
Jens Axboe45711f12007-10-22 21:19:53 +02001054
1055 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (init_irq(hwif)) {
Bartlomiej Zolnierkiewicz8b07ed22009-03-24 23:22:52 +01001058 printk(KERN_ERR "%s: disabled, unable to get IRQ %d\n",
1059 hwif->name, hwif->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 goto out;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Bartlomiej Zolnierkiewicz3a4e7c92008-02-02 19:56:40 +01001063 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1064 THIS_MODULE, ata_probe, ata_lock, hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return 1;
1066
1067out:
1068 unregister_blkdev(hwif->major, hwif->name);
1069 return 0;
1070}
1071
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001072static void hwif_register_devices(ide_hwif_t *hwif)
1073{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001074 ide_drive_t *drive;
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001075 unsigned int i;
1076
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +01001077 ide_port_for_each_present_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewiczc5d70cc2008-02-02 19:56:41 +01001078 struct device *dev = &drive->gendev;
1079 int ret;
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001080
Kay Sieversdc09c782008-12-29 20:27:36 +01001081 dev_set_name(dev, "%u.%u", hwif->index, i);
Bartlomiej Zolnierkiewiczc5d70cc2008-02-02 19:56:41 +01001082 dev->parent = &hwif->gendev;
1083 dev->bus = &ide_bus_type;
1084 dev->driver_data = drive;
1085 dev->release = drive_release_dev;
1086
1087 ret = device_register(dev);
1088 if (ret < 0)
1089 printk(KERN_WARNING "IDE: %s: device_register error: "
1090 "%d\n", __func__, ret);
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001091 }
1092}
1093
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001094static void ide_port_init_devices(ide_hwif_t *hwif)
1095{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001096 const struct ide_port_ops *port_ops = hwif->port_ops;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001097 ide_drive_t *drive;
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001098 int i;
1099
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001100 ide_port_for_each_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +02001101 drive->dn = i + hwif->channel * 2;
1102
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001103 if (hwif->host_flags & IDE_HFLAG_IO_32BIT)
1104 drive->io_32bit = 1;
Bartlomiej Zolnierkiewicz7610c4f2009-03-24 23:22:38 +01001105 if (hwif->host_flags & IDE_HFLAG_NO_IO_32BIT)
1106 drive->dev_flags |= IDE_DFLAG_NO_IO_32BIT;
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001107 if (hwif->host_flags & IDE_HFLAG_UNMASK_IRQS)
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001108 drive->dev_flags |= IDE_DFLAG_UNMASK;
Bartlomiej Zolnierkiewicz807b90d2008-02-02 19:56:40 +01001109 if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS)
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001110 drive->dev_flags |= IDE_DFLAG_NO_UNMASK;
Bartlomiej Zolnierkiewicz1f2cf8b2008-02-02 19:56:40 +01001111
Bartlomiej Zolnierkiewicze6d95bd2008-07-16 20:33:42 +02001112 if (port_ops && port_ops->init_dev)
1113 port_ops->init_dev(drive);
1114 }
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001115}
1116
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001117static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
1118 const struct ide_port_info *d)
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001119{
Adrian Bunkb7691642008-06-10 20:56:36 +02001120 hwif->channel = port;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001121
1122 if (d->chipset)
1123 hwif->chipset = d->chipset;
1124
1125 if (d->init_iops)
1126 d->init_iops(hwif);
1127
Bartlomiej Zolnierkiewicz23f8e4b2008-05-01 14:08:51 +02001128 /* ->host_flags may be set by ->init_iops (or even earlier...) */
1129 hwif->host_flags |= d->host_flags;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001130 hwif->pio_mask = d->pio_mask;
1131
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001132 if (d->tp_ops)
1133 hwif->tp_ops = d->tp_ops;
1134
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001135 /* ->set_pio_mode for DTC2278 is currently limited to port 0 */
1136 if (hwif->chipset != ide_dtc2278 || hwif->channel == 0)
1137 hwif->port_ops = d->port_ops;
1138
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001139 hwif->swdma_mask = d->swdma_mask;
1140 hwif->mwdma_mask = d->mwdma_mask;
1141 hwif->ultra_mask = d->udma_mask;
1142
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001143 if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
1144 int rc;
1145
Sergei Shtylyov592b5312009-01-06 17:21:02 +01001146 hwif->dma_ops = d->dma_ops;
1147
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001148 if (d->init_dma)
1149 rc = d->init_dma(hwif, d);
1150 else
1151 rc = ide_hwif_setup_dma(hwif, d);
1152
1153 if (rc < 0) {
1154 printk(KERN_INFO "%s: DMA disabled\n", hwif->name);
Sergei Shtylyov592b5312009-01-06 17:21:02 +01001155
1156 hwif->dma_ops = NULL;
Bartlomiej Zolnierkiewiczebb00fb2008-07-23 19:55:51 +02001157 hwif->dma_base = 0;
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001158 hwif->swdma_mask = 0;
1159 hwif->mwdma_mask = 0;
1160 hwif->ultra_mask = 0;
Sergei Shtylyov592b5312009-01-06 17:21:02 +01001161 }
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001162 }
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001163
Bartlomiej Zolnierkiewicz1024c5f2008-05-04 17:03:41 +02001164 if ((d->host_flags & IDE_HFLAG_SERIALIZE) ||
Bartlomiej Zolnierkiewicz702c0262008-12-29 20:27:36 +01001165 ((d->host_flags & IDE_HFLAG_SERIALIZE_DMA) && hwif->dma_base))
1166 hwif->host->host_flags |= IDE_HFLAG_SERIALIZE;
Bartlomiej Zolnierkiewicz1024c5f2008-05-04 17:03:41 +02001167
Bartlomiej Zolnierkiewicz6b492492008-12-29 20:27:34 +01001168 if (d->max_sectors)
1169 hwif->rqsize = d->max_sectors;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001170
1171 /* call chipset specific routine for each enabled port */
1172 if (d->init_hwif)
1173 d->init_hwif(hwif);
Bartlomiej Zolnierkiewiczc7f6f212008-04-18 00:46:22 +02001174}
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001175
Bartlomiej Zolnierkiewiczc7f6f212008-04-18 00:46:22 +02001176static void ide_port_cable_detect(ide_hwif_t *hwif)
1177{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001178 const struct ide_port_ops *port_ops = hwif->port_ops;
1179
1180 if (port_ops && port_ops->cable_detect && (hwif->ultra_mask & 0x78)) {
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001181 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001182 hwif->cbl = port_ops->cable_detect(hwif);
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001183 }
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001184}
1185
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001186static const u8 ide_hwif_to_major[] =
1187 { IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR, IDE4_MAJOR,
1188 IDE5_MAJOR, IDE6_MAJOR, IDE7_MAJOR, IDE8_MAJOR, IDE9_MAJOR };
1189
1190static void ide_port_init_devices_data(ide_hwif_t *hwif)
1191{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001192 ide_drive_t *drive;
1193 int i;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001194
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001195 ide_port_for_each_dev(i, drive, hwif) {
1196 u8 j = (hwif->index * MAX_DRIVES) + i;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001197
1198 memset(drive, 0, sizeof(*drive));
1199
1200 drive->media = ide_disk;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001201 drive->select = (i << 4) | ATA_DEVICE_OBS;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001202 drive->hwif = hwif;
1203 drive->ready_stat = ATA_DRDY;
1204 drive->bad_wstat = BAD_W_STAT;
1205 drive->special.b.recalibrate = 1;
1206 drive->special.b.set_geometry = 1;
1207 drive->name[0] = 'h';
1208 drive->name[1] = 'd';
1209 drive->name[2] = 'a' + j;
1210 drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
1211
1212 INIT_LIST_HEAD(&drive->list);
1213 init_completion(&drive->gendev_rel_comp);
1214 }
1215}
1216
1217static void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
1218{
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001219 /* fill in any non-zero initial values */
1220 hwif->index = index;
1221 hwif->major = ide_hwif_to_major[index];
1222
1223 hwif->name[0] = 'i';
1224 hwif->name[1] = 'd';
1225 hwif->name[2] = 'e';
1226 hwif->name[3] = '0' + index;
1227
Bartlomiej Zolnierkiewicz73629512009-03-24 23:22:39 +01001228 spin_lock_init(&hwif->lock);
1229
1230 init_timer(&hwif->timer);
1231 hwif->timer.function = &ide_timer_expiry;
1232 hwif->timer.data = (unsigned long)hwif;
1233
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001234 init_completion(&hwif->gendev_rel_comp);
1235
1236 hwif->tp_ops = &default_tp_ops;
1237
1238 ide_port_init_devices_data(hwif);
1239}
1240
1241static void ide_init_port_hw(ide_hwif_t *hwif, hw_regs_t *hw)
1242{
1243 memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports));
1244 hwif->irq = hw->irq;
1245 hwif->chipset = hw->chipset;
1246 hwif->dev = hw->dev;
1247 hwif->gendev.parent = hw->parent ? hw->parent : hw->dev;
1248 hwif->ack_intr = hw->ack_intr;
1249 hwif->config_data = hw->config;
1250}
1251
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001252static unsigned int ide_indexes;
1253
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001254/**
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001255 * ide_find_port_slot - find free port slot
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001256 * @d: IDE port info
1257 *
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001258 * Return the new port slot index or -ENOENT if we are out of free slots.
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001259 */
1260
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001261static int ide_find_port_slot(const struct ide_port_info *d)
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001262{
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001263 int idx = -ENOENT;
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001264 u8 bootable = (d && (d->host_flags & IDE_HFLAG_NON_BOOTABLE)) ? 0 : 1;
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001265 u8 i = (d && (d->host_flags & IDE_HFLAG_QD_2ND_PORT)) ? 1 : 0;;
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001266
1267 /*
1268 * Claim an unassigned slot.
1269 *
1270 * Give preference to claiming other slots before claiming ide0/ide1,
1271 * just in case there's another interface yet-to-be-scanned
1272 * which uses ports 0x1f0/0x170 (the ide0/ide1 defaults).
1273 *
1274 * Unless there is a bootable card that does not use the standard
1275 * ports 0x1f0/0x170 (the ide0/ide1 defaults).
1276 */
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001277 mutex_lock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewicz75d21ff2008-10-13 21:39:33 +02001278 if (bootable) {
1279 if ((ide_indexes | i) != (1 << MAX_HWIFS) - 1)
1280 idx = ffz(ide_indexes | i);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001281 } else {
Bartlomiej Zolnierkiewicz75d21ff2008-10-13 21:39:33 +02001282 if ((ide_indexes | 3) != (1 << MAX_HWIFS) - 1)
1283 idx = ffz(ide_indexes | 3);
1284 else if ((ide_indexes & 3) != 3)
1285 idx = ffz(ide_indexes);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001286 }
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001287 if (idx >= 0)
1288 ide_indexes |= (1 << idx);
1289 mutex_unlock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001290
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001291 return idx;
1292}
Bartlomiej Zolnierkiewiczeb3aff52008-07-16 20:33:42 +02001293
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001294static void ide_free_port_slot(int idx)
1295{
1296 mutex_lock(&ide_cfg_mtx);
1297 ide_indexes &= ~(1 << idx);
1298 mutex_unlock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001299}
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001300
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001301static void ide_port_free_devices(ide_hwif_t *hwif)
1302{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001303 ide_drive_t *drive;
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001304 int i;
1305
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001306 ide_port_for_each_dev(i, drive, hwif)
1307 kfree(drive);
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001308}
1309
1310static int ide_port_alloc_devices(ide_hwif_t *hwif, int node)
1311{
1312 int i;
1313
1314 for (i = 0; i < MAX_DRIVES; i++) {
1315 ide_drive_t *drive;
1316
1317 drive = kzalloc_node(sizeof(*drive), GFP_KERNEL, node);
1318 if (drive == NULL)
1319 goto out_nomem;
1320
1321 hwif->devices[i] = drive;
1322 }
1323 return 0;
1324
1325out_nomem:
1326 ide_port_free_devices(hwif);
1327 return -ENOMEM;
1328}
1329
Bartlomiej Zolnierkiewicza36223b2008-10-13 21:39:43 +02001330struct ide_host *ide_host_alloc(const struct ide_port_info *d, hw_regs_t **hws)
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001331{
1332 struct ide_host *host;
Bartlomiej Zolnierkiewicza32296f2009-01-06 17:20:56 +01001333 struct device *dev = hws[0] ? hws[0]->dev : NULL;
1334 int node = dev ? dev_to_node(dev) : -1;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001335 int i;
1336
Bartlomiej Zolnierkiewicza32296f2009-01-06 17:20:56 +01001337 host = kzalloc_node(sizeof(*host), GFP_KERNEL, node);
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001338 if (host == NULL)
1339 return NULL;
1340
Bartlomiej Zolnierkiewicza36223b2008-10-13 21:39:43 +02001341 for (i = 0; i < MAX_HOST_PORTS; i++) {
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001342 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001343 int idx;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001344
1345 if (hws[i] == NULL)
1346 continue;
1347
Bartlomiej Zolnierkiewicza32296f2009-01-06 17:20:56 +01001348 hwif = kzalloc_node(sizeof(*hwif), GFP_KERNEL, node);
Bartlomiej Zolnierkiewicz18de1012008-07-23 19:55:58 +02001349 if (hwif == NULL)
1350 continue;
1351
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001352 if (ide_port_alloc_devices(hwif, node) < 0) {
1353 kfree(hwif);
1354 continue;
1355 }
1356
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001357 idx = ide_find_port_slot(d);
1358 if (idx < 0) {
1359 printk(KERN_ERR "%s: no free slot for interface\n",
1360 d ? d->name : "ide");
Bartlomiej Zolnierkiewicz18de1012008-07-23 19:55:58 +02001361 kfree(hwif);
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001362 continue;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001363 }
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001364
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001365 ide_init_port_data(hwif, idx);
1366
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +02001367 hwif->host = host;
1368
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001369 host->ports[i] = hwif;
1370 host->n_ports++;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001371 }
1372
1373 if (host->n_ports == 0) {
1374 kfree(host);
1375 return NULL;
1376 }
1377
Bartlomiej Zolnierkiewicza32296f2009-01-06 17:20:56 +01001378 host->dev[0] = dev;
Bartlomiej Zolnierkiewicz6cdf6eb2008-07-24 22:53:14 +02001379
Bartlomiej Zolnierkiewiczfeb22b72008-10-10 22:39:32 +02001380 if (d) {
1381 host->init_chipset = d->init_chipset;
Bartlomiej Zolnierkiewiczef0b0422008-07-24 22:53:19 +02001382 host->host_flags = d->host_flags;
Bartlomiej Zolnierkiewiczfeb22b72008-10-10 22:39:32 +02001383 }
Bartlomiej Zolnierkiewiczef0b0422008-07-24 22:53:19 +02001384
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001385 return host;
1386}
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001387EXPORT_SYMBOL_GPL(ide_host_alloc);
1388
Bartlomiej Zolnierkiewicz9a100f42009-02-02 20:12:21 +01001389static void ide_port_free(ide_hwif_t *hwif)
1390{
1391 ide_port_free_devices(hwif);
1392 ide_free_port_slot(hwif->index);
1393 kfree(hwif);
1394}
1395
1396static void ide_disable_port(ide_hwif_t *hwif)
1397{
1398 struct ide_host *host = hwif->host;
1399 int i;
1400
1401 printk(KERN_INFO "%s: disabling port\n", hwif->name);
1402
1403 for (i = 0; i < MAX_HOST_PORTS; i++) {
1404 if (host->ports[i] == hwif) {
1405 host->ports[i] = NULL;
1406 host->n_ports--;
1407 }
1408 }
1409
1410 ide_port_free(hwif);
1411}
1412
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001413int ide_host_register(struct ide_host *host, const struct ide_port_info *d,
1414 hw_regs_t **hws)
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001415{
1416 ide_hwif_t *hwif, *mate = NULL;
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001417 int i, j = 0;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001418
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001419 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001420 if (hwif == NULL) {
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001421 mate = NULL;
1422 continue;
1423 }
1424
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +02001425 ide_init_port_hw(hwif, hws[i]);
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +02001426 ide_port_apply_params(hwif);
1427
1428 if (d == NULL) {
1429 mate = NULL;
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +02001430 } else {
1431 if ((i & 1) && mate) {
1432 hwif->mate = mate;
1433 mate->mate = hwif;
1434 }
1435
1436 mate = (i & 1) ? NULL : hwif;
1437
1438 ide_init_port(hwif, i & 1, d);
1439 ide_port_cable_detect(hwif);
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +02001440 }
1441
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001442 ide_port_init_devices(hwif);
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001443 }
1444
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001445 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001446 if (hwif == NULL)
1447 continue;
Bartlomiej Zolnierkiewicz139ddfca2008-02-01 23:09:36 +01001448
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001449 if (ide_probe_port(hwif) == 0)
1450 hwif->present = 1;
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +01001451
1452 if (hwif->chipset != ide_4drives || !hwif->mate ||
Bartlomiej Zolnierkiewicz9a100f42009-02-02 20:12:21 +01001453 !hwif->mate->present) {
1454 if (ide_register_port(hwif)) {
1455 ide_disable_port(hwif);
1456 continue;
1457 }
1458 }
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +01001459
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001460 if (hwif->present)
1461 ide_port_tune_devices(hwif);
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001462 }
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001463
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001464 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001465 if (hwif == NULL)
1466 continue;
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001467
1468 if (hwif_init(hwif) == 0) {
1469 printk(KERN_INFO "%s: failed to initialize IDE "
1470 "interface\n", hwif->name);
Ian Campbell51d6ac72009-02-02 20:12:22 +01001471 device_unregister(&hwif->gendev);
1472 ide_disable_port(hwif);
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001473 continue;
1474 }
Bartlomiej Zolnierkiewiczdecdc3f2008-02-02 19:56:41 +01001475
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001476 if (hwif->present)
Elias Oltmannse415e492008-10-13 21:39:45 +02001477 if (ide_port_setup_devices(hwif) == 0) {
1478 hwif->present = 0;
1479 continue;
1480 }
1481
1482 j++;
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +02001483
Bartlomiej Zolnierkiewicz8b803bd2009-03-24 23:22:41 +01001484 ide_acpi_init_port(hwif);
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001485
1486 if (hwif->present)
1487 ide_acpi_port_init_devices(hwif);
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001488 }
1489
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001490 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001491 if (hwif == NULL)
1492 continue;
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001493
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001494 if (hwif->present)
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001495 hwif_register_devices(hwif);
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001496 }
1497
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001498 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001499 if (hwif == NULL)
1500 continue;
Bartlomiej Zolnierkiewicz327617e2008-02-02 19:56:43 +01001501
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001502 ide_sysfs_register_port(hwif);
1503 ide_proc_register_port(hwif);
1504
1505 if (hwif->present)
Bartlomiej Zolnierkiewiczd9270a32008-02-02 19:56:43 +01001506 ide_proc_port_register_devices(hwif);
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001507 }
1508
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001509 return j ? 0 : -1;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001510}
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001511EXPORT_SYMBOL_GPL(ide_host_register);
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001512
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001513int ide_host_add(const struct ide_port_info *d, hw_regs_t **hws,
1514 struct ide_host **hostp)
1515{
1516 struct ide_host *host;
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001517 int rc;
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001518
1519 host = ide_host_alloc(d, hws);
1520 if (host == NULL)
1521 return -ENOMEM;
1522
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001523 rc = ide_host_register(host, d, hws);
1524 if (rc) {
1525 ide_host_free(host);
1526 return rc;
1527 }
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001528
1529 if (hostp)
1530 *hostp = host;
1531
1532 return 0;
1533}
1534EXPORT_SYMBOL_GPL(ide_host_add);
1535
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001536static void __ide_port_unregister_devices(ide_hwif_t *hwif)
1537{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001538 ide_drive_t *drive;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001539 int i;
1540
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +01001541 ide_port_for_each_present_dev(i, drive, hwif) {
1542 device_unregister(&drive->gendev);
1543 wait_for_completion(&drive->gendev_rel_comp);
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001544 }
1545}
1546
1547void ide_port_unregister_devices(ide_hwif_t *hwif)
1548{
1549 mutex_lock(&ide_cfg_mtx);
1550 __ide_port_unregister_devices(hwif);
1551 hwif->present = 0;
1552 ide_port_init_devices_data(hwif);
1553 mutex_unlock(&ide_cfg_mtx);
1554}
1555EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
1556
1557/**
1558 * ide_unregister - free an IDE interface
1559 * @hwif: IDE interface
1560 *
1561 * Perform the final unregister of an IDE interface.
1562 *
1563 * Locking:
1564 * The caller must not hold the IDE locks.
1565 *
1566 * It is up to the caller to be sure there is no pending I/O here,
1567 * and that the interface will not be reopened (present/vanishing
1568 * locking isn't yet done BTW).
1569 */
1570
1571static void ide_unregister(ide_hwif_t *hwif)
1572{
1573 BUG_ON(in_interrupt());
1574 BUG_ON(irqs_disabled());
1575
1576 mutex_lock(&ide_cfg_mtx);
1577
1578 if (hwif->present) {
1579 __ide_port_unregister_devices(hwif);
1580 hwif->present = 0;
1581 }
1582
1583 ide_proc_unregister_port(hwif);
1584
1585 free_irq(hwif->irq, hwif);
1586
1587 device_unregister(hwif->portdev);
1588 device_unregister(&hwif->gendev);
1589 wait_for_completion(&hwif->gendev_rel_comp);
1590
1591 /*
1592 * Remove us from the kernel's knowledge
1593 */
1594 blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
1595 kfree(hwif->sg_table);
1596 unregister_blkdev(hwif->major, hwif->name);
1597
1598 ide_release_dma_engine(hwif);
1599
1600 mutex_unlock(&ide_cfg_mtx);
1601}
1602
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001603void ide_host_free(struct ide_host *host)
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001604{
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001605 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001606 int i;
1607
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001608 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicz9a100f42009-02-02 20:12:21 +01001609 if (hwif)
1610 ide_port_free(hwif);
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +02001611 }
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001612
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001613 kfree(host);
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001614}
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001615EXPORT_SYMBOL_GPL(ide_host_free);
1616
1617void ide_host_remove(struct ide_host *host)
1618{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001619 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001620 int i;
1621
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001622 ide_host_for_each_port(i, hwif, host) {
1623 if (hwif)
1624 ide_unregister(hwif);
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001625 }
1626
1627 ide_host_free(host);
1628}
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001629EXPORT_SYMBOL_GPL(ide_host_remove);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001630
1631void ide_port_scan(ide_hwif_t *hwif)
1632{
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +02001633 ide_port_apply_params(hwif);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001634 ide_port_cable_detect(hwif);
1635 ide_port_init_devices(hwif);
1636
1637 if (ide_probe_port(hwif) < 0)
1638 return;
1639
1640 hwif->present = 1;
1641
1642 ide_port_tune_devices(hwif);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001643 ide_port_setup_devices(hwif);
Bartlomiej Zolnierkiewicze630fcb2009-01-06 17:21:00 +01001644 ide_acpi_port_init_devices(hwif);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001645 hwif_register_devices(hwif);
1646 ide_proc_port_register_devices(hwif);
1647}
1648EXPORT_SYMBOL_GPL(ide_port_scan);