blob: 63627be0811a740142c769bcc66da80f64cc101e [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>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080039#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#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 Zolnierkiewiczca1b96e2009-05-17 19:12:21 +0200100 drive->special_flags |= IDE_SFLAG_SET_MULTMODE;
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;
Gustavo A. R. Silvaa2eed332018-07-03 14:23:05 -0500145 /* fall through */
Bartlomiej Zolnierkiewicz24630dc2009-01-02 16:12:47 +0100146 case ide_cdrom:
147 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
148#ifdef CONFIG_PPC
149 /* kludge for Apple PowerBook internal zip */
150 if (!strstr(m, "CD-ROM") && strstr(m, "ZIP")) {
151 printk(KERN_CONT "FLOPPY");
152 type = ide_floppy;
153 break;
154 }
155#endif
156 printk(KERN_CONT "CD/DVD-ROM");
157 break;
158 case ide_tape:
159 printk(KERN_CONT "TAPE");
160 break;
161 case ide_optical:
162 printk(KERN_CONT "OPTICAL");
163 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
164 break;
165 default:
166 printk(KERN_CONT "UNKNOWN (type %d)", type);
167 break;
168 }
169
170 printk(KERN_CONT " drive\n");
171 drive->media = type;
172 /* an ATAPI device ignores DRDY */
173 drive->ready_stat = 0;
174 if (ata_id_cdb_intr(id))
175 drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
176 drive->dev_flags |= IDE_DFLAG_DOORLOCKING;
177 /* we don't do head unloading on ATAPI devices */
178 drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * do_identify - identify a drive
183 * @drive: drive to identify
184 * @cmd: command used
Bartlomiej Zolnierkiewicz2ebe1d92009-03-24 23:22:59 +0100185 * @id: buffer for IDENTIFY data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 *
187 * Called when we have issued a drive identify command to
188 * read and parse the results. This function is run with
189 * interrupts disabled.
190 */
Bartlomiej Zolnierkiewicz047140a2008-12-29 20:27:36 +0100191
Bartlomiej Zolnierkiewicz2ebe1d92009-03-24 23:22:59 +0100192static void do_identify(ide_drive_t *drive, u8 cmd, u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100194 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200195 char *m = (char *)&id[ATA_ID_PROD];
Bartlomiej Zolnierkiewicz94b9efd2008-12-29 20:27:38 +0100196 unsigned long flags;
Bartlomiej Zolnierkiewicz24630dc2009-01-02 16:12:47 +0100197 int bswap = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Bartlomiej Zolnierkiewicz94b9efd2008-12-29 20:27:38 +0100199 /* local CPU only; some systems need this */
200 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* read 512 bytes of id info */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200202 hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
Bartlomiej Zolnierkiewicz94b9efd2008-12-29 20:27:38 +0100203 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200205 drive->dev_flags |= IDE_DFLAG_ID_READ;
Bartlomiej Zolnierkiewicz7b9f25b2008-02-01 23:09:28 +0100206#ifdef DEBUG
207 printk(KERN_INFO "%s: dumping identify data\n", drive->name);
208 ide_dump_identify((u8 *)id);
209#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 ide_fix_driveid(id);
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /*
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200213 * ATA_CMD_ID_ATA returns little-endian info,
214 * ATA_CMD_ID_ATAPI *usually* returns little-endian info.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200216 if (cmd == ATA_CMD_ID_ATAPI) {
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200217 if ((m[0] == 'N' && m[1] == 'E') || /* NEC */
218 (m[0] == 'F' && m[1] == 'X') || /* Mitsumi */
219 (m[0] == 'P' && m[1] == 'i')) /* Pioneer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /* Vertos drives may still be weird */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200221 bswap ^= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200223
224 ide_fixstring(m, ATA_ID_PROD_LEN, bswap);
225 ide_fixstring((char *)&id[ATA_ID_FW_REV], ATA_ID_FW_REV_LEN, bswap);
226 ide_fixstring((char *)&id[ATA_ID_SERNO], ATA_ID_SERNO_LEN, bswap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Tejun Heo699b0522007-11-05 21:42:25 +0100228 /* we depend on this a lot! */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200229 m[ATA_ID_PROD_LEN - 1] = '\0';
Tejun Heo699b0522007-11-05 21:42:25 +0100230
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200231 if (strstr(m, "E X A B Y T E N E S T"))
Bartlomiej Zolnierkiewicz69197ad2009-03-27 12:46:26 +0100232 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
233 else
234 drive->dev_flags |= IDE_DFLAG_PRESENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237/**
Bartlomiej Zolnierkiewicz2ebe1d92009-03-24 23:22:59 +0100238 * ide_dev_read_id - send ATA/ATAPI IDENTIFY command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * @drive: drive to identify
240 * @cmd: command to use
Bartlomiej Zolnierkiewicz2ebe1d92009-03-24 23:22:59 +0100241 * @id: buffer for IDENTIFY data
Bartlomiej Zolnierkiewiczfa56d4c2009-06-23 11:29:11 +0000242 * @irq_ctx: flag set when called from the IRQ context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 *
Bartlomiej Zolnierkiewicz2ebe1d92009-03-24 23:22:59 +0100244 * Sends an ATA(PI) IDENTIFY request to a drive and waits for a response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 *
246 * Returns: 0 device was identified
247 * 1 device timed-out (no response to identify request)
248 * 2 device aborted the command (refused to identify itself)
249 */
250
Bartlomiej Zolnierkiewiczfa56d4c2009-06-23 11:29:11 +0000251int ide_dev_read_id(ide_drive_t *drive, u8 cmd, u16 *id, int irq_ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100253 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200254 struct ide_io_ports *io_ports = &hwif->io_ports;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200255 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100256 int use_altstatus = 0, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 unsigned long timeout;
258 u8 s = 0, a = 0;
259
Bartlomiej Zolnierkiewicza1828072009-03-24 23:22:53 +0100260 /*
261 * Disable device IRQ. Otherwise we'll get spurious interrupts
262 * during the identify phase that the IRQ handler isn't expecting.
263 */
264 if (io_ports->ctl_addr)
Sergei Shtylyovecf3a312009-03-31 20:15:30 +0200265 tp_ops->write_devctl(hwif, ATA_NIEN | ATA_DEVCTL_OBS);
Bartlomiej Zolnierkiewicza1828072009-03-24 23:22:53 +0100266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 /* take a deep breath */
Bartlomiej Zolnierkiewiczfa56d4c2009-06-23 11:29:11 +0000268 if (irq_ctx)
269 mdelay(50);
270 else
271 msleep(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Bartlomiej Zolnierkiewicz66364872008-12-02 20:40:03 +0100273 if (io_ports->ctl_addr &&
274 (hwif->host_flags & IDE_HFLAG_BROKEN_ALTSTATUS) == 0) {
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200275 a = tp_ops->read_altstatus(hwif);
276 s = tp_ops->read_status(hwif);
Hannes Reinecke27f00e52015-03-27 16:46:33 +0100277 if ((a ^ s) & ~ATA_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 /* ancient Seagate drives, broken interfaces */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100279 printk(KERN_INFO "%s: probing with STATUS(0x%02x) "
280 "instead of ALTSTATUS(0x%02x)\n",
281 drive->name, s, a);
282 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /* use non-intrusive polling */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100284 use_altstatus = 1;
285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 /* set features register for atapi
288 * identify command to be sure of reply
289 */
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200290 if (cmd == ATA_CMD_ID_ATAPI) {
Sergei Shtylyovc9ff9e72009-04-08 14:13:03 +0200291 struct ide_taskfile tf;
Bartlomiej Zolnierkiewicz4e658372008-07-23 19:55:53 +0200292
Sergei Shtylyovc9ff9e72009-04-08 14:13:03 +0200293 memset(&tf, 0, sizeof(tf));
Bartlomiej Zolnierkiewicz4e658372008-07-23 19:55:53 +0200294 /* disable DMA & overlap */
Sergei Shtylyovc9ff9e72009-04-08 14:13:03 +0200295 tp_ops->tf_load(drive, &tf, IDE_VALID_FEATURE);
Bartlomiej Zolnierkiewicz4e658372008-07-23 19:55:53 +0200296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 /* ask drive for ID */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200299 tp_ops->exec_command(hwif, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200301 timeout = ((cmd == ATA_CMD_ID_ATA) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200302
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200303 /* wait for IRQ and ATA_DRQ */
Bartlomiej Zolnierkiewiczfa56d4c2009-06-23 11:29:11 +0000304 if (irq_ctx) {
305 rc = __ide_wait_stat(drive, ATA_DRQ, BAD_R_STAT, timeout, &s);
306 if (rc)
307 return 1;
308 } else {
309 rc = ide_busy_sleep(drive, timeout, use_altstatus);
310 if (rc)
311 return 1;
312
313 msleep(50);
314 s = tp_ops->read_status(hwif);
315 }
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100316
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200317 if (OK_STAT(s, ATA_DRQ, BAD_R_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /* drive returned ID */
Bartlomiej Zolnierkiewicz2ebe1d92009-03-24 23:22:59 +0100319 do_identify(drive, cmd, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 /* drive responded with ID */
321 rc = 0;
322 /* clear drive IRQ */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200323 (void)tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 } else {
325 /* drive refused ID */
326 rc = 2;
327 }
328 return rc;
329}
330
Bartlomiej Zolnierkiewicz28ee9bc2009-05-22 16:23:38 +0200331int ide_busy_sleep(ide_drive_t *drive, unsigned long timeout, int altstatus)
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100332{
Bartlomiej Zolnierkiewicz28ee9bc2009-05-22 16:23:38 +0200333 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100334 u8 stat;
335
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200336 timeout += jiffies;
337
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100338 do {
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200339 msleep(50); /* give drive a breather */
340 stat = altstatus ? hwif->tp_ops->read_altstatus(hwif)
341 : hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200342 if ((stat & ATA_BUSY) == 0)
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100343 return 0;
344 } while (time_before(jiffies, timeout));
345
Bartlomiej Zolnierkiewicz28ee9bc2009-05-22 16:23:38 +0200346 printk(KERN_ERR "%s: timeout in %s\n", drive->name, __func__);
347
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200348 return 1; /* drive timed-out */
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100349}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200351static u8 ide_read_device(ide_drive_t *drive)
352{
Sergei Shtylyov3153c262009-04-08 14:13:03 +0200353 struct ide_taskfile tf;
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200354
Sergei Shtylyov3153c262009-04-08 14:13:03 +0200355 drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_DEVICE);
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200356
Sergei Shtylyov3153c262009-04-08 14:13:03 +0200357 return tf.device;
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/**
361 * do_probe - probe an IDE device
362 * @drive: drive to probe
363 * @cmd: command to use
364 *
365 * do_probe() has the difficult job of finding a drive if it exists,
366 * without getting hung up if it doesn't exist, without trampling on
367 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
368 *
369 * If a drive is "known" to exist (from CMOS or kernel parameters),
370 * but does not respond right away, the probe will "hang in there"
371 * for the maximum wait time (about 30 seconds), otherwise it will
372 * exit much more quickly.
373 *
374 * Returns: 0 device was identified
375 * 1 device timed-out (no response to identify request)
376 * 2 device aborted the command (refused to identify itself)
377 * 3 bad status from device (possible for ATAPI drives)
378 * 4 probe was not attempted because failure was obvious
379 */
380
381static int do_probe (ide_drive_t *drive, u8 cmd)
382{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100383 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200384 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz2ebe1d92009-03-24 23:22:59 +0100385 u16 *id = drive->id;
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100386 int rc;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200387 u8 present = !!(drive->dev_flags & IDE_DFLAG_PRESENT), stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200389 /* avoid waiting for inappropriate probes */
390 if (present && drive->media != ide_disk && cmd == ATA_CMD_ID_ATA)
391 return 4;
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393#ifdef DEBUG
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200394 printk(KERN_INFO "probing for %s: present=%d, media=%d, probetype=%s\n",
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200395 drive->name, present, drive->media,
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200396 (cmd == ATA_CMD_ID_ATA) ? "ATA" : "ATAPI");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397#endif
398
399 /* needed for some systems
400 * (e.g. crw9624 as drive0 with disk as slave)
401 */
402 msleep(50);
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200403 tp_ops->dev_select(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 msleep(50);
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200405
Bartlomiej Zolnierkiewicz7f612f22008-10-13 21:39:40 +0200406 if (ide_read_device(drive) != drive->select && present == 0) {
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +0200407 if (drive->dn & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /* exit with drive0 selected */
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200409 tp_ops->dev_select(hwif->devices[0]);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200410 /* allow ATA_BUSY to assert & clear */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 msleep(50);
412 }
413 /* no i/f present: mmm.. this should be a 4 -ml */
414 return 3;
415 }
416
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200417 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100418
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200419 if (OK_STAT(stat, ATA_DRDY, ATA_BUSY) ||
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200420 present || cmd == ATA_CMD_ID_ATAPI) {
Bartlomiej Zolnierkiewiczfa56d4c2009-06-23 11:29:11 +0000421 rc = ide_dev_read_id(drive, cmd, id, 0);
Bartlomiej Zolnierkiewicz2ebe1d92009-03-24 23:22:59 +0100422 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 /* failed: try again */
Bartlomiej Zolnierkiewiczfa56d4c2009-06-23 11:29:11 +0000424 rc = ide_dev_read_id(drive, cmd, id, 0);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100425
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200426 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100427
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200428 if (stat == (ATA_BUSY | ATA_DRDY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return 4;
430
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200431 if (rc == 1 && cmd == ATA_CMD_ID_ATAPI) {
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100432 printk(KERN_ERR "%s: no response (status = 0x%02x), "
433 "resetting drive\n", drive->name, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 msleep(50);
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200435 tp_ops->dev_select(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 msleep(50);
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200437 tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
Bartlomiej Zolnierkiewicz28ee9bc2009-05-22 16:23:38 +0200438 (void)ide_busy_sleep(drive, WAIT_WORSTCASE, 0);
Bartlomiej Zolnierkiewiczfa56d4c2009-06-23 11:29:11 +0000439 rc = ide_dev_read_id(drive, cmd, id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100441
442 /* ensure drive IRQ is clear */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200443 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (rc == 1)
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100446 printk(KERN_ERR "%s: no response (status = 0x%02x)\n",
447 drive->name, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 } else {
449 /* not present or maybe ATAPI */
450 rc = 3;
451 }
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +0200452 if (drive->dn & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /* exit with drive0 selected */
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200454 tp_ops->dev_select(hwif->devices[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 msleep(50);
456 /* ensure drive irq is clear */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200457 (void)tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459 return rc;
460}
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462/**
463 * probe_for_drives - upper level drive probe
464 * @drive: drive to probe for
465 *
466 * probe_for_drive() tests for existence of a given drive using do_probe()
467 * and presents things to the user as needed.
468 *
469 * Returns: 0 no device was found
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200470 * 1 device was found
471 * (note: IDE_DFLAG_PRESENT might still be not set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 */
Bartlomiej Zolnierkiewicz047140a2008-12-29 20:27:36 +0100473
474static u8 probe_for_drive(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200476 char *m;
Bartlomiej Zolnierkiewicz1bd4c1f2009-03-24 23:22:58 +0100477 int rc;
478 u8 cmd;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200479
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200480 drive->dev_flags &= ~IDE_DFLAG_ID_READ;
481
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200482 m = (char *)&drive->id[ATA_ID_PROD];
483 strcpy(m, "UNKNOWN");
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 /* skip probing? */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200486 if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /* if !(success||timed-out) */
Bartlomiej Zolnierkiewicz1bd4c1f2009-03-24 23:22:58 +0100488 cmd = ATA_CMD_ID_ATA;
489 rc = do_probe(drive, cmd);
490 if (rc >= 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /* look for ATAPI device */
Bartlomiej Zolnierkiewicz1bd4c1f2009-03-24 23:22:58 +0100492 cmd = ATA_CMD_ID_ATAPI;
493 rc = do_probe(drive, cmd);
494 }
Bartlomiej Zolnierkiewiczc36a7e92008-10-10 22:39:23 +0200495
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200496 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Hugh Dickins5f4417a2009-06-10 14:37:20 +0200497 return 0;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* identification failed? */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200500 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (drive->media == ide_disk) {
502 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
503 drive->name, drive->cyl,
504 drive->head, drive->sect);
505 } else if (drive->media == ide_cdrom) {
506 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
507 } else {
508 /* nuke it */
509 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200510 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
Bartlomiej Zolnierkiewicz1bd4c1f2009-03-24 23:22:58 +0100512 } else {
513 if (cmd == ATA_CMD_ID_ATAPI)
514 ide_classify_atapi_dev(drive);
515 else
516 ide_classify_ata_dev(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200519
520 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Hugh Dickins5f4417a2009-06-10 14:37:20 +0200521 return 0;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /* The drive wasn't being helpful. Add generic info only */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200524 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 generic_id(drive);
526 return 1;
527 }
528
529 if (drive->media == ide_disk) {
530 ide_disk_init_chs(drive);
531 ide_disk_init_mult_count(drive);
532 }
533
Bartlomiej Zolnierkiewicz69197ad2009-03-27 12:46:26 +0100534 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Pavel Machekfc410692008-07-23 19:56:02 +0200537static void hwif_release_dev(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
540
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800541 complete(&hwif->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200544static int ide_register_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Randy Dunlap349ae232006-10-03 01:14:23 -0700546 int ret;
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* register with global device tree */
Kees Cook02aa2a32013-07-03 15:04:56 -0700549 dev_set_name(&hwif->gendev, "%s", hwif->name);
Greg Kroah-Hartmanfcb52072009-04-30 14:43:31 -0700550 dev_set_drvdata(&hwif->gendev, hwif);
Andreas Schwabbb54aff2009-01-19 13:46:56 +0100551 if (hwif->gendev.parent == NULL)
552 hwif->gendev.parent = hwif->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 hwif->gendev.release = hwif_release_dev;
Bartlomiej Zolnierkiewicz96d409412009-01-02 16:12:47 +0100554
Randy Dunlap349ae232006-10-03 01:14:23 -0700555 ret = device_register(&hwif->gendev);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200556 if (ret < 0) {
Randy Dunlap349ae232006-10-03 01:14:23 -0700557 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200558 __func__, ret);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200559 goto out;
560 }
561
Greg Kroah-Hartman3ee074b2008-07-21 20:03:34 -0700562 hwif->portdev = device_create(ide_port_class, &hwif->gendev,
Kees Cook02aa2a32013-07-03 15:04:56 -0700563 MKDEV(0, 0), hwif, "%s", hwif->name);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200564 if (IS_ERR(hwif->portdev)) {
565 ret = PTR_ERR(hwif->portdev);
566 device_unregister(&hwif->gendev);
567 }
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200568out:
569 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
Bartlomiej Zolnierkiewiczc860a8f2008-02-01 23:09:34 +0100572/**
573 * ide_port_wait_ready - wait for port to become ready
574 * @hwif: IDE port
575 *
576 * This is needed on some PPCs and a bunch of BIOS-less embedded
577 * platforms. Typical cases are:
578 *
579 * - The firmware hard reset the disk before booting the kernel,
580 * the drive is still doing it's poweron-reset sequence, that
581 * can take up to 30 seconds.
582 *
583 * - The firmware does nothing (or no firmware), the device is
584 * still in POST state (same as above actually).
585 *
586 * - Some CD/DVD/Writer combo drives tend to drive the bus during
587 * their reset sequence even when they are non-selected slave
588 * devices, thus preventing discovery of the main HD.
589 *
590 * Doing this wait-for-non-busy should not harm any existing
591 * configuration and fix some issues like the above.
592 *
593 * BenH.
594 *
595 * Returns 0 on success, error code (< 0) otherwise.
596 */
597
598static int ide_port_wait_ready(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200600 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100601 ide_drive_t *drive;
602 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
605
606 /* Let HW settle down a bit from whatever init state we
607 * come from */
608 mdelay(2);
609
610 /* Wait for BSY bit to go away, spec timeout is 30 seconds,
611 * I know of at least one disk who takes 31 seconds, I use 35
612 * here to be safe
613 */
614 rc = ide_wait_not_busy(hwif, 35000);
615 if (rc)
616 return rc;
617
618 /* Now make sure both master & slave are ready */
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100619 ide_port_for_each_dev(i, drive, hwif) {
Jonas Stare82661052007-11-27 21:35:53 +0100620 /* Ignore disks that we will not probe for later. */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200621 if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0 ||
622 (drive->dev_flags & IDE_DFLAG_PRESENT)) {
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200623 tp_ops->dev_select(drive);
624 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
Jonas Stare82661052007-11-27 21:35:53 +0100625 mdelay(2);
626 rc = ide_wait_not_busy(hwif, 35000);
627 if (rc)
628 goto out;
629 } else
630 printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
631 drive->name);
632 }
633out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /* Exit function with master reselected (let's be sane) */
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100635 if (i)
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200636 tp_ops->dev_select(hwif->devices[0]);
Jonas Stare82661052007-11-27 21:35:53 +0100637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return rc;
639}
640
641/**
642 * ide_undecoded_slave - look for bad CF adapters
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200643 * @dev1: slave device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 *
645 * Analyse the drives on the interface and attempt to decide if we
646 * have the same drive viewed twice. This occurs with crap CF adapters
647 * and PCMCIA sometimes.
648 */
649
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200650void ide_undecoded_slave(ide_drive_t *dev1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100652 ide_drive_t *dev0 = dev1->hwif->devices[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200654 if ((dev1->dn & 1) == 0 || (dev0->dev_flags & IDE_DFLAG_PRESENT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return;
656
657 /* If the models don't match they are not the same product */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200658 if (strcmp((char *)&dev0->id[ATA_ID_PROD],
659 (char *)&dev1->id[ATA_ID_PROD]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return;
661
662 /* Serial numbers do not match */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200663 if (strncmp((char *)&dev0->id[ATA_ID_SERNO],
664 (char *)&dev1->id[ATA_ID_SERNO], ATA_ID_SERNO_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return;
666
667 /* No serial number, thankfully very rare for CF */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200668 if (*(char *)&dev0->id[ATA_ID_SERNO] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return;
670
671 /* Appears to be an IDE flash adapter with decode bugs */
672 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
673
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200674 dev1->dev_flags &= ~IDE_DFLAG_PRESENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677EXPORT_SYMBOL_GPL(ide_undecoded_slave);
678
Bartlomiej Zolnierkiewicz9d501522008-02-01 23:09:36 +0100679static int ide_probe_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100681 ide_drive_t *drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 unsigned int irqd;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100683 int i, rc = -ENODEV;
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100684
685 BUG_ON(hwif->present);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100687 if ((hwif->devices[0]->dev_flags & IDE_DFLAG_NOPROBE) &&
688 (hwif->devices[1]->dev_flags & IDE_DFLAG_NOPROBE))
Bartlomiej Zolnierkiewicz9d501522008-02-01 23:09:36 +0100689 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /*
692 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
693 * we'll install our IRQ driver much later...
694 */
695 irqd = hwif->irq;
696 if (irqd)
697 disable_irq(hwif->irq);
698
David S. Miller9ce41ae2010-03-28 18:58:28 -0700699 if (ide_port_wait_ready(hwif) == -EBUSY)
700 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 /*
Bartlomiej Zolnierkiewiczf367bed2008-03-29 19:48:21 +0100703 * Second drive should only exist if first drive was found,
704 * but a lot of cdrom drives are configured as single slaves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 */
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100706 ide_port_for_each_dev(i, drive, hwif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 (void) probe_for_drive(drive);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200708 if (drive->dev_flags & IDE_DFLAG_PRESENT)
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100709 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
David S. Miller9ce41ae2010-03-28 18:58:28 -0700711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /*
713 * Use cached IRQ number. It might be (and is...) changed by probe
714 * code above
715 */
716 if (irqd)
717 enable_irq(irqd);
718
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100719 return rc;
Bartlomiej Zolnierkiewicze84e7ea2008-02-01 23:09:36 +0100720}
721
722static void ide_port_tune_devices(ide_hwif_t *hwif)
723{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200724 const struct ide_port_ops *port_ops = hwif->port_ops;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100725 ide_drive_t *drive;
726 int i;
Bartlomiej Zolnierkiewicze84e7ea2008-02-01 23:09:36 +0100727
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100728 ide_port_for_each_present_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewicz8bc1e5a2009-06-07 15:37:09 +0200729 ide_check_nien_quirk_list(drive);
730
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100731 if (port_ops && port_ops->quirkproc)
732 port_ops->quirkproc(drive);
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100733 }
Bartlomiej Zolnierkiewicz0380dad2007-06-08 15:14:29 +0200734
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100735 ide_port_for_each_present_dev(i, drive, hwif) {
736 ide_set_max_pio(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100738 drive->dev_flags |= IDE_DFLAG_NICE1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100740 if (hwif->dma_ops)
741 ide_set_dma(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743}
744
Bart Van Asscheca18d6f2017-06-20 11:15:41 -0700745static void ide_initialize_rq(struct request *rq)
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100746{
747 struct ide_request *req = blk_mq_rq_to_pdu(rq);
748
Jens Axboed16a6762018-11-12 17:19:32 -0700749 req->special = NULL;
Bart Van Asschec8d9cf22017-06-20 11:15:42 -0700750 scsi_req_init(&req->sreq);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100751 req->sreq.sense = req->sense;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100752}
753
Jens Axboe60033522018-10-26 09:53:52 -0600754static const struct blk_mq_ops ide_mq_ops = {
755 .queue_rq = ide_queue_rq,
756 .initialize_rq_fn = ide_initialize_rq,
757};
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 * init request queue
761 */
762static int ide_init_queue(ide_drive_t *drive)
763{
Jens Axboe165125e2007-07-24 09:28:11 +0200764 struct request_queue *q;
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100765 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 int max_sectors = 256;
767 int max_sg_entries = PRD_ENTRIES;
Jens Axboe60033522018-10-26 09:53:52 -0600768 struct blk_mq_tag_set *set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 /*
771 * Our default set up assumes the normal IDE case,
772 * that is 64K segmenting, standard PRD setup
773 * and LBA28. Some drivers then impose their own
774 * limits and LBA48 we could raise it but as yet
775 * do not.
776 */
Jens Axboe60033522018-10-26 09:53:52 -0600777
778 set = &drive->tag_set;
779 set->ops = &ide_mq_ops;
780 set->nr_hw_queues = 1;
781 set->queue_depth = 32;
782 set->reserved_tags = 1;
783 set->cmd_size = sizeof(struct ide_request);
784 set->numa_node = hwif_to_node(hwif);
785 set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING;
786 if (blk_mq_alloc_tag_set(set))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return 1;
788
Jens Axboe60033522018-10-26 09:53:52 -0600789 q = blk_mq_init_queue(set);
790 if (IS_ERR(q)) {
791 blk_mq_free_tag_set(set);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100792 return 1;
793 }
794
Jens Axboe60033522018-10-26 09:53:52 -0600795 blk_queue_flag_set(QUEUE_FLAG_SCSI_PASSTHROUGH, q);
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 q->queuedata = drive;
798 blk_queue_segment_boundary(q, 0xffff);
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (hwif->rqsize < max_sectors)
801 max_sectors = hwif->rqsize;
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500802 blk_queue_max_hw_sectors(q, max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804#ifdef CONFIG_PCI
805 /* When we have an IOMMU, we may have a problem where pci_map_sg()
806 * creates segments that don't completely match our boundary
807 * requirements and thus need to be broken up again. Because it
808 * doesn't align properly either, we may actually have to break up
809 * to more segments than what was we got in the first place, a max
810 * worst case is twice as many.
811 * This will be fixed once we teach pci_map_sg() about our boundary
812 * requirements, hopefully soon. *FIXME*
813 */
Christoph Hellwig164c3d02018-04-03 20:29:44 +0200814 max_sg_entries >>= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815#endif /* CONFIG_PCI */
816
Martin K. Petersen8a783622010-02-26 00:20:39 -0500817 blk_queue_max_segments(q, max_sg_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 /* assign drive queue */
820 drive->queue = q;
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return 0;
823}
824
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +0100825static DEFINE_MUTEX(ide_cfg_mtx);
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827/*
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100828 * For any present drive:
829 * - allocate the block device queue
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100830 */
Elias Oltmannse415e492008-10-13 21:39:45 +0200831static int ide_port_setup_devices(ide_hwif_t *hwif)
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100832{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100833 ide_drive_t *drive;
Elias Oltmannse415e492008-10-13 21:39:45 +0200834 int i, j = 0;
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100835
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +0200836 mutex_lock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100837 ide_port_for_each_present_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100838 if (ide_init_queue(drive)) {
839 printk(KERN_ERR "ide: failed to init %s\n",
840 drive->name);
Elias Oltmannse415e492008-10-13 21:39:45 +0200841 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100842 continue;
843 }
844
Elias Oltmannse415e492008-10-13 21:39:45 +0200845 j++;
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100846 }
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +0200847 mutex_unlock(&ide_cfg_mtx);
Elias Oltmannse415e492008-10-13 21:39:45 +0200848
849 return j;
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100850}
851
Bartlomiej Zolnierkiewiczffc36c72009-06-23 03:43:00 -0700852static void ide_host_enable_irqs(struct ide_host *host)
853{
854 ide_hwif_t *hwif;
855 int i;
856
857 ide_host_for_each_port(i, hwif, host) {
858 if (hwif == NULL)
859 continue;
860
861 /* clear any pending IRQs */
862 hwif->tp_ops->read_status(hwif);
863
864 /* unmask IRQs */
865 if (hwif->io_ports.ctl_addr)
866 hwif->tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
867 }
868}
869
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100870/*
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100871 * This routine sets up the IRQ for an IDE interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 */
873static int init_irq (ide_hwif_t *hwif)
874{
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200875 struct ide_io_ports *io_ports = &hwif->io_ports;
Bartlomiej Zolnierkiewicz255115fb2009-03-27 12:46:27 +0100876 struct ide_host *host = hwif->host;
877 irq_handler_t irq_handler = host->irq_handler;
878 int sa = host->irq_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Stanislaw Gruszka849d7132009-03-05 16:10:57 +0100880 if (irq_handler == NULL)
881 irq_handler = ide_intr;
882
Michael Schmitza259d532014-02-01 13:48:13 +1300883 if (!host->get_lock)
884 if (request_irq(hwif->irq, irq_handler, sa, hwif->name, hwif))
885 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Adrian Bunk7b892802008-02-06 01:36:29 -0800887#if !defined(__mc68000__)
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200888 printk(KERN_INFO "%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200889 io_ports->data_addr, io_ports->status_addr,
890 io_ports->ctl_addr, hwif->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891#else
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200892 printk(KERN_INFO "%s at 0x%08lx on irq %d", hwif->name,
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200893 io_ports->data_addr, hwif->irq);
Adrian Bunk7b892802008-02-06 01:36:29 -0800894#endif /* __mc68000__ */
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100895 if (hwif->host->host_flags & IDE_HFLAG_SERIALIZE)
896 printk(KERN_CONT " (serialized)");
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200897 printk(KERN_CONT "\n");
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900out_up:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return 1;
902}
903
904static int ata_lock(dev_t dev, void *data)
905{
906 /* FIXME: we want to pin hwif down */
907 return 0;
908}
909
910static struct kobject *ata_probe(dev_t dev, int *part, void *data)
911{
912 ide_hwif_t *hwif = data;
913 int unit = *part >> PARTN_BITS;
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100914 ide_drive_t *drive = hwif->devices[unit];
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200915
916 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return NULL;
918
919 if (drive->media == ide_disk)
920 request_module("ide-disk");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (drive->media == ide_cdrom || drive->media == ide_optical)
922 request_module("ide-cd");
923 if (drive->media == ide_tape)
924 request_module("ide-tape");
925 if (drive->media == ide_floppy)
926 request_module("ide-floppy");
927
928 return NULL;
929}
930
931static struct kobject *exact_match(dev_t dev, int *part, void *data)
932{
933 struct gendisk *p = data;
934 *part &= (1 << PARTN_BITS) - 1;
Tejun Heoed9e1982008-08-25 19:56:05 +0900935 return &disk_to_dev(p)->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
938static int exact_lock(dev_t dev, void *data)
939{
940 struct gendisk *p = data;
941
Jan Kara3079c222018-02-26 13:01:38 +0100942 if (!get_disk_and_module(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return -1;
944 return 0;
945}
946
947void ide_register_region(struct gendisk *disk)
948{
949 blk_register_region(MKDEV(disk->major, disk->first_minor),
950 disk->minors, NULL, exact_match, exact_lock, disk);
951}
952
953EXPORT_SYMBOL_GPL(ide_register_region);
954
955void ide_unregister_region(struct gendisk *disk)
956{
957 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
958 disk->minors);
959}
960
961EXPORT_SYMBOL_GPL(ide_unregister_region);
962
963void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
964{
965 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz7f612f22008-10-13 21:39:40 +0200966 unsigned int unit = drive->dn & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 disk->major = hwif->major;
969 disk->first_minor = unit << PARTN_BITS;
970 sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
971 disk->queue = drive->queue;
972}
973
974EXPORT_SYMBOL_GPL(ide_init_disk);
975
976static void drive_release_dev (struct device *dev)
977{
978 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
979
Bartlomiej Zolnierkiewicz5b0c4b32008-04-18 00:46:22 +0200980 ide_proc_unregister_device(drive);
981
Jens Axboe60033522018-10-26 09:53:52 -0600982 if (drive->sense_rq)
983 blk_mq_free_request(drive->sense_rq);
984
Bartlomiej Zolnierkiewiczb5479162009-03-31 20:15:25 +0200985 blk_cleanup_queue(drive->queue);
986 drive->queue = NULL;
Jens Axboe60033522018-10-26 09:53:52 -0600987 blk_mq_free_tag_set(&drive->tag_set);
Bartlomiej Zolnierkiewiczb5479162009-03-31 20:15:25 +0200988
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200989 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200990
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800991 complete(&drive->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994static int hwif_init(ide_hwif_t *hwif)
995{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (!hwif->irq) {
Bartlomiej Zolnierkiewicz8b07ed22009-03-24 23:22:52 +0100997 printk(KERN_ERR "%s: disabled, no IRQ\n", hwif->name);
998 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (register_blkdev(hwif->major, hwif->name))
1002 return 0;
1003
1004 if (!hwif->sg_max_nents)
1005 hwif->sg_max_nents = PRD_ENTRIES;
1006
Kees Cook6da2ec52018-06-12 13:55:00 -07001007 hwif->sg_table = kmalloc_array(hwif->sg_max_nents,
1008 sizeof(struct scatterlist),
1009 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (!hwif->sg_table) {
1011 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1012 goto out;
1013 }
Jens Axboe45711f12007-10-22 21:19:53 +02001014
1015 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (init_irq(hwif)) {
Bartlomiej Zolnierkiewicz8b07ed22009-03-24 23:22:52 +01001018 printk(KERN_ERR "%s: disabled, unable to get IRQ %d\n",
1019 hwif->name, hwif->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 goto out;
1021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Bartlomiej Zolnierkiewicz3a4e7c92008-02-02 19:56:40 +01001023 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1024 THIS_MODULE, ata_probe, ata_lock, hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return 1;
1026
1027out:
1028 unregister_blkdev(hwif->major, hwif->name);
1029 return 0;
1030}
1031
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001032static void hwif_register_devices(ide_hwif_t *hwif)
1033{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001034 ide_drive_t *drive;
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001035 unsigned int i;
1036
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +01001037 ide_port_for_each_present_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewiczc5d70cc2008-02-02 19:56:41 +01001038 struct device *dev = &drive->gendev;
1039 int ret;
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001040
Kay Sieversdc09c782008-12-29 20:27:36 +01001041 dev_set_name(dev, "%u.%u", hwif->index, i);
Greg Kroah-Hartmanfcb52072009-04-30 14:43:31 -07001042 dev_set_drvdata(dev, drive);
Bartlomiej Zolnierkiewiczc5d70cc2008-02-02 19:56:41 +01001043 dev->parent = &hwif->gendev;
1044 dev->bus = &ide_bus_type;
Bartlomiej Zolnierkiewiczc5d70cc2008-02-02 19:56:41 +01001045 dev->release = drive_release_dev;
1046
1047 ret = device_register(dev);
1048 if (ret < 0)
1049 printk(KERN_WARNING "IDE: %s: device_register error: "
1050 "%d\n", __func__, ret);
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001051 }
1052}
1053
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001054static void ide_port_init_devices(ide_hwif_t *hwif)
1055{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001056 const struct ide_port_ops *port_ops = hwif->port_ops;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001057 ide_drive_t *drive;
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001058 int i;
1059
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001060 ide_port_for_each_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +02001061 drive->dn = i + hwif->channel * 2;
1062
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001063 if (hwif->host_flags & IDE_HFLAG_IO_32BIT)
1064 drive->io_32bit = 1;
Bartlomiej Zolnierkiewicz7610c4f2009-03-24 23:22:38 +01001065 if (hwif->host_flags & IDE_HFLAG_NO_IO_32BIT)
1066 drive->dev_flags |= IDE_DFLAG_NO_IO_32BIT;
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001067 if (hwif->host_flags & IDE_HFLAG_UNMASK_IRQS)
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001068 drive->dev_flags |= IDE_DFLAG_UNMASK;
Bartlomiej Zolnierkiewicz807b90d2008-02-02 19:56:40 +01001069 if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS)
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001070 drive->dev_flags |= IDE_DFLAG_NO_UNMASK;
Bartlomiej Zolnierkiewicz1f2cf8b2008-02-02 19:56:40 +01001071
Bartlomiej Zolnierkiewiczd2d4e782010-01-18 07:20:28 +00001072 drive->pio_mode = XFER_PIO_0;
1073
Bartlomiej Zolnierkiewicze6d95bd2008-07-16 20:33:42 +02001074 if (port_ops && port_ops->init_dev)
1075 port_ops->init_dev(drive);
1076 }
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001077}
1078
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001079static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
1080 const struct ide_port_info *d)
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001081{
Adrian Bunkb7691642008-06-10 20:56:36 +02001082 hwif->channel = port;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001083
Bartlomiej Zolnierkiewicz29e52cf2009-05-17 19:12:22 +02001084 hwif->chipset = d->chipset ? d->chipset : ide_pci;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001085
1086 if (d->init_iops)
1087 d->init_iops(hwif);
1088
Bartlomiej Zolnierkiewicz23f8e4b2008-05-01 14:08:51 +02001089 /* ->host_flags may be set by ->init_iops (or even earlier...) */
1090 hwif->host_flags |= d->host_flags;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001091 hwif->pio_mask = d->pio_mask;
1092
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001093 if (d->tp_ops)
1094 hwif->tp_ops = d->tp_ops;
1095
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001096 /* ->set_pio_mode for DTC2278 is currently limited to port 0 */
Bartlomiej Zolnierkiewicz2787cb82009-03-27 12:46:28 +01001097 if ((hwif->host_flags & IDE_HFLAG_DTC2278) == 0 || hwif->channel == 0)
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001098 hwif->port_ops = d->port_ops;
1099
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001100 hwif->swdma_mask = d->swdma_mask;
1101 hwif->mwdma_mask = d->mwdma_mask;
1102 hwif->ultra_mask = d->udma_mask;
1103
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001104 if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
1105 int rc;
1106
Sergei Shtylyov592b5312009-01-06 17:21:02 +01001107 hwif->dma_ops = d->dma_ops;
1108
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001109 if (d->init_dma)
1110 rc = d->init_dma(hwif, d);
1111 else
1112 rc = ide_hwif_setup_dma(hwif, d);
1113
1114 if (rc < 0) {
1115 printk(KERN_INFO "%s: DMA disabled\n", hwif->name);
Sergei Shtylyov592b5312009-01-06 17:21:02 +01001116
1117 hwif->dma_ops = NULL;
Bartlomiej Zolnierkiewiczebb00fb2008-07-23 19:55:51 +02001118 hwif->dma_base = 0;
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001119 hwif->swdma_mask = 0;
1120 hwif->mwdma_mask = 0;
1121 hwif->ultra_mask = 0;
Sergei Shtylyov592b5312009-01-06 17:21:02 +01001122 }
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001123 }
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001124
Bartlomiej Zolnierkiewicz1024c5f2008-05-04 17:03:41 +02001125 if ((d->host_flags & IDE_HFLAG_SERIALIZE) ||
Bartlomiej Zolnierkiewicz702c0262008-12-29 20:27:36 +01001126 ((d->host_flags & IDE_HFLAG_SERIALIZE_DMA) && hwif->dma_base))
1127 hwif->host->host_flags |= IDE_HFLAG_SERIALIZE;
Bartlomiej Zolnierkiewicz1024c5f2008-05-04 17:03:41 +02001128
Bartlomiej Zolnierkiewicz6b492492008-12-29 20:27:34 +01001129 if (d->max_sectors)
1130 hwif->rqsize = d->max_sectors;
Bartlomiej Zolnierkiewicz70775e92009-03-27 12:46:18 +01001131 else {
1132 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
1133 (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
1134 hwif->rqsize = 256;
1135 else
1136 hwif->rqsize = 65536;
1137 }
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001138
1139 /* call chipset specific routine for each enabled port */
1140 if (d->init_hwif)
1141 d->init_hwif(hwif);
Bartlomiej Zolnierkiewiczc7f6f212008-04-18 00:46:22 +02001142}
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001143
Bartlomiej Zolnierkiewiczc7f6f212008-04-18 00:46:22 +02001144static void ide_port_cable_detect(ide_hwif_t *hwif)
1145{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001146 const struct ide_port_ops *port_ops = hwif->port_ops;
1147
1148 if (port_ops && port_ops->cable_detect && (hwif->ultra_mask & 0x78)) {
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001149 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001150 hwif->cbl = port_ops->cable_detect(hwif);
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001151 }
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001152}
1153
Jens Axboe60033522018-10-26 09:53:52 -06001154/*
1155 * Deferred request list insertion handler
1156 */
1157static void drive_rq_insert_work(struct work_struct *work)
1158{
1159 ide_drive_t *drive = container_of(work, ide_drive_t, rq_work);
1160 ide_hwif_t *hwif = drive->hwif;
1161 struct request *rq;
1162 LIST_HEAD(list);
1163
1164 spin_lock_irq(&hwif->lock);
1165 if (!list_empty(&drive->rq_list))
1166 list_splice_init(&drive->rq_list, &list);
1167 spin_unlock_irq(&hwif->lock);
1168
1169 while (!list_empty(&list)) {
1170 rq = list_first_entry(&list, struct request, queuelist);
1171 list_del_init(&rq->queuelist);
1172 blk_execute_rq_nowait(drive->queue, rq->rq_disk, rq, true, NULL);
1173 }
1174}
1175
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001176static const u8 ide_hwif_to_major[] =
1177 { IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR, IDE4_MAJOR,
1178 IDE5_MAJOR, IDE6_MAJOR, IDE7_MAJOR, IDE8_MAJOR, IDE9_MAJOR };
1179
1180static void ide_port_init_devices_data(ide_hwif_t *hwif)
1181{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001182 ide_drive_t *drive;
1183 int i;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001184
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001185 ide_port_for_each_dev(i, drive, hwif) {
1186 u8 j = (hwif->index * MAX_DRIVES) + i;
Hugh Dickins5f4417a2009-06-10 14:37:20 +02001187 u16 *saved_id = drive->id;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001188
1189 memset(drive, 0, sizeof(*drive));
Hugh Dickins5f4417a2009-06-10 14:37:20 +02001190 memset(saved_id, 0, SECTOR_SIZE);
1191 drive->id = saved_id;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001192
1193 drive->media = ide_disk;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001194 drive->select = (i << 4) | ATA_DEVICE_OBS;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001195 drive->hwif = hwif;
1196 drive->ready_stat = ATA_DRDY;
1197 drive->bad_wstat = BAD_W_STAT;
Bartlomiej Zolnierkiewiczca1b96e2009-05-17 19:12:21 +02001198 drive->special_flags = IDE_SFLAG_RECALIBRATE |
1199 IDE_SFLAG_SET_GEOMETRY;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001200 drive->name[0] = 'h';
1201 drive->name[1] = 'd';
1202 drive->name[2] = 'a' + j;
1203 drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
1204
1205 INIT_LIST_HEAD(&drive->list);
1206 init_completion(&drive->gendev_rel_comp);
Jens Axboe60033522018-10-26 09:53:52 -06001207
1208 INIT_WORK(&drive->rq_work, drive_rq_insert_work);
1209 INIT_LIST_HEAD(&drive->rq_list);
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001210 }
1211}
1212
1213static void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
1214{
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001215 /* fill in any non-zero initial values */
1216 hwif->index = index;
1217 hwif->major = ide_hwif_to_major[index];
1218
1219 hwif->name[0] = 'i';
1220 hwif->name[1] = 'd';
1221 hwif->name[2] = 'e';
1222 hwif->name[3] = '0' + index;
1223
Bartlomiej Zolnierkiewicz73629512009-03-24 23:22:39 +01001224 spin_lock_init(&hwif->lock);
1225
Kees Cook10738ba2017-10-17 21:06:32 -07001226 timer_setup(&hwif->timer, ide_timer_expiry, 0);
Bartlomiej Zolnierkiewicz73629512009-03-24 23:22:39 +01001227
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001228 init_completion(&hwif->gendev_rel_comp);
1229
1230 hwif->tp_ops = &default_tp_ops;
1231
1232 ide_port_init_devices_data(hwif);
1233}
1234
Bartlomiej Zolnierkiewicz9f36d312009-05-17 19:12:25 +02001235static void ide_init_port_hw(ide_hwif_t *hwif, struct ide_hw *hw)
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001236{
1237 memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports));
1238 hwif->irq = hw->irq;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001239 hwif->dev = hw->dev;
1240 hwif->gendev.parent = hw->parent ? hw->parent : hw->dev;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001241 hwif->config_data = hw->config;
1242}
1243
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001244static unsigned int ide_indexes;
1245
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001246/**
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001247 * ide_find_port_slot - find free port slot
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001248 * @d: IDE port info
1249 *
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001250 * Return the new port slot index or -ENOENT if we are out of free slots.
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001251 */
1252
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001253static int ide_find_port_slot(const struct ide_port_info *d)
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001254{
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001255 int idx = -ENOENT;
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001256 u8 bootable = (d && (d->host_flags & IDE_HFLAG_NON_BOOTABLE)) ? 0 : 1;
Joe Perchesa419aef2009-08-18 11:18:35 -07001257 u8 i = (d && (d->host_flags & IDE_HFLAG_QD_2ND_PORT)) ? 1 : 0;
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001258
1259 /*
1260 * Claim an unassigned slot.
1261 *
1262 * Give preference to claiming other slots before claiming ide0/ide1,
1263 * just in case there's another interface yet-to-be-scanned
1264 * which uses ports 0x1f0/0x170 (the ide0/ide1 defaults).
1265 *
1266 * Unless there is a bootable card that does not use the standard
1267 * ports 0x1f0/0x170 (the ide0/ide1 defaults).
1268 */
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001269 mutex_lock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewicz75d21ff2008-10-13 21:39:33 +02001270 if (bootable) {
1271 if ((ide_indexes | i) != (1 << MAX_HWIFS) - 1)
1272 idx = ffz(ide_indexes | i);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001273 } else {
Bartlomiej Zolnierkiewicz75d21ff2008-10-13 21:39:33 +02001274 if ((ide_indexes | 3) != (1 << MAX_HWIFS) - 1)
1275 idx = ffz(ide_indexes | 3);
1276 else if ((ide_indexes & 3) != 3)
1277 idx = ffz(ide_indexes);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001278 }
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001279 if (idx >= 0)
1280 ide_indexes |= (1 << idx);
1281 mutex_unlock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001282
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001283 return idx;
1284}
Bartlomiej Zolnierkiewiczeb3aff52008-07-16 20:33:42 +02001285
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001286static void ide_free_port_slot(int idx)
1287{
1288 mutex_lock(&ide_cfg_mtx);
1289 ide_indexes &= ~(1 << idx);
1290 mutex_unlock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001291}
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001292
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001293static void ide_port_free_devices(ide_hwif_t *hwif)
1294{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001295 ide_drive_t *drive;
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001296 int i;
1297
Hugh Dickins5f4417a2009-06-10 14:37:20 +02001298 ide_port_for_each_dev(i, drive, hwif) {
1299 kfree(drive->id);
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001300 kfree(drive);
Hugh Dickins5f4417a2009-06-10 14:37:20 +02001301 }
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001302}
1303
1304static int ide_port_alloc_devices(ide_hwif_t *hwif, int node)
1305{
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001306 ide_drive_t *drive;
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001307 int i;
1308
1309 for (i = 0; i < MAX_DRIVES; i++) {
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001310 drive = kzalloc_node(sizeof(*drive), GFP_KERNEL, node);
1311 if (drive == NULL)
1312 goto out_nomem;
1313
Hugh Dickins5f4417a2009-06-10 14:37:20 +02001314 /*
1315 * In order to keep things simple we have an id
1316 * block for all drives at all times. If the device
1317 * is pre ATA or refuses ATA/ATAPI identify we
1318 * will add faked data to this.
1319 *
1320 * Also note that 0 everywhere means "can't do X"
1321 */
1322 drive->id = kzalloc_node(SECTOR_SIZE, GFP_KERNEL, node);
1323 if (drive->id == NULL)
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001324 goto out_free_drive;
1325
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001326 hwif->devices[i] = drive;
1327 }
1328 return 0;
1329
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001330out_free_drive:
1331 kfree(drive);
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001332out_nomem:
1333 ide_port_free_devices(hwif);
1334 return -ENOMEM;
1335}
1336
Bartlomiej Zolnierkiewicz9f36d312009-05-17 19:12:25 +02001337struct ide_host *ide_host_alloc(const struct ide_port_info *d,
1338 struct ide_hw **hws, unsigned int n_ports)
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001339{
1340 struct ide_host *host;
Bartlomiej Zolnierkiewicza32296f2009-01-06 17:20:56 +01001341 struct device *dev = hws[0] ? hws[0]->dev : NULL;
1342 int node = dev ? dev_to_node(dev) : -1;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001343 int i;
1344
Bartlomiej Zolnierkiewicza32296f2009-01-06 17:20:56 +01001345 host = kzalloc_node(sizeof(*host), GFP_KERNEL, node);
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001346 if (host == NULL)
1347 return NULL;
1348
Bartlomiej Zolnierkiewiczdca39832009-05-17 19:12:24 +02001349 for (i = 0; i < n_ports; i++) {
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001350 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001351 int idx;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001352
1353 if (hws[i] == NULL)
1354 continue;
1355
Bartlomiej Zolnierkiewicza32296f2009-01-06 17:20:56 +01001356 hwif = kzalloc_node(sizeof(*hwif), GFP_KERNEL, node);
Bartlomiej Zolnierkiewicz18de10172008-07-23 19:55:58 +02001357 if (hwif == NULL)
1358 continue;
1359
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001360 if (ide_port_alloc_devices(hwif, node) < 0) {
1361 kfree(hwif);
1362 continue;
1363 }
1364
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001365 idx = ide_find_port_slot(d);
1366 if (idx < 0) {
1367 printk(KERN_ERR "%s: no free slot for interface\n",
1368 d ? d->name : "ide");
Hugh Dickins5f4417a2009-06-10 14:37:20 +02001369 ide_port_free_devices(hwif);
Bartlomiej Zolnierkiewicz18de10172008-07-23 19:55:58 +02001370 kfree(hwif);
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001371 continue;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001372 }
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001373
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001374 ide_init_port_data(hwif, idx);
1375
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +02001376 hwif->host = host;
1377
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001378 host->ports[i] = hwif;
1379 host->n_ports++;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001380 }
1381
1382 if (host->n_ports == 0) {
1383 kfree(host);
1384 return NULL;
1385 }
1386
Bartlomiej Zolnierkiewicza32296f2009-01-06 17:20:56 +01001387 host->dev[0] = dev;
Bartlomiej Zolnierkiewicz6cdf6eb2008-07-24 22:53:14 +02001388
Bartlomiej Zolnierkiewiczfeb22b72008-10-10 22:39:32 +02001389 if (d) {
1390 host->init_chipset = d->init_chipset;
Bartlomiej Zolnierkiewicze354c1d2009-03-27 12:46:22 +01001391 host->get_lock = d->get_lock;
1392 host->release_lock = d->release_lock;
Bartlomiej Zolnierkiewiczef0b0422008-07-24 22:53:19 +02001393 host->host_flags = d->host_flags;
Geert Uytterhoevenaa075732009-04-08 14:12:47 +02001394 host->irq_flags = d->irq_flags;
Bartlomiej Zolnierkiewiczfeb22b72008-10-10 22:39:32 +02001395 }
Bartlomiej Zolnierkiewiczef0b0422008-07-24 22:53:19 +02001396
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001397 return host;
1398}
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001399EXPORT_SYMBOL_GPL(ide_host_alloc);
1400
Bartlomiej Zolnierkiewicz9a100f42009-02-02 20:12:21 +01001401static void ide_port_free(ide_hwif_t *hwif)
1402{
1403 ide_port_free_devices(hwif);
1404 ide_free_port_slot(hwif->index);
1405 kfree(hwif);
1406}
1407
1408static void ide_disable_port(ide_hwif_t *hwif)
1409{
1410 struct ide_host *host = hwif->host;
1411 int i;
1412
1413 printk(KERN_INFO "%s: disabling port\n", hwif->name);
1414
1415 for (i = 0; i < MAX_HOST_PORTS; i++) {
1416 if (host->ports[i] == hwif) {
1417 host->ports[i] = NULL;
1418 host->n_ports--;
1419 }
1420 }
1421
1422 ide_port_free(hwif);
1423}
1424
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001425int ide_host_register(struct ide_host *host, const struct ide_port_info *d,
Bartlomiej Zolnierkiewicz9f36d312009-05-17 19:12:25 +02001426 struct ide_hw **hws)
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001427{
1428 ide_hwif_t *hwif, *mate = NULL;
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001429 int i, j = 0;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001430
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001431 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001432 if (hwif == NULL) {
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001433 mate = NULL;
1434 continue;
1435 }
1436
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +02001437 ide_init_port_hw(hwif, hws[i]);
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +02001438 ide_port_apply_params(hwif);
1439
Bartlomiej Zolnierkiewicz0a6e49e2009-03-27 12:46:27 +01001440 if ((i & 1) && mate) {
1441 hwif->mate = mate;
1442 mate->mate = hwif;
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +02001443 }
1444
Bartlomiej Zolnierkiewicz0a6e49e2009-03-27 12:46:27 +01001445 mate = (i & 1) ? NULL : hwif;
1446
1447 ide_init_port(hwif, i & 1, d);
1448 ide_port_cable_detect(hwif);
Bartlomiej Zolnierkiewicz5880b5d2009-06-15 18:52:54 +02001449
1450 hwif->port_flags |= IDE_PFLAG_PROBING;
1451
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001452 ide_port_init_devices(hwif);
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001453 }
1454
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001455 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001456 if (hwif == NULL)
1457 continue;
Bartlomiej Zolnierkiewicz139ddfc2008-02-01 23:09:36 +01001458
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001459 if (ide_probe_port(hwif) == 0)
1460 hwif->present = 1;
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +01001461
Bartlomiej Zolnierkiewicz5880b5d2009-06-15 18:52:54 +02001462 hwif->port_flags &= ~IDE_PFLAG_PROBING;
1463
Bartlomiej Zolnierkiewiczc094ea02009-03-27 12:46:28 +01001464 if ((hwif->host_flags & IDE_HFLAG_4DRIVES) == 0 ||
1465 hwif->mate == NULL || hwif->mate->present == 0) {
Bartlomiej Zolnierkiewicz9a100f42009-02-02 20:12:21 +01001466 if (ide_register_port(hwif)) {
1467 ide_disable_port(hwif);
1468 continue;
1469 }
1470 }
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +01001471
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001472 if (hwif->present)
1473 ide_port_tune_devices(hwif);
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001474 }
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001475
Bartlomiej Zolnierkiewiczffc36c72009-06-23 03:43:00 -07001476 ide_host_enable_irqs(host);
1477
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001478 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001479 if (hwif == NULL)
1480 continue;
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001481
1482 if (hwif_init(hwif) == 0) {
1483 printk(KERN_INFO "%s: failed to initialize IDE "
1484 "interface\n", hwif->name);
Bartlomiej Zolnierkiewiczd410a642017-10-03 13:18:47 +02001485 device_unregister(hwif->portdev);
Ian Campbell51d6ac72009-02-02 20:12:22 +01001486 device_unregister(&hwif->gendev);
1487 ide_disable_port(hwif);
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001488 continue;
1489 }
Bartlomiej Zolnierkiewiczdecdc3f2008-02-02 19:56:41 +01001490
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001491 if (hwif->present)
Elias Oltmannse415e492008-10-13 21:39:45 +02001492 if (ide_port_setup_devices(hwif) == 0) {
1493 hwif->present = 0;
1494 continue;
1495 }
1496
1497 j++;
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +02001498
Bartlomiej Zolnierkiewicz8b803bd2009-03-24 23:22:41 +01001499 ide_acpi_init_port(hwif);
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001500
1501 if (hwif->present)
1502 ide_acpi_port_init_devices(hwif);
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001503 }
1504
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001505 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001506 if (hwif == NULL)
1507 continue;
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001508
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001509 ide_sysfs_register_port(hwif);
1510 ide_proc_register_port(hwif);
1511
Wolfram Sangdbee0322010-09-02 11:28:39 +00001512 if (hwif->present) {
Bartlomiej Zolnierkiewiczd9270a32008-02-02 19:56:43 +01001513 ide_proc_port_register_devices(hwif);
Wolfram Sangdbee0322010-09-02 11:28:39 +00001514 hwif_register_devices(hwif);
1515 }
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001516 }
1517
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001518 return j ? 0 : -1;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001519}
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001520EXPORT_SYMBOL_GPL(ide_host_register);
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001521
Bartlomiej Zolnierkiewicz9f36d312009-05-17 19:12:25 +02001522int ide_host_add(const struct ide_port_info *d, struct ide_hw **hws,
Bartlomiej Zolnierkiewiczdca39832009-05-17 19:12:24 +02001523 unsigned int n_ports, struct ide_host **hostp)
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001524{
1525 struct ide_host *host;
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001526 int rc;
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001527
Bartlomiej Zolnierkiewiczdca39832009-05-17 19:12:24 +02001528 host = ide_host_alloc(d, hws, n_ports);
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001529 if (host == NULL)
1530 return -ENOMEM;
1531
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001532 rc = ide_host_register(host, d, hws);
1533 if (rc) {
1534 ide_host_free(host);
1535 return rc;
1536 }
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001537
1538 if (hostp)
1539 *hostp = host;
1540
1541 return 0;
1542}
1543EXPORT_SYMBOL_GPL(ide_host_add);
1544
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001545static void __ide_port_unregister_devices(ide_hwif_t *hwif)
1546{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001547 ide_drive_t *drive;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001548 int i;
1549
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +01001550 ide_port_for_each_present_dev(i, drive, hwif) {
1551 device_unregister(&drive->gendev);
1552 wait_for_completion(&drive->gendev_rel_comp);
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001553 }
1554}
1555
1556void ide_port_unregister_devices(ide_hwif_t *hwif)
1557{
1558 mutex_lock(&ide_cfg_mtx);
1559 __ide_port_unregister_devices(hwif);
1560 hwif->present = 0;
1561 ide_port_init_devices_data(hwif);
1562 mutex_unlock(&ide_cfg_mtx);
1563}
1564EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
1565
1566/**
1567 * ide_unregister - free an IDE interface
1568 * @hwif: IDE interface
1569 *
1570 * Perform the final unregister of an IDE interface.
1571 *
1572 * Locking:
1573 * The caller must not hold the IDE locks.
1574 *
1575 * It is up to the caller to be sure there is no pending I/O here,
1576 * and that the interface will not be reopened (present/vanishing
1577 * locking isn't yet done BTW).
1578 */
1579
1580static void ide_unregister(ide_hwif_t *hwif)
1581{
1582 BUG_ON(in_interrupt());
1583 BUG_ON(irqs_disabled());
1584
1585 mutex_lock(&ide_cfg_mtx);
1586
1587 if (hwif->present) {
1588 __ide_port_unregister_devices(hwif);
1589 hwif->present = 0;
1590 }
1591
1592 ide_proc_unregister_port(hwif);
1593
Michael Schmitza259d532014-02-01 13:48:13 +13001594 if (!hwif->host->get_lock)
1595 free_irq(hwif->irq, hwif);
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001596
1597 device_unregister(hwif->portdev);
1598 device_unregister(&hwif->gendev);
1599 wait_for_completion(&hwif->gendev_rel_comp);
1600
1601 /*
1602 * Remove us from the kernel's knowledge
1603 */
1604 blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
1605 kfree(hwif->sg_table);
1606 unregister_blkdev(hwif->major, hwif->name);
1607
1608 ide_release_dma_engine(hwif);
1609
1610 mutex_unlock(&ide_cfg_mtx);
1611}
1612
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001613void ide_host_free(struct ide_host *host)
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001614{
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001615 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001616 int i;
1617
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001618 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicz9a100f42009-02-02 20:12:21 +01001619 if (hwif)
1620 ide_port_free(hwif);
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +02001621 }
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001622
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001623 kfree(host);
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001624}
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001625EXPORT_SYMBOL_GPL(ide_host_free);
1626
1627void ide_host_remove(struct ide_host *host)
1628{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001629 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001630 int i;
1631
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001632 ide_host_for_each_port(i, hwif, host) {
1633 if (hwif)
1634 ide_unregister(hwif);
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001635 }
1636
1637 ide_host_free(host);
1638}
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001639EXPORT_SYMBOL_GPL(ide_host_remove);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001640
1641void ide_port_scan(ide_hwif_t *hwif)
1642{
Bartlomiej Zolnierkiewicz5880b5d2009-06-15 18:52:54 +02001643 int rc;
1644
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +02001645 ide_port_apply_params(hwif);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001646 ide_port_cable_detect(hwif);
Bartlomiej Zolnierkiewicz5880b5d2009-06-15 18:52:54 +02001647
1648 hwif->port_flags |= IDE_PFLAG_PROBING;
1649
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001650 ide_port_init_devices(hwif);
1651
Bartlomiej Zolnierkiewicz5880b5d2009-06-15 18:52:54 +02001652 rc = ide_probe_port(hwif);
1653
1654 hwif->port_flags &= ~IDE_PFLAG_PROBING;
1655
1656 if (rc < 0)
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001657 return;
1658
1659 hwif->present = 1;
1660
1661 ide_port_tune_devices(hwif);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001662 ide_port_setup_devices(hwif);
Bartlomiej Zolnierkiewicze630fcb2009-01-06 17:21:00 +01001663 ide_acpi_port_init_devices(hwif);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001664 hwif_register_devices(hwif);
1665 ide_proc_port_register_devices(hwif);
1666}
1667EXPORT_SYMBOL_GPL(ide_port_scan);