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