1 /* SPDX-License-Identifier: ISC */ 2 /* 3 * Copyright (c) 2005-2011 Atheros Communications Inc. 4 * Copyright (c) 2011-2016 Qualcomm Atheros, Inc. 5 */ 6 7 #ifndef _HTC_H_ 8 #define _HTC_H_ 9 10 #include <linux/kernel.h> 11 #include <linux/list.h> 12 #include <linux/bug.h> 13 #include <linux/skbuff.h> 14 #include <linux/timer.h> 15 #include <linux/bitfield.h> 16 17 struct ath10k; 18 19 /****************/ 20 /* HTC protocol */ 21 /****************/ 22 23 /* 24 * HTC - host-target control protocol 25 * 26 * tx packets are generally <htc_hdr><payload> 27 * rx packets are more complex: <htc_hdr><payload><trailer> 28 * 29 * The payload + trailer length is stored in len. 30 * To get payload-only length one needs to payload - trailer_len. 31 * 32 * Trailer contains (possibly) multiple <htc_record>. 33 * Each record is a id-len-value. 34 * 35 * HTC header flags, control_byte0, control_byte1 36 * have different meaning depending whether its tx 37 * or rx. 38 * 39 * Alignment: htc_hdr, payload and trailer are 40 * 4-byte aligned. 41 */ 42 43 #define HTC_HOST_MAX_MSG_PER_RX_BUNDLE 32 44 45 enum ath10k_htc_tx_flags { 46 ATH10K_HTC_FLAG_NEED_CREDIT_UPDATE = 0x01, 47 ATH10K_HTC_FLAG_SEND_BUNDLE = 0x02 48 }; 49 50 enum ath10k_htc_rx_flags { 51 ATH10K_HTC_FLAGS_RECV_1MORE_BLOCK = 0x01, 52 ATH10K_HTC_FLAG_TRAILER_PRESENT = 0x02, 53 }; 54 55 #define ATH10K_HTC_FLAG_BUNDLE_MASK GENMASK(7, 4) 56 57 /* bits 2-3 are for extra bundle count bits 4-5 */ 58 #define ATH10K_HTC_BUNDLE_EXTRA_MASK GENMASK(3, 2) 59 #define ATH10K_HTC_BUNDLE_EXTRA_SHIFT 4 60 61 static inline unsigned int ath10k_htc_get_bundle_count(u8 max_msgs, u8 flags) 62 { 63 unsigned int count, extra_count = 0; 64 65 count = FIELD_GET(ATH10K_HTC_FLAG_BUNDLE_MASK, flags); 66 67 if (max_msgs > 16) 68 extra_count = FIELD_GET(ATH10K_HTC_BUNDLE_EXTRA_MASK, flags) << 69 ATH10K_HTC_BUNDLE_EXTRA_SHIFT; 70 71 return count + extra_count; 72 } 73 74 struct ath10k_htc_hdr { 75 u8 eid; /* @enum ath10k_htc_ep_id */ 76 u8 flags; /* @enum ath10k_htc_tx_flags, ath10k_htc_rx_flags */ 77 __le16 len; 78 union { 79 u8 trailer_len; /* for rx */ 80 u8 control_byte0; 81 } __packed; 82 union { 83 u8 seq_no; /* for tx */ 84 u8 control_byte1; 85 } __packed; 86 u8 pad0; 87 u8 pad1; 88 } __packed __aligned(4); 89 90 enum ath10k_ath10k_htc_msg_id { 91 ATH10K_HTC_MSG_READY_ID = 1, 92 ATH10K_HTC_MSG_CONNECT_SERVICE_ID = 2, 93 ATH10K_HTC_MSG_CONNECT_SERVICE_RESP_ID = 3, 94 ATH10K_HTC_MSG_SETUP_COMPLETE_ID = 4, 95 ATH10K_HTC_MSG_SETUP_COMPLETE_EX_ID = 5, 96 ATH10K_HTC_MSG_SEND_SUSPEND_COMPLETE = 6 97 }; 98 99 enum ath10k_htc_version { 100 ATH10K_HTC_VERSION_2P0 = 0x00, /* 2.0 */ 101 ATH10K_HTC_VERSION_2P1 = 0x01, /* 2.1 */ 102 }; 103 104 enum ath10k_htc_conn_flags { 105 ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_ONE_FOURTH = 0x0, 106 ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_ONE_HALF = 0x1, 107 ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_THREE_FOURTHS = 0x2, 108 ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_UNITY = 0x3, 109 #define ATH10K_HTC_CONN_FLAGS_THRESHOLD_LEVEL_MASK 0x3 110 ATH10K_HTC_CONN_FLAGS_REDUCE_CREDIT_DRIBBLE = 1 << 2, 111 ATH10K_HTC_CONN_FLAGS_DISABLE_CREDIT_FLOW_CTRL = 1 << 3 112 #define ATH10K_HTC_CONN_FLAGS_RECV_ALLOC_MASK 0xFF00 113 #define ATH10K_HTC_CONN_FLAGS_RECV_ALLOC_LSB 8 114 }; 115 116 enum ath10k_htc_conn_svc_status { 117 ATH10K_HTC_CONN_SVC_STATUS_SUCCESS = 0, 118 ATH10K_HTC_CONN_SVC_STATUS_NOT_FOUND = 1, 119 ATH10K_HTC_CONN_SVC_STATUS_FAILED = 2, 120 ATH10K_HTC_CONN_SVC_STATUS_NO_RESOURCES = 3, 121 ATH10K_HTC_CONN_SVC_STATUS_NO_MORE_EP = 4 122 }; 123 124 enum ath10k_htc_setup_complete_flags { 125 ATH10K_HTC_SETUP_COMPLETE_FLAGS_RX_BNDL_EN = 1 126 }; 127 128 struct ath10k_ath10k_htc_msg_hdr { 129 __le16 message_id; /* @enum htc_message_id */ 130 } __packed; 131 132 struct ath10k_htc_unknown { 133 u8 pad0; 134 u8 pad1; 135 } __packed; 136 137 struct ath10k_htc_ready { 138 __le16 credit_count; 139 __le16 credit_size; 140 u8 max_endpoints; 141 u8 pad0; 142 } __packed; 143 144 struct ath10k_htc_ready_extended { 145 struct ath10k_htc_ready base; 146 u8 htc_version; /* @enum ath10k_htc_version */ 147 u8 max_msgs_per_htc_bundle; 148 u8 pad0; 149 u8 pad1; 150 } __packed; 151 152 struct ath10k_htc_conn_svc { 153 __le16 service_id; 154 __le16 flags; /* @enum ath10k_htc_conn_flags */ 155 u8 pad0; 156 u8 pad1; 157 } __packed; 158 159 struct ath10k_htc_conn_svc_response { 160 __le16 service_id; 161 u8 status; /* @enum ath10k_htc_conn_svc_status */ 162 u8 eid; 163 __le16 max_msg_size; 164 } __packed; 165 166 struct ath10k_htc_setup_complete_extended { 167 u8 pad0; 168 u8 pad1; 169 __le32 flags; /* @enum htc_setup_complete_flags */ 170 u8 max_msgs_per_bundled_recv; 171 u8 pad2; 172 u8 pad3; 173 u8 pad4; 174 } __packed; 175 176 struct ath10k_htc_msg { 177 struct ath10k_ath10k_htc_msg_hdr hdr; 178 union { 179 /* host-to-target */ 180 struct ath10k_htc_conn_svc connect_service; 181 struct ath10k_htc_ready ready; 182 struct ath10k_htc_ready_extended ready_ext; 183 struct ath10k_htc_unknown unknown; 184 struct ath10k_htc_setup_complete_extended setup_complete_ext; 185 186 /* target-to-host */ 187 struct ath10k_htc_conn_svc_response connect_service_response; 188 }; 189 } __packed __aligned(4); 190 191 enum ath10k_ath10k_htc_record_id { 192 ATH10K_HTC_RECORD_NULL = 0, 193 ATH10K_HTC_RECORD_CREDITS = 1, 194 ATH10K_HTC_RECORD_LOOKAHEAD = 2, 195 ATH10K_HTC_RECORD_LOOKAHEAD_BUNDLE = 3, 196 }; 197 198 struct ath10k_ath10k_htc_record_hdr { 199 u8 id; /* @enum ath10k_ath10k_htc_record_id */ 200 u8 len; 201 u8 pad0; 202 u8 pad1; 203 } __packed; 204 205 struct ath10k_htc_credit_report { 206 u8 eid; /* @enum ath10k_htc_ep_id */ 207 u8 credits; 208 u8 pad0; 209 u8 pad1; 210 } __packed; 211 212 struct ath10k_htc_lookahead_report { 213 u8 pre_valid; 214 u8 pad0; 215 u8 pad1; 216 u8 pad2; 217 u8 lookahead[4]; 218 u8 post_valid; 219 u8 pad3; 220 u8 pad4; 221 u8 pad5; 222 } __packed; 223 224 struct ath10k_htc_lookahead_bundle { 225 u8 lookahead[4]; 226 } __packed; 227 228 struct ath10k_htc_record { 229 struct ath10k_ath10k_htc_record_hdr hdr; 230 union { 231 struct ath10k_htc_credit_report credit_report[0]; 232 struct ath10k_htc_lookahead_report lookahead_report[0]; 233 struct ath10k_htc_lookahead_bundle lookahead_bundle[0]; 234 u8 pauload[0]; 235 }; 236 } __packed __aligned(4); 237 238 /* 239 * note: the trailer offset is dynamic depending 240 * on payload length. this is only a struct layout draft 241 */ 242 struct ath10k_htc_frame { 243 struct ath10k_htc_hdr hdr; 244 union { 245 struct ath10k_htc_msg msg; 246 u8 payload[0]; 247 }; 248 struct ath10k_htc_record trailer[0]; 249 } __packed __aligned(4); 250 251 /*******************/ 252 /* Host-side stuff */ 253 /*******************/ 254 255 enum ath10k_htc_svc_gid { 256 ATH10K_HTC_SVC_GRP_RSVD = 0, 257 ATH10K_HTC_SVC_GRP_WMI = 1, 258 ATH10K_HTC_SVC_GRP_NMI = 2, 259 ATH10K_HTC_SVC_GRP_HTT = 3, 260 ATH10K_LOG_SERVICE_GROUP = 6, 261 262 ATH10K_HTC_SVC_GRP_TEST = 254, 263 ATH10K_HTC_SVC_GRP_LAST = 255, 264 }; 265 266 #define SVC(group, idx) \ 267 (int)(((int)(group) << 8) | (int)(idx)) 268 269 enum ath10k_htc_svc_id { 270 /* NOTE: service ID of 0x0000 is reserved and should never be used */ 271 ATH10K_HTC_SVC_ID_RESERVED = 0x0000, 272 ATH10K_HTC_SVC_ID_UNUSED = ATH10K_HTC_SVC_ID_RESERVED, 273 274 ATH10K_HTC_SVC_ID_RSVD_CTRL = SVC(ATH10K_HTC_SVC_GRP_RSVD, 1), 275 ATH10K_HTC_SVC_ID_WMI_CONTROL = SVC(ATH10K_HTC_SVC_GRP_WMI, 0), 276 ATH10K_HTC_SVC_ID_WMI_DATA_BE = SVC(ATH10K_HTC_SVC_GRP_WMI, 1), 277 ATH10K_HTC_SVC_ID_WMI_DATA_BK = SVC(ATH10K_HTC_SVC_GRP_WMI, 2), 278 ATH10K_HTC_SVC_ID_WMI_DATA_VI = SVC(ATH10K_HTC_SVC_GRP_WMI, 3), 279 ATH10K_HTC_SVC_ID_WMI_DATA_VO = SVC(ATH10K_HTC_SVC_GRP_WMI, 4), 280 281 ATH10K_HTC_SVC_ID_NMI_CONTROL = SVC(ATH10K_HTC_SVC_GRP_NMI, 0), 282 ATH10K_HTC_SVC_ID_NMI_DATA = SVC(ATH10K_HTC_SVC_GRP_NMI, 1), 283 284 ATH10K_HTC_SVC_ID_HTT_DATA_MSG = SVC(ATH10K_HTC_SVC_GRP_HTT, 0), 285 286 ATH10K_HTC_SVC_ID_HTT_DATA2_MSG = SVC(ATH10K_HTC_SVC_GRP_HTT, 1), 287 ATH10K_HTC_SVC_ID_HTT_DATA3_MSG = SVC(ATH10K_HTC_SVC_GRP_HTT, 2), 288 ATH10K_HTC_SVC_ID_HTT_LOG_MSG = SVC(ATH10K_LOG_SERVICE_GROUP, 0), 289 /* raw stream service (i.e. flash, tcmd, calibration apps) */ 290 ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS = SVC(ATH10K_HTC_SVC_GRP_TEST, 0), 291 }; 292 293 #undef SVC 294 295 enum ath10k_htc_ep_id { 296 ATH10K_HTC_EP_UNUSED = -1, 297 ATH10K_HTC_EP_0 = 0, 298 ATH10K_HTC_EP_1 = 1, 299 ATH10K_HTC_EP_2, 300 ATH10K_HTC_EP_3, 301 ATH10K_HTC_EP_4, 302 ATH10K_HTC_EP_5, 303 ATH10K_HTC_EP_6, 304 ATH10K_HTC_EP_7, 305 ATH10K_HTC_EP_8, 306 ATH10K_HTC_EP_COUNT, 307 }; 308 309 struct ath10k_htc_ops { 310 void (*target_send_suspend_complete)(struct ath10k *ar); 311 }; 312 313 struct ath10k_htc_ep_ops { 314 void (*ep_tx_complete)(struct ath10k *, struct sk_buff *); 315 void (*ep_rx_complete)(struct ath10k *, struct sk_buff *); 316 void (*ep_tx_credits)(struct ath10k *); 317 }; 318 319 /* service connection information */ 320 struct ath10k_htc_svc_conn_req { 321 u16 service_id; 322 struct ath10k_htc_ep_ops ep_ops; 323 int max_send_queue_depth; 324 }; 325 326 /* service connection response information */ 327 struct ath10k_htc_svc_conn_resp { 328 u8 buffer_len; 329 u8 actual_len; 330 enum ath10k_htc_ep_id eid; 331 unsigned int max_msg_len; 332 u8 connect_resp_code; 333 }; 334 335 #define ATH10K_NUM_CONTROL_TX_BUFFERS 2 336 #define ATH10K_HTC_MAX_LEN 4096 337 #define ATH10K_HTC_MAX_CTRL_MSG_LEN 256 338 #define ATH10K_HTC_WAIT_TIMEOUT_HZ (1 * HZ) 339 #define ATH10K_HTC_CONTROL_BUFFER_SIZE (ATH10K_HTC_MAX_CTRL_MSG_LEN + \ 340 sizeof(struct ath10k_htc_hdr)) 341 #define ATH10K_HTC_CONN_SVC_TIMEOUT_HZ (1 * HZ) 342 343 struct ath10k_htc_ep { 344 struct ath10k_htc *htc; 345 enum ath10k_htc_ep_id eid; 346 enum ath10k_htc_svc_id service_id; 347 struct ath10k_htc_ep_ops ep_ops; 348 349 int max_tx_queue_depth; 350 int max_ep_message_len; 351 u8 ul_pipe_id; 352 u8 dl_pipe_id; 353 354 u8 seq_no; /* for debugging */ 355 int tx_credits; 356 bool tx_credit_flow_enabled; 357 }; 358 359 struct ath10k_htc_svc_tx_credits { 360 u16 service_id; 361 u8 credit_allocation; 362 }; 363 364 struct ath10k_htc { 365 struct ath10k *ar; 366 struct ath10k_htc_ep endpoint[ATH10K_HTC_EP_COUNT]; 367 368 /* protects endpoints */ 369 spinlock_t tx_lock; 370 371 struct ath10k_htc_ops htc_ops; 372 373 u8 control_resp_buffer[ATH10K_HTC_MAX_CTRL_MSG_LEN]; 374 int control_resp_len; 375 376 struct completion ctl_resp; 377 378 int total_transmit_credits; 379 int target_credit_size; 380 u8 max_msgs_per_htc_bundle; 381 }; 382 383 int ath10k_htc_init(struct ath10k *ar); 384 int ath10k_htc_wait_target(struct ath10k_htc *htc); 385 int ath10k_htc_start(struct ath10k_htc *htc); 386 int ath10k_htc_connect_service(struct ath10k_htc *htc, 387 struct ath10k_htc_svc_conn_req *conn_req, 388 struct ath10k_htc_svc_conn_resp *conn_resp); 389 int ath10k_htc_send(struct ath10k_htc *htc, enum ath10k_htc_ep_id eid, 390 struct sk_buff *packet); 391 struct sk_buff *ath10k_htc_alloc_skb(struct ath10k *ar, int size); 392 void ath10k_htc_tx_completion_handler(struct ath10k *ar, struct sk_buff *skb); 393 void ath10k_htc_rx_completion_handler(struct ath10k *ar, struct sk_buff *skb); 394 void ath10k_htc_notify_tx_completion(struct ath10k_htc_ep *ep, 395 struct sk_buff *skb); 396 int ath10k_htc_process_trailer(struct ath10k_htc *htc, 397 u8 *buffer, 398 int length, 399 enum ath10k_htc_ep_id src_eid, 400 void *next_lookaheads, 401 int *next_lookaheads_len); 402 403 #endif 404