659ebd0ee2de535a28351c233455b97ad3a21dd7
[pandora-kernel.git] / drivers / staging / brcm80211 / sys / wlc_alloc.c
1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
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.
7  *
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.
15  */
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>
34
35 static wlc_pub_t *wlc_pub_malloc(struct osl_info *osh, uint unit, uint *err,
36                                  uint devid);
37 static void wlc_pub_mfree(struct osl_info *osh, wlc_pub_t *pub);
38 static void wlc_tunables_init(wlc_tunables_t *tunables, uint devid);
39
40 void *wlc_calloc(struct osl_info *osh, uint unit, uint size)
41 {
42         void *item;
43
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;
48 }
49
50 void wlc_tunables_init(wlc_tunables_t *tunables, uint devid)
51 {
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;
66 }
67
68 static wlc_pub_t *wlc_pub_malloc(struct osl_info *osh, uint unit, uint *err,
69                                  uint devid)
70 {
71         wlc_pub_t *pub;
72
73         pub = (wlc_pub_t *) wlc_calloc(osh, unit, sizeof(wlc_pub_t));
74         if (pub == NULL) {
75                 *err = 1001;
76                 goto fail;
77         }
78
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;
84         }
85
86         /* need to init the tunables now */
87         wlc_tunables_init(pub->tunables, devid);
88
89         pub->multicast = (struct ether_addr *)wlc_calloc(osh, unit,
90                 (sizeof(struct ether_addr) * MAXMULTILIST));
91         if (pub->multicast == NULL) {
92                 *err = 1003;
93                 goto fail;
94         }
95
96         return pub;
97
98  fail:
99         wlc_pub_mfree(osh, pub);
100         return NULL;
101 }
102
103 static void wlc_pub_mfree(struct osl_info *osh, wlc_pub_t *pub)
104 {
105         if (pub == NULL)
106                 return;
107
108         if (pub->multicast)
109                 kfree(pub->multicast);
110         if (pub->tunables) {
111                 kfree(pub->tunables);
112                 pub->tunables = NULL;
113         }
114
115         kfree(pub);
116 }
117
118 wlc_bsscfg_t *wlc_bsscfg_malloc(struct osl_info *osh, uint unit)
119 {
120         wlc_bsscfg_t *cfg;
121
122         cfg = (wlc_bsscfg_t *) wlc_calloc(osh, unit, sizeof(wlc_bsscfg_t));
123         if (cfg == NULL)
124                 goto fail;
125
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;
130
131         return cfg;
132
133  fail:
134         wlc_bsscfg_mfree(osh, cfg);
135         return NULL;
136 }
137
138 void wlc_bsscfg_mfree(struct osl_info *osh, wlc_bsscfg_t *cfg)
139 {
140         if (cfg == NULL)
141                 return;
142
143         if (cfg->maclist) {
144                 kfree(cfg->maclist);
145                 cfg->maclist = NULL;
146         }
147
148         if (cfg->current_bss != NULL) {
149                 wlc_bss_info_t *current_bss = cfg->current_bss;
150                 if (current_bss->bcn_prb != NULL)
151                         kfree(current_bss->bcn_prb);
152                 kfree(current_bss);
153                 cfg->current_bss = NULL;
154         }
155
156         kfree(cfg);
157 }
158
159 void wlc_bsscfg_ID_assign(wlc_info_t *wlc, wlc_bsscfg_t *bsscfg)
160 {
161         bsscfg->ID = wlc->next_bsscfg_ID;
162         wlc->next_bsscfg_ID++;
163 }
164
165 /*
166  * The common driver entry routine. Error codes should be unique
167  */
168 wlc_info_t *wlc_attach_malloc(struct osl_info *osh, uint unit, uint *err,
169                               uint devid)
170 {
171         wlc_info_t *wlc;
172
173         wlc = (wlc_info_t *) wlc_calloc(osh, unit, sizeof(wlc_info_t));
174         if (wlc == NULL) {
175                 *err = 1002;
176                 goto fail;
177         }
178
179         wlc->hwrxoff = WL_HWRXOFF;
180
181         /* allocate wlc_pub_t state structure */
182         wlc->pub = wlc_pub_malloc(osh, unit, err, devid);
183         if (wlc->pub == NULL) {
184                 *err = 1003;
185                 goto fail;
186         }
187         wlc->pub->wlc = wlc;
188
189         /* allocate wlc_hw_info_t state structure */
190
191         wlc->hw = (wlc_hw_info_t *)wlc_calloc(osh, unit,
192                 sizeof(wlc_hw_info_t));
193         if (wlc->hw == NULL) {
194                 *err = 1005;
195                 goto fail;
196         }
197         wlc->hw->wlc = wlc;
198
199         wlc->hw->bandstate[0] = (wlc_hwband_t *)wlc_calloc(osh, unit,
200                 (sizeof(wlc_hwband_t) * MAXBANDS));
201         if (wlc->hw->bandstate[0] == NULL) {
202                 *err = 1006;
203                 goto fail;
204         } else {
205                 int i;
206
207                 for (i = 1; i < MAXBANDS; i++) {
208                         wlc->hw->bandstate[i] = (wlc_hwband_t *)
209                             ((unsigned long)wlc->hw->bandstate[0] +
210                              (sizeof(wlc_hwband_t) * i));
211                 }
212         }
213
214         wlc->modulecb = (modulecb_t *)wlc_calloc(osh, unit,
215                 sizeof(modulecb_t) * WLC_MAXMODULES);
216         if (wlc->modulecb == NULL) {
217                 *err = 1009;
218                 goto fail;
219         }
220
221         wlc->default_bss = (wlc_bss_info_t *)wlc_calloc(osh, unit,
222                 sizeof(wlc_bss_info_t));
223         if (wlc->default_bss == NULL) {
224                 *err = 1010;
225                 goto fail;
226         }
227
228         wlc->cfg = wlc_bsscfg_malloc(osh, unit);
229         if (wlc->cfg == NULL) {
230                 *err = 1011;
231                 goto fail;
232         }
233         wlc_bsscfg_ID_assign(wlc, wlc->cfg);
234
235         wlc->pkt_callback = (pkt_cb_t *)wlc_calloc(osh, unit,
236                 (sizeof(pkt_cb_t) * (wlc->pub->tunables->maxpktcb + 1)));
237         if (wlc->pkt_callback == NULL) {
238                 *err = 1013;
239                 goto fail;
240         }
241
242         wlc->wsec_def_keys[0] = (wsec_key_t *)wlc_calloc(osh, unit,
243                 (sizeof(wsec_key_t) * WLC_DEFAULT_KEYS));
244         if (wlc->wsec_def_keys[0] == NULL) {
245                 *err = 1015;
246                 goto fail;
247         } else {
248                 int i;
249                 for (i = 1; i < WLC_DEFAULT_KEYS; i++) {
250                         wlc->wsec_def_keys[i] = (wsec_key_t *)
251                             ((unsigned long)wlc->wsec_def_keys[0] +
252                              (sizeof(wsec_key_t) * i));
253                 }
254         }
255
256         wlc->protection = (wlc_protection_t *)wlc_calloc(osh, unit,
257                 sizeof(wlc_protection_t));
258         if (wlc->protection == NULL) {
259                 *err = 1016;
260                 goto fail;
261         }
262
263         wlc->stf = (wlc_stf_t *)wlc_calloc(osh, unit, sizeof(wlc_stf_t));
264         if (wlc->stf == NULL) {
265                 *err = 1017;
266                 goto fail;
267         }
268
269         wlc->bandstate[0] = (wlcband_t *)wlc_calloc(osh, unit,
270                                 (sizeof(wlcband_t) * MAXBANDS));
271         if (wlc->bandstate[0] == NULL) {
272                 *err = 1025;
273                 goto fail;
274         } else {
275                 int i;
276
277                 for (i = 1; i < MAXBANDS; i++) {
278                         wlc->bandstate[i] =
279                             (wlcband_t *) ((unsigned long)wlc->bandstate[0] +
280                                            (sizeof(wlcband_t) * i));
281                 }
282         }
283
284         wlc->corestate = (wlccore_t *)wlc_calloc(osh, unit, sizeof(wlccore_t));
285         if (wlc->corestate == NULL) {
286                 *err = 1026;
287                 goto fail;
288         }
289
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;
295         }
296
297         return wlc;
298
299  fail:
300         wlc_detach_mfree(wlc, osh);
301         return NULL;
302 }
303
304 void wlc_detach_mfree(wlc_info_t *wlc, struct osl_info *osh)
305 {
306         if (wlc == NULL)
307                 return;
308
309         if (wlc->modulecb) {
310                 kfree(wlc->modulecb);
311                 wlc->modulecb = NULL;
312         }
313
314         if (wlc->default_bss) {
315                 kfree(wlc->default_bss);
316                 wlc->default_bss = NULL;
317         }
318         if (wlc->cfg) {
319                 wlc_bsscfg_mfree(osh, wlc->cfg);
320                 wlc->cfg = NULL;
321         }
322
323         if (wlc->pkt_callback && wlc->pub && wlc->pub->tunables) {
324                 kfree(wlc->pkt_callback);
325                 wlc->pkt_callback = NULL;
326         }
327
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;
333         }
334
335         if (wlc->stf) {
336                 kfree(wlc->stf);
337                 wlc->stf = NULL;
338         }
339
340         if (wlc->bandstate[0])
341                 kfree(wlc->bandstate[0]);
342
343         if (wlc->corestate) {
344                 if (wlc->corestate->macstat_snapshot) {
345         kfree(wlc->corestate->macstat_snapshot);                        wlc->corestate->macstat_snapshot = NULL;
346                 }
347                 kfree(wlc->corestate);
348                 wlc->corestate = NULL;
349         }
350
351         if (wlc->pub) {
352                 /* free pub struct */
353                 wlc_pub_mfree(osh, wlc->pub);
354                 wlc->pub = NULL;
355         }
356
357         if (wlc->hw) {
358                 if (wlc->hw->bandstate[0]) {
359                         kfree(wlc->hw->bandstate[0]);
360                         wlc->hw->bandstate[0] = NULL;
361                 }
362
363                 /* free hw struct */
364                 kfree(wlc->hw);
365                 wlc->hw = NULL;
366         }
367
368         /* free the wlc */
369         kfree(wlc);
370         wlc = NULL;
371 }