1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
/* $NetBSD: ata_raid.c,v 1.45 2021/08/07 16:19:09 thorpej Exp $ */
/*
* Copyright (c) 2003 Wasabi Systems, Inc.
* All rights reserved.
*
* Written by Jason R. Thorpe for Wasabi Systems, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed for the NetBSD Project by
* Wasabi Systems, Inc.
* 4. The name of Wasabi Systems, Inc. may not be used to endorse
* or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Support for autoconfiguration of RAID sets on ATA RAID controllers.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ata_raid.c,v 1.45 2021/08/07 16:19:09 thorpej Exp $");
#include <sys/param.h>
#include <sys/buf.h>
#include <sys/bufq.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/disk.h>
#include <sys/disklabel.h>
#include <sys/fcntl.h>
#include <sys/vnode.h>
#include <sys/proc.h>
#include <sys/module.h>
#include <miscfs/specfs/specdev.h>
#include <dev/ata/atareg.h>
#include <dev/ata/atavar.h>
#include <dev/ata/wdvar.h>
#include <dev/ata/ata_raidreg.h>
#include <dev/ata/ata_raidvar.h>
#include "locators.h"
#include "ioconf.h"
#ifdef ATA_RAID_DEBUG
#define DPRINTF(x) printf x
#else
#define DPRINTF(x) /* nothing */
#endif
static int ataraid_match(device_t, cfdata_t, void *);
static void ataraid_attach(device_t, device_t, void *);
static int ataraid_rescan(device_t, const char *, const int *);
static int ataraid_print(void *, const char *);
static int ata_raid_finalize(device_t);
static int finalize_done;
ataraid_array_info_list_t ataraid_array_info_list =
TAILQ_HEAD_INITIALIZER(ataraid_array_info_list);
u_int ataraid_array_info_count;
CFATTACH_DECL3_NEW(ataraid, 0,
ataraid_match, ataraid_attach, NULL, NULL, ataraid_rescan, NULL, 0);
/*
* ataraidattach:
*
* Pseudo-device attach routine.
*/
void
ataraidattach(int count)
{
/*
* Register a finalizer which will be used to actually configure
* the logical disks configured by ataraid.
*/
finalize_done = 0;
if (config_finalize_register(NULL, ata_raid_finalize) != 0)
aprint_normal("WARNING: "
"unable to register ATA RAID finalizer\n");
}
/*
* Use the config_finalizer to rescan for new devices, since the
* ld_ataraid driver might not be immediately available.
*/
/* ARGSUSED */
static int
ataraid_rescan(device_t self, const char *ifattr, const int *locs)
{
finalize_done = 0;
(void)ata_raid_finalize(self);
return 0;
}
/*
* ata_raid_type_name:
*
* Return the type of ATA RAID.
*/
const char *
ata_raid_type_name(u_int type)
{
static const char *ata_raid_type_names[] = {
"Promise",
"Adaptec",
"VIA V-RAID",
"nVidia",
"JMicron",
"Intel MatrixRAID"
};
if (type < __arraycount(ata_raid_type_names))
return ata_raid_type_names[type];
return NULL;
}
/*
* ata_raid_finalize:
*
* Autoconfiguration finalizer for ATA RAID.
*/
static int
ata_raid_finalize(device_t self)
{
static struct cfdata ataraid_cfdata = {
.cf_name = "ataraid",
.cf_atname = "ataraid",
.cf_unit = 0,
.cf_fstate = FSTATE_STAR,
};
/*
* Only run once for each instantiation
*/
if (finalize_done)
return 0;
finalize_done = 1;
if (TAILQ_EMPTY(&ataraid_array_info_list))
goto out;
if (config_attach_pseudo(&ataraid_cfdata) == NULL)
printf("%s: unable to attach an instance\n",
ataraid_cd.cd_name);
out:
return 1;
}
/*
* ataraid_match:
*
* Autoconfiguration glue: match routine.
*/
static int
ataraid_match(device_t parent, cfdata_t cf, void *aux)
{
/* pseudo-device; always present */
return 1;
}
/*
* ataraid_attach:
*
* Autoconfiguration glue: attach routine. We attach the children.
*/
static void
ataraid_attach(device_t parent, device_t self, void *aux)
{
struct ataraid_array_info *aai;
int locs[ATARAIDCF_NLOCS];
/*
* We're a pseudo-device, so we get to announce our own
* presence.
*/
aprint_normal_dev(self, "found %u RAID volume%s\n",
ataraid_array_info_count,
ataraid_array_info_count == 1 ? "" : "s");
TAILQ_FOREACH(aai, &ataraid_array_info_list, aai_list) {
locs[ATARAIDCF_VENDTYPE] = aai->aai_type;
locs[ATARAIDCF_UNIT] = aai->aai_arrayno;
config_found(self, aai, ataraid_print,
CFARGS(.submatch = config_stdsubmatch,
.locators = locs));
}
}
/*
* ataraid_print:
*
* Autoconfiguration glue: print routine.
*/
static int
ataraid_print(void *aux, const char *pnp)
{
struct ataraid_array_info *aai = aux;
if (pnp != NULL)
aprint_normal("block device at %s", pnp);
aprint_normal(" vendtype %d unit %d", aai->aai_type, aai->aai_arrayno);
return UNCONF;
}
/*
* ata_raid_check_component:
*
* Check the component for a RAID configuration structure.
* Called via autoconfiguration callback.
*/
void
ata_raid_check_component(device_t self)
{
struct wd_softc *sc = device_private(self);
if (ata_raid_read_config_adaptec(sc) == 0)
return;
if (ata_raid_read_config_promise(sc) == 0)
return;
if (ata_raid_read_config_via(sc) == 0)
return;
if (ata_raid_read_config_nvidia(sc) == 0)
return;
if (ata_raid_read_config_jmicron(sc) == 0)
return;
if (ata_raid_read_config_intel(sc) == 0)
return;
}
struct ataraid_array_info *
ata_raid_get_array_info(u_int type, u_int arrayno)
{
struct ataraid_array_info *aai, *laai;
TAILQ_FOREACH(aai, &ataraid_array_info_list, aai_list) {
if (aai->aai_type == type &&
aai->aai_arrayno == arrayno)
goto out;
}
/* Need to allocate a new one. */
aai = kmem_zalloc(sizeof(*aai), KM_SLEEP);
aai->aai_type = type;
aai->aai_arrayno = arrayno;
aai->aai_curdisk = 0;
ataraid_array_info_count++;
/* Sort it into the list: type first, then array number. */
TAILQ_FOREACH(laai, &ataraid_array_info_list, aai_list) {
if (aai->aai_type < laai->aai_type) {
TAILQ_INSERT_BEFORE(laai, aai, aai_list);
goto out;
}
if (aai->aai_type == laai->aai_type &&
aai->aai_arrayno < laai->aai_arrayno) {
TAILQ_INSERT_BEFORE(laai, aai, aai_list);
goto out;
}
}
TAILQ_INSERT_TAIL(&ataraid_array_info_list, aai, aai_list);
out:
return aai;
}
int
ata_raid_config_block_rw(struct vnode *vp, daddr_t blkno, void *tbuf,
size_t size, int bflags)
{
struct buf *bp;
int error;
bp = getiobuf(vp, false);
bp->b_blkno = blkno;
bp->b_bcount = bp->b_resid = size;
bp->b_flags = bflags;
bp->b_proc = curproc;
bp->b_data = tbuf;
SET(bp->b_cflags, BC_BUSY); /* mark buffer busy */
#ifdef LOCKDOC_VFS
lockdoc_log_lock(P_WRITE, &(bp->b_cflags), __FILE__, __LINE__, __func__, "b_cflags", 0);
#endif
VOP_STRATEGY(vp, bp);
error = biowait(bp);
putiobuf(bp);
return error;
}
MODULE(MODULE_CLASS_DRIVER, ataraid, NULL);
#ifdef _MODULE
CFDRIVER_DECL(ataraid, DV_DISK, NULL);
#endif
static int
ataraid_modcmd(modcmd_t cmd, void *arg)
{
int error = 0;
switch (cmd) {
case MODULE_CMD_INIT:
#ifdef _MODULE
error = config_cfdriver_attach(&ataraid_cd);
if (error)
break;
#endif
error = config_cfattach_attach(ataraid_cd.cd_name, &ataraid_ca);
if (error) {
#ifdef _MODULE
config_cfdriver_detach(&ataraid_cd);
#endif
aprint_error("%s: unable to register cfattach for \n"
"%s, error %d", __func__, ataraid_cd.cd_name,
error);
break;
}
break;
case MODULE_CMD_FINI:
error = config_cfattach_detach(ataraid_cd.cd_name, &ataraid_ca);
if (error) {
aprint_error("%s: failed to detach %s cfattach, "
"error %d\n", __func__, ataraid_cd.cd_name, error);
break;
}
#ifdef _MODULE
error = config_cfdriver_detach(&ataraid_cd);
if (error) {
(void)config_cfattach_attach(ataraid_cd.cd_name,
&ataraid_ca);
aprint_error("%s: failed to detach %s cfdriver, "
"error %d\n", __func__, ataraid_cd.cd_name, error);
break;
}
#endif
break;
case MODULE_CMD_STAT:
default:
error = ENOTTY;
}
return error;
}
|