xref: /netbsd-src/sys/compat/linux/common/linux_hdio.c (revision 41aa5859b647891f23013f613e0858c4b789a7bf)
1*41aa5859Sriastradh /*	$NetBSD: linux_hdio.c,v 1.19 2021/09/07 11:43:04 riastradh Exp $	*/
2a499e569Sfvdl 
3a499e569Sfvdl /*
4a499e569Sfvdl  * Copyright (c) 2000 Wasabi Systems, Inc.
5a499e569Sfvdl  * All rights reserved.
6a499e569Sfvdl  *
7a499e569Sfvdl  * Written by Frank van der Linden for Wasabi Systems, Inc.
8a499e569Sfvdl  *
9a499e569Sfvdl  * Redistribution and use in source and binary forms, with or without
10a499e569Sfvdl  * modification, are permitted provided that the following conditions
11a499e569Sfvdl  * are met:
12a499e569Sfvdl  * 1. Redistributions of source code must retain the above copyright
13a499e569Sfvdl  *    notice, this list of conditions and the following disclaimer.
14a499e569Sfvdl  * 2. Redistributions in binary form must reproduce the above copyright
15a499e569Sfvdl  *    notice, this list of conditions and the following disclaimer in the
16a499e569Sfvdl  *    documentation and/or other materials provided with the distribution.
17a499e569Sfvdl  * 3. All advertising materials mentioning features or use of this software
18a499e569Sfvdl  *    must display the following acknowledgement:
19a499e569Sfvdl  *      This product includes software developed for the NetBSD Project by
20a499e569Sfvdl  *      Wasabi Systems, Inc.
21a499e569Sfvdl  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22a499e569Sfvdl  *    or promote products derived from this software without specific prior
23a499e569Sfvdl  *    written permission.
24a499e569Sfvdl  *
25a499e569Sfvdl  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26a499e569Sfvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27a499e569Sfvdl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28a499e569Sfvdl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29a499e569Sfvdl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30a499e569Sfvdl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31a499e569Sfvdl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32a499e569Sfvdl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33a499e569Sfvdl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34a499e569Sfvdl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35a499e569Sfvdl  * POSSIBILITY OF SUCH DAMAGE.
36a499e569Sfvdl  */
37a499e569Sfvdl 
38dab6ef8bSlukem #include <sys/cdefs.h>
39*41aa5859Sriastradh __KERNEL_RCSID(0, "$NetBSD: linux_hdio.c,v 1.19 2021/09/07 11:43:04 riastradh Exp $");
40dab6ef8bSlukem 
41a499e569Sfvdl #include <sys/param.h>
42a499e569Sfvdl #include <sys/systm.h>
43a499e569Sfvdl #include <sys/ioctl.h>
44a499e569Sfvdl #include <sys/file.h>
45a499e569Sfvdl #include <sys/filedesc.h>
46a499e569Sfvdl #include <sys/mount.h>
47a499e569Sfvdl #include <sys/proc.h>
48a499e569Sfvdl #include <sys/disklabel.h>
49a499e569Sfvdl 
50a499e569Sfvdl #include <dev/ata/atareg.h>
51a499e569Sfvdl #include <dev/ic/wdcreg.h>
52a499e569Sfvdl #include <sys/ataio.h>
53a499e569Sfvdl 
54a499e569Sfvdl #include <sys/syscallargs.h>
55a499e569Sfvdl 
56a499e569Sfvdl #include <compat/linux/common/linux_types.h>
57a499e569Sfvdl #include <compat/linux/common/linux_ioctl.h>
58a499e569Sfvdl #include <compat/linux/common/linux_signal.h>
59a499e569Sfvdl #include <compat/linux/common/linux_util.h>
60a499e569Sfvdl #include <compat/linux/common/linux_hdio.h>
61a478f23bSnjoly #include <compat/linux/common/linux_ipc.h>
62a478f23bSnjoly #include <compat/linux/common/linux_sem.h>
63a499e569Sfvdl 
64a499e569Sfvdl #include <compat/linux/linux_syscallargs.h>
65a499e569Sfvdl 
66a499e569Sfvdl int
linux_ioctl_hdio(struct lwp * l,const struct linux_sys_ioctl_args * uap,register_t * retval)677e2790cfSdsl linux_ioctl_hdio(struct lwp *l, const struct linux_sys_ioctl_args *uap,
68a499e569Sfvdl 		 register_t *retval)
69a499e569Sfvdl {
70a499e569Sfvdl 	u_long com;
71a499e569Sfvdl 	int error, error1;
72a499e569Sfvdl 	struct file *fp;
73a9ca7a37Sad 	int (*ioctlf)(struct file *, u_long, void *);
74a499e569Sfvdl 	struct atareq req;
758d10f962Schristos 	struct disklabel label;
768d10f962Schristos 	struct partinfo pi;
77a499e569Sfvdl 	struct linux_hd_geometry hdg;
78a499e569Sfvdl 	struct linux_hd_big_geometry hdg_big;
79a499e569Sfvdl 
80a9ca7a37Sad 	if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
81a499e569Sfvdl 		return (EBADF);
82a499e569Sfvdl 
83a499e569Sfvdl 	com = SCARG(uap, com);
84a499e569Sfvdl 	ioctlf = fp->f_ops->fo_ioctl;
85a499e569Sfvdl 	retval[0] = error = 0;
86a499e569Sfvdl 
87a499e569Sfvdl 	switch (com) {
88a499e569Sfvdl 	case LINUX_HDIO_OBSOLETE_IDENTITY:
89a499e569Sfvdl 	case LINUX_HDIO_GET_IDENTITY:
90a499e569Sfvdl 		req.flags = ATACMD_READ;
91a499e569Sfvdl 		req.command = WDCC_IDENTIFY;
92bf215415Sdsl 		req.databuf = SCARG(uap, data);
93a499e569Sfvdl 		/*
94a499e569Sfvdl 		 * 142 is the size of the old structure used by Linux,
95a499e569Sfvdl 		 * which doesn't seem to be defined anywhere anymore.
96bf215415Sdsl 		 * The new function should return the entire 512 byte area.
97a499e569Sfvdl 		 */
98bf215415Sdsl 		req.datalen = com == LINUX_HDIO_GET_IDENTITY ? 512 : 142;
99bf215415Sdsl 		req.timeout = 1000;
100a9ca7a37Sad 		error = ioctlf(fp, ATAIOCCOMMAND, &req);
101bf215415Sdsl 		if (error != 0)
102bf215415Sdsl 			break;
103bf215415Sdsl 		if (req.retsts != ATACMD_OK)
104bf215415Sdsl 			error = EIO;
105a499e569Sfvdl 		break;
106a499e569Sfvdl 	case LINUX_HDIO_GETGEO:
10795e1ffb1Schristos 		error = linux_machdepioctl(l, uap, retval);
108a499e569Sfvdl 		if (error == 0)
109a499e569Sfvdl 			break;
1108d10f962Schristos 		error = ioctlf(fp, DIOCGDINFO, &label);
1118d10f962Schristos 		error1 = ioctlf(fp, DIOCGPARTINFO, &pi);
112a499e569Sfvdl 		if (error != 0 && error1 != 0) {
113a499e569Sfvdl 			error = error1;
114a499e569Sfvdl 			break;
115a499e569Sfvdl 		}
116*41aa5859Sriastradh 		memset(&hdg, 0, sizeof(hdg));
1178d10f962Schristos 		hdg.start = error1 != 0 ? pi.pi_offset : 0;
1188d10f962Schristos 		hdg.heads = label.d_ntracks;
1198d10f962Schristos 		hdg.cylinders = label.d_ncylinders;
1208d10f962Schristos 		hdg.sectors = label.d_nsectors;
121a499e569Sfvdl 		error = copyout(&hdg, SCARG(uap, data), sizeof hdg);
122a499e569Sfvdl 		break;
123a499e569Sfvdl 	case LINUX_HDIO_GETGEO_BIG:
12495e1ffb1Schristos 		error = linux_machdepioctl(l, uap, retval);
125a499e569Sfvdl 		if (error == 0)
126a499e569Sfvdl 			break;
127fbffadb9Smrg 		/* FALLTHROUGH */
128a499e569Sfvdl 	case LINUX_HDIO_GETGEO_BIG_RAW:
1298d10f962Schristos 		error = ioctlf(fp, DIOCGDINFO, &label);
1308d10f962Schristos 		error1 = ioctlf(fp, DIOCGPARTINFO, &pi);
131a499e569Sfvdl 		if (error != 0 && error1 != 0) {
132a499e569Sfvdl 			error = error1;
133a499e569Sfvdl 			break;
134a499e569Sfvdl 		}
135*41aa5859Sriastradh 		memset(&hdg_big, 0, sizeof(hdg_big));
1368d10f962Schristos 		hdg_big.start = error1 != 0 ? pi.pi_offset : 0;
1378d10f962Schristos 		hdg_big.heads = label.d_ntracks;
1388d10f962Schristos 		hdg_big.cylinders = label.d_ncylinders;
1398d10f962Schristos 		hdg_big.sectors = label.d_nsectors;
140a499e569Sfvdl 		error = copyout(&hdg_big, SCARG(uap, data), sizeof hdg_big);
141a499e569Sfvdl 		break;
142a499e569Sfvdl 	case LINUX_HDIO_GET_UNMASKINTR:
143a499e569Sfvdl 	case LINUX_HDIO_GET_MULTCOUNT:
144a499e569Sfvdl 	case LINUX_HDIO_GET_KEEPSETTINGS:
145a499e569Sfvdl 	case LINUX_HDIO_GET_32BIT:
146a499e569Sfvdl 	case LINUX_HDIO_GET_NOWERR:
147a499e569Sfvdl 	case LINUX_HDIO_GET_DMA:
148a499e569Sfvdl 	case LINUX_HDIO_GET_NICE:
149a499e569Sfvdl 	case LINUX_HDIO_DRIVE_RESET:
150a499e569Sfvdl 	case LINUX_HDIO_TRISTATE_HWIF:
151a499e569Sfvdl 	case LINUX_HDIO_DRIVE_TASK:
152a499e569Sfvdl 	case LINUX_HDIO_DRIVE_CMD:
153a499e569Sfvdl 	case LINUX_HDIO_SET_MULTCOUNT:
154a499e569Sfvdl 	case LINUX_HDIO_SET_UNMASKINTR:
155a499e569Sfvdl 	case LINUX_HDIO_SET_KEEPSETTINGS:
156a499e569Sfvdl 	case LINUX_HDIO_SET_32BIT:
157a499e569Sfvdl 	case LINUX_HDIO_SET_NOWERR:
158a499e569Sfvdl 	case LINUX_HDIO_SET_DMA:
159a499e569Sfvdl 	case LINUX_HDIO_SET_PIO_MODE:
160a499e569Sfvdl 	case LINUX_HDIO_SCAN_HWIF:
161a499e569Sfvdl 	case LINUX_HDIO_SET_NICE:
162a499e569Sfvdl 	case LINUX_HDIO_UNREGISTER_HWIF:
163a499e569Sfvdl 		error = EINVAL;
164a499e569Sfvdl 	}
165a499e569Sfvdl 
166a9ca7a37Sad 	fd_putfile(SCARG(uap, fd));
167a499e569Sfvdl 
168a499e569Sfvdl 	return error;
169a499e569Sfvdl }
170