Linux Audio

Check our new training course

Loading...
v6.2
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Thunderbolt driver - control channel and configuration commands
   4 *
   5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
   6 * Copyright (C) 2018, Intel Corporation
   7 */
   8
   9#include <linux/crc32.h>
  10#include <linux/delay.h>
  11#include <linux/slab.h>
  12#include <linux/pci.h>
  13#include <linux/dmapool.h>
  14#include <linux/workqueue.h>
 
  15
  16#include "ctl.h"
  17
  18
  19#define TB_CTL_RX_PKG_COUNT	10
  20#define TB_CTL_RETRIES		4
 
 
 
 
 
  21
  22/**
  23 * struct tb_ctl - Thunderbolt control channel
  24 * @nhi: Pointer to the NHI structure
  25 * @tx: Transmit ring
  26 * @rx: Receive ring
  27 * @frame_pool: DMA pool for control messages
  28 * @rx_packets: Received control messages
  29 * @request_queue_lock: Lock protecting @request_queue
  30 * @request_queue: List of outstanding requests
  31 * @running: Is the control channel running at the moment
  32 * @timeout_msec: Default timeout for non-raw control messages
  33 * @callback: Callback called when hotplug message is received
  34 * @callback_data: Data passed to @callback
  35 */
  36struct tb_ctl {
  37	struct tb_nhi *nhi;
  38	struct tb_ring *tx;
  39	struct tb_ring *rx;
  40
  41	struct dma_pool *frame_pool;
  42	struct ctl_pkg *rx_packets[TB_CTL_RX_PKG_COUNT];
  43	struct mutex request_queue_lock;
  44	struct list_head request_queue;
  45	bool running;
  46
  47	int timeout_msec;
  48	event_cb callback;
  49	void *callback_data;
  50};
  51
  52
  53#define tb_ctl_WARN(ctl, format, arg...) \
  54	dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg)
  55
  56#define tb_ctl_err(ctl, format, arg...) \
  57	dev_err(&(ctl)->nhi->pdev->dev, format, ## arg)
  58
  59#define tb_ctl_warn(ctl, format, arg...) \
  60	dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg)
  61
  62#define tb_ctl_info(ctl, format, arg...) \
  63	dev_info(&(ctl)->nhi->pdev->dev, format, ## arg)
  64
  65#define tb_ctl_dbg(ctl, format, arg...) \
  66	dev_dbg(&(ctl)->nhi->pdev->dev, format, ## arg)
  67
  68static DECLARE_WAIT_QUEUE_HEAD(tb_cfg_request_cancel_queue);
  69/* Serializes access to request kref_get/put */
  70static DEFINE_MUTEX(tb_cfg_request_lock);
  71
  72/**
  73 * tb_cfg_request_alloc() - Allocates a new config request
  74 *
  75 * This is refcounted object so when you are done with this, call
  76 * tb_cfg_request_put() to it.
  77 */
  78struct tb_cfg_request *tb_cfg_request_alloc(void)
  79{
  80	struct tb_cfg_request *req;
  81
  82	req = kzalloc(sizeof(*req), GFP_KERNEL);
  83	if (!req)
  84		return NULL;
  85
  86	kref_init(&req->kref);
  87
  88	return req;
  89}
  90
  91/**
  92 * tb_cfg_request_get() - Increase refcount of a request
  93 * @req: Request whose refcount is increased
  94 */
  95void tb_cfg_request_get(struct tb_cfg_request *req)
  96{
  97	mutex_lock(&tb_cfg_request_lock);
  98	kref_get(&req->kref);
  99	mutex_unlock(&tb_cfg_request_lock);
 100}
 101
 102static void tb_cfg_request_destroy(struct kref *kref)
 103{
 104	struct tb_cfg_request *req = container_of(kref, typeof(*req), kref);
 105
 106	kfree(req);
 107}
 108
 109/**
 110 * tb_cfg_request_put() - Decrease refcount and possibly release the request
 111 * @req: Request whose refcount is decreased
 112 *
 113 * Call this function when you are done with the request. When refcount
 114 * goes to %0 the object is released.
 115 */
 116void tb_cfg_request_put(struct tb_cfg_request *req)
 117{
 118	mutex_lock(&tb_cfg_request_lock);
 119	kref_put(&req->kref, tb_cfg_request_destroy);
 120	mutex_unlock(&tb_cfg_request_lock);
 121}
 122
 123static int tb_cfg_request_enqueue(struct tb_ctl *ctl,
 124				  struct tb_cfg_request *req)
 125{
 126	WARN_ON(test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags));
 127	WARN_ON(req->ctl);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 128
 129	mutex_lock(&ctl->request_queue_lock);
 130	if (!ctl->running) {
 131		mutex_unlock(&ctl->request_queue_lock);
 132		return -ENOTCONN;
 133	}
 134	req->ctl = ctl;
 135	list_add_tail(&req->list, &ctl->request_queue);
 136	set_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
 137	mutex_unlock(&ctl->request_queue_lock);
 138	return 0;
 139}
 140
 141static void tb_cfg_request_dequeue(struct tb_cfg_request *req)
 142{
 143	struct tb_ctl *ctl = req->ctl;
 144
 145	mutex_lock(&ctl->request_queue_lock);
 146	list_del(&req->list);
 147	clear_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
 148	if (test_bit(TB_CFG_REQUEST_CANCELED, &req->flags))
 149		wake_up(&tb_cfg_request_cancel_queue);
 150	mutex_unlock(&ctl->request_queue_lock);
 151}
 152
 153static bool tb_cfg_request_is_active(struct tb_cfg_request *req)
 154{
 155	return test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
 156}
 157
 158static struct tb_cfg_request *
 159tb_cfg_request_find(struct tb_ctl *ctl, struct ctl_pkg *pkg)
 160{
 161	struct tb_cfg_request *req = NULL, *iter;
 162
 163	mutex_lock(&pkg->ctl->request_queue_lock);
 164	list_for_each_entry(iter, &pkg->ctl->request_queue, list) {
 165		tb_cfg_request_get(iter);
 166		if (iter->match(iter, pkg)) {
 167			req = iter;
 168			break;
 169		}
 170		tb_cfg_request_put(iter);
 171	}
 172	mutex_unlock(&pkg->ctl->request_queue_lock);
 173
 174	return req;
 175}
 176
 177/* utility functions */
 178
 179
 180static int check_header(const struct ctl_pkg *pkg, u32 len,
 181			enum tb_cfg_pkg_type type, u64 route)
 182{
 183	struct tb_cfg_header *header = pkg->buffer;
 184
 185	/* check frame, TODO: frame flags */
 186	if (WARN(len != pkg->frame.size,
 187			"wrong framesize (expected %#x, got %#x)\n",
 188			len, pkg->frame.size))
 189		return -EIO;
 190	if (WARN(type != pkg->frame.eof, "wrong eof (expected %#x, got %#x)\n",
 191			type, pkg->frame.eof))
 192		return -EIO;
 193	if (WARN(pkg->frame.sof, "wrong sof (expected 0x0, got %#x)\n",
 194			pkg->frame.sof))
 195		return -EIO;
 196
 197	/* check header */
 198	if (WARN(header->unknown != 1 << 9,
 199			"header->unknown is %#x\n", header->unknown))
 200		return -EIO;
 201	if (WARN(route != tb_cfg_get_route(header),
 202			"wrong route (expected %llx, got %llx)",
 203			route, tb_cfg_get_route(header)))
 204		return -EIO;
 205	return 0;
 206}
 207
 208static int check_config_address(struct tb_cfg_address addr,
 209				enum tb_cfg_space space, u32 offset,
 210				u32 length)
 211{
 212	if (WARN(addr.zero, "addr.zero is %#x\n", addr.zero))
 213		return -EIO;
 214	if (WARN(space != addr.space, "wrong space (expected %x, got %x\n)",
 215			space, addr.space))
 216		return -EIO;
 217	if (WARN(offset != addr.offset, "wrong offset (expected %x, got %x\n)",
 218			offset, addr.offset))
 219		return -EIO;
 220	if (WARN(length != addr.length, "wrong space (expected %x, got %x\n)",
 221			length, addr.length))
 222		return -EIO;
 
 
 223	/*
 224	 * We cannot check addr->port as it is set to the upstream port of the
 225	 * sender.
 226	 */
 227	return 0;
 228}
 229
 230static struct tb_cfg_result decode_error(const struct ctl_pkg *response)
 231{
 232	struct cfg_error_pkg *pkg = response->buffer;
 233	struct tb_ctl *ctl = response->ctl;
 234	struct tb_cfg_result res = { 0 };
 235	res.response_route = tb_cfg_get_route(&pkg->header);
 236	res.response_port = 0;
 237	res.err = check_header(response, sizeof(*pkg), TB_CFG_PKG_ERROR,
 238			       tb_cfg_get_route(&pkg->header));
 239	if (res.err)
 240		return res;
 241
 242	if (pkg->zero1)
 243		tb_ctl_warn(ctl, "pkg->zero1 is %#x\n", pkg->zero1);
 244	if (pkg->zero2)
 245		tb_ctl_warn(ctl, "pkg->zero2 is %#x\n", pkg->zero2);
 246	if (pkg->zero3)
 247		tb_ctl_warn(ctl, "pkg->zero3 is %#x\n", pkg->zero3);
 248
 249	res.err = 1;
 250	res.tb_error = pkg->error;
 251	res.response_port = pkg->port;
 252	return res;
 253
 254}
 255
 256static struct tb_cfg_result parse_header(const struct ctl_pkg *pkg, u32 len,
 257					 enum tb_cfg_pkg_type type, u64 route)
 258{
 259	struct tb_cfg_header *header = pkg->buffer;
 260	struct tb_cfg_result res = { 0 };
 261
 262	if (pkg->frame.eof == TB_CFG_PKG_ERROR)
 263		return decode_error(pkg);
 264
 265	res.response_port = 0; /* will be updated later for cfg_read/write */
 266	res.response_route = tb_cfg_get_route(header);
 267	res.err = check_header(pkg, len, type, route);
 268	return res;
 269}
 270
 271static void tb_cfg_print_error(struct tb_ctl *ctl,
 272			       const struct tb_cfg_result *res)
 273{
 274	WARN_ON(res->err != 1);
 275	switch (res->tb_error) {
 276	case TB_CFG_ERROR_PORT_NOT_CONNECTED:
 277		/* Port is not connected. This can happen during surprise
 278		 * removal. Do not warn. */
 279		return;
 280	case TB_CFG_ERROR_INVALID_CONFIG_SPACE:
 281		/*
 282		 * Invalid cfg_space/offset/length combination in
 283		 * cfg_read/cfg_write.
 284		 */
 285		tb_ctl_dbg(ctl, "%llx:%x: invalid config space or offset\n",
 286			   res->response_route, res->response_port);
 
 287		return;
 288	case TB_CFG_ERROR_NO_SUCH_PORT:
 289		/*
 290		 * - The route contains a non-existent port.
 291		 * - The route contains a non-PHY port (e.g. PCIe).
 292		 * - The port in cfg_read/cfg_write does not exist.
 293		 */
 294		tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Invalid port\n",
 295			res->response_route, res->response_port);
 296		return;
 297	case TB_CFG_ERROR_LOOP:
 298		tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Route contains a loop\n",
 299			res->response_route, res->response_port);
 300		return;
 301	case TB_CFG_ERROR_LOCK:
 302		tb_ctl_warn(ctl, "%llx:%x: downstream port is locked\n",
 303			    res->response_route, res->response_port);
 304		return;
 305	default:
 306		/* 5,6,7,9 and 11 are also valid error codes */
 307		tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Unknown error\n",
 308			res->response_route, res->response_port);
 309		return;
 310	}
 311}
 312
 313static __be32 tb_crc(const void *data, size_t len)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 314{
 315	return cpu_to_be32(~__crc32c_le(~0, data, len));
 316}
 317
 318static void tb_ctl_pkg_free(struct ctl_pkg *pkg)
 319{
 320	if (pkg) {
 321		dma_pool_free(pkg->ctl->frame_pool,
 322			      pkg->buffer, pkg->frame.buffer_phy);
 323		kfree(pkg);
 324	}
 325}
 326
 327static struct ctl_pkg *tb_ctl_pkg_alloc(struct tb_ctl *ctl)
 328{
 329	struct ctl_pkg *pkg = kzalloc(sizeof(*pkg), GFP_KERNEL);
 330	if (!pkg)
 331		return NULL;
 332	pkg->ctl = ctl;
 333	pkg->buffer = dma_pool_alloc(ctl->frame_pool, GFP_KERNEL,
 334				     &pkg->frame.buffer_phy);
 335	if (!pkg->buffer) {
 336		kfree(pkg);
 337		return NULL;
 338	}
 339	return pkg;
 340}
 341
 342
 343/* RX/TX handling */
 344
 345static void tb_ctl_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
 346			       bool canceled)
 347{
 348	struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame);
 349	tb_ctl_pkg_free(pkg);
 350}
 351
 352/*
 353 * tb_cfg_tx() - transmit a packet on the control channel
 354 *
 355 * len must be a multiple of four.
 356 *
 357 * Return: Returns 0 on success or an error code on failure.
 358 */
 359static int tb_ctl_tx(struct tb_ctl *ctl, const void *data, size_t len,
 360		     enum tb_cfg_pkg_type type)
 361{
 362	int res;
 363	struct ctl_pkg *pkg;
 364	if (len % 4 != 0) { /* required for le->be conversion */
 365		tb_ctl_WARN(ctl, "TX: invalid size: %zu\n", len);
 366		return -EINVAL;
 367	}
 368	if (len > TB_FRAME_SIZE - 4) { /* checksum is 4 bytes */
 369		tb_ctl_WARN(ctl, "TX: packet too large: %zu/%d\n",
 370			    len, TB_FRAME_SIZE - 4);
 371		return -EINVAL;
 372	}
 373	pkg = tb_ctl_pkg_alloc(ctl);
 374	if (!pkg)
 375		return -ENOMEM;
 376	pkg->frame.callback = tb_ctl_tx_callback;
 377	pkg->frame.size = len + 4;
 378	pkg->frame.sof = type;
 379	pkg->frame.eof = type;
 380	cpu_to_be32_array(pkg->buffer, data, len / 4);
 381	*(__be32 *) (pkg->buffer + len) = tb_crc(pkg->buffer, len);
 382
 383	res = tb_ring_tx(ctl->tx, &pkg->frame);
 384	if (res) /* ring is stopped */
 385		tb_ctl_pkg_free(pkg);
 386	return res;
 387}
 388
 389/*
 390 * tb_ctl_handle_event() - acknowledge a plug event, invoke ctl->callback
 391 */
 392static bool tb_ctl_handle_event(struct tb_ctl *ctl, enum tb_cfg_pkg_type type,
 393				struct ctl_pkg *pkg, size_t size)
 394{
 395	return ctl->callback(ctl->callback_data, type, pkg->buffer, size);
 
 
 
 
 
 
 
 
 
 
 
 
 396}
 397
 398static void tb_ctl_rx_submit(struct ctl_pkg *pkg)
 399{
 400	tb_ring_rx(pkg->ctl->rx, &pkg->frame); /*
 401					     * We ignore failures during stop.
 402					     * All rx packets are referenced
 403					     * from ctl->rx_packets, so we do
 404					     * not loose them.
 405					     */
 406}
 407
 408static int tb_async_error(const struct ctl_pkg *pkg)
 409{
 410	const struct cfg_error_pkg *error = pkg->buffer;
 411
 412	if (pkg->frame.eof != TB_CFG_PKG_ERROR)
 413		return false;
 414
 415	switch (error->error) {
 416	case TB_CFG_ERROR_LINK_ERROR:
 417	case TB_CFG_ERROR_HEC_ERROR_DETECTED:
 418	case TB_CFG_ERROR_FLOW_CONTROL_ERROR:
 419		return true;
 420
 421	default:
 422		return false;
 423	}
 424}
 425
 426static void tb_ctl_rx_callback(struct tb_ring *ring, struct ring_frame *frame,
 427			       bool canceled)
 428{
 429	struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame);
 430	struct tb_cfg_request *req;
 431	__be32 crc32;
 432
 433	if (canceled)
 434		return; /*
 435			 * ring is stopped, packet is referenced from
 436			 * ctl->rx_packets.
 437			 */
 438
 439	if (frame->size < 4 || frame->size % 4 != 0) {
 440		tb_ctl_err(pkg->ctl, "RX: invalid size %#x, dropping packet\n",
 441			   frame->size);
 442		goto rx;
 443	}
 444
 445	frame->size -= 4; /* remove checksum */
 446	crc32 = tb_crc(pkg->buffer, frame->size);
 
 
 
 
 
 447	be32_to_cpu_array(pkg->buffer, pkg->buffer, frame->size / 4);
 448
 449	switch (frame->eof) {
 450	case TB_CFG_PKG_READ:
 451	case TB_CFG_PKG_WRITE:
 452	case TB_CFG_PKG_ERROR:
 453	case TB_CFG_PKG_OVERRIDE:
 454	case TB_CFG_PKG_RESET:
 455		if (*(__be32 *)(pkg->buffer + frame->size) != crc32) {
 456			tb_ctl_err(pkg->ctl,
 457				   "RX: checksum mismatch, dropping packet\n");
 458			goto rx;
 459		}
 460		if (tb_async_error(pkg)) {
 461			tb_ctl_handle_event(pkg->ctl, frame->eof,
 462					    pkg, frame->size);
 463			goto rx;
 464		}
 465		break;
 466
 467	case TB_CFG_PKG_EVENT:
 468	case TB_CFG_PKG_XDOMAIN_RESP:
 469	case TB_CFG_PKG_XDOMAIN_REQ:
 470		if (*(__be32 *)(pkg->buffer + frame->size) != crc32) {
 471			tb_ctl_err(pkg->ctl,
 472				   "RX: checksum mismatch, dropping packet\n");
 473			goto rx;
 474		}
 475		fallthrough;
 476	case TB_CFG_PKG_ICM_EVENT:
 477		if (tb_ctl_handle_event(pkg->ctl, frame->eof, pkg, frame->size))
 478			goto rx;
 479		break;
 480
 481	default:
 482		break;
 483	}
 484
 485	/*
 486	 * The received packet will be processed only if there is an
 487	 * active request and that the packet is what is expected. This
 488	 * prevents packets such as replies coming after timeout has
 489	 * triggered from messing with the active requests.
 490	 */
 491	req = tb_cfg_request_find(pkg->ctl, pkg);
 492	if (req) {
 493		if (req->copy(req, pkg))
 494			schedule_work(&req->work);
 495		tb_cfg_request_put(req);
 496	}
 497
 
 498rx:
 499	tb_ctl_rx_submit(pkg);
 500}
 501
 502static void tb_cfg_request_work(struct work_struct *work)
 503{
 504	struct tb_cfg_request *req = container_of(work, typeof(*req), work);
 505
 506	if (!test_bit(TB_CFG_REQUEST_CANCELED, &req->flags))
 507		req->callback(req->callback_data);
 508
 509	tb_cfg_request_dequeue(req);
 510	tb_cfg_request_put(req);
 511}
 512
 513/**
 514 * tb_cfg_request() - Start control request not waiting for it to complete
 515 * @ctl: Control channel to use
 516 * @req: Request to start
 517 * @callback: Callback called when the request is completed
 518 * @callback_data: Data to be passed to @callback
 519 *
 520 * This queues @req on the given control channel without waiting for it
 521 * to complete. When the request completes @callback is called.
 522 */
 523int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req,
 524		   void (*callback)(void *), void *callback_data)
 525{
 526	int ret;
 527
 528	req->flags = 0;
 529	req->callback = callback;
 530	req->callback_data = callback_data;
 531	INIT_WORK(&req->work, tb_cfg_request_work);
 532	INIT_LIST_HEAD(&req->list);
 533
 534	tb_cfg_request_get(req);
 535	ret = tb_cfg_request_enqueue(ctl, req);
 536	if (ret)
 537		goto err_put;
 538
 539	ret = tb_ctl_tx(ctl, req->request, req->request_size,
 540			req->request_type);
 541	if (ret)
 542		goto err_dequeue;
 543
 544	if (!req->response)
 545		schedule_work(&req->work);
 546
 547	return 0;
 548
 549err_dequeue:
 550	tb_cfg_request_dequeue(req);
 551err_put:
 552	tb_cfg_request_put(req);
 553
 554	return ret;
 555}
 556
 557/**
 558 * tb_cfg_request_cancel() - Cancel a control request
 559 * @req: Request to cancel
 560 * @err: Error to assign to the request
 561 *
 562 * This function can be used to cancel ongoing request. It will wait
 563 * until the request is not active anymore.
 564 */
 565void tb_cfg_request_cancel(struct tb_cfg_request *req, int err)
 566{
 567	set_bit(TB_CFG_REQUEST_CANCELED, &req->flags);
 568	schedule_work(&req->work);
 569	wait_event(tb_cfg_request_cancel_queue, !tb_cfg_request_is_active(req));
 570	req->result.err = err;
 571}
 572
 573static void tb_cfg_request_complete(void *data)
 574{
 575	complete(data);
 576}
 577
 578/**
 579 * tb_cfg_request_sync() - Start control request and wait until it completes
 580 * @ctl: Control channel to use
 581 * @req: Request to start
 582 * @timeout_msec: Timeout how long to wait @req to complete
 583 *
 584 * Starts a control request and waits until it completes. If timeout
 585 * triggers the request is canceled before function returns. Note the
 586 * caller needs to make sure only one message for given switch is active
 587 * at a time.
 588 */
 589struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
 590					 struct tb_cfg_request *req,
 591					 int timeout_msec)
 592{
 593	unsigned long timeout = msecs_to_jiffies(timeout_msec);
 594	struct tb_cfg_result res = { 0 };
 595	DECLARE_COMPLETION_ONSTACK(done);
 596	int ret;
 597
 598	ret = tb_cfg_request(ctl, req, tb_cfg_request_complete, &done);
 599	if (ret) {
 600		res.err = ret;
 601		return res;
 
 
 
 
 602	}
 603
 604	if (!wait_for_completion_timeout(&done, timeout))
 605		tb_cfg_request_cancel(req, -ETIMEDOUT);
 606
 607	flush_work(&req->work);
 608
 609	return req->result;
 610}
 611
 
 612/* public interface, alloc/start/stop/free */
 613
 614/**
 615 * tb_ctl_alloc() - allocate a control channel
 616 * @nhi: Pointer to NHI
 617 * @timeout_msec: Default timeout used with non-raw control messages
 618 * @cb: Callback called for plug events
 619 * @cb_data: Data passed to @cb
 620 *
 621 * cb will be invoked once for every hot plug event.
 622 *
 623 * Return: Returns a pointer on success or NULL on failure.
 624 */
 625struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, int timeout_msec, event_cb cb,
 626			    void *cb_data)
 627{
 628	int i;
 629	struct tb_ctl *ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
 630	if (!ctl)
 631		return NULL;
 632	ctl->nhi = nhi;
 633	ctl->timeout_msec = timeout_msec;
 634	ctl->callback = cb;
 635	ctl->callback_data = cb_data;
 636
 637	mutex_init(&ctl->request_queue_lock);
 638	INIT_LIST_HEAD(&ctl->request_queue);
 639	ctl->frame_pool = dma_pool_create("thunderbolt_ctl", &nhi->pdev->dev,
 640					 TB_FRAME_SIZE, 4, 0);
 641	if (!ctl->frame_pool)
 642		goto err;
 643
 644	ctl->tx = tb_ring_alloc_tx(nhi, 0, 10, RING_FLAG_NO_SUSPEND);
 645	if (!ctl->tx)
 646		goto err;
 647
 648	ctl->rx = tb_ring_alloc_rx(nhi, 0, 10, RING_FLAG_NO_SUSPEND, 0, 0xffff,
 649				   0xffff, NULL, NULL);
 650	if (!ctl->rx)
 651		goto err;
 652
 653	for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++) {
 654		ctl->rx_packets[i] = tb_ctl_pkg_alloc(ctl);
 655		if (!ctl->rx_packets[i])
 656			goto err;
 657		ctl->rx_packets[i]->frame.callback = tb_ctl_rx_callback;
 658	}
 659
 660	tb_ctl_dbg(ctl, "control channel created\n");
 661	return ctl;
 662err:
 663	tb_ctl_free(ctl);
 664	return NULL;
 665}
 666
 667/**
 668 * tb_ctl_free() - free a control channel
 669 * @ctl: Control channel to free
 670 *
 671 * Must be called after tb_ctl_stop.
 672 *
 673 * Must NOT be called from ctl->callback.
 674 */
 675void tb_ctl_free(struct tb_ctl *ctl)
 676{
 677	int i;
 678
 679	if (!ctl)
 680		return;
 681
 682	if (ctl->rx)
 683		tb_ring_free(ctl->rx);
 684	if (ctl->tx)
 685		tb_ring_free(ctl->tx);
 686
 687	/* free RX packets */
 688	for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
 689		tb_ctl_pkg_free(ctl->rx_packets[i]);
 690
 691
 692	dma_pool_destroy(ctl->frame_pool);
 
 693	kfree(ctl);
 694}
 695
 696/**
 697 * tb_ctl_start() - start/resume the control channel
 698 * @ctl: Control channel to start
 699 */
 700void tb_ctl_start(struct tb_ctl *ctl)
 701{
 702	int i;
 703	tb_ctl_dbg(ctl, "control channel starting...\n");
 704	tb_ring_start(ctl->tx); /* is used to ack hotplug packets, start first */
 705	tb_ring_start(ctl->rx);
 706	for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
 707		tb_ctl_rx_submit(ctl->rx_packets[i]);
 708
 709	ctl->running = true;
 710}
 711
 712/**
 713 * tb_ctl_stop() - pause the control channel
 714 * @ctl: Control channel to stop
 715 *
 716 * All invocations of ctl->callback will have finished after this method
 717 * returns.
 718 *
 719 * Must NOT be called from ctl->callback.
 720 */
 721void tb_ctl_stop(struct tb_ctl *ctl)
 722{
 723	mutex_lock(&ctl->request_queue_lock);
 724	ctl->running = false;
 725	mutex_unlock(&ctl->request_queue_lock);
 726
 727	tb_ring_stop(ctl->rx);
 728	tb_ring_stop(ctl->tx);
 729
 730	if (!list_empty(&ctl->request_queue))
 731		tb_ctl_WARN(ctl, "dangling request in request_queue\n");
 732	INIT_LIST_HEAD(&ctl->request_queue);
 733	tb_ctl_dbg(ctl, "control channel stopped\n");
 734}
 735
 736/* public interface, commands */
 737
 738/**
 739 * tb_cfg_ack_plug() - Ack hot plug/unplug event
 740 * @ctl: Control channel to use
 741 * @route: Router that originated the event
 742 * @port: Port where the hot plug/unplug happened
 743 * @unplug: Ack hot plug or unplug
 744 *
 745 * Call this as response for hot plug/unplug event to ack it.
 746 * Returns %0 on success or an error code on failure.
 747 */
 748int tb_cfg_ack_plug(struct tb_ctl *ctl, u64 route, u32 port, bool unplug)
 
 749{
 750	struct cfg_error_pkg pkg = {
 751		.header = tb_cfg_make_header(route),
 752		.port = port,
 753		.error = TB_CFG_ERROR_ACK_PLUG_EVENT,
 754		.pg = unplug ? TB_CFG_ERROR_PG_HOT_UNPLUG
 755			     : TB_CFG_ERROR_PG_HOT_PLUG,
 756	};
 757	tb_ctl_dbg(ctl, "acking hot %splug event on %llx:%x\n",
 758		   unplug ? "un" : "", route, port);
 759	return tb_ctl_tx(ctl, &pkg, sizeof(pkg), TB_CFG_PKG_ERROR);
 760}
 761
 762static bool tb_cfg_match(const struct tb_cfg_request *req,
 763			 const struct ctl_pkg *pkg)
 764{
 765	u64 route = tb_cfg_get_route(pkg->buffer) & ~BIT_ULL(63);
 766
 767	if (pkg->frame.eof == TB_CFG_PKG_ERROR)
 768		return true;
 769
 770	if (pkg->frame.eof != req->response_type)
 771		return false;
 772	if (route != tb_cfg_get_route(req->request))
 773		return false;
 774	if (pkg->frame.size != req->response_size)
 775		return false;
 776
 777	if (pkg->frame.eof == TB_CFG_PKG_READ ||
 778	    pkg->frame.eof == TB_CFG_PKG_WRITE) {
 779		const struct cfg_read_pkg *req_hdr = req->request;
 780		const struct cfg_read_pkg *res_hdr = pkg->buffer;
 781
 782		if (req_hdr->addr.seq != res_hdr->addr.seq)
 783			return false;
 784	}
 785
 786	return true;
 787}
 788
 789static bool tb_cfg_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg)
 790{
 791	struct tb_cfg_result res;
 792
 793	/* Now make sure it is in expected format */
 794	res = parse_header(pkg, req->response_size, req->response_type,
 795			   tb_cfg_get_route(req->request));
 796	if (!res.err)
 797		memcpy(req->response, pkg->buffer, req->response_size);
 798
 799	req->result = res;
 800
 801	/* Always complete when first response is received */
 802	return true;
 803}
 804
 805/**
 806 * tb_cfg_reset() - send a reset packet and wait for a response
 807 * @ctl: Control channel pointer
 808 * @route: Router string for the router to send reset
 809 *
 810 * If the switch at route is incorrectly configured then we will not receive a
 811 * reply (even though the switch will reset). The caller should check for
 812 * -ETIMEDOUT and attempt to reconfigure the switch.
 813 */
 814struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route)
 
 815{
 816	struct cfg_reset_pkg request = { .header = tb_cfg_make_header(route) };
 817	struct tb_cfg_result res = { 0 };
 818	struct tb_cfg_header reply;
 819	struct tb_cfg_request *req;
 820
 821	req = tb_cfg_request_alloc();
 822	if (!req) {
 823		res.err = -ENOMEM;
 824		return res;
 825	}
 826
 827	req->match = tb_cfg_match;
 828	req->copy = tb_cfg_copy;
 829	req->request = &request;
 830	req->request_size = sizeof(request);
 831	req->request_type = TB_CFG_PKG_RESET;
 832	req->response = &reply;
 833	req->response_size = sizeof(reply);
 834	req->response_type = TB_CFG_PKG_RESET;
 835
 836	res = tb_cfg_request_sync(ctl, req, ctl->timeout_msec);
 837
 838	tb_cfg_request_put(req);
 
 
 839
 840	return res;
 
 841}
 842
 843/**
 844 * tb_cfg_read_raw() - read from config space into buffer
 845 * @ctl: Pointer to the control channel
 846 * @buffer: Buffer where the data is read
 847 * @route: Route string of the router
 848 * @port: Port number when reading from %TB_CFG_PORT, %0 otherwise
 849 * @space: Config space selector
 850 * @offset: Dword word offset of the register to start reading
 851 * @length: Number of dwords to read
 852 * @timeout_msec: Timeout in ms how long to wait for the response
 853 *
 854 * Reads from router config space without translating the possible error.
 855 */
 856struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
 857		u64 route, u32 port, enum tb_cfg_space space,
 858		u32 offset, u32 length, int timeout_msec)
 859{
 860	struct tb_cfg_result res = { 0 };
 861	struct cfg_read_pkg request = {
 862		.header = tb_cfg_make_header(route),
 863		.addr = {
 864			.port = port,
 865			.space = space,
 866			.offset = offset,
 867			.length = length,
 868		},
 869	};
 870	struct cfg_write_pkg reply;
 871	int retries = 0;
 872
 873	while (retries < TB_CTL_RETRIES) {
 874		struct tb_cfg_request *req;
 875
 876		req = tb_cfg_request_alloc();
 877		if (!req) {
 878			res.err = -ENOMEM;
 879			return res;
 880		}
 881
 882		request.addr.seq = retries++;
 883
 884		req->match = tb_cfg_match;
 885		req->copy = tb_cfg_copy;
 886		req->request = &request;
 887		req->request_size = sizeof(request);
 888		req->request_type = TB_CFG_PKG_READ;
 889		req->response = &reply;
 890		req->response_size = 12 + 4 * length;
 891		req->response_type = TB_CFG_PKG_READ;
 892
 893		res = tb_cfg_request_sync(ctl, req, timeout_msec);
 894
 895		tb_cfg_request_put(req);
 896
 897		if (res.err != -ETIMEDOUT)
 898			break;
 899
 900		/* Wait a bit (arbitrary time) until we send a retry */
 901		usleep_range(10, 100);
 902	}
 903
 
 
 904	if (res.err)
 905		return res;
 906
 907	res.response_port = reply.addr.port;
 908	res.err = check_config_address(reply.addr, space, offset, length);
 909	if (!res.err)
 910		memcpy(buffer, &reply.data, 4 * length);
 911	return res;
 912}
 913
 914/**
 915 * tb_cfg_write_raw() - write from buffer into config space
 916 * @ctl: Pointer to the control channel
 917 * @buffer: Data to write
 918 * @route: Route string of the router
 919 * @port: Port number when writing to %TB_CFG_PORT, %0 otherwise
 920 * @space: Config space selector
 921 * @offset: Dword word offset of the register to start writing
 922 * @length: Number of dwords to write
 923 * @timeout_msec: Timeout in ms how long to wait for the response
 924 *
 925 * Writes to router config space without translating the possible error.
 926 */
 927struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer,
 928		u64 route, u32 port, enum tb_cfg_space space,
 929		u32 offset, u32 length, int timeout_msec)
 930{
 931	struct tb_cfg_result res = { 0 };
 932	struct cfg_write_pkg request = {
 933		.header = tb_cfg_make_header(route),
 934		.addr = {
 935			.port = port,
 936			.space = space,
 937			.offset = offset,
 938			.length = length,
 939		},
 940	};
 941	struct cfg_read_pkg reply;
 942	int retries = 0;
 943
 944	memcpy(&request.data, buffer, length * 4);
 945
 946	while (retries < TB_CTL_RETRIES) {
 947		struct tb_cfg_request *req;
 948
 949		req = tb_cfg_request_alloc();
 950		if (!req) {
 951			res.err = -ENOMEM;
 952			return res;
 953		}
 954
 955		request.addr.seq = retries++;
 956
 957		req->match = tb_cfg_match;
 958		req->copy = tb_cfg_copy;
 959		req->request = &request;
 960		req->request_size = 12 + 4 * length;
 961		req->request_type = TB_CFG_PKG_WRITE;
 962		req->response = &reply;
 963		req->response_size = sizeof(reply);
 964		req->response_type = TB_CFG_PKG_WRITE;
 965
 966		res = tb_cfg_request_sync(ctl, req, timeout_msec);
 967
 968		tb_cfg_request_put(req);
 969
 970		if (res.err != -ETIMEDOUT)
 971			break;
 972
 973		/* Wait a bit (arbitrary time) until we send a retry */
 974		usleep_range(10, 100);
 975	}
 976
 
 
 977	if (res.err)
 978		return res;
 979
 980	res.response_port = reply.addr.port;
 981	res.err = check_config_address(reply.addr, space, offset, length);
 982	return res;
 983}
 984
 985static int tb_cfg_get_error(struct tb_ctl *ctl, enum tb_cfg_space space,
 986			    const struct tb_cfg_result *res)
 987{
 988	/*
 989	 * For unimplemented ports access to port config space may return
 990	 * TB_CFG_ERROR_INVALID_CONFIG_SPACE (alternatively their type is
 991	 * set to TB_TYPE_INACTIVE). In the former case return -ENODEV so
 992	 * that the caller can mark the port as disabled.
 993	 */
 994	if (space == TB_CFG_PORT &&
 995	    res->tb_error == TB_CFG_ERROR_INVALID_CONFIG_SPACE)
 996		return -ENODEV;
 997
 998	tb_cfg_print_error(ctl, res);
 999
1000	if (res->tb_error == TB_CFG_ERROR_LOCK)
1001		return -EACCES;
1002	else if (res->tb_error == TB_CFG_ERROR_PORT_NOT_CONNECTED)
1003		return -ENOTCONN;
1004
1005	return -EIO;
1006}
1007
1008int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
1009		enum tb_cfg_space space, u32 offset, u32 length)
1010{
1011	struct tb_cfg_result res = tb_cfg_read_raw(ctl, buffer, route, port,
1012			space, offset, length, ctl->timeout_msec);
1013	switch (res.err) {
1014	case 0:
1015		/* Success */
1016		break;
1017
1018	case 1:
1019		/* Thunderbolt error, tb_error holds the actual number */
1020		return tb_cfg_get_error(ctl, space, &res);
1021
1022	case -ETIMEDOUT:
1023		tb_ctl_warn(ctl, "%llx: timeout reading config space %u from %#x\n",
1024			    route, space, offset);
1025		break;
1026
1027	default:
1028		WARN(1, "tb_cfg_read: %d\n", res.err);
1029		break;
1030	}
 
1031	return res.err;
1032}
1033
1034int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port,
1035		 enum tb_cfg_space space, u32 offset, u32 length)
1036{
1037	struct tb_cfg_result res = tb_cfg_write_raw(ctl, buffer, route, port,
1038			space, offset, length, ctl->timeout_msec);
1039	switch (res.err) {
1040	case 0:
1041		/* Success */
1042		break;
1043
1044	case 1:
1045		/* Thunderbolt error, tb_error holds the actual number */
1046		return tb_cfg_get_error(ctl, space, &res);
1047
1048	case -ETIMEDOUT:
1049		tb_ctl_warn(ctl, "%llx: timeout writing config space %u to %#x\n",
1050			    route, space, offset);
1051		break;
1052
1053	default:
1054		WARN(1, "tb_cfg_write: %d\n", res.err);
1055		break;
1056	}
 
1057	return res.err;
1058}
1059
1060/**
1061 * tb_cfg_get_upstream_port() - get upstream port number of switch at route
1062 * @ctl: Pointer to the control channel
1063 * @route: Route string of the router
1064 *
1065 * Reads the first dword from the switches TB_CFG_SWITCH config area and
1066 * returns the port number from which the reply originated.
1067 *
1068 * Return: Returns the upstream port number on success or an error code on
1069 * failure.
1070 */
1071int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route)
1072{
1073	u32 dummy;
1074	struct tb_cfg_result res = tb_cfg_read_raw(ctl, &dummy, route, 0,
1075						   TB_CFG_SWITCH, 0, 1,
1076						   ctl->timeout_msec);
1077	if (res.err == 1)
1078		return -EIO;
1079	if (res.err)
1080		return res.err;
1081	return res.response_port;
1082}
v4.6
 
  1/*
  2 * Thunderbolt Cactus Ridge driver - control channel and configuration commands
  3 *
  4 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
 
  5 */
  6
  7#include <linux/crc32.h>
 
  8#include <linux/slab.h>
  9#include <linux/pci.h>
 10#include <linux/dmapool.h>
 11#include <linux/workqueue.h>
 12#include <linux/kfifo.h>
 13
 14#include "ctl.h"
 15
 16
 17struct ctl_pkg {
 18	struct tb_ctl *ctl;
 19	void *buffer;
 20	struct ring_frame frame;
 21};
 22
 23#define TB_CTL_RX_PKG_COUNT 10
 24
 25/**
 26 * struct tb_cfg - thunderbolt control channel
 
 
 
 
 
 
 
 
 
 
 
 27 */
 28struct tb_ctl {
 29	struct tb_nhi *nhi;
 30	struct tb_ring *tx;
 31	struct tb_ring *rx;
 32
 33	struct dma_pool *frame_pool;
 34	struct ctl_pkg *rx_packets[TB_CTL_RX_PKG_COUNT];
 35	DECLARE_KFIFO(response_fifo, struct ctl_pkg*, 16);
 36	struct completion response_ready;
 
 37
 38	hotplug_cb callback;
 
 39	void *callback_data;
 40};
 41
 42
 43#define tb_ctl_WARN(ctl, format, arg...) \
 44	dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg)
 45
 46#define tb_ctl_err(ctl, format, arg...) \
 47	dev_err(&(ctl)->nhi->pdev->dev, format, ## arg)
 48
 49#define tb_ctl_warn(ctl, format, arg...) \
 50	dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg)
 51
 52#define tb_ctl_info(ctl, format, arg...) \
 53	dev_info(&(ctl)->nhi->pdev->dev, format, ## arg)
 54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 55
 56/* configuration packets definitions */
 
 57
 58enum tb_cfg_pkg_type {
 59	TB_CFG_PKG_READ = 1,
 60	TB_CFG_PKG_WRITE = 2,
 61	TB_CFG_PKG_ERROR = 3,
 62	TB_CFG_PKG_NOTIFY_ACK = 4,
 63	TB_CFG_PKG_EVENT = 5,
 64	TB_CFG_PKG_XDOMAIN_REQ = 6,
 65	TB_CFG_PKG_XDOMAIN_RESP = 7,
 66	TB_CFG_PKG_OVERRIDE = 8,
 67	TB_CFG_PKG_RESET = 9,
 68	TB_CFG_PKG_PREPARE_TO_SLEEP = 0xd,
 69};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 70
 71/* common header */
 72struct tb_cfg_header {
 73	u32 route_hi:22;
 74	u32 unknown:10; /* highest order bit is set on replies */
 75	u32 route_lo;
 76} __packed;
 77
 78/* additional header for read/write packets */
 79struct tb_cfg_address {
 80	u32 offset:13; /* in dwords */
 81	u32 length:6; /* in dwords */
 82	u32 port:6;
 83	enum tb_cfg_space space:2;
 84	u32 seq:2; /* sequence number  */
 85	u32 zero:3;
 86} __packed;
 87
 88/* TB_CFG_PKG_READ, response for TB_CFG_PKG_WRITE */
 89struct cfg_read_pkg {
 90	struct tb_cfg_header header;
 91	struct tb_cfg_address addr;
 92} __packed;
 93
 94/* TB_CFG_PKG_WRITE, response for TB_CFG_PKG_READ */
 95struct cfg_write_pkg {
 96	struct tb_cfg_header header;
 97	struct tb_cfg_address addr;
 98	u32 data[64]; /* maximum size, tb_cfg_address.length has 6 bits */
 99} __packed;
100
101/* TB_CFG_PKG_ERROR */
102struct cfg_error_pkg {
103	struct tb_cfg_header header;
104	enum tb_cfg_error error:4;
105	u32 zero1:4;
106	u32 port:6;
107	u32 zero2:2; /* Both should be zero, still they are different fields. */
108	u32 zero3:16;
109} __packed;
110
111/* TB_CFG_PKG_EVENT */
112struct cfg_event_pkg {
113	struct tb_cfg_header header;
114	u32 port:6;
115	u32 zero:25;
116	bool unplug:1;
117} __packed;
118
119/* TB_CFG_PKG_RESET */
120struct cfg_reset_pkg {
121	struct tb_cfg_header header;
122} __packed;
123
124/* TB_CFG_PKG_PREPARE_TO_SLEEP */
125struct cfg_pts_pkg {
126	struct tb_cfg_header header;
127	u32 data;
128} __packed;
129
 
 
 
 
 
 
 
 
 
 
 
130
131/* utility functions */
 
 
 
 
 
 
 
 
 
 
132
133static u64 get_route(struct tb_cfg_header header)
134{
135	return (u64) header.route_hi << 32 | header.route_lo;
136}
137
138static struct tb_cfg_header make_header(u64 route)
 
139{
140	struct tb_cfg_header header = {
141		.route_hi = route >> 32,
142		.route_lo = route,
143	};
144	/* check for overflow, route_hi is not 32 bits! */
145	WARN_ON(get_route(header) != route);
146	return header;
 
 
 
 
 
 
 
147}
148
149static int check_header(struct ctl_pkg *pkg, u32 len, enum tb_cfg_pkg_type type,
150			u64 route)
 
 
 
151{
152	struct tb_cfg_header *header = pkg->buffer;
153
154	/* check frame, TODO: frame flags */
155	if (WARN(len != pkg->frame.size,
156			"wrong framesize (expected %#x, got %#x)\n",
157			len, pkg->frame.size))
158		return -EIO;
159	if (WARN(type != pkg->frame.eof, "wrong eof (expected %#x, got %#x)\n",
160			type, pkg->frame.eof))
161		return -EIO;
162	if (WARN(pkg->frame.sof, "wrong sof (expected 0x0, got %#x)\n",
163			pkg->frame.sof))
164		return -EIO;
165
166	/* check header */
167	if (WARN(header->unknown != 1 << 9,
168			"header->unknown is %#x\n", header->unknown))
169		return -EIO;
170	if (WARN(route != get_route(*header),
171			"wrong route (expected %llx, got %llx)",
172			route, get_route(*header)))
173		return -EIO;
174	return 0;
175}
176
177static int check_config_address(struct tb_cfg_address addr,
178				enum tb_cfg_space space, u32 offset,
179				u32 length)
180{
181	if (WARN(addr.zero, "addr.zero is %#x\n", addr.zero))
182		return -EIO;
183	if (WARN(space != addr.space, "wrong space (expected %x, got %x\n)",
184			space, addr.space))
185		return -EIO;
186	if (WARN(offset != addr.offset, "wrong offset (expected %x, got %x\n)",
187			offset, addr.offset))
188		return -EIO;
189	if (WARN(length != addr.length, "wrong space (expected %x, got %x\n)",
190			length, addr.length))
191		return -EIO;
192	if (WARN(addr.seq, "addr.seq is %#x\n", addr.seq))
193		return -EIO;
194	/*
195	 * We cannot check addr->port as it is set to the upstream port of the
196	 * sender.
197	 */
198	return 0;
199}
200
201static struct tb_cfg_result decode_error(struct ctl_pkg *response)
202{
203	struct cfg_error_pkg *pkg = response->buffer;
 
204	struct tb_cfg_result res = { 0 };
205	res.response_route = get_route(pkg->header);
206	res.response_port = 0;
207	res.err = check_header(response, sizeof(*pkg), TB_CFG_PKG_ERROR,
208			       get_route(pkg->header));
209	if (res.err)
210		return res;
211
212	WARN(pkg->zero1, "pkg->zero1 is %#x\n", pkg->zero1);
213	WARN(pkg->zero2, "pkg->zero1 is %#x\n", pkg->zero1);
214	WARN(pkg->zero3, "pkg->zero1 is %#x\n", pkg->zero1);
 
 
 
 
215	res.err = 1;
216	res.tb_error = pkg->error;
217	res.response_port = pkg->port;
218	return res;
219
220}
221
222static struct tb_cfg_result parse_header(struct ctl_pkg *pkg, u32 len,
223					 enum tb_cfg_pkg_type type, u64 route)
224{
225	struct tb_cfg_header *header = pkg->buffer;
226	struct tb_cfg_result res = { 0 };
227
228	if (pkg->frame.eof == TB_CFG_PKG_ERROR)
229		return decode_error(pkg);
230
231	res.response_port = 0; /* will be updated later for cfg_read/write */
232	res.response_route = get_route(*header);
233	res.err = check_header(pkg, len, type, route);
234	return res;
235}
236
237static void tb_cfg_print_error(struct tb_ctl *ctl,
238			       const struct tb_cfg_result *res)
239{
240	WARN_ON(res->err != 1);
241	switch (res->tb_error) {
242	case TB_CFG_ERROR_PORT_NOT_CONNECTED:
243		/* Port is not connected. This can happen during surprise
244		 * removal. Do not warn. */
245		return;
246	case TB_CFG_ERROR_INVALID_CONFIG_SPACE:
247		/*
248		 * Invalid cfg_space/offset/length combination in
249		 * cfg_read/cfg_write.
250		 */
251		tb_ctl_WARN(ctl,
252			"CFG_ERROR(%llx:%x): Invalid config space of offset\n",
253			res->response_route, res->response_port);
254		return;
255	case TB_CFG_ERROR_NO_SUCH_PORT:
256		/*
257		 * - The route contains a non-existent port.
258		 * - The route contains a non-PHY port (e.g. PCIe).
259		 * - The port in cfg_read/cfg_write does not exist.
260		 */
261		tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Invalid port\n",
262			res->response_route, res->response_port);
263		return;
264	case TB_CFG_ERROR_LOOP:
265		tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Route contains a loop\n",
266			res->response_route, res->response_port);
267		return;
 
 
 
 
268	default:
269		/* 5,6,7,9 and 11 are also valid error codes */
270		tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Unknown error\n",
271			res->response_route, res->response_port);
272		return;
273	}
274}
275
276static void cpu_to_be32_array(__be32 *dst, u32 *src, size_t len)
277{
278	int i;
279	for (i = 0; i < len; i++)
280		dst[i] = cpu_to_be32(src[i]);
281}
282
283static void be32_to_cpu_array(u32 *dst, __be32 *src, size_t len)
284{
285	int i;
286	for (i = 0; i < len; i++)
287		dst[i] = be32_to_cpu(src[i]);
288}
289
290static __be32 tb_crc(void *data, size_t len)
291{
292	return cpu_to_be32(~__crc32c_le(~0, data, len));
293}
294
295static void tb_ctl_pkg_free(struct ctl_pkg *pkg)
296{
297	if (pkg) {
298		dma_pool_free(pkg->ctl->frame_pool,
299			      pkg->buffer, pkg->frame.buffer_phy);
300		kfree(pkg);
301	}
302}
303
304static struct ctl_pkg *tb_ctl_pkg_alloc(struct tb_ctl *ctl)
305{
306	struct ctl_pkg *pkg = kzalloc(sizeof(*pkg), GFP_KERNEL);
307	if (!pkg)
308		return NULL;
309	pkg->ctl = ctl;
310	pkg->buffer = dma_pool_alloc(ctl->frame_pool, GFP_KERNEL,
311				     &pkg->frame.buffer_phy);
312	if (!pkg->buffer) {
313		kfree(pkg);
314		return NULL;
315	}
316	return pkg;
317}
318
319
320/* RX/TX handling */
321
322static void tb_ctl_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
323			       bool canceled)
324{
325	struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame);
326	tb_ctl_pkg_free(pkg);
327}
328
329/**
330 * tb_cfg_tx() - transmit a packet on the control channel
331 *
332 * len must be a multiple of four.
333 *
334 * Return: Returns 0 on success or an error code on failure.
335 */
336static int tb_ctl_tx(struct tb_ctl *ctl, void *data, size_t len,
337		     enum tb_cfg_pkg_type type)
338{
339	int res;
340	struct ctl_pkg *pkg;
341	if (len % 4 != 0) { /* required for le->be conversion */
342		tb_ctl_WARN(ctl, "TX: invalid size: %zu\n", len);
343		return -EINVAL;
344	}
345	if (len > TB_FRAME_SIZE - 4) { /* checksum is 4 bytes */
346		tb_ctl_WARN(ctl, "TX: packet too large: %zu/%d\n",
347			    len, TB_FRAME_SIZE - 4);
348		return -EINVAL;
349	}
350	pkg = tb_ctl_pkg_alloc(ctl);
351	if (!pkg)
352		return -ENOMEM;
353	pkg->frame.callback = tb_ctl_tx_callback;
354	pkg->frame.size = len + 4;
355	pkg->frame.sof = type;
356	pkg->frame.eof = type;
357	cpu_to_be32_array(pkg->buffer, data, len / 4);
358	*(__be32 *) (pkg->buffer + len) = tb_crc(pkg->buffer, len);
359
360	res = ring_tx(ctl->tx, &pkg->frame);
361	if (res) /* ring is stopped */
362		tb_ctl_pkg_free(pkg);
363	return res;
364}
365
366/**
367 * tb_ctl_handle_plug_event() - acknowledge a plug event, invoke ctl->callback
368 */
369static void tb_ctl_handle_plug_event(struct tb_ctl *ctl,
370				     struct ctl_pkg *response)
371{
372	struct cfg_event_pkg *pkg = response->buffer;
373	u64 route = get_route(pkg->header);
374
375	if (check_header(response, sizeof(*pkg), TB_CFG_PKG_EVENT, route)) {
376		tb_ctl_warn(ctl, "malformed TB_CFG_PKG_EVENT\n");
377		return;
378	}
379
380	if (tb_cfg_error(ctl, route, pkg->port, TB_CFG_ERROR_ACK_PLUG_EVENT))
381		tb_ctl_warn(ctl, "could not ack plug event on %llx:%x\n",
382			    route, pkg->port);
383	WARN(pkg->zero, "pkg->zero is %#x\n", pkg->zero);
384	ctl->callback(ctl->callback_data, route, pkg->port, pkg->unplug);
385}
386
387static void tb_ctl_rx_submit(struct ctl_pkg *pkg)
388{
389	ring_rx(pkg->ctl->rx, &pkg->frame); /*
390					     * We ignore failures during stop.
391					     * All rx packets are referenced
392					     * from ctl->rx_packets, so we do
393					     * not loose them.
394					     */
395}
396
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
397static void tb_ctl_rx_callback(struct tb_ring *ring, struct ring_frame *frame,
398			       bool canceled)
399{
400	struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame);
 
 
401
402	if (canceled)
403		return; /*
404			 * ring is stopped, packet is referenced from
405			 * ctl->rx_packets.
406			 */
407
408	if (frame->size < 4 || frame->size % 4 != 0) {
409		tb_ctl_err(pkg->ctl, "RX: invalid size %#x, dropping packet\n",
410			   frame->size);
411		goto rx;
412	}
413
414	frame->size -= 4; /* remove checksum */
415	if (*(__be32 *) (pkg->buffer + frame->size)
416			!= tb_crc(pkg->buffer, frame->size)) {
417		tb_ctl_err(pkg->ctl,
418			   "RX: checksum mismatch, dropping packet\n");
419		goto rx;
420	}
421	be32_to_cpu_array(pkg->buffer, pkg->buffer, frame->size / 4);
422
423	if (frame->eof == TB_CFG_PKG_EVENT) {
424		tb_ctl_handle_plug_event(pkg->ctl, pkg);
425		goto rx;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
426	}
427	if (!kfifo_put(&pkg->ctl->response_fifo, pkg)) {
428		tb_ctl_err(pkg->ctl, "RX: fifo is full\n");
429		goto rx;
 
 
 
 
 
 
 
 
 
430	}
431	complete(&pkg->ctl->response_ready);
432	return;
433rx:
434	tb_ctl_rx_submit(pkg);
435}
436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
437/**
438 * tb_ctl_rx() - receive a packet from the control channel
 
 
 
 
 
439 */
440static struct tb_cfg_result tb_ctl_rx(struct tb_ctl *ctl, void *buffer,
441				      size_t length, int timeout_msec,
442				      u64 route, enum tb_cfg_pkg_type type)
 
 
 
 
 
 
443{
444	struct tb_cfg_result res;
445	struct ctl_pkg *pkg;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
446
447	if (!wait_for_completion_timeout(&ctl->response_ready,
448					 msecs_to_jiffies(timeout_msec))) {
449		tb_ctl_WARN(ctl, "RX: timeout\n");
450		return (struct tb_cfg_result) { .err = -ETIMEDOUT };
451	}
452	if (!kfifo_get(&ctl->response_fifo, &pkg)) {
453		tb_ctl_WARN(ctl, "empty kfifo\n");
454		return (struct tb_cfg_result) { .err = -EIO };
455	}
456
457	res = parse_header(pkg, length, type, route);
458	if (!res.err)
459		memcpy(buffer, pkg->buffer, length);
460	tb_ctl_rx_submit(pkg);
461	return res;
 
462}
463
464
465/* public interface, alloc/start/stop/free */
466
467/**
468 * tb_ctl_alloc() - allocate a control channel
 
 
 
 
469 *
470 * cb will be invoked once for every hot plug event.
471 *
472 * Return: Returns a pointer on success or NULL on failure.
473 */
474struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, hotplug_cb cb, void *cb_data)
 
475{
476	int i;
477	struct tb_ctl *ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
478	if (!ctl)
479		return NULL;
480	ctl->nhi = nhi;
 
481	ctl->callback = cb;
482	ctl->callback_data = cb_data;
483
484	init_completion(&ctl->response_ready);
485	INIT_KFIFO(ctl->response_fifo);
486	ctl->frame_pool = dma_pool_create("thunderbolt_ctl", &nhi->pdev->dev,
487					 TB_FRAME_SIZE, 4, 0);
488	if (!ctl->frame_pool)
489		goto err;
490
491	ctl->tx = ring_alloc_tx(nhi, 0, 10);
492	if (!ctl->tx)
493		goto err;
494
495	ctl->rx = ring_alloc_rx(nhi, 0, 10);
 
496	if (!ctl->rx)
497		goto err;
498
499	for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++) {
500		ctl->rx_packets[i] = tb_ctl_pkg_alloc(ctl);
501		if (!ctl->rx_packets[i])
502			goto err;
503		ctl->rx_packets[i]->frame.callback = tb_ctl_rx_callback;
504	}
505
506	tb_ctl_info(ctl, "control channel created\n");
507	return ctl;
508err:
509	tb_ctl_free(ctl);
510	return NULL;
511}
512
513/**
514 * tb_ctl_free() - free a control channel
 
515 *
516 * Must be called after tb_ctl_stop.
517 *
518 * Must NOT be called from ctl->callback.
519 */
520void tb_ctl_free(struct tb_ctl *ctl)
521{
522	int i;
 
 
 
 
523	if (ctl->rx)
524		ring_free(ctl->rx);
525	if (ctl->tx)
526		ring_free(ctl->tx);
527
528	/* free RX packets */
529	for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
530		tb_ctl_pkg_free(ctl->rx_packets[i]);
531
532
533	if (ctl->frame_pool)
534		dma_pool_destroy(ctl->frame_pool);
535	kfree(ctl);
536}
537
538/**
539 * tb_cfg_start() - start/resume the control channel
 
540 */
541void tb_ctl_start(struct tb_ctl *ctl)
542{
543	int i;
544	tb_ctl_info(ctl, "control channel starting...\n");
545	ring_start(ctl->tx); /* is used to ack hotplug packets, start first */
546	ring_start(ctl->rx);
547	for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
548		tb_ctl_rx_submit(ctl->rx_packets[i]);
 
 
549}
550
551/**
552 * control() - pause the control channel
 
553 *
554 * All invocations of ctl->callback will have finished after this method
555 * returns.
556 *
557 * Must NOT be called from ctl->callback.
558 */
559void tb_ctl_stop(struct tb_ctl *ctl)
560{
561	ring_stop(ctl->rx);
562	ring_stop(ctl->tx);
563
564	if (!kfifo_is_empty(&ctl->response_fifo))
565		tb_ctl_WARN(ctl, "dangling response in response_fifo\n");
566	kfifo_reset(&ctl->response_fifo);
567	tb_ctl_info(ctl, "control channel stopped\n");
 
 
 
 
568}
569
570/* public interface, commands */
571
572/**
573 * tb_cfg_error() - send error packet
 
 
 
 
574 *
575 * Return: Returns 0 on success or an error code on failure.
 
576 */
577int tb_cfg_error(struct tb_ctl *ctl, u64 route, u32 port,
578		 enum tb_cfg_error error)
579{
580	struct cfg_error_pkg pkg = {
581		.header = make_header(route),
582		.port = port,
583		.error = error,
 
 
584	};
585	tb_ctl_info(ctl, "resetting error on %llx:%x.\n", route, port);
 
586	return tb_ctl_tx(ctl, &pkg, sizeof(pkg), TB_CFG_PKG_ERROR);
587}
588
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
589/**
590 * tb_cfg_reset() - send a reset packet and wait for a response
 
 
591 *
592 * If the switch at route is incorrectly configured then we will not receive a
593 * reply (even though the switch will reset). The caller should check for
594 * -ETIMEDOUT and attempt to reconfigure the switch.
595 */
596struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route,
597				  int timeout_msec)
598{
599	int err;
600	struct cfg_reset_pkg request = { .header = make_header(route) };
601	struct tb_cfg_header reply;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
602
603	err = tb_ctl_tx(ctl, &request, sizeof(request), TB_CFG_PKG_RESET);
604	if (err)
605		return (struct tb_cfg_result) { .err = err };
606
607	return tb_ctl_rx(ctl, &reply, sizeof(reply), timeout_msec, route,
608			 TB_CFG_PKG_RESET);
609}
610
611/**
612 * tb_cfg_read() - read from config space into buffer
 
 
 
 
 
 
 
 
613 *
614 * Offset and length are in dwords.
615 */
616struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
617		u64 route, u32 port, enum tb_cfg_space space,
618		u32 offset, u32 length, int timeout_msec)
619{
620	struct tb_cfg_result res = { 0 };
621	struct cfg_read_pkg request = {
622		.header = make_header(route),
623		.addr = {
624			.port = port,
625			.space = space,
626			.offset = offset,
627			.length = length,
628		},
629	};
630	struct cfg_write_pkg reply;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
631
632	res.err = tb_ctl_tx(ctl, &request, sizeof(request), TB_CFG_PKG_READ);
633	if (res.err)
634		return res;
635
636	res = tb_ctl_rx(ctl, &reply, 12 + 4 * length, timeout_msec, route,
637			TB_CFG_PKG_READ);
638	if (res.err)
639		return res;
640
641	res.response_port = reply.addr.port;
642	res.err = check_config_address(reply.addr, space, offset, length);
643	if (!res.err)
644		memcpy(buffer, &reply.data, 4 * length);
645	return res;
646}
647
648/**
649 * tb_cfg_write() - write from buffer into config space
 
 
 
 
 
 
 
 
650 *
651 * Offset and length are in dwords.
652 */
653struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, void *buffer,
654		u64 route, u32 port, enum tb_cfg_space space,
655		u32 offset, u32 length, int timeout_msec)
656{
657	struct tb_cfg_result res = { 0 };
658	struct cfg_write_pkg request = {
659		.header = make_header(route),
660		.addr = {
661			.port = port,
662			.space = space,
663			.offset = offset,
664			.length = length,
665		},
666	};
667	struct cfg_read_pkg reply;
 
668
669	memcpy(&request.data, buffer, length * 4);
670
671	res.err = tb_ctl_tx(ctl, &request, 12 + 4 * length, TB_CFG_PKG_WRITE);
672	if (res.err)
673		return res;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
674
675	res = tb_ctl_rx(ctl, &reply, sizeof(reply), timeout_msec, route,
676			TB_CFG_PKG_WRITE);
677	if (res.err)
678		return res;
679
680	res.response_port = reply.addr.port;
681	res.err = check_config_address(reply.addr, space, offset, length);
682	return res;
683}
684
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
685int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
686		enum tb_cfg_space space, u32 offset, u32 length)
687{
688	struct tb_cfg_result res = tb_cfg_read_raw(ctl, buffer, route, port,
689			space, offset, length, TB_CFG_DEFAULT_TIMEOUT);
690	if (res.err == 1) {
691		tb_cfg_print_error(ctl, &res);
692		return -EIO;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
693	}
694	WARN(res.err, "tb_cfg_read: %d\n", res.err);
695	return res.err;
696}
697
698int tb_cfg_write(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
699		 enum tb_cfg_space space, u32 offset, u32 length)
700{
701	struct tb_cfg_result res = tb_cfg_write_raw(ctl, buffer, route, port,
702			space, offset, length, TB_CFG_DEFAULT_TIMEOUT);
703	if (res.err == 1) {
704		tb_cfg_print_error(ctl, &res);
705		return -EIO;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
706	}
707	WARN(res.err, "tb_cfg_write: %d\n", res.err);
708	return res.err;
709}
710
711/**
712 * tb_cfg_get_upstream_port() - get upstream port number of switch at route
 
 
713 *
714 * Reads the first dword from the switches TB_CFG_SWITCH config area and
715 * returns the port number from which the reply originated.
716 *
717 * Return: Returns the upstream port number on success or an error code on
718 * failure.
719 */
720int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route)
721{
722	u32 dummy;
723	struct tb_cfg_result res = tb_cfg_read_raw(ctl, &dummy, route, 0,
724						   TB_CFG_SWITCH, 0, 1,
725						   TB_CFG_DEFAULT_TIMEOUT);
726	if (res.err == 1)
727		return -EIO;
728	if (res.err)
729		return res.err;
730	return res.response_port;
731}