1*507c3241Smlf /*
2*507c3241Smlf * CDDL HEADER START
3*507c3241Smlf *
4*507c3241Smlf * The contents of this file are subject to the terms of the
5*507c3241Smlf * Common Development and Distribution License (the "License").
6*507c3241Smlf * You may not use this file except in compliance with the License.
7*507c3241Smlf *
8*507c3241Smlf * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*507c3241Smlf * or http://www.opensolaris.org/os/licensing.
10*507c3241Smlf * See the License for the specific language governing permissions
11*507c3241Smlf * and limitations under the License.
12*507c3241Smlf *
13*507c3241Smlf * When distributing Covered Code, include this CDDL HEADER in each
14*507c3241Smlf * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*507c3241Smlf * If applicable, add the following below this CDDL HEADER, with the
16*507c3241Smlf * fields enclosed by brackets "[]" replaced with your own identifying
17*507c3241Smlf * information: Portions Copyright [yyyy] [name of copyright owner]
18*507c3241Smlf *
19*507c3241Smlf * CDDL HEADER END
20*507c3241Smlf */
21*507c3241Smlf
22*507c3241Smlf /*
23*507c3241Smlf * Copyright 1999 Sun Microsystems, Inc. All rights reserved.
24*507c3241Smlf * Use is subject to license terms.
25*507c3241Smlf */
26*507c3241Smlf
27*507c3241Smlf #include "ghd.h"
28*507c3241Smlf
29*507c3241Smlf /*
30*507c3241Smlf * Round up all allocations so that we can guarantee
31*507c3241Smlf * long-long alignment. This is the same alignment
32*507c3241Smlf * provided by kmem_alloc().
33*507c3241Smlf */
34*507c3241Smlf #define ROUNDUP(x) (((x) + 0x07) & ~0x07)
35*507c3241Smlf
36*507c3241Smlf /*
37*507c3241Smlf * Private wrapper for gcmd_t
38*507c3241Smlf */
39*507c3241Smlf typedef struct gw_gcmd_and_length {
40*507c3241Smlf gcmd_t gcmd; /* this must be first */
41*507c3241Smlf int glen; /* length includes HBA private area */
42*507c3241Smlf }gw_t;
43*507c3241Smlf
44*507c3241Smlf /*
45*507c3241Smlf * round up the size so the HBA private area is on a 8 byte boundary
46*507c3241Smlf */
47*507c3241Smlf #define GW_PADDED_LENGTH ROUNDUP(sizeof (gw_t))
48*507c3241Smlf
49*507c3241Smlf typedef struct gcmd_padded_wrapper {
50*507c3241Smlf union {
51*507c3241Smlf gw_t gw;
52*507c3241Smlf char gw_pad[GW_PADDED_LENGTH];
53*507c3241Smlf
54*507c3241Smlf } gwrap;
55*507c3241Smlf } gwrap_t;
56*507c3241Smlf
57*507c3241Smlf /*
58*507c3241Smlf * Allocate a gcmd_t wrapper and HBA private area
59*507c3241Smlf */
60*507c3241Smlf
61*507c3241Smlf gcmd_t *
ghd_gcmd_alloc(gtgt_t * gtgtp,int ccblen,int sleep)62*507c3241Smlf ghd_gcmd_alloc(gtgt_t *gtgtp,
63*507c3241Smlf int ccblen,
64*507c3241Smlf int sleep)
65*507c3241Smlf {
66*507c3241Smlf gwrap_t *gwp;
67*507c3241Smlf gcmd_t *gcmdp;
68*507c3241Smlf int gwrap_len;
69*507c3241Smlf
70*507c3241Smlf ccblen = ROUNDUP(ccblen);
71*507c3241Smlf gwrap_len = sizeof (gwrap_t) + ccblen;
72*507c3241Smlf gwp = kmem_zalloc(gwrap_len, (sleep ? KM_SLEEP : KM_NOSLEEP));
73*507c3241Smlf if (gwp == NULL) {
74*507c3241Smlf ASSERT(sleep == FALSE);
75*507c3241Smlf return (NULL);
76*507c3241Smlf }
77*507c3241Smlf
78*507c3241Smlf /* save the total length for the free function */
79*507c3241Smlf gwp->gwrap.gw.glen = gwrap_len;
80*507c3241Smlf
81*507c3241Smlf /*
82*507c3241Smlf * save the ptr to HBA private area and initialize all
83*507c3241Smlf * the gcmd_t members and save
84*507c3241Smlf */
85*507c3241Smlf gcmdp = &gwp->gwrap.gw.gcmd;
86*507c3241Smlf GHD_GCMD_INIT(gcmdp, (void *)(gwp + 1), gtgtp);
87*507c3241Smlf return (gcmdp);
88*507c3241Smlf }
89*507c3241Smlf
90*507c3241Smlf
91*507c3241Smlf
92*507c3241Smlf /*
93*507c3241Smlf * Free the gcmd_t wrapper and HBA private area
94*507c3241Smlf */
95*507c3241Smlf
96*507c3241Smlf void
ghd_gcmd_free(gcmd_t * gcmdp)97*507c3241Smlf ghd_gcmd_free(gcmd_t *gcmdp)
98*507c3241Smlf {
99*507c3241Smlf kmem_free(gcmdp, ((gw_t *)gcmdp)->glen);
100*507c3241Smlf }
101