FEATURES: add notes about compilation env changes
[unleashed.git] / kernel / net / mi.c
blob9fe77e88c40b4e900428b3dafd7a79feabb9977c
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 /* Copyright (c) 1990 Mentat Inc. */
27 #include <sys/types.h>
28 #include <inet/common.h> /* for various inet/mi.h and inet/nd.h needs */
29 #include <sys/stream.h>
30 #include <sys/stropts.h>
31 #include <sys/strsun.h>
32 #include <sys/sysmacros.h>
33 #include <inet/nd.h>
34 #include <inet/mi.h>
35 #define _SUN_TPI_VERSION 2
36 #include <sys/tihdr.h>
37 #include <sys/timod.h>
38 #include <sys/vtrace.h>
39 #include <sys/kmem.h>
40 #include <sys/mkdev.h>
41 #include <sys/strlog.h>
42 #include <sys/ddi.h>
43 #include <sys/suntpi.h>
44 #include <sys/cmn_err.h>
45 #include <sys/debug.h>
46 #include <sys/kobj.h>
47 #include <sys/stropts.h>
48 #include <sys/strsubr.h>
49 #include <inet/proto_set.h>
51 #define ISDIGIT(ch) ((ch) >= '0' && (ch) <= '9')
52 #define ISUPPER(ch) ((ch) >= 'A' && (ch) <= 'Z')
53 #define tolower(ch) ('a' + ((ch) - 'A'))
55 #define MI_IS_TRANSPARENT(mp) (mp->b_cont && \
56 (mp->b_cont->b_rptr != mp->b_cont->b_wptr))
59 * NOTE: Whenever anything is allocated by mi_alloc or mi_alloc_sleep (below),
60 * the size of the requested allocation is increased by one word. This extra
61 * word is used to store the size of the object being allocated, and is located
62 * at the beginning of the allocated block. The pointer returned to the caller
63 * is a pointer to the *second* word in the newly-allocated block. The IP
64 * module of mdb is aware of this, and will need to be changed if this
65 * allocation strategy is changed.
68 typedef struct stroptions *STROPTP;
69 typedef union T_primitives *TPRIMP;
71 /* Timer block states. */
72 #define TB_RUNNING 1
73 #define TB_IDLE 2
75 * Could not stop/free before putq
77 #define TB_RESCHED 3 /* mtb_time_left contains tick count */
78 #define TB_CANCELLED 4
79 #define TB_TO_BE_FREED 5
81 typedef struct mtb_s {
82 int mtb_state;
83 timeout_id_t mtb_tid;
84 queue_t *mtb_q;
85 MBLKP mtb_mp;
86 clock_t mtb_time_left;
87 } MTB, *MTBP;
89 static int mi_timer_fire(MTBP);
90 static int mi_iprintf(char *, va_list, pfi_t, char *);
91 static void mi_tpi_addr_and_opt(MBLKP, char *, t_scalar_t, char *, t_scalar_t);
92 static MBLKP mi_tpi_trailer_alloc(MBLKP, size_t, t_scalar_t);
94 /* ARGSUSED1 */
95 void *
96 mi_alloc(size_t size, uint_t pri)
98 size_t *ptr;
100 size += sizeof (size);
101 if (ptr = kmem_alloc(size, KM_NOSLEEP)) {
102 *ptr = size;
103 return (ptr + 1);
105 return (NULL);
108 /* ARGSUSED1 */
109 void *
110 mi_alloc_sleep(size_t size, uint_t pri)
112 size_t *ptr;
114 size += sizeof (size);
115 ptr = kmem_alloc(size, KM_SLEEP);
116 *ptr = size;
117 return (ptr + 1);
121 mi_close_comm(void **mi_headp, queue_t *q)
123 IDP ptr;
125 ptr = q->q_ptr;
126 mi_close_unlink(mi_headp, ptr);
127 mi_close_free(ptr);
128 q->q_ptr = WR(q)->q_ptr = NULL;
129 return (0);
132 void
133 mi_close_unlink(void **mi_headp, IDP ptr)
135 mi_head_t *mi_head = *(mi_head_t **)mi_headp;
136 MI_OP mi_o;
137 dev_t dev;
139 mi_o = (MI_OP)ptr;
140 if (!mi_o)
141 return;
142 mi_o--;
144 if (mi_o->mi_o_next == NULL) {
145 /* Not in list */
146 ASSERT(mi_o->mi_o_prev == NULL);
147 return;
150 /* Free minor number */
151 dev = mi_o->mi_o_dev;
152 if ((dev != OPENFAIL) && (dev != 0) && (dev <= MAXMIN))
153 inet_minor_free(mi_head->mh_arena, dev);
155 /* Unlink from list */
156 ASSERT(mi_o->mi_o_next != NULL);
157 ASSERT(mi_o->mi_o_prev != NULL);
158 ASSERT(mi_o->mi_o_next->mi_o_prev == mi_o);
159 ASSERT(mi_o->mi_o_prev->mi_o_next == mi_o);
161 mi_o->mi_o_next->mi_o_prev = mi_o->mi_o_prev;
162 mi_o->mi_o_prev->mi_o_next = mi_o->mi_o_next;
163 mi_o->mi_o_next = mi_o->mi_o_prev = NULL;
165 mi_o->mi_o_dev = (dev_t)OPENFAIL;
167 /* If list now empty free the list head */
168 if (mi_head->mh_o.mi_o_next == &mi_head->mh_o) {
169 ASSERT(mi_head->mh_o.mi_o_prev == &mi_head->mh_o);
170 if (mi_head->mh_arena != NULL)
171 inet_minor_destroy(mi_head->mh_arena);
172 mi_free((IDP)mi_head);
173 *mi_headp = NULL;
177 void
178 mi_close_free(IDP ptr)
180 MI_OP mi_o;
182 mi_o = (MI_OP)ptr;
183 if (!mi_o)
184 return;
185 mi_o--;
187 ASSERT(mi_o->mi_o_next == NULL && mi_o->mi_o_prev == NULL);
188 mi_free((IDP)mi_o);
192 * mi_copyin - takes care of transparent or non-transparent ioctl for the
193 * calling function so that they have to deal with just M_IOCDATA type
194 * and not worry about M_COPYIN.
196 * mi_copyin checks to see if the ioctl is transparent or non transparent.
197 * In case of a non_transparent ioctl, it packs the data into a M_IOCDATA
198 * message and puts it back onto the current queue for further processing.
199 * In case of transparent ioctl, it sends a M_COPYIN message up to the
200 * streamhead so that a M_IOCDATA with the information comes back down.
202 void
203 mi_copyin(queue_t *q, MBLKP mp, char *uaddr, size_t len)
205 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
206 struct copyreq *cq = (struct copyreq *)mp->b_rptr;
207 struct copyresp *cp = (struct copyresp *)mp->b_rptr;
208 int err;
209 MBLKP mp1;
211 ASSERT(mp->b_datap->db_type == M_IOCTL && !uaddr);
213 /* A transparent ioctl. Send a M_COPYIN message to the streamhead. */
214 if (iocp->ioc_count == TRANSPARENT) {
215 MI_COPY_COUNT(mp) = 1;
216 MI_COPY_DIRECTION(mp) = MI_COPY_IN;
217 cq->cq_private = mp->b_cont;
218 cq->cq_size = len;
219 cq->cq_flag = 0;
220 bcopy(mp->b_cont->b_rptr, &cq->cq_addr, sizeof (cq->cq_addr));
221 mp->b_cont = NULL;
222 mp->b_datap->db_type = M_COPYIN;
223 qreply(q, mp);
224 return;
228 * A non-transparent ioctl. Need to convert into M_IOCDATA message.
230 * We allocate a 0 byte message block and put its address in
231 * cp_private. It also makes the b_prev field = 1 and b_next
232 * field = MI_COPY_IN for this 0 byte block. This is done to
233 * maintain compatibility with old code in mi_copy_state
234 * (which removes the empty block).
236 err = miocpullup(mp, len);
237 if (err != 0)
238 goto err_ret;
240 mp1 = allocb(0, BPRI_MED);
241 if (mp1 == NULL) {
242 err = ENOMEM;
243 goto err_ret;
247 * Temporarily insert mp1 between the M_IOCTL and M_DATA blocks so
248 * that we can use the MI_COPY_COUNT & MI_COPY_DIRECTION macros.
250 mp1->b_cont = mp->b_cont;
251 mp->b_cont = mp1;
252 MI_COPY_COUNT(mp) = 1;
253 MI_COPY_DIRECTION(mp) = MI_COPY_IN;
254 mp->b_cont = mp1->b_cont;
255 mp1->b_cont = NULL;
258 * Leave a pointer to the 0 byte block in cp_private field for
259 * future use by the mi_copy_* routines.
261 mp->b_datap->db_type = M_IOCDATA;
262 cp->cp_private = mp1;
263 cp->cp_rval = NULL;
264 put(q, mp);
265 return;
267 err_ret:
268 iocp->ioc_error = err;
269 iocp->ioc_count = 0;
270 if (mp->b_cont) {
271 freemsg(mp->b_cont);
272 mp->b_cont = NULL;
274 mp->b_datap->db_type = M_IOCACK;
275 qreply(q, mp);
279 * Allows transparent IOCTLs to have multiple copyins. This is needed
280 * for some variable-length structures, where the total size is only known
281 * after the first part is copied in. Rather than setting MI_COPY_COUNT to
282 * 1, as in mi_coypin(), it is simply incremented here. This value can
283 * then be checked in the returned IOCBLK.
285 * As this deals with copyins that follow the initial copyin, the byte
286 * offset into the user buffer from which copying should begin must be
287 * passed in in the offset parameter.
289 * Unlike mi_coypin(), this function expects to be passed an mblk chain
290 * headed by an M_IOCBLK, as that's the chain that will be in use for
291 * copies after the first one (copies where n != 1).
293 void
294 mi_copyin_n(queue_t *q, MBLKP mp, size_t offset, size_t len)
296 struct copyreq *cq = (struct copyreq *)mp->b_rptr;
298 ASSERT(mp->b_datap->db_type == M_IOCDATA);
300 MI_COPY_COUNT(mp)++;
301 MI_COPY_DIRECTION(mp) = MI_COPY_IN;
302 cq->cq_private = mp->b_cont;
303 cq->cq_size = len;
304 cq->cq_flag = 0;
305 bcopy(mp->b_cont->b_rptr, &cq->cq_addr, sizeof (cq->cq_addr));
306 cq->cq_addr += offset;
307 mp->b_cont = NULL;
308 mp->b_datap->db_type = M_COPYIN;
309 qreply(q, mp);
312 void
313 mi_copyout(queue_t *q, MBLKP mp)
315 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
316 struct copyreq *cq = (struct copyreq *)iocp;
317 struct copyresp *cp = (struct copyresp *)cq;
318 MBLKP mp1;
319 MBLKP mp2;
321 if (mp->b_datap->db_type != M_IOCDATA || !mp->b_cont) {
322 mi_copy_done(q, mp, EPROTO);
323 return;
325 /* Check completion of previous copyout operation. */
326 mp1 = mp->b_cont;
327 if ((int)(uintptr_t)cp->cp_rval || !mp1->b_cont) {
328 mi_copy_done(q, mp, (int)(uintptr_t)cp->cp_rval);
329 return;
331 if (!mp1->b_cont->b_cont && !MI_IS_TRANSPARENT(mp)) {
332 mp1->b_next = NULL;
333 mp1->b_prev = NULL;
334 mp->b_cont = mp1->b_cont;
335 freeb(mp1);
336 mp1 = mp->b_cont;
337 mp1->b_next = NULL;
338 mp1->b_prev = NULL;
339 iocp->ioc_count = mp1->b_wptr - mp1->b_rptr;
340 iocp->ioc_error = 0;
341 mp->b_datap->db_type = M_IOCACK;
342 qreply(q, mp);
343 return;
345 if (MI_COPY_DIRECTION(mp) == MI_COPY_IN) {
346 /* Set up for first copyout. */
347 MI_COPY_DIRECTION(mp) = MI_COPY_OUT;
348 MI_COPY_COUNT(mp) = 1;
349 } else {
350 ++MI_COPY_COUNT(mp);
352 cq->cq_private = mp1;
353 /* Find message preceding last. */
354 for (mp2 = mp1; mp2->b_cont->b_cont; mp2 = mp2->b_cont)
356 if (mp2 == mp1)
357 bcopy((char *)mp1->b_rptr, (char *)&cq->cq_addr,
358 sizeof (cq->cq_addr));
359 else
360 cq->cq_addr = (char *)mp2->b_cont->b_next;
361 mp1 = mp2->b_cont;
362 mp->b_datap->db_type = M_COPYOUT;
363 mp->b_cont = mp1;
364 mp2->b_cont = NULL;
365 mp1->b_next = NULL;
366 cq->cq_size = mp1->b_wptr - mp1->b_rptr;
367 cq->cq_flag = 0;
368 qreply(q, mp);
371 MBLKP
372 mi_copyout_alloc(queue_t *q, MBLKP mp, char *uaddr, size_t len,
373 boolean_t free_on_error)
375 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
376 MBLKP mp1;
378 if (mp->b_datap->db_type == M_IOCTL) {
379 if (iocp->ioc_count != TRANSPARENT) {
380 mp1 = allocb(0, BPRI_MED);
381 if (mp1 == NULL) {
382 if (free_on_error) {
383 iocp->ioc_error = ENOMEM;
384 iocp->ioc_count = 0;
385 freemsg(mp->b_cont);
386 mp->b_cont = NULL;
387 mp->b_datap->db_type = M_IOCACK;
388 qreply(q, mp);
390 return (NULL);
392 mp1->b_cont = mp->b_cont;
393 mp->b_cont = mp1;
395 MI_COPY_COUNT(mp) = 0;
396 MI_COPY_DIRECTION(mp) = MI_COPY_OUT;
397 /* Make sure it looks clean to mi_copyout. */
398 mp->b_datap->db_type = M_IOCDATA;
399 ((struct copyresp *)iocp)->cp_rval = NULL;
401 mp1 = allocb(len, BPRI_MED);
402 if (mp1 == NULL) {
403 if (free_on_error)
404 mi_copy_done(q, mp, ENOMEM);
405 return (NULL);
407 linkb(mp, mp1);
408 mp1->b_next = (MBLKP)uaddr;
409 return (mp1);
412 void
413 mi_copy_done(queue_t *q, MBLKP mp, int err)
415 struct iocblk *iocp;
416 MBLKP mp1;
418 if (!mp)
419 return;
420 if (!q || (mp->b_wptr - mp->b_rptr) < sizeof (struct iocblk)) {
421 freemsg(mp);
422 return;
424 iocp = (struct iocblk *)mp->b_rptr;
425 mp->b_datap->db_type = M_IOCACK;
426 iocp->ioc_error = err;
428 iocp->ioc_count = 0;
429 if ((mp1 = mp->b_cont) != NULL) {
430 for (; mp1; mp1 = mp1->b_cont) {
431 mp1->b_prev = NULL;
432 mp1->b_next = NULL;
434 freemsg(mp->b_cont);
435 mp->b_cont = NULL;
437 qreply(q, mp);
441 mi_copy_state(queue_t *q, MBLKP mp, MBLKP *mpp)
443 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
444 struct copyresp *cp = (struct copyresp *)iocp;
445 MBLKP mp1;
447 mp1 = mp->b_cont;
448 mp->b_cont = cp->cp_private;
449 if (mp1) {
450 if (mp1->b_cont && !pullupmsg(mp1, -1)) {
451 mi_copy_done(q, mp, ENOMEM);
452 return (-1);
454 linkb(mp->b_cont, mp1);
456 if ((int)(uintptr_t)cp->cp_rval) {
457 mi_copy_done(q, mp, (int)(uintptr_t)cp->cp_rval);
458 return (-1);
460 if (mpp && MI_COPY_DIRECTION(mp) == MI_COPY_IN)
461 *mpp = mp1;
462 return (MI_COPY_STATE(mp));
465 void
466 mi_free(void *ptr)
468 size_t size;
470 if (!ptr)
471 return;
472 if ((size = ((size_t *)ptr)[-1]) <= 0)
473 cmn_err(CE_PANIC, "mi_free");
475 kmem_free((void *) ((size_t *)ptr - 1), size);
478 static int
479 mi_iprintf(char *fmt, va_list ap, pfi_t putc_func, char *cookie)
481 int base;
482 char buf[(sizeof (long) * 3) + 1];
483 static char hex_val[] = "0123456789abcdef";
484 int ch;
485 int count;
486 char *cp1;
487 int digits;
488 char *fcp;
489 boolean_t is_long;
490 ulong_t uval;
491 long val;
492 boolean_t zero_filled;
494 if (!fmt)
495 return (-1);
496 count = 0;
497 while (*fmt) {
498 if (*fmt != '%' || *++fmt == '%') {
499 count += (*putc_func)(cookie, *fmt++);
500 continue;
502 if (*fmt == '0') {
503 zero_filled = B_TRUE;
504 fmt++;
505 if (!*fmt)
506 break;
507 } else
508 zero_filled = B_FALSE;
509 base = 0;
510 for (digits = 0; ISDIGIT(*fmt); fmt++) {
511 digits *= 10;
512 digits += (*fmt - '0');
514 if (!*fmt)
515 break;
516 is_long = B_FALSE;
517 if (*fmt == 'l') {
518 is_long = B_TRUE;
519 fmt++;
521 if (!*fmt)
522 break;
523 ch = *fmt++;
524 if (ISUPPER(ch)) {
525 ch = tolower(ch);
526 is_long = B_TRUE;
528 switch (ch) {
529 case 'c':
530 count += (*putc_func)(cookie, va_arg(ap, int *));
531 continue;
532 case 'd':
533 base = 10;
534 break;
535 case 'm': /* Print out memory, 2 hex chars per byte */
536 if (is_long)
537 fcp = va_arg(ap, char *);
538 else {
539 if ((cp1 = va_arg(ap, char *)) != NULL)
540 fcp = (char *)cp1;
541 else
542 fcp = NULL;
544 if (!fcp) {
545 for (fcp = (char *)"(NULL)"; *fcp; fcp++)
546 count += (*putc_func)(cookie, *fcp);
547 } else {
548 while (digits--) {
549 int u1 = *fcp++ & 0xFF;
550 count += (*putc_func)(cookie,
551 hex_val[(u1>>4)& 0xF]);
552 count += (*putc_func)(cookie,
553 hex_val[u1& 0xF]);
556 continue;
557 case 'o':
558 base = 8;
559 break;
560 case 'p':
561 is_long = B_TRUE;
562 /* FALLTHRU */
563 case 'x':
564 base = 16;
565 break;
566 case 's':
567 if (is_long)
568 fcp = va_arg(ap, char *);
569 else {
570 if ((cp1 = va_arg(ap, char *)) != NULL)
571 fcp = (char *)cp1;
572 else
573 fcp = NULL;
575 if (!fcp)
576 fcp = (char *)"(NULL)";
577 while (*fcp) {
578 count += (*putc_func)(cookie, *fcp++);
579 if (digits && --digits == 0)
580 break;
582 while (digits > 0) {
583 count += (*putc_func)(cookie, ' ');
584 digits--;
586 continue;
587 case 'u':
588 base = 10;
589 break;
590 default:
591 return (count);
593 if (is_long)
594 val = va_arg(ap, long);
595 else
596 val = va_arg(ap, int);
597 if (base == 10 && ch != 'u') {
598 if (val < 0) {
599 count += (*putc_func)(cookie, '-');
600 val = -val;
602 uval = val;
603 } else {
604 if (is_long)
605 uval = val;
606 else
607 uval = (uint_t)val;
609 /* Hand overload/restore the register variable 'fmt' */
610 cp1 = fmt;
611 fmt = A_END(buf);
612 *--fmt = '\0';
613 do {
614 if (fmt > buf)
615 *--fmt = hex_val[uval % base];
616 if (digits && --digits == 0)
617 break;
618 } while (uval /= base);
619 if (zero_filled) {
620 while (digits > 0 && fmt > buf) {
621 *--fmt = '0';
622 digits--;
625 while (*fmt)
626 count += (*putc_func)(cookie, *fmt++);
627 fmt = cp1;
629 return (count);
632 /* PRINTFLIKE2 */
634 mi_mpprintf(MBLKP mp, char *fmt, ...)
636 va_list ap;
637 int count = -1;
639 va_start(ap, fmt);
640 if (mp) {
641 count = mi_iprintf(fmt, ap, (pfi_t)mi_mpprintf_putc,
642 (char *)mp);
643 if (count != -1)
644 (void) mi_mpprintf_putc((char *)mp, '\0');
646 va_end(ap);
647 return (count);
650 /* PRINTFLIKE2 */
652 mi_mpprintf_nr(MBLKP mp, char *fmt, ...)
654 va_list ap;
655 int count = -1;
657 va_start(ap, fmt);
658 if (mp) {
659 (void) adjmsg(mp, -1);
660 count = mi_iprintf(fmt, ap, (pfi_t)mi_mpprintf_putc,
661 (char *)mp);
662 if (count != -1)
663 (void) mi_mpprintf_putc((char *)mp, '\0');
665 va_end(ap);
666 return (count);
670 mi_mpprintf_putc(char *cookie, int ch)
672 MBLKP mp = (MBLKP)cookie;
674 while (mp->b_cont)
675 mp = mp->b_cont;
676 if (mp->b_wptr >= mp->b_datap->db_lim) {
677 mp->b_cont = allocb(1024, BPRI_HI);
678 mp = mp->b_cont;
679 if (!mp)
680 return (0);
682 *mp->b_wptr++ = (unsigned char)ch;
683 return (1);
687 mi_first_ptr(void **mi_headp)
689 mi_head_t *mi_head = *(mi_head_t **)mi_headp;
690 MI_OP mi_op;
692 mi_op = mi_head->mh_o.mi_o_next;
693 if (mi_op && mi_op != &mi_head->mh_o)
694 return ((IDP)&mi_op[1]);
695 return (NULL);
699 * Clients can choose to have both module instances and device instances
700 * in the same list. Return the first device instance in the list.
703 mi_first_dev_ptr(void **mi_headp)
705 mi_head_t *mi_head = *(mi_head_t **)mi_headp;
706 MI_OP mi_op;
708 mi_op = mi_head->mh_o.mi_o_next;
709 while ((mi_op != NULL) && (mi_op != &mi_head->mh_o)) {
710 if (mi_op->mi_o_isdev)
711 return ((IDP)&mi_op[1]);
712 mi_op = mi_op->mi_o_next;
714 return (NULL);
718 mi_next_ptr(void **mi_headp, IDP ptr)
720 mi_head_t *mi_head = *(mi_head_t **)mi_headp;
721 MI_OP mi_op = ((MI_OP)ptr) - 1;
723 if ((mi_op = mi_op->mi_o_next) != NULL && mi_op != &mi_head->mh_o)
724 return ((IDP)&mi_op[1]);
725 return (NULL);
729 * Clients can choose to have both module instances and device instances
730 * in the same list. Return the next device instance in the list.
733 mi_next_dev_ptr(void **mi_headp, IDP ptr)
735 mi_head_t *mi_head = *(mi_head_t **)mi_headp;
736 MI_OP mi_op = ((MI_OP)ptr) - 1;
738 mi_op = mi_op->mi_o_next;
739 while ((mi_op != NULL) && (mi_op != &mi_head->mh_o)) {
740 if (mi_op->mi_o_isdev)
741 return ((IDP)&mi_op[1]);
742 mi_op = mi_op->mi_o_next;
744 return (NULL);
748 * Self clone the device
749 * XXX - should we still support clone device
751 /* ARGSUSED4 */
753 mi_open_comm(void **mi_headp, size_t size, queue_t *q, dev_t *devp,
754 int flag, int sflag, cred_t *credp)
756 int error;
757 IDP ptr;
759 if (q->q_ptr != NULL)
760 return (0);
762 ptr = mi_open_alloc_sleep(size);
763 q->q_ptr = WR(q)->q_ptr = ptr;
764 error = mi_open_link(mi_headp, ptr, devp, flag, sflag, credp);
765 if (error != 0) {
766 q->q_ptr = WR(q)->q_ptr = NULL;
767 mi_close_free(ptr);
769 return (error);
773 mi_open_alloc_sleep(size_t size)
775 MI_OP mi_o;
777 if (size > (UINT_MAX - sizeof (MI_O)))
778 return (NULL);
780 mi_o = (MI_OP)mi_zalloc_sleep(size + sizeof (MI_O));
781 mi_o++;
782 return ((IDP)mi_o);
786 mi_open_alloc(size_t size)
788 MI_OP mi_o;
790 if (size > (UINT_MAX - sizeof (MI_O)))
791 return (NULL);
793 if ((mi_o = (MI_OP)mi_zalloc(size + sizeof (MI_O))) == NULL)
794 return (NULL);
795 mi_o++;
796 return ((IDP)mi_o);
800 * MODOPEN means just link in without respect of mi_o_dev.
801 * A NULL devp can be used to create a detached instance
802 * Otherwise self-clone the device.
804 /* ARGSUSED3 */
806 mi_open_link(void **mi_headp, IDP ptr, dev_t *devp, int flag, int sflag,
807 cred_t *credp)
809 mi_head_t *mi_head = *(mi_head_t **)mi_headp;
810 MI_OP insert;
811 MI_OP mi_o;
812 dev_t dev;
814 if (mi_head == NULL) {
815 char arena_name[50];
816 char *head_name;
817 ulong_t offset;
819 head_name = kobj_getsymname((uintptr_t)mi_headp, &offset);
820 if (head_name != NULL && offset == 0) {
821 (void) sprintf(arena_name, "%s_", head_name);
822 } else {
823 (void) sprintf(arena_name, "Hex0x%p_",
824 (void *)mi_headp);
826 (void) sprintf(strchr(arena_name, '_') + 1, "minor");
827 mi_head = (mi_head_t *)mi_zalloc_sleep(sizeof (mi_head_t));
828 *mi_headp = (void *)mi_head;
829 /* Setup doubly linked list */
830 mi_head->mh_o.mi_o_next = &mi_head->mh_o;
831 mi_head->mh_o.mi_o_prev = &mi_head->mh_o;
832 mi_head->mh_o.mi_o_dev = 0; /* For asserts only */
833 mi_head->mh_arena = (vmem_t *)inet_minor_create(arena_name,
834 INET_MIN_DEV, MAXMIN, KM_SLEEP);
836 ASSERT(ptr != NULL);
837 mi_o = (MI_OP)ptr;
838 mi_o--;
840 if (sflag == MODOPEN) {
841 devp = NULL;
843 * Set device number to MAXMIN + incrementing number.
845 dev = MAXMIN + ++mi_head->mh_module_dev;
846 /* check for wraparound */
847 if (dev <= MAXMIN) {
848 dev = MAXMIN + 1;
849 mi_head->mh_module_dev = 1;
851 } else if (devp == NULL) {
852 /* Detached open */
853 dev = (dev_t)OPENFAIL;
854 } else if ((dev = inet_minor_alloc(mi_head->mh_arena)) == 0) {
855 return (EBUSY);
858 mi_o->mi_o_dev = dev;
859 insert = (&mi_head->mh_o);
860 mi_o->mi_o_next = insert;
861 insert->mi_o_prev->mi_o_next = mi_o;
862 mi_o->mi_o_prev = insert->mi_o_prev;
863 insert->mi_o_prev = mi_o;
865 if (sflag == MODOPEN)
866 mi_o->mi_o_isdev = B_FALSE;
867 else
868 mi_o->mi_o_isdev = B_TRUE;
870 if (devp)
871 *devp = makedevice(getemajor(*devp), (minor_t)dev);
872 return (0);
875 uint8_t *
876 mi_offset_param(mblk_t *mp, size_t offset, size_t len)
878 size_t msg_len;
880 if (!mp)
881 return (NULL);
882 msg_len = mp->b_wptr - mp->b_rptr;
883 if (msg_len == 0 || offset > msg_len || len > msg_len ||
884 (offset + len) > msg_len || len == 0)
885 return (NULL);
886 return (&mp->b_rptr[offset]);
889 uint8_t *
890 mi_offset_paramc(mblk_t *mp, size_t offset, size_t len)
892 uint8_t *param;
894 for (; mp; mp = mp->b_cont) {
895 int type = mp->b_datap->db_type;
896 if (datamsg(type)) {
897 if (param = mi_offset_param(mp, offset, len))
898 return (param);
899 if (offset < mp->b_wptr - mp->b_rptr)
900 break;
901 offset -= mp->b_wptr - mp->b_rptr;
904 return (NULL);
908 mi_sprintf(char *buf, char *fmt, ...)
910 va_list ap;
911 int count = -1;
912 va_start(ap, fmt);
913 if (buf) {
914 count = mi_iprintf(fmt, ap, (pfi_t)mi_sprintf_putc,
915 (char *)&buf);
916 if (count != -1)
917 (void) mi_sprintf_putc((char *)&buf, '\0');
919 va_end(ap);
920 return (count);
923 /* Used to count without writing data */
924 /* ARGSUSED1 */
925 static int
926 mi_sprintf_noop(char *cookie, int ch)
928 char **cpp = (char **)cookie;
930 (*cpp)++;
931 return (1);
935 mi_sprintf_putc(char *cookie, int ch)
937 char **cpp = (char **)cookie;
939 **cpp = (char)ch;
940 (*cpp)++;
941 return (1);
945 mi_strcmp(const char *cp1, const char *cp2)
947 while (*cp1++ == *cp2++) {
948 if (!cp2[-1])
949 return (0);
951 return ((uint_t)cp2[-1] & 0xFF) - ((uint_t)cp1[-1] & 0xFF);
954 size_t
955 mi_strlen(const char *str)
957 const char *cp = str;
959 while (*cp != '\0')
960 cp++;
961 return ((int)(cp - str));
965 mi_strlog(queue_t *q, char level, ushort_t flags, char *fmt, ...)
967 va_list ap;
968 char buf[200];
969 char *alloc_buf = buf;
970 int count = -1;
971 char *cp;
972 short mid;
973 int ret;
974 short sid;
976 sid = 0;
977 mid = 0;
978 if (q != NULL) {
979 mid = q->q_qinfo->qi_minfo->mi_idnum;
982 /* Find out how many bytes we need and allocate if necesary */
983 va_start(ap, fmt);
984 cp = buf;
985 count = mi_iprintf(fmt, ap, mi_sprintf_noop, (char *)&cp);
986 if (count > sizeof (buf) &&
987 !(alloc_buf = mi_alloc((uint_t)count + 2, BPRI_MED))) {
988 va_end(ap);
989 return (-1);
991 va_end(ap);
993 va_start(ap, fmt);
994 cp = alloc_buf;
995 count = mi_iprintf(fmt, ap, mi_sprintf_putc, (char *)&cp);
996 if (count != -1)
997 (void) mi_sprintf_putc((char *)&cp, '\0');
998 else
999 alloc_buf[0] = '\0';
1000 va_end(ap);
1002 ret = strlog(mid, sid, level, flags, alloc_buf);
1003 if (alloc_buf != buf)
1004 mi_free(alloc_buf);
1005 return (ret);
1008 long
1009 mi_strtol(const char *str, char **ptr, int base)
1011 const char *cp;
1012 int digits;
1013 long value;
1014 boolean_t is_negative;
1016 cp = str;
1017 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
1018 cp++;
1019 is_negative = (*cp == '-');
1020 if (is_negative)
1021 cp++;
1022 if (base == 0) {
1023 base = 10;
1024 if (*cp == '0') {
1025 base = 8;
1026 cp++;
1027 if (*cp == 'x' || *cp == 'X') {
1028 base = 16;
1029 cp++;
1033 value = 0;
1034 for (; *cp != '\0'; cp++) {
1035 if (*cp >= '0' && *cp <= '9')
1036 digits = *cp - '0';
1037 else if (*cp >= 'a' && *cp <= 'f')
1038 digits = *cp - 'a' + 10;
1039 else if (*cp >= 'A' && *cp <= 'F')
1040 digits = *cp - 'A' + 10;
1041 else
1042 break;
1043 if (digits >= base)
1044 break;
1045 value = (value * base) + digits;
1047 /* Note: we cast away const here deliberately */
1048 if (ptr != NULL)
1049 *ptr = (char *)cp;
1050 if (is_negative)
1051 value = -value;
1052 return (value);
1056 * mi_timer mechanism.
1058 * Each timer is represented by a timer mblk and a (streams) queue. When the
1059 * timer fires the timer mblk will be put on the associated streams queue
1060 * so that the streams module can process the timer even in its service
1061 * procedure.
1063 * The interface consists of 4 entry points:
1064 * mi_timer_alloc - create a timer mblk
1065 * mi_timer_free - free a timer mblk
1066 * mi_timer - start, restart, stop, or move the
1067 * timer to a different queue
1068 * mi_timer_valid - called by streams module to verify that
1069 * the timer did indeed fire.
1076 * Start, restart, stop, or move the timer to a new queue.
1077 * If "tim" is -2 the timer is moved to a different queue.
1078 * If "tim" is -1 the timer is stopped.
1079 * Otherwise, the timer is stopped if it is already running, and
1080 * set to fire tim milliseconds from now.
1083 void
1084 mi_timer(queue_t *q, MBLKP mp, clock_t tim)
1086 MTBP mtb;
1087 int state;
1089 ASSERT(tim >= -2);
1090 if (!q || !mp || (mp->b_rptr - mp->b_datap->db_base) != sizeof (MTB))
1091 return;
1092 mtb = (MTBP)mp->b_datap->db_base;
1093 ASSERT(mp->b_datap->db_type == M_PCSIG);
1094 if (tim >= 0) {
1095 mtb->mtb_q = q;
1096 state = mtb->mtb_state;
1097 tim = MSEC_TO_TICK(tim);
1098 if (state == TB_RUNNING) {
1099 if (untimeout(mtb->mtb_tid) < 0) {
1100 /* Message has already been putq */
1101 ASSERT(mtb->mtb_q->q_first == mp ||
1102 mp->b_prev || mp->b_next);
1103 mtb->mtb_state = TB_RESCHED;
1104 mtb->mtb_time_left = tim;
1105 /* mi_timer_valid will start timer */
1106 return;
1108 } else if (state != TB_IDLE) {
1109 ASSERT(state != TB_TO_BE_FREED);
1110 if (state == TB_CANCELLED) {
1111 ASSERT(mtb->mtb_q->q_first == mp ||
1112 mp->b_prev || mp->b_next);
1113 mtb->mtb_state = TB_RESCHED;
1114 mtb->mtb_time_left = tim;
1115 /* mi_timer_valid will start timer */
1116 return;
1118 if (state == TB_RESCHED) {
1119 ASSERT(mtb->mtb_q->q_first == mp ||
1120 mp->b_prev || mp->b_next);
1121 mtb->mtb_time_left = tim;
1122 /* mi_timer_valid will start timer */
1123 return;
1126 mtb->mtb_state = TB_RUNNING;
1127 mtb->mtb_tid = timeout((pfv_t)mi_timer_fire, mtb, tim);
1128 return;
1130 switch (tim) {
1131 case -1:
1132 mi_timer_stop(mp);
1133 break;
1134 case -2:
1135 mi_timer_move(q, mp);
1136 break;
1141 * Allocate an M_PCSIG timer message. The space between db_base and
1142 * b_rptr is used by the mi_timer mechanism, and after b_rptr there are
1143 * "size" bytes that the caller can use for its own purposes.
1145 * Note that db_type has to be a priority message since otherwise
1146 * the putq will not cause the service procedure to run when
1147 * there is flow control.
1149 MBLKP
1150 mi_timer_alloc(size_t size)
1152 MBLKP mp;
1153 MTBP mtb;
1155 if ((mp = allocb(size + sizeof (MTB), BPRI_HI)) != NULL) {
1156 mp->b_datap->db_type = M_PCSIG;
1157 mtb = (MTBP)mp->b_datap->db_base;
1158 mp->b_rptr = (uchar_t *)&mtb[1];
1159 mp->b_wptr = mp->b_rptr + size;
1160 mtb->mtb_state = TB_IDLE;
1161 mtb->mtb_mp = mp;
1162 mtb->mtb_q = NULL;
1163 return (mp);
1165 return (NULL);
1169 * timeout() callback function.
1170 * Put the message on the current queue.
1171 * If the timer is stopped or moved to a different queue after
1172 * it has fired then mi_timer() and mi_timer_valid() will clean
1173 * things up.
1175 static int
1176 mi_timer_fire(MTBP mtb)
1178 ASSERT(mtb == (MTBP)mtb->mtb_mp->b_datap->db_base);
1179 ASSERT(mtb->mtb_mp->b_datap->db_type == M_PCSIG);
1180 return (putq(mtb->mtb_q, mtb->mtb_mp));
1184 * Logically free a timer mblk (that might have a pending timeout().)
1185 * If the timer has fired and the mblk has been put on the queue then
1186 * mi_timer_valid will free the mblk.
1189 void
1190 mi_timer_free(MBLKP mp)
1192 MTBP mtb;
1193 int state;
1195 if (!mp || (mp->b_rptr - mp->b_datap->db_base) != sizeof (MTB))
1196 return;
1197 mtb = (MTBP)mp->b_datap->db_base;
1198 state = mtb->mtb_state;
1199 if (state == TB_RUNNING) {
1200 if (untimeout(mtb->mtb_tid) < 0) {
1201 /* Message has already been putq */
1202 ASSERT(mtb->mtb_q->q_first == mp ||
1203 mp->b_prev || mp->b_next);
1204 mtb->mtb_state = TB_TO_BE_FREED;
1205 /* mi_timer_valid will free the mblk */
1206 return;
1208 } else if (state != TB_IDLE) {
1209 /* Message has already been putq */
1210 ASSERT(mtb->mtb_q->q_first == mp ||
1211 mp->b_prev || mp->b_next);
1212 ASSERT(state != TB_TO_BE_FREED);
1213 mtb->mtb_state = TB_TO_BE_FREED;
1214 /* mi_timer_valid will free the mblk */
1215 return;
1217 ASSERT(mtb->mtb_q == NULL || mtb->mtb_q->q_first != mp);
1218 freemsg(mp);
1222 * Called from mi_timer(,,-2)
1224 void
1225 mi_timer_move(queue_t *q, MBLKP mp)
1227 MTBP mtb;
1228 clock_t tim;
1230 if (!q || !mp || (mp->b_rptr - mp->b_datap->db_base) != sizeof (MTB))
1231 return;
1233 mtb = (MTBP)mp->b_datap->db_base;
1235 * Need to untimeout and restart to make
1236 * sure that the mblk is not about to be putq on the old queue
1237 * by mi_timer_fire.
1239 if (mtb->mtb_state == TB_RUNNING) {
1240 if ((tim = untimeout(mtb->mtb_tid)) < 0) {
1242 * Message has already been putq. Move from old queue
1243 * to new queue.
1245 ASSERT(mtb->mtb_q->q_first == mp ||
1246 mp->b_prev || mp->b_next);
1247 rmvq(mtb->mtb_q, mp);
1248 ASSERT(mtb->mtb_q->q_first != mp &&
1249 mp->b_prev == NULL && mp->b_next == NULL);
1250 mtb->mtb_q = q;
1251 (void) putq(mtb->mtb_q, mp);
1252 return;
1254 mtb->mtb_q = q;
1255 mtb->mtb_state = TB_RUNNING;
1256 mtb->mtb_tid = timeout((pfv_t)mi_timer_fire, mtb, tim);
1257 } else if (mtb->mtb_state != TB_IDLE) {
1258 ASSERT(mtb->mtb_state != TB_TO_BE_FREED);
1260 * Message is already sitting on queue. Move to new queue.
1262 ASSERT(mtb->mtb_q->q_first == mp ||
1263 mp->b_prev || mp->b_next);
1264 rmvq(mtb->mtb_q, mp);
1265 ASSERT(mtb->mtb_q->q_first != mp &&
1266 mp->b_prev == NULL && mp->b_next == NULL);
1267 mtb->mtb_q = q;
1268 (void) putq(mtb->mtb_q, mp);
1269 } else
1270 mtb->mtb_q = q;
1274 * Called from mi_timer(,,-1)
1276 void
1277 mi_timer_stop(MBLKP mp)
1279 MTBP mtb;
1280 int state;
1282 if (!mp || (mp->b_rptr - mp->b_datap->db_base) != sizeof (MTB))
1283 return;
1285 mtb = (MTBP)mp->b_datap->db_base;
1286 state = mtb->mtb_state;
1287 if (state == TB_RUNNING) {
1288 if (untimeout(mtb->mtb_tid) < 0) {
1289 /* Message has already been putq */
1290 ASSERT(mtb->mtb_q->q_first == mp ||
1291 mp->b_prev || mp->b_next);
1292 mtb->mtb_state = TB_CANCELLED;
1293 } else {
1294 mtb->mtb_state = TB_IDLE;
1296 } else if (state == TB_RESCHED) {
1297 ASSERT(mtb->mtb_q->q_first == mp ||
1298 mp->b_prev || mp->b_next);
1299 mtb->mtb_state = TB_CANCELLED;
1304 * The user of the mi_timer mechanism is required to call mi_timer_valid() for
1305 * each M_PCSIG message processed in the service procedures.
1306 * mi_timer_valid will return "true" if the timer actually did fire.
1309 boolean_t
1310 mi_timer_valid(MBLKP mp)
1312 MTBP mtb;
1313 int state;
1315 if (!mp || (mp->b_rptr - mp->b_datap->db_base) != sizeof (MTB) ||
1316 mp->b_datap->db_type != M_PCSIG)
1317 return (B_FALSE);
1318 mtb = (MTBP)mp->b_datap->db_base;
1319 state = mtb->mtb_state;
1320 if (state != TB_RUNNING) {
1321 ASSERT(state != TB_IDLE);
1322 if (state == TB_TO_BE_FREED) {
1324 * mi_timer_free was called after the message
1325 * was putq'ed.
1327 freemsg(mp);
1328 return (B_FALSE);
1330 if (state == TB_CANCELLED) {
1331 /* The timer was stopped after the mblk was putq'ed */
1332 mtb->mtb_state = TB_IDLE;
1333 return (B_FALSE);
1335 if (state == TB_RESCHED) {
1337 * The timer was stopped and then restarted after
1338 * the mblk was putq'ed.
1339 * mtb_time_left contains the number of ticks that
1340 * the timer was restarted with.
1342 mtb->mtb_state = TB_RUNNING;
1343 mtb->mtb_tid = timeout((pfv_t)mi_timer_fire,
1344 mtb, mtb->mtb_time_left);
1345 return (B_FALSE);
1348 mtb->mtb_state = TB_IDLE;
1349 return (B_TRUE);
1352 static void
1353 mi_tpi_addr_and_opt(MBLKP mp, char *addr, t_scalar_t addr_length,
1354 char *opt, t_scalar_t opt_length)
1356 struct T_unitdata_ind *tudi;
1359 * This code is used more than just for unitdata ind
1360 * (also for T_CONN_IND and T_CONN_CON) and
1361 * relies on correct functioning on the happy
1362 * coincidence that the address and option buffers
1363 * represented by length/offset in all these primitives
1364 * are isomorphic in terms of offset from start of data
1365 * structure
1367 tudi = (struct T_unitdata_ind *)mp->b_rptr;
1368 tudi->SRC_offset = (t_scalar_t)(mp->b_wptr - mp->b_rptr);
1369 tudi->SRC_length = addr_length;
1370 if (addr_length > 0) {
1371 bcopy(addr, (char *)mp->b_wptr, addr_length);
1372 mp->b_wptr += addr_length;
1374 tudi->OPT_offset = (t_scalar_t)(mp->b_wptr - mp->b_rptr);
1375 tudi->OPT_length = opt_length;
1376 if (opt_length > 0) {
1377 bcopy(opt, (char *)mp->b_wptr, opt_length);
1378 mp->b_wptr += opt_length;
1382 MBLKP
1383 mi_tpi_conn_con(MBLKP trailer_mp, char *src, t_scalar_t src_length, char *opt,
1384 t_scalar_t opt_length)
1386 size_t len;
1387 MBLKP mp;
1389 len = sizeof (struct T_conn_con) + src_length + opt_length;
1390 if ((mp = mi_tpi_trailer_alloc(trailer_mp, len, T_CONN_CON)) != NULL) {
1391 mp->b_wptr = &mp->b_rptr[sizeof (struct T_conn_con)];
1392 mi_tpi_addr_and_opt(mp, src, src_length, opt, opt_length);
1394 return (mp);
1397 MBLKP
1398 mi_tpi_conn_ind(MBLKP trailer_mp, char *src, t_scalar_t src_length, char *opt,
1399 t_scalar_t opt_length, t_scalar_t seqnum)
1401 size_t len;
1402 MBLKP mp;
1404 len = sizeof (struct T_conn_ind) + src_length + opt_length;
1405 if ((mp = mi_tpi_trailer_alloc(trailer_mp, len, T_CONN_IND)) != NULL) {
1406 mp->b_wptr = &mp->b_rptr[sizeof (struct T_conn_ind)];
1407 mi_tpi_addr_and_opt(mp, src, src_length, opt, opt_length);
1408 ((struct T_conn_ind *)mp->b_rptr)->SEQ_number = seqnum;
1409 mp->b_datap->db_type = M_PROTO;
1411 return (mp);
1414 MBLKP
1415 mi_tpi_extconn_ind(MBLKP trailer_mp, char *src, t_scalar_t src_length,
1416 char *opt, t_scalar_t opt_length, char *dst, t_scalar_t dst_length,
1417 t_scalar_t seqnum)
1419 size_t len;
1420 MBLKP mp;
1422 len = sizeof (struct T_extconn_ind) + src_length + opt_length +
1423 dst_length;
1424 if ((mp = mi_tpi_trailer_alloc(trailer_mp, len, T_EXTCONN_IND)) !=
1425 NULL) {
1426 mp->b_wptr = &mp->b_rptr[sizeof (struct T_extconn_ind)];
1427 mi_tpi_addr_and_opt(mp, src, src_length, opt, opt_length);
1428 ((struct T_extconn_ind *)mp->b_rptr)->DEST_length = dst_length;
1429 ((struct T_extconn_ind *)mp->b_rptr)->DEST_offset =
1430 (t_scalar_t)(mp->b_wptr - mp->b_rptr);
1431 if (dst_length > 0) {
1432 bcopy(dst, (char *)mp->b_wptr, dst_length);
1433 mp->b_wptr += dst_length;
1435 ((struct T_extconn_ind *)mp->b_rptr)->SEQ_number = seqnum;
1436 mp->b_datap->db_type = M_PROTO;
1438 return (mp);
1441 MBLKP
1442 mi_tpi_discon_ind(MBLKP trailer_mp, t_scalar_t reason, t_scalar_t seqnum)
1444 MBLKP mp;
1445 struct T_discon_ind *tdi;
1447 if ((mp = mi_tpi_trailer_alloc(trailer_mp,
1448 sizeof (struct T_discon_ind), T_DISCON_IND)) != NULL) {
1449 tdi = (struct T_discon_ind *)mp->b_rptr;
1450 tdi->DISCON_reason = reason;
1451 tdi->SEQ_number = seqnum;
1453 return (mp);
1457 * Allocate and fill in a TPI err ack packet using the 'mp' passed in
1458 * for the 'error_prim' context as well as sacrifice.
1460 MBLKP
1461 mi_tpi_err_ack_alloc(MBLKP mp, t_scalar_t tlierr, int unixerr)
1463 struct T_error_ack *teackp;
1464 t_scalar_t error_prim;
1466 if (!mp)
1467 return (NULL);
1468 error_prim = ((TPRIMP)mp->b_rptr)->type;
1469 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack),
1470 M_PCPROTO, T_ERROR_ACK)) != NULL) {
1471 teackp = (struct T_error_ack *)mp->b_rptr;
1472 teackp->ERROR_prim = error_prim;
1473 teackp->TLI_error = tlierr;
1474 teackp->UNIX_error = unixerr;
1476 return (mp);
1479 MBLKP
1480 mi_tpi_ok_ack_alloc_extra(MBLKP mp, int extra)
1482 t_scalar_t correct_prim;
1484 if (!mp)
1485 return (NULL);
1486 correct_prim = ((TPRIMP)mp->b_rptr)->type;
1487 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_ok_ack) + extra,
1488 M_PCPROTO, T_OK_ACK)) != NULL) {
1489 ((struct T_ok_ack *)mp->b_rptr)->CORRECT_prim = correct_prim;
1490 mp->b_wptr -= extra;
1492 return (mp);
1495 MBLKP
1496 mi_tpi_ok_ack_alloc(MBLKP mp)
1498 return (mi_tpi_ok_ack_alloc_extra(mp, 0));
1501 MBLKP
1502 mi_tpi_ordrel_ind(void)
1504 MBLKP mp;
1506 if ((mp = allocb(sizeof (struct T_ordrel_ind), BPRI_HI)) != NULL) {
1507 mp->b_datap->db_type = M_PROTO;
1508 ((struct T_ordrel_ind *)mp->b_rptr)->PRIM_type = T_ORDREL_IND;
1509 mp->b_wptr += sizeof (struct T_ordrel_ind);
1511 return (mp);
1514 static MBLKP
1515 mi_tpi_trailer_alloc(MBLKP trailer_mp, size_t size, t_scalar_t type)
1517 MBLKP mp;
1519 if ((mp = allocb(size, BPRI_MED)) != NULL) {
1520 mp->b_cont = trailer_mp;
1521 mp->b_datap->db_type = M_PROTO;
1522 ((union T_primitives *)mp->b_rptr)->type = type;
1523 mp->b_wptr += size;
1525 return (mp);
1528 MBLKP
1529 mi_tpi_uderror_ind(char *dest, t_scalar_t dest_length, char *opt,
1530 t_scalar_t opt_length, t_scalar_t error)
1532 size_t len;
1533 MBLKP mp;
1534 struct T_uderror_ind *tudei;
1536 len = sizeof (struct T_uderror_ind) + dest_length + opt_length;
1537 if ((mp = allocb(len, BPRI_HI)) != NULL) {
1538 mp->b_datap->db_type = M_PROTO;
1539 tudei = (struct T_uderror_ind *)mp->b_rptr;
1540 tudei->PRIM_type = T_UDERROR_IND;
1541 tudei->ERROR_type = error;
1542 mp->b_wptr = &mp->b_rptr[sizeof (struct T_uderror_ind)];
1543 mi_tpi_addr_and_opt(mp, dest, dest_length, opt, opt_length);
1545 return (mp);
1549 mi_zalloc(size_t size)
1551 IDP ptr;
1553 if (ptr = mi_alloc(size, BPRI_LO))
1554 bzero(ptr, size);
1555 return (ptr);
1559 mi_zalloc_sleep(size_t size)
1561 IDP ptr;
1563 if (ptr = mi_alloc_sleep(size, BPRI_LO))
1564 bzero(ptr, size);
1565 return (ptr);