1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Copyright (C) 1994-1998   Linus Torvalds & authors (see below)
4  *  Copyright (C) 2005, 2007  Bartlomiej Zolnierkiewicz
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  *
16  * -- increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
17  *	 by Andrea Arcangeli
18  */
19 
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>
36 #include <linux/scatterlist.h>
37 
38 #include <asm/byteorder.h>
39 #include <asm/irq.h>
40 #include <linux/uaccess.h>
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 
generic_id(ide_drive_t * drive)52 static void generic_id(ide_drive_t *drive)
53 {
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;
59 }
60 
ide_disk_init_chs(ide_drive_t * drive)61 static void ide_disk_init_chs(ide_drive_t *drive)
62 {
63 	u16 *id = drive->id;
64 
65 	/* Extract geometry if we did not already have one for the drive */
66 	if (!drive->cyl || !drive->head || !drive->sect) {
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];
70 	}
71 
72 	/* Handle logical geometry translation by the drive */
73 	if (ata_id_current_chs_valid(id)) {
74 		drive->cyl  = id[ATA_ID_CUR_CYLS];
75 		drive->head = id[ATA_ID_CUR_HEADS];
76 		drive->sect = id[ATA_ID_CUR_SECTORS];
77 	}
78 
79 	/* Use physical geometry if what we have still makes no sense */
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];
84 	}
85 }
86 
ide_disk_init_mult_count(ide_drive_t * drive)87 static void ide_disk_init_mult_count(ide_drive_t *drive)
88 {
89 	u16 *id = drive->id;
90 	u8 max_multsect = id[ATA_ID_MAX_MULTSECT] & 0xff;
91 
92 	if (max_multsect) {
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;
99 
100 		if (drive->mult_req)
101 			drive->special_flags |= IDE_SFLAG_SET_MULTMODE;
102 	}
103 }
104 
ide_classify_ata_dev(ide_drive_t * drive)105 static 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 
ide_classify_atapi_dev(ide_drive_t * drive)124 static 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;
146 		fallthrough;
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 
182 /**
183  *	do_identify	-	identify a drive
184  *	@drive: drive to identify
185  *	@cmd: command used
186  *	@id: buffer for IDENTIFY data
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  */
192 
do_identify(ide_drive_t * drive,u8 cmd,u16 * id)193 static void do_identify(ide_drive_t *drive, u8 cmd, u16 *id)
194 {
195 	ide_hwif_t *hwif = drive->hwif;
196 	char *m = (char *)&id[ATA_ID_PROD];
197 	unsigned long flags;
198 	int bswap = 1;
199 
200 	/* local CPU only; some systems need this */
201 	local_irq_save(flags);
202 	/* read 512 bytes of id info */
203 	hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
204 	local_irq_restore(flags);
205 
206 	drive->dev_flags |= IDE_DFLAG_ID_READ;
207 #ifdef DEBUG
208 	printk(KERN_INFO "%s: dumping identify data\n", drive->name);
209 	ide_dump_identify((u8 *)id);
210 #endif
211 	ide_fix_driveid(id);
212 
213 	/*
214 	 *  ATA_CMD_ID_ATA returns little-endian info,
215 	 *  ATA_CMD_ID_ATAPI *usually* returns little-endian info.
216 	 */
217 	if (cmd == ATA_CMD_ID_ATAPI) {
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 */
221 			/* Vertos drives may still be weird */
222 			bswap ^= 1;
223 	}
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);
228 
229 	/* we depend on this a lot! */
230 	m[ATA_ID_PROD_LEN - 1] = '\0';
231 
232 	if (strstr(m, "E X A B Y T E N E S T"))
233 		drive->dev_flags &= ~IDE_DFLAG_PRESENT;
234 	else
235 		drive->dev_flags |= IDE_DFLAG_PRESENT;
236 }
237 
238 /**
239  *	ide_dev_read_id	-	send ATA/ATAPI IDENTIFY command
240  *	@drive: drive to identify
241  *	@cmd: command to use
242  *	@id: buffer for IDENTIFY data
243  *	@irq_ctx: flag set when called from the IRQ context
244  *
245  *	Sends an ATA(PI) IDENTIFY request to a drive and waits for a response.
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 
ide_dev_read_id(ide_drive_t * drive,u8 cmd,u16 * id,int irq_ctx)252 int ide_dev_read_id(ide_drive_t *drive, u8 cmd, u16 *id, int irq_ctx)
253 {
254 	ide_hwif_t *hwif = drive->hwif;
255 	struct ide_io_ports *io_ports = &hwif->io_ports;
256 	const struct ide_tp_ops *tp_ops = hwif->tp_ops;
257 	int use_altstatus = 0, rc;
258 	unsigned long timeout;
259 	u8 s = 0, a = 0;
260 
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)
266 		tp_ops->write_devctl(hwif, ATA_NIEN | ATA_DEVCTL_OBS);
267 
268 	/* take a deep breath */
269 	if (irq_ctx)
270 		mdelay(50);
271 	else
272 		msleep(50);
273 
274 	if (io_ports->ctl_addr &&
275 	    (hwif->host_flags & IDE_HFLAG_BROKEN_ALTSTATUS) == 0) {
276 		a = tp_ops->read_altstatus(hwif);
277 		s = tp_ops->read_status(hwif);
278 		if ((a ^ s) & ~ATA_SENSE)
279 			/* ancient Seagate drives, broken interfaces */
280 			printk(KERN_INFO "%s: probing with STATUS(0x%02x) "
281 					 "instead of ALTSTATUS(0x%02x)\n",
282 					 drive->name, s, a);
283 		else
284 			/* use non-intrusive polling */
285 			use_altstatus = 1;
286 	}
287 
288 	/* set features register for atapi
289 	 * identify command to be sure of reply
290 	 */
291 	if (cmd == ATA_CMD_ID_ATAPI) {
292 		struct ide_taskfile tf;
293 
294 		memset(&tf, 0, sizeof(tf));
295 		/* disable DMA & overlap */
296 		tp_ops->tf_load(drive, &tf, IDE_VALID_FEATURE);
297 	}
298 
299 	/* ask drive for ID */
300 	tp_ops->exec_command(hwif, cmd);
301 
302 	timeout = ((cmd == ATA_CMD_ID_ATA) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
303 
304 	/* wait for IRQ and ATA_DRQ */
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 	}
317 
318 	if (OK_STAT(s, ATA_DRQ, BAD_R_STAT)) {
319 		/* drive returned ID */
320 		do_identify(drive, cmd, id);
321 		/* drive responded with ID */
322 		rc = 0;
323 		/* clear drive IRQ */
324 		(void)tp_ops->read_status(hwif);
325 	} else {
326 		/* drive refused ID */
327 		rc = 2;
328 	}
329 	return rc;
330 }
331 
ide_busy_sleep(ide_drive_t * drive,unsigned long timeout,int altstatus)332 int ide_busy_sleep(ide_drive_t *drive, unsigned long timeout, int altstatus)
333 {
334 	ide_hwif_t *hwif = drive->hwif;
335 	u8 stat;
336 
337 	timeout += jiffies;
338 
339 	do {
340 		msleep(50);	/* give drive a breather */
341 		stat = altstatus ? hwif->tp_ops->read_altstatus(hwif)
342 				 : hwif->tp_ops->read_status(hwif);
343 		if ((stat & ATA_BUSY) == 0)
344 			return 0;
345 	} while (time_before(jiffies, timeout));
346 
347 	printk(KERN_ERR "%s: timeout in %s\n", drive->name, __func__);
348 
349 	return 1;	/* drive timed-out */
350 }
351 
ide_read_device(ide_drive_t * drive)352 static u8 ide_read_device(ide_drive_t *drive)
353 {
354 	struct ide_taskfile tf;
355 
356 	drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_DEVICE);
357 
358 	return tf.device;
359 }
360 
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 
do_probe(ide_drive_t * drive,u8 cmd)382 static int do_probe (ide_drive_t *drive, u8 cmd)
383 {
384 	ide_hwif_t *hwif = drive->hwif;
385 	const struct ide_tp_ops *tp_ops = hwif->tp_ops;
386 	u16 *id = drive->id;
387 	int rc;
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;
393 
394 #ifdef DEBUG
395 	printk(KERN_INFO "probing for %s: present=%d, media=%d, probetype=%s\n",
396 		drive->name, present, drive->media,
397 		(cmd == ATA_CMD_ID_ATA) ? "ATA" : "ATAPI");
398 #endif
399 
400 	/* needed for some systems
401 	 * (e.g. crw9624 as drive0 with disk as slave)
402 	 */
403 	msleep(50);
404 	tp_ops->dev_select(drive);
405 	msleep(50);
406 
407 	if (ide_read_device(drive) != drive->select && present == 0) {
408 		if (drive->dn & 1) {
409 			/* exit with drive0 selected */
410 			tp_ops->dev_select(hwif->devices[0]);
411 			/* allow ATA_BUSY to assert & clear */
412 			msleep(50);
413 		}
414 		/* no i/f present: mmm.. this should be a 4 -ml */
415 		return 3;
416 	}
417 
418 	stat = tp_ops->read_status(hwif);
419 
420 	if (OK_STAT(stat, ATA_DRDY, ATA_BUSY) ||
421 	    present || cmd == ATA_CMD_ID_ATAPI) {
422 		rc = ide_dev_read_id(drive, cmd, id, 0);
423 		if (rc)
424 			/* failed: try again */
425 			rc = ide_dev_read_id(drive, cmd, id, 0);
426 
427 		stat = tp_ops->read_status(hwif);
428 
429 		if (stat == (ATA_BUSY | ATA_DRDY))
430 			return 4;
431 
432 		if (rc == 1 && cmd == ATA_CMD_ID_ATAPI) {
433 			printk(KERN_ERR "%s: no response (status = 0x%02x), "
434 					"resetting drive\n", drive->name, stat);
435 			msleep(50);
436 			tp_ops->dev_select(drive);
437 			msleep(50);
438 			tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
439 			(void)ide_busy_sleep(drive, WAIT_WORSTCASE, 0);
440 			rc = ide_dev_read_id(drive, cmd, id, 0);
441 		}
442 
443 		/* ensure drive IRQ is clear */
444 		stat = tp_ops->read_status(hwif);
445 
446 		if (rc == 1)
447 			printk(KERN_ERR "%s: no response (status = 0x%02x)\n",
448 					drive->name, stat);
449 	} else {
450 		/* not present or maybe ATAPI */
451 		rc = 3;
452 	}
453 	if (drive->dn & 1) {
454 		/* exit with drive0 selected */
455 		tp_ops->dev_select(hwif->devices[0]);
456 		msleep(50);
457 		/* ensure drive irq is clear */
458 		(void)tp_ops->read_status(hwif);
459 	}
460 	return rc;
461 }
462 
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
471  *			1  device was found
472  *			   (note: IDE_DFLAG_PRESENT might still be not set)
473  */
474 
probe_for_drive(ide_drive_t * drive)475 static u8 probe_for_drive(ide_drive_t *drive)
476 {
477 	char *m;
478 	int rc;
479 	u8 cmd;
480 
481 	drive->dev_flags &= ~IDE_DFLAG_ID_READ;
482 
483 	m = (char *)&drive->id[ATA_ID_PROD];
484 	strcpy(m, "UNKNOWN");
485 
486 	/* skip probing? */
487 	if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0) {
488 		/* if !(success||timed-out) */
489 		cmd = ATA_CMD_ID_ATA;
490 		rc = do_probe(drive, cmd);
491 		if (rc >= 2) {
492 			/* look for ATAPI device */
493 			cmd = ATA_CMD_ID_ATAPI;
494 			rc = do_probe(drive, cmd);
495 		}
496 
497 		if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
498 			return 0;
499 
500 		/* identification failed? */
501 		if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
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);
511 				drive->dev_flags &= ~IDE_DFLAG_PRESENT;
512 			}
513 		} else {
514 			if (cmd == ATA_CMD_ID_ATAPI)
515 				ide_classify_atapi_dev(drive);
516 			else
517 				ide_classify_ata_dev(drive);
518 		}
519 	}
520 
521 	if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
522 		return 0;
523 
524 	/* The drive wasn't being helpful. Add generic info only */
525 	if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
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 
535 	return 1;
536 }
537 
hwif_release_dev(struct device * dev)538 static void hwif_release_dev(struct device *dev)
539 {
540 	ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
541 
542 	complete(&hwif->gendev_rel_comp);
543 }
544 
ide_register_port(ide_hwif_t * hwif)545 static int ide_register_port(ide_hwif_t *hwif)
546 {
547 	int ret;
548 
549 	/* register with global device tree */
550 	dev_set_name(&hwif->gendev, "%s", hwif->name);
551 	dev_set_drvdata(&hwif->gendev, hwif);
552 	if (hwif->gendev.parent == NULL)
553 		hwif->gendev.parent = hwif->dev;
554 	hwif->gendev.release = hwif_release_dev;
555 
556 	ret = device_register(&hwif->gendev);
557 	if (ret < 0) {
558 		printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
559 			__func__, ret);
560 		goto out;
561 	}
562 
563 	hwif->portdev = device_create(ide_port_class, &hwif->gendev,
564 				      MKDEV(0, 0), hwif, "%s", hwif->name);
565 	if (IS_ERR(hwif->portdev)) {
566 		ret = PTR_ERR(hwif->portdev);
567 		device_unregister(&hwif->gendev);
568 	}
569 out:
570 	return ret;
571 }
572 
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 
ide_port_wait_ready(ide_hwif_t * hwif)599 static int ide_port_wait_ready(ide_hwif_t *hwif)
600 {
601 	const struct ide_tp_ops *tp_ops = hwif->tp_ops;
602 	ide_drive_t *drive;
603 	int i, rc;
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 */
620 	ide_port_for_each_dev(i, drive, hwif) {
621 		/* Ignore disks that we will not probe for later. */
622 		if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0 ||
623 		    (drive->dev_flags & IDE_DFLAG_PRESENT)) {
624 			tp_ops->dev_select(drive);
625 			tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
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 	}
634 out:
635 	/* Exit function with master reselected (let's be sane) */
636 	if (i)
637 		tp_ops->dev_select(hwif->devices[0]);
638 
639 	return rc;
640 }
641 
642 /**
643  *	ide_undecoded_slave	-	look for bad CF adapters
644  *	@dev1: slave device
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 
ide_undecoded_slave(ide_drive_t * dev1)651 void ide_undecoded_slave(ide_drive_t *dev1)
652 {
653 	ide_drive_t *dev0 = dev1->hwif->devices[0];
654 
655 	if ((dev1->dn & 1) == 0 || (dev0->dev_flags & IDE_DFLAG_PRESENT) == 0)
656 		return;
657 
658 	/* If the models don't match they are not the same product */
659 	if (strcmp((char *)&dev0->id[ATA_ID_PROD],
660 		   (char *)&dev1->id[ATA_ID_PROD]))
661 		return;
662 
663 	/* Serial numbers do not match */
664 	if (strncmp((char *)&dev0->id[ATA_ID_SERNO],
665 		    (char *)&dev1->id[ATA_ID_SERNO], ATA_ID_SERNO_LEN))
666 		return;
667 
668 	/* No serial number, thankfully very rare for CF */
669 	if (*(char *)&dev0->id[ATA_ID_SERNO] == 0)
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 
675 	dev1->dev_flags &= ~IDE_DFLAG_PRESENT;
676 }
677 
678 EXPORT_SYMBOL_GPL(ide_undecoded_slave);
679 
ide_probe_port(ide_hwif_t * hwif)680 static int ide_probe_port(ide_hwif_t *hwif)
681 {
682 	ide_drive_t *drive;
683 	unsigned int irqd;
684 	int i, rc = -ENODEV;
685 
686 	BUG_ON(hwif->present);
687 
688 	if ((hwif->devices[0]->dev_flags & IDE_DFLAG_NOPROBE) &&
689 	    (hwif->devices[1]->dev_flags & IDE_DFLAG_NOPROBE))
690 		return -EACCES;
691 
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 
700 	if (ide_port_wait_ready(hwif) == -EBUSY)
701 		printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
702 
703 	/*
704 	 * Second drive should only exist if first drive was found,
705 	 * but a lot of cdrom drives are configured as single slaves.
706 	 */
707 	ide_port_for_each_dev(i, drive, hwif) {
708 		(void) probe_for_drive(drive);
709 		if (drive->dev_flags & IDE_DFLAG_PRESENT)
710 			rc = 0;
711 	}
712 
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 
720 	return rc;
721 }
722 
ide_port_tune_devices(ide_hwif_t * hwif)723 static void ide_port_tune_devices(ide_hwif_t *hwif)
724 {
725 	const struct ide_port_ops *port_ops = hwif->port_ops;
726 	ide_drive_t *drive;
727 	int i;
728 
729 	ide_port_for_each_present_dev(i, drive, hwif) {
730 		ide_check_nien_quirk_list(drive);
731 
732 		if (port_ops && port_ops->quirkproc)
733 			port_ops->quirkproc(drive);
734 	}
735 
736 	ide_port_for_each_present_dev(i, drive, hwif) {
737 		ide_set_max_pio(drive);
738 
739 		drive->dev_flags |= IDE_DFLAG_NICE1;
740 
741 		if (hwif->dma_ops)
742 			ide_set_dma(drive);
743 	}
744 }
745 
ide_initialize_rq(struct request * rq)746 static void ide_initialize_rq(struct request *rq)
747 {
748 	struct ide_request *req = blk_mq_rq_to_pdu(rq);
749 
750 	req->special = NULL;
751 	scsi_req_init(&req->sreq);
752 	req->sreq.sense = req->sense;
753 }
754 
755 static const struct blk_mq_ops ide_mq_ops = {
756 	.queue_rq		= ide_queue_rq,
757 	.initialize_rq_fn	= ide_initialize_rq,
758 };
759 
760 /*
761  * init request queue
762  */
ide_init_queue(ide_drive_t * drive)763 static int ide_init_queue(ide_drive_t *drive)
764 {
765 	struct request_queue *q;
766 	ide_hwif_t *hwif = drive->hwif;
767 	int max_sectors = 256;
768 	int max_sg_entries = PRD_ENTRIES;
769 	struct blk_mq_tag_set *set;
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 	 */
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))
788 		return 1;
789 
790 	q = blk_mq_init_queue(set);
791 	if (IS_ERR(q)) {
792 		blk_mq_free_tag_set(set);
793 		return 1;
794 	}
795 
796 	blk_queue_flag_set(QUEUE_FLAG_SCSI_PASSTHROUGH, q);
797 
798 	q->queuedata = drive;
799 	blk_queue_segment_boundary(q, 0xffff);
800 
801 	if (hwif->rqsize < max_sectors)
802 		max_sectors = hwif->rqsize;
803 	blk_queue_max_hw_sectors(q, max_sectors);
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 	 */
815 	max_sg_entries >>= 1;
816 #endif /* CONFIG_PCI */
817 
818 	blk_queue_max_segments(q, max_sg_entries);
819 
820 	/* assign drive queue */
821 	drive->queue = q;
822 
823 	return 0;
824 }
825 
826 static DEFINE_MUTEX(ide_cfg_mtx);
827 
828 /*
829  * For any present drive:
830  * - allocate the block device queue
831  */
ide_port_setup_devices(ide_hwif_t * hwif)832 static int ide_port_setup_devices(ide_hwif_t *hwif)
833 {
834 	ide_drive_t *drive;
835 	int i, j = 0;
836 
837 	mutex_lock(&ide_cfg_mtx);
838 	ide_port_for_each_present_dev(i, drive, hwif) {
839 		if (ide_init_queue(drive)) {
840 			printk(KERN_ERR "ide: failed to init %s\n",
841 					drive->name);
842 			drive->dev_flags &= ~IDE_DFLAG_PRESENT;
843 			continue;
844 		}
845 
846 		j++;
847 	}
848 	mutex_unlock(&ide_cfg_mtx);
849 
850 	return j;
851 }
852 
ide_host_enable_irqs(struct ide_host * host)853 static 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 
871 /*
872  * This routine sets up the IRQ for an IDE interface.
873  */
init_irq(ide_hwif_t * hwif)874 static int init_irq (ide_hwif_t *hwif)
875 {
876 	struct ide_io_ports *io_ports = &hwif->io_ports;
877 	struct ide_host *host = hwif->host;
878 	irq_handler_t irq_handler = host->irq_handler;
879 	int sa = host->irq_flags;
880 
881 	if (irq_handler == NULL)
882 		irq_handler = ide_intr;
883 
884 	if (!host->get_lock)
885 		if (request_irq(hwif->irq, irq_handler, sa, hwif->name, hwif))
886 			goto out_up;
887 
888 #if !defined(__mc68000__)
889 	printk(KERN_INFO "%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
890 		io_ports->data_addr, io_ports->status_addr,
891 		io_ports->ctl_addr, hwif->irq);
892 #else
893 	printk(KERN_INFO "%s at 0x%08lx on irq %d", hwif->name,
894 		io_ports->data_addr, hwif->irq);
895 #endif /* __mc68000__ */
896 	if (hwif->host->host_flags & IDE_HFLAG_SERIALIZE)
897 		printk(KERN_CONT " (serialized)");
898 	printk(KERN_CONT "\n");
899 
900 	return 0;
901 out_up:
902 	return 1;
903 }
904 
ata_probe(dev_t dev)905 static void ata_probe(dev_t dev)
906 {
907 	request_module("ide-disk");
908 	request_module("ide-cd");
909 	request_module("ide-tape");
910 	request_module("ide-floppy");
911 }
912 
ide_init_disk(struct gendisk * disk,ide_drive_t * drive)913 void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
914 {
915 	ide_hwif_t *hwif = drive->hwif;
916 	unsigned int unit = drive->dn & 1;
917 
918 	disk->major = hwif->major;
919 	disk->first_minor = unit << PARTN_BITS;
920 	sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
921 	disk->queue = drive->queue;
922 }
923 
924 EXPORT_SYMBOL_GPL(ide_init_disk);
925 
drive_release_dev(struct device * dev)926 static void drive_release_dev (struct device *dev)
927 {
928 	ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
929 
930 	ide_proc_unregister_device(drive);
931 
932 	if (drive->sense_rq)
933 		blk_mq_free_request(drive->sense_rq);
934 
935 	blk_cleanup_queue(drive->queue);
936 	drive->queue = NULL;
937 	blk_mq_free_tag_set(&drive->tag_set);
938 
939 	drive->dev_flags &= ~IDE_DFLAG_PRESENT;
940 
941 	complete(&drive->gendev_rel_comp);
942 }
943 
hwif_init(ide_hwif_t * hwif)944 static int hwif_init(ide_hwif_t *hwif)
945 {
946 	if (!hwif->irq) {
947 		printk(KERN_ERR "%s: disabled, no IRQ\n", hwif->name);
948 		return 0;
949 	}
950 
951 	if (__register_blkdev(hwif->major, hwif->name, ata_probe))
952 		return 0;
953 
954 	if (!hwif->sg_max_nents)
955 		hwif->sg_max_nents = PRD_ENTRIES;
956 
957 	hwif->sg_table = kmalloc_array(hwif->sg_max_nents,
958 				       sizeof(struct scatterlist),
959 				       GFP_KERNEL);
960 	if (!hwif->sg_table) {
961 		printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
962 		goto out;
963 	}
964 
965 	sg_init_table(hwif->sg_table, hwif->sg_max_nents);
966 
967 	if (init_irq(hwif)) {
968 		printk(KERN_ERR "%s: disabled, unable to get IRQ %d\n",
969 			hwif->name, hwif->irq);
970 		goto out;
971 	}
972 
973 	return 1;
974 
975 out:
976 	unregister_blkdev(hwif->major, hwif->name);
977 	return 0;
978 }
979 
hwif_register_devices(ide_hwif_t * hwif)980 static void hwif_register_devices(ide_hwif_t *hwif)
981 {
982 	ide_drive_t *drive;
983 	unsigned int i;
984 
985 	ide_port_for_each_present_dev(i, drive, hwif) {
986 		struct device *dev = &drive->gendev;
987 		int ret;
988 
989 		dev_set_name(dev, "%u.%u", hwif->index, i);
990 		dev_set_drvdata(dev, drive);
991 		dev->parent = &hwif->gendev;
992 		dev->bus = &ide_bus_type;
993 		dev->release = drive_release_dev;
994 
995 		ret = device_register(dev);
996 		if (ret < 0)
997 			printk(KERN_WARNING "IDE: %s: device_register error: "
998 					    "%d\n", __func__, ret);
999 	}
1000 }
1001 
ide_port_init_devices(ide_hwif_t * hwif)1002 static void ide_port_init_devices(ide_hwif_t *hwif)
1003 {
1004 	const struct ide_port_ops *port_ops = hwif->port_ops;
1005 	ide_drive_t *drive;
1006 	int i;
1007 
1008 	ide_port_for_each_dev(i, drive, hwif) {
1009 		drive->dn = i + hwif->channel * 2;
1010 
1011 		if (hwif->host_flags & IDE_HFLAG_IO_32BIT)
1012 			drive->io_32bit = 1;
1013 		if (hwif->host_flags & IDE_HFLAG_NO_IO_32BIT)
1014 			drive->dev_flags |= IDE_DFLAG_NO_IO_32BIT;
1015 		if (hwif->host_flags & IDE_HFLAG_UNMASK_IRQS)
1016 			drive->dev_flags |= IDE_DFLAG_UNMASK;
1017 		if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS)
1018 			drive->dev_flags |= IDE_DFLAG_NO_UNMASK;
1019 
1020 		drive->pio_mode = XFER_PIO_0;
1021 
1022 		if (port_ops && port_ops->init_dev)
1023 			port_ops->init_dev(drive);
1024 	}
1025 }
1026 
ide_init_port(ide_hwif_t * hwif,unsigned int port,const struct ide_port_info * d)1027 static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
1028 			  const struct ide_port_info *d)
1029 {
1030 	hwif->channel = port;
1031 
1032 	hwif->chipset = d->chipset ? d->chipset : ide_pci;
1033 
1034 	if (d->init_iops)
1035 		d->init_iops(hwif);
1036 
1037 	/* ->host_flags may be set by ->init_iops (or even earlier...) */
1038 	hwif->host_flags |= d->host_flags;
1039 	hwif->pio_mask = d->pio_mask;
1040 
1041 	if (d->tp_ops)
1042 		hwif->tp_ops = d->tp_ops;
1043 
1044 	/* ->set_pio_mode for DTC2278 is currently limited to port 0 */
1045 	if ((hwif->host_flags & IDE_HFLAG_DTC2278) == 0 || hwif->channel == 0)
1046 		hwif->port_ops = d->port_ops;
1047 
1048 	hwif->swdma_mask = d->swdma_mask;
1049 	hwif->mwdma_mask = d->mwdma_mask;
1050 	hwif->ultra_mask = d->udma_mask;
1051 
1052 	if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
1053 		int rc;
1054 
1055 		hwif->dma_ops = d->dma_ops;
1056 
1057 		if (d->init_dma)
1058 			rc = d->init_dma(hwif, d);
1059 		else
1060 			rc = ide_hwif_setup_dma(hwif, d);
1061 
1062 		if (rc < 0) {
1063 			printk(KERN_INFO "%s: DMA disabled\n", hwif->name);
1064 
1065 			hwif->dma_ops = NULL;
1066 			hwif->dma_base = 0;
1067 			hwif->swdma_mask = 0;
1068 			hwif->mwdma_mask = 0;
1069 			hwif->ultra_mask = 0;
1070 		}
1071 	}
1072 
1073 	if ((d->host_flags & IDE_HFLAG_SERIALIZE) ||
1074 	    ((d->host_flags & IDE_HFLAG_SERIALIZE_DMA) && hwif->dma_base))
1075 		hwif->host->host_flags |= IDE_HFLAG_SERIALIZE;
1076 
1077 	if (d->max_sectors)
1078 		hwif->rqsize = d->max_sectors;
1079 	else {
1080 		if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
1081 		    (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
1082 			hwif->rqsize = 256;
1083 		else
1084 			hwif->rqsize = 65536;
1085 	}
1086 
1087 	/* call chipset specific routine for each enabled port */
1088 	if (d->init_hwif)
1089 		d->init_hwif(hwif);
1090 }
1091 
ide_port_cable_detect(ide_hwif_t * hwif)1092 static void ide_port_cable_detect(ide_hwif_t *hwif)
1093 {
1094 	const struct ide_port_ops *port_ops = hwif->port_ops;
1095 
1096 	if (port_ops && port_ops->cable_detect && (hwif->ultra_mask & 0x78)) {
1097 		if (hwif->cbl != ATA_CBL_PATA40_SHORT)
1098 			hwif->cbl = port_ops->cable_detect(hwif);
1099 	}
1100 }
1101 
1102 /*
1103  * Deferred request list insertion handler
1104  */
drive_rq_insert_work(struct work_struct * work)1105 static void drive_rq_insert_work(struct work_struct *work)
1106 {
1107 	ide_drive_t *drive = container_of(work, ide_drive_t, rq_work);
1108 	ide_hwif_t *hwif = drive->hwif;
1109 	struct request *rq;
1110 	blk_status_t ret;
1111 	LIST_HEAD(list);
1112 
1113 	blk_mq_quiesce_queue(drive->queue);
1114 
1115 	ret = BLK_STS_OK;
1116 	spin_lock_irq(&hwif->lock);
1117 	while (!list_empty(&drive->rq_list)) {
1118 		rq = list_first_entry(&drive->rq_list, struct request, queuelist);
1119 		list_del_init(&rq->queuelist);
1120 
1121 		spin_unlock_irq(&hwif->lock);
1122 		ret = ide_issue_rq(drive, rq, true);
1123 		spin_lock_irq(&hwif->lock);
1124 	}
1125 	spin_unlock_irq(&hwif->lock);
1126 
1127 	blk_mq_unquiesce_queue(drive->queue);
1128 
1129 	if (ret != BLK_STS_OK)
1130 		kblockd_schedule_work(&drive->rq_work);
1131 }
1132 
1133 static const u8 ide_hwif_to_major[] =
1134 	{ IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR, IDE4_MAJOR,
1135 	  IDE5_MAJOR, IDE6_MAJOR, IDE7_MAJOR, IDE8_MAJOR, IDE9_MAJOR };
1136 
ide_port_init_devices_data(ide_hwif_t * hwif)1137 static void ide_port_init_devices_data(ide_hwif_t *hwif)
1138 {
1139 	ide_drive_t *drive;
1140 	int i;
1141 
1142 	ide_port_for_each_dev(i, drive, hwif) {
1143 		u8 j = (hwif->index * MAX_DRIVES) + i;
1144 		u16 *saved_id = drive->id;
1145 
1146 		memset(drive, 0, sizeof(*drive));
1147 		memset(saved_id, 0, SECTOR_SIZE);
1148 		drive->id = saved_id;
1149 
1150 		drive->media			= ide_disk;
1151 		drive->select			= (i << 4) | ATA_DEVICE_OBS;
1152 		drive->hwif			= hwif;
1153 		drive->ready_stat		= ATA_DRDY;
1154 		drive->bad_wstat		= BAD_W_STAT;
1155 		drive->special_flags		= IDE_SFLAG_RECALIBRATE |
1156 						  IDE_SFLAG_SET_GEOMETRY;
1157 		drive->name[0]			= 'h';
1158 		drive->name[1]			= 'd';
1159 		drive->name[2]			= 'a' + j;
1160 		drive->max_failures		= IDE_DEFAULT_MAX_FAILURES;
1161 
1162 		INIT_LIST_HEAD(&drive->list);
1163 		init_completion(&drive->gendev_rel_comp);
1164 
1165 		INIT_WORK(&drive->rq_work, drive_rq_insert_work);
1166 		INIT_LIST_HEAD(&drive->rq_list);
1167 	}
1168 }
1169 
ide_init_port_data(ide_hwif_t * hwif,unsigned int index)1170 static void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
1171 {
1172 	/* fill in any non-zero initial values */
1173 	hwif->index	= index;
1174 	hwif->major	= ide_hwif_to_major[index];
1175 
1176 	hwif->name[0]	= 'i';
1177 	hwif->name[1]	= 'd';
1178 	hwif->name[2]	= 'e';
1179 	hwif->name[3]	= '0' + index;
1180 
1181 	spin_lock_init(&hwif->lock);
1182 
1183 	timer_setup(&hwif->timer, ide_timer_expiry, 0);
1184 
1185 	init_completion(&hwif->gendev_rel_comp);
1186 
1187 	hwif->tp_ops = &default_tp_ops;
1188 
1189 	ide_port_init_devices_data(hwif);
1190 }
1191 
ide_init_port_hw(ide_hwif_t * hwif,struct ide_hw * hw)1192 static void ide_init_port_hw(ide_hwif_t *hwif, struct ide_hw *hw)
1193 {
1194 	memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports));
1195 	hwif->irq = hw->irq;
1196 	hwif->dev = hw->dev;
1197 	hwif->gendev.parent = hw->parent ? hw->parent : hw->dev;
1198 	hwif->config_data = hw->config;
1199 }
1200 
1201 static unsigned int ide_indexes;
1202 
1203 /**
1204  *	ide_find_port_slot	-	find free port slot
1205  *	@d: IDE port info
1206  *
1207  *	Return the new port slot index or -ENOENT if we are out of free slots.
1208  */
1209 
ide_find_port_slot(const struct ide_port_info * d)1210 static int ide_find_port_slot(const struct ide_port_info *d)
1211 {
1212 	int idx = -ENOENT;
1213 	u8 bootable = (d && (d->host_flags & IDE_HFLAG_NON_BOOTABLE)) ? 0 : 1;
1214 	u8 i = (d && (d->host_flags & IDE_HFLAG_QD_2ND_PORT)) ? 1 : 0;
1215 
1216 	/*
1217 	 * Claim an unassigned slot.
1218 	 *
1219 	 * Give preference to claiming other slots before claiming ide0/ide1,
1220 	 * just in case there's another interface yet-to-be-scanned
1221 	 * which uses ports 0x1f0/0x170 (the ide0/ide1 defaults).
1222 	 *
1223 	 * Unless there is a bootable card that does not use the standard
1224 	 * ports 0x1f0/0x170 (the ide0/ide1 defaults).
1225 	 */
1226 	mutex_lock(&ide_cfg_mtx);
1227 	if (bootable) {
1228 		if ((ide_indexes | i) != (1 << MAX_HWIFS) - 1)
1229 			idx = ffz(ide_indexes | i);
1230 	} else {
1231 		if ((ide_indexes | 3) != (1 << MAX_HWIFS) - 1)
1232 			idx = ffz(ide_indexes | 3);
1233 		else if ((ide_indexes & 3) != 3)
1234 			idx = ffz(ide_indexes);
1235 	}
1236 	if (idx >= 0)
1237 		ide_indexes |= (1 << idx);
1238 	mutex_unlock(&ide_cfg_mtx);
1239 
1240 	return idx;
1241 }
1242 
ide_free_port_slot(int idx)1243 static void ide_free_port_slot(int idx)
1244 {
1245 	mutex_lock(&ide_cfg_mtx);
1246 	ide_indexes &= ~(1 << idx);
1247 	mutex_unlock(&ide_cfg_mtx);
1248 }
1249 
ide_port_free_devices(ide_hwif_t * hwif)1250 static void ide_port_free_devices(ide_hwif_t *hwif)
1251 {
1252 	ide_drive_t *drive;
1253 	int i;
1254 
1255 	ide_port_for_each_dev(i, drive, hwif) {
1256 		kfree(drive->id);
1257 		kfree(drive);
1258 	}
1259 }
1260 
ide_port_alloc_devices(ide_hwif_t * hwif,int node)1261 static int ide_port_alloc_devices(ide_hwif_t *hwif, int node)
1262 {
1263 	ide_drive_t *drive;
1264 	int i;
1265 
1266 	for (i = 0; i < MAX_DRIVES; i++) {
1267 		drive = kzalloc_node(sizeof(*drive), GFP_KERNEL, node);
1268 		if (drive == NULL)
1269 			goto out_nomem;
1270 
1271 		/*
1272 		 * In order to keep things simple we have an id
1273 		 * block for all drives at all times. If the device
1274 		 * is pre ATA or refuses ATA/ATAPI identify we
1275 		 * will add faked data to this.
1276 		 *
1277 		 * Also note that 0 everywhere means "can't do X"
1278 		 */
1279 		drive->id = kzalloc_node(SECTOR_SIZE, GFP_KERNEL, node);
1280 		if (drive->id == NULL)
1281 			goto out_free_drive;
1282 
1283 		hwif->devices[i] = drive;
1284 	}
1285 	return 0;
1286 
1287 out_free_drive:
1288 	kfree(drive);
1289 out_nomem:
1290 	ide_port_free_devices(hwif);
1291 	return -ENOMEM;
1292 }
1293 
ide_host_alloc(const struct ide_port_info * d,struct ide_hw ** hws,unsigned int n_ports)1294 struct ide_host *ide_host_alloc(const struct ide_port_info *d,
1295 				struct ide_hw **hws, unsigned int n_ports)
1296 {
1297 	struct ide_host *host;
1298 	struct device *dev = hws[0] ? hws[0]->dev : NULL;
1299 	int node = dev ? dev_to_node(dev) : -1;
1300 	int i;
1301 
1302 	host = kzalloc_node(sizeof(*host), GFP_KERNEL, node);
1303 	if (host == NULL)
1304 		return NULL;
1305 
1306 	for (i = 0; i < n_ports; i++) {
1307 		ide_hwif_t *hwif;
1308 		int idx;
1309 
1310 		if (hws[i] == NULL)
1311 			continue;
1312 
1313 		hwif = kzalloc_node(sizeof(*hwif), GFP_KERNEL, node);
1314 		if (hwif == NULL)
1315 			continue;
1316 
1317 		if (ide_port_alloc_devices(hwif, node) < 0) {
1318 			kfree(hwif);
1319 			continue;
1320 		}
1321 
1322 		idx = ide_find_port_slot(d);
1323 		if (idx < 0) {
1324 			printk(KERN_ERR "%s: no free slot for interface\n",
1325 					d ? d->name : "ide");
1326 			ide_port_free_devices(hwif);
1327 			kfree(hwif);
1328 			continue;
1329 		}
1330 
1331 		ide_init_port_data(hwif, idx);
1332 
1333 		hwif->host = host;
1334 
1335 		host->ports[i] = hwif;
1336 		host->n_ports++;
1337 	}
1338 
1339 	if (host->n_ports == 0) {
1340 		kfree(host);
1341 		return NULL;
1342 	}
1343 
1344 	host->dev[0] = dev;
1345 
1346 	if (d) {
1347 		host->init_chipset = d->init_chipset;
1348 		host->get_lock     = d->get_lock;
1349 		host->release_lock = d->release_lock;
1350 		host->host_flags = d->host_flags;
1351 		host->irq_flags = d->irq_flags;
1352 	}
1353 
1354 	return host;
1355 }
1356 EXPORT_SYMBOL_GPL(ide_host_alloc);
1357 
ide_port_free(ide_hwif_t * hwif)1358 static void ide_port_free(ide_hwif_t *hwif)
1359 {
1360 	ide_port_free_devices(hwif);
1361 	ide_free_port_slot(hwif->index);
1362 	kfree(hwif);
1363 }
1364 
ide_disable_port(ide_hwif_t * hwif)1365 static void ide_disable_port(ide_hwif_t *hwif)
1366 {
1367 	struct ide_host *host = hwif->host;
1368 	int i;
1369 
1370 	printk(KERN_INFO "%s: disabling port\n", hwif->name);
1371 
1372 	for (i = 0; i < MAX_HOST_PORTS; i++) {
1373 		if (host->ports[i] == hwif) {
1374 			host->ports[i] = NULL;
1375 			host->n_ports--;
1376 		}
1377 	}
1378 
1379 	ide_port_free(hwif);
1380 }
1381 
ide_host_register(struct ide_host * host,const struct ide_port_info * d,struct ide_hw ** hws)1382 int ide_host_register(struct ide_host *host, const struct ide_port_info *d,
1383 		      struct ide_hw **hws)
1384 {
1385 	ide_hwif_t *hwif, *mate = NULL;
1386 	int i, j = 0;
1387 
1388 	pr_warn("legacy IDE will be removed in 2021, please switch to libata\n"
1389 		"Report any missing HW support to linux-ide@vger.kernel.org\n");
1390 
1391 	ide_host_for_each_port(i, hwif, host) {
1392 		if (hwif == NULL) {
1393 			mate = NULL;
1394 			continue;
1395 		}
1396 
1397 		ide_init_port_hw(hwif, hws[i]);
1398 		ide_port_apply_params(hwif);
1399 
1400 		if ((i & 1) && mate) {
1401 			hwif->mate = mate;
1402 			mate->mate = hwif;
1403 		}
1404 
1405 		mate = (i & 1) ? NULL : hwif;
1406 
1407 		ide_init_port(hwif, i & 1, d);
1408 		ide_port_cable_detect(hwif);
1409 
1410 		hwif->port_flags |= IDE_PFLAG_PROBING;
1411 
1412 		ide_port_init_devices(hwif);
1413 	}
1414 
1415 	ide_host_for_each_port(i, hwif, host) {
1416 		if (hwif == NULL)
1417 			continue;
1418 
1419 		if (ide_probe_port(hwif) == 0)
1420 			hwif->present = 1;
1421 
1422 		hwif->port_flags &= ~IDE_PFLAG_PROBING;
1423 
1424 		if ((hwif->host_flags & IDE_HFLAG_4DRIVES) == 0 ||
1425 		    hwif->mate == NULL || hwif->mate->present == 0) {
1426 			if (ide_register_port(hwif)) {
1427 				ide_disable_port(hwif);
1428 				continue;
1429 			}
1430 		}
1431 
1432 		if (hwif->present)
1433 			ide_port_tune_devices(hwif);
1434 	}
1435 
1436 	ide_host_enable_irqs(host);
1437 
1438 	ide_host_for_each_port(i, hwif, host) {
1439 		if (hwif == NULL)
1440 			continue;
1441 
1442 		if (hwif_init(hwif) == 0) {
1443 			printk(KERN_INFO "%s: failed to initialize IDE "
1444 					 "interface\n", hwif->name);
1445 			device_unregister(hwif->portdev);
1446 			device_unregister(&hwif->gendev);
1447 			ide_disable_port(hwif);
1448 			continue;
1449 		}
1450 
1451 		if (hwif->present)
1452 			if (ide_port_setup_devices(hwif) == 0) {
1453 				hwif->present = 0;
1454 				continue;
1455 			}
1456 
1457 		j++;
1458 
1459 		ide_acpi_init_port(hwif);
1460 
1461 		if (hwif->present)
1462 			ide_acpi_port_init_devices(hwif);
1463 	}
1464 
1465 	ide_host_for_each_port(i, hwif, host) {
1466 		if (hwif == NULL)
1467 			continue;
1468 
1469 		ide_sysfs_register_port(hwif);
1470 		ide_proc_register_port(hwif);
1471 
1472 		if (hwif->present) {
1473 			ide_proc_port_register_devices(hwif);
1474 			hwif_register_devices(hwif);
1475 		}
1476 	}
1477 
1478 	return j ? 0 : -1;
1479 }
1480 EXPORT_SYMBOL_GPL(ide_host_register);
1481 
ide_host_add(const struct ide_port_info * d,struct ide_hw ** hws,unsigned int n_ports,struct ide_host ** hostp)1482 int ide_host_add(const struct ide_port_info *d, struct ide_hw **hws,
1483 		 unsigned int n_ports, struct ide_host **hostp)
1484 {
1485 	struct ide_host *host;
1486 	int rc;
1487 
1488 	host = ide_host_alloc(d, hws, n_ports);
1489 	if (host == NULL)
1490 		return -ENOMEM;
1491 
1492 	rc = ide_host_register(host, d, hws);
1493 	if (rc) {
1494 		ide_host_free(host);
1495 		return rc;
1496 	}
1497 
1498 	if (hostp)
1499 		*hostp = host;
1500 
1501 	return 0;
1502 }
1503 EXPORT_SYMBOL_GPL(ide_host_add);
1504 
__ide_port_unregister_devices(ide_hwif_t * hwif)1505 static void __ide_port_unregister_devices(ide_hwif_t *hwif)
1506 {
1507 	ide_drive_t *drive;
1508 	int i;
1509 
1510 	ide_port_for_each_present_dev(i, drive, hwif) {
1511 		device_unregister(&drive->gendev);
1512 		wait_for_completion(&drive->gendev_rel_comp);
1513 	}
1514 }
1515 
ide_port_unregister_devices(ide_hwif_t * hwif)1516 void ide_port_unregister_devices(ide_hwif_t *hwif)
1517 {
1518 	mutex_lock(&ide_cfg_mtx);
1519 	__ide_port_unregister_devices(hwif);
1520 	hwif->present = 0;
1521 	ide_port_init_devices_data(hwif);
1522 	mutex_unlock(&ide_cfg_mtx);
1523 }
1524 EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
1525 
1526 /**
1527  *	ide_unregister		-	free an IDE interface
1528  *	@hwif: IDE interface
1529  *
1530  *	Perform the final unregister of an IDE interface.
1531  *
1532  *	Locking:
1533  *	The caller must not hold the IDE locks.
1534  *
1535  *	It is up to the caller to be sure there is no pending I/O here,
1536  *	and that the interface will not be reopened (present/vanishing
1537  *	locking isn't yet done BTW).
1538  */
1539 
ide_unregister(ide_hwif_t * hwif)1540 static void ide_unregister(ide_hwif_t *hwif)
1541 {
1542 	mutex_lock(&ide_cfg_mtx);
1543 
1544 	if (hwif->present) {
1545 		__ide_port_unregister_devices(hwif);
1546 		hwif->present = 0;
1547 	}
1548 
1549 	ide_proc_unregister_port(hwif);
1550 
1551 	if (!hwif->host->get_lock)
1552 		free_irq(hwif->irq, hwif);
1553 
1554 	device_unregister(hwif->portdev);
1555 	device_unregister(&hwif->gendev);
1556 	wait_for_completion(&hwif->gendev_rel_comp);
1557 
1558 	/*
1559 	 * Remove us from the kernel's knowledge
1560 	 */
1561 	kfree(hwif->sg_table);
1562 	unregister_blkdev(hwif->major, hwif->name);
1563 
1564 	ide_release_dma_engine(hwif);
1565 
1566 	mutex_unlock(&ide_cfg_mtx);
1567 }
1568 
ide_host_free(struct ide_host * host)1569 void ide_host_free(struct ide_host *host)
1570 {
1571 	ide_hwif_t *hwif;
1572 	int i;
1573 
1574 	ide_host_for_each_port(i, hwif, host) {
1575 		if (hwif)
1576 			ide_port_free(hwif);
1577 	}
1578 
1579 	kfree(host);
1580 }
1581 EXPORT_SYMBOL_GPL(ide_host_free);
1582 
ide_host_remove(struct ide_host * host)1583 void ide_host_remove(struct ide_host *host)
1584 {
1585 	ide_hwif_t *hwif;
1586 	int i;
1587 
1588 	ide_host_for_each_port(i, hwif, host) {
1589 		if (hwif)
1590 			ide_unregister(hwif);
1591 	}
1592 
1593 	ide_host_free(host);
1594 }
1595 EXPORT_SYMBOL_GPL(ide_host_remove);
1596 
ide_port_scan(ide_hwif_t * hwif)1597 void ide_port_scan(ide_hwif_t *hwif)
1598 {
1599 	int rc;
1600 
1601 	ide_port_apply_params(hwif);
1602 	ide_port_cable_detect(hwif);
1603 
1604 	hwif->port_flags |= IDE_PFLAG_PROBING;
1605 
1606 	ide_port_init_devices(hwif);
1607 
1608 	rc = ide_probe_port(hwif);
1609 
1610 	hwif->port_flags &= ~IDE_PFLAG_PROBING;
1611 
1612 	if (rc < 0)
1613 		return;
1614 
1615 	hwif->present = 1;
1616 
1617 	ide_port_tune_devices(hwif);
1618 	ide_port_setup_devices(hwif);
1619 	ide_acpi_port_init_devices(hwif);
1620 	hwif_register_devices(hwif);
1621 	ide_proc_port_register_devices(hwif);
1622 }
1623 EXPORT_SYMBOL_GPL(ide_port_scan);
1624