Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[~shefty/rdma-dev.git] / drivers / net / wireless / iwlegacy / common.c
1 /******************************************************************************
2  *
3  * GPL LICENSE SUMMARY
4  *
5  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of version 2 of the GNU General Public License as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19  * USA
20  *
21  * The full GNU General Public License is included in this distribution
22  * in the file called LICENSE.GPL.
23  *
24  * Contact Information:
25  *  Intel Linux Wireless <ilw@linux.intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *****************************************************************************/
28
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/etherdevice.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/types.h>
35 #include <linux/lockdep.h>
36 #include <linux/init.h>
37 #include <linux/pci.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/delay.h>
40 #include <linux/skbuff.h>
41 #include <net/mac80211.h>
42
43 #include "common.h"
44
45 int
46 _il_poll_bit(struct il_priv *il, u32 addr, u32 bits, u32 mask, int timeout)
47 {
48         const int interval = 10; /* microseconds */
49         int t = 0;
50
51         do {
52                 if ((_il_rd(il, addr) & mask) == (bits & mask))
53                         return t;
54                 udelay(interval);
55                 t += interval;
56         } while (t < timeout);
57
58         return -ETIMEDOUT;
59 }
60 EXPORT_SYMBOL(_il_poll_bit);
61
62 void
63 il_set_bit(struct il_priv *p, u32 r, u32 m)
64 {
65         unsigned long reg_flags;
66
67         spin_lock_irqsave(&p->reg_lock, reg_flags);
68         _il_set_bit(p, r, m);
69         spin_unlock_irqrestore(&p->reg_lock, reg_flags);
70 }
71 EXPORT_SYMBOL(il_set_bit);
72
73 void
74 il_clear_bit(struct il_priv *p, u32 r, u32 m)
75 {
76         unsigned long reg_flags;
77
78         spin_lock_irqsave(&p->reg_lock, reg_flags);
79         _il_clear_bit(p, r, m);
80         spin_unlock_irqrestore(&p->reg_lock, reg_flags);
81 }
82 EXPORT_SYMBOL(il_clear_bit);
83
84 bool
85 _il_grab_nic_access(struct il_priv *il)
86 {
87         int ret;
88         u32 val;
89
90         /* this bit wakes up the NIC */
91         _il_set_bit(il, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
92
93         /*
94          * These bits say the device is running, and should keep running for
95          * at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
96          * but they do not indicate that embedded SRAM is restored yet;
97          * 3945 and 4965 have volatile SRAM, and must save/restore contents
98          * to/from host DRAM when sleeping/waking for power-saving.
99          * Each direction takes approximately 1/4 millisecond; with this
100          * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
101          * series of register accesses are expected (e.g. reading Event Log),
102          * to keep device from sleeping.
103          *
104          * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
105          * SRAM is okay/restored.  We don't check that here because this call
106          * is just for hardware register access; but GP1 MAC_SLEEP check is a
107          * good idea before accessing 3945/4965 SRAM (e.g. reading Event Log).
108          *
109          */
110         ret =
111             _il_poll_bit(il, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
112                          (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
113                           CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
114         if (unlikely(ret < 0)) {
115                 val = _il_rd(il, CSR_GP_CNTRL);
116                 WARN_ONCE(1, "Timeout waiting for ucode processor access "
117                              "(CSR_GP_CNTRL 0x%08x)\n", val);
118                 _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_FORCE_NMI);
119                 return false;
120         }
121
122         return true;
123 }
124 EXPORT_SYMBOL_GPL(_il_grab_nic_access);
125
126 int
127 il_poll_bit(struct il_priv *il, u32 addr, u32 mask, int timeout)
128 {
129         const int interval = 10; /* microseconds */
130         int t = 0;
131
132         do {
133                 if ((il_rd(il, addr) & mask) == mask)
134                         return t;
135                 udelay(interval);
136                 t += interval;
137         } while (t < timeout);
138
139         return -ETIMEDOUT;
140 }
141 EXPORT_SYMBOL(il_poll_bit);
142
143 u32
144 il_rd_prph(struct il_priv *il, u32 reg)
145 {
146         unsigned long reg_flags;
147         u32 val;
148
149         spin_lock_irqsave(&il->reg_lock, reg_flags);
150         _il_grab_nic_access(il);
151         val = _il_rd_prph(il, reg);
152         _il_release_nic_access(il);
153         spin_unlock_irqrestore(&il->reg_lock, reg_flags);
154         return val;
155 }
156 EXPORT_SYMBOL(il_rd_prph);
157
158 void
159 il_wr_prph(struct il_priv *il, u32 addr, u32 val)
160 {
161         unsigned long reg_flags;
162
163         spin_lock_irqsave(&il->reg_lock, reg_flags);
164         if (likely(_il_grab_nic_access(il))) {
165                 _il_wr_prph(il, addr, val);
166                 _il_release_nic_access(il);
167         }
168         spin_unlock_irqrestore(&il->reg_lock, reg_flags);
169 }
170 EXPORT_SYMBOL(il_wr_prph);
171
172 u32
173 il_read_targ_mem(struct il_priv *il, u32 addr)
174 {
175         unsigned long reg_flags;
176         u32 value;
177
178         spin_lock_irqsave(&il->reg_lock, reg_flags);
179         _il_grab_nic_access(il);
180
181         _il_wr(il, HBUS_TARG_MEM_RADDR, addr);
182         value = _il_rd(il, HBUS_TARG_MEM_RDAT);
183
184         _il_release_nic_access(il);
185         spin_unlock_irqrestore(&il->reg_lock, reg_flags);
186         return value;
187 }
188 EXPORT_SYMBOL(il_read_targ_mem);
189
190 void
191 il_write_targ_mem(struct il_priv *il, u32 addr, u32 val)
192 {
193         unsigned long reg_flags;
194
195         spin_lock_irqsave(&il->reg_lock, reg_flags);
196         if (likely(_il_grab_nic_access(il))) {
197                 _il_wr(il, HBUS_TARG_MEM_WADDR, addr);
198                 _il_wr(il, HBUS_TARG_MEM_WDAT, val);
199                 _il_release_nic_access(il);
200         }
201         spin_unlock_irqrestore(&il->reg_lock, reg_flags);
202 }
203 EXPORT_SYMBOL(il_write_targ_mem);
204
205 const char *
206 il_get_cmd_string(u8 cmd)
207 {
208         switch (cmd) {
209                 IL_CMD(N_ALIVE);
210                 IL_CMD(N_ERROR);
211                 IL_CMD(C_RXON);
212                 IL_CMD(C_RXON_ASSOC);
213                 IL_CMD(C_QOS_PARAM);
214                 IL_CMD(C_RXON_TIMING);
215                 IL_CMD(C_ADD_STA);
216                 IL_CMD(C_REM_STA);
217                 IL_CMD(C_WEPKEY);
218                 IL_CMD(N_3945_RX);
219                 IL_CMD(C_TX);
220                 IL_CMD(C_RATE_SCALE);
221                 IL_CMD(C_LEDS);
222                 IL_CMD(C_TX_LINK_QUALITY_CMD);
223                 IL_CMD(C_CHANNEL_SWITCH);
224                 IL_CMD(N_CHANNEL_SWITCH);
225                 IL_CMD(C_SPECTRUM_MEASUREMENT);
226                 IL_CMD(N_SPECTRUM_MEASUREMENT);
227                 IL_CMD(C_POWER_TBL);
228                 IL_CMD(N_PM_SLEEP);
229                 IL_CMD(N_PM_DEBUG_STATS);
230                 IL_CMD(C_SCAN);
231                 IL_CMD(C_SCAN_ABORT);
232                 IL_CMD(N_SCAN_START);
233                 IL_CMD(N_SCAN_RESULTS);
234                 IL_CMD(N_SCAN_COMPLETE);
235                 IL_CMD(N_BEACON);
236                 IL_CMD(C_TX_BEACON);
237                 IL_CMD(C_TX_PWR_TBL);
238                 IL_CMD(C_BT_CONFIG);
239                 IL_CMD(C_STATS);
240                 IL_CMD(N_STATS);
241                 IL_CMD(N_CARD_STATE);
242                 IL_CMD(N_MISSED_BEACONS);
243                 IL_CMD(C_CT_KILL_CONFIG);
244                 IL_CMD(C_SENSITIVITY);
245                 IL_CMD(C_PHY_CALIBRATION);
246                 IL_CMD(N_RX_PHY);
247                 IL_CMD(N_RX_MPDU);
248                 IL_CMD(N_RX);
249                 IL_CMD(N_COMPRESSED_BA);
250         default:
251                 return "UNKNOWN";
252
253         }
254 }
255 EXPORT_SYMBOL(il_get_cmd_string);
256
257 #define HOST_COMPLETE_TIMEOUT (HZ / 2)
258
259 static void
260 il_generic_cmd_callback(struct il_priv *il, struct il_device_cmd *cmd,
261                         struct il_rx_pkt *pkt)
262 {
263         if (pkt->hdr.flags & IL_CMD_FAILED_MSK) {
264                 IL_ERR("Bad return from %s (0x%08X)\n",
265                        il_get_cmd_string(cmd->hdr.cmd), pkt->hdr.flags);
266                 return;
267         }
268 #ifdef CONFIG_IWLEGACY_DEBUG
269         switch (cmd->hdr.cmd) {
270         case C_TX_LINK_QUALITY_CMD:
271         case C_SENSITIVITY:
272                 D_HC_DUMP("back from %s (0x%08X)\n",
273                           il_get_cmd_string(cmd->hdr.cmd), pkt->hdr.flags);
274                 break;
275         default:
276                 D_HC("back from %s (0x%08X)\n", il_get_cmd_string(cmd->hdr.cmd),
277                      pkt->hdr.flags);
278         }
279 #endif
280 }
281
282 static int
283 il_send_cmd_async(struct il_priv *il, struct il_host_cmd *cmd)
284 {
285         int ret;
286
287         BUG_ON(!(cmd->flags & CMD_ASYNC));
288
289         /* An asynchronous command can not expect an SKB to be set. */
290         BUG_ON(cmd->flags & CMD_WANT_SKB);
291
292         /* Assign a generic callback if one is not provided */
293         if (!cmd->callback)
294                 cmd->callback = il_generic_cmd_callback;
295
296         if (test_bit(S_EXIT_PENDING, &il->status))
297                 return -EBUSY;
298
299         ret = il_enqueue_hcmd(il, cmd);
300         if (ret < 0) {
301                 IL_ERR("Error sending %s: enqueue_hcmd failed: %d\n",
302                        il_get_cmd_string(cmd->id), ret);
303                 return ret;
304         }
305         return 0;
306 }
307
308 int
309 il_send_cmd_sync(struct il_priv *il, struct il_host_cmd *cmd)
310 {
311         int cmd_idx;
312         int ret;
313
314         lockdep_assert_held(&il->mutex);
315
316         BUG_ON(cmd->flags & CMD_ASYNC);
317
318         /* A synchronous command can not have a callback set. */
319         BUG_ON(cmd->callback);
320
321         D_INFO("Attempting to send sync command %s\n",
322                il_get_cmd_string(cmd->id));
323
324         set_bit(S_HCMD_ACTIVE, &il->status);
325         D_INFO("Setting HCMD_ACTIVE for command %s\n",
326                il_get_cmd_string(cmd->id));
327
328         cmd_idx = il_enqueue_hcmd(il, cmd);
329         if (cmd_idx < 0) {
330                 ret = cmd_idx;
331                 IL_ERR("Error sending %s: enqueue_hcmd failed: %d\n",
332                        il_get_cmd_string(cmd->id), ret);
333                 goto out;
334         }
335
336         ret = wait_event_timeout(il->wait_command_queue,
337                                  !test_bit(S_HCMD_ACTIVE, &il->status),
338                                  HOST_COMPLETE_TIMEOUT);
339         if (!ret) {
340                 if (test_bit(S_HCMD_ACTIVE, &il->status)) {
341                         IL_ERR("Error sending %s: time out after %dms.\n",
342                                il_get_cmd_string(cmd->id),
343                                jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
344
345                         clear_bit(S_HCMD_ACTIVE, &il->status);
346                         D_INFO("Clearing HCMD_ACTIVE for command %s\n",
347                                il_get_cmd_string(cmd->id));
348                         ret = -ETIMEDOUT;
349                         goto cancel;
350                 }
351         }
352
353         if (test_bit(S_RFKILL, &il->status)) {
354                 IL_ERR("Command %s aborted: RF KILL Switch\n",
355                        il_get_cmd_string(cmd->id));
356                 ret = -ECANCELED;
357                 goto fail;
358         }
359         if (test_bit(S_FW_ERROR, &il->status)) {
360                 IL_ERR("Command %s failed: FW Error\n",
361                        il_get_cmd_string(cmd->id));
362                 ret = -EIO;
363                 goto fail;
364         }
365         if ((cmd->flags & CMD_WANT_SKB) && !cmd->reply_page) {
366                 IL_ERR("Error: Response NULL in '%s'\n",
367                        il_get_cmd_string(cmd->id));
368                 ret = -EIO;
369                 goto cancel;
370         }
371
372         ret = 0;
373         goto out;
374
375 cancel:
376         if (cmd->flags & CMD_WANT_SKB) {
377                 /*
378                  * Cancel the CMD_WANT_SKB flag for the cmd in the
379                  * TX cmd queue. Otherwise in case the cmd comes
380                  * in later, it will possibly set an invalid
381                  * address (cmd->meta.source).
382                  */
383                 il->txq[il->cmd_queue].meta[cmd_idx].flags &= ~CMD_WANT_SKB;
384         }
385 fail:
386         if (cmd->reply_page) {
387                 il_free_pages(il, cmd->reply_page);
388                 cmd->reply_page = 0;
389         }
390 out:
391         return ret;
392 }
393 EXPORT_SYMBOL(il_send_cmd_sync);
394
395 int
396 il_send_cmd(struct il_priv *il, struct il_host_cmd *cmd)
397 {
398         if (cmd->flags & CMD_ASYNC)
399                 return il_send_cmd_async(il, cmd);
400
401         return il_send_cmd_sync(il, cmd);
402 }
403 EXPORT_SYMBOL(il_send_cmd);
404
405 int
406 il_send_cmd_pdu(struct il_priv *il, u8 id, u16 len, const void *data)
407 {
408         struct il_host_cmd cmd = {
409                 .id = id,
410                 .len = len,
411                 .data = data,
412         };
413
414         return il_send_cmd_sync(il, &cmd);
415 }
416 EXPORT_SYMBOL(il_send_cmd_pdu);
417
418 int
419 il_send_cmd_pdu_async(struct il_priv *il, u8 id, u16 len, const void *data,
420                       void (*callback) (struct il_priv *il,
421                                         struct il_device_cmd *cmd,
422                                         struct il_rx_pkt *pkt))
423 {
424         struct il_host_cmd cmd = {
425                 .id = id,
426                 .len = len,
427                 .data = data,
428         };
429
430         cmd.flags |= CMD_ASYNC;
431         cmd.callback = callback;
432
433         return il_send_cmd_async(il, &cmd);
434 }
435 EXPORT_SYMBOL(il_send_cmd_pdu_async);
436
437 /* default: IL_LED_BLINK(0) using blinking idx table */
438 static int led_mode;
439 module_param(led_mode, int, S_IRUGO);
440 MODULE_PARM_DESC(led_mode,
441                  "0=system default, " "1=On(RF On)/Off(RF Off), 2=blinking");
442
443 /* Throughput           OFF time(ms)    ON time (ms)
444  *      >300                    25              25
445  *      >200 to 300             40              40
446  *      >100 to 200             55              55
447  *      >70 to 100              65              65
448  *      >50 to 70               75              75
449  *      >20 to 50               85              85
450  *      >10 to 20               95              95
451  *      >5 to 10                110             110
452  *      >1 to 5                 130             130
453  *      >0 to 1                 167             167
454  *      <=0                                     SOLID ON
455  */
456 static const struct ieee80211_tpt_blink il_blink[] = {
457         {.throughput = 0,               .blink_time = 334},
458         {.throughput = 1 * 1024 - 1,    .blink_time = 260},
459         {.throughput = 5 * 1024 - 1,    .blink_time = 220},
460         {.throughput = 10 * 1024 - 1,   .blink_time = 190},
461         {.throughput = 20 * 1024 - 1,   .blink_time = 170},
462         {.throughput = 50 * 1024 - 1,   .blink_time = 150},
463         {.throughput = 70 * 1024 - 1,   .blink_time = 130},
464         {.throughput = 100 * 1024 - 1,  .blink_time = 110},
465         {.throughput = 200 * 1024 - 1,  .blink_time = 80},
466         {.throughput = 300 * 1024 - 1,  .blink_time = 50},
467 };
468
469 /*
470  * Adjust led blink rate to compensate on a MAC Clock difference on every HW
471  * Led blink rate analysis showed an average deviation of 0% on 3945,
472  * 5% on 4965 HW.
473  * Need to compensate on the led on/off time per HW according to the deviation
474  * to achieve the desired led frequency
475  * The calculation is: (100-averageDeviation)/100 * blinkTime
476  * For code efficiency the calculation will be:
477  *     compensation = (100 - averageDeviation) * 64 / 100
478  *     NewBlinkTime = (compensation * BlinkTime) / 64
479  */
480 static inline u8
481 il_blink_compensation(struct il_priv *il, u8 time, u16 compensation)
482 {
483         if (!compensation) {
484                 IL_ERR("undefined blink compensation: "
485                        "use pre-defined blinking time\n");
486                 return time;
487         }
488
489         return (u8) ((time * compensation) >> 6);
490 }
491
492 /* Set led pattern command */
493 static int
494 il_led_cmd(struct il_priv *il, unsigned long on, unsigned long off)
495 {
496         struct il_led_cmd led_cmd = {
497                 .id = IL_LED_LINK,
498                 .interval = IL_DEF_LED_INTRVL
499         };
500         int ret;
501
502         if (!test_bit(S_READY, &il->status))
503                 return -EBUSY;
504
505         if (il->blink_on == on && il->blink_off == off)
506                 return 0;
507
508         if (off == 0) {
509                 /* led is SOLID_ON */
510                 on = IL_LED_SOLID;
511         }
512
513         D_LED("Led blink time compensation=%u\n",
514               il->cfg->led_compensation);
515         led_cmd.on =
516             il_blink_compensation(il, on,
517                                   il->cfg->led_compensation);
518         led_cmd.off =
519             il_blink_compensation(il, off,
520                                   il->cfg->led_compensation);
521
522         ret = il->ops->send_led_cmd(il, &led_cmd);
523         if (!ret) {
524                 il->blink_on = on;
525                 il->blink_off = off;
526         }
527         return ret;
528 }
529
530 static void
531 il_led_brightness_set(struct led_classdev *led_cdev,
532                       enum led_brightness brightness)
533 {
534         struct il_priv *il = container_of(led_cdev, struct il_priv, led);
535         unsigned long on = 0;
536
537         if (brightness > 0)
538                 on = IL_LED_SOLID;
539
540         il_led_cmd(il, on, 0);
541 }
542
543 static int
544 il_led_blink_set(struct led_classdev *led_cdev, unsigned long *delay_on,
545                  unsigned long *delay_off)
546 {
547         struct il_priv *il = container_of(led_cdev, struct il_priv, led);
548
549         return il_led_cmd(il, *delay_on, *delay_off);
550 }
551
552 void
553 il_leds_init(struct il_priv *il)
554 {
555         int mode = led_mode;
556         int ret;
557
558         if (mode == IL_LED_DEFAULT)
559                 mode = il->cfg->led_mode;
560
561         il->led.name =
562             kasprintf(GFP_KERNEL, "%s-led", wiphy_name(il->hw->wiphy));
563         il->led.brightness_set = il_led_brightness_set;
564         il->led.blink_set = il_led_blink_set;
565         il->led.max_brightness = 1;
566
567         switch (mode) {
568         case IL_LED_DEFAULT:
569                 WARN_ON(1);
570                 break;
571         case IL_LED_BLINK:
572                 il->led.default_trigger =
573                     ieee80211_create_tpt_led_trigger(il->hw,
574                                                      IEEE80211_TPT_LEDTRIG_FL_CONNECTED,
575                                                      il_blink,
576                                                      ARRAY_SIZE(il_blink));
577                 break;
578         case IL_LED_RF_STATE:
579                 il->led.default_trigger = ieee80211_get_radio_led_name(il->hw);
580                 break;
581         }
582
583         ret = led_classdev_register(&il->pci_dev->dev, &il->led);
584         if (ret) {
585                 kfree(il->led.name);
586                 return;
587         }
588
589         il->led_registered = true;
590 }
591 EXPORT_SYMBOL(il_leds_init);
592
593 void
594 il_leds_exit(struct il_priv *il)
595 {
596         if (!il->led_registered)
597                 return;
598
599         led_classdev_unregister(&il->led);
600         kfree(il->led.name);
601 }
602 EXPORT_SYMBOL(il_leds_exit);
603
604 /************************** EEPROM BANDS ****************************
605  *
606  * The il_eeprom_band definitions below provide the mapping from the
607  * EEPROM contents to the specific channel number supported for each
608  * band.
609  *
610  * For example, il_priv->eeprom.band_3_channels[4] from the band_3
611  * definition below maps to physical channel 42 in the 5.2GHz spectrum.
612  * The specific geography and calibration information for that channel
613  * is contained in the eeprom map itself.
614  *
615  * During init, we copy the eeprom information and channel map
616  * information into il->channel_info_24/52 and il->channel_map_24/52
617  *
618  * channel_map_24/52 provides the idx in the channel_info array for a
619  * given channel.  We have to have two separate maps as there is channel
620  * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
621  * band_2
622  *
623  * A value of 0xff stored in the channel_map indicates that the channel
624  * is not supported by the hardware at all.
625  *
626  * A value of 0xfe in the channel_map indicates that the channel is not
627  * valid for Tx with the current hardware.  This means that
628  * while the system can tune and receive on a given channel, it may not
629  * be able to associate or transmit any frames on that
630  * channel.  There is no corresponding channel information for that
631  * entry.
632  *
633  *********************************************************************/
634
635 /* 2.4 GHz */
636 const u8 il_eeprom_band_1[14] = {
637         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
638 };
639
640 /* 5.2 GHz bands */
641 static const u8 il_eeprom_band_2[] = {  /* 4915-5080MHz */
642         183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
643 };
644
645 static const u8 il_eeprom_band_3[] = {  /* 5170-5320MHz */
646         34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
647 };
648
649 static const u8 il_eeprom_band_4[] = {  /* 5500-5700MHz */
650         100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
651 };
652
653 static const u8 il_eeprom_band_5[] = {  /* 5725-5825MHz */
654         145, 149, 153, 157, 161, 165
655 };
656
657 static const u8 il_eeprom_band_6[] = {  /* 2.4 ht40 channel */
658         1, 2, 3, 4, 5, 6, 7
659 };
660
661 static const u8 il_eeprom_band_7[] = {  /* 5.2 ht40 channel */
662         36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
663 };
664
665 /******************************************************************************
666  *
667  * EEPROM related functions
668  *
669 ******************************************************************************/
670
671 static int
672 il_eeprom_verify_signature(struct il_priv *il)
673 {
674         u32 gp = _il_rd(il, CSR_EEPROM_GP) & CSR_EEPROM_GP_VALID_MSK;
675         int ret = 0;
676
677         D_EEPROM("EEPROM signature=0x%08x\n", gp);
678         switch (gp) {
679         case CSR_EEPROM_GP_GOOD_SIG_EEP_LESS_THAN_4K:
680         case CSR_EEPROM_GP_GOOD_SIG_EEP_MORE_THAN_4K:
681                 break;
682         default:
683                 IL_ERR("bad EEPROM signature," "EEPROM_GP=0x%08x\n", gp);
684                 ret = -ENOENT;
685                 break;
686         }
687         return ret;
688 }
689
690 const u8 *
691 il_eeprom_query_addr(const struct il_priv *il, size_t offset)
692 {
693         BUG_ON(offset >= il->cfg->eeprom_size);
694         return &il->eeprom[offset];
695 }
696 EXPORT_SYMBOL(il_eeprom_query_addr);
697
698 u16
699 il_eeprom_query16(const struct il_priv *il, size_t offset)
700 {
701         if (!il->eeprom)
702                 return 0;
703         return (u16) il->eeprom[offset] | ((u16) il->eeprom[offset + 1] << 8);
704 }
705 EXPORT_SYMBOL(il_eeprom_query16);
706
707 /**
708  * il_eeprom_init - read EEPROM contents
709  *
710  * Load the EEPROM contents from adapter into il->eeprom
711  *
712  * NOTE:  This routine uses the non-debug IO access functions.
713  */
714 int
715 il_eeprom_init(struct il_priv *il)
716 {
717         __le16 *e;
718         u32 gp = _il_rd(il, CSR_EEPROM_GP);
719         int sz;
720         int ret;
721         u16 addr;
722
723         /* allocate eeprom */
724         sz = il->cfg->eeprom_size;
725         D_EEPROM("NVM size = %d\n", sz);
726         il->eeprom = kzalloc(sz, GFP_KERNEL);
727         if (!il->eeprom) {
728                 ret = -ENOMEM;
729                 goto alloc_err;
730         }
731         e = (__le16 *) il->eeprom;
732
733         il->ops->apm_init(il);
734
735         ret = il_eeprom_verify_signature(il);
736         if (ret < 0) {
737                 IL_ERR("EEPROM not found, EEPROM_GP=0x%08x\n", gp);
738                 ret = -ENOENT;
739                 goto err;
740         }
741
742         /* Make sure driver (instead of uCode) is allowed to read EEPROM */
743         ret = il->ops->eeprom_acquire_semaphore(il);
744         if (ret < 0) {
745                 IL_ERR("Failed to acquire EEPROM semaphore.\n");
746                 ret = -ENOENT;
747                 goto err;
748         }
749
750         /* eeprom is an array of 16bit values */
751         for (addr = 0; addr < sz; addr += sizeof(u16)) {
752                 u32 r;
753
754                 _il_wr(il, CSR_EEPROM_REG,
755                        CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
756
757                 ret =
758                     _il_poll_bit(il, CSR_EEPROM_REG,
759                                  CSR_EEPROM_REG_READ_VALID_MSK,
760                                  CSR_EEPROM_REG_READ_VALID_MSK,
761                                  IL_EEPROM_ACCESS_TIMEOUT);
762                 if (ret < 0) {
763                         IL_ERR("Time out reading EEPROM[%d]\n", addr);
764                         goto done;
765                 }
766                 r = _il_rd(il, CSR_EEPROM_REG);
767                 e[addr / 2] = cpu_to_le16(r >> 16);
768         }
769
770         D_EEPROM("NVM Type: %s, version: 0x%x\n", "EEPROM",
771                  il_eeprom_query16(il, EEPROM_VERSION));
772
773         ret = 0;
774 done:
775         il->ops->eeprom_release_semaphore(il);
776
777 err:
778         if (ret)
779                 il_eeprom_free(il);
780         /* Reset chip to save power until we load uCode during "up". */
781         il_apm_stop(il);
782 alloc_err:
783         return ret;
784 }
785 EXPORT_SYMBOL(il_eeprom_init);
786
787 void
788 il_eeprom_free(struct il_priv *il)
789 {
790         kfree(il->eeprom);
791         il->eeprom = NULL;
792 }
793 EXPORT_SYMBOL(il_eeprom_free);
794
795 static void
796 il_init_band_reference(const struct il_priv *il, int eep_band,
797                        int *eeprom_ch_count,
798                        const struct il_eeprom_channel **eeprom_ch_info,
799                        const u8 **eeprom_ch_idx)
800 {
801         u32 offset = il->cfg->regulatory_bands[eep_band - 1];
802
803         switch (eep_band) {
804         case 1:         /* 2.4GHz band */
805                 *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_1);
806                 *eeprom_ch_info =
807                     (struct il_eeprom_channel *)il_eeprom_query_addr(il,
808                                                                      offset);
809                 *eeprom_ch_idx = il_eeprom_band_1;
810                 break;
811         case 2:         /* 4.9GHz band */
812                 *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_2);
813                 *eeprom_ch_info =
814                     (struct il_eeprom_channel *)il_eeprom_query_addr(il,
815                                                                      offset);
816                 *eeprom_ch_idx = il_eeprom_band_2;
817                 break;
818         case 3:         /* 5.2GHz band */
819                 *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_3);
820                 *eeprom_ch_info =
821                     (struct il_eeprom_channel *)il_eeprom_query_addr(il,
822                                                                      offset);
823                 *eeprom_ch_idx = il_eeprom_band_3;
824                 break;
825         case 4:         /* 5.5GHz band */
826                 *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_4);
827                 *eeprom_ch_info =
828                     (struct il_eeprom_channel *)il_eeprom_query_addr(il,
829                                                                      offset);
830                 *eeprom_ch_idx = il_eeprom_band_4;
831                 break;
832         case 5:         /* 5.7GHz band */
833                 *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_5);
834                 *eeprom_ch_info =
835                     (struct il_eeprom_channel *)il_eeprom_query_addr(il,
836                                                                      offset);
837                 *eeprom_ch_idx = il_eeprom_band_5;
838                 break;
839         case 6:         /* 2.4GHz ht40 channels */
840                 *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_6);
841                 *eeprom_ch_info =
842                     (struct il_eeprom_channel *)il_eeprom_query_addr(il,
843                                                                      offset);
844                 *eeprom_ch_idx = il_eeprom_band_6;
845                 break;
846         case 7:         /* 5 GHz ht40 channels */
847                 *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_7);
848                 *eeprom_ch_info =
849                     (struct il_eeprom_channel *)il_eeprom_query_addr(il,
850                                                                      offset);
851                 *eeprom_ch_idx = il_eeprom_band_7;
852                 break;
853         default:
854                 BUG();
855         }
856 }
857
858 #define CHECK_AND_PRINT(x) ((eeprom_ch->flags & EEPROM_CHANNEL_##x) \
859                             ? # x " " : "")
860 /**
861  * il_mod_ht40_chan_info - Copy ht40 channel info into driver's il.
862  *
863  * Does not set up a command, or touch hardware.
864  */
865 static int
866 il_mod_ht40_chan_info(struct il_priv *il, enum ieee80211_band band, u16 channel,
867                       const struct il_eeprom_channel *eeprom_ch,
868                       u8 clear_ht40_extension_channel)
869 {
870         struct il_channel_info *ch_info;
871
872         ch_info =
873             (struct il_channel_info *)il_get_channel_info(il, band, channel);
874
875         if (!il_is_channel_valid(ch_info))
876                 return -1;
877
878         D_EEPROM("HT40 Ch. %d [%sGHz] %s%s%s%s%s(0x%02x %ddBm):"
879                  " Ad-Hoc %ssupported\n", ch_info->channel,
880                  il_is_channel_a_band(ch_info) ? "5.2" : "2.4",
881                  CHECK_AND_PRINT(IBSS), CHECK_AND_PRINT(ACTIVE),
882                  CHECK_AND_PRINT(RADAR), CHECK_AND_PRINT(WIDE),
883                  CHECK_AND_PRINT(DFS), eeprom_ch->flags,
884                  eeprom_ch->max_power_avg,
885                  ((eeprom_ch->flags & EEPROM_CHANNEL_IBSS) &&
886                   !(eeprom_ch->flags & EEPROM_CHANNEL_RADAR)) ? "" : "not ");
887
888         ch_info->ht40_eeprom = *eeprom_ch;
889         ch_info->ht40_max_power_avg = eeprom_ch->max_power_avg;
890         ch_info->ht40_flags = eeprom_ch->flags;
891         if (eeprom_ch->flags & EEPROM_CHANNEL_VALID)
892                 ch_info->ht40_extension_channel &=
893                     ~clear_ht40_extension_channel;
894
895         return 0;
896 }
897
898 #define CHECK_AND_PRINT_I(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
899                             ? # x " " : "")
900
901 /**
902  * il_init_channel_map - Set up driver's info for all possible channels
903  */
904 int
905 il_init_channel_map(struct il_priv *il)
906 {
907         int eeprom_ch_count = 0;
908         const u8 *eeprom_ch_idx = NULL;
909         const struct il_eeprom_channel *eeprom_ch_info = NULL;
910         int band, ch;
911         struct il_channel_info *ch_info;
912
913         if (il->channel_count) {
914                 D_EEPROM("Channel map already initialized.\n");
915                 return 0;
916         }
917
918         D_EEPROM("Initializing regulatory info from EEPROM\n");
919
920         il->channel_count =
921             ARRAY_SIZE(il_eeprom_band_1) + ARRAY_SIZE(il_eeprom_band_2) +
922             ARRAY_SIZE(il_eeprom_band_3) + ARRAY_SIZE(il_eeprom_band_4) +
923             ARRAY_SIZE(il_eeprom_band_5);
924
925         D_EEPROM("Parsing data for %d channels.\n", il->channel_count);
926
927         il->channel_info =
928             kzalloc(sizeof(struct il_channel_info) * il->channel_count,
929                     GFP_KERNEL);
930         if (!il->channel_info) {
931                 IL_ERR("Could not allocate channel_info\n");
932                 il->channel_count = 0;
933                 return -ENOMEM;
934         }
935
936         ch_info = il->channel_info;
937
938         /* Loop through the 5 EEPROM bands adding them in order to the
939          * channel map we maintain (that contains additional information than
940          * what just in the EEPROM) */
941         for (band = 1; band <= 5; band++) {
942
943                 il_init_band_reference(il, band, &eeprom_ch_count,
944                                        &eeprom_ch_info, &eeprom_ch_idx);
945
946                 /* Loop through each band adding each of the channels */
947                 for (ch = 0; ch < eeprom_ch_count; ch++) {
948                         ch_info->channel = eeprom_ch_idx[ch];
949                         ch_info->band =
950                             (band ==
951                              1) ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
952
953                         /* permanently store EEPROM's channel regulatory flags
954                          *   and max power in channel info database. */
955                         ch_info->eeprom = eeprom_ch_info[ch];
956
957                         /* Copy the run-time flags so they are there even on
958                          * invalid channels */
959                         ch_info->flags = eeprom_ch_info[ch].flags;
960                         /* First write that ht40 is not enabled, and then enable
961                          * one by one */
962                         ch_info->ht40_extension_channel =
963                             IEEE80211_CHAN_NO_HT40;
964
965                         if (!(il_is_channel_valid(ch_info))) {
966                                 D_EEPROM("Ch. %d Flags %x [%sGHz] - "
967                                          "No traffic\n", ch_info->channel,
968                                          ch_info->flags,
969                                          il_is_channel_a_band(ch_info) ? "5.2" :
970                                          "2.4");
971                                 ch_info++;
972                                 continue;
973                         }
974
975                         /* Initialize regulatory-based run-time data */
976                         ch_info->max_power_avg = ch_info->curr_txpow =
977                             eeprom_ch_info[ch].max_power_avg;
978                         ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
979                         ch_info->min_power = 0;
980
981                         D_EEPROM("Ch. %d [%sGHz] " "%s%s%s%s%s%s(0x%02x %ddBm):"
982                                  " Ad-Hoc %ssupported\n", ch_info->channel,
983                                  il_is_channel_a_band(ch_info) ? "5.2" : "2.4",
984                                  CHECK_AND_PRINT_I(VALID),
985                                  CHECK_AND_PRINT_I(IBSS),
986                                  CHECK_AND_PRINT_I(ACTIVE),
987                                  CHECK_AND_PRINT_I(RADAR),
988                                  CHECK_AND_PRINT_I(WIDE),
989                                  CHECK_AND_PRINT_I(DFS),
990                                  eeprom_ch_info[ch].flags,
991                                  eeprom_ch_info[ch].max_power_avg,
992                                  ((eeprom_ch_info[ch].
993                                    flags & EEPROM_CHANNEL_IBSS) &&
994                                   !(eeprom_ch_info[ch].
995                                     flags & EEPROM_CHANNEL_RADAR)) ? "" :
996                                  "not ");
997
998                         ch_info++;
999                 }
1000         }
1001
1002         /* Check if we do have HT40 channels */
1003         if (il->cfg->regulatory_bands[5] == EEPROM_REGULATORY_BAND_NO_HT40 &&
1004             il->cfg->regulatory_bands[6] == EEPROM_REGULATORY_BAND_NO_HT40)
1005                 return 0;
1006
1007         /* Two additional EEPROM bands for 2.4 and 5 GHz HT40 channels */
1008         for (band = 6; band <= 7; band++) {
1009                 enum ieee80211_band ieeeband;
1010
1011                 il_init_band_reference(il, band, &eeprom_ch_count,
1012                                        &eeprom_ch_info, &eeprom_ch_idx);
1013
1014                 /* EEPROM band 6 is 2.4, band 7 is 5 GHz */
1015                 ieeeband =
1016                     (band == 6) ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
1017
1018                 /* Loop through each band adding each of the channels */
1019                 for (ch = 0; ch < eeprom_ch_count; ch++) {
1020                         /* Set up driver's info for lower half */
1021                         il_mod_ht40_chan_info(il, ieeeband, eeprom_ch_idx[ch],
1022                                               &eeprom_ch_info[ch],
1023                                               IEEE80211_CHAN_NO_HT40PLUS);
1024
1025                         /* Set up driver's info for upper half */
1026                         il_mod_ht40_chan_info(il, ieeeband,
1027                                               eeprom_ch_idx[ch] + 4,
1028                                               &eeprom_ch_info[ch],
1029                                               IEEE80211_CHAN_NO_HT40MINUS);
1030                 }
1031         }
1032
1033         return 0;
1034 }
1035 EXPORT_SYMBOL(il_init_channel_map);
1036
1037 /*
1038  * il_free_channel_map - undo allocations in il_init_channel_map
1039  */
1040 void
1041 il_free_channel_map(struct il_priv *il)
1042 {
1043         kfree(il->channel_info);
1044         il->channel_count = 0;
1045 }
1046 EXPORT_SYMBOL(il_free_channel_map);
1047
1048 /**
1049  * il_get_channel_info - Find driver's ilate channel info
1050  *
1051  * Based on band and channel number.
1052  */
1053 const struct il_channel_info *
1054 il_get_channel_info(const struct il_priv *il, enum ieee80211_band band,
1055                     u16 channel)
1056 {
1057         int i;
1058
1059         switch (band) {
1060         case IEEE80211_BAND_5GHZ:
1061                 for (i = 14; i < il->channel_count; i++) {
1062                         if (il->channel_info[i].channel == channel)
1063                                 return &il->channel_info[i];
1064                 }
1065                 break;
1066         case IEEE80211_BAND_2GHZ:
1067                 if (channel >= 1 && channel <= 14)
1068                         return &il->channel_info[channel - 1];
1069                 break;
1070         default:
1071                 BUG();
1072         }
1073
1074         return NULL;
1075 }
1076 EXPORT_SYMBOL(il_get_channel_info);
1077
1078 /*
1079  * Setting power level allows the card to go to sleep when not busy.
1080  *
1081  * We calculate a sleep command based on the required latency, which
1082  * we get from mac80211. In order to handle thermal throttling, we can
1083  * also use pre-defined power levels.
1084  */
1085
1086 /*
1087  * This defines the old power levels. They are still used by default
1088  * (level 1) and for thermal throttle (levels 3 through 5)
1089  */
1090
1091 struct il_power_vec_entry {
1092         struct il_powertable_cmd cmd;
1093         u8 no_dtim;             /* number of skip dtim */
1094 };
1095
1096 static void
1097 il_power_sleep_cam_cmd(struct il_priv *il, struct il_powertable_cmd *cmd)
1098 {
1099         memset(cmd, 0, sizeof(*cmd));
1100
1101         if (il->power_data.pci_pm)
1102                 cmd->flags |= IL_POWER_PCI_PM_MSK;
1103
1104         D_POWER("Sleep command for CAM\n");
1105 }
1106
1107 static int
1108 il_set_power(struct il_priv *il, struct il_powertable_cmd *cmd)
1109 {
1110         D_POWER("Sending power/sleep command\n");
1111         D_POWER("Flags value = 0x%08X\n", cmd->flags);
1112         D_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1113         D_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1114         D_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1115                 le32_to_cpu(cmd->sleep_interval[0]),
1116                 le32_to_cpu(cmd->sleep_interval[1]),
1117                 le32_to_cpu(cmd->sleep_interval[2]),
1118                 le32_to_cpu(cmd->sleep_interval[3]),
1119                 le32_to_cpu(cmd->sleep_interval[4]));
1120
1121         return il_send_cmd_pdu(il, C_POWER_TBL,
1122                                sizeof(struct il_powertable_cmd), cmd);
1123 }
1124
1125 int
1126 il_power_set_mode(struct il_priv *il, struct il_powertable_cmd *cmd, bool force)
1127 {
1128         int ret;
1129         bool update_chains;
1130
1131         lockdep_assert_held(&il->mutex);
1132
1133         /* Don't update the RX chain when chain noise calibration is running */
1134         update_chains = il->chain_noise_data.state == IL_CHAIN_NOISE_DONE ||
1135             il->chain_noise_data.state == IL_CHAIN_NOISE_ALIVE;
1136
1137         if (!memcmp(&il->power_data.sleep_cmd, cmd, sizeof(*cmd)) && !force)
1138                 return 0;
1139
1140         if (!il_is_ready_rf(il))
1141                 return -EIO;
1142
1143         /* scan complete use sleep_power_next, need to be updated */
1144         memcpy(&il->power_data.sleep_cmd_next, cmd, sizeof(*cmd));
1145         if (test_bit(S_SCANNING, &il->status) && !force) {
1146                 D_INFO("Defer power set mode while scanning\n");
1147                 return 0;
1148         }
1149
1150         if (cmd->flags & IL_POWER_DRIVER_ALLOW_SLEEP_MSK)
1151                 set_bit(S_POWER_PMI, &il->status);
1152
1153         ret = il_set_power(il, cmd);
1154         if (!ret) {
1155                 if (!(cmd->flags & IL_POWER_DRIVER_ALLOW_SLEEP_MSK))
1156                         clear_bit(S_POWER_PMI, &il->status);
1157
1158                 if (il->ops->update_chain_flags && update_chains)
1159                         il->ops->update_chain_flags(il);
1160                 else if (il->ops->update_chain_flags)
1161                         D_POWER("Cannot update the power, chain noise "
1162                                 "calibration running: %d\n",
1163                                 il->chain_noise_data.state);
1164
1165                 memcpy(&il->power_data.sleep_cmd, cmd, sizeof(*cmd));
1166         } else
1167                 IL_ERR("set power fail, ret = %d", ret);
1168
1169         return ret;
1170 }
1171
1172 int
1173 il_power_update_mode(struct il_priv *il, bool force)
1174 {
1175         struct il_powertable_cmd cmd;
1176
1177         il_power_sleep_cam_cmd(il, &cmd);
1178         return il_power_set_mode(il, &cmd, force);
1179 }
1180 EXPORT_SYMBOL(il_power_update_mode);
1181
1182 /* initialize to default */
1183 void
1184 il_power_initialize(struct il_priv *il)
1185 {
1186         u16 lctl;
1187
1188         pcie_capability_read_word(il->pci_dev, PCI_EXP_LNKCTL, &lctl);
1189         il->power_data.pci_pm = !(lctl & PCI_EXP_LNKCTL_ASPM_L0S);
1190
1191         il->power_data.debug_sleep_level_override = -1;
1192
1193         memset(&il->power_data.sleep_cmd, 0, sizeof(il->power_data.sleep_cmd));
1194 }
1195 EXPORT_SYMBOL(il_power_initialize);
1196
1197 /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
1198  * sending probe req.  This should be set long enough to hear probe responses
1199  * from more than one AP.  */
1200 #define IL_ACTIVE_DWELL_TIME_24    (30) /* all times in msec */
1201 #define IL_ACTIVE_DWELL_TIME_52    (20)
1202
1203 #define IL_ACTIVE_DWELL_FACTOR_24GHZ (3)
1204 #define IL_ACTIVE_DWELL_FACTOR_52GHZ (2)
1205
1206 /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
1207  * Must be set longer than active dwell time.
1208  * For the most reliable scan, set > AP beacon interval (typically 100msec). */
1209 #define IL_PASSIVE_DWELL_TIME_24   (20) /* all times in msec */
1210 #define IL_PASSIVE_DWELL_TIME_52   (10)
1211 #define IL_PASSIVE_DWELL_BASE      (100)
1212 #define IL_CHANNEL_TUNE_TIME       5
1213
1214 static int
1215 il_send_scan_abort(struct il_priv *il)
1216 {
1217         int ret;
1218         struct il_rx_pkt *pkt;
1219         struct il_host_cmd cmd = {
1220                 .id = C_SCAN_ABORT,
1221                 .flags = CMD_WANT_SKB,
1222         };
1223
1224         /* Exit instantly with error when device is not ready
1225          * to receive scan abort command or it does not perform
1226          * hardware scan currently */
1227         if (!test_bit(S_READY, &il->status) ||
1228             !test_bit(S_GEO_CONFIGURED, &il->status) ||
1229             !test_bit(S_SCAN_HW, &il->status) ||
1230             test_bit(S_FW_ERROR, &il->status) ||
1231             test_bit(S_EXIT_PENDING, &il->status))
1232                 return -EIO;
1233
1234         ret = il_send_cmd_sync(il, &cmd);
1235         if (ret)
1236                 return ret;
1237
1238         pkt = (struct il_rx_pkt *)cmd.reply_page;
1239         if (pkt->u.status != CAN_ABORT_STATUS) {
1240                 /* The scan abort will return 1 for success or
1241                  * 2 for "failure".  A failure condition can be
1242                  * due to simply not being in an active scan which
1243                  * can occur if we send the scan abort before we
1244                  * the microcode has notified us that a scan is
1245                  * completed. */
1246                 D_SCAN("SCAN_ABORT ret %d.\n", pkt->u.status);
1247                 ret = -EIO;
1248         }
1249
1250         il_free_pages(il, cmd.reply_page);
1251         return ret;
1252 }
1253
1254 static void
1255 il_complete_scan(struct il_priv *il, bool aborted)
1256 {
1257         /* check if scan was requested from mac80211 */
1258         if (il->scan_request) {
1259                 D_SCAN("Complete scan in mac80211\n");
1260                 ieee80211_scan_completed(il->hw, aborted);
1261         }
1262
1263         il->scan_vif = NULL;
1264         il->scan_request = NULL;
1265 }
1266
1267 void
1268 il_force_scan_end(struct il_priv *il)
1269 {
1270         lockdep_assert_held(&il->mutex);
1271
1272         if (!test_bit(S_SCANNING, &il->status)) {
1273                 D_SCAN("Forcing scan end while not scanning\n");
1274                 return;
1275         }
1276
1277         D_SCAN("Forcing scan end\n");
1278         clear_bit(S_SCANNING, &il->status);
1279         clear_bit(S_SCAN_HW, &il->status);
1280         clear_bit(S_SCAN_ABORTING, &il->status);
1281         il_complete_scan(il, true);
1282 }
1283
1284 static void
1285 il_do_scan_abort(struct il_priv *il)
1286 {
1287         int ret;
1288
1289         lockdep_assert_held(&il->mutex);
1290
1291         if (!test_bit(S_SCANNING, &il->status)) {
1292                 D_SCAN("Not performing scan to abort\n");
1293                 return;
1294         }
1295
1296         if (test_and_set_bit(S_SCAN_ABORTING, &il->status)) {
1297                 D_SCAN("Scan abort in progress\n");
1298                 return;
1299         }
1300
1301         ret = il_send_scan_abort(il);
1302         if (ret) {
1303                 D_SCAN("Send scan abort failed %d\n", ret);
1304                 il_force_scan_end(il);
1305         } else
1306                 D_SCAN("Successfully send scan abort\n");
1307 }
1308
1309 /**
1310  * il_scan_cancel - Cancel any currently executing HW scan
1311  */
1312 int
1313 il_scan_cancel(struct il_priv *il)
1314 {
1315         D_SCAN("Queuing abort scan\n");
1316         queue_work(il->workqueue, &il->abort_scan);
1317         return 0;
1318 }
1319 EXPORT_SYMBOL(il_scan_cancel);
1320
1321 /**
1322  * il_scan_cancel_timeout - Cancel any currently executing HW scan
1323  * @ms: amount of time to wait (in milliseconds) for scan to abort
1324  *
1325  */
1326 int
1327 il_scan_cancel_timeout(struct il_priv *il, unsigned long ms)
1328 {
1329         unsigned long timeout = jiffies + msecs_to_jiffies(ms);
1330
1331         lockdep_assert_held(&il->mutex);
1332
1333         D_SCAN("Scan cancel timeout\n");
1334
1335         il_do_scan_abort(il);
1336
1337         while (time_before_eq(jiffies, timeout)) {
1338                 if (!test_bit(S_SCAN_HW, &il->status))
1339                         break;
1340                 msleep(20);
1341         }
1342
1343         return test_bit(S_SCAN_HW, &il->status);
1344 }
1345 EXPORT_SYMBOL(il_scan_cancel_timeout);
1346
1347 /* Service response to C_SCAN (0x80) */
1348 static void
1349 il_hdl_scan(struct il_priv *il, struct il_rx_buf *rxb)
1350 {
1351 #ifdef CONFIG_IWLEGACY_DEBUG
1352         struct il_rx_pkt *pkt = rxb_addr(rxb);
1353         struct il_scanreq_notification *notif =
1354             (struct il_scanreq_notification *)pkt->u.raw;
1355
1356         D_SCAN("Scan request status = 0x%x\n", notif->status);
1357 #endif
1358 }
1359
1360 /* Service N_SCAN_START (0x82) */
1361 static void
1362 il_hdl_scan_start(struct il_priv *il, struct il_rx_buf *rxb)
1363 {
1364         struct il_rx_pkt *pkt = rxb_addr(rxb);
1365         struct il_scanstart_notification *notif =
1366             (struct il_scanstart_notification *)pkt->u.raw;
1367         il->scan_start_tsf = le32_to_cpu(notif->tsf_low);
1368         D_SCAN("Scan start: " "%d [802.11%s] "
1369                "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n", notif->channel,
1370                notif->band ? "bg" : "a", le32_to_cpu(notif->tsf_high),
1371                le32_to_cpu(notif->tsf_low), notif->status, notif->beacon_timer);
1372 }
1373
1374 /* Service N_SCAN_RESULTS (0x83) */
1375 static void
1376 il_hdl_scan_results(struct il_priv *il, struct il_rx_buf *rxb)
1377 {
1378 #ifdef CONFIG_IWLEGACY_DEBUG
1379         struct il_rx_pkt *pkt = rxb_addr(rxb);
1380         struct il_scanresults_notification *notif =
1381             (struct il_scanresults_notification *)pkt->u.raw;
1382
1383         D_SCAN("Scan ch.res: " "%d [802.11%s] " "(TSF: 0x%08X:%08X) - %d "
1384                "elapsed=%lu usec\n", notif->channel, notif->band ? "bg" : "a",
1385                le32_to_cpu(notif->tsf_high), le32_to_cpu(notif->tsf_low),
1386                le32_to_cpu(notif->stats[0]),
1387                le32_to_cpu(notif->tsf_low) - il->scan_start_tsf);
1388 #endif
1389 }
1390
1391 /* Service N_SCAN_COMPLETE (0x84) */
1392 static void
1393 il_hdl_scan_complete(struct il_priv *il, struct il_rx_buf *rxb)
1394 {
1395
1396 #ifdef CONFIG_IWLEGACY_DEBUG
1397         struct il_rx_pkt *pkt = rxb_addr(rxb);
1398         struct il_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
1399 #endif
1400
1401         D_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
1402                scan_notif->scanned_channels, scan_notif->tsf_low,
1403                scan_notif->tsf_high, scan_notif->status);
1404
1405         /* The HW is no longer scanning */
1406         clear_bit(S_SCAN_HW, &il->status);
1407
1408         D_SCAN("Scan on %sGHz took %dms\n",
1409                (il->scan_band == IEEE80211_BAND_2GHZ) ? "2.4" : "5.2",
1410                jiffies_to_msecs(jiffies - il->scan_start));
1411
1412         queue_work(il->workqueue, &il->scan_completed);
1413 }
1414
1415 void
1416 il_setup_rx_scan_handlers(struct il_priv *il)
1417 {
1418         /* scan handlers */
1419         il->handlers[C_SCAN] = il_hdl_scan;
1420         il->handlers[N_SCAN_START] = il_hdl_scan_start;
1421         il->handlers[N_SCAN_RESULTS] = il_hdl_scan_results;
1422         il->handlers[N_SCAN_COMPLETE] = il_hdl_scan_complete;
1423 }
1424 EXPORT_SYMBOL(il_setup_rx_scan_handlers);
1425
1426 inline u16
1427 il_get_active_dwell_time(struct il_priv *il, enum ieee80211_band band,
1428                          u8 n_probes)
1429 {
1430         if (band == IEEE80211_BAND_5GHZ)
1431                 return IL_ACTIVE_DWELL_TIME_52 +
1432                     IL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
1433         else
1434                 return IL_ACTIVE_DWELL_TIME_24 +
1435                     IL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
1436 }
1437 EXPORT_SYMBOL(il_get_active_dwell_time);
1438
1439 u16
1440 il_get_passive_dwell_time(struct il_priv *il, enum ieee80211_band band,
1441                           struct ieee80211_vif *vif)
1442 {
1443         u16 value;
1444
1445         u16 passive =
1446             (band ==
1447              IEEE80211_BAND_2GHZ) ? IL_PASSIVE_DWELL_BASE +
1448             IL_PASSIVE_DWELL_TIME_24 : IL_PASSIVE_DWELL_BASE +
1449             IL_PASSIVE_DWELL_TIME_52;
1450
1451         if (il_is_any_associated(il)) {
1452                 /*
1453                  * If we're associated, we clamp the maximum passive
1454                  * dwell time to be 98% of the smallest beacon interval
1455                  * (minus 2 * channel tune time)
1456                  */
1457                 value = il->vif ? il->vif->bss_conf.beacon_int : 0;
1458                 if (value > IL_PASSIVE_DWELL_BASE || !value)
1459                         value = IL_PASSIVE_DWELL_BASE;
1460                 value = (value * 98) / 100 - IL_CHANNEL_TUNE_TIME * 2;
1461                 passive = min(value, passive);
1462         }
1463
1464         return passive;
1465 }
1466 EXPORT_SYMBOL(il_get_passive_dwell_time);
1467
1468 void
1469 il_init_scan_params(struct il_priv *il)
1470 {
1471         u8 ant_idx = fls(il->hw_params.valid_tx_ant) - 1;
1472         if (!il->scan_tx_ant[IEEE80211_BAND_5GHZ])
1473                 il->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
1474         if (!il->scan_tx_ant[IEEE80211_BAND_2GHZ])
1475                 il->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
1476 }
1477 EXPORT_SYMBOL(il_init_scan_params);
1478
1479 static int
1480 il_scan_initiate(struct il_priv *il, struct ieee80211_vif *vif)
1481 {
1482         int ret;
1483
1484         lockdep_assert_held(&il->mutex);
1485
1486         cancel_delayed_work(&il->scan_check);
1487
1488         if (!il_is_ready_rf(il)) {
1489                 IL_WARN("Request scan called when driver not ready.\n");
1490                 return -EIO;
1491         }
1492
1493         if (test_bit(S_SCAN_HW, &il->status)) {
1494                 D_SCAN("Multiple concurrent scan requests in parallel.\n");
1495                 return -EBUSY;
1496         }
1497
1498         if (test_bit(S_SCAN_ABORTING, &il->status)) {
1499                 D_SCAN("Scan request while abort pending.\n");
1500                 return -EBUSY;
1501         }
1502
1503         D_SCAN("Starting scan...\n");
1504
1505         set_bit(S_SCANNING, &il->status);
1506         il->scan_start = jiffies;
1507
1508         ret = il->ops->request_scan(il, vif);
1509         if (ret) {
1510                 clear_bit(S_SCANNING, &il->status);
1511                 return ret;
1512         }
1513
1514         queue_delayed_work(il->workqueue, &il->scan_check,
1515                            IL_SCAN_CHECK_WATCHDOG);
1516
1517         return 0;
1518 }
1519
1520 int
1521 il_mac_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1522                struct cfg80211_scan_request *req)
1523 {
1524         struct il_priv *il = hw->priv;
1525         int ret;
1526
1527         if (req->n_channels == 0) {
1528                 IL_ERR("Can not scan on no channels.\n");
1529                 return -EINVAL;
1530         }
1531
1532         mutex_lock(&il->mutex);
1533         D_MAC80211("enter\n");
1534
1535         if (test_bit(S_SCANNING, &il->status)) {
1536                 D_SCAN("Scan already in progress.\n");
1537                 ret = -EAGAIN;
1538                 goto out_unlock;
1539         }
1540
1541         /* mac80211 will only ask for one band at a time */
1542         il->scan_request = req;
1543         il->scan_vif = vif;
1544         il->scan_band = req->channels[0]->band;
1545
1546         ret = il_scan_initiate(il, vif);
1547
1548 out_unlock:
1549         D_MAC80211("leave ret %d\n", ret);
1550         mutex_unlock(&il->mutex);
1551
1552         return ret;
1553 }
1554 EXPORT_SYMBOL(il_mac_hw_scan);
1555
1556 static void
1557 il_bg_scan_check(struct work_struct *data)
1558 {
1559         struct il_priv *il =
1560             container_of(data, struct il_priv, scan_check.work);
1561
1562         D_SCAN("Scan check work\n");
1563
1564         /* Since we are here firmware does not finish scan and
1565          * most likely is in bad shape, so we don't bother to
1566          * send abort command, just force scan complete to mac80211 */
1567         mutex_lock(&il->mutex);
1568         il_force_scan_end(il);
1569         mutex_unlock(&il->mutex);
1570 }
1571
1572 /**
1573  * il_fill_probe_req - fill in all required fields and IE for probe request
1574  */
1575
1576 u16
1577 il_fill_probe_req(struct il_priv *il, struct ieee80211_mgmt *frame,
1578                   const u8 *ta, const u8 *ies, int ie_len, int left)
1579 {
1580         int len = 0;
1581         u8 *pos = NULL;
1582
1583         /* Make sure there is enough space for the probe request,
1584          * two mandatory IEs and the data */
1585         left -= 24;
1586         if (left < 0)
1587                 return 0;
1588
1589         frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1590         eth_broadcast_addr(frame->da);
1591         memcpy(frame->sa, ta, ETH_ALEN);
1592         eth_broadcast_addr(frame->bssid);
1593         frame->seq_ctrl = 0;
1594
1595         len += 24;
1596
1597         /* ...next IE... */
1598         pos = &frame->u.probe_req.variable[0];
1599
1600         /* fill in our indirect SSID IE */
1601         left -= 2;
1602         if (left < 0)
1603                 return 0;
1604         *pos++ = WLAN_EID_SSID;
1605         *pos++ = 0;
1606
1607         len += 2;
1608
1609         if (WARN_ON(left < ie_len))
1610                 return len;
1611
1612         if (ies && ie_len) {
1613                 memcpy(pos, ies, ie_len);
1614                 len += ie_len;
1615         }
1616
1617         return (u16) len;
1618 }
1619 EXPORT_SYMBOL(il_fill_probe_req);
1620
1621 static void
1622 il_bg_abort_scan(struct work_struct *work)
1623 {
1624         struct il_priv *il = container_of(work, struct il_priv, abort_scan);
1625
1626         D_SCAN("Abort scan work\n");
1627
1628         /* We keep scan_check work queued in case when firmware will not
1629          * report back scan completed notification */
1630         mutex_lock(&il->mutex);
1631         il_scan_cancel_timeout(il, 200);
1632         mutex_unlock(&il->mutex);
1633 }
1634
1635 static void
1636 il_bg_scan_completed(struct work_struct *work)
1637 {
1638         struct il_priv *il = container_of(work, struct il_priv, scan_completed);
1639         bool aborted;
1640
1641         D_SCAN("Completed scan.\n");
1642
1643         cancel_delayed_work(&il->scan_check);
1644
1645         mutex_lock(&il->mutex);
1646
1647         aborted = test_and_clear_bit(S_SCAN_ABORTING, &il->status);
1648         if (aborted)
1649                 D_SCAN("Aborted scan completed.\n");
1650
1651         if (!test_and_clear_bit(S_SCANNING, &il->status)) {
1652                 D_SCAN("Scan already completed.\n");
1653                 goto out_settings;
1654         }
1655
1656         il_complete_scan(il, aborted);
1657
1658 out_settings:
1659         /* Can we still talk to firmware ? */
1660         if (!il_is_ready_rf(il))
1661                 goto out;
1662
1663         /*
1664          * We do not commit power settings while scan is pending,
1665          * do it now if the settings changed.
1666          */
1667         il_power_set_mode(il, &il->power_data.sleep_cmd_next, false);
1668         il_set_tx_power(il, il->tx_power_next, false);
1669
1670         il->ops->post_scan(il);
1671
1672 out:
1673         mutex_unlock(&il->mutex);
1674 }
1675
1676 void
1677 il_setup_scan_deferred_work(struct il_priv *il)
1678 {
1679         INIT_WORK(&il->scan_completed, il_bg_scan_completed);
1680         INIT_WORK(&il->abort_scan, il_bg_abort_scan);
1681         INIT_DELAYED_WORK(&il->scan_check, il_bg_scan_check);
1682 }
1683 EXPORT_SYMBOL(il_setup_scan_deferred_work);
1684
1685 void
1686 il_cancel_scan_deferred_work(struct il_priv *il)
1687 {
1688         cancel_work_sync(&il->abort_scan);
1689         cancel_work_sync(&il->scan_completed);
1690
1691         if (cancel_delayed_work_sync(&il->scan_check)) {
1692                 mutex_lock(&il->mutex);
1693                 il_force_scan_end(il);
1694                 mutex_unlock(&il->mutex);
1695         }
1696 }
1697 EXPORT_SYMBOL(il_cancel_scan_deferred_work);
1698
1699 /* il->sta_lock must be held */
1700 static void
1701 il_sta_ucode_activate(struct il_priv *il, u8 sta_id)
1702 {
1703
1704         if (!(il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE))
1705                 IL_ERR("ACTIVATE a non DRIVER active station id %u addr %pM\n",
1706                        sta_id, il->stations[sta_id].sta.sta.addr);
1707
1708         if (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE) {
1709                 D_ASSOC("STA id %u addr %pM already present"
1710                         " in uCode (according to driver)\n", sta_id,
1711                         il->stations[sta_id].sta.sta.addr);
1712         } else {
1713                 il->stations[sta_id].used |= IL_STA_UCODE_ACTIVE;
1714                 D_ASSOC("Added STA id %u addr %pM to uCode\n", sta_id,
1715                         il->stations[sta_id].sta.sta.addr);
1716         }
1717 }
1718
1719 static int
1720 il_process_add_sta_resp(struct il_priv *il, struct il_addsta_cmd *addsta,
1721                         struct il_rx_pkt *pkt, bool sync)
1722 {
1723         u8 sta_id = addsta->sta.sta_id;
1724         unsigned long flags;
1725         int ret = -EIO;
1726
1727         if (pkt->hdr.flags & IL_CMD_FAILED_MSK) {
1728                 IL_ERR("Bad return from C_ADD_STA (0x%08X)\n", pkt->hdr.flags);
1729                 return ret;
1730         }
1731
1732         D_INFO("Processing response for adding station %u\n", sta_id);
1733
1734         spin_lock_irqsave(&il->sta_lock, flags);
1735
1736         switch (pkt->u.add_sta.status) {
1737         case ADD_STA_SUCCESS_MSK:
1738                 D_INFO("C_ADD_STA PASSED\n");
1739                 il_sta_ucode_activate(il, sta_id);
1740                 ret = 0;
1741                 break;
1742         case ADD_STA_NO_ROOM_IN_TBL:
1743                 IL_ERR("Adding station %d failed, no room in table.\n", sta_id);
1744                 break;
1745         case ADD_STA_NO_BLOCK_ACK_RESOURCE:
1746                 IL_ERR("Adding station %d failed, no block ack resource.\n",
1747                        sta_id);
1748                 break;
1749         case ADD_STA_MODIFY_NON_EXIST_STA:
1750                 IL_ERR("Attempting to modify non-existing station %d\n",
1751                        sta_id);
1752                 break;
1753         default:
1754                 D_ASSOC("Received C_ADD_STA:(0x%08X)\n", pkt->u.add_sta.status);
1755                 break;
1756         }
1757
1758         D_INFO("%s station id %u addr %pM\n",
1759                il->stations[sta_id].sta.mode ==
1760                STA_CONTROL_MODIFY_MSK ? "Modified" : "Added", sta_id,
1761                il->stations[sta_id].sta.sta.addr);
1762
1763         /*
1764          * XXX: The MAC address in the command buffer is often changed from
1765          * the original sent to the device. That is, the MAC address
1766          * written to the command buffer often is not the same MAC address
1767          * read from the command buffer when the command returns. This
1768          * issue has not yet been resolved and this debugging is left to
1769          * observe the problem.
1770          */
1771         D_INFO("%s station according to cmd buffer %pM\n",
1772                il->stations[sta_id].sta.mode ==
1773                STA_CONTROL_MODIFY_MSK ? "Modified" : "Added", addsta->sta.addr);
1774         spin_unlock_irqrestore(&il->sta_lock, flags);
1775
1776         return ret;
1777 }
1778
1779 static void
1780 il_add_sta_callback(struct il_priv *il, struct il_device_cmd *cmd,
1781                     struct il_rx_pkt *pkt)
1782 {
1783         struct il_addsta_cmd *addsta = (struct il_addsta_cmd *)cmd->cmd.payload;
1784
1785         il_process_add_sta_resp(il, addsta, pkt, false);
1786
1787 }
1788
1789 int
1790 il_send_add_sta(struct il_priv *il, struct il_addsta_cmd *sta, u8 flags)
1791 {
1792         struct il_rx_pkt *pkt = NULL;
1793         int ret = 0;
1794         u8 data[sizeof(*sta)];
1795         struct il_host_cmd cmd = {
1796                 .id = C_ADD_STA,
1797                 .flags = flags,
1798                 .data = data,
1799         };
1800         u8 sta_id __maybe_unused = sta->sta.sta_id;
1801
1802         D_INFO("Adding sta %u (%pM) %ssynchronously\n", sta_id, sta->sta.addr,
1803                flags & CMD_ASYNC ? "a" : "");
1804
1805         if (flags & CMD_ASYNC)
1806                 cmd.callback = il_add_sta_callback;
1807         else {
1808                 cmd.flags |= CMD_WANT_SKB;
1809                 might_sleep();
1810         }
1811
1812         cmd.len = il->ops->build_addsta_hcmd(sta, data);
1813         ret = il_send_cmd(il, &cmd);
1814
1815         if (ret || (flags & CMD_ASYNC))
1816                 return ret;
1817
1818         if (ret == 0) {
1819                 pkt = (struct il_rx_pkt *)cmd.reply_page;
1820                 ret = il_process_add_sta_resp(il, sta, pkt, true);
1821         }
1822         il_free_pages(il, cmd.reply_page);
1823
1824         return ret;
1825 }
1826 EXPORT_SYMBOL(il_send_add_sta);
1827
1828 static void
1829 il_set_ht_add_station(struct il_priv *il, u8 idx, struct ieee80211_sta *sta)
1830 {
1831         struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
1832         __le32 sta_flags;
1833         u8 mimo_ps_mode;
1834
1835         if (!sta || !sta_ht_inf->ht_supported)
1836                 goto done;
1837
1838         mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
1839         D_ASSOC("spatial multiplexing power save mode: %s\n",
1840                 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ? "static" :
1841                 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ? "dynamic" :
1842                 "disabled");
1843
1844         sta_flags = il->stations[idx].sta.station_flags;
1845
1846         sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
1847
1848         switch (mimo_ps_mode) {
1849         case WLAN_HT_CAP_SM_PS_STATIC:
1850                 sta_flags |= STA_FLG_MIMO_DIS_MSK;
1851                 break;
1852         case WLAN_HT_CAP_SM_PS_DYNAMIC:
1853                 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
1854                 break;
1855         case WLAN_HT_CAP_SM_PS_DISABLED:
1856                 break;
1857         default:
1858                 IL_WARN("Invalid MIMO PS mode %d\n", mimo_ps_mode);
1859                 break;
1860         }
1861
1862         sta_flags |=
1863             cpu_to_le32((u32) sta_ht_inf->
1864                         ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
1865
1866         sta_flags |=
1867             cpu_to_le32((u32) sta_ht_inf->
1868                         ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
1869
1870         if (il_is_ht40_tx_allowed(il, &sta->ht_cap))
1871                 sta_flags |= STA_FLG_HT40_EN_MSK;
1872         else
1873                 sta_flags &= ~STA_FLG_HT40_EN_MSK;
1874
1875         il->stations[idx].sta.station_flags = sta_flags;
1876 done:
1877         return;
1878 }
1879
1880 /**
1881  * il_prep_station - Prepare station information for addition
1882  *
1883  * should be called with sta_lock held
1884  */
1885 u8
1886 il_prep_station(struct il_priv *il, const u8 *addr, bool is_ap,
1887                 struct ieee80211_sta *sta)
1888 {
1889         struct il_station_entry *station;
1890         int i;
1891         u8 sta_id = IL_INVALID_STATION;
1892         u16 rate;
1893
1894         if (is_ap)
1895                 sta_id = IL_AP_ID;
1896         else if (is_broadcast_ether_addr(addr))
1897                 sta_id = il->hw_params.bcast_id;
1898         else
1899                 for (i = IL_STA_ID; i < il->hw_params.max_stations; i++) {
1900                         if (ether_addr_equal(il->stations[i].sta.sta.addr,
1901                                              addr)) {
1902                                 sta_id = i;
1903                                 break;
1904                         }
1905
1906                         if (!il->stations[i].used &&
1907                             sta_id == IL_INVALID_STATION)
1908                                 sta_id = i;
1909                 }
1910
1911         /*
1912          * These two conditions have the same outcome, but keep them
1913          * separate
1914          */
1915         if (unlikely(sta_id == IL_INVALID_STATION))
1916                 return sta_id;
1917
1918         /*
1919          * uCode is not able to deal with multiple requests to add a
1920          * station. Keep track if one is in progress so that we do not send
1921          * another.
1922          */
1923         if (il->stations[sta_id].used & IL_STA_UCODE_INPROGRESS) {
1924                 D_INFO("STA %d already in process of being added.\n", sta_id);
1925                 return sta_id;
1926         }
1927
1928         if ((il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE) &&
1929             (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE) &&
1930             ether_addr_equal(il->stations[sta_id].sta.sta.addr, addr)) {
1931                 D_ASSOC("STA %d (%pM) already added, not adding again.\n",
1932                         sta_id, addr);
1933                 return sta_id;
1934         }
1935
1936         station = &il->stations[sta_id];
1937         station->used = IL_STA_DRIVER_ACTIVE;
1938         D_ASSOC("Add STA to driver ID %d: %pM\n", sta_id, addr);
1939         il->num_stations++;
1940
1941         /* Set up the C_ADD_STA command to send to device */
1942         memset(&station->sta, 0, sizeof(struct il_addsta_cmd));
1943         memcpy(station->sta.sta.addr, addr, ETH_ALEN);
1944         station->sta.mode = 0;
1945         station->sta.sta.sta_id = sta_id;
1946         station->sta.station_flags = 0;
1947
1948         /*
1949          * OK to call unconditionally, since local stations (IBSS BSSID
1950          * STA and broadcast STA) pass in a NULL sta, and mac80211
1951          * doesn't allow HT IBSS.
1952          */
1953         il_set_ht_add_station(il, sta_id, sta);
1954
1955         /* 3945 only */
1956         rate = (il->band == IEEE80211_BAND_5GHZ) ? RATE_6M_PLCP : RATE_1M_PLCP;
1957         /* Turn on both antennas for the station... */
1958         station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
1959
1960         return sta_id;
1961
1962 }
1963 EXPORT_SYMBOL_GPL(il_prep_station);
1964
1965 #define STA_WAIT_TIMEOUT (HZ/2)
1966
1967 /**
1968  * il_add_station_common -
1969  */
1970 int
1971 il_add_station_common(struct il_priv *il, const u8 *addr, bool is_ap,
1972                       struct ieee80211_sta *sta, u8 *sta_id_r)
1973 {
1974         unsigned long flags_spin;
1975         int ret = 0;
1976         u8 sta_id;
1977         struct il_addsta_cmd sta_cmd;
1978
1979         *sta_id_r = 0;
1980         spin_lock_irqsave(&il->sta_lock, flags_spin);
1981         sta_id = il_prep_station(il, addr, is_ap, sta);
1982         if (sta_id == IL_INVALID_STATION) {
1983                 IL_ERR("Unable to prepare station %pM for addition\n", addr);
1984                 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
1985                 return -EINVAL;
1986         }
1987
1988         /*
1989          * uCode is not able to deal with multiple requests to add a
1990          * station. Keep track if one is in progress so that we do not send
1991          * another.
1992          */
1993         if (il->stations[sta_id].used & IL_STA_UCODE_INPROGRESS) {
1994                 D_INFO("STA %d already in process of being added.\n", sta_id);
1995                 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
1996                 return -EEXIST;
1997         }
1998
1999         if ((il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE) &&
2000             (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE)) {
2001                 D_ASSOC("STA %d (%pM) already added, not adding again.\n",
2002                         sta_id, addr);
2003                 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
2004                 return -EEXIST;
2005         }
2006
2007         il->stations[sta_id].used |= IL_STA_UCODE_INPROGRESS;
2008         memcpy(&sta_cmd, &il->stations[sta_id].sta,
2009                sizeof(struct il_addsta_cmd));
2010         spin_unlock_irqrestore(&il->sta_lock, flags_spin);
2011
2012         /* Add station to device's station table */
2013         ret = il_send_add_sta(il, &sta_cmd, CMD_SYNC);
2014         if (ret) {
2015                 spin_lock_irqsave(&il->sta_lock, flags_spin);
2016                 IL_ERR("Adding station %pM failed.\n",
2017                        il->stations[sta_id].sta.sta.addr);
2018                 il->stations[sta_id].used &= ~IL_STA_DRIVER_ACTIVE;
2019                 il->stations[sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
2020                 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
2021         }
2022         *sta_id_r = sta_id;
2023         return ret;
2024 }
2025 EXPORT_SYMBOL(il_add_station_common);
2026
2027 /**
2028  * il_sta_ucode_deactivate - deactivate ucode status for a station
2029  *
2030  * il->sta_lock must be held
2031  */
2032 static void
2033 il_sta_ucode_deactivate(struct il_priv *il, u8 sta_id)
2034 {
2035         /* Ucode must be active and driver must be non active */
2036         if ((il->stations[sta_id].
2037              used & (IL_STA_UCODE_ACTIVE | IL_STA_DRIVER_ACTIVE)) !=
2038             IL_STA_UCODE_ACTIVE)
2039                 IL_ERR("removed non active STA %u\n", sta_id);
2040
2041         il->stations[sta_id].used &= ~IL_STA_UCODE_ACTIVE;
2042
2043         memset(&il->stations[sta_id], 0, sizeof(struct il_station_entry));
2044         D_ASSOC("Removed STA %u\n", sta_id);
2045 }
2046
2047 static int
2048 il_send_remove_station(struct il_priv *il, const u8 * addr, int sta_id,
2049                        bool temporary)
2050 {
2051         struct il_rx_pkt *pkt;
2052         int ret;
2053
2054         unsigned long flags_spin;
2055         struct il_rem_sta_cmd rm_sta_cmd;
2056
2057         struct il_host_cmd cmd = {
2058                 .id = C_REM_STA,
2059                 .len = sizeof(struct il_rem_sta_cmd),
2060                 .flags = CMD_SYNC,
2061                 .data = &rm_sta_cmd,
2062         };
2063
2064         memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
2065         rm_sta_cmd.num_sta = 1;
2066         memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
2067
2068         cmd.flags |= CMD_WANT_SKB;
2069
2070         ret = il_send_cmd(il, &cmd);
2071
2072         if (ret)
2073                 return ret;
2074
2075         pkt = (struct il_rx_pkt *)cmd.reply_page;
2076         if (pkt->hdr.flags & IL_CMD_FAILED_MSK) {
2077                 IL_ERR("Bad return from C_REM_STA (0x%08X)\n", pkt->hdr.flags);
2078                 ret = -EIO;
2079         }
2080
2081         if (!ret) {
2082                 switch (pkt->u.rem_sta.status) {
2083                 case REM_STA_SUCCESS_MSK:
2084                         if (!temporary) {
2085                                 spin_lock_irqsave(&il->sta_lock, flags_spin);
2086                                 il_sta_ucode_deactivate(il, sta_id);
2087                                 spin_unlock_irqrestore(&il->sta_lock,
2088                                                        flags_spin);
2089                         }
2090                         D_ASSOC("C_REM_STA PASSED\n");
2091                         break;
2092                 default:
2093                         ret = -EIO;
2094                         IL_ERR("C_REM_STA failed\n");
2095                         break;
2096                 }
2097         }
2098         il_free_pages(il, cmd.reply_page);
2099
2100         return ret;
2101 }
2102
2103 /**
2104  * il_remove_station - Remove driver's knowledge of station.
2105  */
2106 int
2107 il_remove_station(struct il_priv *il, const u8 sta_id, const u8 * addr)
2108 {
2109         unsigned long flags;
2110
2111         if (!il_is_ready(il)) {
2112                 D_INFO("Unable to remove station %pM, device not ready.\n",
2113                        addr);
2114                 /*
2115                  * It is typical for stations to be removed when we are
2116                  * going down. Return success since device will be down
2117                  * soon anyway
2118                  */
2119                 return 0;
2120         }
2121
2122         D_ASSOC("Removing STA from driver:%d  %pM\n", sta_id, addr);
2123
2124         if (WARN_ON(sta_id == IL_INVALID_STATION))
2125                 return -EINVAL;
2126
2127         spin_lock_irqsave(&il->sta_lock, flags);
2128
2129         if (!(il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE)) {
2130                 D_INFO("Removing %pM but non DRIVER active\n", addr);
2131                 goto out_err;
2132         }
2133
2134         if (!(il->stations[sta_id].used & IL_STA_UCODE_ACTIVE)) {
2135                 D_INFO("Removing %pM but non UCODE active\n", addr);
2136                 goto out_err;
2137         }
2138
2139         if (il->stations[sta_id].used & IL_STA_LOCAL) {
2140                 kfree(il->stations[sta_id].lq);
2141                 il->stations[sta_id].lq = NULL;
2142         }
2143
2144         il->stations[sta_id].used &= ~IL_STA_DRIVER_ACTIVE;
2145
2146         il->num_stations--;
2147
2148         BUG_ON(il->num_stations < 0);
2149
2150         spin_unlock_irqrestore(&il->sta_lock, flags);
2151
2152         return il_send_remove_station(il, addr, sta_id, false);
2153 out_err:
2154         spin_unlock_irqrestore(&il->sta_lock, flags);
2155         return -EINVAL;
2156 }
2157 EXPORT_SYMBOL_GPL(il_remove_station);
2158
2159 /**
2160  * il_clear_ucode_stations - clear ucode station table bits
2161  *
2162  * This function clears all the bits in the driver indicating
2163  * which stations are active in the ucode. Call when something
2164  * other than explicit station management would cause this in
2165  * the ucode, e.g. unassociated RXON.
2166  */
2167 void
2168 il_clear_ucode_stations(struct il_priv *il)
2169 {
2170         int i;
2171         unsigned long flags_spin;
2172         bool cleared = false;
2173
2174         D_INFO("Clearing ucode stations in driver\n");
2175
2176         spin_lock_irqsave(&il->sta_lock, flags_spin);
2177         for (i = 0; i < il->hw_params.max_stations; i++) {
2178                 if (il->stations[i].used & IL_STA_UCODE_ACTIVE) {
2179                         D_INFO("Clearing ucode active for station %d\n", i);
2180                         il->stations[i].used &= ~IL_STA_UCODE_ACTIVE;
2181                         cleared = true;
2182                 }
2183         }
2184         spin_unlock_irqrestore(&il->sta_lock, flags_spin);
2185
2186         if (!cleared)
2187                 D_INFO("No active stations found to be cleared\n");
2188 }
2189 EXPORT_SYMBOL(il_clear_ucode_stations);
2190
2191 /**
2192  * il_restore_stations() - Restore driver known stations to device
2193  *
2194  * All stations considered active by driver, but not present in ucode, is
2195  * restored.
2196  *
2197  * Function sleeps.
2198  */
2199 void
2200 il_restore_stations(struct il_priv *il)
2201 {
2202         struct il_addsta_cmd sta_cmd;
2203         struct il_link_quality_cmd lq;
2204         unsigned long flags_spin;
2205         int i;
2206         bool found = false;
2207         int ret;
2208         bool send_lq;
2209
2210         if (!il_is_ready(il)) {
2211                 D_INFO("Not ready yet, not restoring any stations.\n");
2212                 return;
2213         }
2214
2215         D_ASSOC("Restoring all known stations ... start.\n");
2216         spin_lock_irqsave(&il->sta_lock, flags_spin);
2217         for (i = 0; i < il->hw_params.max_stations; i++) {
2218                 if ((il->stations[i].used & IL_STA_DRIVER_ACTIVE) &&
2219                     !(il->stations[i].used & IL_STA_UCODE_ACTIVE)) {
2220                         D_ASSOC("Restoring sta %pM\n",
2221                                 il->stations[i].sta.sta.addr);
2222                         il->stations[i].sta.mode = 0;
2223                         il->stations[i].used |= IL_STA_UCODE_INPROGRESS;
2224                         found = true;
2225                 }
2226         }
2227
2228         for (i = 0; i < il->hw_params.max_stations; i++) {
2229                 if ((il->stations[i].used & IL_STA_UCODE_INPROGRESS)) {
2230                         memcpy(&sta_cmd, &il->stations[i].sta,
2231                                sizeof(struct il_addsta_cmd));
2232                         send_lq = false;
2233                         if (il->stations[i].lq) {
2234                                 memcpy(&lq, il->stations[i].lq,
2235                                        sizeof(struct il_link_quality_cmd));
2236                                 send_lq = true;
2237                         }
2238                         spin_unlock_irqrestore(&il->sta_lock, flags_spin);
2239                         ret = il_send_add_sta(il, &sta_cmd, CMD_SYNC);
2240                         if (ret) {
2241                                 spin_lock_irqsave(&il->sta_lock, flags_spin);
2242                                 IL_ERR("Adding station %pM failed.\n",
2243                                        il->stations[i].sta.sta.addr);
2244                                 il->stations[i].used &= ~IL_STA_DRIVER_ACTIVE;
2245                                 il->stations[i].used &=
2246                                     ~IL_STA_UCODE_INPROGRESS;
2247                                 spin_unlock_irqrestore(&il->sta_lock,
2248                                                        flags_spin);
2249                         }
2250                         /*
2251                          * Rate scaling has already been initialized, send
2252                          * current LQ command
2253                          */
2254                         if (send_lq)
2255                                 il_send_lq_cmd(il, &lq, CMD_SYNC, true);
2256                         spin_lock_irqsave(&il->sta_lock, flags_spin);
2257                         il->stations[i].used &= ~IL_STA_UCODE_INPROGRESS;
2258                 }
2259         }
2260
2261         spin_unlock_irqrestore(&il->sta_lock, flags_spin);
2262         if (!found)
2263                 D_INFO("Restoring all known stations"
2264                        " .... no stations to be restored.\n");
2265         else
2266                 D_INFO("Restoring all known stations" " .... complete.\n");
2267 }
2268 EXPORT_SYMBOL(il_restore_stations);
2269
2270 int
2271 il_get_free_ucode_key_idx(struct il_priv *il)
2272 {
2273         int i;
2274
2275         for (i = 0; i < il->sta_key_max_num; i++)
2276                 if (!test_and_set_bit(i, &il->ucode_key_table))
2277                         return i;
2278
2279         return WEP_INVALID_OFFSET;
2280 }
2281 EXPORT_SYMBOL(il_get_free_ucode_key_idx);
2282
2283 void
2284 il_dealloc_bcast_stations(struct il_priv *il)
2285 {
2286         unsigned long flags;
2287         int i;
2288
2289         spin_lock_irqsave(&il->sta_lock, flags);
2290         for (i = 0; i < il->hw_params.max_stations; i++) {
2291                 if (!(il->stations[i].used & IL_STA_BCAST))
2292                         continue;
2293
2294                 il->stations[i].used &= ~IL_STA_UCODE_ACTIVE;
2295                 il->num_stations--;
2296                 BUG_ON(il->num_stations < 0);
2297                 kfree(il->stations[i].lq);
2298                 il->stations[i].lq = NULL;
2299         }
2300         spin_unlock_irqrestore(&il->sta_lock, flags);
2301 }
2302 EXPORT_SYMBOL_GPL(il_dealloc_bcast_stations);
2303
2304 #ifdef CONFIG_IWLEGACY_DEBUG
2305 static void
2306 il_dump_lq_cmd(struct il_priv *il, struct il_link_quality_cmd *lq)
2307 {
2308         int i;
2309         D_RATE("lq station id 0x%x\n", lq->sta_id);
2310         D_RATE("lq ant 0x%X 0x%X\n", lq->general_params.single_stream_ant_msk,
2311                lq->general_params.dual_stream_ant_msk);
2312
2313         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2314                 D_RATE("lq idx %d 0x%X\n", i, lq->rs_table[i].rate_n_flags);
2315 }
2316 #else
2317 static inline void
2318 il_dump_lq_cmd(struct il_priv *il, struct il_link_quality_cmd *lq)
2319 {
2320 }
2321 #endif
2322
2323 /**
2324  * il_is_lq_table_valid() - Test one aspect of LQ cmd for validity
2325  *
2326  * It sometimes happens when a HT rate has been in use and we
2327  * loose connectivity with AP then mac80211 will first tell us that the
2328  * current channel is not HT anymore before removing the station. In such a
2329  * scenario the RXON flags will be updated to indicate we are not
2330  * communicating HT anymore, but the LQ command may still contain HT rates.
2331  * Test for this to prevent driver from sending LQ command between the time
2332  * RXON flags are updated and when LQ command is updated.
2333  */
2334 static bool
2335 il_is_lq_table_valid(struct il_priv *il, struct il_link_quality_cmd *lq)
2336 {
2337         int i;
2338
2339         if (il->ht.enabled)
2340                 return true;
2341
2342         D_INFO("Channel %u is not an HT channel\n", il->active.channel);
2343         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2344                 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) & RATE_MCS_HT_MSK) {
2345                         D_INFO("idx %d of LQ expects HT channel\n", i);
2346                         return false;
2347                 }
2348         }
2349         return true;
2350 }
2351
2352 /**
2353  * il_send_lq_cmd() - Send link quality command
2354  * @init: This command is sent as part of station initialization right
2355  *        after station has been added.
2356  *
2357  * The link quality command is sent as the last step of station creation.
2358  * This is the special case in which init is set and we call a callback in
2359  * this case to clear the state indicating that station creation is in
2360  * progress.
2361  */
2362 int
2363 il_send_lq_cmd(struct il_priv *il, struct il_link_quality_cmd *lq,
2364                u8 flags, bool init)
2365 {
2366         int ret = 0;
2367         unsigned long flags_spin;
2368
2369         struct il_host_cmd cmd = {
2370                 .id = C_TX_LINK_QUALITY_CMD,
2371                 .len = sizeof(struct il_link_quality_cmd),
2372                 .flags = flags,
2373                 .data = lq,
2374         };
2375
2376         if (WARN_ON(lq->sta_id == IL_INVALID_STATION))
2377                 return -EINVAL;
2378
2379         spin_lock_irqsave(&il->sta_lock, flags_spin);
2380         if (!(il->stations[lq->sta_id].used & IL_STA_DRIVER_ACTIVE)) {
2381                 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
2382                 return -EINVAL;
2383         }
2384         spin_unlock_irqrestore(&il->sta_lock, flags_spin);
2385
2386         il_dump_lq_cmd(il, lq);
2387         BUG_ON(init && (cmd.flags & CMD_ASYNC));
2388
2389         if (il_is_lq_table_valid(il, lq))
2390                 ret = il_send_cmd(il, &cmd);
2391         else
2392                 ret = -EINVAL;
2393
2394         if (cmd.flags & CMD_ASYNC)
2395                 return ret;
2396
2397         if (init) {
2398                 D_INFO("init LQ command complete,"
2399                        " clearing sta addition status for sta %d\n",
2400                        lq->sta_id);
2401                 spin_lock_irqsave(&il->sta_lock, flags_spin);
2402                 il->stations[lq->sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
2403                 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
2404         }
2405         return ret;
2406 }
2407 EXPORT_SYMBOL(il_send_lq_cmd);
2408
2409 int
2410 il_mac_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2411                   struct ieee80211_sta *sta)
2412 {
2413         struct il_priv *il = hw->priv;
2414         struct il_station_priv_common *sta_common = (void *)sta->drv_priv;
2415         int ret;
2416
2417         mutex_lock(&il->mutex);
2418         D_MAC80211("enter station %pM\n", sta->addr);
2419
2420         ret = il_remove_station(il, sta_common->sta_id, sta->addr);
2421         if (ret)
2422                 IL_ERR("Error removing station %pM\n", sta->addr);
2423
2424         D_MAC80211("leave ret %d\n", ret);
2425         mutex_unlock(&il->mutex);
2426
2427         return ret;
2428 }
2429 EXPORT_SYMBOL(il_mac_sta_remove);
2430
2431 /************************** RX-FUNCTIONS ****************************/
2432 /*
2433  * Rx theory of operation
2434  *
2435  * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
2436  * each of which point to Receive Buffers to be filled by the NIC.  These get
2437  * used not only for Rx frames, but for any command response or notification
2438  * from the NIC.  The driver and NIC manage the Rx buffers by means
2439  * of idxes into the circular buffer.
2440  *
2441  * Rx Queue Indexes
2442  * The host/firmware share two idx registers for managing the Rx buffers.
2443  *
2444  * The READ idx maps to the first position that the firmware may be writing
2445  * to -- the driver can read up to (but not including) this position and get
2446  * good data.
2447  * The READ idx is managed by the firmware once the card is enabled.
2448  *
2449  * The WRITE idx maps to the last position the driver has read from -- the
2450  * position preceding WRITE is the last slot the firmware can place a packet.
2451  *
2452  * The queue is empty (no good data) if WRITE = READ - 1, and is full if
2453  * WRITE = READ.
2454  *
2455  * During initialization, the host sets up the READ queue position to the first
2456  * IDX position, and WRITE to the last (READ - 1 wrapped)
2457  *
2458  * When the firmware places a packet in a buffer, it will advance the READ idx
2459  * and fire the RX interrupt.  The driver can then query the READ idx and
2460  * process as many packets as possible, moving the WRITE idx forward as it
2461  * resets the Rx queue buffers with new memory.
2462  *
2463  * The management in the driver is as follows:
2464  * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free.  When
2465  *   iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
2466  *   to replenish the iwl->rxq->rx_free.
2467  * + In il_rx_replenish (scheduled) if 'processed' != 'read' then the
2468  *   iwl->rxq is replenished and the READ IDX is updated (updating the
2469  *   'processed' and 'read' driver idxes as well)
2470  * + A received packet is processed and handed to the kernel network stack,
2471  *   detached from the iwl->rxq.  The driver 'processed' idx is updated.
2472  * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
2473  *   list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
2474  *   IDX is not incremented and iwl->status(RX_STALLED) is set.  If there
2475  *   were enough free buffers and RX_STALLED is set it is cleared.
2476  *
2477  *
2478  * Driver sequence:
2479  *
2480  * il_rx_queue_alloc()   Allocates rx_free
2481  * il_rx_replenish()     Replenishes rx_free list from rx_used, and calls
2482  *                            il_rx_queue_restock
2483  * il_rx_queue_restock() Moves available buffers from rx_free into Rx
2484  *                            queue, updates firmware pointers, and updates
2485  *                            the WRITE idx.  If insufficient rx_free buffers
2486  *                            are available, schedules il_rx_replenish
2487  *
2488  * -- enable interrupts --
2489  * ISR - il_rx()         Detach il_rx_bufs from pool up to the
2490  *                            READ IDX, detaching the SKB from the pool.
2491  *                            Moves the packet buffer from queue to rx_used.
2492  *                            Calls il_rx_queue_restock to refill any empty
2493  *                            slots.
2494  * ...
2495  *
2496  */
2497
2498 /**
2499  * il_rx_queue_space - Return number of free slots available in queue.
2500  */
2501 int
2502 il_rx_queue_space(const struct il_rx_queue *q)
2503 {
2504         int s = q->read - q->write;
2505         if (s <= 0)
2506                 s += RX_QUEUE_SIZE;
2507         /* keep some buffer to not confuse full and empty queue */
2508         s -= 2;
2509         if (s < 0)
2510                 s = 0;
2511         return s;
2512 }
2513 EXPORT_SYMBOL(il_rx_queue_space);
2514
2515 /**
2516  * il_rx_queue_update_write_ptr - Update the write pointer for the RX queue
2517  */
2518 void
2519 il_rx_queue_update_write_ptr(struct il_priv *il, struct il_rx_queue *q)
2520 {
2521         unsigned long flags;
2522         u32 rx_wrt_ptr_reg = il->hw_params.rx_wrt_ptr_reg;
2523         u32 reg;
2524
2525         spin_lock_irqsave(&q->lock, flags);
2526
2527         if (q->need_update == 0)
2528                 goto exit_unlock;
2529
2530         /* If power-saving is in use, make sure device is awake */
2531         if (test_bit(S_POWER_PMI, &il->status)) {
2532                 reg = _il_rd(il, CSR_UCODE_DRV_GP1);
2533
2534                 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
2535                         D_INFO("Rx queue requesting wakeup," " GP1 = 0x%x\n",
2536                                reg);
2537                         il_set_bit(il, CSR_GP_CNTRL,
2538                                    CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
2539                         goto exit_unlock;
2540                 }
2541
2542                 q->write_actual = (q->write & ~0x7);
2543                 il_wr(il, rx_wrt_ptr_reg, q->write_actual);
2544
2545                 /* Else device is assumed to be awake */
2546         } else {
2547                 /* Device expects a multiple of 8 */
2548                 q->write_actual = (q->write & ~0x7);
2549                 il_wr(il, rx_wrt_ptr_reg, q->write_actual);
2550         }
2551
2552         q->need_update = 0;
2553
2554 exit_unlock:
2555         spin_unlock_irqrestore(&q->lock, flags);
2556 }
2557 EXPORT_SYMBOL(il_rx_queue_update_write_ptr);
2558
2559 int
2560 il_rx_queue_alloc(struct il_priv *il)
2561 {
2562         struct il_rx_queue *rxq = &il->rxq;
2563         struct device *dev = &il->pci_dev->dev;
2564         int i;
2565
2566         spin_lock_init(&rxq->lock);
2567         INIT_LIST_HEAD(&rxq->rx_free);
2568         INIT_LIST_HEAD(&rxq->rx_used);
2569
2570         /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
2571         rxq->bd =
2572             dma_alloc_coherent(dev, 4 * RX_QUEUE_SIZE, &rxq->bd_dma,
2573                                GFP_KERNEL);
2574         if (!rxq->bd)
2575                 goto err_bd;
2576
2577         rxq->rb_stts =
2578             dma_alloc_coherent(dev, sizeof(struct il_rb_status),
2579                                &rxq->rb_stts_dma, GFP_KERNEL);
2580         if (!rxq->rb_stts)
2581                 goto err_rb;
2582
2583         /* Fill the rx_used queue with _all_ of the Rx buffers */
2584         for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
2585                 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
2586
2587         /* Set us so that we have processed and used all buffers, but have
2588          * not restocked the Rx queue with fresh buffers */
2589         rxq->read = rxq->write = 0;
2590         rxq->write_actual = 0;
2591         rxq->free_count = 0;
2592         rxq->need_update = 0;
2593         return 0;
2594
2595 err_rb:
2596         dma_free_coherent(&il->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
2597                           rxq->bd_dma);
2598 err_bd:
2599         return -ENOMEM;
2600 }
2601 EXPORT_SYMBOL(il_rx_queue_alloc);
2602
2603 void
2604 il_hdl_spectrum_measurement(struct il_priv *il, struct il_rx_buf *rxb)
2605 {
2606         struct il_rx_pkt *pkt = rxb_addr(rxb);
2607         struct il_spectrum_notification *report = &(pkt->u.spectrum_notif);
2608
2609         if (!report->state) {
2610                 D_11H("Spectrum Measure Notification: Start\n");
2611                 return;
2612         }
2613
2614         memcpy(&il->measure_report, report, sizeof(*report));
2615         il->measurement_status |= MEASUREMENT_READY;
2616 }
2617 EXPORT_SYMBOL(il_hdl_spectrum_measurement);
2618
2619 /*
2620  * returns non-zero if packet should be dropped
2621  */
2622 int
2623 il_set_decrypted_flag(struct il_priv *il, struct ieee80211_hdr *hdr,
2624                       u32 decrypt_res, struct ieee80211_rx_status *stats)
2625 {
2626         u16 fc = le16_to_cpu(hdr->frame_control);
2627
2628         /*
2629          * All contexts have the same setting here due to it being
2630          * a module parameter, so OK to check any context.
2631          */
2632         if (il->active.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2633                 return 0;
2634
2635         if (!(fc & IEEE80211_FCTL_PROTECTED))
2636                 return 0;
2637
2638         D_RX("decrypt_res:0x%x\n", decrypt_res);
2639         switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2640         case RX_RES_STATUS_SEC_TYPE_TKIP:
2641                 /* The uCode has got a bad phase 1 Key, pushes the packet.
2642                  * Decryption will be done in SW. */
2643                 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2644                     RX_RES_STATUS_BAD_KEY_TTAK)
2645                         break;
2646
2647         case RX_RES_STATUS_SEC_TYPE_WEP:
2648                 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2649                     RX_RES_STATUS_BAD_ICV_MIC) {
2650                         /* bad ICV, the packet is destroyed since the
2651                          * decryption is inplace, drop it */
2652                         D_RX("Packet destroyed\n");
2653                         return -1;
2654                 }
2655         case RX_RES_STATUS_SEC_TYPE_CCMP:
2656                 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2657                     RX_RES_STATUS_DECRYPT_OK) {
2658                         D_RX("hw decrypt successfully!!!\n");
2659                         stats->flag |= RX_FLAG_DECRYPTED;
2660                 }
2661                 break;
2662
2663         default:
2664                 break;
2665         }
2666         return 0;
2667 }
2668 EXPORT_SYMBOL(il_set_decrypted_flag);
2669
2670 /**
2671  * il_txq_update_write_ptr - Send new write idx to hardware
2672  */
2673 void
2674 il_txq_update_write_ptr(struct il_priv *il, struct il_tx_queue *txq)
2675 {
2676         u32 reg = 0;
2677         int txq_id = txq->q.id;
2678
2679         if (txq->need_update == 0)
2680                 return;
2681
2682         /* if we're trying to save power */
2683         if (test_bit(S_POWER_PMI, &il->status)) {
2684                 /* wake up nic if it's powered down ...
2685                  * uCode will wake up, and interrupt us again, so next
2686                  * time we'll skip this part. */
2687                 reg = _il_rd(il, CSR_UCODE_DRV_GP1);
2688
2689                 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
2690                         D_INFO("Tx queue %d requesting wakeup," " GP1 = 0x%x\n",
2691                                txq_id, reg);
2692                         il_set_bit(il, CSR_GP_CNTRL,
2693                                    CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
2694                         return;
2695                 }
2696
2697                 il_wr(il, HBUS_TARG_WRPTR, txq->q.write_ptr | (txq_id << 8));
2698
2699                 /*
2700                  * else not in power-save mode,
2701                  * uCode will never sleep when we're
2702                  * trying to tx (during RFKILL, we're not trying to tx).
2703                  */
2704         } else
2705                 _il_wr(il, HBUS_TARG_WRPTR, txq->q.write_ptr | (txq_id << 8));
2706         txq->need_update = 0;
2707 }
2708 EXPORT_SYMBOL(il_txq_update_write_ptr);
2709
2710 /**
2711  * il_tx_queue_unmap -  Unmap any remaining DMA mappings and free skb's
2712  */
2713 void
2714 il_tx_queue_unmap(struct il_priv *il, int txq_id)
2715 {
2716         struct il_tx_queue *txq = &il->txq[txq_id];
2717         struct il_queue *q = &txq->q;
2718
2719         if (q->n_bd == 0)
2720                 return;
2721
2722         while (q->write_ptr != q->read_ptr) {
2723                 il->ops->txq_free_tfd(il, txq);
2724                 q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd);
2725         }
2726 }
2727 EXPORT_SYMBOL(il_tx_queue_unmap);
2728
2729 /**
2730  * il_tx_queue_free - Deallocate DMA queue.
2731  * @txq: Transmit queue to deallocate.
2732  *
2733  * Empty queue by removing and destroying all BD's.
2734  * Free all buffers.
2735  * 0-fill, but do not free "txq" descriptor structure.
2736  */
2737 void
2738 il_tx_queue_free(struct il_priv *il, int txq_id)
2739 {
2740         struct il_tx_queue *txq = &il->txq[txq_id];
2741         struct device *dev = &il->pci_dev->dev;
2742         int i;
2743
2744         il_tx_queue_unmap(il, txq_id);
2745
2746         /* De-alloc array of command/tx buffers */
2747         for (i = 0; i < TFD_TX_CMD_SLOTS; i++)
2748                 kfree(txq->cmd[i]);
2749
2750         /* De-alloc circular buffer of TFDs */
2751         if (txq->q.n_bd)
2752                 dma_free_coherent(dev, il->hw_params.tfd_size * txq->q.n_bd,
2753                                   txq->tfds, txq->q.dma_addr);
2754
2755         /* De-alloc array of per-TFD driver data */
2756         kfree(txq->skbs);
2757         txq->skbs = NULL;
2758
2759         /* deallocate arrays */
2760         kfree(txq->cmd);
2761         kfree(txq->meta);
2762         txq->cmd = NULL;
2763         txq->meta = NULL;
2764
2765         /* 0-fill queue descriptor structure */
2766         memset(txq, 0, sizeof(*txq));
2767 }
2768 EXPORT_SYMBOL(il_tx_queue_free);
2769
2770 /**
2771  * il_cmd_queue_unmap - Unmap any remaining DMA mappings from command queue
2772  */
2773 void
2774 il_cmd_queue_unmap(struct il_priv *il)
2775 {
2776         struct il_tx_queue *txq = &il->txq[il->cmd_queue];
2777         struct il_queue *q = &txq->q;
2778         int i;
2779
2780         if (q->n_bd == 0)
2781                 return;
2782
2783         while (q->read_ptr != q->write_ptr) {
2784                 i = il_get_cmd_idx(q, q->read_ptr, 0);
2785
2786                 if (txq->meta[i].flags & CMD_MAPPED) {
2787                         pci_unmap_single(il->pci_dev,
2788                                          dma_unmap_addr(&txq->meta[i], mapping),
2789                                          dma_unmap_len(&txq->meta[i], len),
2790                                          PCI_DMA_BIDIRECTIONAL);
2791                         txq->meta[i].flags = 0;
2792                 }
2793
2794                 q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd);
2795         }
2796
2797         i = q->n_win;
2798         if (txq->meta[i].flags & CMD_MAPPED) {
2799                 pci_unmap_single(il->pci_dev,
2800                                  dma_unmap_addr(&txq->meta[i], mapping),
2801                                  dma_unmap_len(&txq->meta[i], len),
2802                                  PCI_DMA_BIDIRECTIONAL);
2803                 txq->meta[i].flags = 0;
2804         }
2805 }
2806 EXPORT_SYMBOL(il_cmd_queue_unmap);
2807
2808 /**
2809  * il_cmd_queue_free - Deallocate DMA queue.
2810  * @txq: Transmit queue to deallocate.
2811  *
2812  * Empty queue by removing and destroying all BD's.
2813  * Free all buffers.
2814  * 0-fill, but do not free "txq" descriptor structure.
2815  */
2816 void
2817 il_cmd_queue_free(struct il_priv *il)
2818 {
2819         struct il_tx_queue *txq = &il->txq[il->cmd_queue];
2820         struct device *dev = &il->pci_dev->dev;
2821         int i;
2822
2823         il_cmd_queue_unmap(il);
2824
2825         /* De-alloc array of command/tx buffers */
2826         for (i = 0; i <= TFD_CMD_SLOTS; i++)
2827                 kfree(txq->cmd[i]);
2828
2829         /* De-alloc circular buffer of TFDs */
2830         if (txq->q.n_bd)
2831                 dma_free_coherent(dev, il->hw_params.tfd_size * txq->q.n_bd,
2832                                   txq->tfds, txq->q.dma_addr);
2833
2834         /* deallocate arrays */
2835         kfree(txq->cmd);
2836         kfree(txq->meta);
2837         txq->cmd = NULL;
2838         txq->meta = NULL;
2839
2840         /* 0-fill queue descriptor structure */
2841         memset(txq, 0, sizeof(*txq));
2842 }
2843 EXPORT_SYMBOL(il_cmd_queue_free);
2844
2845 /*************** DMA-QUEUE-GENERAL-FUNCTIONS  *****
2846  * DMA services
2847  *
2848  * Theory of operation
2849  *
2850  * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
2851  * of buffer descriptors, each of which points to one or more data buffers for
2852  * the device to read from or fill.  Driver and device exchange status of each
2853  * queue via "read" and "write" pointers.  Driver keeps minimum of 2 empty
2854  * entries in each circular buffer, to protect against confusing empty and full
2855  * queue states.
2856  *
2857  * The device reads or writes the data in the queues via the device's several
2858  * DMA/FIFO channels.  Each queue is mapped to a single DMA channel.
2859  *
2860  * For Tx queue, there are low mark and high mark limits. If, after queuing
2861  * the packet for Tx, free space become < low mark, Tx queue stopped. When
2862  * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
2863  * Tx queue resumed.
2864  *
2865  * See more detailed info in 4965.h.
2866  ***************************************************/
2867
2868 int
2869 il_queue_space(const struct il_queue *q)
2870 {
2871         int s = q->read_ptr - q->write_ptr;
2872
2873         if (q->read_ptr > q->write_ptr)
2874                 s -= q->n_bd;
2875
2876         if (s <= 0)
2877                 s += q->n_win;
2878         /* keep some reserve to not confuse empty and full situations */
2879         s -= 2;
2880         if (s < 0)
2881                 s = 0;
2882         return s;
2883 }
2884 EXPORT_SYMBOL(il_queue_space);
2885
2886
2887 /**
2888  * il_queue_init - Initialize queue's high/low-water and read/write idxes
2889  */
2890 static int
2891 il_queue_init(struct il_priv *il, struct il_queue *q, int slots, u32 id)
2892 {
2893         /*
2894          * TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
2895          * il_queue_inc_wrap and il_queue_dec_wrap are broken.
2896          */
2897         BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
2898         /* FIXME: remove q->n_bd */
2899         q->n_bd = TFD_QUEUE_SIZE_MAX;
2900
2901         q->n_win = slots;
2902         q->id = id;
2903
2904         /* slots_must be power-of-two size, otherwise
2905          * il_get_cmd_idx is broken. */
2906         BUG_ON(!is_power_of_2(slots));
2907
2908         q->low_mark = q->n_win / 4;
2909         if (q->low_mark < 4)
2910                 q->low_mark = 4;
2911
2912         q->high_mark = q->n_win / 8;
2913         if (q->high_mark < 2)
2914                 q->high_mark = 2;
2915
2916         q->write_ptr = q->read_ptr = 0;
2917
2918         return 0;
2919 }
2920
2921 /**
2922  * il_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
2923  */
2924 static int
2925 il_tx_queue_alloc(struct il_priv *il, struct il_tx_queue *txq, u32 id)
2926 {
2927         struct device *dev = &il->pci_dev->dev;
2928         size_t tfd_sz = il->hw_params.tfd_size * TFD_QUEUE_SIZE_MAX;
2929
2930         /* Driver ilate data, only for Tx (not command) queues,
2931          * not shared with device. */
2932         if (id != il->cmd_queue) {
2933                 txq->skbs = kcalloc(TFD_QUEUE_SIZE_MAX, sizeof(struct skb *),
2934                                     GFP_KERNEL);
2935                 if (!txq->skbs) {
2936                         IL_ERR("Fail to alloc skbs\n");
2937                         goto error;
2938                 }
2939         } else
2940                 txq->skbs = NULL;
2941
2942         /* Circular buffer of transmit frame descriptors (TFDs),
2943          * shared with device */
2944         txq->tfds =
2945             dma_alloc_coherent(dev, tfd_sz, &txq->q.dma_addr, GFP_KERNEL);
2946         if (!txq->tfds) {
2947                 IL_ERR("Fail to alloc TFDs\n");
2948                 goto error;
2949         }
2950         txq->q.id = id;
2951
2952         return 0;
2953
2954 error:
2955         kfree(txq->skbs);
2956         txq->skbs = NULL;
2957
2958         return -ENOMEM;
2959 }
2960
2961 /**
2962  * il_tx_queue_init - Allocate and initialize one tx/cmd queue
2963  */
2964 int
2965 il_tx_queue_init(struct il_priv *il, u32 txq_id)
2966 {
2967         int i, len, ret;
2968         int slots, actual_slots;
2969         struct il_tx_queue *txq = &il->txq[txq_id];
2970
2971         /*
2972          * Alloc buffer array for commands (Tx or other types of commands).
2973          * For the command queue (#4/#9), allocate command space + one big
2974          * command for scan, since scan command is very huge; the system will
2975          * not have two scans at the same time, so only one is needed.
2976          * For normal Tx queues (all other queues), no super-size command
2977          * space is needed.
2978          */
2979         if (txq_id == il->cmd_queue) {
2980                 slots = TFD_CMD_SLOTS;
2981                 actual_slots = slots + 1;
2982         } else {
2983                 slots = TFD_TX_CMD_SLOTS;
2984                 actual_slots = slots;
2985         }
2986
2987         txq->meta =
2988             kzalloc(sizeof(struct il_cmd_meta) * actual_slots, GFP_KERNEL);
2989         txq->cmd =
2990             kzalloc(sizeof(struct il_device_cmd *) * actual_slots, GFP_KERNEL);
2991
2992         if (!txq->meta || !txq->cmd)
2993                 goto out_free_arrays;
2994
2995         len = sizeof(struct il_device_cmd);
2996         for (i = 0; i < actual_slots; i++) {
2997                 /* only happens for cmd queue */
2998                 if (i == slots)
2999                         len = IL_MAX_CMD_SIZE;
3000
3001                 txq->cmd[i] = kmalloc(len, GFP_KERNEL);
3002                 if (!txq->cmd[i])
3003                         goto err;
3004         }
3005
3006         /* Alloc driver data array and TFD circular buffer */
3007         ret = il_tx_queue_alloc(il, txq, txq_id);
3008         if (ret)
3009                 goto err;
3010
3011         txq->need_update = 0;
3012
3013         /*
3014          * For the default queues 0-3, set up the swq_id
3015          * already -- all others need to get one later
3016          * (if they need one at all).
3017          */
3018         if (txq_id < 4)
3019                 il_set_swq_id(txq, txq_id, txq_id);
3020
3021         /* Initialize queue's high/low-water marks, and head/tail idxes */
3022         il_queue_init(il, &txq->q, slots, txq_id);
3023
3024         /* Tell device where to find queue */
3025         il->ops->txq_init(il, txq);
3026
3027         return 0;
3028 err:
3029         for (i = 0; i < actual_slots; i++)
3030                 kfree(txq->cmd[i]);
3031 out_free_arrays:
3032         kfree(txq->meta);
3033         kfree(txq->cmd);
3034
3035         return -ENOMEM;
3036 }
3037 EXPORT_SYMBOL(il_tx_queue_init);
3038
3039 void
3040 il_tx_queue_reset(struct il_priv *il, u32 txq_id)
3041 {
3042         int slots, actual_slots;
3043         struct il_tx_queue *txq = &il->txq[txq_id];
3044
3045         if (txq_id == il->cmd_queue) {
3046                 slots = TFD_CMD_SLOTS;
3047                 actual_slots = TFD_CMD_SLOTS + 1;
3048         } else {
3049                 slots = TFD_TX_CMD_SLOTS;
3050                 actual_slots = TFD_TX_CMD_SLOTS;
3051         }
3052
3053         memset(txq->meta, 0, sizeof(struct il_cmd_meta) * actual_slots);
3054         txq->need_update = 0;
3055
3056         /* Initialize queue's high/low-water marks, and head/tail idxes */
3057         il_queue_init(il, &txq->q, slots, txq_id);
3058
3059         /* Tell device where to find queue */
3060         il->ops->txq_init(il, txq);
3061 }
3062 EXPORT_SYMBOL(il_tx_queue_reset);
3063
3064 /*************** HOST COMMAND QUEUE FUNCTIONS   *****/
3065
3066 /**
3067  * il_enqueue_hcmd - enqueue a uCode command
3068  * @il: device ilate data point
3069  * @cmd: a point to the ucode command structure
3070  *
3071  * The function returns < 0 values to indicate the operation is
3072  * failed. On success, it turns the idx (> 0) of command in the
3073  * command queue.
3074  */
3075 int
3076 il_enqueue_hcmd(struct il_priv *il, struct il_host_cmd *cmd)
3077 {
3078         struct il_tx_queue *txq = &il->txq[il->cmd_queue];
3079         struct il_queue *q = &txq->q;
3080         struct il_device_cmd *out_cmd;
3081         struct il_cmd_meta *out_meta;
3082         dma_addr_t phys_addr;
3083         unsigned long flags;
3084         int len;
3085         u32 idx;
3086         u16 fix_size;
3087
3088         cmd->len = il->ops->get_hcmd_size(cmd->id, cmd->len);
3089         fix_size = (u16) (cmd->len + sizeof(out_cmd->hdr));
3090
3091         /* If any of the command structures end up being larger than
3092          * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
3093          * we will need to increase the size of the TFD entries
3094          * Also, check to see if command buffer should not exceed the size
3095          * of device_cmd and max_cmd_size. */
3096         BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
3097                !(cmd->flags & CMD_SIZE_HUGE));
3098         BUG_ON(fix_size > IL_MAX_CMD_SIZE);
3099
3100         if (il_is_rfkill(il) || il_is_ctkill(il)) {
3101                 IL_WARN("Not sending command - %s KILL\n",
3102                         il_is_rfkill(il) ? "RF" : "CT");
3103                 return -EIO;
3104         }
3105
3106         spin_lock_irqsave(&il->hcmd_lock, flags);
3107
3108         if (il_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
3109                 spin_unlock_irqrestore(&il->hcmd_lock, flags);
3110
3111                 IL_ERR("Restarting adapter due to command queue full\n");
3112                 queue_work(il->workqueue, &il->restart);
3113                 return -ENOSPC;
3114         }
3115
3116         idx = il_get_cmd_idx(q, q->write_ptr, cmd->flags & CMD_SIZE_HUGE);
3117         out_cmd = txq->cmd[idx];
3118         out_meta = &txq->meta[idx];
3119
3120         if (WARN_ON(out_meta->flags & CMD_MAPPED)) {
3121                 spin_unlock_irqrestore(&il->hcmd_lock, flags);
3122                 return -ENOSPC;
3123         }
3124
3125         memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
3126         out_meta->flags = cmd->flags | CMD_MAPPED;
3127         if (cmd->flags & CMD_WANT_SKB)
3128                 out_meta->source = cmd;
3129         if (cmd->flags & CMD_ASYNC)
3130                 out_meta->callback = cmd->callback;
3131
3132         out_cmd->hdr.cmd = cmd->id;
3133         memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
3134
3135         /* At this point, the out_cmd now has all of the incoming cmd
3136          * information */
3137
3138         out_cmd->hdr.flags = 0;
3139         out_cmd->hdr.sequence =
3140             cpu_to_le16(QUEUE_TO_SEQ(il->cmd_queue) | IDX_TO_SEQ(q->write_ptr));
3141         if (cmd->flags & CMD_SIZE_HUGE)
3142                 out_cmd->hdr.sequence |= SEQ_HUGE_FRAME;
3143         len = sizeof(struct il_device_cmd);
3144         if (idx == TFD_CMD_SLOTS)
3145                 len = IL_MAX_CMD_SIZE;
3146
3147 #ifdef CONFIG_IWLEGACY_DEBUG
3148         switch (out_cmd->hdr.cmd) {
3149         case C_TX_LINK_QUALITY_CMD:
3150         case C_SENSITIVITY:
3151                 D_HC_DUMP("Sending command %s (#%x), seq: 0x%04X, "
3152                           "%d bytes at %d[%d]:%d\n",
3153                           il_get_cmd_string(out_cmd->hdr.cmd), out_cmd->hdr.cmd,
3154                           le16_to_cpu(out_cmd->hdr.sequence), fix_size,
3155                           q->write_ptr, idx, il->cmd_queue);
3156                 break;
3157         default:
3158                 D_HC("Sending command %s (#%x), seq: 0x%04X, "
3159                      "%d bytes at %d[%d]:%d\n",
3160                      il_get_cmd_string(out_cmd->hdr.cmd), out_cmd->hdr.cmd,
3161                      le16_to_cpu(out_cmd->hdr.sequence), fix_size, q->write_ptr,
3162                      idx, il->cmd_queue);
3163         }
3164 #endif
3165         txq->need_update = 1;
3166
3167         if (il->ops->txq_update_byte_cnt_tbl)
3168                 /* Set up entry in queue's byte count circular buffer */
3169                 il->ops->txq_update_byte_cnt_tbl(il, txq, 0);
3170
3171         phys_addr =
3172             pci_map_single(il->pci_dev, &out_cmd->hdr, fix_size,
3173                            PCI_DMA_BIDIRECTIONAL);
3174         dma_unmap_addr_set(out_meta, mapping, phys_addr);
3175         dma_unmap_len_set(out_meta, len, fix_size);
3176
3177         il->ops->txq_attach_buf_to_tfd(il, txq, phys_addr, fix_size, 1,
3178                                             U32_PAD(cmd->len));
3179
3180         /* Increment and update queue's write idx */
3181         q->write_ptr = il_queue_inc_wrap(q->write_ptr, q->n_bd);
3182         il_txq_update_write_ptr(il, txq);
3183
3184         spin_unlock_irqrestore(&il->hcmd_lock, flags);
3185         return idx;
3186 }
3187
3188 /**
3189  * il_hcmd_queue_reclaim - Reclaim TX command queue entries already Tx'd
3190  *
3191  * When FW advances 'R' idx, all entries between old and new 'R' idx
3192  * need to be reclaimed. As result, some free space forms.  If there is
3193  * enough free space (> low mark), wake the stack that feeds us.
3194  */
3195 static void
3196 il_hcmd_queue_reclaim(struct il_priv *il, int txq_id, int idx, int cmd_idx)
3197 {
3198         struct il_tx_queue *txq = &il->txq[txq_id];
3199         struct il_queue *q = &txq->q;
3200         int nfreed = 0;
3201
3202         if (idx >= q->n_bd || il_queue_used(q, idx) == 0) {
3203                 IL_ERR("Read idx for DMA queue txq id (%d), idx %d, "
3204                        "is out of range [0-%d] %d %d.\n", txq_id, idx, q->n_bd,
3205                        q->write_ptr, q->read_ptr);
3206                 return;
3207         }
3208
3209         for (idx = il_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
3210              q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd)) {
3211
3212                 if (nfreed++ > 0) {
3213                         IL_ERR("HCMD skipped: idx (%d) %d %d\n", idx,
3214                                q->write_ptr, q->read_ptr);
3215                         queue_work(il->workqueue, &il->restart);
3216                 }
3217
3218         }
3219 }
3220
3221 /**
3222  * il_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
3223  * @rxb: Rx buffer to reclaim
3224  *
3225  * If an Rx buffer has an async callback associated with it the callback
3226  * will be executed.  The attached skb (if present) will only be freed
3227  * if the callback returns 1
3228  */
3229 void
3230 il_tx_cmd_complete(struct il_priv *il, struct il_rx_buf *rxb)
3231 {
3232         struct il_rx_pkt *pkt = rxb_addr(rxb);
3233         u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3234         int txq_id = SEQ_TO_QUEUE(sequence);
3235         int idx = SEQ_TO_IDX(sequence);
3236         int cmd_idx;
3237         bool huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
3238         struct il_device_cmd *cmd;
3239         struct il_cmd_meta *meta;
3240         struct il_tx_queue *txq = &il->txq[il->cmd_queue];
3241         unsigned long flags;
3242
3243         /* If a Tx command is being handled and it isn't in the actual
3244          * command queue then there a command routing bug has been introduced
3245          * in the queue management code. */
3246         if (WARN
3247             (txq_id != il->cmd_queue,
3248              "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
3249              txq_id, il->cmd_queue, sequence, il->txq[il->cmd_queue].q.read_ptr,
3250              il->txq[il->cmd_queue].q.write_ptr)) {
3251                 il_print_hex_error(il, pkt, 32);
3252                 return;
3253         }
3254
3255         cmd_idx = il_get_cmd_idx(&txq->q, idx, huge);
3256         cmd = txq->cmd[cmd_idx];
3257