]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cam/cam_compat.c
Merge bmake-20230909
[FreeBSD/FreeBSD.git] / sys / cam / cam_compat.c
1 /*-
2  * CAM ioctl compatibility shims
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2013 Scott Long
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions, and the following disclaimer,
14  *    without modification, immediately at the beginning of the file.
15  * 2. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/types.h>
36 #include <sys/kernel.h>
37 #include <sys/conf.h>
38 #include <sys/fcntl.h>
39
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/sysctl.h>
43 #include <sys/kthread.h>
44
45 #include <cam/cam.h>
46 #include <cam/cam_ccb.h>
47 #include <cam/cam_xpt.h>
48 #include <cam/cam_compat.h>
49 #include <cam/cam_periph.h>
50
51 #include <cam/scsi/scsi_pass.h>
52
53 #include "opt_cam.h"
54
55 static int cam_compat_handle_0x17(struct cdev *dev, u_long cmd, caddr_t addr,
56     int flag, struct thread *td, d_ioctl_t *cbfnp);
57 static int cam_compat_handle_0x18(struct cdev *dev, u_long cmd, caddr_t addr,
58     int flag, struct thread *td, d_ioctl_t *cbfnp);
59 static int cam_compat_handle_0x19(struct cdev *dev, u_long cmd, caddr_t addr,
60     int flag, struct thread *td, d_ioctl_t *cbfnp);
61 static int cam_compat_translate_dev_match_0x18(union ccb *ccb);
62
63 int
64 cam_compat_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
65     struct thread *td, d_ioctl_t *cbfnp)
66 {
67         int error;
68
69         switch (cmd) {
70         case CAMIOCOMMAND_0x16:
71         {
72                 struct ccb_hdr_0x17 *hdr17;
73
74                 hdr17 = (struct ccb_hdr_0x17 *)addr;
75                 if (hdr17->flags & CAM_SG_LIST_PHYS_0x16) {
76                         hdr17->flags &= ~CAM_SG_LIST_PHYS_0x16;
77                         hdr17->flags |= CAM_DATA_SG_PADDR;
78                 }
79                 if (hdr17->flags & CAM_DATA_PHYS_0x16) {
80                         hdr17->flags &= ~CAM_DATA_PHYS_0x16;
81                         hdr17->flags |= CAM_DATA_PADDR;
82                 }
83                 if (hdr17->flags & CAM_SCATTER_VALID_0x16) {
84                         hdr17->flags &= CAM_SCATTER_VALID_0x16;
85                         hdr17->flags |= CAM_DATA_SG;
86                 }
87                 cmd = CAMIOCOMMAND;
88                 error = cam_compat_handle_0x17(dev, cmd, addr, flag, td, cbfnp);
89                 break;
90         }
91         case CAMGETPASSTHRU_0x16:
92                 cmd = CAMGETPASSTHRU;
93                 error = cam_compat_handle_0x17(dev, cmd, addr, flag, td, cbfnp);
94                 break;
95         case CAMIOCOMMAND_0x17:
96                 cmd = CAMIOCOMMAND;
97                 error = cam_compat_handle_0x17(dev, cmd, addr, flag, td, cbfnp);
98                 break;
99         case CAMGETPASSTHRU_0x17:
100                 cmd = CAMGETPASSTHRU;
101                 error = cam_compat_handle_0x17(dev, cmd, addr, flag, td, cbfnp);
102                 break;
103         case CAMIOCOMMAND_0x18:
104                 cmd = CAMIOCOMMAND;
105                 error = cam_compat_handle_0x18(dev, cmd, addr, flag, td, cbfnp);
106                 break;
107         case CAMGETPASSTHRU_0x18:
108                 cmd = CAMGETPASSTHRU;
109                 error = cam_compat_handle_0x18(dev, cmd, addr, flag, td, cbfnp);
110                 break;
111         case CAMIOCOMMAND_0x19:
112                 cmd = CAMIOCOMMAND;
113                 error = cam_compat_handle_0x19(dev, cmd, addr, flag, td, cbfnp);
114                 break;
115         case CAMGETPASSTHRU_0x19:
116                 cmd = CAMGETPASSTHRU;
117                 error = cam_compat_handle_0x19(dev, cmd, addr, flag, td, cbfnp);
118                 break;
119         case CAMIOQUEUE_0x19:
120                 cmd = CAMIOQUEUE;
121                 error = cam_compat_handle_0x19(dev, cmd, addr, flag, td, cbfnp);
122                 break;
123         case CAMIOGET_0x19:
124                 cmd = CAMIOGET;
125                 error = cam_compat_handle_0x19(dev, cmd, addr, flag, td, cbfnp);
126                 break;
127         default:
128                 error = ENOTTY;
129         }
130
131         return (error);
132 }
133
134 static int
135 cam_compat_handle_0x17(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
136     struct thread *td, d_ioctl_t *cbfnp)
137 {
138         union ccb               *ccb;
139         struct ccb_hdr          *hdr;
140         struct ccb_hdr_0x17     *hdr17;
141         uint8_t                 *ccbb, *ccbb17;
142         u_int                   error;
143
144         hdr17 = (struct ccb_hdr_0x17 *)addr;
145         ccb = xpt_alloc_ccb();
146         hdr = &ccb->ccb_h;
147
148         hdr->pinfo = hdr17->pinfo;
149         hdr->xpt_links = hdr17->xpt_links;
150         hdr->sim_links = hdr17->sim_links;
151         hdr->periph_links = hdr17->periph_links;
152         hdr->retry_count = hdr17->retry_count;
153         hdr->cbfcnp = hdr17->cbfcnp;
154         hdr->func_code = hdr17->func_code;
155         hdr->status = hdr17->status;
156         hdr->path = hdr17->path;
157         hdr->path_id = hdr17->path_id;
158         hdr->target_id = hdr17->target_id;
159         hdr->target_lun = hdr17->target_lun;
160         hdr->flags = hdr17->flags;
161         hdr->xflags = 0;
162         hdr->periph_priv = hdr17->periph_priv;
163         hdr->sim_priv = hdr17->sim_priv;
164         hdr->timeout = hdr17->timeout;
165         hdr->softtimeout.tv_sec = 0;
166         hdr->softtimeout.tv_usec = 0;
167
168         ccbb = (uint8_t *)&hdr[1];
169         ccbb17 = (uint8_t *)&hdr17[1];
170         if (ccb->ccb_h.func_code == XPT_SET_TRAN_SETTINGS) {
171                 struct ccb_trans_settings *cts;
172                 struct ccb_trans_settings_0x17 *cts17;
173
174                 cts = &ccb->cts;
175                 cts17 = (struct ccb_trans_settings_0x17 *)hdr17;
176                 cts->type = cts17->type;
177                 cts->protocol = cts17->protocol;
178                 cts->protocol_version = cts17->protocol_version;
179                 cts->transport = cts17->transport;
180                 cts->transport_version = cts17->transport_version;
181                 bcopy(&cts17->proto_specific, &cts->proto_specific,
182                     sizeof(cts17->proto_specific));
183                 bcopy(&cts17->xport_specific, &cts->xport_specific,
184                     sizeof(cts17->xport_specific));
185         } else {
186                 bcopy(ccbb17, ccbb, CAM_0X17_DATA_LEN);
187         }
188
189         error = cam_compat_handle_0x19(dev, cmd, (caddr_t)ccb, flag, td, cbfnp);
190
191         hdr17->pinfo = hdr->pinfo;
192         hdr17->xpt_links = hdr->xpt_links;
193         hdr17->sim_links = hdr->sim_links;
194         hdr17->periph_links = hdr->periph_links;
195         hdr17->retry_count = hdr->retry_count;
196         hdr17->cbfcnp = hdr->cbfcnp;
197         hdr17->func_code = hdr->func_code;
198         hdr17->status = hdr->status;
199         hdr17->path = hdr->path;
200         hdr17->path_id = hdr->path_id;
201         hdr17->target_id = hdr->target_id;
202         hdr17->target_lun = hdr->target_lun;
203         hdr17->flags = hdr->flags;
204         hdr17->periph_priv = hdr->periph_priv;
205         hdr17->sim_priv = hdr->sim_priv;
206         hdr17->timeout = hdr->timeout;
207
208         if (ccb->ccb_h.func_code == XPT_PATH_INQ) {
209                 struct ccb_pathinq      *cpi;
210                 struct ccb_pathinq_0x17 *cpi17;
211
212                 /* The PATH_INQ only needs special handling on the way out */
213                 cpi = &ccb->cpi;
214                 cpi17 = (struct ccb_pathinq_0x17 *)hdr17;
215                 cpi17->version_num = cpi->version_num;
216                 cpi17->hba_inquiry = cpi->hba_inquiry;
217                 cpi17->target_sprt = (uint8_t)cpi->target_sprt;
218                 cpi17->hba_misc = (uint8_t)cpi->hba_misc;
219                 cpi17->hba_eng_cnt = cpi->hba_eng_cnt;
220                 bcopy(&cpi->vuhba_flags[0], &cpi17->vuhba_flags[0], VUHBALEN);
221                 cpi17->max_target = cpi->max_target;
222                 cpi17->max_lun = cpi->max_lun;
223                 cpi17->async_flags = cpi->async_flags;
224                 cpi17->hpath_id = cpi->hpath_id;
225                 cpi17->initiator_id = cpi->initiator_id;
226                 bcopy(&cpi->sim_vid[0], &cpi17->sim_vid[0], SIM_IDLEN);
227                 bcopy(&cpi->hba_vid[0], &cpi17->hba_vid[0], HBA_IDLEN);
228                 bcopy(&cpi->dev_name[0], &cpi17->dev_name[0], DEV_IDLEN);
229                 cpi17->unit_number = cpi->unit_number;
230                 cpi17->bus_id = cpi->bus_id;
231                 cpi17->base_transfer_speed = cpi->base_transfer_speed;
232                 cpi17->protocol = cpi->protocol;
233                 cpi17->protocol_version = cpi->protocol_version;
234                 cpi17->transport = cpi->transport;
235                 cpi17->transport_version = cpi->transport_version;
236                 bcopy(&cpi->xport_specific, &cpi17->xport_specific,
237                     PATHINQ_SETTINGS_SIZE);
238                 cpi17->maxio = cpi->maxio;
239                 cpi17->hba_vendor = cpi->hba_vendor;
240                 cpi17->hba_device = cpi->hba_device;
241                 cpi17->hba_subvendor = cpi->hba_subvendor;
242                 cpi17->hba_subdevice = cpi->hba_subdevice;
243         } else if (ccb->ccb_h.func_code == XPT_GET_TRAN_SETTINGS) {
244                 struct ccb_trans_settings *cts;
245                 struct ccb_trans_settings_0x17 *cts17;
246
247                 cts = &ccb->cts;
248                 cts17 = (struct ccb_trans_settings_0x17 *)hdr17;
249                 cts17->type = cts->type;
250                 cts17->protocol = cts->protocol;
251                 cts17->protocol_version = cts->protocol_version;
252                 cts17->transport = cts->transport;
253                 cts17->transport_version = cts->transport_version;
254                 bcopy(&cts->proto_specific, &cts17->proto_specific,
255                     sizeof(cts17->proto_specific));
256                 bcopy(&cts->xport_specific, &cts17->xport_specific,
257                     sizeof(cts17->xport_specific));
258         } else if (ccb->ccb_h.func_code == XPT_DEV_MATCH) {
259                 /* Copy the rest of the header over */
260                 bcopy(ccbb, ccbb17, CAM_0X17_DATA_LEN);
261
262                 cam_compat_translate_dev_match_0x18(ccb);
263         } else {
264                 bcopy(ccbb, ccbb17, CAM_0X17_DATA_LEN);
265         }
266
267         xpt_free_ccb(ccb);
268
269         return (error);
270 }
271
272 static int
273 cam_compat_handle_0x18(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
274     struct thread *td, d_ioctl_t *cbfnp)
275 {
276         union ccb               *ccb;
277         struct ccb_hdr          *hdr;
278         struct ccb_hdr_0x18     *hdr18;
279         uint8_t                 *ccbb, *ccbb18;
280         u_int                   error;
281
282         hdr18 = (struct ccb_hdr_0x18 *)addr;
283         ccb = xpt_alloc_ccb();
284         hdr = &ccb->ccb_h;
285
286         hdr->pinfo = hdr18->pinfo;
287         hdr->xpt_links = hdr18->xpt_links;
288         hdr->sim_links = hdr18->sim_links;
289         hdr->periph_links = hdr18->periph_links;
290         hdr->retry_count = hdr18->retry_count;
291         hdr->cbfcnp = hdr18->cbfcnp;
292         hdr->func_code = hdr18->func_code;
293         hdr->status = hdr18->status;
294         hdr->path = hdr18->path;
295         hdr->path_id = hdr18->path_id;
296         hdr->target_id = hdr18->target_id;
297         hdr->target_lun = hdr18->target_lun;
298         if (hdr18->xflags & CAM_EXTLUN_VALID_0x18)
299                 hdr->target_lun = hdr18->ext_lun;
300         hdr->flags = hdr18->flags;
301         hdr->xflags = hdr18->xflags;
302         hdr->periph_priv = hdr18->periph_priv;
303         hdr->sim_priv = hdr18->sim_priv;
304         hdr->timeout = hdr18->timeout;
305         hdr->softtimeout.tv_sec = 0;
306         hdr->softtimeout.tv_usec = 0;
307
308         ccbb = (uint8_t *)&hdr[1];
309         ccbb18 = (uint8_t *)&hdr18[1];
310         if (ccb->ccb_h.func_code == XPT_SET_TRAN_SETTINGS) {
311                 struct ccb_trans_settings *cts;
312                 struct ccb_trans_settings_0x18 *cts18;
313
314                 cts = &ccb->cts;
315                 cts18 = (struct ccb_trans_settings_0x18 *)hdr18;
316                 cts->type = cts18->type;
317                 cts->protocol = cts18->protocol;
318                 cts->protocol_version = cts18->protocol_version;
319                 cts->transport = cts18->transport;
320                 cts->transport_version = cts18->transport_version;
321                 bcopy(&cts18->proto_specific, &cts->proto_specific,
322                     sizeof(cts18->proto_specific));
323                 bcopy(&cts18->xport_specific, &cts->xport_specific,
324                     sizeof(cts18->xport_specific));
325         } else {
326                 bcopy(ccbb18, ccbb, CAM_0X18_DATA_LEN);
327         }
328
329         error = cam_compat_handle_0x19(dev, cmd, (caddr_t)ccb, flag, td, cbfnp);
330
331         hdr18->pinfo = hdr->pinfo;
332         hdr18->xpt_links = hdr->xpt_links;
333         hdr18->sim_links = hdr->sim_links;
334         hdr18->periph_links = hdr->periph_links;
335         hdr18->retry_count = hdr->retry_count;
336         hdr18->cbfcnp = hdr->cbfcnp;
337         hdr18->func_code = hdr->func_code;
338         hdr18->status = hdr->status;
339         hdr18->path = hdr->path;
340         hdr18->path_id = hdr->path_id;
341         hdr18->target_id = hdr->target_id;
342         hdr18->target_lun = hdr->target_lun;
343         hdr18->ext_lun = hdr->target_lun;
344         hdr18->flags = hdr->flags;
345         hdr18->xflags = hdr->xflags | CAM_EXTLUN_VALID_0x18;
346         hdr18->periph_priv = hdr->periph_priv;
347         hdr18->sim_priv = hdr->sim_priv;
348         hdr18->timeout = hdr->timeout;
349
350         if (ccb->ccb_h.func_code == XPT_GET_TRAN_SETTINGS) {
351                 struct ccb_trans_settings *cts;
352                 struct ccb_trans_settings_0x18 *cts18;
353
354                 cts = &ccb->cts;
355                 cts18 = (struct ccb_trans_settings_0x18 *)hdr18;
356                 cts18->type = cts->type;
357                 cts18->protocol = cts->protocol;
358                 cts18->protocol_version = cts->protocol_version;
359                 cts18->transport = cts->transport;
360                 cts18->transport_version = cts->transport_version;
361                 bcopy(&cts->proto_specific, &cts18->proto_specific,
362                     sizeof(cts18->proto_specific));
363                 bcopy(&cts->xport_specific, &cts18->xport_specific,
364                     sizeof(cts18->xport_specific));
365         } else if (ccb->ccb_h.func_code == XPT_DEV_MATCH) {
366                 bcopy(ccbb, ccbb18, CAM_0X18_DATA_LEN);
367                 cam_compat_translate_dev_match_0x18(ccb);
368         } else {
369                 bcopy(ccbb, ccbb18, CAM_0X18_DATA_LEN);
370         }
371
372         xpt_free_ccb(ccb);
373
374         return (error);
375 }
376
377 static int
378 cam_compat_translate_dev_match_0x18(union ccb *ccb)
379 {
380         struct dev_match_result         *dm;
381         struct dev_match_result_0x18    *dm18;
382         struct cam_periph_map_info      mapinfo;
383         int i;
384
385         /* Remap the CCB into kernel address space */
386         bzero(&mapinfo, sizeof(mapinfo));
387         cam_periph_mapmem(ccb, &mapinfo, maxphys);
388
389         dm = ccb->cdm.matches;
390         /* Translate in-place: old fields are smaller */
391         dm18 = (struct dev_match_result_0x18 *)(dm);
392
393         for (i = 0; i < ccb->cdm.num_matches; i++) {
394                 dm18[i].type = dm[i].type;
395                 switch (dm[i].type) {
396                 case DEV_MATCH_PERIPH:
397                         memcpy(&dm18[i].result.periph_result.periph_name, 
398                             &dm[i].result.periph_result.periph_name,
399                             DEV_IDLEN);
400                         dm18[i].result.periph_result.unit_number =
401                            dm[i].result.periph_result.unit_number;
402                         dm18[i].result.periph_result.path_id =
403                            dm[i].result.periph_result.path_id;
404                         dm18[i].result.periph_result.target_id =
405                            dm[i].result.periph_result.target_id;
406                         dm18[i].result.periph_result.target_lun =
407                            dm[i].result.periph_result.target_lun;
408                         break;
409                 case DEV_MATCH_DEVICE:
410                         dm18[i].result.device_result.path_id =
411                            dm[i].result.device_result.path_id;
412                         dm18[i].result.device_result.target_id =
413                            dm[i].result.device_result.target_id;
414                         dm18[i].result.device_result.target_lun =
415                            dm[i].result.device_result.target_lun;
416                         dm18[i].result.device_result.protocol =
417                            dm[i].result.device_result.protocol;
418                         memcpy(&dm18[i].result.device_result.inq_data, 
419                             &dm[i].result.device_result.inq_data,
420                             sizeof(struct scsi_inquiry_data));
421                         memcpy(&dm18[i].result.device_result.ident_data,
422                             &dm[i].result.device_result.ident_data,
423                             sizeof(struct ata_params));
424                         dm18[i].result.device_result.flags =
425                            dm[i].result.device_result.flags;
426                         break;
427                 case DEV_MATCH_BUS:
428                         memcpy(&dm18[i].result.bus_result, 
429                             &dm[i].result.bus_result,
430                             sizeof(struct bus_match_result));
431                         break;
432                 }
433         }
434
435         cam_periph_unmapmem(ccb, &mapinfo);
436
437         return (0);
438 }
439
440 static int
441 cam_compat_handle_0x19(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
442     struct thread *td, d_ioctl_t *cbfnp)
443 {
444         union ccb *ccb = (union ccb *)addr;
445         struct cam_periph_map_info      mapinfo;
446
447         if (cmd == CAMIOCOMMAND && ccb->ccb_h.func_code == XPT_DEV_MATCH) {
448                 bzero(&mapinfo, sizeof(mapinfo));
449                 cam_periph_mapmem(ccb, &mapinfo, maxphys);
450                 for (int i = 0; i < ccb->cdm.num_patterns; i++) {
451                         struct dev_match_pattern *p = &ccb->cdm.patterns[i];
452
453                         if (p->type == DEV_MATCH_BUS &&
454                             p->pattern.bus_pattern.flags == 0x00f)
455                                 p->pattern.bus_pattern.flags = BUS_MATCH_ANY;
456                         if (p->type == DEV_MATCH_DEVICE &&
457                             p->pattern.device_pattern.flags == 0x00f)
458                                 p->pattern.device_pattern.flags = DEV_MATCH_ANY;
459                         if (p->type == DEV_MATCH_PERIPH &&
460                             p->pattern.periph_pattern.flags == 0x01f)
461                                 p->pattern.periph_pattern.flags = PERIPH_MATCH_ANY;
462                 }
463                 cam_periph_unmapmem(ccb, &mapinfo);
464         }
465         return ((cbfnp)(dev, cmd, addr, flag, td));
466 }