Print this page
8368 remove warlock leftovers from usr/src/uts
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/io/cmlb.c
+++ new/usr/src/uts/common/io/cmlb.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2012 DEY Storage Systems, Inc. All rights reserved.
24 24 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
25 25 * Use is subject to license terms.
26 26 * Copyright 2016 Toomas Soome <tsoome@me.com>
27 27 */
28 28
29 29 /*
30 30 * This module provides support for labeling operations for target
31 31 * drivers.
32 32 */
33 33
34 34 #include <sys/scsi/scsi.h>
35 35 #include <sys/sunddi.h>
36 36 #include <sys/dklabel.h>
37 37 #include <sys/dkio.h>
38 38 #include <sys/vtoc.h>
39 39 #include <sys/dktp/fdisk.h>
40 40 #include <sys/vtrace.h>
41 41 #include <sys/efi_partition.h>
42 42 #include <sys/cmlb.h>
43 43 #include <sys/cmlb_impl.h>
44 44 #if defined(__i386) || defined(__amd64)
45 45 #include <sys/fs/dv_node.h>
46 46 #endif
47 47 #include <sys/ddi_impldefs.h>
48 48
49 49 /*
50 50 * Driver minor node structure and data table
51 51 */
52 52 struct driver_minor_data {
53 53 char *name;
54 54 minor_t minor;
55 55 int type;
56 56 };
57 57
58 58 static struct driver_minor_data dk_minor_data[] = {
59 59 {"a", 0, S_IFBLK},
60 60 {"b", 1, S_IFBLK},
61 61 {"c", 2, S_IFBLK},
62 62 {"d", 3, S_IFBLK},
63 63 {"e", 4, S_IFBLK},
64 64 {"f", 5, S_IFBLK},
65 65 {"g", 6, S_IFBLK},
66 66 {"h", 7, S_IFBLK},
67 67 #if defined(_SUNOS_VTOC_16)
68 68 {"i", 8, S_IFBLK},
69 69 {"j", 9, S_IFBLK},
70 70 {"k", 10, S_IFBLK},
71 71 {"l", 11, S_IFBLK},
72 72 {"m", 12, S_IFBLK},
73 73 {"n", 13, S_IFBLK},
74 74 {"o", 14, S_IFBLK},
75 75 {"p", 15, S_IFBLK},
76 76 #endif /* defined(_SUNOS_VTOC_16) */
77 77 #if defined(_FIRMWARE_NEEDS_FDISK)
78 78 {"q", 16, S_IFBLK},
79 79 {"r", 17, S_IFBLK},
80 80 {"s", 18, S_IFBLK},
81 81 {"t", 19, S_IFBLK},
82 82 {"u", 20, S_IFBLK},
83 83 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
84 84 {"a,raw", 0, S_IFCHR},
85 85 {"b,raw", 1, S_IFCHR},
86 86 {"c,raw", 2, S_IFCHR},
87 87 {"d,raw", 3, S_IFCHR},
88 88 {"e,raw", 4, S_IFCHR},
89 89 {"f,raw", 5, S_IFCHR},
90 90 {"g,raw", 6, S_IFCHR},
91 91 {"h,raw", 7, S_IFCHR},
92 92 #if defined(_SUNOS_VTOC_16)
93 93 {"i,raw", 8, S_IFCHR},
94 94 {"j,raw", 9, S_IFCHR},
95 95 {"k,raw", 10, S_IFCHR},
96 96 {"l,raw", 11, S_IFCHR},
97 97 {"m,raw", 12, S_IFCHR},
98 98 {"n,raw", 13, S_IFCHR},
99 99 {"o,raw", 14, S_IFCHR},
100 100 {"p,raw", 15, S_IFCHR},
101 101 #endif /* defined(_SUNOS_VTOC_16) */
102 102 #if defined(_FIRMWARE_NEEDS_FDISK)
103 103 {"q,raw", 16, S_IFCHR},
104 104 {"r,raw", 17, S_IFCHR},
105 105 {"s,raw", 18, S_IFCHR},
106 106 {"t,raw", 19, S_IFCHR},
107 107 {"u,raw", 20, S_IFCHR},
108 108 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
109 109 {0}
110 110 };
111 111
112 112 #if defined(__i386) || defined(__amd64)
113 113 #if defined(_FIRMWARE_NEEDS_FDISK)
114 114 static struct driver_minor_data dk_ext_minor_data[] = {
115 115 {"p5", 21, S_IFBLK},
116 116 {"p6", 22, S_IFBLK},
117 117 {"p7", 23, S_IFBLK},
118 118 {"p8", 24, S_IFBLK},
119 119 {"p9", 25, S_IFBLK},
120 120 {"p10", 26, S_IFBLK},
121 121 {"p11", 27, S_IFBLK},
122 122 {"p12", 28, S_IFBLK},
123 123 {"p13", 29, S_IFBLK},
124 124 {"p14", 30, S_IFBLK},
125 125 {"p15", 31, S_IFBLK},
126 126 {"p16", 32, S_IFBLK},
127 127 {"p17", 33, S_IFBLK},
128 128 {"p18", 34, S_IFBLK},
129 129 {"p19", 35, S_IFBLK},
130 130 {"p20", 36, S_IFBLK},
131 131 {"p21", 37, S_IFBLK},
132 132 {"p22", 38, S_IFBLK},
133 133 {"p23", 39, S_IFBLK},
134 134 {"p24", 40, S_IFBLK},
135 135 {"p25", 41, S_IFBLK},
136 136 {"p26", 42, S_IFBLK},
137 137 {"p27", 43, S_IFBLK},
138 138 {"p28", 44, S_IFBLK},
139 139 {"p29", 45, S_IFBLK},
140 140 {"p30", 46, S_IFBLK},
141 141 {"p31", 47, S_IFBLK},
142 142 {"p32", 48, S_IFBLK},
143 143 {"p33", 49, S_IFBLK},
144 144 {"p34", 50, S_IFBLK},
145 145 {"p35", 51, S_IFBLK},
146 146 {"p36", 52, S_IFBLK},
147 147 {"p5,raw", 21, S_IFCHR},
148 148 {"p6,raw", 22, S_IFCHR},
149 149 {"p7,raw", 23, S_IFCHR},
150 150 {"p8,raw", 24, S_IFCHR},
151 151 {"p9,raw", 25, S_IFCHR},
152 152 {"p10,raw", 26, S_IFCHR},
153 153 {"p11,raw", 27, S_IFCHR},
154 154 {"p12,raw", 28, S_IFCHR},
155 155 {"p13,raw", 29, S_IFCHR},
156 156 {"p14,raw", 30, S_IFCHR},
157 157 {"p15,raw", 31, S_IFCHR},
158 158 {"p16,raw", 32, S_IFCHR},
159 159 {"p17,raw", 33, S_IFCHR},
160 160 {"p18,raw", 34, S_IFCHR},
161 161 {"p19,raw", 35, S_IFCHR},
162 162 {"p20,raw", 36, S_IFCHR},
163 163 {"p21,raw", 37, S_IFCHR},
164 164 {"p22,raw", 38, S_IFCHR},
165 165 {"p23,raw", 39, S_IFCHR},
166 166 {"p24,raw", 40, S_IFCHR},
167 167 {"p25,raw", 41, S_IFCHR},
168 168 {"p26,raw", 42, S_IFCHR},
169 169 {"p27,raw", 43, S_IFCHR},
170 170 {"p28,raw", 44, S_IFCHR},
171 171 {"p29,raw", 45, S_IFCHR},
172 172 {"p30,raw", 46, S_IFCHR},
173 173 {"p31,raw", 47, S_IFCHR},
174 174 {"p32,raw", 48, S_IFCHR},
175 175 {"p33,raw", 49, S_IFCHR},
176 176 {"p34,raw", 50, S_IFCHR},
177 177 {"p35,raw", 51, S_IFCHR},
178 178 {"p36,raw", 52, S_IFCHR},
179 179 {0}
180 180 };
181 181 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
182 182 #endif /* if defined(__i386) || defined(__amd64) */
183 183
184 184 static struct driver_minor_data dk_minor_data_efi[] = {
185 185 {"a", 0, S_IFBLK},
186 186 {"b", 1, S_IFBLK},
187 187 {"c", 2, S_IFBLK},
188 188 {"d", 3, S_IFBLK},
189 189 {"e", 4, S_IFBLK},
190 190 {"f", 5, S_IFBLK},
191 191 {"g", 6, S_IFBLK},
192 192 {"wd", 7, S_IFBLK},
193 193 #if defined(_SUNOS_VTOC_16)
194 194 {"i", 8, S_IFBLK},
195 195 {"j", 9, S_IFBLK},
196 196 {"k", 10, S_IFBLK},
197 197 {"l", 11, S_IFBLK},
198 198 {"m", 12, S_IFBLK},
199 199 {"n", 13, S_IFBLK},
200 200 {"o", 14, S_IFBLK},
201 201 {"p", 15, S_IFBLK},
202 202 #endif /* defined(_SUNOS_VTOC_16) */
203 203 #if defined(_FIRMWARE_NEEDS_FDISK)
204 204 {"q", 16, S_IFBLK},
205 205 {"r", 17, S_IFBLK},
206 206 {"s", 18, S_IFBLK},
207 207 {"t", 19, S_IFBLK},
208 208 {"u", 20, S_IFBLK},
209 209 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
210 210 {"a,raw", 0, S_IFCHR},
211 211 {"b,raw", 1, S_IFCHR},
212 212 {"c,raw", 2, S_IFCHR},
213 213 {"d,raw", 3, S_IFCHR},
214 214 {"e,raw", 4, S_IFCHR},
215 215 {"f,raw", 5, S_IFCHR},
216 216 {"g,raw", 6, S_IFCHR},
217 217 {"wd,raw", 7, S_IFCHR},
218 218 #if defined(_SUNOS_VTOC_16)
219 219 {"i,raw", 8, S_IFCHR},
220 220 {"j,raw", 9, S_IFCHR},
221 221 {"k,raw", 10, S_IFCHR},
222 222 {"l,raw", 11, S_IFCHR},
223 223 {"m,raw", 12, S_IFCHR},
224 224 {"n,raw", 13, S_IFCHR},
225 225 {"o,raw", 14, S_IFCHR},
226 226 {"p,raw", 15, S_IFCHR},
227 227 #endif /* defined(_SUNOS_VTOC_16) */
228 228 #if defined(_FIRMWARE_NEEDS_FDISK)
229 229 {"q,raw", 16, S_IFCHR},
230 230 {"r,raw", 17, S_IFCHR},
231 231 {"s,raw", 18, S_IFCHR},
232 232 {"t,raw", 19, S_IFCHR},
233 233 {"u,raw", 20, S_IFCHR},
234 234 #endif /* defined(_FIRMWARE_NEEDS_FDISK) */
235 235 {0}
236 236 };
237 237
238 238 /*
239 239 * Declare the dynamic properties implemented in prop_op(9E) implementation
240 240 * that we want to have show up in a di_init(3DEVINFO) device tree snapshot
241 241 * of drivers that call cmlb_attach().
242 242 */
243 243 static i_ddi_prop_dyn_t cmlb_prop_dyn[] = {
244 244 {"Nblocks", DDI_PROP_TYPE_INT64, S_IFBLK},
245 245 {"Size", DDI_PROP_TYPE_INT64, S_IFCHR},
246 246 {"device-nblocks", DDI_PROP_TYPE_INT64},
247 247 {"device-blksize", DDI_PROP_TYPE_INT},
248 248 {"device-solid-state", DDI_PROP_TYPE_INT},
249 249 {"device-rotational", DDI_PROP_TYPE_INT},
250 250 {NULL}
251 251 };
252 252
253 253 /*
254 254 * This implies an upper limit of 8192 GPT partitions
255 255 * in one transfer for GUID Partition Entry Array.
256 256 */
257 257 len_t cmlb_tg_max_efi_xfer = 1024 * 1024;
258 258
259 259 /*
260 260 * External kernel interfaces
261 261 */
262 262 extern struct mod_ops mod_miscops;
263 263
264 264 extern int ddi_create_internal_pathname(dev_info_t *dip, char *name,
265 265 int spec_type, minor_t minor_num);
266 266
267 267 /*
268 268 * Global buffer and mutex for debug logging
269 269 */
270 270 static char cmlb_log_buffer[1024];
271 271 static kmutex_t cmlb_log_mutex;
272 272
273 273
274 274 struct cmlb_lun *cmlb_debug_cl = NULL;
275 275 uint_t cmlb_level_mask = 0x0;
276 276
277 277 int cmlb_rot_delay = 4; /* default rotational delay */
278 278
279 279 static struct modlmisc modlmisc = {
280 280 &mod_miscops, /* Type of module */
281 281 "Common Labeling module"
282 282 };
283 283
284 284 static struct modlinkage modlinkage = {
285 285 MODREV_1, (void *)&modlmisc, NULL
286 286 };
287 287
288 288 /* Local function prototypes */
289 289 static dev_t cmlb_make_device(struct cmlb_lun *cl);
290 290 static int cmlb_validate_geometry(struct cmlb_lun *cl, boolean_t forcerevalid,
291 291 int flags, void *tg_cookie);
292 292 static void cmlb_resync_geom_caches(struct cmlb_lun *cl, diskaddr_t capacity,
293 293 void *tg_cookie);
294 294 static int cmlb_read_fdisk(struct cmlb_lun *cl, diskaddr_t capacity,
295 295 void *tg_cookie);
296 296 static void cmlb_swap_efi_gpt(efi_gpt_t *e);
297 297 static void cmlb_swap_efi_gpe(int nparts, efi_gpe_t *p);
298 298 static int cmlb_validate_efi(efi_gpt_t *labp);
299 299 static int cmlb_use_efi(struct cmlb_lun *cl, diskaddr_t capacity, int flags,
300 300 void *tg_cookie);
301 301 static void cmlb_build_default_label(struct cmlb_lun *cl, void *tg_cookie);
302 302 static int cmlb_uselabel(struct cmlb_lun *cl, struct dk_label *l, int flags);
303 303 #if defined(_SUNOS_VTOC_8)
304 304 static void cmlb_build_user_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc);
305 305 #endif
306 306 static int cmlb_build_label_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc);
307 307 static int cmlb_write_label(struct cmlb_lun *cl, void *tg_cookie);
308 308 static int cmlb_set_vtoc(struct cmlb_lun *cl, struct dk_label *dkl,
309 309 void *tg_cookie);
310 310 static void cmlb_clear_efi(struct cmlb_lun *cl, void *tg_cookie);
311 311 static void cmlb_clear_vtoc(struct cmlb_lun *cl, void *tg_cookie);
312 312 static void cmlb_setup_default_geometry(struct cmlb_lun *cl, void *tg_cookie);
313 313 static int cmlb_create_minor_nodes(struct cmlb_lun *cl);
314 314 static int cmlb_check_update_blockcount(struct cmlb_lun *cl, void *tg_cookie);
315 315 static boolean_t cmlb_check_efi_mbr(uchar_t *buf, boolean_t *is_mbr);
316 316
317 317 #if defined(__i386) || defined(__amd64)
318 318 static int cmlb_update_fdisk_and_vtoc(struct cmlb_lun *cl, void *tg_cookie);
319 319 #endif
320 320
321 321 #if defined(_FIRMWARE_NEEDS_FDISK)
322 322 static boolean_t cmlb_has_max_chs_vals(struct ipart *fdp);
323 323 #endif
324 324
325 325 #if defined(_SUNOS_VTOC_16)
326 326 static void cmlb_convert_geometry(struct cmlb_lun *cl, diskaddr_t capacity,
327 327 struct dk_geom *cl_g, void *tg_cookie);
328 328 #endif
329 329
330 330 static int cmlb_dkio_get_geometry(struct cmlb_lun *cl, caddr_t arg, int flag,
331 331 void *tg_cookie);
332 332 static int cmlb_dkio_set_geometry(struct cmlb_lun *cl, caddr_t arg, int flag);
333 333 static int cmlb_dkio_get_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
334 334 void *tg_cookie);
335 335 static int cmlb_dkio_set_partition(struct cmlb_lun *cl, caddr_t arg, int flag);
336 336 static int cmlb_dkio_get_efi(struct cmlb_lun *cl, caddr_t arg, int flag,
337 337 void *tg_cookie);
338 338 static int cmlb_dkio_set_efi(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
339 339 int flag, void *tg_cookie);
340 340 static int cmlb_dkio_get_vtoc(struct cmlb_lun *cl, caddr_t arg, int flag,
341 341 void *tg_cookie);
342 342 static int cmlb_dkio_get_extvtoc(struct cmlb_lun *cl, caddr_t arg, int flag,
343 343 void *tg_cookie);
344 344 static int cmlb_dkio_set_vtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
345 345 int flag, void *tg_cookie);
346 346 static int cmlb_dkio_set_extvtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
347 347 int flag, void *tg_cookie);
348 348 static int cmlb_dkio_get_mboot(struct cmlb_lun *cl, caddr_t arg, int flag,
349 349 void *tg_cookie);
350 350 static int cmlb_dkio_set_mboot(struct cmlb_lun *cl, caddr_t arg, int flag,
351 351 void *tg_cookie);
352 352 static int cmlb_dkio_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
353 353 void *tg_cookie);
354 354
355 355 #if defined(__i386) || defined(__amd64)
356 356 static int cmlb_dkio_set_ext_part(struct cmlb_lun *cl, caddr_t arg, int flag,
357 357 void *tg_cookie);
358 358 static int cmlb_validate_ext_part(struct cmlb_lun *cl, int part, int epart,
359 359 uint32_t start, uint32_t size);
360 360 static int cmlb_is_linux_swap(struct cmlb_lun *cl, uint32_t part_start,
361 361 void *tg_cookie);
362 362 static int cmlb_dkio_get_virtgeom(struct cmlb_lun *cl, caddr_t arg, int flag);
363 363 static int cmlb_dkio_get_phygeom(struct cmlb_lun *cl, caddr_t arg, int flag,
364 364 void *tg_cookie);
365 365 static int cmlb_dkio_partinfo(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
366 366 int flag);
367 367 static int cmlb_dkio_extpartinfo(struct cmlb_lun *cl, dev_t dev, caddr_t arg,
368 368 int flag);
369 369 #endif
370 370
371 371 static void cmlb_dbg(uint_t comp, struct cmlb_lun *cl, const char *fmt, ...);
372 372 static void cmlb_v_log(dev_info_t *dev, const char *label, uint_t level,
373 373 const char *fmt, va_list ap);
374 374 static void cmlb_log(dev_info_t *dev, const char *label, uint_t level,
375 375 const char *fmt, ...);
376 376
377 377 int
378 378 _init(void)
379 379 {
380 380 mutex_init(&cmlb_log_mutex, NULL, MUTEX_DRIVER, NULL);
381 381 return (mod_install(&modlinkage));
382 382 }
383 383
384 384 int
385 385 _info(struct modinfo *modinfop)
386 386 {
387 387 return (mod_info(&modlinkage, modinfop));
388 388 }
389 389
390 390 int
391 391 _fini(void)
392 392 {
393 393 int err;
394 394
395 395 if ((err = mod_remove(&modlinkage)) != 0) {
396 396 return (err);
397 397 }
398 398
399 399 mutex_destroy(&cmlb_log_mutex);
400 400 return (err);
401 401 }
402 402
403 403 /*
404 404 * cmlb_dbg is used for debugging to log additional info
405 405 * Level of output is controlled via cmlb_level_mask setting.
406 406 */
407 407 static void
408 408 cmlb_dbg(uint_t comp, struct cmlb_lun *cl, const char *fmt, ...)
409 409 {
410 410 va_list ap;
411 411 dev_info_t *dev;
412 412 uint_t level_mask = 0;
413 413
414 414 ASSERT(cl != NULL);
415 415 dev = CMLB_DEVINFO(cl);
416 416 ASSERT(dev != NULL);
417 417 /*
418 418 * Filter messages based on the global component and level masks,
419 419 * also print if cl matches the value of cmlb_debug_cl, or if
420 420 * cmlb_debug_cl is set to NULL.
421 421 */
422 422 if (comp & CMLB_TRACE)
423 423 level_mask |= CMLB_LOGMASK_TRACE;
424 424
425 425 if (comp & CMLB_INFO)
426 426 level_mask |= CMLB_LOGMASK_INFO;
427 427
428 428 if (comp & CMLB_ERROR)
429 429 level_mask |= CMLB_LOGMASK_ERROR;
430 430
431 431 if ((cmlb_level_mask & level_mask) &&
432 432 ((cmlb_debug_cl == NULL) || (cmlb_debug_cl == cl))) {
433 433 va_start(ap, fmt);
434 434 cmlb_v_log(dev, CMLB_LABEL(cl), CE_CONT, fmt, ap);
435 435 va_end(ap);
436 436 }
437 437 }
438 438
439 439 /*
440 440 * cmlb_log is basically a duplicate of scsi_log. It is redefined here
441 441 * so that this module does not depend on scsi module.
442 442 */
443 443 static void
444 444 cmlb_log(dev_info_t *dev, const char *label, uint_t level, const char *fmt, ...)
445 445 {
446 446 va_list ap;
447 447
448 448 va_start(ap, fmt);
449 449 cmlb_v_log(dev, label, level, fmt, ap);
450 450 va_end(ap);
451 451 }
452 452
453 453 static void
454 454 cmlb_v_log(dev_info_t *dev, const char *label, uint_t level, const char *fmt,
455 455 va_list ap)
456 456 {
457 457 static char name[256];
458 458 int log_only = 0;
459 459 int boot_only = 0;
460 460 int console_only = 0;
461 461
462 462 mutex_enter(&cmlb_log_mutex);
463 463
464 464 if (dev) {
465 465 if (level == CE_PANIC || level == CE_WARN ||
466 466 level == CE_NOTE) {
467 467 (void) sprintf(name, "%s (%s%d):\n",
468 468 ddi_pathname(dev, cmlb_log_buffer),
469 469 label, ddi_get_instance(dev));
470 470 } else {
471 471 name[0] = '\0';
472 472 }
473 473 } else {
474 474 (void) sprintf(name, "%s:", label);
475 475 }
476 476
477 477 (void) vsprintf(cmlb_log_buffer, fmt, ap);
478 478
479 479 switch (cmlb_log_buffer[0]) {
480 480 case '!':
481 481 log_only = 1;
482 482 break;
483 483 case '?':
484 484 boot_only = 1;
485 485 break;
486 486 case '^':
487 487 console_only = 1;
488 488 break;
489 489 }
490 490
491 491 switch (level) {
492 492 case CE_NOTE:
493 493 level = CE_CONT;
494 494 /* FALLTHROUGH */
495 495 case CE_CONT:
496 496 case CE_WARN:
497 497 case CE_PANIC:
498 498 if (boot_only) {
499 499 cmn_err(level, "?%s\t%s", name, &cmlb_log_buffer[1]);
500 500 } else if (console_only) {
501 501 cmn_err(level, "^%s\t%s", name, &cmlb_log_buffer[1]);
502 502 } else if (log_only) {
503 503 cmn_err(level, "!%s\t%s", name, &cmlb_log_buffer[1]);
504 504 } else {
505 505 cmn_err(level, "%s\t%s", name, cmlb_log_buffer);
506 506 }
507 507 break;
508 508 case CE_IGNORE:
509 509 break;
510 510 default:
511 511 cmn_err(CE_CONT, "^DEBUG: %s\t%s", name, cmlb_log_buffer);
512 512 break;
513 513 }
514 514 mutex_exit(&cmlb_log_mutex);
515 515 }
516 516
517 517
518 518 /*
519 519 * cmlb_alloc_handle:
520 520 *
521 521 * Allocates a handle.
522 522 *
523 523 * Arguments:
524 524 * cmlbhandlep pointer to handle
525 525 *
526 526 * Notes:
527 527 * Allocates a handle and stores the allocated handle in the area
528 528 * pointed to by cmlbhandlep
529 529 *
530 530 * Context:
531 531 * Kernel thread only (can sleep).
532 532 */
533 533 void
534 534 cmlb_alloc_handle(cmlb_handle_t *cmlbhandlep)
535 535 {
536 536 struct cmlb_lun *cl;
537 537
538 538 cl = kmem_zalloc(sizeof (struct cmlb_lun), KM_SLEEP);
539 539 ASSERT(cmlbhandlep != NULL);
540 540
541 541 cl->cl_state = CMLB_INITED;
542 542 cl->cl_def_labeltype = CMLB_LABEL_UNDEF;
543 543 mutex_init(CMLB_MUTEX(cl), NULL, MUTEX_DRIVER, NULL);
544 544
545 545 *cmlbhandlep = (cmlb_handle_t)(cl);
546 546 }
547 547
548 548 /*
549 549 * cmlb_free_handle
550 550 *
551 551 * Frees handle.
552 552 *
553 553 * Arguments:
554 554 * cmlbhandlep pointer to handle
555 555 */
556 556 void
557 557 cmlb_free_handle(cmlb_handle_t *cmlbhandlep)
558 558 {
559 559 struct cmlb_lun *cl;
560 560
561 561 cl = (struct cmlb_lun *)*cmlbhandlep;
562 562 if (cl != NULL) {
563 563 mutex_destroy(CMLB_MUTEX(cl));
564 564 kmem_free(cl, sizeof (struct cmlb_lun));
565 565 }
566 566
567 567 }
568 568
569 569 /*
570 570 * cmlb_attach:
571 571 *
572 572 * Attach handle to device, create minor nodes for device.
573 573 *
574 574 * Arguments:
575 575 * devi pointer to device's dev_info structure.
576 576 * tgopsp pointer to array of functions cmlb can use to callback
577 577 * to target driver.
578 578 *
579 579 * device_type Peripheral device type as defined in
580 580 * scsi/generic/inquiry.h
581 581 *
582 582 * is_removable whether or not device is removable.
583 583 *
584 584 * is_hotpluggable whether or not device is hotpluggable.
585 585 *
586 586 * node_type minor node type (as used by ddi_create_minor_node)
587 587 *
588 588 * alter_behavior
589 589 * bit flags:
590 590 *
591 591 * CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT: create
592 592 * an alternate slice for the default label, if
593 593 * device type is DTYPE_DIRECT an architectures default
594 594 * label type is VTOC16.
595 595 * Otherwise alternate slice will no be created.
596 596 *
597 597 *
598 598 * CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8: report a default
599 599 * geometry and label for DKIOCGGEOM and DKIOCGVTOC
600 600 * on architecture with VTOC8 label types.
601 601 *
602 602 * CMLB_OFF_BY_ONE: do the workaround for legacy off-by-
603 603 * one bug in obtaining capacity (in sd):
604 604 * SCSI READ_CAPACITY command returns the LBA number of the
605 605 * last logical block, but sd once treated this number as
606 606 * disks' capacity on x86 platform. And LBAs are addressed
607 607 * based 0. So the last block was lost on x86 platform.
608 608 *
609 609 * Now, we remove this workaround. In order for present sd
610 610 * driver to work with disks which are labeled/partitioned
611 611 * via previous sd, we add workaround as follows:
612 612 *
613 613 * 1) Locate backup EFI label: cmlb searches the next to
614 614 * last
615 615 * block for backup EFI label. If fails, it will
616 616 * turn to the last block for backup EFI label;
617 617 *
618 618 * 2) Clear backup EFI label: cmlb first search the last
619 619 * block for backup EFI label, and will search the
620 620 * next to last block only if failed for the last
621 621 * block.
622 622 *
623 623 * 3) Calculate geometry:refer to cmlb_convert_geometry()
624 624 * If capacity increasing by 1 causes disks' capacity
625 625 * to cross over the limits in geometry calculation,
626 626 * geometry info will change. This will raise an issue:
627 627 * In case that primary VTOC label is destroyed, format
628 628 * commandline can restore it via backup VTOC labels.
629 629 * And format locates backup VTOC labels by use of
630 630 * geometry. So changing geometry will
631 631 * prevent format from finding backup VTOC labels. To
632 632 * eliminate this side effect for compatibility,
633 633 * sd uses (capacity -1) to calculate geometry;
634 634 *
635 635 * 4) 1TB disks: some important data structures use
636 636 * 32-bit signed long/int (for example, daddr_t),
637 637 * so that sd doesn't support a disk with capacity
638 638 * larger than 1TB on 32-bit platform. However,
639 639 * for exactly 1TB disk, it was treated as (1T - 512)B
640 640 * in the past, and could have valid Solaris
641 641 * partitions. To workaround this, if an exactly 1TB
642 642 * disk has Solaris fdisk partition, it will be allowed
643 643 * to work with sd.
644 644 *
645 645 *
646 646 *
647 647 * CMLB_FAKE_LABEL_ONE_PARTITION: create s0 and s2 covering
648 648 * the entire disk, if there is no valid partition info.
649 649 * If there is a valid Solaris partition, s0 and s2 will
650 650 * only cover the entire Solaris partition.
651 651 *
652 652 * CMLB_CREATE_P0_MINOR_NODE: create p0 node covering
653 653 * the entire disk. Used by lofi to ensure presence of
654 654 * whole disk device node in case of LOFI_MAP_FILE ioctl.
655 655 *
656 656 * cmlbhandle cmlb handle associated with device
657 657 *
658 658 * tg_cookie cookie from target driver to be passed back to target
659 659 * driver when we call back to it through tg_ops.
660 660 *
661 661 * Notes:
662 662 * Assumes a default label based on capacity for non-removable devices.
663 663 * If capacity > 1TB, EFI is assumed otherwise VTOC (default VTOC
664 664 * for the architecture).
665 665 *
666 666 * For removable devices, default label type is assumed to be VTOC
667 667 * type. Create minor nodes based on a default label type.
668 668 * Label on the media is not validated.
669 669 * minor number consists of:
670 670 * if _SUNOS_VTOC_8 is defined
671 671 * lowest 3 bits is taken as partition number
672 672 * the rest is instance number
673 673 * if _SUNOS_VTOC_16 is defined
674 674 * lowest 6 bits is taken as partition number
675 675 * the rest is instance number
676 676 *
677 677 *
678 678 * Return values:
679 679 * 0 Success
680 680 * ENXIO creating minor nodes failed.
681 681 * EINVAL invalid arg, unsupported tg_ops version
682 682 */
683 683 int
684 684 cmlb_attach(dev_info_t *devi, cmlb_tg_ops_t *tgopsp, int device_type,
685 685 boolean_t is_removable, boolean_t is_hotpluggable, char *node_type,
686 686 int alter_behavior, cmlb_handle_t cmlbhandle, void *tg_cookie)
687 687 {
688 688
689 689 struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
690 690 diskaddr_t cap;
691 691 int status;
692 692
693 693 ASSERT(VALID_BOOLEAN(is_removable));
694 694 ASSERT(VALID_BOOLEAN(is_hotpluggable));
695 695
696 696 if (tgopsp->tg_version < TG_DK_OPS_VERSION_1)
697 697 return (EINVAL);
698 698
699 699 mutex_enter(CMLB_MUTEX(cl));
700 700
701 701 CMLB_DEVINFO(cl) = devi;
702 702 cl->cmlb_tg_ops = tgopsp;
703 703 cl->cl_device_type = device_type;
704 704 cl->cl_is_removable = is_removable;
705 705 cl->cl_is_hotpluggable = is_hotpluggable;
706 706 cl->cl_node_type = node_type;
707 707 cl->cl_sys_blocksize = DEV_BSIZE;
708 708 cl->cl_f_geometry_is_valid = B_FALSE;
709 709 cl->cl_def_labeltype = CMLB_LABEL_VTOC;
710 710 cl->cl_alter_behavior = alter_behavior;
711 711 cl->cl_reserved = -1;
712 712 cl->cl_msglog_flag |= CMLB_ALLOW_2TB_WARN;
713 713 #if defined(__i386) || defined(__amd64)
714 714 cl->cl_logical_drive_count = 0;
715 715 #endif
716 716
717 717 if (!is_removable) {
718 718 mutex_exit(CMLB_MUTEX(cl));
719 719 status = DK_TG_GETCAP(cl, &cap, tg_cookie);
720 720 mutex_enter(CMLB_MUTEX(cl));
721 721 if (status == 0 && cap > CMLB_EXTVTOC_LIMIT) {
722 722 /* set default EFI if > 2TB */
723 723 cl->cl_def_labeltype = CMLB_LABEL_EFI;
724 724 }
725 725 }
726 726
727 727 /* create minor nodes based on default label type */
728 728 cl->cl_last_labeltype = CMLB_LABEL_UNDEF;
729 729 cl->cl_cur_labeltype = CMLB_LABEL_UNDEF;
730 730
731 731 if (cmlb_create_minor_nodes(cl) != 0) {
732 732 mutex_exit(CMLB_MUTEX(cl));
733 733 return (ENXIO);
734 734 }
735 735
736 736 /* Define the dynamic properties for devinfo spapshots. */
737 737 i_ddi_prop_dyn_driver_set(CMLB_DEVINFO(cl), cmlb_prop_dyn);
738 738
739 739 cl->cl_state = CMLB_ATTACHED;
740 740
741 741 mutex_exit(CMLB_MUTEX(cl));
742 742 return (0);
743 743 }
744 744
745 745 /*
746 746 * cmlb_detach:
747 747 *
748 748 * Invalidate in-core labeling data and remove all minor nodes for
749 749 * the device associate with handle.
750 750 *
751 751 * Arguments:
752 752 * cmlbhandle cmlb handle associated with device.
753 753 *
754 754 * tg_cookie cookie from target driver to be passed back to target
755 755 * driver when we call back to it through tg_ops.
756 756 *
757 757 */
758 758 /*ARGSUSED1*/
759 759 void
760 760 cmlb_detach(cmlb_handle_t cmlbhandle, void *tg_cookie)
761 761 {
762 762 struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
763 763
764 764 mutex_enter(CMLB_MUTEX(cl));
765 765 cl->cl_def_labeltype = CMLB_LABEL_UNDEF;
766 766 cl->cl_f_geometry_is_valid = B_FALSE;
767 767 ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
768 768 i_ddi_prop_dyn_driver_set(CMLB_DEVINFO(cl), NULL);
769 769 cl->cl_state = CMLB_INITED;
770 770 mutex_exit(CMLB_MUTEX(cl));
771 771 }
772 772
773 773 /*
774 774 * cmlb_validate:
775 775 *
776 776 * Validates label.
777 777 *
778 778 * Arguments
779 779 * cmlbhandle cmlb handle associated with device.
780 780 *
781 781 * flags operation flags. used for verbosity control
782 782 *
783 783 * tg_cookie cookie from target driver to be passed back to target
784 784 * driver when we call back to it through tg_ops.
785 785 *
786 786 *
787 787 * Notes:
788 788 * If new label type is different from the current, adjust minor nodes
789 789 * accordingly.
790 790 *
791 791 * Return values:
792 792 * 0 success
793 793 * Note: having fdisk but no solaris partition is assumed
794 794 * success.
795 795 *
796 796 * ENOMEM memory allocation failed
797 797 * EIO i/o errors during read or get capacity
798 798 * EACCESS reservation conflicts
799 799 * EINVAL label was corrupt, or no default label was assumed
800 800 * ENXIO invalid handle
801 801 */
802 802 int
803 803 cmlb_validate(cmlb_handle_t cmlbhandle, int flags, void *tg_cookie)
804 804 {
805 805 struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
806 806 int rval;
807 807 int ret = 0;
808 808
809 809 /*
810 810 * Temp work-around checking cl for NULL since there is a bug
811 811 * in sd_detach calling this routine from taskq_dispatch
812 812 * inited function.
813 813 */
814 814 if (cl == NULL)
815 815 return (ENXIO);
816 816
817 817 mutex_enter(CMLB_MUTEX(cl));
818 818 if (cl->cl_state < CMLB_ATTACHED) {
819 819 mutex_exit(CMLB_MUTEX(cl));
820 820 return (ENXIO);
821 821 }
822 822
823 823 rval = cmlb_validate_geometry((struct cmlb_lun *)cmlbhandle, B_TRUE,
824 824 flags, tg_cookie);
825 825
826 826 if (rval == ENOTSUP) {
827 827 if (cl->cl_f_geometry_is_valid) {
828 828 cl->cl_cur_labeltype = CMLB_LABEL_EFI;
829 829 ret = 0;
830 830 } else {
831 831 ret = EINVAL;
832 832 }
833 833 } else {
834 834 ret = rval;
835 835 if (ret == 0)
836 836 cl->cl_cur_labeltype = CMLB_LABEL_VTOC;
837 837 }
838 838
839 839 if (ret == 0)
840 840 (void) cmlb_create_minor_nodes(cl);
841 841
842 842 mutex_exit(CMLB_MUTEX(cl));
843 843 return (ret);
844 844 }
845 845
846 846 /*
847 847 * cmlb_invalidate:
848 848 * Invalidate in core label data
849 849 *
850 850 * Arguments:
851 851 * cmlbhandle cmlb handle associated with device.
852 852 * tg_cookie cookie from target driver to be passed back to target
853 853 * driver when we call back to it through tg_ops.
854 854 */
855 855 /*ARGSUSED1*/
856 856 void
857 857 cmlb_invalidate(cmlb_handle_t cmlbhandle, void *tg_cookie)
858 858 {
859 859 struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
860 860
861 861 if (cl == NULL)
862 862 return;
863 863
864 864 mutex_enter(CMLB_MUTEX(cl));
865 865 cl->cl_f_geometry_is_valid = B_FALSE;
866 866 mutex_exit(CMLB_MUTEX(cl));
867 867 }
868 868
869 869 /*
870 870 * cmlb_is_valid
871 871 * Get status on whether the incore label/geom data is valid
872 872 *
873 873 * Arguments:
874 874 * cmlbhandle cmlb handle associated with device.
875 875 *
876 876 * Return values:
877 877 * B_TRUE if incore label/geom data is valid.
878 878 * B_FALSE otherwise.
879 879 *
880 880 */
881 881
882 882
883 883 boolean_t
884 884 cmlb_is_valid(cmlb_handle_t cmlbhandle)
885 885 {
886 886 struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
887 887
888 888 if (cmlbhandle == NULL)
889 889 return (B_FALSE);
890 890
891 891 return (cl->cl_f_geometry_is_valid);
892 892
893 893 }
894 894
895 895
896 896
897 897 /*
898 898 * cmlb_close:
899 899 *
900 900 * Close the device, revert to a default label minor node for the device,
901 901 * if it is removable.
902 902 *
903 903 * Arguments:
904 904 * cmlbhandle cmlb handle associated with device.
905 905 *
906 906 * tg_cookie cookie from target driver to be passed back to target
907 907 * driver when we call back to it through tg_ops.
908 908 * Return values:
909 909 * 0 Success
910 910 * ENXIO Re-creating minor node failed.
911 911 */
912 912 /*ARGSUSED1*/
913 913 int
914 914 cmlb_close(cmlb_handle_t cmlbhandle, void *tg_cookie)
915 915 {
916 916 struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
917 917
918 918 mutex_enter(CMLB_MUTEX(cl));
919 919 cl->cl_f_geometry_is_valid = B_FALSE;
920 920
921 921 /* revert to default minor node for this device */
922 922 if (ISREMOVABLE(cl)) {
923 923 cl->cl_cur_labeltype = CMLB_LABEL_UNDEF;
924 924 (void) cmlb_create_minor_nodes(cl);
925 925 }
926 926
927 927 mutex_exit(CMLB_MUTEX(cl));
928 928 return (0);
929 929 }
930 930
931 931 /*
932 932 * cmlb_get_devid_block:
933 933 * get the block number where device id is stored.
934 934 *
935 935 * Arguments:
936 936 * cmlbhandle cmlb handle associated with device.
937 937 * devidblockp pointer to block number.
938 938 * tg_cookie cookie from target driver to be passed back to target
939 939 * driver when we call back to it through tg_ops.
940 940 *
941 941 * Notes:
942 942 * It stores the block number of device id in the area pointed to
943 943 * by devidblockp.
944 944 * with the block number of device id.
945 945 *
946 946 * Return values:
947 947 * 0 success
948 948 * EINVAL device id does not apply to current label type.
949 949 */
950 950 /*ARGSUSED2*/
951 951 int
952 952 cmlb_get_devid_block(cmlb_handle_t cmlbhandle, diskaddr_t *devidblockp,
953 953 void *tg_cookie)
954 954 {
955 955 daddr_t spc, blk, head, cyl;
956 956 struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
957 957
958 958 mutex_enter(CMLB_MUTEX(cl));
959 959 if (cl->cl_state < CMLB_ATTACHED) {
960 960 mutex_exit(CMLB_MUTEX(cl));
961 961 return (EINVAL);
962 962 }
963 963
964 964 if ((!cl->cl_f_geometry_is_valid) ||
965 965 (cl->cl_solaris_size < DK_LABEL_LOC)) {
966 966 mutex_exit(CMLB_MUTEX(cl));
967 967 return (EINVAL);
968 968 }
969 969
970 970 if (cl->cl_cur_labeltype == CMLB_LABEL_EFI) {
971 971 if (cl->cl_reserved != -1) {
972 972 blk = cl->cl_map[cl->cl_reserved].dkl_cylno;
973 973 } else {
974 974 mutex_exit(CMLB_MUTEX(cl));
975 975 return (EINVAL);
976 976 }
977 977 } else {
978 978 /* if the disk is unlabeled, don't write a devid to it */
979 979 if (cl->cl_label_from_media != CMLB_LABEL_VTOC) {
980 980 mutex_exit(CMLB_MUTEX(cl));
981 981 return (EINVAL);
982 982 }
983 983
984 984 /* this geometry doesn't allow us to write a devid */
985 985 if (cl->cl_g.dkg_acyl < 2) {
986 986 mutex_exit(CMLB_MUTEX(cl));
987 987 return (EINVAL);
988 988 }
989 989
990 990 /*
991 991 * Subtract 2 guarantees that the next to last cylinder
992 992 * is used
993 993 */
994 994 cyl = cl->cl_g.dkg_ncyl + cl->cl_g.dkg_acyl - 2;
995 995 spc = cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
996 996 head = cl->cl_g.dkg_nhead - 1;
997 997 blk = cl->cl_solaris_offset +
998 998 (cyl * (spc - cl->cl_g.dkg_apc)) +
999 999 (head * cl->cl_g.dkg_nsect) + 1;
1000 1000 }
1001 1001
1002 1002 *devidblockp = blk;
1003 1003 mutex_exit(CMLB_MUTEX(cl));
1004 1004 return (0);
1005 1005 }
1006 1006
1007 1007 /*
1008 1008 * cmlb_partinfo:
1009 1009 * Get partition info for specified partition number.
1010 1010 *
1011 1011 * Arguments:
1012 1012 * cmlbhandle cmlb handle associated with device.
1013 1013 * part partition number
1014 1014 * nblocksp pointer to number of blocks
1015 1015 * startblockp pointer to starting block
1016 1016 * partnamep pointer to name of partition
1017 1017 * tagp pointer to tag info
1018 1018 * tg_cookie cookie from target driver to be passed back to target
1019 1019 * driver when we call back to it through tg_ops.
1020 1020 *
1021 1021 *
1022 1022 * Notes:
1023 1023 * If in-core label is not valid, this functions tries to revalidate
1024 1024 * the label. If label is valid, it stores the total number of blocks
1025 1025 * in this partition in the area pointed to by nblocksp, starting
1026 1026 * block number in area pointed to by startblockp, pointer to partition
1027 1027 * name in area pointed to by partnamep, and tag value in area
1028 1028 * pointed by tagp.
1029 1029 * For EFI labels, tag value will be set to 0.
1030 1030 *
1031 1031 * For all nblocksp, startblockp and partnamep, tagp, a value of NULL
1032 1032 * indicates the corresponding info is not requested.
1033 1033 *
1034 1034 *
1035 1035 * Return values:
1036 1036 * 0 success
1037 1037 * EINVAL no valid label or requested partition number is invalid.
1038 1038 *
1039 1039 */
1040 1040 int
1041 1041 cmlb_partinfo(cmlb_handle_t cmlbhandle, int part, diskaddr_t *nblocksp,
1042 1042 diskaddr_t *startblockp, char **partnamep, uint16_t *tagp, void *tg_cookie)
1043 1043 {
1044 1044
1045 1045 struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
1046 1046 int rval;
1047 1047 #if defined(__i386) || defined(__amd64)
1048 1048 int ext_part;
1049 1049 #endif
1050 1050
1051 1051 ASSERT(cl != NULL);
1052 1052 mutex_enter(CMLB_MUTEX(cl));
1053 1053 if (cl->cl_state < CMLB_ATTACHED) {
1054 1054 mutex_exit(CMLB_MUTEX(cl));
1055 1055 return (EINVAL);
1056 1056 }
1057 1057
1058 1058 if (part < 0 || part >= MAXPART) {
1059 1059 rval = EINVAL;
1060 1060 } else {
1061 1061 if (!cl->cl_f_geometry_is_valid)
1062 1062 (void) cmlb_validate_geometry((struct cmlb_lun *)cl,
1063 1063 B_FALSE, 0, tg_cookie);
1064 1064
1065 1065 if (((!cl->cl_f_geometry_is_valid) ||
1066 1066 (part < NDKMAP && cl->cl_solaris_size == 0)) &&
1067 1067 (part != P0_RAW_DISK)) {
1068 1068 rval = EINVAL;
1069 1069 } else {
1070 1070 if (startblockp != NULL)
1071 1071 *startblockp = (diskaddr_t)cl->cl_offset[part];
1072 1072
1073 1073 if (nblocksp != NULL)
1074 1074 *nblocksp = (diskaddr_t)
1075 1075 cl->cl_map[part].dkl_nblk;
1076 1076
1077 1077 if (tagp != NULL)
1078 1078 *tagp =
1079 1079 ((cl->cl_cur_labeltype == CMLB_LABEL_EFI) ||
1080 1080 (part >= NDKMAP)) ? V_UNASSIGNED :
1081 1081 cl->cl_vtoc.v_part[part].p_tag;
1082 1082 rval = 0;
1083 1083 }
1084 1084
1085 1085 /* consistent with behavior of sd for getting minor name */
1086 1086 if (partnamep != NULL) {
1087 1087 #if defined(__i386) || defined(__amd64)
1088 1088 #if defined(_FIRMWARE_NEEDS_FDISK)
1089 1089 if (part > FDISK_P4) {
1090 1090 ext_part = part-FDISK_P4-1;
1091 1091 *partnamep = dk_ext_minor_data[ext_part].name;
1092 1092 } else
1093 1093 #endif
1094 1094 #endif
1095 1095 *partnamep = dk_minor_data[part].name;
1096 1096 }
1097 1097
1098 1098 }
1099 1099
1100 1100 mutex_exit(CMLB_MUTEX(cl));
1101 1101 return (rval);
1102 1102 }
1103 1103
1104 1104 /*
1105 1105 * cmlb_efi_label_capacity:
1106 1106 * Get capacity stored in EFI disk label.
1107 1107 *
1108 1108 * Arguments:
1109 1109 * cmlbhandle cmlb handle associated with device.
1110 1110 * capacity pointer to capacity stored in EFI disk label.
1111 1111 * tg_cookie cookie from target driver to be passed back to target
1112 1112 * driver when we call back to it through tg_ops.
1113 1113 *
1114 1114 *
1115 1115 * Notes:
1116 1116 * If in-core label is not valid, this functions tries to revalidate
1117 1117 * the label. If label is valid and is an EFI label, it stores the capacity
1118 1118 * in disk label in the area pointed to by capacity.
1119 1119 *
1120 1120 *
1121 1121 * Return values:
1122 1122 * 0 success
1123 1123 * EINVAL no valid EFI label or capacity is NULL.
1124 1124 *
1125 1125 */
1126 1126 int
1127 1127 cmlb_efi_label_capacity(cmlb_handle_t cmlbhandle, diskaddr_t *capacity,
1128 1128 void *tg_cookie)
1129 1129 {
1130 1130 struct cmlb_lun *cl = (struct cmlb_lun *)cmlbhandle;
1131 1131 int rval;
1132 1132
1133 1133 ASSERT(cl != NULL);
1134 1134 mutex_enter(CMLB_MUTEX(cl));
1135 1135 if (cl->cl_state < CMLB_ATTACHED) {
1136 1136 mutex_exit(CMLB_MUTEX(cl));
1137 1137 return (EINVAL);
1138 1138 }
1139 1139
1140 1140 if (!cl->cl_f_geometry_is_valid)
1141 1141 (void) cmlb_validate_geometry((struct cmlb_lun *)cl, B_FALSE,
1142 1142 0, tg_cookie);
1143 1143
1144 1144 if ((!cl->cl_f_geometry_is_valid) || (capacity == NULL) ||
1145 1145 (cl->cl_cur_labeltype != CMLB_LABEL_EFI)) {
1146 1146 rval = EINVAL;
1147 1147 } else {
1148 1148 *capacity = (diskaddr_t)cl->cl_map[WD_NODE].dkl_nblk;
1149 1149 rval = 0;
1150 1150 }
1151 1151
1152 1152 mutex_exit(CMLB_MUTEX(cl));
1153 1153 return (rval);
1154 1154 }
1155 1155
1156 1156 /* Caller should make sure Test Unit Ready succeeds before calling this. */
1157 1157 /*ARGSUSED*/
1158 1158 int
1159 1159 cmlb_ioctl(cmlb_handle_t cmlbhandle, dev_t dev, int cmd, intptr_t arg,
1160 1160 int flag, cred_t *cred_p, int *rval_p, void *tg_cookie)
1161 1161 {
1162 1162
1163 1163 int err;
1164 1164 struct cmlb_lun *cl;
1165 1165
1166 1166 cl = (struct cmlb_lun *)cmlbhandle;
1167 1167
1168 1168 ASSERT(cl != NULL);
1169 1169
1170 1170 mutex_enter(CMLB_MUTEX(cl));
1171 1171 if (cl->cl_state < CMLB_ATTACHED) {
1172 1172 mutex_exit(CMLB_MUTEX(cl));
1173 1173 return (EIO);
1174 1174 }
1175 1175
1176 1176 switch (cmd) {
1177 1177 case DKIOCSEXTVTOC:
1178 1178 case DKIOCSGEOM:
1179 1179 case DKIOCSETEFI:
1180 1180 case DKIOCSMBOOT:
1181 1181 #if defined(__i386) || defined(__amd64)
1182 1182 case DKIOCSETEXTPART:
1183 1183 #endif
1184 1184 break;
1185 1185 case DKIOCSVTOC:
1186 1186 #if defined(__i386) || defined(__amd64)
1187 1187 case DKIOCPARTINFO:
1188 1188 #endif
1189 1189 if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
1190 1190 mutex_exit(CMLB_MUTEX(cl));
1191 1191 return (EOVERFLOW);
1192 1192 }
1193 1193 break;
1194 1194 default:
1195 1195 (void) cmlb_validate_geometry(cl, 1, CMLB_SILENT,
1196 1196 tg_cookie);
1197 1197
1198 1198 switch (cmd) {
1199 1199 case DKIOCGVTOC:
1200 1200 case DKIOCGAPART:
1201 1201 case DKIOCSAPART:
1202 1202
1203 1203 if (cl->cl_label_from_media == CMLB_LABEL_EFI) {
1204 1204 /* GPT label on disk */
1205 1205 mutex_exit(CMLB_MUTEX(cl));
1206 1206 return (ENOTSUP);
1207 1207 } else if
1208 1208 (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
1209 1209 mutex_exit(CMLB_MUTEX(cl));
1210 1210 return (EOVERFLOW);
1211 1211 }
1212 1212 break;
1213 1213
1214 1214 case DKIOCGGEOM:
1215 1215 if (cl->cl_label_from_media == CMLB_LABEL_EFI) {
1216 1216 /* GPT label on disk */
1217 1217 mutex_exit(CMLB_MUTEX(cl));
1218 1218 return (ENOTSUP);
1219 1219 }
1220 1220 break;
1221 1221 default:
1222 1222 break;
1223 1223 }
1224 1224 }
1225 1225
1226 1226 mutex_exit(CMLB_MUTEX(cl));
1227 1227
1228 1228 switch (cmd) {
1229 1229 case DKIOCGGEOM:
1230 1230 cmlb_dbg(CMLB_TRACE, cl, "DKIOCGGEOM\n");
1231 1231 err = cmlb_dkio_get_geometry(cl, (caddr_t)arg, flag, tg_cookie);
1232 1232 break;
1233 1233
1234 1234 case DKIOCSGEOM:
1235 1235 cmlb_dbg(CMLB_TRACE, cl, "DKIOCSGEOM\n");
1236 1236 err = cmlb_dkio_set_geometry(cl, (caddr_t)arg, flag);
1237 1237 break;
1238 1238
1239 1239 case DKIOCGAPART:
1240 1240 cmlb_dbg(CMLB_TRACE, cl, "DKIOCGAPART\n");
1241 1241 err = cmlb_dkio_get_partition(cl, (caddr_t)arg,
1242 1242 flag, tg_cookie);
1243 1243 break;
1244 1244
1245 1245 case DKIOCSAPART:
1246 1246 cmlb_dbg(CMLB_TRACE, cl, "DKIOCSAPART\n");
1247 1247 err = cmlb_dkio_set_partition(cl, (caddr_t)arg, flag);
1248 1248 break;
1249 1249
1250 1250 case DKIOCGVTOC:
1251 1251 cmlb_dbg(CMLB_TRACE, cl, "DKIOCGVTOC\n");
1252 1252 err = cmlb_dkio_get_vtoc(cl, (caddr_t)arg, flag, tg_cookie);
1253 1253 break;
1254 1254
1255 1255 case DKIOCGEXTVTOC:
1256 1256 cmlb_dbg(CMLB_TRACE, cl, "DKIOCGVTOC\n");
1257 1257 err = cmlb_dkio_get_extvtoc(cl, (caddr_t)arg, flag, tg_cookie);
1258 1258 break;
1259 1259
1260 1260 case DKIOCGETEFI:
1261 1261 cmlb_dbg(CMLB_TRACE, cl, "DKIOCGETEFI\n");
1262 1262 err = cmlb_dkio_get_efi(cl, (caddr_t)arg, flag, tg_cookie);
1263 1263 break;
1264 1264
1265 1265 case DKIOCPARTITION:
1266 1266 cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTITION\n");
1267 1267 err = cmlb_dkio_partition(cl, (caddr_t)arg, flag, tg_cookie);
1268 1268 break;
1269 1269
1270 1270 case DKIOCSVTOC:
1271 1271 cmlb_dbg(CMLB_TRACE, cl, "DKIOCSVTOC\n");
1272 1272 err = cmlb_dkio_set_vtoc(cl, dev, (caddr_t)arg, flag,
1273 1273 tg_cookie);
1274 1274 break;
1275 1275
1276 1276 case DKIOCSEXTVTOC:
1277 1277 cmlb_dbg(CMLB_TRACE, cl, "DKIOCSVTOC\n");
1278 1278 err = cmlb_dkio_set_extvtoc(cl, dev, (caddr_t)arg, flag,
1279 1279 tg_cookie);
1280 1280 break;
1281 1281
1282 1282 case DKIOCSETEFI:
1283 1283 cmlb_dbg(CMLB_TRACE, cl, "DKIOCSETEFI\n");
1284 1284 err = cmlb_dkio_set_efi(cl, dev, (caddr_t)arg, flag, tg_cookie);
1285 1285 break;
1286 1286
1287 1287 case DKIOCGMBOOT:
1288 1288 cmlb_dbg(CMLB_TRACE, cl, "DKIOCGMBOOT\n");
1289 1289 err = cmlb_dkio_get_mboot(cl, (caddr_t)arg, flag, tg_cookie);
1290 1290 break;
1291 1291
1292 1292 case DKIOCSMBOOT:
1293 1293 cmlb_dbg(CMLB_TRACE, cl, "DKIOCSMBOOT\n");
1294 1294 err = cmlb_dkio_set_mboot(cl, (caddr_t)arg, flag, tg_cookie);
1295 1295 break;
1296 1296 case DKIOCG_PHYGEOM:
1297 1297 cmlb_dbg(CMLB_TRACE, cl, "DKIOCG_PHYGEOM\n");
1298 1298 #if defined(__i386) || defined(__amd64)
1299 1299 err = cmlb_dkio_get_phygeom(cl, (caddr_t)arg, flag, tg_cookie);
1300 1300 #else
1301 1301 err = ENOTTY;
1302 1302 #endif
1303 1303 break;
1304 1304 case DKIOCG_VIRTGEOM:
1305 1305 cmlb_dbg(CMLB_TRACE, cl, "DKIOCG_VIRTGEOM\n");
1306 1306 #if defined(__i386) || defined(__amd64)
1307 1307 err = cmlb_dkio_get_virtgeom(cl, (caddr_t)arg, flag);
1308 1308 #else
1309 1309 err = ENOTTY;
1310 1310 #endif
1311 1311 break;
1312 1312 case DKIOCPARTINFO:
1313 1313 cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTINFO");
1314 1314 #if defined(__i386) || defined(__amd64)
1315 1315 err = cmlb_dkio_partinfo(cl, dev, (caddr_t)arg, flag);
1316 1316 #else
1317 1317 err = ENOTTY;
1318 1318 #endif
1319 1319 break;
1320 1320 case DKIOCEXTPARTINFO:
1321 1321 cmlb_dbg(CMLB_TRACE, cl, "DKIOCPARTINFO");
1322 1322 #if defined(__i386) || defined(__amd64)
1323 1323 err = cmlb_dkio_extpartinfo(cl, dev, (caddr_t)arg, flag);
1324 1324 #else
1325 1325 err = ENOTTY;
1326 1326 #endif
1327 1327 break;
1328 1328 #if defined(__i386) || defined(__amd64)
1329 1329 case DKIOCSETEXTPART:
1330 1330 cmlb_dbg(CMLB_TRACE, cl, "DKIOCSETEXTPART");
1331 1331 err = cmlb_dkio_set_ext_part(cl, (caddr_t)arg, flag, tg_cookie);
1332 1332 break;
1333 1333 #endif
1334 1334 default:
1335 1335 err = ENOTTY;
1336 1336
1337 1337 }
1338 1338
1339 1339 /*
1340 1340 * An ioctl that succeeds and changed ('set') size(9P) information
1341 1341 * needs to invalidate the cached devinfo snapshot to avoid having
1342 1342 * old information being returned in a snapshots.
1343 1343 *
1344 1344 * NB: When available, call ddi_change_minor_node() to clear
1345 1345 * SSIZEVALID in specfs vnodes via spec_size_invalidate().
1346 1346 */
1347 1347 if (err == 0) {
1348 1348 switch (cmd) {
1349 1349 case DKIOCSGEOM:
1350 1350 case DKIOCSAPART:
1351 1351 case DKIOCSVTOC:
1352 1352 case DKIOCSEXTVTOC:
1353 1353 case DKIOCSETEFI:
1354 1354 i_ddi_prop_dyn_cache_invalidate(CMLB_DEVINFO(cl),
1355 1355 i_ddi_prop_dyn_driver_get(CMLB_DEVINFO(cl)));
1356 1356 }
1357 1357 }
1358 1358 return (err);
1359 1359 }
1360 1360
1361 1361 dev_t
1362 1362 cmlb_make_device(struct cmlb_lun *cl)
1363 1363 {
1364 1364 if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE) {
1365 1365 return (makedevice(ddi_driver_major(CMLB_DEVINFO(cl)),
1366 1366 ddi_get_instance(
1367 1367 CMLB_DEVINFO(cl)) << CMLBUNIT_FORCE_P0_SHIFT));
1368 1368 } else {
1369 1369 return (makedevice(ddi_driver_major(CMLB_DEVINFO(cl)),
1370 1370 ddi_get_instance(CMLB_DEVINFO(cl)) << CMLBUNIT_SHIFT));
1371 1371 }
1372 1372 }
1373 1373
1374 1374 /*
1375 1375 * Function: cmlb_check_update_blockcount
1376 1376 *
1377 1377 * Description: If current capacity value is invalid, obtains the
1378 1378 * current capacity from target driver.
1379 1379 *
1380 1380 * Return Code: 0 success
1381 1381 * EIO failure
1382 1382 */
1383 1383 static int
1384 1384 cmlb_check_update_blockcount(struct cmlb_lun *cl, void *tg_cookie)
1385 1385 {
1386 1386 int status;
1387 1387 diskaddr_t capacity;
1388 1388 uint32_t lbasize;
1389 1389
1390 1390 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1391 1391
1392 1392 if (cl->cl_f_geometry_is_valid)
1393 1393 return (0);
1394 1394
1395 1395 mutex_exit(CMLB_MUTEX(cl));
1396 1396 status = DK_TG_GETCAP(cl, &capacity, tg_cookie);
1397 1397 if (status != 0) {
1398 1398 mutex_enter(CMLB_MUTEX(cl));
1399 1399 return (EIO);
1400 1400 }
1401 1401
1402 1402 status = DK_TG_GETBLOCKSIZE(cl, &lbasize, tg_cookie);
1403 1403 mutex_enter(CMLB_MUTEX(cl));
1404 1404 if (status != 0)
1405 1405 return (EIO);
1406 1406
1407 1407 if ((capacity != 0) && (lbasize != 0)) {
1408 1408 cl->cl_blockcount = capacity;
1409 1409 cl->cl_tgt_blocksize = lbasize;
1410 1410 if (!cl->cl_is_removable) {
1411 1411 cl->cl_sys_blocksize = lbasize;
1412 1412 }
1413 1413 return (0);
1414 1414 } else {
1415 1415 return (EIO);
1416 1416 }
1417 1417 }
1418 1418
1419 1419 static int
1420 1420 cmlb_create_minor(dev_info_t *dip, char *name, int spec_type,
1421 1421 minor_t minor_num, char *node_type, int flag, boolean_t internal)
1422 1422 {
1423 1423 ASSERT(VALID_BOOLEAN(internal));
1424 1424
1425 1425 if (internal)
1426 1426 return (ddi_create_internal_pathname(dip,
1427 1427 name, spec_type, minor_num));
1428 1428 else
1429 1429 return (ddi_create_minor_node(dip,
1430 1430 name, spec_type, minor_num, node_type, flag));
1431 1431 }
1432 1432
1433 1433 /*
1434 1434 * Function: cmlb_create_minor_nodes
1435 1435 *
1436 1436 * Description: Create or adjust the minor device nodes for the instance.
1437 1437 * Minor nodes are created based on default label type,
1438 1438 * current label type and last label type we created
1439 1439 * minor nodes based on.
1440 1440 *
1441 1441 *
1442 1442 * Arguments: cl - driver soft state (unit) structure
1443 1443 *
1444 1444 * Return Code: 0 success
1445 1445 * ENXIO failure.
1446 1446 *
1447 1447 * Context: Kernel thread context
1448 1448 */
1449 1449 static int
1450 1450 cmlb_create_minor_nodes(struct cmlb_lun *cl)
1451 1451 {
1452 1452 struct driver_minor_data *dmdp;
1453 1453 int instance, shift;
1454 1454 char name[48];
1455 1455 cmlb_label_t newlabeltype;
1456 1456 boolean_t internal;
1457 1457
1458 1458 ASSERT(cl != NULL);
1459 1459 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1460 1460
1461 1461 internal = VOID2BOOLEAN(
1462 1462 (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
1463 1463
1464 1464 if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE)
1465 1465 shift = CMLBUNIT_FORCE_P0_SHIFT;
1466 1466 else
1467 1467 shift = CMLBUNIT_SHIFT;
1468 1468
1469 1469 /* check the most common case */
1470 1470 if (cl->cl_cur_labeltype != CMLB_LABEL_UNDEF &&
1471 1471 cl->cl_last_labeltype == cl->cl_cur_labeltype) {
1472 1472 /* do nothing */
1473 1473 return (0);
1474 1474 }
1475 1475
1476 1476 if (cl->cl_def_labeltype == CMLB_LABEL_UNDEF) {
1477 1477 /* we should never get here */
1478 1478 return (ENXIO);
1479 1479 }
1480 1480
1481 1481 if (cl->cl_last_labeltype == CMLB_LABEL_UNDEF) {
1482 1482 /* first time during attach */
1483 1483 newlabeltype = cl->cl_def_labeltype;
1484 1484
1485 1485 instance = ddi_get_instance(CMLB_DEVINFO(cl));
1486 1486
1487 1487 /* Create all the minor nodes for this target. */
1488 1488 dmdp = (newlabeltype == CMLB_LABEL_EFI) ? dk_minor_data_efi :
1489 1489 dk_minor_data;
1490 1490 while (dmdp->name != NULL) {
1491 1491
1492 1492 (void) sprintf(name, "%s", dmdp->name);
1493 1493
1494 1494 if (cmlb_create_minor(CMLB_DEVINFO(cl), name,
1495 1495 dmdp->type,
1496 1496 (instance << shift) | dmdp->minor,
1497 1497 cl->cl_node_type, NULL, internal) == DDI_FAILURE) {
1498 1498 /*
1499 1499 * Clean up any nodes that may have been
1500 1500 * created, in case this fails in the middle
1501 1501 * of the loop.
1502 1502 */
1503 1503 ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
1504 1504 return (ENXIO);
1505 1505 }
1506 1506 dmdp++;
1507 1507 }
1508 1508 cl->cl_last_labeltype = newlabeltype;
1509 1509 #if defined(_SUNOS_VTOC_8)
1510 1510 /*
1511 1511 * "emulate" p0 device for sparc, used by lofi
1512 1512 */
1513 1513 if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE) {
1514 1514 if (cmlb_create_minor(CMLB_DEVINFO(cl), "q", S_IFBLK,
1515 1515 (instance << CMLBUNIT_FORCE_P0_SHIFT) | P0_RAW_DISK,
1516 1516 cl->cl_node_type, NULL, internal) == DDI_FAILURE) {
1517 1517 ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
1518 1518 return (ENXIO);
1519 1519 }
1520 1520
1521 1521 if (cmlb_create_minor(CMLB_DEVINFO(cl), "q,raw",
1522 1522 S_IFCHR,
1523 1523 (instance << CMLBUNIT_FORCE_P0_SHIFT) | P0_RAW_DISK,
1524 1524 cl->cl_node_type, NULL, internal) == DDI_FAILURE) {
1525 1525 ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
1526 1526 return (ENXIO);
1527 1527 }
1528 1528 }
1529 1529 #endif /* defined(_SUNOS_VTOC_8) */
1530 1530 return (0);
1531 1531 }
1532 1532
1533 1533 /* Not first time */
1534 1534 if (cl->cl_cur_labeltype == CMLB_LABEL_UNDEF) {
1535 1535 if (cl->cl_last_labeltype != cl->cl_def_labeltype) {
1536 1536 /* close time, revert to default. */
1537 1537 newlabeltype = cl->cl_def_labeltype;
1538 1538 } else {
1539 1539 /*
1540 1540 * do nothing since the type for which we last created
1541 1541 * nodes matches the default
1542 1542 */
1543 1543 return (0);
1544 1544 }
1545 1545 } else {
1546 1546 if (cl->cl_cur_labeltype != cl->cl_last_labeltype) {
1547 1547 /* We are not closing, use current label type */
1548 1548 newlabeltype = cl->cl_cur_labeltype;
1549 1549 } else {
1550 1550 /*
1551 1551 * do nothing since the type for which we last created
1552 1552 * nodes matches the current label type
1553 1553 */
1554 1554 return (0);
1555 1555 }
1556 1556 }
1557 1557
1558 1558 instance = ddi_get_instance(CMLB_DEVINFO(cl));
1559 1559
1560 1560 /*
1561 1561 * Currently we only fix up the s7 node when we are switching
1562 1562 * label types from or to EFI. This is consistent with
1563 1563 * current behavior of sd.
1564 1564 */
1565 1565 if (newlabeltype == CMLB_LABEL_EFI &&
1566 1566 cl->cl_last_labeltype != CMLB_LABEL_EFI) {
1567 1567 /* from vtoc to EFI */
1568 1568 ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
1569 1569 ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
1570 1570 (void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd",
1571 1571 S_IFBLK, (instance << shift) | WD_NODE,
1572 1572 cl->cl_node_type, NULL, internal);
1573 1573 (void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd,raw",
1574 1574 S_IFCHR, (instance << shift) | WD_NODE,
1575 1575 cl->cl_node_type, NULL, internal);
1576 1576 } else {
1577 1577 /* from efi to vtoc */
1578 1578 ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd");
1579 1579 ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd,raw");
1580 1580 (void) cmlb_create_minor(CMLB_DEVINFO(cl), "h",
1581 1581 S_IFBLK, (instance << shift) | WD_NODE,
1582 1582 cl->cl_node_type, NULL, internal);
1583 1583 (void) cmlb_create_minor(CMLB_DEVINFO(cl), "h,raw",
1584 1584 S_IFCHR, (instance << shift) | WD_NODE,
1585 1585 cl->cl_node_type, NULL, internal);
1586 1586 }
1587 1587
1588 1588 cl->cl_last_labeltype = newlabeltype;
1589 1589 return (0);
1590 1590 }
1591 1591
1592 1592 /*
1593 1593 * Function: cmlb_validate_geometry
1594 1594 *
1595 1595 * Description: Read the label from the disk (if present). Update the unit's
1596 1596 * geometry and vtoc information from the data in the label.
1597 1597 * Verify that the label is valid.
1598 1598 *
1599 1599 * Arguments:
1600 1600 * cl driver soft state (unit) structure
1601 1601 *
1602 1602 * forcerevalid force revalidation even if we are already valid.
1603 1603 * flags operation flags from target driver. Used for verbosity
1604 1604 * control at this time.
1605 1605 * tg_cookie cookie from target driver to be passed back to target
1606 1606 * driver when we call back to it through tg_ops.
1607 1607 *
1608 1608 * Return Code: 0 - Successful completion
1609 1609 * EINVAL - Invalid value in cl->cl_tgt_blocksize or
1610 1610 * cl->cl_blockcount; or label on disk is corrupted
1611 1611 * or unreadable.
1612 1612 * EACCES - Reservation conflict at the device.
1613 1613 * ENOMEM - Resource allocation error
1614 1614 * ENOTSUP - geometry not applicable
1615 1615 *
1616 1616 * Context: Kernel thread only (can sleep).
1617 1617 */
1618 1618 static int
1619 1619 cmlb_validate_geometry(struct cmlb_lun *cl, boolean_t forcerevalid, int flags,
1620 1620 void *tg_cookie)
1621 1621 {
1622 1622 int label_error = 0;
1623 1623 diskaddr_t capacity;
1624 1624 int count;
1625 1625
1626 1626 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1627 1627 ASSERT(VALID_BOOLEAN(forcerevalid));
1628 1628
1629 1629 if ((cl->cl_f_geometry_is_valid) && (!forcerevalid)) {
1630 1630 if (cl->cl_cur_labeltype == CMLB_LABEL_EFI)
1631 1631 return (ENOTSUP);
1632 1632 return (0);
1633 1633 }
1634 1634
1635 1635 if (cmlb_check_update_blockcount(cl, tg_cookie) != 0)
1636 1636 return (EIO);
1637 1637
1638 1638 capacity = cl->cl_blockcount;
1639 1639
1640 1640 /*
1641 1641 * Set up the "whole disk" fdisk partition; this should always
1642 1642 * exist, regardless of whether the disk contains an fdisk table
1643 1643 * or vtoc.
1644 1644 */
1645 1645 cl->cl_map[P0_RAW_DISK].dkl_cylno = 0;
1646 1646 cl->cl_offset[P0_RAW_DISK] = 0;
1647 1647 /*
1648 1648 * note if capacity > int32_max(1TB) we are in 64bit environment
1649 1649 * so no truncation happens
1650 1650 */
1651 1651 cl->cl_map[P0_RAW_DISK].dkl_nblk = capacity;
1652 1652
1653 1653 /*
1654 1654 * Refresh the logical and physical geometry caches.
1655 1655 * (data from MODE SENSE format/rigid disk geometry pages,
1656 1656 * and scsi_ifgetcap("geometry").
1657 1657 */
1658 1658 cmlb_resync_geom_caches(cl, capacity, tg_cookie);
1659 1659
1660 1660 cl->cl_label_from_media = CMLB_LABEL_UNDEF;
1661 1661 label_error = cmlb_use_efi(cl, capacity, flags, tg_cookie);
1662 1662 if (label_error == 0) {
1663 1663
1664 1664 /* found a valid EFI label */
1665 1665 cmlb_dbg(CMLB_TRACE, cl,
1666 1666 "cmlb_validate_geometry: found EFI label\n");
1667 1667 /*
1668 1668 * solaris_size and geometry_is_valid are set in
1669 1669 * cmlb_use_efi
1670 1670 */
1671 1671 return (ENOTSUP);
1672 1672 }
1673 1673
1674 1674 /* NO EFI label found */
1675 1675
1676 1676 if (capacity > CMLB_EXTVTOC_LIMIT) {
1677 1677 if (label_error == ESRCH) {
1678 1678 /*
1679 1679 * they've configured a LUN over 2TB, but used
1680 1680 * format.dat to restrict format's view of the
1681 1681 * capacity to be under 2TB in some earlier Solaris
1682 1682 * release.
1683 1683 */
1684 1684 /* i.e > 2TB with a VTOC < 2TB */
1685 1685 if (!(flags & CMLB_SILENT) &&
1686 1686 (cl->cl_msglog_flag & CMLB_ALLOW_2TB_WARN)) {
1687 1687
1688 1688 cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
1689 1689 CE_NOTE, "!Disk (%s%d) is limited to 2 TB "
1690 1690 "due to VTOC label. To use the full "
1691 1691 "capacity of the disk, use format(1M) to "
1692 1692 "relabel the disk with EFI/GPT label.\n",
1693 1693 CMLB_LABEL(cl),
1694 1694 ddi_get_instance(CMLB_DEVINFO(cl)));
1695 1695
1696 1696 cl->cl_msglog_flag &= ~CMLB_ALLOW_2TB_WARN;
1697 1697 }
1698 1698 } else {
1699 1699 return (ENOTSUP);
1700 1700 }
1701 1701 }
1702 1702
1703 1703 label_error = 0;
1704 1704
1705 1705 /*
1706 1706 * at this point it is either labeled with a VTOC or it is
1707 1707 * under 1TB (<= 1TB actually for off-by-1)
1708 1708 */
1709 1709
1710 1710 /*
1711 1711 * Only DIRECT ACCESS devices will have Scl labels.
1712 1712 * CD's supposedly have a Scl label, too
1713 1713 */
1714 1714 if (cl->cl_device_type == DTYPE_DIRECT || ISREMOVABLE(cl)) {
1715 1715 struct dk_label *dkl;
1716 1716 offset_t label_addr;
1717 1717 int rval;
1718 1718 size_t buffer_size;
1719 1719
1720 1720 /*
1721 1721 * Note: This will set up cl->cl_solaris_size and
1722 1722 * cl->cl_solaris_offset.
1723 1723 */
1724 1724 rval = cmlb_read_fdisk(cl, capacity, tg_cookie);
1725 1725 if ((rval != 0) && !ISCD(cl)) {
1726 1726 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1727 1727 return (rval);
1728 1728 }
1729 1729
1730 1730 if (cl->cl_solaris_size <= DK_LABEL_LOC) {
1731 1731 /*
1732 1732 * Found fdisk table but no Solaris partition entry,
1733 1733 * so don't call cmlb_uselabel() and don't create
1734 1734 * a default label.
1735 1735 */
1736 1736 label_error = 0;
1737 1737 cl->cl_f_geometry_is_valid = B_TRUE;
1738 1738 goto no_solaris_partition;
1739 1739 }
1740 1740
1741 1741 label_addr = (daddr_t)(cl->cl_solaris_offset + DK_LABEL_LOC);
1742 1742
1743 1743 buffer_size = cl->cl_sys_blocksize;
1744 1744
1745 1745 cmlb_dbg(CMLB_TRACE, cl, "cmlb_validate_geometry: "
1746 1746 "label_addr: 0x%x allocation size: 0x%x\n",
1747 1747 label_addr, buffer_size);
1748 1748
1749 1749 if ((dkl = kmem_zalloc(buffer_size, KM_NOSLEEP)) == NULL)
1750 1750 return (ENOMEM);
1751 1751
1752 1752 mutex_exit(CMLB_MUTEX(cl));
1753 1753 rval = DK_TG_READ(cl, dkl, label_addr, buffer_size, tg_cookie);
1754 1754 mutex_enter(CMLB_MUTEX(cl));
1755 1755
1756 1756 switch (rval) {
1757 1757 case 0:
1758 1758 /*
1759 1759 * cmlb_uselabel will establish that the geometry
1760 1760 * is valid.
1761 1761 */
1762 1762 if (cmlb_uselabel(cl,
1763 1763 (struct dk_label *)(uintptr_t)dkl, flags) !=
1764 1764 CMLB_LABEL_IS_VALID) {
1765 1765 label_error = EINVAL;
1766 1766 } else
1767 1767 cl->cl_label_from_media = CMLB_LABEL_VTOC;
1768 1768 break;
1769 1769 case EACCES:
1770 1770 label_error = EACCES;
1771 1771 break;
1772 1772 default:
1773 1773 label_error = EINVAL;
1774 1774 break;
1775 1775 }
1776 1776
1777 1777 kmem_free(dkl, buffer_size);
1778 1778 }
1779 1779
1780 1780 /*
1781 1781 * If a valid label was not found, AND if no reservation conflict
1782 1782 * was detected, then go ahead and create a default label (4069506).
1783 1783 *
1784 1784 * Note: currently, for VTOC_8 devices, the default label is created
1785 1785 * for removables and hotpluggables only. For VTOC_16 devices, the
1786 1786 * default label will be created for all devices.
1787 1787 * (see cmlb_build_default_label)
1788 1788 */
1789 1789 #if defined(_SUNOS_VTOC_8)
1790 1790 if ((ISREMOVABLE(cl) || ISHOTPLUGGABLE(cl)) &&
1791 1791 (label_error != EACCES)) {
1792 1792 #elif defined(_SUNOS_VTOC_16)
1793 1793 if (label_error != EACCES) {
1794 1794 #endif
1795 1795 if (!cl->cl_f_geometry_is_valid) {
1796 1796 cmlb_build_default_label(cl, tg_cookie);
1797 1797 }
1798 1798 label_error = 0;
1799 1799 }
1800 1800
1801 1801 no_solaris_partition:
1802 1802
1803 1803 #if defined(_SUNOS_VTOC_16)
1804 1804 /*
1805 1805 * If we have valid geometry, set up the remaining fdisk partitions.
1806 1806 * Note that dkl_cylno is not used for the fdisk map entries, so
1807 1807 * we set it to an entirely bogus value.
1808 1808 */
1809 1809 for (count = 0; count < FDISK_PARTS; count++) {
1810 1810 cl->cl_map[FDISK_P1 + count].dkl_cylno = UINT16_MAX;
1811 1811 cl->cl_map[FDISK_P1 + count].dkl_nblk =
1812 1812 cl->cl_fmap[count].fmap_nblk;
1813 1813
1814 1814 cl->cl_offset[FDISK_P1 + count] =
1815 1815 cl->cl_fmap[count].fmap_start;
1816 1816 }
1817 1817 #endif
1818 1818
1819 1819 for (count = 0; count < NDKMAP; count++) {
1820 1820 #if defined(_SUNOS_VTOC_8)
1821 1821 struct dk_map *lp = &cl->cl_map[count];
1822 1822 cl->cl_offset[count] =
1823 1823 cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
1824 1824 #elif defined(_SUNOS_VTOC_16)
1825 1825 struct dkl_partition *vp = &cl->cl_vtoc.v_part[count];
1826 1826
1827 1827 cl->cl_offset[count] = vp->p_start + cl->cl_solaris_offset;
1828 1828 #else
1829 1829 #error "No VTOC format defined."
1830 1830 #endif
1831 1831 }
1832 1832
1833 1833 return (label_error);
1834 1834 }
1835 1835
1836 1836 #if defined(_SUNOS_VTOC_16)
1837 1837 /*
1838 1838 * Function: cmlb_convert_geometry
1839 1839 *
1840 1840 * Description: Convert physical geometry into a dk_geom structure. In
1841 1841 * other words, make sure we don't wrap 16-bit values.
1842 1842 * e.g. converting from geom_cache to dk_geom
1843 1843 *
1844 1844 * Context: Kernel thread only
1845 1845 */
1846 1846 static void
1847 1847 cmlb_convert_geometry(struct cmlb_lun *cl, diskaddr_t capacity,
1848 1848 struct dk_geom *cl_g, void *tg_cookie)
1849 1849 {
1850 1850
1851 1851 ASSERT(cl != NULL);
1852 1852 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1853 1853
1854 1854 /* Unlabeled SCSI floppy device */
1855 1855 if (capacity < 160) {
1856 1856 /* Less than 80K */
1857 1857 cl_g->dkg_nhead = 1;
1858 1858 cl_g->dkg_ncyl = capacity;
1859 1859 cl_g->dkg_nsect = 1;
1860 1860 return;
1861 1861 } else if (capacity <= 0x1000) {
1862 1862 cl_g->dkg_nhead = 2;
1863 1863 cl_g->dkg_ncyl = 80;
1864 1864 cl_g->dkg_nsect = capacity / (cl_g->dkg_nhead * cl_g->dkg_ncyl);
1865 1865 return;
1866 1866 }
1867 1867
1868 1868 /*
1869 1869 * For all devices we calculate cylinders using the heads and sectors
1870 1870 * we assign based on capacity of the device. The algorithm is
1871 1871 * designed to be compatible with the way other operating systems
1872 1872 * lay out fdisk tables for X86 and to insure that the cylinders never
1873 1873 * exceed 65535 to prevent problems with X86 ioctls that report
1874 1874 * geometry.
1875 1875 * For some smaller disk sizes we report geometry that matches those
1876 1876 * used by X86 BIOS usage. For larger disks, we use SPT that are
1877 1877 * multiples of 63, since other OSes that are not limited to 16-bits
1878 1878 * for cylinders stop at 63 SPT we make do by using multiples of 63 SPT.
1879 1879 *
1880 1880 * The following table (in order) illustrates some end result
1881 1881 * calculations:
1882 1882 *
1883 1883 * Maximum number of blocks nhead nsect
1884 1884 *
1885 1885 * 2097152 (1GB) 64 32
1886 1886 * 16777216 (8GB) 128 32
1887 1887 * 1052819775 (502.02GB) 255 63
1888 1888 * 2105639550 (0.98TB) 255 126
1889 1889 * 3158459325 (1.47TB) 255 189
1890 1890 * 4211279100 (1.96TB) 255 252
1891 1891 * 5264098875 (2.45TB) 255 315
1892 1892 * ...
1893 1893 *
1894 1894 * For Solid State Drive(SSD), it uses 4K page size inside and may be
1895 1895 * double with every new generation. If the I/O is not aligned with
1896 1896 * page size on SSDs, SSDs perform a lot slower.
1897 1897 * By default, Solaris partition starts from cylinder 1. It will be
1898 1898 * misaligned even with 4K if using heads(255) and SPT(63). To
1899 1899 * workaround the problem, if the device is SSD, we use heads(224) and
1900 1900 * SPT multiple of 56. Thus the default Solaris partition starts from
1901 1901 * a position that aligns with 128K on a 512 bytes sector size SSD.
1902 1902 */
1903 1903
1904 1904 if (capacity <= 0x200000) {
1905 1905 cl_g->dkg_nhead = 64;
1906 1906 cl_g->dkg_nsect = 32;
1907 1907 } else if (capacity <= 0x01000000) {
1908 1908 cl_g->dkg_nhead = 128;
1909 1909 cl_g->dkg_nsect = 32;
1910 1910 } else {
1911 1911 tg_attribute_t tgattribute;
1912 1912 int is_solid_state;
1913 1913 unsigned short nhead;
1914 1914 unsigned short nsect;
1915 1915
1916 1916 bzero(&tgattribute, sizeof (tg_attribute_t));
1917 1917
1918 1918 mutex_exit(CMLB_MUTEX(cl));
1919 1919 is_solid_state =
1920 1920 (DK_TG_GETATTRIBUTE(cl, &tgattribute, tg_cookie) == 0) ?
1921 1921 tgattribute.media_is_solid_state : FALSE;
1922 1922 mutex_enter(CMLB_MUTEX(cl));
1923 1923
1924 1924 if (is_solid_state) {
1925 1925 nhead = 224;
1926 1926 nsect = 56;
1927 1927 } else {
1928 1928 nhead = 255;
1929 1929 nsect = 63;
1930 1930 }
1931 1931
1932 1932 cl_g->dkg_nhead = nhead;
1933 1933
1934 1934 /* make dkg_nsect be smallest multiple of nsect */
1935 1935 cl_g->dkg_nsect = ((capacity +
1936 1936 (UINT16_MAX * nhead * nsect) - 1) /
1937 1937 (UINT16_MAX * nhead * nsect)) * nsect;
1938 1938
1939 1939 if (cl_g->dkg_nsect == 0)
1940 1940 cl_g->dkg_nsect = (UINT16_MAX / nsect) * nsect;
1941 1941 }
1942 1942
1943 1943 }
1944 1944 #endif
1945 1945
1946 1946 /*
1947 1947 * Function: cmlb_resync_geom_caches
1948 1948 *
1949 1949 * Description: (Re)initialize both geometry caches: the virtual geometry
1950 1950 * information is extracted from the HBA (the "geometry"
1951 1951 * capability), and the physical geometry cache data is
1952 1952 * generated by issuing MODE SENSE commands.
1953 1953 *
1954 1954 * Arguments:
1955 1955 * cl driver soft state (unit) structure
1956 1956 * capacity disk capacity in #blocks
1957 1957 * tg_cookie cookie from target driver to be passed back to target
1958 1958 * driver when we call back to it through tg_ops.
1959 1959 *
1960 1960 * Context: Kernel thread only (can sleep).
1961 1961 */
1962 1962 static void
1963 1963 cmlb_resync_geom_caches(struct cmlb_lun *cl, diskaddr_t capacity,
1964 1964 void *tg_cookie)
1965 1965 {
1966 1966 struct cmlb_geom pgeom;
1967 1967 struct cmlb_geom lgeom;
1968 1968 struct cmlb_geom *pgeomp = &pgeom;
1969 1969 unsigned short nhead;
1970 1970 unsigned short nsect;
1971 1971 int spc;
1972 1972 int ret;
1973 1973
1974 1974 ASSERT(cl != NULL);
1975 1975 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
1976 1976
1977 1977 /*
1978 1978 * Ask the controller for its logical geometry.
1979 1979 * Note: if the HBA does not support scsi_ifgetcap("geometry"),
1980 1980 * then the lgeom cache will be invalid.
1981 1981 */
1982 1982 mutex_exit(CMLB_MUTEX(cl));
1983 1983 bzero(&lgeom, sizeof (struct cmlb_geom));
1984 1984 ret = DK_TG_GETVIRTGEOM(cl, &lgeom, tg_cookie);
1985 1985 mutex_enter(CMLB_MUTEX(cl));
1986 1986
1987 1987 bcopy(&lgeom, &cl->cl_lgeom, sizeof (cl->cl_lgeom));
1988 1988
1989 1989 /*
1990 1990 * Initialize the pgeom cache from lgeom, so that if MODE SENSE
1991 1991 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values.
1992 1992 */
1993 1993 if (ret != 0 || cl->cl_lgeom.g_nsect == 0 ||
1994 1994 cl->cl_lgeom.g_nhead == 0) {
1995 1995 /*
1996 1996 * Note: Perhaps this needs to be more adaptive? The rationale
1997 1997 * is that, if there's no HBA geometry from the HBA driver, any
1998 1998 * guess is good, since this is the physical geometry. If MODE
1999 1999 * SENSE fails this gives a max cylinder size for non-LBA access
2000 2000 */
2001 2001 nhead = 255;
2002 2002 nsect = 63;
2003 2003 } else {
2004 2004 nhead = cl->cl_lgeom.g_nhead;
2005 2005 nsect = cl->cl_lgeom.g_nsect;
2006 2006 }
2007 2007
2008 2008 if (ISCD(cl)) {
2009 2009 pgeomp->g_nhead = 1;
2010 2010 pgeomp->g_nsect = nsect * nhead;
2011 2011 } else {
2012 2012 pgeomp->g_nhead = nhead;
2013 2013 pgeomp->g_nsect = nsect;
2014 2014 }
2015 2015
2016 2016 spc = pgeomp->g_nhead * pgeomp->g_nsect;
2017 2017 pgeomp->g_capacity = capacity;
2018 2018 if (spc == 0)
2019 2019 pgeomp->g_ncyl = 0;
2020 2020 else
2021 2021 pgeomp->g_ncyl = pgeomp->g_capacity / spc;
2022 2022 pgeomp->g_acyl = 0;
2023 2023
2024 2024 /*
2025 2025 * Retrieve fresh geometry data from the hardware, stash it
2026 2026 * here temporarily before we rebuild the incore label.
2027 2027 *
2028 2028 * We want to use the MODE SENSE commands to derive the
2029 2029 * physical geometry of the device, but if either command
2030 2030 * fails, the logical geometry is used as the fallback for
2031 2031 * disk label geometry.
2032 2032 */
2033 2033
2034 2034 mutex_exit(CMLB_MUTEX(cl));
2035 2035 (void) DK_TG_GETPHYGEOM(cl, pgeomp, tg_cookie);
2036 2036 mutex_enter(CMLB_MUTEX(cl));
2037 2037
2038 2038 /*
2039 2039 * Now update the real copy while holding the mutex. This
2040 2040 * way the global copy is never in an inconsistent state.
2041 2041 */
2042 2042 bcopy(pgeomp, &cl->cl_pgeom, sizeof (cl->cl_pgeom));
2043 2043
2044 2044 cmlb_dbg(CMLB_INFO, cl, "cmlb_resync_geom_caches: "
2045 2045 "(cached from lgeom)\n");
2046 2046 cmlb_dbg(CMLB_INFO, cl,
2047 2047 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
2048 2048 cl->cl_pgeom.g_ncyl, cl->cl_pgeom.g_acyl,
2049 2049 cl->cl_pgeom.g_nhead, cl->cl_pgeom.g_nsect);
2050 2050 cmlb_dbg(CMLB_INFO, cl, " lbasize: %d; capacity: %ld; "
2051 2051 "intrlv: %d; rpm: %d\n", cl->cl_pgeom.g_secsize,
2052 2052 cl->cl_pgeom.g_capacity, cl->cl_pgeom.g_intrlv,
2053 2053 cl->cl_pgeom.g_rpm);
2054 2054 }
2055 2055
2056 2056
2057 2057 #if defined(__i386) || defined(__amd64)
2058 2058 /*
2059 2059 * Function: cmlb_update_ext_minor_nodes
2060 2060 *
2061 2061 * Description: Routine to add/remove extended partition device nodes
2062 2062 *
2063 2063 * Arguments:
2064 2064 * cl driver soft state (unit) structure
2065 2065 * num_parts Number of logical drives found on the LUN
2066 2066 *
2067 2067 * Should be called with the mutex held
2068 2068 *
2069 2069 * Return Code: 0 for success
2070 2070 *
2071 2071 * Context: User and Kernel thread
2072 2072 *
2073 2073 */
2074 2074 static int
2075 2075 cmlb_update_ext_minor_nodes(struct cmlb_lun *cl, int num_parts)
2076 2076 {
2077 2077 int i, count, shift;
2078 2078 char name[48];
2079 2079 int instance;
2080 2080 struct driver_minor_data *demdp, *demdpr;
2081 2081 char *devnm;
2082 2082 dev_info_t *pdip;
2083 2083 boolean_t internal;
2084 2084
2085 2085 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2086 2086 ASSERT(cl->cl_update_ext_minor_nodes == 1);
2087 2087
2088 2088 internal = VOID2BOOLEAN(
2089 2089 (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
2090 2090 instance = ddi_get_instance(CMLB_DEVINFO(cl));
2091 2091 demdp = dk_ext_minor_data;
2092 2092 demdpr = &dk_ext_minor_data[MAX_EXT_PARTS];
2093 2093
2094 2094 if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE)
2095 2095 shift = CMLBUNIT_FORCE_P0_SHIFT;
2096 2096 else
2097 2097 shift = CMLBUNIT_SHIFT;
2098 2098
2099 2099 if (cl->cl_logical_drive_count) {
2100 2100 for (i = 0; i < cl->cl_logical_drive_count; i++) {
2101 2101 (void) sprintf(name, "%s", demdp->name);
2102 2102 ddi_remove_minor_node(CMLB_DEVINFO(cl), name);
2103 2103 (void) sprintf(name, "%s", demdpr->name);
2104 2104 ddi_remove_minor_node(CMLB_DEVINFO(cl), name);
2105 2105 demdp++;
2106 2106 demdpr++;
2107 2107 }
2108 2108 /* There are existing device nodes. Remove them */
2109 2109 devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
2110 2110 (void) ddi_deviname(cl->cl_devi, devnm);
2111 2111 pdip = ddi_get_parent(cl->cl_devi);
2112 2112 (void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
2113 2113 kmem_free(devnm, MAXNAMELEN + 1);
2114 2114 }
2115 2115
2116 2116 demdp = dk_ext_minor_data;
2117 2117 demdpr = &dk_ext_minor_data[MAX_EXT_PARTS];
2118 2118
2119 2119 for (i = 0; i < num_parts; i++) {
2120 2120 (void) sprintf(name, "%s", demdp->name);
2121 2121 if (cmlb_create_minor(CMLB_DEVINFO(cl), name,
2122 2122 demdp->type,
2123 2123 (instance << shift) | demdp->minor,
2124 2124 cl->cl_node_type, NULL, internal) == DDI_FAILURE) {
2125 2125 /*
2126 2126 * Clean up any nodes that may have been
2127 2127 * created, in case this fails in the middle
2128 2128 * of the loop.
2129 2129 */
2130 2130 ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
2131 2131 cl->cl_logical_drive_count = 0;
2132 2132 return (ENXIO);
2133 2133 }
2134 2134 (void) sprintf(name, "%s", demdpr->name);
2135 2135 if (ddi_create_minor_node(CMLB_DEVINFO(cl), name,
2136 2136 demdpr->type,
2137 2137 (instance << shift) | demdpr->minor,
2138 2138 cl->cl_node_type, NULL) == DDI_FAILURE) {
2139 2139 /*
2140 2140 * Clean up any nodes that may have been
2141 2141 * created, in case this fails in the middle
2142 2142 * of the loop.
2143 2143 */
2144 2144 ddi_remove_minor_node(CMLB_DEVINFO(cl), NULL);
2145 2145 cl->cl_logical_drive_count = 0;
2146 2146 return (ENXIO);
2147 2147 }
2148 2148 demdp++;
2149 2149 demdpr++;
2150 2150 }
2151 2151
2152 2152 /* Update the cl_map array for logical drives */
2153 2153 for (count = 0; count < MAX_EXT_PARTS; count++) {
2154 2154 cl->cl_map[FDISK_P4 + 1 + count].dkl_cylno = UINT32_MAX;
2155 2155 cl->cl_map[FDISK_P4 + 1 + count].dkl_nblk =
2156 2156 cl->cl_fmap[FD_NUMPART + count].fmap_nblk;
2157 2157 cl->cl_offset[FDISK_P4 + 1 + count] =
2158 2158 cl->cl_fmap[FD_NUMPART + count].fmap_start;
2159 2159 }
2160 2160
2161 2161 cl->cl_logical_drive_count = i;
2162 2162 cl->cl_update_ext_minor_nodes = 0;
2163 2163 return (0);
2164 2164 }
2165 2165 /*
2166 2166 * Function: cmlb_validate_ext_part
2167 2167 *
2168 2168 * Description: utility routine to validate an extended partition's
2169 2169 * metadata as found on disk
2170 2170 *
2171 2171 * Arguments:
2172 2172 * cl driver soft state (unit) structure
2173 2173 * part partition number of the extended partition
2174 2174 * epart partition number of the logical drive
2175 2175 * start absolute sector number of the start of the logical
2176 2176 * drive being validated
2177 2177 * size size of logical drive being validated
2178 2178 *
2179 2179 * Return Code: 0 for success
2180 2180 *
2181 2181 * Context: User and Kernel thread
2182 2182 *
2183 2183 * Algorithm :
2184 2184 * Error cases are :
2185 2185 * 1. If start block is lesser than or equal to the end block
2186 2186 * 2. If either start block or end block is beyond the bounadry
2187 2187 * of the extended partition.
2188 2188 * 3. start or end block overlap with existing partitions.
2189 2189 * To check this, first make sure that the start block doesnt
2190 2190 * overlap with existing partitions. Then, calculate the
2191 2191 * possible end block for the given start block that doesnt
2192 2192 * overlap with existing partitions. This can be calculated by
2193 2193 * first setting the possible end block to the end of the
2194 2194 * extended partition (optimistic) and then, checking if there
2195 2195 * is any other partition that lies after the start of the
2196 2196 * partition being validated. If so, set the possible end to
2197 2197 * one block less than the beginning of the next nearest partition
2198 2198 * If the actual end block is greater than the calculated end
2199 2199 * block, we have an overlap.
2200 2200 *
2201 2201 */
2202 2202 static int
2203 2203 cmlb_validate_ext_part(struct cmlb_lun *cl, int part, int epart, uint32_t start,
2204 2204 uint32_t size)
2205 2205 {
2206 2206 int i;
2207 2207 uint32_t end = start + size - 1;
2208 2208 uint32_t ext_start = cl->cl_fmap[part].fmap_start;
2209 2209 uint32_t ext_end = ext_start + cl->cl_fmap[part].fmap_nblk - 1;
2210 2210 uint32_t ts, te;
2211 2211 uint32_t poss_end = ext_end;
2212 2212
2213 2213 if (end <= start) {
2214 2214 return (1);
2215 2215 }
2216 2216
2217 2217 /*
2218 2218 * Check if the logical drive boundaries are within that of the
2219 2219 * extended partition.
2220 2220 */
2221 2221 if (start <= ext_start || start > ext_end || end <= ext_start ||
2222 2222 end > ext_end) {
2223 2223 return (1);
2224 2224 }
2225 2225
2226 2226 /*
2227 2227 * epart will be equal to FD_NUMPART if it is the first logical drive.
2228 2228 * There is no need to check for overlaps with other logical drives,
2229 2229 * since it is the only logical drive that we have come across so far.
2230 2230 */
2231 2231 if (epart == FD_NUMPART) {
2232 2232 return (0);
2233 2233 }
2234 2234
2235 2235 /* Check for overlaps with existing logical drives */
2236 2236 i = FD_NUMPART;
2237 2237 ts = cl->cl_fmap[FD_NUMPART].fmap_start;
2238 2238 te = ts + cl->cl_fmap[FD_NUMPART].fmap_nblk - 1;
2239 2239
2240 2240 while ((i < epart) && ts && te) {
2241 2241 if (start >= ts && start <= te) {
2242 2242 return (1);
2243 2243 }
2244 2244
2245 2245 if ((ts < poss_end) && (ts > start)) {
2246 2246 poss_end = ts - 1;
2247 2247 }
2248 2248
2249 2249 i++;
2250 2250 ts = cl->cl_fmap[i].fmap_start;
2251 2251 te = ts + cl->cl_fmap[i].fmap_nblk - 1;
2252 2252 }
2253 2253
2254 2254 if (end > poss_end) {
2255 2255 return (1);
2256 2256 }
2257 2257
2258 2258 return (0);
2259 2259 }
2260 2260
2261 2261
2262 2262 /*
2263 2263 * Function: cmlb_is_linux_swap
2264 2264 *
2265 2265 * Description: utility routine to verify if a partition is a linux swap
2266 2266 * partition or not.
2267 2267 *
2268 2268 * Arguments:
2269 2269 * cl driver soft state (unit) structure
2270 2270 * part_start absolute sector number of the start of the partition
2271 2271 * being verified
2272 2272 * tg_cookie cookie from target driver to be passed back to target
2273 2273 * driver when we call back to it through tg_ops.
2274 2274 *
2275 2275 * Return Code: 0 for success
2276 2276 *
2277 2277 * Context: User and Kernel thread
2278 2278 *
2279 2279 * Notes:
2280 2280 * The linux swap magic "SWAP-SPACE" or "SWAPSPACE2" is found as the
2281 2281 * last 10 bytes of a disk block whose size is that of the linux page
2282 2282 * size. This disk block is found at the beginning of the swap partition.
2283 2283 */
2284 2284 static int
2285 2285 cmlb_is_linux_swap(struct cmlb_lun *cl, uint32_t part_start, void *tg_cookie)
2286 2286 {
2287 2287 int i;
2288 2288 int rval = -1;
2289 2289 uint32_t seek_offset;
2290 2290 uint32_t linux_pg_size;
2291 2291 char *buf, *linux_swap_magic;
2292 2292 int sec_sz = cl->cl_sys_blocksize;
2293 2293 /* Known linux kernel page sizes */
2294 2294 uint32_t linux_pg_size_arr[] = {4096, };
2295 2295
2296 2296 ASSERT(cl != NULL);
2297 2297 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2298 2298
2299 2299 if ((buf = kmem_zalloc(sec_sz, KM_NOSLEEP)) == NULL) {
2300 2300 return (ENOMEM);
2301 2301 }
2302 2302
2303 2303 /*
2304 2304 * Check if there is a sane Solaris VTOC
2305 2305 * If there is a valid vtoc, no need to lookup
2306 2306 * for the linux swap signature.
2307 2307 */
2308 2308 mutex_exit(CMLB_MUTEX(cl));
2309 2309 rval = DK_TG_READ(cl, buf, part_start + DK_LABEL_LOC,
2310 2310 sec_sz, tg_cookie);
2311 2311 mutex_enter(CMLB_MUTEX(cl));
2312 2312 if (rval != 0) {
2313 2313 cmlb_dbg(CMLB_ERROR, cl,
2314 2314 "cmlb_is_linux_swap: disk vtoc read err\n");
2315 2315 rval = EIO;
2316 2316 goto done;
2317 2317 }
2318 2318
2319 2319 if ((((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) &&
2320 2320 (((struct dk_label *)buf)->dkl_vtoc.v_sanity == VTOC_SANE)) {
2321 2321 rval = -1;
2322 2322 goto done;
2323 2323 }
2324 2324
2325 2325
2326 2326 /* No valid vtoc, so check for linux swap signature */
2327 2327 linux_swap_magic = buf + sec_sz - 10;
2328 2328
2329 2329 for (i = 0; i < sizeof (linux_pg_size_arr)/sizeof (uint32_t); i++) {
2330 2330 linux_pg_size = linux_pg_size_arr[i];
2331 2331 seek_offset = linux_pg_size/sec_sz - 1;
2332 2332 seek_offset += part_start;
2333 2333
2334 2334 mutex_exit(CMLB_MUTEX(cl));
2335 2335 rval = DK_TG_READ(cl, buf, seek_offset, sec_sz, tg_cookie);
2336 2336 mutex_enter(CMLB_MUTEX(cl));
2337 2337
2338 2338 if (rval != 0) {
2339 2339 cmlb_dbg(CMLB_ERROR, cl,
2340 2340 "cmlb_is_linux_swap: disk read err\n");
2341 2341 rval = EIO;
2342 2342 break;
2343 2343 }
2344 2344
2345 2345 rval = -1;
2346 2346
2347 2347 if ((strncmp(linux_swap_magic, "SWAP-SPACE", 10) == 0) ||
2348 2348 (strncmp(linux_swap_magic, "SWAPSPACE2", 10) == 0)) {
2349 2349 /* Found a linux swap */
2350 2350 rval = 0;
2351 2351 break;
2352 2352 }
2353 2353 }
2354 2354
2355 2355 done:
2356 2356 kmem_free(buf, sec_sz);
2357 2357 return (rval);
2358 2358 }
2359 2359 #endif
2360 2360
2361 2361 /*
2362 2362 * Function: cmlb_read_fdisk
2363 2363 *
2364 2364 * Description: utility routine to read the fdisk table.
2365 2365 *
2366 2366 * Arguments:
2367 2367 * cl driver soft state (unit) structure
2368 2368 * capacity disk capacity in #blocks
2369 2369 * tg_cookie cookie from target driver to be passed back to target
2370 2370 * driver when we call back to it through tg_ops.
2371 2371 *
2372 2372 * Return Code: 0 for success (includes not reading for no_fdisk_present case
2373 2373 * errnos from tg_rw if failed to read the first block.
2374 2374 *
2375 2375 * Context: Kernel thread only (can sleep).
2376 2376 */
2377 2377 /*ARGSUSED*/
2378 2378 static int
2379 2379 cmlb_read_fdisk(struct cmlb_lun *cl, diskaddr_t capacity, void *tg_cookie)
2380 2380 {
2381 2381 #if defined(_NO_FDISK_PRESENT)
2382 2382
2383 2383 cl->cl_solaris_offset = 0;
2384 2384 cl->cl_solaris_size = capacity;
2385 2385 bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
2386 2386 return (0);
2387 2387
2388 2388 #elif defined(_FIRMWARE_NEEDS_FDISK)
2389 2389
2390 2390 struct ipart *fdp;
2391 2391 struct mboot *mbp;
2392 2392 struct ipart fdisk[FD_NUMPART];
2393 2393 int i, k;
2394 2394 char sigbuf[2];
2395 2395 caddr_t bufp;
2396 2396 int uidx;
2397 2397 int rval;
2398 2398 int lba = 0;
2399 2399 uint_t solaris_offset; /* offset to solaris part. */
2400 2400 daddr_t solaris_size; /* size of solaris partition */
2401 2401 uint32_t blocksize;
2402 2402 #if defined(__i386) || defined(__amd64)
2403 2403 struct ipart eparts[2];
2404 2404 struct ipart *efdp1 = &eparts[0];
2405 2405 struct ipart *efdp2 = &eparts[1];
2406 2406 int ext_part_exists = 0;
2407 2407 int ld_count = 0;
2408 2408 #endif
2409 2409
2410 2410 ASSERT(cl != NULL);
2411 2411 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2412 2412
2413 2413 /*
2414 2414 * Start off assuming no fdisk table
2415 2415 */
2416 2416 solaris_offset = 0;
2417 2417 solaris_size = capacity;
2418 2418
2419 2419 blocksize = cl->cl_tgt_blocksize;
2420 2420
2421 2421 bufp = kmem_zalloc(blocksize, KM_SLEEP);
2422 2422
2423 2423 mutex_exit(CMLB_MUTEX(cl));
2424 2424 rval = DK_TG_READ(cl, bufp, 0, blocksize, tg_cookie);
2425 2425 mutex_enter(CMLB_MUTEX(cl));
2426 2426
2427 2427 if (rval != 0) {
2428 2428 cmlb_dbg(CMLB_ERROR, cl,
2429 2429 "cmlb_read_fdisk: fdisk read err\n");
2430 2430 bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
2431 2431 goto done;
2432 2432 }
2433 2433
2434 2434 mbp = (struct mboot *)bufp;
2435 2435
2436 2436 /*
2437 2437 * The fdisk table does not begin on a 4-byte boundary within the
2438 2438 * master boot record, so we copy it to an aligned structure to avoid
2439 2439 * alignment exceptions on some processors.
2440 2440 */
2441 2441 bcopy(&mbp->parts[0], fdisk, sizeof (fdisk));
2442 2442
2443 2443 /*
2444 2444 * Check for lba support before verifying sig; sig might not be
2445 2445 * there, say on a blank disk, but the max_chs mark may still
2446 2446 * be present.
2447 2447 *
2448 2448 * Note: LBA support and BEFs are an x86-only concept but this
2449 2449 * code should work OK on SPARC as well.
2450 2450 */
2451 2451
2452 2452 /*
2453 2453 * First, check for lba-access-ok on root node (or prom root node)
2454 2454 * if present there, don't need to search fdisk table.
2455 2455 */
2456 2456 if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0,
2457 2457 "lba-access-ok", 0) != 0) {
2458 2458 /* All drives do LBA; don't search fdisk table */
2459 2459 lba = 1;
2460 2460 } else {
2461 2461 /* Okay, look for mark in fdisk table */
2462 2462 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
2463 2463 /* accumulate "lba" value from all partitions */
2464 2464 lba = (lba || cmlb_has_max_chs_vals(fdp));
2465 2465 }
2466 2466 }
2467 2467
2468 2468 if (lba != 0) {
2469 2469 dev_t dev = cmlb_make_device(cl);
2470 2470
2471 2471 if (ddi_getprop(dev, CMLB_DEVINFO(cl), DDI_PROP_DONTPASS,
2472 2472 "lba-access-ok", 0) == 0) {
2473 2473 /* not found; create it */
2474 2474 if (ddi_prop_create(dev, CMLB_DEVINFO(cl), 0,
2475 2475 "lba-access-ok", (caddr_t)NULL, 0) !=
2476 2476 DDI_PROP_SUCCESS) {
2477 2477 cmlb_dbg(CMLB_ERROR, cl,
2478 2478 "cmlb_read_fdisk: Can't create lba "
2479 2479 "property for instance %d\n",
2480 2480 ddi_get_instance(CMLB_DEVINFO(cl)));
2481 2481 }
2482 2482 }
2483 2483 }
2484 2484
2485 2485 bcopy(&mbp->signature, sigbuf, sizeof (sigbuf));
2486 2486
2487 2487 /*
2488 2488 * Endian-independent signature check
2489 2489 */
2490 2490 if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) ||
2491 2491 (sigbuf[0] != (MBB_MAGIC & 0xFF))) {
2492 2492 cmlb_dbg(CMLB_ERROR, cl,
2493 2493 "cmlb_read_fdisk: no fdisk\n");
2494 2494 bzero(cl->cl_fmap, sizeof (struct fmap) * FD_NUMPART);
2495 2495 goto done;
2496 2496 }
2497 2497
2498 2498 #ifdef CMLBDEBUG
2499 2499 if (cmlb_level_mask & CMLB_LOGMASK_INFO) {
2500 2500 fdp = fdisk;
2501 2501 cmlb_dbg(CMLB_INFO, cl, "cmlb_read_fdisk:\n");
2502 2502 cmlb_dbg(CMLB_INFO, cl, " relsect "
2503 2503 "numsect sysid bootid\n");
2504 2504 for (i = 0; i < FD_NUMPART; i++, fdp++) {
2505 2505 cmlb_dbg(CMLB_INFO, cl,
2506 2506 " %d: %8d %8d 0x%08x 0x%08x\n",
2507 2507 i, fdp->relsect, fdp->numsect,
2508 2508 fdp->systid, fdp->bootid);
2509 2509 }
2510 2510 }
2511 2511 #endif
2512 2512
2513 2513 /*
2514 2514 * Try to find the unix partition
2515 2515 */
2516 2516 uidx = -1;
2517 2517 solaris_offset = 0;
2518 2518 solaris_size = 0;
2519 2519
2520 2520 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
2521 2521 uint32_t relsect;
2522 2522 uint32_t numsect;
2523 2523 uchar_t systid;
2524 2524 #if defined(__i386) || defined(__amd64)
2525 2525 /*
2526 2526 * Stores relative block offset from the beginning of the
2527 2527 * Extended Partition.
2528 2528 */
2529 2529 int ext_relsect = 0;
2530 2530 #endif
2531 2531
2532 2532 if (fdp->numsect == 0) {
2533 2533 cl->cl_fmap[i].fmap_start = 0;
2534 2534 cl->cl_fmap[i].fmap_nblk = 0;
2535 2535 continue;
2536 2536 }
2537 2537
2538 2538 /*
2539 2539 * Data in the fdisk table is little-endian.
2540 2540 */
2541 2541 relsect = LE_32(fdp->relsect);
2542 2542 numsect = LE_32(fdp->numsect);
2543 2543
2544 2544 cl->cl_fmap[i].fmap_start = relsect;
2545 2545 cl->cl_fmap[i].fmap_nblk = numsect;
2546 2546 cl->cl_fmap[i].fmap_systid = LE_8(fdp->systid);
2547 2547
2548 2548 #if defined(__i386) || defined(__amd64)
2549 2549 /* Support only one extended partition per LUN */
2550 2550 if ((fdp->systid == EXTDOS || fdp->systid == FDISK_EXTLBA) &&
2551 2551 (ext_part_exists == 0)) {
2552 2552 int j;
2553 2553 uint32_t logdrive_offset;
2554 2554 uint32_t ext_numsect;
2555 2555 uint32_t abs_secnum;
2556 2556
2557 2557 ext_part_exists = 1;
2558 2558
2559 2559 for (j = FD_NUMPART; j < FDISK_PARTS; j++) {
2560 2560 mutex_exit(CMLB_MUTEX(cl));
2561 2561 rval = DK_TG_READ(cl, bufp,
2562 2562 (relsect + ext_relsect), blocksize,
2563 2563 tg_cookie);
2564 2564 mutex_enter(CMLB_MUTEX(cl));
2565 2565
2566 2566 if (rval != 0) {
2567 2567 cmlb_dbg(CMLB_ERROR, cl,
2568 2568 "cmlb_read_fdisk: Extended "
2569 2569 "partition read err\n");
2570 2570 goto done;
2571 2571 }
2572 2572 /*
2573 2573 * The first ipart entry provides the offset
2574 2574 * at which the logical drive starts off from
2575 2575 * the beginning of the container partition
2576 2576 * and the size of the logical drive.
2577 2577 * The second ipart entry provides the offset
2578 2578 * of the next container partition from the
2579 2579 * beginning of the extended partition.
2580 2580 */
2581 2581 bcopy(&bufp[FDISK_PART_TABLE_START], eparts,
2582 2582 sizeof (eparts));
2583 2583 logdrive_offset = LE_32(efdp1->relsect);
2584 2584 ext_numsect = LE_32(efdp1->numsect);
2585 2585 systid = LE_8(efdp1->systid);
2586 2586 if (logdrive_offset <= 0 || ext_numsect <= 0)
2587 2587 break;
2588 2588 abs_secnum = relsect + ext_relsect +
2589 2589 logdrive_offset;
2590 2590
2591 2591 /* Boundary condition and overlap checking */
2592 2592 if (cmlb_validate_ext_part(cl, i, j, abs_secnum,
2593 2593 ext_numsect)) {
2594 2594 break;
2595 2595 }
2596 2596
2597 2597 if ((cl->cl_fmap[j].fmap_start != abs_secnum) ||
2598 2598 (cl->cl_fmap[j].fmap_nblk != ext_numsect) ||
2599 2599 (cl->cl_fmap[j].fmap_systid != systid)) {
2600 2600 /*
2601 2601 * Indicates change from previous
2602 2602 * partinfo. Need to recreate
2603 2603 * logical device nodes.
2604 2604 */
2605 2605 cl->cl_update_ext_minor_nodes = 1;
2606 2606 }
2607 2607 cl->cl_fmap[j].fmap_start = abs_secnum;
2608 2608 cl->cl_fmap[j].fmap_nblk = ext_numsect;
2609 2609 cl->cl_fmap[j].fmap_systid = systid;
2610 2610 ld_count++;
2611 2611
2612 2612 if ((efdp1->systid == SUNIXOS &&
2613 2613 (cmlb_is_linux_swap(cl, abs_secnum,
2614 2614 tg_cookie) != 0)) ||
2615 2615 efdp1->systid == SUNIXOS2) {
2616 2616 if (uidx == -1) {
2617 2617 uidx = 0;
2618 2618 solaris_offset = abs_secnum;
2619 2619 solaris_size = ext_numsect;
2620 2620 }
2621 2621 }
2622 2622
2623 2623 if ((ext_relsect = LE_32(efdp2->relsect)) == 0)
2624 2624 break;
2625 2625 }
2626 2626 }
2627 2627
2628 2628 #endif
2629 2629
2630 2630 if (fdp->systid != SUNIXOS &&
2631 2631 fdp->systid != SUNIXOS2 &&
2632 2632 fdp->systid != EFI_PMBR) {
2633 2633 continue;
2634 2634 }
2635 2635
2636 2636 /*
2637 2637 * use the last active solaris partition id found
2638 2638 * (there should only be 1 active partition id)
2639 2639 *
2640 2640 * if there are no active solaris partition id
2641 2641 * then use the first inactive solaris partition id
2642 2642 */
2643 2643 if ((uidx == -1) || (fdp->bootid == ACTIVE)) {
2644 2644 #if defined(__i386) || defined(__amd64)
2645 2645 if (fdp->systid != SUNIXOS ||
2646 2646 (fdp->systid == SUNIXOS &&
2647 2647 (cmlb_is_linux_swap(cl, relsect,
2648 2648 tg_cookie) != 0))) {
2649 2649 #endif
2650 2650 uidx = i;
2651 2651 solaris_offset = relsect;
2652 2652 solaris_size = numsect;
2653 2653 #if defined(__i386) || defined(__amd64)
2654 2654 }
2655 2655 #endif
2656 2656 }
2657 2657 }
2658 2658 #if defined(__i386) || defined(__amd64)
2659 2659 if (ld_count < cl->cl_logical_drive_count) {
2660 2660 /*
2661 2661 * Some/all logical drives were deleted. Clear out
2662 2662 * the fmap entries correspoding to those deleted drives.
2663 2663 */
2664 2664 for (k = ld_count + FD_NUMPART;
2665 2665 k < cl->cl_logical_drive_count + FD_NUMPART; k++) {
2666 2666 cl->cl_fmap[k].fmap_start = 0;
2667 2667 cl->cl_fmap[k].fmap_nblk = 0;
2668 2668 cl->cl_fmap[k].fmap_systid = 0;
2669 2669 }
2670 2670 cl->cl_update_ext_minor_nodes = 1;
2671 2671 }
2672 2672 if (cl->cl_update_ext_minor_nodes) {
2673 2673 rval = cmlb_update_ext_minor_nodes(cl, ld_count);
2674 2674 if (rval != 0) {
2675 2675 goto done;
2676 2676 }
2677 2677 }
2678 2678 #endif
2679 2679 cmlb_dbg(CMLB_INFO, cl, "fdisk 0x%x 0x%lx",
2680 2680 cl->cl_solaris_offset, cl->cl_solaris_size);
2681 2681 done:
2682 2682
2683 2683 /*
2684 2684 * Clear the VTOC info, only if the Solaris partition entry
2685 2685 * has moved, changed size, been deleted, or if the size of
2686 2686 * the partition is too small to even fit the label sector.
2687 2687 */
2688 2688 if ((cl->cl_solaris_offset != solaris_offset) ||
2689 2689 (cl->cl_solaris_size != solaris_size) ||
2690 2690 solaris_size <= DK_LABEL_LOC) {
2691 2691 cmlb_dbg(CMLB_INFO, cl, "fdisk moved 0x%x 0x%lx",
2692 2692 solaris_offset, solaris_size);
2693 2693 bzero(&cl->cl_g, sizeof (struct dk_geom));
2694 2694 bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
2695 2695 bzero(&cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
2696 2696 cl->cl_f_geometry_is_valid = B_FALSE;
2697 2697 }
2698 2698 cl->cl_solaris_offset = solaris_offset;
2699 2699 cl->cl_solaris_size = solaris_size;
2700 2700 kmem_free(bufp, blocksize);
↓ open down ↓ |
2700 lines elided |
↑ open up ↑ |
2701 2701 return (rval);
2702 2702
2703 2703 #else /* #elif defined(_FIRMWARE_NEEDS_FDISK) */
2704 2704 #error "fdisk table presence undetermined for this platform."
2705 2705 #endif /* #if defined(_NO_FDISK_PRESENT) */
2706 2706 }
2707 2707
2708 2708 static void
2709 2709 cmlb_swap_efi_gpt(efi_gpt_t *e)
2710 2710 {
2711 - _NOTE(ASSUMING_PROTECTED(*e))
2712 2711 e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature);
2713 2712 e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision);
2714 2713 e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize);
2715 2714 e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32);
2716 2715 e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA);
2717 2716 e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA);
2718 2717 e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA);
2719 2718 e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA);
2720 2719 UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID);
2721 2720 e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA);
2722 2721 e->efi_gpt_NumberOfPartitionEntries =
2723 2722 LE_32(e->efi_gpt_NumberOfPartitionEntries);
2724 2723 e->efi_gpt_SizeOfPartitionEntry =
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
2725 2724 LE_32(e->efi_gpt_SizeOfPartitionEntry);
2726 2725 e->efi_gpt_PartitionEntryArrayCRC32 =
2727 2726 LE_32(e->efi_gpt_PartitionEntryArrayCRC32);
2728 2727 }
2729 2728
2730 2729 static void
2731 2730 cmlb_swap_efi_gpe(int nparts, efi_gpe_t *p)
2732 2731 {
2733 2732 int i;
2734 2733
2735 - _NOTE(ASSUMING_PROTECTED(*p))
2736 2734 for (i = 0; i < nparts; i++) {
2737 2735 UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID,
2738 2736 p[i].efi_gpe_PartitionTypeGUID);
2739 2737 p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA);
2740 2738 p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA);
2741 2739 /* PartitionAttrs */
2742 2740 }
2743 2741 }
2744 2742
2745 2743 static int
2746 2744 cmlb_validate_efi(efi_gpt_t *labp)
2747 2745 {
2748 2746 if (labp->efi_gpt_Signature != EFI_SIGNATURE)
2749 2747 return (EINVAL);
2750 2748 /* at least 96 bytes in this version of the spec. */
2751 2749 if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) >
2752 2750 labp->efi_gpt_HeaderSize)
2753 2751 return (EINVAL);
2754 2752 /* this should be 128 bytes */
2755 2753 if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t))
2756 2754 return (EINVAL);
2757 2755 return (0);
2758 2756 }
2759 2757
2760 2758 /*
2761 2759 * This function returns B_FALSE if there is a valid MBR signature and no
2762 2760 * partition table entries of type EFI_PMBR (0xEE). Otherwise it returns B_TRUE.
2763 2761 *
2764 2762 * The EFI spec (1.10 and later) requires having a Protective MBR (PMBR) to
2765 2763 * recognize the disk as GPT partitioned. However, some other OS creates an MBR
2766 2764 * where a PMBR entry is not the only one. Also, if the first block has been
2767 2765 * corrupted, currently best attempt to allow data access would be to try to
2768 2766 * check for GPT headers. Hence in case of more than one partition entry, but
2769 2767 * at least one EFI_PMBR partition type or no valid magic number, the function
2770 2768 * returns B_TRUE to continue with looking for GPT header.
2771 2769 */
2772 2770
2773 2771 static boolean_t
2774 2772 cmlb_check_efi_mbr(uchar_t *buf, boolean_t *is_mbr)
2775 2773 {
2776 2774 struct ipart *fdp;
2777 2775 struct mboot *mbp = (struct mboot *)buf;
2778 2776 struct ipart fdisk[FD_NUMPART];
2779 2777 int i;
2780 2778
2781 2779 if (is_mbr != NULL)
2782 2780 *is_mbr = B_TRUE;
2783 2781
2784 2782 if (LE_16(mbp->signature) != MBB_MAGIC) {
2785 2783 if (is_mbr != NULL)
2786 2784 *is_mbr = B_FALSE;
2787 2785 return (B_TRUE);
2788 2786 }
2789 2787
2790 2788 bcopy(&mbp->parts[0], fdisk, sizeof (fdisk));
2791 2789
2792 2790 for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
2793 2791 if (fdp->systid == EFI_PMBR)
2794 2792 return (B_TRUE);
2795 2793 }
2796 2794
2797 2795 return (B_FALSE);
2798 2796 }
2799 2797
2800 2798 static int
2801 2799 cmlb_use_efi(struct cmlb_lun *cl, diskaddr_t capacity, int flags,
2802 2800 void *tg_cookie)
2803 2801 {
2804 2802 int i;
2805 2803 int rval = 0;
2806 2804 efi_gpe_t *partitions;
2807 2805 uchar_t *buf;
2808 2806 uint_t lbasize; /* is really how much to read */
2809 2807 diskaddr_t cap = 0;
2810 2808 uint_t nparts;
2811 2809 diskaddr_t gpe_lba;
2812 2810 diskaddr_t alternate_lba;
2813 2811 int iofailed = 0;
2814 2812 struct uuid uuid_type_reserved = EFI_RESERVED;
2815 2813 #if defined(_FIRMWARE_NEEDS_FDISK)
2816 2814 boolean_t is_mbr;
2817 2815 #endif
2818 2816
2819 2817 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
2820 2818
2821 2819 lbasize = cl->cl_sys_blocksize;
2822 2820
2823 2821 cl->cl_reserved = -1;
2824 2822 mutex_exit(CMLB_MUTEX(cl));
2825 2823
2826 2824 buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
2827 2825
2828 2826 rval = DK_TG_READ(cl, buf, 0, lbasize, tg_cookie);
2829 2827 if (rval) {
2830 2828 iofailed = 1;
2831 2829 goto done_err;
2832 2830 }
2833 2831 if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) {
2834 2832 /* not ours */
2835 2833 rval = ESRCH;
2836 2834 goto done_err;
2837 2835 }
2838 2836
2839 2837 #if defined(_FIRMWARE_NEEDS_FDISK)
2840 2838 if (!cmlb_check_efi_mbr(buf, &is_mbr)) {
2841 2839 if (is_mbr)
2842 2840 rval = ESRCH;
2843 2841 else
2844 2842 rval = EINVAL;
2845 2843 goto done_err;
2846 2844 }
2847 2845 #else
2848 2846 if (!cmlb_check_efi_mbr(buf, NULL)) {
2849 2847 rval = EINVAL;
2850 2848 goto done_err;
2851 2849 }
2852 2850
2853 2851 #endif
2854 2852
2855 2853 rval = DK_TG_READ(cl, buf, 1, lbasize, tg_cookie);
2856 2854 if (rval) {
2857 2855 iofailed = 1;
2858 2856 goto done_err;
2859 2857 }
2860 2858 cmlb_swap_efi_gpt((efi_gpt_t *)buf);
2861 2859
2862 2860 if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0) {
2863 2861 /*
2864 2862 * Couldn't read the primary, try the backup. Our
2865 2863 * capacity at this point could be based on CHS, so
2866 2864 * check what the device reports.
2867 2865 */
2868 2866 rval = DK_TG_GETCAP(cl, &cap, tg_cookie);
2869 2867 if (rval) {
2870 2868 iofailed = 1;
2871 2869 goto done_err;
2872 2870 }
2873 2871
2874 2872 /*
2875 2873 * CMLB_OFF_BY_ONE case, we check the next to last block first
2876 2874 * for backup GPT header, otherwise check the last block.
2877 2875 */
2878 2876
2879 2877 if ((rval = DK_TG_READ(cl, buf,
2880 2878 cap - ((cl->cl_alter_behavior & CMLB_OFF_BY_ONE) ? 2 : 1),
2881 2879 lbasize, tg_cookie))
2882 2880 != 0) {
2883 2881 iofailed = 1;
2884 2882 goto done_err;
2885 2883 }
2886 2884 cmlb_swap_efi_gpt((efi_gpt_t *)buf);
2887 2885
2888 2886 if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0) {
2889 2887
2890 2888 if (!(cl->cl_alter_behavior & CMLB_OFF_BY_ONE))
2891 2889 goto done_err;
2892 2890 if ((rval = DK_TG_READ(cl, buf, cap - 1, lbasize,
2893 2891 tg_cookie)) != 0)
2894 2892 goto done_err;
2895 2893 cmlb_swap_efi_gpt((efi_gpt_t *)buf);
2896 2894 if ((rval = cmlb_validate_efi((efi_gpt_t *)buf)) != 0)
2897 2895 goto done_err;
2898 2896 }
2899 2897 if (!(flags & CMLB_SILENT))
2900 2898 cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
2901 2899 "primary label corrupt; using backup\n");
2902 2900 }
2903 2901
2904 2902 nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries;
2905 2903 gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA;
2906 2904 alternate_lba = ((efi_gpt_t *)buf)->efi_gpt_AlternateLBA;
2907 2905
2908 2906 rval = DK_TG_READ(cl, buf, gpe_lba, EFI_MIN_ARRAY_SIZE, tg_cookie);
2909 2907 if (rval) {
2910 2908 iofailed = 1;
2911 2909 goto done_err;
2912 2910 }
2913 2911 partitions = (efi_gpe_t *)buf;
2914 2912
2915 2913 if (nparts > MAXPART) {
2916 2914 nparts = MAXPART;
2917 2915 }
2918 2916 cmlb_swap_efi_gpe(nparts, partitions);
2919 2917
2920 2918 mutex_enter(CMLB_MUTEX(cl));
2921 2919
2922 2920 /* Fill in partition table. */
2923 2921 for (i = 0; i < nparts; i++) {
2924 2922 if (partitions->efi_gpe_StartingLBA != 0 ||
2925 2923 partitions->efi_gpe_EndingLBA != 0) {
2926 2924 cl->cl_map[i].dkl_cylno =
2927 2925 partitions->efi_gpe_StartingLBA;
2928 2926 cl->cl_map[i].dkl_nblk =
2929 2927 partitions->efi_gpe_EndingLBA -
2930 2928 partitions->efi_gpe_StartingLBA + 1;
2931 2929 cl->cl_offset[i] =
2932 2930 partitions->efi_gpe_StartingLBA;
2933 2931 }
2934 2932
2935 2933 if (cl->cl_reserved == -1) {
2936 2934 if (bcmp(&partitions->efi_gpe_PartitionTypeGUID,
2937 2935 &uuid_type_reserved, sizeof (struct uuid)) == 0) {
2938 2936 cl->cl_reserved = i;
2939 2937 }
2940 2938 }
2941 2939 if (i == WD_NODE) {
2942 2940 /*
2943 2941 * minor number 7 corresponds to the whole disk
2944 2942 * if the disk capacity is expanded after disk is
2945 2943 * labeled, minor number 7 represents the capacity
2946 2944 * indicated by the disk label.
2947 2945 */
2948 2946 cl->cl_map[i].dkl_cylno = 0;
2949 2947 if (alternate_lba == 1) {
2950 2948 /*
2951 2949 * We are using backup label. Since we can
2952 2950 * find a valid label at the end of disk,
2953 2951 * the disk capacity is not expanded.
2954 2952 */
2955 2953 cl->cl_map[i].dkl_nblk = capacity;
2956 2954 } else {
2957 2955 cl->cl_map[i].dkl_nblk = alternate_lba + 1;
2958 2956 }
2959 2957 cl->cl_offset[i] = 0;
2960 2958 }
2961 2959 partitions++;
2962 2960 }
2963 2961 cl->cl_solaris_offset = 0;
2964 2962 cl->cl_solaris_size = capacity;
2965 2963 cl->cl_label_from_media = CMLB_LABEL_EFI;
2966 2964 cl->cl_f_geometry_is_valid = B_TRUE;
2967 2965
2968 2966 /* clear the vtoc label */
2969 2967 bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
2970 2968
2971 2969 kmem_free(buf, EFI_MIN_ARRAY_SIZE);
2972 2970 return (0);
2973 2971
2974 2972 done_err:
2975 2973 kmem_free(buf, EFI_MIN_ARRAY_SIZE);
2976 2974 mutex_enter(CMLB_MUTEX(cl));
2977 2975 done_err1:
2978 2976 /*
2979 2977 * if we didn't find something that could look like a VTOC
2980 2978 * and the disk is over 1TB, we know there isn't a valid label.
2981 2979 * Otherwise let cmlb_uselabel decide what to do. We only
2982 2980 * want to invalidate this if we're certain the label isn't
2983 2981 * valid because cmlb_prop_op will now fail, which in turn
2984 2982 * causes things like opens and stats on the partition to fail.
2985 2983 */
2986 2984 if ((capacity > CMLB_EXTVTOC_LIMIT) && (rval != ESRCH) && !iofailed) {
2987 2985 cl->cl_f_geometry_is_valid = B_FALSE;
2988 2986 }
2989 2987 return (rval);
2990 2988 }
2991 2989
2992 2990
2993 2991 /*
2994 2992 * Function: cmlb_uselabel
2995 2993 *
2996 2994 * Description: Validate the disk label and update the relevant data (geometry,
2997 2995 * partition, vtoc, and capacity data) in the cmlb_lun struct.
2998 2996 * Marks the geometry of the unit as being valid.
2999 2997 *
3000 2998 * Arguments: cl: unit struct.
3001 2999 * dk_label: disk label
3002 3000 *
3003 3001 * Return Code: CMLB_LABEL_IS_VALID: Label read from disk is OK; geometry,
3004 3002 * partition, vtoc, and capacity data are good.
3005 3003 *
3006 3004 * CMLB_LABEL_IS_INVALID: Magic number or checksum error in the
3007 3005 * label; or computed capacity does not jibe with capacity
3008 3006 * reported from the READ CAPACITY command.
3009 3007 *
3010 3008 * Context: Kernel thread only (can sleep).
3011 3009 */
3012 3010 static int
3013 3011 cmlb_uselabel(struct cmlb_lun *cl, struct dk_label *labp, int flags)
3014 3012 {
3015 3013 short *sp;
3016 3014 short sum;
3017 3015 short count;
3018 3016 int label_error = CMLB_LABEL_IS_VALID;
3019 3017 int i;
3020 3018 diskaddr_t label_capacity;
3021 3019 uint32_t part_end;
3022 3020 diskaddr_t track_capacity;
3023 3021 #if defined(_SUNOS_VTOC_16)
3024 3022 struct dkl_partition *vpartp;
3025 3023 #endif
3026 3024 ASSERT(cl != NULL);
3027 3025 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
3028 3026
3029 3027 /* Validate the magic number of the label. */
3030 3028 if (labp->dkl_magic != DKL_MAGIC) {
3031 3029 #if defined(__sparc)
3032 3030 if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
3033 3031 if (!(flags & CMLB_SILENT))
3034 3032 cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
3035 3033 CE_WARN,
3036 3034 "Corrupt label; wrong magic number\n");
3037 3035 }
3038 3036 #endif
3039 3037 return (CMLB_LABEL_IS_INVALID);
3040 3038 }
3041 3039
3042 3040 /* Validate the checksum of the label. */
3043 3041 sp = (short *)labp;
3044 3042 sum = 0;
3045 3043 count = sizeof (struct dk_label) / sizeof (short);
3046 3044 while (count--) {
3047 3045 sum ^= *sp++;
3048 3046 }
3049 3047
3050 3048 if (sum != 0) {
3051 3049 #if defined(_SUNOS_VTOC_16)
3052 3050 if (!ISCD(cl)) {
3053 3051 #elif defined(_SUNOS_VTOC_8)
3054 3052 if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
3055 3053 #endif
3056 3054 if (!(flags & CMLB_SILENT))
3057 3055 cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl),
3058 3056 CE_WARN,
3059 3057 "Corrupt label - label checksum failed\n");
3060 3058 }
3061 3059 return (CMLB_LABEL_IS_INVALID);
3062 3060 }
3063 3061
3064 3062
3065 3063 /*
3066 3064 * Fill in geometry structure with data from label.
3067 3065 */
3068 3066 bzero(&cl->cl_g, sizeof (struct dk_geom));
3069 3067 cl->cl_g.dkg_ncyl = labp->dkl_ncyl;
3070 3068 cl->cl_g.dkg_acyl = labp->dkl_acyl;
3071 3069 cl->cl_g.dkg_bcyl = 0;
3072 3070 cl->cl_g.dkg_nhead = labp->dkl_nhead;
3073 3071 cl->cl_g.dkg_nsect = labp->dkl_nsect;
3074 3072 cl->cl_g.dkg_intrlv = labp->dkl_intrlv;
3075 3073
3076 3074 #if defined(_SUNOS_VTOC_8)
3077 3075 cl->cl_g.dkg_gap1 = labp->dkl_gap1;
3078 3076 cl->cl_g.dkg_gap2 = labp->dkl_gap2;
3079 3077 cl->cl_g.dkg_bhead = labp->dkl_bhead;
3080 3078 #endif
3081 3079 #if defined(_SUNOS_VTOC_16)
3082 3080 cl->cl_dkg_skew = labp->dkl_skew;
3083 3081 #endif
3084 3082
3085 3083 #if defined(__i386) || defined(__amd64)
3086 3084 cl->cl_g.dkg_apc = labp->dkl_apc;
3087 3085 #endif
3088 3086
3089 3087 /*
3090 3088 * Currently we rely on the values in the label being accurate. If
3091 3089 * dkl_rpm or dkl_pcly are zero in the label, use a default value.
3092 3090 *
3093 3091 * Note: In the future a MODE SENSE may be used to retrieve this data,
3094 3092 * although this command is optional in SCSI-2.
3095 3093 */
3096 3094 cl->cl_g.dkg_rpm = (labp->dkl_rpm != 0) ? labp->dkl_rpm : 3600;
3097 3095 cl->cl_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl :
3098 3096 (cl->cl_g.dkg_ncyl + cl->cl_g.dkg_acyl);
3099 3097
3100 3098 /*
3101 3099 * The Read and Write reinstruct values may not be valid
3102 3100 * for older disks.
3103 3101 */
3104 3102 cl->cl_g.dkg_read_reinstruct = labp->dkl_read_reinstruct;
3105 3103 cl->cl_g.dkg_write_reinstruct = labp->dkl_write_reinstruct;
3106 3104
3107 3105 /* Fill in partition table. */
3108 3106 #if defined(_SUNOS_VTOC_8)
3109 3107 for (i = 0; i < NDKMAP; i++) {
3110 3108 cl->cl_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno;
3111 3109 cl->cl_map[i].dkl_nblk = labp->dkl_map[i].dkl_nblk;
3112 3110 }
3113 3111 #endif
3114 3112 #if defined(_SUNOS_VTOC_16)
3115 3113 vpartp = labp->dkl_vtoc.v_part;
3116 3114 track_capacity = labp->dkl_nhead * labp->dkl_nsect;
3117 3115
3118 3116 /* Prevent divide by zero */
3119 3117 if (track_capacity == 0) {
3120 3118 if (!(flags & CMLB_SILENT))
3121 3119 cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
3122 3120 "Corrupt label - zero nhead or nsect value\n");
3123 3121
3124 3122 return (CMLB_LABEL_IS_INVALID);
3125 3123 }
3126 3124
3127 3125 for (i = 0; i < NDKMAP; i++, vpartp++) {
3128 3126 cl->cl_map[i].dkl_cylno = vpartp->p_start / track_capacity;
3129 3127 cl->cl_map[i].dkl_nblk = vpartp->p_size;
3130 3128 }
3131 3129 #endif
3132 3130
3133 3131 /* Fill in VTOC Structure. */
3134 3132 bcopy(&labp->dkl_vtoc, &cl->cl_vtoc, sizeof (struct dk_vtoc));
3135 3133 #if defined(_SUNOS_VTOC_8)
3136 3134 /*
3137 3135 * The 8-slice vtoc does not include the ascii label; save it into
3138 3136 * the device's soft state structure here.
3139 3137 */
3140 3138 bcopy(labp->dkl_asciilabel, cl->cl_asciilabel, LEN_DKL_ASCII);
3141 3139 #endif
3142 3140
3143 3141 /* Now look for a valid capacity. */
3144 3142 track_capacity = (cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect);
3145 3143 label_capacity = (cl->cl_g.dkg_ncyl * track_capacity);
3146 3144
3147 3145 if (cl->cl_g.dkg_acyl) {
3148 3146 #if defined(__i386) || defined(__amd64)
3149 3147 /* we may have > 1 alts cylinder */
3150 3148 label_capacity += (track_capacity * cl->cl_g.dkg_acyl);
3151 3149 #else
3152 3150 label_capacity += track_capacity;
3153 3151 #endif
3154 3152 }
3155 3153
3156 3154 /*
3157 3155 * Force check here to ensure the computed capacity is valid.
3158 3156 * If capacity is zero, it indicates an invalid label and
3159 3157 * we should abort updating the relevant data then.
3160 3158 */
3161 3159 if (label_capacity == 0) {
3162 3160 if (!(flags & CMLB_SILENT))
3163 3161 cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
3164 3162 "Corrupt label - no valid capacity could be "
3165 3163 "retrieved\n");
3166 3164
3167 3165 return (CMLB_LABEL_IS_INVALID);
3168 3166 }
3169 3167
3170 3168 /* Mark the geometry as valid. */
3171 3169 cl->cl_f_geometry_is_valid = B_TRUE;
3172 3170
3173 3171 /*
3174 3172 * if we got invalidated when mutex exit and entered again,
3175 3173 * if blockcount different than when we came in, need to
3176 3174 * retry from beginning of cmlb_validate_geometry.
3177 3175 * revisit this on next phase of utilizing this for
3178 3176 * sd.
3179 3177 */
3180 3178
3181 3179 if (label_capacity <= cl->cl_blockcount) {
3182 3180 #if defined(_SUNOS_VTOC_8)
3183 3181 /*
3184 3182 * We can't let this happen on drives that are subdivided
3185 3183 * into logical disks (i.e., that have an fdisk table).
3186 3184 * The cl_blockcount field should always hold the full media
3187 3185 * size in sectors, period. This code would overwrite
3188 3186 * cl_blockcount with the size of the Solaris fdisk partition.
3189 3187 */
3190 3188 cmlb_dbg(CMLB_ERROR, cl,
3191 3189 "cmlb_uselabel: Label %d blocks; Drive %d blocks\n",
3192 3190 label_capacity, cl->cl_blockcount);
3193 3191 cl->cl_solaris_size = label_capacity;
3194 3192
3195 3193 #endif /* defined(_SUNOS_VTOC_8) */
3196 3194 goto done;
3197 3195 }
3198 3196
3199 3197 if (ISCD(cl)) {
3200 3198 /* For CDROMs, we trust that the data in the label is OK. */
3201 3199 #if defined(_SUNOS_VTOC_8)
3202 3200 for (i = 0; i < NDKMAP; i++) {
3203 3201 part_end = labp->dkl_nhead * labp->dkl_nsect *
3204 3202 labp->dkl_map[i].dkl_cylno +
3205 3203 labp->dkl_map[i].dkl_nblk - 1;
3206 3204
3207 3205 if ((labp->dkl_map[i].dkl_nblk) &&
3208 3206 (part_end > cl->cl_blockcount)) {
3209 3207 cl->cl_f_geometry_is_valid = B_FALSE;
3210 3208 break;
3211 3209 }
3212 3210 }
3213 3211 #endif
3214 3212 #if defined(_SUNOS_VTOC_16)
3215 3213 vpartp = &(labp->dkl_vtoc.v_part[0]);
3216 3214 for (i = 0; i < NDKMAP; i++, vpartp++) {
3217 3215 part_end = vpartp->p_start + vpartp->p_size;
3218 3216 if ((vpartp->p_size > 0) &&
3219 3217 (part_end > cl->cl_blockcount)) {
3220 3218 cl->cl_f_geometry_is_valid = B_FALSE;
3221 3219 break;
3222 3220 }
3223 3221 }
3224 3222 #endif
3225 3223 } else {
3226 3224 /* label_capacity > cl->cl_blockcount */
3227 3225 if (!(flags & CMLB_SILENT)) {
3228 3226 cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_WARN,
3229 3227 "Corrupt label - bad geometry\n");
3230 3228 cmlb_log(CMLB_DEVINFO(cl), CMLB_LABEL(cl), CE_CONT,
3231 3229 "Label says %llu blocks; Drive says %llu blocks\n",
3232 3230 label_capacity, cl->cl_blockcount);
3233 3231 }
3234 3232 cl->cl_f_geometry_is_valid = B_FALSE;
3235 3233 label_error = CMLB_LABEL_IS_INVALID;
3236 3234 }
3237 3235
3238 3236 done:
3239 3237
3240 3238 cmlb_dbg(CMLB_INFO, cl, "cmlb_uselabel: (label geometry)\n");
3241 3239 cmlb_dbg(CMLB_INFO, cl,
3242 3240 " ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n",
3243 3241 cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl,
3244 3242 cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
3245 3243
3246 3244 cmlb_dbg(CMLB_INFO, cl,
3247 3245 " label_capacity: %d; intrlv: %d; rpm: %d\n",
3248 3246 cl->cl_blockcount, cl->cl_g.dkg_intrlv, cl->cl_g.dkg_rpm);
3249 3247 cmlb_dbg(CMLB_INFO, cl, " wrt_reinstr: %d; rd_reinstr: %d\n",
3250 3248 cl->cl_g.dkg_write_reinstruct, cl->cl_g.dkg_read_reinstruct);
3251 3249
3252 3250 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
3253 3251
3254 3252 return (label_error);
3255 3253 }
3256 3254
3257 3255
3258 3256 /*
3259 3257 * Function: cmlb_build_default_label
3260 3258 *
3261 3259 * Description: Generate a default label for those devices that do not have
3262 3260 * one, e.g., new media, removable cartridges, etc..
3263 3261 *
3264 3262 * Context: Kernel thread only
3265 3263 */
3266 3264 /*ARGSUSED*/
3267 3265 static void
3268 3266 cmlb_build_default_label(struct cmlb_lun *cl, void *tg_cookie)
3269 3267 {
3270 3268 #if defined(_SUNOS_VTOC_16)
3271 3269 uint_t phys_spc;
3272 3270 uint_t disksize;
3273 3271 struct dk_geom cl_g;
3274 3272 diskaddr_t capacity;
3275 3273 #endif
3276 3274
3277 3275 ASSERT(cl != NULL);
3278 3276 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
3279 3277
3280 3278 #if defined(_SUNOS_VTOC_8)
3281 3279 /*
3282 3280 * Note: This is a legacy check for non-removable devices on VTOC_8
3283 3281 * only. This may be a valid check for VTOC_16 as well.
3284 3282 * Once we understand why there is this difference between SPARC and
3285 3283 * x86 platform, we could remove this legacy check.
3286 3284 */
3287 3285 if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
3288 3286 return;
3289 3287 }
3290 3288 #endif
3291 3289
3292 3290 bzero(&cl->cl_g, sizeof (struct dk_geom));
3293 3291 bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
3294 3292 bzero(&cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
3295 3293
3296 3294 #if defined(_SUNOS_VTOC_8)
3297 3295
3298 3296 /*
3299 3297 * It's a REMOVABLE media, therefore no label (on sparc, anyway).
3300 3298 * But it is still necessary to set up various geometry information,
3301 3299 * and we are doing this here.
3302 3300 */
3303 3301
3304 3302 /*
3305 3303 * For the rpm, we use the minimum for the disk. For the head, cyl,
3306 3304 * and number of sector per track, if the capacity <= 1GB, head = 64,
3307 3305 * sect = 32. else head = 255, sect 63 Note: the capacity should be
3308 3306 * equal to C*H*S values. This will cause some truncation of size due
3309 3307 * to round off errors. For CD-ROMs, this truncation can have adverse
3310 3308 * side effects, so returning ncyl and nhead as 1. The nsect will
3311 3309 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569)
3312 3310 */
3313 3311 cl->cl_solaris_size = cl->cl_blockcount;
3314 3312 if (ISCD(cl)) {
3315 3313 tg_attribute_t tgattribute;
3316 3314 int is_writable;
3317 3315 /*
3318 3316 * Preserve the old behavior for non-writable
3319 3317 * medias. Since dkg_nsect is a ushort, it
3320 3318 * will lose bits as cdroms have more than
3321 3319 * 65536 sectors. So if we recalculate
3322 3320 * capacity, it will become much shorter.
3323 3321 * But the dkg_* information is not
3324 3322 * used for CDROMs so it is OK. But for
3325 3323 * Writable CDs we need this information
3326 3324 * to be valid (for newfs say). So we
3327 3325 * make nsect and nhead > 1 that way
3328 3326 * nsect can still stay within ushort limit
3329 3327 * without losing any bits.
3330 3328 */
3331 3329
3332 3330 bzero(&tgattribute, sizeof (tg_attribute_t));
3333 3331
3334 3332 mutex_exit(CMLB_MUTEX(cl));
3335 3333 is_writable =
3336 3334 (DK_TG_GETATTRIBUTE(cl, &tgattribute, tg_cookie) == 0) ?
3337 3335 tgattribute.media_is_writable : 1;
3338 3336 mutex_enter(CMLB_MUTEX(cl));
3339 3337
3340 3338 if (is_writable) {
3341 3339 cl->cl_g.dkg_nhead = 64;
3342 3340 cl->cl_g.dkg_nsect = 32;
3343 3341 cl->cl_g.dkg_ncyl = cl->cl_blockcount / (64 * 32);
3344 3342 cl->cl_solaris_size = (diskaddr_t)cl->cl_g.dkg_ncyl *
3345 3343 cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
3346 3344 } else {
3347 3345 cl->cl_g.dkg_ncyl = 1;
3348 3346 cl->cl_g.dkg_nhead = 1;
3349 3347 cl->cl_g.dkg_nsect = cl->cl_blockcount;
3350 3348 }
3351 3349 } else {
3352 3350 if (cl->cl_blockcount < 160) {
3353 3351 /* Less than 80K */
3354 3352 cl->cl_g.dkg_nhead = 1;
3355 3353 cl->cl_g.dkg_ncyl = cl->cl_blockcount;
3356 3354 cl->cl_g.dkg_nsect = 1;
3357 3355 } else if (cl->cl_blockcount <= 0x1000) {
3358 3356 /* unlabeled SCSI floppy device */
3359 3357 cl->cl_g.dkg_nhead = 2;
3360 3358 cl->cl_g.dkg_ncyl = 80;
3361 3359 cl->cl_g.dkg_nsect = cl->cl_blockcount / (2 * 80);
3362 3360 } else if (cl->cl_blockcount <= 0x200000) {
3363 3361 cl->cl_g.dkg_nhead = 64;
3364 3362 cl->cl_g.dkg_nsect = 32;
3365 3363 cl->cl_g.dkg_ncyl = cl->cl_blockcount / (64 * 32);
3366 3364 } else {
3367 3365 cl->cl_g.dkg_nhead = 255;
3368 3366
3369 3367 cl->cl_g.dkg_nsect = ((cl->cl_blockcount +
3370 3368 (UINT16_MAX * 255 * 63) - 1) /
3371 3369 (UINT16_MAX * 255 * 63)) * 63;
3372 3370
3373 3371 if (cl->cl_g.dkg_nsect == 0)
3374 3372 cl->cl_g.dkg_nsect = (UINT16_MAX / 63) * 63;
3375 3373
3376 3374 cl->cl_g.dkg_ncyl = cl->cl_blockcount /
3377 3375 (255 * cl->cl_g.dkg_nsect);
3378 3376 }
3379 3377
3380 3378 cl->cl_solaris_size =
3381 3379 (diskaddr_t)cl->cl_g.dkg_ncyl * cl->cl_g.dkg_nhead *
3382 3380 cl->cl_g.dkg_nsect;
3383 3381
3384 3382 }
3385 3383
3386 3384 cl->cl_g.dkg_acyl = 0;
3387 3385 cl->cl_g.dkg_bcyl = 0;
3388 3386 cl->cl_g.dkg_rpm = 200;
3389 3387 cl->cl_asciilabel[0] = '\0';
3390 3388 cl->cl_g.dkg_pcyl = cl->cl_g.dkg_ncyl;
3391 3389
3392 3390 cl->cl_map[0].dkl_cylno = 0;
3393 3391 cl->cl_map[0].dkl_nblk = cl->cl_solaris_size;
3394 3392
3395 3393 cl->cl_map[2].dkl_cylno = 0;
3396 3394 cl->cl_map[2].dkl_nblk = cl->cl_solaris_size;
3397 3395
3398 3396 #elif defined(_SUNOS_VTOC_16)
3399 3397
3400 3398 if (cl->cl_solaris_size == 0) {
3401 3399 /*
3402 3400 * Got fdisk table but no solaris entry therefore
3403 3401 * don't create a default label
3404 3402 */
3405 3403 cl->cl_f_geometry_is_valid = B_TRUE;
3406 3404 return;
3407 3405 }
3408 3406
3409 3407 /*
3410 3408 * For CDs we continue to use the physical geometry to calculate
3411 3409 * number of cylinders. All other devices must convert the
3412 3410 * physical geometry (cmlb_geom) to values that will fit
3413 3411 * in a dk_geom structure.
3414 3412 */
3415 3413 if (ISCD(cl)) {
3416 3414 phys_spc = cl->cl_pgeom.g_nhead * cl->cl_pgeom.g_nsect;
3417 3415 } else {
3418 3416 /* Convert physical geometry to disk geometry */
3419 3417 bzero(&cl_g, sizeof (struct dk_geom));
3420 3418
3421 3419 /*
3422 3420 * Refer to comments related to off-by-1 at the
3423 3421 * header of this file.
3424 3422 * Before calculating geometry, capacity should be
3425 3423 * decreased by 1.
3426 3424 */
3427 3425
3428 3426 if (cl->cl_alter_behavior & CMLB_OFF_BY_ONE)
3429 3427 capacity = cl->cl_blockcount - 1;
3430 3428 else
3431 3429 capacity = cl->cl_blockcount;
3432 3430
3433 3431
3434 3432 cmlb_convert_geometry(cl, capacity, &cl_g, tg_cookie);
3435 3433 bcopy(&cl_g, &cl->cl_g, sizeof (cl->cl_g));
3436 3434 phys_spc = cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
3437 3435 }
3438 3436
3439 3437 if (phys_spc == 0)
3440 3438 return;
3441 3439 cl->cl_g.dkg_pcyl = cl->cl_solaris_size / phys_spc;
3442 3440 if (cl->cl_alter_behavior & CMLB_FAKE_LABEL_ONE_PARTITION) {
3443 3441 /* disable devid */
3444 3442 cl->cl_g.dkg_ncyl = cl->cl_g.dkg_pcyl;
3445 3443 disksize = cl->cl_solaris_size;
3446 3444 } else {
3447 3445 cl->cl_g.dkg_acyl = DK_ACYL;
3448 3446 cl->cl_g.dkg_ncyl = cl->cl_g.dkg_pcyl - DK_ACYL;
3449 3447 disksize = cl->cl_g.dkg_ncyl * phys_spc;
3450 3448 }
3451 3449
3452 3450 if (ISCD(cl)) {
3453 3451 /*
3454 3452 * CD's don't use the "heads * sectors * cyls"-type of
3455 3453 * geometry, but instead use the entire capacity of the media.
3456 3454 */
3457 3455 disksize = cl->cl_solaris_size;
3458 3456 cl->cl_g.dkg_nhead = 1;
3459 3457 cl->cl_g.dkg_nsect = 1;
3460 3458 cl->cl_g.dkg_rpm =
3461 3459 (cl->cl_pgeom.g_rpm == 0) ? 200 : cl->cl_pgeom.g_rpm;
3462 3460
3463 3461 cl->cl_vtoc.v_part[0].p_start = 0;
3464 3462 cl->cl_vtoc.v_part[0].p_size = disksize;
3465 3463 cl->cl_vtoc.v_part[0].p_tag = V_BACKUP;
3466 3464 cl->cl_vtoc.v_part[0].p_flag = V_UNMNT;
3467 3465
3468 3466 cl->cl_map[0].dkl_cylno = 0;
3469 3467 cl->cl_map[0].dkl_nblk = disksize;
3470 3468 cl->cl_offset[0] = 0;
3471 3469
3472 3470 } else {
3473 3471 /*
3474 3472 * Hard disks and removable media cartridges
3475 3473 */
3476 3474 cl->cl_g.dkg_rpm =
3477 3475 (cl->cl_pgeom.g_rpm == 0) ? 3600: cl->cl_pgeom.g_rpm;
3478 3476 cl->cl_vtoc.v_sectorsz = cl->cl_sys_blocksize;
3479 3477
3480 3478 /* Add boot slice */
3481 3479 cl->cl_vtoc.v_part[8].p_start = 0;
3482 3480 cl->cl_vtoc.v_part[8].p_size = phys_spc;
3483 3481 cl->cl_vtoc.v_part[8].p_tag = V_BOOT;
3484 3482 cl->cl_vtoc.v_part[8].p_flag = V_UNMNT;
3485 3483
3486 3484 cl->cl_map[8].dkl_cylno = 0;
3487 3485 cl->cl_map[8].dkl_nblk = phys_spc;
3488 3486 cl->cl_offset[8] = 0;
3489 3487
3490 3488 if ((cl->cl_alter_behavior &
3491 3489 CMLB_CREATE_ALTSLICE_VTOC_16_DTYPE_DIRECT) &&
3492 3490 cl->cl_device_type == DTYPE_DIRECT) {
3493 3491 cl->cl_vtoc.v_part[9].p_start = phys_spc;
3494 3492 cl->cl_vtoc.v_part[9].p_size = 2 * phys_spc;
3495 3493 cl->cl_vtoc.v_part[9].p_tag = V_ALTSCTR;
3496 3494 cl->cl_vtoc.v_part[9].p_flag = 0;
3497 3495
3498 3496 cl->cl_map[9].dkl_cylno = 1;
3499 3497 cl->cl_map[9].dkl_nblk = 2 * phys_spc;
3500 3498 cl->cl_offset[9] = phys_spc;
3501 3499 }
3502 3500 }
3503 3501
3504 3502 cl->cl_g.dkg_apc = 0;
3505 3503
3506 3504 /* Add backup slice */
3507 3505 cl->cl_vtoc.v_part[2].p_start = 0;
3508 3506 cl->cl_vtoc.v_part[2].p_size = disksize;
3509 3507 cl->cl_vtoc.v_part[2].p_tag = V_BACKUP;
3510 3508 cl->cl_vtoc.v_part[2].p_flag = V_UNMNT;
3511 3509
3512 3510 cl->cl_map[2].dkl_cylno = 0;
3513 3511 cl->cl_map[2].dkl_nblk = disksize;
3514 3512 cl->cl_offset[2] = 0;
3515 3513
3516 3514 /*
3517 3515 * single slice (s0) covering the entire disk
3518 3516 */
3519 3517 if (cl->cl_alter_behavior & CMLB_FAKE_LABEL_ONE_PARTITION) {
3520 3518 cl->cl_vtoc.v_part[0].p_start = 0;
3521 3519 cl->cl_vtoc.v_part[0].p_tag = V_UNASSIGNED;
3522 3520 cl->cl_vtoc.v_part[0].p_flag = 0;
3523 3521 cl->cl_vtoc.v_part[0].p_size = disksize;
3524 3522 cl->cl_map[0].dkl_cylno = 0;
3525 3523 cl->cl_map[0].dkl_nblk = disksize;
3526 3524 cl->cl_offset[0] = 0;
3527 3525 }
3528 3526
3529 3527 (void) sprintf(cl->cl_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d"
3530 3528 " hd %d sec %d", cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl,
3531 3529 cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
3532 3530
3533 3531 #else
3534 3532 #error "No VTOC format defined."
3535 3533 #endif
3536 3534
3537 3535 cl->cl_g.dkg_read_reinstruct = 0;
3538 3536 cl->cl_g.dkg_write_reinstruct = 0;
3539 3537
3540 3538 cl->cl_g.dkg_intrlv = 1;
3541 3539
3542 3540 cl->cl_vtoc.v_sanity = VTOC_SANE;
3543 3541 cl->cl_vtoc.v_nparts = V_NUMPAR;
3544 3542 cl->cl_vtoc.v_version = V_VERSION;
3545 3543
3546 3544 cl->cl_f_geometry_is_valid = B_TRUE;
3547 3545 cl->cl_label_from_media = CMLB_LABEL_UNDEF;
3548 3546
3549 3547 cmlb_dbg(CMLB_INFO, cl,
3550 3548 "cmlb_build_default_label: Default label created: "
3551 3549 "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n",
3552 3550 cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl, cl->cl_g.dkg_nhead,
3553 3551 cl->cl_g.dkg_nsect, cl->cl_blockcount);
3554 3552 }
3555 3553
3556 3554
3557 3555 #if defined(_FIRMWARE_NEEDS_FDISK)
3558 3556 /*
3559 3557 * Max CHS values, as they are encoded into bytes, for 1022/254/63
3560 3558 */
3561 3559 #define LBA_MAX_SECT (63 | ((1022 & 0x300) >> 2))
3562 3560 #define LBA_MAX_CYL (1022 & 0xFF)
3563 3561 #define LBA_MAX_HEAD (254)
3564 3562
3565 3563
3566 3564 /*
3567 3565 * Function: cmlb_has_max_chs_vals
3568 3566 *
3569 3567 * Description: Return B_TRUE if Cylinder-Head-Sector values are all at maximum.
3570 3568 *
3571 3569 * Arguments: fdp - ptr to CHS info
3572 3570 *
3573 3571 * Return Code: True or false
3574 3572 *
3575 3573 * Context: Any.
3576 3574 */
3577 3575 static boolean_t
3578 3576 cmlb_has_max_chs_vals(struct ipart *fdp)
3579 3577 {
3580 3578 return ((fdp->begcyl == LBA_MAX_CYL) &&
3581 3579 (fdp->beghead == LBA_MAX_HEAD) &&
3582 3580 (fdp->begsect == LBA_MAX_SECT) &&
3583 3581 (fdp->endcyl == LBA_MAX_CYL) &&
3584 3582 (fdp->endhead == LBA_MAX_HEAD) &&
3585 3583 (fdp->endsect == LBA_MAX_SECT));
3586 3584 }
3587 3585 #endif
3588 3586
3589 3587 /*
3590 3588 * Function: cmlb_dkio_get_geometry
3591 3589 *
3592 3590 * Description: This routine is the driver entry point for handling user
3593 3591 * requests to get the device geometry (DKIOCGGEOM).
3594 3592 *
3595 3593 * Arguments:
3596 3594 * arg pointer to user provided dk_geom structure specifying
3597 3595 * the controller's notion of the current geometry.
3598 3596 *
3599 3597 * flag this argument is a pass through to ddi_copyxxx()
3600 3598 * directly from the mode argument of ioctl().
3601 3599 *
3602 3600 * tg_cookie cookie from target driver to be passed back to target
3603 3601 * driver when we call back to it through tg_ops.
3604 3602 *
3605 3603 * Return Code: 0
3606 3604 * EFAULT
3607 3605 * ENXIO
3608 3606 * EIO
3609 3607 */
3610 3608 static int
3611 3609 cmlb_dkio_get_geometry(struct cmlb_lun *cl, caddr_t arg, int flag,
3612 3610 void *tg_cookie)
3613 3611 {
3614 3612 struct dk_geom *tmp_geom = NULL;
3615 3613 int rval = 0;
3616 3614
3617 3615 /*
3618 3616 * cmlb_validate_geometry does not spin a disk up
3619 3617 * if it was spcl down. We need to make sure it
3620 3618 * is ready.
3621 3619 */
3622 3620 mutex_enter(CMLB_MUTEX(cl));
3623 3621 rval = cmlb_validate_geometry(cl, B_TRUE, 0, tg_cookie);
3624 3622 #if defined(_SUNOS_VTOC_8)
3625 3623 if (rval == EINVAL &&
3626 3624 cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8) {
3627 3625 /*
3628 3626 * This is to return a default label geometry even when we
3629 3627 * do not really assume a default label for the device.
3630 3628 * dad driver utilizes this.
3631 3629 */
3632 3630 if (cl->cl_blockcount <= CMLB_OLDVTOC_LIMIT) {
3633 3631 cmlb_setup_default_geometry(cl, tg_cookie);
3634 3632 rval = 0;
3635 3633 }
3636 3634 }
3637 3635 #endif
3638 3636 if (rval) {
3639 3637 mutex_exit(CMLB_MUTEX(cl));
3640 3638 return (rval);
3641 3639 }
3642 3640
3643 3641 #if defined(__i386) || defined(__amd64)
3644 3642 if (cl->cl_solaris_size == 0) {
3645 3643 mutex_exit(CMLB_MUTEX(cl));
3646 3644 return (EIO);
3647 3645 }
3648 3646 #endif
3649 3647
3650 3648 /*
3651 3649 * Make a local copy of the soft state geometry to avoid some potential
3652 3650 * race conditions associated with holding the mutex and updating the
3653 3651 * write_reinstruct value
3654 3652 */
3655 3653 tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
3656 3654 bcopy(&cl->cl_g, tmp_geom, sizeof (struct dk_geom));
3657 3655
3658 3656 if (tmp_geom->dkg_write_reinstruct == 0) {
3659 3657 tmp_geom->dkg_write_reinstruct =
3660 3658 (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm *
3661 3659 cmlb_rot_delay) / (int)60000);
3662 3660 }
3663 3661 mutex_exit(CMLB_MUTEX(cl));
3664 3662
3665 3663 rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom),
3666 3664 flag);
3667 3665 if (rval != 0) {
3668 3666 rval = EFAULT;
3669 3667 }
3670 3668
3671 3669 kmem_free(tmp_geom, sizeof (struct dk_geom));
3672 3670 return (rval);
3673 3671
3674 3672 }
3675 3673
3676 3674
3677 3675 /*
3678 3676 * Function: cmlb_dkio_set_geometry
3679 3677 *
3680 3678 * Description: This routine is the driver entry point for handling user
3681 3679 * requests to set the device geometry (DKIOCSGEOM). The actual
3682 3680 * device geometry is not updated, just the driver "notion" of it.
3683 3681 *
3684 3682 * Arguments:
3685 3683 * arg pointer to user provided dk_geom structure used to set
3686 3684 * the controller's notion of the current geometry.
3687 3685 *
3688 3686 * flag this argument is a pass through to ddi_copyxxx()
3689 3687 * directly from the mode argument of ioctl().
3690 3688 *
3691 3689 * tg_cookie cookie from target driver to be passed back to target
3692 3690 * driver when we call back to it through tg_ops.
3693 3691 *
3694 3692 * Return Code: 0
3695 3693 * EFAULT
3696 3694 * ENXIO
3697 3695 * EIO
3698 3696 */
3699 3697 static int
3700 3698 cmlb_dkio_set_geometry(struct cmlb_lun *cl, caddr_t arg, int flag)
3701 3699 {
3702 3700 struct dk_geom *tmp_geom;
3703 3701 struct dk_map *lp;
3704 3702 int rval = 0;
3705 3703 int i;
3706 3704
3707 3705
3708 3706 #if defined(__i386) || defined(__amd64)
3709 3707 if (cl->cl_solaris_size == 0) {
3710 3708 return (EIO);
3711 3709 }
3712 3710 #endif
3713 3711 /*
3714 3712 * We need to copy the user specified geometry into local
3715 3713 * storage and then update the softstate. We don't want to hold
3716 3714 * the mutex and copyin directly from the user to the soft state
3717 3715 */
3718 3716 tmp_geom = (struct dk_geom *)
3719 3717 kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
3720 3718 rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag);
3721 3719 if (rval != 0) {
3722 3720 kmem_free(tmp_geom, sizeof (struct dk_geom));
3723 3721 return (EFAULT);
3724 3722 }
3725 3723
3726 3724 mutex_enter(CMLB_MUTEX(cl));
3727 3725 bcopy(tmp_geom, &cl->cl_g, sizeof (struct dk_geom));
3728 3726 for (i = 0; i < NDKMAP; i++) {
3729 3727 lp = &cl->cl_map[i];
3730 3728 cl->cl_offset[i] =
3731 3729 cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
3732 3730 #if defined(__i386) || defined(__amd64)
3733 3731 cl->cl_offset[i] += cl->cl_solaris_offset;
3734 3732 #endif
3735 3733 }
3736 3734 cl->cl_f_geometry_is_valid = B_FALSE;
3737 3735 mutex_exit(CMLB_MUTEX(cl));
3738 3736 kmem_free(tmp_geom, sizeof (struct dk_geom));
3739 3737
3740 3738 return (rval);
3741 3739 }
3742 3740
3743 3741 /*
3744 3742 * Function: cmlb_dkio_get_partition
3745 3743 *
3746 3744 * Description: This routine is the driver entry point for handling user
3747 3745 * requests to get the partition table (DKIOCGAPART).
3748 3746 *
3749 3747 * Arguments:
3750 3748 * arg pointer to user provided dk_allmap structure specifying
3751 3749 * the controller's notion of the current partition table.
3752 3750 *
3753 3751 * flag this argument is a pass through to ddi_copyxxx()
3754 3752 * directly from the mode argument of ioctl().
3755 3753 *
3756 3754 * tg_cookie cookie from target driver to be passed back to target
3757 3755 * driver when we call back to it through tg_ops.
3758 3756 *
3759 3757 * Return Code: 0
3760 3758 * EFAULT
3761 3759 * ENXIO
3762 3760 * EIO
3763 3761 */
3764 3762 static int
3765 3763 cmlb_dkio_get_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
3766 3764 void *tg_cookie)
3767 3765 {
3768 3766 int rval = 0;
3769 3767 int size;
3770 3768
3771 3769 /*
3772 3770 * Make sure the geometry is valid before getting the partition
3773 3771 * information.
3774 3772 */
3775 3773 mutex_enter(CMLB_MUTEX(cl));
3776 3774 if ((rval = cmlb_validate_geometry(cl, B_TRUE, 0, tg_cookie)) != 0) {
3777 3775 mutex_exit(CMLB_MUTEX(cl));
3778 3776 return (rval);
3779 3777 }
3780 3778 mutex_exit(CMLB_MUTEX(cl));
3781 3779
3782 3780 #if defined(__i386) || defined(__amd64)
3783 3781 if (cl->cl_solaris_size == 0) {
3784 3782 return (EIO);
3785 3783 }
3786 3784 #endif
3787 3785
3788 3786 #ifdef _MULTI_DATAMODEL
3789 3787 switch (ddi_model_convert_from(flag & FMODELS)) {
3790 3788 case DDI_MODEL_ILP32: {
3791 3789 struct dk_map32 dk_map32[NDKMAP];
3792 3790 int i;
3793 3791
3794 3792 for (i = 0; i < NDKMAP; i++) {
3795 3793 dk_map32[i].dkl_cylno = cl->cl_map[i].dkl_cylno;
3796 3794 dk_map32[i].dkl_nblk = cl->cl_map[i].dkl_nblk;
3797 3795 }
3798 3796 size = NDKMAP * sizeof (struct dk_map32);
3799 3797 rval = ddi_copyout(dk_map32, (void *)arg, size, flag);
3800 3798 if (rval != 0) {
3801 3799 rval = EFAULT;
3802 3800 }
3803 3801 break;
3804 3802 }
3805 3803 case DDI_MODEL_NONE:
3806 3804 size = NDKMAP * sizeof (struct dk_map);
3807 3805 rval = ddi_copyout(cl->cl_map, (void *)arg, size, flag);
3808 3806 if (rval != 0) {
3809 3807 rval = EFAULT;
3810 3808 }
3811 3809 break;
3812 3810 }
3813 3811 #else /* ! _MULTI_DATAMODEL */
3814 3812 size = NDKMAP * sizeof (struct dk_map);
3815 3813 rval = ddi_copyout(cl->cl_map, (void *)arg, size, flag);
3816 3814 if (rval != 0) {
3817 3815 rval = EFAULT;
3818 3816 }
3819 3817 #endif /* _MULTI_DATAMODEL */
3820 3818 return (rval);
3821 3819 }
3822 3820
3823 3821 /*
3824 3822 * Function: cmlb_dkio_set_partition
3825 3823 *
3826 3824 * Description: This routine is the driver entry point for handling user
3827 3825 * requests to set the partition table (DKIOCSAPART). The actual
3828 3826 * device partition is not updated.
3829 3827 *
3830 3828 * Arguments:
3831 3829 * arg - pointer to user provided dk_allmap structure used to set
3832 3830 * the controller's notion of the partition table.
3833 3831 * flag - this argument is a pass through to ddi_copyxxx()
3834 3832 * directly from the mode argument of ioctl().
3835 3833 *
3836 3834 * Return Code: 0
3837 3835 * EINVAL
3838 3836 * EFAULT
3839 3837 * ENXIO
3840 3838 * EIO
3841 3839 */
3842 3840 static int
3843 3841 cmlb_dkio_set_partition(struct cmlb_lun *cl, caddr_t arg, int flag)
3844 3842 {
3845 3843 struct dk_map dk_map[NDKMAP];
3846 3844 struct dk_map *lp;
3847 3845 int rval = 0;
3848 3846 int size;
↓ open down ↓ |
1103 lines elided |
↑ open up ↑ |
3849 3847 int i;
3850 3848 #if defined(_SUNOS_VTOC_16)
3851 3849 struct dkl_partition *vp;
3852 3850 #endif
3853 3851
3854 3852 /*
3855 3853 * Set the map for all logical partitions. We lock
3856 3854 * the priority just to make sure an interrupt doesn't
3857 3855 * come in while the map is half updated.
3858 3856 */
3859 - _NOTE(DATA_READABLE_WITHOUT_LOCK(cmlb_lun::cl_solaris_size))
3860 3857 mutex_enter(CMLB_MUTEX(cl));
3861 3858
3862 3859 if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
3863 3860 mutex_exit(CMLB_MUTEX(cl));
3864 3861 return (ENOTSUP);
3865 3862 }
3866 3863 mutex_exit(CMLB_MUTEX(cl));
3867 3864 if (cl->cl_solaris_size == 0) {
3868 3865 return (EIO);
3869 3866 }
3870 3867
3871 3868 #ifdef _MULTI_DATAMODEL
3872 3869 switch (ddi_model_convert_from(flag & FMODELS)) {
3873 3870 case DDI_MODEL_ILP32: {
3874 3871 struct dk_map32 dk_map32[NDKMAP];
3875 3872
3876 3873 size = NDKMAP * sizeof (struct dk_map32);
3877 3874 rval = ddi_copyin((void *)arg, dk_map32, size, flag);
3878 3875 if (rval != 0) {
3879 3876 return (EFAULT);
3880 3877 }
3881 3878 for (i = 0; i < NDKMAP; i++) {
3882 3879 dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno;
3883 3880 dk_map[i].dkl_nblk = dk_map32[i].dkl_nblk;
3884 3881 }
3885 3882 break;
3886 3883 }
3887 3884 case DDI_MODEL_NONE:
3888 3885 size = NDKMAP * sizeof (struct dk_map);
3889 3886 rval = ddi_copyin((void *)arg, dk_map, size, flag);
3890 3887 if (rval != 0) {
3891 3888 return (EFAULT);
3892 3889 }
3893 3890 break;
3894 3891 }
3895 3892 #else /* ! _MULTI_DATAMODEL */
3896 3893 size = NDKMAP * sizeof (struct dk_map);
3897 3894 rval = ddi_copyin((void *)arg, dk_map, size, flag);
3898 3895 if (rval != 0) {
3899 3896 return (EFAULT);
3900 3897 }
3901 3898 #endif /* _MULTI_DATAMODEL */
3902 3899
3903 3900 mutex_enter(CMLB_MUTEX(cl));
3904 3901 /* Note: The size used in this bcopy is set based upon the data model */
3905 3902 bcopy(dk_map, cl->cl_map, size);
3906 3903 #if defined(_SUNOS_VTOC_16)
3907 3904 vp = (struct dkl_partition *)&(cl->cl_vtoc);
3908 3905 #endif /* defined(_SUNOS_VTOC_16) */
3909 3906 for (i = 0; i < NDKMAP; i++) {
3910 3907 lp = &cl->cl_map[i];
3911 3908 cl->cl_offset[i] =
3912 3909 cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
3913 3910 #if defined(_SUNOS_VTOC_16)
3914 3911 vp->p_start = cl->cl_offset[i];
3915 3912 vp->p_size = lp->dkl_nblk;
3916 3913 vp++;
3917 3914 #endif /* defined(_SUNOS_VTOC_16) */
3918 3915 #if defined(__i386) || defined(__amd64)
3919 3916 cl->cl_offset[i] += cl->cl_solaris_offset;
3920 3917 #endif
3921 3918 }
3922 3919 mutex_exit(CMLB_MUTEX(cl));
3923 3920 return (rval);
3924 3921 }
3925 3922
3926 3923
3927 3924 /*
3928 3925 * Function: cmlb_dkio_get_vtoc
3929 3926 *
3930 3927 * Description: This routine is the driver entry point for handling user
3931 3928 * requests to get the current volume table of contents
3932 3929 * (DKIOCGVTOC).
3933 3930 *
3934 3931 * Arguments:
3935 3932 * arg pointer to user provided vtoc structure specifying
3936 3933 * the current vtoc.
3937 3934 *
3938 3935 * flag this argument is a pass through to ddi_copyxxx()
3939 3936 * directly from the mode argument of ioctl().
3940 3937 *
3941 3938 * tg_cookie cookie from target driver to be passed back to target
3942 3939 * driver when we call back to it through tg_ops.
3943 3940 *
3944 3941 * Return Code: 0
3945 3942 * EFAULT
3946 3943 * ENXIO
3947 3944 * EIO
3948 3945 */
3949 3946 static int
3950 3947 cmlb_dkio_get_vtoc(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
3951 3948 {
3952 3949 #if defined(_SUNOS_VTOC_8)
3953 3950 struct vtoc user_vtoc;
3954 3951 #endif /* defined(_SUNOS_VTOC_8) */
3955 3952 int rval = 0;
3956 3953
3957 3954 mutex_enter(CMLB_MUTEX(cl));
3958 3955 if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
3959 3956 mutex_exit(CMLB_MUTEX(cl));
3960 3957 return (EOVERFLOW);
3961 3958 }
3962 3959
3963 3960 rval = cmlb_validate_geometry(cl, B_TRUE, 0, tg_cookie);
3964 3961
3965 3962 #if defined(_SUNOS_VTOC_8)
3966 3963 if (rval == EINVAL &&
3967 3964 (cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8)) {
3968 3965 /*
3969 3966 * This is to return a default label even when we do not
3970 3967 * really assume a default label for the device.
3971 3968 * dad driver utilizes this.
3972 3969 */
3973 3970 if (cl->cl_blockcount <= CMLB_OLDVTOC_LIMIT) {
3974 3971 cmlb_setup_default_geometry(cl, tg_cookie);
3975 3972 rval = 0;
3976 3973 }
3977 3974 }
3978 3975 #endif
3979 3976 if (rval) {
3980 3977 mutex_exit(CMLB_MUTEX(cl));
3981 3978 return (rval);
3982 3979 }
3983 3980
3984 3981 #if defined(_SUNOS_VTOC_8)
3985 3982 cmlb_build_user_vtoc(cl, &user_vtoc);
3986 3983 mutex_exit(CMLB_MUTEX(cl));
3987 3984
3988 3985 #ifdef _MULTI_DATAMODEL
3989 3986 switch (ddi_model_convert_from(flag & FMODELS)) {
3990 3987 case DDI_MODEL_ILP32: {
3991 3988 struct vtoc32 user_vtoc32;
3992 3989
3993 3990 vtoctovtoc32(user_vtoc, user_vtoc32);
3994 3991 if (ddi_copyout(&user_vtoc32, (void *)arg,
3995 3992 sizeof (struct vtoc32), flag)) {
3996 3993 return (EFAULT);
3997 3994 }
3998 3995 break;
3999 3996 }
4000 3997
4001 3998 case DDI_MODEL_NONE:
4002 3999 if (ddi_copyout(&user_vtoc, (void *)arg,
4003 4000 sizeof (struct vtoc), flag)) {
4004 4001 return (EFAULT);
4005 4002 }
4006 4003 break;
4007 4004 }
4008 4005 #else /* ! _MULTI_DATAMODEL */
4009 4006 if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) {
4010 4007 return (EFAULT);
4011 4008 }
4012 4009 #endif /* _MULTI_DATAMODEL */
4013 4010
4014 4011 #elif defined(_SUNOS_VTOC_16)
4015 4012 mutex_exit(CMLB_MUTEX(cl));
4016 4013
4017 4014 #ifdef _MULTI_DATAMODEL
4018 4015 /*
4019 4016 * The cl_vtoc structure is a "struct dk_vtoc" which is always
4020 4017 * 32-bit to maintain compatibility with existing on-disk
4021 4018 * structures. Thus, we need to convert the structure when copying
4022 4019 * it out to a datamodel-dependent "struct vtoc" in a 64-bit
4023 4020 * program. If the target is a 32-bit program, then no conversion
4024 4021 * is necessary.
4025 4022 */
4026 4023 /* LINTED: logical expression always true: op "||" */
4027 4024 ASSERT(sizeof (cl->cl_vtoc) == sizeof (struct vtoc32));
4028 4025 switch (ddi_model_convert_from(flag & FMODELS)) {
4029 4026 case DDI_MODEL_ILP32:
4030 4027 if (ddi_copyout(&(cl->cl_vtoc), (void *)arg,
4031 4028 sizeof (cl->cl_vtoc), flag)) {
4032 4029 return (EFAULT);
4033 4030 }
4034 4031 break;
4035 4032
4036 4033 case DDI_MODEL_NONE: {
4037 4034 struct vtoc user_vtoc;
4038 4035
4039 4036 vtoc32tovtoc(cl->cl_vtoc, user_vtoc);
4040 4037 if (ddi_copyout(&user_vtoc, (void *)arg,
4041 4038 sizeof (struct vtoc), flag)) {
4042 4039 return (EFAULT);
4043 4040 }
4044 4041 break;
4045 4042 }
4046 4043 }
4047 4044 #else /* ! _MULTI_DATAMODEL */
4048 4045 if (ddi_copyout(&(cl->cl_vtoc), (void *)arg, sizeof (cl->cl_vtoc),
4049 4046 flag)) {
4050 4047 return (EFAULT);
4051 4048 }
4052 4049 #endif /* _MULTI_DATAMODEL */
4053 4050 #else
4054 4051 #error "No VTOC format defined."
4055 4052 #endif
4056 4053
4057 4054 return (rval);
4058 4055 }
4059 4056
4060 4057
4061 4058 /*
4062 4059 * Function: cmlb_dkio_get_extvtoc
4063 4060 */
4064 4061 static int
4065 4062 cmlb_dkio_get_extvtoc(struct cmlb_lun *cl, caddr_t arg, int flag,
4066 4063 void *tg_cookie)
4067 4064 {
4068 4065 struct extvtoc ext_vtoc;
4069 4066 #if defined(_SUNOS_VTOC_8)
4070 4067 struct vtoc user_vtoc;
4071 4068 #endif /* defined(_SUNOS_VTOC_8) */
4072 4069 int rval = 0;
4073 4070
4074 4071 bzero(&ext_vtoc, sizeof (struct extvtoc));
4075 4072 mutex_enter(CMLB_MUTEX(cl));
4076 4073 rval = cmlb_validate_geometry(cl, B_TRUE, 0, tg_cookie);
4077 4074
4078 4075 #if defined(_SUNOS_VTOC_8)
4079 4076 if (rval == EINVAL &&
4080 4077 (cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8)) {
4081 4078 /*
4082 4079 * This is to return a default label even when we do not
4083 4080 * really assume a default label for the device.
4084 4081 * dad driver utilizes this.
4085 4082 */
4086 4083 if (cl->cl_blockcount <= CMLB_OLDVTOC_LIMIT) {
4087 4084 cmlb_setup_default_geometry(cl, tg_cookie);
4088 4085 rval = 0;
4089 4086 }
4090 4087 }
4091 4088 #endif
4092 4089 if (rval) {
4093 4090 mutex_exit(CMLB_MUTEX(cl));
4094 4091 return (rval);
4095 4092 }
4096 4093
4097 4094 #if defined(_SUNOS_VTOC_8)
4098 4095 cmlb_build_user_vtoc(cl, &user_vtoc);
4099 4096 mutex_exit(CMLB_MUTEX(cl));
4100 4097
4101 4098 /*
4102 4099 * Checking callers data model does not make much sense here
4103 4100 * since extvtoc will always be equivalent to 64bit vtoc.
4104 4101 * What is important is whether the kernel is in 32 or 64 bit
4105 4102 */
4106 4103
4107 4104 #ifdef _LP64
4108 4105 if (ddi_copyout(&user_vtoc, (void *)arg,
4109 4106 sizeof (struct extvtoc), flag)) {
4110 4107 return (EFAULT);
4111 4108 }
4112 4109 #else
4113 4110 vtoc32tovtoc(user_vtoc, ext_vtoc);
4114 4111 if (ddi_copyout(&ext_vtoc, (void *)arg,
4115 4112 sizeof (struct extvtoc), flag)) {
4116 4113 return (EFAULT);
4117 4114 }
4118 4115 #endif
4119 4116
4120 4117 #elif defined(_SUNOS_VTOC_16)
4121 4118 /*
4122 4119 * The cl_vtoc structure is a "struct dk_vtoc" which is always
4123 4120 * 32-bit to maintain compatibility with existing on-disk
4124 4121 * structures. Thus, we need to convert the structure when copying
4125 4122 * it out to extvtoc
4126 4123 */
4127 4124 vtoc32tovtoc(cl->cl_vtoc, ext_vtoc);
4128 4125 mutex_exit(CMLB_MUTEX(cl));
4129 4126
4130 4127 if (ddi_copyout(&ext_vtoc, (void *)arg, sizeof (struct extvtoc), flag))
4131 4128 return (EFAULT);
4132 4129 #else
4133 4130 #error "No VTOC format defined."
4134 4131 #endif
4135 4132
4136 4133 return (rval);
4137 4134 }
4138 4135
4139 4136 /*
4140 4137 * This routine implements the DKIOCGETEFI ioctl. This ioctl is currently
4141 4138 * used to read the GPT Partition Table Header (primary/backup), the GUID
4142 4139 * partition Entry Array (primary/backup), and the MBR.
4143 4140 */
4144 4141 static int
4145 4142 cmlb_dkio_get_efi(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
4146 4143 {
4147 4144 dk_efi_t user_efi;
4148 4145 int rval = 0;
4149 4146 void *buffer;
4150 4147 diskaddr_t tgt_lba;
4151 4148
4152 4149 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
4153 4150 return (EFAULT);
4154 4151
4155 4152 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
4156 4153
4157 4154 if (user_efi.dki_length == 0 ||
4158 4155 user_efi.dki_length > cmlb_tg_max_efi_xfer)
4159 4156 return (EINVAL);
4160 4157
4161 4158 tgt_lba = user_efi.dki_lba;
4162 4159
4163 4160 mutex_enter(CMLB_MUTEX(cl));
4164 4161 if ((cmlb_check_update_blockcount(cl, tg_cookie) != 0) ||
4165 4162 (cl->cl_tgt_blocksize == 0) ||
4166 4163 (user_efi.dki_length % cl->cl_sys_blocksize)) {
4167 4164 mutex_exit(CMLB_MUTEX(cl));
4168 4165 return (EINVAL);
4169 4166 }
4170 4167 if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize)
4171 4168 tgt_lba = tgt_lba * cl->cl_tgt_blocksize /
4172 4169 cl->cl_sys_blocksize;
4173 4170 mutex_exit(CMLB_MUTEX(cl));
4174 4171
4175 4172 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
4176 4173 rval = DK_TG_READ(cl, buffer, tgt_lba, user_efi.dki_length, tg_cookie);
4177 4174 if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data,
4178 4175 user_efi.dki_length, flag) != 0)
4179 4176 rval = EFAULT;
4180 4177
4181 4178 kmem_free(buffer, user_efi.dki_length);
4182 4179 return (rval);
4183 4180 }
4184 4181
4185 4182 #if defined(_SUNOS_VTOC_8)
4186 4183 /*
4187 4184 * Function: cmlb_build_user_vtoc
4188 4185 *
4189 4186 * Description: This routine populates a pass by reference variable with the
4190 4187 * current volume table of contents.
4191 4188 *
4192 4189 * Arguments: cl - driver soft state (unit) structure
4193 4190 * user_vtoc - pointer to vtoc structure to be populated
4194 4191 */
4195 4192 static void
4196 4193 cmlb_build_user_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc)
4197 4194 {
4198 4195 struct dk_map2 *lpart;
4199 4196 struct dk_map *lmap;
4200 4197 struct partition *vpart;
4201 4198 uint32_t nblks;
4202 4199 int i;
4203 4200
4204 4201 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
4205 4202
4206 4203 /*
4207 4204 * Return vtoc structure fields in the provided VTOC area, addressed
4208 4205 * by *vtoc.
4209 4206 */
4210 4207 bzero(user_vtoc, sizeof (struct vtoc));
4211 4208 user_vtoc->v_bootinfo[0] = cl->cl_vtoc.v_bootinfo[0];
4212 4209 user_vtoc->v_bootinfo[1] = cl->cl_vtoc.v_bootinfo[1];
4213 4210 user_vtoc->v_bootinfo[2] = cl->cl_vtoc.v_bootinfo[2];
4214 4211 user_vtoc->v_sanity = VTOC_SANE;
4215 4212 user_vtoc->v_version = cl->cl_vtoc.v_version;
4216 4213 bcopy(cl->cl_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL);
4217 4214 user_vtoc->v_sectorsz = cl->cl_sys_blocksize;
4218 4215 user_vtoc->v_nparts = cl->cl_vtoc.v_nparts;
4219 4216
4220 4217 for (i = 0; i < 10; i++)
4221 4218 user_vtoc->v_reserved[i] = cl->cl_vtoc.v_reserved[i];
4222 4219
4223 4220 /*
4224 4221 * Convert partitioning information.
4225 4222 *
4226 4223 * Note the conversion from starting cylinder number
4227 4224 * to starting sector number.
4228 4225 */
4229 4226 lmap = cl->cl_map;
4230 4227 lpart = (struct dk_map2 *)cl->cl_vtoc.v_part;
4231 4228 vpart = user_vtoc->v_part;
4232 4229
4233 4230 nblks = cl->cl_g.dkg_nsect * cl->cl_g.dkg_nhead;
4234 4231
4235 4232 for (i = 0; i < V_NUMPAR; i++) {
4236 4233 vpart->p_tag = lpart->p_tag;
4237 4234 vpart->p_flag = lpart->p_flag;
4238 4235 vpart->p_start = lmap->dkl_cylno * nblks;
4239 4236 vpart->p_size = lmap->dkl_nblk;
4240 4237 lmap++;
4241 4238 lpart++;
4242 4239 vpart++;
4243 4240
4244 4241 /* (4364927) */
4245 4242 user_vtoc->timestamp[i] = (time_t)cl->cl_vtoc.v_timestamp[i];
4246 4243 }
4247 4244
4248 4245 bcopy(cl->cl_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII);
4249 4246 }
4250 4247 #endif
4251 4248
4252 4249 static int
4253 4250 cmlb_dkio_partition(struct cmlb_lun *cl, caddr_t arg, int flag,
4254 4251 void *tg_cookie)
4255 4252 {
4256 4253 struct partition64 p64;
4257 4254 int rval = 0;
4258 4255 uint_t nparts;
4259 4256 efi_gpe_t *partitions;
4260 4257 efi_gpt_t *buffer;
4261 4258 diskaddr_t gpe_lba;
4262 4259 int n_gpe_per_blk = 0;
4263 4260
4264 4261 if (ddi_copyin((const void *)arg, &p64,
4265 4262 sizeof (struct partition64), flag)) {
4266 4263 return (EFAULT);
4267 4264 }
4268 4265
4269 4266 buffer = kmem_alloc(cl->cl_sys_blocksize, KM_SLEEP);
4270 4267 rval = DK_TG_READ(cl, buffer, 1, cl->cl_sys_blocksize, tg_cookie);
4271 4268 if (rval != 0)
4272 4269 goto done_error;
4273 4270
4274 4271 cmlb_swap_efi_gpt(buffer);
4275 4272
4276 4273 if ((rval = cmlb_validate_efi(buffer)) != 0)
4277 4274 goto done_error;
4278 4275
4279 4276 nparts = buffer->efi_gpt_NumberOfPartitionEntries;
4280 4277 gpe_lba = buffer->efi_gpt_PartitionEntryLBA;
4281 4278 if (p64.p_partno >= nparts) {
4282 4279 /* couldn't find it */
4283 4280 rval = ESRCH;
4284 4281 goto done_error;
4285 4282 }
4286 4283 /*
4287 4284 * Read the block that contains the requested GPE.
4288 4285 */
4289 4286 n_gpe_per_blk = cl->cl_sys_blocksize / sizeof (efi_gpe_t);
4290 4287 gpe_lba += p64.p_partno / n_gpe_per_blk;
4291 4288 rval = DK_TG_READ(cl, buffer, gpe_lba, cl->cl_sys_blocksize, tg_cookie);
4292 4289
4293 4290 if (rval) {
4294 4291 goto done_error;
4295 4292 }
4296 4293 partitions = (efi_gpe_t *)buffer;
4297 4294 partitions += p64.p_partno % n_gpe_per_blk;
4298 4295
4299 4296 /* Byte swap only the requested GPE */
4300 4297 cmlb_swap_efi_gpe(1, partitions);
4301 4298
4302 4299 bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type,
4303 4300 sizeof (struct uuid));
4304 4301 p64.p_start = partitions->efi_gpe_StartingLBA;
4305 4302 p64.p_size = partitions->efi_gpe_EndingLBA -
4306 4303 p64.p_start + 1;
4307 4304
4308 4305 if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag))
4309 4306 rval = EFAULT;
4310 4307
4311 4308 done_error:
4312 4309 kmem_free(buffer, cl->cl_sys_blocksize);
4313 4310 return (rval);
4314 4311 }
4315 4312
4316 4313
4317 4314 /*
4318 4315 * Function: cmlb_dkio_set_vtoc
4319 4316 *
4320 4317 * Description: This routine is the driver entry point for handling user
4321 4318 * requests to set the current volume table of contents
4322 4319 * (DKIOCSVTOC).
4323 4320 *
4324 4321 * Arguments:
4325 4322 * dev the device number
4326 4323 * arg pointer to user provided vtoc structure used to set the
4327 4324 * current vtoc.
4328 4325 *
4329 4326 * flag this argument is a pass through to ddi_copyxxx()
4330 4327 * directly from the mode argument of ioctl().
4331 4328 *
4332 4329 * tg_cookie cookie from target driver to be passed back to target
4333 4330 * driver when we call back to it through tg_ops.
4334 4331 *
4335 4332 * Return Code: 0
4336 4333 * EFAULT
4337 4334 * ENXIO
4338 4335 * EINVAL
4339 4336 * ENOTSUP
4340 4337 */
4341 4338 static int
4342 4339 cmlb_dkio_set_vtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag,
4343 4340 void *tg_cookie)
4344 4341 {
4345 4342 struct vtoc user_vtoc;
4346 4343 int shift, rval = 0;
4347 4344 boolean_t internal;
4348 4345
4349 4346 internal = VOID2BOOLEAN(
4350 4347 (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
4351 4348
4352 4349 if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE)
4353 4350 shift = CMLBUNIT_FORCE_P0_SHIFT;
4354 4351 else
4355 4352 shift = CMLBUNIT_SHIFT;
4356 4353
4357 4354 #ifdef _MULTI_DATAMODEL
4358 4355 switch (ddi_model_convert_from(flag & FMODELS)) {
4359 4356 case DDI_MODEL_ILP32: {
4360 4357 struct vtoc32 user_vtoc32;
4361 4358
4362 4359 if (ddi_copyin((const void *)arg, &user_vtoc32,
4363 4360 sizeof (struct vtoc32), flag)) {
4364 4361 return (EFAULT);
4365 4362 }
4366 4363 vtoc32tovtoc(user_vtoc32, user_vtoc);
4367 4364 break;
4368 4365 }
4369 4366
4370 4367 case DDI_MODEL_NONE:
4371 4368 if (ddi_copyin((const void *)arg, &user_vtoc,
4372 4369 sizeof (struct vtoc), flag)) {
4373 4370 return (EFAULT);
4374 4371 }
4375 4372 break;
4376 4373 }
4377 4374 #else /* ! _MULTI_DATAMODEL */
4378 4375 if (ddi_copyin((const void *)arg, &user_vtoc,
4379 4376 sizeof (struct vtoc), flag)) {
4380 4377 return (EFAULT);
4381 4378 }
4382 4379 #endif /* _MULTI_DATAMODEL */
4383 4380
4384 4381 mutex_enter(CMLB_MUTEX(cl));
4385 4382
4386 4383 if (cl->cl_blockcount > CMLB_OLDVTOC_LIMIT) {
4387 4384 mutex_exit(CMLB_MUTEX(cl));
4388 4385 return (EOVERFLOW);
4389 4386 }
4390 4387
4391 4388 #if defined(__i386) || defined(__amd64)
4392 4389 if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize) {
4393 4390 mutex_exit(CMLB_MUTEX(cl));
4394 4391 return (EINVAL);
4395 4392 }
4396 4393 #endif
4397 4394
4398 4395 if (cl->cl_g.dkg_ncyl == 0) {
4399 4396 mutex_exit(CMLB_MUTEX(cl));
4400 4397 return (EINVAL);
4401 4398 }
4402 4399
4403 4400 mutex_exit(CMLB_MUTEX(cl));
4404 4401 cmlb_clear_efi(cl, tg_cookie);
4405 4402 ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd");
4406 4403 ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd,raw");
4407 4404
4408 4405 /*
4409 4406 * cmlb_dkio_set_vtoc creates duplicate minor nodes when
4410 4407 * relabeling an SMI disk. To avoid that we remove them
4411 4408 * before creating.
4412 4409 * It should be OK to remove a non-existed minor node.
4413 4410 */
4414 4411 ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
4415 4412 ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
4416 4413
4417 4414 (void) cmlb_create_minor(CMLB_DEVINFO(cl), "h",
4418 4415 S_IFBLK, (CMLBUNIT(dev, shift) << shift) | WD_NODE,
4419 4416 cl->cl_node_type, NULL, internal);
4420 4417 (void) cmlb_create_minor(CMLB_DEVINFO(cl), "h,raw",
4421 4418 S_IFCHR, (CMLBUNIT(dev, shift) << shift) | WD_NODE,
4422 4419 cl->cl_node_type, NULL, internal);
4423 4420 mutex_enter(CMLB_MUTEX(cl));
4424 4421
4425 4422 if ((rval = cmlb_build_label_vtoc(cl, &user_vtoc)) == 0) {
4426 4423 if ((rval = cmlb_write_label(cl, tg_cookie)) == 0) {
4427 4424 if (cmlb_validate_geometry(cl,
4428 4425 B_TRUE, 0, tg_cookie) != 0) {
4429 4426 cmlb_dbg(CMLB_ERROR, cl,
4430 4427 "cmlb_dkio_set_vtoc: "
4431 4428 "Failed validate geometry\n");
4432 4429 }
4433 4430 cl->cl_msglog_flag |= CMLB_ALLOW_2TB_WARN;
4434 4431 }
4435 4432 }
4436 4433 mutex_exit(CMLB_MUTEX(cl));
4437 4434 return (rval);
4438 4435 }
4439 4436
4440 4437 /*
4441 4438 * Function: cmlb_dkio_set_extvtoc
4442 4439 */
4443 4440 static int
4444 4441 cmlb_dkio_set_extvtoc(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag,
4445 4442 void *tg_cookie)
4446 4443 {
4447 4444 int shift, rval = 0;
4448 4445 struct vtoc user_vtoc;
4449 4446 boolean_t internal;
4450 4447
4451 4448 if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE)
4452 4449 shift = CMLBUNIT_FORCE_P0_SHIFT;
4453 4450 else
4454 4451 shift = CMLBUNIT_SHIFT;
4455 4452
4456 4453 /*
4457 4454 * Checking callers data model does not make much sense here
4458 4455 * since extvtoc will always be equivalent to 64bit vtoc.
4459 4456 * What is important is whether the kernel is in 32 or 64 bit
4460 4457 */
4461 4458
4462 4459 #ifdef _LP64
4463 4460 if (ddi_copyin((const void *)arg, &user_vtoc,
4464 4461 sizeof (struct extvtoc), flag)) {
4465 4462 return (EFAULT);
4466 4463 }
4467 4464 #else
4468 4465 struct extvtoc user_extvtoc;
4469 4466 if (ddi_copyin((const void *)arg, &user_extvtoc,
4470 4467 sizeof (struct extvtoc), flag)) {
4471 4468 return (EFAULT);
4472 4469 }
4473 4470
4474 4471 vtoctovtoc32(user_extvtoc, user_vtoc);
4475 4472 #endif
4476 4473
4477 4474 internal = VOID2BOOLEAN(
4478 4475 (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
4479 4476 mutex_enter(CMLB_MUTEX(cl));
4480 4477 #if defined(__i386) || defined(__amd64)
4481 4478 if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize) {
4482 4479 mutex_exit(CMLB_MUTEX(cl));
4483 4480 return (EINVAL);
4484 4481 }
4485 4482 #endif
4486 4483
4487 4484 if (cl->cl_g.dkg_ncyl == 0) {
4488 4485 mutex_exit(CMLB_MUTEX(cl));
4489 4486 return (EINVAL);
4490 4487 }
4491 4488
4492 4489 mutex_exit(CMLB_MUTEX(cl));
4493 4490 cmlb_clear_efi(cl, tg_cookie);
4494 4491 ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd");
4495 4492 ddi_remove_minor_node(CMLB_DEVINFO(cl), "wd,raw");
4496 4493 /*
4497 4494 * cmlb_dkio_set_extvtoc creates duplicate minor nodes when
4498 4495 * relabeling an SMI disk. To avoid that we remove them
4499 4496 * before creating.
4500 4497 * It should be OK to remove a non-existed minor node.
4501 4498 */
4502 4499 ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
4503 4500 ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
4504 4501
4505 4502 (void) cmlb_create_minor(CMLB_DEVINFO(cl), "h",
4506 4503 S_IFBLK, (CMLBUNIT(dev, shift) << shift) | WD_NODE,
4507 4504 cl->cl_node_type, NULL, internal);
4508 4505 (void) cmlb_create_minor(CMLB_DEVINFO(cl), "h,raw",
4509 4506 S_IFCHR, (CMLBUNIT(dev, shift) << shift) | WD_NODE,
4510 4507 cl->cl_node_type, NULL, internal);
4511 4508
4512 4509 mutex_enter(CMLB_MUTEX(cl));
4513 4510
4514 4511 if ((rval = cmlb_build_label_vtoc(cl, &user_vtoc)) == 0) {
4515 4512 if ((rval = cmlb_write_label(cl, tg_cookie)) == 0) {
4516 4513 if (cmlb_validate_geometry(cl,
4517 4514 B_TRUE, 0, tg_cookie) != 0) {
4518 4515 cmlb_dbg(CMLB_ERROR, cl,
4519 4516 "cmlb_dkio_set_vtoc: "
4520 4517 "Failed validate geometry\n");
4521 4518 }
4522 4519 }
4523 4520 }
4524 4521 mutex_exit(CMLB_MUTEX(cl));
4525 4522 return (rval);
4526 4523 }
4527 4524
4528 4525 /*
4529 4526 * Function: cmlb_build_label_vtoc
4530 4527 *
4531 4528 * Description: This routine updates the driver soft state current volume table
4532 4529 * of contents based on a user specified vtoc.
4533 4530 *
4534 4531 * Arguments: cl - driver soft state (unit) structure
4535 4532 * user_vtoc - pointer to vtoc structure specifying vtoc to be used
4536 4533 * to update the driver soft state.
4537 4534 *
4538 4535 * Return Code: 0
4539 4536 * EINVAL
4540 4537 */
4541 4538 static int
4542 4539 cmlb_build_label_vtoc(struct cmlb_lun *cl, struct vtoc *user_vtoc)
4543 4540 {
4544 4541 struct dk_map *lmap;
4545 4542 struct partition *vpart;
4546 4543 uint_t nblks;
4547 4544 #if defined(_SUNOS_VTOC_8)
4548 4545 int ncyl;
4549 4546 struct dk_map2 *lpart;
4550 4547 #endif /* defined(_SUNOS_VTOC_8) */
4551 4548 int i;
4552 4549
4553 4550 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
4554 4551
4555 4552 /* Sanity-check the vtoc */
4556 4553 if (user_vtoc->v_sanity != VTOC_SANE ||
4557 4554 user_vtoc->v_sectorsz != cl->cl_sys_blocksize ||
4558 4555 user_vtoc->v_nparts != V_NUMPAR) {
4559 4556 cmlb_dbg(CMLB_INFO, cl,
4560 4557 "cmlb_build_label_vtoc: vtoc not valid\n");
4561 4558 return (EINVAL);
4562 4559 }
4563 4560
4564 4561 nblks = cl->cl_g.dkg_nsect * cl->cl_g.dkg_nhead;
4565 4562 if (nblks == 0) {
4566 4563 cmlb_dbg(CMLB_INFO, cl,
4567 4564 "cmlb_build_label_vtoc: geom nblks is 0\n");
4568 4565 return (EINVAL);
4569 4566 }
4570 4567
4571 4568 #if defined(_SUNOS_VTOC_8)
4572 4569 vpart = user_vtoc->v_part;
4573 4570 for (i = 0; i < V_NUMPAR; i++) {
4574 4571 if (((unsigned)vpart->p_start % nblks) != 0) {
4575 4572 cmlb_dbg(CMLB_INFO, cl,
4576 4573 "cmlb_build_label_vtoc: p_start not multiply of"
4577 4574 "nblks part %d p_start %d nblks %d\n", i,
4578 4575 vpart->p_start, nblks);
4579 4576 return (EINVAL);
4580 4577 }
4581 4578 ncyl = (unsigned)vpart->p_start / nblks;
4582 4579 ncyl += (unsigned)vpart->p_size / nblks;
4583 4580 if (((unsigned)vpart->p_size % nblks) != 0) {
4584 4581 ncyl++;
4585 4582 }
4586 4583 if (ncyl > (int)cl->cl_g.dkg_ncyl) {
4587 4584 cmlb_dbg(CMLB_INFO, cl,
4588 4585 "cmlb_build_label_vtoc: ncyl %d > dkg_ncyl %d"
4589 4586 "p_size %ld p_start %ld nblks %d part number %d"
4590 4587 "tag %d\n",
4591 4588 ncyl, cl->cl_g.dkg_ncyl, vpart->p_size,
4592 4589 vpart->p_start, nblks,
4593 4590 i, vpart->p_tag);
4594 4591
4595 4592 return (EINVAL);
4596 4593 }
4597 4594 vpart++;
4598 4595 }
4599 4596 #endif /* defined(_SUNOS_VTOC_8) */
4600 4597
4601 4598 /* Put appropriate vtoc structure fields into the disk label */
4602 4599 #if defined(_SUNOS_VTOC_16)
4603 4600 /*
4604 4601 * The vtoc is always a 32bit data structure to maintain the
4605 4602 * on-disk format. Convert "in place" instead of doing bcopy.
4606 4603 */
4607 4604 vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(cl->cl_vtoc))));
4608 4605
4609 4606 /*
4610 4607 * in the 16-slice vtoc, starting sectors are expressed in
4611 4608 * numbers *relative* to the start of the Solaris fdisk partition.
4612 4609 */
4613 4610 lmap = cl->cl_map;
4614 4611 vpart = user_vtoc->v_part;
4615 4612
4616 4613 for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) {
4617 4614 lmap->dkl_cylno = (unsigned)vpart->p_start / nblks;
4618 4615 lmap->dkl_nblk = (unsigned)vpart->p_size;
4619 4616 }
4620 4617
4621 4618 #elif defined(_SUNOS_VTOC_8)
4622 4619
4623 4620 cl->cl_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0];
4624 4621 cl->cl_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1];
4625 4622 cl->cl_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2];
4626 4623
4627 4624 cl->cl_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity;
4628 4625 cl->cl_vtoc.v_version = (uint32_t)user_vtoc->v_version;
4629 4626
4630 4627 bcopy(user_vtoc->v_volume, cl->cl_vtoc.v_volume, LEN_DKL_VVOL);
4631 4628
4632 4629 cl->cl_vtoc.v_nparts = user_vtoc->v_nparts;
4633 4630
4634 4631 for (i = 0; i < 10; i++)
4635 4632 cl->cl_vtoc.v_reserved[i] = user_vtoc->v_reserved[i];
4636 4633
4637 4634 /*
4638 4635 * Note the conversion from starting sector number
4639 4636 * to starting cylinder number.
4640 4637 * Return error if division results in a remainder.
4641 4638 */
4642 4639 lmap = cl->cl_map;
4643 4640 lpart = cl->cl_vtoc.v_part;
4644 4641 vpart = user_vtoc->v_part;
4645 4642
4646 4643 for (i = 0; i < (int)user_vtoc->v_nparts; i++) {
4647 4644 lpart->p_tag = vpart->p_tag;
4648 4645 lpart->p_flag = vpart->p_flag;
4649 4646 lmap->dkl_cylno = (unsigned)vpart->p_start / nblks;
4650 4647 lmap->dkl_nblk = (unsigned)vpart->p_size;
4651 4648
4652 4649 lmap++;
4653 4650 lpart++;
4654 4651 vpart++;
4655 4652
4656 4653 /* (4387723) */
4657 4654 #ifdef _LP64
4658 4655 if (user_vtoc->timestamp[i] > TIME32_MAX) {
4659 4656 cl->cl_vtoc.v_timestamp[i] = TIME32_MAX;
4660 4657 } else {
4661 4658 cl->cl_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
4662 4659 }
4663 4660 #else
4664 4661 cl->cl_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
4665 4662 #endif
4666 4663 }
4667 4664
4668 4665 bcopy(user_vtoc->v_asciilabel, cl->cl_asciilabel, LEN_DKL_ASCII);
4669 4666 #else
4670 4667 #error "No VTOC format defined."
4671 4668 #endif
4672 4669 return (0);
4673 4670 }
4674 4671
4675 4672 /*
4676 4673 * Function: cmlb_clear_efi
4677 4674 *
4678 4675 * Description: This routine clears all EFI labels.
4679 4676 *
4680 4677 * Arguments:
4681 4678 * cl driver soft state (unit) structure
4682 4679 *
4683 4680 * tg_cookie cookie from target driver to be passed back to target
4684 4681 * driver when we call back to it through tg_ops.
4685 4682 * Return Code: void
4686 4683 */
4687 4684 static void
4688 4685 cmlb_clear_efi(struct cmlb_lun *cl, void *tg_cookie)
4689 4686 {
4690 4687 efi_gpt_t *gpt;
4691 4688 diskaddr_t cap;
4692 4689 int rval;
4693 4690
4694 4691 ASSERT(!mutex_owned(CMLB_MUTEX(cl)));
4695 4692
4696 4693 mutex_enter(CMLB_MUTEX(cl));
4697 4694 cl->cl_reserved = -1;
4698 4695 mutex_exit(CMLB_MUTEX(cl));
4699 4696
4700 4697 gpt = kmem_alloc(cl->cl_sys_blocksize, KM_SLEEP);
4701 4698
4702 4699 if (DK_TG_READ(cl, gpt, 1, cl->cl_sys_blocksize, tg_cookie) != 0) {
4703 4700 goto done;
4704 4701 }
4705 4702
4706 4703 cmlb_swap_efi_gpt(gpt);
4707 4704 rval = cmlb_validate_efi(gpt);
4708 4705 if (rval == 0) {
4709 4706 /* clear primary */
4710 4707 bzero(gpt, sizeof (efi_gpt_t));
4711 4708 if (rval = DK_TG_WRITE(cl, gpt, 1, cl->cl_sys_blocksize,
4712 4709 tg_cookie)) {
4713 4710 cmlb_dbg(CMLB_INFO, cl,
4714 4711 "cmlb_clear_efi: clear primary label failed\n");
4715 4712 }
4716 4713 }
4717 4714 /* the backup */
4718 4715 rval = DK_TG_GETCAP(cl, &cap, tg_cookie);
4719 4716 if (rval) {
4720 4717 goto done;
4721 4718 }
4722 4719
4723 4720 if ((rval = DK_TG_READ(cl, gpt, cap - 1, cl->cl_sys_blocksize,
4724 4721 tg_cookie)) != 0) {
4725 4722 goto done;
4726 4723 }
4727 4724 cmlb_swap_efi_gpt(gpt);
4728 4725 rval = cmlb_validate_efi(gpt);
4729 4726 if (rval == 0) {
4730 4727 /* clear backup */
4731 4728 cmlb_dbg(CMLB_TRACE, cl,
4732 4729 "cmlb_clear_efi clear backup@%lu\n", cap - 1);
4733 4730 bzero(gpt, sizeof (efi_gpt_t));
4734 4731 if ((rval = DK_TG_WRITE(cl, gpt, cap - 1, cl->cl_sys_blocksize,
4735 4732 tg_cookie))) {
4736 4733 cmlb_dbg(CMLB_INFO, cl,
4737 4734 "cmlb_clear_efi: clear backup label failed\n");
4738 4735 }
4739 4736 } else {
4740 4737 /*
4741 4738 * Refer to comments related to off-by-1 at the
4742 4739 * header of this file
4743 4740 */
4744 4741 if ((rval = DK_TG_READ(cl, gpt, cap - 2,
4745 4742 cl->cl_sys_blocksize, tg_cookie)) != 0) {
4746 4743 goto done;
4747 4744 }
4748 4745 cmlb_swap_efi_gpt(gpt);
4749 4746 rval = cmlb_validate_efi(gpt);
4750 4747 if (rval == 0) {
4751 4748 /* clear legacy backup EFI label */
4752 4749 cmlb_dbg(CMLB_TRACE, cl,
4753 4750 "cmlb_clear_efi clear legacy backup@%lu\n",
4754 4751 cap - 2);
4755 4752 bzero(gpt, sizeof (efi_gpt_t));
4756 4753 if ((rval = DK_TG_WRITE(cl, gpt, cap - 2,
4757 4754 cl->cl_sys_blocksize, tg_cookie))) {
4758 4755 cmlb_dbg(CMLB_INFO, cl,
4759 4756 "cmlb_clear_efi: clear legacy backup label "
4760 4757 "failed\n");
4761 4758 }
4762 4759 }
4763 4760 }
4764 4761
4765 4762 done:
4766 4763 kmem_free(gpt, cl->cl_sys_blocksize);
4767 4764 }
4768 4765
4769 4766 /*
4770 4767 * Function: cmlb_set_vtoc
4771 4768 *
4772 4769 * Description: This routine writes data to the appropriate positions
4773 4770 *
4774 4771 * Arguments:
4775 4772 * cl driver soft state (unit) structure
4776 4773 *
4777 4774 * dkl the data to be written
4778 4775 *
4779 4776 * tg_cookie cookie from target driver to be passed back to target
4780 4777 * driver when we call back to it through tg_ops.
4781 4778 *
4782 4779 * Return: void
4783 4780 */
4784 4781 static int
4785 4782 cmlb_set_vtoc(struct cmlb_lun *cl, struct dk_label *dkl, void *tg_cookie)
4786 4783 {
4787 4784 uint_t label_addr;
4788 4785 int sec;
4789 4786 diskaddr_t blk;
4790 4787 int head;
4791 4788 int cyl;
4792 4789 int rval;
4793 4790
4794 4791 #if defined(__i386) || defined(__amd64)
4795 4792 label_addr = cl->cl_solaris_offset + DK_LABEL_LOC;
4796 4793 #else
4797 4794 /* Write the primary label at block 0 of the solaris partition. */
4798 4795 label_addr = 0;
4799 4796 #endif
4800 4797
4801 4798 rval = DK_TG_WRITE(cl, dkl, label_addr, cl->cl_sys_blocksize,
4802 4799 tg_cookie);
4803 4800
4804 4801 if (rval != 0) {
4805 4802 return (rval);
4806 4803 }
4807 4804
4808 4805 /*
4809 4806 * Calculate where the backup labels go. They are always on
4810 4807 * the last alternate cylinder, but some older drives put them
4811 4808 * on head 2 instead of the last head. They are always on the
4812 4809 * first 5 odd sectors of the appropriate track.
4813 4810 *
4814 4811 * We have no choice at this point, but to believe that the
4815 4812 * disk label is valid. Use the geometry of the disk
4816 4813 * as described in the label.
4817 4814 */
4818 4815 cyl = dkl->dkl_ncyl + dkl->dkl_acyl - 1;
4819 4816 head = dkl->dkl_nhead - 1;
4820 4817
4821 4818 /*
4822 4819 * Write and verify the backup labels. Make sure we don't try to
4823 4820 * write past the last cylinder.
4824 4821 */
4825 4822 for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) {
4826 4823 blk = (diskaddr_t)(
4827 4824 (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) +
4828 4825 (head * dkl->dkl_nsect) + sec);
4829 4826 #if defined(__i386) || defined(__amd64)
4830 4827 blk += cl->cl_solaris_offset;
4831 4828 #endif
4832 4829 rval = DK_TG_WRITE(cl, dkl, blk, cl->cl_sys_blocksize,
4833 4830 tg_cookie);
4834 4831 cmlb_dbg(CMLB_INFO, cl,
4835 4832 "cmlb_set_vtoc: wrote backup label %llx\n", blk);
4836 4833 if (rval != 0) {
4837 4834 goto exit;
4838 4835 }
4839 4836 }
4840 4837 exit:
4841 4838 return (rval);
4842 4839 }
4843 4840
4844 4841 /*
4845 4842 * Function: cmlb_clear_vtoc
4846 4843 *
4847 4844 * Description: This routine clears out the VTOC labels.
4848 4845 *
4849 4846 * Arguments:
4850 4847 * cl driver soft state (unit) structure
4851 4848 *
4852 4849 * tg_cookie cookie from target driver to be passed back to target
4853 4850 * driver when we call back to it through tg_ops.
4854 4851 *
4855 4852 * Return: void
4856 4853 */
4857 4854 static void
4858 4855 cmlb_clear_vtoc(struct cmlb_lun *cl, void *tg_cookie)
4859 4856 {
4860 4857 struct dk_label *dkl;
4861 4858
4862 4859 mutex_exit(CMLB_MUTEX(cl));
4863 4860 dkl = kmem_zalloc(cl->cl_sys_blocksize, KM_SLEEP);
4864 4861 mutex_enter(CMLB_MUTEX(cl));
4865 4862 /*
4866 4863 * cmlb_set_vtoc uses these fields in order to figure out
4867 4864 * where to overwrite the backup labels
4868 4865 */
4869 4866 dkl->dkl_apc = cl->cl_g.dkg_apc;
4870 4867 dkl->dkl_ncyl = cl->cl_g.dkg_ncyl;
4871 4868 dkl->dkl_acyl = cl->cl_g.dkg_acyl;
4872 4869 dkl->dkl_nhead = cl->cl_g.dkg_nhead;
4873 4870 dkl->dkl_nsect = cl->cl_g.dkg_nsect;
4874 4871 mutex_exit(CMLB_MUTEX(cl));
4875 4872 (void) cmlb_set_vtoc(cl, dkl, tg_cookie);
4876 4873 kmem_free(dkl, cl->cl_sys_blocksize);
4877 4874
4878 4875 mutex_enter(CMLB_MUTEX(cl));
4879 4876 }
4880 4877
4881 4878 /*
4882 4879 * Function: cmlb_write_label
4883 4880 *
4884 4881 * Description: This routine will validate and write the driver soft state vtoc
4885 4882 * contents to the device.
4886 4883 *
4887 4884 * Arguments:
4888 4885 * cl cmlb handle
4889 4886 *
4890 4887 * tg_cookie cookie from target driver to be passed back to target
4891 4888 * driver when we call back to it through tg_ops.
4892 4889 *
4893 4890 *
4894 4891 * Return Code: the code returned by cmlb_send_scsi_cmd()
4895 4892 * 0
4896 4893 * EINVAL
4897 4894 * ENXIO
4898 4895 * ENOMEM
4899 4896 */
4900 4897 static int
4901 4898 cmlb_write_label(struct cmlb_lun *cl, void *tg_cookie)
4902 4899 {
4903 4900 struct dk_label *dkl;
4904 4901 short sum;
4905 4902 short *sp;
4906 4903 int i;
4907 4904 int rval;
4908 4905
4909 4906 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
4910 4907 mutex_exit(CMLB_MUTEX(cl));
4911 4908 dkl = kmem_zalloc(cl->cl_sys_blocksize, KM_SLEEP);
4912 4909 mutex_enter(CMLB_MUTEX(cl));
4913 4910
4914 4911 bcopy(&cl->cl_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc));
4915 4912 dkl->dkl_rpm = cl->cl_g.dkg_rpm;
4916 4913 dkl->dkl_pcyl = cl->cl_g.dkg_pcyl;
4917 4914 dkl->dkl_apc = cl->cl_g.dkg_apc;
4918 4915 dkl->dkl_intrlv = cl->cl_g.dkg_intrlv;
4919 4916 dkl->dkl_ncyl = cl->cl_g.dkg_ncyl;
4920 4917 dkl->dkl_acyl = cl->cl_g.dkg_acyl;
4921 4918 dkl->dkl_nhead = cl->cl_g.dkg_nhead;
4922 4919 dkl->dkl_nsect = cl->cl_g.dkg_nsect;
4923 4920
4924 4921 #if defined(_SUNOS_VTOC_8)
4925 4922 dkl->dkl_obs1 = cl->cl_g.dkg_obs1;
4926 4923 dkl->dkl_obs2 = cl->cl_g.dkg_obs2;
4927 4924 dkl->dkl_obs3 = cl->cl_g.dkg_obs3;
4928 4925 for (i = 0; i < NDKMAP; i++) {
4929 4926 dkl->dkl_map[i].dkl_cylno = cl->cl_map[i].dkl_cylno;
4930 4927 dkl->dkl_map[i].dkl_nblk = cl->cl_map[i].dkl_nblk;
4931 4928 }
4932 4929 bcopy(cl->cl_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII);
4933 4930 #elif defined(_SUNOS_VTOC_16)
4934 4931 dkl->dkl_skew = cl->cl_dkg_skew;
4935 4932 #else
4936 4933 #error "No VTOC format defined."
4937 4934 #endif
4938 4935
4939 4936 dkl->dkl_magic = DKL_MAGIC;
4940 4937 dkl->dkl_write_reinstruct = cl->cl_g.dkg_write_reinstruct;
4941 4938 dkl->dkl_read_reinstruct = cl->cl_g.dkg_read_reinstruct;
4942 4939
4943 4940 /* Construct checksum for the new disk label */
4944 4941 sum = 0;
4945 4942 sp = (short *)dkl;
4946 4943 i = sizeof (struct dk_label) / sizeof (short);
4947 4944 while (i--) {
4948 4945 sum ^= *sp++;
4949 4946 }
4950 4947 dkl->dkl_cksum = sum;
4951 4948
4952 4949 mutex_exit(CMLB_MUTEX(cl));
4953 4950
4954 4951 rval = cmlb_set_vtoc(cl, dkl, tg_cookie);
4955 4952 exit:
4956 4953 kmem_free(dkl, cl->cl_sys_blocksize);
4957 4954 mutex_enter(CMLB_MUTEX(cl));
4958 4955 return (rval);
4959 4956 }
4960 4957
4961 4958 /*
4962 4959 * This routine implements the DKIOCSETEFI ioctl. This ioctl is currently
4963 4960 * used to write (or clear) the GPT Partition Table header (primary/backup)
4964 4961 * and GUID partition Entry Array (primary/backup). It is also used to write
4965 4962 * the Protective MBR.
4966 4963 */
4967 4964 static int
4968 4965 cmlb_dkio_set_efi(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag,
4969 4966 void *tg_cookie)
4970 4967 {
4971 4968 dk_efi_t user_efi;
4972 4969 int shift, rval = 0;
4973 4970 void *buffer;
4974 4971 diskaddr_t tgt_lba;
4975 4972 boolean_t internal;
4976 4973
4977 4974 if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
4978 4975 return (EFAULT);
4979 4976
4980 4977 internal = VOID2BOOLEAN(
4981 4978 (cl->cl_alter_behavior & (CMLB_INTERNAL_MINOR_NODES)) != 0);
4982 4979
4983 4980 if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE)
4984 4981 shift = CMLBUNIT_FORCE_P0_SHIFT;
4985 4982 else
4986 4983 shift = CMLBUNIT_SHIFT;
4987 4984
4988 4985 user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
4989 4986
4990 4987 if (user_efi.dki_length == 0 ||
4991 4988 user_efi.dki_length > cmlb_tg_max_efi_xfer)
4992 4989 return (EINVAL);
4993 4990
4994 4991 tgt_lba = user_efi.dki_lba;
4995 4992
4996 4993 mutex_enter(CMLB_MUTEX(cl));
4997 4994 if ((cmlb_check_update_blockcount(cl, tg_cookie) != 0) ||
4998 4995 (cl->cl_tgt_blocksize == 0) ||
4999 4996 (user_efi.dki_length % cl->cl_sys_blocksize)) {
5000 4997 mutex_exit(CMLB_MUTEX(cl));
5001 4998 return (EINVAL);
5002 4999 }
5003 5000 if (cl->cl_tgt_blocksize != cl->cl_sys_blocksize)
5004 5001 tgt_lba = tgt_lba *
5005 5002 cl->cl_tgt_blocksize / cl->cl_sys_blocksize;
5006 5003 mutex_exit(CMLB_MUTEX(cl));
5007 5004
5008 5005 buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
5009 5006 if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) {
5010 5007 rval = EFAULT;
5011 5008 } else {
5012 5009 /*
5013 5010 * let's clear the vtoc labels and clear the softstate
5014 5011 * vtoc.
5015 5012 */
5016 5013 mutex_enter(CMLB_MUTEX(cl));
5017 5014 if (cl->cl_vtoc.v_sanity == VTOC_SANE) {
5018 5015 cmlb_dbg(CMLB_TRACE, cl,
5019 5016 "cmlb_dkio_set_efi: CLEAR VTOC\n");
5020 5017 if (cl->cl_label_from_media == CMLB_LABEL_VTOC)
5021 5018 cmlb_clear_vtoc(cl, tg_cookie);
5022 5019 bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
5023 5020 mutex_exit(CMLB_MUTEX(cl));
5024 5021 ddi_remove_minor_node(CMLB_DEVINFO(cl), "h");
5025 5022 ddi_remove_minor_node(CMLB_DEVINFO(cl), "h,raw");
5026 5023 (void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd",
5027 5024 S_IFBLK,
5028 5025 (CMLBUNIT(dev, shift) << shift) | WD_NODE,
5029 5026 cl->cl_node_type, NULL, internal);
5030 5027 (void) cmlb_create_minor(CMLB_DEVINFO(cl), "wd,raw",
5031 5028 S_IFCHR,
5032 5029 (CMLBUNIT(dev, shift) << shift) | WD_NODE,
5033 5030 cl->cl_node_type, NULL, internal);
5034 5031 } else
5035 5032 mutex_exit(CMLB_MUTEX(cl));
5036 5033
5037 5034 rval = DK_TG_WRITE(cl, buffer, tgt_lba, user_efi.dki_length,
5038 5035 tg_cookie);
5039 5036
5040 5037 if (rval == 0) {
5041 5038 mutex_enter(CMLB_MUTEX(cl));
5042 5039 cl->cl_f_geometry_is_valid = B_FALSE;
5043 5040 mutex_exit(CMLB_MUTEX(cl));
5044 5041 }
5045 5042 }
5046 5043 kmem_free(buffer, user_efi.dki_length);
5047 5044 return (rval);
5048 5045 }
5049 5046
5050 5047 /*
5051 5048 * Function: cmlb_dkio_get_mboot
5052 5049 *
5053 5050 * Description: This routine is the driver entry point for handling user
5054 5051 * requests to get the current device mboot (DKIOCGMBOOT)
5055 5052 *
5056 5053 * Arguments:
5057 5054 * arg pointer to user provided mboot structure specifying
5058 5055 * the current mboot.
5059 5056 *
5060 5057 * flag this argument is a pass through to ddi_copyxxx()
5061 5058 * directly from the mode argument of ioctl().
5062 5059 *
5063 5060 * tg_cookie cookie from target driver to be passed back to target
5064 5061 * driver when we call back to it through tg_ops.
5065 5062 *
5066 5063 * Return Code: 0
5067 5064 * EINVAL
5068 5065 * EFAULT
5069 5066 * ENXIO
5070 5067 */
5071 5068 static int
5072 5069 cmlb_dkio_get_mboot(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
5073 5070 {
5074 5071 struct mboot *mboot;
5075 5072 int rval;
5076 5073 size_t buffer_size;
5077 5074
5078 5075
5079 5076 #if defined(_SUNOS_VTOC_8)
5080 5077 if ((!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) || (arg == NULL)) {
5081 5078 #elif defined(_SUNOS_VTOC_16)
5082 5079 if (arg == NULL) {
5083 5080 #endif
5084 5081 return (EINVAL);
5085 5082 }
5086 5083
5087 5084 /*
5088 5085 * Read the mboot block, located at absolute block 0 on the target.
5089 5086 */
5090 5087 buffer_size = cl->cl_sys_blocksize;
5091 5088
5092 5089 cmlb_dbg(CMLB_TRACE, cl,
5093 5090 "cmlb_dkio_get_mboot: allocation size: 0x%x\n", buffer_size);
5094 5091
5095 5092 mboot = kmem_zalloc(buffer_size, KM_SLEEP);
5096 5093 if ((rval = DK_TG_READ(cl, mboot, 0, buffer_size, tg_cookie)) == 0) {
5097 5094 if (ddi_copyout(mboot, (void *)arg,
5098 5095 sizeof (struct mboot), flag) != 0) {
5099 5096 rval = EFAULT;
5100 5097 }
5101 5098 }
5102 5099 kmem_free(mboot, buffer_size);
5103 5100 return (rval);
5104 5101 }
5105 5102
5106 5103
5107 5104 /*
5108 5105 * Function: cmlb_dkio_set_mboot
5109 5106 *
5110 5107 * Description: This routine is the driver entry point for handling user
5111 5108 * requests to validate and set the device master boot
5112 5109 * (DKIOCSMBOOT).
5113 5110 *
5114 5111 * Arguments:
5115 5112 * arg pointer to user provided mboot structure used to set the
5116 5113 * master boot.
5117 5114 *
5118 5115 * flag this argument is a pass through to ddi_copyxxx()
5119 5116 * directly from the mode argument of ioctl().
5120 5117 *
5121 5118 * tg_cookie cookie from target driver to be passed back to target
5122 5119 * driver when we call back to it through tg_ops.
5123 5120 *
5124 5121 * Return Code: 0
5125 5122 * EINVAL
5126 5123 * EFAULT
5127 5124 * ENXIO
5128 5125 */
5129 5126 static int
5130 5127 cmlb_dkio_set_mboot(struct cmlb_lun *cl, caddr_t arg, int flag, void *tg_cookie)
5131 5128 {
5132 5129 struct mboot *mboot = NULL;
5133 5130 int rval;
5134 5131 ushort_t magic;
5135 5132
5136 5133
5137 5134 ASSERT(!mutex_owned(CMLB_MUTEX(cl)));
5138 5135
5139 5136 #if defined(_SUNOS_VTOC_8)
5140 5137 if (!ISREMOVABLE(cl) && !ISHOTPLUGGABLE(cl)) {
5141 5138 return (EINVAL);
5142 5139 }
5143 5140 #endif
5144 5141
5145 5142 if (arg == NULL) {
5146 5143 return (EINVAL);
5147 5144 }
5148 5145
5149 5146 mboot = kmem_zalloc(cl->cl_sys_blocksize, KM_SLEEP);
5150 5147
5151 5148 if (ddi_copyin((const void *)arg, mboot,
5152 5149 cl->cl_sys_blocksize, flag) != 0) {
5153 5150 kmem_free(mboot, cl->cl_sys_blocksize);
5154 5151 return (EFAULT);
5155 5152 }
5156 5153
5157 5154 /* Is this really a master boot record? */
5158 5155 magic = LE_16(mboot->signature);
5159 5156 if (magic != MBB_MAGIC) {
5160 5157 kmem_free(mboot, cl->cl_sys_blocksize);
5161 5158 return (EINVAL);
5162 5159 }
5163 5160
5164 5161 rval = DK_TG_WRITE(cl, mboot, 0, cl->cl_sys_blocksize, tg_cookie);
5165 5162
5166 5163 mutex_enter(CMLB_MUTEX(cl));
5167 5164 #if defined(__i386) || defined(__amd64)
5168 5165 if (rval == 0) {
5169 5166 /*
↓ open down ↓ |
1300 lines elided |
↑ open up ↑ |
5170 5167 * mboot has been written successfully.
5171 5168 * update the fdisk and vtoc tables in memory
5172 5169 */
5173 5170 rval = cmlb_update_fdisk_and_vtoc(cl, tg_cookie);
5174 5171 if ((!cl->cl_f_geometry_is_valid) || (rval != 0)) {
5175 5172 mutex_exit(CMLB_MUTEX(cl));
5176 5173 kmem_free(mboot, cl->cl_sys_blocksize);
5177 5174 return (rval);
5178 5175 }
5179 5176 }
5180 -
5181 -#ifdef __lock_lint
5182 - cmlb_setup_default_geometry(cl, tg_cookie);
5183 -#endif
5184 -
5185 5177 #else
5186 5178 if (rval == 0) {
5187 5179 /*
5188 5180 * mboot has been written successfully.
5189 5181 * set up the default geometry and VTOC
5190 5182 */
5191 5183 if (cl->cl_blockcount <= CMLB_EXTVTOC_LIMIT)
5192 5184 cmlb_setup_default_geometry(cl, tg_cookie);
5193 5185 }
5194 5186 #endif
5195 5187 cl->cl_msglog_flag |= CMLB_ALLOW_2TB_WARN;
5196 5188 mutex_exit(CMLB_MUTEX(cl));
5197 5189 kmem_free(mboot, cl->cl_sys_blocksize);
5198 5190 return (rval);
5199 5191 }
5200 5192
5201 5193
5202 5194 #if defined(__i386) || defined(__amd64)
5203 5195 /*ARGSUSED*/
5204 5196 static int
5205 5197 cmlb_dkio_set_ext_part(struct cmlb_lun *cl, caddr_t arg, int flag,
5206 5198 void *tg_cookie)
5207 5199 {
5208 5200 int fdisk_rval;
5209 5201 diskaddr_t capacity;
5210 5202
5211 5203 ASSERT(!mutex_owned(CMLB_MUTEX(cl)));
5212 5204
5213 5205 mutex_enter(CMLB_MUTEX(cl));
5214 5206 capacity = cl->cl_blockcount;
5215 5207 fdisk_rval = cmlb_read_fdisk(cl, capacity, tg_cookie);
5216 5208 if (fdisk_rval != 0) {
5217 5209 mutex_exit(CMLB_MUTEX(cl));
5218 5210 return (fdisk_rval);
5219 5211 }
5220 5212
5221 5213 mutex_exit(CMLB_MUTEX(cl));
5222 5214 return (fdisk_rval);
5223 5215 }
5224 5216 #endif
5225 5217
5226 5218 /*
5227 5219 * Function: cmlb_setup_default_geometry
5228 5220 *
5229 5221 * Description: This local utility routine sets the default geometry as part of
5230 5222 * setting the device mboot.
5231 5223 *
5232 5224 * Arguments:
5233 5225 * cl driver soft state (unit) structure
5234 5226 *
5235 5227 * tg_cookie cookie from target driver to be passed back to target
5236 5228 * driver when we call back to it through tg_ops.
5237 5229 *
5238 5230 *
5239 5231 * Note: This may be redundant with cmlb_build_default_label.
5240 5232 */
5241 5233 static void
5242 5234 cmlb_setup_default_geometry(struct cmlb_lun *cl, void *tg_cookie)
5243 5235 {
5244 5236 struct cmlb_geom pgeom;
5245 5237 struct cmlb_geom *pgeomp = &pgeom;
5246 5238 int ret;
5247 5239 int geom_base_cap = 1;
5248 5240
5249 5241
5250 5242 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
5251 5243
5252 5244 /* zero out the soft state geometry and partition table. */
5253 5245 bzero(&cl->cl_g, sizeof (struct dk_geom));
5254 5246 bzero(&cl->cl_vtoc, sizeof (struct dk_vtoc));
5255 5247 bzero(cl->cl_map, NDKMAP * (sizeof (struct dk_map)));
5256 5248
5257 5249 /*
5258 5250 * For the rpm, we use the minimum for the disk.
5259 5251 * For the head, cyl and number of sector per track,
5260 5252 * if the capacity <= 1GB, head = 64, sect = 32.
5261 5253 * else head = 255, sect 63
5262 5254 * Note: the capacity should be equal to C*H*S values.
5263 5255 * This will cause some truncation of size due to
5264 5256 * round off errors. For CD-ROMs, this truncation can
5265 5257 * have adverse side effects, so returning ncyl and
5266 5258 * nhead as 1. The nsect will overflow for most of
5267 5259 * CD-ROMs as nsect is of type ushort.
5268 5260 */
5269 5261 if (cl->cl_alter_behavior & CMLB_FAKE_GEOM_LABEL_IOCTLS_VTOC8) {
5270 5262 /*
5271 5263 * newfs currently can not handle 255 ntracks for SPARC
5272 5264 * so get the geometry from target driver instead of coming up
5273 5265 * with one based on capacity.
5274 5266 */
5275 5267 mutex_exit(CMLB_MUTEX(cl));
5276 5268 ret = DK_TG_GETPHYGEOM(cl, pgeomp, tg_cookie);
5277 5269 mutex_enter(CMLB_MUTEX(cl));
5278 5270
5279 5271 if (ret == 0) {
5280 5272 geom_base_cap = 0;
5281 5273 } else {
5282 5274 cmlb_dbg(CMLB_ERROR, cl,
5283 5275 "cmlb_setup_default_geometry: "
5284 5276 "tg_getphygeom failed %d\n", ret);
5285 5277
5286 5278 /* do default setting, geometry based on capacity */
5287 5279 }
5288 5280 }
5289 5281
5290 5282 if (geom_base_cap) {
5291 5283 if (ISCD(cl)) {
5292 5284 cl->cl_g.dkg_ncyl = 1;
5293 5285 cl->cl_g.dkg_nhead = 1;
5294 5286 cl->cl_g.dkg_nsect = cl->cl_blockcount;
5295 5287 } else if (cl->cl_blockcount < 160) {
5296 5288 /* Less than 80K */
5297 5289 cl->cl_g.dkg_nhead = 1;
5298 5290 cl->cl_g.dkg_ncyl = cl->cl_blockcount;
5299 5291 cl->cl_g.dkg_nsect = 1;
5300 5292 } else if (cl->cl_blockcount <= 0x1000) {
5301 5293 /* Needed for unlabeled SCSI floppies. */
5302 5294 cl->cl_g.dkg_nhead = 2;
5303 5295 cl->cl_g.dkg_ncyl = 80;
5304 5296 cl->cl_g.dkg_pcyl = 80;
5305 5297 cl->cl_g.dkg_nsect = cl->cl_blockcount / (2 * 80);
5306 5298 } else if (cl->cl_blockcount <= 0x200000) {
5307 5299 cl->cl_g.dkg_nhead = 64;
5308 5300 cl->cl_g.dkg_nsect = 32;
5309 5301 cl->cl_g.dkg_ncyl = cl->cl_blockcount / (64 * 32);
5310 5302 } else {
5311 5303 cl->cl_g.dkg_nhead = 255;
5312 5304
5313 5305 cl->cl_g.dkg_nsect = ((cl->cl_blockcount +
5314 5306 (UINT16_MAX * 255 * 63) - 1) /
5315 5307 (UINT16_MAX * 255 * 63)) * 63;
5316 5308
5317 5309 if (cl->cl_g.dkg_nsect == 0)
5318 5310 cl->cl_g.dkg_nsect = (UINT16_MAX / 63) * 63;
5319 5311
5320 5312 cl->cl_g.dkg_ncyl = cl->cl_blockcount /
5321 5313 (255 * cl->cl_g.dkg_nsect);
5322 5314 }
5323 5315
5324 5316 cl->cl_g.dkg_acyl = 0;
5325 5317 cl->cl_g.dkg_bcyl = 0;
5326 5318 cl->cl_g.dkg_intrlv = 1;
5327 5319 cl->cl_g.dkg_rpm = 200;
5328 5320 if (cl->cl_g.dkg_pcyl == 0)
5329 5321 cl->cl_g.dkg_pcyl = cl->cl_g.dkg_ncyl +
5330 5322 cl->cl_g.dkg_acyl;
5331 5323 } else {
5332 5324 cl->cl_g.dkg_ncyl = (short)pgeomp->g_ncyl;
5333 5325 cl->cl_g.dkg_acyl = pgeomp->g_acyl;
5334 5326 cl->cl_g.dkg_nhead = pgeomp->g_nhead;
5335 5327 cl->cl_g.dkg_nsect = pgeomp->g_nsect;
5336 5328 cl->cl_g.dkg_intrlv = pgeomp->g_intrlv;
5337 5329 cl->cl_g.dkg_rpm = pgeomp->g_rpm;
5338 5330 cl->cl_g.dkg_pcyl = cl->cl_g.dkg_ncyl + cl->cl_g.dkg_acyl;
5339 5331 }
5340 5332
5341 5333 cl->cl_g.dkg_read_reinstruct = 0;
5342 5334 cl->cl_g.dkg_write_reinstruct = 0;
5343 5335 cl->cl_solaris_size = cl->cl_g.dkg_ncyl *
5344 5336 cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect;
5345 5337
5346 5338 cl->cl_map['a'-'a'].dkl_cylno = 0;
5347 5339 cl->cl_map['a'-'a'].dkl_nblk = cl->cl_solaris_size;
5348 5340
5349 5341 cl->cl_map['c'-'a'].dkl_cylno = 0;
5350 5342 cl->cl_map['c'-'a'].dkl_nblk = cl->cl_solaris_size;
5351 5343
5352 5344 cl->cl_vtoc.v_part[2].p_tag = V_BACKUP;
5353 5345 cl->cl_vtoc.v_part[2].p_flag = V_UNMNT;
5354 5346 cl->cl_vtoc.v_nparts = V_NUMPAR;
5355 5347 cl->cl_vtoc.v_version = V_VERSION;
5356 5348 (void) sprintf((char *)cl->cl_asciilabel, "DEFAULT cyl %d alt %d"
5357 5349 " hd %d sec %d", cl->cl_g.dkg_ncyl, cl->cl_g.dkg_acyl,
5358 5350 cl->cl_g.dkg_nhead, cl->cl_g.dkg_nsect);
5359 5351
5360 5352 cl->cl_f_geometry_is_valid = B_FALSE;
5361 5353 }
5362 5354
5363 5355
5364 5356 #if defined(__i386) || defined(__amd64)
5365 5357 /*
5366 5358 * Function: cmlb_update_fdisk_and_vtoc
5367 5359 *
5368 5360 * Description: This local utility routine updates the device fdisk and vtoc
5369 5361 * as part of setting the device mboot.
5370 5362 *
5371 5363 * Arguments:
5372 5364 * cl driver soft state (unit) structure
5373 5365 *
5374 5366 * tg_cookie cookie from target driver to be passed back to target
5375 5367 * driver when we call back to it through tg_ops.
5376 5368 *
5377 5369 *
5378 5370 * Return Code: 0 for success or errno-type return code.
5379 5371 *
5380 5372 * Note:x86: This looks like a duplicate of cmlb_validate_geometry(), but
5381 5373 * these did exist separately in x86 sd.c.
5382 5374 */
5383 5375 static int
5384 5376 cmlb_update_fdisk_and_vtoc(struct cmlb_lun *cl, void *tg_cookie)
5385 5377 {
5386 5378 int count;
5387 5379 int label_rc = 0;
5388 5380 int fdisk_rval;
5389 5381 diskaddr_t capacity;
5390 5382
5391 5383 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
5392 5384
5393 5385 if (cmlb_check_update_blockcount(cl, tg_cookie) != 0)
5394 5386 return (EINVAL);
5395 5387
5396 5388 /*
5397 5389 * Set up the "whole disk" fdisk partition; this should always
5398 5390 * exist, regardless of whether the disk contains an fdisk table
5399 5391 * or vtoc.
5400 5392 */
5401 5393 cl->cl_map[P0_RAW_DISK].dkl_cylno = 0;
5402 5394 cl->cl_map[P0_RAW_DISK].dkl_nblk = cl->cl_blockcount;
5403 5395
5404 5396 /*
5405 5397 * copy the lbasize and capacity so that if they're
5406 5398 * reset while we're not holding the CMLB_MUTEX(cl), we will
5407 5399 * continue to use valid values after the CMLB_MUTEX(cl) is
5408 5400 * reacquired.
5409 5401 */
5410 5402 capacity = cl->cl_blockcount;
5411 5403
5412 5404 /*
5413 5405 * refresh the logical and physical geometry caches.
5414 5406 * (data from mode sense format/rigid disk geometry pages,
5415 5407 * and scsi_ifgetcap("geometry").
5416 5408 */
5417 5409 cmlb_resync_geom_caches(cl, capacity, tg_cookie);
5418 5410
5419 5411 /*
5420 5412 * Only DIRECT ACCESS devices will have Scl labels.
5421 5413 * CD's supposedly have a Scl label, too
5422 5414 */
5423 5415 if (cl->cl_device_type == DTYPE_DIRECT || ISREMOVABLE(cl)) {
5424 5416 fdisk_rval = cmlb_read_fdisk(cl, capacity, tg_cookie);
5425 5417 if (fdisk_rval != 0) {
5426 5418 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
5427 5419 return (fdisk_rval);
5428 5420 }
5429 5421
5430 5422 if (cl->cl_solaris_size <= DK_LABEL_LOC) {
5431 5423 /*
5432 5424 * Found fdisk table but no Solaris partition entry,
5433 5425 * so don't call cmlb_uselabel() and don't create
5434 5426 * a default label.
5435 5427 */
5436 5428 label_rc = 0;
5437 5429 cl->cl_f_geometry_is_valid = B_TRUE;
5438 5430 goto no_solaris_partition;
5439 5431 }
5440 5432 } else if (capacity < 0) {
5441 5433 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
5442 5434 return (EINVAL);
5443 5435 }
5444 5436
5445 5437 /*
5446 5438 * For Removable media We reach here if we have found a
5447 5439 * SOLARIS PARTITION.
5448 5440 * If cl_f_geometry_is_valid is B_FALSE it indicates that the SOLARIS
5449 5441 * PARTITION has changed from the previous one, hence we will setup a
5450 5442 * default VTOC in this case.
5451 5443 */
5452 5444 if (!cl->cl_f_geometry_is_valid) {
5453 5445 /* if we get here it is writable */
5454 5446 /* we are called from SMBOOT, and after a write of fdisk */
5455 5447 cmlb_build_default_label(cl, tg_cookie);
5456 5448 label_rc = 0;
5457 5449 }
5458 5450
5459 5451 no_solaris_partition:
5460 5452
5461 5453 #if defined(_SUNOS_VTOC_16)
5462 5454 /*
5463 5455 * If we have valid geometry, set up the remaining fdisk partitions.
5464 5456 * Note that dkl_cylno is not used for the fdisk map entries, so
5465 5457 * we set it to an entirely bogus value.
5466 5458 */
5467 5459 for (count = 0; count < FDISK_PARTS; count++) {
5468 5460 cl->cl_map[FDISK_P1 + count].dkl_cylno = UINT32_MAX;
5469 5461 cl->cl_map[FDISK_P1 + count].dkl_nblk =
5470 5462 cl->cl_fmap[count].fmap_nblk;
5471 5463 cl->cl_offset[FDISK_P1 + count] =
5472 5464 cl->cl_fmap[count].fmap_start;
5473 5465 }
5474 5466 #endif
5475 5467
5476 5468 for (count = 0; count < NDKMAP; count++) {
5477 5469 #if defined(_SUNOS_VTOC_8)
5478 5470 struct dk_map *lp = &cl->cl_map[count];
5479 5471 cl->cl_offset[count] =
5480 5472 cl->cl_g.dkg_nhead * cl->cl_g.dkg_nsect * lp->dkl_cylno;
5481 5473 #elif defined(_SUNOS_VTOC_16)
5482 5474 struct dkl_partition *vp = &cl->cl_vtoc.v_part[count];
5483 5475 cl->cl_offset[count] = vp->p_start + cl->cl_solaris_offset;
5484 5476 #else
5485 5477 #error "No VTOC format defined."
5486 5478 #endif
5487 5479 }
5488 5480
5489 5481 ASSERT(mutex_owned(CMLB_MUTEX(cl)));
5490 5482 return (label_rc);
5491 5483 }
5492 5484 #endif
5493 5485
5494 5486 #if defined(__i386) || defined(__amd64)
5495 5487 static int
5496 5488 cmlb_dkio_get_virtgeom(struct cmlb_lun *cl, caddr_t arg, int flag)
5497 5489 {
5498 5490 int err = 0;
5499 5491
5500 5492 /* Return the driver's notion of the media's logical geometry */
5501 5493 struct dk_geom disk_geom;
5502 5494 struct dk_geom *dkgp = &disk_geom;
5503 5495
5504 5496 mutex_enter(CMLB_MUTEX(cl));
5505 5497 /*
5506 5498 * If there is no HBA geometry available, or
5507 5499 * if the HBA returned us something that doesn't
5508 5500 * really fit into an Int 13/function 8 geometry
5509 5501 * result, just fail the ioctl. See PSARC 1998/313.
5510 5502 */
5511 5503 if (cl->cl_lgeom.g_nhead == 0 ||
5512 5504 cl->cl_lgeom.g_nsect == 0 ||
5513 5505 cl->cl_lgeom.g_ncyl > 1024) {
5514 5506 mutex_exit(CMLB_MUTEX(cl));
5515 5507 err = EINVAL;
5516 5508 } else {
5517 5509 dkgp->dkg_ncyl = cl->cl_lgeom.g_ncyl;
5518 5510 dkgp->dkg_acyl = cl->cl_lgeom.g_acyl;
5519 5511 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl;
5520 5512 dkgp->dkg_nhead = cl->cl_lgeom.g_nhead;
5521 5513 dkgp->dkg_nsect = cl->cl_lgeom.g_nsect;
5522 5514
5523 5515 mutex_exit(CMLB_MUTEX(cl));
5524 5516 if (ddi_copyout(dkgp, (void *)arg,
5525 5517 sizeof (struct dk_geom), flag)) {
5526 5518 err = EFAULT;
5527 5519 } else {
5528 5520 err = 0;
5529 5521 }
5530 5522 }
5531 5523 return (err);
5532 5524 }
5533 5525 #endif
5534 5526
5535 5527 #if defined(__i386) || defined(__amd64)
5536 5528 static int
5537 5529 cmlb_dkio_get_phygeom(struct cmlb_lun *cl, caddr_t arg, int flag,
5538 5530 void *tg_cookie)
5539 5531 {
5540 5532 int err = 0;
5541 5533 diskaddr_t capacity;
5542 5534
5543 5535
5544 5536 /* Return the driver's notion of the media physical geometry */
5545 5537 struct dk_geom disk_geom;
5546 5538 struct dk_geom *dkgp = &disk_geom;
5547 5539
5548 5540 mutex_enter(CMLB_MUTEX(cl));
5549 5541
5550 5542 if (cl->cl_g.dkg_nhead != 0 &&
5551 5543 cl->cl_g.dkg_nsect != 0) {
5552 5544 /*
5553 5545 * We succeeded in getting a geometry, but
5554 5546 * right now it is being reported as just the
5555 5547 * Solaris fdisk partition, just like for
5556 5548 * DKIOCGGEOM. We need to change that to be
5557 5549 * correct for the entire disk now.
5558 5550 */
5559 5551 bcopy(&cl->cl_g, dkgp, sizeof (*dkgp));
5560 5552 dkgp->dkg_acyl = 0;
5561 5553 dkgp->dkg_ncyl = cl->cl_blockcount /
5562 5554 (dkgp->dkg_nhead * dkgp->dkg_nsect);
5563 5555 } else {
5564 5556 bzero(dkgp, sizeof (struct dk_geom));
5565 5557 /*
5566 5558 * This disk does not have a Solaris VTOC
5567 5559 * so we must present a physical geometry
5568 5560 * that will remain consistent regardless
5569 5561 * of how the disk is used. This will ensure
5570 5562 * that the geometry does not change regardless
5571 5563 * of the fdisk partition type (ie. EFI, FAT32,
5572 5564 * Solaris, etc).
5573 5565 */
5574 5566 if (ISCD(cl)) {
5575 5567 dkgp->dkg_nhead = cl->cl_pgeom.g_nhead;
5576 5568 dkgp->dkg_nsect = cl->cl_pgeom.g_nsect;
5577 5569 dkgp->dkg_ncyl = cl->cl_pgeom.g_ncyl;
5578 5570 dkgp->dkg_acyl = cl->cl_pgeom.g_acyl;
5579 5571 } else {
5580 5572 /*
5581 5573 * Invalid cl_blockcount can generate invalid
5582 5574 * dk_geom and may result in division by zero
5583 5575 * system failure. Should make sure blockcount
5584 5576 * is valid before using it here.
5585 5577 */
5586 5578 if (cl->cl_blockcount == 0) {
5587 5579 mutex_exit(CMLB_MUTEX(cl));
5588 5580 err = EIO;
5589 5581 return (err);
5590 5582 }
5591 5583 /*
5592 5584 * Refer to comments related to off-by-1 at the
5593 5585 * header of this file
5594 5586 */
5595 5587 if (cl->cl_alter_behavior & CMLB_OFF_BY_ONE)
5596 5588 capacity = cl->cl_blockcount - 1;
5597 5589 else
5598 5590 capacity = cl->cl_blockcount;
5599 5591
5600 5592 cmlb_convert_geometry(cl, capacity, dkgp, tg_cookie);
5601 5593 dkgp->dkg_acyl = 0;
5602 5594 dkgp->dkg_ncyl = capacity /
5603 5595 (dkgp->dkg_nhead * dkgp->dkg_nsect);
5604 5596 }
5605 5597 }
5606 5598 dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl;
5607 5599
5608 5600 mutex_exit(CMLB_MUTEX(cl));
5609 5601 if (ddi_copyout(dkgp, (void *)arg, sizeof (struct dk_geom), flag))
5610 5602 err = EFAULT;
5611 5603
5612 5604 return (err);
5613 5605 }
5614 5606 #endif
5615 5607
5616 5608 #if defined(__i386) || defined(__amd64)
5617 5609 static int
5618 5610 cmlb_dkio_partinfo(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag)
5619 5611 {
5620 5612 int err = 0;
5621 5613
5622 5614 /*
5623 5615 * Return parameters describing the selected disk slice.
5624 5616 * Note: this ioctl is for the intel platform only
5625 5617 */
5626 5618 int part;
5627 5619
5628 5620 if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE)
5629 5621 part = getminor(dev) & ((1 << CMLBUNIT_FORCE_P0_SHIFT) - 1);
5630 5622 else
5631 5623 part = CMLBPART(dev);
5632 5624
5633 5625 mutex_enter(CMLB_MUTEX(cl));
5634 5626 /* don't check cl_solaris_size for pN */
5635 5627 if (part < P0_RAW_DISK && cl->cl_solaris_size == 0) {
5636 5628 err = EIO;
5637 5629 mutex_exit(CMLB_MUTEX(cl));
5638 5630 } else {
5639 5631 struct part_info p;
5640 5632
5641 5633 p.p_start = (daddr_t)cl->cl_offset[part];
5642 5634 p.p_length = (int)cl->cl_map[part].dkl_nblk;
5643 5635 mutex_exit(CMLB_MUTEX(cl));
5644 5636 #ifdef _MULTI_DATAMODEL
5645 5637 switch (ddi_model_convert_from(flag & FMODELS)) {
5646 5638 case DDI_MODEL_ILP32:
5647 5639 {
5648 5640 struct part_info32 p32;
5649 5641
5650 5642 p32.p_start = (daddr32_t)p.p_start;
5651 5643 p32.p_length = p.p_length;
5652 5644 if (ddi_copyout(&p32, (void *)arg,
5653 5645 sizeof (p32), flag))
5654 5646 err = EFAULT;
5655 5647 break;
5656 5648 }
5657 5649
5658 5650 case DDI_MODEL_NONE:
5659 5651 {
5660 5652 if (ddi_copyout(&p, (void *)arg, sizeof (p),
5661 5653 flag))
5662 5654 err = EFAULT;
5663 5655 break;
5664 5656 }
5665 5657 }
5666 5658 #else /* ! _MULTI_DATAMODEL */
5667 5659 if (ddi_copyout(&p, (void *)arg, sizeof (p), flag))
5668 5660 err = EFAULT;
5669 5661 #endif /* _MULTI_DATAMODEL */
5670 5662 }
5671 5663 return (err);
5672 5664 }
5673 5665 static int
5674 5666 cmlb_dkio_extpartinfo(struct cmlb_lun *cl, dev_t dev, caddr_t arg, int flag)
5675 5667 {
5676 5668 int err = 0;
5677 5669
5678 5670 /*
5679 5671 * Return parameters describing the selected disk slice.
5680 5672 * Note: this ioctl is for the intel platform only
5681 5673 */
5682 5674 int part;
5683 5675
5684 5676 if (cl->cl_alter_behavior & CMLB_CREATE_P0_MINOR_NODE)
5685 5677 part = getminor(dev) & ((1 << CMLBUNIT_FORCE_P0_SHIFT) - 1);
5686 5678 else
5687 5679 part = CMLBPART(dev);
5688 5680
5689 5681 mutex_enter(CMLB_MUTEX(cl));
5690 5682 /* don't check cl_solaris_size for pN */
5691 5683 if (part < P0_RAW_DISK && cl->cl_solaris_size == 0) {
5692 5684 err = EIO;
5693 5685 mutex_exit(CMLB_MUTEX(cl));
5694 5686 } else {
5695 5687 struct extpart_info p;
5696 5688
5697 5689 p.p_start = (diskaddr_t)cl->cl_offset[part];
5698 5690 p.p_length = (diskaddr_t)cl->cl_map[part].dkl_nblk;
5699 5691 mutex_exit(CMLB_MUTEX(cl));
5700 5692 if (ddi_copyout(&p, (void *)arg, sizeof (p), flag))
5701 5693 err = EFAULT;
5702 5694 }
5703 5695 return (err);
5704 5696 }
5705 5697 #endif
5706 5698
5707 5699 int
5708 5700 cmlb_prop_op(cmlb_handle_t cmlbhandle,
5709 5701 dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
5710 5702 char *name, caddr_t valuep, int *lengthp, int part, void *tg_cookie)
5711 5703 {
5712 5704 struct cmlb_lun *cl;
5713 5705 diskaddr_t capacity;
5714 5706 uint32_t lbasize;
5715 5707 enum dp { DP_NBLOCKS, DP_BLKSIZE, DP_SSD, DP_ROT } dp;
5716 5708 int callers_length;
5717 5709 caddr_t buffer;
5718 5710 uint64_t nblocks64;
5719 5711 uint_t dblk;
5720 5712 tg_attribute_t tgattr;
5721 5713
5722 5714 /* Always fallback to ddi_prop_op... */
5723 5715 cl = (struct cmlb_lun *)cmlbhandle;
5724 5716 if (cl == NULL) {
5725 5717 fallback: return (ddi_prop_op(dev, dip, prop_op, mod_flags,
5726 5718 name, valuep, lengthp));
5727 5719 }
5728 5720
5729 5721 /* Pick up capacity and blocksize information. */
5730 5722 capacity = cl->cl_blockcount;
5731 5723 if (capacity == 0)
5732 5724 goto fallback;
5733 5725 lbasize = cl->cl_tgt_blocksize;
5734 5726 if (lbasize == 0)
5735 5727 lbasize = DEV_BSIZE; /* 0 -> DEV_BSIZE units */
5736 5728
5737 5729 /* Check for dynamic property of whole device. */
5738 5730 if (dev == DDI_DEV_T_ANY) {
5739 5731 /* Fallback to ddi_prop_op if we don't understand. */
5740 5732 if (strcmp(name, "device-nblocks") == 0)
5741 5733 dp = DP_NBLOCKS;
5742 5734 else if (strcmp(name, "device-blksize") == 0)
5743 5735 dp = DP_BLKSIZE;
5744 5736 else if (strcmp(name, "device-solid-state") == 0)
5745 5737 dp = DP_SSD;
5746 5738 else if (strcmp(name, "device-rotational") == 0)
5747 5739 dp = DP_ROT;
5748 5740 else
5749 5741 goto fallback;
5750 5742
5751 5743 /* get callers length, establish length of our dynamic prop */
5752 5744 callers_length = *lengthp;
5753 5745 if (dp == DP_NBLOCKS)
5754 5746 *lengthp = sizeof (uint64_t);
5755 5747 else if ((dp == DP_BLKSIZE) || (dp == DP_SSD))
5756 5748 *lengthp = sizeof (uint32_t);
5757 5749
5758 5750 /* service request for the length of the property */
5759 5751 if (prop_op == PROP_LEN)
5760 5752 return (DDI_PROP_SUCCESS);
5761 5753
5762 5754 switch (prop_op) {
5763 5755 case PROP_LEN_AND_VAL_ALLOC:
5764 5756 if ((buffer = kmem_alloc(*lengthp,
5765 5757 (mod_flags & DDI_PROP_CANSLEEP) ?
5766 5758 KM_SLEEP : KM_NOSLEEP)) == NULL)
5767 5759 return (DDI_PROP_NO_MEMORY);
5768 5760 *(caddr_t *)valuep = buffer; /* set callers buf */
5769 5761 break;
5770 5762
5771 5763 case PROP_LEN_AND_VAL_BUF:
5772 5764 /* the length of the prop and the request must match */
5773 5765 if (callers_length != *lengthp)
5774 5766 return (DDI_PROP_INVAL_ARG);
5775 5767 buffer = valuep; /* get callers buf */
5776 5768 break;
5777 5769
5778 5770 default:
5779 5771 return (DDI_PROP_INVAL_ARG);
5780 5772 }
5781 5773
5782 5774 /* transfer the value into the buffer */
5783 5775 switch (dp) {
5784 5776 case DP_NBLOCKS:
5785 5777 *((uint64_t *)buffer) = capacity;
5786 5778 break;
5787 5779 case DP_BLKSIZE:
5788 5780 *((uint32_t *)buffer) = lbasize;
5789 5781 break;
5790 5782 case DP_SSD:
5791 5783 if (DK_TG_GETATTRIBUTE(cl, &tgattr, tg_cookie) != 0)
5792 5784 tgattr.media_is_solid_state = B_FALSE;
5793 5785 *((uint32_t *)buffer) =
5794 5786 tgattr.media_is_solid_state ? 1 : 0;
5795 5787 break;
5796 5788 case DP_ROT:
5797 5789 if (DK_TG_GETATTRIBUTE(cl, &tgattr, tg_cookie) != 0)
5798 5790 tgattr.media_is_rotational = B_TRUE;
5799 5791 *((uint32_t *)buffer) =
5800 5792 tgattr.media_is_rotational ? 1 : 0;
5801 5793 break;
5802 5794 }
5803 5795 return (DDI_PROP_SUCCESS);
5804 5796 }
5805 5797
5806 5798 /*
5807 5799 * Support dynamic size oriented properties of partition. Requests
5808 5800 * issued under conditions where size is valid are passed to
5809 5801 * ddi_prop_op_nblocks with the size information, otherwise the
5810 5802 * request is passed to ddi_prop_op. Size depends on valid geometry.
5811 5803 */
5812 5804 if (!cmlb_is_valid(cmlbhandle))
5813 5805 goto fallback;
5814 5806
5815 5807 /* Get partition nblocks value. */
5816 5808 (void) cmlb_partinfo(cmlbhandle, part,
5817 5809 (diskaddr_t *)&nblocks64, NULL, NULL, NULL, tg_cookie);
5818 5810
5819 5811 /*
5820 5812 * Assume partition information is in sys_blocksize units, compute
5821 5813 * divisor for size(9P) property representation.
5822 5814 */
5823 5815 dblk = lbasize / cl->cl_sys_blocksize;
5824 5816
5825 5817 /* Now let ddi_prop_op_nblocks_blksize() handle the request. */
5826 5818 return (ddi_prop_op_nblocks_blksize(dev, dip, prop_op, mod_flags,
5827 5819 name, valuep, lengthp, nblocks64 / dblk, lbasize));
5828 5820 }
↓ open down ↓ |
634 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX