1 /*        $NetBSD: rf_compat80.c,v 1.17 2022/06/28 03:13:27 oster Exp $         */
2 
3 /*
4  * Copyright (c) 2017 Matthew R. Green
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/module.h>
33 
34 #include <sys/compat_stub.h>
35 
36 #include <dev/raidframe/raidframeio.h>
37 #include <dev/raidframe/raidframevar.h>
38 
39 #include "rf_raid.h"
40 #include "rf_compat80.h"
41 #include "rf_kintf.h"
42 
43 /* NetBSD 8.99.x removed the row, raidPtr and next members */
44 struct rf_recon_req80 {
45           RF_RowCol_t row, col;
46           RF_ReconReqFlags_t flags;
47           void   *raidPtr;    /* used internally; need not be set at ioctl
48                                          * time */
49           struct rf_recon_req *next;    /* used internally; need not be set at
50                                                    * ioctl time */
51 };
52 
53 /* NetBSD 8.99.x made this structure alignment neutral */
54 typedef struct RF_RaidDisk_s80 {
55         char    devname[56];    /* name of device file */
56         RF_DiskStatus_t status; /* whether it is up or down */
57         RF_RowCol_t spareRow;   /* if in status "spared", this identifies the
58                                  * spare disk */
59         RF_RowCol_t spareCol;   /* if in status "spared", this identifies the
60                                  * spare disk */
61         RF_SectorCount_t numBlocks;     /* number of blocks, obtained via READ
62                                          * CAPACITY */
63         int     blockSize;
64         RF_SectorCount_t partitionSize; /* The *actual* and *full* size of
65                                            the partition, from the disklabel */
66         int     auto_configured;/* 1 if this component was autoconfigured.
67                                    0 otherwise. */
68         dev_t   dev;
69 } RF_RaidDisk_t80;
70 
71 typedef struct RF_DeviceConfig_s80 {
72           u_int   rows;
73           u_int   cols;
74           u_int   maxqdepth;
75           int     ndevs;
76           RF_RaidDisk_t80 devs[RF_MAX_DISKS];
77           int     nspares;
78           RF_RaidDisk_t80 spares[RF_MAX_DISKS];
79 } RF_DeviceConfig_t80;
80 
81 typedef struct RF_Config_s80 {
82           RF_RowCol_t numRow, numCol, numSpare;   /* number of rows, columns,
83                                                              * and spare disks */
84           dev_t   devs[RF_MAXROW][RF_MAXCOL];     /* device numbers for disks
85                                                              * comprising array */
86           char    devnames[RF_MAXROW][RF_MAXCOL][50];       /* device names */
87           dev_t   spare_devs[RF_MAXSPARE];        /* device numbers for spare
88                                                              * disks */
89           char    spare_names[RF_MAXSPARE][50];   /* device names */
90           RF_SectorNum_t sectPerSU;     /* sectors per stripe unit */
91           RF_StripeNum_t SUsPerPU;/* stripe units per parity unit */
92           RF_StripeNum_t SUsPerRU;/* stripe units per reconstruction unit */
93           RF_ParityConfig_t parityConfig;         /* identifies the RAID architecture to
94                                                    * be used */
95           RF_DiskQueueType_t diskQueueType;       /* 'f' = fifo, 'c' = cvscan,
96                                                              * not used in kernel */
97           char    maxOutstandingDiskReqs;         /* # concurrent reqs to be sent to a
98                                                    * disk.  not used in kernel. */
99           char    debugVars[RF_MAXDBGV][50];      /* space for specifying debug
100                                                              * variables & their values */
101           unsigned int layoutSpecificSize;        /* size in bytes of
102                                                              * layout-specific info */
103           void   *layoutSpecific;       /* a pointer to a layout-specific structure to
104                                          * be copied in */
105           int     force;                          /* if !0, ignore many fatal
106                                                                configuration conditions */
107           /*
108              "force" is used to override cases where the component labels would
109              indicate that configuration should not proceed without user
110              intervention
111            */
112 } RF_Config_t80;
113 
114 static int
rf_check_recon_status_ext80(RF_Raid_t * raidPtr,void * data)115 rf_check_recon_status_ext80(RF_Raid_t *raidPtr, void *data)
116 {
117           RF_ProgressInfo_t info, **infoPtr = data;
118 
119           rf_check_recon_status_ext(raidPtr, &info);
120           return copyout(&info, *infoPtr, sizeof(info));
121 }
122 
123 static int
rf_check_parityrewrite_status_ext80(RF_Raid_t * raidPtr,void * data)124 rf_check_parityrewrite_status_ext80(RF_Raid_t *raidPtr, void *data)
125 {
126           RF_ProgressInfo_t info, **infoPtr = data;
127 
128           rf_check_parityrewrite_status_ext(raidPtr, &info);
129           return copyout(&info, *infoPtr, sizeof(info));
130 }
131 
132 static int
rf_check_copyback_status_ext80(RF_Raid_t * raidPtr,void * data)133 rf_check_copyback_status_ext80(RF_Raid_t *raidPtr, void *data)
134 {
135           RF_ProgressInfo_t info, **infoPtr = data;
136 
137           rf_check_copyback_status_ext(raidPtr, &info);
138           return copyout(&info, *infoPtr, sizeof(info));
139 }
140 
141 static void
rf_copy_raiddisk80(RF_RaidDisk_t * disk,RF_RaidDisk_t80 * disk80)142 rf_copy_raiddisk80(RF_RaidDisk_t *disk, RF_RaidDisk_t80 *disk80)
143 {
144 
145           /* Be sure the padding areas don't have kernel memory. */
146           memset(disk80, 0, sizeof(*disk80));
147           memcpy(disk80->devname, disk->devname, sizeof(disk80->devname));
148           disk80->status = disk->status;
149           disk80->spareRow = 0;
150           disk80->spareCol = disk->spareCol;
151           disk80->numBlocks = disk->numBlocks;
152           disk80->blockSize = disk->blockSize;
153           disk80->partitionSize = disk->partitionSize;
154           disk80->auto_configured = disk->auto_configured;
155           disk80->dev = disk->dev;
156 }
157 
158 static int
rf_get_info80(RF_Raid_t * raidPtr,void * data)159 rf_get_info80(RF_Raid_t *raidPtr, void *data)
160 {
161           RF_DeviceConfig_t *config;
162           RF_DeviceConfig_t80 *config80, **configPtr80 = data;
163           int rv;
164 
165           config = RF_Malloc(sizeof(*config));
166           if (config == NULL)
167                     return ENOMEM;
168           config80 = RF_Malloc(sizeof(*config80));
169           if (config80 == NULL) {
170                     RF_Free(config, sizeof(*config));
171                     return ENOMEM;
172           }
173           rv = rf_get_info(raidPtr, config);
174           if (rv == 0) {
175                     /* convert new to old */
176                     config80->rows = 1;
177                     config80->cols = config->cols;
178                     config80->maxqdepth = config->maxqdepth;
179                     config80->ndevs = config->ndevs;
180                     config80->nspares = config->nspares;
181                     for (size_t i = 0; i < RF_MAX_DISKS; i++) {
182                               rf_copy_raiddisk80(&config->devs[i],
183                                                      &config80->devs[i]);
184                               rf_copy_raiddisk80(&config->spares[i],
185                                                      &config80->spares[i]);
186                     }
187                     rv = copyout(config80, *configPtr80, sizeof(*config80));
188           }
189           RF_Free(config, sizeof(*config));
190           RF_Free(config80, sizeof(*config80));
191 
192           return rv;
193 }
194 
195 static int
rf_get_component_label80(RF_Raid_t * raidPtr,void * data)196 rf_get_component_label80(RF_Raid_t *raidPtr, void *data)
197 {
198           RF_ComponentLabel_t **clabel_ptr = (RF_ComponentLabel_t **)data;
199           RF_ComponentLabel_t *clabel;
200           int retcode;
201 
202           /*
203            * Perhaps there should be an option to skip the in-core
204            * copy and hit the disk, as with disklabel(8).
205            */
206           clabel = RF_Malloc(sizeof(*clabel));
207           if (clabel == NULL)
208                     return ENOMEM;
209           retcode = copyin(*clabel_ptr, clabel, sizeof(*clabel));
210           if (retcode) {
211                     RF_Free(clabel, sizeof(*clabel));
212                     return retcode;
213           }
214 
215           rf_get_component_label(raidPtr, clabel);
216           /* Fix-up for userland. */
217           if (clabel->version == bswap32(RF_COMPONENT_LABEL_VERSION))
218                     clabel->version = RF_COMPONENT_LABEL_VERSION;
219 
220           retcode = copyout(clabel, *clabel_ptr, sizeof(**clabel_ptr));
221           RF_Free(clabel, sizeof(*clabel));
222 
223           return retcode;
224 }
225 
226 static int
rf_config80(struct raid_softc * rs,void * data)227 rf_config80(struct raid_softc *rs, void *data)
228 {
229           RF_Config_t80 *u80_cfg, *k80_cfg;
230           RF_Config_t *k_cfg;
231           RF_Raid_t *raidPtr = rf_get_raid(rs);
232           size_t i, j;
233           int error;
234 
235           if (raidPtr->valid) {
236                     /* There is a valid RAID set running on this unit! */
237                     printf("raid%d: Device already configured!\n", rf_get_unit(rs));
238                     return EINVAL;
239           }
240 
241           /* copy-in the configuration information */
242           /* data points to a pointer to the configuration structure */
243 
244           u80_cfg = *((RF_Config_t80 **) data);
245           k80_cfg = RF_Malloc(sizeof(*k80_cfg));
246           if (k80_cfg == NULL)
247                     return ENOMEM;
248 
249           error = copyin(u80_cfg, k80_cfg, sizeof(*k80_cfg));
250           if (error) {
251                     RF_Free(k80_cfg, sizeof(*k80_cfg));
252                     return error;
253           }
254           k_cfg = RF_Malloc(sizeof(*k_cfg));
255           if (k_cfg == NULL) {
256                     RF_Free(k80_cfg, sizeof(*k80_cfg));
257                     return ENOMEM;
258           }
259 
260           k_cfg->numCol = k80_cfg->numCol;
261           k_cfg->numSpare = k80_cfg->numSpare;
262 
263           for (i = 0; i < RF_MAXROW; i++)
264                     for (j = 0; j < RF_MAXCOL; j++)
265                               k_cfg->devs[i][j] = k80_cfg->devs[i][j];
266 
267           memcpy(k_cfg->devnames, k80_cfg->devnames,
268               sizeof(k_cfg->devnames));
269 
270           for (i = 0; i < RF_MAXSPARE; i++)
271                     k_cfg->spare_devs[i] = k80_cfg->spare_devs[i];
272 
273           memcpy(k_cfg->spare_names, k80_cfg->spare_names,
274               sizeof(k_cfg->spare_names));
275 
276           k_cfg->sectPerSU = k80_cfg->sectPerSU;
277           k_cfg->SUsPerPU = k80_cfg->SUsPerPU;
278           k_cfg->SUsPerRU = k80_cfg->SUsPerRU;
279           k_cfg->parityConfig = k80_cfg->parityConfig;
280 
281           memcpy(k_cfg->diskQueueType, k80_cfg->diskQueueType,
282               sizeof(k_cfg->diskQueueType));
283 
284           k_cfg->maxOutstandingDiskReqs = k80_cfg->maxOutstandingDiskReqs;
285 
286           memcpy(k_cfg->debugVars, k80_cfg->debugVars,
287               sizeof(k_cfg->debugVars));
288 
289           k_cfg->layoutSpecificSize = k80_cfg->layoutSpecificSize;
290           k_cfg->layoutSpecific = k80_cfg->layoutSpecific;
291           k_cfg->force = k80_cfg->force;
292 
293           RF_Free(k80_cfg, sizeof(*k80_cfg));
294           return rf_construct(rs, k_cfg);
295 }
296 
297 static int
rf_fail_disk80(RF_Raid_t * raidPtr,struct rf_recon_req80 * req80)298 rf_fail_disk80(RF_Raid_t *raidPtr, struct rf_recon_req80 *req80)
299 {
300           struct rf_recon_req req = {
301                     .col = req80->col,
302                     .flags = req80->flags,
303           };
304           return rf_fail_disk(raidPtr, &req);
305 }
306 
307 static int
raidframe_ioctl_80(struct raid_softc * rs,u_long cmd,void * data)308 raidframe_ioctl_80(struct raid_softc *rs, u_long cmd, void *data)
309 {
310           RF_Raid_t *raidPtr = rf_get_raid(rs);
311 
312           switch (cmd) {
313           case RAIDFRAME_CHECK_RECON_STATUS_EXT80:
314           case RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT80:
315           case RAIDFRAME_CHECK_COPYBACK_STATUS_EXT80:
316           case RAIDFRAME_GET_INFO80:
317           case RAIDFRAME_GET_COMPONENT_LABEL80:
318           case RAIDFRAME_FAIL_DISK80:
319                     if (!rf_inited(rs))
320                               return ENXIO;
321                     break;
322           case RAIDFRAME_CONFIGURE80:
323                     break;
324           default:
325                     return EPASSTHROUGH;
326           }
327 
328           switch (cmd) {
329           case RAIDFRAME_CHECK_RECON_STATUS_EXT80:
330                     return rf_check_recon_status_ext80(raidPtr, data);
331           case RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT80:
332                     return rf_check_parityrewrite_status_ext80(raidPtr, data);
333           case RAIDFRAME_CHECK_COPYBACK_STATUS_EXT80:
334                     return rf_check_copyback_status_ext80(raidPtr, data);
335           case RAIDFRAME_GET_INFO80:
336                     return rf_get_info80(raidPtr, data);
337           case RAIDFRAME_GET_COMPONENT_LABEL80:
338                     return rf_get_component_label80(raidPtr, data);
339           case RAIDFRAME_CONFIGURE80:
340                     return rf_config80(rs, data);
341           case RAIDFRAME_FAIL_DISK80:
342                     return rf_fail_disk80(raidPtr, data);
343           default:
344                     /* abort really */
345                     return EPASSTHROUGH;
346           }
347 }
348 
349 static void
raidframe_80_init(void)350 raidframe_80_init(void)
351 {
352 
353           MODULE_HOOK_SET(raidframe_ioctl_80_hook, raidframe_ioctl_80);
354 }
355 
356 static void
raidframe_80_fini(void)357 raidframe_80_fini(void)
358 {
359 
360           MODULE_HOOK_UNSET(raidframe_ioctl_80_hook);
361 }
362 
363 MODULE(MODULE_CLASS_EXEC, compat_raid_80, "raid,compat_80");
364 
365 static int
compat_raid_80_modcmd(modcmd_t cmd,void * arg)366 compat_raid_80_modcmd(modcmd_t cmd, void *arg)
367 {
368 
369           switch (cmd) {
370           case MODULE_CMD_INIT:
371                     raidframe_80_init();
372                     return 0;
373           case MODULE_CMD_FINI:
374                     raidframe_80_fini();
375                     return 0;
376           default:
377                     return ENOTTY;
378           }
379 }
380