staging: brcm80211: moved code around for cleanup
[linux-2.6.git] / drivers / staging / brcm80211 / brcmsmac / sys / wlc_alloc.c
blob7a9fdbb5daee51f6da92b28f629a8968062a765d
1 /*
2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <bcmdefs.h>
19 #include <wlc_cfg.h>
20 #include <linux/module.h>
21 #include <linux/pci.h>
22 #include <osl.h>
23 #include <bcmutils.h>
24 #include <siutils.h>
25 #include <wlioctl.h>
26 #include <wlc_pub.h>
27 #include <wlc_key.h>
28 #include <sbhndpio.h>
29 #include <sbhnddma.h>
30 #include <wlc_event.h>
31 #include <wlc_mac80211.h>
32 #include <wlc_alloc.h>
33 #include <wl_dbg.h>
35 static struct wlc_pub *wlc_pub_malloc(struct osl_info *osh, uint unit,
36 uint *err, uint devid);
37 static void wlc_pub_mfree(struct osl_info *osh, struct wlc_pub *pub);
38 static void wlc_tunables_init(wlc_tunables_t *tunables, uint devid);
40 void *wlc_calloc(struct osl_info *osh, uint unit, uint size)
42 void *item;
44 item = kzalloc(size, GFP_ATOMIC);
45 if (item == NULL)
46 WL_ERROR("wl%d: %s: out of memory\n", unit, __func__);
47 return item;
50 void wlc_tunables_init(wlc_tunables_t *tunables, uint devid)
52 tunables->ntxd = NTXD;
53 tunables->nrxd = NRXD;
54 tunables->rxbufsz = RXBUFSZ;
55 tunables->nrxbufpost = NRXBUFPOST;
56 tunables->maxscb = MAXSCB;
57 tunables->ampdunummpdu = AMPDU_NUM_MPDU;
58 tunables->maxpktcb = MAXPKTCB;
59 tunables->maxucodebss = WLC_MAX_UCODE_BSS;
60 tunables->maxucodebss4 = WLC_MAX_UCODE_BSS4;
61 tunables->maxbss = MAXBSS;
62 tunables->datahiwat = WLC_DATAHIWAT;
63 tunables->ampdudatahiwat = WLC_AMPDUDATAHIWAT;
64 tunables->rxbnd = RXBND;
65 tunables->txsbnd = TXSBND;
68 static struct wlc_pub *wlc_pub_malloc(struct osl_info *osh, uint unit,
69 uint *err, uint devid)
71 struct wlc_pub *pub;
73 pub = (struct wlc_pub *) wlc_calloc(osh, unit, sizeof(struct wlc_pub));
74 if (pub == NULL) {
75 *err = 1001;
76 goto fail;
79 pub->tunables = (wlc_tunables_t *)wlc_calloc(osh, unit,
80 sizeof(wlc_tunables_t));
81 if (pub->tunables == NULL) {
82 *err = 1028;
83 goto fail;
86 /* need to init the tunables now */
87 wlc_tunables_init(pub->tunables, devid);
89 pub->multicast = (u8 *)wlc_calloc(osh, unit,
90 (ETH_ALEN * MAXMULTILIST));
91 if (pub->multicast == NULL) {
92 *err = 1003;
93 goto fail;
96 return pub;
98 fail:
99 wlc_pub_mfree(osh, pub);
100 return NULL;
103 static void wlc_pub_mfree(struct osl_info *osh, struct wlc_pub *pub)
105 if (pub == NULL)
106 return;
108 if (pub->multicast)
109 kfree(pub->multicast);
110 if (pub->tunables) {
111 kfree(pub->tunables);
112 pub->tunables = NULL;
115 kfree(pub);
118 wlc_bsscfg_t *wlc_bsscfg_malloc(struct osl_info *osh, uint unit)
120 wlc_bsscfg_t *cfg;
122 cfg = (wlc_bsscfg_t *) wlc_calloc(osh, unit, sizeof(wlc_bsscfg_t));
123 if (cfg == NULL)
124 goto fail;
126 cfg->current_bss = (wlc_bss_info_t *)wlc_calloc(osh, unit,
127 sizeof(wlc_bss_info_t));
128 if (cfg->current_bss == NULL)
129 goto fail;
131 return cfg;
133 fail:
134 wlc_bsscfg_mfree(osh, cfg);
135 return NULL;
138 void wlc_bsscfg_mfree(struct osl_info *osh, wlc_bsscfg_t *cfg)
140 if (cfg == NULL)
141 return;
143 if (cfg->maclist) {
144 kfree(cfg->maclist);
145 cfg->maclist = NULL;
148 if (cfg->current_bss != NULL) {
149 wlc_bss_info_t *current_bss = cfg->current_bss;
150 kfree(current_bss);
151 cfg->current_bss = NULL;
154 kfree(cfg);
157 void wlc_bsscfg_ID_assign(struct wlc_info *wlc, wlc_bsscfg_t *bsscfg)
159 bsscfg->ID = wlc->next_bsscfg_ID;
160 wlc->next_bsscfg_ID++;
164 * The common driver entry routine. Error codes should be unique
166 struct wlc_info *wlc_attach_malloc(struct osl_info *osh, uint unit, uint *err,
167 uint devid)
169 struct wlc_info *wlc;
171 wlc = (struct wlc_info *) wlc_calloc(osh, unit,
172 sizeof(struct wlc_info));
173 if (wlc == NULL) {
174 *err = 1002;
175 goto fail;
178 wlc->hwrxoff = WL_HWRXOFF;
180 /* allocate struct wlc_pub state structure */
181 wlc->pub = wlc_pub_malloc(osh, unit, err, devid);
182 if (wlc->pub == NULL) {
183 *err = 1003;
184 goto fail;
186 wlc->pub->wlc = wlc;
188 /* allocate struct wlc_hw_info state structure */
190 wlc->hw = (struct wlc_hw_info *)wlc_calloc(osh, unit,
191 sizeof(struct wlc_hw_info));
192 if (wlc->hw == NULL) {
193 *err = 1005;
194 goto fail;
196 wlc->hw->wlc = wlc;
198 wlc->hw->bandstate[0] = (wlc_hwband_t *)wlc_calloc(osh, unit,
199 (sizeof(wlc_hwband_t) * MAXBANDS));
200 if (wlc->hw->bandstate[0] == NULL) {
201 *err = 1006;
202 goto fail;
203 } else {
204 int i;
206 for (i = 1; i < MAXBANDS; i++) {
207 wlc->hw->bandstate[i] = (wlc_hwband_t *)
208 ((unsigned long)wlc->hw->bandstate[0] +
209 (sizeof(wlc_hwband_t) * i));
213 wlc->modulecb = (modulecb_t *)wlc_calloc(osh, unit,
214 sizeof(modulecb_t) * WLC_MAXMODULES);
215 if (wlc->modulecb == NULL) {
216 *err = 1009;
217 goto fail;
220 wlc->default_bss = (wlc_bss_info_t *)wlc_calloc(osh, unit,
221 sizeof(wlc_bss_info_t));
222 if (wlc->default_bss == NULL) {
223 *err = 1010;
224 goto fail;
227 wlc->cfg = wlc_bsscfg_malloc(osh, unit);
228 if (wlc->cfg == NULL) {
229 *err = 1011;
230 goto fail;
232 wlc_bsscfg_ID_assign(wlc, wlc->cfg);
234 wlc->pkt_callback = (pkt_cb_t *)wlc_calloc(osh, unit,
235 (sizeof(pkt_cb_t) * (wlc->pub->tunables->maxpktcb + 1)));
236 if (wlc->pkt_callback == NULL) {
237 *err = 1013;
238 goto fail;
241 wlc->wsec_def_keys[0] = (wsec_key_t *)wlc_calloc(osh, unit,
242 (sizeof(wsec_key_t) * WLC_DEFAULT_KEYS));
243 if (wlc->wsec_def_keys[0] == NULL) {
244 *err = 1015;
245 goto fail;
246 } else {
247 int i;
248 for (i = 1; i < WLC_DEFAULT_KEYS; i++) {
249 wlc->wsec_def_keys[i] = (wsec_key_t *)
250 ((unsigned long)wlc->wsec_def_keys[0] +
251 (sizeof(wsec_key_t) * i));
255 wlc->protection = (wlc_protection_t *)wlc_calloc(osh, unit,
256 sizeof(wlc_protection_t));
257 if (wlc->protection == NULL) {
258 *err = 1016;
259 goto fail;
262 wlc->stf = (wlc_stf_t *)wlc_calloc(osh, unit, sizeof(wlc_stf_t));
263 if (wlc->stf == NULL) {
264 *err = 1017;
265 goto fail;
268 wlc->bandstate[0] = (struct wlcband *)wlc_calloc(osh, unit,
269 (sizeof(struct wlcband)*MAXBANDS));
270 if (wlc->bandstate[0] == NULL) {
271 *err = 1025;
272 goto fail;
273 } else {
274 int i;
276 for (i = 1; i < MAXBANDS; i++) {
277 wlc->bandstate[i] =
278 (struct wlcband *) ((unsigned long)wlc->bandstate[0]
279 + (sizeof(struct wlcband)*i));
283 wlc->corestate = (struct wlccore *)wlc_calloc(osh, unit,
284 sizeof(struct wlccore));
285 if (wlc->corestate == NULL) {
286 *err = 1026;
287 goto fail;
290 wlc->corestate->macstat_snapshot =
291 (macstat_t *)wlc_calloc(osh, unit, sizeof(macstat_t));
292 if (wlc->corestate->macstat_snapshot == NULL) {
293 *err = 1027;
294 goto fail;
297 return wlc;
299 fail:
300 wlc_detach_mfree(wlc, osh);
301 return NULL;
304 void wlc_detach_mfree(struct wlc_info *wlc, struct osl_info *osh)
306 if (wlc == NULL)
307 return;
309 if (wlc->modulecb) {
310 kfree(wlc->modulecb);
311 wlc->modulecb = NULL;
314 if (wlc->default_bss) {
315 kfree(wlc->default_bss);
316 wlc->default_bss = NULL;
318 if (wlc->cfg) {
319 wlc_bsscfg_mfree(osh, wlc->cfg);
320 wlc->cfg = NULL;
323 if (wlc->pkt_callback && wlc->pub && wlc->pub->tunables) {
324 kfree(wlc->pkt_callback);
325 wlc->pkt_callback = NULL;
328 if (wlc->wsec_def_keys[0])
329 kfree(wlc->wsec_def_keys[0]);
330 if (wlc->protection) {
331 kfree(wlc->protection);
332 wlc->protection = NULL;
335 if (wlc->stf) {
336 kfree(wlc->stf);
337 wlc->stf = NULL;
340 if (wlc->bandstate[0])
341 kfree(wlc->bandstate[0]);
343 if (wlc->corestate) {
344 if (wlc->corestate->macstat_snapshot) {
345 kfree(wlc->corestate->macstat_snapshot); wlc->corestate->macstat_snapshot = NULL;
347 kfree(wlc->corestate);
348 wlc->corestate = NULL;
351 if (wlc->pub) {
352 /* free pub struct */
353 wlc_pub_mfree(osh, wlc->pub);
354 wlc->pub = NULL;
357 if (wlc->hw) {
358 if (wlc->hw->bandstate[0]) {
359 kfree(wlc->hw->bandstate[0]);
360 wlc->hw->bandstate[0] = NULL;
363 /* free hw struct */
364 kfree(wlc->hw);
365 wlc->hw = NULL;
368 /* free the wlc */
369 kfree(wlc);
370 wlc = NULL;