Merge tag 'platform-drivers-x86-v5.1-1' of git://git.infradead.org/linux-platform...
[linux-2.6-block.git] / drivers / ide / ide-probe.c
CommitLineData
1da177e4 1/*
59bca8cc
BZ
2 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
3 * Copyright (C) 2005, 2007 Bartlomiej Zolnierkiewicz
1da177e4
LT
4 */
5
6/*
7 * Mostly written by Mark Lord <mlord@pobox.com>
8 * and Gadi Oxman <gadio@netvision.net.il>
9 * and Andre Hedrick <andre@linux-ide.org>
10 *
11 * See linux/MAINTAINERS for address of current maintainer.
12 *
13 * This is the IDE probe module, as evolved from hd.c and ide.c.
14 *
bbe4d6d8
BZ
15 * -- increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
16 * by Andrea Arcangeli
1da177e4
LT
17 */
18
1da177e4
LT
19#include <linux/module.h>
20#include <linux/types.h>
21#include <linux/string.h>
22#include <linux/kernel.h>
23#include <linux/timer.h>
24#include <linux/mm.h>
25#include <linux/interrupt.h>
26#include <linux/major.h>
27#include <linux/errno.h>
28#include <linux/genhd.h>
29#include <linux/slab.h>
30#include <linux/delay.h>
31#include <linux/ide.h>
32#include <linux/spinlock.h>
33#include <linux/kmod.h>
34#include <linux/pci.h>
dc81785d 35#include <linux/scatterlist.h>
1da177e4
LT
36
37#include <asm/byteorder.h>
38#include <asm/irq.h>
7c0f6ba6 39#include <linux/uaccess.h>
1da177e4
LT
40#include <asm/io.h>
41
42/**
43 * generic_id - add a generic drive id
44 * @drive: drive to make an ID block for
45 *
46 * Add a fake id field to the drive we are passed. This allows
47 * use to skip a ton of NULL checks (which people always miss)
48 * and make drive properties unconditional outside of this file
49 */
50
51static void generic_id(ide_drive_t *drive)
52{
4dde4492
BZ
53 u16 *id = drive->id;
54
55 id[ATA_ID_CUR_CYLS] = id[ATA_ID_CYLS] = drive->cyl;
56 id[ATA_ID_CUR_HEADS] = id[ATA_ID_HEADS] = drive->head;
57 id[ATA_ID_CUR_SECTORS] = id[ATA_ID_SECTORS] = drive->sect;
1da177e4
LT
58}
59
60static void ide_disk_init_chs(ide_drive_t *drive)
61{
4dde4492 62 u16 *id = drive->id;
1da177e4
LT
63
64 /* Extract geometry if we did not already have one for the drive */
65 if (!drive->cyl || !drive->head || !drive->sect) {
4dde4492
BZ
66 drive->cyl = drive->bios_cyl = id[ATA_ID_CYLS];
67 drive->head = drive->bios_head = id[ATA_ID_HEADS];
68 drive->sect = drive->bios_sect = id[ATA_ID_SECTORS];
1da177e4
LT
69 }
70
71 /* Handle logical geometry translation by the drive */
dd8f46f6 72 if (ata_id_current_chs_valid(id)) {
4dde4492
BZ
73 drive->cyl = id[ATA_ID_CUR_CYLS];
74 drive->head = id[ATA_ID_CUR_HEADS];
75 drive->sect = id[ATA_ID_CUR_SECTORS];
1da177e4
LT
76 }
77
78 /* Use physical geometry if what we have still makes no sense */
4dde4492
BZ
79 if (drive->head > 16 && id[ATA_ID_HEADS] && id[ATA_ID_HEADS] <= 16) {
80 drive->cyl = id[ATA_ID_CYLS];
81 drive->head = id[ATA_ID_HEADS];
82 drive->sect = id[ATA_ID_SECTORS];
1da177e4
LT
83 }
84}
85
86static void ide_disk_init_mult_count(ide_drive_t *drive)
87{
48fb2688
BZ
88 u16 *id = drive->id;
89 u8 max_multsect = id[ATA_ID_MAX_MULTSECT] & 0xff;
1da177e4 90
48fb2688 91 if (max_multsect) {
48fb2688
BZ
92 if ((max_multsect / 2) > 1)
93 id[ATA_ID_MULTSECT] = max_multsect | 0x100;
94 else
95 id[ATA_ID_MULTSECT] &= ~0x1ff;
96
97 drive->mult_req = id[ATA_ID_MULTSECT] & 0xff;
7c51c17e
BZ
98
99 if (drive->mult_req)
ca1b96e0 100 drive->special_flags |= IDE_SFLAG_SET_MULTMODE;
1da177e4
LT
101 }
102}
103
24630dc6
BZ
104static void ide_classify_ata_dev(ide_drive_t *drive)
105{
106 u16 *id = drive->id;
107 char *m = (char *)&id[ATA_ID_PROD];
108 int is_cfa = ata_id_is_cfa(id);
109
110 /* CF devices are *not* removable in Linux definition of the term */
111 if (is_cfa == 0 && (id[ATA_ID_CONFIG] & (1 << 7)))
112 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
113
114 drive->media = ide_disk;
115
116 if (!ata_id_has_unload(drive->id))
117 drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
118
119 printk(KERN_INFO "%s: %s, %s DISK drive\n", drive->name, m,
120 is_cfa ? "CFA" : "ATA");
121}
122
123static void ide_classify_atapi_dev(ide_drive_t *drive)
124{
125 u16 *id = drive->id;
126 char *m = (char *)&id[ATA_ID_PROD];
127 u8 type = (id[ATA_ID_CONFIG] >> 8) & 0x1f;
128
129 printk(KERN_INFO "%s: %s, ATAPI ", drive->name, m);
130 switch (type) {
131 case ide_floppy:
132 if (!strstr(m, "CD-ROM")) {
133 if (!strstr(m, "oppy") &&
134 !strstr(m, "poyp") &&
135 !strstr(m, "ZIP"))
136 printk(KERN_CONT "cdrom or floppy?, assuming ");
137 if (drive->media != ide_cdrom) {
138 printk(KERN_CONT "FLOPPY");
139 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
140 break;
141 }
142 }
143 /* Early cdrom models used zero */
144 type = ide_cdrom;
a2eed33d 145 /* fall through */
24630dc6
BZ
146 case ide_cdrom:
147 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
148#ifdef CONFIG_PPC
149 /* kludge for Apple PowerBook internal zip */
150 if (!strstr(m, "CD-ROM") && strstr(m, "ZIP")) {
151 printk(KERN_CONT "FLOPPY");
152 type = ide_floppy;
153 break;
154 }
155#endif
156 printk(KERN_CONT "CD/DVD-ROM");
157 break;
158 case ide_tape:
159 printk(KERN_CONT "TAPE");
160 break;
161 case ide_optical:
162 printk(KERN_CONT "OPTICAL");
163 drive->dev_flags |= IDE_DFLAG_REMOVABLE;
164 break;
165 default:
166 printk(KERN_CONT "UNKNOWN (type %d)", type);
167 break;
168 }
169
170 printk(KERN_CONT " drive\n");
171 drive->media = type;
172 /* an ATAPI device ignores DRDY */
173 drive->ready_stat = 0;
174 if (ata_id_cdb_intr(id))
175 drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
176 drive->dev_flags |= IDE_DFLAG_DOORLOCKING;
177 /* we don't do head unloading on ATAPI devices */
178 drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
179}
180
1da177e4
LT
181/**
182 * do_identify - identify a drive
183 * @drive: drive to identify
184 * @cmd: command used
2ebe1d9e 185 * @id: buffer for IDENTIFY data
1da177e4
LT
186 *
187 * Called when we have issued a drive identify command to
188 * read and parse the results. This function is run with
189 * interrupts disabled.
190 */
047140ae 191
2ebe1d9e 192static void do_identify(ide_drive_t *drive, u8 cmd, u16 *id)
1da177e4 193{
898ec223 194 ide_hwif_t *hwif = drive->hwif;
4dde4492 195 char *m = (char *)&id[ATA_ID_PROD];
94b9efdf 196 unsigned long flags;
24630dc6 197 int bswap = 1;
1da177e4 198
94b9efdf
BZ
199 /* local CPU only; some systems need this */
200 local_irq_save(flags);
1da177e4 201 /* read 512 bytes of id info */
374e042c 202 hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
94b9efdf 203 local_irq_restore(flags);
1da177e4 204
97100fc8 205 drive->dev_flags |= IDE_DFLAG_ID_READ;
7b9f25b5
BZ
206#ifdef DEBUG
207 printk(KERN_INFO "%s: dumping identify data\n", drive->name);
208 ide_dump_identify((u8 *)id);
209#endif
1da177e4
LT
210 ide_fix_driveid(id);
211
1da177e4 212 /*
aaaade3f
BZ
213 * ATA_CMD_ID_ATA returns little-endian info,
214 * ATA_CMD_ID_ATAPI *usually* returns little-endian info.
1da177e4 215 */
aaaade3f 216 if (cmd == ATA_CMD_ID_ATAPI) {
4dde4492
BZ
217 if ((m[0] == 'N' && m[1] == 'E') || /* NEC */
218 (m[0] == 'F' && m[1] == 'X') || /* Mitsumi */
219 (m[0] == 'P' && m[1] == 'i')) /* Pioneer */
1da177e4 220 /* Vertos drives may still be weird */
4dde4492 221 bswap ^= 1;
1da177e4 222 }
4dde4492
BZ
223
224 ide_fixstring(m, ATA_ID_PROD_LEN, bswap);
225 ide_fixstring((char *)&id[ATA_ID_FW_REV], ATA_ID_FW_REV_LEN, bswap);
226 ide_fixstring((char *)&id[ATA_ID_SERNO], ATA_ID_SERNO_LEN, bswap);
1da177e4 227
699b052a 228 /* we depend on this a lot! */
4dde4492 229 m[ATA_ID_PROD_LEN - 1] = '\0';
699b052a 230
4dde4492 231 if (strstr(m, "E X A B Y T E N E S T"))
69197ad7
BZ
232 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
233 else
234 drive->dev_flags |= IDE_DFLAG_PRESENT;
1da177e4
LT
235}
236
237/**
2ebe1d9e 238 * ide_dev_read_id - send ATA/ATAPI IDENTIFY command
1da177e4
LT
239 * @drive: drive to identify
240 * @cmd: command to use
2ebe1d9e 241 * @id: buffer for IDENTIFY data
fa56d4cb 242 * @irq_ctx: flag set when called from the IRQ context
1da177e4 243 *
2ebe1d9e 244 * Sends an ATA(PI) IDENTIFY request to a drive and waits for a response.
1da177e4
LT
245 *
246 * Returns: 0 device was identified
247 * 1 device timed-out (no response to identify request)
248 * 2 device aborted the command (refused to identify itself)
249 */
250
fa56d4cb 251int ide_dev_read_id(ide_drive_t *drive, u8 cmd, u16 *id, int irq_ctx)
1da177e4 252{
898ec223 253 ide_hwif_t *hwif = drive->hwif;
4c3032d8 254 struct ide_io_ports *io_ports = &hwif->io_ports;
374e042c 255 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
c47137a9 256 int use_altstatus = 0, rc;
1da177e4
LT
257 unsigned long timeout;
258 u8 s = 0, a = 0;
259
a182807a
BZ
260 /*
261 * Disable device IRQ. Otherwise we'll get spurious interrupts
262 * during the identify phase that the IRQ handler isn't expecting.
263 */
264 if (io_ports->ctl_addr)
ecf3a31d 265 tp_ops->write_devctl(hwif, ATA_NIEN | ATA_DEVCTL_OBS);
a182807a 266
1da177e4 267 /* take a deep breath */
fa56d4cb
BZ
268 if (irq_ctx)
269 mdelay(50);
270 else
271 msleep(50);
1da177e4 272
6636487e
BZ
273 if (io_ports->ctl_addr &&
274 (hwif->host_flags & IDE_HFLAG_BROKEN_ALTSTATUS) == 0) {
374e042c
BZ
275 a = tp_ops->read_altstatus(hwif);
276 s = tp_ops->read_status(hwif);
27f00e53 277 if ((a ^ s) & ~ATA_SENSE)
1da177e4 278 /* ancient Seagate drives, broken interfaces */
c47137a9
BZ
279 printk(KERN_INFO "%s: probing with STATUS(0x%02x) "
280 "instead of ALTSTATUS(0x%02x)\n",
281 drive->name, s, a);
282 else
1da177e4 283 /* use non-intrusive polling */
c47137a9
BZ
284 use_altstatus = 1;
285 }
1da177e4
LT
286
287 /* set features register for atapi
288 * identify command to be sure of reply
289 */
aaaade3f 290 if (cmd == ATA_CMD_ID_ATAPI) {
c9ff9e7b 291 struct ide_taskfile tf;
4e65837b 292
c9ff9e7b 293 memset(&tf, 0, sizeof(tf));
4e65837b 294 /* disable DMA & overlap */
c9ff9e7b 295 tp_ops->tf_load(drive, &tf, IDE_VALID_FEATURE);
4e65837b 296 }
1da177e4
LT
297
298 /* ask drive for ID */
374e042c 299 tp_ops->exec_command(hwif, cmd);
1da177e4 300
aaaade3f 301 timeout = ((cmd == ATA_CMD_ID_ATA) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
b163f46d 302
3a7d2484 303 /* wait for IRQ and ATA_DRQ */
fa56d4cb
BZ
304 if (irq_ctx) {
305 rc = __ide_wait_stat(drive, ATA_DRQ, BAD_R_STAT, timeout, &s);
306 if (rc)
307 return 1;
308 } else {
309 rc = ide_busy_sleep(drive, timeout, use_altstatus);
310 if (rc)
311 return 1;
312
313 msleep(50);
314 s = tp_ops->read_status(hwif);
315 }
c47137a9 316
3a7d2484 317 if (OK_STAT(s, ATA_DRQ, BAD_R_STAT)) {
1da177e4 318 /* drive returned ID */
2ebe1d9e 319 do_identify(drive, cmd, id);
1da177e4
LT
320 /* drive responded with ID */
321 rc = 0;
322 /* clear drive IRQ */
374e042c 323 (void)tp_ops->read_status(hwif);
1da177e4
LT
324 } else {
325 /* drive refused ID */
326 rc = 2;
327 }
328 return rc;
329}
330
28ee9bc5 331int ide_busy_sleep(ide_drive_t *drive, unsigned long timeout, int altstatus)
3a5015cc 332{
28ee9bc5 333 ide_hwif_t *hwif = drive->hwif;
3a5015cc
BZ
334 u8 stat;
335
b163f46d
BZ
336 timeout += jiffies;
337
3a5015cc 338 do {
b163f46d
BZ
339 msleep(50); /* give drive a breather */
340 stat = altstatus ? hwif->tp_ops->read_altstatus(hwif)
341 : hwif->tp_ops->read_status(hwif);
3a7d2484 342 if ((stat & ATA_BUSY) == 0)
3a5015cc
BZ
343 return 0;
344 } while (time_before(jiffies, timeout));
345
28ee9bc5
BZ
346 printk(KERN_ERR "%s: timeout in %s\n", drive->name, __func__);
347
b163f46d 348 return 1; /* drive timed-out */
3a5015cc 349}
1da177e4 350
1f2efb82
BZ
351static u8 ide_read_device(ide_drive_t *drive)
352{
3153c26b 353 struct ide_taskfile tf;
1f2efb82 354
3153c26b 355 drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_DEVICE);
1f2efb82 356
3153c26b 357 return tf.device;
1f2efb82
BZ
358}
359
1da177e4
LT
360/**
361 * do_probe - probe an IDE device
362 * @drive: drive to probe
363 * @cmd: command to use
364 *
365 * do_probe() has the difficult job of finding a drive if it exists,
366 * without getting hung up if it doesn't exist, without trampling on
367 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
368 *
369 * If a drive is "known" to exist (from CMOS or kernel parameters),
370 * but does not respond right away, the probe will "hang in there"
371 * for the maximum wait time (about 30 seconds), otherwise it will
372 * exit much more quickly.
373 *
374 * Returns: 0 device was identified
375 * 1 device timed-out (no response to identify request)
376 * 2 device aborted the command (refused to identify itself)
377 * 3 bad status from device (possible for ATAPI drives)
378 * 4 probe was not attempted because failure was obvious
379 */
380
381static int do_probe (ide_drive_t *drive, u8 cmd)
382{
898ec223 383 ide_hwif_t *hwif = drive->hwif;
374e042c 384 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
2ebe1d9e 385 u16 *id = drive->id;
57b55275 386 int rc;
97100fc8
BZ
387 u8 present = !!(drive->dev_flags & IDE_DFLAG_PRESENT), stat;
388
389 /* avoid waiting for inappropriate probes */
390 if (present && drive->media != ide_disk && cmd == ATA_CMD_ID_ATA)
391 return 4;
1da177e4 392
1da177e4 393#ifdef DEBUG
1b8ebad8 394 printk(KERN_INFO "probing for %s: present=%d, media=%d, probetype=%s\n",
97100fc8 395 drive->name, present, drive->media,
aaaade3f 396 (cmd == ATA_CMD_ID_ATA) ? "ATA" : "ATAPI");
1da177e4
LT
397#endif
398
399 /* needed for some systems
400 * (e.g. crw9624 as drive0 with disk as slave)
401 */
402 msleep(50);
fdd88f0a 403 tp_ops->dev_select(drive);
1da177e4 404 msleep(50);
1f2efb82 405
7f612f27 406 if (ide_read_device(drive) != drive->select && present == 0) {
123995b9 407 if (drive->dn & 1) {
1da177e4 408 /* exit with drive0 selected */
fdd88f0a 409 tp_ops->dev_select(hwif->devices[0]);
3a7d2484 410 /* allow ATA_BUSY to assert & clear */
1da177e4
LT
411 msleep(50);
412 }
413 /* no i/f present: mmm.. this should be a 4 -ml */
414 return 3;
415 }
416
374e042c 417 stat = tp_ops->read_status(hwif);
c47137a9 418
3a7d2484 419 if (OK_STAT(stat, ATA_DRDY, ATA_BUSY) ||
97100fc8 420 present || cmd == ATA_CMD_ID_ATAPI) {
fa56d4cb 421 rc = ide_dev_read_id(drive, cmd, id, 0);
2ebe1d9e 422 if (rc)
1da177e4 423 /* failed: try again */
fa56d4cb 424 rc = ide_dev_read_id(drive, cmd, id, 0);
57b55275 425
374e042c 426 stat = tp_ops->read_status(hwif);
57b55275 427
3a7d2484 428 if (stat == (ATA_BUSY | ATA_DRDY))
1da177e4
LT
429 return 4;
430
aaaade3f 431 if (rc == 1 && cmd == ATA_CMD_ID_ATAPI) {
57b55275
BZ
432 printk(KERN_ERR "%s: no response (status = 0x%02x), "
433 "resetting drive\n", drive->name, stat);
1da177e4 434 msleep(50);
fdd88f0a 435 tp_ops->dev_select(drive);
1da177e4 436 msleep(50);
aaaade3f 437 tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
28ee9bc5 438 (void)ide_busy_sleep(drive, WAIT_WORSTCASE, 0);
fa56d4cb 439 rc = ide_dev_read_id(drive, cmd, id, 0);
1da177e4 440 }
57b55275
BZ
441
442 /* ensure drive IRQ is clear */
374e042c 443 stat = tp_ops->read_status(hwif);
57b55275 444
1da177e4 445 if (rc == 1)
57b55275
BZ
446 printk(KERN_ERR "%s: no response (status = 0x%02x)\n",
447 drive->name, stat);
1da177e4
LT
448 } else {
449 /* not present or maybe ATAPI */
450 rc = 3;
451 }
123995b9 452 if (drive->dn & 1) {
1da177e4 453 /* exit with drive0 selected */
fdd88f0a 454 tp_ops->dev_select(hwif->devices[0]);
1da177e4
LT
455 msleep(50);
456 /* ensure drive irq is clear */
374e042c 457 (void)tp_ops->read_status(hwif);
1da177e4
LT
458 }
459 return rc;
460}
461
1da177e4
LT
462/**
463 * probe_for_drives - upper level drive probe
464 * @drive: drive to probe for
465 *
466 * probe_for_drive() tests for existence of a given drive using do_probe()
467 * and presents things to the user as needed.
468 *
469 * Returns: 0 no device was found
97100fc8
BZ
470 * 1 device was found
471 * (note: IDE_DFLAG_PRESENT might still be not set)
1da177e4 472 */
047140ae
BZ
473
474static u8 probe_for_drive(ide_drive_t *drive)
1da177e4 475{
4dde4492 476 char *m;
1bd4c1f4
BZ
477 int rc;
478 u8 cmd;
4dde4492 479
97100fc8
BZ
480 drive->dev_flags &= ~IDE_DFLAG_ID_READ;
481
4dde4492
BZ
482 m = (char *)&drive->id[ATA_ID_PROD];
483 strcpy(m, "UNKNOWN");
484
1da177e4 485 /* skip probing? */
97100fc8 486 if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0) {
1da177e4 487 /* if !(success||timed-out) */
1bd4c1f4
BZ
488 cmd = ATA_CMD_ID_ATA;
489 rc = do_probe(drive, cmd);
490 if (rc >= 2) {
1da177e4 491 /* look for ATAPI device */
1bd4c1f4
BZ
492 cmd = ATA_CMD_ID_ATAPI;
493 rc = do_probe(drive, cmd);
494 }
c36a7e98 495
97100fc8 496 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
5f4417a1 497 return 0;
4dde4492 498
1da177e4 499 /* identification failed? */
97100fc8 500 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
1da177e4
LT
501 if (drive->media == ide_disk) {
502 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
503 drive->name, drive->cyl,
504 drive->head, drive->sect);
505 } else if (drive->media == ide_cdrom) {
506 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
507 } else {
508 /* nuke it */
509 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
97100fc8 510 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
1da177e4 511 }
1bd4c1f4
BZ
512 } else {
513 if (cmd == ATA_CMD_ID_ATAPI)
514 ide_classify_atapi_dev(drive);
515 else
516 ide_classify_ata_dev(drive);
1da177e4 517 }
1da177e4 518 }
97100fc8
BZ
519
520 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
5f4417a1 521 return 0;
97100fc8 522
1da177e4 523 /* The drive wasn't being helpful. Add generic info only */
97100fc8 524 if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
1da177e4
LT
525 generic_id(drive);
526 return 1;
527 }
528
529 if (drive->media == ide_disk) {
530 ide_disk_init_chs(drive);
531 ide_disk_init_mult_count(drive);
532 }
533
69197ad7 534 return 1;
1da177e4
LT
535}
536
fc410698 537static void hwif_release_dev(struct device *dev)
1da177e4
LT
538{
539 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
540
f36d4024 541 complete(&hwif->gendev_rel_comp);
1da177e4
LT
542}
543
f74c9141 544static int ide_register_port(ide_hwif_t *hwif)
1da177e4 545{
349ae23f
RD
546 int ret;
547
1da177e4 548 /* register with global device tree */
02aa2a37 549 dev_set_name(&hwif->gendev, "%s", hwif->name);
fcb52077 550 dev_set_drvdata(&hwif->gendev, hwif);
bb54affa
AS
551 if (hwif->gendev.parent == NULL)
552 hwif->gendev.parent = hwif->dev;
1da177e4 553 hwif->gendev.release = hwif_release_dev;
96d40941 554
349ae23f 555 ret = device_register(&hwif->gendev);
f74c9141 556 if (ret < 0) {
349ae23f 557 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
eb63963a 558 __func__, ret);
f74c9141
BZ
559 goto out;
560 }
561
3ee074bf 562 hwif->portdev = device_create(ide_port_class, &hwif->gendev,
02aa2a37 563 MKDEV(0, 0), hwif, "%s", hwif->name);
f74c9141
BZ
564 if (IS_ERR(hwif->portdev)) {
565 ret = PTR_ERR(hwif->portdev);
566 device_unregister(&hwif->gendev);
567 }
f74c9141
BZ
568out:
569 return ret;
1da177e4
LT
570}
571
c860a8f2
BZ
572/**
573 * ide_port_wait_ready - wait for port to become ready
574 * @hwif: IDE port
575 *
576 * This is needed on some PPCs and a bunch of BIOS-less embedded
577 * platforms. Typical cases are:
578 *
579 * - The firmware hard reset the disk before booting the kernel,
580 * the drive is still doing it's poweron-reset sequence, that
581 * can take up to 30 seconds.
582 *
583 * - The firmware does nothing (or no firmware), the device is
584 * still in POST state (same as above actually).
585 *
586 * - Some CD/DVD/Writer combo drives tend to drive the bus during
587 * their reset sequence even when they are non-selected slave
588 * devices, thus preventing discovery of the main HD.
589 *
590 * Doing this wait-for-non-busy should not harm any existing
591 * configuration and fix some issues like the above.
592 *
593 * BenH.
594 *
595 * Returns 0 on success, error code (< 0) otherwise.
596 */
597
598static int ide_port_wait_ready(ide_hwif_t *hwif)
1da177e4 599{
fdd88f0a 600 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
2bd24a1c
BZ
601 ide_drive_t *drive;
602 int i, rc;
1da177e4
LT
603
604 printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
605
606 /* Let HW settle down a bit from whatever init state we
607 * come from */
608 mdelay(2);
609
610 /* Wait for BSY bit to go away, spec timeout is 30 seconds,
611 * I know of at least one disk who takes 31 seconds, I use 35
612 * here to be safe
613 */
614 rc = ide_wait_not_busy(hwif, 35000);
615 if (rc)
616 return rc;
617
618 /* Now make sure both master & slave are ready */
2bd24a1c 619 ide_port_for_each_dev(i, drive, hwif) {
8266105b 620 /* Ignore disks that we will not probe for later. */
97100fc8
BZ
621 if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0 ||
622 (drive->dev_flags & IDE_DFLAG_PRESENT)) {
fdd88f0a
SS
623 tp_ops->dev_select(drive);
624 tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
8266105b
JS
625 mdelay(2);
626 rc = ide_wait_not_busy(hwif, 35000);
627 if (rc)
628 goto out;
629 } else
630 printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
631 drive->name);
632 }
633out:
1da177e4 634 /* Exit function with master reselected (let's be sane) */
2bd24a1c 635 if (i)
fdd88f0a 636 tp_ops->dev_select(hwif->devices[0]);
8266105b 637
1da177e4
LT
638 return rc;
639}
640
641/**
642 * ide_undecoded_slave - look for bad CF adapters
4dde4492 643 * @dev1: slave device
1da177e4
LT
644 *
645 * Analyse the drives on the interface and attempt to decide if we
646 * have the same drive viewed twice. This occurs with crap CF adapters
647 * and PCMCIA sometimes.
648 */
649
4dde4492 650void ide_undecoded_slave(ide_drive_t *dev1)
1da177e4 651{
5e7f3a46 652 ide_drive_t *dev0 = dev1->hwif->devices[0];
1da177e4 653
97100fc8 654 if ((dev1->dn & 1) == 0 || (dev0->dev_flags & IDE_DFLAG_PRESENT) == 0)
1da177e4
LT
655 return;
656
657 /* If the models don't match they are not the same product */
4dde4492
BZ
658 if (strcmp((char *)&dev0->id[ATA_ID_PROD],
659 (char *)&dev1->id[ATA_ID_PROD]))
1da177e4
LT
660 return;
661
662 /* Serial numbers do not match */
4dde4492
BZ
663 if (strncmp((char *)&dev0->id[ATA_ID_SERNO],
664 (char *)&dev1->id[ATA_ID_SERNO], ATA_ID_SERNO_LEN))
1da177e4
LT
665 return;
666
667 /* No serial number, thankfully very rare for CF */
4dde4492 668 if (*(char *)&dev0->id[ATA_ID_SERNO] == 0)
1da177e4
LT
669 return;
670
671 /* Appears to be an IDE flash adapter with decode bugs */
672 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
673
97100fc8 674 dev1->dev_flags &= ~IDE_DFLAG_PRESENT;
1da177e4
LT
675}
676
677EXPORT_SYMBOL_GPL(ide_undecoded_slave);
678
9d501529 679static int ide_probe_port(ide_hwif_t *hwif)
1da177e4 680{
2bd24a1c 681 ide_drive_t *drive;
1da177e4 682 unsigned int irqd;
2bd24a1c 683 int i, rc = -ENODEV;
a14dc574
BZ
684
685 BUG_ON(hwif->present);
1da177e4 686
5e7f3a46
BZ
687 if ((hwif->devices[0]->dev_flags & IDE_DFLAG_NOPROBE) &&
688 (hwif->devices[1]->dev_flags & IDE_DFLAG_NOPROBE))
9d501529 689 return -EACCES;
1da177e4 690
1da177e4
LT
691 /*
692 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
693 * we'll install our IRQ driver much later...
694 */
695 irqd = hwif->irq;
696 if (irqd)
697 disable_irq(hwif->irq);
698
9ce41aed
DM
699 if (ide_port_wait_ready(hwif) == -EBUSY)
700 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
1da177e4
LT
701
702 /*
f367bed0
BZ
703 * Second drive should only exist if first drive was found,
704 * but a lot of cdrom drives are configured as single slaves.
1da177e4 705 */
2bd24a1c 706 ide_port_for_each_dev(i, drive, hwif) {
1da177e4 707 (void) probe_for_drive(drive);
97100fc8 708 if (drive->dev_flags & IDE_DFLAG_PRESENT)
a14dc574 709 rc = 0;
1da177e4 710 }
9ce41aed 711
1da177e4
LT
712 /*
713 * Use cached IRQ number. It might be (and is...) changed by probe
714 * code above
715 */
716 if (irqd)
717 enable_irq(irqd);
718
a14dc574 719 return rc;
e84e7ea7
BZ
720}
721
722static void ide_port_tune_devices(ide_hwif_t *hwif)
723{
ac95beed 724 const struct ide_port_ops *port_ops = hwif->port_ops;
2bd24a1c
BZ
725 ide_drive_t *drive;
726 int i;
f01393e4 727
7ed5b157 728 ide_port_for_each_present_dev(i, drive, hwif) {
8bc1e5aa
BZ
729 ide_check_nien_quirk_list(drive);
730
7ed5b157
BZ
731 if (port_ops && port_ops->quirkproc)
732 port_ops->quirkproc(drive);
f01393e4 733 }
0380dad4 734
7ed5b157
BZ
735 ide_port_for_each_present_dev(i, drive, hwif) {
736 ide_set_max_pio(drive);
1da177e4 737
7ed5b157 738 drive->dev_flags |= IDE_DFLAG_NICE1;
1da177e4 739
7ed5b157
BZ
740 if (hwif->dma_ops)
741 ide_set_dma(drive);
1da177e4
LT
742 }
743}
744
ca18d6f7 745static void ide_initialize_rq(struct request *rq)
82ed4db4
CH
746{
747 struct ide_request *req = blk_mq_rq_to_pdu(rq);
748
d16a6766 749 req->special = NULL;
c8d9cf22 750 scsi_req_init(&req->sreq);
82ed4db4 751 req->sreq.sense = req->sense;
82ed4db4
CH
752}
753
60033520
JA
754static const struct blk_mq_ops ide_mq_ops = {
755 .queue_rq = ide_queue_rq,
756 .initialize_rq_fn = ide_initialize_rq,
757};
758
1da177e4
LT
759/*
760 * init request queue
761 */
762static int ide_init_queue(ide_drive_t *drive)
763{
165125e1 764 struct request_queue *q;
898ec223 765 ide_hwif_t *hwif = drive->hwif;
1da177e4
LT
766 int max_sectors = 256;
767 int max_sg_entries = PRD_ENTRIES;
60033520 768 struct blk_mq_tag_set *set;
1da177e4
LT
769
770 /*
771 * Our default set up assumes the normal IDE case,
772 * that is 64K segmenting, standard PRD setup
773 * and LBA28. Some drivers then impose their own
774 * limits and LBA48 we could raise it but as yet
775 * do not.
776 */
60033520
JA
777
778 set = &drive->tag_set;
779 set->ops = &ide_mq_ops;
780 set->nr_hw_queues = 1;
781 set->queue_depth = 32;
782 set->reserved_tags = 1;
783 set->cmd_size = sizeof(struct ide_request);
784 set->numa_node = hwif_to_node(hwif);
785 set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING;
786 if (blk_mq_alloc_tag_set(set))
1da177e4
LT
787 return 1;
788
60033520
JA
789 q = blk_mq_init_queue(set);
790 if (IS_ERR(q)) {
791 blk_mq_free_tag_set(set);
82ed4db4
CH
792 return 1;
793 }
794
60033520
JA
795 blk_queue_flag_set(QUEUE_FLAG_SCSI_PASSTHROUGH, q);
796
1da177e4
LT
797 q->queuedata = drive;
798 blk_queue_segment_boundary(q, 0xffff);
799
1da177e4
LT
800 if (hwif->rqsize < max_sectors)
801 max_sectors = hwif->rqsize;
086fa5ff 802 blk_queue_max_hw_sectors(q, max_sectors);
1da177e4
LT
803
804#ifdef CONFIG_PCI
805 /* When we have an IOMMU, we may have a problem where pci_map_sg()
806 * creates segments that don't completely match our boundary
807 * requirements and thus need to be broken up again. Because it
808 * doesn't align properly either, we may actually have to break up
809 * to more segments than what was we got in the first place, a max
810 * worst case is twice as many.
811 * This will be fixed once we teach pci_map_sg() about our boundary
812 * requirements, hopefully soon. *FIXME*
813 */
164c3d09 814 max_sg_entries >>= 1;
1da177e4
LT
815#endif /* CONFIG_PCI */
816
8a78362c 817 blk_queue_max_segments(q, max_sg_entries);
1da177e4
LT
818
819 /* assign drive queue */
820 drive->queue = q;
821
1da177e4
LT
822 return 0;
823}
824
b40d1b88
BZ
825static DEFINE_MUTEX(ide_cfg_mtx);
826
d5bc6592
BZ
827/*
828 * For any present drive:
829 * - allocate the block device queue
d5bc6592 830 */
e415e495 831static int ide_port_setup_devices(ide_hwif_t *hwif)
d5bc6592 832{
2bd24a1c 833 ide_drive_t *drive;
e415e495 834 int i, j = 0;
d5bc6592 835
26042d05 836 mutex_lock(&ide_cfg_mtx);
7ed5b157 837 ide_port_for_each_present_dev(i, drive, hwif) {
d5bc6592
BZ
838 if (ide_init_queue(drive)) {
839 printk(KERN_ERR "ide: failed to init %s\n",
840 drive->name);
e415e495 841 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
d5bc6592
BZ
842 continue;
843 }
844
e415e495 845 j++;
d5bc6592 846 }
26042d05 847 mutex_unlock(&ide_cfg_mtx);
e415e495
EO
848
849 return j;
d5bc6592
BZ
850}
851
ffc36c76
BZ
852static void ide_host_enable_irqs(struct ide_host *host)
853{
854 ide_hwif_t *hwif;
855 int i;
856
857 ide_host_for_each_port(i, hwif, host) {
858 if (hwif == NULL)
859 continue;
860
861 /* clear any pending IRQs */
862 hwif->tp_ops->read_status(hwif);
863
864 /* unmask IRQs */
865 if (hwif->io_ports.ctl_addr)
866 hwif->tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
867 }
868}
869
1da177e4 870/*
b65fac32 871 * This routine sets up the IRQ for an IDE interface.
1da177e4
LT
872 */
873static int init_irq (ide_hwif_t *hwif)
874{
4c3032d8 875 struct ide_io_ports *io_ports = &hwif->io_ports;
255115fb
BZ
876 struct ide_host *host = hwif->host;
877 irq_handler_t irq_handler = host->irq_handler;
878 int sa = host->irq_flags;
1da177e4 879
849d7130
SG
880 if (irq_handler == NULL)
881 irq_handler = ide_intr;
882
a259d532
MS
883 if (!host->get_lock)
884 if (request_irq(hwif->irq, irq_handler, sa, hwif->name, hwif))
885 goto out_up;
1da177e4 886
7b892806 887#if !defined(__mc68000__)
1b8ebad8 888 printk(KERN_INFO "%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
4c3032d8
BZ
889 io_ports->data_addr, io_ports->status_addr,
890 io_ports->ctl_addr, hwif->irq);
1da177e4 891#else
1b8ebad8 892 printk(KERN_INFO "%s at 0x%08lx on irq %d", hwif->name,
4c3032d8 893 io_ports->data_addr, hwif->irq);
7b892806 894#endif /* __mc68000__ */
b65fac32
BZ
895 if (hwif->host->host_flags & IDE_HFLAG_SERIALIZE)
896 printk(KERN_CONT " (serialized)");
1b8ebad8 897 printk(KERN_CONT "\n");
d5bc6592 898
1da177e4 899 return 0;
1da177e4 900out_up:
1da177e4
LT
901 return 1;
902}
903
904static int ata_lock(dev_t dev, void *data)
905{
906 /* FIXME: we want to pin hwif down */
907 return 0;
908}
909
910static struct kobject *ata_probe(dev_t dev, int *part, void *data)
911{
912 ide_hwif_t *hwif = data;
913 int unit = *part >> PARTN_BITS;
5e7f3a46 914 ide_drive_t *drive = hwif->devices[unit];
97100fc8
BZ
915
916 if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
1da177e4
LT
917 return NULL;
918
919 if (drive->media == ide_disk)
920 request_module("ide-disk");
1da177e4
LT
921 if (drive->media == ide_cdrom || drive->media == ide_optical)
922 request_module("ide-cd");
923 if (drive->media == ide_tape)
924 request_module("ide-tape");
925 if (drive->media == ide_floppy)
926 request_module("ide-floppy");
927
928 return NULL;
929}
930
931static struct kobject *exact_match(dev_t dev, int *part, void *data)
932{
933 struct gendisk *p = data;
934 *part &= (1 << PARTN_BITS) - 1;
ed9e1982 935 return &disk_to_dev(p)->kobj;
1da177e4
LT
936}
937
938static int exact_lock(dev_t dev, void *data)
939{
940 struct gendisk *p = data;
941
3079c22e 942 if (!get_disk_and_module(p))
1da177e4
LT
943 return -1;
944 return 0;
945}
946
947void ide_register_region(struct gendisk *disk)
948{
949 blk_register_region(MKDEV(disk->major, disk->first_minor),
950 disk->minors, NULL, exact_match, exact_lock, disk);
951}
952
953EXPORT_SYMBOL_GPL(ide_register_region);
954
955void ide_unregister_region(struct gendisk *disk)
956{
957 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
958 disk->minors);
959}
960
961EXPORT_SYMBOL_GPL(ide_unregister_region);
962
963void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
964{
965 ide_hwif_t *hwif = drive->hwif;
7f612f27 966 unsigned int unit = drive->dn & 1;
1da177e4
LT
967
968 disk->major = hwif->major;
969 disk->first_minor = unit << PARTN_BITS;
970 sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
971 disk->queue = drive->queue;
972}
973
974EXPORT_SYMBOL_GPL(ide_init_disk);
975
976static void drive_release_dev (struct device *dev)
977{
978 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
979
5b0c4b30
BZ
980 ide_proc_unregister_device(drive);
981
60033520
JA
982 if (drive->sense_rq)
983 blk_mq_free_request(drive->sense_rq);
984
b5479167
BZ
985 blk_cleanup_queue(drive->queue);
986 drive->queue = NULL;
60033520 987 blk_mq_free_tag_set(&drive->tag_set);
b5479167 988
97100fc8 989 drive->dev_flags &= ~IDE_DFLAG_PRESENT;
8604affd 990
f36d4024 991 complete(&drive->gendev_rel_comp);
1da177e4
LT
992}
993
1da177e4
LT
994static int hwif_init(ide_hwif_t *hwif)
995{
1da177e4 996 if (!hwif->irq) {
8b07ed26
BZ
997 printk(KERN_ERR "%s: disabled, no IRQ\n", hwif->name);
998 return 0;
1da177e4 999 }
1da177e4 1000
1da177e4
LT
1001 if (register_blkdev(hwif->major, hwif->name))
1002 return 0;
1003
1004 if (!hwif->sg_max_nents)
1005 hwif->sg_max_nents = PRD_ENTRIES;
1006
6da2ec56
KC
1007 hwif->sg_table = kmalloc_array(hwif->sg_max_nents,
1008 sizeof(struct scatterlist),
1009 GFP_KERNEL);
1da177e4
LT
1010 if (!hwif->sg_table) {
1011 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1012 goto out;
1013 }
45711f1a
JA
1014
1015 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
1da177e4 1016
1da177e4 1017 if (init_irq(hwif)) {
8b07ed26
BZ
1018 printk(KERN_ERR "%s: disabled, unable to get IRQ %d\n",
1019 hwif->name, hwif->irq);
1da177e4
LT
1020 goto out;
1021 }
1da177e4 1022
3a4e7c96
BZ
1023 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1024 THIS_MODULE, ata_probe, ata_lock, hwif);
1da177e4
LT
1025 return 1;
1026
1027out:
1028 unregister_blkdev(hwif->major, hwif->name);
1029 return 0;
1030}
1031
9601a607
BZ
1032static void hwif_register_devices(ide_hwif_t *hwif)
1033{
2bd24a1c 1034 ide_drive_t *drive;
9601a607
BZ
1035 unsigned int i;
1036
7ed5b157 1037 ide_port_for_each_present_dev(i, drive, hwif) {
c5d70cc7
BZ
1038 struct device *dev = &drive->gendev;
1039 int ret;
9601a607 1040
dc09c784 1041 dev_set_name(dev, "%u.%u", hwif->index, i);
fcb52077 1042 dev_set_drvdata(dev, drive);
c5d70cc7
BZ
1043 dev->parent = &hwif->gendev;
1044 dev->bus = &ide_bus_type;
c5d70cc7
BZ
1045 dev->release = drive_release_dev;
1046
1047 ret = device_register(dev);
1048 if (ret < 0)
1049 printk(KERN_WARNING "IDE: %s: device_register error: "
1050 "%d\n", __func__, ret);
9601a607
BZ
1051 }
1052}
1053
7704ca2a
BZ
1054static void ide_port_init_devices(ide_hwif_t *hwif)
1055{
ac95beed 1056 const struct ide_port_ops *port_ops = hwif->port_ops;
2bd24a1c 1057 ide_drive_t *drive;
7704ca2a
BZ
1058 int i;
1059
2bd24a1c 1060 ide_port_for_each_dev(i, drive, hwif) {
123995b9
BZ
1061 drive->dn = i + hwif->channel * 2;
1062
7704ca2a
BZ
1063 if (hwif->host_flags & IDE_HFLAG_IO_32BIT)
1064 drive->io_32bit = 1;
7610c4f5
BZ
1065 if (hwif->host_flags & IDE_HFLAG_NO_IO_32BIT)
1066 drive->dev_flags |= IDE_DFLAG_NO_IO_32BIT;
7704ca2a 1067 if (hwif->host_flags & IDE_HFLAG_UNMASK_IRQS)
97100fc8 1068 drive->dev_flags |= IDE_DFLAG_UNMASK;
807b90d0 1069 if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS)
97100fc8 1070 drive->dev_flags |= IDE_DFLAG_NO_UNMASK;
1f2cf8b0 1071
d2d4e780
BZ
1072 drive->pio_mode = XFER_PIO_0;
1073
e6d95bd1
BZ
1074 if (port_ops && port_ops->init_dev)
1075 port_ops->init_dev(drive);
1076 }
7704ca2a
BZ
1077}
1078
c413b9b9
BZ
1079static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
1080 const struct ide_port_info *d)
8447d9d5 1081{
b7691646 1082 hwif->channel = port;
c413b9b9 1083
29e52cf7 1084 hwif->chipset = d->chipset ? d->chipset : ide_pci;
c413b9b9
BZ
1085
1086 if (d->init_iops)
1087 d->init_iops(hwif);
1088
23f8e4bf
BZ
1089 /* ->host_flags may be set by ->init_iops (or even earlier...) */
1090 hwif->host_flags |= d->host_flags;
c413b9b9
BZ
1091 hwif->pio_mask = d->pio_mask;
1092
374e042c
BZ
1093 if (d->tp_ops)
1094 hwif->tp_ops = d->tp_ops;
1095
ac95beed 1096 /* ->set_pio_mode for DTC2278 is currently limited to port 0 */
2787cb8a 1097 if ((hwif->host_flags & IDE_HFLAG_DTC2278) == 0 || hwif->channel == 0)
ac95beed
BZ
1098 hwif->port_ops = d->port_ops;
1099
c413b9b9
BZ
1100 hwif->swdma_mask = d->swdma_mask;
1101 hwif->mwdma_mask = d->mwdma_mask;
1102 hwif->ultra_mask = d->udma_mask;
1103
b123f56e
BZ
1104 if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
1105 int rc;
1106
592b5315
SS
1107 hwif->dma_ops = d->dma_ops;
1108
b123f56e
BZ
1109 if (d->init_dma)
1110 rc = d->init_dma(hwif, d);
1111 else
1112 rc = ide_hwif_setup_dma(hwif, d);
1113
1114 if (rc < 0) {
1115 printk(KERN_INFO "%s: DMA disabled\n", hwif->name);
592b5315
SS
1116
1117 hwif->dma_ops = NULL;
ebb00fb5 1118 hwif->dma_base = 0;
b123f56e
BZ
1119 hwif->swdma_mask = 0;
1120 hwif->mwdma_mask = 0;
1121 hwif->ultra_mask = 0;
592b5315 1122 }
b123f56e 1123 }
c413b9b9 1124
1024c5f4 1125 if ((d->host_flags & IDE_HFLAG_SERIALIZE) ||
702c026b
BZ
1126 ((d->host_flags & IDE_HFLAG_SERIALIZE_DMA) && hwif->dma_base))
1127 hwif->host->host_flags |= IDE_HFLAG_SERIALIZE;
1024c5f4 1128
6b492496
BZ
1129 if (d->max_sectors)
1130 hwif->rqsize = d->max_sectors;
70775e9c
BZ
1131 else {
1132 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
1133 (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
1134 hwif->rqsize = 256;
1135 else
1136 hwif->rqsize = 65536;
1137 }
c413b9b9
BZ
1138
1139 /* call chipset specific routine for each enabled port */
1140 if (d->init_hwif)
1141 d->init_hwif(hwif);
c7f6f21a 1142}
bfa14b42 1143
c7f6f21a
BZ
1144static void ide_port_cable_detect(ide_hwif_t *hwif)
1145{
ac95beed
BZ
1146 const struct ide_port_ops *port_ops = hwif->port_ops;
1147
1148 if (port_ops && port_ops->cable_detect && (hwif->ultra_mask & 0x78)) {
bfa14b42 1149 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
ac95beed 1150 hwif->cbl = port_ops->cable_detect(hwif);
bfa14b42 1151 }
c413b9b9
BZ
1152}
1153
60033520
JA
1154/*
1155 * Deferred request list insertion handler
1156 */
1157static void drive_rq_insert_work(struct work_struct *work)
1158{
1159 ide_drive_t *drive = container_of(work, ide_drive_t, rq_work);
1160 ide_hwif_t *hwif = drive->hwif;
1161 struct request *rq;
9a6d5488 1162 blk_status_t ret;
60033520
JA
1163 LIST_HEAD(list);
1164
9a6d5488 1165 blk_mq_quiesce_queue(drive->queue);
60033520 1166
9a6d5488
JA
1167 ret = BLK_STS_OK;
1168 spin_lock_irq(&hwif->lock);
1169 while (!list_empty(&drive->rq_list)) {
1170 rq = list_first_entry(&drive->rq_list, struct request, queuelist);
60033520 1171 list_del_init(&rq->queuelist);
9a6d5488
JA
1172
1173 spin_unlock_irq(&hwif->lock);
1174 ret = ide_issue_rq(drive, rq, true);
1175 spin_lock_irq(&hwif->lock);
60033520 1176 }
9a6d5488
JA
1177 spin_unlock_irq(&hwif->lock);
1178
1179 blk_mq_unquiesce_queue(drive->queue);
1180
1181 if (ret != BLK_STS_OK)
1182 kblockd_schedule_work(&drive->rq_work);
60033520
JA
1183}
1184
b40d1b88
BZ
1185static const u8 ide_hwif_to_major[] =
1186 { IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR, IDE4_MAJOR,
1187 IDE5_MAJOR, IDE6_MAJOR, IDE7_MAJOR, IDE8_MAJOR, IDE9_MAJOR };
1188
1189static void ide_port_init_devices_data(ide_hwif_t *hwif)
1190{
2bd24a1c
BZ
1191 ide_drive_t *drive;
1192 int i;
b40d1b88 1193
2bd24a1c
BZ
1194 ide_port_for_each_dev(i, drive, hwif) {
1195 u8 j = (hwif->index * MAX_DRIVES) + i;
5f4417a1 1196 u16 *saved_id = drive->id;
b40d1b88
BZ
1197
1198 memset(drive, 0, sizeof(*drive));
5f4417a1
HD
1199 memset(saved_id, 0, SECTOR_SIZE);
1200 drive->id = saved_id;
b40d1b88
BZ
1201
1202 drive->media = ide_disk;
2bd24a1c 1203 drive->select = (i << 4) | ATA_DEVICE_OBS;
b40d1b88
BZ
1204 drive->hwif = hwif;
1205 drive->ready_stat = ATA_DRDY;
1206 drive->bad_wstat = BAD_W_STAT;
ca1b96e0
BZ
1207 drive->special_flags = IDE_SFLAG_RECALIBRATE |
1208 IDE_SFLAG_SET_GEOMETRY;
b40d1b88
BZ
1209 drive->name[0] = 'h';
1210 drive->name[1] = 'd';
1211 drive->name[2] = 'a' + j;
1212 drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
1213
1214 INIT_LIST_HEAD(&drive->list);
1215 init_completion(&drive->gendev_rel_comp);
60033520
JA
1216
1217 INIT_WORK(&drive->rq_work, drive_rq_insert_work);
1218 INIT_LIST_HEAD(&drive->rq_list);
b40d1b88
BZ
1219 }
1220}
1221
1222static void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
1223{
b40d1b88
BZ
1224 /* fill in any non-zero initial values */
1225 hwif->index = index;
1226 hwif->major = ide_hwif_to_major[index];
1227
1228 hwif->name[0] = 'i';
1229 hwif->name[1] = 'd';
1230 hwif->name[2] = 'e';
1231 hwif->name[3] = '0' + index;
1232
7362951b
BZ
1233 spin_lock_init(&hwif->lock);
1234
10738ba8 1235 timer_setup(&hwif->timer, ide_timer_expiry, 0);
7362951b 1236
b40d1b88
BZ
1237 init_completion(&hwif->gendev_rel_comp);
1238
1239 hwif->tp_ops = &default_tp_ops;
1240
1241 ide_port_init_devices_data(hwif);
1242}
1243
9f36d314 1244static void ide_init_port_hw(ide_hwif_t *hwif, struct ide_hw *hw)
b40d1b88
BZ
1245{
1246 memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports));
1247 hwif->irq = hw->irq;
b40d1b88
BZ
1248 hwif->dev = hw->dev;
1249 hwif->gendev.parent = hw->parent ? hw->parent : hw->dev;
b40d1b88
BZ
1250 hwif->config_data = hw->config;
1251}
1252
8cdf3100
BZ
1253static unsigned int ide_indexes;
1254
fe80b937 1255/**
8cdf3100 1256 * ide_find_port_slot - find free port slot
fe80b937
BZ
1257 * @d: IDE port info
1258 *
8cdf3100 1259 * Return the new port slot index or -ENOENT if we are out of free slots.
fe80b937
BZ
1260 */
1261
8cdf3100 1262static int ide_find_port_slot(const struct ide_port_info *d)
fe80b937 1263{
8cdf3100 1264 int idx = -ENOENT;
fe80b937 1265 u8 bootable = (d && (d->host_flags & IDE_HFLAG_NON_BOOTABLE)) ? 0 : 1;
a419aef8 1266 u8 i = (d && (d->host_flags & IDE_HFLAG_QD_2ND_PORT)) ? 1 : 0;
fe80b937
BZ
1267
1268 /*
1269 * Claim an unassigned slot.
1270 *
1271 * Give preference to claiming other slots before claiming ide0/ide1,
1272 * just in case there's another interface yet-to-be-scanned
1273 * which uses ports 0x1f0/0x170 (the ide0/ide1 defaults).
1274 *
1275 * Unless there is a bootable card that does not use the standard
1276 * ports 0x1f0/0x170 (the ide0/ide1 defaults).
1277 */
8cdf3100 1278 mutex_lock(&ide_cfg_mtx);
75d21fff
BZ
1279 if (bootable) {
1280 if ((ide_indexes | i) != (1 << MAX_HWIFS) - 1)
1281 idx = ffz(ide_indexes | i);
fe80b937 1282 } else {
75d21fff
BZ
1283 if ((ide_indexes | 3) != (1 << MAX_HWIFS) - 1)
1284 idx = ffz(ide_indexes | 3);
1285 else if ((ide_indexes & 3) != 3)
1286 idx = ffz(ide_indexes);
fe80b937 1287 }
8cdf3100
BZ
1288 if (idx >= 0)
1289 ide_indexes |= (1 << idx);
1290 mutex_unlock(&ide_cfg_mtx);
fe80b937 1291
8cdf3100
BZ
1292 return idx;
1293}
256c5f8e 1294
8cdf3100
BZ
1295static void ide_free_port_slot(int idx)
1296{
1297 mutex_lock(&ide_cfg_mtx);
1298 ide_indexes &= ~(1 << idx);
1299 mutex_unlock(&ide_cfg_mtx);
fe80b937 1300}
fe80b937 1301
5e7f3a46
BZ
1302static void ide_port_free_devices(ide_hwif_t *hwif)
1303{
2bd24a1c 1304 ide_drive_t *drive;
5e7f3a46
BZ
1305 int i;
1306
5f4417a1
HD
1307 ide_port_for_each_dev(i, drive, hwif) {
1308 kfree(drive->id);
2bd24a1c 1309 kfree(drive);
5f4417a1 1310 }
5e7f3a46
BZ
1311}
1312
1313static int ide_port_alloc_devices(ide_hwif_t *hwif, int node)
1314{
82ed4db4 1315 ide_drive_t *drive;
5e7f3a46
BZ
1316 int i;
1317
1318 for (i = 0; i < MAX_DRIVES; i++) {
5e7f3a46
BZ
1319 drive = kzalloc_node(sizeof(*drive), GFP_KERNEL, node);
1320 if (drive == NULL)
1321 goto out_nomem;
1322
5f4417a1
HD
1323 /*
1324 * In order to keep things simple we have an id
1325 * block for all drives at all times. If the device
1326 * is pre ATA or refuses ATA/ATAPI identify we
1327 * will add faked data to this.
1328 *
1329 * Also note that 0 everywhere means "can't do X"
1330 */
1331 drive->id = kzalloc_node(SECTOR_SIZE, GFP_KERNEL, node);
1332 if (drive->id == NULL)
82ed4db4
CH
1333 goto out_free_drive;
1334
5e7f3a46
BZ
1335 hwif->devices[i] = drive;
1336 }
1337 return 0;
1338
82ed4db4
CH
1339out_free_drive:
1340 kfree(drive);
5e7f3a46
BZ
1341out_nomem:
1342 ide_port_free_devices(hwif);
1343 return -ENOMEM;
1344}
1345
9f36d314
BZ
1346struct ide_host *ide_host_alloc(const struct ide_port_info *d,
1347 struct ide_hw **hws, unsigned int n_ports)
48c3c107
BZ
1348{
1349 struct ide_host *host;
a32296f9
BZ
1350 struct device *dev = hws[0] ? hws[0]->dev : NULL;
1351 int node = dev ? dev_to_node(dev) : -1;
48c3c107
BZ
1352 int i;
1353
a32296f9 1354 host = kzalloc_node(sizeof(*host), GFP_KERNEL, node);
48c3c107
BZ
1355 if (host == NULL)
1356 return NULL;
1357
dca39830 1358 for (i = 0; i < n_ports; i++) {
48c3c107 1359 ide_hwif_t *hwif;
8cdf3100 1360 int idx;
48c3c107
BZ
1361
1362 if (hws[i] == NULL)
1363 continue;
1364
a32296f9 1365 hwif = kzalloc_node(sizeof(*hwif), GFP_KERNEL, node);
18de1017
BZ
1366 if (hwif == NULL)
1367 continue;
1368
5e7f3a46
BZ
1369 if (ide_port_alloc_devices(hwif, node) < 0) {
1370 kfree(hwif);
1371 continue;
1372 }
1373
8cdf3100
BZ
1374 idx = ide_find_port_slot(d);
1375 if (idx < 0) {
1376 printk(KERN_ERR "%s: no free slot for interface\n",
1377 d ? d->name : "ide");
5f4417a1 1378 ide_port_free_devices(hwif);
18de1017 1379 kfree(hwif);
8cdf3100 1380 continue;
48c3c107 1381 }
8cdf3100 1382
8cdf3100
BZ
1383 ide_init_port_data(hwif, idx);
1384
08da591e
BZ
1385 hwif->host = host;
1386
8cdf3100
BZ
1387 host->ports[i] = hwif;
1388 host->n_ports++;
48c3c107
BZ
1389 }
1390
1391 if (host->n_ports == 0) {
1392 kfree(host);
1393 return NULL;
1394 }
1395
a32296f9 1396 host->dev[0] = dev;
6cdf6eb3 1397
feb22b7f
BZ
1398 if (d) {
1399 host->init_chipset = d->init_chipset;
e354c1d8
BZ
1400 host->get_lock = d->get_lock;
1401 host->release_lock = d->release_lock;
ef0b0427 1402 host->host_flags = d->host_flags;
aa07573b 1403 host->irq_flags = d->irq_flags;
feb22b7f 1404 }
ef0b0427 1405
48c3c107
BZ
1406 return host;
1407}
48c3c107
BZ
1408EXPORT_SYMBOL_GPL(ide_host_alloc);
1409
9a100f4b
BZ
1410static void ide_port_free(ide_hwif_t *hwif)
1411{
1412 ide_port_free_devices(hwif);
1413 ide_free_port_slot(hwif->index);
1414 kfree(hwif);
1415}
1416
1417static void ide_disable_port(ide_hwif_t *hwif)
1418{
1419 struct ide_host *host = hwif->host;
1420 int i;
1421
1422 printk(KERN_INFO "%s: disabling port\n", hwif->name);
1423
1424 for (i = 0; i < MAX_HOST_PORTS; i++) {
1425 if (host->ports[i] == hwif) {
1426 host->ports[i] = NULL;
1427 host->n_ports--;
1428 }
1429 }
1430
1431 ide_port_free(hwif);
1432}
1433
48c3c107 1434int ide_host_register(struct ide_host *host, const struct ide_port_info *d,
9f36d314 1435 struct ide_hw **hws)
c413b9b9
BZ
1436{
1437 ide_hwif_t *hwif, *mate = NULL;
e0d00207 1438 int i, j = 0;
8447d9d5 1439
2bd24a1c 1440 ide_host_for_each_port(i, hwif, host) {
e0d00207 1441 if (hwif == NULL) {
c413b9b9
BZ
1442 mate = NULL;
1443 continue;
1444 }
1445
c97c6aca 1446 ide_init_port_hw(hwif, hws[i]);
9fd91d95
BZ
1447 ide_port_apply_params(hwif);
1448
0a6e49e9
BZ
1449 if ((i & 1) && mate) {
1450 hwif->mate = mate;
1451 mate->mate = hwif;
123995b9 1452 }
c413b9b9 1453
0a6e49e9
BZ
1454 mate = (i & 1) ? NULL : hwif;
1455
1456 ide_init_port(hwif, i & 1, d);
1457 ide_port_cable_detect(hwif);
5880b5de
BZ
1458
1459 hwif->port_flags |= IDE_PFLAG_PROBING;
1460
7704ca2a 1461 ide_port_init_devices(hwif);
c413b9b9
BZ
1462 }
1463
2bd24a1c 1464 ide_host_for_each_port(i, hwif, host) {
e0d00207
BZ
1465 if (hwif == NULL)
1466 continue;
139ddfca 1467
eb716beb
BZ
1468 if (ide_probe_port(hwif) == 0)
1469 hwif->present = 1;
a14dc574 1470
5880b5de
BZ
1471 hwif->port_flags &= ~IDE_PFLAG_PROBING;
1472
c094ea07
BZ
1473 if ((hwif->host_flags & IDE_HFLAG_4DRIVES) == 0 ||
1474 hwif->mate == NULL || hwif->mate->present == 0) {
9a100f4b
BZ
1475 if (ide_register_port(hwif)) {
1476 ide_disable_port(hwif);
1477 continue;
1478 }
1479 }
a14dc574 1480
eb716beb
BZ
1481 if (hwif->present)
1482 ide_port_tune_devices(hwif);
2e13093a 1483 }
ba6560aa 1484
ffc36c76
BZ
1485 ide_host_enable_irqs(host);
1486
2bd24a1c 1487 ide_host_for_each_port(i, hwif, host) {
e0d00207
BZ
1488 if (hwif == NULL)
1489 continue;
ba6560aa
BZ
1490
1491 if (hwif_init(hwif) == 0) {
1492 printk(KERN_INFO "%s: failed to initialize IDE "
1493 "interface\n", hwif->name);
d410a641 1494 device_unregister(hwif->portdev);
51d6ac70
IC
1495 device_unregister(&hwif->gendev);
1496 ide_disable_port(hwif);
ba6560aa
BZ
1497 continue;
1498 }
decdc3f0 1499
eb716beb 1500 if (hwif->present)
e415e495
EO
1501 if (ide_port_setup_devices(hwif) == 0) {
1502 hwif->present = 0;
1503 continue;
1504 }
1505
1506 j++;
26042d05 1507
8b803bd1 1508 ide_acpi_init_port(hwif);
eb716beb
BZ
1509
1510 if (hwif->present)
1511 ide_acpi_port_init_devices(hwif);
2e13093a
BZ
1512 }
1513
2bd24a1c 1514 ide_host_for_each_port(i, hwif, host) {
e0d00207
BZ
1515 if (hwif == NULL)
1516 continue;
327617e1 1517
eb716beb
BZ
1518 ide_sysfs_register_port(hwif);
1519 ide_proc_register_port(hwif);
1520
dbee0322 1521 if (hwif->present) {
d9270a3f 1522 ide_proc_port_register_devices(hwif);
dbee0322
WS
1523 hwif_register_devices(hwif);
1524 }
8447d9d5
BZ
1525 }
1526
e0d00207 1527 return j ? 0 : -1;
8447d9d5 1528}
48c3c107 1529EXPORT_SYMBOL_GPL(ide_host_register);
151575e4 1530
9f36d314 1531int ide_host_add(const struct ide_port_info *d, struct ide_hw **hws,
dca39830 1532 unsigned int n_ports, struct ide_host **hostp)
6f904d01
BZ
1533{
1534 struct ide_host *host;
8a69580e 1535 int rc;
6f904d01 1536
dca39830 1537 host = ide_host_alloc(d, hws, n_ports);
6f904d01
BZ
1538 if (host == NULL)
1539 return -ENOMEM;
1540
8a69580e
BZ
1541 rc = ide_host_register(host, d, hws);
1542 if (rc) {
1543 ide_host_free(host);
1544 return rc;
1545 }
6f904d01
BZ
1546
1547 if (hostp)
1548 *hostp = host;
1549
1550 return 0;
1551}
1552EXPORT_SYMBOL_GPL(ide_host_add);
1553
b40d1b88
BZ
1554static void __ide_port_unregister_devices(ide_hwif_t *hwif)
1555{
2bd24a1c 1556 ide_drive_t *drive;
b40d1b88
BZ
1557 int i;
1558
7ed5b157
BZ
1559 ide_port_for_each_present_dev(i, drive, hwif) {
1560 device_unregister(&drive->gendev);
1561 wait_for_completion(&drive->gendev_rel_comp);
b40d1b88
BZ
1562 }
1563}
1564
1565void ide_port_unregister_devices(ide_hwif_t *hwif)
1566{
1567 mutex_lock(&ide_cfg_mtx);
1568 __ide_port_unregister_devices(hwif);
1569 hwif->present = 0;
1570 ide_port_init_devices_data(hwif);
1571 mutex_unlock(&ide_cfg_mtx);
1572}
1573EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
1574
1575/**
1576 * ide_unregister - free an IDE interface
1577 * @hwif: IDE interface
1578 *
1579 * Perform the final unregister of an IDE interface.
1580 *
1581 * Locking:
1582 * The caller must not hold the IDE locks.
1583 *
1584 * It is up to the caller to be sure there is no pending I/O here,
1585 * and that the interface will not be reopened (present/vanishing
1586 * locking isn't yet done BTW).
1587 */
1588
1589static void ide_unregister(ide_hwif_t *hwif)
1590{
1591 BUG_ON(in_interrupt());
1592 BUG_ON(irqs_disabled());
1593
1594 mutex_lock(&ide_cfg_mtx);
1595
1596 if (hwif->present) {
1597 __ide_port_unregister_devices(hwif);
1598 hwif->present = 0;
1599 }
1600
1601 ide_proc_unregister_port(hwif);
1602
a259d532
MS
1603 if (!hwif->host->get_lock)
1604 free_irq(hwif->irq, hwif);
b40d1b88
BZ
1605
1606 device_unregister(hwif->portdev);
1607 device_unregister(&hwif->gendev);
1608 wait_for_completion(&hwif->gendev_rel_comp);
1609
1610 /*
1611 * Remove us from the kernel's knowledge
1612 */
1613 blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
1614 kfree(hwif->sg_table);
1615 unregister_blkdev(hwif->major, hwif->name);
1616
1617 ide_release_dma_engine(hwif);
1618
1619 mutex_unlock(&ide_cfg_mtx);
1620}
1621
8a69580e 1622void ide_host_free(struct ide_host *host)
151575e4 1623{
8cdf3100 1624 ide_hwif_t *hwif;
151575e4 1625 int i;
8447d9d5 1626
2bd24a1c 1627 ide_host_for_each_port(i, hwif, host) {
9a100f4b
BZ
1628 if (hwif)
1629 ide_port_free(hwif);
c97c6aca 1630 }
151575e4 1631
48c3c107 1632 kfree(host);
151575e4 1633}
8a69580e
BZ
1634EXPORT_SYMBOL_GPL(ide_host_free);
1635
1636void ide_host_remove(struct ide_host *host)
1637{
2bd24a1c 1638 ide_hwif_t *hwif;
8a69580e
BZ
1639 int i;
1640
2bd24a1c
BZ
1641 ide_host_for_each_port(i, hwif, host) {
1642 if (hwif)
1643 ide_unregister(hwif);
8a69580e
BZ
1644 }
1645
1646 ide_host_free(host);
1647}
48c3c107 1648EXPORT_SYMBOL_GPL(ide_host_remove);
2dde7861
BZ
1649
1650void ide_port_scan(ide_hwif_t *hwif)
1651{
5880b5de
BZ
1652 int rc;
1653
9fd91d95 1654 ide_port_apply_params(hwif);
2dde7861 1655 ide_port_cable_detect(hwif);
5880b5de
BZ
1656
1657 hwif->port_flags |= IDE_PFLAG_PROBING;
1658
2dde7861
BZ
1659 ide_port_init_devices(hwif);
1660
5880b5de
BZ
1661 rc = ide_probe_port(hwif);
1662
1663 hwif->port_flags &= ~IDE_PFLAG_PROBING;
1664
1665 if (rc < 0)
2dde7861
BZ
1666 return;
1667
1668 hwif->present = 1;
1669
1670 ide_port_tune_devices(hwif);
2dde7861 1671 ide_port_setup_devices(hwif);
e630fcbe 1672 ide_acpi_port_init_devices(hwif);
2dde7861
BZ
1673 hwif_register_devices(hwif);
1674 ide_proc_port_register_devices(hwif);
1675}
1676EXPORT_SYMBOL_GPL(ide_port_scan);