xref: /linux/drivers/net/wireless/intel/iwlwifi/mei/main.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2021-2024 Intel Corporation
4  * Copyright (C) 2026 Intel Corporation
5  */
6 
7 #include <linux/etherdevice.h>
8 #include <linux/netdevice.h>
9 #include <linux/ieee80211.h>
10 #include <linux/rtnetlink.h>
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/mei_cl_bus.h>
14 #include <linux/rcupdate.h>
15 #include <linux/debugfs.h>
16 #include <linux/skbuff.h>
17 #include <linux/wait.h>
18 #include <linux/slab.h>
19 #include <linux/mm.h>
20 
21 #include <net/cfg80211.h>
22 
23 #include "internal.h"
24 #include "iwl-mei.h"
25 #include "trace.h"
26 #include "trace-data.h"
27 #include "sap.h"
28 
29 MODULE_DESCRIPTION("The Intel(R) wireless / CSME firmware interface");
30 MODULE_LICENSE("GPL");
31 
32 #define MEI_WLAN_UUID UUID_LE(0x13280904, 0x7792, 0x4fcb, \
33 			      0xa1, 0xaa, 0x5e, 0x70, 0xcb, 0xb1, 0xe8, 0x65)
34 
35 /* After CSME takes ownership, it won't release it for 60 seconds to avoid
36  * frequent ownership transitions.
37  */
38 #define MEI_OWNERSHIP_RETAKE_TIMEOUT_MS	msecs_to_jiffies(60000)
39 
40 /*
41  * Since iwlwifi calls iwlmei without any context, hold a pointer to the
42  * mei_cl_device structure here.
43  * Define a mutex that will synchronize all the flows between iwlwifi and
44  * iwlmei.
45  * Note that iwlmei can't have several instances, so it ok to have static
46  * variables here.
47  */
48 static struct mei_cl_device *iwl_mei_global_cldev;
49 static DEFINE_MUTEX(iwl_mei_mutex);
50 static unsigned long iwl_mei_status;
51 
52 enum iwl_mei_status_bits {
53 	IWL_MEI_STATUS_SAP_CONNECTED,
54 };
55 
iwl_mei_is_connected(void)56 bool iwl_mei_is_connected(void)
57 {
58 	return test_bit(IWL_MEI_STATUS_SAP_CONNECTED, &iwl_mei_status);
59 }
60 EXPORT_SYMBOL_GPL(iwl_mei_is_connected);
61 
62 #define SAP_CONTROL_BLOCK_ID 0x21504153 /* SAP! in ASCII */
63 
64 struct iwl_sap_q_ctrl_blk {
65 	__le32 wr_ptr;
66 	__le32 rd_ptr;
67 	__le32 size;
68 };
69 
70 enum iwl_sap_q_idx {
71 	SAP_QUEUE_IDX_NOTIF = 0,
72 	SAP_QUEUE_IDX_DATA,
73 	SAP_QUEUE_IDX_MAX,
74 };
75 
76 struct iwl_sap_dir {
77 	__le32 reserved;
78 	struct iwl_sap_q_ctrl_blk q_ctrl_blk[SAP_QUEUE_IDX_MAX];
79 };
80 
81 enum iwl_sap_dir_idx {
82 	SAP_DIRECTION_HOST_TO_ME = 0,
83 	SAP_DIRECTION_ME_TO_HOST,
84 	SAP_DIRECTION_MAX,
85 };
86 
87 struct iwl_sap_shared_mem_ctrl_blk {
88 	__le32 sap_id;
89 	__le32 size;
90 	struct iwl_sap_dir dir[SAP_DIRECTION_MAX];
91 };
92 
93 /*
94  * The shared area has the following layout:
95  *
96  * +-----------------------------------+
97  * |struct iwl_sap_shared_mem_ctrl_blk |
98  * +-----------------------------------+
99  * |Host -> ME data queue              |
100  * +-----------------------------------+
101  * |Host -> ME notif queue             |
102  * +-----------------------------------+
103  * |ME -> Host data queue              |
104  * +-----------------------------------+
105  * |ME -> host notif queue             |
106  * +-----------------------------------+
107  * |SAP control block id (SAP!)        |
108  * +-----------------------------------+
109  */
110 
111 #define SAP_H2M_DATA_Q_SZ	48256
112 #define SAP_M2H_DATA_Q_SZ	24128
113 #define SAP_H2M_NOTIF_Q_SZ_VER3	2240
114 #define SAP_H2M_NOTIF_Q_SZ_VER4	32768
115 #define SAP_M2H_NOTIF_Q_SZ	62720
116 
117 #define _IWL_MEI_SAP_SHARED_MEM_SZ_VER3 \
118 	(sizeof(struct iwl_sap_shared_mem_ctrl_blk) + \
119 	 SAP_H2M_DATA_Q_SZ + SAP_H2M_NOTIF_Q_SZ_VER3 + \
120 	 SAP_M2H_DATA_Q_SZ + SAP_M2H_NOTIF_Q_SZ + 4)
121 
122 #define _IWL_MEI_SAP_SHARED_MEM_SZ_VER4 \
123 	(sizeof(struct iwl_sap_shared_mem_ctrl_blk) + \
124 	 SAP_H2M_DATA_Q_SZ + SAP_H2M_NOTIF_Q_SZ_VER4 + \
125 	 SAP_M2H_DATA_Q_SZ + SAP_M2H_NOTIF_Q_SZ + 4)
126 
127 struct iwl_mei_shared_mem_ptrs {
128 	struct iwl_sap_shared_mem_ctrl_blk *ctrl;
129 	void *q_head[SAP_DIRECTION_MAX][SAP_QUEUE_IDX_MAX];
130 	size_t q_size[SAP_DIRECTION_MAX][SAP_QUEUE_IDX_MAX];
131 };
132 
133 struct iwl_mei_filters {
134 	struct rcu_head rcu_head;
135 	struct iwl_sap_oob_filters filters;
136 };
137 
138 /**
139  * struct iwl_mei - holds the private date for iwl_mei
140  *
141  * @get_nvm_wq: the wait queue for the get_nvm flow
142  * @send_csa_msg_wk: used to defer the transmission of the CHECK_SHARED_AREA
143  *	message. Used so that we can send CHECK_SHARED_AREA from atomic
144  *	contexts.
145  * @get_ownership_wq: the wait queue for the get_ownership_flow
146  * @shared_mem: the memory that is shared between CSME and the host
147  * @cldev: the pointer to the MEI client device
148  * @nvm: the data returned by the CSME for the NVM
149  * @filters: the filters sent by CSME
150  * @got_ownership: true if we own the device
151  * @amt_enabled: true if CSME has wireless enabled
152  * @csa_throttled: when true, we can't send CHECK_SHARED_AREA over the MEI
153  *	bus, but rather need to wait until send_csa_msg_wk runs
154  * @csme_taking_ownership: true when CSME is taking ownership. Used to remember
155  *	to send CSME_OWNERSHIP_CONFIRMED when the driver completes its down
156  *	flow.
157  * @link_prot_state: true when we are in link protection PASSIVE
158  * @device_down: true if the device is down. Used to remember to send
159  *	CSME_OWNERSHIP_CONFIRMED when the driver is already down.
160  * @csa_throttle_end_wk: used when &csa_throttled is true
161  * @pldr_wq: the wait queue for PLDR flow
162  * @pldr_active: PLDR flow is in progress
163  * @data_q_lock: protects the access to the data queues which are
164  *	accessed without the mutex.
165  * @netdev_work: used to defer registering and unregistering of the netdev to
166  *	avoid taking the rtnl lock in the SAP messages handlers.
167  * @ownership_dwork: used to re-ask for NIC ownership after ownership was taken
168  *	by CSME or when a previous ownership request failed.
169  * @sap_seq_no: the sequence number for the SAP messages
170  * @seq_no: the sequence number for the SAP messages
171  * @dbgfs_dir: the debugfs dir entry
172  */
173 struct iwl_mei {
174 	wait_queue_head_t get_nvm_wq;
175 	struct work_struct send_csa_msg_wk;
176 	wait_queue_head_t get_ownership_wq;
177 	struct iwl_mei_shared_mem_ptrs shared_mem;
178 	struct mei_cl_device *cldev;
179 	struct iwl_mei_nvm *nvm;
180 	struct iwl_mei_filters __rcu *filters;
181 	bool got_ownership;
182 	bool amt_enabled;
183 	bool csa_throttled;
184 	bool csme_taking_ownership;
185 	bool link_prot_state;
186 	bool device_down;
187 	struct delayed_work csa_throttle_end_wk;
188 	wait_queue_head_t pldr_wq;
189 	bool pldr_active;
190 	spinlock_t data_q_lock;
191 	struct work_struct netdev_work;
192 	struct delayed_work ownership_dwork;
193 
194 	atomic_t sap_seq_no;
195 	atomic_t seq_no;
196 
197 	struct dentry *dbgfs_dir;
198 };
199 
200 /**
201  * struct iwl_mei_cache - cache for the parameters from iwlwifi
202  * @ops: Callbacks to iwlwifi.
203  * @netdev: The netdev that will be used to transmit / receive packets.
204  * @conn_info: The connection info message triggered by iwlwifi's association.
205  * @power_limit: pointer to an array of 10 elements (le16) represents the power
206  *	restrictions per chain.
207  * @rf_kill: rf kill state.
208  * @mcc: MCC info
209  * @mac_address: interface MAC address.
210  * @nvm_address: NVM MAC address.
211  * @priv: A pointer to iwlwifi.
212  * @sap_version: The SAP version to use. enum iwl_mei_sap_version.
213  *
214  * This used to cache the configurations coming from iwlwifi's way. The data
215  * is cached here so that we can buffer the configuration even if we don't have
216  * a bind from the mei bus and hence, on iwl_mei structure.
217  */
218 struct iwl_mei_cache {
219 	const struct iwl_mei_ops *ops;
220 	struct net_device __rcu *netdev;
221 	const struct iwl_sap_notif_connection_info *conn_info;
222 	const __le16 *power_limit;
223 	u32 rf_kill;
224 	u16 mcc;
225 	u8 mac_address[6];
226 	u8 nvm_address[6];
227 	enum iwl_mei_sap_version sap_version;
228 	void *priv;
229 };
230 
231 static struct iwl_mei_cache iwl_mei_cache = {
232 	.rf_kill = SAP_HW_RFKILL_DEASSERTED | SAP_SW_RFKILL_DEASSERTED
233 };
234 
iwl_mei_free_shared_mem(struct mei_cl_device * cldev)235 static void iwl_mei_free_shared_mem(struct mei_cl_device *cldev)
236 {
237 	struct iwl_mei *mei = mei_cldev_get_drvdata(cldev);
238 
239 	if (mei_cldev_dma_unmap(cldev))
240 		dev_err(&cldev->dev, "Couldn't unmap the shared mem properly\n");
241 	memset(&mei->shared_mem, 0, sizeof(mei->shared_mem));
242 }
243 
244 #define HBM_DMA_BUF_ID_WLAN 1
245 
iwl_mei_alloc_mem_for_version(struct mei_cl_device * cldev,enum iwl_mei_sap_version version)246 static int iwl_mei_alloc_mem_for_version(struct mei_cl_device *cldev,
247 					 enum iwl_mei_sap_version version)
248 {
249 	struct iwl_mei *mei = mei_cldev_get_drvdata(cldev);
250 	struct iwl_mei_shared_mem_ptrs *mem = &mei->shared_mem;
251 	u32 mem_size = roundup(version == IWL_MEI_SAP_VERSION_4 ?
252 			       _IWL_MEI_SAP_SHARED_MEM_SZ_VER4 :
253 			       _IWL_MEI_SAP_SHARED_MEM_SZ_VER3, PAGE_SIZE);
254 
255 	iwl_mei_cache.sap_version = version;
256 	mem->ctrl = mei_cldev_dma_map(cldev, HBM_DMA_BUF_ID_WLAN, mem_size);
257 	if (IS_ERR(mem->ctrl)) {
258 		int ret = PTR_ERR(mem->ctrl);
259 
260 		mem->ctrl = NULL;
261 
262 		return ret;
263 	}
264 
265 	memset(mem->ctrl, 0, mem_size);
266 
267 	return 0;
268 }
269 
iwl_mei_alloc_shared_mem(struct mei_cl_device * cldev)270 static int iwl_mei_alloc_shared_mem(struct mei_cl_device *cldev)
271 {
272 	int ret;
273 
274 	/*
275 	 * SAP version 4 uses a larger Host to MEI notif queue.
276 	 * Since it is unknown at this stage which SAP version is used by the
277 	 * CSME firmware on this platform, try to allocate the version 4 first.
278 	 * If the CSME firmware uses version 3, this allocation is expected to
279 	 * fail because the CSME firmware allocated less memory for our driver.
280 	 */
281 	ret = iwl_mei_alloc_mem_for_version(cldev, IWL_MEI_SAP_VERSION_4);
282 	if (ret)
283 		ret = iwl_mei_alloc_mem_for_version(cldev,
284 						    IWL_MEI_SAP_VERSION_3);
285 
286 	return ret;
287 }
288 
iwl_mei_init_shared_mem(struct iwl_mei * mei)289 static void iwl_mei_init_shared_mem(struct iwl_mei *mei)
290 {
291 	struct iwl_mei_shared_mem_ptrs *mem = &mei->shared_mem;
292 	struct iwl_sap_dir *h2m;
293 	struct iwl_sap_dir *m2h;
294 	int dir, queue;
295 	u8 *q_head;
296 
297 	mem->ctrl->sap_id = cpu_to_le32(SAP_CONTROL_BLOCK_ID);
298 
299 	mem->ctrl->size = cpu_to_le32(sizeof(*mem->ctrl));
300 
301 	h2m = &mem->ctrl->dir[SAP_DIRECTION_HOST_TO_ME];
302 	m2h = &mem->ctrl->dir[SAP_DIRECTION_ME_TO_HOST];
303 
304 	h2m->q_ctrl_blk[SAP_QUEUE_IDX_DATA].size =
305 		cpu_to_le32(SAP_H2M_DATA_Q_SZ);
306 	h2m->q_ctrl_blk[SAP_QUEUE_IDX_NOTIF].size =
307 		iwl_mei_cache.sap_version == IWL_MEI_SAP_VERSION_3 ?
308 		cpu_to_le32(SAP_H2M_NOTIF_Q_SZ_VER3) :
309 		cpu_to_le32(SAP_H2M_NOTIF_Q_SZ_VER4);
310 	m2h->q_ctrl_blk[SAP_QUEUE_IDX_DATA].size =
311 		cpu_to_le32(SAP_M2H_DATA_Q_SZ);
312 	m2h->q_ctrl_blk[SAP_QUEUE_IDX_NOTIF].size =
313 		cpu_to_le32(SAP_M2H_NOTIF_Q_SZ);
314 
315 	/* q_head points to the start of the first queue */
316 	q_head = (void *)(mem->ctrl + 1);
317 
318 	/* Initialize the queue heads */
319 	for (dir = 0; dir < SAP_DIRECTION_MAX; dir++) {
320 		for (queue = 0; queue < SAP_QUEUE_IDX_MAX; queue++) {
321 			mem->q_head[dir][queue] = q_head;
322 			q_head +=
323 				le32_to_cpu(mem->ctrl->dir[dir].q_ctrl_blk[queue].size);
324 			mem->q_size[dir][queue] =
325 				le32_to_cpu(mem->ctrl->dir[dir].q_ctrl_blk[queue].size);
326 		}
327 	}
328 
329 	*(__le32 *)q_head = cpu_to_le32(SAP_CONTROL_BLOCK_ID);
330 }
331 
iwl_mei_write_cyclic_buf(struct mei_cl_device * cldev,struct iwl_sap_q_ctrl_blk * notif_q,u8 * q_head,const struct iwl_sap_hdr * hdr,u32 q_sz)332 static ssize_t iwl_mei_write_cyclic_buf(struct mei_cl_device *cldev,
333 					struct iwl_sap_q_ctrl_blk *notif_q,
334 					u8 *q_head,
335 					const struct iwl_sap_hdr *hdr,
336 					u32 q_sz)
337 {
338 	u32 rd = le32_to_cpu(READ_ONCE(notif_q->rd_ptr));
339 	u32 wr = le32_to_cpu(READ_ONCE(notif_q->wr_ptr));
340 	size_t room_in_buf;
341 	size_t tx_sz = sizeof(*hdr) + le16_to_cpu(hdr->len);
342 
343 	if (rd > q_sz || wr > q_sz) {
344 		dev_err(&cldev->dev,
345 			"Pointers are past the end of the buffer\n");
346 		return -EINVAL;
347 	}
348 
349 	room_in_buf = wr >= rd ? q_sz - wr + rd : rd - wr;
350 
351 	/* we don't have enough room for the data to write */
352 	if (room_in_buf < tx_sz) {
353 		dev_err(&cldev->dev,
354 			"Not enough room in the buffer\n");
355 		return -ENOSPC;
356 	}
357 
358 	if (wr + tx_sz <= q_sz) {
359 		memcpy(q_head + wr, hdr, tx_sz);
360 	} else {
361 		memcpy(q_head + wr, hdr, q_sz - wr);
362 		memcpy(q_head, (const u8 *)hdr + q_sz - wr, tx_sz - (q_sz - wr));
363 	}
364 
365 	WRITE_ONCE(notif_q->wr_ptr, cpu_to_le32((wr + tx_sz) % q_sz));
366 	return 0;
367 }
368 
iwl_mei_host_to_me_data_pending(const struct iwl_mei * mei)369 static bool iwl_mei_host_to_me_data_pending(const struct iwl_mei *mei)
370 {
371 	struct iwl_sap_q_ctrl_blk *notif_q;
372 	struct iwl_sap_dir *dir;
373 
374 	dir = &mei->shared_mem.ctrl->dir[SAP_DIRECTION_HOST_TO_ME];
375 	notif_q = &dir->q_ctrl_blk[SAP_QUEUE_IDX_DATA];
376 
377 	if (READ_ONCE(notif_q->wr_ptr) != READ_ONCE(notif_q->rd_ptr))
378 		return true;
379 
380 	notif_q = &dir->q_ctrl_blk[SAP_QUEUE_IDX_NOTIF];
381 	return READ_ONCE(notif_q->wr_ptr) != READ_ONCE(notif_q->rd_ptr);
382 }
383 
iwl_mei_send_check_shared_area(struct mei_cl_device * cldev)384 static int iwl_mei_send_check_shared_area(struct mei_cl_device *cldev)
385 {
386 	struct iwl_mei *mei = mei_cldev_get_drvdata(cldev);
387 	struct iwl_sap_me_msg_start msg = {
388 		.hdr.type = cpu_to_le32(SAP_ME_MSG_CHECK_SHARED_AREA),
389 		.hdr.seq_num = cpu_to_le32(atomic_inc_return(&mei->seq_no)),
390 	};
391 	int ret;
392 
393 	lockdep_assert_held(&iwl_mei_mutex);
394 
395 	if (mei->csa_throttled)
396 		return 0;
397 
398 	trace_iwlmei_me_msg(&msg.hdr, true);
399 	ret = mei_cldev_send(cldev, (void *)&msg, sizeof(msg));
400 	if (ret != sizeof(msg)) {
401 		dev_err(&cldev->dev,
402 			"failed to send the SAP_ME_MSG_CHECK_SHARED_AREA message %d\n",
403 			ret);
404 		return ret;
405 	}
406 
407 	mei->csa_throttled = true;
408 
409 	schedule_delayed_work(&mei->csa_throttle_end_wk,
410 			      msecs_to_jiffies(100));
411 
412 	return 0;
413 }
414 
iwl_mei_csa_throttle_end_wk(struct work_struct * wk)415 static void iwl_mei_csa_throttle_end_wk(struct work_struct *wk)
416 {
417 	struct iwl_mei *mei =
418 		container_of(wk, struct iwl_mei, csa_throttle_end_wk.work);
419 
420 	mutex_lock(&iwl_mei_mutex);
421 
422 	mei->csa_throttled = false;
423 
424 	if (iwl_mei_host_to_me_data_pending(mei))
425 		iwl_mei_send_check_shared_area(mei->cldev);
426 
427 	mutex_unlock(&iwl_mei_mutex);
428 }
429 
iwl_mei_send_sap_msg_payload(struct mei_cl_device * cldev,struct iwl_sap_hdr * hdr)430 static int iwl_mei_send_sap_msg_payload(struct mei_cl_device *cldev,
431 					struct iwl_sap_hdr *hdr)
432 {
433 	struct iwl_mei *mei = mei_cldev_get_drvdata(cldev);
434 	struct iwl_sap_q_ctrl_blk *notif_q;
435 	struct iwl_sap_dir *dir;
436 	void *q_head;
437 	u32 q_sz;
438 	int ret;
439 
440 	lockdep_assert_held(&iwl_mei_mutex);
441 
442 	if (!mei->shared_mem.ctrl) {
443 		dev_err(&cldev->dev,
444 			"No shared memory, can't send any SAP message\n");
445 		return -EINVAL;
446 	}
447 
448 	if (!iwl_mei_is_connected()) {
449 		dev_err(&cldev->dev,
450 			"Can't send a SAP message if we're not connected\n");
451 		return -ENODEV;
452 	}
453 
454 	hdr->seq_num = cpu_to_le32(atomic_inc_return(&mei->sap_seq_no));
455 	dev_dbg(&cldev->dev, "Sending %d\n", hdr->type);
456 
457 	dir = &mei->shared_mem.ctrl->dir[SAP_DIRECTION_HOST_TO_ME];
458 	notif_q = &dir->q_ctrl_blk[SAP_QUEUE_IDX_NOTIF];
459 	q_head = mei->shared_mem.q_head[SAP_DIRECTION_HOST_TO_ME][SAP_QUEUE_IDX_NOTIF];
460 	q_sz = mei->shared_mem.q_size[SAP_DIRECTION_HOST_TO_ME][SAP_QUEUE_IDX_NOTIF];
461 	ret = iwl_mei_write_cyclic_buf(cldev, notif_q, q_head, hdr, q_sz);
462 
463 	if (ret < 0)
464 		return ret;
465 
466 	trace_iwlmei_sap_cmd(hdr, true);
467 
468 	return iwl_mei_send_check_shared_area(cldev);
469 }
470 
iwl_mei_add_data_to_ring(struct sk_buff * skb,bool cb_tx)471 void iwl_mei_add_data_to_ring(struct sk_buff *skb, bool cb_tx)
472 {
473 	struct iwl_sap_q_ctrl_blk *notif_q;
474 	struct iwl_sap_dir *dir;
475 	struct iwl_mei *mei;
476 	size_t room_in_buf;
477 	size_t tx_sz;
478 	size_t hdr_sz;
479 	u32 q_sz;
480 	u32 rd;
481 	u32 wr;
482 	u8 *q_head;
483 
484 	if (!iwl_mei_global_cldev)
485 		return;
486 
487 	mei = mei_cldev_get_drvdata(iwl_mei_global_cldev);
488 
489 	/*
490 	 * We access this path for Rx packets (the more common case)
491 	 * and from Tx path when we send DHCP packets, the latter is
492 	 * very unlikely.
493 	 * Take the lock already here to make sure we see that remove()
494 	 * might have cleared the IWL_MEI_STATUS_SAP_CONNECTED bit.
495 	 */
496 	spin_lock_bh(&mei->data_q_lock);
497 
498 	if (!iwl_mei_is_connected()) {
499 		spin_unlock_bh(&mei->data_q_lock);
500 		return;
501 	}
502 
503 	/*
504 	 * We are in a RCU critical section and the remove from the CSME bus
505 	 * which would free this memory waits for the readers to complete (this
506 	 * is done in netdev_rx_handler_unregister).
507 	 */
508 	dir = &mei->shared_mem.ctrl->dir[SAP_DIRECTION_HOST_TO_ME];
509 	notif_q = &dir->q_ctrl_blk[SAP_QUEUE_IDX_DATA];
510 	q_head = mei->shared_mem.q_head[SAP_DIRECTION_HOST_TO_ME][SAP_QUEUE_IDX_DATA];
511 	q_sz = mei->shared_mem.q_size[SAP_DIRECTION_HOST_TO_ME][SAP_QUEUE_IDX_DATA];
512 
513 	rd = le32_to_cpu(READ_ONCE(notif_q->rd_ptr));
514 	wr = le32_to_cpu(READ_ONCE(notif_q->wr_ptr));
515 	hdr_sz = cb_tx ? sizeof(struct iwl_sap_cb_data) :
516 			 sizeof(struct iwl_sap_hdr);
517 	tx_sz = skb->len + hdr_sz;
518 
519 	if (rd > q_sz || wr > q_sz) {
520 		dev_err(&mei->cldev->dev,
521 			"can't write the data: pointers are past the end of the buffer\n");
522 		goto out;
523 	}
524 
525 	room_in_buf = wr >= rd ? q_sz - wr + rd : rd - wr;
526 
527 	/* we don't have enough room for the data to write */
528 	if (room_in_buf < tx_sz) {
529 		dev_err(&mei->cldev->dev,
530 			"Not enough room in the buffer for this data\n");
531 		goto out;
532 	}
533 
534 	if (skb_headroom(skb) < hdr_sz) {
535 		dev_err(&mei->cldev->dev,
536 			"Not enough headroom in the skb to write the SAP header\n");
537 		goto out;
538 	}
539 
540 	if (cb_tx) {
541 		struct iwl_sap_cb_data *cb_hdr = skb_push(skb, sizeof(*cb_hdr));
542 
543 		memset(cb_hdr, 0, sizeof(*cb_hdr));
544 		cb_hdr->hdr.type = cpu_to_le16(SAP_MSG_CB_DATA_PACKET);
545 		cb_hdr->hdr.len = cpu_to_le16(skb->len - sizeof(cb_hdr->hdr));
546 		cb_hdr->hdr.seq_num = cpu_to_le32(atomic_inc_return(&mei->sap_seq_no));
547 		cb_hdr->to_me_filt_status = cpu_to_le32(BIT(CB_TX_DHCP_FILT_IDX));
548 		cb_hdr->data_len = cpu_to_le32(skb->len - sizeof(*cb_hdr));
549 		trace_iwlmei_sap_data(skb, IWL_SAP_TX_DHCP);
550 	} else {
551 		struct iwl_sap_hdr *hdr = skb_push(skb, sizeof(*hdr));
552 
553 		hdr->type = cpu_to_le16(SAP_MSG_DATA_PACKET);
554 		hdr->len = cpu_to_le16(skb->len - sizeof(*hdr));
555 		hdr->seq_num = cpu_to_le32(atomic_inc_return(&mei->sap_seq_no));
556 		trace_iwlmei_sap_data(skb, IWL_SAP_TX_DATA_FROM_AIR);
557 	}
558 
559 	if (wr + tx_sz <= q_sz) {
560 		skb_copy_bits(skb, 0, q_head + wr, tx_sz);
561 	} else {
562 		skb_copy_bits(skb, 0, q_head + wr, q_sz - wr);
563 		skb_copy_bits(skb, q_sz - wr, q_head, tx_sz - (q_sz - wr));
564 	}
565 
566 	WRITE_ONCE(notif_q->wr_ptr, cpu_to_le32((wr + tx_sz) % q_sz));
567 
568 out:
569 	spin_unlock_bh(&mei->data_q_lock);
570 }
571 
572 static int
iwl_mei_send_sap_msg(struct mei_cl_device * cldev,u16 type)573 iwl_mei_send_sap_msg(struct mei_cl_device *cldev, u16 type)
574 {
575 	struct iwl_sap_hdr msg = {
576 		.type = cpu_to_le16(type),
577 	};
578 
579 	return iwl_mei_send_sap_msg_payload(cldev, &msg);
580 }
581 
iwl_mei_send_csa_msg_wk(struct work_struct * wk)582 static void iwl_mei_send_csa_msg_wk(struct work_struct *wk)
583 {
584 	struct iwl_mei *mei =
585 		container_of(wk, struct iwl_mei, send_csa_msg_wk);
586 
587 	if (!iwl_mei_is_connected())
588 		return;
589 
590 	mutex_lock(&iwl_mei_mutex);
591 
592 	iwl_mei_send_check_shared_area(mei->cldev);
593 
594 	mutex_unlock(&iwl_mei_mutex);
595 }
596 
597 /* Called in a RCU read critical section from netif_receive_skb */
iwl_mei_rx_handler(struct sk_buff ** pskb)598 static rx_handler_result_t iwl_mei_rx_handler(struct sk_buff **pskb)
599 {
600 	struct sk_buff *skb = *pskb;
601 	struct iwl_mei *mei =
602 		rcu_dereference(skb->dev->rx_handler_data);
603 	struct iwl_mei_filters *filters = rcu_dereference(mei->filters);
604 	bool rx_for_csme = false;
605 	rx_handler_result_t res;
606 
607 	/*
608 	 * remove() unregisters this handler and synchronize_net, so this
609 	 * should never happen.
610 	 */
611 	if (!iwl_mei_is_connected()) {
612 		dev_err(&mei->cldev->dev,
613 			"Got an Rx packet, but we're not connected to SAP?\n");
614 		return RX_HANDLER_PASS;
615 	}
616 
617 	if (filters)
618 		res = iwl_mei_rx_filter(skb, &filters->filters, &rx_for_csme);
619 	else
620 		res = RX_HANDLER_PASS;
621 
622 	/*
623 	 * The data is already on the ring of the shared area, all we
624 	 * need to do is to tell the CSME firmware to check what we have
625 	 * there.
626 	 */
627 	if (rx_for_csme)
628 		schedule_work(&mei->send_csa_msg_wk);
629 
630 	if (res != RX_HANDLER_PASS) {
631 		trace_iwlmei_sap_data(skb, IWL_SAP_RX_DATA_DROPPED_FROM_AIR);
632 		dev_kfree_skb(skb);
633 	}
634 
635 	return res;
636 }
637 
iwl_mei_netdev_work(struct work_struct * wk)638 static void iwl_mei_netdev_work(struct work_struct *wk)
639 {
640 	struct iwl_mei *mei =
641 		container_of(wk, struct iwl_mei, netdev_work);
642 	struct net_device *netdev;
643 
644 	/*
645 	 * First take rtnl and only then the mutex to avoid an ABBA
646 	 * with iwl_mei_set_netdev()
647 	 */
648 	rtnl_lock();
649 	mutex_lock(&iwl_mei_mutex);
650 
651 	netdev = rcu_dereference_protected(iwl_mei_cache.netdev,
652 					   lockdep_is_held(&iwl_mei_mutex));
653 	if (netdev) {
654 		if (mei->amt_enabled)
655 			netdev_rx_handler_register(netdev, iwl_mei_rx_handler,
656 						   mei);
657 		else
658 			netdev_rx_handler_unregister(netdev);
659 	}
660 
661 	mutex_unlock(&iwl_mei_mutex);
662 	rtnl_unlock();
663 }
664 
665 static void
iwl_mei_handle_rx_start_ok(struct mei_cl_device * cldev,const struct iwl_sap_me_msg_start_ok * rsp,ssize_t len)666 iwl_mei_handle_rx_start_ok(struct mei_cl_device *cldev,
667 			   const struct iwl_sap_me_msg_start_ok *rsp,
668 			   ssize_t len)
669 {
670 	if (len != sizeof(*rsp)) {
671 		dev_err(&cldev->dev,
672 			"got invalid SAP_ME_MSG_START_OK from CSME firmware\n");
673 		dev_err(&cldev->dev,
674 			"size is incorrect: %zd instead of %zu\n",
675 			len, sizeof(*rsp));
676 		return;
677 	}
678 
679 	if (rsp->supported_version != iwl_mei_cache.sap_version) {
680 		dev_err(&cldev->dev,
681 			"didn't get the expected version: got %d\n",
682 			rsp->supported_version);
683 		return;
684 	}
685 
686 	mutex_lock(&iwl_mei_mutex);
687 	set_bit(IWL_MEI_STATUS_SAP_CONNECTED, &iwl_mei_status);
688 	/*
689 	 * We'll receive AMT_STATE SAP message in a bit and
690 	 * that will continue the flow
691 	 */
692 	mutex_unlock(&iwl_mei_mutex);
693 }
694 
iwl_mei_handle_csme_filters(struct mei_cl_device * cldev,const struct iwl_sap_csme_filters * filters)695 static void iwl_mei_handle_csme_filters(struct mei_cl_device *cldev,
696 					const struct iwl_sap_csme_filters *filters)
697 {
698 	struct iwl_mei *mei = mei_cldev_get_drvdata(iwl_mei_global_cldev);
699 	struct iwl_mei_filters *new_filters;
700 	struct iwl_mei_filters *old_filters;
701 
702 	old_filters =
703 		rcu_dereference_protected(mei->filters,
704 					  lockdep_is_held(&iwl_mei_mutex));
705 
706 	new_filters = kzalloc_obj(*new_filters);
707 	if (!new_filters)
708 		return;
709 
710 	/* Copy the OOB filters */
711 	new_filters->filters = filters->filters;
712 
713 	rcu_assign_pointer(mei->filters, new_filters);
714 
715 	if (old_filters)
716 		kfree_rcu(old_filters, rcu_head);
717 }
718 
719 static void
iwl_mei_handle_conn_status(struct mei_cl_device * cldev,const struct iwl_sap_notif_conn_status * status)720 iwl_mei_handle_conn_status(struct mei_cl_device *cldev,
721 			   const struct iwl_sap_notif_conn_status *status)
722 {
723 	struct iwl_mei *mei = mei_cldev_get_drvdata(cldev);
724 	struct iwl_mei_conn_info conn_info = {
725 		.lp_state = le32_to_cpu(status->link_prot_state),
726 		.ssid_len = le32_to_cpu(status->conn_info.ssid_len),
727 		.channel = status->conn_info.channel,
728 		.band = status->conn_info.band,
729 		.auth_mode = le32_to_cpu(status->conn_info.auth_mode),
730 		.pairwise_cipher = le32_to_cpu(status->conn_info.pairwise_cipher),
731 	};
732 
733 	if (!iwl_mei_cache.ops ||
734 	    conn_info.ssid_len > ARRAY_SIZE(conn_info.ssid))
735 		return;
736 
737 	memcpy(conn_info.ssid, status->conn_info.ssid, conn_info.ssid_len);
738 	ether_addr_copy(conn_info.bssid, status->conn_info.bssid);
739 
740 	iwl_mei_cache.ops->me_conn_status(iwl_mei_cache.priv, &conn_info);
741 
742 	mei->link_prot_state = status->link_prot_state;
743 
744 	/*
745 	 * Update the Rfkill state in case the host does not own the device:
746 	 * if we are in Link Protection, ask to not touch the device, else,
747 	 * unblock rfkill.
748 	 * If the host owns the device, inform the user space whether it can
749 	 * roam.
750 	 */
751 	if (mei->got_ownership)
752 		iwl_mei_cache.ops->roaming_forbidden(iwl_mei_cache.priv,
753 						     status->link_prot_state);
754 	else
755 		iwl_mei_cache.ops->rfkill(iwl_mei_cache.priv,
756 					  status->link_prot_state, false);
757 }
758 
iwl_mei_set_init_conf(struct iwl_mei * mei)759 static void iwl_mei_set_init_conf(struct iwl_mei *mei)
760 {
761 	struct iwl_sap_notif_host_link_up link_msg = {
762 		.hdr.type = cpu_to_le16(SAP_MSG_NOTIF_HOST_LINK_UP),
763 		.hdr.len = cpu_to_le16(sizeof(link_msg) - sizeof(link_msg.hdr)),
764 	};
765 	struct iwl_sap_notif_country_code mcc_msg = {
766 		.hdr.type = cpu_to_le16(SAP_MSG_NOTIF_COUNTRY_CODE),
767 		.hdr.len = cpu_to_le16(sizeof(mcc_msg) - sizeof(mcc_msg.hdr)),
768 		.mcc = cpu_to_le16(iwl_mei_cache.mcc),
769 	};
770 	struct iwl_sap_notif_sar_limits sar_msg = {
771 		.hdr.type = cpu_to_le16(SAP_MSG_NOTIF_SAR_LIMITS),
772 		.hdr.len = cpu_to_le16(sizeof(sar_msg) - sizeof(sar_msg.hdr)),
773 	};
774 	struct iwl_sap_notif_host_nic_info nic_info_msg = {
775 		.hdr.type = cpu_to_le16(SAP_MSG_NOTIF_NIC_INFO),
776 		.hdr.len = cpu_to_le16(sizeof(nic_info_msg) - sizeof(nic_info_msg.hdr)),
777 	};
778 	struct iwl_sap_msg_dw rfkill_msg = {
779 		.hdr.type = cpu_to_le16(SAP_MSG_NOTIF_RADIO_STATE),
780 		.hdr.len = cpu_to_le16(sizeof(rfkill_msg) - sizeof(rfkill_msg.hdr)),
781 		.val = cpu_to_le32(iwl_mei_cache.rf_kill),
782 	};
783 
784 	/* wifi driver has registered already */
785 	if (iwl_mei_cache.ops) {
786 		iwl_mei_send_sap_msg(mei->cldev,
787 				     SAP_MSG_NOTIF_WIFIDR_UP);
788 		iwl_mei_cache.ops->sap_connected(iwl_mei_cache.priv);
789 	}
790 
791 	iwl_mei_send_sap_msg(mei->cldev, SAP_MSG_NOTIF_WHO_OWNS_NIC);
792 
793 	if (iwl_mei_cache.conn_info) {
794 		link_msg.conn_info = *iwl_mei_cache.conn_info;
795 		iwl_mei_send_sap_msg_payload(mei->cldev, &link_msg.hdr);
796 	}
797 
798 	iwl_mei_send_sap_msg_payload(mei->cldev, &mcc_msg.hdr);
799 
800 	if (iwl_mei_cache.power_limit) {
801 		memcpy(sar_msg.sar_chain_info_table, iwl_mei_cache.power_limit,
802 		       sizeof(sar_msg.sar_chain_info_table));
803 		iwl_mei_send_sap_msg_payload(mei->cldev, &sar_msg.hdr);
804 	}
805 
806 	if (is_valid_ether_addr(iwl_mei_cache.mac_address)) {
807 		ether_addr_copy(nic_info_msg.mac_address,
808 				iwl_mei_cache.mac_address);
809 		ether_addr_copy(nic_info_msg.nvm_address,
810 				iwl_mei_cache.nvm_address);
811 		iwl_mei_send_sap_msg_payload(mei->cldev, &nic_info_msg.hdr);
812 	}
813 
814 	iwl_mei_send_sap_msg_payload(mei->cldev, &rfkill_msg.hdr);
815 }
816 
iwl_mei_handle_amt_state(struct mei_cl_device * cldev,const struct iwl_sap_msg_dw * dw)817 static void iwl_mei_handle_amt_state(struct mei_cl_device *cldev,
818 				     const struct iwl_sap_msg_dw *dw)
819 {
820 	struct iwl_mei *mei = mei_cldev_get_drvdata(cldev);
821 
822 	mutex_lock(&iwl_mei_mutex);
823 
824 	if (mei->amt_enabled == !!le32_to_cpu(dw->val))
825 		goto out;
826 
827 	mei->amt_enabled = dw->val;
828 
829 	if (mei->amt_enabled)
830 		iwl_mei_set_init_conf(mei);
831 	else if (iwl_mei_cache.ops)
832 		iwl_mei_cache.ops->rfkill(iwl_mei_cache.priv, false, false);
833 
834 	schedule_work(&mei->netdev_work);
835 
836 out:
837 	mutex_unlock(&iwl_mei_mutex);
838 }
839 
iwl_mei_handle_nic_owner(struct mei_cl_device * cldev,const struct iwl_sap_msg_dw * dw)840 static void iwl_mei_handle_nic_owner(struct mei_cl_device *cldev,
841 				     const struct iwl_sap_msg_dw *dw)
842 {
843 	struct iwl_mei *mei = mei_cldev_get_drvdata(cldev);
844 
845 	mei->got_ownership = dw->val != cpu_to_le32(SAP_NIC_OWNER_ME);
846 }
847 
iwl_mei_handle_can_release_ownership(struct mei_cl_device * cldev,const void * payload)848 static void iwl_mei_handle_can_release_ownership(struct mei_cl_device *cldev,
849 						 const void *payload)
850 {
851 	/* We can get ownership and driver is registered, go ahead */
852 	if (iwl_mei_cache.ops)
853 		iwl_mei_send_sap_msg(cldev,
854 				     SAP_MSG_NOTIF_HOST_ASKS_FOR_NIC_OWNERSHIP);
855 }
856 
iwl_mei_handle_csme_taking_ownership(struct mei_cl_device * cldev,const void * payload)857 static void iwl_mei_handle_csme_taking_ownership(struct mei_cl_device *cldev,
858 						 const void *payload)
859 {
860 	struct iwl_mei *mei = mei_cldev_get_drvdata(cldev);
861 
862 	dev_info(&cldev->dev, "CSME takes ownership\n");
863 
864 	mei->got_ownership = false;
865 
866 	if (iwl_mei_cache.ops && !mei->device_down) {
867 		/*
868 		 * Remember to send CSME_OWNERSHIP_CONFIRMED when the wifi
869 		 * driver is finished taking the device down.
870 		 */
871 		mei->csme_taking_ownership = true;
872 
873 		iwl_mei_cache.ops->rfkill(iwl_mei_cache.priv, true, true);
874 	} else {
875 		iwl_mei_send_sap_msg(cldev,
876 				     SAP_MSG_NOTIF_CSME_OWNERSHIP_CONFIRMED);
877 		schedule_delayed_work(&mei->ownership_dwork,
878 				      MEI_OWNERSHIP_RETAKE_TIMEOUT_MS);
879 	}
880 }
881 
iwl_mei_handle_nvm(struct mei_cl_device * cldev,const struct iwl_sap_nvm * sap_nvm)882 static void iwl_mei_handle_nvm(struct mei_cl_device *cldev,
883 			       const struct iwl_sap_nvm *sap_nvm)
884 {
885 	struct iwl_mei *mei = mei_cldev_get_drvdata(cldev);
886 	const struct iwl_mei_nvm *mei_nvm = (const void *)sap_nvm;
887 	int i;
888 
889 	kfree(mei->nvm);
890 	mei->nvm = kzalloc_obj(*mei_nvm);
891 	if (!mei->nvm)
892 		return;
893 
894 	ether_addr_copy(mei->nvm->hw_addr, sap_nvm->hw_addr);
895 	mei->nvm->n_hw_addrs = sap_nvm->n_hw_addrs;
896 	mei->nvm->radio_cfg = le32_to_cpu(sap_nvm->radio_cfg);
897 	mei->nvm->caps = le32_to_cpu(sap_nvm->caps);
898 	mei->nvm->nvm_version = le32_to_cpu(sap_nvm->nvm_version);
899 
900 	for (i = 0; i < ARRAY_SIZE(mei->nvm->channels); i++)
901 		mei->nvm->channels[i] = le32_to_cpu(sap_nvm->channels[i]);
902 
903 	wake_up_all(&mei->get_nvm_wq);
904 }
905 
iwl_mei_handle_rx_host_own_req(struct mei_cl_device * cldev,const struct iwl_sap_msg_dw * dw)906 static void iwl_mei_handle_rx_host_own_req(struct mei_cl_device *cldev,
907 					   const struct iwl_sap_msg_dw *dw)
908 {
909 	struct iwl_mei *mei = mei_cldev_get_drvdata(cldev);
910 
911 	/*
912 	 * This means that we can't use the wifi device right now, CSME is not
913 	 * ready to let us use it.
914 	 */
915 	if (!dw->val) {
916 		dev_info(&cldev->dev, "Ownership req denied\n");
917 		return;
918 	}
919 
920 	mei->got_ownership = true;
921 	wake_up_all(&mei->get_ownership_wq);
922 
923 	iwl_mei_send_sap_msg(cldev,
924 			     SAP_MSG_NOTIF_HOST_OWNERSHIP_CONFIRMED);
925 
926 	/* We can now start the connection, unblock rfkill */
927 	if (iwl_mei_cache.ops)
928 		iwl_mei_cache.ops->rfkill(iwl_mei_cache.priv, false, false);
929 }
930 
iwl_mei_handle_pldr_ack(struct mei_cl_device * cldev,const struct iwl_sap_pldr_ack_data * ack)931 static void iwl_mei_handle_pldr_ack(struct mei_cl_device *cldev,
932 				    const struct iwl_sap_pldr_ack_data *ack)
933 {
934 	struct iwl_mei *mei = mei_cldev_get_drvdata(cldev);
935 
936 	mei->pldr_active = le32_to_cpu(ack->status) == SAP_PLDR_STATUS_SUCCESS;
937 	wake_up_all(&mei->pldr_wq);
938 }
939 
iwl_mei_handle_ping(struct mei_cl_device * cldev,const struct iwl_sap_hdr * hdr)940 static void iwl_mei_handle_ping(struct mei_cl_device *cldev,
941 				const struct iwl_sap_hdr *hdr)
942 {
943 	iwl_mei_send_sap_msg(cldev, SAP_MSG_NOTIF_PONG);
944 }
945 
iwl_mei_handle_sap_msg(struct mei_cl_device * cldev,const struct iwl_sap_hdr * hdr)946 static void iwl_mei_handle_sap_msg(struct mei_cl_device *cldev,
947 				   const struct iwl_sap_hdr *hdr)
948 {
949 	u16 len = le16_to_cpu(hdr->len) + sizeof(*hdr);
950 	u16 type = le16_to_cpu(hdr->type);
951 
952 	dev_dbg(&cldev->dev,
953 		"Got a new SAP message: type %d, len %d, seq %d\n",
954 		le16_to_cpu(hdr->type), len,
955 		le32_to_cpu(hdr->seq_num));
956 
957 #define SAP_MSG_HANDLER(_cmd, _handler, _sz)				\
958 	case SAP_MSG_NOTIF_ ## _cmd:					\
959 		if (len < _sz) {					\
960 			dev_err(&cldev->dev,				\
961 				"Bad size for %d: %u < %u\n",		\
962 				le16_to_cpu(hdr->type),			\
963 				(unsigned int)len,			\
964 				(unsigned int)_sz);			\
965 			break;						\
966 		}							\
967 		mutex_lock(&iwl_mei_mutex);				\
968 		_handler(cldev, (const void *)hdr);			\
969 		mutex_unlock(&iwl_mei_mutex);				\
970 		break
971 
972 #define SAP_MSG_HANDLER_NO_LOCK(_cmd, _handler, _sz)			\
973 	case SAP_MSG_NOTIF_ ## _cmd:					\
974 		if (len < _sz) {					\
975 			dev_err(&cldev->dev,				\
976 				"Bad size for %d: %u < %u\n",		\
977 				le16_to_cpu(hdr->type),			\
978 				(unsigned int)len,			\
979 				(unsigned int)_sz);			\
980 			break;						\
981 		}							\
982 		_handler(cldev, (const void *)hdr);			\
983 		break
984 
985 #define SAP_MSG_HANDLER_NO_HANDLER(_cmd, _sz)				\
986 	case SAP_MSG_NOTIF_ ## _cmd:					\
987 		if (len < _sz) {					\
988 			dev_err(&cldev->dev,				\
989 				"Bad size for %d: %u < %u\n",		\
990 				le16_to_cpu(hdr->type),			\
991 				(unsigned int)len,			\
992 				(unsigned int)_sz);			\
993 			break;						\
994 		}							\
995 		break
996 
997 	switch (type) {
998 	SAP_MSG_HANDLER(PING, iwl_mei_handle_ping, 0);
999 	SAP_MSG_HANDLER(CSME_FILTERS,
1000 			iwl_mei_handle_csme_filters,
1001 			sizeof(struct iwl_sap_csme_filters));
1002 	SAP_MSG_HANDLER(CSME_CONN_STATUS,
1003 			iwl_mei_handle_conn_status,
1004 			sizeof(struct iwl_sap_notif_conn_status));
1005 	SAP_MSG_HANDLER_NO_LOCK(AMT_STATE,
1006 				iwl_mei_handle_amt_state,
1007 				sizeof(struct iwl_sap_msg_dw));
1008 	SAP_MSG_HANDLER_NO_HANDLER(PONG, 0);
1009 	SAP_MSG_HANDLER(NVM, iwl_mei_handle_nvm,
1010 			sizeof(struct iwl_sap_nvm));
1011 	SAP_MSG_HANDLER(CSME_REPLY_TO_HOST_OWNERSHIP_REQ,
1012 			iwl_mei_handle_rx_host_own_req,
1013 			sizeof(struct iwl_sap_msg_dw));
1014 	SAP_MSG_HANDLER(NIC_OWNER, iwl_mei_handle_nic_owner,
1015 			sizeof(struct iwl_sap_msg_dw));
1016 	SAP_MSG_HANDLER(CSME_CAN_RELEASE_OWNERSHIP,
1017 			iwl_mei_handle_can_release_ownership, 0);
1018 	SAP_MSG_HANDLER(CSME_TAKING_OWNERSHIP,
1019 			iwl_mei_handle_csme_taking_ownership, 0);
1020 	SAP_MSG_HANDLER(PLDR_ACK, iwl_mei_handle_pldr_ack,
1021 			sizeof(struct iwl_sap_pldr_ack_data));
1022 	default:
1023 	/*
1024 	 * This is not really an error, there are message that we decided
1025 	 * to ignore, yet, it is useful to be able to leave a note if debug
1026 	 * is enabled.
1027 	 */
1028 	dev_dbg(&cldev->dev, "Unsupported message: type %d, len %d\n",
1029 		le16_to_cpu(hdr->type), len);
1030 	}
1031 
1032 #undef SAP_MSG_HANDLER
1033 #undef SAP_MSG_HANDLER_NO_LOCK
1034 }
1035 
iwl_mei_read_from_q(const u8 * q_head,u32 q_sz,u32 * _rd,u32 wr,void * _buf,u32 len)1036 static void iwl_mei_read_from_q(const u8 *q_head, u32 q_sz,
1037 				u32 *_rd, u32 wr,
1038 				void *_buf, u32 len)
1039 {
1040 	u8 *buf = _buf;
1041 	u32 rd = *_rd;
1042 
1043 	if (rd + len <= q_sz) {
1044 		if (buf)
1045 			memcpy(buf, q_head + rd, len);
1046 		rd += len;
1047 	} else {
1048 		if (buf) {
1049 			memcpy(buf, q_head + rd, q_sz - rd);
1050 			memcpy(buf + q_sz - rd, q_head, len - (q_sz - rd));
1051 		}
1052 		rd = len - (q_sz - rd);
1053 	}
1054 
1055 	*_rd = rd;
1056 }
1057 
1058 #define QOS_HDR_IV_SNAP_LEN (sizeof(struct ieee80211_qos_hdr) +      \
1059 			     IEEE80211_TKIP_IV_LEN +                 \
1060 			     sizeof(rfc1042_header) + ETH_TLEN)
1061 
iwl_mei_handle_sap_data(struct mei_cl_device * cldev,const u8 * q_head,u32 q_sz,u32 rd,u32 wr,ssize_t valid_rx_sz,struct sk_buff_head * tx_skbs)1062 static void iwl_mei_handle_sap_data(struct mei_cl_device *cldev,
1063 				    const u8 *q_head, u32 q_sz,
1064 				    u32 rd, u32 wr, ssize_t valid_rx_sz,
1065 				    struct sk_buff_head *tx_skbs)
1066 {
1067 	struct iwl_sap_hdr hdr;
1068 	struct net_device *netdev =
1069 		rcu_dereference_protected(iwl_mei_cache.netdev,
1070 					  lockdep_is_held(&iwl_mei_mutex));
1071 
1072 	if (!netdev)
1073 		return;
1074 
1075 	while (valid_rx_sz >= sizeof(hdr)) {
1076 		struct ethhdr *ethhdr;
1077 		unsigned char *data;
1078 		struct sk_buff *skb;
1079 		u16 len;
1080 
1081 		iwl_mei_read_from_q(q_head, q_sz, &rd, wr, &hdr, sizeof(hdr));
1082 		valid_rx_sz -= sizeof(hdr);
1083 		len = le16_to_cpu(hdr.len);
1084 
1085 		if (valid_rx_sz < len) {
1086 			dev_err(&cldev->dev,
1087 				"Data queue is corrupted: valid data len %zd, len %d\n",
1088 				valid_rx_sz, len);
1089 			break;
1090 		}
1091 
1092 		valid_rx_sz -= len;
1093 
1094 		if (len < sizeof(*ethhdr)) {
1095 			dev_err(&cldev->dev,
1096 				"Data len is smaller than an ethernet header? len = %d\n",
1097 				len);
1098 			iwl_mei_read_from_q(q_head, q_sz, &rd, wr, NULL, len);
1099 			continue;
1100 		}
1101 
1102 		if (le16_to_cpu(hdr.type) != SAP_MSG_DATA_PACKET) {
1103 			dev_err(&cldev->dev, "Unsupported Rx data: type %d, len %d\n",
1104 				le16_to_cpu(hdr.type), len);
1105 			iwl_mei_read_from_q(q_head, q_sz, &rd, wr, NULL, len);
1106 			continue;
1107 		}
1108 
1109 		/* We need enough room for the WiFi header + SNAP + IV */
1110 		skb = netdev_alloc_skb(netdev, len + QOS_HDR_IV_SNAP_LEN);
1111 		if (!skb) {
1112 			iwl_mei_read_from_q(q_head, q_sz, &rd, wr, NULL, len);
1113 			continue;
1114 		}
1115 
1116 		skb_reserve(skb, QOS_HDR_IV_SNAP_LEN);
1117 		ethhdr = skb_push(skb, sizeof(*ethhdr));
1118 
1119 		iwl_mei_read_from_q(q_head, q_sz, &rd, wr,
1120 				    ethhdr, sizeof(*ethhdr));
1121 		len -= sizeof(*ethhdr);
1122 
1123 		skb_reset_mac_header(skb);
1124 		skb_reset_network_header(skb);
1125 		skb->protocol = ethhdr->h_proto;
1126 
1127 		data = skb_put(skb, len);
1128 		iwl_mei_read_from_q(q_head, q_sz, &rd, wr, data, len);
1129 
1130 		/*
1131 		 * Enqueue the skb here so that it can be sent later when we
1132 		 * do not hold the mutex. TX'ing a packet with a mutex held is
1133 		 * possible, but it wouldn't be nice to forbid the TX path to
1134 		 * call any of iwlmei's functions, since every API from iwlmei
1135 		 * needs the mutex.
1136 		 */
1137 		__skb_queue_tail(tx_skbs, skb);
1138 	}
1139 }
1140 
iwl_mei_handle_sap_rx_cmd(struct mei_cl_device * cldev,const u8 * q_head,u32 q_sz,u32 rd,u32 wr,ssize_t valid_rx_sz)1141 static void iwl_mei_handle_sap_rx_cmd(struct mei_cl_device *cldev,
1142 				      const u8 *q_head, u32 q_sz,
1143 				      u32 rd, u32 wr, ssize_t valid_rx_sz)
1144 {
1145 	struct page *p = alloc_page(GFP_KERNEL);
1146 	struct iwl_sap_hdr *hdr;
1147 
1148 	if (!p)
1149 		return;
1150 
1151 	hdr = page_address(p);
1152 
1153 	while (valid_rx_sz >= sizeof(*hdr)) {
1154 		u16 len;
1155 
1156 		iwl_mei_read_from_q(q_head, q_sz, &rd, wr, hdr, sizeof(*hdr));
1157 		valid_rx_sz -= sizeof(*hdr);
1158 		len = le16_to_cpu(hdr->len);
1159 		if (len + sizeof(*hdr) > PAGE_SIZE) {
1160 			dev_err(&cldev->dev,
1161 				"SAP message is too big: %u\n", len);
1162 			break;
1163 		}
1164 
1165 		if (valid_rx_sz < len)
1166 			break;
1167 
1168 		iwl_mei_read_from_q(q_head, q_sz, &rd, wr, hdr + 1, len);
1169 
1170 		trace_iwlmei_sap_cmd(hdr, false);
1171 		iwl_mei_handle_sap_msg(cldev, hdr);
1172 		valid_rx_sz -= len;
1173 	}
1174 
1175 	/* valid_rx_sz must be 0 now... */
1176 	if (valid_rx_sz)
1177 		dev_err(&cldev->dev,
1178 			"More data in the buffer although we read it all\n");
1179 
1180 	__free_page(p);
1181 }
1182 
iwl_mei_handle_sap_rx(struct mei_cl_device * cldev,struct iwl_sap_q_ctrl_blk * notif_q,const u8 * q_head,struct sk_buff_head * skbs,u32 q_sz)1183 static void iwl_mei_handle_sap_rx(struct mei_cl_device *cldev,
1184 				  struct iwl_sap_q_ctrl_blk *notif_q,
1185 				  const u8 *q_head,
1186 				  struct sk_buff_head *skbs,
1187 				  u32 q_sz)
1188 {
1189 	u32 rd = le32_to_cpu(READ_ONCE(notif_q->rd_ptr));
1190 	u32 wr = le32_to_cpu(READ_ONCE(notif_q->wr_ptr));
1191 	ssize_t valid_rx_sz;
1192 
1193 	if (rd > q_sz || wr > q_sz) {
1194 		dev_err(&cldev->dev,
1195 			"Pointers are past the buffer limit\n");
1196 		return;
1197 	}
1198 
1199 	if (rd == wr)
1200 		return;
1201 
1202 	valid_rx_sz = wr > rd ? wr - rd : q_sz - rd + wr;
1203 
1204 	if (skbs)
1205 		iwl_mei_handle_sap_data(cldev, q_head, q_sz, rd, wr,
1206 					valid_rx_sz, skbs);
1207 	else
1208 		iwl_mei_handle_sap_rx_cmd(cldev, q_head, q_sz, rd, wr,
1209 					  valid_rx_sz);
1210 
1211 	/* Increment the read pointer to point to the write pointer */
1212 	WRITE_ONCE(notif_q->rd_ptr, cpu_to_le32(wr));
1213 }
1214 
iwl_mei_handle_check_shared_area(struct mei_cl_device * cldev)1215 static void iwl_mei_handle_check_shared_area(struct mei_cl_device *cldev)
1216 {
1217 	struct iwl_mei *mei = mei_cldev_get_drvdata(cldev);
1218 	struct iwl_sap_q_ctrl_blk *notif_q;
1219 	struct sk_buff_head tx_skbs;
1220 	struct iwl_sap_dir *dir;
1221 	void *q_head;
1222 	u32 q_sz;
1223 
1224 	if (!mei->shared_mem.ctrl)
1225 		return;
1226 
1227 	dir = &mei->shared_mem.ctrl->dir[SAP_DIRECTION_ME_TO_HOST];
1228 	notif_q = &dir->q_ctrl_blk[SAP_QUEUE_IDX_NOTIF];
1229 	q_head = mei->shared_mem.q_head[SAP_DIRECTION_ME_TO_HOST][SAP_QUEUE_IDX_NOTIF];
1230 	q_sz = mei->shared_mem.q_size[SAP_DIRECTION_ME_TO_HOST][SAP_QUEUE_IDX_NOTIF];
1231 
1232 	/*
1233 	 * Do not hold the mutex here, but rather each and every message
1234 	 * handler takes it.
1235 	 * This allows message handlers to take it at a certain time.
1236 	 */
1237 	iwl_mei_handle_sap_rx(cldev, notif_q, q_head, NULL, q_sz);
1238 
1239 	mutex_lock(&iwl_mei_mutex);
1240 	dir = &mei->shared_mem.ctrl->dir[SAP_DIRECTION_ME_TO_HOST];
1241 	notif_q = &dir->q_ctrl_blk[SAP_QUEUE_IDX_DATA];
1242 	q_head = mei->shared_mem.q_head[SAP_DIRECTION_ME_TO_HOST][SAP_QUEUE_IDX_DATA];
1243 	q_sz = mei->shared_mem.q_size[SAP_DIRECTION_ME_TO_HOST][SAP_QUEUE_IDX_DATA];
1244 
1245 	__skb_queue_head_init(&tx_skbs);
1246 
1247 	iwl_mei_handle_sap_rx(cldev, notif_q, q_head, &tx_skbs, q_sz);
1248 
1249 	if (skb_queue_empty(&tx_skbs)) {
1250 		mutex_unlock(&iwl_mei_mutex);
1251 		return;
1252 	}
1253 
1254 	/*
1255 	 * Take the RCU read lock before we unlock the mutex to make sure that
1256 	 * even if the netdev is replaced by another non-NULL netdev right after
1257 	 * we unlock the mutex, the old netdev will still be valid when we
1258 	 * transmit the frames. We can't allow to replace the netdev here because
1259 	 * the skbs hold a pointer to the netdev.
1260 	 */
1261 	rcu_read_lock();
1262 
1263 	mutex_unlock(&iwl_mei_mutex);
1264 
1265 	if (!rcu_access_pointer(iwl_mei_cache.netdev)) {
1266 		dev_err(&cldev->dev, "Can't Tx without a netdev\n");
1267 		skb_queue_purge(&tx_skbs);
1268 		goto out;
1269 	}
1270 
1271 	while (!skb_queue_empty(&tx_skbs)) {
1272 		struct sk_buff *skb = __skb_dequeue(&tx_skbs);
1273 
1274 		trace_iwlmei_sap_data(skb, IWL_SAP_RX_DATA_TO_AIR);
1275 		dev_queue_xmit(skb);
1276 	}
1277 
1278 out:
1279 	rcu_read_unlock();
1280 }
1281 
iwl_mei_rx(struct mei_cl_device * cldev)1282 static void iwl_mei_rx(struct mei_cl_device *cldev)
1283 {
1284 	struct iwl_sap_me_msg_hdr *hdr;
1285 	u8 msg[100];
1286 	ssize_t ret;
1287 
1288 	ret = mei_cldev_recv(cldev, (u8 *)&msg, sizeof(msg));
1289 	if (ret < 0) {
1290 		dev_err(&cldev->dev, "failed to receive data: %zd\n", ret);
1291 		return;
1292 	}
1293 
1294 	if (ret == 0) {
1295 		dev_err(&cldev->dev, "got an empty response\n");
1296 		return;
1297 	}
1298 
1299 	hdr = (void *)msg;
1300 	trace_iwlmei_me_msg(hdr, false);
1301 
1302 	switch (le32_to_cpu(hdr->type)) {
1303 	case SAP_ME_MSG_START_OK:
1304 		BUILD_BUG_ON(sizeof(struct iwl_sap_me_msg_start_ok) >
1305 			     sizeof(msg));
1306 
1307 		iwl_mei_handle_rx_start_ok(cldev, (void *)msg, ret);
1308 		break;
1309 	case SAP_ME_MSG_CHECK_SHARED_AREA:
1310 		iwl_mei_handle_check_shared_area(cldev);
1311 		break;
1312 	default:
1313 		dev_err(&cldev->dev, "got a RX notification: %d\n",
1314 			le32_to_cpu(hdr->type));
1315 		break;
1316 	}
1317 }
1318 
iwl_mei_send_start(struct mei_cl_device * cldev)1319 static int iwl_mei_send_start(struct mei_cl_device *cldev)
1320 {
1321 	struct iwl_mei *mei = mei_cldev_get_drvdata(cldev);
1322 	struct iwl_sap_me_msg_start msg = {
1323 		.hdr.type = cpu_to_le32(SAP_ME_MSG_START),
1324 		.hdr.seq_num = cpu_to_le32(atomic_inc_return(&mei->seq_no)),
1325 		.hdr.len = cpu_to_le32(sizeof(msg)),
1326 		.supported_versions[0] = iwl_mei_cache.sap_version,
1327 		.init_data_seq_num = cpu_to_le16(0x100),
1328 		.init_notif_seq_num = cpu_to_le16(0x800),
1329 	};
1330 	int ret;
1331 
1332 	trace_iwlmei_me_msg(&msg.hdr, true);
1333 	ret = mei_cldev_send(cldev, (void *)&msg, sizeof(msg));
1334 	if (ret != sizeof(msg)) {
1335 		dev_err(&cldev->dev,
1336 			"failed to send the SAP_ME_MSG_START message %d\n",
1337 			ret);
1338 		return ret;
1339 	}
1340 
1341 	return 0;
1342 }
1343 
iwl_mei_enable(struct mei_cl_device * cldev)1344 static int iwl_mei_enable(struct mei_cl_device *cldev)
1345 {
1346 	int ret;
1347 
1348 	ret = mei_cldev_enable(cldev);
1349 	if (ret < 0) {
1350 		dev_err(&cldev->dev, "failed to enable the device: %d\n", ret);
1351 		return ret;
1352 	}
1353 
1354 	ret = mei_cldev_register_rx_cb(cldev, iwl_mei_rx);
1355 	if (ret) {
1356 		dev_err(&cldev->dev,
1357 			"failed to register to the rx cb: %d\n", ret);
1358 		mei_cldev_disable(cldev);
1359 		return ret;
1360 	}
1361 
1362 	return 0;
1363 }
1364 
iwl_mei_get_nvm(void)1365 struct iwl_mei_nvm *iwl_mei_get_nvm(void)
1366 {
1367 	struct iwl_mei_nvm *nvm = NULL;
1368 	struct iwl_mei *mei;
1369 	int ret;
1370 
1371 	mutex_lock(&iwl_mei_mutex);
1372 
1373 	if (!iwl_mei_is_connected())
1374 		goto out;
1375 
1376 	mei = mei_cldev_get_drvdata(iwl_mei_global_cldev);
1377 
1378 	if (!mei)
1379 		goto out;
1380 
1381 	ret = iwl_mei_send_sap_msg(iwl_mei_global_cldev,
1382 				   SAP_MSG_NOTIF_GET_NVM);
1383 	if (ret)
1384 		goto out;
1385 
1386 	mutex_unlock(&iwl_mei_mutex);
1387 
1388 	ret = wait_event_timeout(mei->get_nvm_wq, mei->nvm, 2 * HZ);
1389 	if (!ret)
1390 		return NULL;
1391 
1392 	mutex_lock(&iwl_mei_mutex);
1393 
1394 	if (!iwl_mei_is_connected())
1395 		goto out;
1396 
1397 	mei = mei_cldev_get_drvdata(iwl_mei_global_cldev);
1398 
1399 	if (!mei)
1400 		goto out;
1401 
1402 	if (mei->nvm)
1403 		nvm = kmemdup(mei->nvm, sizeof(*mei->nvm), GFP_KERNEL);
1404 
1405 out:
1406 	mutex_unlock(&iwl_mei_mutex);
1407 	return nvm;
1408 }
1409 EXPORT_SYMBOL_GPL(iwl_mei_get_nvm);
1410 
1411 #define IWL_MEI_PLDR_NUM_RETRIES	3
1412 
iwl_mei_pldr_req(void)1413 int iwl_mei_pldr_req(void)
1414 {
1415 	struct iwl_mei *mei;
1416 	int ret;
1417 	struct iwl_sap_pldr_data msg = {
1418 		.hdr.type = cpu_to_le16(SAP_MSG_NOTIF_PLDR),
1419 		.hdr.len = cpu_to_le16(sizeof(msg) - sizeof(msg.hdr)),
1420 	};
1421 	int i;
1422 
1423 	mutex_lock(&iwl_mei_mutex);
1424 
1425 	/* In case we didn't have a bind */
1426 	if (!iwl_mei_is_connected()) {
1427 		ret = 0;
1428 		goto out;
1429 	}
1430 
1431 	mei = mei_cldev_get_drvdata(iwl_mei_global_cldev);
1432 
1433 	if (!mei) {
1434 		ret = -ENODEV;
1435 		goto out;
1436 	}
1437 
1438 	if (!mei->amt_enabled) {
1439 		ret = 0;
1440 		goto out;
1441 	}
1442 
1443 	for (i = 0; i < IWL_MEI_PLDR_NUM_RETRIES; i++) {
1444 		ret = iwl_mei_send_sap_msg_payload(mei->cldev, &msg.hdr);
1445 		mutex_unlock(&iwl_mei_mutex);
1446 		if (ret)
1447 			return ret;
1448 
1449 		ret = wait_event_timeout(mei->pldr_wq, mei->pldr_active, HZ / 2);
1450 		if (ret)
1451 			break;
1452 
1453 		/* Take the mutex for the next iteration */
1454 		mutex_lock(&iwl_mei_mutex);
1455 	}
1456 
1457 	if (ret)
1458 		return 0;
1459 
1460 	ret = -ETIMEDOUT;
1461 out:
1462 	mutex_unlock(&iwl_mei_mutex);
1463 	return ret;
1464 }
1465 EXPORT_SYMBOL_GPL(iwl_mei_pldr_req);
1466 
iwl_mei_get_ownership(void)1467 int iwl_mei_get_ownership(void)
1468 {
1469 	struct iwl_mei *mei;
1470 	int ret;
1471 
1472 	mutex_lock(&iwl_mei_mutex);
1473 
1474 	/* In case we didn't have a bind */
1475 	if (!iwl_mei_is_connected()) {
1476 		ret = 0;
1477 		goto out;
1478 	}
1479 
1480 	mei = mei_cldev_get_drvdata(iwl_mei_global_cldev);
1481 
1482 	if (!mei) {
1483 		ret = -ENODEV;
1484 		goto out;
1485 	}
1486 
1487 	if (!mei->amt_enabled) {
1488 		ret = 0;
1489 		goto out;
1490 	}
1491 
1492 	if (mei->got_ownership) {
1493 		ret = 0;
1494 		goto out;
1495 	}
1496 
1497 	ret = iwl_mei_send_sap_msg(mei->cldev,
1498 				   SAP_MSG_NOTIF_HOST_ASKS_FOR_NIC_OWNERSHIP);
1499 	if (ret)
1500 		goto out;
1501 
1502 	mutex_unlock(&iwl_mei_mutex);
1503 
1504 	ret = wait_event_timeout(mei->get_ownership_wq,
1505 				 mei->got_ownership, HZ / 2);
1506 	if (!ret) {
1507 		schedule_delayed_work(&mei->ownership_dwork,
1508 				      MEI_OWNERSHIP_RETAKE_TIMEOUT_MS);
1509 		return -ETIMEDOUT;
1510 	}
1511 
1512 	return 0;
1513 out:
1514 	mutex_unlock(&iwl_mei_mutex);
1515 	return ret;
1516 }
1517 EXPORT_SYMBOL_GPL(iwl_mei_get_ownership);
1518 
iwl_mei_alive_notif(bool success)1519 void iwl_mei_alive_notif(bool success)
1520 {
1521 	struct iwl_mei *mei;
1522 	struct iwl_sap_pldr_end_data msg = {
1523 		.hdr.type = cpu_to_le16(SAP_MSG_NOTIF_PLDR_END),
1524 		.hdr.len = cpu_to_le16(sizeof(msg) - sizeof(msg.hdr)),
1525 		.status = success ? cpu_to_le32(SAP_PLDR_STATUS_SUCCESS) :
1526 			cpu_to_le32(SAP_PLDR_STATUS_FAILURE),
1527 	};
1528 
1529 	mutex_lock(&iwl_mei_mutex);
1530 
1531 	if (!iwl_mei_is_connected())
1532 		goto out;
1533 
1534 	mei = mei_cldev_get_drvdata(iwl_mei_global_cldev);
1535 	if (!mei || !mei->pldr_active)
1536 		goto out;
1537 
1538 	mei->pldr_active = false;
1539 
1540 	iwl_mei_send_sap_msg_payload(mei->cldev, &msg.hdr);
1541 out:
1542 	mutex_unlock(&iwl_mei_mutex);
1543 }
1544 EXPORT_SYMBOL_GPL(iwl_mei_alive_notif);
1545 
iwl_mei_host_associated(const struct iwl_mei_conn_info * conn_info,const struct iwl_mei_colloc_info * colloc_info)1546 void iwl_mei_host_associated(const struct iwl_mei_conn_info *conn_info,
1547 			     const struct iwl_mei_colloc_info *colloc_info)
1548 {
1549 	struct iwl_sap_notif_host_link_up msg = {
1550 		.hdr.type = cpu_to_le16(SAP_MSG_NOTIF_HOST_LINK_UP),
1551 		.hdr.len = cpu_to_le16(sizeof(msg) - sizeof(msg.hdr)),
1552 		.conn_info = {
1553 			.ssid_len = cpu_to_le32(conn_info->ssid_len),
1554 			.channel = conn_info->channel,
1555 			.band = conn_info->band,
1556 			.pairwise_cipher = cpu_to_le32(conn_info->pairwise_cipher),
1557 			.auth_mode = cpu_to_le32(conn_info->auth_mode),
1558 		},
1559 	};
1560 	struct iwl_mei *mei;
1561 
1562 	if (conn_info->ssid_len > ARRAY_SIZE(msg.conn_info.ssid))
1563 		return;
1564 
1565 	memcpy(msg.conn_info.ssid, conn_info->ssid, conn_info->ssid_len);
1566 	memcpy(msg.conn_info.bssid, conn_info->bssid, ETH_ALEN);
1567 
1568 	if (colloc_info) {
1569 		msg.colloc_channel = colloc_info->channel;
1570 		msg.colloc_band = colloc_info->channel <= 14 ? 0 : 1;
1571 		memcpy(msg.colloc_bssid, colloc_info->bssid, ETH_ALEN);
1572 	}
1573 
1574 	mutex_lock(&iwl_mei_mutex);
1575 
1576 	if (!iwl_mei_is_connected())
1577 		goto out;
1578 
1579 	mei = mei_cldev_get_drvdata(iwl_mei_global_cldev);
1580 
1581 	if (!mei || !mei->amt_enabled)
1582 		goto out;
1583 
1584 	iwl_mei_send_sap_msg_payload(mei->cldev, &msg.hdr);
1585 
1586 out:
1587 	kfree(iwl_mei_cache.conn_info);
1588 	iwl_mei_cache.conn_info =
1589 		kmemdup(&msg.conn_info, sizeof(msg.conn_info), GFP_KERNEL);
1590 	mutex_unlock(&iwl_mei_mutex);
1591 }
1592 EXPORT_SYMBOL_GPL(iwl_mei_host_associated);
1593 
iwl_mei_host_disassociated(void)1594 void iwl_mei_host_disassociated(void)
1595 {
1596 	struct iwl_mei *mei;
1597 	struct iwl_sap_notif_host_link_down msg = {
1598 		.hdr.type = cpu_to_le16(SAP_MSG_NOTIF_HOST_LINK_DOWN),
1599 		.hdr.len = cpu_to_le16(sizeof(msg) - sizeof(msg.hdr)),
1600 		.type = HOST_LINK_DOWN_TYPE_TEMPORARY,
1601 	};
1602 
1603 	mutex_lock(&iwl_mei_mutex);
1604 
1605 	if (!iwl_mei_is_connected())
1606 		goto out;
1607 
1608 	mei = mei_cldev_get_drvdata(iwl_mei_global_cldev);
1609 
1610 	if (!mei || !mei->amt_enabled)
1611 		goto out;
1612 
1613 	iwl_mei_send_sap_msg_payload(mei->cldev, &msg.hdr);
1614 
1615 out:
1616 	kfree(iwl_mei_cache.conn_info);
1617 	iwl_mei_cache.conn_info = NULL;
1618 	mutex_unlock(&iwl_mei_mutex);
1619 }
1620 EXPORT_SYMBOL_GPL(iwl_mei_host_disassociated);
1621 
iwl_mei_set_rfkill_state(bool hw_rfkill,bool sw_rfkill)1622 void iwl_mei_set_rfkill_state(bool hw_rfkill, bool sw_rfkill)
1623 {
1624 	struct iwl_mei *mei;
1625 	u32 rfkill_state = 0;
1626 	struct iwl_sap_msg_dw msg = {
1627 		.hdr.type = cpu_to_le16(SAP_MSG_NOTIF_RADIO_STATE),
1628 		.hdr.len = cpu_to_le16(sizeof(msg) - sizeof(msg.hdr)),
1629 	};
1630 
1631 	if (!sw_rfkill)
1632 		rfkill_state |= SAP_SW_RFKILL_DEASSERTED;
1633 
1634 	if (!hw_rfkill)
1635 		rfkill_state |= SAP_HW_RFKILL_DEASSERTED;
1636 
1637 	mutex_lock(&iwl_mei_mutex);
1638 
1639 	if (!iwl_mei_is_connected())
1640 		goto out;
1641 
1642 	msg.val = cpu_to_le32(rfkill_state);
1643 
1644 	mei = mei_cldev_get_drvdata(iwl_mei_global_cldev);
1645 
1646 	if (!mei || !mei->amt_enabled)
1647 		goto out;
1648 
1649 	iwl_mei_send_sap_msg_payload(mei->cldev, &msg.hdr);
1650 
1651 out:
1652 	iwl_mei_cache.rf_kill = rfkill_state;
1653 	mutex_unlock(&iwl_mei_mutex);
1654 }
1655 EXPORT_SYMBOL_GPL(iwl_mei_set_rfkill_state);
1656 
iwl_mei_set_nic_info(const u8 * mac_address,const u8 * nvm_address)1657 void iwl_mei_set_nic_info(const u8 *mac_address, const u8 *nvm_address)
1658 {
1659 	struct iwl_mei *mei;
1660 	struct iwl_sap_notif_host_nic_info msg = {
1661 		.hdr.type = cpu_to_le16(SAP_MSG_NOTIF_NIC_INFO),
1662 		.hdr.len = cpu_to_le16(sizeof(msg) - sizeof(msg.hdr)),
1663 	};
1664 
1665 	mutex_lock(&iwl_mei_mutex);
1666 
1667 	if (!iwl_mei_is_connected())
1668 		goto out;
1669 
1670 	ether_addr_copy(msg.mac_address, mac_address);
1671 	ether_addr_copy(msg.nvm_address, nvm_address);
1672 
1673 	mei = mei_cldev_get_drvdata(iwl_mei_global_cldev);
1674 
1675 	if (!mei || !mei->amt_enabled)
1676 		goto out;
1677 
1678 	iwl_mei_send_sap_msg_payload(mei->cldev, &msg.hdr);
1679 
1680 out:
1681 	ether_addr_copy(iwl_mei_cache.mac_address, mac_address);
1682 	ether_addr_copy(iwl_mei_cache.nvm_address, nvm_address);
1683 	mutex_unlock(&iwl_mei_mutex);
1684 }
1685 EXPORT_SYMBOL_GPL(iwl_mei_set_nic_info);
1686 
iwl_mei_set_country_code(u16 mcc)1687 void iwl_mei_set_country_code(u16 mcc)
1688 {
1689 	struct iwl_mei *mei;
1690 	struct iwl_sap_notif_country_code msg = {
1691 		.hdr.type = cpu_to_le16(SAP_MSG_NOTIF_COUNTRY_CODE),
1692 		.hdr.len = cpu_to_le16(sizeof(msg) - sizeof(msg.hdr)),
1693 		.mcc = cpu_to_le16(mcc),
1694 	};
1695 
1696 	mutex_lock(&iwl_mei_mutex);
1697 
1698 	if (!iwl_mei_is_connected())
1699 		goto out;
1700 
1701 	mei = mei_cldev_get_drvdata(iwl_mei_global_cldev);
1702 
1703 	if (!mei || !mei->amt_enabled)
1704 		goto out;
1705 
1706 	iwl_mei_send_sap_msg_payload(mei->cldev, &msg.hdr);
1707 
1708 out:
1709 	iwl_mei_cache.mcc = mcc;
1710 	mutex_unlock(&iwl_mei_mutex);
1711 }
1712 EXPORT_SYMBOL_GPL(iwl_mei_set_country_code);
1713 
iwl_mei_set_power_limit(const __le16 * power_limit)1714 void iwl_mei_set_power_limit(const __le16 *power_limit)
1715 {
1716 	struct iwl_mei *mei;
1717 	struct iwl_sap_notif_sar_limits msg = {
1718 		.hdr.type = cpu_to_le16(SAP_MSG_NOTIF_SAR_LIMITS),
1719 		.hdr.len = cpu_to_le16(sizeof(msg) - sizeof(msg.hdr)),
1720 	};
1721 
1722 	mutex_lock(&iwl_mei_mutex);
1723 
1724 	if (!iwl_mei_is_connected())
1725 		goto out;
1726 
1727 	mei = mei_cldev_get_drvdata(iwl_mei_global_cldev);
1728 
1729 	if (!mei || !mei->amt_enabled)
1730 		goto out;
1731 
1732 	memcpy(msg.sar_chain_info_table, power_limit, sizeof(msg.sar_chain_info_table));
1733 
1734 	iwl_mei_send_sap_msg_payload(mei->cldev, &msg.hdr);
1735 
1736 out:
1737 	kfree(iwl_mei_cache.power_limit);
1738 	iwl_mei_cache.power_limit = kmemdup(power_limit,
1739 					    sizeof(msg.sar_chain_info_table), GFP_KERNEL);
1740 	mutex_unlock(&iwl_mei_mutex);
1741 }
1742 EXPORT_SYMBOL_GPL(iwl_mei_set_power_limit);
1743 
iwl_mei_set_netdev(struct net_device * netdev)1744 void iwl_mei_set_netdev(struct net_device *netdev)
1745 {
1746 	struct iwl_mei *mei;
1747 
1748 	mutex_lock(&iwl_mei_mutex);
1749 
1750 	if (!iwl_mei_is_connected()) {
1751 		rcu_assign_pointer(iwl_mei_cache.netdev, netdev);
1752 		goto out;
1753 	}
1754 
1755 	mei = mei_cldev_get_drvdata(iwl_mei_global_cldev);
1756 
1757 	if (!mei)
1758 		goto out;
1759 
1760 	if (!netdev) {
1761 		struct net_device *dev =
1762 			rcu_dereference_protected(iwl_mei_cache.netdev,
1763 						  lockdep_is_held(&iwl_mei_mutex));
1764 
1765 		if (!dev)
1766 			goto out;
1767 
1768 		netdev_rx_handler_unregister(dev);
1769 	}
1770 
1771 	rcu_assign_pointer(iwl_mei_cache.netdev, netdev);
1772 
1773 	if (netdev && mei->amt_enabled)
1774 		netdev_rx_handler_register(netdev, iwl_mei_rx_handler, mei);
1775 
1776 out:
1777 	mutex_unlock(&iwl_mei_mutex);
1778 }
1779 EXPORT_SYMBOL_GPL(iwl_mei_set_netdev);
1780 
iwl_mei_device_state(bool up)1781 void iwl_mei_device_state(bool up)
1782 {
1783 	struct iwl_mei *mei;
1784 
1785 	mutex_lock(&iwl_mei_mutex);
1786 
1787 	if (!iwl_mei_is_connected())
1788 		goto out;
1789 
1790 	mei = mei_cldev_get_drvdata(iwl_mei_global_cldev);
1791 
1792 	if (!mei)
1793 		goto out;
1794 
1795 	mei->device_down = !up;
1796 
1797 	if (up || !mei->csme_taking_ownership)
1798 		goto out;
1799 
1800 	iwl_mei_send_sap_msg(mei->cldev,
1801 			     SAP_MSG_NOTIF_CSME_OWNERSHIP_CONFIRMED);
1802 	mei->csme_taking_ownership = false;
1803 	schedule_delayed_work(&mei->ownership_dwork,
1804 			      MEI_OWNERSHIP_RETAKE_TIMEOUT_MS);
1805 out:
1806 	mutex_unlock(&iwl_mei_mutex);
1807 }
1808 EXPORT_SYMBOL_GPL(iwl_mei_device_state);
1809 
iwl_mei_register(void * priv,const struct iwl_mei_ops * ops)1810 int iwl_mei_register(void *priv, const struct iwl_mei_ops *ops)
1811 {
1812 	int ret;
1813 
1814 	/*
1815 	 * We must have a non-NULL priv pointer to not crash when there are
1816 	 * multiple WiFi devices.
1817 	 */
1818 	if (!priv)
1819 		return -EINVAL;
1820 
1821 	mutex_lock(&iwl_mei_mutex);
1822 
1823 	/* do not allow registration if someone else already registered */
1824 	if (iwl_mei_cache.priv || iwl_mei_cache.ops) {
1825 		ret = -EBUSY;
1826 		goto out;
1827 	}
1828 
1829 	iwl_mei_cache.priv = priv;
1830 	iwl_mei_cache.ops = ops;
1831 
1832 	if (iwl_mei_global_cldev) {
1833 		struct iwl_mei *mei =
1834 			mei_cldev_get_drvdata(iwl_mei_global_cldev);
1835 
1836 		/* we have already a SAP connection */
1837 		if (iwl_mei_is_connected()) {
1838 			if (mei->amt_enabled)
1839 				iwl_mei_send_sap_msg(mei->cldev,
1840 						     SAP_MSG_NOTIF_WIFIDR_UP);
1841 			ops->rfkill(priv, mei->link_prot_state, false);
1842 		}
1843 	}
1844 	ret = 0;
1845 
1846 out:
1847 	mutex_unlock(&iwl_mei_mutex);
1848 	return ret;
1849 }
1850 EXPORT_SYMBOL_GPL(iwl_mei_register);
1851 
iwl_mei_start_unregister(void)1852 void iwl_mei_start_unregister(void)
1853 {
1854 	mutex_lock(&iwl_mei_mutex);
1855 
1856 	/* At this point, the wifi driver should have removed the netdev */
1857 	if (rcu_access_pointer(iwl_mei_cache.netdev))
1858 		pr_err("Still had a netdev pointer set upon unregister\n");
1859 
1860 	kfree(iwl_mei_cache.conn_info);
1861 	iwl_mei_cache.conn_info = NULL;
1862 	kfree(iwl_mei_cache.power_limit);
1863 	iwl_mei_cache.power_limit = NULL;
1864 	iwl_mei_cache.ops = NULL;
1865 	/* leave iwl_mei_cache.priv non-NULL to prevent any new registration */
1866 
1867 	mutex_unlock(&iwl_mei_mutex);
1868 }
1869 EXPORT_SYMBOL_GPL(iwl_mei_start_unregister);
1870 
iwl_mei_unregister_complete(void)1871 void iwl_mei_unregister_complete(void)
1872 {
1873 	mutex_lock(&iwl_mei_mutex);
1874 
1875 	iwl_mei_cache.priv = NULL;
1876 
1877 	if (iwl_mei_global_cldev) {
1878 		struct iwl_mei *mei =
1879 			mei_cldev_get_drvdata(iwl_mei_global_cldev);
1880 
1881 		if (mei->amt_enabled)
1882 			iwl_mei_send_sap_msg(mei->cldev,
1883 					     SAP_MSG_NOTIF_WIFIDR_DOWN);
1884 		mei->got_ownership = false;
1885 	}
1886 
1887 	mutex_unlock(&iwl_mei_mutex);
1888 }
1889 EXPORT_SYMBOL_GPL(iwl_mei_unregister_complete);
1890 
1891 #if IS_ENABLED(CONFIG_DEBUG_FS)
1892 
1893 static ssize_t
iwl_mei_dbgfs_send_start_message_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)1894 iwl_mei_dbgfs_send_start_message_write(struct file *file,
1895 				       const char __user *user_buf,
1896 				       size_t count, loff_t *ppos)
1897 {
1898 	int ret;
1899 
1900 	mutex_lock(&iwl_mei_mutex);
1901 
1902 	if (!iwl_mei_global_cldev) {
1903 		ret = -ENODEV;
1904 		goto out;
1905 	}
1906 
1907 	ret = iwl_mei_send_start(iwl_mei_global_cldev);
1908 
1909 out:
1910 	mutex_unlock(&iwl_mei_mutex);
1911 	return ret ?: count;
1912 }
1913 
1914 static const struct file_operations iwl_mei_dbgfs_send_start_message_ops = {
1915 	.write = iwl_mei_dbgfs_send_start_message_write,
1916 	.open = simple_open,
1917 	.llseek = default_llseek,
1918 };
1919 
iwl_mei_dbgfs_req_ownership_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)1920 static ssize_t iwl_mei_dbgfs_req_ownership_write(struct file *file,
1921 						 const char __user *user_buf,
1922 						 size_t count, loff_t *ppos)
1923 {
1924 	iwl_mei_get_ownership();
1925 
1926 	return count;
1927 }
1928 
1929 static const struct file_operations iwl_mei_dbgfs_req_ownership_ops = {
1930 	.write = iwl_mei_dbgfs_req_ownership_write,
1931 	.open = simple_open,
1932 	.llseek = default_llseek,
1933 };
1934 
iwl_mei_dbgfs_register(struct iwl_mei * mei)1935 static void iwl_mei_dbgfs_register(struct iwl_mei *mei)
1936 {
1937 	mei->dbgfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
1938 
1939 	if (!mei->dbgfs_dir)
1940 		return;
1941 
1942 	debugfs_create_ulong("status", S_IRUSR,
1943 			     mei->dbgfs_dir, &iwl_mei_status);
1944 	debugfs_create_file("send_start_message", S_IWUSR, mei->dbgfs_dir,
1945 			    mei, &iwl_mei_dbgfs_send_start_message_ops);
1946 	debugfs_create_file("req_ownership", S_IWUSR, mei->dbgfs_dir,
1947 			    mei, &iwl_mei_dbgfs_req_ownership_ops);
1948 }
1949 
iwl_mei_dbgfs_unregister(struct iwl_mei * mei)1950 static void iwl_mei_dbgfs_unregister(struct iwl_mei *mei)
1951 {
1952 	debugfs_remove_recursive(mei->dbgfs_dir);
1953 	mei->dbgfs_dir = NULL;
1954 }
1955 
1956 #else
1957 
iwl_mei_dbgfs_register(struct iwl_mei * mei)1958 static void iwl_mei_dbgfs_register(struct iwl_mei *mei) {}
iwl_mei_dbgfs_unregister(struct iwl_mei * mei)1959 static void iwl_mei_dbgfs_unregister(struct iwl_mei *mei) {}
1960 
1961 #endif /* CONFIG_DEBUG_FS */
1962 
iwl_mei_ownership_dwork(struct work_struct * wk)1963 static void iwl_mei_ownership_dwork(struct work_struct *wk)
1964 {
1965 	iwl_mei_get_ownership();
1966 }
1967 
1968 #define ALLOC_SHARED_MEM_RETRY_MAX_NUM	3
1969 
1970 /*
1971  * iwl_mei_probe - the probe function called by the mei bus enumeration
1972  *
1973  * This allocates the data needed by iwlmei and sets a pointer to this data
1974  * into the mei_cl_device's drvdata.
1975  * It starts the SAP protocol by sending the SAP_ME_MSG_START without
1976  * waiting for the answer. The answer will be caught later by the Rx callback.
1977  */
iwl_mei_probe(struct mei_cl_device * cldev,const struct mei_cl_device_id * id)1978 static int iwl_mei_probe(struct mei_cl_device *cldev,
1979 			 const struct mei_cl_device_id *id)
1980 {
1981 	int alloc_retry = ALLOC_SHARED_MEM_RETRY_MAX_NUM;
1982 	struct iwl_mei *mei;
1983 	int ret;
1984 
1985 	mei = devm_kzalloc(&cldev->dev, sizeof(*mei), GFP_KERNEL);
1986 	if (!mei)
1987 		return -ENOMEM;
1988 
1989 	init_waitqueue_head(&mei->get_nvm_wq);
1990 	INIT_WORK(&mei->send_csa_msg_wk, iwl_mei_send_csa_msg_wk);
1991 	INIT_DELAYED_WORK(&mei->csa_throttle_end_wk,
1992 			  iwl_mei_csa_throttle_end_wk);
1993 	init_waitqueue_head(&mei->get_ownership_wq);
1994 	init_waitqueue_head(&mei->pldr_wq);
1995 	spin_lock_init(&mei->data_q_lock);
1996 	INIT_WORK(&mei->netdev_work, iwl_mei_netdev_work);
1997 	INIT_DELAYED_WORK(&mei->ownership_dwork, iwl_mei_ownership_dwork);
1998 
1999 	mei_cldev_set_drvdata(cldev, mei);
2000 	mei->cldev = cldev;
2001 	mei->device_down = true;
2002 
2003 	do {
2004 		ret = iwl_mei_alloc_shared_mem(cldev);
2005 		if (!ret)
2006 			break;
2007 		/*
2008 		 * The CSME firmware needs to boot the internal WLAN client.
2009 		 * This can take time in certain configurations (usually
2010 		 * upon resume and when the whole CSME firmware is shut down
2011 		 * during suspend).
2012 		 *
2013 		 * Wait a bit before retrying and hope we'll succeed next time.
2014 		 */
2015 
2016 		dev_dbg(&cldev->dev,
2017 			"Couldn't allocate the shared memory: %d, attempt %d / %d\n",
2018 			ret, alloc_retry, ALLOC_SHARED_MEM_RETRY_MAX_NUM);
2019 		msleep(100);
2020 		alloc_retry--;
2021 	} while (alloc_retry);
2022 
2023 	if (ret) {
2024 		dev_err(&cldev->dev, "Couldn't allocate the shared memory: %d\n",
2025 			ret);
2026 		goto free;
2027 	}
2028 
2029 	iwl_mei_init_shared_mem(mei);
2030 
2031 	ret = iwl_mei_enable(cldev);
2032 	if (ret)
2033 		goto free_shared_mem;
2034 
2035 	iwl_mei_dbgfs_register(mei);
2036 
2037 	/*
2038 	 * We now have a Rx function in place, start the SAP protocol
2039 	 * we expect to get the SAP_ME_MSG_START_OK response later on.
2040 	 */
2041 	mutex_lock(&iwl_mei_mutex);
2042 	ret = iwl_mei_send_start(cldev);
2043 	mutex_unlock(&iwl_mei_mutex);
2044 	if (ret)
2045 		goto debugfs_unregister;
2046 
2047 	/* must be last */
2048 	iwl_mei_global_cldev = cldev;
2049 
2050 	return 0;
2051 
2052 debugfs_unregister:
2053 	iwl_mei_dbgfs_unregister(mei);
2054 	mei_cldev_disable(cldev);
2055 free_shared_mem:
2056 	iwl_mei_free_shared_mem(cldev);
2057 free:
2058 	mei_cldev_set_drvdata(cldev, NULL);
2059 	devm_kfree(&cldev->dev, mei);
2060 
2061 	return ret;
2062 }
2063 
2064 #define SEND_SAP_MAX_WAIT_ITERATION 10
2065 #define IWLMEI_DEVICE_DOWN_WAIT_ITERATION 50
2066 
iwl_mei_remove(struct mei_cl_device * cldev)2067 static void iwl_mei_remove(struct mei_cl_device *cldev)
2068 {
2069 	struct iwl_mei *mei = mei_cldev_get_drvdata(cldev);
2070 	int i;
2071 
2072 	/*
2073 	 * We are being removed while the bus is active, it means we are
2074 	 * going to suspend/ shutdown, so the NIC will disappear.
2075 	 */
2076 	if (mei_cldev_enabled(cldev) && iwl_mei_cache.ops) {
2077 		unsigned int iter = IWLMEI_DEVICE_DOWN_WAIT_ITERATION;
2078 		bool down = false;
2079 
2080 		/*
2081 		 * In case of suspend, wait for the mac to stop and don't remove
2082 		 * the interface. This will allow the interface to come back
2083 		 * on resume.
2084 		 */
2085 		while (!down && iter--) {
2086 			mdelay(1);
2087 
2088 			mutex_lock(&iwl_mei_mutex);
2089 			down = mei->device_down;
2090 			mutex_unlock(&iwl_mei_mutex);
2091 		}
2092 
2093 		if (!down)
2094 			iwl_mei_cache.ops->nic_stolen(iwl_mei_cache.priv);
2095 	}
2096 
2097 	if (rcu_access_pointer(iwl_mei_cache.netdev)) {
2098 		struct net_device *dev;
2099 
2100 		/*
2101 		 * First take rtnl and only then the mutex to avoid an ABBA
2102 		 * with iwl_mei_set_netdev()
2103 		 */
2104 		rtnl_lock();
2105 		mutex_lock(&iwl_mei_mutex);
2106 
2107 		/*
2108 		 * If we are suspending and the wifi driver hasn't removed it's netdev
2109 		 * yet, do it now. In any case, don't change the cache.netdev pointer.
2110 		 */
2111 		dev = rcu_dereference_protected(iwl_mei_cache.netdev,
2112 						lockdep_is_held(&iwl_mei_mutex));
2113 
2114 		netdev_rx_handler_unregister(dev);
2115 		mutex_unlock(&iwl_mei_mutex);
2116 		rtnl_unlock();
2117 	}
2118 
2119 	mutex_lock(&iwl_mei_mutex);
2120 
2121 	/* Tell CSME that we are going down so that it won't access the
2122 	 * memory anymore, make sure this message goes through immediately.
2123 	 */
2124 	mei->csa_throttled = false;
2125 	iwl_mei_send_sap_msg(mei->cldev,
2126 			     SAP_MSG_NOTIF_HOST_GOES_DOWN);
2127 
2128 	for (i = 0; i < SEND_SAP_MAX_WAIT_ITERATION; i++) {
2129 		if (!iwl_mei_host_to_me_data_pending(mei))
2130 			break;
2131 
2132 		msleep(20);
2133 	}
2134 
2135 	/* If we couldn't make sure that CSME saw the HOST_GOES_DOWN
2136 	 * message, it means that it will probably keep reading memory
2137 	 * that we are going to unmap and free, expect IOMMU error
2138 	 * messages.
2139 	 */
2140 	if (i == SEND_SAP_MAX_WAIT_ITERATION)
2141 		dev_err(&mei->cldev->dev,
2142 			"Couldn't get ACK from CSME on HOST_GOES_DOWN message\n");
2143 
2144 	mutex_unlock(&iwl_mei_mutex);
2145 
2146 	/*
2147 	 * This looks strange, but this lock is taken here to make sure that
2148 	 * iwl_mei_add_data_to_ring called from the Tx path sees that we
2149 	 * clear the IWL_MEI_STATUS_SAP_CONNECTED bit.
2150 	 * Rx isn't a problem because the rx_handler can't be called after
2151 	 * having been unregistered.
2152 	 */
2153 	spin_lock_bh(&mei->data_q_lock);
2154 	clear_bit(IWL_MEI_STATUS_SAP_CONNECTED, &iwl_mei_status);
2155 	spin_unlock_bh(&mei->data_q_lock);
2156 
2157 	if (iwl_mei_cache.ops)
2158 		iwl_mei_cache.ops->rfkill(iwl_mei_cache.priv, false, false);
2159 
2160 	/*
2161 	 * mei_cldev_disable will return only after all the MEI Rx is done.
2162 	 * It must be called when iwl_mei_mutex is *not* held, since it waits
2163 	 * for our Rx handler to complete.
2164 	 * After it returns, no new Rx will start.
2165 	 */
2166 	mei_cldev_disable(cldev);
2167 
2168 	/*
2169 	 * Since the netdev was already removed and the netdev's removal
2170 	 * includes a call to synchronize_net() so that we know there won't be
2171 	 * any new Rx that will trigger the following workers.
2172 	 */
2173 	cancel_work_sync(&mei->send_csa_msg_wk);
2174 	cancel_delayed_work_sync(&mei->csa_throttle_end_wk);
2175 	cancel_work_sync(&mei->netdev_work);
2176 	cancel_delayed_work_sync(&mei->ownership_dwork);
2177 
2178 	/*
2179 	 * If someone waits for the ownership, let him know that we are going
2180 	 * down and that we are not connected anymore. He'll be able to take
2181 	 * the device.
2182 	 */
2183 	wake_up_all(&mei->get_ownership_wq);
2184 	wake_up_all(&mei->pldr_wq);
2185 
2186 	mutex_lock(&iwl_mei_mutex);
2187 
2188 	iwl_mei_global_cldev = NULL;
2189 
2190 	wake_up_all(&mei->get_nvm_wq);
2191 
2192 	iwl_mei_free_shared_mem(cldev);
2193 
2194 	iwl_mei_dbgfs_unregister(mei);
2195 
2196 	mei_cldev_set_drvdata(cldev, NULL);
2197 
2198 	kfree(mei->nvm);
2199 
2200 	kfree(rcu_access_pointer(mei->filters));
2201 
2202 	devm_kfree(&cldev->dev, mei);
2203 
2204 	mutex_unlock(&iwl_mei_mutex);
2205 }
2206 
2207 static const struct mei_cl_device_id iwl_mei_tbl[] = {
2208 	{
2209 		.name = KBUILD_MODNAME,
2210 		.uuid = MEI_WLAN_UUID,
2211 		.version = MEI_CL_VERSION_ANY,
2212 	},
2213 
2214 	/* required last entry */
2215 	{ }
2216 };
2217 
2218 /*
2219  * Do not export the device table because this module is loaded by
2220  * iwlwifi's dependency.
2221  */
2222 
2223 static struct mei_cl_driver iwl_mei_cl_driver = {
2224 	.id_table = iwl_mei_tbl,
2225 	.name = KBUILD_MODNAME,
2226 	.probe = iwl_mei_probe,
2227 	.remove = iwl_mei_remove,
2228 };
2229 
2230 module_mei_cl_driver(iwl_mei_cl_driver);
2231