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