blob: 1ddc45a04418cde357f71819b1e6a9318c35787c [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01003 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
4 * Copyright (C) 2005, 2007 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
6
7/*
8 * Mostly written by Mark Lord <mlord@pobox.com>
9 * and Gadi Oxman <gadio@netvision.net.il>
10 * and Andre Hedrick <andre@linux-ide.org>
11 *
12 * See linux/MAINTAINERS for address of current maintainer.
13 *
14 * This is the IDE probe module, as evolved from hd.c and ide.c.
15 *
Bartlomiej Zolnierkiewiczbbe4d6d2007-12-12 23:32:00 +010016 * -- increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
17 * by Andrea Arcangeli
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/string.h>
23#include <linux/kernel.h>
24#include <linux/timer.h>
25#include <linux/mm.h>
26#include <linux/interrupt.h>
27#include <linux/major.h>
28#include <linux/errno.h>
29#include <linux/genhd.h>
30#include <linux/slab.h>
31#include <linux/delay.h>
32#include <linux/ide.h>
33#include <linux/spinlock.h>
34#include <linux/kmod.h>
35#include <linux/pci.h>
FUJITA Tomonoridc817852007-10-23 09:29:58 +020036#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/byteorder.h>
39#include <asm/irq.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080040#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/io.h>
42
43/**
44 * generic_id - add a generic drive id
45 * @drive: drive to make an ID block for
46 *
47 * Add a fake id field to the drive we are passed. This allows
48 * use to skip a ton of NULL checks (which people always miss)
49 * and make drive properties unconditional outside of this file
50 */
51
52static void generic_id(ide_drive_t *drive)
53{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +020054 u16 *id = drive->id;
55
56 id[ATA_ID_CUR_CYLS] = id[ATA_ID_CYLS] = drive->cyl;
57 id[ATA_ID_CUR_HEADS] = id[ATA_ID_HEADS] = drive->head;
58 id[ATA_ID_CUR_SECTORS] = id[ATA_ID_SECTORS] = drive->sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
61static void ide_disk_init_chs(ide_drive_t *drive)
62{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +020063 u16 *id = drive->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 /* Extract geometry if we did not already have one for the drive */
66 if (!drive->cyl || !drive->head || !drive->sect) {
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +020067 drive->cyl = drive->bios_cyl = id[ATA_ID_CYLS];
68 drive->head = drive->bios_head = id[ATA_ID_HEADS];
69 drive->sect = drive->bios_sect = id[ATA_ID_SECTORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
71
72 /* Handle logical geometry translation by the drive */
Bartlomiej Zolnierkiewiczdd8f46f2008-10-10 22:39:19 +020073 if (ata_id_current_chs_valid(id)) {
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +020074 drive->cyl = id[ATA_ID_CUR_CYLS];
75 drive->head = id[ATA_ID_CUR_HEADS];
76 drive->sect = id[ATA_ID_CUR_SECTORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
78
79 /* Use physical geometry if what we have still makes no sense */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +020080 if (drive->head > 16 && id[ATA_ID_HEADS] && id[ATA_ID_HEADS] <= 16) {
81 drive->cyl = id[ATA_ID_CYLS];
82 drive->head = id[ATA_ID_HEADS];
83 drive->sect = id[ATA_ID_SECTORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85}
86
87static void ide_disk_init_mult_count(ide_drive_t *drive)
88{
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +020089 u16 *id = drive->id;
90 u8 max_multsect = id[ATA_ID_MAX_MULTSECT] & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +020092 if (max_multsect) {
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +020093 if ((max_multsect / 2) > 1)
94 id[ATA_ID_MULTSECT] = max_multsect | 0x100;
95 else
96 id[ATA_ID_MULTSECT] &= ~0x1ff;
97
98 drive->mult_req = id[ATA_ID_MULTSECT] & 0xff;
Bartlomiej Zolnierkiewicz7c51c172008-10-10 22:39:26 +020099
100 if (drive->mult_req)
Bartlomiej Zolnierkiewiczca1b96e2009-05-17 19:12:21 +0200101 drive->special_flags |= IDE_SFLAG_SET_MULTMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103}
104
Bartlomiej Zolnierkiewicz24630dc2009-01-02 16:12:47 +0100105static void ide_classify_ata_dev(ide_drive_t *drive)
106{
107 u16 *id = drive->id;
108 char *m = (char *)&id[ATA_ID_PROD];
109 int is_cfa = ata_id_is_cfa(id);
110
111 /* CF devices are *not* removable in Linux definition of the term */
112 if (is_cfa == 0 && (id[ATA_ID_CONFIG] & (1 << 7)))
113 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
114
115 drive->media = ide_disk;
116
117 if (!ata_id_has_unload(drive->id))
118 drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
119
120 printk(KERN_INFO "%s: %s, %s DISK drive\n", drive->name, m,
121 is_cfa ? "CFA" : "ATA");
122}
123
124static void ide_classify_atapi_dev(ide_drive_t *drive)
125{
126 u16 *id = drive->id;
127 char *m = (char *)&id[ATA_ID_PROD];
128 u8 type = (id[ATA_ID_CONFIG] >> 8) & 0x1f;
129
130 printk(KERN_INFO "%s: %s, ATAPI ", drive->name, m);
131 switch (type) {
132 case ide_floppy:
133 if (!strstr(m, "CD-ROM")) {
134 if (!strstr(m, "oppy") &&
135 !strstr(m, "poyp") &&
136 !strstr(m, "ZIP"))
137 printk(KERN_CONT "cdrom or floppy?, assuming ");
138 if (drive->media != ide_cdrom) {
139 printk(KERN_CONT "FLOPPY");
140 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
141 break;
142 }
143 }
144 /* Early cdrom models used zero */
145 type = ide_cdrom;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500146 fallthrough;
Bartlomiej Zolnierkiewicz24630dc2009-01-02 16:12:47 +0100147 case ide_cdrom:
148 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
149#ifdef CONFIG_PPC
150 /* kludge for Apple PowerBook internal zip */
151 if (!strstr(m, "CD-ROM") && strstr(m, "ZIP")) {
152 printk(KERN_CONT "FLOPPY");
153 type = ide_floppy;
154 break;
155 }
156#endif
157 printk(KERN_CONT "CD/DVD-ROM");
158 break;
159 case ide_tape:
160 printk(KERN_CONT "TAPE");
161 break;
162 case ide_optical:
163 printk(KERN_CONT "OPTICAL");
164 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
165 break;
166 default:
167 printk(KERN_CONT "UNKNOWN (type %d)", type);
168 break;
169 }
170
171 printk(KERN_CONT " drive\n");
172 drive->media = type;
173 /* an ATAPI device ignores DRDY */
174 drive->ready_stat = 0;
175 if (ata_id_cdb_intr(id))
176 drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
177 drive->dev_flags |= IDE_DFLAG_DOORLOCKING;
178 /* we don't do head unloading on ATAPI devices */
179 drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * do_identify - identify a drive
184 * @drive: drive to identify
185 * @cmd: command used
Bartlomiej Zolnierkiewicz2ebe1d92009-03-24 23:22:59 +0100186 * @id: buffer for IDENTIFY data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 *
188 * Called when we have issued a drive identify command to
189 * read and parse the results. This function is run with
190 * interrupts disabled.
191 */
Bartlomiej Zolnierkiewicz047140a2008-12-29 20:27:36 +0100192
Bartlomiej Zolnierkiewicz2ebe1d92009-03-24 23:22:59 +0100193static void do_identify(ide_drive_t *drive, u8 cmd, u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100195 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200196 char *m = (char *)&id[ATA_ID_PROD];
Bartlomiej Zolnierkiewicz94b9efd2008-12-29 20:27:38 +0100197 unsigned long flags;
Bartlomiej Zolnierkiewicz24630dc2009-01-02 16:12:47 +0100198 int bswap = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Bartlomiej Zolnierkiewicz94b9efd2008-12-29 20:27:38 +0100200 /* local CPU only; some systems need this */
201 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* read 512 bytes of id info */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200203 hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
Bartlomiej Zolnierkiewicz94b9efd2008-12-29 20:27:38 +0100204 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200206 drive->dev_flags |= IDE_DFLAG_ID_READ;
Bartlomiej Zolnierkiewicz7b9f25b2008-02-01 23:09:28 +0100207#ifdef DEBUG
208 printk(KERN_INFO "%s: dumping identify data\n", drive->name);
209 ide_dump_identify((u8 *)id);
210#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 ide_fix_driveid(id);
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /*
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200214 * ATA_CMD_ID_ATA returns little-endian info,
215 * ATA_CMD_ID_ATAPI *usually* returns little-endian info.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 */
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200217 if (cmd == ATA_CMD_ID_ATAPI) {
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200218 if ((m[0] == 'N' && m[1] == 'E') || /* NEC */
219 (m[0] == 'F' && m[1] == 'X') || /* Mitsumi */
220 (m[0] == 'P' && m[1] == 'i')) /* Pioneer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /* Vertos drives may still be weird */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200222 bswap ^= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200224
225 ide_fixstring(m, ATA_ID_PROD_LEN, bswap);
226 ide_fixstring((char *)&id[ATA_ID_FW_REV], ATA_ID_FW_REV_LEN, bswap);
227 ide_fixstring((char *)&id[ATA_ID_SERNO], ATA_ID_SERNO_LEN, bswap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Tejun Heo699b0522007-11-05 21:42:25 +0100229 /* we depend on this a lot! */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200230 m[ATA_ID_PROD_LEN - 1] = '\0';
Tejun Heo699b0522007-11-05 21:42:25 +0100231
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200232 if (strstr(m, "E X A B Y T E N E S T"))
Bartlomiej Zolnierkiewicz69197ad2009-03-27 12:46:26 +0100233 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
234 else
235 drive->dev_flags |= IDE_DFLAG_PRESENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238/**
Bartlomiej Zolnierkiewicz2ebe1d92009-03-24 23:22:59 +0100239 * ide_dev_read_id - send ATA/ATAPI IDENTIFY command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 * @drive: drive to identify
241 * @cmd: command to use
Bartlomiej Zolnierkiewicz2ebe1d92009-03-24 23:22:59 +0100242 * @id: buffer for IDENTIFY data
Bartlomiej Zolnierkiewiczfa56d4c2009-06-23 11:29:11 +0000243 * @irq_ctx: flag set when called from the IRQ context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 *
Bartlomiej Zolnierkiewicz2ebe1d92009-03-24 23:22:59 +0100245 * Sends an ATA(PI) IDENTIFY request to a drive and waits for a response.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 *
247 * Returns: 0 device was identified
248 * 1 device timed-out (no response to identify request)
249 * 2 device aborted the command (refused to identify itself)
250 */
251
Bartlomiej Zolnierkiewiczfa56d4c2009-06-23 11:29:11 +0000252int ide_dev_read_id(ide_drive_t *drive, u8 cmd, u16 *id, int irq_ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100254 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200255 struct ide_io_ports *io_ports = &hwif->io_ports;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200256 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100257 int use_altstatus = 0, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 unsigned long timeout;
259 u8 s = 0, a = 0;
260
Bartlomiej Zolnierkiewicza1828072009-03-24 23:22:53 +0100261 /*
262 * Disable device IRQ. Otherwise we'll get spurious interrupts
263 * during the identify phase that the IRQ handler isn't expecting.
264 */
265 if (io_ports->ctl_addr)
Sergei Shtylyovecf3a312009-03-31 20:15:30 +0200266 tp_ops->write_devctl(hwif, ATA_NIEN | ATA_DEVCTL_OBS);
Bartlomiej Zolnierkiewicza1828072009-03-24 23:22:53 +0100267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /* take a deep breath */
Bartlomiej Zolnierkiewiczfa56d4c2009-06-23 11:29:11 +0000269 if (irq_ctx)
270 mdelay(50);
271 else
272 msleep(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Bartlomiej Zolnierkiewicz66364872008-12-02 20:40:03 +0100274 if (io_ports->ctl_addr &&
275 (hwif->host_flags & IDE_HFLAG_BROKEN_ALTSTATUS) == 0) {
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200276 a = tp_ops->read_altstatus(hwif);
277 s = tp_ops->read_status(hwif);
Hannes Reinecke27f00e52015-03-27 16:46:33 +0100278 if ((a ^ s) & ~ATA_SENSE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 /* ancient Seagate drives, broken interfaces */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100280 printk(KERN_INFO "%s: probing with STATUS(0x%02x) "
281 "instead of ALTSTATUS(0x%02x)\n",
282 drive->name, s, a);
283 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /* use non-intrusive polling */
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100285 use_altstatus = 1;
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 /* set features register for atapi
289 * identify command to be sure of reply
290 */
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200291 if (cmd == ATA_CMD_ID_ATAPI) {
Sergei Shtylyovc9ff9e72009-04-08 14:13:03 +0200292 struct ide_taskfile tf;
Bartlomiej Zolnierkiewicz4e658372008-07-23 19:55:53 +0200293
Sergei Shtylyovc9ff9e72009-04-08 14:13:03 +0200294 memset(&tf, 0, sizeof(tf));
Bartlomiej Zolnierkiewicz4e658372008-07-23 19:55:53 +0200295 /* disable DMA & overlap */
Sergei Shtylyovc9ff9e72009-04-08 14:13:03 +0200296 tp_ops->tf_load(drive, &tf, IDE_VALID_FEATURE);
Bartlomiej Zolnierkiewicz4e658372008-07-23 19:55:53 +0200297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 /* ask drive for ID */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200300 tp_ops->exec_command(hwif, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200302 timeout = ((cmd == ATA_CMD_ID_ATA) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200303
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200304 /* wait for IRQ and ATA_DRQ */
Bartlomiej Zolnierkiewiczfa56d4c2009-06-23 11:29:11 +0000305 if (irq_ctx) {
306 rc = __ide_wait_stat(drive, ATA_DRQ, BAD_R_STAT, timeout, &s);
307 if (rc)
308 return 1;
309 } else {
310 rc = ide_busy_sleep(drive, timeout, use_altstatus);
311 if (rc)
312 return 1;
313
314 msleep(50);
315 s = tp_ops->read_status(hwif);
316 }
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100317
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200318 if (OK_STAT(s, ATA_DRQ, BAD_R_STAT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* drive returned ID */
Bartlomiej Zolnierkiewicz2ebe1d92009-03-24 23:22:59 +0100320 do_identify(drive, cmd, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /* drive responded with ID */
322 rc = 0;
323 /* clear drive IRQ */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200324 (void)tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 } else {
326 /* drive refused ID */
327 rc = 2;
328 }
329 return rc;
330}
331
Bartlomiej Zolnierkiewicz28ee9bc2009-05-22 16:23:38 +0200332int ide_busy_sleep(ide_drive_t *drive, unsigned long timeout, int altstatus)
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100333{
Bartlomiej Zolnierkiewicz28ee9bc2009-05-22 16:23:38 +0200334 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100335 u8 stat;
336
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200337 timeout += jiffies;
338
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100339 do {
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200340 msleep(50); /* give drive a breather */
341 stat = altstatus ? hwif->tp_ops->read_altstatus(hwif)
342 : hwif->tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200343 if ((stat & ATA_BUSY) == 0)
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100344 return 0;
345 } while (time_before(jiffies, timeout));
346
Bartlomiej Zolnierkiewicz28ee9bc2009-05-22 16:23:38 +0200347 printk(KERN_ERR "%s: timeout in %s\n", drive->name, __func__);
348
Bartlomiej Zolnierkiewiczb163f462008-10-10 22:39:23 +0200349 return 1; /* drive timed-out */
Bartlomiej Zolnierkiewicz3a5015c2008-01-26 20:13:09 +0100350}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200352static u8 ide_read_device(ide_drive_t *drive)
353{
Sergei Shtylyov3153c262009-04-08 14:13:03 +0200354 struct ide_taskfile tf;
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200355
Sergei Shtylyov3153c262009-04-08 14:13:03 +0200356 drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_DEVICE);
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200357
Sergei Shtylyov3153c262009-04-08 14:13:03 +0200358 return tf.device;
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200359}
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361/**
362 * do_probe - probe an IDE device
363 * @drive: drive to probe
364 * @cmd: command to use
365 *
366 * do_probe() has the difficult job of finding a drive if it exists,
367 * without getting hung up if it doesn't exist, without trampling on
368 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
369 *
370 * If a drive is "known" to exist (from CMOS or kernel parameters),
371 * but does not respond right away, the probe will "hang in there"
372 * for the maximum wait time (about 30 seconds), otherwise it will
373 * exit much more quickly.
374 *
375 * Returns: 0 device was identified
376 * 1 device timed-out (no response to identify request)
377 * 2 device aborted the command (refused to identify itself)
378 * 3 bad status from device (possible for ATAPI drives)
379 * 4 probe was not attempted because failure was obvious
380 */
381
382static int do_probe (ide_drive_t *drive, u8 cmd)
383{
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100384 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200385 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz2ebe1d92009-03-24 23:22:59 +0100386 u16 *id = drive->id;
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100387 int rc;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200388 u8 present = !!(drive->dev_flags & IDE_DFLAG_PRESENT), stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200390 /* avoid waiting for inappropriate probes */
391 if (present && drive->media != ide_disk && cmd == ATA_CMD_ID_ATA)
392 return 4;
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394#ifdef DEBUG
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200395 printk(KERN_INFO "probing for %s: present=%d, media=%d, probetype=%s\n",
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200396 drive->name, present, drive->media,
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200397 (cmd == ATA_CMD_ID_ATA) ? "ATA" : "ATAPI");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398#endif
399
400 /* needed for some systems
401 * (e.g. crw9624 as drive0 with disk as slave)
402 */
403 msleep(50);
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200404 tp_ops->dev_select(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 msleep(50);
Bartlomiej Zolnierkiewicz1f2efb82008-07-23 19:55:54 +0200406
Bartlomiej Zolnierkiewicz7f612f22008-10-13 21:39:40 +0200407 if (ide_read_device(drive) != drive->select && present == 0) {
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +0200408 if (drive->dn & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /* exit with drive0 selected */
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200410 tp_ops->dev_select(hwif->devices[0]);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200411 /* allow ATA_BUSY to assert & clear */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 msleep(50);
413 }
414 /* no i/f present: mmm.. this should be a 4 -ml */
415 return 3;
416 }
417
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200418 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewiczc47137a2008-02-06 02:57:51 +0100419
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200420 if (OK_STAT(stat, ATA_DRDY, ATA_BUSY) ||
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200421 present || cmd == ATA_CMD_ID_ATAPI) {
Bartlomiej Zolnierkiewiczfa56d4c2009-06-23 11:29:11 +0000422 rc = ide_dev_read_id(drive, cmd, id, 0);
Bartlomiej Zolnierkiewicz2ebe1d92009-03-24 23:22:59 +0100423 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /* failed: try again */
Bartlomiej Zolnierkiewiczfa56d4c2009-06-23 11:29:11 +0000425 rc = ide_dev_read_id(drive, cmd, id, 0);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100426
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200427 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100428
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200429 if (stat == (ATA_BUSY | ATA_DRDY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return 4;
431
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200432 if (rc == 1 && cmd == ATA_CMD_ID_ATAPI) {
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100433 printk(KERN_ERR "%s: no response (status = 0x%02x), "
434 "resetting drive\n", drive->name, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 msleep(50);
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200436 tp_ops->dev_select(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 msleep(50);
Bartlomiej Zolnierkiewiczaaaade32008-10-10 22:39:21 +0200438 tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
Bartlomiej Zolnierkiewicz28ee9bc2009-05-22 16:23:38 +0200439 (void)ide_busy_sleep(drive, WAIT_WORSTCASE, 0);
Bartlomiej Zolnierkiewiczfa56d4c2009-06-23 11:29:11 +0000440 rc = ide_dev_read_id(drive, cmd, id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100442
443 /* ensure drive IRQ is clear */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200444 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (rc == 1)
Bartlomiej Zolnierkiewicz57b55272008-02-02 19:56:45 +0100447 printk(KERN_ERR "%s: no response (status = 0x%02x)\n",
448 drive->name, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 } else {
450 /* not present or maybe ATAPI */
451 rc = 3;
452 }
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +0200453 if (drive->dn & 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /* exit with drive0 selected */
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200455 tp_ops->dev_select(hwif->devices[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 msleep(50);
457 /* ensure drive irq is clear */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200458 (void)tp_ops->read_status(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460 return rc;
461}
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463/**
464 * probe_for_drives - upper level drive probe
465 * @drive: drive to probe for
466 *
467 * probe_for_drive() tests for existence of a given drive using do_probe()
468 * and presents things to the user as needed.
469 *
470 * Returns: 0 no device was found
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200471 * 1 device was found
472 * (note: IDE_DFLAG_PRESENT might still be not set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 */
Bartlomiej Zolnierkiewicz047140a2008-12-29 20:27:36 +0100474
475static u8 probe_for_drive(ide_drive_t *drive)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200477 char *m;
Bartlomiej Zolnierkiewicz1bd4c1f2009-03-24 23:22:58 +0100478 int rc;
479 u8 cmd;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200480
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200481 drive->dev_flags &= ~IDE_DFLAG_ID_READ;
482
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200483 m = (char *)&drive->id[ATA_ID_PROD];
484 strcpy(m, "UNKNOWN");
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /* skip probing? */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200487 if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 /* if !(success||timed-out) */
Bartlomiej Zolnierkiewicz1bd4c1f2009-03-24 23:22:58 +0100489 cmd = ATA_CMD_ID_ATA;
490 rc = do_probe(drive, cmd);
491 if (rc >= 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /* look for ATAPI device */
Bartlomiej Zolnierkiewicz1bd4c1f2009-03-24 23:22:58 +0100493 cmd = ATA_CMD_ID_ATAPI;
494 rc = do_probe(drive, cmd);
495 }
Bartlomiej Zolnierkiewiczc36a7e92008-10-10 22:39:23 +0200496
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200497 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Hugh Dickins5f4417a2009-06-10 14:37:20 +0200498 return 0;
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /* identification failed? */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200501 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (drive->media == ide_disk) {
503 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
504 drive->name, drive->cyl,
505 drive->head, drive->sect);
506 } else if (drive->media == ide_cdrom) {
507 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
508 } else {
509 /* nuke it */
510 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200511 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
Bartlomiej Zolnierkiewicz1bd4c1f2009-03-24 23:22:58 +0100513 } else {
514 if (cmd == ATA_CMD_ID_ATAPI)
515 ide_classify_atapi_dev(drive);
516 else
517 ide_classify_ata_dev(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200520
521 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Hugh Dickins5f4417a2009-06-10 14:37:20 +0200522 return 0;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /* The drive wasn't being helpful. Add generic info only */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200525 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 generic_id(drive);
527 return 1;
528 }
529
530 if (drive->media == ide_disk) {
531 ide_disk_init_chs(drive);
532 ide_disk_init_mult_count(drive);
533 }
534
Bartlomiej Zolnierkiewicz69197ad2009-03-27 12:46:26 +0100535 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Pavel Machekfc410692008-07-23 19:56:02 +0200538static void hwif_release_dev(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
541
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800542 complete(&hwif->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200545static int ide_register_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Randy Dunlap349ae232006-10-03 01:14:23 -0700547 int ret;
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /* register with global device tree */
Kees Cook02aa2a32013-07-03 15:04:56 -0700550 dev_set_name(&hwif->gendev, "%s", hwif->name);
Greg Kroah-Hartmanfcb52072009-04-30 14:43:31 -0700551 dev_set_drvdata(&hwif->gendev, hwif);
Andreas Schwabbb54aff2009-01-19 13:46:56 +0100552 if (hwif->gendev.parent == NULL)
553 hwif->gendev.parent = hwif->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 hwif->gendev.release = hwif_release_dev;
Bartlomiej Zolnierkiewicz96d409412009-01-02 16:12:47 +0100555
Randy Dunlap349ae232006-10-03 01:14:23 -0700556 ret = device_register(&hwif->gendev);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200557 if (ret < 0) {
Randy Dunlap349ae232006-10-03 01:14:23 -0700558 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
Harvey Harrisoneb639632008-04-26 22:25:20 +0200559 __func__, ret);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200560 goto out;
561 }
562
Greg Kroah-Hartman3ee074b2008-07-21 20:03:34 -0700563 hwif->portdev = device_create(ide_port_class, &hwif->gendev,
Kees Cook02aa2a32013-07-03 15:04:56 -0700564 MKDEV(0, 0), hwif, "%s", hwif->name);
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200565 if (IS_ERR(hwif->portdev)) {
566 ret = PTR_ERR(hwif->portdev);
567 device_unregister(&hwif->gendev);
568 }
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200569out:
570 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Bartlomiej Zolnierkiewiczc860a8f2008-02-01 23:09:34 +0100573/**
574 * ide_port_wait_ready - wait for port to become ready
575 * @hwif: IDE port
576 *
577 * This is needed on some PPCs and a bunch of BIOS-less embedded
578 * platforms. Typical cases are:
579 *
580 * - The firmware hard reset the disk before booting the kernel,
581 * the drive is still doing it's poweron-reset sequence, that
582 * can take up to 30 seconds.
583 *
584 * - The firmware does nothing (or no firmware), the device is
585 * still in POST state (same as above actually).
586 *
587 * - Some CD/DVD/Writer combo drives tend to drive the bus during
588 * their reset sequence even when they are non-selected slave
589 * devices, thus preventing discovery of the main HD.
590 *
591 * Doing this wait-for-non-busy should not harm any existing
592 * configuration and fix some issues like the above.
593 *
594 * BenH.
595 *
596 * Returns 0 on success, error code (< 0) otherwise.
597 */
598
599static int ide_port_wait_ready(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200601 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100602 ide_drive_t *drive;
603 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
606
607 /* Let HW settle down a bit from whatever init state we
608 * come from */
609 mdelay(2);
610
611 /* Wait for BSY bit to go away, spec timeout is 30 seconds,
612 * I know of at least one disk who takes 31 seconds, I use 35
613 * here to be safe
614 */
615 rc = ide_wait_not_busy(hwif, 35000);
616 if (rc)
617 return rc;
618
619 /* Now make sure both master & slave are ready */
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100620 ide_port_for_each_dev(i, drive, hwif) {
Jonas Stare82661052007-11-27 21:35:53 +0100621 /* Ignore disks that we will not probe for later. */
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200622 if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0 ||
623 (drive->dev_flags & IDE_DFLAG_PRESENT)) {
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200624 tp_ops->dev_select(drive);
625 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
Jonas Stare82661052007-11-27 21:35:53 +0100626 mdelay(2);
627 rc = ide_wait_not_busy(hwif, 35000);
628 if (rc)
629 goto out;
630 } else
631 printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
632 drive->name);
633 }
634out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /* Exit function with master reselected (let's be sane) */
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100636 if (i)
Sergei Shtylyovfdd88f02009-03-31 20:15:33 +0200637 tp_ops->dev_select(hwif->devices[0]);
Jonas Stare82661052007-11-27 21:35:53 +0100638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return rc;
640}
641
642/**
643 * ide_undecoded_slave - look for bad CF adapters
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200644 * @dev1: slave device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 *
646 * Analyse the drives on the interface and attempt to decide if we
647 * have the same drive viewed twice. This occurs with crap CF adapters
648 * and PCMCIA sometimes.
649 */
650
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200651void ide_undecoded_slave(ide_drive_t *dev1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100653 ide_drive_t *dev0 = dev1->hwif->devices[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200655 if ((dev1->dn & 1) == 0 || (dev0->dev_flags & IDE_DFLAG_PRESENT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return;
657
658 /* If the models don't match they are not the same product */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200659 if (strcmp((char *)&dev0->id[ATA_ID_PROD],
660 (char *)&dev1->id[ATA_ID_PROD]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return;
662
663 /* Serial numbers do not match */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200664 if (strncmp((char *)&dev0->id[ATA_ID_SERNO],
665 (char *)&dev1->id[ATA_ID_SERNO], ATA_ID_SERNO_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return;
667
668 /* No serial number, thankfully very rare for CF */
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200669 if (*(char *)&dev0->id[ATA_ID_SERNO] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return;
671
672 /* Appears to be an IDE flash adapter with decode bugs */
673 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
674
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200675 dev1->dev_flags &= ~IDE_DFLAG_PRESENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
678EXPORT_SYMBOL_GPL(ide_undecoded_slave);
679
Bartlomiej Zolnierkiewicz9d501522008-02-01 23:09:36 +0100680static int ide_probe_port(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100682 ide_drive_t *drive;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 unsigned int irqd;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100684 int i, rc = -ENODEV;
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100685
686 BUG_ON(hwif->present);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100688 if ((hwif->devices[0]->dev_flags & IDE_DFLAG_NOPROBE) &&
689 (hwif->devices[1]->dev_flags & IDE_DFLAG_NOPROBE))
Bartlomiej Zolnierkiewicz9d501522008-02-01 23:09:36 +0100690 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /*
693 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
694 * we'll install our IRQ driver much later...
695 */
696 irqd = hwif->irq;
697 if (irqd)
698 disable_irq(hwif->irq);
699
David S. Miller9ce41ae2010-03-28 18:58:28 -0700700 if (ide_port_wait_ready(hwif) == -EBUSY)
701 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 /*
Bartlomiej Zolnierkiewiczf367bed2008-03-29 19:48:21 +0100704 * Second drive should only exist if first drive was found,
705 * but a lot of cdrom drives are configured as single slaves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 */
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100707 ide_port_for_each_dev(i, drive, hwif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 (void) probe_for_drive(drive);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200709 if (drive->dev_flags & IDE_DFLAG_PRESENT)
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100710 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
David S. Miller9ce41ae2010-03-28 18:58:28 -0700712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /*
714 * Use cached IRQ number. It might be (and is...) changed by probe
715 * code above
716 */
717 if (irqd)
718 enable_irq(irqd);
719
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +0100720 return rc;
Bartlomiej Zolnierkiewicze84e7ea2008-02-01 23:09:36 +0100721}
722
723static void ide_port_tune_devices(ide_hwif_t *hwif)
724{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200725 const struct ide_port_ops *port_ops = hwif->port_ops;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100726 ide_drive_t *drive;
727 int i;
Bartlomiej Zolnierkiewicze84e7ea2008-02-01 23:09:36 +0100728
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100729 ide_port_for_each_present_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewicz8bc1e5a2009-06-07 15:37:09 +0200730 ide_check_nien_quirk_list(drive);
731
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100732 if (port_ops && port_ops->quirkproc)
733 port_ops->quirkproc(drive);
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100734 }
Bartlomiej Zolnierkiewicz0380dad2007-06-08 15:14:29 +0200735
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100736 ide_port_for_each_present_dev(i, drive, hwif) {
737 ide_set_max_pio(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100739 drive->dev_flags |= IDE_DFLAG_NICE1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100741 if (hwif->dma_ops)
742 ide_set_dma(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744}
745
Bart Van Asscheca18d6f2017-06-20 11:15:41 -0700746static void ide_initialize_rq(struct request *rq)
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100747{
748 struct ide_request *req = blk_mq_rq_to_pdu(rq);
749
Jens Axboed16a6762018-11-12 17:19:32 -0700750 req->special = NULL;
Bart Van Asschec8d9cf22017-06-20 11:15:42 -0700751 scsi_req_init(&req->sreq);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100752 req->sreq.sense = req->sense;
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100753}
754
Jens Axboe60033522018-10-26 09:53:52 -0600755static const struct blk_mq_ops ide_mq_ops = {
756 .queue_rq = ide_queue_rq,
757 .initialize_rq_fn = ide_initialize_rq,
758};
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * init request queue
762 */
763static int ide_init_queue(ide_drive_t *drive)
764{
Jens Axboe165125e2007-07-24 09:28:11 +0200765 struct request_queue *q;
Bartlomiej Zolnierkiewicz898ec222009-01-06 17:20:52 +0100766 ide_hwif_t *hwif = drive->hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 int max_sectors = 256;
768 int max_sg_entries = PRD_ENTRIES;
Jens Axboe60033522018-10-26 09:53:52 -0600769 struct blk_mq_tag_set *set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 /*
772 * Our default set up assumes the normal IDE case,
773 * that is 64K segmenting, standard PRD setup
774 * and LBA28. Some drivers then impose their own
775 * limits and LBA48 we could raise it but as yet
776 * do not.
777 */
Jens Axboe60033522018-10-26 09:53:52 -0600778
779 set = &drive->tag_set;
780 set->ops = &ide_mq_ops;
781 set->nr_hw_queues = 1;
782 set->queue_depth = 32;
783 set->reserved_tags = 1;
784 set->cmd_size = sizeof(struct ide_request);
785 set->numa_node = hwif_to_node(hwif);
786 set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING;
787 if (blk_mq_alloc_tag_set(set))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return 1;
789
Jens Axboe60033522018-10-26 09:53:52 -0600790 q = blk_mq_init_queue(set);
791 if (IS_ERR(q)) {
792 blk_mq_free_tag_set(set);
Christoph Hellwig82ed4db2017-01-27 09:46:29 +0100793 return 1;
794 }
795
Jens Axboe60033522018-10-26 09:53:52 -0600796 blk_queue_flag_set(QUEUE_FLAG_SCSI_PASSTHROUGH, q);
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 q->queuedata = drive;
799 blk_queue_segment_boundary(q, 0xffff);
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (hwif->rqsize < max_sectors)
802 max_sectors = hwif->rqsize;
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500803 blk_queue_max_hw_sectors(q, max_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805#ifdef CONFIG_PCI
806 /* When we have an IOMMU, we may have a problem where pci_map_sg()
807 * creates segments that don't completely match our boundary
808 * requirements and thus need to be broken up again. Because it
809 * doesn't align properly either, we may actually have to break up
810 * to more segments than what was we got in the first place, a max
811 * worst case is twice as many.
812 * This will be fixed once we teach pci_map_sg() about our boundary
813 * requirements, hopefully soon. *FIXME*
814 */
Christoph Hellwig164c3d02018-04-03 20:29:44 +0200815 max_sg_entries >>= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816#endif /* CONFIG_PCI */
817
Martin K. Petersen8a783622010-02-26 00:20:39 -0500818 blk_queue_max_segments(q, max_sg_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 /* assign drive queue */
821 drive->queue = q;
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return 0;
824}
825
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +0100826static DEFINE_MUTEX(ide_cfg_mtx);
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828/*
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100829 * For any present drive:
830 * - allocate the block device queue
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100831 */
Elias Oltmannse415e492008-10-13 21:39:45 +0200832static int ide_port_setup_devices(ide_hwif_t *hwif)
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100833{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100834 ide_drive_t *drive;
Elias Oltmannse415e492008-10-13 21:39:45 +0200835 int i, j = 0;
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100836
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +0200837 mutex_lock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +0100838 ide_port_for_each_present_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100839 if (ide_init_queue(drive)) {
840 printk(KERN_ERR "ide: failed to init %s\n",
841 drive->name);
Elias Oltmannse415e492008-10-13 21:39:45 +0200842 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100843 continue;
844 }
845
Elias Oltmannse415e492008-10-13 21:39:45 +0200846 j++;
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100847 }
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +0200848 mutex_unlock(&ide_cfg_mtx);
Elias Oltmannse415e492008-10-13 21:39:45 +0200849
850 return j;
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100851}
852
Bartlomiej Zolnierkiewiczffc36c72009-06-23 03:43:00 -0700853static void ide_host_enable_irqs(struct ide_host *host)
854{
855 ide_hwif_t *hwif;
856 int i;
857
858 ide_host_for_each_port(i, hwif, host) {
859 if (hwif == NULL)
860 continue;
861
862 /* clear any pending IRQs */
863 hwif->tp_ops->read_status(hwif);
864
865 /* unmask IRQs */
866 if (hwif->io_ports.ctl_addr)
867 hwif->tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
868 }
869}
870
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100871/*
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100872 * This routine sets up the IRQ for an IDE interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 */
874static int init_irq (ide_hwif_t *hwif)
875{
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200876 struct ide_io_ports *io_ports = &hwif->io_ports;
Bartlomiej Zolnierkiewicz255115fb2009-03-27 12:46:27 +0100877 struct ide_host *host = hwif->host;
878 irq_handler_t irq_handler = host->irq_handler;
879 int sa = host->irq_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Stanislaw Gruszka849d7132009-03-05 16:10:57 +0100881 if (irq_handler == NULL)
882 irq_handler = ide_intr;
883
Michael Schmitza259d532014-02-01 13:48:13 +1300884 if (!host->get_lock)
885 if (request_irq(hwif->irq, irq_handler, sa, hwif->name, hwif))
886 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Adrian Bunk7b892802008-02-06 01:36:29 -0800888#if !defined(__mc68000__)
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200889 printk(KERN_INFO "%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200890 io_ports->data_addr, io_ports->status_addr,
891 io_ports->ctl_addr, hwif->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892#else
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200893 printk(KERN_INFO "%s at 0x%08lx on irq %d", hwif->name,
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200894 io_ports->data_addr, hwif->irq);
Adrian Bunk7b892802008-02-06 01:36:29 -0800895#endif /* __mc68000__ */
Bartlomiej Zolnierkiewiczb65fac32009-01-06 17:20:50 +0100896 if (hwif->host->host_flags & IDE_HFLAG_SERIALIZE)
897 printk(KERN_CONT " (serialized)");
Bartlomiej Zolnierkiewicz1b8ebad2008-07-24 22:53:36 +0200898 printk(KERN_CONT "\n");
Bartlomiej Zolnierkiewiczd5bc6592008-02-02 19:56:41 +0100899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901out_up:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return 1;
903}
904
905static int ata_lock(dev_t dev, void *data)
906{
907 /* FIXME: we want to pin hwif down */
908 return 0;
909}
910
911static struct kobject *ata_probe(dev_t dev, int *part, void *data)
912{
913 ide_hwif_t *hwif = data;
914 int unit = *part >> PARTN_BITS;
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +0100915 ide_drive_t *drive = hwif->devices[unit];
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200916
917 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return NULL;
919
920 if (drive->media == ide_disk)
921 request_module("ide-disk");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (drive->media == ide_cdrom || drive->media == ide_optical)
923 request_module("ide-cd");
924 if (drive->media == ide_tape)
925 request_module("ide-tape");
926 if (drive->media == ide_floppy)
927 request_module("ide-floppy");
928
929 return NULL;
930}
931
932static struct kobject *exact_match(dev_t dev, int *part, void *data)
933{
934 struct gendisk *p = data;
935 *part &= (1 << PARTN_BITS) - 1;
Tejun Heoed9e1982008-08-25 19:56:05 +0900936 return &disk_to_dev(p)->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
939static int exact_lock(dev_t dev, void *data)
940{
941 struct gendisk *p = data;
942
Jan Kara3079c222018-02-26 13:01:38 +0100943 if (!get_disk_and_module(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return -1;
945 return 0;
946}
947
948void ide_register_region(struct gendisk *disk)
949{
950 blk_register_region(MKDEV(disk->major, disk->first_minor),
951 disk->minors, NULL, exact_match, exact_lock, disk);
952}
953
954EXPORT_SYMBOL_GPL(ide_register_region);
955
956void ide_unregister_region(struct gendisk *disk)
957{
958 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
959 disk->minors);
960}
961
962EXPORT_SYMBOL_GPL(ide_unregister_region);
963
964void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
965{
966 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewicz7f612f22008-10-13 21:39:40 +0200967 unsigned int unit = drive->dn & 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 disk->major = hwif->major;
970 disk->first_minor = unit << PARTN_BITS;
971 sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
972 disk->queue = drive->queue;
973}
974
975EXPORT_SYMBOL_GPL(ide_init_disk);
976
977static void drive_release_dev (struct device *dev)
978{
979 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
980
Bartlomiej Zolnierkiewicz5b0c4b32008-04-18 00:46:22 +0200981 ide_proc_unregister_device(drive);
982
Jens Axboe60033522018-10-26 09:53:52 -0600983 if (drive->sense_rq)
984 blk_mq_free_request(drive->sense_rq);
985
Bartlomiej Zolnierkiewiczb5479162009-03-31 20:15:25 +0200986 blk_cleanup_queue(drive->queue);
987 drive->queue = NULL;
Jens Axboe60033522018-10-26 09:53:52 -0600988 blk_mq_free_tag_set(&drive->tag_set);
Bartlomiej Zolnierkiewiczb5479162009-03-31 20:15:25 +0200989
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200990 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200991
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800992 complete(&drive->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995static int hwif_init(ide_hwif_t *hwif)
996{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (!hwif->irq) {
Bartlomiej Zolnierkiewicz8b07ed22009-03-24 23:22:52 +0100998 printk(KERN_ERR "%s: disabled, no IRQ\n", hwif->name);
999 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (register_blkdev(hwif->major, hwif->name))
1003 return 0;
1004
1005 if (!hwif->sg_max_nents)
1006 hwif->sg_max_nents = PRD_ENTRIES;
1007
Kees Cook6da2ec52018-06-12 13:55:00 -07001008 hwif->sg_table = kmalloc_array(hwif->sg_max_nents,
1009 sizeof(struct scatterlist),
1010 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (!hwif->sg_table) {
1012 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1013 goto out;
1014 }
Jens Axboe45711f12007-10-22 21:19:53 +02001015
1016 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (init_irq(hwif)) {
Bartlomiej Zolnierkiewicz8b07ed22009-03-24 23:22:52 +01001019 printk(KERN_ERR "%s: disabled, unable to get IRQ %d\n",
1020 hwif->name, hwif->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 goto out;
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Bartlomiej Zolnierkiewicz3a4e7c92008-02-02 19:56:40 +01001024 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1025 THIS_MODULE, ata_probe, ata_lock, hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return 1;
1027
1028out:
1029 unregister_blkdev(hwif->major, hwif->name);
1030 return 0;
1031}
1032
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001033static void hwif_register_devices(ide_hwif_t *hwif)
1034{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001035 ide_drive_t *drive;
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001036 unsigned int i;
1037
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +01001038 ide_port_for_each_present_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewiczc5d70cc2008-02-02 19:56:41 +01001039 struct device *dev = &drive->gendev;
1040 int ret;
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001041
Kay Sieversdc09c782008-12-29 20:27:36 +01001042 dev_set_name(dev, "%u.%u", hwif->index, i);
Greg Kroah-Hartmanfcb52072009-04-30 14:43:31 -07001043 dev_set_drvdata(dev, drive);
Bartlomiej Zolnierkiewiczc5d70cc2008-02-02 19:56:41 +01001044 dev->parent = &hwif->gendev;
1045 dev->bus = &ide_bus_type;
Bartlomiej Zolnierkiewiczc5d70cc2008-02-02 19:56:41 +01001046 dev->release = drive_release_dev;
1047
1048 ret = device_register(dev);
1049 if (ret < 0)
1050 printk(KERN_WARNING "IDE: %s: device_register error: "
1051 "%d\n", __func__, ret);
Bartlomiej Zolnierkiewicz9601a602007-10-20 00:32:29 +02001052 }
1053}
1054
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001055static void ide_port_init_devices(ide_hwif_t *hwif)
1056{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001057 const struct ide_port_ops *port_ops = hwif->port_ops;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001058 ide_drive_t *drive;
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001059 int i;
1060
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001061 ide_port_for_each_dev(i, drive, hwif) {
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +02001062 drive->dn = i + hwif->channel * 2;
1063
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001064 if (hwif->host_flags & IDE_HFLAG_IO_32BIT)
1065 drive->io_32bit = 1;
Bartlomiej Zolnierkiewicz7610c4f2009-03-24 23:22:38 +01001066 if (hwif->host_flags & IDE_HFLAG_NO_IO_32BIT)
1067 drive->dev_flags |= IDE_DFLAG_NO_IO_32BIT;
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001068 if (hwif->host_flags & IDE_HFLAG_UNMASK_IRQS)
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001069 drive->dev_flags |= IDE_DFLAG_UNMASK;
Bartlomiej Zolnierkiewicz807b90d2008-02-02 19:56:40 +01001070 if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS)
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +02001071 drive->dev_flags |= IDE_DFLAG_NO_UNMASK;
Bartlomiej Zolnierkiewicz1f2cf8b2008-02-02 19:56:40 +01001072
Bartlomiej Zolnierkiewiczd2d4e782010-01-18 07:20:28 +00001073 drive->pio_mode = XFER_PIO_0;
1074
Bartlomiej Zolnierkiewicze6d95bd2008-07-16 20:33:42 +02001075 if (port_ops && port_ops->init_dev)
1076 port_ops->init_dev(drive);
1077 }
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001078}
1079
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001080static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
1081 const struct ide_port_info *d)
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001082{
Adrian Bunkb7691642008-06-10 20:56:36 +02001083 hwif->channel = port;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001084
Bartlomiej Zolnierkiewicz29e52cf2009-05-17 19:12:22 +02001085 hwif->chipset = d->chipset ? d->chipset : ide_pci;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001086
1087 if (d->init_iops)
1088 d->init_iops(hwif);
1089
Bartlomiej Zolnierkiewicz23f8e4b2008-05-01 14:08:51 +02001090 /* ->host_flags may be set by ->init_iops (or even earlier...) */
1091 hwif->host_flags |= d->host_flags;
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001092 hwif->pio_mask = d->pio_mask;
1093
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +02001094 if (d->tp_ops)
1095 hwif->tp_ops = d->tp_ops;
1096
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001097 /* ->set_pio_mode for DTC2278 is currently limited to port 0 */
Bartlomiej Zolnierkiewicz2787cb82009-03-27 12:46:28 +01001098 if ((hwif->host_flags & IDE_HFLAG_DTC2278) == 0 || hwif->channel == 0)
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001099 hwif->port_ops = d->port_ops;
1100
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001101 hwif->swdma_mask = d->swdma_mask;
1102 hwif->mwdma_mask = d->mwdma_mask;
1103 hwif->ultra_mask = d->udma_mask;
1104
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001105 if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
1106 int rc;
1107
Sergei Shtylyov592b5312009-01-06 17:21:02 +01001108 hwif->dma_ops = d->dma_ops;
1109
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001110 if (d->init_dma)
1111 rc = d->init_dma(hwif, d);
1112 else
1113 rc = ide_hwif_setup_dma(hwif, d);
1114
1115 if (rc < 0) {
1116 printk(KERN_INFO "%s: DMA disabled\n", hwif->name);
Sergei Shtylyov592b5312009-01-06 17:21:02 +01001117
1118 hwif->dma_ops = NULL;
Bartlomiej Zolnierkiewiczebb00fb2008-07-23 19:55:51 +02001119 hwif->dma_base = 0;
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001120 hwif->swdma_mask = 0;
1121 hwif->mwdma_mask = 0;
1122 hwif->ultra_mask = 0;
Sergei Shtylyov592b5312009-01-06 17:21:02 +01001123 }
Bartlomiej Zolnierkiewiczb123f562008-04-26 22:25:22 +02001124 }
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001125
Bartlomiej Zolnierkiewicz1024c5f2008-05-04 17:03:41 +02001126 if ((d->host_flags & IDE_HFLAG_SERIALIZE) ||
Bartlomiej Zolnierkiewicz702c0262008-12-29 20:27:36 +01001127 ((d->host_flags & IDE_HFLAG_SERIALIZE_DMA) && hwif->dma_base))
1128 hwif->host->host_flags |= IDE_HFLAG_SERIALIZE;
Bartlomiej Zolnierkiewicz1024c5f2008-05-04 17:03:41 +02001129
Bartlomiej Zolnierkiewicz6b492492008-12-29 20:27:34 +01001130 if (d->max_sectors)
1131 hwif->rqsize = d->max_sectors;
Bartlomiej Zolnierkiewicz70775e92009-03-27 12:46:18 +01001132 else {
1133 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
1134 (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
1135 hwif->rqsize = 256;
1136 else
1137 hwif->rqsize = 65536;
1138 }
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001139
1140 /* call chipset specific routine for each enabled port */
1141 if (d->init_hwif)
1142 d->init_hwif(hwif);
Bartlomiej Zolnierkiewiczc7f6f212008-04-18 00:46:22 +02001143}
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001144
Bartlomiej Zolnierkiewiczc7f6f212008-04-18 00:46:22 +02001145static void ide_port_cable_detect(ide_hwif_t *hwif)
1146{
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001147 const struct ide_port_ops *port_ops = hwif->port_ops;
1148
1149 if (port_ops && port_ops->cable_detect && (hwif->ultra_mask & 0x78)) {
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001150 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +02001151 hwif->cbl = port_ops->cable_detect(hwif);
Bartlomiej Zolnierkiewiczbfa14b42008-02-02 19:56:31 +01001152 }
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001153}
1154
Jens Axboe60033522018-10-26 09:53:52 -06001155/*
1156 * Deferred request list insertion handler
1157 */
1158static void drive_rq_insert_work(struct work_struct *work)
1159{
1160 ide_drive_t *drive = container_of(work, ide_drive_t, rq_work);
1161 ide_hwif_t *hwif = drive->hwif;
1162 struct request *rq;
Jens Axboe9a6d5482019-01-30 08:41:40 -07001163 blk_status_t ret;
Jens Axboe60033522018-10-26 09:53:52 -06001164 LIST_HEAD(list);
1165
Jens Axboe9a6d5482019-01-30 08:41:40 -07001166 blk_mq_quiesce_queue(drive->queue);
1167
1168 ret = BLK_STS_OK;
Jens Axboe60033522018-10-26 09:53:52 -06001169 spin_lock_irq(&hwif->lock);
Jens Axboe9a6d5482019-01-30 08:41:40 -07001170 while (!list_empty(&drive->rq_list)) {
1171 rq = list_first_entry(&drive->rq_list, struct request, queuelist);
1172 list_del_init(&rq->queuelist);
1173
1174 spin_unlock_irq(&hwif->lock);
1175 ret = ide_issue_rq(drive, rq, true);
1176 spin_lock_irq(&hwif->lock);
1177 }
Jens Axboe60033522018-10-26 09:53:52 -06001178 spin_unlock_irq(&hwif->lock);
1179
Jens Axboe9a6d5482019-01-30 08:41:40 -07001180 blk_mq_unquiesce_queue(drive->queue);
1181
1182 if (ret != BLK_STS_OK)
1183 kblockd_schedule_work(&drive->rq_work);
Jens Axboe60033522018-10-26 09:53:52 -06001184}
1185
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001186static const u8 ide_hwif_to_major[] =
1187 { IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR, IDE4_MAJOR,
1188 IDE5_MAJOR, IDE6_MAJOR, IDE7_MAJOR, IDE8_MAJOR, IDE9_MAJOR };
1189
1190static void ide_port_init_devices_data(ide_hwif_t *hwif)
1191{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001192 ide_drive_t *drive;
1193 int i;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001194
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001195 ide_port_for_each_dev(i, drive, hwif) {
1196 u8 j = (hwif->index * MAX_DRIVES) + i;
Hugh Dickins5f4417a2009-06-10 14:37:20 +02001197 u16 *saved_id = drive->id;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001198
1199 memset(drive, 0, sizeof(*drive));
Hugh Dickins5f4417a2009-06-10 14:37:20 +02001200 memset(saved_id, 0, SECTOR_SIZE);
1201 drive->id = saved_id;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001202
1203 drive->media = ide_disk;
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001204 drive->select = (i << 4) | ATA_DEVICE_OBS;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001205 drive->hwif = hwif;
1206 drive->ready_stat = ATA_DRDY;
1207 drive->bad_wstat = BAD_W_STAT;
Bartlomiej Zolnierkiewiczca1b96e2009-05-17 19:12:21 +02001208 drive->special_flags = IDE_SFLAG_RECALIBRATE |
1209 IDE_SFLAG_SET_GEOMETRY;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001210 drive->name[0] = 'h';
1211 drive->name[1] = 'd';
1212 drive->name[2] = 'a' + j;
1213 drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
1214
1215 INIT_LIST_HEAD(&drive->list);
1216 init_completion(&drive->gendev_rel_comp);
Jens Axboe60033522018-10-26 09:53:52 -06001217
1218 INIT_WORK(&drive->rq_work, drive_rq_insert_work);
1219 INIT_LIST_HEAD(&drive->rq_list);
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001220 }
1221}
1222
1223static void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
1224{
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001225 /* fill in any non-zero initial values */
1226 hwif->index = index;
1227 hwif->major = ide_hwif_to_major[index];
1228
1229 hwif->name[0] = 'i';
1230 hwif->name[1] = 'd';
1231 hwif->name[2] = 'e';
1232 hwif->name[3] = '0' + index;
1233
Bartlomiej Zolnierkiewicz73629512009-03-24 23:22:39 +01001234 spin_lock_init(&hwif->lock);
1235
Kees Cook10738ba2017-10-17 21:06:32 -07001236 timer_setup(&hwif->timer, ide_timer_expiry, 0);
Bartlomiej Zolnierkiewicz73629512009-03-24 23:22:39 +01001237
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001238 init_completion(&hwif->gendev_rel_comp);
1239
1240 hwif->tp_ops = &default_tp_ops;
1241
1242 ide_port_init_devices_data(hwif);
1243}
1244
Bartlomiej Zolnierkiewicz9f36d312009-05-17 19:12:25 +02001245static void ide_init_port_hw(ide_hwif_t *hwif, struct ide_hw *hw)
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001246{
1247 memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports));
1248 hwif->irq = hw->irq;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001249 hwif->dev = hw->dev;
1250 hwif->gendev.parent = hw->parent ? hw->parent : hw->dev;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001251 hwif->config_data = hw->config;
1252}
1253
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001254static unsigned int ide_indexes;
1255
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001256/**
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001257 * ide_find_port_slot - find free port slot
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001258 * @d: IDE port info
1259 *
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001260 * Return the new port slot index or -ENOENT if we are out of free slots.
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001261 */
1262
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001263static int ide_find_port_slot(const struct ide_port_info *d)
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001264{
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001265 int idx = -ENOENT;
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001266 u8 bootable = (d && (d->host_flags & IDE_HFLAG_NON_BOOTABLE)) ? 0 : 1;
Joe Perchesa419aef2009-08-18 11:18:35 -07001267 u8 i = (d && (d->host_flags & IDE_HFLAG_QD_2ND_PORT)) ? 1 : 0;
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001268
1269 /*
1270 * Claim an unassigned slot.
1271 *
1272 * Give preference to claiming other slots before claiming ide0/ide1,
1273 * just in case there's another interface yet-to-be-scanned
1274 * which uses ports 0x1f0/0x170 (the ide0/ide1 defaults).
1275 *
1276 * Unless there is a bootable card that does not use the standard
1277 * ports 0x1f0/0x170 (the ide0/ide1 defaults).
1278 */
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001279 mutex_lock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewicz75d21ff2008-10-13 21:39:33 +02001280 if (bootable) {
1281 if ((ide_indexes | i) != (1 << MAX_HWIFS) - 1)
1282 idx = ffz(ide_indexes | i);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001283 } else {
Bartlomiej Zolnierkiewicz75d21ff2008-10-13 21:39:33 +02001284 if ((ide_indexes | 3) != (1 << MAX_HWIFS) - 1)
1285 idx = ffz(ide_indexes | 3);
1286 else if ((ide_indexes & 3) != 3)
1287 idx = ffz(ide_indexes);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001288 }
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001289 if (idx >= 0)
1290 ide_indexes |= (1 << idx);
1291 mutex_unlock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001292
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001293 return idx;
1294}
Bartlomiej Zolnierkiewiczeb3aff52008-07-16 20:33:42 +02001295
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001296static void ide_free_port_slot(int idx)
1297{
1298 mutex_lock(&ide_cfg_mtx);
1299 ide_indexes &= ~(1 << idx);
1300 mutex_unlock(&ide_cfg_mtx);
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001301}
Bartlomiej Zolnierkiewiczfe80b932008-04-26 17:36:36 +02001302
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001303static void ide_port_free_devices(ide_hwif_t *hwif)
1304{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001305 ide_drive_t *drive;
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001306 int i;
1307
Hugh Dickins5f4417a2009-06-10 14:37:20 +02001308 ide_port_for_each_dev(i, drive, hwif) {
1309 kfree(drive->id);
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001310 kfree(drive);
Hugh Dickins5f4417a2009-06-10 14:37:20 +02001311 }
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001312}
1313
1314static int ide_port_alloc_devices(ide_hwif_t *hwif, int node)
1315{
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001316 ide_drive_t *drive;
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001317 int i;
1318
1319 for (i = 0; i < MAX_DRIVES; i++) {
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001320 drive = kzalloc_node(sizeof(*drive), GFP_KERNEL, node);
1321 if (drive == NULL)
1322 goto out_nomem;
1323
Hugh Dickins5f4417a2009-06-10 14:37:20 +02001324 /*
1325 * In order to keep things simple we have an id
1326 * block for all drives at all times. If the device
1327 * is pre ATA or refuses ATA/ATAPI identify we
1328 * will add faked data to this.
1329 *
1330 * Also note that 0 everywhere means "can't do X"
1331 */
1332 drive->id = kzalloc_node(SECTOR_SIZE, GFP_KERNEL, node);
1333 if (drive->id == NULL)
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001334 goto out_free_drive;
1335
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001336 hwif->devices[i] = drive;
1337 }
1338 return 0;
1339
Christoph Hellwig82ed4db2017-01-27 09:46:29 +01001340out_free_drive:
1341 kfree(drive);
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001342out_nomem:
1343 ide_port_free_devices(hwif);
1344 return -ENOMEM;
1345}
1346
Bartlomiej Zolnierkiewicz9f36d312009-05-17 19:12:25 +02001347struct ide_host *ide_host_alloc(const struct ide_port_info *d,
1348 struct ide_hw **hws, unsigned int n_ports)
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001349{
1350 struct ide_host *host;
Bartlomiej Zolnierkiewicza32296f2009-01-06 17:20:56 +01001351 struct device *dev = hws[0] ? hws[0]->dev : NULL;
1352 int node = dev ? dev_to_node(dev) : -1;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001353 int i;
1354
Bartlomiej Zolnierkiewicza32296f2009-01-06 17:20:56 +01001355 host = kzalloc_node(sizeof(*host), GFP_KERNEL, node);
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001356 if (host == NULL)
1357 return NULL;
1358
Bartlomiej Zolnierkiewiczdca39832009-05-17 19:12:24 +02001359 for (i = 0; i < n_ports; i++) {
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001360 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001361 int idx;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001362
1363 if (hws[i] == NULL)
1364 continue;
1365
Bartlomiej Zolnierkiewicza32296f2009-01-06 17:20:56 +01001366 hwif = kzalloc_node(sizeof(*hwif), GFP_KERNEL, node);
Bartlomiej Zolnierkiewicz18de10172008-07-23 19:55:58 +02001367 if (hwif == NULL)
1368 continue;
1369
Bartlomiej Zolnierkiewicz5e7f3a42009-01-06 17:20:56 +01001370 if (ide_port_alloc_devices(hwif, node) < 0) {
1371 kfree(hwif);
1372 continue;
1373 }
1374
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001375 idx = ide_find_port_slot(d);
1376 if (idx < 0) {
1377 printk(KERN_ERR "%s: no free slot for interface\n",
1378 d ? d->name : "ide");
Hugh Dickins5f4417a2009-06-10 14:37:20 +02001379 ide_port_free_devices(hwif);
Bartlomiej Zolnierkiewicz18de10172008-07-23 19:55:58 +02001380 kfree(hwif);
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001381 continue;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001382 }
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001383
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001384 ide_init_port_data(hwif, idx);
1385
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +02001386 hwif->host = host;
1387
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001388 host->ports[i] = hwif;
1389 host->n_ports++;
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001390 }
1391
1392 if (host->n_ports == 0) {
1393 kfree(host);
1394 return NULL;
1395 }
1396
Bartlomiej Zolnierkiewicza32296f2009-01-06 17:20:56 +01001397 host->dev[0] = dev;
Bartlomiej Zolnierkiewicz6cdf6eb2008-07-24 22:53:14 +02001398
Bartlomiej Zolnierkiewiczfeb22b72008-10-10 22:39:32 +02001399 if (d) {
1400 host->init_chipset = d->init_chipset;
Bartlomiej Zolnierkiewicze354c1d2009-03-27 12:46:22 +01001401 host->get_lock = d->get_lock;
1402 host->release_lock = d->release_lock;
Bartlomiej Zolnierkiewiczef0b0422008-07-24 22:53:19 +02001403 host->host_flags = d->host_flags;
Geert Uytterhoevenaa075732009-04-08 14:12:47 +02001404 host->irq_flags = d->irq_flags;
Bartlomiej Zolnierkiewiczfeb22b72008-10-10 22:39:32 +02001405 }
Bartlomiej Zolnierkiewiczef0b0422008-07-24 22:53:19 +02001406
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001407 return host;
1408}
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001409EXPORT_SYMBOL_GPL(ide_host_alloc);
1410
Bartlomiej Zolnierkiewicz9a100f42009-02-02 20:12:21 +01001411static void ide_port_free(ide_hwif_t *hwif)
1412{
1413 ide_port_free_devices(hwif);
1414 ide_free_port_slot(hwif->index);
1415 kfree(hwif);
1416}
1417
1418static void ide_disable_port(ide_hwif_t *hwif)
1419{
1420 struct ide_host *host = hwif->host;
1421 int i;
1422
1423 printk(KERN_INFO "%s: disabling port\n", hwif->name);
1424
1425 for (i = 0; i < MAX_HOST_PORTS; i++) {
1426 if (host->ports[i] == hwif) {
1427 host->ports[i] = NULL;
1428 host->n_ports--;
1429 }
1430 }
1431
1432 ide_port_free(hwif);
1433}
1434
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001435int ide_host_register(struct ide_host *host, const struct ide_port_info *d,
Bartlomiej Zolnierkiewicz9f36d312009-05-17 19:12:25 +02001436 struct ide_hw **hws)
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001437{
1438 ide_hwif_t *hwif, *mate = NULL;
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001439 int i, j = 0;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001440
Christoph Hellwig7ad19a92019-05-08 20:01:40 +02001441 pr_warn("legacy IDE will be removed in 2021, please switch to libata\n"
1442 "Report any missing HW support to linux-ide@vger.kernel.org\n");
1443
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001444 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001445 if (hwif == NULL) {
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001446 mate = NULL;
1447 continue;
1448 }
1449
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +02001450 ide_init_port_hw(hwif, hws[i]);
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +02001451 ide_port_apply_params(hwif);
1452
Bartlomiej Zolnierkiewicz0a6e49e2009-03-27 12:46:27 +01001453 if ((i & 1) && mate) {
1454 hwif->mate = mate;
1455 mate->mate = hwif;
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +02001456 }
1457
Bartlomiej Zolnierkiewicz0a6e49e2009-03-27 12:46:27 +01001458 mate = (i & 1) ? NULL : hwif;
1459
1460 ide_init_port(hwif, i & 1, d);
1461 ide_port_cable_detect(hwif);
Bartlomiej Zolnierkiewicz5880b5d2009-06-15 18:52:54 +02001462
1463 hwif->port_flags |= IDE_PFLAG_PROBING;
1464
Bartlomiej Zolnierkiewicz7704ca22008-02-02 19:56:39 +01001465 ide_port_init_devices(hwif);
Bartlomiej Zolnierkiewiczc413b9b2008-02-02 19:56:31 +01001466 }
1467
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001468 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001469 if (hwif == NULL)
1470 continue;
Bartlomiej Zolnierkiewicz139ddfc2008-02-01 23:09:36 +01001471
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001472 if (ide_probe_port(hwif) == 0)
1473 hwif->present = 1;
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +01001474
Bartlomiej Zolnierkiewicz5880b5d2009-06-15 18:52:54 +02001475 hwif->port_flags &= ~IDE_PFLAG_PROBING;
1476
Bartlomiej Zolnierkiewiczc094ea02009-03-27 12:46:28 +01001477 if ((hwif->host_flags & IDE_HFLAG_4DRIVES) == 0 ||
1478 hwif->mate == NULL || hwif->mate->present == 0) {
Bartlomiej Zolnierkiewicz9a100f42009-02-02 20:12:21 +01001479 if (ide_register_port(hwif)) {
1480 ide_disable_port(hwif);
1481 continue;
1482 }
1483 }
Bartlomiej Zolnierkiewicza14dc572008-02-01 23:09:36 +01001484
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001485 if (hwif->present)
1486 ide_port_tune_devices(hwif);
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001487 }
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001488
Bartlomiej Zolnierkiewiczffc36c72009-06-23 03:43:00 -07001489 ide_host_enable_irqs(host);
1490
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001491 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001492 if (hwif == NULL)
1493 continue;
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001494
1495 if (hwif_init(hwif) == 0) {
1496 printk(KERN_INFO "%s: failed to initialize IDE "
1497 "interface\n", hwif->name);
Bartlomiej Zolnierkiewiczd410a642017-10-03 13:18:47 +02001498 device_unregister(hwif->portdev);
Ian Campbell51d6ac72009-02-02 20:12:22 +01001499 device_unregister(&hwif->gendev);
1500 ide_disable_port(hwif);
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001501 continue;
1502 }
Bartlomiej Zolnierkiewiczdecdc3f2008-02-02 19:56:41 +01001503
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001504 if (hwif->present)
Elias Oltmannse415e492008-10-13 21:39:45 +02001505 if (ide_port_setup_devices(hwif) == 0) {
1506 hwif->present = 0;
1507 continue;
1508 }
1509
1510 j++;
Bartlomiej Zolnierkiewicz26042d02008-04-18 00:46:22 +02001511
Bartlomiej Zolnierkiewicz8b803bd2009-03-24 23:22:41 +01001512 ide_acpi_init_port(hwif);
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001513
1514 if (hwif->present)
1515 ide_acpi_port_init_devices(hwif);
Bartlomiej Zolnierkiewicz2e130932008-01-26 20:13:04 +01001516 }
1517
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001518 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001519 if (hwif == NULL)
1520 continue;
Bartlomiej Zolnierkiewiczba6560a2008-01-26 20:13:04 +01001521
Bartlomiej Zolnierkiewiczeb716be2008-04-26 22:25:17 +02001522 ide_sysfs_register_port(hwif);
1523 ide_proc_register_port(hwif);
1524
Wolfram Sangdbee0322010-09-02 11:28:39 +00001525 if (hwif->present) {
Bartlomiej Zolnierkiewiczd9270a32008-02-02 19:56:43 +01001526 ide_proc_port_register_devices(hwif);
Wolfram Sangdbee0322010-09-02 11:28:39 +00001527 hwif_register_devices(hwif);
1528 }
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001529 }
1530
Bartlomiej Zolnierkiewicze0d00202008-07-23 19:55:57 +02001531 return j ? 0 : -1;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001532}
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001533EXPORT_SYMBOL_GPL(ide_host_register);
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +02001534
Bartlomiej Zolnierkiewicz9f36d312009-05-17 19:12:25 +02001535int ide_host_add(const struct ide_port_info *d, struct ide_hw **hws,
Bartlomiej Zolnierkiewiczdca39832009-05-17 19:12:24 +02001536 unsigned int n_ports, struct ide_host **hostp)
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001537{
1538 struct ide_host *host;
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001539 int rc;
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001540
Bartlomiej Zolnierkiewiczdca39832009-05-17 19:12:24 +02001541 host = ide_host_alloc(d, hws, n_ports);
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001542 if (host == NULL)
1543 return -ENOMEM;
1544
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001545 rc = ide_host_register(host, d, hws);
1546 if (rc) {
1547 ide_host_free(host);
1548 return rc;
1549 }
Bartlomiej Zolnierkiewicz6f904d02008-07-23 19:55:57 +02001550
1551 if (hostp)
1552 *hostp = host;
1553
1554 return 0;
1555}
1556EXPORT_SYMBOL_GPL(ide_host_add);
1557
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001558static void __ide_port_unregister_devices(ide_hwif_t *hwif)
1559{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001560 ide_drive_t *drive;
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001561 int i;
1562
Bartlomiej Zolnierkiewicz7ed5b152009-03-24 23:22:41 +01001563 ide_port_for_each_present_dev(i, drive, hwif) {
1564 device_unregister(&drive->gendev);
1565 wait_for_completion(&drive->gendev_rel_comp);
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001566 }
1567}
1568
1569void ide_port_unregister_devices(ide_hwif_t *hwif)
1570{
1571 mutex_lock(&ide_cfg_mtx);
1572 __ide_port_unregister_devices(hwif);
1573 hwif->present = 0;
1574 ide_port_init_devices_data(hwif);
1575 mutex_unlock(&ide_cfg_mtx);
1576}
1577EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
1578
1579/**
1580 * ide_unregister - free an IDE interface
1581 * @hwif: IDE interface
1582 *
1583 * Perform the final unregister of an IDE interface.
1584 *
1585 * Locking:
1586 * The caller must not hold the IDE locks.
1587 *
1588 * It is up to the caller to be sure there is no pending I/O here,
1589 * and that the interface will not be reopened (present/vanishing
1590 * locking isn't yet done BTW).
1591 */
1592
1593static void ide_unregister(ide_hwif_t *hwif)
1594{
1595 BUG_ON(in_interrupt());
1596 BUG_ON(irqs_disabled());
1597
1598 mutex_lock(&ide_cfg_mtx);
1599
1600 if (hwif->present) {
1601 __ide_port_unregister_devices(hwif);
1602 hwif->present = 0;
1603 }
1604
1605 ide_proc_unregister_port(hwif);
1606
Michael Schmitza259d532014-02-01 13:48:13 +13001607 if (!hwif->host->get_lock)
1608 free_irq(hwif->irq, hwif);
Bartlomiej Zolnierkiewiczb40d1b82009-01-06 17:20:51 +01001609
1610 device_unregister(hwif->portdev);
1611 device_unregister(&hwif->gendev);
1612 wait_for_completion(&hwif->gendev_rel_comp);
1613
1614 /*
1615 * Remove us from the kernel's knowledge
1616 */
1617 blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
1618 kfree(hwif->sg_table);
1619 unregister_blkdev(hwif->major, hwif->name);
1620
1621 ide_release_dma_engine(hwif);
1622
1623 mutex_unlock(&ide_cfg_mtx);
1624}
1625
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001626void ide_host_free(struct ide_host *host)
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001627{
Bartlomiej Zolnierkiewicz8cdf3102008-07-23 19:55:57 +02001628 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001629 int i;
1630
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001631 ide_host_for_each_port(i, hwif, host) {
Bartlomiej Zolnierkiewicz9a100f42009-02-02 20:12:21 +01001632 if (hwif)
1633 ide_port_free(hwif);
Bartlomiej Zolnierkiewiczc97c6ac2008-07-23 19:55:50 +02001634 }
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001635
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001636 kfree(host);
Bartlomiej Zolnierkiewicz151575e2008-01-26 20:13:05 +01001637}
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001638EXPORT_SYMBOL_GPL(ide_host_free);
1639
1640void ide_host_remove(struct ide_host *host)
1641{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001642 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001643 int i;
1644
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +01001645 ide_host_for_each_port(i, hwif, host) {
1646 if (hwif)
1647 ide_unregister(hwif);
Bartlomiej Zolnierkiewicz8a695802008-07-23 19:55:59 +02001648 }
1649
1650 ide_host_free(host);
1651}
Bartlomiej Zolnierkiewicz48c3c102008-07-23 19:55:57 +02001652EXPORT_SYMBOL_GPL(ide_host_remove);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001653
1654void ide_port_scan(ide_hwif_t *hwif)
1655{
Bartlomiej Zolnierkiewicz5880b5d2009-06-15 18:52:54 +02001656 int rc;
1657
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +02001658 ide_port_apply_params(hwif);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001659 ide_port_cable_detect(hwif);
Bartlomiej Zolnierkiewicz5880b5d2009-06-15 18:52:54 +02001660
1661 hwif->port_flags |= IDE_PFLAG_PROBING;
1662
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001663 ide_port_init_devices(hwif);
1664
Bartlomiej Zolnierkiewicz5880b5d2009-06-15 18:52:54 +02001665 rc = ide_probe_port(hwif);
1666
1667 hwif->port_flags &= ~IDE_PFLAG_PROBING;
1668
1669 if (rc < 0)
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001670 return;
1671
1672 hwif->present = 1;
1673
1674 ide_port_tune_devices(hwif);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001675 ide_port_setup_devices(hwif);
Bartlomiej Zolnierkiewicze630fcb2009-01-06 17:21:00 +01001676 ide_acpi_port_init_devices(hwif);
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +02001677 hwif_register_devices(hwif);
1678 ide_proc_port_register_devices(hwif);
1679}
1680EXPORT_SYMBOL_GPL(ide_port_scan);