1 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License, Version 1.0 only 7 * (the "License"). You may not use this file except in compliance 8 * with the License. 9 * 10 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 11 * or http://www.opensolaris.org/os/licensing. 12 * See the License for the specific language governing permissions 13 * and limitations under the License. 14 * 15 * When distributing Covered Code, include this CDDL HEADER in each 16 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 17 * If applicable, add the following below this CDDL HEADER, with the 18 * fields enclosed by brackets "[]" replaced with your own identifying 19 * information: Portions Copyright [yyyy] [name of copyright owner] 20 * 21 * CDDL HEADER END 22 */ 23 /* 24 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 25 * Use is subject to license terms. 26 */ 27 28 /* Copyright (c) 1988 AT&T */ 29 /* All Rights Reserved */ 30 31 #pragma ident "%Z%%M% %I% %E% SMI" 32 33 #include <sys/types.h> 34 #include <sys/ddi.h> 35 #include <sys/errno.h> 36 #include <sys/param.h> 37 #include <sys/kernel.h> 38 #include <sys/kmem.h> 39 #include <sys/cmn_err.h> 40 #include <sys/namei.h> 41 #include <sys/stat.h> 42 #include <sys/vfs_syscalls.h> 43 44 __strong_alias(ddi_strtol,ddi_strtoul) 45 46 /* 47 * String to integer conversion routines. 48 * 49 * This file is derived from usr/src/common/util/strtol.c 50 * 51 * We cannot use the user land versions as there is no errno to report 52 * error in kernel. So the return value is used to return an error, 53 * and the result is stored in an extra parameter passed by reference. 54 * Otherwise, the following functions are identical to the user land 55 * versions. 56 */ 57 58 /* 59 * We should have a kernel version of ctype.h. 60 */ 61 #define isalnum(ch) (isalpha(ch) || isdigit(ch)) 62 #define isalpha(ch) (isupper(ch) || islower(ch)) 63 #define isdigit(ch) ((ch) >= '0' && (ch) <= '9') 64 #define islower(ch) ((ch) >= 'a' && (ch) <= 'z') 65 #define isspace(ch) (((ch) == ' ') || ((ch) == '\r') || ((ch) == '\n') || \ 66 ((ch) == '\t') || ((ch) == '\f')) 67 #define isupper(ch) ((ch) >= 'A' && (ch) <= 'Z') 68 #define isxdigit(ch) (isdigit(ch) || ((ch) >= 'a' && (ch) <= 'f') || \ 69 ((ch) >= 'A' && (ch) <= 'F')) 70 71 #define DIGIT(x) \ 72 (isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A') 73 74 #define MBASE ('z' - 'a' + 1 + 10) 75 76 /* 77 * The following macro is a local version of isalnum() which limits 78 * alphabetic characters to the ranges a-z and A-Z; locale dependent 79 * characters will not return 1. The members of a-z and A-Z are 80 * assumed to be in ascending order and contiguous 81 */ 82 #define lisalnum(x) \ 83 (isdigit(x) || ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z')) 84 85 static int 86 do_mkdirp(const char *path) 87 { 88 struct lwp *l = curlwp; 89 int mode; 90 int error; 91 register_t ret; 92 93 const char *s, *e; 94 char *here; 95 96 error = 0; 97 mode = 493; 98 99 if (*path != '/') 100 panic("Not an absolute path"); 101 102 here = PNBUF_GET(); 103 for (s = path;; s = e) { 104 e = strchr(s + 1, '/'); 105 if (e == NULL) 106 break; 107 108 strlcpy(here, path, e - path + 1); 109 error = do_sys_mkdir((const char *)here, mode, UIO_SYSSPACE); 110 } 111 PNBUF_PUT(here); 112 113 if (error == EEXIST) 114 error = 0; 115 116 return error; 117 } 118 119 int 120 ddi_strtoul(const char *str, char **nptr, int base, unsigned long *result) 121 { 122 unsigned long val; 123 int c; 124 int xx; 125 unsigned long multmax; 126 int neg = 0; 127 const char **ptr = (const char **)nptr; 128 const unsigned char *ustr = (const unsigned char *)str; 129 130 if (ptr != (const char **)0) 131 *ptr = (char *)ustr; /* in case no number is formed */ 132 if (base < 0 || base > MBASE || base == 1) { 133 /* base is invalid -- should be a fatal error */ 134 return (EINVAL); 135 } 136 if (!isalnum(c = *ustr)) { 137 while (isspace(c)) 138 c = *++ustr; 139 switch (c) { 140 case '-': 141 neg++; 142 /* FALLTHROUGH */ 143 case '+': 144 c = *++ustr; 145 } 146 } 147 if (base == 0) 148 if (c != '0') 149 base = 10; 150 else if (ustr[1] == 'x' || ustr[1] == 'X') 151 base = 16; 152 else 153 base = 8; 154 /* 155 * for any base > 10, the digits incrementally following 156 * 9 are assumed to be "abc...z" or "ABC...Z" 157 */ 158 if (!lisalnum(c) || (xx = DIGIT(c)) >= base) 159 return (EINVAL); /* no number formed */ 160 if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') && 161 isxdigit(ustr[2])) 162 c = *(ustr += 2); /* skip over leading "0x" or "0X" */ 163 164 multmax = ULONG_MAX / (unsigned long)base; 165 val = DIGIT(c); 166 for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) { 167 if (val > multmax) 168 goto overflow; 169 val *= base; 170 if (ULONG_MAX - val < xx) 171 goto overflow; 172 val += xx; 173 c = *++ustr; 174 } 175 if (ptr != (const char **)0) 176 *ptr = (char *)ustr; 177 *result = neg ? -val : val; 178 return (0); 179 180 overflow: 181 for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr)) 182 ; 183 if (ptr != (const char **)0) 184 *ptr = (char *)ustr; 185 return (ERANGE); 186 } 187 188 /* 189 * Find first bit set in a mask (returned counting from 1 up) 190 */ 191 192 int 193 ddi_ffs(long mask) 194 { 195 return (ffs(mask)); 196 } 197 198 /* 199 * Find last bit set. Take mask and clear 200 * all but the most significant bit, and 201 * then let ffs do the rest of the work. 202 * 203 * Algorithm courtesy of Steve Chessin. 204 */ 205 206 int 207 ddi_fls(long mask) 208 { 209 while (mask) { 210 long nx; 211 212 if ((nx = (mask & (mask - 1))) == 0) 213 break; 214 mask = nx; 215 } 216 return (ffs(mask)); 217 } 218 219 /* 220 * The next five routines comprise generic storage management utilities 221 * for driver soft state structures (in "the old days," this was done 222 * with a statically sized array - big systems and dynamic loading 223 * and unloading make heap allocation more attractive) 224 */ 225 226 /* 227 * Allocate a set of pointers to 'n_items' objects of size 'size' 228 * bytes. Each pointer is initialized to nil. 229 * 230 * The 'size' and 'n_items' values are stashed in the opaque 231 * handle returned to the caller. 232 * 233 * This implementation interprets 'set of pointers' to mean 'array 234 * of pointers' but note that nothing in the interface definition 235 * precludes an implementation that uses, for example, a linked list. 236 * However there should be a small efficiency gain from using an array 237 * at lookup time. 238 * 239 * NOTE As an optimization, we make our growable array allocations in 240 * powers of two (bytes), since that's how much kmem_alloc (currently) 241 * gives us anyway. It should save us some free/realloc's .. 242 * 243 * As a further optimization, we make the growable array start out 244 * with MIN_N_ITEMS in it. 245 */ 246 247 /* 248 * This data structure is entirely private to the soft state allocator. 249 */ 250 struct i_ddi_soft_state { 251 void **array; /* the array of pointers */ 252 kmutex_t lock; /* serialize access to this struct */ 253 size_t size; /* how many bytes per state struct */ 254 size_t n_items; /* how many structs herein */ 255 struct i_ddi_soft_state *next; /* 'dirty' elements */ 256 }; 257 258 #define MIN_N_ITEMS 8 /* 8 void *'s == 32 bytes */ 259 260 int 261 ddi_soft_state_init(void **state_p, size_t size, size_t n_items) 262 { 263 struct i_ddi_soft_state *ss; 264 265 if (state_p == NULL || *state_p != NULL || size == 0) 266 return (EINVAL); 267 268 ss = kmem_zalloc(sizeof (*ss), KM_SLEEP); 269 mutex_init(&ss->lock, NULL, MUTEX_DRIVER, NULL); 270 ss->size = size; 271 272 if (n_items < MIN_N_ITEMS) 273 ss->n_items = MIN_N_ITEMS; 274 else { 275 int bitlog; 276 277 if ((bitlog = ddi_fls(n_items)) == ddi_ffs(n_items)) 278 bitlog--; 279 ss->n_items = 1 << bitlog; 280 } 281 282 ASSERT(ss->n_items >= n_items); 283 284 ss->array = kmem_zalloc(ss->n_items * sizeof (void *), KM_SLEEP); 285 286 *state_p = ss; 287 288 return (0); 289 } 290 291 292 /* 293 * Allocate a state structure of size 'size' to be associated 294 * with item 'item'. 295 * 296 * In this implementation, the array is extended to 297 * allow the requested offset, if needed. 298 */ 299 int 300 ddi_soft_state_zalloc(void *state, int item) 301 { 302 struct i_ddi_soft_state *ss; 303 void **array; 304 void *new_element; 305 306 if ((ss = state) == NULL || item < 0) 307 return (DDI_FAILURE); 308 309 mutex_enter(&ss->lock); 310 if (ss->size == 0) { 311 mutex_exit(&ss->lock); 312 cmn_err(CE_WARN, "ddi_soft_state_zalloc: bad handle"); 313 return (DDI_FAILURE); 314 } 315 316 array = ss->array; /* NULL if ss->n_items == 0 */ 317 ASSERT(ss->n_items != 0 && array != NULL); 318 319 /* 320 * refuse to tread on an existing element 321 */ 322 if (item < ss->n_items && array[item] != NULL) { 323 mutex_exit(&ss->lock); 324 return (DDI_FAILURE); 325 } 326 327 /* 328 * Allocate a new element to plug in 329 */ 330 new_element = kmem_zalloc(ss->size, KM_SLEEP); 331 332 /* 333 * Check if the array is big enough, if not, grow it. 334 */ 335 if (item >= ss->n_items) { 336 void **new_array; 337 size_t new_n_items; 338 struct i_ddi_soft_state *dirty; 339 340 /* 341 * Allocate a new array of the right length, copy 342 * all the old pointers to the new array, then 343 * if it exists at all, put the old array on the 344 * dirty list. 345 * 346 * Note that we can't kmem_free() the old array. 347 * 348 * Why -- well the 'get' operation is 'mutex-free', so we 349 * can't easily catch a suspended thread that is just about 350 * to dereference the array we just grew out of. So we 351 * cons up a header and put it on a list of 'dirty' 352 * pointer arrays. (Dirty in the sense that there may 353 * be suspended threads somewhere that are in the middle 354 * of referencing them). Fortunately, we -can- garbage 355 * collect it all at ddi_soft_state_fini time. 356 */ 357 new_n_items = ss->n_items; 358 while (new_n_items < (1 + item)) 359 new_n_items <<= 1; /* double array size .. */ 360 361 ASSERT(new_n_items >= (1 + item)); /* sanity check! */ 362 363 new_array = kmem_zalloc(new_n_items * sizeof (void *), 364 KM_SLEEP); 365 /* 366 * Copy the pointers into the new array 367 */ 368 bcopy(array, new_array, ss->n_items * sizeof (void *)); 369 370 /* 371 * Save the old array on the dirty list 372 */ 373 dirty = kmem_zalloc(sizeof (*dirty), KM_SLEEP); 374 dirty->array = ss->array; 375 dirty->n_items = ss->n_items; 376 dirty->next = ss->next; 377 ss->next = dirty; 378 379 ss->array = (array = new_array); 380 ss->n_items = new_n_items; 381 } 382 383 ASSERT(array != NULL && item < ss->n_items && array[item] == NULL); 384 385 array[item] = new_element; 386 387 mutex_exit(&ss->lock); 388 return (DDI_SUCCESS); 389 } 390 391 392 /* 393 * Fetch a pointer to the allocated soft state structure. 394 * 395 * This is designed to be cheap. 396 * 397 * There's an argument that there should be more checking for 398 * nil pointers and out of bounds on the array.. but we do a lot 399 * of that in the alloc/free routines. 400 * 401 * An array has the convenience that we don't need to lock read-access 402 * to it c.f. a linked list. However our "expanding array" strategy 403 * means that we should hold a readers lock on the i_ddi_soft_state 404 * structure. 405 * 406 * However, from a performance viewpoint, we need to do it without 407 * any locks at all -- this also makes it a leaf routine. The algorithm 408 * is 'lock-free' because we only discard the pointer arrays at 409 * ddi_soft_state_fini() time. 410 */ 411 void * 412 ddi_get_soft_state(void *state, int item) 413 { 414 struct i_ddi_soft_state *ss = state; 415 416 ASSERT(ss != NULL && item >= 0); 417 418 if (item < ss->n_items && ss->array != NULL) 419 return (ss->array[item]); 420 return (NULL); 421 } 422 423 /* 424 * Free the state structure corresponding to 'item.' Freeing an 425 * element that has either gone or was never allocated is not 426 * considered an error. Note that we free the state structure, but 427 * we don't shrink our pointer array, or discard 'dirty' arrays, 428 * since even a few pointers don't really waste too much memory. 429 * 430 * Passing an item number that is out of bounds, or a null pointer will 431 * provoke an error message. 432 */ 433 void 434 ddi_soft_state_free(void *state, int item) 435 { 436 struct i_ddi_soft_state *ss; 437 void **array; 438 void *element; 439 static char msg[] = "ddi_soft_state_free:"; 440 441 if ((ss = state) == NULL) { 442 cmn_err(CE_WARN, "%s null handle", 443 msg); 444 return; 445 } 446 447 element = NULL; 448 449 mutex_enter(&ss->lock); 450 451 if ((array = ss->array) == NULL || ss->size == 0) { 452 cmn_err(CE_WARN, "%s bad handle", 453 msg); 454 } else if (item < 0 || item >= ss->n_items) { 455 cmn_err(CE_WARN, "%s item %d not in range [0..%lu]", 456 msg, item, ss->n_items - 1); 457 } else if (array[item] != NULL) { 458 element = array[item]; 459 array[item] = NULL; 460 } 461 462 mutex_exit(&ss->lock); 463 464 if (element) 465 kmem_free(element, ss->size); 466 } 467 468 469 /* 470 * Free the entire set of pointers, and any 471 * soft state structures contained therein. 472 * 473 * Note that we don't grab the ss->lock mutex, even though 474 * we're inspecting the various fields of the data structure. 475 * 476 * There is an implicit assumption that this routine will 477 * never run concurrently with any of the above on this 478 * particular state structure i.e. by the time the driver 479 * calls this routine, there should be no other threads 480 * running in the driver. 481 */ 482 void 483 ddi_soft_state_fini(void **state_p) 484 { 485 struct i_ddi_soft_state *ss, *dirty; 486 int item; 487 static char msg[] = "ddi_soft_state_fini:"; 488 489 if (state_p == NULL || (ss = *state_p) == NULL) { 490 cmn_err(CE_WARN, "%s null handle", 491 msg); 492 return; 493 } 494 495 if (ss->size == 0) { 496 cmn_err(CE_WARN, "%s bad handle", 497 msg); 498 return; 499 } 500 501 if (ss->n_items > 0) { 502 for (item = 0; item < ss->n_items; item++) 503 ddi_soft_state_free(ss, item); 504 kmem_free(ss->array, ss->n_items * sizeof (void *)); 505 } 506 507 /* 508 * Now delete any dirty arrays from previous 'grow' operations 509 */ 510 for (dirty = ss->next; dirty; dirty = ss->next) { 511 ss->next = dirty->next; 512 kmem_free(dirty->array, dirty->n_items * sizeof (void *)); 513 kmem_free(dirty, sizeof (*dirty)); 514 } 515 516 mutex_destroy(&ss->lock); 517 kmem_free(ss, sizeof (*ss)); 518 519 *state_p = NULL; 520 } 521 522 int 523 ddi_create_minor_node(dev_info_t *dip, char *name, int spec_type, 524 minor_t minor_num, char *node_type, int flag) 525 { 526 struct lwp *l = curlwp; 527 char *pn; 528 dev_t dev; 529 int error; 530 register_t ret; 531 532 printf("ddi_create_minor_node: name %s\n", name); 533 534 dev = makedev(flag, minor_num); 535 536 pn = PNBUF_GET(); 537 if (spec_type == S_IFCHR) 538 snprintf(pn, MAXPATHLEN, "/dev/zvol/rdsk/%s", name); 539 else 540 snprintf(pn, MAXPATHLEN, "/dev/zvol/dsk/%s", name); 541 542 if ((error = do_mkdirp(pn)) != 0) 543 goto exit; 544 545 error = do_sys_mknod(l, (const char *)pn, spec_type, dev, &ret, UIO_SYSSPACE); 546 547 exit: 548 PNBUF_PUT(pn); 549 550 return error; 551 } 552 553 void 554 ddi_remove_minor_node(dev_info_t *dip, char *name) 555 { 556 char *pn; 557 int error; 558 559 pn = PNBUF_GET(); 560 snprintf(pn, MAXPATHLEN, "/dev/zvol/dsk/%s", name); 561 (void)do_sys_unlink(pn, UIO_SYSSPACE); 562 PNBUF_PUT(pn); 563 564 /* We need to remove raw and block device nodes */ 565 pn = PNBUF_GET(); 566 snprintf(pn, MAXPATHLEN, "/dev/zvol/rdsk/%s", name); 567 (void)do_sys_unlink(pn, UIO_SYSSPACE); 568 PNBUF_PUT(pn); 569 } 570 571 clock_t 572 ddi_get_lbolt() 573 { 574 575 return hardclock_ticks; 576 } 577 578 int64_t 579 ddi_get_lbolt64() 580 { 581 582 return hardclock_ticks; 583 } 584