1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * System Control and Management Interface (SCMI) Raw mode support 4 * 5 * Copyright (C) 2022 ARM Ltd. 6 */ 7 /** 8 * DOC: Theory of operation 9 * 10 * When enabled the SCMI Raw mode support exposes a userspace API which allows 11 * to send and receive SCMI commands, replies and notifications from a user 12 * application through injection and snooping of bare SCMI messages in binary 13 * little-endian format. 14 * 15 * Such injected SCMI transactions will then be routed through the SCMI core 16 * stack towards the SCMI backend server using whatever SCMI transport is 17 * currently configured on the system under test. 18 * 19 * It is meant to help in running any sort of SCMI backend server testing, no 20 * matter where the server is placed, as long as it is normally reachable via 21 * the transport configured on the system. 22 * 23 * It is activated by a Kernel configuration option since it is NOT meant to 24 * be used in production but only during development and in CI deployments. 25 * 26 * In order to avoid possible interferences between the SCMI Raw transactions 27 * originated from a test-suite and the normal operations of the SCMI drivers, 28 * when Raw mode is enabled, by default, all the regular SCMI drivers are 29 * inhibited, unless CONFIG_ARM_SCMI_RAW_MODE_SUPPORT_COEX is enabled: in this 30 * latter case the regular SCMI stack drivers will be loaded as usual and it is 31 * up to the user of this interface to take care of manually inhibiting the 32 * regular SCMI drivers in order to avoid interferences during the test runs. 33 * 34 * The exposed API is as follows. 35 * 36 * All SCMI Raw entries are rooted under a common top /raw debugfs top directory 37 * which in turn is rooted under the corresponding underlying SCMI instance. 38 * 39 * /sys/kernel/debug/scmi/ 40 * `-- 0 41 * |-- atomic_threshold_us 42 * |-- instance_name 43 * |-- raw 44 * | |-- channels 45 * | | |-- 0x10 46 * | | | |-- message 47 * | | | `-- message_async 48 * | | `-- 0x13 49 * | | |-- message 50 * | | `-- message_async 51 * | |-- errors 52 * | |-- message 53 * | |-- message_async 54 * | |-- notification 55 * | `-- reset 56 * `-- transport 57 * |-- is_atomic 58 * |-- max_msg_size 59 * |-- max_rx_timeout_ms 60 * |-- rx_max_msg 61 * |-- tx_max_msg 62 * `-- type 63 * 64 * where: 65 * 66 * - errors: used to read back timed-out and unexpected replies 67 * - message*: used to send sync/async commands and read back immediate and 68 * delayed reponses (if any) 69 * - notification: used to read any notification being emitted by the system 70 * (if previously enabled by the user app) 71 * - reset: used to flush the queues of messages (of any kind) still pending 72 * to be read; this is useful at test-suite start/stop to get 73 * rid of any unread messages from the previous run. 74 * 75 * with the per-channel entries rooted at /channels being present only on a 76 * system where multiple transport channels have been configured. 77 * 78 * Such per-channel entries can be used to explicitly choose a specific channel 79 * for SCMI bare message injection, in contrast with the general entries above 80 * where, instead, the selection of the proper channel to use is automatically 81 * performed based the protocol embedded in the injected message and on how the 82 * transport is configured on the system. 83 * 84 * Note that other common general entries are available under transport/ to let 85 * the user applications properly make up their expectations in terms of 86 * timeouts and message characteristics. 87 * 88 * Each write to the message* entries causes one command request to be built 89 * and sent while the replies or delayed response are read back from those same 90 * entries one message at time (receiving an EOF at each message boundary). 91 * 92 * The user application running the test is in charge of handling timeouts 93 * on replies and properly choosing SCMI sequence numbers for the outgoing 94 * requests (using the same sequence number is supported but discouraged). 95 * 96 * Injection of multiple in-flight requests is supported as long as the user 97 * application uses properly distinct sequence numbers for concurrent requests 98 * and takes care to properly manage all the related issues about concurrency 99 * and command/reply pairing. Keep in mind that, anyway, the real level of 100 * parallelism attainable in such scenario is dependent on the characteristics 101 * of the underlying transport being used. 102 * 103 * Since the SCMI core regular stack is partially used to deliver and collect 104 * the messages, late replies arrived after timeouts and any other sort of 105 * unexpected message can be identified by the SCMI core as usual and they will 106 * be reported as messages under "errors" for later analysis. 107 */ 108 109 #include <linux/bitmap.h> 110 #include <linux/debugfs.h> 111 #include <linux/delay.h> 112 #include <linux/device.h> 113 #include <linux/export.h> 114 #include <linux/io.h> 115 #include <linux/kernel.h> 116 #include <linux/fs.h> 117 #include <linux/list.h> 118 #include <linux/module.h> 119 #include <linux/poll.h> 120 #include <linux/of.h> 121 #include <linux/slab.h> 122 #include <linux/xarray.h> 123 124 #include "common.h" 125 126 #include "raw_mode.h" 127 128 #include <trace/events/scmi.h> 129 130 #define SCMI_XFER_RAW_MAX_RETRIES 10 131 132 /** 133 * struct scmi_raw_queue - Generic Raw queue descriptor 134 * 135 * @free_bufs: A freelists listhead used to keep unused raw buffers 136 * @free_bufs_lock: Spinlock used to protect access to @free_bufs 137 * @msg_q: A listhead to a queue of snooped messages waiting to be read out 138 * @msg_q_lock: Spinlock used to protect access to @msg_q 139 * @wq: A waitqueue used to wait and poll on related @msg_q 140 */ 141 struct scmi_raw_queue { 142 struct list_head free_bufs; 143 /* Protect free_bufs[] lists */ 144 spinlock_t free_bufs_lock; 145 struct list_head msg_q; 146 /* Protect msg_q[] lists */ 147 spinlock_t msg_q_lock; 148 wait_queue_head_t wq; 149 }; 150 151 /** 152 * struct scmi_raw_mode_info - Structure holding SCMI Raw instance data 153 * 154 * @id: Sequential Raw instance ID. 155 * @handle: Pointer to SCMI entity handle to use 156 * @desc: Pointer to the transport descriptor to use 157 * @tx_max_msg: Maximum number of concurrent TX in-flight messages 158 * @q: An array of Raw queue descriptors 159 * @chans_q: An XArray mapping optional additional per-channel queues 160 * @free_waiters: Head of freelist for unused waiters 161 * @free_mtx: A mutex to protect the waiters freelist 162 * @active_waiters: Head of list for currently active and used waiters 163 * @active_mtx: A mutex to protect the active waiters list 164 * @waiters_work: A work descriptor to be used with the workqueue machinery 165 * @wait_wq: A workqueue reference to the created workqueue 166 * @dentry: Top debugfs root dentry for SCMI Raw 167 * @gid: A group ID used for devres accounting 168 * 169 * Note that this descriptor is passed back to the core after SCMI Raw is 170 * initialized as an opaque handle to use by subsequent SCMI Raw call hooks. 171 * 172 */ 173 struct scmi_raw_mode_info { 174 unsigned int id; 175 const struct scmi_handle *handle; 176 const struct scmi_desc *desc; 177 int tx_max_msg; 178 struct scmi_raw_queue *q[SCMI_RAW_MAX_QUEUE]; 179 struct xarray chans_q; 180 struct list_head free_waiters; 181 /* Protect free_waiters list */ 182 struct mutex free_mtx; 183 struct list_head active_waiters; 184 /* Protect active_waiters list */ 185 struct mutex active_mtx; 186 struct work_struct waiters_work; 187 struct workqueue_struct *wait_wq; 188 struct dentry *dentry; 189 void *gid; 190 }; 191 192 /** 193 * struct scmi_xfer_raw_waiter - Structure to describe an xfer to be waited for 194 * 195 * @start_jiffies: The timestamp in jiffies of when this structure was queued. 196 * @cinfo: A reference to the channel to use for this transaction 197 * @xfer: A reference to the xfer to be waited for 198 * @async_response: A completion to be, optionally, used for async waits: it 199 * will be setup by @scmi_do_xfer_raw_start, if needed, to be 200 * pointed at by xfer->async_done. 201 * @async: True if @async_response was armed on @xfer, i.e. if a delayed 202 * response has to be waited for. 203 * @node: A list node. 204 */ 205 struct scmi_xfer_raw_waiter { 206 unsigned long start_jiffies; 207 struct scmi_chan_info *cinfo; 208 struct scmi_xfer *xfer; 209 struct completion async_response; 210 bool async; 211 struct list_head node; 212 }; 213 214 /** 215 * struct scmi_raw_buffer - Structure to hold a full SCMI message 216 * 217 * @max_len: The maximum allowed message size (header included) that can be 218 * stored into @msg 219 * @msg: A message buffer used to collect a full message grabbed from an xfer. 220 * @node: A list node. 221 */ 222 struct scmi_raw_buffer { 223 size_t max_len; 224 struct scmi_msg msg; 225 struct list_head node; 226 }; 227 228 /** 229 * struct scmi_dbg_raw_data - Structure holding data needed by the debugfs 230 * layer 231 * 232 * @chan_id: The preferred channel to use: if zero the channel is automatically 233 * selected based on protocol. 234 * @raw: A reference to the Raw instance. 235 * @tx: A message buffer used to collect TX message on write. 236 * @tx_size: The effective size of the TX message. 237 * @tx_req_size: The final expected size of the complete TX message. 238 * @rx: A message buffer to collect RX message on read. 239 * @rx_size: The effective size of the RX message. 240 */ 241 struct scmi_dbg_raw_data { 242 u8 chan_id; 243 struct scmi_raw_mode_info *raw; 244 struct scmi_msg tx; 245 size_t tx_size; 246 size_t tx_req_size; 247 struct scmi_msg rx; 248 size_t rx_size; 249 }; 250 251 static struct scmi_raw_queue * 252 scmi_raw_queue_select(struct scmi_raw_mode_info *raw, unsigned int idx, 253 unsigned int chan_id) 254 { 255 if (!chan_id) 256 return raw->q[idx]; 257 258 return xa_load(&raw->chans_q, chan_id); 259 } 260 261 static struct scmi_raw_buffer *scmi_raw_buffer_get(struct scmi_raw_queue *q) 262 { 263 unsigned long flags; 264 struct scmi_raw_buffer *rb = NULL; 265 struct list_head *head = &q->free_bufs; 266 267 spin_lock_irqsave(&q->free_bufs_lock, flags); 268 if (!list_empty(head)) { 269 rb = list_first_entry(head, struct scmi_raw_buffer, node); 270 list_del_init(&rb->node); 271 } 272 spin_unlock_irqrestore(&q->free_bufs_lock, flags); 273 274 return rb; 275 } 276 277 static void scmi_raw_buffer_put(struct scmi_raw_queue *q, 278 struct scmi_raw_buffer *rb) 279 { 280 unsigned long flags; 281 282 /* Reset to full buffer length */ 283 rb->msg.len = rb->max_len; 284 285 spin_lock_irqsave(&q->free_bufs_lock, flags); 286 list_add_tail(&rb->node, &q->free_bufs); 287 spin_unlock_irqrestore(&q->free_bufs_lock, flags); 288 } 289 290 static void scmi_raw_buffer_enqueue(struct scmi_raw_queue *q, 291 struct scmi_raw_buffer *rb) 292 { 293 unsigned long flags; 294 295 spin_lock_irqsave(&q->msg_q_lock, flags); 296 list_add_tail(&rb->node, &q->msg_q); 297 spin_unlock_irqrestore(&q->msg_q_lock, flags); 298 299 wake_up_interruptible(&q->wq); 300 } 301 302 static struct scmi_raw_buffer* 303 scmi_raw_buffer_dequeue_unlocked(struct scmi_raw_queue *q) 304 { 305 struct scmi_raw_buffer *rb = NULL; 306 307 if (!list_empty(&q->msg_q)) { 308 rb = list_first_entry(&q->msg_q, struct scmi_raw_buffer, node); 309 list_del_init(&rb->node); 310 } 311 312 return rb; 313 } 314 315 static struct scmi_raw_buffer *scmi_raw_buffer_dequeue(struct scmi_raw_queue *q) 316 { 317 unsigned long flags; 318 struct scmi_raw_buffer *rb; 319 320 spin_lock_irqsave(&q->msg_q_lock, flags); 321 rb = scmi_raw_buffer_dequeue_unlocked(q); 322 spin_unlock_irqrestore(&q->msg_q_lock, flags); 323 324 return rb; 325 } 326 327 static void scmi_raw_buffer_queue_flush(struct scmi_raw_queue *q) 328 { 329 struct scmi_raw_buffer *rb; 330 331 do { 332 rb = scmi_raw_buffer_dequeue(q); 333 if (rb) 334 scmi_raw_buffer_put(q, rb); 335 } while (rb); 336 } 337 338 static struct scmi_xfer_raw_waiter * 339 scmi_xfer_raw_waiter_get(struct scmi_raw_mode_info *raw, struct scmi_xfer *xfer, 340 struct scmi_chan_info *cinfo, bool async) 341 { 342 struct scmi_xfer_raw_waiter *rw = NULL; 343 344 mutex_lock(&raw->free_mtx); 345 if (!list_empty(&raw->free_waiters)) { 346 rw = list_first_entry(&raw->free_waiters, 347 struct scmi_xfer_raw_waiter, node); 348 list_del_init(&rw->node); 349 350 if (async) { 351 reinit_completion(&rw->async_response); 352 scmi_xfer_async_response_arm(xfer, &rw->async_response); 353 } 354 355 rw->async = async; 356 rw->cinfo = cinfo; 357 rw->xfer = xfer; 358 } 359 mutex_unlock(&raw->free_mtx); 360 361 return rw; 362 } 363 364 static void scmi_xfer_raw_waiter_put(struct scmi_raw_mode_info *raw, 365 struct scmi_xfer_raw_waiter *rw) 366 { 367 if (rw->xfer) { 368 /* 369 * Disarm the delayed response before this waiter, and its 370 * embedded completion, can be picked up again for a new 371 * transaction: a delayed response received late, after the 372 * related wait timed out, must not signal a completion which 373 * has been in the meantime re-armed on a different xfer. 374 */ 375 scmi_xfer_async_response_disarm(rw->xfer); 376 rw->xfer = NULL; 377 rw->async = false; 378 } 379 380 mutex_lock(&raw->free_mtx); 381 list_add_tail(&rw->node, &raw->free_waiters); 382 mutex_unlock(&raw->free_mtx); 383 } 384 385 static void scmi_xfer_raw_waiter_enqueue(struct scmi_raw_mode_info *raw, 386 struct scmi_xfer_raw_waiter *rw) 387 { 388 /* A timestamp for the deferred worker to know how much this has aged */ 389 rw->start_jiffies = jiffies; 390 391 trace_scmi_xfer_response_wait(rw->xfer->transfer_id, rw->xfer->hdr.id, 392 rw->xfer->hdr.protocol_id, 393 rw->xfer->hdr.seq, 394 raw->desc->max_rx_timeout_ms, 395 rw->xfer->hdr.poll_completion); 396 397 mutex_lock(&raw->active_mtx); 398 list_add_tail(&rw->node, &raw->active_waiters); 399 mutex_unlock(&raw->active_mtx); 400 401 /* kick waiter work */ 402 queue_work(raw->wait_wq, &raw->waiters_work); 403 } 404 405 static struct scmi_xfer_raw_waiter * 406 scmi_xfer_raw_waiter_dequeue(struct scmi_raw_mode_info *raw) 407 { 408 struct scmi_xfer_raw_waiter *rw = NULL; 409 410 mutex_lock(&raw->active_mtx); 411 if (!list_empty(&raw->active_waiters)) { 412 rw = list_first_entry(&raw->active_waiters, 413 struct scmi_xfer_raw_waiter, node); 414 list_del_init(&rw->node); 415 } 416 mutex_unlock(&raw->active_mtx); 417 418 return rw; 419 } 420 421 /** 422 * scmi_xfer_raw_worker - Work function to wait for Raw xfers completions 423 * 424 * @work: A reference to the work. 425 * 426 * In SCMI Raw mode, once a user-provided injected SCMI message is sent, we 427 * cannot wait to receive its response (if any) in the context of the injection 428 * routines so as not to leave the userspace write syscall, which delivered the 429 * SCMI message to send, pending till eventually a reply is received. 430 * Userspace should and will poll/wait instead on the read syscalls which will 431 * be in charge of reading a received reply (if any). 432 * 433 * Even though reply messages are collected and reported into the SCMI Raw layer 434 * on the RX path, nonetheless we have to properly wait for their completion as 435 * usual (and async_completion too if needed) in order to properly release the 436 * xfer structure at the end: to do this out of the context of the write/send 437 * these waiting jobs are delegated to this deferred worker. 438 * 439 * Any sent xfer, to be waited for, is timestamped and queued for later 440 * consumption by this worker: queue aging is accounted for while choosing a 441 * timeout for the completion, BUT we do not really care here if we end up 442 * accidentally waiting for a bit too long. 443 */ 444 static void scmi_xfer_raw_worker(struct work_struct *work) 445 { 446 struct scmi_raw_mode_info *raw; 447 struct device *dev; 448 unsigned long max_tmo; 449 450 raw = container_of(work, struct scmi_raw_mode_info, waiters_work); 451 dev = raw->handle->dev; 452 max_tmo = msecs_to_jiffies(raw->desc->max_rx_timeout_ms); 453 454 do { 455 int ret = 0; 456 unsigned int timeout_ms; 457 unsigned long aging; 458 struct scmi_xfer *xfer; 459 struct scmi_xfer_raw_waiter *rw; 460 struct scmi_chan_info *cinfo; 461 462 rw = scmi_xfer_raw_waiter_dequeue(raw); 463 if (!rw) 464 return; 465 466 cinfo = rw->cinfo; 467 xfer = rw->xfer; 468 /* 469 * Waiters are queued by wait-deadline at the end, so some of 470 * them could have been already expired when processed, BUT we 471 * have to check the completion status anyway just in case a 472 * virtually expired (aged) transaction was indeed completed 473 * fine and we'll have to wait for the asynchronous part (if 474 * any): for this reason a 1 ms timeout is used for already 475 * expired/aged xfers. 476 */ 477 aging = jiffies - rw->start_jiffies; 478 timeout_ms = max_tmo > aging ? 479 jiffies_to_msecs(max_tmo - aging) : 1; 480 481 ret = scmi_xfer_raw_wait_for_message_response(cinfo, xfer, 482 timeout_ms); 483 if (!ret && xfer->hdr.status) 484 ret = scmi_to_linux_errno(xfer->hdr.status); 485 486 if (raw->desc->ops->mark_txdone) 487 raw->desc->ops->mark_txdone(rw->cinfo, ret, xfer); 488 489 trace_scmi_xfer_end(xfer->transfer_id, xfer->hdr.id, 490 xfer->hdr.protocol_id, xfer->hdr.seq, 491 ret, scmi_inflight_count(raw->handle)); 492 493 /* Wait also for an async delayed response if needed */ 494 if (!ret && rw->async) { 495 unsigned long tmo = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT); 496 497 if (!wait_for_completion_timeout(&rw->async_response, tmo)) 498 dev_err(dev, 499 "timed out in RAW delayed resp - HDR:%08X\n", 500 pack_scmi_header(&xfer->hdr)); 501 } 502 503 /* 504 * Release the waiter first: this disarms the delayed response 505 * while we still hold a reference on the xfer, so that the xfer 506 * cannot be recycled by a new transaction while it still points 507 * at this waiter's completion. 508 */ 509 scmi_xfer_raw_waiter_put(raw, rw); 510 scmi_xfer_raw_put(raw->handle, xfer); 511 } while (1); 512 } 513 514 static void scmi_xfer_raw_reset(struct scmi_raw_mode_info *raw) 515 { 516 int i; 517 518 dev_info(raw->handle->dev, "Resetting SCMI Raw stack.\n"); 519 520 for (i = 0; i < SCMI_RAW_MAX_QUEUE; i++) 521 scmi_raw_buffer_queue_flush(raw->q[i]); 522 } 523 524 /** 525 * scmi_xfer_raw_get_init - An helper to build a valid xfer from the provided 526 * bare SCMI message. 527 * 528 * @raw: A reference to the Raw instance. 529 * @buf: A buffer containing the whole SCMI message to send (including the 530 * header) in little-endian binary formmat. 531 * @len: Length of the message in @buf. 532 * @p: A pointer to return the initialized Raw xfer. 533 * 534 * After an xfer is picked from the TX pool and filled in with the message 535 * content, the xfer is registered as pending with the core in the usual way 536 * using the original sequence number provided by the user with the message. 537 * 538 * Note that, in case the testing user application is NOT using distinct 539 * sequence-numbers between successive SCMI messages such registration could 540 * fail temporarily if the previous message, using the same sequence number, 541 * had still not released; in such a case we just wait and retry. 542 * 543 * Return: 0 on Success 544 */ 545 static int scmi_xfer_raw_get_init(struct scmi_raw_mode_info *raw, void *buf, 546 size_t len, struct scmi_xfer **p) 547 { 548 u32 msg_hdr; 549 size_t tx_size; 550 struct scmi_xfer *xfer; 551 int ret, retry = SCMI_XFER_RAW_MAX_RETRIES; 552 struct device *dev = raw->handle->dev; 553 554 if (!buf || len < sizeof(u32)) 555 return -EINVAL; 556 557 tx_size = len - sizeof(u32); 558 /* Ensure we have sane transfer sizes */ 559 if (tx_size > raw->desc->max_msg_size) 560 return -ERANGE; 561 562 xfer = scmi_xfer_raw_get(raw->handle); 563 if (IS_ERR(xfer)) { 564 dev_warn(dev, "RAW - Cannot get a free RAW xfer !\n"); 565 return PTR_ERR(xfer); 566 } 567 568 /* Build xfer from the provided SCMI bare LE message */ 569 msg_hdr = le32_to_cpu(*((__le32 *)buf)); 570 unpack_scmi_header(msg_hdr, &xfer->hdr); 571 xfer->hdr.seq = (u16)MSG_XTRACT_TOKEN(msg_hdr); 572 /* Polling not supported */ 573 xfer->hdr.poll_completion = false; 574 xfer->hdr.status = SCMI_SUCCESS; 575 xfer->tx.len = tx_size; 576 xfer->rx.len = raw->desc->max_msg_size; 577 /* Clear the whole TX buffer */ 578 memset(xfer->tx.buf, 0x00, raw->desc->max_msg_size); 579 if (xfer->tx.len) 580 memcpy(xfer->tx.buf, (u8 *)buf + sizeof(msg_hdr), xfer->tx.len); 581 *p = xfer; 582 583 /* 584 * In flight registration can temporarily fail in case of Raw messages 585 * if the user injects messages without using monotonically increasing 586 * sequence numbers since, in Raw mode, the xfer (and the token) is 587 * finally released later by a deferred worker. Just retry for a while. 588 */ 589 do { 590 ret = scmi_xfer_raw_inflight_register(raw->handle, xfer); 591 if (ret) { 592 dev_dbg(dev, 593 "...retrying[%d] inflight registration\n", 594 retry); 595 msleep(raw->desc->max_rx_timeout_ms / 596 SCMI_XFER_RAW_MAX_RETRIES); 597 } 598 } while (ret && --retry); 599 600 if (ret) { 601 dev_warn(dev, 602 "RAW - Could NOT register xfer %d in-flight HDR:0x%08X\n", 603 xfer->hdr.seq, msg_hdr); 604 scmi_xfer_raw_put(raw->handle, xfer); 605 } 606 607 return ret; 608 } 609 610 /** 611 * scmi_do_xfer_raw_start - An helper to send a valid raw xfer 612 * 613 * @raw: A reference to the Raw instance. 614 * @xfer: The xfer to send 615 * @chan_id: The channel ID to use, if zero the channels is automatically 616 * selected based on the protocol used. 617 * @async: A flag stating if an asynchronous command is required. 618 * 619 * This function send a previously built raw xfer using an appropriate channel 620 * and queues the related waiting work. 621 * 622 * Note that we need to know explicitly if the required command is meant to be 623 * asynchronous in kind since we have to properly setup the waiter. 624 * (and deducing this from the payload is weak and do not scale given there is 625 * NOT a common header-flag stating if the command is asynchronous or not) 626 * 627 * Return: 0 on Success 628 */ 629 static int scmi_do_xfer_raw_start(struct scmi_raw_mode_info *raw, 630 struct scmi_xfer *xfer, u8 chan_id, 631 bool async) 632 { 633 int ret; 634 struct scmi_chan_info *cinfo; 635 struct scmi_xfer_raw_waiter *rw; 636 struct device *dev = raw->handle->dev; 637 638 if (!chan_id) 639 chan_id = xfer->hdr.protocol_id; 640 else 641 xfer->flags |= SCMI_XFER_FLAG_CHAN_SET; 642 643 cinfo = scmi_xfer_raw_channel_get(raw->handle, chan_id); 644 if (IS_ERR(cinfo)) 645 return PTR_ERR(cinfo); 646 647 rw = scmi_xfer_raw_waiter_get(raw, xfer, cinfo, async); 648 if (!rw) { 649 dev_warn(dev, "RAW - Cannot get a free waiter !\n"); 650 return -ENOMEM; 651 } 652 653 /* True ONLY if also supported by transport. */ 654 if (is_polling_enabled(cinfo, raw->desc)) 655 xfer->hdr.poll_completion = true; 656 657 reinit_completion(&xfer->done); 658 /* Make sure xfer state update is visible before sending */ 659 smp_store_mb(xfer->state, SCMI_XFER_SENT_OK); 660 661 trace_scmi_xfer_begin(xfer->transfer_id, xfer->hdr.id, 662 xfer->hdr.protocol_id, xfer->hdr.seq, 663 xfer->hdr.poll_completion, 664 scmi_inflight_count(raw->handle)); 665 666 ret = raw->desc->ops->send_message(rw->cinfo, xfer); 667 if (ret) { 668 dev_err(dev, "Failed to send RAW message %d\n", ret); 669 scmi_xfer_raw_waiter_put(raw, rw); 670 return ret; 671 } 672 673 trace_scmi_msg_dump(raw->id, cinfo->id, xfer->hdr.protocol_id, 674 xfer->hdr.id, "cmnd", xfer->hdr.seq, 675 xfer->hdr.status, 676 xfer->tx.buf, xfer->tx.len); 677 678 scmi_xfer_raw_waiter_enqueue(raw, rw); 679 680 return ret; 681 } 682 683 /** 684 * scmi_raw_message_send - An helper to build and send an SCMI command using 685 * the provided SCMI bare message buffer 686 * 687 * @raw: A reference to the Raw instance. 688 * @buf: A buffer containing the whole SCMI message to send (including the 689 * header) in little-endian binary format. 690 * @len: Length of the message in @buf. 691 * @chan_id: The channel ID to use. 692 * @async: A flag stating if an asynchronous command is required. 693 * @poll: A flag stating if a polling transmission is required. 694 * 695 * Return: 0 on Success 696 */ 697 static int scmi_raw_message_send(struct scmi_raw_mode_info *raw, 698 void *buf, size_t len, u8 chan_id, 699 bool async, bool poll) 700 { 701 int ret; 702 struct scmi_xfer *xfer; 703 704 ret = scmi_xfer_raw_get_init(raw, buf, len, &xfer); 705 if (ret) 706 return ret; 707 708 if (poll) { 709 if (is_transport_polling_capable(raw->desc)) { 710 xfer->hdr.poll_completion = true; 711 } else { 712 dev_err(raw->handle->dev, 713 "Failed to send RAW message - Polling NOT supported\n"); 714 scmi_xfer_raw_put(raw->handle, xfer); 715 return -EINVAL; 716 } 717 } 718 719 ret = scmi_do_xfer_raw_start(raw, xfer, chan_id, async); 720 if (ret) 721 scmi_xfer_raw_put(raw->handle, xfer); 722 723 return ret; 724 } 725 726 static struct scmi_raw_buffer * 727 scmi_raw_message_dequeue(struct scmi_raw_queue *q, bool o_nonblock) 728 { 729 unsigned long flags; 730 struct scmi_raw_buffer *rb; 731 732 spin_lock_irqsave(&q->msg_q_lock, flags); 733 while (list_empty(&q->msg_q)) { 734 spin_unlock_irqrestore(&q->msg_q_lock, flags); 735 736 if (o_nonblock) 737 return ERR_PTR(-EAGAIN); 738 739 if (wait_event_interruptible(q->wq, !list_empty(&q->msg_q))) 740 return ERR_PTR(-ERESTARTSYS); 741 742 spin_lock_irqsave(&q->msg_q_lock, flags); 743 } 744 745 rb = scmi_raw_buffer_dequeue_unlocked(q); 746 747 spin_unlock_irqrestore(&q->msg_q_lock, flags); 748 749 return rb; 750 } 751 752 /** 753 * scmi_raw_message_receive - An helper to dequeue and report the next 754 * available enqueued raw message payload that has been collected. 755 * 756 * @raw: A reference to the Raw instance. 757 * @buf: A buffer to get hold of the whole SCMI message received and represented 758 * in little-endian binary format. 759 * @len: Length of @buf. 760 * @size: The effective size of the message copied into @buf 761 * @idx: The index of the queue to pick the next queued message from. 762 * @chan_id: The channel ID to use. 763 * @o_nonblock: A flag to request a non-blocking message dequeue. 764 * 765 * Return: 0 on Success 766 */ 767 static int scmi_raw_message_receive(struct scmi_raw_mode_info *raw, 768 void *buf, size_t len, size_t *size, 769 unsigned int idx, unsigned int chan_id, 770 bool o_nonblock) 771 { 772 int ret = 0; 773 struct scmi_raw_buffer *rb; 774 struct scmi_raw_queue *q; 775 776 q = scmi_raw_queue_select(raw, idx, chan_id); 777 if (!q) 778 return -ENODEV; 779 780 rb = scmi_raw_message_dequeue(q, o_nonblock); 781 if (IS_ERR(rb)) { 782 dev_dbg(raw->handle->dev, "RAW - No message available!\n"); 783 return PTR_ERR(rb); 784 } 785 786 if (rb->msg.len <= len) { 787 memcpy(buf, rb->msg.buf, rb->msg.len); 788 *size = rb->msg.len; 789 } else { 790 ret = -ENOSPC; 791 } 792 793 scmi_raw_buffer_put(q, rb); 794 795 return ret; 796 } 797 798 /* SCMI Raw debugfs helpers */ 799 800 static ssize_t scmi_dbg_raw_mode_common_read(struct file *filp, 801 char __user *buf, 802 size_t count, loff_t *ppos, 803 unsigned int idx) 804 { 805 ssize_t cnt; 806 struct scmi_dbg_raw_data *rd = filp->private_data; 807 808 if (!rd->rx_size) { 809 int ret; 810 811 ret = scmi_raw_message_receive(rd->raw, rd->rx.buf, rd->rx.len, 812 &rd->rx_size, idx, rd->chan_id, 813 filp->f_flags & O_NONBLOCK); 814 if (ret) { 815 rd->rx_size = 0; 816 return ret; 817 } 818 819 /* Reset any previous filepos change, including writes */ 820 *ppos = 0; 821 } else if (*ppos == rd->rx_size) { 822 /* Return EOF once all the message has been read-out */ 823 rd->rx_size = 0; 824 return 0; 825 } 826 827 cnt = simple_read_from_buffer(buf, count, ppos, 828 rd->rx.buf, rd->rx_size); 829 830 return cnt; 831 } 832 833 static ssize_t scmi_dbg_raw_mode_common_write(struct file *filp, 834 const char __user *buf, 835 size_t count, loff_t *ppos, 836 bool async, bool poll) 837 { 838 int ret; 839 struct scmi_dbg_raw_data *rd = filp->private_data; 840 841 if (count > rd->tx.len - rd->tx_size) 842 return -ENOSPC; 843 844 /* On first write attempt @count carries the total full message size. */ 845 if (!rd->tx_size) 846 rd->tx_req_size = count; 847 848 /* 849 * Gather a full message, possibly across multiple interrupted wrrtes, 850 * before sending it with a single RAW xfer. 851 */ 852 if (rd->tx_size < rd->tx_req_size) { 853 ssize_t cnt; 854 855 cnt = simple_write_to_buffer(rd->tx.buf, rd->tx.len, ppos, 856 buf, count); 857 if (cnt < 0) 858 return cnt; 859 860 rd->tx_size += cnt; 861 if (cnt < count) 862 return cnt; 863 } 864 865 ret = scmi_raw_message_send(rd->raw, rd->tx.buf, rd->tx_size, 866 rd->chan_id, async, poll); 867 868 /* Reset ppos for next message ... */ 869 rd->tx_size = 0; 870 *ppos = 0; 871 872 return ret ?: count; 873 } 874 875 static __poll_t scmi_test_dbg_raw_common_poll(struct file *filp, 876 struct poll_table_struct *wait, 877 unsigned int idx) 878 { 879 unsigned long flags; 880 struct scmi_dbg_raw_data *rd = filp->private_data; 881 struct scmi_raw_queue *q; 882 __poll_t mask = 0; 883 884 q = scmi_raw_queue_select(rd->raw, idx, rd->chan_id); 885 if (!q) 886 return mask; 887 888 poll_wait(filp, &q->wq, wait); 889 890 spin_lock_irqsave(&q->msg_q_lock, flags); 891 if (!list_empty(&q->msg_q)) 892 mask = EPOLLIN | EPOLLRDNORM; 893 spin_unlock_irqrestore(&q->msg_q_lock, flags); 894 895 return mask; 896 } 897 898 static ssize_t scmi_dbg_raw_mode_message_read(struct file *filp, 899 char __user *buf, 900 size_t count, loff_t *ppos) 901 { 902 return scmi_dbg_raw_mode_common_read(filp, buf, count, ppos, 903 SCMI_RAW_REPLY_QUEUE); 904 } 905 906 static ssize_t scmi_dbg_raw_mode_message_write(struct file *filp, 907 const char __user *buf, 908 size_t count, loff_t *ppos) 909 { 910 return scmi_dbg_raw_mode_common_write(filp, buf, count, ppos, 911 false, false); 912 } 913 914 static __poll_t scmi_dbg_raw_mode_message_poll(struct file *filp, 915 struct poll_table_struct *wait) 916 { 917 return scmi_test_dbg_raw_common_poll(filp, wait, SCMI_RAW_REPLY_QUEUE); 918 } 919 920 static int scmi_dbg_raw_mode_open(struct inode *inode, struct file *filp) 921 { 922 struct scmi_raw_mode_info *raw; 923 struct scmi_dbg_raw_data *rd; 924 925 if (!inode->i_private) 926 return -ENODEV; 927 928 raw = inode->i_private; 929 rd = kzalloc_obj(*rd); 930 if (!rd) 931 return -ENOMEM; 932 933 rd->rx.len = raw->desc->max_msg_size + sizeof(u32); 934 rd->rx.buf = kzalloc(rd->rx.len, GFP_KERNEL); 935 if (!rd->rx.buf) { 936 kfree(rd); 937 return -ENOMEM; 938 } 939 940 rd->tx.len = raw->desc->max_msg_size + sizeof(u32); 941 rd->tx.buf = kzalloc(rd->tx.len, GFP_KERNEL); 942 if (!rd->tx.buf) { 943 kfree(rd->rx.buf); 944 kfree(rd); 945 return -ENOMEM; 946 } 947 948 /* Grab channel ID from debugfs entry naming if any */ 949 /* not set - reassing 0 we already had after kzalloc() */ 950 rd->chan_id = debugfs_get_aux_num(filp); 951 952 rd->raw = raw; 953 filp->private_data = rd; 954 955 return nonseekable_open(inode, filp); 956 } 957 958 static int scmi_dbg_raw_mode_release(struct inode *inode, struct file *filp) 959 { 960 struct scmi_dbg_raw_data *rd = filp->private_data; 961 962 kfree(rd->rx.buf); 963 kfree(rd->tx.buf); 964 kfree(rd); 965 966 return 0; 967 } 968 969 static ssize_t scmi_dbg_raw_mode_reset_write(struct file *filp, 970 const char __user *buf, 971 size_t count, loff_t *ppos) 972 { 973 struct scmi_dbg_raw_data *rd = filp->private_data; 974 975 scmi_xfer_raw_reset(rd->raw); 976 977 return count; 978 } 979 980 static const struct file_operations scmi_dbg_raw_mode_reset_fops = { 981 .open = scmi_dbg_raw_mode_open, 982 .release = scmi_dbg_raw_mode_release, 983 .write = scmi_dbg_raw_mode_reset_write, 984 .owner = THIS_MODULE, 985 }; 986 987 static const struct file_operations scmi_dbg_raw_mode_message_fops = { 988 .open = scmi_dbg_raw_mode_open, 989 .release = scmi_dbg_raw_mode_release, 990 .read = scmi_dbg_raw_mode_message_read, 991 .write = scmi_dbg_raw_mode_message_write, 992 .poll = scmi_dbg_raw_mode_message_poll, 993 .owner = THIS_MODULE, 994 }; 995 996 static ssize_t scmi_dbg_raw_mode_message_async_write(struct file *filp, 997 const char __user *buf, 998 size_t count, loff_t *ppos) 999 { 1000 return scmi_dbg_raw_mode_common_write(filp, buf, count, ppos, 1001 true, false); 1002 } 1003 1004 static const struct file_operations scmi_dbg_raw_mode_message_async_fops = { 1005 .open = scmi_dbg_raw_mode_open, 1006 .release = scmi_dbg_raw_mode_release, 1007 .read = scmi_dbg_raw_mode_message_read, 1008 .write = scmi_dbg_raw_mode_message_async_write, 1009 .poll = scmi_dbg_raw_mode_message_poll, 1010 .owner = THIS_MODULE, 1011 }; 1012 1013 static ssize_t scmi_dbg_raw_mode_message_poll_write(struct file *filp, 1014 const char __user *buf, 1015 size_t count, loff_t *ppos) 1016 { 1017 return scmi_dbg_raw_mode_common_write(filp, buf, count, ppos, 1018 false, true); 1019 } 1020 1021 static const struct file_operations scmi_dbg_raw_mode_message_poll_fops = { 1022 .open = scmi_dbg_raw_mode_open, 1023 .release = scmi_dbg_raw_mode_release, 1024 .read = scmi_dbg_raw_mode_message_read, 1025 .write = scmi_dbg_raw_mode_message_poll_write, 1026 .poll = scmi_dbg_raw_mode_message_poll, 1027 .owner = THIS_MODULE, 1028 }; 1029 1030 static ssize_t scmi_dbg_raw_mode_message_poll_async_write(struct file *filp, 1031 const char __user *buf, 1032 size_t count, loff_t *ppos) 1033 { 1034 return scmi_dbg_raw_mode_common_write(filp, buf, count, ppos, 1035 true, true); 1036 } 1037 1038 static const struct file_operations scmi_dbg_raw_mode_message_poll_async_fops = { 1039 .open = scmi_dbg_raw_mode_open, 1040 .release = scmi_dbg_raw_mode_release, 1041 .read = scmi_dbg_raw_mode_message_read, 1042 .write = scmi_dbg_raw_mode_message_poll_async_write, 1043 .poll = scmi_dbg_raw_mode_message_poll, 1044 .owner = THIS_MODULE, 1045 }; 1046 1047 static ssize_t scmi_test_dbg_raw_mode_notif_read(struct file *filp, 1048 char __user *buf, 1049 size_t count, loff_t *ppos) 1050 { 1051 return scmi_dbg_raw_mode_common_read(filp, buf, count, ppos, 1052 SCMI_RAW_NOTIF_QUEUE); 1053 } 1054 1055 static __poll_t 1056 scmi_test_dbg_raw_mode_notif_poll(struct file *filp, 1057 struct poll_table_struct *wait) 1058 { 1059 return scmi_test_dbg_raw_common_poll(filp, wait, SCMI_RAW_NOTIF_QUEUE); 1060 } 1061 1062 static const struct file_operations scmi_dbg_raw_mode_notification_fops = { 1063 .open = scmi_dbg_raw_mode_open, 1064 .release = scmi_dbg_raw_mode_release, 1065 .read = scmi_test_dbg_raw_mode_notif_read, 1066 .poll = scmi_test_dbg_raw_mode_notif_poll, 1067 .owner = THIS_MODULE, 1068 }; 1069 1070 static ssize_t scmi_test_dbg_raw_mode_errors_read(struct file *filp, 1071 char __user *buf, 1072 size_t count, loff_t *ppos) 1073 { 1074 return scmi_dbg_raw_mode_common_read(filp, buf, count, ppos, 1075 SCMI_RAW_ERRS_QUEUE); 1076 } 1077 1078 static __poll_t 1079 scmi_test_dbg_raw_mode_errors_poll(struct file *filp, 1080 struct poll_table_struct *wait) 1081 { 1082 return scmi_test_dbg_raw_common_poll(filp, wait, SCMI_RAW_ERRS_QUEUE); 1083 } 1084 1085 static const struct file_operations scmi_dbg_raw_mode_errors_fops = { 1086 .open = scmi_dbg_raw_mode_open, 1087 .release = scmi_dbg_raw_mode_release, 1088 .read = scmi_test_dbg_raw_mode_errors_read, 1089 .poll = scmi_test_dbg_raw_mode_errors_poll, 1090 .owner = THIS_MODULE, 1091 }; 1092 1093 static struct scmi_raw_queue * 1094 scmi_raw_queue_init(struct scmi_raw_mode_info *raw) 1095 { 1096 int i; 1097 struct scmi_raw_buffer *rb; 1098 struct device *dev = raw->handle->dev; 1099 struct scmi_raw_queue *q; 1100 1101 q = devm_kzalloc(dev, sizeof(*q), GFP_KERNEL); 1102 if (!q) 1103 return ERR_PTR(-ENOMEM); 1104 1105 rb = devm_kcalloc(dev, raw->tx_max_msg, sizeof(*rb), GFP_KERNEL); 1106 if (!rb) 1107 return ERR_PTR(-ENOMEM); 1108 1109 spin_lock_init(&q->free_bufs_lock); 1110 INIT_LIST_HEAD(&q->free_bufs); 1111 for (i = 0; i < raw->tx_max_msg; i++, rb++) { 1112 rb->max_len = raw->desc->max_msg_size + sizeof(u32); 1113 rb->msg.buf = devm_kzalloc(dev, rb->max_len, GFP_KERNEL); 1114 if (!rb->msg.buf) 1115 return ERR_PTR(-ENOMEM); 1116 scmi_raw_buffer_put(q, rb); 1117 } 1118 1119 spin_lock_init(&q->msg_q_lock); 1120 INIT_LIST_HEAD(&q->msg_q); 1121 init_waitqueue_head(&q->wq); 1122 1123 return q; 1124 } 1125 1126 static int scmi_xfer_raw_worker_init(struct scmi_raw_mode_info *raw) 1127 { 1128 int i; 1129 struct scmi_xfer_raw_waiter *rw; 1130 struct device *dev = raw->handle->dev; 1131 1132 rw = devm_kcalloc(dev, raw->tx_max_msg, sizeof(*rw), GFP_KERNEL); 1133 if (!rw) 1134 return -ENOMEM; 1135 1136 raw->wait_wq = alloc_workqueue("scmi-raw-wait-wq-%d", 1137 WQ_UNBOUND | WQ_FREEZABLE | 1138 WQ_HIGHPRI | WQ_SYSFS, 0, raw->id); 1139 if (!raw->wait_wq) 1140 return -ENOMEM; 1141 1142 mutex_init(&raw->free_mtx); 1143 INIT_LIST_HEAD(&raw->free_waiters); 1144 mutex_init(&raw->active_mtx); 1145 INIT_LIST_HEAD(&raw->active_waiters); 1146 1147 for (i = 0; i < raw->tx_max_msg; i++, rw++) { 1148 init_completion(&rw->async_response); 1149 scmi_xfer_raw_waiter_put(raw, rw); 1150 } 1151 INIT_WORK(&raw->waiters_work, scmi_xfer_raw_worker); 1152 1153 return 0; 1154 } 1155 1156 static int scmi_raw_mode_setup(struct scmi_raw_mode_info *raw, 1157 u8 *channels, int num_chans) 1158 { 1159 int ret, idx; 1160 void *gid; 1161 struct device *dev = raw->handle->dev; 1162 1163 gid = devres_open_group(dev, NULL, GFP_KERNEL); 1164 if (!gid) 1165 return -ENOMEM; 1166 1167 for (idx = 0; idx < SCMI_RAW_MAX_QUEUE; idx++) { 1168 raw->q[idx] = scmi_raw_queue_init(raw); 1169 if (IS_ERR(raw->q[idx])) { 1170 ret = PTR_ERR(raw->q[idx]); 1171 goto err; 1172 } 1173 } 1174 1175 xa_init(&raw->chans_q); 1176 if (num_chans > 1) { 1177 int i; 1178 1179 for (i = 0; i < num_chans; i++) { 1180 struct scmi_raw_queue *q; 1181 1182 q = scmi_raw_queue_init(raw); 1183 if (IS_ERR(q)) { 1184 ret = PTR_ERR(q); 1185 goto err_xa; 1186 } 1187 1188 ret = xa_insert(&raw->chans_q, channels[i], q, 1189 GFP_KERNEL); 1190 if (ret) { 1191 dev_err(dev, 1192 "Fail to allocate Raw queue 0x%02X\n", 1193 channels[i]); 1194 goto err_xa; 1195 } 1196 } 1197 } 1198 1199 ret = scmi_xfer_raw_worker_init(raw); 1200 if (ret) 1201 goto err_xa; 1202 1203 devres_close_group(dev, gid); 1204 raw->gid = gid; 1205 1206 return 0; 1207 1208 err_xa: 1209 xa_destroy(&raw->chans_q); 1210 err: 1211 devres_release_group(dev, gid); 1212 return ret; 1213 } 1214 1215 /** 1216 * scmi_raw_mode_init - Function to initialize the SCMI Raw stack 1217 * 1218 * @handle: Pointer to SCMI entity handle 1219 * @top_dentry: A reference to the top Raw debugfs dentry 1220 * @instance_id: The ID of the underlying SCMI platform instance represented by 1221 * this Raw instance 1222 * @channels: The list of the existing channels 1223 * @num_chans: The number of entries in @channels 1224 * @desc: Reference to the transport operations 1225 * @tx_max_msg: Max number of in-flight messages allowed by the transport 1226 * 1227 * This function prepare the SCMI Raw stack and creates the debugfs API. 1228 * 1229 * Return: An opaque handle to the Raw instance on Success, an ERR_PTR otherwise 1230 */ 1231 void *scmi_raw_mode_init(const struct scmi_handle *handle, 1232 struct dentry *top_dentry, int instance_id, 1233 u8 *channels, int num_chans, 1234 const struct scmi_desc *desc, int tx_max_msg) 1235 { 1236 int ret; 1237 struct scmi_raw_mode_info *raw; 1238 struct device *dev; 1239 1240 if (!handle || !desc) 1241 return ERR_PTR(-EINVAL); 1242 1243 dev = handle->dev; 1244 raw = devm_kzalloc(dev, sizeof(*raw), GFP_KERNEL); 1245 if (!raw) 1246 return ERR_PTR(-ENOMEM); 1247 1248 raw->handle = handle; 1249 raw->desc = desc; 1250 raw->tx_max_msg = tx_max_msg; 1251 raw->id = instance_id; 1252 1253 ret = scmi_raw_mode_setup(raw, channels, num_chans); 1254 if (ret) { 1255 devm_kfree(dev, raw); 1256 return ERR_PTR(ret); 1257 } 1258 1259 raw->dentry = debugfs_create_dir("raw", top_dentry); 1260 1261 debugfs_create_file("reset", 0200, raw->dentry, raw, 1262 &scmi_dbg_raw_mode_reset_fops); 1263 1264 debugfs_create_file("message", 0600, raw->dentry, raw, 1265 &scmi_dbg_raw_mode_message_fops); 1266 1267 debugfs_create_file("message_async", 0600, raw->dentry, raw, 1268 &scmi_dbg_raw_mode_message_async_fops); 1269 1270 debugfs_create_file("message_poll", 0600, raw->dentry, raw, 1271 &scmi_dbg_raw_mode_message_poll_fops); 1272 1273 debugfs_create_file("message_poll_async", 0600, raw->dentry, raw, 1274 &scmi_dbg_raw_mode_message_poll_async_fops); 1275 1276 debugfs_create_file("notification", 0400, raw->dentry, raw, 1277 &scmi_dbg_raw_mode_notification_fops); 1278 1279 debugfs_create_file("errors", 0400, raw->dentry, raw, 1280 &scmi_dbg_raw_mode_errors_fops); 1281 1282 /* 1283 * Expose per-channel entries if multiple channels available. 1284 * Just ignore errors while setting up these interfaces since we 1285 * have anyway already a working core Raw support. 1286 */ 1287 if (num_chans > 1) { 1288 int i; 1289 struct dentry *top_chans; 1290 1291 top_chans = debugfs_create_dir("channels", raw->dentry); 1292 1293 for (i = 0; i < num_chans; i++) { 1294 char cdir[8]; 1295 struct dentry *chd; 1296 1297 snprintf(cdir, 8, "0x%02X", channels[i]); 1298 chd = debugfs_create_dir(cdir, top_chans); 1299 1300 debugfs_create_file_aux_num("message", 0600, chd, 1301 raw, channels[i], 1302 &scmi_dbg_raw_mode_message_fops); 1303 1304 debugfs_create_file_aux_num("message_async", 0600, chd, 1305 raw, channels[i], 1306 &scmi_dbg_raw_mode_message_async_fops); 1307 1308 debugfs_create_file_aux_num("message_poll", 0600, chd, 1309 raw, channels[i], 1310 &scmi_dbg_raw_mode_message_poll_fops); 1311 1312 debugfs_create_file_aux_num("message_poll_async", 0600, 1313 chd, raw, channels[i], 1314 &scmi_dbg_raw_mode_message_poll_async_fops); 1315 } 1316 } 1317 1318 dev_info(dev, "SCMI RAW Mode initialized for instance %d\n", raw->id); 1319 1320 return raw; 1321 } 1322 1323 /** 1324 * scmi_raw_mode_cleanup - Function to cleanup the SCMI Raw stack 1325 * 1326 * @r: An opaque handle to an initialized SCMI Raw instance 1327 */ 1328 void scmi_raw_mode_cleanup(void *r) 1329 { 1330 struct scmi_raw_mode_info *raw = r; 1331 1332 if (!raw) 1333 return; 1334 1335 debugfs_remove_recursive(raw->dentry); 1336 1337 cancel_work_sync(&raw->waiters_work); 1338 destroy_workqueue(raw->wait_wq); 1339 xa_destroy(&raw->chans_q); 1340 } 1341 1342 static int scmi_xfer_raw_collect(void *msg, size_t *msg_len, 1343 struct scmi_xfer *xfer) 1344 { 1345 __le32 *m; 1346 size_t msg_size; 1347 1348 if (!xfer || !msg || !msg_len) 1349 return -EINVAL; 1350 1351 /* Account for hdr ...*/ 1352 msg_size = xfer->rx.len + sizeof(u32); 1353 /* ... and status if needed */ 1354 if (xfer->hdr.type != MSG_TYPE_NOTIFICATION) 1355 msg_size += sizeof(u32); 1356 1357 if (msg_size > *msg_len) 1358 return -ENOSPC; 1359 1360 m = msg; 1361 *m = cpu_to_le32(pack_scmi_header(&xfer->hdr)); 1362 if (xfer->hdr.type != MSG_TYPE_NOTIFICATION) 1363 *++m = cpu_to_le32(xfer->hdr.status); 1364 1365 memcpy(++m, xfer->rx.buf, xfer->rx.len); 1366 1367 *msg_len = msg_size; 1368 1369 return 0; 1370 } 1371 1372 /** 1373 * scmi_raw_message_report - Helper to report back valid reponses/notifications 1374 * to raw message requests. 1375 * 1376 * @r: An opaque reference to the raw instance configuration 1377 * @xfer: The xfer containing the message to be reported 1378 * @idx: The index of the queue. 1379 * @chan_id: The channel ID to use. 1380 * 1381 * If Raw mode is enabled, this is called from the SCMI core on the regular RX 1382 * path to save and enqueue the response/notification payload carried by this 1383 * xfer into a dedicated scmi_raw_buffer for later consumption by the user. 1384 * 1385 * This way the caller can free the related xfer immediately afterwards and the 1386 * user can read back the raw message payload at its own pace (if ever) without 1387 * holding an xfer for too long. 1388 */ 1389 void scmi_raw_message_report(void *r, struct scmi_xfer *xfer, 1390 unsigned int idx, unsigned int chan_id) 1391 { 1392 int ret; 1393 unsigned long flags; 1394 struct scmi_raw_buffer *rb; 1395 struct device *dev; 1396 struct scmi_raw_queue *q; 1397 struct scmi_raw_mode_info *raw = r; 1398 1399 if (!raw || (idx == SCMI_RAW_REPLY_QUEUE && !SCMI_XFER_IS_RAW(xfer))) 1400 return; 1401 1402 dev = raw->handle->dev; 1403 q = scmi_raw_queue_select(raw, idx, 1404 SCMI_XFER_IS_CHAN_SET(xfer) ? chan_id : 0); 1405 if (!q) { 1406 dev_warn(dev, 1407 "RAW[%d] - NO queue for chan 0x%X. Dropping report.\n", 1408 idx, chan_id); 1409 return; 1410 } 1411 1412 /* 1413 * Grab the msg_q_lock upfront to avoid a possible race between 1414 * realizing the free list was empty and effectively picking the next 1415 * buffer to use from the oldest one enqueued and still unread on this 1416 * msg_q. 1417 * 1418 * Note that nowhere else these locks are taken together, so no risk of 1419 * deadlocks du eto inversion. 1420 */ 1421 spin_lock_irqsave(&q->msg_q_lock, flags); 1422 rb = scmi_raw_buffer_get(q); 1423 if (!rb) { 1424 /* 1425 * Immediate and delayed replies to previously injected Raw 1426 * commands MUST be read back from userspace to free the buffers: 1427 * if this is not happening something is seriously broken and 1428 * must be fixed at the application level: complain loudly. 1429 */ 1430 if (idx == SCMI_RAW_REPLY_QUEUE) { 1431 spin_unlock_irqrestore(&q->msg_q_lock, flags); 1432 dev_warn(dev, 1433 "RAW[%d] - Buffers exhausted. Dropping report.\n", 1434 idx); 1435 return; 1436 } 1437 1438 /* 1439 * Notifications and errors queues are instead handled in a 1440 * circular manner: unread old buffers are just overwritten by 1441 * newer ones. 1442 * 1443 * The main reason for this is that notifications originated 1444 * by Raw requests cannot be distinguished from normal ones, so 1445 * your Raw buffers queues risk to be flooded and depleted by 1446 * notifications if you left it mistakenly enabled or when in 1447 * coexistence mode. 1448 */ 1449 rb = scmi_raw_buffer_dequeue_unlocked(q); 1450 if (WARN_ON(!rb)) { 1451 spin_unlock_irqrestore(&q->msg_q_lock, flags); 1452 return; 1453 } 1454 1455 /* Reset to full buffer length */ 1456 rb->msg.len = rb->max_len; 1457 1458 dev_warn_once(dev, 1459 "RAW[%d] - Buffers exhausted. Re-using oldest.\n", 1460 idx); 1461 } 1462 spin_unlock_irqrestore(&q->msg_q_lock, flags); 1463 1464 ret = scmi_xfer_raw_collect(rb->msg.buf, &rb->msg.len, xfer); 1465 if (ret) { 1466 dev_warn(dev, "RAW - Cannot collect xfer into buffer !\n"); 1467 scmi_raw_buffer_put(q, rb); 1468 return; 1469 } 1470 1471 scmi_raw_buffer_enqueue(q, rb); 1472 } 1473 1474 static void scmi_xfer_raw_fill(struct scmi_raw_mode_info *raw, 1475 struct scmi_chan_info *cinfo, 1476 struct scmi_xfer *xfer, u32 msg_hdr) 1477 { 1478 /* Unpack received HDR as it is */ 1479 unpack_scmi_header(msg_hdr, &xfer->hdr); 1480 xfer->hdr.seq = MSG_XTRACT_TOKEN(msg_hdr); 1481 1482 memset(xfer->rx.buf, 0x00, xfer->rx.len); 1483 1484 raw->desc->ops->fetch_response(cinfo, xfer); 1485 } 1486 1487 /** 1488 * scmi_raw_error_report - Helper to report back timed-out or generally 1489 * unexpected replies. 1490 * 1491 * @r: An opaque reference to the raw instance configuration 1492 * @cinfo: A reference to the channel to use to retrieve the broken xfer 1493 * @msg_hdr: The SCMI message header of the message to fetch and report 1494 * @priv: Any private data related to the xfer. 1495 * 1496 * If Raw mode is enabled, this is called from the SCMI core on the RX path in 1497 * case of errors to save and enqueue the bad message payload carried by the 1498 * message that has just been received. 1499 * 1500 * Note that we have to manually fetch any available payload into a temporary 1501 * xfer to be able to save and enqueue the message, since the regular RX error 1502 * path which had called this would have not fetched the message payload having 1503 * classified it as an error. 1504 */ 1505 void scmi_raw_error_report(void *r, struct scmi_chan_info *cinfo, 1506 u32 msg_hdr, void *priv) 1507 { 1508 struct scmi_xfer xfer; 1509 struct scmi_raw_mode_info *raw = r; 1510 1511 if (!raw) 1512 return; 1513 1514 xfer.rx.len = raw->desc->max_msg_size; 1515 xfer.rx.buf = kzalloc(xfer.rx.len, GFP_ATOMIC); 1516 if (!xfer.rx.buf) { 1517 dev_info(raw->handle->dev, 1518 "Cannot report Raw error for HDR:0x%X - ENOMEM\n", 1519 msg_hdr); 1520 return; 1521 } 1522 1523 /* Any transport-provided priv must be passed back down to transport */ 1524 if (priv) 1525 /* Ensure priv is visible */ 1526 smp_store_mb(xfer.priv, priv); 1527 1528 scmi_xfer_raw_fill(raw, cinfo, &xfer, msg_hdr); 1529 scmi_raw_message_report(raw, &xfer, SCMI_RAW_ERRS_QUEUE, 0); 1530 1531 kfree(xfer.rx.buf); 1532 } 1533