| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* MHI Endpoint bus stack |
| 4 |
* |
| 5 |
* Copyright (C) 2022 Linaro Ltd. |
| 6 |
* Author: Manivannan Sadhasivam <[email protected]> |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <linux/bitfield.h> |
| 10 |
#include <linux/delay.h> |
| 11 |
#include <linux/dma-direction.h> |
| 12 |
#include <linux/interrupt.h> |
| 13 |
#include <linux/io.h> |
| 14 |
#include <linux/irq.h> |
| 15 |
#include <linux/mhi_ep.h> |
| 16 |
#include <linux/mod_devicetable.h> |
| 17 |
#include <linux/module.h> |
| 18 |
#include "internal.h" |
| 19 |
|
| 20 |
#define M0_WAIT_DELAY_MS 100 |
| 21 |
#define M0_WAIT_COUNT 100 |
| 22 |
|
| 23 |
static DEFINE_IDA(mhi_ep_cntrl_ida); |
| 24 |
|
| 25 |
static int mhi_ep_create_device(struct mhi_ep_cntrl *mhi_cntrl, u32 ch_id); |
| 26 |
static int mhi_ep_destroy_device(struct device *dev, void *data); |
| 27 |
|
| 28 |
static int mhi_ep_send_event(struct mhi_ep_cntrl *mhi_cntrl, u32 ring_idx, |
| 29 |
struct mhi_ring_element *el, bool bei) |
| 30 |
{ |
| 31 |
struct device *dev = &mhi_cntrl->mhi_dev->dev; |
| 32 |
union mhi_ep_ring_ctx *ctx; |
| 33 |
struct mhi_ep_ring *ring; |
| 34 |
int ret; |
| 35 |
|
| 36 |
mutex_lock(&mhi_cntrl->event_lock); |
| 37 |
ring = &mhi_cntrl->mhi_event[ring_idx].ring; |
| 38 |
ctx = (union mhi_ep_ring_ctx *)&mhi_cntrl->ev_ctx_cache[ring_idx]; |
| 39 |
if (!ring->started) { |
| 40 |
ret = mhi_ep_ring_start(mhi_cntrl, ring, ctx); |
| 41 |
if (ret) { |
| 42 |
dev_err(dev, "Error starting event ring (%u)\n", ring_idx); |
| 43 |
goto err_unlock; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/* Add element to the event ring */ |
| 48 |
ret = mhi_ep_ring_add_element(ring, el); |
| 49 |
if (ret) { |
| 50 |
dev_err(dev, "Error adding element to event ring (%u)\n", ring_idx); |
| 51 |
goto err_unlock; |
| 52 |
} |
| 53 |
|
| 54 |
mutex_unlock(&mhi_cntrl->event_lock); |
| 55 |
|
| 56 |
/* |
| 57 |
* As per the MHI specification, section 4.3, Interrupt moderation: |
| 58 |
* |
| 59 |
* 1. If BEI flag is not set, cancel any pending intmodt work if started |
| 60 |
* for the event ring and raise IRQ immediately. |
| 61 |
* |
| 62 |
* 2. If both BEI and intmodt are set, and if no IRQ is pending for the |
| 63 |
* same event ring, start the IRQ delayed work as per the value of |
| 64 |
* intmodt. If previous IRQ is pending, then do nothing as the pending |
| 65 |
* IRQ is enough for the host to process the current event ring element. |
| 66 |
* |
| 67 |
* 3. If BEI is set and intmodt is not set, no need to raise IRQ. |
| 68 |
*/ |
| 69 |
if (!bei) { |
| 70 |
if (READ_ONCE(ring->irq_pending)) |
| 71 |
cancel_delayed_work(&ring->intmodt_work); |
| 72 |
|
| 73 |
mhi_cntrl->raise_irq(mhi_cntrl, ring->irq_vector); |
| 74 |
} else if (ring->intmodt && !READ_ONCE(ring->irq_pending)) { |
| 75 |
WRITE_ONCE(ring->irq_pending, true); |
| 76 |
schedule_delayed_work(&ring->intmodt_work, msecs_to_jiffies(ring->intmodt)); |
| 77 |
} |
| 78 |
|
| 79 |
return 0; |
| 80 |
|
| 81 |
err_unlock: |
| 82 |
mutex_unlock(&mhi_cntrl->event_lock); |
| 83 |
|
| 84 |
return ret; |
| 85 |
} |
| 86 |
|
| 87 |
static int mhi_ep_send_completion_event(struct mhi_ep_cntrl *mhi_cntrl, struct mhi_ep_ring *ring, |
| 88 |
struct mhi_ring_element *tre, u32 len, enum mhi_ev_ccs code) |
| 89 |
{ |
| 90 |
struct mhi_ring_element *event; |
| 91 |
int ret; |
| 92 |
|
| 93 |
event = kmem_cache_zalloc(mhi_cntrl->ev_ring_el_cache, GFP_KERNEL); |
| 94 |
if (!event) |
| 95 |
return -ENOMEM; |
| 96 |
|
| 97 |
event->ptr = cpu_to_le64(ring->rbase + ring->rd_offset * sizeof(*tre)); |
| 98 |
event->dword[0] = MHI_TRE_EV_DWORD0(code, len); |
| 99 |
event->dword[1] = MHI_TRE_EV_DWORD1(ring->ch_id, MHI_PKT_TYPE_TX_EVENT); |
| 100 |
|
| 101 |
ret = mhi_ep_send_event(mhi_cntrl, ring->er_index, event, MHI_TRE_DATA_GET_BEI(tre)); |
| 102 |
kmem_cache_free(mhi_cntrl->ev_ring_el_cache, event); |
| 103 |
|
| 104 |
return ret; |
| 105 |
} |
| 106 |
|
| 107 |
int mhi_ep_send_state_change_event(struct mhi_ep_cntrl *mhi_cntrl, enum mhi_state state) |
| 108 |
{ |
| 109 |
struct mhi_ring_element *event; |
| 110 |
int ret; |
| 111 |
|
| 112 |
event = kmem_cache_zalloc(mhi_cntrl->ev_ring_el_cache, GFP_KERNEL); |
| 113 |
if (!event) |
| 114 |
return -ENOMEM; |
| 115 |
|
| 116 |
event->dword[0] = MHI_SC_EV_DWORD0(state); |
| 117 |
event->dword[1] = MHI_SC_EV_DWORD1(MHI_PKT_TYPE_STATE_CHANGE_EVENT); |
| 118 |
|
| 119 |
ret = mhi_ep_send_event(mhi_cntrl, 0, event, 0); |
| 120 |
kmem_cache_free(mhi_cntrl->ev_ring_el_cache, event); |
| 121 |
|
| 122 |
return ret; |
| 123 |
} |
| 124 |
|
| 125 |
int mhi_ep_send_ee_event(struct mhi_ep_cntrl *mhi_cntrl, enum mhi_ee_type exec_env) |
| 126 |
{ |
| 127 |
struct mhi_ring_element *event; |
| 128 |
int ret; |
| 129 |
|
| 130 |
event = kmem_cache_zalloc(mhi_cntrl->ev_ring_el_cache, GFP_KERNEL); |
| 131 |
if (!event) |
| 132 |
return -ENOMEM; |
| 133 |
|
| 134 |
event->dword[0] = MHI_EE_EV_DWORD0(exec_env); |
| 135 |
event->dword[1] = MHI_SC_EV_DWORD1(MHI_PKT_TYPE_EE_EVENT); |
| 136 |
|
| 137 |
ret = mhi_ep_send_event(mhi_cntrl, 0, event, 0); |
| 138 |
kmem_cache_free(mhi_cntrl->ev_ring_el_cache, event); |
| 139 |
|
| 140 |
return ret; |
| 141 |
} |
| 142 |
|
| 143 |
static int mhi_ep_send_cmd_comp_event(struct mhi_ep_cntrl *mhi_cntrl, enum mhi_ev_ccs code) |
| 144 |
{ |
| 145 |
struct mhi_ep_ring *ring = &mhi_cntrl->mhi_cmd->ring; |
| 146 |
struct mhi_ring_element *event; |
| 147 |
int ret; |
| 148 |
|
| 149 |
event = kmem_cache_zalloc(mhi_cntrl->ev_ring_el_cache, GFP_KERNEL); |
| 150 |
if (!event) |
| 151 |
return -ENOMEM; |
| 152 |
|
| 153 |
event->ptr = cpu_to_le64(ring->rbase + ring->rd_offset * sizeof(struct mhi_ring_element)); |
| 154 |
event->dword[0] = MHI_CC_EV_DWORD0(code); |
| 155 |
event->dword[1] = MHI_CC_EV_DWORD1(MHI_PKT_TYPE_CMD_COMPLETION_EVENT); |
| 156 |
|
| 157 |
ret = mhi_ep_send_event(mhi_cntrl, 0, event, 0); |
| 158 |
kmem_cache_free(mhi_cntrl->ev_ring_el_cache, event); |
| 159 |
|
| 160 |
return ret; |
| 161 |
} |
| 162 |
|
| 163 |
static int mhi_ep_process_cmd_ring(struct mhi_ep_ring *ring, struct mhi_ring_element *el) |
| 164 |
{ |
| 165 |
struct mhi_ep_cntrl *mhi_cntrl = ring->mhi_cntrl; |
| 166 |
struct device *dev = &mhi_cntrl->mhi_dev->dev; |
| 167 |
struct mhi_result result = {}; |
| 168 |
struct mhi_ep_chan *mhi_chan; |
| 169 |
struct mhi_ep_ring *ch_ring; |
| 170 |
u32 tmp, ch_id; |
| 171 |
int ret; |
| 172 |
|
| 173 |
ch_id = MHI_TRE_GET_CMD_CHID(el); |
| 174 |
|
| 175 |
/* Check if the channel is supported by the controller */ |
| 176 |
if ((ch_id >= mhi_cntrl->max_chan) || !mhi_cntrl->mhi_chan[ch_id].name) { |
| 177 |
dev_dbg(dev, "Channel (%u) not supported!\n", ch_id); |
| 178 |
return -ENODEV; |
| 179 |
} |
| 180 |
|
| 181 |
mhi_chan = &mhi_cntrl->mhi_chan[ch_id]; |
| 182 |
ch_ring = &mhi_cntrl->mhi_chan[ch_id].ring; |
| 183 |
|
| 184 |
switch (MHI_TRE_GET_CMD_TYPE(el)) { |
| 185 |
case MHI_PKT_TYPE_START_CHAN_CMD: |
| 186 |
dev_dbg(dev, "Received START command for channel (%u)\n", ch_id); |
| 187 |
|
| 188 |
mutex_lock(&mhi_chan->lock); |
| 189 |
/* Initialize and configure the corresponding channel ring */ |
| 190 |
if (!ch_ring->started) { |
| 191 |
ret = mhi_ep_ring_start(mhi_cntrl, ch_ring, |
| 192 |
(union mhi_ep_ring_ctx *)&mhi_cntrl->ch_ctx_cache[ch_id]); |
| 193 |
if (ret) { |
| 194 |
dev_err(dev, "Failed to start ring for channel (%u)\n", ch_id); |
| 195 |
ret = mhi_ep_send_cmd_comp_event(mhi_cntrl, |
| 196 |
MHI_EV_CC_UNDEFINED_ERR); |
| 197 |
if (ret) |
| 198 |
dev_err(dev, "Error sending completion event: %d\n", ret); |
| 199 |
|
| 200 |
goto err_unlock; |
| 201 |
} |
| 202 |
|
| 203 |
mhi_chan->rd_offset = ch_ring->rd_offset; |
| 204 |
} |
| 205 |
|
| 206 |
/* Set channel state to RUNNING */ |
| 207 |
mhi_chan->state = MHI_CH_STATE_RUNNING; |
| 208 |
tmp = le32_to_cpu(mhi_cntrl->ch_ctx_cache[ch_id].chcfg); |
| 209 |
tmp &= ~CHAN_CTX_CHSTATE_MASK; |
| 210 |
tmp |= FIELD_PREP(CHAN_CTX_CHSTATE_MASK, MHI_CH_STATE_RUNNING); |
| 211 |
mhi_cntrl->ch_ctx_cache[ch_id].chcfg = cpu_to_le32(tmp); |
| 212 |
|
| 213 |
ret = mhi_ep_send_cmd_comp_event(mhi_cntrl, MHI_EV_CC_SUCCESS); |
| 214 |
if (ret) { |
| 215 |
dev_err(dev, "Error sending command completion event (%u)\n", |
| 216 |
MHI_EV_CC_SUCCESS); |
| 217 |
goto err_unlock; |
| 218 |
} |
| 219 |
|
| 220 |
mutex_unlock(&mhi_chan->lock); |
| 221 |
|
| 222 |
/* |
| 223 |
* Create MHI device only during UL channel start. Since the MHI |
| 224 |
* channels operate in a pair, we'll associate both UL and DL |
| 225 |
* channels to the same device. |
| 226 |
* |
| 227 |
* We also need to check for mhi_dev != NULL because, the host |
| 228 |
* will issue START_CHAN command during resume and we don't |
| 229 |
* destroy the device during suspend. |
| 230 |
*/ |
| 231 |
if (!(ch_id % 2) && !mhi_chan->mhi_dev) { |
| 232 |
ret = mhi_ep_create_device(mhi_cntrl, ch_id); |
| 233 |
if (ret) { |
| 234 |
dev_err(dev, "Error creating device for channel (%u)\n", ch_id); |
| 235 |
mhi_ep_handle_syserr(mhi_cntrl); |
| 236 |
return ret; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
/* Finally, enable DB for the channel */ |
| 241 |
mhi_ep_mmio_enable_chdb(mhi_cntrl, ch_id); |
| 242 |
|
| 243 |
break; |
| 244 |
case MHI_PKT_TYPE_STOP_CHAN_CMD: |
| 245 |
dev_dbg(dev, "Received STOP command for channel (%u)\n", ch_id); |
| 246 |
if (!ch_ring->started) { |
| 247 |
dev_err(dev, "Channel (%u) not opened\n", ch_id); |
| 248 |
return -ENODEV; |
| 249 |
} |
| 250 |
|
| 251 |
mutex_lock(&mhi_chan->lock); |
| 252 |
/* Disable DB for the channel */ |
| 253 |
mhi_ep_mmio_disable_chdb(mhi_cntrl, ch_id); |
| 254 |
|
| 255 |
/* Send channel disconnect status to client drivers */ |
| 256 |
if (mhi_chan->xfer_cb) { |
| 257 |
result.transaction_status = -ENOTCONN; |
| 258 |
result.bytes_xferd = 0; |
| 259 |
mhi_chan->xfer_cb(mhi_chan->mhi_dev, &result); |
| 260 |
} |
| 261 |
|
| 262 |
/* Set channel state to STOP */ |
| 263 |
mhi_chan->state = MHI_CH_STATE_STOP; |
| 264 |
tmp = le32_to_cpu(mhi_cntrl->ch_ctx_cache[ch_id].chcfg); |
| 265 |
tmp &= ~CHAN_CTX_CHSTATE_MASK; |
| 266 |
tmp |= FIELD_PREP(CHAN_CTX_CHSTATE_MASK, MHI_CH_STATE_STOP); |
| 267 |
mhi_cntrl->ch_ctx_cache[ch_id].chcfg = cpu_to_le32(tmp); |
| 268 |
|
| 269 |
ret = mhi_ep_send_cmd_comp_event(mhi_cntrl, MHI_EV_CC_SUCCESS); |
| 270 |
if (ret) { |
| 271 |
dev_err(dev, "Error sending command completion event (%u)\n", |
| 272 |
MHI_EV_CC_SUCCESS); |
| 273 |
goto err_unlock; |
| 274 |
} |
| 275 |
|
| 276 |
mutex_unlock(&mhi_chan->lock); |
| 277 |
break; |
| 278 |
case MHI_PKT_TYPE_RESET_CHAN_CMD: |
| 279 |
dev_dbg(dev, "Received RESET command for channel (%u)\n", ch_id); |
| 280 |
if (!ch_ring->started) { |
| 281 |
dev_err(dev, "Channel (%u) not opened\n", ch_id); |
| 282 |
return -ENODEV; |
| 283 |
} |
| 284 |
|
| 285 |
mutex_lock(&mhi_chan->lock); |
| 286 |
/* Stop and reset the transfer ring */ |
| 287 |
mhi_ep_ring_reset(mhi_cntrl, ch_ring); |
| 288 |
|
| 289 |
/* Send channel disconnect status to client driver */ |
| 290 |
if (mhi_chan->xfer_cb) { |
| 291 |
result.transaction_status = -ENOTCONN; |
| 292 |
result.bytes_xferd = 0; |
| 293 |
mhi_chan->xfer_cb(mhi_chan->mhi_dev, &result); |
| 294 |
} |
| 295 |
|
| 296 |
/* Set channel state to DISABLED */ |
| 297 |
mhi_chan->state = MHI_CH_STATE_DISABLED; |
| 298 |
tmp = le32_to_cpu(mhi_cntrl->ch_ctx_cache[ch_id].chcfg); |
| 299 |
tmp &= ~CHAN_CTX_CHSTATE_MASK; |
| 300 |
tmp |= FIELD_PREP(CHAN_CTX_CHSTATE_MASK, MHI_CH_STATE_DISABLED); |
| 301 |
mhi_cntrl->ch_ctx_cache[ch_id].chcfg = cpu_to_le32(tmp); |
| 302 |
|
| 303 |
ret = mhi_ep_send_cmd_comp_event(mhi_cntrl, MHI_EV_CC_SUCCESS); |
| 304 |
if (ret) { |
| 305 |
dev_err(dev, "Error sending command completion event (%u)\n", |
| 306 |
MHI_EV_CC_SUCCESS); |
| 307 |
goto err_unlock; |
| 308 |
} |
| 309 |
|
| 310 |
mutex_unlock(&mhi_chan->lock); |
| 311 |
break; |
| 312 |
default: |
| 313 |
dev_err(dev, "Invalid command received: %lu for channel (%u)\n", |
| 314 |
MHI_TRE_GET_CMD_TYPE(el), ch_id); |
| 315 |
return -EINVAL; |
| 316 |
} |
| 317 |
|
| 318 |
return 0; |
| 319 |
|
| 320 |
err_unlock: |
| 321 |
mutex_unlock(&mhi_chan->lock); |
| 322 |
|
| 323 |
return ret; |
| 324 |
} |
| 325 |
|
| 326 |
bool mhi_ep_queue_is_empty(struct mhi_ep_device *mhi_dev, enum dma_data_direction dir) |
| 327 |
{ |
| 328 |
struct mhi_ep_chan *mhi_chan = (dir == DMA_FROM_DEVICE) ? mhi_dev->dl_chan : |
| 329 |
mhi_dev->ul_chan; |
| 330 |
struct mhi_ep_cntrl *mhi_cntrl = mhi_dev->mhi_cntrl; |
| 331 |
struct mhi_ep_ring *ring = &mhi_cntrl->mhi_chan[mhi_chan->chan].ring; |
| 332 |
|
| 333 |
return !!(mhi_chan->rd_offset == ring->wr_offset); |
| 334 |
} |
| 335 |
EXPORT_SYMBOL_GPL(mhi_ep_queue_is_empty); |
| 336 |
|
| 337 |
static void mhi_ep_read_completion(struct mhi_ep_buf_info *buf_info) |
| 338 |
{ |
| 339 |
struct mhi_ep_device *mhi_dev = buf_info->mhi_dev; |
| 340 |
struct mhi_ep_cntrl *mhi_cntrl = mhi_dev->mhi_cntrl; |
| 341 |
struct mhi_ep_chan *mhi_chan = mhi_dev->ul_chan; |
| 342 |
struct mhi_ep_ring *ring = &mhi_cntrl->mhi_chan[mhi_chan->chan].ring; |
| 343 |
struct mhi_ring_element *el = &ring->ring_cache[ring->rd_offset]; |
| 344 |
struct mhi_result result = {}; |
| 345 |
int ret; |
| 346 |
|
| 347 |
if (mhi_chan->xfer_cb) { |
| 348 |
result.buf_addr = buf_info->cb_buf; |
| 349 |
result.dir = mhi_chan->dir; |
| 350 |
result.bytes_xferd = buf_info->size; |
| 351 |
|
| 352 |
mhi_chan->xfer_cb(mhi_dev, &result); |
| 353 |
} |
| 354 |
|
| 355 |
/* |
| 356 |
* The host will split the data packet into multiple TREs if it can't fit |
| 357 |
* the packet in a single TRE. In that case, CHAIN flag will be set by the |
| 358 |
* host for all TREs except the last one. |
| 359 |
*/ |
| 360 |
if (buf_info->code != MHI_EV_CC_OVERFLOW) { |
| 361 |
if (MHI_TRE_DATA_GET_CHAIN(el)) { |
| 362 |
/* |
| 363 |
* IEOB (Interrupt on End of Block) flag will be set by the host if |
| 364 |
* it expects the completion event for all TREs of a TD. |
| 365 |
*/ |
| 366 |
if (MHI_TRE_DATA_GET_IEOB(el)) { |
| 367 |
ret = mhi_ep_send_completion_event(mhi_cntrl, ring, el, |
| 368 |
MHI_TRE_DATA_GET_LEN(el), |
| 369 |
MHI_EV_CC_EOB); |
| 370 |
if (ret < 0) { |
| 371 |
dev_err(&mhi_chan->mhi_dev->dev, |
| 372 |
"Error sending transfer compl. event\n"); |
| 373 |
goto err_free_tre_buf; |
| 374 |
} |
| 375 |
} |
| 376 |
} else { |
| 377 |
/* |
| 378 |
* IEOT (Interrupt on End of Transfer) flag will be set by the host |
| 379 |
* for the last TRE of the TD and expects the completion event for |
| 380 |
* the same. |
| 381 |
*/ |
| 382 |
if (MHI_TRE_DATA_GET_IEOT(el)) { |
| 383 |
ret = mhi_ep_send_completion_event(mhi_cntrl, ring, el, |
| 384 |
MHI_TRE_DATA_GET_LEN(el), |
| 385 |
MHI_EV_CC_EOT); |
| 386 |
if (ret < 0) { |
| 387 |
dev_err(&mhi_chan->mhi_dev->dev, |
| 388 |
"Error sending transfer compl. event\n"); |
| 389 |
goto err_free_tre_buf; |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
mhi_ep_ring_inc_index(ring); |
| 396 |
|
| 397 |
err_free_tre_buf: |
| 398 |
kmem_cache_free(mhi_cntrl->tre_buf_cache, buf_info->cb_buf); |
| 399 |
} |
| 400 |
|
| 401 |
static int mhi_ep_read_channel(struct mhi_ep_cntrl *mhi_cntrl, |
| 402 |
struct mhi_ep_ring *ring) |
| 403 |
{ |
| 404 |
struct mhi_ep_chan *mhi_chan = &mhi_cntrl->mhi_chan[ring->ch_id]; |
| 405 |
struct device *dev = &mhi_cntrl->mhi_dev->dev; |
| 406 |
size_t tr_len, read_offset; |
| 407 |
struct mhi_ep_buf_info buf_info = {}; |
| 408 |
u32 len = MHI_EP_DEFAULT_MTU; |
| 409 |
struct mhi_ring_element *el; |
| 410 |
void *buf_addr; |
| 411 |
int ret; |
| 412 |
|
| 413 |
do { |
| 414 |
/* Don't process the transfer ring if the channel is not in RUNNING state */ |
| 415 |
if (mhi_chan->state != MHI_CH_STATE_RUNNING) { |
| 416 |
dev_err(dev, "Channel not available\n"); |
| 417 |
return -ENODEV; |
| 418 |
} |
| 419 |
|
| 420 |
el = &ring->ring_cache[mhi_chan->rd_offset]; |
| 421 |
|
| 422 |
/* Check if there is data pending to be read from previous read operation */ |
| 423 |
if (mhi_chan->tre_bytes_left) { |
| 424 |
dev_dbg(dev, "TRE bytes remaining: %u\n", mhi_chan->tre_bytes_left); |
| 425 |
tr_len = min(len, mhi_chan->tre_bytes_left); |
| 426 |
} else { |
| 427 |
mhi_chan->tre_loc = MHI_TRE_DATA_GET_PTR(el); |
| 428 |
mhi_chan->tre_size = MHI_TRE_DATA_GET_LEN(el); |
| 429 |
mhi_chan->tre_bytes_left = mhi_chan->tre_size; |
| 430 |
|
| 431 |
tr_len = min(len, mhi_chan->tre_size); |
| 432 |
} |
| 433 |
|
| 434 |
read_offset = mhi_chan->tre_size - mhi_chan->tre_bytes_left; |
| 435 |
|
| 436 |
buf_addr = kmem_cache_zalloc(mhi_cntrl->tre_buf_cache, GFP_KERNEL); |
| 437 |
if (!buf_addr) |
| 438 |
return -ENOMEM; |
| 439 |
|
| 440 |
buf_info.host_addr = mhi_chan->tre_loc + read_offset; |
| 441 |
buf_info.dev_addr = buf_addr; |
| 442 |
buf_info.size = tr_len; |
| 443 |
buf_info.cb = mhi_ep_read_completion; |
| 444 |
buf_info.cb_buf = buf_addr; |
| 445 |
buf_info.mhi_dev = mhi_chan->mhi_dev; |
| 446 |
|
| 447 |
if (mhi_chan->tre_bytes_left - tr_len) |
| 448 |
buf_info.code = MHI_EV_CC_OVERFLOW; |
| 449 |
|
| 450 |
dev_dbg(dev, "Reading %zd bytes from channel (%u)\n", tr_len, ring->ch_id); |
| 451 |
ret = mhi_cntrl->read_async(mhi_cntrl, &buf_info); |
| 452 |
if (ret < 0) { |
| 453 |
dev_err(&mhi_chan->mhi_dev->dev, "Error reading from channel\n"); |
| 454 |
goto err_free_buf_addr; |
| 455 |
} |
| 456 |
|
| 457 |
mhi_chan->tre_bytes_left -= tr_len; |
| 458 |
|
| 459 |
if (!mhi_chan->tre_bytes_left) |
| 460 |
mhi_chan->rd_offset = (mhi_chan->rd_offset + 1) % ring->ring_size; |
| 461 |
/* Read until the some buffer is left or the ring becomes not empty */ |
| 462 |
} while (!mhi_ep_queue_is_empty(mhi_chan->mhi_dev, DMA_TO_DEVICE)); |
| 463 |
|
| 464 |
return 0; |
| 465 |
|
| 466 |
err_free_buf_addr: |
| 467 |
kmem_cache_free(mhi_cntrl->tre_buf_cache, buf_addr); |
| 468 |
|
| 469 |
return ret; |
| 470 |
} |
| 471 |
|
| 472 |
static int mhi_ep_process_ch_ring(struct mhi_ep_ring *ring) |
| 473 |
{ |
| 474 |
struct mhi_ep_cntrl *mhi_cntrl = ring->mhi_cntrl; |
| 475 |
struct mhi_result result = {}; |
| 476 |
struct mhi_ep_chan *mhi_chan; |
| 477 |
int ret; |
| 478 |
|
| 479 |
mhi_chan = &mhi_cntrl->mhi_chan[ring->ch_id]; |
| 480 |
|
| 481 |
/* |
| 482 |
* Bail out if transfer callback is not registered for the channel. |
| 483 |
* This is most likely due to the client driver not loaded at this point. |
| 484 |
*/ |
| 485 |
if (!mhi_chan->xfer_cb) { |
| 486 |
dev_err(&mhi_chan->mhi_dev->dev, "Client driver not available\n"); |
| 487 |
return -ENODEV; |
| 488 |
} |
| 489 |
|
| 490 |
if (ring->ch_id % 2) { |
| 491 |
/* DL channel */ |
| 492 |
result.dir = mhi_chan->dir; |
| 493 |
mhi_chan->xfer_cb(mhi_chan->mhi_dev, &result); |
| 494 |
} else { |
| 495 |
/* UL channel */ |
| 496 |
ret = mhi_ep_read_channel(mhi_cntrl, ring); |
| 497 |
if (ret < 0) { |
| 498 |
dev_err(&mhi_chan->mhi_dev->dev, "Failed to read channel\n"); |
| 499 |
return ret; |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
return 0; |
| 504 |
} |
| 505 |
|
| 506 |
static void mhi_ep_skb_completion(struct mhi_ep_buf_info *buf_info) |
| 507 |
{ |
| 508 |
struct mhi_ep_device *mhi_dev = buf_info->mhi_dev; |
| 509 |
struct mhi_ep_cntrl *mhi_cntrl = mhi_dev->mhi_cntrl; |
| 510 |
struct mhi_ep_chan *mhi_chan = mhi_dev->dl_chan; |
| 511 |
struct mhi_ep_ring *ring = &mhi_cntrl->mhi_chan[mhi_chan->chan].ring; |
| 512 |
struct mhi_ring_element *el = &ring->ring_cache[ring->rd_offset]; |
| 513 |
struct device *dev = &mhi_dev->dev; |
| 514 |
struct mhi_result result = {}; |
| 515 |
int ret; |
| 516 |
|
| 517 |
if (mhi_chan->xfer_cb) { |
| 518 |
result.buf_addr = buf_info->cb_buf; |
| 519 |
result.dir = mhi_chan->dir; |
| 520 |
result.bytes_xferd = buf_info->size; |
| 521 |
|
| 522 |
mhi_chan->xfer_cb(mhi_dev, &result); |
| 523 |
} |
| 524 |
|
| 525 |
ret = mhi_ep_send_completion_event(mhi_cntrl, ring, el, buf_info->size, |
| 526 |
buf_info->code); |
| 527 |
if (ret) { |
| 528 |
dev_err(dev, "Error sending transfer completion event\n"); |
| 529 |
return; |
| 530 |
} |
| 531 |
|
| 532 |
mhi_ep_ring_inc_index(ring); |
| 533 |
} |
| 534 |
|
| 535 |
/* TODO: Handle partially formed TDs */ |
| 536 |
int mhi_ep_queue_skb(struct mhi_ep_device *mhi_dev, struct sk_buff *skb) |
| 537 |
{ |
| 538 |
struct mhi_ep_cntrl *mhi_cntrl = mhi_dev->mhi_cntrl; |
| 539 |
struct mhi_ep_chan *mhi_chan = mhi_dev->dl_chan; |
| 540 |
struct device *dev = &mhi_chan->mhi_dev->dev; |
| 541 |
struct mhi_ep_buf_info buf_info = {}; |
| 542 |
struct mhi_ring_element *el; |
| 543 |
u32 buf_left, read_offset; |
| 544 |
struct mhi_ep_ring *ring; |
| 545 |
size_t tr_len; |
| 546 |
u32 tre_len; |
| 547 |
int ret; |
| 548 |
|
| 549 |
buf_left = skb->len; |
| 550 |
ring = &mhi_cntrl->mhi_chan[mhi_chan->chan].ring; |
| 551 |
|
| 552 |
mutex_lock(&mhi_chan->lock); |
| 553 |
|
| 554 |
do { |
| 555 |
/* Don't process the transfer ring if the channel is not in RUNNING state */ |
| 556 |
if (mhi_chan->state != MHI_CH_STATE_RUNNING) { |
| 557 |
dev_err(dev, "Channel not available\n"); |
| 558 |
ret = -ENODEV; |
| 559 |
goto err_exit; |
| 560 |
} |
| 561 |
|
| 562 |
if (mhi_ep_queue_is_empty(mhi_dev, DMA_FROM_DEVICE)) { |
| 563 |
dev_err(dev, "TRE not available!\n"); |
| 564 |
ret = -ENOSPC; |
| 565 |
goto err_exit; |
| 566 |
} |
| 567 |
|
| 568 |
el = &ring->ring_cache[mhi_chan->rd_offset]; |
| 569 |
tre_len = MHI_TRE_DATA_GET_LEN(el); |
| 570 |
|
| 571 |
tr_len = min(buf_left, tre_len); |
| 572 |
read_offset = skb->len - buf_left; |
| 573 |
|
| 574 |
buf_info.dev_addr = skb->data + read_offset; |
| 575 |
buf_info.host_addr = MHI_TRE_DATA_GET_PTR(el); |
| 576 |
buf_info.size = tr_len; |
| 577 |
buf_info.cb = mhi_ep_skb_completion; |
| 578 |
buf_info.cb_buf = skb; |
| 579 |
buf_info.mhi_dev = mhi_dev; |
| 580 |
|
| 581 |
/* |
| 582 |
* For all TREs queued by the host for DL channel, only the EOT flag will be set. |
| 583 |
* If the packet doesn't fit into a single TRE, send the OVERFLOW event to |
| 584 |
* the host so that the host can adjust the packet boundary to next TREs. Else send |
| 585 |
* the EOT event to the host indicating the packet boundary. |
| 586 |
*/ |
| 587 |
if (buf_left - tr_len) |
| 588 |
buf_info.code = MHI_EV_CC_OVERFLOW; |
| 589 |
else |
| 590 |
buf_info.code = MHI_EV_CC_EOT; |
| 591 |
|
| 592 |
dev_dbg(dev, "Writing %zd bytes to channel (%u)\n", tr_len, ring->ch_id); |
| 593 |
ret = mhi_cntrl->write_async(mhi_cntrl, &buf_info); |
| 594 |
if (ret < 0) { |
| 595 |
dev_err(dev, "Error writing to the channel\n"); |
| 596 |
goto err_exit; |
| 597 |
} |
| 598 |
|
| 599 |
buf_left -= tr_len; |
| 600 |
|
| 601 |
/* |
| 602 |
* Update the read offset cached in mhi_chan. Actual read offset |
| 603 |
* will be updated by the completion handler. |
| 604 |
*/ |
| 605 |
mhi_chan->rd_offset = (mhi_chan->rd_offset + 1) % ring->ring_size; |
| 606 |
} while (buf_left); |
| 607 |
|
| 608 |
mutex_unlock(&mhi_chan->lock); |
| 609 |
|
| 610 |
return 0; |
| 611 |
|
| 612 |
err_exit: |
| 613 |
mutex_unlock(&mhi_chan->lock); |
| 614 |
|
| 615 |
return ret; |
| 616 |
} |
| 617 |
EXPORT_SYMBOL_GPL(mhi_ep_queue_skb); |
| 618 |
|
| 619 |
static int mhi_ep_cache_host_cfg(struct mhi_ep_cntrl *mhi_cntrl) |
| 620 |
{ |
| 621 |
size_t cmd_ctx_host_size, ch_ctx_host_size, ev_ctx_host_size; |
| 622 |
struct device *dev = &mhi_cntrl->mhi_dev->dev; |
| 623 |
int ret; |
| 624 |
|
| 625 |
/* Update the number of event rings (NER) programmed by the host */ |
| 626 |
mhi_ep_mmio_update_ner(mhi_cntrl); |
| 627 |
|
| 628 |
dev_dbg(dev, "Number of Event rings: %u, HW Event rings: %u\n", |
| 629 |
mhi_cntrl->event_rings, mhi_cntrl->hw_event_rings); |
| 630 |
|
| 631 |
ch_ctx_host_size = sizeof(struct mhi_chan_ctxt) * mhi_cntrl->max_chan; |
| 632 |
ev_ctx_host_size = sizeof(struct mhi_event_ctxt) * mhi_cntrl->event_rings; |
| 633 |
cmd_ctx_host_size = sizeof(struct mhi_cmd_ctxt) * NR_OF_CMD_RINGS; |
| 634 |
|
| 635 |
/* Get the channel context base pointer from host */ |
| 636 |
mhi_ep_mmio_get_chc_base(mhi_cntrl); |
| 637 |
|
| 638 |
/* Allocate and map memory for caching host channel context */ |
| 639 |
ret = mhi_cntrl->alloc_map(mhi_cntrl, mhi_cntrl->ch_ctx_host_pa, |
| 640 |
&mhi_cntrl->ch_ctx_cache_phys, |
| 641 |
(void __iomem **) &mhi_cntrl->ch_ctx_cache, |
| 642 |
ch_ctx_host_size); |
| 643 |
if (ret) { |
| 644 |
dev_err(dev, "Failed to allocate and map ch_ctx_cache\n"); |
| 645 |
return ret; |
| 646 |
} |
| 647 |
|
| 648 |
/* Get the event context base pointer from host */ |
| 649 |
mhi_ep_mmio_get_erc_base(mhi_cntrl); |
| 650 |
|
| 651 |
/* Allocate and map memory for caching host event context */ |
| 652 |
ret = mhi_cntrl->alloc_map(mhi_cntrl, mhi_cntrl->ev_ctx_host_pa, |
| 653 |
&mhi_cntrl->ev_ctx_cache_phys, |
| 654 |
(void __iomem **) &mhi_cntrl->ev_ctx_cache, |
| 655 |
ev_ctx_host_size); |
| 656 |
if (ret) { |
| 657 |
dev_err(dev, "Failed to allocate and map ev_ctx_cache\n"); |
| 658 |
goto err_ch_ctx; |
| 659 |
} |
| 660 |
|
| 661 |
/* Get the command context base pointer from host */ |
| 662 |
mhi_ep_mmio_get_crc_base(mhi_cntrl); |
| 663 |
|
| 664 |
/* Allocate and map memory for caching host command context */ |
| 665 |
ret = mhi_cntrl->alloc_map(mhi_cntrl, mhi_cntrl->cmd_ctx_host_pa, |
| 666 |
&mhi_cntrl->cmd_ctx_cache_phys, |
| 667 |
(void __iomem **) &mhi_cntrl->cmd_ctx_cache, |
| 668 |
cmd_ctx_host_size); |
| 669 |
if (ret) { |
| 670 |
dev_err(dev, "Failed to allocate and map cmd_ctx_cache\n"); |
| 671 |
goto err_ev_ctx; |
| 672 |
} |
| 673 |
|
| 674 |
/* Initialize command ring */ |
| 675 |
ret = mhi_ep_ring_start(mhi_cntrl, &mhi_cntrl->mhi_cmd->ring, |
| 676 |
(union mhi_ep_ring_ctx *)mhi_cntrl->cmd_ctx_cache); |
| 677 |
if (ret) { |
| 678 |
dev_err(dev, "Failed to start the command ring\n"); |
| 679 |
goto err_cmd_ctx; |
| 680 |
} |
| 681 |
|
| 682 |
return ret; |
| 683 |
|
| 684 |
err_cmd_ctx: |
| 685 |
mhi_cntrl->unmap_free(mhi_cntrl, mhi_cntrl->cmd_ctx_host_pa, mhi_cntrl->cmd_ctx_cache_phys, |
| 686 |
(void __iomem *) mhi_cntrl->cmd_ctx_cache, cmd_ctx_host_size); |
| 687 |
|
| 688 |
err_ev_ctx: |
| 689 |
mhi_cntrl->unmap_free(mhi_cntrl, mhi_cntrl->ev_ctx_host_pa, mhi_cntrl->ev_ctx_cache_phys, |
| 690 |
(void __iomem *) mhi_cntrl->ev_ctx_cache, ev_ctx_host_size); |
| 691 |
|
| 692 |
err_ch_ctx: |
| 693 |
mhi_cntrl->unmap_free(mhi_cntrl, mhi_cntrl->ch_ctx_host_pa, mhi_cntrl->ch_ctx_cache_phys, |
| 694 |
(void __iomem *) mhi_cntrl->ch_ctx_cache, ch_ctx_host_size); |
| 695 |
|
| 696 |
return ret; |
| 697 |
} |
| 698 |
|
| 699 |
static void mhi_ep_free_host_cfg(struct mhi_ep_cntrl *mhi_cntrl) |
| 700 |
{ |
| 701 |
size_t cmd_ctx_host_size, ch_ctx_host_size, ev_ctx_host_size; |
| 702 |
|
| 703 |
ch_ctx_host_size = sizeof(struct mhi_chan_ctxt) * mhi_cntrl->max_chan; |
| 704 |
ev_ctx_host_size = sizeof(struct mhi_event_ctxt) * mhi_cntrl->event_rings; |
| 705 |
cmd_ctx_host_size = sizeof(struct mhi_cmd_ctxt) * NR_OF_CMD_RINGS; |
| 706 |
|
| 707 |
mhi_cntrl->unmap_free(mhi_cntrl, mhi_cntrl->cmd_ctx_host_pa, mhi_cntrl->cmd_ctx_cache_phys, |
| 708 |
(void __iomem *) mhi_cntrl->cmd_ctx_cache, cmd_ctx_host_size); |
| 709 |
|
| 710 |
mhi_cntrl->unmap_free(mhi_cntrl, mhi_cntrl->ev_ctx_host_pa, mhi_cntrl->ev_ctx_cache_phys, |
| 711 |
(void __iomem *) mhi_cntrl->ev_ctx_cache, ev_ctx_host_size); |
| 712 |
|
| 713 |
mhi_cntrl->unmap_free(mhi_cntrl, mhi_cntrl->ch_ctx_host_pa, mhi_cntrl->ch_ctx_cache_phys, |
| 714 |
(void __iomem *) mhi_cntrl->ch_ctx_cache, ch_ctx_host_size); |
| 715 |
} |
| 716 |
|
| 717 |
static void mhi_ep_enable_int(struct mhi_ep_cntrl *mhi_cntrl) |
| 718 |
{ |
| 719 |
/* |
| 720 |
* Doorbell interrupts are enabled when the corresponding channel gets started. |
| 721 |
* Enabling all interrupts here triggers spurious irqs as some of the interrupts |
| 722 |
* associated with hw channels always get triggered. |
| 723 |
*/ |
| 724 |
mhi_ep_mmio_enable_ctrl_interrupt(mhi_cntrl); |
| 725 |
mhi_ep_mmio_enable_cmdb_interrupt(mhi_cntrl); |
| 726 |
} |
| 727 |
|
| 728 |
static int mhi_ep_enable(struct mhi_ep_cntrl *mhi_cntrl) |
| 729 |
{ |
| 730 |
struct device *dev = &mhi_cntrl->mhi_dev->dev; |
| 731 |
enum mhi_state state; |
| 732 |
bool mhi_reset; |
| 733 |
u32 count = 0; |
| 734 |
int ret; |
| 735 |
|
| 736 |
/* Wait for Host to set the M0 state */ |
| 737 |
do { |
| 738 |
msleep(M0_WAIT_DELAY_MS); |
| 739 |
mhi_ep_mmio_get_mhi_state(mhi_cntrl, &state, &mhi_reset); |
| 740 |
if (mhi_reset) { |
| 741 |
/* Clear the MHI reset if host is in reset state */ |
| 742 |
mhi_ep_mmio_clear_reset(mhi_cntrl); |
| 743 |
dev_info(dev, "Detected Host reset while waiting for M0\n"); |
| 744 |
} |
| 745 |
count++; |
| 746 |
} while (state != MHI_STATE_M0 && count < M0_WAIT_COUNT); |
| 747 |
|
| 748 |
if (state != MHI_STATE_M0) { |
| 749 |
dev_err(dev, "Host failed to enter M0\n"); |
| 750 |
return -ETIMEDOUT; |
| 751 |
} |
| 752 |
|
| 753 |
ret = mhi_ep_cache_host_cfg(mhi_cntrl); |
| 754 |
if (ret) { |
| 755 |
dev_err(dev, "Failed to cache host config\n"); |
| 756 |
return ret; |
| 757 |
} |
| 758 |
|
| 759 |
mhi_ep_mmio_set_env(mhi_cntrl, MHI_EE_AMSS); |
| 760 |
|
| 761 |
/* Enable all interrupts now */ |
| 762 |
mhi_ep_enable_int(mhi_cntrl); |
| 763 |
|
| 764 |
return 0; |
| 765 |
} |
| 766 |
|
| 767 |
static void mhi_ep_cmd_ring_worker(struct work_struct *work) |
| 768 |
{ |
| 769 |
struct mhi_ep_cntrl *mhi_cntrl = container_of(work, struct mhi_ep_cntrl, cmd_ring_work); |
| 770 |
struct mhi_ep_ring *ring = &mhi_cntrl->mhi_cmd->ring; |
| 771 |
struct device *dev = &mhi_cntrl->mhi_dev->dev; |
| 772 |
struct mhi_ring_element *el; |
| 773 |
int ret; |
| 774 |
|
| 775 |
/* Update the write offset for the ring */ |
| 776 |
ret = mhi_ep_update_wr_offset(ring); |
| 777 |
if (ret) { |
| 778 |
dev_err(dev, "Error updating write offset for ring\n"); |
| 779 |
return; |
| 780 |
} |
| 781 |
|
| 782 |
/* Sanity check to make sure there are elements in the ring */ |
| 783 |
if (ring->rd_offset == ring->wr_offset) |
| 784 |
return; |
| 785 |
|
| 786 |
/* |
| 787 |
* Process command ring element till write offset. In case of an error, just try to |
| 788 |
* process next element. |
| 789 |
*/ |
| 790 |
while (ring->rd_offset != ring->wr_offset) { |
| 791 |
el = &ring->ring_cache[ring->rd_offset]; |
| 792 |
|
| 793 |
ret = mhi_ep_process_cmd_ring(ring, el); |
| 794 |
if (ret && ret != -ENODEV) |
| 795 |
dev_err(dev, "Error processing cmd ring element: %zu\n", ring->rd_offset); |
| 796 |
|
| 797 |
mhi_ep_ring_inc_index(ring); |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
static void mhi_ep_ch_ring_worker(struct work_struct *work) |
| 802 |
{ |
| 803 |
struct mhi_ep_cntrl *mhi_cntrl = container_of(work, struct mhi_ep_cntrl, ch_ring_work); |
| 804 |
struct device *dev = &mhi_cntrl->mhi_dev->dev; |
| 805 |
struct mhi_ep_ring_item *itr, *tmp; |
| 806 |
struct mhi_ep_ring *ring; |
| 807 |
struct mhi_ep_chan *chan; |
| 808 |
unsigned long flags; |
| 809 |
LIST_HEAD(head); |
| 810 |
int ret; |
| 811 |
|
| 812 |
spin_lock_irqsave(&mhi_cntrl->list_lock, flags); |
| 813 |
list_splice_tail_init(&mhi_cntrl->ch_db_list, &head); |
| 814 |
spin_unlock_irqrestore(&mhi_cntrl->list_lock, flags); |
| 815 |
|
| 816 |
/* Process each queued channel ring. In case of an error, just process next element. */ |
| 817 |
list_for_each_entry_safe(itr, tmp, &head, node) { |
| 818 |
list_del(&itr->node); |
| 819 |
ring = itr->ring; |
| 820 |
|
| 821 |
chan = &mhi_cntrl->mhi_chan[ring->ch_id]; |
| 822 |
mutex_lock(&chan->lock); |
| 823 |
|
| 824 |
/* |
| 825 |
* The ring could've stopped while we waited to grab the (chan->lock), so do |
| 826 |
* a sanity check before going further. |
| 827 |
*/ |
| 828 |
if (!ring->started) { |
| 829 |
mutex_unlock(&chan->lock); |
| 830 |
kfree(itr); |
| 831 |
continue; |
| 832 |
} |
| 833 |
|
| 834 |
/* Update the write offset for the ring */ |
| 835 |
ret = mhi_ep_update_wr_offset(ring); |
| 836 |
if (ret) { |
| 837 |
dev_err(dev, "Error updating write offset for ring\n"); |
| 838 |
mutex_unlock(&chan->lock); |
| 839 |
kmem_cache_free(mhi_cntrl->ring_item_cache, itr); |
| 840 |
continue; |
| 841 |
} |
| 842 |
|
| 843 |
/* Sanity check to make sure there are elements in the ring */ |
| 844 |
if (chan->rd_offset == ring->wr_offset) { |
| 845 |
mutex_unlock(&chan->lock); |
| 846 |
kmem_cache_free(mhi_cntrl->ring_item_cache, itr); |
| 847 |
continue; |
| 848 |
} |
| 849 |
|
| 850 |
dev_dbg(dev, "Processing the ring for channel (%u)\n", ring->ch_id); |
| 851 |
ret = mhi_ep_process_ch_ring(ring); |
| 852 |
if (ret) { |
| 853 |
dev_err(dev, "Error processing ring for channel (%u): %d\n", |
| 854 |
ring->ch_id, ret); |
| 855 |
mutex_unlock(&chan->lock); |
| 856 |
kmem_cache_free(mhi_cntrl->ring_item_cache, itr); |
| 857 |
continue; |
| 858 |
} |
| 859 |
|
| 860 |
mutex_unlock(&chan->lock); |
| 861 |
kmem_cache_free(mhi_cntrl->ring_item_cache, itr); |
| 862 |
} |
| 863 |
} |
| 864 |
|
| 865 |
static void mhi_ep_state_worker(struct work_struct *work) |
| 866 |
{ |
| 867 |
struct mhi_ep_cntrl *mhi_cntrl = container_of(work, struct mhi_ep_cntrl, state_work); |
| 868 |
struct device *dev = &mhi_cntrl->mhi_dev->dev; |
| 869 |
struct mhi_ep_state_transition *itr, *tmp; |
| 870 |
unsigned long flags; |
| 871 |
LIST_HEAD(head); |
| 872 |
int ret; |
| 873 |
|
| 874 |
spin_lock_irqsave(&mhi_cntrl->list_lock, flags); |
| 875 |
list_splice_tail_init(&mhi_cntrl->st_transition_list, &head); |
| 876 |
spin_unlock_irqrestore(&mhi_cntrl->list_lock, flags); |
| 877 |
|
| 878 |
list_for_each_entry_safe(itr, tmp, &head, node) { |
| 879 |
list_del(&itr->node); |
| 880 |
dev_dbg(dev, "Handling MHI state transition to %s\n", |
| 881 |
mhi_state_str(itr->state)); |
| 882 |
|
| 883 |
switch (itr->state) { |
| 884 |
case MHI_STATE_M0: |
| 885 |
ret = mhi_ep_set_m0_state(mhi_cntrl); |
| 886 |
if (ret) |
| 887 |
dev_err(dev, "Failed to transition to M0 state\n"); |
| 888 |
break; |
| 889 |
case MHI_STATE_M3: |
| 890 |
ret = mhi_ep_set_m3_state(mhi_cntrl); |
| 891 |
if (ret) |
| 892 |
dev_err(dev, "Failed to transition to M3 state\n"); |
| 893 |
break; |
| 894 |
default: |
| 895 |
dev_err(dev, "Invalid MHI state transition: %d\n", itr->state); |
| 896 |
break; |
| 897 |
} |
| 898 |
kfree(itr); |
| 899 |
} |
| 900 |
} |
| 901 |
|
| 902 |
static void mhi_ep_queue_channel_db(struct mhi_ep_cntrl *mhi_cntrl, unsigned long ch_int, |
| 903 |
u32 ch_idx) |
| 904 |
{ |
| 905 |
struct mhi_ep_ring_item *item; |
| 906 |
struct mhi_ep_ring *ring; |
| 907 |
bool work = !!ch_int; |
| 908 |
LIST_HEAD(head); |
| 909 |
u32 i; |
| 910 |
|
| 911 |
/* First add the ring items to a local list */ |
| 912 |
for_each_set_bit(i, &ch_int, 32) { |
| 913 |
/* Channel index varies for each register: 0, 32, 64, 96 */ |
| 914 |
u32 ch_id = ch_idx + i; |
| 915 |
|
| 916 |
ring = &mhi_cntrl->mhi_chan[ch_id].ring; |
| 917 |
item = kmem_cache_zalloc(mhi_cntrl->ring_item_cache, GFP_ATOMIC); |
| 918 |
if (!item) |
| 919 |
return; |
| 920 |
|
| 921 |
item->ring = ring; |
| 922 |
list_add_tail(&item->node, &head); |
| 923 |
} |
| 924 |
|
| 925 |
/* Now, splice the local list into ch_db_list and queue the work item */ |
| 926 |
if (work) { |
| 927 |
spin_lock(&mhi_cntrl->list_lock); |
| 928 |
list_splice_tail_init(&head, &mhi_cntrl->ch_db_list); |
| 929 |
spin_unlock(&mhi_cntrl->list_lock); |
| 930 |
|
| 931 |
queue_work(mhi_cntrl->wq, &mhi_cntrl->ch_ring_work); |
| 932 |
} |
| 933 |
} |
| 934 |
|
| 935 |
/* |
| 936 |
* Channel interrupt statuses are contained in 4 registers each of 32bit length. |
| 937 |
* For checking all interrupts, we need to loop through each registers and then |
| 938 |
* check for bits set. |
| 939 |
*/ |
| 940 |
static void mhi_ep_check_channel_interrupt(struct mhi_ep_cntrl *mhi_cntrl) |
| 941 |
{ |
| 942 |
u32 ch_int, ch_idx, i; |
| 943 |
|
| 944 |
/* Bail out if there is no channel doorbell interrupt */ |
| 945 |
if (!mhi_ep_mmio_read_chdb_status_interrupts(mhi_cntrl)) |
| 946 |
return; |
| 947 |
|
| 948 |
for (i = 0; i < MHI_MASK_ROWS_CH_DB; i++) { |
| 949 |
ch_idx = i * MHI_MASK_CH_LEN; |
| 950 |
|
| 951 |
/* Only process channel interrupt if the mask is enabled */ |
| 952 |
ch_int = mhi_cntrl->chdb[i].status & mhi_cntrl->chdb[i].mask; |
| 953 |
if (ch_int) { |
| 954 |
mhi_ep_queue_channel_db(mhi_cntrl, ch_int, ch_idx); |
| 955 |
mhi_ep_mmio_write(mhi_cntrl, MHI_CHDB_INT_CLEAR_n(i), |
| 956 |
mhi_cntrl->chdb[i].status); |
| 957 |
} |
| 958 |
} |
| 959 |
} |
| 960 |
|
| 961 |
static void mhi_ep_process_ctrl_interrupt(struct mhi_ep_cntrl *mhi_cntrl, |
| 962 |
enum mhi_state state) |
| 963 |
{ |
| 964 |
struct mhi_ep_state_transition *item; |
| 965 |
|
| 966 |
item = kzalloc_obj(*item, GFP_ATOMIC); |
| 967 |
if (!item) |
| 968 |
return; |
| 969 |
|
| 970 |
item->state = state; |
| 971 |
spin_lock(&mhi_cntrl->list_lock); |
| 972 |
list_add_tail(&item->node, &mhi_cntrl->st_transition_list); |
| 973 |
spin_unlock(&mhi_cntrl->list_lock); |
| 974 |
|
| 975 |
queue_work(mhi_cntrl->wq, &mhi_cntrl->state_work); |
| 976 |
} |
| 977 |
|
| 978 |
/* |
| 979 |
* Interrupt handler that services interrupts raised by the host writing to |
| 980 |
* MHICTRL and Command ring doorbell (CRDB) registers for state change and |
| 981 |
* channel interrupts. |
| 982 |
*/ |
| 983 |
static irqreturn_t mhi_ep_irq(int irq, void *data) |
| 984 |
{ |
| 985 |
struct mhi_ep_cntrl *mhi_cntrl = data; |
| 986 |
struct device *dev = &mhi_cntrl->mhi_dev->dev; |
| 987 |
enum mhi_state state; |
| 988 |
u32 int_value; |
| 989 |
bool mhi_reset; |
| 990 |
|
| 991 |
/* Acknowledge the ctrl interrupt */ |
| 992 |
int_value = mhi_ep_mmio_read(mhi_cntrl, MHI_CTRL_INT_STATUS); |
| 993 |
mhi_ep_mmio_write(mhi_cntrl, MHI_CTRL_INT_CLEAR, int_value); |
| 994 |
|
| 995 |
/* Check for ctrl interrupt */ |
| 996 |
if (FIELD_GET(MHI_CTRL_INT_STATUS_MSK, int_value)) { |
| 997 |
dev_dbg(dev, "Processing ctrl interrupt\n"); |
| 998 |
mhi_ep_mmio_get_mhi_state(mhi_cntrl, &state, &mhi_reset); |
| 999 |
if (mhi_reset) { |
| 1000 |
dev_info(dev, "Host triggered MHI reset!\n"); |
| 1001 |
disable_irq_nosync(mhi_cntrl->irq); |
| 1002 |
schedule_work(&mhi_cntrl->reset_work); |
| 1003 |
return IRQ_HANDLED; |
| 1004 |
} |
| 1005 |
|
| 1006 |
mhi_ep_process_ctrl_interrupt(mhi_cntrl, state); |
| 1007 |
} |
| 1008 |
|
| 1009 |
/* Check for command doorbell interrupt */ |
| 1010 |
if (FIELD_GET(MHI_CTRL_INT_STATUS_CRDB_MSK, int_value)) { |
| 1011 |
dev_dbg(dev, "Processing command doorbell interrupt\n"); |
| 1012 |
queue_work(mhi_cntrl->wq, &mhi_cntrl->cmd_ring_work); |
| 1013 |
} |
| 1014 |
|
| 1015 |
/* Check for channel interrupts */ |
| 1016 |
mhi_ep_check_channel_interrupt(mhi_cntrl); |
| 1017 |
|
| 1018 |
return IRQ_HANDLED; |
| 1019 |
} |
| 1020 |
|
| 1021 |
static void mhi_ep_abort_transfer(struct mhi_ep_cntrl *mhi_cntrl) |
| 1022 |
{ |
| 1023 |
struct mhi_ep_ring *ch_ring, *ev_ring; |
| 1024 |
struct mhi_result result = {}; |
| 1025 |
struct mhi_ep_chan *mhi_chan; |
| 1026 |
int i; |
| 1027 |
|
| 1028 |
/* Stop all the channels */ |
| 1029 |
for (i = 0; i < mhi_cntrl->max_chan; i++) { |
| 1030 |
mhi_chan = &mhi_cntrl->mhi_chan[i]; |
| 1031 |
if (!mhi_chan->ring.started) |
| 1032 |
continue; |
| 1033 |
|
| 1034 |
mutex_lock(&mhi_chan->lock); |
| 1035 |
/* Send channel disconnect status to client drivers */ |
| 1036 |
if (mhi_chan->xfer_cb) { |
| 1037 |
result.transaction_status = -ENOTCONN; |
| 1038 |
result.bytes_xferd = 0; |
| 1039 |
mhi_chan->xfer_cb(mhi_chan->mhi_dev, &result); |
| 1040 |
} |
| 1041 |
|
| 1042 |
mhi_chan->state = MHI_CH_STATE_DISABLED; |
| 1043 |
mutex_unlock(&mhi_chan->lock); |
| 1044 |
} |
| 1045 |
|
| 1046 |
flush_workqueue(mhi_cntrl->wq); |
| 1047 |
|
| 1048 |
/* Destroy devices associated with all channels */ |
| 1049 |
device_for_each_child(&mhi_cntrl->mhi_dev->dev, NULL, mhi_ep_destroy_device); |
| 1050 |
|
| 1051 |
/* Stop and reset the transfer rings */ |
| 1052 |
for (i = 0; i < mhi_cntrl->max_chan; i++) { |
| 1053 |
mhi_chan = &mhi_cntrl->mhi_chan[i]; |
| 1054 |
if (!mhi_chan->ring.started) |
| 1055 |
continue; |
| 1056 |
|
| 1057 |
ch_ring = &mhi_cntrl->mhi_chan[i].ring; |
| 1058 |
mutex_lock(&mhi_chan->lock); |
| 1059 |
mhi_ep_ring_reset(mhi_cntrl, ch_ring); |
| 1060 |
mutex_unlock(&mhi_chan->lock); |
| 1061 |
} |
| 1062 |
|
| 1063 |
/* Stop and reset the event rings */ |
| 1064 |
for (i = 0; i < mhi_cntrl->event_rings; i++) { |
| 1065 |
ev_ring = &mhi_cntrl->mhi_event[i].ring; |
| 1066 |
if (!ev_ring->started) |
| 1067 |
continue; |
| 1068 |
|
| 1069 |
mutex_lock(&mhi_cntrl->event_lock); |
| 1070 |
mhi_ep_ring_reset(mhi_cntrl, ev_ring); |
| 1071 |
mutex_unlock(&mhi_cntrl->event_lock); |
| 1072 |
} |
| 1073 |
|
| 1074 |
/* Stop and reset the command ring */ |
| 1075 |
mhi_ep_ring_reset(mhi_cntrl, &mhi_cntrl->mhi_cmd->ring); |
| 1076 |
|
| 1077 |
mhi_ep_free_host_cfg(mhi_cntrl); |
| 1078 |
mhi_ep_mmio_mask_interrupts(mhi_cntrl); |
| 1079 |
|
| 1080 |
mhi_cntrl->enabled = false; |
| 1081 |
} |
| 1082 |
|
| 1083 |
static void mhi_ep_reset_worker(struct work_struct *work) |
| 1084 |
{ |
| 1085 |
struct mhi_ep_cntrl *mhi_cntrl = container_of(work, struct mhi_ep_cntrl, reset_work); |
| 1086 |
enum mhi_state cur_state; |
| 1087 |
|
| 1088 |
mhi_ep_power_down(mhi_cntrl); |
| 1089 |
|
| 1090 |
mutex_lock(&mhi_cntrl->state_lock); |
| 1091 |
|
| 1092 |
/* Reset MMIO to signal host that the MHI_RESET is completed in endpoint */ |
| 1093 |
mhi_ep_mmio_reset(mhi_cntrl); |
| 1094 |
cur_state = mhi_cntrl->mhi_state; |
| 1095 |
|
| 1096 |
/* |
| 1097 |
* Only proceed further if the reset is due to SYS_ERR. The host will |
| 1098 |
* issue reset during shutdown also and we don't need to do re-init in |
| 1099 |
* that case. |
| 1100 |
*/ |
| 1101 |
if (cur_state == MHI_STATE_SYS_ERR) |
| 1102 |
mhi_ep_power_up(mhi_cntrl); |
| 1103 |
|
| 1104 |
mutex_unlock(&mhi_cntrl->state_lock); |
| 1105 |
} |
| 1106 |
|
| 1107 |
/* |
| 1108 |
* We don't need to do anything special other than setting the MHI SYS_ERR |
| 1109 |
* state. The host will reset all contexts and issue MHI RESET so that we |
| 1110 |
* could also recover from error state. |
| 1111 |
*/ |
| 1112 |
void mhi_ep_handle_syserr(struct mhi_ep_cntrl *mhi_cntrl) |
| 1113 |
{ |
| 1114 |
struct device *dev = &mhi_cntrl->mhi_dev->dev; |
| 1115 |
int ret; |
| 1116 |
|
| 1117 |
ret = mhi_ep_set_mhi_state(mhi_cntrl, MHI_STATE_SYS_ERR); |
| 1118 |
if (ret) |
| 1119 |
return; |
| 1120 |
|
| 1121 |
/* Signal host that the device went to SYS_ERR state */ |
| 1122 |
ret = mhi_ep_send_state_change_event(mhi_cntrl, MHI_STATE_SYS_ERR); |
| 1123 |
if (ret) |
| 1124 |
dev_err(dev, "Failed sending SYS_ERR state change event: %d\n", ret); |
| 1125 |
} |
| 1126 |
|
| 1127 |
int mhi_ep_power_up(struct mhi_ep_cntrl *mhi_cntrl) |
| 1128 |
{ |
| 1129 |
struct device *dev = &mhi_cntrl->mhi_dev->dev; |
| 1130 |
int ret, i; |
| 1131 |
|
| 1132 |
/* |
| 1133 |
* Mask all interrupts until the state machine is ready. Interrupts will |
| 1134 |
* be enabled later with mhi_ep_enable(). |
| 1135 |
*/ |
| 1136 |
mhi_ep_mmio_mask_interrupts(mhi_cntrl); |
| 1137 |
mhi_ep_mmio_init(mhi_cntrl); |
| 1138 |
|
| 1139 |
mhi_cntrl->mhi_event = kzalloc_objs(*mhi_cntrl->mhi_event, |
| 1140 |
mhi_cntrl->event_rings); |
| 1141 |
if (!mhi_cntrl->mhi_event) |
| 1142 |
return -ENOMEM; |
| 1143 |
|
| 1144 |
/* Initialize command, channel and event rings */ |
| 1145 |
mhi_ep_ring_init(&mhi_cntrl->mhi_cmd->ring, RING_TYPE_CMD, 0); |
| 1146 |
for (i = 0; i < mhi_cntrl->max_chan; i++) |
| 1147 |
mhi_ep_ring_init(&mhi_cntrl->mhi_chan[i].ring, RING_TYPE_CH, i); |
| 1148 |
for (i = 0; i < mhi_cntrl->event_rings; i++) |
| 1149 |
mhi_ep_ring_init(&mhi_cntrl->mhi_event[i].ring, RING_TYPE_ER, i); |
| 1150 |
|
| 1151 |
mhi_cntrl->mhi_state = MHI_STATE_RESET; |
| 1152 |
|
| 1153 |
/* Set AMSS EE before signaling ready state */ |
| 1154 |
mhi_ep_mmio_set_env(mhi_cntrl, MHI_EE_AMSS); |
| 1155 |
|
| 1156 |
/* All set, notify the host that we are ready */ |
| 1157 |
ret = mhi_ep_set_ready_state(mhi_cntrl); |
| 1158 |
if (ret) |
| 1159 |
goto err_free_event; |
| 1160 |
|
| 1161 |
dev_dbg(dev, "READY state notification sent to the host\n"); |
| 1162 |
|
| 1163 |
ret = mhi_ep_enable(mhi_cntrl); |
| 1164 |
if (ret) { |
| 1165 |
dev_err(dev, "Failed to enable MHI endpoint\n"); |
| 1166 |
goto err_free_event; |
| 1167 |
} |
| 1168 |
|
| 1169 |
enable_irq(mhi_cntrl->irq); |
| 1170 |
mhi_cntrl->enabled = true; |
| 1171 |
|
| 1172 |
return 0; |
| 1173 |
|
| 1174 |
err_free_event: |
| 1175 |
kfree(mhi_cntrl->mhi_event); |
| 1176 |
|
| 1177 |
return ret; |
| 1178 |
} |
| 1179 |
EXPORT_SYMBOL_GPL(mhi_ep_power_up); |
| 1180 |
|
| 1181 |
void mhi_ep_power_down(struct mhi_ep_cntrl *mhi_cntrl) |
| 1182 |
{ |
| 1183 |
if (mhi_cntrl->enabled) { |
| 1184 |
mhi_ep_abort_transfer(mhi_cntrl); |
| 1185 |
kfree(mhi_cntrl->mhi_event); |
| 1186 |
disable_irq(mhi_cntrl->irq); |
| 1187 |
} |
| 1188 |
} |
| 1189 |
EXPORT_SYMBOL_GPL(mhi_ep_power_down); |
| 1190 |
|
| 1191 |
void mhi_ep_suspend_channels(struct mhi_ep_cntrl *mhi_cntrl) |
| 1192 |
{ |
| 1193 |
struct mhi_ep_chan *mhi_chan; |
| 1194 |
u32 tmp; |
| 1195 |
int i; |
| 1196 |
|
| 1197 |
for (i = 0; i < mhi_cntrl->max_chan; i++) { |
| 1198 |
mhi_chan = &mhi_cntrl->mhi_chan[i]; |
| 1199 |
|
| 1200 |
if (!mhi_chan->mhi_dev) |
| 1201 |
continue; |
| 1202 |
|
| 1203 |
mutex_lock(&mhi_chan->lock); |
| 1204 |
/* Skip if the channel is not currently running */ |
| 1205 |
tmp = le32_to_cpu(mhi_cntrl->ch_ctx_cache[i].chcfg); |
| 1206 |
if (FIELD_GET(CHAN_CTX_CHSTATE_MASK, tmp) != MHI_CH_STATE_RUNNING) { |
| 1207 |
mutex_unlock(&mhi_chan->lock); |
| 1208 |
continue; |
| 1209 |
} |
| 1210 |
|
| 1211 |
dev_dbg(&mhi_chan->mhi_dev->dev, "Suspending channel\n"); |
| 1212 |
/* Set channel state to SUSPENDED */ |
| 1213 |
mhi_chan->state = MHI_CH_STATE_SUSPENDED; |
| 1214 |
tmp &= ~CHAN_CTX_CHSTATE_MASK; |
| 1215 |
tmp |= FIELD_PREP(CHAN_CTX_CHSTATE_MASK, MHI_CH_STATE_SUSPENDED); |
| 1216 |
mhi_cntrl->ch_ctx_cache[i].chcfg = cpu_to_le32(tmp); |
| 1217 |
mutex_unlock(&mhi_chan->lock); |
| 1218 |
} |
| 1219 |
} |
| 1220 |
|
| 1221 |
void mhi_ep_resume_channels(struct mhi_ep_cntrl *mhi_cntrl) |
| 1222 |
{ |
| 1223 |
struct mhi_ep_chan *mhi_chan; |
| 1224 |
u32 tmp; |
| 1225 |
int i; |
| 1226 |
|
| 1227 |
for (i = 0; i < mhi_cntrl->max_chan; i++) { |
| 1228 |
mhi_chan = &mhi_cntrl->mhi_chan[i]; |
| 1229 |
|
| 1230 |
if (!mhi_chan->mhi_dev) |
| 1231 |
continue; |
| 1232 |
|
| 1233 |
mutex_lock(&mhi_chan->lock); |
| 1234 |
/* Skip if the channel is not currently suspended */ |
| 1235 |
tmp = le32_to_cpu(mhi_cntrl->ch_ctx_cache[i].chcfg); |
| 1236 |
if (FIELD_GET(CHAN_CTX_CHSTATE_MASK, tmp) != MHI_CH_STATE_SUSPENDED) { |
| 1237 |
mutex_unlock(&mhi_chan->lock); |
| 1238 |
continue; |
| 1239 |
} |
| 1240 |
|
| 1241 |
dev_dbg(&mhi_chan->mhi_dev->dev, "Resuming channel\n"); |
| 1242 |
/* Set channel state to RUNNING */ |
| 1243 |
mhi_chan->state = MHI_CH_STATE_RUNNING; |
| 1244 |
tmp &= ~CHAN_CTX_CHSTATE_MASK; |
| 1245 |
tmp |= FIELD_PREP(CHAN_CTX_CHSTATE_MASK, MHI_CH_STATE_RUNNING); |
| 1246 |
mhi_cntrl->ch_ctx_cache[i].chcfg = cpu_to_le32(tmp); |
| 1247 |
mutex_unlock(&mhi_chan->lock); |
| 1248 |
} |
| 1249 |
} |
| 1250 |
|
| 1251 |
static void mhi_ep_release_device(struct device *dev) |
| 1252 |
{ |
| 1253 |
struct mhi_ep_device *mhi_dev = to_mhi_ep_device(dev); |
| 1254 |
|
| 1255 |
if (mhi_dev->dev_type == MHI_DEVICE_CONTROLLER) |
| 1256 |
mhi_dev->mhi_cntrl->mhi_dev = NULL; |
| 1257 |
|
| 1258 |
/* |
| 1259 |
* We need to set the mhi_chan->mhi_dev to NULL here since the MHI |
| 1260 |
* devices for the channels will only get created in mhi_ep_create_device() |
| 1261 |
* if the mhi_dev associated with it is NULL. |
| 1262 |
*/ |
| 1263 |
if (mhi_dev->ul_chan) |
| 1264 |
mhi_dev->ul_chan->mhi_dev = NULL; |
| 1265 |
|
| 1266 |
if (mhi_dev->dl_chan) |
| 1267 |
mhi_dev->dl_chan->mhi_dev = NULL; |
| 1268 |
|
| 1269 |
kfree(mhi_dev); |
| 1270 |
} |
| 1271 |
|
| 1272 |
static struct mhi_ep_device *mhi_ep_alloc_device(struct mhi_ep_cntrl *mhi_cntrl, |
| 1273 |
enum mhi_device_type dev_type) |
| 1274 |
{ |
| 1275 |
struct mhi_ep_device *mhi_dev; |
| 1276 |
struct device *dev; |
| 1277 |
|
| 1278 |
mhi_dev = kzalloc_obj(*mhi_dev); |
| 1279 |
if (!mhi_dev) |
| 1280 |
return ERR_PTR(-ENOMEM); |
| 1281 |
|
| 1282 |
dev = &mhi_dev->dev; |
| 1283 |
device_initialize(dev); |
| 1284 |
dev->bus = &mhi_ep_bus_type; |
| 1285 |
dev->release = mhi_ep_release_device; |
| 1286 |
|
| 1287 |
/* Controller device is always allocated first */ |
| 1288 |
if (dev_type == MHI_DEVICE_CONTROLLER) |
| 1289 |
/* for MHI controller device, parent is the bus device (e.g. PCI EPF) */ |
| 1290 |
dev->parent = mhi_cntrl->cntrl_dev; |
| 1291 |
else |
| 1292 |
/* for MHI client devices, parent is the MHI controller device */ |
| 1293 |
dev->parent = &mhi_cntrl->mhi_dev->dev; |
| 1294 |
|
| 1295 |
mhi_dev->mhi_cntrl = mhi_cntrl; |
| 1296 |
mhi_dev->dev_type = dev_type; |
| 1297 |
|
| 1298 |
return mhi_dev; |
| 1299 |
} |
| 1300 |
|
| 1301 |
/* |
| 1302 |
* MHI channels are always defined in pairs with UL as the even numbered |
| 1303 |
* channel and DL as odd numbered one. This function gets UL channel (primary) |
| 1304 |
* as the ch_id and always looks after the next entry in channel list for |
| 1305 |
* the corresponding DL channel (secondary). |
| 1306 |
*/ |
| 1307 |
static int mhi_ep_create_device(struct mhi_ep_cntrl *mhi_cntrl, u32 ch_id) |
| 1308 |
{ |
| 1309 |
struct mhi_ep_chan *mhi_chan = &mhi_cntrl->mhi_chan[ch_id]; |
| 1310 |
struct device *dev = mhi_cntrl->cntrl_dev; |
| 1311 |
struct mhi_ep_device *mhi_dev; |
| 1312 |
int ret; |
| 1313 |
|
| 1314 |
/* Check if the channel name is same for both UL and DL */ |
| 1315 |
if (strcmp(mhi_chan->name, mhi_chan[1].name)) { |
| 1316 |
dev_err(dev, "UL and DL channel names are not same: (%s) != (%s)\n", |
| 1317 |
mhi_chan->name, mhi_chan[1].name); |
| 1318 |
return -EINVAL; |
| 1319 |
} |
| 1320 |
|
| 1321 |
mhi_dev = mhi_ep_alloc_device(mhi_cntrl, MHI_DEVICE_XFER); |
| 1322 |
if (IS_ERR(mhi_dev)) |
| 1323 |
return PTR_ERR(mhi_dev); |
| 1324 |
|
| 1325 |
/* Configure primary channel */ |
| 1326 |
mhi_dev->ul_chan = mhi_chan; |
| 1327 |
get_device(&mhi_dev->dev); |
| 1328 |
mhi_chan->mhi_dev = mhi_dev; |
| 1329 |
|
| 1330 |
/* Configure secondary channel as well */ |
| 1331 |
mhi_chan++; |
| 1332 |
mhi_dev->dl_chan = mhi_chan; |
| 1333 |
get_device(&mhi_dev->dev); |
| 1334 |
mhi_chan->mhi_dev = mhi_dev; |
| 1335 |
|
| 1336 |
/* Channel name is same for both UL and DL */ |
| 1337 |
mhi_dev->name = mhi_chan->name; |
| 1338 |
ret = dev_set_name(&mhi_dev->dev, "%s_%s", |
| 1339 |
dev_name(&mhi_cntrl->mhi_dev->dev), |
| 1340 |
mhi_dev->name); |
| 1341 |
if (ret) { |
| 1342 |
put_device(&mhi_dev->dev); |
| 1343 |
return ret; |
| 1344 |
} |
| 1345 |
|
| 1346 |
ret = device_add(&mhi_dev->dev); |
| 1347 |
if (ret) |
| 1348 |
put_device(&mhi_dev->dev); |
| 1349 |
|
| 1350 |
return ret; |
| 1351 |
} |
| 1352 |
|
| 1353 |
static int mhi_ep_destroy_device(struct device *dev, void *data) |
| 1354 |
{ |
| 1355 |
struct mhi_ep_device *mhi_dev; |
| 1356 |
struct mhi_ep_cntrl *mhi_cntrl; |
| 1357 |
struct mhi_ep_chan *ul_chan, *dl_chan; |
| 1358 |
|
| 1359 |
if (dev->bus != &mhi_ep_bus_type) |
| 1360 |
return 0; |
| 1361 |
|
| 1362 |
mhi_dev = to_mhi_ep_device(dev); |
| 1363 |
mhi_cntrl = mhi_dev->mhi_cntrl; |
| 1364 |
|
| 1365 |
/* Only destroy devices created for channels */ |
| 1366 |
if (mhi_dev->dev_type == MHI_DEVICE_CONTROLLER) |
| 1367 |
return 0; |
| 1368 |
|
| 1369 |
ul_chan = mhi_dev->ul_chan; |
| 1370 |
dl_chan = mhi_dev->dl_chan; |
| 1371 |
|
| 1372 |
if (ul_chan) |
| 1373 |
put_device(&ul_chan->mhi_dev->dev); |
| 1374 |
|
| 1375 |
if (dl_chan) |
| 1376 |
put_device(&dl_chan->mhi_dev->dev); |
| 1377 |
|
| 1378 |
dev_dbg(&mhi_cntrl->mhi_dev->dev, "Destroying device for chan:%s\n", |
| 1379 |
mhi_dev->name); |
| 1380 |
|
| 1381 |
/* Notify the client and remove the device from MHI bus */ |
| 1382 |
device_del(dev); |
| 1383 |
put_device(dev); |
| 1384 |
|
| 1385 |
return 0; |
| 1386 |
} |
| 1387 |
|
| 1388 |
static int mhi_ep_chan_init(struct mhi_ep_cntrl *mhi_cntrl, |
| 1389 |
const struct mhi_ep_cntrl_config *config) |
| 1390 |
{ |
| 1391 |
const struct mhi_ep_channel_config *ch_cfg; |
| 1392 |
struct device *dev = mhi_cntrl->cntrl_dev; |
| 1393 |
u32 chan, i; |
| 1394 |
int ret = -EINVAL; |
| 1395 |
|
| 1396 |
mhi_cntrl->max_chan = config->max_channels; |
| 1397 |
|
| 1398 |
/* |
| 1399 |
* Allocate max_channels supported by the MHI endpoint and populate |
| 1400 |
* only the defined channels |
| 1401 |
*/ |
| 1402 |
mhi_cntrl->mhi_chan = kzalloc_objs(*mhi_cntrl->mhi_chan, |
| 1403 |
mhi_cntrl->max_chan); |
| 1404 |
if (!mhi_cntrl->mhi_chan) |
| 1405 |
return -ENOMEM; |
| 1406 |
|
| 1407 |
for (i = 0; i < config->num_channels; i++) { |
| 1408 |
struct mhi_ep_chan *mhi_chan; |
| 1409 |
|
| 1410 |
ch_cfg = &config->ch_cfg[i]; |
| 1411 |
|
| 1412 |
chan = ch_cfg->num; |
| 1413 |
if (chan >= mhi_cntrl->max_chan) { |
| 1414 |
dev_err(dev, "Channel (%u) exceeds maximum available channels (%u)\n", |
| 1415 |
chan, mhi_cntrl->max_chan); |
| 1416 |
goto error_chan_cfg; |
| 1417 |
} |
| 1418 |
|
| 1419 |
/* Bi-directional and direction less channels are not supported */ |
| 1420 |
if (ch_cfg->dir == DMA_BIDIRECTIONAL || ch_cfg->dir == DMA_NONE) { |
| 1421 |
dev_err(dev, "Invalid direction (%u) for channel (%u)\n", |
| 1422 |
ch_cfg->dir, chan); |
| 1423 |
goto error_chan_cfg; |
| 1424 |
} |
| 1425 |
|
| 1426 |
mhi_chan = &mhi_cntrl->mhi_chan[chan]; |
| 1427 |
mhi_chan->name = ch_cfg->name; |
| 1428 |
mhi_chan->chan = chan; |
| 1429 |
mhi_chan->dir = ch_cfg->dir; |
| 1430 |
mutex_init(&mhi_chan->lock); |
| 1431 |
} |
| 1432 |
|
| 1433 |
return 0; |
| 1434 |
|
| 1435 |
error_chan_cfg: |
| 1436 |
kfree(mhi_cntrl->mhi_chan); |
| 1437 |
|
| 1438 |
return ret; |
| 1439 |
} |
| 1440 |
|
| 1441 |
/* |
| 1442 |
* Allocate channel and command rings here. Event rings will be allocated |
| 1443 |
* in mhi_ep_power_up() as the config comes from the host. |
| 1444 |
*/ |
| 1445 |
int mhi_ep_register_controller(struct mhi_ep_cntrl *mhi_cntrl, |
| 1446 |
const struct mhi_ep_cntrl_config *config) |
| 1447 |
{ |
| 1448 |
struct mhi_ep_device *mhi_dev; |
| 1449 |
int ret; |
| 1450 |
|
| 1451 |
if (!mhi_cntrl || !mhi_cntrl->cntrl_dev || !mhi_cntrl->mmio || !mhi_cntrl->irq) |
| 1452 |
return -EINVAL; |
| 1453 |
|
| 1454 |
if (!mhi_cntrl->read_sync || !mhi_cntrl->write_sync || |
| 1455 |
!mhi_cntrl->read_async || !mhi_cntrl->write_async) |
| 1456 |
return -EINVAL; |
| 1457 |
|
| 1458 |
ret = mhi_ep_chan_init(mhi_cntrl, config); |
| 1459 |
if (ret) |
| 1460 |
return ret; |
| 1461 |
|
| 1462 |
mhi_cntrl->mhi_cmd = kzalloc_objs(*mhi_cntrl->mhi_cmd, NR_OF_CMD_RINGS); |
| 1463 |
if (!mhi_cntrl->mhi_cmd) { |
| 1464 |
ret = -ENOMEM; |
| 1465 |
goto err_free_ch; |
| 1466 |
} |
| 1467 |
|
| 1468 |
mhi_cntrl->ev_ring_el_cache = kmem_cache_create("mhi_ep_event_ring_el", |
| 1469 |
sizeof(struct mhi_ring_element), 0, |
| 1470 |
0, NULL); |
| 1471 |
if (!mhi_cntrl->ev_ring_el_cache) { |
| 1472 |
ret = -ENOMEM; |
| 1473 |
goto err_free_cmd; |
| 1474 |
} |
| 1475 |
|
| 1476 |
mhi_cntrl->tre_buf_cache = kmem_cache_create("mhi_ep_tre_buf", MHI_EP_DEFAULT_MTU, 0, |
| 1477 |
0, NULL); |
| 1478 |
if (!mhi_cntrl->tre_buf_cache) { |
| 1479 |
ret = -ENOMEM; |
| 1480 |
goto err_destroy_ev_ring_el_cache; |
| 1481 |
} |
| 1482 |
|
| 1483 |
mhi_cntrl->ring_item_cache = kmem_cache_create("mhi_ep_ring_item", |
| 1484 |
sizeof(struct mhi_ep_ring_item), 0, |
| 1485 |
0, NULL); |
| 1486 |
if (!mhi_cntrl->ring_item_cache) { |
| 1487 |
ret = -ENOMEM; |
| 1488 |
goto err_destroy_tre_buf_cache; |
| 1489 |
} |
| 1490 |
|
| 1491 |
INIT_WORK(&mhi_cntrl->state_work, mhi_ep_state_worker); |
| 1492 |
INIT_WORK(&mhi_cntrl->reset_work, mhi_ep_reset_worker); |
| 1493 |
INIT_WORK(&mhi_cntrl->cmd_ring_work, mhi_ep_cmd_ring_worker); |
| 1494 |
INIT_WORK(&mhi_cntrl->ch_ring_work, mhi_ep_ch_ring_worker); |
| 1495 |
|
| 1496 |
mhi_cntrl->wq = alloc_workqueue("mhi_ep_wq", WQ_PERCPU, 0); |
| 1497 |
if (!mhi_cntrl->wq) { |
| 1498 |
ret = -ENOMEM; |
| 1499 |
goto err_destroy_ring_item_cache; |
| 1500 |
} |
| 1501 |
|
| 1502 |
INIT_LIST_HEAD(&mhi_cntrl->st_transition_list); |
| 1503 |
INIT_LIST_HEAD(&mhi_cntrl->ch_db_list); |
| 1504 |
spin_lock_init(&mhi_cntrl->list_lock); |
| 1505 |
mutex_init(&mhi_cntrl->state_lock); |
| 1506 |
mutex_init(&mhi_cntrl->event_lock); |
| 1507 |
|
| 1508 |
/* Set MHI version and AMSS EE before enumeration */ |
| 1509 |
mhi_ep_mmio_write(mhi_cntrl, EP_MHIVER, config->mhi_version); |
| 1510 |
mhi_ep_mmio_set_env(mhi_cntrl, MHI_EE_AMSS); |
| 1511 |
|
| 1512 |
/* Set controller index */ |
| 1513 |
ret = ida_alloc(&mhi_ep_cntrl_ida, GFP_KERNEL); |
| 1514 |
if (ret < 0) |
| 1515 |
goto err_destroy_wq; |
| 1516 |
|
| 1517 |
mhi_cntrl->index = ret; |
| 1518 |
|
| 1519 |
irq_set_status_flags(mhi_cntrl->irq, IRQ_NOAUTOEN); |
| 1520 |
ret = request_irq(mhi_cntrl->irq, mhi_ep_irq, IRQF_TRIGGER_HIGH, |
| 1521 |
"doorbell_irq", mhi_cntrl); |
| 1522 |
if (ret) { |
| 1523 |
dev_err(mhi_cntrl->cntrl_dev, "Failed to request Doorbell IRQ\n"); |
| 1524 |
goto err_ida_free; |
| 1525 |
} |
| 1526 |
|
| 1527 |
/* Allocate the controller device */ |
| 1528 |
mhi_dev = mhi_ep_alloc_device(mhi_cntrl, MHI_DEVICE_CONTROLLER); |
| 1529 |
if (IS_ERR(mhi_dev)) { |
| 1530 |
dev_err(mhi_cntrl->cntrl_dev, "Failed to allocate controller device\n"); |
| 1531 |
ret = PTR_ERR(mhi_dev); |
| 1532 |
goto err_free_irq; |
| 1533 |
} |
| 1534 |
|
| 1535 |
ret = dev_set_name(&mhi_dev->dev, "mhi_ep%u", mhi_cntrl->index); |
| 1536 |
if (ret) |
| 1537 |
goto err_put_dev; |
| 1538 |
|
| 1539 |
mhi_dev->name = dev_name(&mhi_dev->dev); |
| 1540 |
mhi_cntrl->mhi_dev = mhi_dev; |
| 1541 |
|
| 1542 |
ret = device_add(&mhi_dev->dev); |
| 1543 |
if (ret) |
| 1544 |
goto err_put_dev; |
| 1545 |
|
| 1546 |
dev_dbg(&mhi_dev->dev, "MHI EP Controller registered\n"); |
| 1547 |
|
| 1548 |
return 0; |
| 1549 |
|
| 1550 |
err_put_dev: |
| 1551 |
put_device(&mhi_dev->dev); |
| 1552 |
err_free_irq: |
| 1553 |
free_irq(mhi_cntrl->irq, mhi_cntrl); |
| 1554 |
err_ida_free: |
| 1555 |
ida_free(&mhi_ep_cntrl_ida, mhi_cntrl->index); |
| 1556 |
err_destroy_wq: |
| 1557 |
destroy_workqueue(mhi_cntrl->wq); |
| 1558 |
err_destroy_ring_item_cache: |
| 1559 |
kmem_cache_destroy(mhi_cntrl->ring_item_cache); |
| 1560 |
err_destroy_ev_ring_el_cache: |
| 1561 |
kmem_cache_destroy(mhi_cntrl->ev_ring_el_cache); |
| 1562 |
err_destroy_tre_buf_cache: |
| 1563 |
kmem_cache_destroy(mhi_cntrl->tre_buf_cache); |
| 1564 |
err_free_cmd: |
| 1565 |
kfree(mhi_cntrl->mhi_cmd); |
| 1566 |
err_free_ch: |
| 1567 |
kfree(mhi_cntrl->mhi_chan); |
| 1568 |
|
| 1569 |
return ret; |
| 1570 |
} |
| 1571 |
EXPORT_SYMBOL_GPL(mhi_ep_register_controller); |
| 1572 |
|
| 1573 |
/* |
| 1574 |
* It is expected that the controller drivers will power down the MHI EP stack |
| 1575 |
* using "mhi_ep_power_down()" before calling this function to unregister themselves. |
| 1576 |
*/ |
| 1577 |
void mhi_ep_unregister_controller(struct mhi_ep_cntrl *mhi_cntrl) |
| 1578 |
{ |
| 1579 |
struct mhi_ep_device *mhi_dev = mhi_cntrl->mhi_dev; |
| 1580 |
|
| 1581 |
destroy_workqueue(mhi_cntrl->wq); |
| 1582 |
|
| 1583 |
free_irq(mhi_cntrl->irq, mhi_cntrl); |
| 1584 |
|
| 1585 |
kmem_cache_destroy(mhi_cntrl->tre_buf_cache); |
| 1586 |
kmem_cache_destroy(mhi_cntrl->ev_ring_el_cache); |
| 1587 |
kmem_cache_destroy(mhi_cntrl->ring_item_cache); |
| 1588 |
kfree(mhi_cntrl->mhi_cmd); |
| 1589 |
kfree(mhi_cntrl->mhi_chan); |
| 1590 |
|
| 1591 |
device_del(&mhi_dev->dev); |
| 1592 |
put_device(&mhi_dev->dev); |
| 1593 |
|
| 1594 |
ida_free(&mhi_ep_cntrl_ida, mhi_cntrl->index); |
| 1595 |
} |
| 1596 |
EXPORT_SYMBOL_GPL(mhi_ep_unregister_controller); |
| 1597 |
|
| 1598 |
static int mhi_ep_probe(struct device *dev) |
| 1599 |
{ |
| 1600 |
struct mhi_ep_device *mhi_dev = to_mhi_ep_device(dev); |
| 1601 |
struct mhi_ep_driver *mhi_drv = to_mhi_ep_driver(dev->driver); |
| 1602 |
struct mhi_ep_chan *ul_chan = mhi_dev->ul_chan; |
| 1603 |
struct mhi_ep_chan *dl_chan = mhi_dev->dl_chan; |
| 1604 |
|
| 1605 |
ul_chan->xfer_cb = mhi_drv->ul_xfer_cb; |
| 1606 |
dl_chan->xfer_cb = mhi_drv->dl_xfer_cb; |
| 1607 |
|
| 1608 |
return mhi_drv->probe(mhi_dev, mhi_dev->id); |
| 1609 |
} |
| 1610 |
|
| 1611 |
static void mhi_ep_remove(struct device *dev) |
| 1612 |
{ |
| 1613 |
struct mhi_ep_device *mhi_dev = to_mhi_ep_device(dev); |
| 1614 |
struct mhi_ep_driver *mhi_drv = to_mhi_ep_driver(dev->driver); |
| 1615 |
struct mhi_result result = {}; |
| 1616 |
struct mhi_ep_chan *mhi_chan; |
| 1617 |
int dir; |
| 1618 |
|
| 1619 |
/* Skip if it is a controller device */ |
| 1620 |
if (mhi_dev->dev_type == MHI_DEVICE_CONTROLLER) |
| 1621 |
return; |
| 1622 |
|
| 1623 |
/* Disconnect the channels associated with the driver */ |
| 1624 |
for (dir = 0; dir < 2; dir++) { |
| 1625 |
mhi_chan = dir ? mhi_dev->ul_chan : mhi_dev->dl_chan; |
| 1626 |
|
| 1627 |
if (!mhi_chan) |
| 1628 |
continue; |
| 1629 |
|
| 1630 |
mutex_lock(&mhi_chan->lock); |
| 1631 |
/* Send channel disconnect status to the client driver */ |
| 1632 |
if (mhi_chan->xfer_cb) { |
| 1633 |
result.transaction_status = -ENOTCONN; |
| 1634 |
result.bytes_xferd = 0; |
| 1635 |
mhi_chan->xfer_cb(mhi_chan->mhi_dev, &result); |
| 1636 |
} |
| 1637 |
|
| 1638 |
mhi_chan->state = MHI_CH_STATE_DISABLED; |
| 1639 |
mhi_chan->xfer_cb = NULL; |
| 1640 |
mutex_unlock(&mhi_chan->lock); |
| 1641 |
} |
| 1642 |
|
| 1643 |
/* Remove the client driver now */ |
| 1644 |
mhi_drv->remove(mhi_dev); |
| 1645 |
} |
| 1646 |
|
| 1647 |
int __mhi_ep_driver_register(struct mhi_ep_driver *mhi_drv, struct module *owner) |
| 1648 |
{ |
| 1649 |
struct device_driver *driver = &mhi_drv->driver; |
| 1650 |
|
| 1651 |
if (!mhi_drv->probe || !mhi_drv->remove) |
| 1652 |
return -EINVAL; |
| 1653 |
|
| 1654 |
/* Client drivers should have callbacks defined for both channels */ |
| 1655 |
if (!mhi_drv->ul_xfer_cb || !mhi_drv->dl_xfer_cb) |
| 1656 |
return -EINVAL; |
| 1657 |
|
| 1658 |
driver->bus = &mhi_ep_bus_type; |
| 1659 |
driver->owner = owner; |
| 1660 |
|
| 1661 |
return driver_register(driver); |
| 1662 |
} |
| 1663 |
EXPORT_SYMBOL_GPL(__mhi_ep_driver_register); |
| 1664 |
|
| 1665 |
void mhi_ep_driver_unregister(struct mhi_ep_driver *mhi_drv) |
| 1666 |
{ |
| 1667 |
driver_unregister(&mhi_drv->driver); |
| 1668 |
} |
| 1669 |
EXPORT_SYMBOL_GPL(mhi_ep_driver_unregister); |
| 1670 |
|
| 1671 |
static int mhi_ep_uevent(const struct device *dev, struct kobj_uevent_env *env) |
| 1672 |
{ |
| 1673 |
const struct mhi_ep_device *mhi_dev = to_mhi_ep_device(dev); |
| 1674 |
|
| 1675 |
return add_uevent_var(env, "MODALIAS=" MHI_EP_DEVICE_MODALIAS_FMT, |
| 1676 |
mhi_dev->name); |
| 1677 |
} |
| 1678 |
|
| 1679 |
static int mhi_ep_match(struct device *dev, const struct device_driver *drv) |
| 1680 |
{ |
| 1681 |
struct mhi_ep_device *mhi_dev = to_mhi_ep_device(dev); |
| 1682 |
const struct mhi_ep_driver *mhi_drv = to_mhi_ep_driver(drv); |
| 1683 |
const struct mhi_device_id *id; |
| 1684 |
|
| 1685 |
/* |
| 1686 |
* If the device is a controller type then there is no client driver |
| 1687 |
* associated with it |
| 1688 |
*/ |
| 1689 |
if (mhi_dev->dev_type == MHI_DEVICE_CONTROLLER) |
| 1690 |
return 0; |
| 1691 |
|
| 1692 |
for (id = mhi_drv->id_table; id->chan[0]; id++) |
| 1693 |
if (!strcmp(mhi_dev->name, id->chan)) { |
| 1694 |
mhi_dev->id = id; |
| 1695 |
return 1; |
| 1696 |
} |
| 1697 |
|
| 1698 |
return 0; |
| 1699 |
}; |
| 1700 |
|
| 1701 |
const struct bus_type mhi_ep_bus_type = { |
| 1702 |
.name = "mhi_ep", |
| 1703 |
.dev_name = "mhi_ep", |
| 1704 |
.match = mhi_ep_match, |
| 1705 |
.uevent = mhi_ep_uevent, |
| 1706 |
.probe = mhi_ep_probe, |
| 1707 |
.remove = mhi_ep_remove, |
| 1708 |
}; |
| 1709 |
|
| 1710 |
static int __init mhi_ep_init(void) |
| 1711 |
{ |
| 1712 |
return bus_register(&mhi_ep_bus_type); |
| 1713 |
} |
| 1714 |
|
| 1715 |
static void __exit mhi_ep_exit(void) |
| 1716 |
{ |
| 1717 |
bus_unregister(&mhi_ep_bus_type); |
| 1718 |
} |
| 1719 |
|
| 1720 |
postcore_initcall(mhi_ep_init); |
| 1721 |
module_exit(mhi_ep_exit); |
| 1722 |
|
| 1723 |
MODULE_LICENSE("GPL v2"); |
| 1724 |
MODULE_DESCRIPTION("MHI Bus Endpoint stack"); |
| 1725 |
MODULE_AUTHOR("Manivannan Sadhasivam <[email protected]>"); |