treewide: Use fallthrough pseudo-keyword
[linux-block.git] / drivers / ide / ide-probe.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4 2/*
59bca8cc
BZ
3 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
4 * Copyright (C) 2005, 2007 Bartlomiej Zolnierkiewicz
1da177e4
LT
5 */
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 *
bbe4d6d8
BZ
16 * -- increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
17 * by Andrea Arcangeli
1da177e4
LT
18 */
19
1da177e4
LT
20#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>
dc81785d 36#include <linux/scatterlist.h>
1da177e4
LT
37
38#include <asm/byteorder.h>
39#include <asm/irq.h>
7c0f6ba6 40#include <linux/uaccess.h>
1da177e4
LT
41#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{
4dde4492
BZ
54 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;
1da177e4
LT
59}
60
61static void ide_disk_init_chs(ide_drive_t *drive)
62{
4dde4492 63 u16 *id = drive->id;
1da177e4
LT
64
65 /* Extract geometry if we did not already have one for the drive */
66 if (!drive->cyl || !drive->head || !drive->sect) {
4dde4492
BZ
67 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];
1da177e4
LT
70 }
71
72 /* Handle logical geometry translation by the drive */
dd8f46f6 73 if (ata_id_current_chs_valid(id)) {
4dde4492
BZ
74 drive->cyl = id[ATA_ID_CUR_CYLS];
75 drive->head = id[ATA_ID_CUR_HEADS];
76 drive->sect = id[ATA_ID_CUR_SECTORS];
1da177e4
LT
77 }
78
79 /* Use physical geometry if what we have still makes no sense */
4dde4492
BZ
80 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];
1da177e4
LT
84 }
85}
86
87static void ide_disk_init_mult_count(ide_drive_t *drive)
88{
48fb2688
BZ
89 u16 *id = drive->id;
90 u8 max_multsect = id[ATA_ID_MAX_MULTSECT] & 0xff;
1da177e4 91
48fb2688 92 if (max_multsect) {
48fb2688
BZ
93 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;
7c51c17e
BZ
99
100 if (drive->mult_req)
ca1b96e0 101 drive->special_flags |= IDE_SFLAG_SET_MULTMODE;
1da177e4
LT
102 }
103}
104
24630dc6
BZ
105static 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;
df561f66 146 fallthrough;
24630dc6
BZ
147 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
1da177e4
LT
182/**
183 * do_identify - identify a drive
184 * @drive: drive to identify
185 * @cmd: command used
2ebe1d9e 186 * @id: buffer for IDENTIFY data
1da177e4
LT
187 *
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 */
047140ae 192
2ebe1d9e 193static void do_identify(ide_drive_t *drive, u8 cmd, u16 *id)
1da177e4 194{
898ec223 195 ide_hwif_t *hwif = drive->hwif;
4dde4492 196 char *m = (char *)&id[ATA_ID_PROD];
94b9efdf 197 unsigned long flags;
24630dc6 198 int bswap = 1;
1da177e4 199
94b9efdf
BZ
200 /* local CPU only; some systems need this */
201 local_irq_save(flags);
1da177e4 202 /* read 512 bytes of id info */
374e042c 203 hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
94b9efdf 204 local_irq_restore(flags);
1da177e4 205
97100fc8 206 drive->dev_flags |= IDE_DFLAG_ID_READ;
7b9f25b5
BZ
207#ifdef DEBUG
208 printk(KERN_INFO "%s: dumping identify data\n", drive->name);
209 ide_dump_identify((u8 *)id);
210#endif
1da177e4
LT
211 ide_fix_driveid(id);
212
1da177e4 213 /*
aaaade3f
BZ
214 * ATA_CMD_ID_ATA returns little-endian info,
215 * ATA_CMD_ID_ATAPI *usually* returns little-endian info.
1da177e4 216 */
aaaade3f 217 if (cmd == ATA_CMD_ID_ATAPI) {
4dde4492
BZ
218 if ((m[0] == 'N' && m[1] == 'E') || /* NEC */
219 (m[0] == 'F' && m[1] == 'X') || /* Mitsumi */
220 (m[0] == 'P' && m[1] == 'i')) /* Pioneer */
1da177e4 221 /* Vertos drives may still be weird */
4dde4492 222 bswap ^= 1;
1da177e4 223 }
4dde4492
BZ
224
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);
1da177e4 228
699b052a 229 /* we depend on this a lot! */
4dde4492 230 m[ATA_ID_PROD_LEN - 1] = '\0';
699b052a 231
4dde4492 232 if (strstr(m, "E X A B Y T E N E S T"))
69197ad7
BZ
233 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
234 else
235 drive->dev_flags |= IDE_DFLAG_PRESENT;
1da177e4
LT
236}
237
238/**
2ebe1d9e 239 * ide_dev_read_id - send ATA/ATAPI IDENTIFY command
1da177e4
LT
240 * @drive: drive to identify
241 * @cmd: command to use
2ebe1d9e 242 * @id: buffer for IDENTIFY data
fa56d4cb 243 * @irq_ctx: flag set when called from the IRQ context
1da177e4 244 *
2ebe1d9e 245 * Sends an ATA(PI) IDENTIFY request to a drive and waits for a response.
1da177e4
LT
246 *
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
fa56d4cb 252int ide_dev_read_id(ide_drive_t *drive, u8 cmd, u16 *id, int irq_ctx)
1da177e4 253{
898ec223 254 ide_hwif_t *hwif = drive->hwif;
4c3032d8 255 struct ide_io_ports *io_ports = &hwif->io_ports;
374e042c 256 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
c47137a9 257 int use_altstatus = 0, rc;
1da177e4
LT
258 unsigned long timeout;
259 u8 s = 0, a = 0;
260
a182807a
BZ
261 /*
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)
ecf3a31d 266 tp_ops->write_devctl(hwif, ATA_NIEN | ATA_DEVCTL_OBS);
a182807a 267
1da177e4 268 /* take a deep breath */
fa56d4cb
BZ
269 if (irq_ctx)
270 mdelay(50);
271 else
272 msleep(50);
1da177e4 273
6636487e
BZ
274 if (io_ports->ctl_addr &&
275 (hwif->host_flags & IDE_HFLAG_BROKEN_ALTSTATUS) == 0) {
374e042c
BZ
276 a = tp_ops->read_altstatus(hwif);
277 s = tp_ops->read_status(hwif);
27f00e53 278 if ((a ^ s) & ~ATA_SENSE)
1da177e4 279 /* ancient Seagate drives, broken interfaces */
c47137a9
BZ
280 printk(KERN_INFO "%s: probing with STATUS(0x%02x) "
281 "instead of ALTSTATUS(0x%02x)\n",
282 drive->name, s, a);
283 else
1da177e4 284 /* use non-intrusive polling */
c47137a9
BZ
285 use_altstatus = 1;
286 }
1da177e4
LT
287
288 /* set features register for atapi
289 * identify command to be sure of reply
290 */
aaaade3f 291 if (cmd == ATA_CMD_ID_ATAPI) {
c9ff9e7b 292 struct ide_taskfile tf;
4e65837b 293
c9ff9e7b 294 memset(&tf, 0, sizeof(tf));
4e65837b 295 /* disable DMA & overlap */
c9ff9e7b 296 tp_ops->tf_load(drive, &tf, IDE_VALID_FEATURE);
4e65837b 297 }
1da177e4
LT
298
299 /* ask drive for ID */
374e042c 300 tp_ops->exec_command(hwif, cmd);
1da177e4 301
aaaade3f 302 timeout = ((cmd == ATA_CMD_ID_ATA) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
b163f46d 303
3a7d2484 304 /* wait for IRQ and ATA_DRQ */
fa56d4cb
BZ
305 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 }
c47137a9 317
3a7d2484 318 if (OK_STAT(s, ATA_DRQ, BAD_R_STAT)) {
1da177e4 319 /* drive returned ID */
2ebe1d9e 320 do_identify(drive, cmd, id);
1da177e4
LT
321 /* drive responded with ID */
322 rc = 0;
323 /* clear drive IRQ */
374e042c 324 (void)tp_ops->read_status(hwif);
1da177e4
LT
325 } else {
326 /* drive refused ID */
327 rc = 2;
328 }
329 return rc;
330}
331
28ee9bc5 332int ide_busy_sleep(ide_drive_t *drive, unsigned long timeout, int altstatus)
3a5015cc 333{
28ee9bc5 334 ide_hwif_t *hwif = drive->hwif;
3a5015cc
BZ
335 u8 stat;
336
b163f46d
BZ
337 timeout += jiffies;
338
3a5015cc 339 do {
b163f46d
BZ
340 msleep(50); /* give drive a breather */
341 stat = altstatus ? hwif->tp_ops->read_altstatus(hwif)
342 : hwif->tp_ops->read_status(hwif);
3a7d2484 343 if ((stat & ATA_BUSY) == 0)
3a5015cc
BZ
344 return 0;
345 } while (time_before(jiffies, timeout));
346
28ee9bc5
BZ
347 printk(KERN_ERR "%s: timeout in %s\n", drive->name, __func__);
348
b163f46d 349 return 1; /* drive timed-out */
3a5015cc 350}
1da177e4 351
1f2efb82
BZ
352static u8 ide_read_device(ide_drive_t *drive)
353{
3153c26b 354 struct ide_taskfile tf;
1f2efb82 355
3153c26b 356 drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_DEVICE);
1f2efb82 357
3153c26b 358 return tf.device;
1f2efb82
BZ
359}
360
1da177e4
LT
361/**
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{
898ec223 384 ide_hwif_t *hwif = drive->hwif;
374e042c 385 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
2ebe1d9e 386 u16 *id = drive->id;
57b55275 387 int rc;
97100fc8
BZ
388 u8 present = !!(drive->dev_flags & IDE_DFLAG_PRESENT), stat;
389
390 /* avoid waiting for inappropriate probes */
391 if (present && drive->media != ide_disk && cmd == ATA_CMD_ID_ATA)
392 return 4;
1da177e4 393
1da177e4 394#ifdef DEBUG
1b8ebad8 395 printk(KERN_INFO "probing for %s: present=%d, media=%d, probetype=%s\n",
97100fc8 396 drive->name, present, drive->media,
aaaade3f 397 (cmd == ATA_CMD_ID_ATA) ? "ATA" : "ATAPI");
1da177e4
LT
398#endif
399
400 /* needed for some systems
401 * (e.g. crw9624 as drive0 with disk as slave)
402 */
403 msleep(50);
fdd88f0a 404 tp_ops->dev_select(drive);
1da177e4 405 msleep(50);
1f2efb82 406
7f612f27 407 if (ide_read_device(drive) != drive->select && present == 0) {
123995b9 408 if (drive->dn & 1) {
1da177e4 409 /* exit with drive0 selected */
fdd88f0a 410 tp_ops->dev_select(hwif->devices[0]);
3a7d2484 411 /* allow ATA_BUSY to assert & clear */
1da177e4
LT
412 msleep(50);
413 }
414 /* no i/f present: mmm.. this should be a 4 -ml */
415 return 3;
416 }
417
374e042c 418 stat = tp_ops->read_status(hwif);
c47137a9 419
3a7d2484 420 if (OK_STAT(stat, ATA_DRDY, ATA_BUSY) ||
97100fc8 421 present || cmd == ATA_CMD_ID_ATAPI) {
fa56d4cb 422 rc = ide_dev_read_id(drive, cmd, id, 0);
2ebe1d9e 423 if (rc)
1da177e4 424 /* failed: try again */
fa56d4cb 425 rc = ide_dev_read_id(drive, cmd, id, 0);
57b55275 426
374e042c 427 stat = tp_ops->read_status(hwif);
57b55275 428
3a7d2484 429 if (stat == (ATA_BUSY | ATA_DRDY))
1da177e4
LT
430 return 4;
431
aaaade3f 432 if (rc == 1 && cmd == ATA_CMD_ID_ATAPI) {
57b55275
BZ
433 printk(KERN_ERR "%s: no response (status = 0x%02x), "
434 "resetting drive\n", drive->name, stat);
1da177e4 435 msleep(50);
fdd88f0a 436 tp_ops->dev_select(drive);
1da177e4 437 msleep(50);
aaaade3f 438 tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
28ee9bc5 439 (void)ide_busy_sleep(drive, WAIT_WORSTCASE, 0);
fa56d4cb 440 rc = ide_dev_read_id(drive, cmd, id, 0);
1da177e4 441 }
57b55275
BZ
442
443 /* ensure drive IRQ is clear */
374e042c 444 stat = tp_ops->read_status(hwif);
57b55275 445
1da177e4 446 if (rc == 1)
57b55275
BZ
447 printk(KERN_ERR "%s: no response (status = 0x%02x)\n",
448 drive->name, stat);
1da177e4
LT
449 } else {
450 /* not present or maybe ATAPI */
451 rc = 3;
452 }
123995b9 453 if (drive->dn & 1) {
1da177e4 454 /* exit with drive0 selected */
fdd88f0a 455 tp_ops->dev_select(hwif->devices[0]);
1da177e4
LT
456 msleep(50);
457 /* ensure drive irq is clear */
374e042c 458 (void)tp_ops->read_status(hwif);
1da177e4
LT
459 }
460 return rc;
461}
462
1da177e4
LT
463/**
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
97100fc8
BZ
471 * 1 device was found
472 * (note: IDE_DFLAG_PRESENT might still be not set)
1da177e4 473 */
047140ae
BZ
474
475static u8 probe_for_drive(ide_drive_t *drive)
1da177e4 476{
4dde4492 477 char *m;
1bd4c1f4
BZ
478 int rc;
479 u8 cmd;
4dde4492 480
97100fc8
BZ
481 drive->dev_flags &= ~IDE_DFLAG_ID_READ;
482
4dde4492
BZ
483 m = (char *)&drive->id[ATA_ID_PROD];
484 strcpy(m, "UNKNOWN");
485
1da177e4 486 /* skip probing? */
97100fc8 487 if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0) {
1da177e4 488 /* if !(success||timed-out) */
1bd4c1f4
BZ
489 cmd = ATA_CMD_ID_ATA;
490 rc = do_probe(drive, cmd);
491 if (rc >= 2) {
1da177e4 492 /* look for ATAPI device */
1bd4c1f4
BZ
493 cmd = ATA_CMD_ID_ATAPI;
494 rc = do_probe(drive, cmd);
495 }
c36a7e98 496
97100fc8 497 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
5f4417a1 498 return 0;
4dde4492 499
1da177e4 500 /* identification failed? */
97100fc8 501 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
1da177e4
LT
502 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);
97100fc8 511 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
1da177e4 512 }
1bd4c1f4
BZ
513 } else {
514 if (cmd == ATA_CMD_ID_ATAPI)
515 ide_classify_atapi_dev(drive);
516 else
517 ide_classify_ata_dev(drive);
1da177e4 518 }
1da177e4 519 }
97100fc8
BZ
520
521 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
5f4417a1 522 return 0;
97100fc8 523
1da177e4 524 /* The drive wasn't being helpful. Add generic info only */
97100fc8 525 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
1da177e4
LT
526 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
69197ad7 535 return 1;
1da177e4
LT
536}
537
fc410698 538static void hwif_release_dev(struct device *dev)
1da177e4
LT
539{
540 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
541
f36d4024 542 complete(&hwif->gendev_rel_comp);
1da177e4
LT
543}
544
f74c9141 545static int ide_register_port(ide_hwif_t *hwif)
1da177e4 546{
349ae23f
RD
547 int ret;
548
1da177e4 549 /* register with global device tree */
02aa2a37 550 dev_set_name(&hwif->gendev, "%s", hwif->name);
fcb52077 551 dev_set_drvdata(&hwif->gendev, hwif);
bb54affa
AS
552 if (hwif->gendev.parent == NULL)
553 hwif->gendev.parent = hwif->dev;
1da177e4 554 hwif->gendev.release = hwif_release_dev;
96d40941 555
349ae23f 556 ret = device_register(&hwif->gendev);
f74c9141 557 if (ret < 0) {
349ae23f 558 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
eb63963a 559 __func__, ret);
f74c9141
BZ
560 goto out;
561 }
562
3ee074bf 563 hwif->portdev = device_create(ide_port_class, &hwif->gendev,
02aa2a37 564 MKDEV(0, 0), hwif, "%s", hwif->name);
f74c9141
BZ
565 if (IS_ERR(hwif->portdev)) {
566 ret = PTR_ERR(hwif->portdev);
567 device_unregister(&hwif->gendev);
568 }
f74c9141
BZ
569out:
570 return ret;
1da177e4
LT
571}
572
c860a8f2
BZ
573/**
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)
1da177e4 600{
fdd88f0a 601 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
2bd24a1c
BZ
602 ide_drive_t *drive;
603 int i, rc;
1da177e4
LT
604
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 */
2bd24a1c 620 ide_port_for_each_dev(i, drive, hwif) {
8266105b 621 /* Ignore disks that we will not probe for later. */
97100fc8
BZ
622 if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0 ||
623 (drive->dev_flags & IDE_DFLAG_PRESENT)) {
fdd88f0a
SS
624 tp_ops->dev_select(drive);
625 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
8266105b
JS
626 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:
1da177e4 635 /* Exit function with master reselected (let's be sane) */
2bd24a1c 636 if (i)
fdd88f0a 637 tp_ops->dev_select(hwif->devices[0]);
8266105b 638
1da177e4
LT
639 return rc;
640}
641
642/**
643 * ide_undecoded_slave - look for bad CF adapters
4dde4492 644 * @dev1: slave device
1da177e4
LT
645 *
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
4dde4492 651void ide_undecoded_slave(ide_drive_t *dev1)
1da177e4 652{
5e7f3a46 653 ide_drive_t *dev0 = dev1->hwif->devices[0];
1da177e4 654
97100fc8 655 if ((dev1->dn & 1) == 0 || (dev0->dev_flags & IDE_DFLAG_PRESENT) == 0)
1da177e4
LT
656 return;
657
658 /* If the models don't match they are not the same product */
4dde4492
BZ
659 if (strcmp((char *)&dev0->id[ATA_ID_PROD],
660 (char *)&dev1->id[ATA_ID_PROD]))
1da177e4
LT
661 return;
662
663 /* Serial numbers do not match */
4dde4492
BZ
664 if (strncmp((char *)&dev0->id[ATA_ID_SERNO],
665 (char *)&dev1->id[ATA_ID_SERNO], ATA_ID_SERNO_LEN))
1da177e4
LT
666 return;
667
668 /* No serial number, thankfully very rare for CF */
4dde4492 669 if (*(char *)&dev0->id[ATA_ID_SERNO] == 0)
1da177e4
LT
670 return;
671
672 /* Appears to be an IDE flash adapter with decode bugs */
673 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
674
97100fc8 675 dev1->dev_flags &= ~IDE_DFLAG_PRESENT;
1da177e4
LT
676}
677
678EXPORT_SYMBOL_GPL(ide_undecoded_slave);
679
9d501529 680static int ide_probe_port(ide_hwif_t *hwif)
1da177e4 681{
2bd24a1c 682 ide_drive_t *drive;
1da177e4 683 unsigned int irqd;
2bd24a1c 684 int i, rc = -ENODEV;
a14dc574
BZ
685
686 BUG_ON(hwif->present);
1da177e4 687
5e7f3a46
BZ
688 if ((hwif->devices[0]->dev_flags & IDE_DFLAG_NOPROBE) &&
689 (hwif->devices[1]->dev_flags & IDE_DFLAG_NOPROBE))
9d501529 690 return -EACCES;
1da177e4 691
1da177e4
LT
692 /*
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
9ce41aed
DM
700 if (ide_port_wait_ready(hwif) == -EBUSY)
701 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
1da177e4
LT
702
703 /*
f367bed0
BZ
704 * Second drive should only exist if first drive was found,
705 * but a lot of cdrom drives are configured as single slaves.
1da177e4 706 */
2bd24a1c 707 ide_port_for_each_dev(i, drive, hwif) {
1da177e4 708 (void) probe_for_drive(drive);
97100fc8 709 if (drive->dev_flags & IDE_DFLAG_PRESENT)
a14dc574 710 rc = 0;
1da177e4 711 }
9ce41aed 712
1da177e4
LT
713 /*
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
a14dc574 720 return rc;
e84e7ea7
BZ
721}
722
723static void ide_port_tune_devices(ide_hwif_t *hwif)
724{
ac95beed 725 const struct ide_port_ops *port_ops = hwif->port_ops;
2bd24a1c
BZ
726 ide_drive_t *drive;
727 int i;
f01393e4 728
7ed5b157 729 ide_port_for_each_present_dev(i, drive, hwif) {
8bc1e5aa
BZ
730 ide_check_nien_quirk_list(drive);
731
7ed5b157
BZ
732 if (port_ops && port_ops->quirkproc)
733 port_ops->quirkproc(drive);
f01393e4 734 }
0380dad4 735
7ed5b157
BZ
736 ide_port_for_each_present_dev(i, drive, hwif) {
737 ide_set_max_pio(drive);
1da177e4 738
7ed5b157 739 drive->dev_flags |= IDE_DFLAG_NICE1;
1da177e4 740
7ed5b157
BZ
741 if (hwif->dma_ops)
742 ide_set_dma(drive);
1da177e4
LT
743 }
744}
745
ca18d6f7 746static void ide_initialize_rq(struct request *rq)
82ed4db4
CH
747{
748 struct ide_request *req = blk_mq_rq_to_pdu(rq);
749
d16a6766 750 req->special = NULL;
c8d9cf22 751 scsi_req_init(&req->sreq);
82ed4db4 752 req->sreq.sense = req->sense;
82ed4db4
CH
753}
754
60033520
JA
755static const struct blk_mq_ops ide_mq_ops = {
756 .queue_rq = ide_queue_rq,
757 .initialize_rq_fn = ide_initialize_rq,
758};
759
1da177e4
LT
760/*
761 * init request queue
762 */
763static int ide_init_queue(ide_drive_t *drive)
764{
165125e1 765 struct request_queue *q;
898ec223 766 ide_hwif_t *hwif = drive->hwif;
1da177e4
LT
767 int max_sectors = 256;
768 int max_sg_entries = PRD_ENTRIES;
60033520 769 struct blk_mq_tag_set *set;
1da177e4
LT
770
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 */
60033520
JA
778
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))
1da177e4
LT
788 return 1;
789
60033520
JA
790 q = blk_mq_init_queue(set);
791 if (IS_ERR(q)) {
792 blk_mq_free_tag_set(set);
82ed4db4
CH
793 return 1;
794 }
795
60033520
JA
796 blk_queue_flag_set(QUEUE_FLAG_SCSI_PASSTHROUGH, q);
797
1da177e4
LT
798 q->queuedata = drive;
799 blk_queue_segment_boundary(q, 0xffff);
800
1da177e4
LT
801 if (hwif->rqsize < max_sectors)
802 max_sectors = hwif->rqsize;
086fa5ff 803 blk_queue_max_hw_sectors(q, max_sectors);
1da177e4
LT
804
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 */
164c3d09 815 max_sg_entries >>= 1;
1da177e4
LT
816#endif /* CONFIG_PCI */
817
8a78362c 818 blk_queue_max_segments(q, max_sg_entries);
1da177e4
LT
819
820 /* assign drive queue */
821 drive->queue = q;
822
1da177e4
LT
823 return 0;
824}
825
b40d1b88
BZ
826static DEFINE_MUTEX(ide_cfg_mtx);
827
d5bc6592
BZ
828/*
829 * For any present drive:
830 * - allocate the block device queue
d5bc6592 831 */
e415e495 832static int ide_port_setup_devices(ide_hwif_t *hwif)
d5bc6592 833{
2bd24a1c 834 ide_drive_t *drive;
e415e495 835 int i, j = 0;
d5bc6592 836
26042d05 837 mutex_lock(&ide_cfg_mtx);
7ed5b157 838 ide_port_for_each_present_dev(i, drive, hwif) {
d5bc6592
BZ
839 if (ide_init_queue(drive)) {
840 printk(KERN_ERR "ide: failed to init %s\n",
841 drive->name);
e415e495 842 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
d5bc6592
BZ
843 continue;
844 }
845
e415e495 846 j++;
d5bc6592 847 }
26042d05 848 mutex_unlock(&ide_cfg_mtx);
e415e495
EO
849
850 return j;
d5bc6592
BZ
851}
852
ffc36c76
BZ
853static 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
1da177e4 871/*
b65fac32 872 * This routine sets up the IRQ for an IDE interface.
1da177e4
LT
873 */
874static int init_irq (ide_hwif_t *hwif)
875{
4c3032d8 876 struct ide_io_ports *io_ports = &hwif->io_ports;
255115fb
BZ
877 struct ide_host *host = hwif->host;
878 irq_handler_t irq_handler = host->irq_handler;
879 int sa = host->irq_flags;
1da177e4 880
849d7130
SG
881 if (irq_handler == NULL)
882 irq_handler = ide_intr;
883
a259d532
MS
884 if (!host->get_lock)
885 if (request_irq(hwif->irq, irq_handler, sa, hwif->name, hwif))
886 goto out_up;
1da177e4 887
7b892806 888#if !defined(__mc68000__)
1b8ebad8 889 printk(KERN_INFO "%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
4c3032d8
BZ
890 io_ports->data_addr, io_ports->status_addr,
891 io_ports->ctl_addr, hwif->irq);
1da177e4 892#else
1b8ebad8 893 printk(KERN_INFO "%s at 0x%08lx on irq %d", hwif->name,
4c3032d8 894 io_ports->data_addr, hwif->irq);
7b892806 895#endif /* __mc68000__ */
b65fac32
BZ
896 if (hwif->host->host_flags & IDE_HFLAG_SERIALIZE)
897 printk(KERN_CONT " (serialized)");
1b8ebad8 898 printk(KERN_CONT "\n");
d5bc6592 899
1da177e4 900 return 0;
1da177e4 901out_up:
1da177e4
LT
902 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;
5e7f3a46 915 ide_drive_t *drive = hwif->devices[unit];
97100fc8
BZ
916
917 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
1da177e4
LT
918 return NULL;
919
920 if (drive->media == ide_disk)
921 request_module("ide-disk");
1da177e4
LT
922 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;
ed9e1982 936 return &disk_to_dev(p)->kobj;
1da177e4
LT
937}
938
939static int exact_lock(dev_t dev, void *data)
940{
941 struct gendisk *p = data;
942
3079c22e 943 if (!get_disk_and_module(p))
1da177e4
LT
944 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;
7f612f27 967 unsigned int unit = drive->dn & 1;
1da177e4
LT
968
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
5b0c4b30
BZ
981 ide_proc_unregister_device(drive);
982
60033520
JA
983 if (drive->sense_rq)
984 blk_mq_free_request(drive->sense_rq);
985
b5479167
BZ
986 blk_cleanup_queue(drive->queue);
987 drive->queue = NULL;
60033520 988 blk_mq_free_tag_set(&drive->tag_set);
b5479167 989
97100fc8 990 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
8604affd 991
f36d4024 992 complete(&drive->gendev_rel_comp);
1da177e4
LT
993}
994
1da177e4
LT
995static int hwif_init(ide_hwif_t *hwif)
996{
1da177e4 997 if (!hwif->irq) {
8b07ed26
BZ
998 printk(KERN_ERR "%s: disabled, no IRQ\n", hwif->name);
999 return 0;
1da177e4 1000 }
1da177e4 1001
1da177e4
LT
1002 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
6da2ec56
KC
1008 hwif->sg_table = kmalloc_array(hwif->sg_max_nents,
1009 sizeof(struct scatterlist),
1010 GFP_KERNEL);
1da177e4
LT
1011 if (!hwif->sg_table) {
1012 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1013 goto out;
1014 }
45711f1a
JA
1015
1016 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
1da177e4 1017
1da177e4 1018 if (init_irq(hwif)) {
8b07ed26
BZ
1019 printk(KERN_ERR "%s: disabled, unable to get IRQ %d\n",
1020 hwif->name, hwif->irq);
1da177e4
LT
1021 goto out;
1022 }
1da177e4 1023
3a4e7c96
BZ
1024 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1025 THIS_MODULE, ata_probe, ata_lock, hwif);
1da177e4
LT
1026 return 1;
1027
1028out:
1029 unregister_blkdev(hwif->major, hwif->name);
1030 return 0;
1031}
1032
9601a607
BZ
1033static void hwif_register_devices(ide_hwif_t *hwif)
1034{
2bd24a1c 1035 ide_drive_t *drive;
9601a607
BZ
1036 unsigned int i;
1037
7ed5b157 1038 ide_port_for_each_present_dev(i, drive, hwif) {
c5d70cc7
BZ
1039 struct device *dev = &drive->gendev;
1040 int ret;
9601a607 1041
dc09c784 1042 dev_set_name(dev, "%u.%u", hwif->index, i);
fcb52077 1043 dev_set_drvdata(dev, drive);
c5d70cc7
BZ
1044 dev->parent = &hwif->gendev;
1045 dev->bus = &ide_bus_type;
c5d70cc7
BZ
1046 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);
9601a607
BZ
1052 }
1053}
1054
7704ca2a
BZ
1055static void ide_port_init_devices(ide_hwif_t *hwif)
1056{
ac95beed 1057 const struct ide_port_ops *port_ops = hwif->port_ops;
2bd24a1c 1058 ide_drive_t *drive;
7704ca2a
BZ
1059 int i;
1060
2bd24a1c 1061 ide_port_for_each_dev(i, drive, hwif) {
123995b9
BZ
1062 drive->dn = i + hwif->channel * 2;
1063
7704ca2a
BZ
1064 if (hwif->host_flags & IDE_HFLAG_IO_32BIT)
1065 drive->io_32bit = 1;
7610c4f5
BZ
1066 if (hwif->host_flags & IDE_HFLAG_NO_IO_32BIT)
1067 drive->dev_flags |= IDE_DFLAG_NO_IO_32BIT;
7704ca2a 1068 if (hwif->host_flags & IDE_HFLAG_UNMASK_IRQS)
97100fc8 1069 drive->dev_flags |= IDE_DFLAG_UNMASK;
807b90d0 1070 if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS)
97100fc8 1071 drive->dev_flags |= IDE_DFLAG_NO_UNMASK;
1f2cf8b0 1072
d2d4e780
BZ
1073 drive->pio_mode = XFER_PIO_0;
1074
e6d95bd1
BZ
1075 if (port_ops && port_ops->init_dev)
1076 port_ops->init_dev(drive);
1077 }
7704ca2a
BZ
1078}
1079
c413b9b9
BZ
1080static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
1081 const struct ide_port_info *d)
8447d9d5 1082{
b7691646 1083 hwif->channel = port;
c413b9b9 1084
29e52cf7 1085 hwif->chipset = d->chipset ? d->chipset : ide_pci;
c413b9b9
BZ
1086
1087 if (d->init_iops)
1088 d->init_iops(hwif);
1089
23f8e4bf
BZ
1090 /* ->host_flags may be set by ->init_iops (or even earlier...) */
1091 hwif->host_flags |= d->host_flags;
c413b9b9
BZ
1092 hwif->pio_mask = d->pio_mask;
1093
374e042c
BZ
1094 if (d->tp_ops)
1095 hwif->tp_ops = d->tp_ops;
1096
ac95beed 1097 /* ->set_pio_mode for DTC2278 is currently limited to port 0 */
2787cb8a 1098 if ((hwif->host_flags & IDE_HFLAG_DTC2278) == 0 || hwif->channel == 0)
ac95beed
BZ
1099 hwif->port_ops = d->port_ops;
1100
c413b9b9
BZ
1101 hwif->swdma_mask = d->swdma_mask;
1102 hwif->mwdma_mask = d->mwdma_mask;
1103 hwif->ultra_mask = d->udma_mask;
1104
b123f56e
BZ
1105 if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
1106 int rc;
1107
592b5315
SS
1108 hwif->dma_ops = d->dma_ops;
1109
b123f56e
BZ
1110 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);
592b5315
SS
1117
1118 hwif->dma_ops = NULL;
ebb00fb5 1119 hwif->dma_base = 0;
b123f56e
BZ
1120 hwif->swdma_mask = 0;
1121 hwif->mwdma_mask = 0;
1122 hwif->ultra_mask = 0;
592b5315 1123 }
b123f56e 1124 }
c413b9b9 1125
1024c5f4 1126 if ((d->host_flags & IDE_HFLAG_SERIALIZE) ||
702c026b
BZ
1127 ((d->host_flags & IDE_HFLAG_SERIALIZE_DMA) && hwif->dma_base))
1128 hwif->host->host_flags |= IDE_HFLAG_SERIALIZE;
1024c5f4 1129
6b492496
BZ
1130 if (d->max_sectors)
1131 hwif->rqsize = d->max_sectors;
70775e9c
BZ
1132 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 }
c413b9b9
BZ
1139
1140 /* call chipset specific routine for each enabled port */
1141 if (d->init_hwif)
1142 d->init_hwif(hwif);
c7f6f21a 1143}
bfa14b42 1144
c7f6f21a
BZ
1145static void ide_port_cable_detect(ide_hwif_t *hwif)
1146{
ac95beed
BZ
1147 const struct ide_port_ops *port_ops = hwif->port_ops;
1148
1149 if (port_ops && port_ops->cable_detect && (hwif->ultra_mask & 0x78)) {
bfa14b42 1150 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
ac95beed 1151 hwif->cbl = port_ops->cable_detect(hwif);
bfa14b42 1152 }
c413b9b9
BZ
1153}
1154
60033520
JA
1155/*
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;
9a6d5488 1163 blk_status_t ret;
60033520
JA
1164 LIST_HEAD(list);
1165
9a6d5488 1166 blk_mq_quiesce_queue(drive->queue);
60033520 1167
9a6d5488
JA
1168 ret = BLK_STS_OK;
1169 spin_lock_irq(&hwif->lock);
1170 while (!list_empty(&drive->rq_list)) {
1171 rq = list_first_entry(&drive->rq_list, struct request, queuelist);
60033520 1172 list_del_init(&rq->queuelist);
9a6d5488
JA
1173
1174 spin_unlock_irq(&hwif->lock);
1175 ret = ide_issue_rq(drive, rq, true);
1176 spin_lock_irq(&hwif->lock);
60033520 1177 }
9a6d5488
JA
1178 spin_unlock_irq(&hwif->lock);
1179
1180 blk_mq_unquiesce_queue(drive->queue);
1181
1182 if (ret != BLK_STS_OK)
1183 kblockd_schedule_work(&drive->rq_work);
60033520
JA
1184}
1185
b40d1b88
BZ
1186static 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{
2bd24a1c
BZ
1192 ide_drive_t *drive;
1193 int i;
b40d1b88 1194
2bd24a1c
BZ
1195 ide_port_for_each_dev(i, drive, hwif) {
1196 u8 j = (hwif->index * MAX_DRIVES) + i;
5f4417a1 1197 u16 *saved_id = drive->id;
b40d1b88
BZ
1198
1199 memset(drive, 0, sizeof(*drive));
5f4417a1
HD
1200 memset(saved_id, 0, SECTOR_SIZE);
1201 drive->id = saved_id;
b40d1b88
BZ
1202
1203 drive->media = ide_disk;
2bd24a1c 1204 drive->select = (i << 4) | ATA_DEVICE_OBS;
b40d1b88
BZ
1205 drive->hwif = hwif;
1206 drive->ready_stat = ATA_DRDY;
1207 drive->bad_wstat = BAD_W_STAT;
ca1b96e0
BZ
1208 drive->special_flags = IDE_SFLAG_RECALIBRATE |
1209 IDE_SFLAG_SET_GEOMETRY;
b40d1b88
BZ
1210 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);
60033520
JA
1217
1218 INIT_WORK(&drive->rq_work, drive_rq_insert_work);
1219 INIT_LIST_HEAD(&drive->rq_list);
b40d1b88
BZ
1220 }
1221}
1222
1223static void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
1224{
b40d1b88
BZ
1225 /* 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
7362951b
BZ
1234 spin_lock_init(&hwif->lock);
1235
10738ba8 1236 timer_setup(&hwif->timer, ide_timer_expiry, 0);
7362951b 1237
b40d1b88
BZ
1238 init_completion(&hwif->gendev_rel_comp);
1239
1240 hwif->tp_ops = &default_tp_ops;
1241
1242 ide_port_init_devices_data(hwif);
1243}
1244
9f36d314 1245static void ide_init_port_hw(ide_hwif_t *hwif, struct ide_hw *hw)
b40d1b88
BZ
1246{
1247 memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports));
1248 hwif->irq = hw->irq;
b40d1b88
BZ
1249 hwif->dev = hw->dev;
1250 hwif->gendev.parent = hw->parent ? hw->parent : hw->dev;
b40d1b88
BZ
1251 hwif->config_data = hw->config;
1252}
1253
8cdf3100
BZ
1254static unsigned int ide_indexes;
1255
fe80b937 1256/**
8cdf3100 1257 * ide_find_port_slot - find free port slot
fe80b937
BZ
1258 * @d: IDE port info
1259 *
8cdf3100 1260 * Return the new port slot index or -ENOENT if we are out of free slots.
fe80b937
BZ
1261 */
1262
8cdf3100 1263static int ide_find_port_slot(const struct ide_port_info *d)
fe80b937 1264{
8cdf3100 1265 int idx = -ENOENT;
fe80b937 1266 u8 bootable = (d && (d->host_flags & IDE_HFLAG_NON_BOOTABLE)) ? 0 : 1;
a419aef8 1267 u8 i = (d && (d->host_flags & IDE_HFLAG_QD_2ND_PORT)) ? 1 : 0;
fe80b937
BZ
1268
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 */
8cdf3100 1279 mutex_lock(&ide_cfg_mtx);
75d21fff
BZ
1280 if (bootable) {
1281 if ((ide_indexes | i) != (1 << MAX_HWIFS) - 1)
1282 idx = ffz(ide_indexes | i);
fe80b937 1283 } else {
75d21fff
BZ
1284 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);
fe80b937 1288 }
8cdf3100
BZ
1289 if (idx >= 0)
1290 ide_indexes |= (1 << idx);
1291 mutex_unlock(&ide_cfg_mtx);
fe80b937 1292
8cdf3100
BZ
1293 return idx;
1294}
256c5f8e 1295
8cdf3100
BZ
1296static 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);
fe80b937 1301}
fe80b937 1302
5e7f3a46
BZ
1303static void ide_port_free_devices(ide_hwif_t *hwif)
1304{
2bd24a1c 1305 ide_drive_t *drive;
5e7f3a46
BZ
1306 int i;
1307
5f4417a1
HD
1308 ide_port_for_each_dev(i, drive, hwif) {
1309 kfree(drive->id);
2bd24a1c 1310 kfree(drive);
5f4417a1 1311 }
5e7f3a46
BZ
1312}
1313
1314static int ide_port_alloc_devices(ide_hwif_t *hwif, int node)
1315{
82ed4db4 1316 ide_drive_t *drive;
5e7f3a46
BZ
1317 int i;
1318
1319 for (i = 0; i < MAX_DRIVES; i++) {
5e7f3a46
BZ
1320 drive = kzalloc_node(sizeof(*drive), GFP_KERNEL, node);
1321 if (drive == NULL)
1322 goto out_nomem;
1323
5f4417a1
HD
1324 /*
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)
82ed4db4
CH
1334 goto out_free_drive;
1335
5e7f3a46
BZ
1336 hwif->devices[i] = drive;
1337 }
1338 return 0;
1339
82ed4db4
CH
1340out_free_drive:
1341 kfree(drive);
5e7f3a46
BZ
1342out_nomem:
1343 ide_port_free_devices(hwif);
1344 return -ENOMEM;
1345}
1346
9f36d314
BZ
1347struct ide_host *ide_host_alloc(const struct ide_port_info *d,
1348 struct ide_hw **hws, unsigned int n_ports)
48c3c107
BZ
1349{
1350 struct ide_host *host;
a32296f9
BZ
1351 struct device *dev = hws[0] ? hws[0]->dev : NULL;
1352 int node = dev ? dev_to_node(dev) : -1;
48c3c107
BZ
1353 int i;
1354
a32296f9 1355 host = kzalloc_node(sizeof(*host), GFP_KERNEL, node);
48c3c107
BZ
1356 if (host == NULL)
1357 return NULL;
1358
dca39830 1359 for (i = 0; i < n_ports; i++) {
48c3c107 1360 ide_hwif_t *hwif;
8cdf3100 1361 int idx;
48c3c107
BZ
1362
1363 if (hws[i] == NULL)
1364 continue;
1365
a32296f9 1366 hwif = kzalloc_node(sizeof(*hwif), GFP_KERNEL, node);
18de1017
BZ
1367 if (hwif == NULL)
1368 continue;
1369
5e7f3a46
BZ
1370 if (ide_port_alloc_devices(hwif, node) < 0) {
1371 kfree(hwif);
1372 continue;
1373 }
1374
8cdf3100
BZ
1375 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");
5f4417a1 1379 ide_port_free_devices(hwif);
18de1017 1380 kfree(hwif);
8cdf3100 1381 continue;
48c3c107 1382 }
8cdf3100 1383
8cdf3100
BZ
1384 ide_init_port_data(hwif, idx);
1385
08da591e
BZ
1386 hwif->host = host;
1387
8cdf3100
BZ
1388 host->ports[i] = hwif;
1389 host->n_ports++;
48c3c107
BZ
1390 }
1391
1392 if (host->n_ports == 0) {
1393 kfree(host);
1394 return NULL;
1395 }
1396
a32296f9 1397 host->dev[0] = dev;
6cdf6eb3 1398
feb22b7f
BZ
1399 if (d) {
1400 host->init_chipset = d->init_chipset;
e354c1d8
BZ
1401 host->get_lock = d->get_lock;
1402 host->release_lock = d->release_lock;
ef0b0427 1403 host->host_flags = d->host_flags;
aa07573b 1404 host->irq_flags = d->irq_flags;
feb22b7f 1405 }
ef0b0427 1406
48c3c107
BZ
1407 return host;
1408}
48c3c107
BZ
1409EXPORT_SYMBOL_GPL(ide_host_alloc);
1410
9a100f4b
BZ
1411static 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
48c3c107 1435int ide_host_register(struct ide_host *host, const struct ide_port_info *d,
9f36d314 1436 struct ide_hw **hws)
c413b9b9
BZ
1437{
1438 ide_hwif_t *hwif, *mate = NULL;
e0d00207 1439 int i, j = 0;
8447d9d5 1440
7ad19a99
CH
1441 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
2bd24a1c 1444 ide_host_for_each_port(i, hwif, host) {
e0d00207 1445 if (hwif == NULL) {
c413b9b9
BZ
1446 mate = NULL;
1447 continue;
1448 }
1449
c97c6aca 1450 ide_init_port_hw(hwif, hws[i]);
9fd91d95
BZ
1451 ide_port_apply_params(hwif);
1452
0a6e49e9
BZ
1453 if ((i & 1) && mate) {
1454 hwif->mate = mate;
1455 mate->mate = hwif;
123995b9 1456 }
c413b9b9 1457
0a6e49e9
BZ
1458 mate = (i & 1) ? NULL : hwif;
1459
1460 ide_init_port(hwif, i & 1, d);
1461 ide_port_cable_detect(hwif);
5880b5de
BZ
1462
1463 hwif->port_flags |= IDE_PFLAG_PROBING;
1464
7704ca2a 1465 ide_port_init_devices(hwif);
c413b9b9
BZ
1466 }
1467
2bd24a1c 1468 ide_host_for_each_port(i, hwif, host) {
e0d00207
BZ
1469 if (hwif == NULL)
1470 continue;
139ddfca 1471
eb716beb
BZ
1472 if (ide_probe_port(hwif) == 0)
1473 hwif->present = 1;
a14dc574 1474
5880b5de
BZ
1475 hwif->port_flags &= ~IDE_PFLAG_PROBING;
1476
c094ea07
BZ
1477 if ((hwif->host_flags & IDE_HFLAG_4DRIVES) == 0 ||
1478 hwif->mate == NULL || hwif->mate->present == 0) {
9a100f4b
BZ
1479 if (ide_register_port(hwif)) {
1480 ide_disable_port(hwif);
1481 continue;
1482 }
1483 }
a14dc574 1484
eb716beb
BZ
1485 if (hwif->present)
1486 ide_port_tune_devices(hwif);
2e13093a 1487 }
ba6560aa 1488
ffc36c76
BZ
1489 ide_host_enable_irqs(host);
1490
2bd24a1c 1491 ide_host_for_each_port(i, hwif, host) {
e0d00207
BZ
1492 if (hwif == NULL)
1493 continue;
ba6560aa
BZ
1494
1495 if (hwif_init(hwif) == 0) {
1496 printk(KERN_INFO "%s: failed to initialize IDE "
1497 "interface\n", hwif->name);
d410a641 1498 device_unregister(hwif->portdev);
51d6ac70
IC
1499 device_unregister(&hwif->gendev);
1500 ide_disable_port(hwif);
ba6560aa
BZ
1501 continue;
1502 }
decdc3f0 1503
eb716beb 1504 if (hwif->present)
e415e495
EO
1505 if (ide_port_setup_devices(hwif) == 0) {
1506 hwif->present = 0;
1507 continue;
1508 }
1509
1510 j++;
26042d05 1511
8b803bd1 1512 ide_acpi_init_port(hwif);
eb716beb
BZ
1513
1514 if (hwif->present)
1515 ide_acpi_port_init_devices(hwif);
2e13093a
BZ
1516 }
1517
2bd24a1c 1518 ide_host_for_each_port(i, hwif, host) {
e0d00207
BZ
1519 if (hwif == NULL)
1520 continue;
327617e1 1521
eb716beb
BZ
1522 ide_sysfs_register_port(hwif);
1523 ide_proc_register_port(hwif);
1524
dbee0322 1525 if (hwif->present) {
d9270a3f 1526 ide_proc_port_register_devices(hwif);
dbee0322
WS
1527 hwif_register_devices(hwif);
1528 }
8447d9d5
BZ
1529 }
1530
e0d00207 1531 return j ? 0 : -1;
8447d9d5 1532}
48c3c107 1533EXPORT_SYMBOL_GPL(ide_host_register);
151575e4 1534
9f36d314 1535int ide_host_add(const struct ide_port_info *d, struct ide_hw **hws,
dca39830 1536 unsigned int n_ports, struct ide_host **hostp)
6f904d01
BZ
1537{
1538 struct ide_host *host;
8a69580e 1539 int rc;
6f904d01 1540
dca39830 1541 host = ide_host_alloc(d, hws, n_ports);
6f904d01
BZ
1542 if (host == NULL)
1543 return -ENOMEM;
1544
8a69580e
BZ
1545 rc = ide_host_register(host, d, hws);
1546 if (rc) {
1547 ide_host_free(host);
1548 return rc;
1549 }
6f904d01
BZ
1550
1551 if (hostp)
1552 *hostp = host;
1553
1554 return 0;
1555}
1556EXPORT_SYMBOL_GPL(ide_host_add);
1557
b40d1b88
BZ
1558static void __ide_port_unregister_devices(ide_hwif_t *hwif)
1559{
2bd24a1c 1560 ide_drive_t *drive;
b40d1b88
BZ
1561 int i;
1562
7ed5b157
BZ
1563 ide_port_for_each_present_dev(i, drive, hwif) {
1564 device_unregister(&drive->gendev);
1565 wait_for_completion(&drive->gendev_rel_comp);
b40d1b88
BZ
1566 }
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
a259d532
MS
1607 if (!hwif->host->get_lock)
1608 free_irq(hwif->irq, hwif);
b40d1b88
BZ
1609
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
8a69580e 1626void ide_host_free(struct ide_host *host)
151575e4 1627{
8cdf3100 1628 ide_hwif_t *hwif;
151575e4 1629 int i;
8447d9d5 1630
2bd24a1c 1631 ide_host_for_each_port(i, hwif, host) {
9a100f4b
BZ
1632 if (hwif)
1633 ide_port_free(hwif);
c97c6aca 1634 }
151575e4 1635
48c3c107 1636 kfree(host);
151575e4 1637}
8a69580e
BZ
1638EXPORT_SYMBOL_GPL(ide_host_free);
1639
1640void ide_host_remove(struct ide_host *host)
1641{
2bd24a1c 1642 ide_hwif_t *hwif;
8a69580e
BZ
1643 int i;
1644
2bd24a1c
BZ
1645 ide_host_for_each_port(i, hwif, host) {
1646 if (hwif)
1647 ide_unregister(hwif);
8a69580e
BZ
1648 }
1649
1650 ide_host_free(host);
1651}
48c3c107 1652EXPORT_SYMBOL_GPL(ide_host_remove);
2dde7861
BZ
1653
1654void ide_port_scan(ide_hwif_t *hwif)
1655{
5880b5de
BZ
1656 int rc;
1657
9fd91d95 1658 ide_port_apply_params(hwif);
2dde7861 1659 ide_port_cable_detect(hwif);
5880b5de
BZ
1660
1661 hwif->port_flags |= IDE_PFLAG_PROBING;
1662
2dde7861
BZ
1663 ide_port_init_devices(hwif);
1664
5880b5de
BZ
1665 rc = ide_probe_port(hwif);
1666
1667 hwif->port_flags &= ~IDE_PFLAG_PROBING;
1668
1669 if (rc < 0)
2dde7861
BZ
1670 return;
1671
1672 hwif->present = 1;
1673
1674 ide_port_tune_devices(hwif);
2dde7861 1675 ide_port_setup_devices(hwif);
e630fcbe 1676 ide_acpi_port_init_devices(hwif);
2dde7861
BZ
1677 hwif_register_devices(hwif);
1678 ide_proc_port_register_devices(hwif);
1679}
1680EXPORT_SYMBOL_GPL(ide_port_scan);