Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

cdx: add MCDI protocol interface for firmware interaction

The MCDI (Management CPU Driver Interface) is used as a
protocol to communicate with the RPU firmware. It has
pre-defined set of messages for different message exchanges
between APU and RPU.

Signed-off-by: Puneet Gupta <puneet.gupta@amd.com>
Signed-off-by: Nipun Gupta <nipun.gupta@amd.com>
Signed-off-by: Tarak Reddy <tarak.reddy@amd.com>
Reviewed-by: Pieter Jansen van Vuuren <pieter.jansen-van-vuuren@amd.com>
Tested-by: Nikhil Agarwal <nikhil.agarwal@amd.com>
Link: https://lore.kernel.org/r/20230313132636.31850-5-nipun.gupta@amd.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Nipun Gupta and committed by
Greg Kroah-Hartman
eb96b740 c47a88e1

+1853 -1
+2
drivers/cdx/Kconfig
··· 15 15 of CDX devices. CDX devices are memory mapped on system bus 16 16 for embedded CPUs. CDX bus uses CDX controller and firmware 17 17 to scan these CDX devices. 18 + 19 + source "drivers/cdx/controller/Kconfig"
+1 -1
drivers/cdx/Makefile
··· 5 5 # Copyright (C) 2022-2023, Advanced Micro Devices, Inc. 6 6 # 7 7 8 - obj-$(CONFIG_CDX_BUS) += cdx.o 8 + obj-$(CONFIG_CDX_BUS) += cdx.o controller/
+20
drivers/cdx/controller/Kconfig
··· 1 + # SPDX-License-Identifier: GPL-2.0-only 2 + # 3 + # CDX controller configuration 4 + # 5 + # Copyright (C) 2022-2023, Advanced Micro Devices, Inc. 6 + # 7 + 8 + if CDX_BUS 9 + 10 + config MCDI_LOGGING 11 + bool "MCDI Logging for the CDX controller" 12 + depends on CDX_CONTROLLER 13 + help 14 + Enable MCDI Logging for 15 + the CDX Controller for debug 16 + purpose. 17 + 18 + If unsure, say N. 19 + 20 + endif
+9
drivers/cdx/controller/Makefile
··· 1 + # SPDX-License-Identifier: GPL-2.0-only 2 + # 3 + # Makefile for CDX controller drivers 4 + # 5 + # Copyright (C) 2022-2023, Advanced Micro Devices, Inc. 6 + # 7 + 8 + obj-$(CONFIG_CDX_CONTROLLER) += cdx-controller.o 9 + cdx-controller-objs := mcdi.o
+90
drivers/cdx/controller/bitfield.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 2 + * 3 + * Copyright 2005-2006 Fen Systems Ltd. 4 + * Copyright 2006-2013 Solarflare Communications Inc. 5 + * Copyright (C) 2022-2023, Advanced Micro Devices, Inc. 6 + */ 7 + 8 + #ifndef CDX_BITFIELD_H 9 + #define CDX_BITFIELD_H 10 + 11 + #include <linux/bitfield.h> 12 + 13 + /* Lowest bit numbers and widths */ 14 + #define CDX_DWORD_LBN 0 15 + #define CDX_DWORD_WIDTH 32 16 + 17 + /* Specified attribute (e.g. LBN) of the specified field */ 18 + #define CDX_VAL(field, attribute) field ## _ ## attribute 19 + /* Low bit number of the specified field */ 20 + #define CDX_LOW_BIT(field) CDX_VAL(field, LBN) 21 + /* Bit width of the specified field */ 22 + #define CDX_WIDTH(field) CDX_VAL(field, WIDTH) 23 + /* High bit number of the specified field */ 24 + #define CDX_HIGH_BIT(field) (CDX_LOW_BIT(field) + CDX_WIDTH(field) - 1) 25 + 26 + /* A doubleword (i.e. 4 byte) datatype - little-endian in HW */ 27 + struct cdx_dword { 28 + __le32 cdx_u32; 29 + }; 30 + 31 + /* Value expanders for printk */ 32 + #define CDX_DWORD_VAL(dword) \ 33 + ((unsigned int)le32_to_cpu((dword).cdx_u32)) 34 + 35 + /* 36 + * Extract bit field portion [low,high) from the 32-bit little-endian 37 + * element which contains bits [min,max) 38 + */ 39 + #define CDX_DWORD_FIELD(dword, field) \ 40 + (FIELD_GET(GENMASK(CDX_HIGH_BIT(field), CDX_LOW_BIT(field)), \ 41 + le32_to_cpu((dword).cdx_u32))) 42 + 43 + /* 44 + * Creates the portion of the named bit field that lies within the 45 + * range [min,max). 46 + */ 47 + #define CDX_INSERT_FIELD(field, value) \ 48 + (FIELD_PREP(GENMASK(CDX_HIGH_BIT(field), \ 49 + CDX_LOW_BIT(field)), value)) 50 + 51 + /* 52 + * Creates the portion of the named bit fields that lie within the 53 + * range [min,max). 54 + */ 55 + #define CDX_INSERT_FIELDS(field1, value1, \ 56 + field2, value2, \ 57 + field3, value3, \ 58 + field4, value4, \ 59 + field5, value5, \ 60 + field6, value6, \ 61 + field7, value7) \ 62 + (CDX_INSERT_FIELD(field1, (value1)) | \ 63 + CDX_INSERT_FIELD(field2, (value2)) | \ 64 + CDX_INSERT_FIELD(field3, (value3)) | \ 65 + CDX_INSERT_FIELD(field4, (value4)) | \ 66 + CDX_INSERT_FIELD(field5, (value5)) | \ 67 + CDX_INSERT_FIELD(field6, (value6)) | \ 68 + CDX_INSERT_FIELD(field7, (value7))) 69 + 70 + #define CDX_POPULATE_DWORD(dword, ...) \ 71 + (dword).cdx_u32 = cpu_to_le32(CDX_INSERT_FIELDS(__VA_ARGS__)) 72 + 73 + /* Populate a dword field with various numbers of arguments */ 74 + #define CDX_POPULATE_DWORD_7 CDX_POPULATE_DWORD 75 + #define CDX_POPULATE_DWORD_6(dword, ...) \ 76 + CDX_POPULATE_DWORD_7(dword, CDX_DWORD, 0, __VA_ARGS__) 77 + #define CDX_POPULATE_DWORD_5(dword, ...) \ 78 + CDX_POPULATE_DWORD_6(dword, CDX_DWORD, 0, __VA_ARGS__) 79 + #define CDX_POPULATE_DWORD_4(dword, ...) \ 80 + CDX_POPULATE_DWORD_5(dword, CDX_DWORD, 0, __VA_ARGS__) 81 + #define CDX_POPULATE_DWORD_3(dword, ...) \ 82 + CDX_POPULATE_DWORD_4(dword, CDX_DWORD, 0, __VA_ARGS__) 83 + #define CDX_POPULATE_DWORD_2(dword, ...) \ 84 + CDX_POPULATE_DWORD_3(dword, CDX_DWORD, 0, __VA_ARGS__) 85 + #define CDX_POPULATE_DWORD_1(dword, ...) \ 86 + CDX_POPULATE_DWORD_2(dword, CDX_DWORD, 0, __VA_ARGS__) 87 + #define CDX_SET_DWORD(dword) \ 88 + CDX_POPULATE_DWORD_1(dword, CDX_DWORD, 0xffffffff) 89 + 90 + #endif /* CDX_BITFIELD_H */
+590
drivers/cdx/controller/mc_cdx_pcol.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 2 + * 3 + * Driver for AMD network controllers and boards 4 + * 5 + * Copyright (C) 2021, Xilinx, Inc. 6 + * Copyright (C) 2022-2023, Advanced Micro Devices, Inc. 7 + */ 8 + 9 + #ifndef MC_CDX_PCOL_H 10 + #define MC_CDX_PCOL_H 11 + 12 + /* The current version of the MCDI protocol. */ 13 + #define MCDI_PCOL_VERSION 2 14 + 15 + /* 16 + * Each MCDI request starts with an MCDI_HEADER, which is a 32bit 17 + * structure, filled in by the client. 18 + * 19 + * 0 7 8 16 20 22 23 24 31 20 + * | CODE | R | LEN | SEQ | Rsvd | E | R | XFLAGS | 21 + * | | | 22 + * | | \--- Response 23 + * | \------- Error 24 + * \------------------------------ Resync (always set) 25 + * 26 + * The client writes its request into MC shared memory, and rings the 27 + * doorbell. Each request is completed either by the MC writing 28 + * back into shared memory, or by writing out an event. 29 + * 30 + * All MCDI commands support completion by shared memory response. Each 31 + * request may also contain additional data (accounted for by HEADER.LEN), 32 + * and some responses may also contain additional data (again, accounted 33 + * for by HEADER.LEN). 34 + * 35 + * Some MCDI commands support completion by event, in which any associated 36 + * response data is included in the event. 37 + * 38 + * The protocol requires one response to be delivered for every request; a 39 + * request should not be sent unless the response for the previous request 40 + * has been received (either by polling shared memory, or by receiving 41 + * an event). 42 + */ 43 + 44 + /** Request/Response structure */ 45 + #define MCDI_HEADER_OFST 0 46 + #define MCDI_HEADER_CODE_LBN 0 47 + #define MCDI_HEADER_CODE_WIDTH 7 48 + #define MCDI_HEADER_RESYNC_LBN 7 49 + #define MCDI_HEADER_RESYNC_WIDTH 1 50 + #define MCDI_HEADER_DATALEN_LBN 8 51 + #define MCDI_HEADER_DATALEN_WIDTH 8 52 + #define MCDI_HEADER_SEQ_LBN 16 53 + #define MCDI_HEADER_SEQ_WIDTH 4 54 + #define MCDI_HEADER_RSVD_LBN 20 55 + #define MCDI_HEADER_RSVD_WIDTH 1 56 + #define MCDI_HEADER_NOT_EPOCH_LBN 21 57 + #define MCDI_HEADER_NOT_EPOCH_WIDTH 1 58 + #define MCDI_HEADER_ERROR_LBN 22 59 + #define MCDI_HEADER_ERROR_WIDTH 1 60 + #define MCDI_HEADER_RESPONSE_LBN 23 61 + #define MCDI_HEADER_RESPONSE_WIDTH 1 62 + #define MCDI_HEADER_XFLAGS_LBN 24 63 + #define MCDI_HEADER_XFLAGS_WIDTH 8 64 + /* Request response using event */ 65 + #define MCDI_HEADER_XFLAGS_EVREQ 0x01 66 + /* Request (and signal) early doorbell return */ 67 + #define MCDI_HEADER_XFLAGS_DBRET 0x02 68 + 69 + /* Maximum number of payload bytes */ 70 + #define MCDI_CTL_SDU_LEN_MAX_V2 0x400 71 + 72 + #define MCDI_CTL_SDU_LEN_MAX MCDI_CTL_SDU_LEN_MAX_V2 73 + 74 + /* 75 + * The MC can generate events for two reasons: 76 + * - To advance a shared memory request if XFLAGS_EVREQ was set 77 + * - As a notification (link state, i2c event), controlled 78 + * via MC_CMD_LOG_CTRL 79 + * 80 + * Both events share a common structure: 81 + * 82 + * 0 32 33 36 44 52 60 83 + * | Data | Cont | Level | Src | Code | Rsvd | 84 + * | 85 + * \ There is another event pending in this notification 86 + * 87 + * If Code==CMDDONE, then the fields are further interpreted as: 88 + * 89 + * - LEVEL==INFO Command succeeded 90 + * - LEVEL==ERR Command failed 91 + * 92 + * 0 8 16 24 32 93 + * | Seq | Datalen | Errno | Rsvd | 94 + * 95 + * These fields are taken directly out of the standard MCDI header, i.e., 96 + * LEVEL==ERR, Datalen == 0 => Reboot 97 + * 98 + * Events can be squirted out of the UART (using LOG_CTRL) without a 99 + * MCDI header. An event can be distinguished from a MCDI response by 100 + * examining the first byte which is 0xc0. This corresponds to the 101 + * non-existent MCDI command MC_CMD_DEBUG_LOG. 102 + * 103 + * 0 7 8 104 + * | command | Resync | = 0xc0 105 + * 106 + * Since the event is written in big-endian byte order, this works 107 + * providing bits 56-63 of the event are 0xc0. 108 + * 109 + * 56 60 63 110 + * | Rsvd | Code | = 0xc0 111 + * 112 + * Which means for convenience the event code is 0xc for all MC 113 + * generated events. 114 + */ 115 + 116 + /* 117 + * the errno value may be followed by the (0-based) number of the 118 + * first argument that could not be processed. 119 + */ 120 + #define MC_CMD_ERR_ARG_OFST 4 121 + 122 + /* MC_CMD_ERR MCDI error codes. */ 123 + /* Operation not permitted. */ 124 + #define MC_CMD_ERR_EPERM 0x1 125 + /* Non-existent command target */ 126 + #define MC_CMD_ERR_ENOENT 0x2 127 + /* assert() has killed the MC */ 128 + #define MC_CMD_ERR_EINTR 0x4 129 + /* I/O failure */ 130 + #define MC_CMD_ERR_EIO 0x5 131 + /* Already exists */ 132 + #define MC_CMD_ERR_EEXIST 0x6 133 + /* Try again */ 134 + #define MC_CMD_ERR_EAGAIN 0xb 135 + /* Out of memory */ 136 + #define MC_CMD_ERR_ENOMEM 0xc 137 + /* Caller does not hold required locks */ 138 + #define MC_CMD_ERR_EACCES 0xd 139 + /* Resource is currently unavailable (e.g. lock contention) */ 140 + #define MC_CMD_ERR_EBUSY 0x10 141 + /* No such device */ 142 + #define MC_CMD_ERR_ENODEV 0x13 143 + /* Invalid argument to target */ 144 + #define MC_CMD_ERR_EINVAL 0x16 145 + /* No space */ 146 + #define MC_CMD_ERR_ENOSPC 0x1c 147 + /* Read-only */ 148 + #define MC_CMD_ERR_EROFS 0x1e 149 + /* Broken pipe */ 150 + #define MC_CMD_ERR_EPIPE 0x20 151 + /* Out of range */ 152 + #define MC_CMD_ERR_ERANGE 0x22 153 + /* Non-recursive resource is already acquired */ 154 + #define MC_CMD_ERR_EDEADLK 0x23 155 + /* Operation not implemented */ 156 + #define MC_CMD_ERR_ENOSYS 0x26 157 + /* Operation timed out */ 158 + #define MC_CMD_ERR_ETIME 0x3e 159 + /* Link has been severed */ 160 + #define MC_CMD_ERR_ENOLINK 0x43 161 + /* Protocol error */ 162 + #define MC_CMD_ERR_EPROTO 0x47 163 + /* Bad message */ 164 + #define MC_CMD_ERR_EBADMSG 0x4a 165 + /* Operation not supported */ 166 + #define MC_CMD_ERR_ENOTSUP 0x5f 167 + /* Address not available */ 168 + #define MC_CMD_ERR_EADDRNOTAVAIL 0x63 169 + /* Not connected */ 170 + #define MC_CMD_ERR_ENOTCONN 0x6b 171 + /* Operation already in progress */ 172 + #define MC_CMD_ERR_EALREADY 0x72 173 + /* Stale handle. The handle references resource that no longer exists */ 174 + #define MC_CMD_ERR_ESTALE 0x74 175 + /* Resource allocation failed. */ 176 + #define MC_CMD_ERR_ALLOC_FAIL 0x1000 177 + /* V-adaptor not found. */ 178 + #define MC_CMD_ERR_NO_VADAPTOR 0x1001 179 + /* EVB port not found. */ 180 + #define MC_CMD_ERR_NO_EVB_PORT 0x1002 181 + /* V-switch not found. */ 182 + #define MC_CMD_ERR_NO_VSWITCH 0x1003 183 + /* Too many VLAN tags. */ 184 + #define MC_CMD_ERR_VLAN_LIMIT 0x1004 185 + /* Bad PCI function number. */ 186 + #define MC_CMD_ERR_BAD_PCI_FUNC 0x1005 187 + /* Invalid VLAN mode. */ 188 + #define MC_CMD_ERR_BAD_VLAN_MODE 0x1006 189 + /* Invalid v-switch type. */ 190 + #define MC_CMD_ERR_BAD_VSWITCH_TYPE 0x1007 191 + /* Invalid v-port type. */ 192 + #define MC_CMD_ERR_BAD_VPORT_TYPE 0x1008 193 + /* MAC address exists. */ 194 + #define MC_CMD_ERR_MAC_EXIST 0x1009 195 + /* Slave core not present */ 196 + #define MC_CMD_ERR_SLAVE_NOT_PRESENT 0x100a 197 + /* The datapath is disabled. */ 198 + #define MC_CMD_ERR_DATAPATH_DISABLED 0x100b 199 + /* The requesting client is not a function */ 200 + #define MC_CMD_ERR_CLIENT_NOT_FN 0x100c 201 + /* 202 + * The requested operation might require the command to be passed between 203 + * MCs, and the transport doesn't support that. Should only ever been seen over 204 + * the UART. 205 + */ 206 + #define MC_CMD_ERR_NO_PRIVILEGE 0x1013 207 + /* 208 + * Workaround 26807 could not be turned on/off because some functions 209 + * have already installed filters. See the comment at 210 + * MC_CMD_WORKAROUND_BUG26807. May also returned for other operations such as 211 + * sub-variant switching. 212 + */ 213 + #define MC_CMD_ERR_FILTERS_PRESENT 0x1014 214 + /* The clock whose frequency you've attempted to set doesn't exist */ 215 + #define MC_CMD_ERR_NO_CLOCK 0x1015 216 + /* 217 + * Returned by MC_CMD_TESTASSERT if the action that should have caused an 218 + * assertion failed to do so. 219 + */ 220 + #define MC_CMD_ERR_UNREACHABLE 0x1016 221 + /* 222 + * This command needs to be processed in the background but there were no 223 + * resources to do so. Send it again after a command has completed. 224 + */ 225 + #define MC_CMD_ERR_QUEUE_FULL 0x1017 226 + /* 227 + * The operation could not be completed because the PCIe link has gone 228 + * away. This error code is never expected to be returned over the TLP 229 + * transport. 230 + */ 231 + #define MC_CMD_ERR_NO_PCIE 0x1018 232 + /* 233 + * The operation could not be completed because the datapath has gone 234 + * away. This is distinct from MC_CMD_ERR_DATAPATH_DISABLED in that the 235 + * datapath absence may be temporary 236 + */ 237 + #define MC_CMD_ERR_NO_DATAPATH 0x1019 238 + /* The operation could not complete because some VIs are allocated */ 239 + #define MC_CMD_ERR_VIS_PRESENT 0x101a 240 + /* 241 + * The operation could not complete because some PIO buffers are 242 + * allocated 243 + */ 244 + #define MC_CMD_ERR_PIOBUFS_PRESENT 0x101b 245 + 246 + /***********************************/ 247 + /* 248 + * MC_CMD_CDX_BUS_ENUM_BUSES 249 + * CDX bus hosts devices (functions) that are implemented using the Composable 250 + * DMA subsystem and directly mapped into the memory space of the FGPA PSX 251 + * Application Processors (APUs). As such, they only apply to the PSX APU side, 252 + * not the host (PCIe). Unlike PCIe, these devices have no native configuration 253 + * space or enumeration mechanism, so this message set provides a minimal 254 + * interface for discovery and management (bus reset, FLR, BME) of such 255 + * devices. This command returns the number of CDX buses present in the system. 256 + */ 257 + #define MC_CMD_CDX_BUS_ENUM_BUSES 0x1 258 + #define MC_CMD_CDX_BUS_ENUM_BUSES_MSGSET 0x1 259 + #undef MC_CMD_0x1_PRIVILEGE_CTG 260 + 261 + #define MC_CMD_0x1_PRIVILEGE_CTG SRIOV_CTG_ADMIN 262 + 263 + /* MC_CMD_CDX_BUS_ENUM_BUSES_IN msgrequest */ 264 + #define MC_CMD_CDX_BUS_ENUM_BUSES_IN_LEN 0 265 + 266 + /* MC_CMD_CDX_BUS_ENUM_BUSES_OUT msgresponse */ 267 + #define MC_CMD_CDX_BUS_ENUM_BUSES_OUT_LEN 4 268 + /* 269 + * Number of CDX buses present in the system. Buses are numbered 0 to 270 + * BUS_COUNT-1 271 + */ 272 + #define MC_CMD_CDX_BUS_ENUM_BUSES_OUT_BUS_COUNT_OFST 0 273 + #define MC_CMD_CDX_BUS_ENUM_BUSES_OUT_BUS_COUNT_LEN 4 274 + 275 + /***********************************/ 276 + /* 277 + * MC_CMD_CDX_BUS_ENUM_DEVICES 278 + * Enumerate CDX bus devices on a given bus 279 + */ 280 + #define MC_CMD_CDX_BUS_ENUM_DEVICES 0x2 281 + #define MC_CMD_CDX_BUS_ENUM_DEVICES_MSGSET 0x2 282 + #undef MC_CMD_0x2_PRIVILEGE_CTG 283 + 284 + #define MC_CMD_0x2_PRIVILEGE_CTG SRIOV_CTG_ADMIN 285 + 286 + /* MC_CMD_CDX_BUS_ENUM_DEVICES_IN msgrequest */ 287 + #define MC_CMD_CDX_BUS_ENUM_DEVICES_IN_LEN 4 288 + /* 289 + * Bus number to enumerate, in range 0 to BUS_COUNT-1, as returned by 290 + * MC_CMD_CDX_BUS_ENUM_BUSES_OUT 291 + */ 292 + #define MC_CMD_CDX_BUS_ENUM_DEVICES_IN_BUS_OFST 0 293 + #define MC_CMD_CDX_BUS_ENUM_DEVICES_IN_BUS_LEN 4 294 + 295 + /* MC_CMD_CDX_BUS_ENUM_DEVICES_OUT msgresponse */ 296 + #define MC_CMD_CDX_BUS_ENUM_DEVICES_OUT_LEN 4 297 + /* 298 + * Number of devices present on the bus. Devices on the bus are numbered 0 to 299 + * DEVICE_COUNT-1. Returns EAGAIN if number of devices unknown or if the target 300 + * devices are not ready (e.g. undergoing a bus reset) 301 + */ 302 + #define MC_CMD_CDX_BUS_ENUM_DEVICES_OUT_DEVICE_COUNT_OFST 0 303 + #define MC_CMD_CDX_BUS_ENUM_DEVICES_OUT_DEVICE_COUNT_LEN 4 304 + 305 + /***********************************/ 306 + /* 307 + * MC_CMD_CDX_BUS_GET_DEVICE_CONFIG 308 + * Returns device identification and MMIO/MSI resource data for a CDX device. 309 + * The expected usage is for the caller to first retrieve the number of devices 310 + * on the bus using MC_CMD_BUS_ENUM_DEVICES, then loop through the range (0, 311 + * DEVICE_COUNT - 1), retrieving device resource data. May return EAGAIN if the 312 + * number of exposed devices or device resources change during enumeration (due 313 + * to e.g. a PL reload / bus reset), in which case the caller is expected to 314 + * restart the enumeration loop. MMIO addresses are specified in terms of bus 315 + * addresses (prior to any potential IOMMU translation). For versal-net, these 316 + * are equivalent to APU physical addresses. Implementation note - for this to 317 + * work, the implementation needs to keep state (generation count) per client. 318 + */ 319 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG 0x3 320 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_MSGSET 0x3 321 + #undef MC_CMD_0x3_PRIVILEGE_CTG 322 + 323 + #define MC_CMD_0x3_PRIVILEGE_CTG SRIOV_CTG_ADMIN 324 + 325 + /* MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_IN msgrequest */ 326 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_IN_LEN 8 327 + /* Device bus number, in range 0 to BUS_COUNT-1 */ 328 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_IN_BUS_OFST 0 329 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_IN_BUS_LEN 4 330 + /* Device number relative to the bus, in range 0 to DEVICE_COUNT-1 for that bus */ 331 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_IN_DEVICE_OFST 4 332 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_IN_DEVICE_LEN 4 333 + 334 + /* MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT msgresponse */ 335 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_LEN 88 336 + /* 16-bit Vendor identifier, compliant with PCI-SIG VendorID assignment. */ 337 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_VENDOR_ID_OFST 0 338 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_VENDOR_ID_LEN 2 339 + /* 16-bit Device ID assigned by the vendor */ 340 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_DEVICE_ID_OFST 2 341 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_DEVICE_ID_LEN 2 342 + /* 343 + * 16-bit Subsystem Vendor ID, , compliant with PCI-SIG VendorID assignment. 344 + * For further device differentiation, as required. 0 if unused. 345 + */ 346 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_SUBSYS_VENDOR_ID_OFST 4 347 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_SUBSYS_VENDOR_ID_LEN 2 348 + /* 349 + * 16-bit Subsystem Device ID assigned by the vendor. For further device 350 + * differentiation, as required. 0 if unused. 351 + */ 352 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_SUBSYS_DEVICE_ID_OFST 6 353 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_SUBSYS_DEVICE_ID_LEN 2 354 + /* 24-bit Device Class code, compliant with PCI-SIG Device Class codes */ 355 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_DEVICE_CLASS_OFST 8 356 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_DEVICE_CLASS_LEN 3 357 + /* 8-bit vendor-assigned revision */ 358 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_DEVICE_REVISION_OFST 11 359 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_DEVICE_REVISION_LEN 1 360 + /* Reserved (alignment) */ 361 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_RESERVED_OFST 12 362 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_RESERVED_LEN 4 363 + /* MMIO region 0 base address (bus address), 0 if unused */ 364 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_BASE_OFST 16 365 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_BASE_LEN 8 366 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_BASE_LO_OFST 16 367 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_BASE_LO_LEN 4 368 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_BASE_LO_LBN 128 369 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_BASE_LO_WIDTH 32 370 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_BASE_HI_OFST 20 371 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_BASE_HI_LEN 4 372 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_BASE_HI_LBN 160 373 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_BASE_HI_WIDTH 32 374 + /* MMIO region 0 size, 0 if unused */ 375 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_SIZE_OFST 24 376 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_SIZE_LEN 8 377 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_SIZE_LO_OFST 24 378 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_SIZE_LO_LEN 4 379 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_SIZE_LO_LBN 192 380 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_SIZE_LO_WIDTH 32 381 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_SIZE_HI_OFST 28 382 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_SIZE_HI_LEN 4 383 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_SIZE_HI_LBN 224 384 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION0_SIZE_HI_WIDTH 32 385 + /* MMIO region 1 base address (bus address), 0 if unused */ 386 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_BASE_OFST 32 387 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_BASE_LEN 8 388 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_BASE_LO_OFST 32 389 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_BASE_LO_LEN 4 390 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_BASE_LO_LBN 256 391 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_BASE_LO_WIDTH 32 392 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_BASE_HI_OFST 36 393 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_BASE_HI_LEN 4 394 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_BASE_HI_LBN 288 395 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_BASE_HI_WIDTH 32 396 + /* MMIO region 1 size, 0 if unused */ 397 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_SIZE_OFST 40 398 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_SIZE_LEN 8 399 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_SIZE_LO_OFST 40 400 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_SIZE_LO_LEN 4 401 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_SIZE_LO_LBN 320 402 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_SIZE_LO_WIDTH 32 403 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_SIZE_HI_OFST 44 404 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_SIZE_HI_LEN 4 405 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_SIZE_HI_LBN 352 406 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION1_SIZE_HI_WIDTH 32 407 + /* MMIO region 2 base address (bus address), 0 if unused */ 408 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_BASE_OFST 48 409 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_BASE_LEN 8 410 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_BASE_LO_OFST 48 411 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_BASE_LO_LEN 4 412 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_BASE_LO_LBN 384 413 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_BASE_LO_WIDTH 32 414 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_BASE_HI_OFST 52 415 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_BASE_HI_LEN 4 416 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_BASE_HI_LBN 416 417 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_BASE_HI_WIDTH 32 418 + /* MMIO region 2 size, 0 if unused */ 419 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_SIZE_OFST 56 420 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_SIZE_LEN 8 421 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_SIZE_LO_OFST 56 422 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_SIZE_LO_LEN 4 423 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_SIZE_LO_LBN 448 424 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_SIZE_LO_WIDTH 32 425 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_SIZE_HI_OFST 60 426 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_SIZE_HI_LEN 4 427 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_SIZE_HI_LBN 480 428 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION2_SIZE_HI_WIDTH 32 429 + /* MMIO region 3 base address (bus address), 0 if unused */ 430 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_BASE_OFST 64 431 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_BASE_LEN 8 432 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_BASE_LO_OFST 64 433 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_BASE_LO_LEN 4 434 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_BASE_LO_LBN 512 435 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_BASE_LO_WIDTH 32 436 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_BASE_HI_OFST 68 437 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_BASE_HI_LEN 4 438 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_BASE_HI_LBN 544 439 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_BASE_HI_WIDTH 32 440 + /* MMIO region 3 size, 0 if unused */ 441 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_SIZE_OFST 72 442 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_SIZE_LEN 8 443 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_SIZE_LO_OFST 72 444 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_SIZE_LO_LEN 4 445 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_SIZE_LO_LBN 576 446 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_SIZE_LO_WIDTH 32 447 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_SIZE_HI_OFST 76 448 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_SIZE_HI_LEN 4 449 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_SIZE_HI_LBN 608 450 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MMIO_REGION3_SIZE_HI_WIDTH 32 451 + /* MSI vector count */ 452 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MSI_COUNT_OFST 80 453 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_MSI_COUNT_LEN 4 454 + /* Requester ID used by device (SMMU StreamID, GIC ITS DeviceID) */ 455 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_REQUESTER_ID_OFST 84 456 + #define MC_CMD_CDX_BUS_GET_DEVICE_CONFIG_OUT_REQUESTER_ID_LEN 4 457 + 458 + /***********************************/ 459 + /* 460 + * MC_CMD_CDX_DEVICE_RESET 461 + * After this call completes, device DMA and interrupts are quiesced, devices 462 + * logic is reset in a hardware-specific way and DMA bus mastering is disabled. 463 + */ 464 + #define MC_CMD_CDX_DEVICE_RESET 0x6 465 + #define MC_CMD_CDX_DEVICE_RESET_MSGSET 0x6 466 + #undef MC_CMD_0x6_PRIVILEGE_CTG 467 + 468 + #define MC_CMD_0x6_PRIVILEGE_CTG SRIOV_CTG_ADMIN 469 + 470 + /* MC_CMD_CDX_DEVICE_RESET_IN msgrequest */ 471 + #define MC_CMD_CDX_DEVICE_RESET_IN_LEN 8 472 + /* Device bus number, in range 0 to BUS_COUNT-1 */ 473 + #define MC_CMD_CDX_DEVICE_RESET_IN_BUS_OFST 0 474 + #define MC_CMD_CDX_DEVICE_RESET_IN_BUS_LEN 4 475 + /* Device number relative to the bus, in range 0 to DEVICE_COUNT-1 for that bus */ 476 + #define MC_CMD_CDX_DEVICE_RESET_IN_DEVICE_OFST 4 477 + #define MC_CMD_CDX_DEVICE_RESET_IN_DEVICE_LEN 4 478 + 479 + /* 480 + * MC_CMD_CDX_DEVICE_RESET_OUT msgresponse: The device is quiesced and all 481 + * pending device initiated DMA has completed. 482 + */ 483 + #define MC_CMD_CDX_DEVICE_RESET_OUT_LEN 0 484 + 485 + /***********************************/ 486 + /* 487 + * MC_CMD_CDX_DEVICE_CONTROL_SET 488 + * If BUS_MASTER is set to disabled, device DMA and interrupts are quiesced. 489 + * Pending DMA requests and MSI interrupts are flushed and no further DMA or 490 + * interrupts are issued after this command returns. If BUS_MASTER is set to 491 + * enabled, device is allowed to initiate DMA. Whether interrupts are enabled 492 + * also depends on the value of MSI_ENABLE bit. Note that, in this case, the 493 + * device may start DMA before the host receives and processes the MCDI 494 + * response. MSI_ENABLE masks or unmasks device interrupts only. Note that for 495 + * interrupts to be delivered to the host, both BUS_MASTER and MSI_ENABLE needs 496 + * to be set. MMIO_REGIONS_ENABLE enables or disables host accesses to device 497 + * MMIO regions. Note that an implementation is allowed to permanently set this 498 + * bit to 1, in which case MC_CMD_CDX_DEVICE_CONTROL_GET will always return 1 499 + * for this bit, regardless of the value set here. 500 + */ 501 + #define MC_CMD_CDX_DEVICE_CONTROL_SET 0x7 502 + #define MC_CMD_CDX_DEVICE_CONTROL_SET_MSGSET 0x7 503 + #undef MC_CMD_0x7_PRIVILEGE_CTG 504 + 505 + #define MC_CMD_0x7_PRIVILEGE_CTG SRIOV_CTG_ADMIN 506 + 507 + /* MC_CMD_CDX_DEVICE_CONTROL_SET_IN msgrequest */ 508 + #define MC_CMD_CDX_DEVICE_CONTROL_SET_IN_LEN 12 509 + /* Device bus number, in range 0 to BUS_COUNT-1 */ 510 + #define MC_CMD_CDX_DEVICE_CONTROL_SET_IN_BUS_OFST 0 511 + #define MC_CMD_CDX_DEVICE_CONTROL_SET_IN_BUS_LEN 4 512 + /* Device number relative to the bus, in range 0 to DEVICE_COUNT-1 for that bus */ 513 + #define MC_CMD_CDX_DEVICE_CONTROL_SET_IN_DEVICE_OFST 4 514 + #define MC_CMD_CDX_DEVICE_CONTROL_SET_IN_DEVICE_LEN 4 515 + #define MC_CMD_CDX_DEVICE_CONTROL_SET_IN_FLAGS_OFST 8 516 + #define MC_CMD_CDX_DEVICE_CONTROL_SET_IN_FLAGS_LEN 4 517 + #define MC_CMD_CDX_DEVICE_CONTROL_SET_IN_BUS_MASTER_ENABLE_OFST 8 518 + #define MC_CMD_CDX_DEVICE_CONTROL_SET_IN_BUS_MASTER_ENABLE_LBN 0 519 + #define MC_CMD_CDX_DEVICE_CONTROL_SET_IN_BUS_MASTER_ENABLE_WIDTH 1 520 + #define MC_CMD_CDX_DEVICE_CONTROL_SET_IN_MSI_ENABLE_OFST 8 521 + #define MC_CMD_CDX_DEVICE_CONTROL_SET_IN_MSI_ENABLE_LBN 1 522 + #define MC_CMD_CDX_DEVICE_CONTROL_SET_IN_MSI_ENABLE_WIDTH 1 523 + #define MC_CMD_CDX_DEVICE_CONTROL_SET_IN_MMIO_REGIONS_ENABLE_OFST 8 524 + #define MC_CMD_CDX_DEVICE_CONTROL_SET_IN_MMIO_REGIONS_ENABLE_LBN 2 525 + #define MC_CMD_CDX_DEVICE_CONTROL_SET_IN_MMIO_REGIONS_ENABLE_WIDTH 1 526 + 527 + /* MC_CMD_CDX_DEVICE_CONTROL_SET_OUT msgresponse */ 528 + #define MC_CMD_CDX_DEVICE_CONTROL_SET_OUT_LEN 0 529 + 530 + /***********************************/ 531 + /* 532 + * MC_CMD_CDX_DEVICE_CONTROL_GET 533 + * Returns device DMA, interrupt and MMIO region access control bits. See 534 + * MC_CMD_CDX_DEVICE_CONTROL_SET for definition of the available control bits. 535 + */ 536 + #define MC_CMD_CDX_DEVICE_CONTROL_GET 0x8 537 + #define MC_CMD_CDX_DEVICE_CONTROL_GET_MSGSET 0x8 538 + #undef MC_CMD_0x8_PRIVILEGE_CTG 539 + 540 + #define MC_CMD_0x8_PRIVILEGE_CTG SRIOV_CTG_ADMIN 541 + 542 + /* MC_CMD_CDX_DEVICE_CONTROL_GET_IN msgrequest */ 543 + #define MC_CMD_CDX_DEVICE_CONTROL_GET_IN_LEN 8 544 + /* Device bus number, in range 0 to BUS_COUNT-1 */ 545 + #define MC_CMD_CDX_DEVICE_CONTROL_GET_IN_BUS_OFST 0 546 + #define MC_CMD_CDX_DEVICE_CONTROL_GET_IN_BUS_LEN 4 547 + /* Device number relative to the bus, in range 0 to DEVICE_COUNT-1 for that bus */ 548 + #define MC_CMD_CDX_DEVICE_CONTROL_GET_IN_DEVICE_OFST 4 549 + #define MC_CMD_CDX_DEVICE_CONTROL_GET_IN_DEVICE_LEN 4 550 + 551 + /* MC_CMD_CDX_DEVICE_CONTROL_GET_OUT msgresponse */ 552 + #define MC_CMD_CDX_DEVICE_CONTROL_GET_OUT_LEN 4 553 + #define MC_CMD_CDX_DEVICE_CONTROL_GET_OUT_FLAGS_OFST 0 554 + #define MC_CMD_CDX_DEVICE_CONTROL_GET_OUT_FLAGS_LEN 4 555 + #define MC_CMD_CDX_DEVICE_CONTROL_GET_OUT_BUS_MASTER_ENABLE_OFST 0 556 + #define MC_CMD_CDX_DEVICE_CONTROL_GET_OUT_BUS_MASTER_ENABLE_LBN 0 557 + #define MC_CMD_CDX_DEVICE_CONTROL_GET_OUT_BUS_MASTER_ENABLE_WIDTH 1 558 + #define MC_CMD_CDX_DEVICE_CONTROL_GET_OUT_MSI_ENABLE_OFST 0 559 + #define MC_CMD_CDX_DEVICE_CONTROL_GET_OUT_MSI_ENABLE_LBN 1 560 + #define MC_CMD_CDX_DEVICE_CONTROL_GET_OUT_MSI_ENABLE_WIDTH 1 561 + #define MC_CMD_CDX_DEVICE_CONTROL_GET_OUT_MMIO_REGIONS_ENABLE_OFST 0 562 + #define MC_CMD_CDX_DEVICE_CONTROL_GET_OUT_MMIO_REGIONS_ENABLE_LBN 2 563 + #define MC_CMD_CDX_DEVICE_CONTROL_GET_OUT_MMIO_REGIONS_ENABLE_WIDTH 1 564 + 565 + /***********************************/ 566 + /* MC_CMD_V2_EXTN - Encapsulation for a v2 extended command */ 567 + #define MC_CMD_V2_EXTN 0x7f 568 + 569 + /* MC_CMD_V2_EXTN_IN msgrequest */ 570 + #define MC_CMD_V2_EXTN_IN_LEN 4 571 + /* the extended command number */ 572 + #define MC_CMD_V2_EXTN_IN_EXTENDED_CMD_LBN 0 573 + #define MC_CMD_V2_EXTN_IN_EXTENDED_CMD_WIDTH 15 574 + #define MC_CMD_V2_EXTN_IN_UNUSED_LBN 15 575 + #define MC_CMD_V2_EXTN_IN_UNUSED_WIDTH 1 576 + /* the actual length of the encapsulated command */ 577 + #define MC_CMD_V2_EXTN_IN_ACTUAL_LEN_LBN 16 578 + #define MC_CMD_V2_EXTN_IN_ACTUAL_LEN_WIDTH 10 579 + #define MC_CMD_V2_EXTN_IN_UNUSED2_LBN 26 580 + #define MC_CMD_V2_EXTN_IN_UNUSED2_WIDTH 2 581 + /* Type of command/response */ 582 + #define MC_CMD_V2_EXTN_IN_MESSAGE_TYPE_LBN 28 583 + #define MC_CMD_V2_EXTN_IN_MESSAGE_TYPE_WIDTH 4 584 + /* 585 + * enum: MCDI command directed to versal-net. MCDI responses of this type 586 + * are not defined. 587 + */ 588 + #define MC_CMD_V2_EXTN_IN_MCDI_MESSAGE_TYPE_PLATFORM 0x2 589 + 590 + #endif /* MC_CDX_PCOL_H */
+903
drivers/cdx/controller/mcdi.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Management-Controller-to-Driver Interface 4 + * 5 + * Copyright 2008-2013 Solarflare Communications Inc. 6 + * Copyright (C) 2022-2023, Advanced Micro Devices, Inc. 7 + */ 8 + #include <linux/delay.h> 9 + #include <linux/slab.h> 10 + #include <linux/io.h> 11 + #include <linux/spinlock.h> 12 + #include <linux/netdevice.h> 13 + #include <linux/etherdevice.h> 14 + #include <linux/ethtool.h> 15 + #include <linux/if_vlan.h> 16 + #include <linux/timer.h> 17 + #include <linux/list.h> 18 + #include <linux/pci.h> 19 + #include <linux/device.h> 20 + #include <linux/rwsem.h> 21 + #include <linux/vmalloc.h> 22 + #include <net/netevent.h> 23 + #include <linux/log2.h> 24 + #include <linux/net_tstamp.h> 25 + #include <linux/wait.h> 26 + 27 + #include "bitfield.h" 28 + #include "mcdi.h" 29 + 30 + struct cdx_mcdi_copy_buffer { 31 + struct cdx_dword buffer[DIV_ROUND_UP(MCDI_CTL_SDU_LEN_MAX, 4)]; 32 + }; 33 + 34 + #ifdef CONFIG_MCDI_LOGGING 35 + #define LOG_LINE_MAX (1024 - 32) 36 + #endif 37 + 38 + static void cdx_mcdi_cancel_cmd(struct cdx_mcdi *cdx, struct cdx_mcdi_cmd *cmd); 39 + static void cdx_mcdi_wait_for_cleanup(struct cdx_mcdi *cdx); 40 + static int cdx_mcdi_rpc_async_internal(struct cdx_mcdi *cdx, 41 + struct cdx_mcdi_cmd *cmd, 42 + unsigned int *handle); 43 + static void cdx_mcdi_start_or_queue(struct cdx_mcdi_iface *mcdi, 44 + bool allow_retry); 45 + static void cdx_mcdi_cmd_start_or_queue(struct cdx_mcdi_iface *mcdi, 46 + struct cdx_mcdi_cmd *cmd); 47 + static bool cdx_mcdi_complete_cmd(struct cdx_mcdi_iface *mcdi, 48 + struct cdx_mcdi_cmd *cmd, 49 + struct cdx_dword *outbuf, 50 + int len, 51 + struct list_head *cleanup_list); 52 + static void cdx_mcdi_timeout_cmd(struct cdx_mcdi_iface *mcdi, 53 + struct cdx_mcdi_cmd *cmd, 54 + struct list_head *cleanup_list); 55 + static void cdx_mcdi_cmd_work(struct work_struct *context); 56 + static void cdx_mcdi_mode_fail(struct cdx_mcdi *cdx, struct list_head *cleanup_list); 57 + static void _cdx_mcdi_display_error(struct cdx_mcdi *cdx, unsigned int cmd, 58 + size_t inlen, int raw, int arg, int err_no); 59 + 60 + static bool cdx_cmd_cancelled(struct cdx_mcdi_cmd *cmd) 61 + { 62 + return cmd->state == MCDI_STATE_RUNNING_CANCELLED; 63 + } 64 + 65 + static void cdx_mcdi_cmd_release(struct kref *ref) 66 + { 67 + kfree(container_of(ref, struct cdx_mcdi_cmd, ref)); 68 + } 69 + 70 + static unsigned int cdx_mcdi_cmd_handle(struct cdx_mcdi_cmd *cmd) 71 + { 72 + return cmd->handle; 73 + } 74 + 75 + static void _cdx_mcdi_remove_cmd(struct cdx_mcdi_iface *mcdi, 76 + struct cdx_mcdi_cmd *cmd, 77 + struct list_head *cleanup_list) 78 + { 79 + /* if cancelled, the completers have already been called */ 80 + if (cdx_cmd_cancelled(cmd)) 81 + return; 82 + 83 + if (cmd->completer) { 84 + list_add_tail(&cmd->cleanup_list, cleanup_list); 85 + ++mcdi->outstanding_cleanups; 86 + kref_get(&cmd->ref); 87 + } 88 + } 89 + 90 + static void cdx_mcdi_remove_cmd(struct cdx_mcdi_iface *mcdi, 91 + struct cdx_mcdi_cmd *cmd, 92 + struct list_head *cleanup_list) 93 + { 94 + list_del(&cmd->list); 95 + _cdx_mcdi_remove_cmd(mcdi, cmd, cleanup_list); 96 + cmd->state = MCDI_STATE_FINISHED; 97 + kref_put(&cmd->ref, cdx_mcdi_cmd_release); 98 + if (list_empty(&mcdi->cmd_list)) 99 + wake_up(&mcdi->cmd_complete_wq); 100 + } 101 + 102 + static unsigned long cdx_mcdi_rpc_timeout(struct cdx_mcdi *cdx, unsigned int cmd) 103 + { 104 + if (!cdx->mcdi_ops->mcdi_rpc_timeout) 105 + return MCDI_RPC_TIMEOUT; 106 + else 107 + return cdx->mcdi_ops->mcdi_rpc_timeout(cdx, cmd); 108 + } 109 + 110 + int cdx_mcdi_init(struct cdx_mcdi *cdx) 111 + { 112 + struct cdx_mcdi_iface *mcdi; 113 + int rc = -ENOMEM; 114 + 115 + cdx->mcdi = kzalloc(sizeof(*cdx->mcdi), GFP_KERNEL); 116 + if (!cdx->mcdi) 117 + goto fail; 118 + 119 + mcdi = cdx_mcdi_if(cdx); 120 + mcdi->cdx = cdx; 121 + 122 + #ifdef CONFIG_MCDI_LOGGING 123 + mcdi->logging_buffer = kmalloc(LOG_LINE_MAX, GFP_KERNEL); 124 + if (!mcdi->logging_buffer) 125 + goto fail2; 126 + #endif 127 + mcdi->workqueue = alloc_ordered_workqueue("mcdi_wq", 0); 128 + if (!mcdi->workqueue) 129 + goto fail3; 130 + mutex_init(&mcdi->iface_lock); 131 + mcdi->mode = MCDI_MODE_EVENTS; 132 + INIT_LIST_HEAD(&mcdi->cmd_list); 133 + init_waitqueue_head(&mcdi->cmd_complete_wq); 134 + 135 + mcdi->new_epoch = true; 136 + 137 + return 0; 138 + fail3: 139 + #ifdef CONFIG_MCDI_LOGGING 140 + kfree(mcdi->logging_buffer); 141 + fail2: 142 + #endif 143 + kfree(cdx->mcdi); 144 + cdx->mcdi = NULL; 145 + fail: 146 + return rc; 147 + } 148 + 149 + void cdx_mcdi_finish(struct cdx_mcdi *cdx) 150 + { 151 + struct cdx_mcdi_iface *mcdi; 152 + 153 + mcdi = cdx_mcdi_if(cdx); 154 + if (!mcdi) 155 + return; 156 + 157 + cdx_mcdi_wait_for_cleanup(cdx); 158 + 159 + #ifdef CONFIG_MCDI_LOGGING 160 + kfree(mcdi->logging_buffer); 161 + #endif 162 + 163 + destroy_workqueue(mcdi->workqueue); 164 + kfree(cdx->mcdi); 165 + cdx->mcdi = NULL; 166 + } 167 + 168 + static bool cdx_mcdi_flushed(struct cdx_mcdi_iface *mcdi, bool ignore_cleanups) 169 + { 170 + bool flushed; 171 + 172 + mutex_lock(&mcdi->iface_lock); 173 + flushed = list_empty(&mcdi->cmd_list) && 174 + (ignore_cleanups || !mcdi->outstanding_cleanups); 175 + mutex_unlock(&mcdi->iface_lock); 176 + return flushed; 177 + } 178 + 179 + /* Wait for outstanding MCDI commands to complete. */ 180 + static void cdx_mcdi_wait_for_cleanup(struct cdx_mcdi *cdx) 181 + { 182 + struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx); 183 + 184 + if (!mcdi) 185 + return; 186 + 187 + wait_event(mcdi->cmd_complete_wq, 188 + cdx_mcdi_flushed(mcdi, false)); 189 + } 190 + 191 + int cdx_mcdi_wait_for_quiescence(struct cdx_mcdi *cdx, 192 + unsigned int timeout_jiffies) 193 + { 194 + struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx); 195 + DEFINE_WAIT_FUNC(wait, woken_wake_function); 196 + int rc = 0; 197 + 198 + if (!mcdi) 199 + return -EINVAL; 200 + 201 + flush_workqueue(mcdi->workqueue); 202 + 203 + add_wait_queue(&mcdi->cmd_complete_wq, &wait); 204 + 205 + while (!cdx_mcdi_flushed(mcdi, true)) { 206 + rc = wait_woken(&wait, TASK_IDLE, timeout_jiffies); 207 + if (rc) 208 + continue; 209 + break; 210 + } 211 + 212 + remove_wait_queue(&mcdi->cmd_complete_wq, &wait); 213 + 214 + if (rc > 0) 215 + rc = 0; 216 + else if (rc == 0) 217 + rc = -ETIMEDOUT; 218 + 219 + return rc; 220 + } 221 + 222 + static u8 cdx_mcdi_payload_csum(const struct cdx_dword *hdr, size_t hdr_len, 223 + const struct cdx_dword *sdu, size_t sdu_len) 224 + { 225 + u8 *p = (u8 *)hdr; 226 + u8 csum = 0; 227 + int i; 228 + 229 + for (i = 0; i < hdr_len; i++) 230 + csum += p[i]; 231 + 232 + p = (u8 *)sdu; 233 + for (i = 0; i < sdu_len; i++) 234 + csum += p[i]; 235 + 236 + return ~csum & 0xff; 237 + } 238 + 239 + static void cdx_mcdi_send_request(struct cdx_mcdi *cdx, 240 + struct cdx_mcdi_cmd *cmd) 241 + { 242 + struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx); 243 + const struct cdx_dword *inbuf = cmd->inbuf; 244 + size_t inlen = cmd->inlen; 245 + struct cdx_dword hdr[2]; 246 + size_t hdr_len; 247 + bool not_epoch; 248 + u32 xflags; 249 + #ifdef CONFIG_MCDI_LOGGING 250 + char *buf; 251 + #endif 252 + 253 + if (!mcdi) 254 + return; 255 + #ifdef CONFIG_MCDI_LOGGING 256 + buf = mcdi->logging_buffer; /* page-sized */ 257 + #endif 258 + 259 + mcdi->prev_seq = cmd->seq; 260 + mcdi->seq_held_by[cmd->seq] = cmd; 261 + mcdi->db_held_by = cmd; 262 + cmd->started = jiffies; 263 + 264 + not_epoch = !mcdi->new_epoch; 265 + xflags = 0; 266 + 267 + /* MCDI v2 */ 268 + WARN_ON(inlen > MCDI_CTL_SDU_LEN_MAX_V2); 269 + CDX_POPULATE_DWORD_7(hdr[0], 270 + MCDI_HEADER_RESPONSE, 0, 271 + MCDI_HEADER_RESYNC, 1, 272 + MCDI_HEADER_CODE, MC_CMD_V2_EXTN, 273 + MCDI_HEADER_DATALEN, 0, 274 + MCDI_HEADER_SEQ, cmd->seq, 275 + MCDI_HEADER_XFLAGS, xflags, 276 + MCDI_HEADER_NOT_EPOCH, not_epoch); 277 + CDX_POPULATE_DWORD_3(hdr[1], 278 + MC_CMD_V2_EXTN_IN_EXTENDED_CMD, cmd->cmd, 279 + MC_CMD_V2_EXTN_IN_ACTUAL_LEN, inlen, 280 + MC_CMD_V2_EXTN_IN_MESSAGE_TYPE, 281 + MC_CMD_V2_EXTN_IN_MCDI_MESSAGE_TYPE_PLATFORM); 282 + hdr_len = 8; 283 + 284 + #ifdef CONFIG_MCDI_LOGGING 285 + if (!WARN_ON_ONCE(!buf)) { 286 + const struct cdx_dword *frags[] = { hdr, inbuf }; 287 + const size_t frag_len[] = { hdr_len, round_up(inlen, 4) }; 288 + int bytes = 0; 289 + int i, j; 290 + 291 + for (j = 0; j < ARRAY_SIZE(frags); j++) { 292 + const struct cdx_dword *frag; 293 + 294 + frag = frags[j]; 295 + for (i = 0; 296 + i < frag_len[j] / 4; 297 + i++) { 298 + /* 299 + * Do not exceed the internal printk limit. 300 + * The string before that is just over 70 bytes. 301 + */ 302 + if ((bytes + 75) > LOG_LINE_MAX) { 303 + pr_info("MCDI RPC REQ:%s \\\n", buf); 304 + bytes = 0; 305 + } 306 + bytes += snprintf(buf + bytes, 307 + LOG_LINE_MAX - bytes, " %08x", 308 + le32_to_cpu(frag[i].cdx_u32)); 309 + } 310 + } 311 + 312 + pr_info("MCDI RPC REQ:%s\n", buf); 313 + } 314 + #endif 315 + hdr[0].cdx_u32 |= (__force __le32)(cdx_mcdi_payload_csum(hdr, hdr_len, inbuf, inlen) << 316 + MCDI_HEADER_XFLAGS_LBN); 317 + cdx->mcdi_ops->mcdi_request(cdx, hdr, hdr_len, inbuf, inlen); 318 + 319 + mcdi->new_epoch = false; 320 + } 321 + 322 + static int cdx_mcdi_errno(struct cdx_mcdi *cdx, unsigned int mcdi_err) 323 + { 324 + switch (mcdi_err) { 325 + case 0: 326 + case MC_CMD_ERR_QUEUE_FULL: 327 + return mcdi_err; 328 + case MC_CMD_ERR_EPERM: 329 + return -EPERM; 330 + case MC_CMD_ERR_ENOENT: 331 + return -ENOENT; 332 + case MC_CMD_ERR_EINTR: 333 + return -EINTR; 334 + case MC_CMD_ERR_EAGAIN: 335 + return -EAGAIN; 336 + case MC_CMD_ERR_EACCES: 337 + return -EACCES; 338 + case MC_CMD_ERR_EBUSY: 339 + return -EBUSY; 340 + case MC_CMD_ERR_EINVAL: 341 + return -EINVAL; 342 + case MC_CMD_ERR_ERANGE: 343 + return -ERANGE; 344 + case MC_CMD_ERR_EDEADLK: 345 + return -EDEADLK; 346 + case MC_CMD_ERR_ENOSYS: 347 + return -EOPNOTSUPP; 348 + case MC_CMD_ERR_ETIME: 349 + return -ETIME; 350 + case MC_CMD_ERR_EALREADY: 351 + return -EALREADY; 352 + case MC_CMD_ERR_ENOSPC: 353 + return -ENOSPC; 354 + case MC_CMD_ERR_ENOMEM: 355 + return -ENOMEM; 356 + case MC_CMD_ERR_ENOTSUP: 357 + return -EOPNOTSUPP; 358 + case MC_CMD_ERR_ALLOC_FAIL: 359 + return -ENOBUFS; 360 + case MC_CMD_ERR_MAC_EXIST: 361 + return -EADDRINUSE; 362 + case MC_CMD_ERR_NO_EVB_PORT: 363 + return -EAGAIN; 364 + default: 365 + return -EPROTO; 366 + } 367 + } 368 + 369 + static void cdx_mcdi_process_cleanup_list(struct cdx_mcdi *cdx, 370 + struct list_head *cleanup_list) 371 + { 372 + struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx); 373 + unsigned int cleanups = 0; 374 + 375 + if (!mcdi) 376 + return; 377 + 378 + while (!list_empty(cleanup_list)) { 379 + struct cdx_mcdi_cmd *cmd = 380 + list_first_entry(cleanup_list, 381 + struct cdx_mcdi_cmd, cleanup_list); 382 + cmd->completer(cdx, cmd->cookie, cmd->rc, 383 + cmd->outbuf, cmd->outlen); 384 + list_del(&cmd->cleanup_list); 385 + kref_put(&cmd->ref, cdx_mcdi_cmd_release); 386 + ++cleanups; 387 + } 388 + 389 + if (cleanups) { 390 + bool all_done; 391 + 392 + mutex_lock(&mcdi->iface_lock); 393 + CDX_WARN_ON_PARANOID(cleanups > mcdi->outstanding_cleanups); 394 + all_done = (mcdi->outstanding_cleanups -= cleanups) == 0; 395 + mutex_unlock(&mcdi->iface_lock); 396 + if (all_done) 397 + wake_up(&mcdi->cmd_complete_wq); 398 + } 399 + } 400 + 401 + static void _cdx_mcdi_cancel_cmd(struct cdx_mcdi_iface *mcdi, 402 + unsigned int handle, 403 + struct list_head *cleanup_list) 404 + { 405 + struct cdx_mcdi_cmd *cmd; 406 + 407 + list_for_each_entry(cmd, &mcdi->cmd_list, list) 408 + if (cdx_mcdi_cmd_handle(cmd) == handle) { 409 + switch (cmd->state) { 410 + case MCDI_STATE_QUEUED: 411 + case MCDI_STATE_RETRY: 412 + pr_debug("command %#x inlen %zu cancelled in queue\n", 413 + cmd->cmd, cmd->inlen); 414 + /* if not yet running, properly cancel it */ 415 + cmd->rc = -EPIPE; 416 + cdx_mcdi_remove_cmd(mcdi, cmd, cleanup_list); 417 + break; 418 + case MCDI_STATE_RUNNING: 419 + case MCDI_STATE_RUNNING_CANCELLED: 420 + case MCDI_STATE_FINISHED: 421 + default: 422 + /* invalid state? */ 423 + WARN_ON(1); 424 + } 425 + break; 426 + } 427 + } 428 + 429 + static void cdx_mcdi_cancel_cmd(struct cdx_mcdi *cdx, struct cdx_mcdi_cmd *cmd) 430 + { 431 + struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx); 432 + LIST_HEAD(cleanup_list); 433 + 434 + if (!mcdi) 435 + return; 436 + 437 + mutex_lock(&mcdi->iface_lock); 438 + cdx_mcdi_timeout_cmd(mcdi, cmd, &cleanup_list); 439 + mutex_unlock(&mcdi->iface_lock); 440 + cdx_mcdi_process_cleanup_list(cdx, &cleanup_list); 441 + } 442 + 443 + struct cdx_mcdi_blocking_data { 444 + struct kref ref; 445 + bool done; 446 + wait_queue_head_t wq; 447 + int rc; 448 + struct cdx_dword *outbuf; 449 + size_t outlen; 450 + size_t outlen_actual; 451 + }; 452 + 453 + static void cdx_mcdi_blocking_data_release(struct kref *ref) 454 + { 455 + kfree(container_of(ref, struct cdx_mcdi_blocking_data, ref)); 456 + } 457 + 458 + static void cdx_mcdi_rpc_completer(struct cdx_mcdi *cdx, unsigned long cookie, 459 + int rc, struct cdx_dword *outbuf, 460 + size_t outlen_actual) 461 + { 462 + struct cdx_mcdi_blocking_data *wait_data = 463 + (struct cdx_mcdi_blocking_data *)cookie; 464 + 465 + wait_data->rc = rc; 466 + memcpy(wait_data->outbuf, outbuf, 467 + min(outlen_actual, wait_data->outlen)); 468 + wait_data->outlen_actual = outlen_actual; 469 + /* memory barrier */ 470 + smp_wmb(); 471 + wait_data->done = true; 472 + wake_up(&wait_data->wq); 473 + kref_put(&wait_data->ref, cdx_mcdi_blocking_data_release); 474 + } 475 + 476 + static int cdx_mcdi_rpc_sync(struct cdx_mcdi *cdx, unsigned int cmd, 477 + const struct cdx_dword *inbuf, size_t inlen, 478 + struct cdx_dword *outbuf, size_t outlen, 479 + size_t *outlen_actual, bool quiet) 480 + { 481 + struct cdx_mcdi_blocking_data *wait_data; 482 + struct cdx_mcdi_cmd *cmd_item; 483 + unsigned int handle; 484 + int rc; 485 + 486 + if (outlen_actual) 487 + *outlen_actual = 0; 488 + 489 + wait_data = kmalloc(sizeof(*wait_data), GFP_KERNEL); 490 + if (!wait_data) 491 + return -ENOMEM; 492 + 493 + cmd_item = kmalloc(sizeof(*cmd_item), GFP_KERNEL); 494 + if (!cmd_item) { 495 + kfree(wait_data); 496 + return -ENOMEM; 497 + } 498 + 499 + kref_init(&wait_data->ref); 500 + wait_data->done = false; 501 + init_waitqueue_head(&wait_data->wq); 502 + wait_data->outbuf = outbuf; 503 + wait_data->outlen = outlen; 504 + 505 + kref_init(&cmd_item->ref); 506 + cmd_item->quiet = quiet; 507 + cmd_item->cookie = (unsigned long)wait_data; 508 + cmd_item->completer = &cdx_mcdi_rpc_completer; 509 + cmd_item->cmd = cmd; 510 + cmd_item->inlen = inlen; 511 + cmd_item->inbuf = inbuf; 512 + 513 + /* Claim an extra reference for the completer to put. */ 514 + kref_get(&wait_data->ref); 515 + rc = cdx_mcdi_rpc_async_internal(cdx, cmd_item, &handle); 516 + if (rc) { 517 + kref_put(&wait_data->ref, cdx_mcdi_blocking_data_release); 518 + goto out; 519 + } 520 + 521 + if (!wait_event_timeout(wait_data->wq, wait_data->done, 522 + cdx_mcdi_rpc_timeout(cdx, cmd)) && 523 + !wait_data->done) { 524 + pr_err("MC command 0x%x inlen %zu timed out (sync)\n", 525 + cmd, inlen); 526 + 527 + cdx_mcdi_cancel_cmd(cdx, cmd_item); 528 + 529 + wait_data->rc = -ETIMEDOUT; 530 + wait_data->outlen_actual = 0; 531 + } 532 + 533 + if (outlen_actual) 534 + *outlen_actual = wait_data->outlen_actual; 535 + rc = wait_data->rc; 536 + 537 + out: 538 + kref_put(&wait_data->ref, cdx_mcdi_blocking_data_release); 539 + 540 + return rc; 541 + } 542 + 543 + static bool cdx_mcdi_get_seq(struct cdx_mcdi_iface *mcdi, unsigned char *seq) 544 + { 545 + *seq = mcdi->prev_seq; 546 + do { 547 + *seq = (*seq + 1) % ARRAY_SIZE(mcdi->seq_held_by); 548 + } while (mcdi->seq_held_by[*seq] && *seq != mcdi->prev_seq); 549 + return !mcdi->seq_held_by[*seq]; 550 + } 551 + 552 + static int cdx_mcdi_rpc_async_internal(struct cdx_mcdi *cdx, 553 + struct cdx_mcdi_cmd *cmd, 554 + unsigned int *handle) 555 + { 556 + struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx); 557 + LIST_HEAD(cleanup_list); 558 + 559 + if (!mcdi) { 560 + kref_put(&cmd->ref, cdx_mcdi_cmd_release); 561 + return -ENETDOWN; 562 + } 563 + 564 + if (mcdi->mode == MCDI_MODE_FAIL) { 565 + kref_put(&cmd->ref, cdx_mcdi_cmd_release); 566 + return -ENETDOWN; 567 + } 568 + 569 + cmd->mcdi = mcdi; 570 + INIT_WORK(&cmd->work, cdx_mcdi_cmd_work); 571 + INIT_LIST_HEAD(&cmd->list); 572 + INIT_LIST_HEAD(&cmd->cleanup_list); 573 + cmd->rc = 0; 574 + cmd->outbuf = NULL; 575 + cmd->outlen = 0; 576 + 577 + queue_work(mcdi->workqueue, &cmd->work); 578 + return 0; 579 + } 580 + 581 + static void cdx_mcdi_cmd_start_or_queue(struct cdx_mcdi_iface *mcdi, 582 + struct cdx_mcdi_cmd *cmd) 583 + { 584 + struct cdx_mcdi *cdx = mcdi->cdx; 585 + u8 seq; 586 + 587 + if (!mcdi->db_held_by && 588 + cdx_mcdi_get_seq(mcdi, &seq)) { 589 + cmd->seq = seq; 590 + cmd->reboot_seen = false; 591 + cdx_mcdi_send_request(cdx, cmd); 592 + cmd->state = MCDI_STATE_RUNNING; 593 + } else { 594 + cmd->state = MCDI_STATE_QUEUED; 595 + } 596 + } 597 + 598 + /* try to advance other commands */ 599 + static void cdx_mcdi_start_or_queue(struct cdx_mcdi_iface *mcdi, 600 + bool allow_retry) 601 + { 602 + struct cdx_mcdi_cmd *cmd, *tmp; 603 + 604 + list_for_each_entry_safe(cmd, tmp, &mcdi->cmd_list, list) 605 + if (cmd->state == MCDI_STATE_QUEUED || 606 + (cmd->state == MCDI_STATE_RETRY && allow_retry)) 607 + cdx_mcdi_cmd_start_or_queue(mcdi, cmd); 608 + } 609 + 610 + void cdx_mcdi_process_cmd(struct cdx_mcdi *cdx, struct cdx_dword *outbuf, int len) 611 + { 612 + struct cdx_mcdi_iface *mcdi; 613 + struct cdx_mcdi_cmd *cmd; 614 + LIST_HEAD(cleanup_list); 615 + unsigned int respseq; 616 + 617 + if (!len || !outbuf) { 618 + pr_err("Got empty MC response\n"); 619 + return; 620 + } 621 + 622 + mcdi = cdx_mcdi_if(cdx); 623 + if (!mcdi) 624 + return; 625 + 626 + respseq = CDX_DWORD_FIELD(outbuf[0], MCDI_HEADER_SEQ); 627 + 628 + mutex_lock(&mcdi->iface_lock); 629 + cmd = mcdi->seq_held_by[respseq]; 630 + 631 + if (cmd) { 632 + if (cmd->state == MCDI_STATE_FINISHED) { 633 + mutex_unlock(&mcdi->iface_lock); 634 + kref_put(&cmd->ref, cdx_mcdi_cmd_release); 635 + return; 636 + } 637 + 638 + cdx_mcdi_complete_cmd(mcdi, cmd, outbuf, len, &cleanup_list); 639 + } else { 640 + pr_err("MC response unexpected for seq : %0X\n", respseq); 641 + } 642 + 643 + mutex_unlock(&mcdi->iface_lock); 644 + 645 + cdx_mcdi_process_cleanup_list(mcdi->cdx, &cleanup_list); 646 + } 647 + 648 + static void cdx_mcdi_cmd_work(struct work_struct *context) 649 + { 650 + struct cdx_mcdi_cmd *cmd = 651 + container_of(context, struct cdx_mcdi_cmd, work); 652 + struct cdx_mcdi_iface *mcdi = cmd->mcdi; 653 + 654 + mutex_lock(&mcdi->iface_lock); 655 + 656 + cmd->handle = mcdi->prev_handle++; 657 + list_add_tail(&cmd->list, &mcdi->cmd_list); 658 + cdx_mcdi_cmd_start_or_queue(mcdi, cmd); 659 + 660 + mutex_unlock(&mcdi->iface_lock); 661 + } 662 + 663 + /* 664 + * Returns true if the MCDI module is finished with the command. 665 + * (examples of false would be if the command was proxied, or it was 666 + * rejected by the MC due to lack of resources and requeued). 667 + */ 668 + static bool cdx_mcdi_complete_cmd(struct cdx_mcdi_iface *mcdi, 669 + struct cdx_mcdi_cmd *cmd, 670 + struct cdx_dword *outbuf, 671 + int len, 672 + struct list_head *cleanup_list) 673 + { 674 + size_t resp_hdr_len, resp_data_len; 675 + struct cdx_mcdi *cdx = mcdi->cdx; 676 + unsigned int respcmd, error; 677 + bool completed = false; 678 + int rc; 679 + 680 + /* ensure the command can't go away before this function returns */ 681 + kref_get(&cmd->ref); 682 + 683 + respcmd = CDX_DWORD_FIELD(outbuf[0], MCDI_HEADER_CODE); 684 + error = CDX_DWORD_FIELD(outbuf[0], MCDI_HEADER_ERROR); 685 + 686 + if (respcmd != MC_CMD_V2_EXTN) { 687 + resp_hdr_len = 4; 688 + resp_data_len = CDX_DWORD_FIELD(outbuf[0], MCDI_HEADER_DATALEN); 689 + } else { 690 + resp_data_len = 0; 691 + resp_hdr_len = 8; 692 + if (len >= 8) 693 + resp_data_len = 694 + CDX_DWORD_FIELD(outbuf[1], MC_CMD_V2_EXTN_IN_ACTUAL_LEN); 695 + } 696 + 697 + if ((resp_hdr_len + resp_data_len) > len) { 698 + pr_warn("Incomplete MCDI response received %d. Expected %zu\n", 699 + len, (resp_hdr_len + resp_data_len)); 700 + resp_data_len = 0; 701 + } 702 + 703 + #ifdef CONFIG_MCDI_LOGGING 704 + if (!WARN_ON_ONCE(!mcdi->logging_buffer)) { 705 + char *log = mcdi->logging_buffer; 706 + int i, bytes = 0; 707 + size_t rlen; 708 + 709 + WARN_ON_ONCE(resp_hdr_len % 4); 710 + 711 + rlen = resp_hdr_len / 4 + DIV_ROUND_UP(resp_data_len, 4); 712 + 713 + for (i = 0; i < rlen; i++) { 714 + if ((bytes + 75) > LOG_LINE_MAX) { 715 + pr_info("MCDI RPC RESP:%s \\\n", log); 716 + bytes = 0; 717 + } 718 + bytes += snprintf(log + bytes, LOG_LINE_MAX - bytes, 719 + " %08x", le32_to_cpu(outbuf[i].cdx_u32)); 720 + } 721 + 722 + pr_info("MCDI RPC RESP:%s\n", log); 723 + } 724 + #endif 725 + 726 + if (error && resp_data_len == 0) { 727 + /* MC rebooted during command */ 728 + rc = -EIO; 729 + } else { 730 + if (WARN_ON_ONCE(error && resp_data_len < 4)) 731 + resp_data_len = 4; 732 + if (error) { 733 + rc = CDX_DWORD_FIELD(outbuf[resp_hdr_len / 4], CDX_DWORD); 734 + if (!cmd->quiet) { 735 + int err_arg = 0; 736 + 737 + if (resp_data_len >= MC_CMD_ERR_ARG_OFST + 4) { 738 + int offset = (resp_hdr_len + MC_CMD_ERR_ARG_OFST) / 4; 739 + 740 + err_arg = CDX_DWORD_VAL(outbuf[offset]); 741 + } 742 + 743 + _cdx_mcdi_display_error(cdx, cmd->cmd, 744 + cmd->inlen, rc, err_arg, 745 + cdx_mcdi_errno(cdx, rc)); 746 + } 747 + rc = cdx_mcdi_errno(cdx, rc); 748 + } else { 749 + rc = 0; 750 + } 751 + } 752 + 753 + /* free doorbell */ 754 + if (mcdi->db_held_by == cmd) 755 + mcdi->db_held_by = NULL; 756 + 757 + if (cdx_cmd_cancelled(cmd)) { 758 + list_del(&cmd->list); 759 + kref_put(&cmd->ref, cdx_mcdi_cmd_release); 760 + completed = true; 761 + } else if (rc == MC_CMD_ERR_QUEUE_FULL) { 762 + cmd->state = MCDI_STATE_RETRY; 763 + } else { 764 + cmd->rc = rc; 765 + cmd->outbuf = outbuf + DIV_ROUND_UP(resp_hdr_len, 4); 766 + cmd->outlen = resp_data_len; 767 + cdx_mcdi_remove_cmd(mcdi, cmd, cleanup_list); 768 + completed = true; 769 + } 770 + 771 + /* free sequence number and buffer */ 772 + mcdi->seq_held_by[cmd->seq] = NULL; 773 + 774 + cdx_mcdi_start_or_queue(mcdi, rc != MC_CMD_ERR_QUEUE_FULL); 775 + 776 + /* wake up anyone waiting for flush */ 777 + wake_up(&mcdi->cmd_complete_wq); 778 + 779 + kref_put(&cmd->ref, cdx_mcdi_cmd_release); 780 + 781 + return completed; 782 + } 783 + 784 + static void cdx_mcdi_timeout_cmd(struct cdx_mcdi_iface *mcdi, 785 + struct cdx_mcdi_cmd *cmd, 786 + struct list_head *cleanup_list) 787 + { 788 + struct cdx_mcdi *cdx = mcdi->cdx; 789 + 790 + pr_err("MC command 0x%x inlen %zu state %d timed out after %u ms\n", 791 + cmd->cmd, cmd->inlen, cmd->state, 792 + jiffies_to_msecs(jiffies - cmd->started)); 793 + 794 + cmd->rc = -ETIMEDOUT; 795 + cdx_mcdi_remove_cmd(mcdi, cmd, cleanup_list); 796 + 797 + cdx_mcdi_mode_fail(cdx, cleanup_list); 798 + } 799 + 800 + /** 801 + * cdx_mcdi_rpc - Issue an MCDI command and wait for completion 802 + * @cdx: NIC through which to issue the command 803 + * @cmd: Command type number 804 + * @inbuf: Command parameters 805 + * @inlen: Length of command parameters, in bytes. Must be a multiple 806 + * of 4 and no greater than %MCDI_CTL_SDU_LEN_MAX_V1. 807 + * @outbuf: Response buffer. May be %NULL if @outlen is 0. 808 + * @outlen: Length of response buffer, in bytes. If the actual 809 + * response is longer than @outlen & ~3, it will be truncated 810 + * to that length. 811 + * @outlen_actual: Pointer through which to return the actual response 812 + * length. May be %NULL if this is not needed. 813 + * 814 + * This function may sleep and therefore must be called in process 815 + * context. 816 + * 817 + * Return: A negative error code, or zero if successful. The error 818 + * code may come from the MCDI response or may indicate a failure 819 + * to communicate with the MC. In the former case, the response 820 + * will still be copied to @outbuf and *@outlen_actual will be 821 + * set accordingly. In the latter case, *@outlen_actual will be 822 + * set to zero. 823 + */ 824 + int cdx_mcdi_rpc(struct cdx_mcdi *cdx, unsigned int cmd, 825 + const struct cdx_dword *inbuf, size_t inlen, 826 + struct cdx_dword *outbuf, size_t outlen, 827 + size_t *outlen_actual) 828 + { 829 + return cdx_mcdi_rpc_sync(cdx, cmd, inbuf, inlen, outbuf, outlen, 830 + outlen_actual, false); 831 + } 832 + 833 + /** 834 + * cdx_mcdi_rpc_async - Schedule an MCDI command to run asynchronously 835 + * @cdx: NIC through which to issue the command 836 + * @cmd: Command type number 837 + * @inbuf: Command parameters 838 + * @inlen: Length of command parameters, in bytes 839 + * @complete: Function to be called on completion or cancellation. 840 + * @cookie: Arbitrary value to be passed to @complete. 841 + * 842 + * This function does not sleep and therefore may be called in atomic 843 + * context. It will fail if event queues are disabled or if MCDI 844 + * event completions have been disabled due to an error. 845 + * 846 + * If it succeeds, the @complete function will be called exactly once 847 + * in process context, when one of the following occurs: 848 + * (a) the completion event is received (in process context) 849 + * (b) event queues are disabled (in the process that disables them) 850 + */ 851 + int 852 + cdx_mcdi_rpc_async(struct cdx_mcdi *cdx, unsigned int cmd, 853 + const struct cdx_dword *inbuf, size_t inlen, 854 + cdx_mcdi_async_completer *complete, unsigned long cookie) 855 + { 856 + struct cdx_mcdi_cmd *cmd_item = 857 + kmalloc(sizeof(struct cdx_mcdi_cmd) + inlen, GFP_ATOMIC); 858 + 859 + if (!cmd_item) 860 + return -ENOMEM; 861 + 862 + kref_init(&cmd_item->ref); 863 + cmd_item->quiet = true; 864 + cmd_item->cookie = cookie; 865 + cmd_item->completer = complete; 866 + cmd_item->cmd = cmd; 867 + cmd_item->inlen = inlen; 868 + /* inbuf is probably not valid after return, so take a copy */ 869 + cmd_item->inbuf = (struct cdx_dword *)(cmd_item + 1); 870 + memcpy(cmd_item + 1, inbuf, inlen); 871 + 872 + return cdx_mcdi_rpc_async_internal(cdx, cmd_item, NULL); 873 + } 874 + 875 + static void _cdx_mcdi_display_error(struct cdx_mcdi *cdx, unsigned int cmd, 876 + size_t inlen, int raw, int arg, int err_no) 877 + { 878 + pr_err("MC command 0x%x inlen %d failed err_no=%d (raw=%d) arg=%d\n", 879 + cmd, (int)inlen, err_no, raw, arg); 880 + } 881 + 882 + /* 883 + * Set MCDI mode to fail to prevent any new commands, then cancel any 884 + * outstanding commands. 885 + * Caller must hold the mcdi iface_lock. 886 + */ 887 + static void cdx_mcdi_mode_fail(struct cdx_mcdi *cdx, struct list_head *cleanup_list) 888 + { 889 + struct cdx_mcdi_iface *mcdi = cdx_mcdi_if(cdx); 890 + 891 + if (!mcdi) 892 + return; 893 + 894 + mcdi->mode = MCDI_MODE_FAIL; 895 + 896 + while (!list_empty(&mcdi->cmd_list)) { 897 + struct cdx_mcdi_cmd *cmd; 898 + 899 + cmd = list_first_entry(&mcdi->cmd_list, struct cdx_mcdi_cmd, 900 + list); 901 + _cdx_mcdi_cancel_cmd(mcdi, cdx_mcdi_cmd_handle(cmd), cleanup_list); 902 + } 903 + }
+238
drivers/cdx/controller/mcdi.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 2 + * 3 + * Copyright 2008-2013 Solarflare Communications Inc. 4 + * Copyright (C) 2022-2023, Advanced Micro Devices, Inc. 5 + */ 6 + 7 + #ifndef CDX_MCDI_H 8 + #define CDX_MCDI_H 9 + 10 + #include <linux/mutex.h> 11 + #include <linux/kref.h> 12 + 13 + #include "bitfield.h" 14 + #include "mc_cdx_pcol.h" 15 + 16 + #ifdef DEBUG 17 + #define CDX_WARN_ON_ONCE_PARANOID(x) WARN_ON_ONCE(x) 18 + #define CDX_WARN_ON_PARANOID(x) WARN_ON(x) 19 + #else 20 + #define CDX_WARN_ON_ONCE_PARANOID(x) do {} while (0) 21 + #define CDX_WARN_ON_PARANOID(x) do {} while (0) 22 + #endif 23 + 24 + /** 25 + * enum cdx_mcdi_mode - MCDI transaction mode 26 + * @MCDI_MODE_EVENTS: wait for an mcdi response callback. 27 + * @MCDI_MODE_FAIL: we think MCDI is dead, so fail-fast all calls 28 + */ 29 + enum cdx_mcdi_mode { 30 + MCDI_MODE_EVENTS, 31 + MCDI_MODE_FAIL, 32 + }; 33 + 34 + #define MCDI_RPC_TIMEOUT (10 * HZ) 35 + #define MCDI_RPC_LONG_TIMEOU (60 * HZ) 36 + #define MCDI_RPC_POST_RST_TIME (10 * HZ) 37 + 38 + #define MCDI_BUF_LEN (8 + MCDI_CTL_SDU_LEN_MAX) 39 + 40 + /** 41 + * enum cdx_mcdi_cmd_state - State for an individual MCDI command 42 + * @MCDI_STATE_QUEUED: Command not started and is waiting to run. 43 + * @MCDI_STATE_RETRY: Command was submitted and MC rejected with no resources, 44 + * as MC have too many outstanding commands. Command will be retried once 45 + * another command returns. 46 + * @MCDI_STATE_RUNNING: Command was accepted and is running. 47 + * @MCDI_STATE_RUNNING_CANCELLED: Command is running but the issuer cancelled 48 + * the command. 49 + * @MCDI_STATE_FINISHED: Processing of this command has completed. 50 + */ 51 + 52 + enum cdx_mcdi_cmd_state { 53 + MCDI_STATE_QUEUED, 54 + MCDI_STATE_RETRY, 55 + MCDI_STATE_RUNNING, 56 + MCDI_STATE_RUNNING_CANCELLED, 57 + MCDI_STATE_FINISHED, 58 + }; 59 + 60 + /** 61 + * struct cdx_mcdi - CDX MCDI Firmware interface, to interact 62 + * with CDX controller. 63 + * @mcdi: MCDI interface 64 + * @mcdi_ops: MCDI operations 65 + */ 66 + struct cdx_mcdi { 67 + /* MCDI interface */ 68 + struct cdx_mcdi_data *mcdi; 69 + const struct cdx_mcdi_ops *mcdi_ops; 70 + }; 71 + 72 + struct cdx_mcdi_ops { 73 + void (*mcdi_request)(struct cdx_mcdi *cdx, 74 + const struct cdx_dword *hdr, size_t hdr_len, 75 + const struct cdx_dword *sdu, size_t sdu_len); 76 + unsigned int (*mcdi_rpc_timeout)(struct cdx_mcdi *cdx, unsigned int cmd); 77 + }; 78 + 79 + typedef void cdx_mcdi_async_completer(struct cdx_mcdi *cdx, 80 + unsigned long cookie, int rc, 81 + struct cdx_dword *outbuf, 82 + size_t outlen_actual); 83 + 84 + /** 85 + * struct cdx_mcdi_cmd - An outstanding MCDI command 86 + * @ref: Reference count. There will be one reference if the command is 87 + * in the mcdi_iface cmd_list, another if it's on a cleanup list, 88 + * and a third if it's queued in the work queue. 89 + * @list: The data for this entry in mcdi->cmd_list 90 + * @cleanup_list: The data for this entry in a cleanup list 91 + * @work: The work item for this command, queued in mcdi->workqueue 92 + * @mcdi: The mcdi_iface for this command 93 + * @state: The state of this command 94 + * @inlen: inbuf length 95 + * @inbuf: Input buffer 96 + * @quiet: Whether to silence errors 97 + * @reboot_seen: Whether a reboot has been seen during this command, 98 + * to prevent duplicates 99 + * @seq: Sequence number 100 + * @started: Jiffies this command was started at 101 + * @cookie: Context for completion function 102 + * @completer: Completion function 103 + * @handle: Command handle 104 + * @cmd: Command number 105 + * @rc: Return code 106 + * @outlen: Length of output buffer 107 + * @outbuf: Output buffer 108 + */ 109 + struct cdx_mcdi_cmd { 110 + struct kref ref; 111 + struct list_head list; 112 + struct list_head cleanup_list; 113 + struct work_struct work; 114 + struct cdx_mcdi_iface *mcdi; 115 + enum cdx_mcdi_cmd_state state; 116 + size_t inlen; 117 + const struct cdx_dword *inbuf; 118 + bool quiet; 119 + bool reboot_seen; 120 + u8 seq; 121 + unsigned long started; 122 + unsigned long cookie; 123 + cdx_mcdi_async_completer *completer; 124 + unsigned int handle; 125 + unsigned int cmd; 126 + int rc; 127 + size_t outlen; 128 + struct cdx_dword *outbuf; 129 + /* followed by inbuf data if necessary */ 130 + }; 131 + 132 + /** 133 + * struct cdx_mcdi_iface - MCDI protocol context 134 + * @cdx: The associated NIC 135 + * @iface_lock: Serialise access to this structure 136 + * @outstanding_cleanups: Count of cleanups 137 + * @cmd_list: List of outstanding and running commands 138 + * @workqueue: Workqueue used for delayed processing 139 + * @cmd_complete_wq: Waitqueue for command completion 140 + * @db_held_by: Command the MC doorbell is in use by 141 + * @seq_held_by: Command each sequence number is in use by 142 + * @prev_handle: The last used command handle 143 + * @mode: Poll for mcdi completion, or wait for an mcdi_event 144 + * @prev_seq: The last used sequence number 145 + * @new_epoch: Indicates start of day or start of MC reboot recovery 146 + * @logging_buffer: Buffer that may be used to build MCDI tracing messages 147 + * @logging_enabled: Whether to trace MCDI 148 + */ 149 + struct cdx_mcdi_iface { 150 + struct cdx_mcdi *cdx; 151 + /* Serialise access */ 152 + struct mutex iface_lock; 153 + unsigned int outstanding_cleanups; 154 + struct list_head cmd_list; 155 + struct workqueue_struct *workqueue; 156 + wait_queue_head_t cmd_complete_wq; 157 + struct cdx_mcdi_cmd *db_held_by; 158 + struct cdx_mcdi_cmd *seq_held_by[16]; 159 + unsigned int prev_handle; 160 + enum cdx_mcdi_mode mode; 161 + u8 prev_seq; 162 + bool new_epoch; 163 + #ifdef CONFIG_MCDI_LOGGING 164 + bool logging_enabled; 165 + char *logging_buffer; 166 + #endif 167 + }; 168 + 169 + /** 170 + * struct cdx_mcdi_data - extra state for NICs that implement MCDI 171 + * @iface: Interface/protocol state 172 + * @fn_flags: Flags for this function, as returned by %MC_CMD_DRV_ATTACH. 173 + */ 174 + struct cdx_mcdi_data { 175 + struct cdx_mcdi_iface iface; 176 + u32 fn_flags; 177 + }; 178 + 179 + static inline struct cdx_mcdi_iface *cdx_mcdi_if(struct cdx_mcdi *cdx) 180 + { 181 + return cdx->mcdi ? &cdx->mcdi->iface : NULL; 182 + } 183 + 184 + int cdx_mcdi_init(struct cdx_mcdi *cdx); 185 + void cdx_mcdi_finish(struct cdx_mcdi *cdx); 186 + 187 + void cdx_mcdi_process_cmd(struct cdx_mcdi *cdx, struct cdx_dword *outbuf, int len); 188 + int cdx_mcdi_rpc(struct cdx_mcdi *cdx, unsigned int cmd, 189 + const struct cdx_dword *inbuf, size_t inlen, 190 + struct cdx_dword *outbuf, size_t outlen, size_t *outlen_actual); 191 + int cdx_mcdi_rpc_async(struct cdx_mcdi *cdx, unsigned int cmd, 192 + const struct cdx_dword *inbuf, size_t inlen, 193 + cdx_mcdi_async_completer *complete, 194 + unsigned long cookie); 195 + int cdx_mcdi_wait_for_quiescence(struct cdx_mcdi *cdx, 196 + unsigned int timeout_jiffies); 197 + 198 + /* 199 + * We expect that 16- and 32-bit fields in MCDI requests and responses 200 + * are appropriately aligned, but 64-bit fields are only 201 + * 32-bit-aligned. 202 + */ 203 + #define MCDI_DECLARE_BUF(_name, _len) struct cdx_dword _name[DIV_ROUND_UP(_len, 4)] = {{0}} 204 + #define _MCDI_PTR(_buf, _offset) \ 205 + ((u8 *)(_buf) + (_offset)) 206 + #define MCDI_PTR(_buf, _field) \ 207 + _MCDI_PTR(_buf, MC_CMD_ ## _field ## _OFST) 208 + #define _MCDI_CHECK_ALIGN(_ofst, _align) \ 209 + ((void)BUILD_BUG_ON_ZERO((_ofst) & ((_align) - 1)), \ 210 + (_ofst)) 211 + #define _MCDI_DWORD(_buf, _field) \ 212 + ((_buf) + (_MCDI_CHECK_ALIGN(MC_CMD_ ## _field ## _OFST, 4) >> 2)) 213 + 214 + #define MCDI_BYTE(_buf, _field) \ 215 + ((void)BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 1), \ 216 + *MCDI_PTR(_buf, _field)) 217 + #define MCDI_WORD(_buf, _field) \ 218 + ((void)BUILD_BUG_ON_ZERO(MC_CMD_ ## _field ## _LEN != 2), \ 219 + le16_to_cpu(*(__force const __le16 *)MCDI_PTR(_buf, _field))) 220 + #define MCDI_SET_DWORD(_buf, _field, _value) \ 221 + CDX_POPULATE_DWORD_1(*_MCDI_DWORD(_buf, _field), CDX_DWORD, _value) 222 + #define MCDI_DWORD(_buf, _field) \ 223 + CDX_DWORD_FIELD(*_MCDI_DWORD(_buf, _field), CDX_DWORD) 224 + #define MCDI_POPULATE_DWORD_1(_buf, _field, _name1, _value1) \ 225 + CDX_POPULATE_DWORD_1(*_MCDI_DWORD(_buf, _field), \ 226 + MC_CMD_ ## _name1, _value1) 227 + #define MCDI_SET_QWORD(_buf, _field, _value) \ 228 + do { \ 229 + CDX_POPULATE_DWORD_1(_MCDI_DWORD(_buf, _field)[0], \ 230 + CDX_DWORD, (u32)(_value)); \ 231 + CDX_POPULATE_DWORD_1(_MCDI_DWORD(_buf, _field)[1], \ 232 + CDX_DWORD, (u64)(_value) >> 32); \ 233 + } while (0) 234 + #define MCDI_QWORD(_buf, _field) \ 235 + (CDX_DWORD_FIELD(_MCDI_DWORD(_buf, _field)[0], CDX_DWORD) | \ 236 + (u64)CDX_DWORD_FIELD(_MCDI_DWORD(_buf, _field)[1], CDX_DWORD) << 32) 237 + 238 + #endif /* CDX_MCDI_H */