cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

bdc_cmd.c (9186B)


      1// SPDX-License-Identifier: GPL-2.0+
      2/*
      3 * bdc_cmd.c - BRCM BDC USB3.0 device controller
      4 *
      5 * Copyright (C) 2014 Broadcom Corporation
      6 *
      7 * Author: Ashwini Pahuja
      8 */
      9#include <linux/scatterlist.h>
     10#include <linux/slab.h>
     11
     12#include "bdc.h"
     13#include "bdc_cmd.h"
     14#include "bdc_dbg.h"
     15
     16/* Issues a cmd to cmd processor and waits for cmd completion */
     17static int bdc_issue_cmd(struct bdc *bdc, u32 cmd_sc, u32 param0,
     18							u32 param1, u32 param2)
     19{
     20	u32 timeout = BDC_CMD_TIMEOUT;
     21	u32 cmd_status;
     22	u32 temp;
     23
     24	bdc_writel(bdc->regs, BDC_CMDPAR0, param0);
     25	bdc_writel(bdc->regs, BDC_CMDPAR1, param1);
     26	bdc_writel(bdc->regs, BDC_CMDPAR2, param2);
     27
     28	/* Issue the cmd */
     29	/* Make sure the cmd params are written before asking HW to exec cmd */
     30	wmb();
     31	bdc_writel(bdc->regs, BDC_CMDSC, cmd_sc | BDC_CMD_CWS | BDC_CMD_SRD);
     32	do {
     33		temp = bdc_readl(bdc->regs, BDC_CMDSC);
     34		dev_dbg_ratelimited(bdc->dev, "cmdsc=%x", temp);
     35		cmd_status =  BDC_CMD_CST(temp);
     36		if (cmd_status != BDC_CMDS_BUSY)  {
     37			dev_dbg(bdc->dev,
     38				"command completed cmd_sts:%x\n", cmd_status);
     39			return cmd_status;
     40		}
     41		udelay(1);
     42	} while (timeout--);
     43
     44	dev_err(bdc->dev,
     45		"command operation timedout cmd_status=%d\n", cmd_status);
     46
     47	return cmd_status;
     48}
     49
     50/* Submits cmd and analyze the return value of bdc_issue_cmd */
     51static int bdc_submit_cmd(struct bdc *bdc, u32 cmd_sc,
     52					u32 param0, u32 param1,	u32 param2)
     53{
     54	u32 temp, cmd_status;
     55	int ret;
     56
     57	temp = bdc_readl(bdc->regs, BDC_CMDSC);
     58	dev_dbg(bdc->dev,
     59		"%s:CMDSC:%08x cmdsc:%08x param0=%08x param1=%08x param2=%08x\n",
     60		 __func__, temp, cmd_sc, param0, param1, param2);
     61
     62	cmd_status = BDC_CMD_CST(temp);
     63	if (cmd_status  ==  BDC_CMDS_BUSY) {
     64		dev_err(bdc->dev, "command processor busy: %x\n", cmd_status);
     65		return -EBUSY;
     66	}
     67	ret = bdc_issue_cmd(bdc, cmd_sc, param0, param1, param2);
     68	switch (ret) {
     69	case BDC_CMDS_SUCC:
     70		dev_dbg(bdc->dev, "command completed successfully\n");
     71		ret = 0;
     72		break;
     73
     74	case BDC_CMDS_PARA:
     75		dev_err(bdc->dev, "command parameter error\n");
     76		ret = -EINVAL;
     77		break;
     78
     79	case BDC_CMDS_STAT:
     80		dev_err(bdc->dev, "Invalid device/ep state\n");
     81		ret = -EINVAL;
     82		break;
     83
     84	case BDC_CMDS_FAIL:
     85		dev_err(bdc->dev, "Command failed?\n");
     86		ret = -EAGAIN;
     87		break;
     88
     89	case BDC_CMDS_INTL:
     90		dev_err(bdc->dev, "BDC Internal error\n");
     91		ret = -ECONNRESET;
     92		break;
     93
     94	case BDC_CMDS_BUSY:
     95		dev_err(bdc->dev,
     96			"command timedout waited for %dusec\n",
     97			BDC_CMD_TIMEOUT);
     98		ret = -ECONNRESET;
     99		break;
    100	default:
    101		dev_dbg(bdc->dev, "Unknown command completion code:%x\n", ret);
    102	}
    103
    104	return ret;
    105}
    106
    107/* Deconfigure the endpoint from HW */
    108int bdc_dconfig_ep(struct bdc *bdc, struct bdc_ep *ep)
    109{
    110	u32 cmd_sc;
    111
    112	cmd_sc = BDC_SUB_CMD_DRP_EP|BDC_CMD_EPN(ep->ep_num)|BDC_CMD_EPC;
    113	dev_dbg(bdc->dev, "%s ep->ep_num =%d cmd_sc=%x\n", __func__,
    114							ep->ep_num, cmd_sc);
    115
    116	return bdc_submit_cmd(bdc, cmd_sc, 0, 0, 0);
    117}
    118
    119/* Reinitalize the bdlist after config ep command */
    120static void ep_bd_list_reinit(struct bdc_ep *ep)
    121{
    122	struct bdc *bdc = ep->bdc;
    123	struct bdc_bd *bd;
    124
    125	ep->bd_list.eqp_bdi = 0;
    126	ep->bd_list.hwd_bdi = 0;
    127	bd = ep->bd_list.bd_table_array[0]->start_bd;
    128	dev_dbg(bdc->dev, "%s ep:%p bd:%p\n", __func__, ep, bd);
    129	memset(bd, 0, sizeof(struct bdc_bd));
    130	bd->offset[3] |= cpu_to_le32(BD_SBF);
    131}
    132
    133/* Configure an endpoint */
    134int bdc_config_ep(struct bdc *bdc, struct bdc_ep *ep)
    135{
    136	const struct usb_ss_ep_comp_descriptor *comp_desc;
    137	const struct usb_endpoint_descriptor	*desc;
    138	u32 param0, param1, param2, cmd_sc;
    139	u32 mps, mbs, mul, si;
    140	int ret;
    141
    142	desc = ep->desc;
    143	comp_desc = ep->comp_desc;
    144	cmd_sc = mul = mbs = param2 = 0;
    145	param0 = lower_32_bits(ep->bd_list.bd_table_array[0]->dma);
    146	param1 = upper_32_bits(ep->bd_list.bd_table_array[0]->dma);
    147	cpu_to_le32s(&param0);
    148	cpu_to_le32s(&param1);
    149
    150	dev_dbg(bdc->dev, "%s: param0=%08x param1=%08x",
    151						__func__, param0, param1);
    152	si = desc->bInterval;
    153	si = clamp_val(si, 1, 16) - 1;
    154
    155	mps = usb_endpoint_maxp(desc);
    156	param2 |= mps << MP_SHIFT;
    157	param2 |= usb_endpoint_type(desc) << EPT_SHIFT;
    158
    159	switch (bdc->gadget.speed) {
    160	case USB_SPEED_SUPER:
    161		if (usb_endpoint_xfer_int(desc) ||
    162					usb_endpoint_xfer_isoc(desc)) {
    163			param2 |= si;
    164			if (usb_endpoint_xfer_isoc(desc) && comp_desc)
    165				mul = comp_desc->bmAttributes;
    166
    167		}
    168		param2 |= mul << EPM_SHIFT;
    169		if (comp_desc)
    170			mbs = comp_desc->bMaxBurst;
    171		param2 |= mbs << MB_SHIFT;
    172		break;
    173
    174	case USB_SPEED_HIGH:
    175		if (usb_endpoint_xfer_isoc(desc) ||
    176					usb_endpoint_xfer_int(desc)) {
    177			param2 |= si;
    178
    179			mbs = usb_endpoint_maxp_mult(desc);
    180			param2 |= mbs << MB_SHIFT;
    181		}
    182		break;
    183
    184	case USB_SPEED_FULL:
    185	case USB_SPEED_LOW:
    186		/* the hardware accepts SI in 125usec range */
    187		if (usb_endpoint_xfer_isoc(desc))
    188			si += 3;
    189
    190		/*
    191		 * FS Int endpoints can have si of 1-255ms but the controller
    192		 * accepts 2^bInterval*125usec, so convert ms to nearest power
    193		 * of 2
    194		 */
    195		if (usb_endpoint_xfer_int(desc))
    196			si = fls(desc->bInterval * 8) - 1;
    197
    198		param2 |= si;
    199		break;
    200	default:
    201		dev_err(bdc->dev, "UNKNOWN speed ERR\n");
    202		return -EINVAL;
    203	}
    204
    205	cmd_sc |= BDC_CMD_EPC|BDC_CMD_EPN(ep->ep_num)|BDC_SUB_CMD_ADD_EP;
    206
    207	dev_dbg(bdc->dev, "cmd_sc=%x param2=%08x\n", cmd_sc, param2);
    208	ret = bdc_submit_cmd(bdc, cmd_sc, param0, param1, param2);
    209	if (ret) {
    210		dev_err(bdc->dev, "command failed :%x\n", ret);
    211		return ret;
    212	}
    213	ep_bd_list_reinit(ep);
    214
    215	return ret;
    216}
    217
    218/*
    219 * Change the HW deq pointer, if this command is successful, HW will start
    220 * fetching the next bd from address dma_addr.
    221 */
    222int bdc_ep_bla(struct bdc *bdc, struct bdc_ep *ep, dma_addr_t dma_addr)
    223{
    224	u32 param0, param1;
    225	u32 cmd_sc = 0;
    226
    227	dev_dbg(bdc->dev, "%s: add=%08llx\n", __func__,
    228				(unsigned long long)(dma_addr));
    229	param0 = lower_32_bits(dma_addr);
    230	param1 = upper_32_bits(dma_addr);
    231	cpu_to_le32s(&param0);
    232	cpu_to_le32s(&param1);
    233
    234	cmd_sc |= BDC_CMD_EPN(ep->ep_num)|BDC_CMD_BLA;
    235	dev_dbg(bdc->dev, "cmd_sc=%x\n", cmd_sc);
    236
    237	return bdc_submit_cmd(bdc, cmd_sc, param0, param1, 0);
    238}
    239
    240/* Set the address sent bu Host in SET_ADD request */
    241int bdc_address_device(struct bdc *bdc, u32 add)
    242{
    243	u32 cmd_sc = 0;
    244	u32 param2;
    245
    246	dev_dbg(bdc->dev, "%s: add=%d\n", __func__, add);
    247	cmd_sc |=  BDC_SUB_CMD_ADD|BDC_CMD_DVC;
    248	param2 = add & 0x7f;
    249
    250	return bdc_submit_cmd(bdc, cmd_sc, 0, 0, param2);
    251}
    252
    253/* Send a Function Wake notification packet using FH command */
    254int bdc_function_wake_fh(struct bdc *bdc, u8 intf)
    255{
    256	u32 param0, param1;
    257	u32 cmd_sc = 0;
    258
    259	param0 = param1 = 0;
    260	dev_dbg(bdc->dev, "%s intf=%d\n", __func__, intf);
    261	cmd_sc  |=  BDC_CMD_FH;
    262	param0 |= TRA_PACKET;
    263	param0 |= (bdc->dev_addr << 25);
    264	param1 |= DEV_NOTF_TYPE;
    265	param1 |= (FWK_SUBTYPE<<4);
    266	dev_dbg(bdc->dev, "param0=%08x param1=%08x\n", param0, param1);
    267
    268	return bdc_submit_cmd(bdc, cmd_sc, param0, param1, 0);
    269}
    270
    271/* Send a Function Wake notification packet using DNC command */
    272int bdc_function_wake(struct bdc *bdc, u8 intf)
    273{
    274	u32 cmd_sc = 0;
    275	u32 param2 = 0;
    276
    277	dev_dbg(bdc->dev, "%s intf=%d", __func__, intf);
    278	param2 |= intf;
    279	cmd_sc |= BDC_SUB_CMD_FWK|BDC_CMD_DNC;
    280
    281	return bdc_submit_cmd(bdc, cmd_sc, 0, 0, param2);
    282}
    283
    284/* Stall the endpoint */
    285int bdc_ep_set_stall(struct bdc *bdc, int epnum)
    286{
    287	u32 cmd_sc = 0;
    288
    289	dev_dbg(bdc->dev, "%s epnum=%d\n", __func__, epnum);
    290	/* issue a stall endpoint command */
    291	cmd_sc |=  BDC_SUB_CMD_EP_STL | BDC_CMD_EPN(epnum) | BDC_CMD_EPO;
    292
    293	return bdc_submit_cmd(bdc, cmd_sc, 0, 0, 0);
    294}
    295
    296/* resets the endpoint, called when host sends CLEAR_FEATURE(HALT) */
    297int bdc_ep_clear_stall(struct bdc *bdc, int epnum)
    298{
    299	struct bdc_ep *ep;
    300	u32 cmd_sc = 0;
    301	int ret;
    302
    303	dev_dbg(bdc->dev, "%s: epnum=%d\n", __func__, epnum);
    304	ep = bdc->bdc_ep_array[epnum];
    305	/*
    306	 * If we are not in stalled then stall Endpoint and issue clear stall,
    307	 * his will reset the seq number for non EP0.
    308	 */
    309	if (epnum != 1) {
    310		/* if the endpoint it not stallled */
    311		if (!(ep->flags & BDC_EP_STALL)) {
    312			ret = bdc_ep_set_stall(bdc, epnum);
    313			if (ret)
    314				return ret;
    315		}
    316	}
    317	/* Preserve the seq number for ep0 only */
    318	if (epnum != 1)
    319		cmd_sc |= BDC_CMD_EPO_RST_SN;
    320
    321	/* issue a reset endpoint command */
    322	cmd_sc |=  BDC_SUB_CMD_EP_RST | BDC_CMD_EPN(epnum) | BDC_CMD_EPO;
    323
    324	ret = bdc_submit_cmd(bdc, cmd_sc, 0, 0, 0);
    325	if (ret) {
    326		dev_err(bdc->dev, "command failed:%x\n", ret);
    327		return ret;
    328	}
    329	bdc_notify_xfr(bdc, epnum);
    330
    331	return ret;
    332}
    333
    334/* Stop the endpoint, called when software wants to dequeue some request */
    335int bdc_stop_ep(struct bdc *bdc, int epnum)
    336{
    337	struct bdc_ep *ep;
    338	u32 cmd_sc = 0;
    339	int ret;
    340
    341	ep = bdc->bdc_ep_array[epnum];
    342	dev_dbg(bdc->dev, "%s: ep:%s ep->flags:%08x\n", __func__,
    343						ep->name, ep->flags);
    344	/* Endpoint has to be in running state to execute stop ep command */
    345	if (!(ep->flags & BDC_EP_ENABLED)) {
    346		dev_err(bdc->dev, "stop endpoint called for disabled ep\n");
    347		return   -EINVAL;
    348	}
    349	if ((ep->flags & BDC_EP_STALL) || (ep->flags & BDC_EP_STOP))
    350		return 0;
    351
    352	/* issue a stop endpoint command */
    353	cmd_sc |= BDC_CMD_EP0_XSD | BDC_SUB_CMD_EP_STP
    354				| BDC_CMD_EPN(epnum) | BDC_CMD_EPO;
    355
    356	ret = bdc_submit_cmd(bdc, cmd_sc, 0, 0, 0);
    357	if (ret) {
    358		dev_err(bdc->dev,
    359			"stop endpoint command didn't complete:%d ep:%s\n",
    360			ret, ep->name);
    361		return ret;
    362	}
    363	ep->flags |= BDC_EP_STOP;
    364	bdc_dump_epsts(bdc);
    365
    366	return ret;
    367}