staging: brcm80211: remove include file sbhndpio.h
[firefly-linux-kernel-4.4.55.git] / drivers / staging / brcm80211 / brcmsmac / 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/types.h>
18
19 #include <osl.h>
20 #include <bcmdefs.h>
21 #include <bcmutils.h>
22 #include <siutils.h>
23 #include <wlioctl.h>
24 #include <sbhnddma.h>
25
26 #include "d11.h"
27 #include "wlc_types.h"
28 #include "wlc_cfg.h"
29 #include "wlc_scb.h"
30 #include "wlc_pub.h"
31 #include "wlc_key.h"
32 #include "wlc_alloc.h"
33 #include "wl_dbg.h"
34 #include "wlc_rate.h"
35 #include "wlc_bsscfg.h"
36 #include "phy/wlc_phy_hal.h"
37 #include "wlc_channel.h"
38 #include "wlc_mac80211.h"
39
40 static struct wlc_bsscfg *wlc_bsscfg_malloc(struct osl_info *osh, uint unit);
41 static void wlc_bsscfg_mfree(struct osl_info *osh, struct wlc_bsscfg *cfg);
42 static struct wlc_pub *wlc_pub_malloc(struct osl_info *osh, uint unit,
43                                       uint *err, uint devid);
44 static void wlc_pub_mfree(struct osl_info *osh, struct wlc_pub *pub);
45 static void wlc_tunables_init(wlc_tunables_t *tunables, uint devid);
46
47 void *wlc_calloc(struct osl_info *osh, uint unit, uint size)
48 {
49         void *item;
50
51         item = kzalloc(size, GFP_ATOMIC);
52         if (item == NULL)
53                 WL_ERROR("wl%d: %s: out of memory\n", unit, __func__);
54         return item;
55 }
56
57 void wlc_tunables_init(wlc_tunables_t *tunables, uint devid)
58 {
59         tunables->ntxd = NTXD;
60         tunables->nrxd = NRXD;
61         tunables->rxbufsz = RXBUFSZ;
62         tunables->nrxbufpost = NRXBUFPOST;
63         tunables->maxscb = MAXSCB;
64         tunables->ampdunummpdu = AMPDU_NUM_MPDU;
65         tunables->maxpktcb = MAXPKTCB;
66         tunables->maxucodebss = WLC_MAX_UCODE_BSS;
67         tunables->maxucodebss4 = WLC_MAX_UCODE_BSS4;
68         tunables->maxbss = MAXBSS;
69         tunables->datahiwat = WLC_DATAHIWAT;
70         tunables->ampdudatahiwat = WLC_AMPDUDATAHIWAT;
71         tunables->rxbnd = RXBND;
72         tunables->txsbnd = TXSBND;
73 }
74
75 static struct wlc_pub *wlc_pub_malloc(struct osl_info *osh, uint unit,
76                                       uint *err, uint devid)
77 {
78         struct wlc_pub *pub;
79
80         pub = wlc_calloc(osh, unit, sizeof(struct wlc_pub));
81         if (pub == NULL) {
82                 *err = 1001;
83                 goto fail;
84         }
85
86         pub->tunables = wlc_calloc(osh, unit,
87                 sizeof(wlc_tunables_t));
88         if (pub->tunables == NULL) {
89                 *err = 1028;
90                 goto fail;
91         }
92
93         /* need to init the tunables now */
94         wlc_tunables_init(pub->tunables, devid);
95
96         pub->_cnt = wlc_calloc(osh, unit, sizeof(struct wl_cnt));
97         if (pub->_cnt == NULL)
98                 goto fail;
99
100         pub->multicast = (u8 *)wlc_calloc(osh, unit,
101                 (ETH_ALEN * MAXMULTILIST));
102         if (pub->multicast == NULL) {
103                 *err = 1003;
104                 goto fail;
105         }
106
107         return pub;
108
109  fail:
110         wlc_pub_mfree(osh, pub);
111         return NULL;
112 }
113
114 static void wlc_pub_mfree(struct osl_info *osh, struct wlc_pub *pub)
115 {
116         if (pub == NULL)
117                 return;
118
119         kfree(pub->multicast);
120         kfree(pub->_cnt);
121         kfree(pub->tunables);
122         kfree(pub);
123 }
124
125 static wlc_bsscfg_t *wlc_bsscfg_malloc(struct osl_info *osh, uint unit)
126 {
127         wlc_bsscfg_t *cfg;
128
129         cfg = (wlc_bsscfg_t *) wlc_calloc(osh, unit, sizeof(wlc_bsscfg_t));
130         if (cfg == NULL)
131                 goto fail;
132
133         cfg->current_bss = (wlc_bss_info_t *)wlc_calloc(osh, unit,
134                 sizeof(wlc_bss_info_t));
135         if (cfg->current_bss == NULL)
136                 goto fail;
137
138         return cfg;
139
140  fail:
141         wlc_bsscfg_mfree(osh, cfg);
142         return NULL;
143 }
144
145 static void wlc_bsscfg_mfree(struct osl_info *osh, wlc_bsscfg_t *cfg)
146 {
147         if (cfg == NULL)
148                 return;
149
150         if (cfg->maclist) {
151                 kfree(cfg->maclist);
152                 cfg->maclist = NULL;
153         }
154
155         if (cfg->current_bss != NULL) {
156                 wlc_bss_info_t *current_bss = cfg->current_bss;
157                 kfree(current_bss);
158                 cfg->current_bss = NULL;
159         }
160
161         kfree(cfg);
162 }
163
164 void wlc_bsscfg_ID_assign(struct wlc_info *wlc, wlc_bsscfg_t *bsscfg)
165 {
166         bsscfg->ID = wlc->next_bsscfg_ID;
167         wlc->next_bsscfg_ID++;
168 }
169
170 /*
171  * The common driver entry routine. Error codes should be unique
172  */
173 struct wlc_info *wlc_attach_malloc(struct osl_info *osh, uint unit, uint *err,
174                               uint devid)
175 {
176         struct wlc_info *wlc;
177
178         wlc = (struct wlc_info *) wlc_calloc(osh, unit,
179                                              sizeof(struct wlc_info));
180         if (wlc == NULL) {
181                 *err = 1002;
182                 goto fail;
183         }
184
185         wlc->hwrxoff = WL_HWRXOFF;
186
187         /* allocate struct wlc_pub state structure */
188         wlc->pub = wlc_pub_malloc(osh, unit, err, devid);
189         if (wlc->pub == NULL) {
190                 *err = 1003;
191                 goto fail;
192         }
193         wlc->pub->wlc = wlc;
194
195         /* allocate struct wlc_hw_info state structure */
196
197         wlc->hw = (struct wlc_hw_info *)wlc_calloc(osh, unit,
198                 sizeof(struct wlc_hw_info));
199         if (wlc->hw == NULL) {
200                 *err = 1005;
201                 goto fail;
202         }
203         wlc->hw->wlc = wlc;
204
205         wlc->hw->bandstate[0] = wlc_calloc(osh, unit,
206                 (sizeof(struct wlc_hwband) * MAXBANDS));
207         if (wlc->hw->bandstate[0] == NULL) {
208                 *err = 1006;
209                 goto fail;
210         } else {
211                 int i;
212
213                 for (i = 1; i < MAXBANDS; i++) {
214                         wlc->hw->bandstate[i] = (struct wlc_hwband *)
215                             ((unsigned long)wlc->hw->bandstate[0] +
216                              (sizeof(struct wlc_hwband) * i));
217                 }
218         }
219
220         wlc->modulecb = wlc_calloc(osh, unit,
221                 sizeof(struct modulecb) * WLC_MAXMODULES);
222         if (wlc->modulecb == NULL) {
223                 *err = 1009;
224                 goto fail;
225         }
226
227         wlc->default_bss = (wlc_bss_info_t *)wlc_calloc(osh, unit,
228                 sizeof(wlc_bss_info_t));
229         if (wlc->default_bss == NULL) {
230                 *err = 1010;
231                 goto fail;
232         }
233
234         wlc->cfg = wlc_bsscfg_malloc(osh, unit);
235         if (wlc->cfg == NULL) {
236                 *err = 1011;
237                 goto fail;
238         }
239         wlc_bsscfg_ID_assign(wlc, wlc->cfg);
240
241         wlc->pkt_callback = wlc_calloc(osh, unit,
242                 (sizeof(struct pkt_cb) * (wlc->pub->tunables->maxpktcb + 1)));
243         if (wlc->pkt_callback == NULL) {
244                 *err = 1013;
245                 goto fail;
246         }
247
248         wlc->wsec_def_keys[0] = (wsec_key_t *)wlc_calloc(osh, unit,
249                 (sizeof(wsec_key_t) * WLC_DEFAULT_KEYS));
250         if (wlc->wsec_def_keys[0] == NULL) {
251                 *err = 1015;
252                 goto fail;
253         } else {
254                 int i;
255                 for (i = 1; i < WLC_DEFAULT_KEYS; i++) {
256                         wlc->wsec_def_keys[i] = (wsec_key_t *)
257                             ((unsigned long)wlc->wsec_def_keys[0] +
258                              (sizeof(wsec_key_t) * i));
259                 }
260         }
261
262         wlc->protection = wlc_calloc(osh, unit,
263                 sizeof(struct wlc_protection));
264         if (wlc->protection == NULL) {
265                 *err = 1016;
266                 goto fail;
267         }
268
269         wlc->stf = wlc_calloc(osh, unit, sizeof(struct wlc_stf));
270         if (wlc->stf == NULL) {
271                 *err = 1017;
272                 goto fail;
273         }
274
275         wlc->bandstate[0] = (struct wlcband *)wlc_calloc(osh, unit,
276                                 (sizeof(struct wlcband)*MAXBANDS));
277         if (wlc->bandstate[0] == NULL) {
278                 *err = 1025;
279                 goto fail;
280         } else {
281                 int i;
282
283                 for (i = 1; i < MAXBANDS; i++) {
284                         wlc->bandstate[i] =
285                             (struct wlcband *) ((unsigned long)wlc->bandstate[0]
286                             + (sizeof(struct wlcband)*i));
287                 }
288         }
289
290         wlc->corestate = (struct wlccore *)wlc_calloc(osh, unit,
291                                                       sizeof(struct wlccore));
292         if (wlc->corestate == NULL) {
293                 *err = 1026;
294                 goto fail;
295         }
296
297         wlc->corestate->macstat_snapshot =
298                 (macstat_t *)wlc_calloc(osh, unit, sizeof(macstat_t));
299         if (wlc->corestate->macstat_snapshot == NULL) {
300                 *err = 1027;
301                 goto fail;
302         }
303
304         return wlc;
305
306  fail:
307         wlc_detach_mfree(wlc, osh);
308         return NULL;
309 }
310
311 void wlc_detach_mfree(struct wlc_info *wlc, struct osl_info *osh)
312 {
313         if (wlc == NULL)
314                 return;
315
316         if (wlc->modulecb) {
317                 kfree(wlc->modulecb);
318                 wlc->modulecb = NULL;
319         }
320
321         if (wlc->default_bss) {
322                 kfree(wlc->default_bss);
323                 wlc->default_bss = NULL;
324         }
325         if (wlc->cfg) {
326                 wlc_bsscfg_mfree(osh, wlc->cfg);
327                 wlc->cfg = NULL;
328         }
329
330         if (wlc->pkt_callback && wlc->pub && wlc->pub->tunables) {
331                 kfree(wlc->pkt_callback);
332                 wlc->pkt_callback = NULL;
333         }
334
335         if (wlc->wsec_def_keys[0])
336                 kfree(wlc->wsec_def_keys[0]);
337         if (wlc->protection) {
338                 kfree(wlc->protection);
339                 wlc->protection = NULL;
340         }
341
342         if (wlc->stf) {
343                 kfree(wlc->stf);
344                 wlc->stf = NULL;
345         }
346
347         if (wlc->bandstate[0])
348                 kfree(wlc->bandstate[0]);
349
350         if (wlc->corestate) {
351                 if (wlc->corestate->macstat_snapshot) {
352         kfree(wlc->corestate->macstat_snapshot);                        wlc->corestate->macstat_snapshot = NULL;
353                 }
354                 kfree(wlc->corestate);
355                 wlc->corestate = NULL;
356         }
357
358         if (wlc->pub) {
359                 /* free pub struct */
360                 wlc_pub_mfree(osh, wlc->pub);
361                 wlc->pub = NULL;
362         }
363
364         if (wlc->hw) {
365                 if (wlc->hw->bandstate[0]) {
366                         kfree(wlc->hw->bandstate[0]);
367                         wlc->hw->bandstate[0] = NULL;
368                 }
369
370                 /* free hw struct */
371                 kfree(wlc->hw);
372                 wlc->hw = NULL;
373         }
374
375         /* free the wlc */
376         kfree(wlc);
377         wlc = NULL;
378 }