]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/brcm80211/brcmsmac/alloc.c
dbf64d74025d2465cb7af19e124f55c75a7487a2
[mv-sheeva.git] / drivers / staging / brcm80211 / brcmsmac / 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
17 #include <brcmu_utils.h>
18 #include "types.h"
19 #include "pub.h"
20 #include "main.h"
21 #include "alloc.h"
22
23 static struct brcms_c_bsscfg *wlc_bsscfg_malloc(uint unit);
24 static void wlc_bsscfg_mfree(struct brcms_c_bsscfg *cfg);
25 static struct wlc_pub *wlc_pub_malloc(uint unit,
26                                       uint *err, uint devid);
27 static void wlc_pub_mfree(struct wlc_pub *pub);
28 static void wlc_tunables_init(wlc_tunables_t *tunables, uint devid);
29
30 static void wlc_tunables_init(wlc_tunables_t *tunables, uint devid)
31 {
32         tunables->ntxd = NTXD;
33         tunables->nrxd = NRXD;
34         tunables->rxbufsz = RXBUFSZ;
35         tunables->nrxbufpost = NRXBUFPOST;
36         tunables->maxscb = MAXSCB;
37         tunables->ampdunummpdu = AMPDU_NUM_MPDU;
38         tunables->maxpktcb = MAXPKTCB;
39         tunables->maxucodebss = WLC_MAX_UCODE_BSS;
40         tunables->maxucodebss4 = WLC_MAX_UCODE_BSS4;
41         tunables->maxbss = MAXBSS;
42         tunables->datahiwat = WLC_DATAHIWAT;
43         tunables->ampdudatahiwat = WLC_AMPDUDATAHIWAT;
44         tunables->rxbnd = RXBND;
45         tunables->txsbnd = TXSBND;
46 }
47
48 static struct wlc_pub *wlc_pub_malloc(uint unit, uint *err, uint devid)
49 {
50         struct wlc_pub *pub;
51
52         pub = kzalloc(sizeof(struct wlc_pub), GFP_ATOMIC);
53         if (pub == NULL) {
54                 *err = 1001;
55                 goto fail;
56         }
57
58         pub->tunables = kzalloc(sizeof(wlc_tunables_t), GFP_ATOMIC);
59         if (pub->tunables == NULL) {
60                 *err = 1028;
61                 goto fail;
62         }
63
64         /* need to init the tunables now */
65         wlc_tunables_init(pub->tunables, devid);
66
67         pub->multicast = kzalloc(ETH_ALEN * MAXMULTILIST, GFP_ATOMIC);
68         if (pub->multicast == NULL) {
69                 *err = 1003;
70                 goto fail;
71         }
72
73         return pub;
74
75  fail:
76         wlc_pub_mfree(pub);
77         return NULL;
78 }
79
80 static void wlc_pub_mfree(struct wlc_pub *pub)
81 {
82         if (pub == NULL)
83                 return;
84
85         kfree(pub->multicast);
86         kfree(pub->tunables);
87         kfree(pub);
88 }
89
90 static struct brcms_c_bsscfg *wlc_bsscfg_malloc(uint unit)
91 {
92         struct brcms_c_bsscfg *cfg;
93
94         cfg = kzalloc(sizeof(struct brcms_c_bsscfg), GFP_ATOMIC);
95         if (cfg == NULL)
96                 goto fail;
97
98         cfg->current_bss = kzalloc(sizeof(wlc_bss_info_t), GFP_ATOMIC);
99         if (cfg->current_bss == NULL)
100                 goto fail;
101
102         return cfg;
103
104  fail:
105         wlc_bsscfg_mfree(cfg);
106         return NULL;
107 }
108
109 static void wlc_bsscfg_mfree(struct brcms_c_bsscfg *cfg)
110 {
111         if (cfg == NULL)
112                 return;
113
114         kfree(cfg->maclist);
115         kfree(cfg->current_bss);
116         kfree(cfg);
117 }
118
119 static void wlc_bsscfg_ID_assign(struct brcms_c_info *wlc,
120                                  struct brcms_c_bsscfg *bsscfg)
121 {
122         bsscfg->ID = wlc->next_bsscfg_ID;
123         wlc->next_bsscfg_ID++;
124 }
125
126 /*
127  * The common driver entry routine. Error codes should be unique
128  */
129 struct brcms_c_info *brcms_c_attach_malloc(uint unit, uint *err, uint devid)
130 {
131         struct brcms_c_info *wlc;
132
133         wlc = kzalloc(sizeof(struct brcms_c_info), GFP_ATOMIC);
134         if (wlc == NULL) {
135                 *err = 1002;
136                 goto fail;
137         }
138
139         /* allocate struct brcms_c_pub state structure */
140         wlc->pub = wlc_pub_malloc(unit, err, devid);
141         if (wlc->pub == NULL) {
142                 *err = 1003;
143                 goto fail;
144         }
145         wlc->pub->wlc = wlc;
146
147         /* allocate struct brcms_c_hw_info state structure */
148
149         wlc->hw = kzalloc(sizeof(struct brcms_c_hw_info), GFP_ATOMIC);
150         if (wlc->hw == NULL) {
151                 *err = 1005;
152                 goto fail;
153         }
154         wlc->hw->wlc = wlc;
155
156         wlc->hw->bandstate[0] =
157                 kzalloc(sizeof(struct brcms_c_hwband) * MAXBANDS, GFP_ATOMIC);
158         if (wlc->hw->bandstate[0] == NULL) {
159                 *err = 1006;
160                 goto fail;
161         } else {
162                 int i;
163
164                 for (i = 1; i < MAXBANDS; i++) {
165                         wlc->hw->bandstate[i] = (struct brcms_c_hwband *)
166                             ((unsigned long)wlc->hw->bandstate[0] +
167                              (sizeof(struct brcms_c_hwband) * i));
168                 }
169         }
170
171         wlc->modulecb =
172                 kzalloc(sizeof(struct modulecb) * WLC_MAXMODULES, GFP_ATOMIC);
173         if (wlc->modulecb == NULL) {
174                 *err = 1009;
175                 goto fail;
176         }
177
178         wlc->default_bss = kzalloc(sizeof(wlc_bss_info_t), GFP_ATOMIC);
179         if (wlc->default_bss == NULL) {
180                 *err = 1010;
181                 goto fail;
182         }
183
184         wlc->cfg = wlc_bsscfg_malloc(unit);
185         if (wlc->cfg == NULL) {
186                 *err = 1011;
187                 goto fail;
188         }
189         wlc_bsscfg_ID_assign(wlc, wlc->cfg);
190
191         wlc->wsec_def_keys[0] =
192                 kzalloc(sizeof(wsec_key_t) * WLC_DEFAULT_KEYS, GFP_ATOMIC);
193         if (wlc->wsec_def_keys[0] == NULL) {
194                 *err = 1015;
195                 goto fail;
196         } else {
197                 int i;
198                 for (i = 1; i < WLC_DEFAULT_KEYS; i++) {
199                         wlc->wsec_def_keys[i] = (wsec_key_t *)
200                             ((unsigned long)wlc->wsec_def_keys[0] +
201                              (sizeof(wsec_key_t) * i));
202                 }
203         }
204
205         wlc->protection = kzalloc(sizeof(struct brcms_c_protection),
206                                   GFP_ATOMIC);
207         if (wlc->protection == NULL) {
208                 *err = 1016;
209                 goto fail;
210         }
211
212         wlc->stf = kzalloc(sizeof(struct brcms_c_stf), GFP_ATOMIC);
213         if (wlc->stf == NULL) {
214                 *err = 1017;
215                 goto fail;
216         }
217
218         wlc->bandstate[0] =
219                 kzalloc(sizeof(struct brcms_c_band)*MAXBANDS, GFP_ATOMIC);
220         if (wlc->bandstate[0] == NULL) {
221                 *err = 1025;
222                 goto fail;
223         } else {
224                 int i;
225
226                 for (i = 1; i < MAXBANDS; i++) {
227                         wlc->bandstate[i] = (struct brcms_c_band *)
228                                 ((unsigned long)wlc->bandstate[0]
229                                 + (sizeof(struct brcms_c_band)*i));
230                 }
231         }
232
233         wlc->corestate = kzalloc(sizeof(struct brcms_c_core), GFP_ATOMIC);
234         if (wlc->corestate == NULL) {
235                 *err = 1026;
236                 goto fail;
237         }
238
239         wlc->corestate->macstat_snapshot =
240                 kzalloc(sizeof(macstat_t), GFP_ATOMIC);
241         if (wlc->corestate->macstat_snapshot == NULL) {
242                 *err = 1027;
243                 goto fail;
244         }
245
246         return wlc;
247
248  fail:
249         brcms_c_detach_mfree(wlc);
250         return NULL;
251 }
252
253 void brcms_c_detach_mfree(struct brcms_c_info *wlc)
254 {
255         if (wlc == NULL)
256                 return;
257
258         wlc_bsscfg_mfree(wlc->cfg);
259         wlc_pub_mfree(wlc->pub);
260         kfree(wlc->modulecb);
261         kfree(wlc->default_bss);
262         kfree(wlc->wsec_def_keys[0]);
263         kfree(wlc->protection);
264         kfree(wlc->stf);
265         kfree(wlc->bandstate[0]);
266         kfree(wlc->corestate->macstat_snapshot);
267         kfree(wlc->corestate);
268         kfree(wlc->hw->bandstate[0]);
269         kfree(wlc->hw);
270
271         /* free the wlc */
272         kfree(wlc);
273         wlc = NULL;
274 }