blob: 5e0c3fb3b43a70ded4d5f4575c380569f635353c [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/**
Bartlomiej Zolnierkiewicza1828072009-03-24 23:22:53 +0100253 * try_to_identify - send ATA/ATAPI identify
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 * @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
Bartlomiej Zolnierkiewicza1828072009-03-24 23:22:53 +0100265static int try_to_identify(ide_drive_t *drive, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
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
Bartlomiej Zolnierkiewicza1828072009-03-24 23:22:53 +0100274 /*
275 * Disable device IRQ. Otherwise we'll get spurious interrupts
276 * during the identify phase that the IRQ handler isn't expecting.
277 */
278 if (io_ports->ctl_addr)
279 tp_ops->set_irq(hwif, 0);
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 /* take a deep breath */
282 msleep(50);
283
Bartlomiej Zolnierkiewicz66364872008-12-02 20:40:03 +0100284 if (io_ports->ctl_addr &&
285 (hwif->host_flags & IDE_HFLAG_BROKEN_ALTSTATUS) == 0) {
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200286 a = tp_ops->read_altstatus(hwif);
287 s = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200288 if ((a ^ s) & ~ATA_IDX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 /* ancient Seagate drives, broken interfaces */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100290 printk(KERN_INFO "%s: probing with STATUS(0x%02x) "
291 "instead of ALTSTATUS(0x%02x)\n",
292 drive->name, s, a);
293 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /* use non-intrusive polling */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100295 use_altstatus = 1;
296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 /* set features register for atapi
299 * identify command to be sure of reply
300 */
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200301 if (cmd == ATA_CMD_ID_ATAPI) {
Bartlomiej Zolnierkiewicz4e658372008-07-23 19:55:53 +0200302 ide_task_t task;
303
304 memset(&task, 0, sizeof(task));
305 /* disable DMA & overlap */
306 task.tf_flags = IDE_TFLAG_OUT_FEATURE;
307
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200308 tp_ops->tf_load(drive, &task);
Bartlomiej Zolnierkiewicz4e658372008-07-23 19:55:53 +0200309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /* ask drive for ID */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200312 tp_ops->exec_command(hwif, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200314 timeout = ((cmd == ATA_CMD_ID_ATA) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200315
316 if (ide_busy_sleep(hwif, timeout, use_altstatus))
317 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200319 /* wait for IRQ and ATA_DRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 msleep(50);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200321 s = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100322
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200323 if (OK_STAT(s, ATA_DRQ, BAD_R_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /* drive returned ID */
325 do_identify(drive, cmd);
326 /* drive responded with ID */
327 rc = 0;
328 /* clear drive IRQ */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200329 (void)tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 } else {
331 /* drive refused ID */
332 rc = 2;
333 }
334 return rc;
335}
336
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200337int ide_busy_sleep(ide_hwif_t *hwif, unsigned long timeout, int altstatus)
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100338{
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100339 u8 stat;
340
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200341 timeout += jiffies;
342
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100343 do {
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200344 msleep(50); /* give drive a breather */
345 stat = altstatus ? hwif->tp_ops->read_altstatus(hwif)
346 : hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200347 if ((stat & ATA_BUSY) == 0)
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100348 return 0;
349 } while (time_before(jiffies, timeout));
350
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200351 return 1; /* drive timed-out */
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100352}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200354static u8 ide_read_device(ide_drive_t *drive)
355{
356 ide_task_t task;
357
358 memset(&task, 0, sizeof(task));
359 task.tf_flags = IDE_TFLAG_IN_DEVICE;
360
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200361 drive->hwif->tp_ops->tf_read(drive, &task);
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200362
363 return task.tf.device;
364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366/**
367 * do_probe - probe an IDE device
368 * @drive: drive to probe
369 * @cmd: command to use
370 *
371 * do_probe() has the difficult job of finding a drive if it exists,
372 * without getting hung up if it doesn't exist, without trampling on
373 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
374 *
375 * If a drive is "known" to exist (from CMOS or kernel parameters),
376 * but does not respond right away, the probe will "hang in there"
377 * for the maximum wait time (about 30 seconds), otherwise it will
378 * exit much more quickly.
379 *
380 * Returns: 0 device was identified
381 * 1 device timed-out (no response to identify request)
382 * 2 device aborted the command (refused to identify itself)
383 * 3 bad status from device (possible for ATAPI drives)
384 * 4 probe was not attempted because failure was obvious
385 */
386
387static int do_probe (ide_drive_t *drive, u8 cmd)
388{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100389 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200390 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100391 int rc;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200392 u8 present = !!(drive->dev_flags & IDE_DFLAG_PRESENT), stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200394 /* avoid waiting for inappropriate probes */
395 if (present && drive->media != ide_disk && cmd == ATA_CMD_ID_ATA)
396 return 4;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398#ifdef DEBUG
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200399 printk(KERN_INFO "probing for %s: present=%d, media=%d, probetype=%s\n",
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200400 drive->name, present, drive->media,
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200401 (cmd == ATA_CMD_ID_ATA) ? "ATA" : "ATAPI");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402#endif
403
404 /* needed for some systems
405 * (e.g. crw9624 as drive0 with disk as slave)
406 */
407 msleep(50);
408 SELECT_DRIVE(drive);
409 msleep(50);
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200410
Bartlomiej Zolnierkiewicz7f612f22008-10-13 21:39:40 +0200411 if (ide_read_device(drive) != drive->select && present == 0) {
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +0200412 if (drive->dn & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /* exit with drive0 selected */
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100414 SELECT_DRIVE(hwif->devices[0]);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200415 /* allow ATA_BUSY to assert & clear */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 msleep(50);
417 }
418 /* no i/f present: mmm.. this should be a 4 -ml */
419 return 3;
420 }
421
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200422 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100423
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200424 if (OK_STAT(stat, ATA_DRDY, ATA_BUSY) ||
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200425 present || cmd == ATA_CMD_ID_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /* send cmd and wait */
427 if ((rc = try_to_identify(drive, cmd))) {
428 /* failed: try again */
429 rc = try_to_identify(drive,cmd);
430 }
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100431
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200432 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100433
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200434 if (stat == (ATA_BUSY | ATA_DRDY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return 4;
436
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200437 if (rc == 1 && cmd == ATA_CMD_ID_ATAPI) {
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100438 printk(KERN_ERR "%s: no response (status = 0x%02x), "
439 "resetting drive\n", drive->name, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 msleep(50);
Bartlomiej Zolnierkiewicze81a3bd2008-07-15 21:21:48 +0200441 SELECT_DRIVE(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 msleep(50);
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200443 tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200444 (void)ide_busy_sleep(hwif, WAIT_WORSTCASE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 rc = try_to_identify(drive, cmd);
446 }
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100447
448 /* ensure drive IRQ is clear */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200449 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (rc == 1)
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100452 printk(KERN_ERR "%s: no response (status = 0x%02x)\n",
453 drive->name, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 } else {
455 /* not present or maybe ATAPI */
456 rc = 3;
457 }
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +0200458 if (drive->dn & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 /* exit with drive0 selected */
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100460 SELECT_DRIVE(hwif->devices[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 msleep(50);
462 /* ensure drive irq is clear */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200463 (void)tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465 return rc;
466}
467
468/*
469 *
470 */
471static void enable_nest (ide_drive_t *drive)
472{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100473 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200474 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100475 u8 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200477 printk(KERN_INFO "%s: enabling %s -- ",
478 hwif->name, (char *)&drive->id[ATA_ID_PROD]);
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 SELECT_DRIVE(drive);
481 msleep(50);
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200482 tp_ops->exec_command(hwif, ATA_EXABYTE_ENABLE_NEST);
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100483
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200484 if (ide_busy_sleep(hwif, WAIT_WORSTCASE, 0)) {
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100485 printk(KERN_CONT "failed (timeout)\n");
486 return;
487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 msleep(50);
490
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200491 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100492
493 if (!OK_STAT(stat, 0, BAD_STAT))
494 printk(KERN_CONT "failed (status = 0x%02x)\n", stat);
495 else
496 printk(KERN_CONT "success\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499/**
500 * probe_for_drives - upper level drive probe
501 * @drive: drive to probe for
502 *
503 * probe_for_drive() tests for existence of a given drive using do_probe()
504 * and presents things to the user as needed.
505 *
506 * Returns: 0 no device was found
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200507 * 1 device was found
508 * (note: IDE_DFLAG_PRESENT might still be not set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 */
Bartlomiej Zolnierkiewicz047140a2008-12-29 20:27:36 +0100510
511static u8 probe_for_drive(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200513 char *m;
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /*
516 * In order to keep things simple we have an id
517 * block for all drives at all times. If the device
518 * is pre ATA or refuses ATA/ATAPI identify we
519 * will add faked data to this.
520 *
521 * Also note that 0 everywhere means "can't do X"
522 */
523
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200524 drive->dev_flags &= ~IDE_DFLAG_ID_READ;
525
Bartlomiej Zolnierkiewicz151a6702008-10-10 22:39:28 +0200526 drive->id = kzalloc(SECTOR_SIZE, GFP_KERNEL);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200527 if (drive->id == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 printk(KERN_ERR "ide: out of memory for id data.\n");
529 return 0;
530 }
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200531
532 m = (char *)&drive->id[ATA_ID_PROD];
533 strcpy(m, "UNKNOWN");
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* skip probing? */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200536 if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0) {
Bartlomiej Zolnierkiewiczc36a7e92008-10-10 22:39:23 +0200537retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* if !(success||timed-out) */
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200539 if (do_probe(drive, ATA_CMD_ID_ATA) >= 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /* look for ATAPI device */
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200541 (void)do_probe(drive, ATA_CMD_ID_ATAPI);
Bartlomiej Zolnierkiewiczc36a7e92008-10-10 22:39:23 +0200542
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200543 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* drive not found */
545 return 0;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200546
Bartlomiej Zolnierkiewiczc36a7e92008-10-10 22:39:23 +0200547 if (strstr(m, "E X A B Y T E N E S T")) {
Alan Cox78595572007-07-03 22:28:35 +0200548 enable_nest(drive);
Bartlomiej Zolnierkiewiczc36a7e92008-10-10 22:39:23 +0200549 goto retry;
550 }
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /* identification failed? */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200553 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (drive->media == ide_disk) {
555 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
556 drive->name, drive->cyl,
557 drive->head, drive->sect);
558 } else if (drive->media == ide_cdrom) {
559 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
560 } else {
561 /* nuke it */
562 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200563 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565 }
566 /* drive was found */
567 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200568
569 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return 0;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* The drive wasn't being helpful. Add generic info only */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200573 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 generic_id(drive);
575 return 1;
576 }
577
578 if (drive->media == ide_disk) {
579 ide_disk_init_chs(drive);
580 ide_disk_init_mult_count(drive);
581 }
582
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200583 return !!(drive->dev_flags & IDE_DFLAG_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Pavel Machekfc410692008-07-23 19:56:02 +0200586static void hwif_release_dev(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
589
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800590 complete(&hwif->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200593static int ide_register_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Randy Dunlap349ae232006-10-03 01:14:23 -0700595 int ret;
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 /* register with global device tree */
Kay Sieversdc09c782008-12-29 20:27:36 +0100598 dev_set_name(&hwif->gendev, hwif->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 hwif->gendev.driver_data = hwif;
Andreas Schwabbb54aff2009-01-19 13:46:56 +0100600 if (hwif->gendev.parent == NULL)
601 hwif->gendev.parent = hwif->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 hwif->gendev.release = hwif_release_dev;
Bartlomiej Zolnierkiewicz96d40942009-01-02 16:12:47 +0100603
Randy Dunlap349ae232006-10-03 01:14:23 -0700604 ret = device_register(&hwif->gendev);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200605 if (ret < 0) {
Randy Dunlap349ae232006-10-03 01:14:23 -0700606 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200607 __func__, ret);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200608 goto out;
609 }
610
Greg Kroah-Hartman3ee074b2008-07-21 20:03:34 -0700611 hwif->portdev = device_create(ide_port_class, &hwif->gendev,
612 MKDEV(0, 0), hwif, hwif->name);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200613 if (IS_ERR(hwif->portdev)) {
614 ret = PTR_ERR(hwif->portdev);
615 device_unregister(&hwif->gendev);
616 }
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200617out:
618 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Bartlomiej Zolnierkiewiczc860a8f2008-02-01 23:09:34 +0100621/**
622 * ide_port_wait_ready - wait for port to become ready
623 * @hwif: IDE port
624 *
625 * This is needed on some PPCs and a bunch of BIOS-less embedded
626 * platforms. Typical cases are:
627 *
628 * - The firmware hard reset the disk before booting the kernel,
629 * the drive is still doing it's poweron-reset sequence, that
630 * can take up to 30 seconds.
631 *
632 * - The firmware does nothing (or no firmware), the device is
633 * still in POST state (same as above actually).
634 *
635 * - Some CD/DVD/Writer combo drives tend to drive the bus during
636 * their reset sequence even when they are non-selected slave
637 * devices, thus preventing discovery of the main HD.
638 *
639 * Doing this wait-for-non-busy should not harm any existing
640 * configuration and fix some issues like the above.
641 *
642 * BenH.
643 *
644 * Returns 0 on success, error code (< 0) otherwise.
645 */
646
647static int ide_port_wait_ready(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100649 ide_drive_t *drive;
650 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
653
654 /* Let HW settle down a bit from whatever init state we
655 * come from */
656 mdelay(2);
657
658 /* Wait for BSY bit to go away, spec timeout is 30 seconds,
659 * I know of at least one disk who takes 31 seconds, I use 35
660 * here to be safe
661 */
662 rc = ide_wait_not_busy(hwif, 35000);
663 if (rc)
664 return rc;
665
666 /* Now make sure both master & slave are ready */
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100667 ide_port_for_each_dev(i, drive, hwif) {
Jonas Stare82661052007-11-27 21:35:53 +0100668 /* Ignore disks that we will not probe for later. */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200669 if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0 ||
670 (drive->dev_flags & IDE_DFLAG_PRESENT)) {
Jonas Stare82661052007-11-27 21:35:53 +0100671 SELECT_DRIVE(drive);
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200672 hwif->tp_ops->set_irq(hwif, 1);
Jonas Stare82661052007-11-27 21:35:53 +0100673 mdelay(2);
674 rc = ide_wait_not_busy(hwif, 35000);
675 if (rc)
676 goto out;
677 } else
678 printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
679 drive->name);
680 }
681out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /* Exit function with master reselected (let's be sane) */
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100683 if (i)
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100684 SELECT_DRIVE(hwif->devices[0]);
Jonas Stare82661052007-11-27 21:35:53 +0100685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return rc;
687}
688
689/**
690 * ide_undecoded_slave - look for bad CF adapters
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200691 * @dev1: slave device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 *
693 * Analyse the drives on the interface and attempt to decide if we
694 * have the same drive viewed twice. This occurs with crap CF adapters
695 * and PCMCIA sometimes.
696 */
697
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200698void ide_undecoded_slave(ide_drive_t *dev1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100700 ide_drive_t *dev0 = dev1->hwif->devices[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200702 if ((dev1->dn & 1) == 0 || (dev0->dev_flags & IDE_DFLAG_PRESENT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return;
704
705 /* If the models don't match they are not the same product */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200706 if (strcmp((char *)&dev0->id[ATA_ID_PROD],
707 (char *)&dev1->id[ATA_ID_PROD]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return;
709
710 /* Serial numbers do not match */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200711 if (strncmp((char *)&dev0->id[ATA_ID_SERNO],
712 (char *)&dev1->id[ATA_ID_SERNO], ATA_ID_SERNO_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return;
714
715 /* No serial number, thankfully very rare for CF */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200716 if (*(char *)&dev0->id[ATA_ID_SERNO] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return;
718
719 /* Appears to be an IDE flash adapter with decode bugs */
720 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
721
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200722 dev1->dev_flags &= ~IDE_DFLAG_PRESENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
725EXPORT_SYMBOL_GPL(ide_undecoded_slave);
726
Bartlomiej Zolnierkiewicz9d501522008-02-01 23:09:36 +0100727static int ide_probe_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100729 ide_drive_t *drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 unsigned int irqd;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100731 int i, rc = -ENODEV;
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100732
733 BUG_ON(hwif->present);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100735 if ((hwif->devices[0]->dev_flags & IDE_DFLAG_NOPROBE) &&
736 (hwif->devices[1]->dev_flags & IDE_DFLAG_NOPROBE))
Bartlomiej Zolnierkiewicz9d501522008-02-01 23:09:36 +0100737 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /*
740 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
741 * we'll install our IRQ driver much later...
742 */
743 irqd = hwif->irq;
744 if (irqd)
745 disable_irq(hwif->irq);
746
Bartlomiej Zolnierkiewiczc860a8f2008-02-01 23:09:34 +0100747 if (ide_port_wait_ready(hwif) == -EBUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
749
750 /*
Bartlomiej Zolnierkiewiczf367bed2008-03-29 19:48:21 +0100751 * Second drive should only exist if first drive was found,
752 * but a lot of cdrom drives are configured as single slaves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 */
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100754 ide_port_for_each_dev(i, drive, hwif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 (void) probe_for_drive(drive);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200756 if (drive->dev_flags & IDE_DFLAG_PRESENT)
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100757 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Bartlomiej Zolnierkiewicze460a592008-04-27 15:38:24 +0200759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /*
761 * Use cached IRQ number. It might be (and is...) changed by probe
762 * code above
763 */
764 if (irqd)
765 enable_irq(irqd);
766
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100767 return rc;
Bartlomiej Zolnierkiewicze84e7ea2008-02-01 23:09:36 +0100768}
769
770static void ide_port_tune_devices(ide_hwif_t *hwif)
771{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200772 const struct ide_port_ops *port_ops = hwif->port_ops;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100773 ide_drive_t *drive;
774 int i;
Bartlomiej Zolnierkiewicze84e7ea2008-02-01 23:09:36 +0100775
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100776 ide_port_for_each_present_dev(i, drive, hwif) {
777 if (port_ops && port_ops->quirkproc)
778 port_ops->quirkproc(drive);
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100779 }
Bartlomiej Zolnierkiewicz0380dad2007-06-08 15:14:29 +0200780
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100781 ide_port_for_each_present_dev(i, drive, hwif) {
782 ide_set_max_pio(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100784 drive->dev_flags |= IDE_DFLAG_NICE1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100786 if (hwif->dma_ops)
787 ide_set_dma(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789}
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 * init request queue
793 */
794static int ide_init_queue(ide_drive_t *drive)
795{
Jens Axboe165125e2007-07-24 09:28:11 +0200796 struct request_queue *q;
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100797 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 int max_sectors = 256;
799 int max_sg_entries = PRD_ENTRIES;
800
801 /*
802 * Our default set up assumes the normal IDE case,
803 * that is 64K segmenting, standard PRD setup
804 * and LBA28. Some drivers then impose their own
805 * limits and LBA48 we could raise it but as yet
806 * do not.
807 */
Christoph Lameter19460892005-06-23 00:08:19 -0700808
Bartlomiej Zolnierkiewicz201bffa2009-01-02 16:12:50 +0100809 q = blk_init_queue_node(do_ide_request, NULL, hwif_to_node(hwif));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (!q)
811 return 1;
812
813 q->queuedata = drive;
814 blk_queue_segment_boundary(q, 0xffff);
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (hwif->rqsize < max_sectors)
817 max_sectors = hwif->rqsize;
818 blk_queue_max_sectors(q, max_sectors);
819
820#ifdef CONFIG_PCI
821 /* When we have an IOMMU, we may have a problem where pci_map_sg()
822 * creates segments that don't completely match our boundary
823 * requirements and thus need to be broken up again. Because it
824 * doesn't align properly either, we may actually have to break up
825 * to more segments than what was we got in the first place, a max
826 * worst case is twice as many.
827 * This will be fixed once we teach pci_map_sg() about our boundary
828 * requirements, hopefully soon. *FIXME*
829 */
830 if (!PCI_DMA_BUS_IS_PHYS)
831 max_sg_entries >>= 1;
832#endif /* CONFIG_PCI */
833
834 blk_queue_max_hw_segments(q, max_sg_entries);
835 blk_queue_max_phys_segments(q, max_sg_entries);
836
837 /* assign drive queue */
838 drive->queue = q;
839
840 /* needs drive->queue to be set */
841 ide_toggle_bounce(drive, 1);
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return 0;
844}
845
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +0100846static DEFINE_MUTEX(ide_cfg_mtx);
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848/*
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100849 * For any present drive:
850 * - allocate the block device queue
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100851 */
Elias Oltmannse415e492008-10-13 21:39:45 +0200852static int ide_port_setup_devices(ide_hwif_t *hwif)
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100853{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100854 ide_drive_t *drive;
Elias Oltmannse415e492008-10-13 21:39:45 +0200855 int i, j = 0;
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100856
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +0200857 mutex_lock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100858 ide_port_for_each_present_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100859 if (ide_init_queue(drive)) {
860 printk(KERN_ERR "ide: failed to init %s\n",
861 drive->name);
Elias Oltmannse415e492008-10-13 21:39:45 +0200862 kfree(drive->id);
863 drive->id = NULL;
864 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100865 continue;
866 }
867
Elias Oltmannse415e492008-10-13 21:39:45 +0200868 j++;
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100869 }
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +0200870 mutex_unlock(&ide_cfg_mtx);
Elias Oltmannse415e492008-10-13 21:39:45 +0200871
872 return j;
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100873}
874
875/*
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100876 * This routine sets up the IRQ for an IDE interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 */
878static int init_irq (ide_hwif_t *hwif)
879{
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200880 struct ide_io_ports *io_ports = &hwif->io_ports;
Stanislaw Gruszka849d7132009-03-05 16:10:57 +0100881 irq_handler_t irq_handler;
Bartlomiej Zolnierkiewiczae86afa2009-01-06 17:20:48 +0100882 int sa = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Stanislaw Gruszka849d7132009-03-05 16:10:57 +0100884 irq_handler = hwif->host->irq_handler;
885 if (irq_handler == NULL)
886 irq_handler = ide_intr;
887
Adrian Bunk7b892802008-02-06 01:36:29 -0800888#if defined(__mc68000__)
Bartlomiej Zolnierkiewiczae86afa2009-01-06 17:20:48 +0100889 sa = IRQF_SHARED;
Bartlomiej Zolnierkiewicze1771e22008-02-11 00:32:15 +0100890#endif /* __mc68000__ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Bartlomiej Zolnierkiewiczae86afa2009-01-06 17:20:48 +0100892 if (hwif->chipset == ide_pci)
893 sa = IRQF_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Bartlomiej Zolnierkiewiczae86afa2009-01-06 17:20:48 +0100895 if (io_ports->ctl_addr)
896 hwif->tp_ops->set_irq(hwif, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Stanislaw Gruszka849d7132009-03-05 16:10:57 +0100898 if (request_irq(hwif->irq, irq_handler, sa, hwif->name, hwif))
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100899 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Bartlomiej Zolnierkiewicz8a0e7e12008-02-02 19:56:41 +0100901 if (!hwif->rqsize) {
902 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
903 (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
904 hwif->rqsize = 256;
905 else
906 hwif->rqsize = 65536;
907 }
908
Adrian Bunk7b892802008-02-06 01:36:29 -0800909#if !defined(__mc68000__)
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200910 printk(KERN_INFO "%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200911 io_ports->data_addr, io_ports->status_addr,
912 io_ports->ctl_addr, hwif->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913#else
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200914 printk(KERN_INFO "%s at 0x%08lx on irq %d", hwif->name,
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200915 io_ports->data_addr, hwif->irq);
Adrian Bunk7b892802008-02-06 01:36:29 -0800916#endif /* __mc68000__ */
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100917 if (hwif->host->host_flags & IDE_HFLAG_SERIALIZE)
918 printk(KERN_CONT " (serialized)");
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200919 printk(KERN_CONT "\n");
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922out_up:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return 1;
924}
925
926static int ata_lock(dev_t dev, void *data)
927{
928 /* FIXME: we want to pin hwif down */
929 return 0;
930}
931
932static struct kobject *ata_probe(dev_t dev, int *part, void *data)
933{
934 ide_hwif_t *hwif = data;
935 int unit = *part >> PARTN_BITS;
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100936 ide_drive_t *drive = hwif->devices[unit];
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200937
938 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return NULL;
940
941 if (drive->media == ide_disk)
942 request_module("ide-disk");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (drive->media == ide_cdrom || drive->media == ide_optical)
944 request_module("ide-cd");
945 if (drive->media == ide_tape)
946 request_module("ide-tape");
947 if (drive->media == ide_floppy)
948 request_module("ide-floppy");
949
950 return NULL;
951}
952
953static struct kobject *exact_match(dev_t dev, int *part, void *data)
954{
955 struct gendisk *p = data;
956 *part &= (1 << PARTN_BITS) - 1;
Tejun Heoed9e1982008-08-25 19:56:05 +0900957 return &disk_to_dev(p)->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
960static int exact_lock(dev_t dev, void *data)
961{
962 struct gendisk *p = data;
963
964 if (!get_disk(p))
965 return -1;
966 return 0;
967}
968
969void ide_register_region(struct gendisk *disk)
970{
971 blk_register_region(MKDEV(disk->major, disk->first_minor),
972 disk->minors, NULL, exact_match, exact_lock, disk);
973}
974
975EXPORT_SYMBOL_GPL(ide_register_region);
976
977void ide_unregister_region(struct gendisk *disk)
978{
979 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
980 disk->minors);
981}
982
983EXPORT_SYMBOL_GPL(ide_unregister_region);
984
985void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
986{
987 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz7f612f22008-10-13 21:39:40 +0200988 unsigned int unit = drive->dn & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 disk->major = hwif->major;
991 disk->first_minor = unit << PARTN_BITS;
992 sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
993 disk->queue = drive->queue;
994}
995
996EXPORT_SYMBOL_GPL(ide_init_disk);
997
998static void drive_release_dev (struct device *dev)
999{
1000 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +01001001 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Bartlomiej Zolnierkiewicz5b0c4b32008-04-18 00:46:22 +02001003 ide_proc_unregister_device(drive);
1004
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +01001005 spin_lock_irq(&hwif->lock);
Jesper Juhl6044ec82005-11-07 01:01:32 -08001006 kfree(drive->id);
1007 drive->id = NULL;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001008 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001009 /* Messed up locking ... */
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +01001010 spin_unlock_irq(&hwif->lock);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001011 blk_cleanup_queue(drive->queue);
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +01001012 spin_lock_irq(&hwif->lock);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001013 drive->queue = NULL;
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +01001014 spin_unlock_irq(&hwif->lock);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001015
Aleksey Makarovf36d4022006-01-09 15:59:27 -08001016 complete(&drive->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019static int hwif_init(ide_hwif_t *hwif)
1020{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (!hwif->irq) {
Bartlomiej Zolnierkiewicz8b07ed22009-03-24 23:22:52 +01001022 printk(KERN_ERR "%s: disabled, no IRQ\n", hwif->name);
1023 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (register_blkdev(hwif->major, hwif->name))
1027 return 0;
1028
1029 if (!hwif->sg_max_nents)
1030 hwif->sg_max_nents = PRD_ENTRIES;
1031
Jens Axboe45711f12007-10-22 21:19:53 +02001032 hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 GFP_KERNEL);
1034 if (!hwif->sg_table) {
1035 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1036 goto out;
1037 }
Jens Axboe45711f12007-10-22 21:19:53 +02001038
1039 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (init_irq(hwif)) {
Bartlomiej Zolnierkiewicz8b07ed22009-03-24 23:22:52 +01001042 printk(KERN_ERR "%s: disabled, unable to get IRQ %d\n",
1043 hwif->name, hwif->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 goto out;
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Bartlomiej Zolnierkiewicz3a4e7c92008-02-02 19:56:40 +01001047 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1048 THIS_MODULE, ata_probe, ata_lock, hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return 1;
1050
1051out:
1052 unregister_blkdev(hwif->major, hwif->name);
1053 return 0;
1054}
1055
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001056static void hwif_register_devices(ide_hwif_t *hwif)
1057{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001058 ide_drive_t *drive;
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001059 unsigned int i;
1060
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +01001061 ide_port_for_each_present_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewiczc5d70cc2008-02-02 19:56:41 +01001062 struct device *dev = &drive->gendev;
1063 int ret;
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001064
Kay Sieversdc09c782008-12-29 20:27:36 +01001065 dev_set_name(dev, "%u.%u", hwif->index, i);
Bartlomiej Zolnierkiewiczc5d70cc2008-02-02 19:56:41 +01001066 dev->parent = &hwif->gendev;
1067 dev->bus = &ide_bus_type;
1068 dev->driver_data = drive;
1069 dev->release = drive_release_dev;
1070
1071 ret = device_register(dev);
1072 if (ret < 0)
1073 printk(KERN_WARNING "IDE: %s: device_register error: "
1074 "%d\n", __func__, ret);
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001075 }
1076}
1077
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001078static void ide_port_init_devices(ide_hwif_t *hwif)
1079{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001080 const struct ide_port_ops *port_ops = hwif->port_ops;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001081 ide_drive_t *drive;
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001082 int i;
1083
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001084 ide_port_for_each_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +02001085 drive->dn = i + hwif->channel * 2;
1086
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001087 if (hwif->host_flags & IDE_HFLAG_IO_32BIT)
1088 drive->io_32bit = 1;
Bartlomiej Zolnierkiewicz7610c4f2009-03-24 23:22:38 +01001089 if (hwif->host_flags & IDE_HFLAG_NO_IO_32BIT)
1090 drive->dev_flags |= IDE_DFLAG_NO_IO_32BIT;
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001091 if (hwif->host_flags & IDE_HFLAG_UNMASK_IRQS)
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001092 drive->dev_flags |= IDE_DFLAG_UNMASK;
Bartlomiej Zolnierkiewicz807b90d2008-02-02 19:56:40 +01001093 if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS)
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001094 drive->dev_flags |= IDE_DFLAG_NO_UNMASK;
Bartlomiej Zolnierkiewicz1f2cf8b2008-02-02 19:56:40 +01001095
Bartlomiej Zolnierkiewicze6d95bd2008-07-16 20:33:42 +02001096 if (port_ops && port_ops->init_dev)
1097 port_ops->init_dev(drive);
1098 }
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001099}
1100
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001101static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
1102 const struct ide_port_info *d)
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001103{
Adrian Bunkb7691642008-06-10 20:56:36 +02001104 hwif->channel = port;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001105
1106 if (d->chipset)
1107 hwif->chipset = d->chipset;
1108
1109 if (d->init_iops)
1110 d->init_iops(hwif);
1111
Bartlomiej Zolnierkiewicz23f8e4b2008-05-01 14:08:51 +02001112 /* ->host_flags may be set by ->init_iops (or even earlier...) */
1113 hwif->host_flags |= d->host_flags;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001114 hwif->pio_mask = d->pio_mask;
1115
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001116 if (d->tp_ops)
1117 hwif->tp_ops = d->tp_ops;
1118
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001119 /* ->set_pio_mode for DTC2278 is currently limited to port 0 */
1120 if (hwif->chipset != ide_dtc2278 || hwif->channel == 0)
1121 hwif->port_ops = d->port_ops;
1122
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001123 hwif->swdma_mask = d->swdma_mask;
1124 hwif->mwdma_mask = d->mwdma_mask;
1125 hwif->ultra_mask = d->udma_mask;
1126
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001127 if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
1128 int rc;
1129
Sergei Shtylyov592b5312009-01-06 17:21:02 +01001130 hwif->dma_ops = d->dma_ops;
1131
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001132 if (d->init_dma)
1133 rc = d->init_dma(hwif, d);
1134 else
1135 rc = ide_hwif_setup_dma(hwif, d);
1136
1137 if (rc < 0) {
1138 printk(KERN_INFO "%s: DMA disabled\n", hwif->name);
Sergei Shtylyov592b5312009-01-06 17:21:02 +01001139
1140 hwif->dma_ops = NULL;
Bartlomiej Zolnierkiewiczebb00fb2008-07-23 19:55:51 +02001141 hwif->dma_base = 0;
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001142 hwif->swdma_mask = 0;
1143 hwif->mwdma_mask = 0;
1144 hwif->ultra_mask = 0;
Sergei Shtylyov592b5312009-01-06 17:21:02 +01001145 }
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001146 }
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001147
Bartlomiej Zolnierkiewicz1024c5f2008-05-04 17:03:41 +02001148 if ((d->host_flags & IDE_HFLAG_SERIALIZE) ||
Bartlomiej Zolnierkiewicz702c0262008-12-29 20:27:36 +01001149 ((d->host_flags & IDE_HFLAG_SERIALIZE_DMA) && hwif->dma_base))
1150 hwif->host->host_flags |= IDE_HFLAG_SERIALIZE;
Bartlomiej Zolnierkiewicz1024c5f2008-05-04 17:03:41 +02001151
Bartlomiej Zolnierkiewicz6b492492008-12-29 20:27:34 +01001152 if (d->max_sectors)
1153 hwif->rqsize = d->max_sectors;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001154
1155 /* call chipset specific routine for each enabled port */
1156 if (d->init_hwif)
1157 d->init_hwif(hwif);
Bartlomiej Zolnierkiewiczc7f6f212008-04-18 00:46:22 +02001158}
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001159
Bartlomiej Zolnierkiewiczc7f6f212008-04-18 00:46:22 +02001160static void ide_port_cable_detect(ide_hwif_t *hwif)
1161{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001162 const struct ide_port_ops *port_ops = hwif->port_ops;
1163
1164 if (port_ops && port_ops->cable_detect && (hwif->ultra_mask & 0x78)) {
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001165 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001166 hwif->cbl = port_ops->cable_detect(hwif);
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001167 }
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001168}
1169
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001170static const u8 ide_hwif_to_major[] =
1171 { IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR, IDE4_MAJOR,
1172 IDE5_MAJOR, IDE6_MAJOR, IDE7_MAJOR, IDE8_MAJOR, IDE9_MAJOR };
1173
1174static void ide_port_init_devices_data(ide_hwif_t *hwif)
1175{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001176 ide_drive_t *drive;
1177 int i;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001178
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001179 ide_port_for_each_dev(i, drive, hwif) {
1180 u8 j = (hwif->index * MAX_DRIVES) + i;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001181
1182 memset(drive, 0, sizeof(*drive));
1183
1184 drive->media = ide_disk;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001185 drive->select = (i << 4) | ATA_DEVICE_OBS;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001186 drive->hwif = hwif;
1187 drive->ready_stat = ATA_DRDY;
1188 drive->bad_wstat = BAD_W_STAT;
1189 drive->special.b.recalibrate = 1;
1190 drive->special.b.set_geometry = 1;
1191 drive->name[0] = 'h';
1192 drive->name[1] = 'd';
1193 drive->name[2] = 'a' + j;
1194 drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
1195
1196 INIT_LIST_HEAD(&drive->list);
1197 init_completion(&drive->gendev_rel_comp);
1198 }
1199}
1200
1201static void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
1202{
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001203 /* fill in any non-zero initial values */
1204 hwif->index = index;
1205 hwif->major = ide_hwif_to_major[index];
1206
1207 hwif->name[0] = 'i';
1208 hwif->name[1] = 'd';
1209 hwif->name[2] = 'e';
1210 hwif->name[3] = '0' + index;
1211
Bartlomiej Zolnierkiewicz73629512009-03-24 23:22:39 +01001212 spin_lock_init(&hwif->lock);
1213
1214 init_timer(&hwif->timer);
1215 hwif->timer.function = &ide_timer_expiry;
1216 hwif->timer.data = (unsigned long)hwif;
1217
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001218 init_completion(&hwif->gendev_rel_comp);
1219
1220 hwif->tp_ops = &default_tp_ops;
1221
1222 ide_port_init_devices_data(hwif);
1223}
1224
1225static void ide_init_port_hw(ide_hwif_t *hwif, hw_regs_t *hw)
1226{
1227 memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports));
1228 hwif->irq = hw->irq;
1229 hwif->chipset = hw->chipset;
1230 hwif->dev = hw->dev;
1231 hwif->gendev.parent = hw->parent ? hw->parent : hw->dev;
1232 hwif->ack_intr = hw->ack_intr;
1233 hwif->config_data = hw->config;
1234}
1235
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001236static unsigned int ide_indexes;
1237
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001238/**
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001239 * ide_find_port_slot - find free port slot
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001240 * @d: IDE port info
1241 *
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001242 * Return the new port slot index or -ENOENT if we are out of free slots.
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001243 */
1244
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001245static int ide_find_port_slot(const struct ide_port_info *d)
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001246{
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001247 int idx = -ENOENT;
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001248 u8 bootable = (d && (d->host_flags & IDE_HFLAG_NON_BOOTABLE)) ? 0 : 1;
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001249 u8 i = (d && (d->host_flags & IDE_HFLAG_QD_2ND_PORT)) ? 1 : 0;;
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001250
1251 /*
1252 * Claim an unassigned slot.
1253 *
1254 * Give preference to claiming other slots before claiming ide0/ide1,
1255 * just in case there's another interface yet-to-be-scanned
1256 * which uses ports 0x1f0/0x170 (the ide0/ide1 defaults).
1257 *
1258 * Unless there is a bootable card that does not use the standard
1259 * ports 0x1f0/0x170 (the ide0/ide1 defaults).
1260 */
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001261 mutex_lock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewicz75d21ff2008-10-13 21:39:33 +02001262 if (bootable) {
1263 if ((ide_indexes | i) != (1 << MAX_HWIFS) - 1)
1264 idx = ffz(ide_indexes | i);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001265 } else {
Bartlomiej Zolnierkiewicz75d21ff2008-10-13 21:39:33 +02001266 if ((ide_indexes | 3) != (1 << MAX_HWIFS) - 1)
1267 idx = ffz(ide_indexes | 3);
1268 else if ((ide_indexes & 3) != 3)
1269 idx = ffz(ide_indexes);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001270 }
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001271 if (idx >= 0)
1272 ide_indexes |= (1 << idx);
1273 mutex_unlock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001274
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001275 return idx;
1276}
Bartlomiej Zolnierkiewiczeb3aff52008-07-16 20:33:42 +02001277
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001278static void ide_free_port_slot(int idx)
1279{
1280 mutex_lock(&ide_cfg_mtx);
1281 ide_indexes &= ~(1 << idx);
1282 mutex_unlock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001283}
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001284
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001285static void ide_port_free_devices(ide_hwif_t *hwif)
1286{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001287 ide_drive_t *drive;
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001288 int i;
1289
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001290 ide_port_for_each_dev(i, drive, hwif)
1291 kfree(drive);
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001292}
1293
1294static int ide_port_alloc_devices(ide_hwif_t *hwif, int node)
1295{
1296 int i;
1297
1298 for (i = 0; i < MAX_DRIVES; i++) {
1299 ide_drive_t *drive;
1300
1301 drive = kzalloc_node(sizeof(*drive), GFP_KERNEL, node);
1302 if (drive == NULL)
1303 goto out_nomem;
1304
1305 hwif->devices[i] = drive;
1306 }
1307 return 0;
1308
1309out_nomem:
1310 ide_port_free_devices(hwif);
1311 return -ENOMEM;
1312}
1313
Bartlomiej Zolnierkiewicza36223b2008-10-13 21:39:43 +02001314struct ide_host *ide_host_alloc(const struct ide_port_info *d, hw_regs_t **hws)
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001315{
1316 struct ide_host *host;
Bartlomiej Zolnierkiewicza32296f2009-01-06 17:20:56 +01001317 struct device *dev = hws[0] ? hws[0]->dev : NULL;
1318 int node = dev ? dev_to_node(dev) : -1;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001319 int i;
1320
Bartlomiej Zolnierkiewicza32296f2009-01-06 17:20:56 +01001321 host = kzalloc_node(sizeof(*host), GFP_KERNEL, node);
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001322 if (host == NULL)
1323 return NULL;
1324
Bartlomiej Zolnierkiewicza36223b2008-10-13 21:39:43 +02001325 for (i = 0; i < MAX_HOST_PORTS; i++) {
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001326 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001327 int idx;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001328
1329 if (hws[i] == NULL)
1330 continue;
1331
Bartlomiej Zolnierkiewicza32296f2009-01-06 17:20:56 +01001332 hwif = kzalloc_node(sizeof(*hwif), GFP_KERNEL, node);
Bartlomiej Zolnierkiewicz18de1012008-07-23 19:55:58 +02001333 if (hwif == NULL)
1334 continue;
1335
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001336 if (ide_port_alloc_devices(hwif, node) < 0) {
1337 kfree(hwif);
1338 continue;
1339 }
1340
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001341 idx = ide_find_port_slot(d);
1342 if (idx < 0) {
1343 printk(KERN_ERR "%s: no free slot for interface\n",
1344 d ? d->name : "ide");
Bartlomiej Zolnierkiewicz18de1012008-07-23 19:55:58 +02001345 kfree(hwif);
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001346 continue;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001347 }
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001348
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001349 ide_init_port_data(hwif, idx);
1350
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +02001351 hwif->host = host;
1352
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001353 host->ports[i] = hwif;
1354 host->n_ports++;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001355 }
1356
1357 if (host->n_ports == 0) {
1358 kfree(host);
1359 return NULL;
1360 }
1361
Bartlomiej Zolnierkiewicza32296f2009-01-06 17:20:56 +01001362 host->dev[0] = dev;
Bartlomiej Zolnierkiewicz6cdf6eb2008-07-24 22:53:14 +02001363
Bartlomiej Zolnierkiewiczfeb22b72008-10-10 22:39:32 +02001364 if (d) {
1365 host->init_chipset = d->init_chipset;
Bartlomiej Zolnierkiewiczef0b0422008-07-24 22:53:19 +02001366 host->host_flags = d->host_flags;
Bartlomiej Zolnierkiewiczfeb22b72008-10-10 22:39:32 +02001367 }
Bartlomiej Zolnierkiewiczef0b0422008-07-24 22:53:19 +02001368
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001369 return host;
1370}
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001371EXPORT_SYMBOL_GPL(ide_host_alloc);
1372
Bartlomiej Zolnierkiewicz9a100f42009-02-02 20:12:21 +01001373static void ide_port_free(ide_hwif_t *hwif)
1374{
1375 ide_port_free_devices(hwif);
1376 ide_free_port_slot(hwif->index);
1377 kfree(hwif);
1378}
1379
1380static void ide_disable_port(ide_hwif_t *hwif)
1381{
1382 struct ide_host *host = hwif->host;
1383 int i;
1384
1385 printk(KERN_INFO "%s: disabling port\n", hwif->name);
1386
1387 for (i = 0; i < MAX_HOST_PORTS; i++) {
1388 if (host->ports[i] == hwif) {
1389 host->ports[i] = NULL;
1390 host->n_ports--;
1391 }
1392 }
1393
1394 ide_port_free(hwif);
1395}
1396
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001397int ide_host_register(struct ide_host *host, const struct ide_port_info *d,
1398 hw_regs_t **hws)
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001399{
1400 ide_hwif_t *hwif, *mate = NULL;
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001401 int i, j = 0;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001402
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001403 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001404 if (hwif == NULL) {
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001405 mate = NULL;
1406 continue;
1407 }
1408
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +02001409 ide_init_port_hw(hwif, hws[i]);
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +02001410 ide_port_apply_params(hwif);
1411
1412 if (d == NULL) {
1413 mate = NULL;
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +02001414 } else {
1415 if ((i & 1) && mate) {
1416 hwif->mate = mate;
1417 mate->mate = hwif;
1418 }
1419
1420 mate = (i & 1) ? NULL : hwif;
1421
1422 ide_init_port(hwif, i & 1, d);
1423 ide_port_cable_detect(hwif);
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +02001424 }
1425
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001426 ide_port_init_devices(hwif);
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001427 }
1428
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001429 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001430 if (hwif == NULL)
1431 continue;
Bartlomiej Zolnierkiewicz139ddfc2008-02-01 23:09:36 +01001432
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001433 if (ide_probe_port(hwif) == 0)
1434 hwif->present = 1;
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +01001435
1436 if (hwif->chipset != ide_4drives || !hwif->mate ||
Bartlomiej Zolnierkiewicz9a100f42009-02-02 20:12:21 +01001437 !hwif->mate->present) {
1438 if (ide_register_port(hwif)) {
1439 ide_disable_port(hwif);
1440 continue;
1441 }
1442 }
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +01001443
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001444 if (hwif->present)
1445 ide_port_tune_devices(hwif);
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001446 }
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001447
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001448 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001449 if (hwif == NULL)
1450 continue;
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001451
1452 if (hwif_init(hwif) == 0) {
1453 printk(KERN_INFO "%s: failed to initialize IDE "
1454 "interface\n", hwif->name);
Ian Campbell51d6ac72009-02-02 20:12:22 +01001455 device_unregister(&hwif->gendev);
1456 ide_disable_port(hwif);
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001457 continue;
1458 }
Bartlomiej Zolnierkiewiczdecdc3f2008-02-02 19:56:41 +01001459
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001460 if (hwif->present)
Elias Oltmannse415e492008-10-13 21:39:45 +02001461 if (ide_port_setup_devices(hwif) == 0) {
1462 hwif->present = 0;
1463 continue;
1464 }
1465
1466 j++;
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +02001467
Bartlomiej Zolnierkiewicz8b803bd2009-03-24 23:22:41 +01001468 ide_acpi_init_port(hwif);
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001469
1470 if (hwif->present)
1471 ide_acpi_port_init_devices(hwif);
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001472 }
1473
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001474 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001475 if (hwif == NULL)
1476 continue;
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001477
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001478 if (hwif->present)
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001479 hwif_register_devices(hwif);
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001480 }
1481
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001482 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001483 if (hwif == NULL)
1484 continue;
Bartlomiej Zolnierkiewicz327617e2008-02-02 19:56:43 +01001485
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001486 ide_sysfs_register_port(hwif);
1487 ide_proc_register_port(hwif);
1488
1489 if (hwif->present)
Bartlomiej Zolnierkiewiczd9270a32008-02-02 19:56:43 +01001490 ide_proc_port_register_devices(hwif);
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001491 }
1492
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001493 return j ? 0 : -1;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001494}
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001495EXPORT_SYMBOL_GPL(ide_host_register);
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001496
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001497int ide_host_add(const struct ide_port_info *d, hw_regs_t **hws,
1498 struct ide_host **hostp)
1499{
1500 struct ide_host *host;
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001501 int rc;
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001502
1503 host = ide_host_alloc(d, hws);
1504 if (host == NULL)
1505 return -ENOMEM;
1506
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001507 rc = ide_host_register(host, d, hws);
1508 if (rc) {
1509 ide_host_free(host);
1510 return rc;
1511 }
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001512
1513 if (hostp)
1514 *hostp = host;
1515
1516 return 0;
1517}
1518EXPORT_SYMBOL_GPL(ide_host_add);
1519
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001520static void __ide_port_unregister_devices(ide_hwif_t *hwif)
1521{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001522 ide_drive_t *drive;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001523 int i;
1524
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +01001525 ide_port_for_each_present_dev(i, drive, hwif) {
1526 device_unregister(&drive->gendev);
1527 wait_for_completion(&drive->gendev_rel_comp);
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001528 }
1529}
1530
1531void ide_port_unregister_devices(ide_hwif_t *hwif)
1532{
1533 mutex_lock(&ide_cfg_mtx);
1534 __ide_port_unregister_devices(hwif);
1535 hwif->present = 0;
1536 ide_port_init_devices_data(hwif);
1537 mutex_unlock(&ide_cfg_mtx);
1538}
1539EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
1540
1541/**
1542 * ide_unregister - free an IDE interface
1543 * @hwif: IDE interface
1544 *
1545 * Perform the final unregister of an IDE interface.
1546 *
1547 * Locking:
1548 * The caller must not hold the IDE locks.
1549 *
1550 * It is up to the caller to be sure there is no pending I/O here,
1551 * and that the interface will not be reopened (present/vanishing
1552 * locking isn't yet done BTW).
1553 */
1554
1555static void ide_unregister(ide_hwif_t *hwif)
1556{
1557 BUG_ON(in_interrupt());
1558 BUG_ON(irqs_disabled());
1559
1560 mutex_lock(&ide_cfg_mtx);
1561
1562 if (hwif->present) {
1563 __ide_port_unregister_devices(hwif);
1564 hwif->present = 0;
1565 }
1566
1567 ide_proc_unregister_port(hwif);
1568
1569 free_irq(hwif->irq, hwif);
1570
1571 device_unregister(hwif->portdev);
1572 device_unregister(&hwif->gendev);
1573 wait_for_completion(&hwif->gendev_rel_comp);
1574
1575 /*
1576 * Remove us from the kernel's knowledge
1577 */
1578 blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
1579 kfree(hwif->sg_table);
1580 unregister_blkdev(hwif->major, hwif->name);
1581
1582 ide_release_dma_engine(hwif);
1583
1584 mutex_unlock(&ide_cfg_mtx);
1585}
1586
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001587void ide_host_free(struct ide_host *host)
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001588{
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001589 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001590 int i;
1591
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001592 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicz9a100f42009-02-02 20:12:21 +01001593 if (hwif)
1594 ide_port_free(hwif);
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +02001595 }
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001596
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001597 kfree(host);
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001598}
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001599EXPORT_SYMBOL_GPL(ide_host_free);
1600
1601void ide_host_remove(struct ide_host *host)
1602{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001603 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001604 int i;
1605
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001606 ide_host_for_each_port(i, hwif, host) {
1607 if (hwif)
1608 ide_unregister(hwif);
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001609 }
1610
1611 ide_host_free(host);
1612}
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001613EXPORT_SYMBOL_GPL(ide_host_remove);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001614
1615void ide_port_scan(ide_hwif_t *hwif)
1616{
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +02001617 ide_port_apply_params(hwif);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001618 ide_port_cable_detect(hwif);
1619 ide_port_init_devices(hwif);
1620
1621 if (ide_probe_port(hwif) < 0)
1622 return;
1623
1624 hwif->present = 1;
1625
1626 ide_port_tune_devices(hwif);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001627 ide_port_setup_devices(hwif);
Bartlomiej Zolnierkiewicze630fcb2009-01-06 17:21:00 +01001628 ide_acpi_port_init_devices(hwif);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001629 hwif_register_devices(hwif);
1630 ide_proc_port_register_devices(hwif);
1631}
1632EXPORT_SYMBOL_GPL(ide_port_scan);