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