| @@ -142,11 +142,83 @@ enum mhi_state mhi_get_mhi_state(struct mhi_controller *mhi_cntrl) · 141 unmodified lines |
| 142 | 142 | return ret ? MHI_STATE_MAX : state; |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | +int mhi_map_single_no_bb(struct mhi_controller *mhi_cntrl, |
| 146 | + struct mhi_buf_info *buf_info) |
| 147 | +{ |
| 148 | + buf_info->p_addr = dma_map_single(mhi_cntrl->cntrl_dev, |
| 149 | + buf_info->v_addr, buf_info->len, |
| 150 | + buf_info->dir); |
| 151 | + if (dma_mapping_error(mhi_cntrl->cntrl_dev, buf_info->p_addr)) |
| 152 | + return -ENOMEM; |
| 153 | + |
| 154 | + return 0; |
| 155 | +} |
| 156 | + |
| 157 | +int mhi_map_single_use_bb(struct mhi_controller *mhi_cntrl, |
| 158 | + struct mhi_buf_info *buf_info) |
| 159 | +{ |
| 160 | + void *buf = mhi_alloc_coherent(mhi_cntrl, buf_info->len, |
| 161 | + &buf_info->p_addr, GFP_ATOMIC); |
| 162 | + |
| 163 | + if (!buf) |
| 164 | + return -ENOMEM; |
| 165 | + |
| 166 | + if (buf_info->dir == DMA_TO_DEVICE) |
| 167 | + memcpy(buf, buf_info->v_addr, buf_info->len); |
| 168 | + |
| 169 | + buf_info->bb_addr = buf; |
| 170 | + |
| 171 | + return 0; |
| 172 | +} |
| 173 | + |
| 174 | +void mhi_unmap_single_no_bb(struct mhi_controller *mhi_cntrl, |
| 175 | + struct mhi_buf_info *buf_info) |
| 176 | +{ |
| 177 | + dma_unmap_single(mhi_cntrl->cntrl_dev, buf_info->p_addr, buf_info->len, |
| 178 | + buf_info->dir); |
| 179 | +} |
| 180 | + |
| 181 | +void mhi_unmap_single_use_bb(struct mhi_controller *mhi_cntrl, |
| 182 | + struct mhi_buf_info *buf_info) |
| 183 | +{ |
| 184 | + if (buf_info->dir == DMA_FROM_DEVICE) |
| 185 | + memcpy(buf_info->v_addr, buf_info->bb_addr, buf_info->len); |
| 186 | + |
| 187 | + mhi_free_coherent(mhi_cntrl, buf_info->len, buf_info->bb_addr, |
| 188 | + buf_info->p_addr); |
| 189 | +} |
| 190 | + |
| 191 | +static int get_nr_avail_ring_elements(struct mhi_controller *mhi_cntrl, |
| 192 | + struct mhi_ring *ring) |
| 193 | +{ |
| 194 | + int nr_el; |
| 195 | + |
| 196 | + if (ring->wp < ring->rp) { |
| 197 | + nr_el = ((ring->rp - ring->wp) / ring->el_size) - 1; |
| 198 | + } else { |
| 199 | + nr_el = (ring->rp - ring->base) / ring->el_size; |
| 200 | + nr_el += ((ring->base + ring->len - ring->wp) / |
| 201 | + ring->el_size) - 1; |
| 202 | + } |
| 203 | + |
| 204 | + return nr_el; |
| 205 | +} |
| 206 | + |
| 145 | 207 | static void *mhi_to_virtual(struct mhi_ring *ring, dma_addr_t addr) |
| 146 | 208 | { |
| 147 | 209 | return (addr - ring->iommu_base) + ring->base; |
| 148 | 210 | } |
| 149 | 211 | |
| 212 | +static void mhi_add_ring_element(struct mhi_controller *mhi_cntrl, |
| 213 | + struct mhi_ring *ring) |
| 214 | +{ |
| 215 | + ring->wp += ring->el_size; |
| 216 | + if (ring->wp >= (ring->base + ring->len)) |
| 217 | + ring->wp = ring->base; |
| 218 | + /* smp update */ |
| 219 | + smp_wmb(); |
| 220 | +} |
| 221 | + |
| 150 | 222 | static void mhi_del_ring_element(struct mhi_controller *mhi_cntrl, |
| 151 | 223 | struct mhi_ring *ring) |
| 152 | 224 | { |
| @@ -416,14 +488,12 @@ static int parse_xfer_event(struct mhi_controller *mhi_cntrl, · 263 unmodified lines |
| 416 | 488 | /* Get the TRB this event points to */ |
| 417 | 489 | ev_tre = mhi_to_virtual(tre_ring, ptr); |
| 418 | 490 | |
| 419 | | − /* device rp after servicing the TREs */ |
| 420 | 491 | dev_rp = ev_tre + 1; |
| 421 | 492 | if (dev_rp >= (tre_ring->base + tre_ring->len)) |
| 422 | 493 | dev_rp = tre_ring->base; |
| 423 | 494 | |
| 424 | 495 | result.dir = mhi_chan->dir; |
| 425 | 496 | |
| 426 | | − /* local rp */ |
| 427 | 497 | local_rp = tre_ring->rp; |
| 428 | 498 | while (local_rp != dev_rp) { |
| 429 | 499 | buf_info = buf_ring->rp; |
| @@ -433,6 +503,10 @@ static int parse_xfer_event(struct mhi_controller *mhi_cntrl, · 3 unmodified lines |
| 433 | 503 | else |
| 434 | 504 | xfer_len = buf_info->len; |
| 435 | 505 | |
| 506 | + /* Unmap if it's not pre-mapped by client */ |
| 507 | + if (likely(!buf_info->pre_mapped)) |
| 508 | + mhi_cntrl->unmap_single(mhi_cntrl, buf_info); |
| 509 | + |
| 436 | 510 | result.buf_addr = buf_info->cb_buf; |
| 437 | 511 | result.bytes_xferd = xfer_len; |
| 438 | 512 | mhi_del_ring_element(mhi_cntrl, buf_ring); |
| @@ -444,6 +518,23 @@ static int parse_xfer_event(struct mhi_controller *mhi_cntrl, · 5 unmodified lines |
| 444 | 518 | |
| 445 | 519 | if (mhi_chan->dir == DMA_TO_DEVICE) |
| 446 | 520 | atomic_dec(&mhi_cntrl->pending_pkts); |
| 521 | + |
| 522 | + /* |
| 523 | + * Recycle the buffer if buffer is pre-allocated, |
| 524 | + * if there is an error, not much we can do apart |
| 525 | + * from dropping the packet |
| 526 | + */ |
| 527 | + if (mhi_chan->pre_alloc) { |
| 528 | + if (mhi_queue_buf(mhi_chan->mhi_dev, |
| 529 | + mhi_chan->dir, |
| 530 | + buf_info->cb_buf, |
| 531 | + buf_info->len, MHI_EOT)) { |
| 532 | + dev_err(dev, |
| 533 | + "Error recycling buffer for chan:%d\n", |
| 534 | + mhi_chan->chan); |
| 535 | + kfree(buf_info->cb_buf); |
| 536 | + } |
| 537 | + } |
| 447 | 538 | } |
| 448 | 539 | break; |
| 449 | 540 | } /* CC_EOT */ |
| @@ -803,3 +894,623 @@ void mhi_ctrl_ev_task(unsigned long data) · 353 unmodified lines |
| 803 | 894 | schedule_work(&mhi_cntrl->syserr_worker); |
| 804 | 895 | } |
| 805 | 896 | } |
| 897 | + |
| 898 | +static bool mhi_is_ring_full(struct mhi_controller *mhi_cntrl, |
| 899 | + struct mhi_ring *ring) |
| 900 | +{ |
| 901 | + void *tmp = ring->wp + ring->el_size; |
| 902 | + |
| 903 | + if (tmp >= (ring->base + ring->len)) |
| 904 | + tmp = ring->base; |
| 905 | + |
| 906 | + return (tmp == ring->rp); |
| 907 | +} |
| 908 | + |
| 909 | +int mhi_queue_skb(struct mhi_device *mhi_dev, enum dma_data_direction dir, |
| 910 | + struct sk_buff *skb, size_t len, enum mhi_flags mflags) |
| 911 | +{ |
| 912 | + struct mhi_controller *mhi_cntrl = mhi_dev->mhi_cntrl; |
| 913 | + struct mhi_chan *mhi_chan = (dir == DMA_TO_DEVICE) ? mhi_dev->ul_chan : |
| 914 | + mhi_dev->dl_chan; |
| 915 | + struct mhi_ring *tre_ring = &mhi_chan->tre_ring; |
| 916 | + struct mhi_ring *buf_ring = &mhi_chan->buf_ring; |
| 917 | + struct mhi_buf_info *buf_info; |
| 918 | + struct mhi_tre *mhi_tre; |
| 919 | + int ret; |
| 920 | + |
| 921 | + /* If MHI host pre-allocates buffers then client drivers cannot queue */ |
| 922 | + if (mhi_chan->pre_alloc) |
| 923 | + return -EINVAL; |
| 924 | + |
| 925 | + if (mhi_is_ring_full(mhi_cntrl, tre_ring)) |
| 926 | + return -ENOMEM; |
| 927 | + |
| 928 | + read_lock_bh(&mhi_cntrl->pm_lock); |
| 929 | + if (unlikely(MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state))) { |
| 930 | + read_unlock_bh(&mhi_cntrl->pm_lock); |
| 931 | + return -EIO; |
| 932 | + } |
| 933 | + |
| 934 | + /* we're in M3 or transitioning to M3 */ |
| 935 | + if (MHI_PM_IN_SUSPEND_STATE(mhi_cntrl->pm_state)) { |
| 936 | + mhi_cntrl->runtime_get(mhi_cntrl); |
| 937 | + mhi_cntrl->runtime_put(mhi_cntrl); |
| 938 | + } |
| 939 | + |
| 940 | + /* Toggle wake to exit out of M2 */ |
| 941 | + mhi_cntrl->wake_toggle(mhi_cntrl); |
| 942 | + |
| 943 | + /* Generate the TRE */ |
| 944 | + buf_info = buf_ring->wp; |
| 945 | + |
| 946 | + buf_info->v_addr = skb->data; |
| 947 | + buf_info->cb_buf = skb; |
| 948 | + buf_info->wp = tre_ring->wp; |
| 949 | + buf_info->dir = mhi_chan->dir; |
| 950 | + buf_info->len = len; |
| 951 | + ret = mhi_cntrl->map_single(mhi_cntrl, buf_info); |
| 952 | + if (ret) |
| 953 | + goto map_error; |
| 954 | + |
| 955 | + mhi_tre = tre_ring->wp; |
| 956 | + |
| 957 | + mhi_tre->ptr = MHI_TRE_DATA_PTR(buf_info->p_addr); |
| 958 | + mhi_tre->dword[0] = MHI_TRE_DATA_DWORD0(buf_info->len); |
| 959 | + mhi_tre->dword[1] = MHI_TRE_DATA_DWORD1(1, 1, 0, 0); |
| 960 | + |
| 961 | + /* increment WP */ |
| 962 | + mhi_add_ring_element(mhi_cntrl, tre_ring); |
| 963 | + mhi_add_ring_element(mhi_cntrl, buf_ring); |
| 964 | + |
| 965 | + if (mhi_chan->dir == DMA_TO_DEVICE) |
| 966 | + atomic_inc(&mhi_cntrl->pending_pkts); |
| 967 | + |
| 968 | + if (likely(MHI_DB_ACCESS_VALID(mhi_cntrl))) { |
| 969 | + read_lock_bh(&mhi_chan->lock); |
| 970 | + mhi_ring_chan_db(mhi_cntrl, mhi_chan); |
| 971 | + read_unlock_bh(&mhi_chan->lock); |
| 972 | + } |
| 973 | + |
| 974 | + read_unlock_bh(&mhi_cntrl->pm_lock); |
| 975 | + |
| 976 | + return 0; |
| 977 | + |
| 978 | +map_error: |
| 979 | + read_unlock_bh(&mhi_cntrl->pm_lock); |
| 980 | + |
| 981 | + return ret; |
| 982 | +} |
| 983 | +EXPORT_SYMBOL_GPL(mhi_queue_skb); |
| 984 | + |
| 985 | +int mhi_queue_dma(struct mhi_device *mhi_dev, enum dma_data_direction dir, |
| 986 | + struct mhi_buf *mhi_buf, size_t len, enum mhi_flags mflags) |
| 987 | +{ |
| 988 | + struct mhi_controller *mhi_cntrl = mhi_dev->mhi_cntrl; |
| 989 | + struct mhi_chan *mhi_chan = (dir == DMA_TO_DEVICE) ? mhi_dev->ul_chan : |
| 990 | + mhi_dev->dl_chan; |
| 991 | + struct device *dev = &mhi_cntrl->mhi_dev->dev; |
| 992 | + struct mhi_ring *tre_ring = &mhi_chan->tre_ring; |
| 993 | + struct mhi_ring *buf_ring = &mhi_chan->buf_ring; |
| 994 | + struct mhi_buf_info *buf_info; |
| 995 | + struct mhi_tre *mhi_tre; |
| 996 | + |
| 997 | + /* If MHI host pre-allocates buffers then client drivers cannot queue */ |
| 998 | + if (mhi_chan->pre_alloc) |
| 999 | + return -EINVAL; |
| 1000 | + |
| 1001 | + if (mhi_is_ring_full(mhi_cntrl, tre_ring)) |
| 1002 | + return -ENOMEM; |
| 1003 | + |
| 1004 | + read_lock_bh(&mhi_cntrl->pm_lock); |
| 1005 | + if (unlikely(MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state))) { |
| 1006 | + dev_err(dev, "MHI is not in activate state, PM state: %s\n", |
| 1007 | + to_mhi_pm_state_str(mhi_cntrl->pm_state)); |
| 1008 | + read_unlock_bh(&mhi_cntrl->pm_lock); |
| 1009 | + |
| 1010 | + return -EIO; |
| 1011 | + } |
| 1012 | + |
| 1013 | + /* we're in M3 or transitioning to M3 */ |
| 1014 | + if (MHI_PM_IN_SUSPEND_STATE(mhi_cntrl->pm_state)) { |
| 1015 | + mhi_cntrl->runtime_get(mhi_cntrl); |
| 1016 | + mhi_cntrl->runtime_put(mhi_cntrl); |
| 1017 | + } |
| 1018 | + |
| 1019 | + /* Toggle wake to exit out of M2 */ |
| 1020 | + mhi_cntrl->wake_toggle(mhi_cntrl); |
| 1021 | + |
| 1022 | + /* Generate the TRE */ |
| 1023 | + buf_info = buf_ring->wp; |
| 1024 | + WARN_ON(buf_info->used); |
| 1025 | + buf_info->p_addr = mhi_buf->dma_addr; |
| 1026 | + buf_info->pre_mapped = true; |
| 1027 | + buf_info->cb_buf = mhi_buf; |
| 1028 | + buf_info->wp = tre_ring->wp; |
| 1029 | + buf_info->dir = mhi_chan->dir; |
| 1030 | + buf_info->len = len; |
| 1031 | + |
| 1032 | + mhi_tre = tre_ring->wp; |
| 1033 | + |
| 1034 | + mhi_tre->ptr = MHI_TRE_DATA_PTR(buf_info->p_addr); |
| 1035 | + mhi_tre->dword[0] = MHI_TRE_DATA_DWORD0(buf_info->len); |
| 1036 | + mhi_tre->dword[1] = MHI_TRE_DATA_DWORD1(1, 1, 0, 0); |
| 1037 | + |
| 1038 | + /* increment WP */ |
| 1039 | + mhi_add_ring_element(mhi_cntrl, tre_ring); |
| 1040 | + mhi_add_ring_element(mhi_cntrl, buf_ring); |
| 1041 | + |
| 1042 | + if (mhi_chan->dir == DMA_TO_DEVICE) |
| 1043 | + atomic_inc(&mhi_cntrl->pending_pkts); |
| 1044 | + |
| 1045 | + if (likely(MHI_DB_ACCESS_VALID(mhi_cntrl))) { |
| 1046 | + read_lock_bh(&mhi_chan->lock); |
| 1047 | + mhi_ring_chan_db(mhi_cntrl, mhi_chan); |
| 1048 | + read_unlock_bh(&mhi_chan->lock); |
| 1049 | + } |
| 1050 | + |
| 1051 | + read_unlock_bh(&mhi_cntrl->pm_lock); |
| 1052 | + |
| 1053 | + return 0; |
| 1054 | +} |
| 1055 | +EXPORT_SYMBOL_GPL(mhi_queue_dma); |
| 1056 | + |
| 1057 | +int mhi_gen_tre(struct mhi_controller *mhi_cntrl, struct mhi_chan *mhi_chan, |
| 1058 | + void *buf, void *cb, size_t buf_len, enum mhi_flags flags) |
| 1059 | +{ |
| 1060 | + struct mhi_ring *buf_ring, *tre_ring; |
| 1061 | + struct mhi_tre *mhi_tre; |
| 1062 | + struct mhi_buf_info *buf_info; |
| 1063 | + int eot, eob, chain, bei; |
| 1064 | + int ret; |
| 1065 | + |
| 1066 | + buf_ring = &mhi_chan->buf_ring; |
| 1067 | + tre_ring = &mhi_chan->tre_ring; |
| 1068 | + |
| 1069 | + buf_info = buf_ring->wp; |
| 1070 | + buf_info->v_addr = buf; |
| 1071 | + buf_info->cb_buf = cb; |
| 1072 | + buf_info->wp = tre_ring->wp; |
| 1073 | + buf_info->dir = mhi_chan->dir; |
| 1074 | + buf_info->len = buf_len; |
| 1075 | + |
| 1076 | + ret = mhi_cntrl->map_single(mhi_cntrl, buf_info); |
| 1077 | + if (ret) |
| 1078 | + return ret; |
| 1079 | + |
| 1080 | + eob = !!(flags & MHI_EOB); |
| 1081 | + eot = !!(flags & MHI_EOT); |
| 1082 | + chain = !!(flags & MHI_CHAIN); |
| 1083 | + bei = !!(mhi_chan->intmod); |
| 1084 | + |
| 1085 | + mhi_tre = tre_ring->wp; |
| 1086 | + mhi_tre->ptr = MHI_TRE_DATA_PTR(buf_info->p_addr); |
| 1087 | + mhi_tre->dword[0] = MHI_TRE_DATA_DWORD0(buf_len); |
| 1088 | + mhi_tre->dword[1] = MHI_TRE_DATA_DWORD1(bei, eot, eob, chain); |
| 1089 | + |
| 1090 | + /* increment WP */ |
| 1091 | + mhi_add_ring_element(mhi_cntrl, tre_ring); |
| 1092 | + mhi_add_ring_element(mhi_cntrl, buf_ring); |
| 1093 | + |
| 1094 | + return 0; |
| 1095 | +} |
| 1096 | + |
| 1097 | +int mhi_queue_buf(struct mhi_device *mhi_dev, enum dma_data_direction dir, |
| 1098 | + void *buf, size_t len, enum mhi_flags mflags) |
| 1099 | +{ |
| 1100 | + struct mhi_controller *mhi_cntrl = mhi_dev->mhi_cntrl; |
| 1101 | + struct mhi_chan *mhi_chan = (dir == DMA_TO_DEVICE) ? mhi_dev->ul_chan : |
| 1102 | + mhi_dev->dl_chan; |
| 1103 | + struct mhi_ring *tre_ring; |
| 1104 | + unsigned long flags; |
| 1105 | + int ret; |
| 1106 | + |
| 1107 | + /* |
| 1108 | + * this check here only as a guard, it's always |
| 1109 | + * possible mhi can enter error while executing rest of function, |
| 1110 | + * which is not fatal so we do not need to hold pm_lock |
| 1111 | + */ |
| 1112 | + if (unlikely(MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state))) |
| 1113 | + return -EIO; |
| 1114 | + |
| 1115 | + tre_ring = &mhi_chan->tre_ring; |
| 1116 | + if (mhi_is_ring_full(mhi_cntrl, tre_ring)) |
| 1117 | + return -ENOMEM; |
| 1118 | + |
| 1119 | + ret = mhi_gen_tre(mhi_cntrl, mhi_chan, buf, buf, len, mflags); |
| 1120 | + if (unlikely(ret)) |
| 1121 | + return ret; |
| 1122 | + |
| 1123 | + read_lock_irqsave(&mhi_cntrl->pm_lock, flags); |
| 1124 | + |
| 1125 | + /* we're in M3 or transitioning to M3 */ |
| 1126 | + if (MHI_PM_IN_SUSPEND_STATE(mhi_cntrl->pm_state)) { |
| 1127 | + mhi_cntrl->runtime_get(mhi_cntrl); |
| 1128 | + mhi_cntrl->runtime_put(mhi_cntrl); |
| 1129 | + } |
| 1130 | + |
| 1131 | + /* Toggle wake to exit out of M2 */ |
| 1132 | + mhi_cntrl->wake_toggle(mhi_cntrl); |
| 1133 | + |
| 1134 | + if (mhi_chan->dir == DMA_TO_DEVICE) |
| 1135 | + atomic_inc(&mhi_cntrl->pending_pkts); |
| 1136 | + |
| 1137 | + if (likely(MHI_DB_ACCESS_VALID(mhi_cntrl))) { |
| 1138 | + unsigned long flags; |
| 1139 | + |
| 1140 | + read_lock_irqsave(&mhi_chan->lock, flags); |
| 1141 | + mhi_ring_chan_db(mhi_cntrl, mhi_chan); |
| 1142 | + read_unlock_irqrestore(&mhi_chan->lock, flags); |
| 1143 | + } |
| 1144 | + |
| 1145 | + read_unlock_irqrestore(&mhi_cntrl->pm_lock, flags); |
| 1146 | + |
| 1147 | + return 0; |
| 1148 | +} |
| 1149 | +EXPORT_SYMBOL_GPL(mhi_queue_buf); |
| 1150 | + |
| 1151 | +int mhi_send_cmd(struct mhi_controller *mhi_cntrl, |
| 1152 | + struct mhi_chan *mhi_chan, |
| 1153 | + enum mhi_cmd_type cmd) |
| 1154 | +{ |
| 1155 | + struct mhi_tre *cmd_tre = NULL; |
| 1156 | + struct mhi_cmd *mhi_cmd = &mhi_cntrl->mhi_cmd[PRIMARY_CMD_RING]; |
| 1157 | + struct mhi_ring *ring = &mhi_cmd->ring; |
| 1158 | + struct device *dev = &mhi_cntrl->mhi_dev->dev; |
| 1159 | + int chan = 0; |
| 1160 | + |
| 1161 | + if (mhi_chan) |
| 1162 | + chan = mhi_chan->chan; |
| 1163 | + |
| 1164 | + spin_lock_bh(&mhi_cmd->lock); |
| 1165 | + if (!get_nr_avail_ring_elements(mhi_cntrl, ring)) { |
| 1166 | + spin_unlock_bh(&mhi_cmd->lock); |
| 1167 | + return -ENOMEM; |
| 1168 | + } |
| 1169 | + |
| 1170 | + /* prepare the cmd tre */ |
| 1171 | + cmd_tre = ring->wp; |
| 1172 | + switch (cmd) { |
| 1173 | + case MHI_CMD_RESET_CHAN: |
| 1174 | + cmd_tre->ptr = MHI_TRE_CMD_RESET_PTR; |
| 1175 | + cmd_tre->dword[0] = MHI_TRE_CMD_RESET_DWORD0; |
| 1176 | + cmd_tre->dword[1] = MHI_TRE_CMD_RESET_DWORD1(chan); |
| 1177 | + break; |
| 1178 | + case MHI_CMD_START_CHAN: |
| 1179 | + cmd_tre->ptr = MHI_TRE_CMD_START_PTR; |
| 1180 | + cmd_tre->dword[0] = MHI_TRE_CMD_START_DWORD0; |
| 1181 | + cmd_tre->dword[1] = MHI_TRE_CMD_START_DWORD1(chan); |
| 1182 | + break; |
| 1183 | + default: |
| 1184 | + dev_err(dev, "Command not supported\n"); |
| 1185 | + break; |
| 1186 | + } |
| 1187 | + |
| 1188 | + /* queue to hardware */ |
| 1189 | + mhi_add_ring_element(mhi_cntrl, ring); |
| 1190 | + read_lock_bh(&mhi_cntrl->pm_lock); |
| 1191 | + if (likely(MHI_DB_ACCESS_VALID(mhi_cntrl))) |
| 1192 | + mhi_ring_cmd_db(mhi_cntrl, mhi_cmd); |
| 1193 | + read_unlock_bh(&mhi_cntrl->pm_lock); |
| 1194 | + spin_unlock_bh(&mhi_cmd->lock); |
| 1195 | + |
| 1196 | + return 0; |
| 1197 | +} |
| 1198 | + |
| 1199 | +static void __mhi_unprepare_channel(struct mhi_controller *mhi_cntrl, |
| 1200 | + struct mhi_chan *mhi_chan) |
| 1201 | +{ |
| 1202 | + int ret; |
| 1203 | + struct device *dev = &mhi_cntrl->mhi_dev->dev; |
| 1204 | + |
| 1205 | + dev_dbg(dev, "Entered: unprepare channel:%d\n", mhi_chan->chan); |
| 1206 | + |
| 1207 | + /* no more processing events for this channel */ |
| 1208 | + mutex_lock(&mhi_chan->mutex); |
| 1209 | + write_lock_irq(&mhi_chan->lock); |
| 1210 | + if (mhi_chan->ch_state != MHI_CH_STATE_ENABLED) { |
| 1211 | + write_unlock_irq(&mhi_chan->lock); |
| 1212 | + mutex_unlock(&mhi_chan->mutex); |
| 1213 | + return; |
| 1214 | + } |
| 1215 | + |
| 1216 | + mhi_chan->ch_state = MHI_CH_STATE_DISABLED; |
| 1217 | + write_unlock_irq(&mhi_chan->lock); |
| 1218 | + |
| 1219 | + reinit_completion(&mhi_chan->completion); |
| 1220 | + read_lock_bh(&mhi_cntrl->pm_lock); |
| 1221 | + if (MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state)) { |
| 1222 | + read_unlock_bh(&mhi_cntrl->pm_lock); |
| 1223 | + goto error_invalid_state; |
| 1224 | + } |
| 1225 | + |
| 1226 | + mhi_cntrl->wake_toggle(mhi_cntrl); |
| 1227 | + read_unlock_bh(&mhi_cntrl->pm_lock); |
| 1228 | + |
| 1229 | + mhi_cntrl->runtime_get(mhi_cntrl); |
| 1230 | + mhi_cntrl->runtime_put(mhi_cntrl); |
| 1231 | + ret = mhi_send_cmd(mhi_cntrl, mhi_chan, MHI_CMD_RESET_CHAN); |
| 1232 | + if (ret) |
| 1233 | + goto error_invalid_state; |
| 1234 | + |
| 1235 | + /* even if it fails we will still reset */ |
| 1236 | + ret = wait_for_completion_timeout(&mhi_chan->completion, |
| 1237 | + msecs_to_jiffies(mhi_cntrl->timeout_ms)); |
| 1238 | + if (!ret || mhi_chan->ccs != MHI_EV_CC_SUCCESS) |
| 1239 | + dev_err(dev, |
| 1240 | + "Failed to receive cmd completion, still resetting\n"); |
| 1241 | + |
| 1242 | +error_invalid_state: |
| 1243 | + if (!mhi_chan->offload_ch) { |
| 1244 | + mhi_reset_chan(mhi_cntrl, mhi_chan); |
| 1245 | + mhi_deinit_chan_ctxt(mhi_cntrl, mhi_chan); |
| 1246 | + } |
| 1247 | + dev_dbg(dev, "chan:%d successfully resetted\n", mhi_chan->chan); |
| 1248 | + mutex_unlock(&mhi_chan->mutex); |
| 1249 | +} |
| 1250 | + |
| 1251 | +int mhi_prepare_channel(struct mhi_controller *mhi_cntrl, |
| 1252 | + struct mhi_chan *mhi_chan) |
| 1253 | +{ |
| 1254 | + int ret = 0; |
| 1255 | + struct device *dev = &mhi_cntrl->mhi_dev->dev; |
| 1256 | + |
| 1257 | + dev_dbg(dev, "Preparing channel: %d\n", mhi_chan->chan); |
| 1258 | + |
| 1259 | + if (!(BIT(mhi_cntrl->ee) & mhi_chan->ee_mask)) { |
| 1260 | + dev_err(dev, |
| 1261 | + "Current EE: %s Required EE Mask: 0x%x for chan: %s\n", |
| 1262 | + TO_MHI_EXEC_STR(mhi_cntrl->ee), mhi_chan->ee_mask, |
| 1263 | + mhi_chan->name); |
| 1264 | + return -ENOTCONN; |
| 1265 | + } |
| 1266 | + |
| 1267 | + mutex_lock(&mhi_chan->mutex); |
| 1268 | + |
| 1269 | + /* If channel is not in disable state, do not allow it to start */ |
| 1270 | + if (mhi_chan->ch_state != MHI_CH_STATE_DISABLED) { |
| 1271 | + ret = -EIO; |
| 1272 | + dev_dbg(dev, "channel: %d is not in disabled state\n", |
| 1273 | + mhi_chan->chan); |
| 1274 | + goto error_init_chan; |
| 1275 | + } |
| 1276 | + |
| 1277 | + /* Check of client manages channel context for offload channels */ |
| 1278 | + if (!mhi_chan->offload_ch) { |
| 1279 | + ret = mhi_init_chan_ctxt(mhi_cntrl, mhi_chan); |
| 1280 | + if (ret) |
| 1281 | + goto error_init_chan; |
| 1282 | + } |
| 1283 | + |
| 1284 | + reinit_completion(&mhi_chan->completion); |
| 1285 | + read_lock_bh(&mhi_cntrl->pm_lock); |
| 1286 | + if (MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state)) { |
| 1287 | + read_unlock_bh(&mhi_cntrl->pm_lock); |
| 1288 | + ret = -EIO; |
| 1289 | + goto error_pm_state; |
| 1290 | + } |
| 1291 | + |
| 1292 | + mhi_cntrl->wake_toggle(mhi_cntrl); |
| 1293 | + read_unlock_bh(&mhi_cntrl->pm_lock); |
| 1294 | + mhi_cntrl->runtime_get(mhi_cntrl); |
| 1295 | + mhi_cntrl->runtime_put(mhi_cntrl); |
| 1296 | + |
| 1297 | + ret = mhi_send_cmd(mhi_cntrl, mhi_chan, MHI_CMD_START_CHAN); |
| 1298 | + if (ret) |
| 1299 | + goto error_pm_state; |
| 1300 | + |
| 1301 | + ret = wait_for_completion_timeout(&mhi_chan->completion, |
| 1302 | + msecs_to_jiffies(mhi_cntrl->timeout_ms)); |
| 1303 | + if (!ret || mhi_chan->ccs != MHI_EV_CC_SUCCESS) { |
| 1304 | + ret = -EIO; |
| 1305 | + goto error_pm_state; |
| 1306 | + } |
| 1307 | + |
| 1308 | + write_lock_irq(&mhi_chan->lock); |
| 1309 | + mhi_chan->ch_state = MHI_CH_STATE_ENABLED; |
| 1310 | + write_unlock_irq(&mhi_chan->lock); |
| 1311 | + |
| 1312 | + /* Pre-allocate buffer for xfer ring */ |
| 1313 | + if (mhi_chan->pre_alloc) { |
| 1314 | + int nr_el = get_nr_avail_ring_elements(mhi_cntrl, |
| 1315 | + &mhi_chan->tre_ring); |
| 1316 | + size_t len = mhi_cntrl->buffer_len; |
| 1317 | + |
| 1318 | + while (nr_el--) { |
| 1319 | + void *buf; |
| 1320 | + |
| 1321 | + buf = kmalloc(len, GFP_KERNEL); |
| 1322 | + if (!buf) { |
| 1323 | + ret = -ENOMEM; |
| 1324 | + goto error_pre_alloc; |
| 1325 | + } |
| 1326 | + |
| 1327 | + /* Prepare transfer descriptors */ |
| 1328 | + ret = mhi_gen_tre(mhi_cntrl, mhi_chan, buf, buf, |
| 1329 | + len, MHI_EOT); |
| 1330 | + if (ret) { |
| 1331 | + kfree(buf); |
| 1332 | + goto error_pre_alloc; |
| 1333 | + } |
| 1334 | + } |
| 1335 | + |
| 1336 | + read_lock_bh(&mhi_cntrl->pm_lock); |
| 1337 | + if (MHI_DB_ACCESS_VALID(mhi_cntrl)) { |
| 1338 | + read_lock_irq(&mhi_chan->lock); |
| 1339 | + mhi_ring_chan_db(mhi_cntrl, mhi_chan); |
| 1340 | + read_unlock_irq(&mhi_chan->lock); |
| 1341 | + } |
| 1342 | + read_unlock_bh(&mhi_cntrl->pm_lock); |
| 1343 | + } |
| 1344 | + |
| 1345 | + mutex_unlock(&mhi_chan->mutex); |
| 1346 | + |
| 1347 | + dev_dbg(dev, "Chan: %d successfully moved to start state\n", |
| 1348 | + mhi_chan->chan); |
| 1349 | + |
| 1350 | + return 0; |
| 1351 | + |
| 1352 | +error_pm_state: |
| 1353 | + if (!mhi_chan->offload_ch) |
| 1354 | + mhi_deinit_chan_ctxt(mhi_cntrl, mhi_chan); |
| 1355 | + |
| 1356 | +error_init_chan: |
| 1357 | + mutex_unlock(&mhi_chan->mutex); |
| 1358 | + |
| 1359 | + return ret; |
| 1360 | + |
| 1361 | +error_pre_alloc: |
| 1362 | + mutex_unlock(&mhi_chan->mutex); |
| 1363 | + __mhi_unprepare_channel(mhi_cntrl, mhi_chan); |
| 1364 | + |
| 1365 | + return ret; |
| 1366 | +} |
| 1367 | + |
| 1368 | +static void mhi_mark_stale_events(struct mhi_controller *mhi_cntrl, |
| 1369 | + struct mhi_event *mhi_event, |
| 1370 | + struct mhi_event_ctxt *er_ctxt, |
| 1371 | + int chan) |
| 1372 | + |
| 1373 | +{ |
| 1374 | + struct mhi_tre *dev_rp, *local_rp; |
| 1375 | + struct mhi_ring *ev_ring; |
| 1376 | + struct device *dev = &mhi_cntrl->mhi_dev->dev; |
| 1377 | + unsigned long flags; |
| 1378 | + |
| 1379 | + dev_dbg(dev, "Marking all events for chan: %d as stale\n", chan); |
| 1380 | + |
| 1381 | + ev_ring = &mhi_event->ring; |
| 1382 | + |
| 1383 | + /* mark all stale events related to channel as STALE event */ |
| 1384 | + spin_lock_irqsave(&mhi_event->lock, flags); |
| 1385 | + dev_rp = mhi_to_virtual(ev_ring, er_ctxt->rp); |
| 1386 | + |
| 1387 | + local_rp = ev_ring->rp; |
| 1388 | + while (dev_rp != local_rp) { |
| 1389 | + if (MHI_TRE_GET_EV_TYPE(local_rp) == MHI_PKT_TYPE_TX_EVENT && |
| 1390 | + chan == MHI_TRE_GET_EV_CHID(local_rp)) |
| 1391 | + local_rp->dword[1] = MHI_TRE_EV_DWORD1(chan, |
| 1392 | + MHI_PKT_TYPE_STALE_EVENT); |
| 1393 | + local_rp++; |
| 1394 | + if (local_rp == (ev_ring->base + ev_ring->len)) |
| 1395 | + local_rp = ev_ring->base; |
| 1396 | + } |
| 1397 | + |
| 1398 | + dev_dbg(dev, "Finished marking events as stale events\n"); |
| 1399 | + spin_unlock_irqrestore(&mhi_event->lock, flags); |
| 1400 | +} |
| 1401 | + |
| 1402 | +static void mhi_reset_data_chan(struct mhi_controller *mhi_cntrl, |
| 1403 | + struct mhi_chan *mhi_chan) |
| 1404 | +{ |
| 1405 | + struct mhi_ring *buf_ring, *tre_ring; |
| 1406 | + struct mhi_result result; |
| 1407 | + |
| 1408 | + /* Reset any pending buffers */ |
| 1409 | + buf_ring = &mhi_chan->buf_ring; |
| 1410 | + tre_ring = &mhi_chan->tre_ring; |
| 1411 | + result.transaction_status = -ENOTCONN; |
| 1412 | + result.bytes_xferd = 0; |
| 1413 | + while (tre_ring->rp != tre_ring->wp) { |
| 1414 | + struct mhi_buf_info *buf_info = buf_ring->rp; |
| 1415 | + |
| 1416 | + if (mhi_chan->dir == DMA_TO_DEVICE) |
| 1417 | + atomic_dec(&mhi_cntrl->pending_pkts); |
| 1418 | + |
| 1419 | + if (!buf_info->pre_mapped) |
| 1420 | + mhi_cntrl->unmap_single(mhi_cntrl, buf_info); |
| 1421 | + |
| 1422 | + mhi_del_ring_element(mhi_cntrl, buf_ring); |
| 1423 | + mhi_del_ring_element(mhi_cntrl, tre_ring); |
| 1424 | + |
| 1425 | + if (mhi_chan->pre_alloc) { |
| 1426 | + kfree(buf_info->cb_buf); |
| 1427 | + } else { |
| 1428 | + result.buf_addr = buf_info->cb_buf; |
| 1429 | + mhi_chan->xfer_cb(mhi_chan->mhi_dev, &result); |
| 1430 | + } |
| 1431 | + } |
| 1432 | +} |
| 1433 | + |
| 1434 | +void mhi_reset_chan(struct mhi_controller *mhi_cntrl, struct mhi_chan *mhi_chan) |
| 1435 | +{ |
| 1436 | + struct mhi_event *mhi_event; |
| 1437 | + struct mhi_event_ctxt *er_ctxt; |
| 1438 | + int chan = mhi_chan->chan; |
| 1439 | + |
| 1440 | + /* Nothing to reset, client doesn't queue buffers */ |
| 1441 | + if (mhi_chan->offload_ch) |
| 1442 | + return; |
| 1443 | + |
| 1444 | + read_lock_bh(&mhi_cntrl->pm_lock); |
| 1445 | + mhi_event = &mhi_cntrl->mhi_event[mhi_chan->er_index]; |
| 1446 | + er_ctxt = &mhi_cntrl->mhi_ctxt->er_ctxt[mhi_chan->er_index]; |
| 1447 | + |
| 1448 | + mhi_mark_stale_events(mhi_cntrl, mhi_event, er_ctxt, chan); |
| 1449 | + |
| 1450 | + mhi_reset_data_chan(mhi_cntrl, mhi_chan); |
| 1451 | + |
| 1452 | + read_unlock_bh(&mhi_cntrl->pm_lock); |
| 1453 | +} |
| 1454 | + |
| 1455 | +/* Move channel to start state */ |
| 1456 | +int mhi_prepare_for_transfer(struct mhi_device *mhi_dev) |
| 1457 | +{ |
| 1458 | + int ret, dir; |
| 1459 | + struct mhi_controller *mhi_cntrl = mhi_dev->mhi_cntrl; |
| 1460 | + struct mhi_chan *mhi_chan; |
| 1461 | + |
| 1462 | + for (dir = 0; dir < 2; dir++) { |
| 1463 | + mhi_chan = dir ? mhi_dev->dl_chan : mhi_dev->ul_chan; |
| 1464 | + if (!mhi_chan) |
| 1465 | + continue; |
| 1466 | + |
| 1467 | + ret = mhi_prepare_channel(mhi_cntrl, mhi_chan); |
| 1468 | + if (ret) |
| 1469 | + goto error_open_chan; |
| 1470 | + } |
| 1471 | + |
| 1472 | + return 0; |
| 1473 | + |
| 1474 | +error_open_chan: |
| 1475 | + for (--dir; dir >= 0; dir--) { |
| 1476 | + mhi_chan = dir ? mhi_dev->dl_chan : mhi_dev->ul_chan; |
| 1477 | + if (!mhi_chan) |
| 1478 | + continue; |
| 1479 | + |
| 1480 | + __mhi_unprepare_channel(mhi_cntrl, mhi_chan); |
| 1481 | + } |
| 1482 | + |
| 1483 | + return ret; |
| 1484 | +} |
| 1485 | +EXPORT_SYMBOL_GPL(mhi_prepare_for_transfer); |
| 1486 | + |
| 1487 | +void mhi_unprepare_from_transfer(struct mhi_device *mhi_dev) |
| 1488 | +{ |
| 1489 | + struct mhi_controller *mhi_cntrl = mhi_dev->mhi_cntrl; |
| 1490 | + struct mhi_chan *mhi_chan; |
| 1491 | + int dir; |
| 1492 | + |
| 1493 | + for (dir = 0; dir < 2; dir++) { |
| 1494 | + mhi_chan = dir ? mhi_dev->ul_chan : mhi_dev->dl_chan; |
| 1495 | + if (!mhi_chan) |
| 1496 | + continue; |
| 1497 | + |
| 1498 | + __mhi_unprepare_channel(mhi_cntrl, mhi_chan); |
| 1499 | + } |
| 1500 | +} |
| 1501 | +EXPORT_SYMBOL_GPL(mhi_unprepare_from_transfer); |
| 1502 | + |
| 1503 | +int mhi_poll(struct mhi_device *mhi_dev, u32 budget) |
| 1504 | +{ |
| 1505 | + struct mhi_controller *mhi_cntrl = mhi_dev->mhi_cntrl; |
| 1506 | + struct mhi_chan *mhi_chan = mhi_dev->dl_chan; |
| 1507 | + struct mhi_event *mhi_event = &mhi_cntrl->mhi_event[mhi_chan->er_index]; |
| 1508 | + int ret; |
| 1509 | + |
| 1510 | + spin_lock_bh(&mhi_event->lock); |
| 1511 | + ret = mhi_event->process_event(mhi_cntrl, mhi_event, budget); |
| 1512 | + spin_unlock_bh(&mhi_event->lock); |
| 1513 | + |
| 1514 | + return ret; |
| 1515 | +} |
| 1516 | +EXPORT_SYMBOL_GPL(mhi_poll); |