1 // SPDX-License-Identifier: GPL-2.0-only
3 * Implementation of host-to-chip commands (aka request/confirmation) of WFxxx
6 * Copyright (c) 2017-2019, Silicon Laboratories, Inc.
7 * Copyright (c) 2010, ST-Ericsson
9 #include <linux/etherdevice.h>
18 void wfx_init_hif_cmd(struct wfx_hif_cmd
*hif_cmd
)
20 init_completion(&hif_cmd
->ready
);
21 init_completion(&hif_cmd
->done
);
22 mutex_init(&hif_cmd
->lock
);
23 mutex_init(&hif_cmd
->key_renew_lock
);
26 static void wfx_fill_header(struct hif_msg
*hif
, int if_id
, unsigned int cmd
,
32 WARN(cmd
> 0x3f, "invalid WSM command %#.2x", cmd
);
33 WARN(size
> 0xFFF, "requested buffer is too large: %zu bytes", size
);
34 WARN(if_id
> 0x3, "invalid interface ID %d", if_id
);
36 hif
->len
= cpu_to_le16(size
+ 4);
38 hif
->interface
= if_id
;
41 static void *wfx_alloc_hif(size_t body_len
, struct hif_msg
**hif
)
43 *hif
= kzalloc(sizeof(struct hif_msg
) + body_len
, GFP_KERNEL
);
50 int wfx_cmd_send(struct wfx_dev
*wdev
, struct hif_msg
*request
, void *reply
,
51 size_t reply_len
, bool async
)
53 const char *mib_name
= "";
54 const char *mib_sep
= "";
55 int cmd
= request
->id
;
56 int vif
= request
->interface
;
59 WARN(wdev
->hif_cmd
.buf_recv
&& wdev
->hif_cmd
.async
, "API usage error");
61 // Do not wait for any reply if chip is frozen
62 if (wdev
->chip_frozen
)
65 if (cmd
!= HIF_REQ_ID_SL_EXCHANGE_PUB_KEYS
)
66 mutex_lock(&wdev
->hif_cmd
.key_renew_lock
);
68 mutex_lock(&wdev
->hif_cmd
.lock
);
69 WARN(wdev
->hif_cmd
.buf_send
, "data locking error");
71 // Note: call to complete() below has an implicit memory barrier that
72 // hopefully protect buf_send
73 wdev
->hif_cmd
.buf_send
= request
;
74 wdev
->hif_cmd
.buf_recv
= reply
;
75 wdev
->hif_cmd
.len_recv
= reply_len
;
76 wdev
->hif_cmd
.async
= async
;
77 complete(&wdev
->hif_cmd
.ready
);
79 wfx_bh_request_tx(wdev
);
81 // NOTE: no timeout is catched async is enabled
85 ret
= wait_for_completion_timeout(&wdev
->hif_cmd
.done
, 1 * HZ
);
87 dev_err(wdev
->dev
, "chip is abnormally long to answer\n");
88 reinit_completion(&wdev
->hif_cmd
.ready
);
89 ret
= wait_for_completion_timeout(&wdev
->hif_cmd
.done
, 3 * HZ
);
92 dev_err(wdev
->dev
, "chip did not answer\n");
93 wfx_pending_dump_old_frames(wdev
, 3000);
94 wdev
->chip_frozen
= 1;
95 reinit_completion(&wdev
->hif_cmd
.done
);
98 ret
= wdev
->hif_cmd
.ret
;
101 wdev
->hif_cmd
.buf_send
= NULL
;
102 mutex_unlock(&wdev
->hif_cmd
.lock
);
105 (cmd
== HIF_REQ_ID_READ_MIB
|| cmd
== HIF_REQ_ID_WRITE_MIB
)) {
106 mib_name
= get_mib_name(((u16
*) request
)[2]);
111 "WSM request %s%s%s (%#.2x) on vif %d returned error %d\n",
112 get_hif_name(cmd
), mib_sep
, mib_name
, cmd
, vif
, ret
);
115 "WSM request %s%s%s (%#.2x) on vif %d returned status %d\n",
116 get_hif_name(cmd
), mib_sep
, mib_name
, cmd
, vif
, ret
);
118 if (cmd
!= HIF_REQ_ID_SL_EXCHANGE_PUB_KEYS
)
119 mutex_unlock(&wdev
->hif_cmd
.key_renew_lock
);
123 // This function is special. After HIF_REQ_ID_SHUT_DOWN, chip won't reply to any
124 // request anymore. We need to slightly hack struct wfx_hif_cmd for that job. Be
125 // carefull to only call this funcion during device unregister.
126 int hif_shutdown(struct wfx_dev
*wdev
)
131 wfx_alloc_hif(0, &hif
);
132 wfx_fill_header(hif
, -1, HIF_REQ_ID_SHUT_DOWN
, 0);
133 ret
= wfx_cmd_send(wdev
, hif
, NULL
, 0, true);
134 // After this command, chip won't reply. Be sure to give enough time to
135 // bh to send buffer:
137 wdev
->hif_cmd
.buf_send
= NULL
;
138 if (wdev
->pdata
.gpio_wakeup
)
139 gpiod_set_value(wdev
->pdata
.gpio_wakeup
, 0);
141 control_reg_write(wdev
, 0);
142 mutex_unlock(&wdev
->hif_cmd
.lock
);
147 int hif_configuration(struct wfx_dev
*wdev
, const u8
*conf
, size_t len
)
150 size_t buf_len
= sizeof(struct hif_req_configuration
) + len
;
152 struct hif_req_configuration
*body
= wfx_alloc_hif(buf_len
, &hif
);
154 body
->length
= cpu_to_le16(len
);
155 memcpy(body
->pds_data
, conf
, len
);
156 wfx_fill_header(hif
, -1, HIF_REQ_ID_CONFIGURATION
, buf_len
);
157 ret
= wfx_cmd_send(wdev
, hif
, NULL
, 0, false);
162 int hif_reset(struct wfx_vif
*wvif
, bool reset_stat
)
166 struct hif_req_reset
*body
= wfx_alloc_hif(sizeof(*body
), &hif
);
168 body
->reset_flags
.reset_stat
= reset_stat
;
169 wfx_fill_header(hif
, wvif
->id
, HIF_REQ_ID_RESET
, sizeof(*body
));
170 ret
= wfx_cmd_send(wvif
->wdev
, hif
, NULL
, 0, false);
175 int hif_read_mib(struct wfx_dev
*wdev
, int vif_id
, u16 mib_id
, void *val
,
180 int buf_len
= sizeof(struct hif_cnf_read_mib
) + val_len
;
181 struct hif_req_read_mib
*body
= wfx_alloc_hif(sizeof(*body
), &hif
);
182 struct hif_cnf_read_mib
*reply
= kmalloc(buf_len
, GFP_KERNEL
);
184 body
->mib_id
= cpu_to_le16(mib_id
);
185 wfx_fill_header(hif
, vif_id
, HIF_REQ_ID_READ_MIB
, sizeof(*body
));
186 ret
= wfx_cmd_send(wdev
, hif
, reply
, buf_len
, false);
188 if (!ret
&& mib_id
!= reply
->mib_id
) {
190 "%s: confirmation mismatch request\n", __func__
);
195 "buffer is too small to receive %s (%zu < %d)\n",
196 get_mib_name(mib_id
), val_len
, reply
->length
);
198 memcpy(val
, &reply
->mib_data
, reply
->length
);
200 memset(val
, 0xFF, val_len
);
206 int hif_write_mib(struct wfx_dev
*wdev
, int vif_id
, u16 mib_id
, void *val
,
211 int buf_len
= sizeof(struct hif_req_write_mib
) + val_len
;
212 struct hif_req_write_mib
*body
= wfx_alloc_hif(buf_len
, &hif
);
214 body
->mib_id
= cpu_to_le16(mib_id
);
215 body
->length
= cpu_to_le16(val_len
);
216 memcpy(&body
->mib_data
, val
, val_len
);
217 wfx_fill_header(hif
, vif_id
, HIF_REQ_ID_WRITE_MIB
, buf_len
);
218 ret
= wfx_cmd_send(wdev
, hif
, NULL
, 0, false);
223 int hif_scan(struct wfx_vif
*wvif
, struct cfg80211_scan_request
*req
,
224 int chan_start_idx
, int chan_num
)
229 sizeof(struct hif_req_start_scan_alt
) + chan_num
* sizeof(u8
);
230 struct hif_req_start_scan_alt
*body
= wfx_alloc_hif(buf_len
, &hif
);
231 int tmo_chan_fg
, tmo_chan_bg
, tmo
;
233 WARN(chan_num
> HIF_API_MAX_NB_CHANNELS
, "invalid params");
234 WARN(req
->n_ssids
> HIF_API_MAX_NB_SSIDS
, "invalid params");
236 compiletime_assert(IEEE80211_MAX_SSID_LEN
== HIF_API_SSID_SIZE
,
237 "API inconsistency");
238 for (i
= 0; i
< req
->n_ssids
; i
++) {
239 memcpy(body
->ssid_def
[i
].ssid
, req
->ssids
[i
].ssid
,
240 IEEE80211_MAX_SSID_LEN
);
241 body
->ssid_def
[i
].ssid_length
=
242 cpu_to_le32(req
->ssids
[i
].ssid_len
);
244 body
->num_of_ssids
= HIF_API_MAX_NB_SSIDS
;
245 // Background scan is always a good idea
246 body
->scan_type
.type
= 1;
247 body
->scan_flags
.fbg
= 1;
248 body
->tx_power_level
=
249 cpu_to_le32(req
->channels
[chan_start_idx
]->max_power
);
250 body
->num_of_channels
= chan_num
;
251 for (i
= 0; i
< chan_num
; i
++)
252 body
->channel_list
[i
] =
253 req
->channels
[i
+ chan_start_idx
]->hw_value
;
255 body
->max_transmit_rate
= API_RATE_INDEX_G_6MBPS
;
257 body
->max_transmit_rate
= API_RATE_INDEX_B_1MBPS
;
258 if (req
->channels
[chan_start_idx
]->flags
& IEEE80211_CHAN_NO_IR
) {
259 body
->min_channel_time
= cpu_to_le32(50);
260 body
->max_channel_time
= cpu_to_le32(150);
262 body
->min_channel_time
= cpu_to_le32(10);
263 body
->max_channel_time
= cpu_to_le32(50);
264 body
->num_of_probe_requests
= 2;
265 body
->probe_delay
= 100;
267 tmo_chan_bg
= le32_to_cpu(body
->max_channel_time
) * USEC_PER_TU
;
268 tmo_chan_fg
= 512 * USEC_PER_TU
+ body
->probe_delay
;
269 tmo_chan_fg
*= body
->num_of_probe_requests
;
270 tmo
= chan_num
* max(tmo_chan_bg
, tmo_chan_fg
);
272 wfx_fill_header(hif
, wvif
->id
, HIF_REQ_ID_START_SCAN
, buf_len
);
273 ret
= wfx_cmd_send(wvif
->wdev
, hif
, NULL
, 0, false);
275 return ret
? ret
: usecs_to_jiffies(tmo
);
278 int hif_stop_scan(struct wfx_vif
*wvif
)
282 // body associated to HIF_REQ_ID_STOP_SCAN is empty
283 wfx_alloc_hif(0, &hif
);
285 wfx_fill_header(hif
, wvif
->id
, HIF_REQ_ID_STOP_SCAN
, 0);
286 ret
= wfx_cmd_send(wvif
->wdev
, hif
, NULL
, 0, false);
291 int hif_join(struct wfx_vif
*wvif
, const struct hif_req_join
*arg
)
295 struct hif_req_join
*body
= wfx_alloc_hif(sizeof(*body
), &hif
);
297 memcpy(body
, arg
, sizeof(struct hif_req_join
));
298 cpu_to_le16s(&body
->channel_number
);
299 cpu_to_le16s(&body
->atim_window
);
300 cpu_to_le32s(&body
->ssid_length
);
301 cpu_to_le32s(&body
->beacon_interval
);
302 cpu_to_le32s(&body
->basic_rate_set
);
303 wfx_fill_header(hif
, wvif
->id
, HIF_REQ_ID_JOIN
, sizeof(*body
));
304 ret
= wfx_cmd_send(wvif
->wdev
, hif
, NULL
, 0, false);
309 int hif_set_bss_params(struct wfx_vif
*wvif
,
310 const struct hif_req_set_bss_params
*arg
)
314 struct hif_req_set_bss_params
*body
= wfx_alloc_hif(sizeof(*body
),
317 memcpy(body
, arg
, sizeof(*body
));
318 cpu_to_le16s(&body
->aid
);
319 cpu_to_le32s(&body
->operational_rate_set
);
320 wfx_fill_header(hif
, wvif
->id
, HIF_REQ_ID_SET_BSS_PARAMS
,
322 ret
= wfx_cmd_send(wvif
->wdev
, hif
, NULL
, 0, false);
327 int hif_add_key(struct wfx_dev
*wdev
, const struct hif_req_add_key
*arg
)
331 // FIXME: only send necessary bits
332 struct hif_req_add_key
*body
= wfx_alloc_hif(sizeof(*body
), &hif
);
334 // FIXME: swap bytes as necessary in body
335 memcpy(body
, arg
, sizeof(*body
));
336 if (wfx_api_older_than(wdev
, 1, 5))
337 // Legacy firmwares expect that add_key to be sent on right
339 wfx_fill_header(hif
, arg
->int_id
, HIF_REQ_ID_ADD_KEY
,
342 wfx_fill_header(hif
, -1, HIF_REQ_ID_ADD_KEY
, sizeof(*body
));
343 ret
= wfx_cmd_send(wdev
, hif
, NULL
, 0, false);
348 int hif_remove_key(struct wfx_dev
*wdev
, int idx
)
352 struct hif_req_remove_key
*body
= wfx_alloc_hif(sizeof(*body
), &hif
);
354 body
->entry_index
= idx
;
355 wfx_fill_header(hif
, -1, HIF_REQ_ID_REMOVE_KEY
, sizeof(*body
));
356 ret
= wfx_cmd_send(wdev
, hif
, NULL
, 0, false);
361 int hif_set_edca_queue_params(struct wfx_vif
*wvif
, u16 queue
,
362 const struct ieee80211_tx_queue_params
*arg
)
366 struct hif_req_edca_queue_params
*body
= wfx_alloc_hif(sizeof(*body
),
369 WARN_ON(arg
->aifs
> 255);
370 body
->aifsn
= arg
->aifs
;
371 body
->cw_min
= cpu_to_le16(arg
->cw_min
);
372 body
->cw_max
= cpu_to_le16(arg
->cw_max
);
373 body
->tx_op_limit
= cpu_to_le16(arg
->txop
* USEC_PER_TXOP
);
374 body
->queue_id
= 3 - queue
;
375 // API 2.0 has changed queue IDs values
376 if (wfx_api_older_than(wvif
->wdev
, 2, 0) && queue
== IEEE80211_AC_BE
)
377 body
->queue_id
= HIF_QUEUE_ID_BACKGROUND
;
378 if (wfx_api_older_than(wvif
->wdev
, 2, 0) && queue
== IEEE80211_AC_BK
)
379 body
->queue_id
= HIF_QUEUE_ID_BESTEFFORT
;
380 wfx_fill_header(hif
, wvif
->id
, HIF_REQ_ID_EDCA_QUEUE_PARAMS
,
382 ret
= wfx_cmd_send(wvif
->wdev
, hif
, NULL
, 0, false);
387 int hif_set_pm(struct wfx_vif
*wvif
, bool ps
, int dynamic_ps_timeout
)
391 struct hif_req_set_pm_mode
*body
= wfx_alloc_hif(sizeof(*body
), &hif
);
394 body
->pm_mode
.enter_psm
= 1;
395 // Firmware does not support more than 128ms
396 body
->fast_psm_idle_period
= min(dynamic_ps_timeout
* 2, 255);
397 if (body
->fast_psm_idle_period
)
398 body
->pm_mode
.fast_psm
= 1;
400 wfx_fill_header(hif
, wvif
->id
, HIF_REQ_ID_SET_PM_MODE
, sizeof(*body
));
401 ret
= wfx_cmd_send(wvif
->wdev
, hif
, NULL
, 0, false);
406 int hif_start(struct wfx_vif
*wvif
, const struct hif_req_start
*arg
)
410 struct hif_req_start
*body
= wfx_alloc_hif(sizeof(*body
), &hif
);
412 memcpy(body
, arg
, sizeof(*body
));
413 cpu_to_le16s(&body
->channel_number
);
414 cpu_to_le32s(&body
->beacon_interval
);
415 cpu_to_le32s(&body
->basic_rate_set
);
416 wfx_fill_header(hif
, wvif
->id
, HIF_REQ_ID_START
, sizeof(*body
));
417 ret
= wfx_cmd_send(wvif
->wdev
, hif
, NULL
, 0, false);
422 int hif_beacon_transmit(struct wfx_vif
*wvif
, bool enable_beaconing
)
426 struct hif_req_beacon_transmit
*body
= wfx_alloc_hif(sizeof(*body
),
429 body
->enable_beaconing
= enable_beaconing
? 1 : 0;
430 wfx_fill_header(hif
, wvif
->id
, HIF_REQ_ID_BEACON_TRANSMIT
,
432 ret
= wfx_cmd_send(wvif
->wdev
, hif
, NULL
, 0, false);
437 int hif_map_link(struct wfx_vif
*wvif
, u8
*mac_addr
, int flags
, int sta_id
)
441 struct hif_req_map_link
*body
= wfx_alloc_hif(sizeof(*body
), &hif
);
444 ether_addr_copy(body
->mac_addr
, mac_addr
);
445 body
->map_link_flags
= *(struct hif_map_link_flags
*) &flags
;
446 body
->peer_sta_id
= sta_id
;
447 wfx_fill_header(hif
, wvif
->id
, HIF_REQ_ID_MAP_LINK
, sizeof(*body
));
448 ret
= wfx_cmd_send(wvif
->wdev
, hif
, NULL
, 0, false);
453 int hif_update_ie(struct wfx_vif
*wvif
, const struct hif_ie_flags
*target_frame
,
454 const u8
*ies
, size_t ies_len
)
458 int buf_len
= sizeof(struct hif_req_update_ie
) + ies_len
;
459 struct hif_req_update_ie
*body
= wfx_alloc_hif(buf_len
, &hif
);
461 memcpy(&body
->ie_flags
, target_frame
, sizeof(struct hif_ie_flags
));
462 body
->num_ies
= cpu_to_le16(1);
463 memcpy(body
->ie
, ies
, ies_len
);
464 wfx_fill_header(hif
, wvif
->id
, HIF_REQ_ID_UPDATE_IE
, buf_len
);
465 ret
= wfx_cmd_send(wvif
->wdev
, hif
, NULL
, 0, false);
470 int hif_sl_send_pub_keys(struct wfx_dev
*wdev
, const uint8_t *pubkey
,
471 const uint8_t *pubkey_hmac
)
475 struct hif_req_sl_exchange_pub_keys
*body
= wfx_alloc_hif(sizeof(*body
),
478 body
->algorithm
= HIF_SL_CURVE25519
;
479 memcpy(body
->host_pub_key
, pubkey
, sizeof(body
->host_pub_key
));
480 memcpy(body
->host_pub_key_mac
, pubkey_hmac
,
481 sizeof(body
->host_pub_key_mac
));
482 wfx_fill_header(hif
, -1, HIF_REQ_ID_SL_EXCHANGE_PUB_KEYS
,
484 ret
= wfx_cmd_send(wdev
, hif
, NULL
, 0, false);
486 // Compatibility with legacy secure link
487 if (ret
== SL_PUB_KEY_EXCHANGE_STATUS_SUCCESS
)
492 int hif_sl_config(struct wfx_dev
*wdev
, const unsigned long *bitmap
)
496 struct hif_req_sl_configure
*body
= wfx_alloc_hif(sizeof(*body
), &hif
);
498 memcpy(body
->encr_bmp
, bitmap
, sizeof(body
->encr_bmp
));
499 wfx_fill_header(hif
, -1, HIF_REQ_ID_SL_CONFIGURE
, sizeof(*body
));
500 ret
= wfx_cmd_send(wdev
, hif
, NULL
, 0, false);
505 int hif_sl_set_mac_key(struct wfx_dev
*wdev
, const u8
*slk_key
,
510 struct hif_req_set_sl_mac_key
*body
= wfx_alloc_hif(sizeof(*body
),
513 memcpy(body
->key_value
, slk_key
, sizeof(body
->key_value
));
514 body
->otp_or_ram
= destination
;
515 wfx_fill_header(hif
, -1, HIF_REQ_ID_SET_SL_MAC_KEY
, sizeof(*body
));
516 ret
= wfx_cmd_send(wdev
, hif
, NULL
, 0, false);
518 // Compatibility with legacy secure link
519 if (ret
== SL_MAC_KEY_STATUS_SUCCESS
)