]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/iwm/if_iwm_phy_db.c
MFV r304060:
[FreeBSD/FreeBSD.git] / sys / dev / iwm / if_iwm_phy_db.c
1 /*      $OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $ */
2
3 /*
4  * Copyright (c) 2014 genua mbh <info@genua.de>
5  * Copyright (c) 2014 Fixup Software Ltd.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19
20 /*-
21  * Based on BSD-licensed source modules in the Linux iwlwifi driver,
22  * which were used as the reference documentation for this implementation.
23  *
24  * Driver version we are currently based off of is
25  * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd)
26  *
27  ***********************************************************************
28  *
29  * This file is provided under a dual BSD/GPLv2 license.  When using or
30  * redistributing this file, you may do so under either license.
31  *
32  * GPL LICENSE SUMMARY
33  *
34  * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved.
35  *
36  * This program is free software; you can redistribute it and/or modify
37  * it under the terms of version 2 of the GNU General Public License as
38  * published by the Free Software Foundation.
39  *
40  * This program is distributed in the hope that it will be useful, but
41  * WITHOUT ANY WARRANTY; without even the implied warranty of
42  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
43  * General Public License for more details.
44  *
45  * You should have received a copy of the GNU General Public License
46  * along with this program; if not, write to the Free Software
47  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
48  * USA
49  *
50  * The full GNU General Public License is included in this distribution
51  * in the file called COPYING.
52  *
53  * Contact Information:
54  *  Intel Linux Wireless <ilw@linux.intel.com>
55  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
56  *
57  *
58  * BSD LICENSE
59  *
60  * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
61  * All rights reserved.
62  *
63  * Redistribution and use in source and binary forms, with or without
64  * modification, are permitted provided that the following conditions
65  * are met:
66  *
67  *  * Redistributions of source code must retain the above copyright
68  *    notice, this list of conditions and the following disclaimer.
69  *  * Redistributions in binary form must reproduce the above copyright
70  *    notice, this list of conditions and the following disclaimer in
71  *    the documentation and/or other materials provided with the
72  *    distribution.
73  *  * Neither the name Intel Corporation nor the names of its
74  *    contributors may be used to endorse or promote products derived
75  *    from this software without specific prior written permission.
76  *
77  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
78  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
79  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
80  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
81  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
82  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
83  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
84  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
85  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
86  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
87  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88  */
89
90 /*-
91  * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini@free.fr>
92  *
93  * Permission to use, copy, modify, and distribute this software for any
94  * purpose with or without fee is hereby granted, provided that the above
95  * copyright notice and this permission notice appear in all copies.
96  *
97  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
98  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
99  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
100  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
101  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
102  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
103  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
104  */
105 #include <sys/cdefs.h>
106 __FBSDID("$FreeBSD$");
107
108 #include "opt_wlan.h"
109
110 #include <sys/param.h>
111 #include <sys/bus.h>
112 #include <sys/conf.h>
113 #include <sys/endian.h>
114 #include <sys/firmware.h>
115 #include <sys/kernel.h>
116 #include <sys/malloc.h>
117 #include <sys/mbuf.h>
118 #include <sys/mutex.h>
119 #include <sys/module.h>
120 #include <sys/proc.h>
121 #include <sys/rman.h>
122 #include <sys/socket.h>
123 #include <sys/sockio.h>
124 #include <sys/sysctl.h>
125 #include <sys/linker.h>
126
127 #include <machine/bus.h>
128 #include <machine/endian.h>
129 #include <machine/resource.h>
130
131 #include <dev/pci/pcivar.h>
132 #include <dev/pci/pcireg.h>
133
134 #include <net/bpf.h>
135
136 #include <net/if.h>
137 #include <net/if_var.h>
138 #include <net/if_arp.h>
139 #include <net/if_dl.h>
140 #include <net/if_media.h>
141 #include <net/if_types.h>
142
143 #include <netinet/in.h>
144 #include <netinet/in_systm.h>
145 #include <netinet/if_ether.h>
146 #include <netinet/ip.h>
147
148 #include <net80211/ieee80211_var.h>
149 #include <net80211/ieee80211_regdomain.h>
150 #include <net80211/ieee80211_ratectl.h>
151 #include <net80211/ieee80211_radiotap.h>
152
153 #include <dev/iwm/if_iwmreg.h>
154 #include <dev/iwm/if_iwmvar.h>
155 #include <dev/iwm/if_iwm_debug.h>
156 #include <dev/iwm/if_iwm_util.h>
157 #include <dev/iwm/if_iwm_phy_db.h>
158
159 /*
160  * BEGIN iwl-phy-db.c
161  */
162 /*
163  * get phy db section: returns a pointer to a phy db section specified by
164  * type and channel group id.
165  */
166 static struct iwm_phy_db_entry *
167 iwm_phy_db_get_section(struct iwm_softc *sc,
168         enum iwm_phy_db_section_type type, uint16_t chg_id)
169 {
170         struct iwm_phy_db *phy_db = &sc->sc_phy_db;
171
172         if (type >= IWM_PHY_DB_MAX)
173                 return NULL;
174
175         switch (type) {
176         case IWM_PHY_DB_CFG:
177                 return &phy_db->cfg;
178         case IWM_PHY_DB_CALIB_NCH:
179                 return &phy_db->calib_nch;
180         case IWM_PHY_DB_CALIB_CHG_PAPD:
181                 if (chg_id >= IWM_NUM_PAPD_CH_GROUPS)
182                         return NULL;
183                 return &phy_db->calib_ch_group_papd[chg_id];
184         case IWM_PHY_DB_CALIB_CHG_TXP:
185                 if (chg_id >= IWM_NUM_TXP_CH_GROUPS)
186                         return NULL;
187                 return &phy_db->calib_ch_group_txp[chg_id];
188         default:
189                 return NULL;
190         }
191         return NULL;
192 }
193
194 int
195 iwm_phy_db_set_section(struct iwm_softc *sc,
196         struct iwm_calib_res_notif_phy_db *phy_db_notif)
197 {
198         enum iwm_phy_db_section_type type = le16toh(phy_db_notif->type);
199         uint16_t size  = le16toh(phy_db_notif->length);
200         struct iwm_phy_db_entry *entry;
201         uint16_t chg_id = 0;
202
203         if (type == IWM_PHY_DB_CALIB_CHG_PAPD ||
204             type == IWM_PHY_DB_CALIB_CHG_TXP)
205                 chg_id = le16toh(*(uint16_t *)phy_db_notif->data);
206
207         entry = iwm_phy_db_get_section(sc, type, chg_id);
208         if (!entry)
209                 return EINVAL;
210
211         if (entry->data)
212                 free(entry->data, M_DEVBUF);
213         entry->data = malloc(size, M_DEVBUF, M_NOWAIT);
214         if (!entry->data) {
215                 entry->size = 0;
216                 return ENOMEM;
217         }
218         memcpy(entry->data, phy_db_notif->data, size);
219         entry->size = size;
220
221         IWM_DPRINTF(sc, IWM_DEBUG_RESET,
222             "%s(%d): [PHYDB]SET: Type %d , Size: %d, data: %p\n",
223             __func__, __LINE__, type, size, entry->data);
224
225         return 0;
226 }
227
228 static int
229 iwm_is_valid_channel(uint16_t ch_id)
230 {
231         if (ch_id <= 14 ||
232             (36 <= ch_id && ch_id <= 64 && ch_id % 4 == 0) ||
233             (100 <= ch_id && ch_id <= 140 && ch_id % 4 == 0) ||
234             (145 <= ch_id && ch_id <= 165 && ch_id % 4 == 1))
235                 return 1;
236         return 0;
237 }
238
239 static uint8_t
240 iwm_ch_id_to_ch_index(uint16_t ch_id)
241 {
242         if (!iwm_is_valid_channel(ch_id))
243                 return 0xff;
244
245         if (ch_id <= 14)
246                 return ch_id - 1;
247         if (ch_id <= 64)
248                 return (ch_id + 20) / 4;
249         if (ch_id <= 140)
250                 return (ch_id - 12) / 4;
251         return (ch_id - 13) / 4;
252 }
253
254
255 static uint16_t
256 iwm_channel_id_to_papd(uint16_t ch_id)
257 {
258         if (!iwm_is_valid_channel(ch_id))
259                 return 0xff;
260
261         if (1 <= ch_id && ch_id <= 14)
262                 return 0;
263         if (36 <= ch_id && ch_id <= 64)
264                 return 1;
265         if (100 <= ch_id && ch_id <= 140)
266                 return 2;
267         return 3;
268 }
269
270 static uint16_t
271 iwm_channel_id_to_txp(struct iwm_softc *sc, uint16_t ch_id)
272 {
273         struct iwm_phy_db *phy_db = &sc->sc_phy_db;
274         struct iwm_phy_db_chg_txp *txp_chg;
275         int i;
276         uint8_t ch_index = iwm_ch_id_to_ch_index(ch_id);
277
278         if (ch_index == 0xff)
279                 return 0xff;
280
281         for (i = 0; i < IWM_NUM_TXP_CH_GROUPS; i++) {
282                 txp_chg = (void *)phy_db->calib_ch_group_txp[i].data;
283                 if (!txp_chg)
284                         return 0xff;
285                 /*
286                  * Looking for the first channel group that its max channel is
287                  * higher then wanted channel.
288                  */
289                 if (le16toh(txp_chg->max_channel_idx) >= ch_index)
290                         return i;
291         }
292         return 0xff;
293 }
294
295 static int
296 iwm_phy_db_get_section_data(struct iwm_softc *sc,
297         uint32_t type, uint8_t **data, uint16_t *size, uint16_t ch_id)
298 {
299         struct iwm_phy_db_entry *entry;
300         uint16_t ch_group_id = 0;
301
302         IWM_DPRINTF(sc, IWM_DEBUG_RESET, "->%s\n", __func__);
303         /* find wanted channel group */
304         if (type == IWM_PHY_DB_CALIB_CHG_PAPD)
305                 ch_group_id = iwm_channel_id_to_papd(ch_id);
306         else if (type == IWM_PHY_DB_CALIB_CHG_TXP)
307                 ch_group_id = iwm_channel_id_to_txp(sc, ch_id);
308
309         entry = iwm_phy_db_get_section(sc, type, ch_group_id);
310         if (!entry)
311                 return EINVAL;
312
313         *data = entry->data;
314         *size = entry->size;
315
316         IWM_DPRINTF(sc, IWM_DEBUG_RESET,
317             "%s(%d): [PHYDB] GET: Type %d , Size: %d\n",
318             __func__, __LINE__, type, *size);
319
320         return 0;
321 }
322
323 static int
324 iwm_send_phy_db_cmd(struct iwm_softc *sc, uint16_t type,
325         uint16_t length, void *data)
326 {
327         struct iwm_phy_db_cmd phy_db_cmd;
328         struct iwm_host_cmd cmd = {
329                 .id = IWM_PHY_DB_CMD,
330                 .flags = IWM_CMD_SYNC,
331         };
332
333         IWM_DPRINTF(sc, IWM_DEBUG_CMD,
334             "Sending PHY-DB hcmd of type %d, of length %d\n",
335             type, length);
336
337         /* Set phy db cmd variables */
338         phy_db_cmd.type = le16toh(type);
339         phy_db_cmd.length = le16toh(length);
340
341         /* Set hcmd variables */
342         cmd.data[0] = &phy_db_cmd;
343         cmd.len[0] = sizeof(struct iwm_phy_db_cmd);
344         cmd.data[1] = data;
345         cmd.len[1] = length;
346
347         return iwm_send_cmd(sc, &cmd);
348 }
349
350 static int
351 iwm_phy_db_send_all_channel_groups(struct iwm_softc *sc,
352         enum iwm_phy_db_section_type type, uint8_t max_ch_groups)
353 {
354         uint16_t i;
355         int err;
356         struct iwm_phy_db_entry *entry;
357
358         /* Send all the channel-specific groups to operational fw */
359         for (i = 0; i < max_ch_groups; i++) {
360                 entry = iwm_phy_db_get_section(sc, type, i);
361                 if (!entry)
362                         return EINVAL;
363
364                 if (!entry->size)
365                         continue;
366
367                 /* Send the requested PHY DB section */
368                 err = iwm_send_phy_db_cmd(sc, type, entry->size, entry->data);
369                 if (err) {
370                         IWM_DPRINTF(sc, IWM_DEBUG_CMD,
371                             "%s: Can't SEND phy_db section %d (%d), "
372                             "err %d\n", __func__, type, i, err);
373                         return err;
374                 }
375
376                 DELAY(1000);
377                 IWM_DPRINTF(sc, IWM_DEBUG_CMD,
378                     "Sent PHY_DB HCMD, type = %d num = %d\n", type, i);
379         }
380
381         return 0;
382 }
383
384 int
385 iwm_send_phy_db_data(struct iwm_softc *sc)
386 {
387         uint8_t *data = NULL;
388         uint16_t size = 0;
389         int err;
390
391         IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
392             "%s: Sending phy db data and configuration to runtime image\n",
393             __func__);
394
395         /* Send PHY DB CFG section */
396         err = iwm_phy_db_get_section_data(sc, IWM_PHY_DB_CFG, &data, &size, 0);
397         if (err) {
398                 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
399                     "%s: Cannot get Phy DB cfg section, %d\n",
400                     __func__, err);
401                 return err;
402         }
403
404         err = iwm_send_phy_db_cmd(sc, IWM_PHY_DB_CFG, size, data);
405         if (err) {
406                 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
407                     "%s: Cannot send HCMD of Phy DB cfg section, %d\n",
408                     __func__, err);
409                 return err;
410         }
411
412         err = iwm_phy_db_get_section_data(sc, IWM_PHY_DB_CALIB_NCH,
413             &data, &size, 0);
414         if (err) {
415                 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
416                     "%s: Cannot get Phy DB non specific channel section, "
417                     "%d\n", __func__, err);
418                 return err;
419         }
420
421         err = iwm_send_phy_db_cmd(sc, IWM_PHY_DB_CALIB_NCH, size, data);
422         if (err) {
423                 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
424                     "%s: Cannot send HCMD of Phy DB non specific channel "
425                     "sect, %d\n", __func__, err);
426                 return err;
427         }
428
429         /* Send all the TXP channel specific data */
430         err = iwm_phy_db_send_all_channel_groups(sc,
431             IWM_PHY_DB_CALIB_CHG_PAPD, IWM_NUM_PAPD_CH_GROUPS);
432         if (err) {
433                 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
434                     "%s: Cannot send channel specific PAPD groups, %d\n",
435                     __func__, err);
436                 return err;
437         }
438
439         /* Send all the TXP channel specific data */
440         err = iwm_phy_db_send_all_channel_groups(sc,
441             IWM_PHY_DB_CALIB_CHG_TXP, IWM_NUM_TXP_CH_GROUPS);
442         if (err) {
443                 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
444                     "%s: Cannot send channel specific TX power groups, "
445                     "%d\n", __func__, err);
446                 return err;
447         }
448
449         IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
450             "%s: Finished sending phy db non channel data\n",
451             __func__);
452         return 0;
453 }
454
455 static void
456 iwm_phy_db_free_section(struct iwm_softc *sc,
457     enum iwm_phy_db_section_type type, uint16_t chg_id)
458 {
459         struct iwm_phy_db_entry *entry =
460             iwm_phy_db_get_section(sc, type, chg_id);
461         if (!entry)
462                 return;
463
464         if (entry->data != NULL)
465                 free(entry->data, M_DEVBUF);
466         entry->data = NULL;
467         entry->size = 0;
468 }
469
470 void
471 iwm_phy_db_free(struct iwm_softc *sc)
472 {
473         int i;
474
475         iwm_phy_db_free_section(sc, IWM_PHY_DB_CFG, 0);
476         iwm_phy_db_free_section(sc, IWM_PHY_DB_CALIB_NCH, 0);
477
478         for (i = 0; i < IWM_NUM_PAPD_CH_GROUPS; i++)
479                 iwm_phy_db_free_section(sc, IWM_PHY_DB_CALIB_CHG_PAPD, i);
480
481         for (i = 0; i < IWM_NUM_TXP_CH_GROUPS; i++)
482                 iwm_phy_db_free_section(sc, IWM_PHY_DB_CALIB_CHG_TXP, i);
483 }