blob: fa82e349a5a8de8b746926674153fa205f74412b [file] [log] [blame]
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001/* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14/*
15 * BAM DMUX module.
16 */
17
18#define DEBUG
19
20#include <linux/delay.h>
21#include <linux/module.h>
22#include <linux/netdevice.h>
23#include <linux/platform_device.h>
24#include <linux/sched.h>
25#include <linux/skbuff.h>
26#include <linux/debugfs.h>
Jeff Hugoaab7ebc2011-09-07 16:46:04 -060027#include <linux/clk.h>
Jeff Hugoae3a85e2011-12-02 17:10:18 -070028#include <linux/wakelock.h>
Eric Holmberg878923a2012-01-10 14:28:19 -070029#include <linux/kfifo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030
31#include <mach/sps.h>
32#include <mach/bam_dmux.h>
Jeff Hugoade1f842011-08-03 15:53:59 -060033#include <mach/msm_smsm.h>
Jeff Hugo6e7a92a2011-10-24 05:25:13 -060034#include <mach/subsystem_notif.h>
Jeff Hugo75913c82011-12-05 15:59:01 -070035#include <mach/socinfo.h>
Jeff Hugo4838f412012-01-20 11:19:37 -070036#include <mach/subsystem_restart.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037
38#define BAM_CH_LOCAL_OPEN 0x1
39#define BAM_CH_REMOTE_OPEN 0x2
Jeff Hugo6e7a92a2011-10-24 05:25:13 -060040#define BAM_CH_IN_RESET 0x4
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070041
42#define BAM_MUX_HDR_MAGIC_NO 0x33fc
43
Eric Holmberg006057d2012-01-11 10:10:42 -070044#define BAM_MUX_HDR_CMD_DATA 0
45#define BAM_MUX_HDR_CMD_OPEN 1
46#define BAM_MUX_HDR_CMD_CLOSE 2
47#define BAM_MUX_HDR_CMD_STATUS 3 /* unused */
48#define BAM_MUX_HDR_CMD_OPEN_NO_A2_PC 4
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049
Jeff Hugo949080a2011-08-30 11:58:56 -060050#define POLLING_MIN_SLEEP 950 /* 0.95 ms */
51#define POLLING_MAX_SLEEP 1050 /* 1.05 ms */
52#define POLLING_INACTIVITY 40 /* cycles before switch to intr mode */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070053
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -070054#define LOW_WATERMARK 2
55#define HIGH_WATERMARK 4
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070056
57static int msm_bam_dmux_debug_enable;
58module_param_named(debug_enable, msm_bam_dmux_debug_enable,
59 int, S_IRUGO | S_IWUSR | S_IWGRP);
60
61#if defined(DEBUG)
62static uint32_t bam_dmux_read_cnt;
63static uint32_t bam_dmux_write_cnt;
64static uint32_t bam_dmux_write_cpy_cnt;
65static uint32_t bam_dmux_write_cpy_bytes;
Eric Holmberg2fddbcd2011-11-28 18:25:57 -070066static uint32_t bam_dmux_tx_sps_failure_cnt;
Eric Holmberg6074aba2012-01-18 17:59:44 -070067static uint32_t bam_dmux_tx_stall_cnt;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068
69#define DBG(x...) do { \
70 if (msm_bam_dmux_debug_enable) \
71 pr_debug(x); \
72 } while (0)
73
74#define DBG_INC_READ_CNT(x) do { \
75 bam_dmux_read_cnt += (x); \
76 if (msm_bam_dmux_debug_enable) \
77 pr_debug("%s: total read bytes %u\n", \
78 __func__, bam_dmux_read_cnt); \
79 } while (0)
80
81#define DBG_INC_WRITE_CNT(x) do { \
82 bam_dmux_write_cnt += (x); \
83 if (msm_bam_dmux_debug_enable) \
84 pr_debug("%s: total written bytes %u\n", \
85 __func__, bam_dmux_write_cnt); \
86 } while (0)
87
88#define DBG_INC_WRITE_CPY(x) do { \
89 bam_dmux_write_cpy_bytes += (x); \
90 bam_dmux_write_cpy_cnt++; \
91 if (msm_bam_dmux_debug_enable) \
92 pr_debug("%s: total write copy cnt %u, bytes %u\n", \
93 __func__, bam_dmux_write_cpy_cnt, \
94 bam_dmux_write_cpy_bytes); \
95 } while (0)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -070096
97#define DBG_INC_TX_SPS_FAILURE_CNT() do { \
98 bam_dmux_tx_sps_failure_cnt++; \
99} while (0)
100
Eric Holmberg6074aba2012-01-18 17:59:44 -0700101#define DBG_INC_TX_STALL_CNT() do { \
102 bam_dmux_tx_stall_cnt++; \
103} while (0)
104
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105#else
106#define DBG(x...) do { } while (0)
107#define DBG_INC_READ_CNT(x...) do { } while (0)
108#define DBG_INC_WRITE_CNT(x...) do { } while (0)
109#define DBG_INC_WRITE_CPY(x...) do { } while (0)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700110#define DBG_INC_TX_SPS_FAILURE_CNT() do { } while (0)
Eric Holmberg6074aba2012-01-18 17:59:44 -0700111#define DBG_INC_TX_STALL_CNT() do { } while (0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112#endif
113
114struct bam_ch_info {
115 uint32_t status;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600116 void (*notify)(void *, int, unsigned long);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700117 void *priv;
118 spinlock_t lock;
Jeff Hugo7960abd2011-08-02 15:39:38 -0600119 struct platform_device *pdev;
120 char name[BAM_DMUX_CH_NAME_MAX_LEN];
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700121 int num_tx_pkts;
122 int use_wm;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700123};
124
125struct tx_pkt_info {
126 struct sk_buff *skb;
127 dma_addr_t dma_address;
128 char is_cmd;
129 uint32_t len;
130 struct work_struct work;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600131 struct list_head list_node;
Eric Holmberg878923a2012-01-10 14:28:19 -0700132 unsigned ts_sec;
133 unsigned long ts_nsec;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134};
135
136struct rx_pkt_info {
137 struct sk_buff *skb;
138 dma_addr_t dma_address;
139 struct work_struct work;
Jeff Hugo949080a2011-08-30 11:58:56 -0600140 struct list_head list_node;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700141};
142
143#define A2_NUM_PIPES 6
144#define A2_SUMMING_THRESHOLD 4096
145#define A2_DEFAULT_DESCRIPTORS 32
146#define A2_PHYS_BASE 0x124C2000
147#define A2_PHYS_SIZE 0x2000
148#define BUFFER_SIZE 2048
149#define NUM_BUFFERS 32
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700150static struct sps_bam_props a2_props;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600151static u32 a2_device_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700152static struct sps_pipe *bam_tx_pipe;
153static struct sps_pipe *bam_rx_pipe;
154static struct sps_connect tx_connection;
155static struct sps_connect rx_connection;
156static struct sps_mem_buffer tx_desc_mem_buf;
157static struct sps_mem_buffer rx_desc_mem_buf;
158static struct sps_register_event tx_register_event;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600159static struct sps_register_event rx_register_event;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700160
161static struct bam_ch_info bam_ch[BAM_DMUX_NUM_CHANNELS];
162static int bam_mux_initialized;
163
Jeff Hugo949080a2011-08-30 11:58:56 -0600164static int polling_mode;
165
166static LIST_HEAD(bam_rx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600167static DEFINE_MUTEX(bam_rx_pool_mutexlock);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700168static int bam_rx_pool_len;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600169static LIST_HEAD(bam_tx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600170static DEFINE_SPINLOCK(bam_tx_pool_spinlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600171
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172struct bam_mux_hdr {
173 uint16_t magic_num;
174 uint8_t reserved;
175 uint8_t cmd;
176 uint8_t pad_len;
177 uint8_t ch_id;
178 uint16_t pkt_len;
179};
180
Jeff Hugod98b1082011-10-24 10:30:23 -0600181static void notify_all(int event, unsigned long data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182static void bam_mux_write_done(struct work_struct *work);
183static void handle_bam_mux_cmd(struct work_struct *work);
Jeff Hugo949080a2011-08-30 11:58:56 -0600184static void rx_timer_work_func(struct work_struct *work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700185
Jeff Hugo949080a2011-08-30 11:58:56 -0600186static DECLARE_WORK(rx_timer_work, rx_timer_work_func);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700187
188static struct workqueue_struct *bam_mux_rx_workqueue;
189static struct workqueue_struct *bam_mux_tx_workqueue;
190
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600191/* A2 power collaspe */
192#define UL_TIMEOUT_DELAY 1000 /* in ms */
193static void toggle_apps_ack(void);
194static void reconnect_to_bam(void);
195static void disconnect_to_bam(void);
196static void ul_wakeup(void);
197static void ul_timeout(struct work_struct *work);
198static void vote_dfab(void);
199static void unvote_dfab(void);
Jeff Hugod98b1082011-10-24 10:30:23 -0600200static void kickoff_ul_wakeup_func(struct work_struct *work);
Eric Holmberg006057d2012-01-11 10:10:42 -0700201static void grab_wakelock(void);
202static void release_wakelock(void);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600203
204static int bam_is_connected;
205static DEFINE_MUTEX(wakeup_lock);
206static struct completion ul_wakeup_ack_completion;
207static struct completion bam_connection_completion;
208static struct delayed_work ul_timeout_work;
209static int ul_packet_written;
210static struct clk *dfab_clk;
211static DEFINE_RWLOCK(ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600212static DECLARE_WORK(kickoff_ul_wakeup, kickoff_ul_wakeup_func);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600213static int bam_connection_is_active;
Jeff Hugof6c1c1e2011-12-01 17:43:49 -0700214static int wait_for_ack;
Jeff Hugoae3a85e2011-12-02 17:10:18 -0700215static struct wake_lock bam_wakelock;
Eric Holmberg006057d2012-01-11 10:10:42 -0700216static int a2_pc_disabled;
217static DEFINE_MUTEX(dfab_status_lock);
218static int dfab_is_on;
219static int wait_for_dfab;
220static struct completion dfab_unvote_completion;
221static DEFINE_SPINLOCK(wakelock_reference_lock);
222static int wakelock_reference_count;
Eric Holmberg604ab252012-01-15 00:01:18 -0700223static struct delayed_work msm9615_bam_init_work;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600224/* End A2 power collaspe */
225
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600226/* subsystem restart */
227static int restart_notifier_cb(struct notifier_block *this,
228 unsigned long code,
229 void *data);
230
231static struct notifier_block restart_notifier = {
232 .notifier_call = restart_notifier_cb,
233};
234static int in_global_reset;
235/* end subsystem restart */
236
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700237#define bam_ch_is_open(x) \
238 (bam_ch[(x)].status == (BAM_CH_LOCAL_OPEN | BAM_CH_REMOTE_OPEN))
239
240#define bam_ch_is_local_open(x) \
241 (bam_ch[(x)].status & BAM_CH_LOCAL_OPEN)
242
243#define bam_ch_is_remote_open(x) \
244 (bam_ch[(x)].status & BAM_CH_REMOTE_OPEN)
245
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600246#define bam_ch_is_in_reset(x) \
247 (bam_ch[(x)].status & BAM_CH_IN_RESET)
248
Eric Holmberg878923a2012-01-10 14:28:19 -0700249#define LOG_MESSAGE_MAX_SIZE 80
250struct kfifo bam_dmux_state_log;
251static uint32_t bam_dmux_state_logging_disabled;
252static DEFINE_SPINLOCK(bam_dmux_logging_spinlock);
253static int bam_dmux_uplink_vote;
254static int bam_dmux_power_state;
255
256
257#define DMUX_LOG_KERR(fmt...) \
258do { \
259 bam_dmux_log(fmt); \
260 pr_err(fmt); \
261} while (0)
262
263/**
264 * Log a state change along with a small message.
265 *
266 * Complete size of messsage is limited to @todo.
267 */
268static void bam_dmux_log(const char *fmt, ...)
269{
270 char buff[LOG_MESSAGE_MAX_SIZE];
271 unsigned long flags;
272 va_list arg_list;
273 unsigned long long t_now;
274 unsigned long nanosec_rem;
275 int len = 0;
276
277 if (bam_dmux_state_logging_disabled)
278 return;
279
280 t_now = sched_clock();
281 nanosec_rem = do_div(t_now, 1000000000U);
282
283 /*
284 * States
Eric Holmberg006057d2012-01-11 10:10:42 -0700285 * D: 1 = Power collapse disabled
Eric Holmberg878923a2012-01-10 14:28:19 -0700286 * R: 1 = in global reset
287 * P: 1 = BAM is powered up
288 * A: 1 = BAM initialized and ready for data
289 *
290 * V: 1 = Uplink vote for power
291 * U: 1 = Uplink active
292 * W: 1 = Uplink Wait-for-ack
293 * A: 1 = Uplink ACK received
294 */
295 len += scnprintf(buff, sizeof(buff),
Eric Holmberg006057d2012-01-11 10:10:42 -0700296 "<DMUX> %u.%09lu %c%c%c%c %c%c%c%c ",
Eric Holmberg878923a2012-01-10 14:28:19 -0700297 (unsigned)t_now, nanosec_rem,
Eric Holmberg006057d2012-01-11 10:10:42 -0700298 a2_pc_disabled ? 'D' : 'd',
Eric Holmberg878923a2012-01-10 14:28:19 -0700299 in_global_reset ? 'R' : 'r',
300 bam_dmux_power_state ? 'P' : 'p',
301 bam_connection_is_active ? 'A' : 'a',
302 bam_dmux_uplink_vote ? 'V' : 'v',
303 bam_is_connected ? 'U' : 'u',
304 wait_for_ack ? 'W' : 'w',
305 ul_wakeup_ack_completion.done ? 'A' : 'a'
306 );
307
308 va_start(arg_list, fmt);
309 len += vscnprintf(buff + len, sizeof(buff) - len, fmt, arg_list);
310 va_end(arg_list);
311 memset(buff + len, 0x0, sizeof(buff) - len);
312
313 spin_lock_irqsave(&bam_dmux_logging_spinlock, flags);
314 if (kfifo_avail(&bam_dmux_state_log) < LOG_MESSAGE_MAX_SIZE) {
315 char junk[LOG_MESSAGE_MAX_SIZE];
316 int ret;
317
318 ret = kfifo_out(&bam_dmux_state_log, junk, sizeof(junk));
319 if (ret != LOG_MESSAGE_MAX_SIZE) {
320 pr_err("%s: unable to empty log %d\n", __func__, ret);
321 spin_unlock_irqrestore(&bam_dmux_logging_spinlock,
322 flags);
323 return;
324 }
325 }
326 kfifo_in(&bam_dmux_state_log, buff, sizeof(buff));
327 spin_unlock_irqrestore(&bam_dmux_logging_spinlock, flags);
328}
329
330static inline void set_tx_timestamp(struct tx_pkt_info *pkt)
331{
332 unsigned long long t_now;
333
334 t_now = sched_clock();
335 pkt->ts_nsec = do_div(t_now, 1000000000U);
336 pkt->ts_sec = (unsigned)t_now;
337}
338
339static inline void verify_tx_queue_is_empty(const char *func)
340{
341 unsigned long flags;
342 struct tx_pkt_info *info;
343 int reported = 0;
344
345 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
346 list_for_each_entry(info, &bam_tx_pool, list_node) {
347 if (!reported) {
Eric Holmberg454d9da2012-01-12 09:37:14 -0700348 bam_dmux_log("%s: tx pool not empty\n", func);
349 if (!in_global_reset)
350 pr_err("%s: tx pool not empty\n", func);
Eric Holmberg878923a2012-01-10 14:28:19 -0700351 reported = 1;
352 }
Eric Holmberg454d9da2012-01-12 09:37:14 -0700353 bam_dmux_log("%s: node=%p ts=%u.%09lu\n", __func__,
354 &info->list_node, info->ts_sec, info->ts_nsec);
355 if (!in_global_reset)
356 pr_err("%s: node=%p ts=%u.%09lu\n", __func__,
357 &info->list_node, info->ts_sec, info->ts_nsec);
Eric Holmberg878923a2012-01-10 14:28:19 -0700358 }
359 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
360}
361
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700362static void queue_rx(void)
363{
364 void *ptr;
365 struct rx_pkt_info *info;
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700366 int ret;
367 int rx_len_cached;
Jeff Hugo949080a2011-08-30 11:58:56 -0600368
Jeff Hugoc9749932011-11-02 17:50:40 -0600369 mutex_lock(&bam_rx_pool_mutexlock);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700370 rx_len_cached = bam_rx_pool_len;
Jeff Hugoc9749932011-11-02 17:50:40 -0600371 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600372
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700373 while (rx_len_cached < NUM_BUFFERS) {
374 if (in_global_reset)
375 goto fail;
376
377 info = kmalloc(sizeof(struct rx_pkt_info), GFP_KERNEL);
378 if (!info) {
379 pr_err("%s: unable to alloc rx_pkt_info\n", __func__);
380 goto fail;
381 }
382
383 INIT_WORK(&info->work, handle_bam_mux_cmd);
384
385 info->skb = __dev_alloc_skb(BUFFER_SIZE, GFP_KERNEL);
386 if (info->skb == NULL) {
387 DMUX_LOG_KERR("%s: unable to alloc skb\n", __func__);
388 goto fail_info;
389 }
390 ptr = skb_put(info->skb, BUFFER_SIZE);
391
392 info->dma_address = dma_map_single(NULL, ptr, BUFFER_SIZE,
393 DMA_FROM_DEVICE);
394 if (info->dma_address == 0 || info->dma_address == ~0) {
395 DMUX_LOG_KERR("%s: dma_map_single failure %p for %p\n",
396 __func__, (void *)info->dma_address, ptr);
397 goto fail_skb;
398 }
399
400 mutex_lock(&bam_rx_pool_mutexlock);
401 list_add_tail(&info->list_node, &bam_rx_pool);
402 rx_len_cached = ++bam_rx_pool_len;
403 mutex_unlock(&bam_rx_pool_mutexlock);
404
405 ret = sps_transfer_one(bam_rx_pipe, info->dma_address,
406 BUFFER_SIZE, info,
407 SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
408
409 if (ret) {
410 DMUX_LOG_KERR("%s: sps_transfer_one failed %d\n",
411 __func__, ret);
412 goto fail_transfer;
413 }
414 }
415 return;
416
417fail_transfer:
418 mutex_lock(&bam_rx_pool_mutexlock);
419 list_del(&info->list_node);
420 --bam_rx_pool_len;
421 rx_len_cached = bam_rx_pool_len;
422 mutex_unlock(&bam_rx_pool_mutexlock);
423
424 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
425 DMA_FROM_DEVICE);
426
427fail_skb:
428 dev_kfree_skb_any(info->skb);
429
430fail_info:
431 kfree(info);
432
433fail:
434 if (rx_len_cached == 0) {
435 DMUX_LOG_KERR("%s: RX queue failure\n", __func__);
436 in_global_reset = 1;
437 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700438}
439
440static void bam_mux_process_data(struct sk_buff *rx_skb)
441{
442 unsigned long flags;
443 struct bam_mux_hdr *rx_hdr;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600444 unsigned long event_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700445
446 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
447
448 rx_skb->data = (unsigned char *)(rx_hdr + 1);
449 rx_skb->tail = rx_skb->data + rx_hdr->pkt_len;
450 rx_skb->len = rx_hdr->pkt_len;
Jeff Hugoee88f672011-10-04 17:14:52 -0600451 rx_skb->truesize = rx_hdr->pkt_len + sizeof(struct sk_buff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600453 event_data = (unsigned long)(rx_skb);
454
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700455 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600456 if (bam_ch[rx_hdr->ch_id].notify)
457 bam_ch[rx_hdr->ch_id].notify(
458 bam_ch[rx_hdr->ch_id].priv, BAM_DMUX_RECEIVE,
459 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700460 else
461 dev_kfree_skb_any(rx_skb);
462 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
463
464 queue_rx();
465}
466
Eric Holmberg006057d2012-01-11 10:10:42 -0700467static inline void handle_bam_mux_cmd_open(struct bam_mux_hdr *rx_hdr)
468{
469 unsigned long flags;
470 int ret;
471
472 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
473 bam_ch[rx_hdr->ch_id].status |= BAM_CH_REMOTE_OPEN;
474 bam_ch[rx_hdr->ch_id].num_tx_pkts = 0;
475 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
476 queue_rx();
477 ret = platform_device_add(bam_ch[rx_hdr->ch_id].pdev);
478 if (ret)
479 pr_err("%s: platform_device_add() error: %d\n",
480 __func__, ret);
481}
482
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483static void handle_bam_mux_cmd(struct work_struct *work)
484{
485 unsigned long flags;
486 struct bam_mux_hdr *rx_hdr;
487 struct rx_pkt_info *info;
488 struct sk_buff *rx_skb;
489
490 info = container_of(work, struct rx_pkt_info, work);
491 rx_skb = info->skb;
Jeff Hugo949080a2011-08-30 11:58:56 -0600492 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE, DMA_FROM_DEVICE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493 kfree(info);
494
495 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
496
497 DBG_INC_READ_CNT(sizeof(struct bam_mux_hdr));
498 DBG("%s: magic %x reserved %d cmd %d pad %d ch %d len %d\n", __func__,
499 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
500 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
501 if (rx_hdr->magic_num != BAM_MUX_HDR_MAGIC_NO) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700502 DMUX_LOG_KERR("%s: dropping invalid hdr. magic %x"
503 " reserved %d cmd %d"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504 " pad %d ch %d len %d\n", __func__,
505 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
506 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
507 dev_kfree_skb_any(rx_skb);
508 queue_rx();
509 return;
510 }
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700511
512 if (rx_hdr->ch_id >= BAM_DMUX_NUM_CHANNELS) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700513 DMUX_LOG_KERR("%s: dropping invalid LCID %d"
514 " reserved %d cmd %d"
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700515 " pad %d ch %d len %d\n", __func__,
516 rx_hdr->ch_id, rx_hdr->reserved, rx_hdr->cmd,
517 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
518 dev_kfree_skb_any(rx_skb);
519 queue_rx();
520 return;
521 }
522
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700523 switch (rx_hdr->cmd) {
524 case BAM_MUX_HDR_CMD_DATA:
525 DBG_INC_READ_CNT(rx_hdr->pkt_len);
526 bam_mux_process_data(rx_skb);
527 break;
528 case BAM_MUX_HDR_CMD_OPEN:
Eric Holmberg006057d2012-01-11 10:10:42 -0700529 bam_dmux_log("%s: opening cid %d PC enabled\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -0700530 rx_hdr->ch_id);
Eric Holmberg006057d2012-01-11 10:10:42 -0700531 handle_bam_mux_cmd_open(rx_hdr);
532 dev_kfree_skb_any(rx_skb);
533 break;
534 case BAM_MUX_HDR_CMD_OPEN_NO_A2_PC:
535 bam_dmux_log("%s: opening cid %d PC disabled\n", __func__,
536 rx_hdr->ch_id);
537
538 if (!a2_pc_disabled) {
539 a2_pc_disabled = 1;
540 schedule_delayed_work(&ul_timeout_work,
541 msecs_to_jiffies(UL_TIMEOUT_DELAY));
542 }
543
544 handle_bam_mux_cmd_open(rx_hdr);
Eric Holmberge779dba2011-11-04 18:22:01 -0600545 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700546 break;
547 case BAM_MUX_HDR_CMD_CLOSE:
548 /* probably should drop pending write */
Eric Holmberg878923a2012-01-10 14:28:19 -0700549 bam_dmux_log("%s: closing cid %d\n", __func__,
550 rx_hdr->ch_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700551 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
552 bam_ch[rx_hdr->ch_id].status &= ~BAM_CH_REMOTE_OPEN;
553 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554 queue_rx();
Jeff Hugo7960abd2011-08-02 15:39:38 -0600555 platform_device_unregister(bam_ch[rx_hdr->ch_id].pdev);
556 bam_ch[rx_hdr->ch_id].pdev =
557 platform_device_alloc(bam_ch[rx_hdr->ch_id].name, 2);
558 if (!bam_ch[rx_hdr->ch_id].pdev)
559 pr_err("%s: platform_device_alloc failed\n", __func__);
Eric Holmberge779dba2011-11-04 18:22:01 -0600560 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561 break;
562 default:
Eric Holmberg878923a2012-01-10 14:28:19 -0700563 DMUX_LOG_KERR("%s: dropping invalid hdr. magic %x"
564 " reserved %d cmd %d pad %d ch %d len %d\n",
565 __func__, rx_hdr->magic_num, rx_hdr->reserved,
566 rx_hdr->cmd, rx_hdr->pad_len, rx_hdr->ch_id,
567 rx_hdr->pkt_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568 dev_kfree_skb_any(rx_skb);
569 queue_rx();
570 return;
571 }
572}
573
574static int bam_mux_write_cmd(void *data, uint32_t len)
575{
576 int rc;
577 struct tx_pkt_info *pkt;
578 dma_addr_t dma_address;
Jeff Hugo626303bf2011-11-21 11:43:28 -0700579 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580
Eric Holmbergd83cd2b2011-11-04 15:54:17 -0600581 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582 if (pkt == NULL) {
583 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
584 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585 return rc;
586 }
587
588 dma_address = dma_map_single(NULL, data, len,
589 DMA_TO_DEVICE);
590 if (!dma_address) {
591 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugo96cb7482011-12-07 13:28:31 -0700592 kfree(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594 return rc;
595 }
596 pkt->skb = (struct sk_buff *)(data);
597 pkt->len = len;
598 pkt->dma_address = dma_address;
599 pkt->is_cmd = 1;
Eric Holmberg878923a2012-01-10 14:28:19 -0700600 set_tx_timestamp(pkt);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600601 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700602 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600603 list_add_tail(&pkt->list_node, &bam_tx_pool);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604 rc = sps_transfer_one(bam_tx_pipe, dma_address, len,
605 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600606 if (rc) {
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700607 DMUX_LOG_KERR("%s sps_transfer_one failed rc=%d\n",
608 __func__, rc);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600609 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700610 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700611 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700612 dma_unmap_single(NULL, pkt->dma_address,
613 pkt->len,
614 DMA_TO_DEVICE);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600615 kfree(pkt);
Jeff Hugobb6da952012-01-16 15:02:42 -0700616 } else {
617 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600618 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600620 ul_packet_written = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700621 return rc;
622}
623
624static void bam_mux_write_done(struct work_struct *work)
625{
626 struct sk_buff *skb;
627 struct bam_mux_hdr *hdr;
628 struct tx_pkt_info *info;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700629 struct tx_pkt_info *info_expected;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600630 unsigned long event_data;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700631 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600633 if (in_global_reset)
634 return;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700635
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700636 info = container_of(work, struct tx_pkt_info, work);
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700637
638 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
639 info_expected = list_first_entry(&bam_tx_pool,
640 struct tx_pkt_info, list_node);
641 if (unlikely(info != info_expected)) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700642 struct tx_pkt_info *errant_pkt;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700643
Eric Holmberg878923a2012-01-10 14:28:19 -0700644 DMUX_LOG_KERR("%s: bam_tx_pool mismatch .next=%p,"
645 " list_node=%p, ts=%u.%09lu\n",
646 __func__, bam_tx_pool.next, &info->list_node,
647 info->ts_sec, info->ts_nsec
648 );
649
650 list_for_each_entry(errant_pkt, &bam_tx_pool, list_node) {
651 DMUX_LOG_KERR("%s: node=%p ts=%u.%09lu\n", __func__,
652 &errant_pkt->list_node, errant_pkt->ts_sec,
653 errant_pkt->ts_nsec);
654
655 }
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700656 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
657 BUG();
658 }
659 list_del(&info->list_node);
660 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
661
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600662 if (info->is_cmd) {
663 kfree(info->skb);
664 kfree(info);
665 return;
666 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700667 skb = info->skb;
668 kfree(info);
669 hdr = (struct bam_mux_hdr *)skb->data;
670 DBG_INC_WRITE_CNT(skb->data_len);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600671 event_data = (unsigned long)(skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700672 spin_lock_irqsave(&bam_ch[hdr->ch_id].lock, flags);
673 bam_ch[hdr->ch_id].num_tx_pkts--;
674 spin_unlock_irqrestore(&bam_ch[hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600675 if (bam_ch[hdr->ch_id].notify)
676 bam_ch[hdr->ch_id].notify(
677 bam_ch[hdr->ch_id].priv, BAM_DMUX_WRITE_DONE,
678 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700679 else
680 dev_kfree_skb_any(skb);
681}
682
683int msm_bam_dmux_write(uint32_t id, struct sk_buff *skb)
684{
685 int rc = 0;
686 struct bam_mux_hdr *hdr;
687 unsigned long flags;
688 struct sk_buff *new_skb = NULL;
689 dma_addr_t dma_address;
690 struct tx_pkt_info *pkt;
691
692 if (id >= BAM_DMUX_NUM_CHANNELS)
693 return -EINVAL;
694 if (!skb)
695 return -EINVAL;
696 if (!bam_mux_initialized)
697 return -ENODEV;
698
699 DBG("%s: writing to ch %d len %d\n", __func__, id, skb->len);
700 spin_lock_irqsave(&bam_ch[id].lock, flags);
701 if (!bam_ch_is_open(id)) {
702 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
703 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
704 return -ENODEV;
705 }
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700706
707 if (bam_ch[id].use_wm &&
708 (bam_ch[id].num_tx_pkts >= HIGH_WATERMARK)) {
709 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
710 pr_err("%s: watermark exceeded: %d\n", __func__, id);
711 return -EAGAIN;
712 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700713 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
714
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600715 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600716 if (!bam_is_connected) {
717 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600718 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -0700719 if (unlikely(in_global_reset == 1))
720 return -EFAULT;
Jeff Hugo061ce672011-10-21 17:15:32 -0600721 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600722 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600723 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600724
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725 /* if skb do not have any tailroom for padding,
726 copy the skb into a new expanded skb */
727 if ((skb->len & 0x3) && (skb_tailroom(skb) < (4 - (skb->len & 0x3)))) {
728 /* revisit, probably dev_alloc_skb and memcpy is effecient */
729 new_skb = skb_copy_expand(skb, skb_headroom(skb),
730 4 - (skb->len & 0x3), GFP_ATOMIC);
731 if (new_skb == NULL) {
732 pr_err("%s: cannot allocate skb\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600733 goto write_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700734 }
735 dev_kfree_skb_any(skb);
736 skb = new_skb;
737 DBG_INC_WRITE_CPY(skb->len);
738 }
739
740 hdr = (struct bam_mux_hdr *)skb_push(skb, sizeof(struct bam_mux_hdr));
741
742 /* caller should allocate for hdr and padding
743 hdr is fine, padding is tricky */
744 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
745 hdr->cmd = BAM_MUX_HDR_CMD_DATA;
746 hdr->reserved = 0;
747 hdr->ch_id = id;
748 hdr->pkt_len = skb->len - sizeof(struct bam_mux_hdr);
749 if (skb->len & 0x3)
750 skb_put(skb, 4 - (skb->len & 0x3));
751
752 hdr->pad_len = skb->len - (sizeof(struct bam_mux_hdr) + hdr->pkt_len);
753
754 DBG("%s: data %p, tail %p skb len %d pkt len %d pad len %d\n",
755 __func__, skb->data, skb->tail, skb->len,
756 hdr->pkt_len, hdr->pad_len);
757
758 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
759 if (pkt == NULL) {
760 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600761 goto write_fail2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700762 }
763
764 dma_address = dma_map_single(NULL, skb->data, skb->len,
765 DMA_TO_DEVICE);
766 if (!dma_address) {
767 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600768 goto write_fail3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700769 }
770 pkt->skb = skb;
771 pkt->dma_address = dma_address;
772 pkt->is_cmd = 0;
Eric Holmberg878923a2012-01-10 14:28:19 -0700773 set_tx_timestamp(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700775 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600776 list_add_tail(&pkt->list_node, &bam_tx_pool);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700777 rc = sps_transfer_one(bam_tx_pipe, dma_address, skb->len,
778 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600779 if (rc) {
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700780 DMUX_LOG_KERR("%s sps_transfer_one failed rc=%d\n",
781 __func__, rc);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600782 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700783 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700784 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700785 dma_unmap_single(NULL, pkt->dma_address,
786 pkt->skb->len, DMA_TO_DEVICE);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600787 kfree(pkt);
Jeff Hugo872bd062011-11-15 17:47:21 -0700788 if (new_skb)
789 dev_kfree_skb_any(new_skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700790 } else {
Jeff Hugobb6da952012-01-16 15:02:42 -0700791 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700792 spin_lock_irqsave(&bam_ch[id].lock, flags);
793 bam_ch[id].num_tx_pkts++;
794 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600795 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600796 ul_packet_written = 1;
797 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798 return rc;
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600799
800write_fail3:
801 kfree(pkt);
802write_fail2:
803 if (new_skb)
804 dev_kfree_skb_any(new_skb);
805write_fail:
806 read_unlock(&ul_wakeup_lock);
807 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700808}
809
810int msm_bam_dmux_open(uint32_t id, void *priv,
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600811 void (*notify)(void *, int, unsigned long))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700812{
813 struct bam_mux_hdr *hdr;
814 unsigned long flags;
815 int rc = 0;
816
817 DBG("%s: opening ch %d\n", __func__, id);
Eric Holmberg5d775432011-11-09 10:23:35 -0700818 if (!bam_mux_initialized) {
819 DBG("%s: not inititialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700820 return -ENODEV;
Eric Holmberg5d775432011-11-09 10:23:35 -0700821 }
822 if (id >= BAM_DMUX_NUM_CHANNELS) {
823 pr_err("%s: invalid channel id %d\n", __func__, id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700824 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700825 }
826 if (notify == NULL) {
827 pr_err("%s: notify function is NULL\n", __func__);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600828 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700829 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700830
831 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL);
832 if (hdr == NULL) {
833 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
834 return -ENOMEM;
835 }
836 spin_lock_irqsave(&bam_ch[id].lock, flags);
837 if (bam_ch_is_open(id)) {
838 DBG("%s: Already opened %d\n", __func__, id);
839 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
840 kfree(hdr);
841 goto open_done;
842 }
843 if (!bam_ch_is_remote_open(id)) {
844 DBG("%s: Remote not open; ch: %d\n", __func__, id);
845 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
846 kfree(hdr);
Eric Holmberg5d775432011-11-09 10:23:35 -0700847 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700848 }
849
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600850 bam_ch[id].notify = notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700851 bam_ch[id].priv = priv;
852 bam_ch[id].status |= BAM_CH_LOCAL_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700853 bam_ch[id].num_tx_pkts = 0;
854 bam_ch[id].use_wm = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
856
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600857 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600858 if (!bam_is_connected) {
859 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600860 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -0700861 if (unlikely(in_global_reset == 1))
862 return -EFAULT;
Jeff Hugo061ce672011-10-21 17:15:32 -0600863 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600864 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600865 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600866
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700867 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
868 hdr->cmd = BAM_MUX_HDR_CMD_OPEN;
869 hdr->reserved = 0;
870 hdr->ch_id = id;
871 hdr->pkt_len = 0;
872 hdr->pad_len = 0;
873
874 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600875 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700876
877open_done:
878 DBG("%s: opened ch %d\n", __func__, id);
879 return rc;
880}
881
882int msm_bam_dmux_close(uint32_t id)
883{
884 struct bam_mux_hdr *hdr;
885 unsigned long flags;
886 int rc;
887
888 if (id >= BAM_DMUX_NUM_CHANNELS)
889 return -EINVAL;
890 DBG("%s: closing ch %d\n", __func__, id);
891 if (!bam_mux_initialized)
892 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700893
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600894 read_lock(&ul_wakeup_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600895 if (!bam_is_connected && !bam_ch_is_in_reset(id)) {
Jeff Hugo061ce672011-10-21 17:15:32 -0600896 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600897 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -0700898 if (unlikely(in_global_reset == 1))
899 return -EFAULT;
Jeff Hugo061ce672011-10-21 17:15:32 -0600900 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600901 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600902 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600903
Jeff Hugo061ce672011-10-21 17:15:32 -0600904 spin_lock_irqsave(&bam_ch[id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600905 bam_ch[id].notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700906 bam_ch[id].priv = NULL;
907 bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN;
908 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
909
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600910 if (bam_ch_is_in_reset(id)) {
911 read_unlock(&ul_wakeup_lock);
912 bam_ch[id].status &= ~BAM_CH_IN_RESET;
913 return 0;
914 }
915
Jeff Hugobb5802f2011-11-02 17:10:29 -0600916 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700917 if (hdr == NULL) {
918 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600919 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700920 return -ENOMEM;
921 }
922 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
923 hdr->cmd = BAM_MUX_HDR_CMD_CLOSE;
924 hdr->reserved = 0;
925 hdr->ch_id = id;
926 hdr->pkt_len = 0;
927 hdr->pad_len = 0;
928
929 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600930 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700931
932 DBG("%s: closed ch %d\n", __func__, id);
933 return rc;
934}
935
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700936int msm_bam_dmux_is_ch_full(uint32_t id)
937{
938 unsigned long flags;
939 int ret;
940
941 if (id >= BAM_DMUX_NUM_CHANNELS)
942 return -EINVAL;
943
944 spin_lock_irqsave(&bam_ch[id].lock, flags);
945 bam_ch[id].use_wm = 1;
946 ret = bam_ch[id].num_tx_pkts >= HIGH_WATERMARK;
947 DBG("%s: ch %d num tx pkts=%d, HWM=%d\n", __func__,
948 id, bam_ch[id].num_tx_pkts, ret);
949 if (!bam_ch_is_local_open(id)) {
950 ret = -ENODEV;
951 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
952 }
953 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
954
955 return ret;
956}
957
958int msm_bam_dmux_is_ch_low(uint32_t id)
959{
960 int ret;
961
962 if (id >= BAM_DMUX_NUM_CHANNELS)
963 return -EINVAL;
964
965 bam_ch[id].use_wm = 1;
966 ret = bam_ch[id].num_tx_pkts <= LOW_WATERMARK;
967 DBG("%s: ch %d num tx pkts=%d, LWM=%d\n", __func__,
968 id, bam_ch[id].num_tx_pkts, ret);
969 if (!bam_ch_is_local_open(id)) {
970 ret = -ENODEV;
971 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
972 }
973
974 return ret;
975}
976
Eric Holmberg8df0cdb2012-01-04 17:40:46 -0700977static void rx_switch_to_interrupt_mode(void)
978{
979 struct sps_connect cur_rx_conn;
980 struct sps_iovec iov;
981 struct rx_pkt_info *info;
982 int ret;
983
984 /*
985 * Attempt to enable interrupts - if this fails,
986 * continue polling and we will retry later.
987 */
988 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
989 if (ret) {
990 pr_err("%s: sps_get_config() failed %d\n", __func__, ret);
991 goto fail;
992 }
993
994 rx_register_event.options = SPS_O_EOT;
995 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
996 if (ret) {
997 pr_err("%s: sps_register_event() failed %d\n", __func__, ret);
998 goto fail;
999 }
1000
1001 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
1002 SPS_O_EOT | SPS_O_ACK_TRANSFERS;
1003 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
1004 if (ret) {
1005 pr_err("%s: sps_set_config() failed %d\n", __func__, ret);
1006 goto fail;
1007 }
1008 polling_mode = 0;
Eric Holmberg006057d2012-01-11 10:10:42 -07001009 release_wakelock();
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001010
1011 /* handle any rx packets before interrupt was enabled */
1012 while (bam_connection_is_active && !polling_mode) {
1013 ret = sps_get_iovec(bam_rx_pipe, &iov);
1014 if (ret) {
1015 pr_err("%s: sps_get_iovec failed %d\n",
1016 __func__, ret);
1017 break;
1018 }
1019 if (iov.addr == 0)
1020 break;
1021
1022 mutex_lock(&bam_rx_pool_mutexlock);
1023 if (unlikely(list_empty(&bam_rx_pool))) {
1024 mutex_unlock(&bam_rx_pool_mutexlock);
1025 continue;
1026 }
1027 info = list_first_entry(&bam_rx_pool, struct rx_pkt_info,
1028 list_node);
1029 list_del(&info->list_node);
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001030 --bam_rx_pool_len;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001031 mutex_unlock(&bam_rx_pool_mutexlock);
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001032 if (info->dma_address != iov.addr)
1033 DMUX_LOG_KERR("%s: iovec %p != dma %p\n",
1034 __func__,
1035 (void *)info->dma_address, (void *)iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001036 handle_bam_mux_cmd(&info->work);
1037 }
1038 return;
1039
1040fail:
1041 pr_err("%s: reverting to polling\n", __func__);
1042 queue_work(bam_mux_rx_workqueue, &rx_timer_work);
1043}
1044
Jeff Hugo949080a2011-08-30 11:58:56 -06001045static void rx_timer_work_func(struct work_struct *work)
1046{
1047 struct sps_iovec iov;
Jeff Hugo949080a2011-08-30 11:58:56 -06001048 struct rx_pkt_info *info;
1049 int inactive_cycles = 0;
1050 int ret;
Jeff Hugo949080a2011-08-30 11:58:56 -06001051
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001052 while (bam_connection_is_active) { /* timer loop */
Jeff Hugo949080a2011-08-30 11:58:56 -06001053 ++inactive_cycles;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001054 while (bam_connection_is_active) { /* deplete queue loop */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001055 if (in_global_reset)
1056 return;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001057
1058 ret = sps_get_iovec(bam_rx_pipe, &iov);
1059 if (ret) {
1060 pr_err("%s: sps_get_iovec failed %d\n",
1061 __func__, ret);
1062 break;
1063 }
Jeff Hugo949080a2011-08-30 11:58:56 -06001064 if (iov.addr == 0)
1065 break;
1066 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -06001067 mutex_lock(&bam_rx_pool_mutexlock);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001068 if (unlikely(list_empty(&bam_rx_pool))) {
1069 mutex_unlock(&bam_rx_pool_mutexlock);
1070 continue;
1071 }
1072 info = list_first_entry(&bam_rx_pool,
1073 struct rx_pkt_info, list_node);
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001074 --bam_rx_pool_len;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001075 list_del(&info->list_node);
Jeff Hugoc9749932011-11-02 17:50:40 -06001076 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -06001077 handle_bam_mux_cmd(&info->work);
1078 }
1079
1080 if (inactive_cycles == POLLING_INACTIVITY) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001081 rx_switch_to_interrupt_mode();
1082 break;
Jeff Hugo949080a2011-08-30 11:58:56 -06001083 }
1084
1085 usleep_range(POLLING_MIN_SLEEP, POLLING_MAX_SLEEP);
1086 }
1087}
1088
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001089static void bam_mux_tx_notify(struct sps_event_notify *notify)
1090{
1091 struct tx_pkt_info *pkt;
1092
1093 DBG("%s: event %d notified\n", __func__, notify->event_id);
1094
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001095 if (in_global_reset)
1096 return;
1097
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001098 switch (notify->event_id) {
1099 case SPS_EVENT_EOT:
1100 pkt = notify->data.transfer.user;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001101 if (!pkt->is_cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001102 dma_unmap_single(NULL, pkt->dma_address,
1103 pkt->skb->len,
1104 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001105 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001106 dma_unmap_single(NULL, pkt->dma_address,
1107 pkt->len,
1108 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001109 queue_work(bam_mux_tx_workqueue, &pkt->work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001110 break;
1111 default:
1112 pr_err("%s: recieved unexpected event id %d\n", __func__,
1113 notify->event_id);
1114 }
1115}
1116
Jeff Hugo33dbc002011-08-25 15:52:53 -06001117static void bam_mux_rx_notify(struct sps_event_notify *notify)
1118{
Jeff Hugo949080a2011-08-30 11:58:56 -06001119 int ret;
1120 struct sps_connect cur_rx_conn;
Jeff Hugo33dbc002011-08-25 15:52:53 -06001121
1122 DBG("%s: event %d notified\n", __func__, notify->event_id);
1123
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001124 if (in_global_reset)
1125 return;
1126
Jeff Hugo33dbc002011-08-25 15:52:53 -06001127 switch (notify->event_id) {
1128 case SPS_EVENT_EOT:
Jeff Hugo949080a2011-08-30 11:58:56 -06001129 /* attempt to disable interrupts in this pipe */
1130 if (!polling_mode) {
1131 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
1132 if (ret) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001133 pr_err("%s: sps_get_config() failed %d, interrupts"
1134 " not disabled\n", __func__, ret);
Jeff Hugo949080a2011-08-30 11:58:56 -06001135 break;
1136 }
Jeff Hugoa9d32ba2011-11-21 14:59:48 -07001137 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
Jeff Hugo949080a2011-08-30 11:58:56 -06001138 SPS_O_ACK_TRANSFERS | SPS_O_POLL;
1139 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
1140 if (ret) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001141 pr_err("%s: sps_set_config() failed %d, interrupts"
1142 " not disabled\n", __func__, ret);
Jeff Hugo949080a2011-08-30 11:58:56 -06001143 break;
1144 }
Eric Holmberg006057d2012-01-11 10:10:42 -07001145 grab_wakelock();
Jeff Hugo949080a2011-08-30 11:58:56 -06001146 polling_mode = 1;
1147 queue_work(bam_mux_rx_workqueue, &rx_timer_work);
1148 }
Jeff Hugo33dbc002011-08-25 15:52:53 -06001149 break;
1150 default:
1151 pr_err("%s: recieved unexpected event id %d\n", __func__,
1152 notify->event_id);
1153 }
1154}
1155
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001156#ifdef CONFIG_DEBUG_FS
1157
1158static int debug_tbl(char *buf, int max)
1159{
1160 int i = 0;
1161 int j;
1162
1163 for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) {
1164 i += scnprintf(buf + i, max - i,
1165 "ch%02d local open=%s remote open=%s\n",
1166 j, bam_ch_is_local_open(j) ? "Y" : "N",
1167 bam_ch_is_remote_open(j) ? "Y" : "N");
1168 }
1169
1170 return i;
1171}
1172
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001173static int debug_ul_pkt_cnt(char *buf, int max)
1174{
1175 struct list_head *p;
1176 unsigned long flags;
1177 int n = 0;
1178
1179 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
1180 __list_for_each(p, &bam_tx_pool) {
1181 ++n;
1182 }
1183 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
1184
1185 return scnprintf(buf, max, "Number of UL packets in flight: %d\n", n);
1186}
1187
1188static int debug_stats(char *buf, int max)
1189{
1190 int i = 0;
1191
1192 i += scnprintf(buf + i, max - i,
1193 "skb copy cnt: %u\n"
1194 "skb copy bytes: %u\n"
Eric Holmberg6074aba2012-01-18 17:59:44 -07001195 "sps tx failures: %u\n"
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001196 "sps tx stalls: %u\n"
1197 "rx queue len: %d\n",
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001198 bam_dmux_write_cpy_cnt,
1199 bam_dmux_write_cpy_bytes,
Eric Holmberg6074aba2012-01-18 17:59:44 -07001200 bam_dmux_tx_sps_failure_cnt,
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001201 bam_dmux_tx_stall_cnt,
1202 bam_rx_pool_len
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001203 );
1204
1205 return i;
1206}
1207
Eric Holmberg878923a2012-01-10 14:28:19 -07001208static int debug_log(char *buff, int max, loff_t *ppos)
1209{
1210 unsigned long flags;
1211 int i = 0;
1212
1213 if (bam_dmux_state_logging_disabled) {
1214 i += scnprintf(buff - i, max - i, "Logging disabled\n");
1215 return i;
1216 }
1217
1218 if (*ppos == 0) {
1219 i += scnprintf(buff - i, max - i,
1220 "<DMUX> timestamp FLAGS [Message]\n"
1221 "FLAGS:\n"
Eric Holmberg006057d2012-01-11 10:10:42 -07001222 "\tD: 1 = Power collapse disabled\n"
Eric Holmberg878923a2012-01-10 14:28:19 -07001223 "\tR: 1 = in global reset\n"
1224 "\tP: 1 = BAM is powered up\n"
1225 "\tA: 1 = BAM initialized and ready for data\n"
1226 "\n"
1227 "\tV: 1 = Uplink vote for power\n"
1228 "\tU: 1 = Uplink active\n"
1229 "\tW: 1 = Uplink Wait-for-ack\n"
1230 "\tA: 1 = Uplink ACK received\n"
1231 );
1232 buff += i;
1233 }
1234
1235 spin_lock_irqsave(&bam_dmux_logging_spinlock, flags);
1236 while (kfifo_len(&bam_dmux_state_log)
1237 && (i + LOG_MESSAGE_MAX_SIZE) < max) {
1238 int k_len;
1239 k_len = kfifo_out(&bam_dmux_state_log,
1240 buff, LOG_MESSAGE_MAX_SIZE);
1241 if (k_len != LOG_MESSAGE_MAX_SIZE) {
1242 pr_err("%s: retrieve failure %d\n", __func__, k_len);
1243 break;
1244 }
1245
1246 /* keep non-null portion of string and add line break */
1247 k_len = strnlen(buff, LOG_MESSAGE_MAX_SIZE);
1248 buff += k_len;
1249 i += k_len;
1250 if (k_len && *(buff - 1) != '\n') {
1251 *buff++ = '\n';
1252 ++i;
1253 }
1254 }
1255 spin_unlock_irqrestore(&bam_dmux_logging_spinlock, flags);
1256
1257 return i;
1258}
1259
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001260#define DEBUG_BUFMAX 4096
1261static char debug_buffer[DEBUG_BUFMAX];
1262
1263static ssize_t debug_read(struct file *file, char __user *buf,
1264 size_t count, loff_t *ppos)
1265{
1266 int (*fill)(char *buf, int max) = file->private_data;
1267 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
1268 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
1269}
1270
Eric Holmberg878923a2012-01-10 14:28:19 -07001271static ssize_t debug_read_multiple(struct file *file, char __user *buff,
1272 size_t count, loff_t *ppos)
1273{
1274 int (*util_func)(char *buf, int max, loff_t *) = file->private_data;
1275 char *buffer;
1276 int bsize;
1277
1278 buffer = kmalloc(count, GFP_KERNEL);
1279 if (!buffer)
1280 return -ENOMEM;
1281
1282 bsize = util_func(buffer, count, ppos);
1283
1284 if (bsize >= 0) {
1285 if (copy_to_user(buff, buffer, bsize)) {
1286 kfree(buffer);
1287 return -EFAULT;
1288 }
1289 *ppos += bsize;
1290 }
1291 kfree(buffer);
1292 return bsize;
1293}
1294
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001295static int debug_open(struct inode *inode, struct file *file)
1296{
1297 file->private_data = inode->i_private;
1298 return 0;
1299}
1300
1301
1302static const struct file_operations debug_ops = {
1303 .read = debug_read,
1304 .open = debug_open,
1305};
1306
Eric Holmberg878923a2012-01-10 14:28:19 -07001307static const struct file_operations debug_ops_multiple = {
1308 .read = debug_read_multiple,
1309 .open = debug_open,
1310};
1311
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001312static void debug_create(const char *name, mode_t mode,
1313 struct dentry *dent,
1314 int (*fill)(char *buf, int max))
1315{
Eric Holmberge4ac80b2012-01-12 09:21:59 -07001316 struct dentry *file;
1317
1318 file = debugfs_create_file(name, mode, dent, fill, &debug_ops);
1319 if (IS_ERR(file))
1320 pr_err("%s: debugfs create failed %d\n", __func__,
1321 (int)PTR_ERR(file));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001322}
1323
Eric Holmberge4ac80b2012-01-12 09:21:59 -07001324static void debug_create_multiple(const char *name, mode_t mode,
1325 struct dentry *dent,
1326 int (*fill)(char *buf, int max, loff_t *ppos))
1327{
1328 struct dentry *file;
1329
1330 file = debugfs_create_file(name, mode, dent, fill, &debug_ops_multiple);
1331 if (IS_ERR(file))
1332 pr_err("%s: debugfs create failed %d\n", __func__,
1333 (int)PTR_ERR(file));
1334}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001335#endif
1336
Jeff Hugod98b1082011-10-24 10:30:23 -06001337static void notify_all(int event, unsigned long data)
1338{
1339 int i;
1340
1341 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
Eric Holmberg454d9da2012-01-12 09:37:14 -07001342 if (bam_ch_is_open(i)) {
Jeff Hugod98b1082011-10-24 10:30:23 -06001343 bam_ch[i].notify(bam_ch[i].priv, event, data);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001344 bam_dmux_log("%s: cid=%d, event=%d, data=%lu\n",
1345 __func__, i, event, data);
1346 }
Jeff Hugod98b1082011-10-24 10:30:23 -06001347 }
1348}
1349
1350static void kickoff_ul_wakeup_func(struct work_struct *work)
1351{
1352 read_lock(&ul_wakeup_lock);
1353 if (!bam_is_connected) {
1354 read_unlock(&ul_wakeup_lock);
1355 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -07001356 if (unlikely(in_global_reset == 1))
1357 return;
Jeff Hugod98b1082011-10-24 10:30:23 -06001358 read_lock(&ul_wakeup_lock);
1359 ul_packet_written = 1;
1360 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
1361 }
1362 read_unlock(&ul_wakeup_lock);
1363}
1364
1365void msm_bam_dmux_kickoff_ul_wakeup(void)
1366{
1367 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
1368}
1369
Eric Holmberg878923a2012-01-10 14:28:19 -07001370static void power_vote(int vote)
1371{
1372 bam_dmux_log("%s: curr=%d, vote=%d\n", __func__,
1373 bam_dmux_uplink_vote, vote);
1374
1375 if (bam_dmux_uplink_vote == vote)
1376 bam_dmux_log("%s: warning - duplicate power vote\n", __func__);
1377
1378 bam_dmux_uplink_vote = vote;
1379 if (vote)
1380 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_A2_POWER_CONTROL);
1381 else
1382 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1383}
1384
Eric Holmberg454d9da2012-01-12 09:37:14 -07001385/*
1386 * @note: Must be called with ul_wakeup_lock locked.
1387 */
1388static inline void ul_powerdown(void)
1389{
1390 bam_dmux_log("%s: powerdown\n", __func__);
1391 verify_tx_queue_is_empty(__func__);
1392
1393 if (a2_pc_disabled) {
1394 wait_for_dfab = 1;
1395 INIT_COMPLETION(dfab_unvote_completion);
1396 release_wakelock();
1397 } else {
1398 wait_for_ack = 1;
1399 INIT_COMPLETION(ul_wakeup_ack_completion);
1400 power_vote(0);
1401 }
1402 bam_is_connected = 0;
1403 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
1404}
1405
1406static inline void ul_powerdown_finish(void)
1407{
1408 if (a2_pc_disabled && wait_for_dfab) {
1409 unvote_dfab();
1410 complete_all(&dfab_unvote_completion);
1411 wait_for_dfab = 0;
1412 }
1413}
1414
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001415static void ul_timeout(struct work_struct *work)
1416{
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001417 unsigned long flags;
1418 int ret;
1419
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001420 if (in_global_reset)
1421 return;
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001422 ret = write_trylock_irqsave(&ul_wakeup_lock, flags);
1423 if (!ret) { /* failed to grab lock, reschedule and bail */
1424 schedule_delayed_work(&ul_timeout_work,
1425 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1426 return;
1427 }
Eric Holmberg454d9da2012-01-12 09:37:14 -07001428 if (bam_is_connected) {
Eric Holmberg6074aba2012-01-18 17:59:44 -07001429 if (!ul_packet_written) {
1430 spin_lock(&bam_tx_pool_spinlock);
1431 if (!list_empty(&bam_tx_pool)) {
1432 struct tx_pkt_info *info;
1433
1434 info = list_first_entry(&bam_tx_pool,
1435 struct tx_pkt_info, list_node);
1436 DMUX_LOG_KERR("%s: UL delayed ts=%u.%09lu\n",
1437 __func__, info->ts_sec, info->ts_nsec);
1438 DBG_INC_TX_STALL_CNT();
1439 ul_packet_written = 1;
1440 }
1441 spin_unlock(&bam_tx_pool_spinlock);
1442 }
1443
Eric Holmberg454d9da2012-01-12 09:37:14 -07001444 if (ul_packet_written) {
1445 bam_dmux_log("%s: packet written\n", __func__);
1446 ul_packet_written = 0;
1447 schedule_delayed_work(&ul_timeout_work,
1448 msecs_to_jiffies(UL_TIMEOUT_DELAY));
Eric Holmberg006057d2012-01-11 10:10:42 -07001449 } else {
Eric Holmberg454d9da2012-01-12 09:37:14 -07001450 ul_powerdown();
Eric Holmberg006057d2012-01-11 10:10:42 -07001451 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001452 }
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001453 write_unlock_irqrestore(&ul_wakeup_lock, flags);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001454 ul_powerdown_finish();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001455}
Jeff Hugo4838f412012-01-20 11:19:37 -07001456
1457static int ssrestart_check(void)
1458{
1459 /*
1460 * if the restart level is RESET_SOC, SSR is not on
1461 * so the crashed modem will end up crashing the system
1462 * anyways, so use BUG() to report the error
1463 * else prepare for the restart event which should
1464 * happen soon
1465 */
1466 DMUX_LOG_KERR("%s: modem timeout\n", __func__);
1467 if (get_restart_level() <= RESET_SOC) {
1468 BUG();
1469 return 0;
1470 } else {
1471 in_global_reset = 1;
1472 return 1;
1473 }
1474}
1475
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001476static void ul_wakeup(void)
1477{
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001478 int ret;
Eric Holmberg006057d2012-01-11 10:10:42 -07001479 static int called_before;
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001480
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001481 mutex_lock(&wakeup_lock);
1482 if (bam_is_connected) { /* bam got connected before lock grabbed */
Eric Holmberg878923a2012-01-10 14:28:19 -07001483 bam_dmux_log("%s Already awake\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001484 mutex_unlock(&wakeup_lock);
1485 return;
1486 }
Eric Holmberg878923a2012-01-10 14:28:19 -07001487
Eric Holmberg006057d2012-01-11 10:10:42 -07001488 if (a2_pc_disabled) {
1489 /*
1490 * don't grab the wakelock the first time because it is
1491 * already grabbed when a2 powers on
1492 */
1493 if (likely(called_before))
1494 grab_wakelock();
1495 else
1496 called_before = 1;
1497 if (wait_for_dfab) {
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001498 ret = wait_for_completion_timeout(
Eric Holmberg006057d2012-01-11 10:10:42 -07001499 &dfab_unvote_completion, HZ);
1500 BUG_ON(ret == 0);
1501 }
1502 vote_dfab();
1503 schedule_delayed_work(&ul_timeout_work,
1504 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1505 bam_is_connected = 1;
1506 mutex_unlock(&wakeup_lock);
1507 return;
1508 }
1509
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001510 /*
1511 * must wait for the previous power down request to have been acked
1512 * chances are it already came in and this will just fall through
1513 * instead of waiting
1514 */
1515 if (wait_for_ack) {
Eric Holmberg878923a2012-01-10 14:28:19 -07001516 bam_dmux_log("%s waiting for previous ack\n", __func__);
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001517 ret = wait_for_completion_timeout(
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001518 &ul_wakeup_ack_completion, HZ);
Eric Holmberg006057d2012-01-11 10:10:42 -07001519 wait_for_ack = 0;
Jeff Hugo4838f412012-01-20 11:19:37 -07001520 if (unlikely(ret == 0) && ssrestart_check()) {
1521 mutex_unlock(&wakeup_lock);
1522 bam_dmux_log("%s timeout previous ack\n", __func__);
1523 return;
1524 }
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001525 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001526 INIT_COMPLETION(ul_wakeup_ack_completion);
Eric Holmberg878923a2012-01-10 14:28:19 -07001527 power_vote(1);
1528 bam_dmux_log("%s waiting for wakeup ack\n", __func__);
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001529 ret = wait_for_completion_timeout(&ul_wakeup_ack_completion, HZ);
Jeff Hugo4838f412012-01-20 11:19:37 -07001530 if (unlikely(ret == 0) && ssrestart_check()) {
1531 mutex_unlock(&wakeup_lock);
1532 bam_dmux_log("%s timeout wakeup ack\n", __func__);
1533 return;
1534 }
Eric Holmberg878923a2012-01-10 14:28:19 -07001535 bam_dmux_log("%s waiting completion\n", __func__);
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001536 ret = wait_for_completion_timeout(&bam_connection_completion, HZ);
Jeff Hugo4838f412012-01-20 11:19:37 -07001537 if (unlikely(ret == 0) && ssrestart_check()) {
1538 mutex_unlock(&wakeup_lock);
1539 bam_dmux_log("%s timeout power on\n", __func__);
1540 return;
1541 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001542
1543 bam_is_connected = 1;
Eric Holmberg878923a2012-01-10 14:28:19 -07001544 bam_dmux_log("%s complete\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001545 schedule_delayed_work(&ul_timeout_work,
1546 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1547 mutex_unlock(&wakeup_lock);
1548}
1549
1550static void reconnect_to_bam(void)
1551{
1552 int i;
1553
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001554 in_global_reset = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001555 vote_dfab();
1556 i = sps_device_reset(a2_device_handle);
1557 if (i)
1558 pr_err("%s: device reset failed rc = %d\n", __func__, i);
1559 i = sps_connect(bam_tx_pipe, &tx_connection);
1560 if (i)
1561 pr_err("%s: tx connection failed rc = %d\n", __func__, i);
1562 i = sps_connect(bam_rx_pipe, &rx_connection);
1563 if (i)
1564 pr_err("%s: rx connection failed rc = %d\n", __func__, i);
1565 i = sps_register_event(bam_tx_pipe, &tx_register_event);
1566 if (i)
1567 pr_err("%s: tx event reg failed rc = %d\n", __func__, i);
1568 i = sps_register_event(bam_rx_pipe, &rx_register_event);
1569 if (i)
1570 pr_err("%s: rx event reg failed rc = %d\n", __func__, i);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001571
1572 bam_connection_is_active = 1;
1573
1574 if (polling_mode)
1575 rx_switch_to_interrupt_mode();
1576
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001577 queue_rx();
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001578
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001579 toggle_apps_ack();
1580 complete_all(&bam_connection_completion);
1581}
1582
1583static void disconnect_to_bam(void)
1584{
1585 struct list_head *node;
1586 struct rx_pkt_info *info;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001587 unsigned long flags;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001588
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001589 bam_connection_is_active = 0;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001590
1591 /* handle disconnect during active UL */
1592 write_lock_irqsave(&ul_wakeup_lock, flags);
1593 if (bam_is_connected) {
1594 bam_dmux_log("%s: UL active - forcing powerdown\n", __func__);
1595 ul_powerdown();
1596 }
1597 write_unlock_irqrestore(&ul_wakeup_lock, flags);
1598 ul_powerdown_finish();
1599
1600 /* tear down BAM connection */
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001601 INIT_COMPLETION(bam_connection_completion);
1602 sps_disconnect(bam_tx_pipe);
1603 sps_disconnect(bam_rx_pipe);
1604 unvote_dfab();
1605 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
1606 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001607
1608 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001609 while (!list_empty(&bam_rx_pool)) {
1610 node = bam_rx_pool.next;
1611 list_del(node);
1612 info = container_of(node, struct rx_pkt_info, list_node);
1613 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
1614 DMA_FROM_DEVICE);
1615 dev_kfree_skb_any(info->skb);
1616 kfree(info);
1617 }
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001618 bam_rx_pool_len = 0;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001619 mutex_unlock(&bam_rx_pool_mutexlock);
Eric Holmberg878923a2012-01-10 14:28:19 -07001620
1621 verify_tx_queue_is_empty(__func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001622}
1623
1624static void vote_dfab(void)
1625{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001626 int rc;
1627
Eric Holmberg006057d2012-01-11 10:10:42 -07001628 bam_dmux_log("%s\n", __func__);
1629 mutex_lock(&dfab_status_lock);
1630 if (dfab_is_on) {
1631 bam_dmux_log("%s: dfab is already on\n", __func__);
1632 mutex_unlock(&dfab_status_lock);
1633 return;
1634 }
Jeff Hugo23a812b2012-01-13 13:43:42 -07001635 rc = clk_prepare_enable(dfab_clk);
Jeff Hugoca0caa82011-12-05 16:05:23 -07001636 if (rc)
Eric Holmberg006057d2012-01-11 10:10:42 -07001637 DMUX_LOG_KERR("bam_dmux vote for dfab failed rc = %d\n", rc);
1638 dfab_is_on = 1;
1639 mutex_unlock(&dfab_status_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001640}
1641
1642static void unvote_dfab(void)
1643{
Eric Holmberg006057d2012-01-11 10:10:42 -07001644 bam_dmux_log("%s\n", __func__);
1645 mutex_lock(&dfab_status_lock);
1646 if (!dfab_is_on) {
1647 DMUX_LOG_KERR("%s: dfab is already off\n", __func__);
1648 dump_stack();
1649 mutex_unlock(&dfab_status_lock);
1650 return;
1651 }
Jeff Hugo23a812b2012-01-13 13:43:42 -07001652 clk_disable_unprepare(dfab_clk);
Eric Holmberg006057d2012-01-11 10:10:42 -07001653 dfab_is_on = 0;
1654 mutex_unlock(&dfab_status_lock);
1655}
1656
1657/* reference counting wrapper around wakelock */
1658static void grab_wakelock(void)
1659{
1660 unsigned long flags;
1661
1662 spin_lock_irqsave(&wakelock_reference_lock, flags);
1663 bam_dmux_log("%s: ref count = %d\n", __func__,
1664 wakelock_reference_count);
1665 if (wakelock_reference_count == 0)
1666 wake_lock(&bam_wakelock);
1667 ++wakelock_reference_count;
1668 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
1669}
1670
1671static void release_wakelock(void)
1672{
1673 unsigned long flags;
1674
1675 spin_lock_irqsave(&wakelock_reference_lock, flags);
1676 if (wakelock_reference_count == 0) {
1677 DMUX_LOG_KERR("%s: bam_dmux wakelock not locked\n", __func__);
1678 dump_stack();
1679 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
1680 return;
1681 }
1682 bam_dmux_log("%s: ref count = %d\n", __func__,
1683 wakelock_reference_count);
1684 --wakelock_reference_count;
1685 if (wakelock_reference_count == 0)
1686 wake_unlock(&bam_wakelock);
1687 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001688}
1689
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001690static int restart_notifier_cb(struct notifier_block *this,
1691 unsigned long code,
1692 void *data)
1693{
1694 int i;
1695 struct list_head *node;
1696 struct tx_pkt_info *info;
1697 int temp_remote_status;
Jeff Hugo626303bf2011-11-21 11:43:28 -07001698 unsigned long flags;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001699
1700 if (code != SUBSYS_AFTER_SHUTDOWN)
1701 return NOTIFY_DONE;
1702
Eric Holmberg878923a2012-01-10 14:28:19 -07001703 bam_dmux_log("%s: begin\n", __func__);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001704 in_global_reset = 1;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001705
1706 /* Handle uplink Powerdown */
1707 write_lock_irqsave(&ul_wakeup_lock, flags);
1708 if (bam_is_connected) {
1709 ul_powerdown();
1710 wait_for_ack = 0;
1711 }
Jeff Hugo4838f412012-01-20 11:19:37 -07001712 /*
1713 * if modem crash during ul_wakeup(), power_vote is 1, needs to be
1714 * reset to 0. harmless if bam_is_connected check above passes
1715 */
1716 power_vote(0);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001717 write_unlock_irqrestore(&ul_wakeup_lock, flags);
1718 ul_powerdown_finish();
Eric Holmberg006057d2012-01-11 10:10:42 -07001719 a2_pc_disabled = 0;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001720
1721 /* Cleanup Channel States */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001722 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
1723 temp_remote_status = bam_ch_is_remote_open(i);
1724 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001725 bam_ch[i].num_tx_pkts = 0;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001726 if (bam_ch_is_local_open(i))
1727 bam_ch[i].status |= BAM_CH_IN_RESET;
1728 if (temp_remote_status) {
1729 platform_device_unregister(bam_ch[i].pdev);
1730 bam_ch[i].pdev = platform_device_alloc(
1731 bam_ch[i].name, 2);
1732 }
1733 }
Eric Holmberg454d9da2012-01-12 09:37:14 -07001734
1735 /* Cleanup pending UL data */
Jeff Hugo626303bf2011-11-21 11:43:28 -07001736 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001737 while (!list_empty(&bam_tx_pool)) {
1738 node = bam_tx_pool.next;
1739 list_del(node);
1740 info = container_of(node, struct tx_pkt_info,
1741 list_node);
1742 if (!info->is_cmd) {
1743 dma_unmap_single(NULL, info->dma_address,
1744 info->skb->len,
1745 DMA_TO_DEVICE);
1746 dev_kfree_skb_any(info->skb);
1747 } else {
1748 dma_unmap_single(NULL, info->dma_address,
1749 info->len,
1750 DMA_TO_DEVICE);
1751 kfree(info->skb);
1752 }
1753 kfree(info);
1754 }
Jeff Hugo626303bf2011-11-21 11:43:28 -07001755 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001756
Eric Holmberg878923a2012-01-10 14:28:19 -07001757 bam_dmux_log("%s: complete\n", __func__);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001758 return NOTIFY_DONE;
1759}
1760
Jeff Hugo9dea05c2011-12-21 12:23:05 -07001761static int bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001762{
1763 u32 h;
1764 dma_addr_t dma_addr;
1765 int ret;
1766 void *a2_virt_addr;
Jeff Hugo4b2890d2012-01-16 16:14:21 -07001767 int skip_iounmap = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001768
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001769 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001770 /* init BAM */
1771 a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE);
1772 if (!a2_virt_addr) {
1773 pr_err("%s: ioremap failed\n", __func__);
1774 ret = -ENOMEM;
Jeff Hugo994a92d2012-01-05 13:25:21 -07001775 goto ioremap_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001776 }
1777 a2_props.phys_addr = A2_PHYS_BASE;
1778 a2_props.virt_addr = a2_virt_addr;
1779 a2_props.virt_size = A2_PHYS_SIZE;
1780 a2_props.irq = A2_BAM_IRQ;
Jeff Hugo927cba62011-11-11 11:49:52 -07001781 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001782 a2_props.num_pipes = A2_NUM_PIPES;
1783 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
Jeff Hugo75913c82011-12-05 15:59:01 -07001784 if (cpu_is_msm9615())
1785 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001786 /* need to free on tear down */
1787 ret = sps_register_bam_device(&a2_props, &h);
1788 if (ret < 0) {
1789 pr_err("%s: register bam error %d\n", __func__, ret);
1790 goto register_bam_failed;
1791 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001792 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001793
1794 bam_tx_pipe = sps_alloc_endpoint();
1795 if (bam_tx_pipe == NULL) {
1796 pr_err("%s: tx alloc endpoint failed\n", __func__);
1797 ret = -ENOMEM;
1798 goto register_bam_failed;
1799 }
1800 ret = sps_get_config(bam_tx_pipe, &tx_connection);
1801 if (ret) {
1802 pr_err("%s: tx get config failed %d\n", __func__, ret);
1803 goto tx_get_config_failed;
1804 }
1805
1806 tx_connection.source = SPS_DEV_HANDLE_MEM;
1807 tx_connection.src_pipe_index = 0;
1808 tx_connection.destination = h;
1809 tx_connection.dest_pipe_index = 4;
1810 tx_connection.mode = SPS_MODE_DEST;
1811 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
1812 tx_desc_mem_buf.size = 0x800; /* 2k */
1813 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
1814 &dma_addr, 0);
1815 if (tx_desc_mem_buf.base == NULL) {
1816 pr_err("%s: tx memory alloc failed\n", __func__);
1817 ret = -ENOMEM;
1818 goto tx_mem_failed;
1819 }
1820 tx_desc_mem_buf.phys_base = dma_addr;
1821 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
1822 tx_connection.desc = tx_desc_mem_buf;
1823 tx_connection.event_thresh = 0x10;
1824
1825 ret = sps_connect(bam_tx_pipe, &tx_connection);
1826 if (ret < 0) {
1827 pr_err("%s: tx connect error %d\n", __func__, ret);
1828 goto tx_connect_failed;
1829 }
1830
1831 bam_rx_pipe = sps_alloc_endpoint();
1832 if (bam_rx_pipe == NULL) {
1833 pr_err("%s: rx alloc endpoint failed\n", __func__);
1834 ret = -ENOMEM;
1835 goto tx_connect_failed;
1836 }
1837 ret = sps_get_config(bam_rx_pipe, &rx_connection);
1838 if (ret) {
1839 pr_err("%s: rx get config failed %d\n", __func__, ret);
1840 goto rx_get_config_failed;
1841 }
1842
1843 rx_connection.source = h;
1844 rx_connection.src_pipe_index = 5;
1845 rx_connection.destination = SPS_DEV_HANDLE_MEM;
1846 rx_connection.dest_pipe_index = 1;
1847 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06001848 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
1849 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001850 rx_desc_mem_buf.size = 0x800; /* 2k */
1851 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
1852 &dma_addr, 0);
1853 if (rx_desc_mem_buf.base == NULL) {
1854 pr_err("%s: rx memory alloc failed\n", __func__);
1855 ret = -ENOMEM;
1856 goto rx_mem_failed;
1857 }
1858 rx_desc_mem_buf.phys_base = dma_addr;
1859 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
1860 rx_connection.desc = rx_desc_mem_buf;
1861 rx_connection.event_thresh = 0x10;
1862
1863 ret = sps_connect(bam_rx_pipe, &rx_connection);
1864 if (ret < 0) {
1865 pr_err("%s: rx connect error %d\n", __func__, ret);
1866 goto rx_connect_failed;
1867 }
1868
1869 tx_register_event.options = SPS_O_EOT;
1870 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
1871 tx_register_event.xfer_done = NULL;
1872 tx_register_event.callback = bam_mux_tx_notify;
1873 tx_register_event.user = NULL;
1874 ret = sps_register_event(bam_tx_pipe, &tx_register_event);
1875 if (ret < 0) {
1876 pr_err("%s: tx register event error %d\n", __func__, ret);
1877 goto rx_event_reg_failed;
1878 }
1879
Jeff Hugo33dbc002011-08-25 15:52:53 -06001880 rx_register_event.options = SPS_O_EOT;
1881 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
1882 rx_register_event.xfer_done = NULL;
1883 rx_register_event.callback = bam_mux_rx_notify;
1884 rx_register_event.user = NULL;
1885 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1886 if (ret < 0) {
1887 pr_err("%s: tx register event error %d\n", __func__, ret);
1888 goto rx_event_reg_failed;
1889 }
1890
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001891 bam_mux_initialized = 1;
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001892 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001893 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001894 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001895 complete_all(&bam_connection_completion);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07001896 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001897
1898rx_event_reg_failed:
1899 sps_disconnect(bam_rx_pipe);
1900rx_connect_failed:
1901 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
1902 rx_desc_mem_buf.phys_base);
1903rx_mem_failed:
1904 sps_disconnect(bam_tx_pipe);
1905rx_get_config_failed:
1906 sps_free_endpoint(bam_rx_pipe);
1907tx_connect_failed:
1908 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
1909 tx_desc_mem_buf.phys_base);
1910tx_get_config_failed:
1911 sps_free_endpoint(bam_tx_pipe);
1912tx_mem_failed:
1913 sps_deregister_bam_device(h);
Jeff Hugo4b2890d2012-01-16 16:14:21 -07001914 /*
1915 * sps_deregister_bam_device() calls iounmap. calling iounmap on the
1916 * same handle below will cause a crash, so skip it if we've freed
1917 * the handle here.
1918 */
1919 skip_iounmap = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001920register_bam_failed:
Jeff Hugo4b2890d2012-01-16 16:14:21 -07001921 if (!skip_iounmap)
1922 iounmap(a2_virt_addr);
Jeff Hugo994a92d2012-01-05 13:25:21 -07001923ioremap_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001924 /*destroy_workqueue(bam_mux_workqueue);*/
Jeff Hugo9dea05c2011-12-21 12:23:05 -07001925 return ret;
1926}
1927
1928static int bam_init_fallback(void)
1929{
1930 u32 h;
1931 int ret;
1932 void *a2_virt_addr;
1933
1934 unvote_dfab();
1935 /* init BAM */
1936 a2_virt_addr = ioremap_nocache(A2_PHYS_BASE, A2_PHYS_SIZE);
1937 if (!a2_virt_addr) {
1938 pr_err("%s: ioremap failed\n", __func__);
1939 ret = -ENOMEM;
1940 goto ioremap_failed;
1941 }
1942 a2_props.phys_addr = A2_PHYS_BASE;
1943 a2_props.virt_addr = a2_virt_addr;
1944 a2_props.virt_size = A2_PHYS_SIZE;
1945 a2_props.irq = A2_BAM_IRQ;
1946 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
1947 a2_props.num_pipes = A2_NUM_PIPES;
1948 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
1949 if (cpu_is_msm9615())
1950 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
1951 ret = sps_register_bam_device(&a2_props, &h);
1952 if (ret < 0) {
1953 pr_err("%s: register bam error %d\n", __func__, ret);
1954 goto register_bam_failed;
1955 }
1956 a2_device_handle = h;
1957
1958 return 0;
1959
1960register_bam_failed:
Jeff Hugo4b2890d2012-01-16 16:14:21 -07001961 iounmap(a2_virt_addr);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07001962ioremap_failed:
1963 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001964}
Jeff Hugoade1f842011-08-03 15:53:59 -06001965
Eric Holmberg604ab252012-01-15 00:01:18 -07001966static void msm9615_bam_init(struct work_struct *work)
1967{
1968 int ret = 0;
1969
1970 ret = bam_init();
1971 if (ret) {
1972 ret = bam_init_fallback();
1973 if (ret)
1974 pr_err("%s: bam init fallback failed: %d",
1975 __func__, ret);
1976 }
1977}
1978
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001979static void toggle_apps_ack(void)
1980{
1981 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
Eric Holmberg878923a2012-01-10 14:28:19 -07001982
1983 bam_dmux_log("%s: apps ack %d->%d\n", __func__,
1984 clear_bit & 0x1, ~clear_bit & 0x1);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001985 smsm_change_state(SMSM_APPS_STATE,
1986 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
1987 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
1988 clear_bit = ~clear_bit;
1989}
1990
Jeff Hugoade1f842011-08-03 15:53:59 -06001991static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
1992{
Eric Holmberg878923a2012-01-10 14:28:19 -07001993 bam_dmux_power_state = new_state & SMSM_A2_POWER_CONTROL ? 1 : 0;
1994 bam_dmux_log("%s: 0x%08x -> 0x%08x\n", __func__, old_state,
1995 new_state);
1996
Jeff Hugoae3a85e2011-12-02 17:10:18 -07001997 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL) {
Eric Holmberg878923a2012-01-10 14:28:19 -07001998 bam_dmux_log("%s: reconnect\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07001999 grab_wakelock();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002000 reconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002001 } else if (bam_mux_initialized &&
2002 !(new_state & SMSM_A2_POWER_CONTROL)) {
Eric Holmberg878923a2012-01-10 14:28:19 -07002003 bam_dmux_log("%s: disconnect\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002004 disconnect_to_bam();
Eric Holmberg006057d2012-01-11 10:10:42 -07002005 release_wakelock();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002006 } else if (new_state & SMSM_A2_POWER_CONTROL) {
Eric Holmberg878923a2012-01-10 14:28:19 -07002007 bam_dmux_log("%s: init\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07002008 grab_wakelock();
Eric Holmberg604ab252012-01-15 00:01:18 -07002009 if (cpu_is_msm9615()) {
2010 /*
2011 * even though a2 has signaled it is ready via the
2012 * SMSM_A2_POWER_CONTROL bit, it has not yet
2013 * enabled the pipes as needed by sps_connect
2014 * in satallite mode. Add a short delay to give modem
2015 * time to enable the pipes.
2016 */
2017 schedule_delayed_work(&msm9615_bam_init_work,
2018 msecs_to_jiffies(100));
2019 } else {
2020 bam_init();
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002021 }
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002022 } else {
Eric Holmberg878923a2012-01-10 14:28:19 -07002023 bam_dmux_log("%s: bad state change\n", __func__);
Jeff Hugoade1f842011-08-03 15:53:59 -06002024 pr_err("%s: unsupported state change\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002025 }
Jeff Hugoade1f842011-08-03 15:53:59 -06002026
2027}
2028
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002029static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
2030 uint32_t new_state)
2031{
Eric Holmberg878923a2012-01-10 14:28:19 -07002032 bam_dmux_log("%s: 0x%08x -> 0x%08x\n", __func__, old_state,
2033 new_state);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002034 complete_all(&ul_wakeup_ack_completion);
2035}
2036
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002037static int bam_dmux_probe(struct platform_device *pdev)
2038{
2039 int rc;
2040
2041 DBG("%s probe called\n", __func__);
2042 if (bam_mux_initialized)
2043 return 0;
2044
Stephen Boyd1c51a492011-10-26 12:11:47 -07002045 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002046 if (IS_ERR(dfab_clk)) {
2047 pr_err("%s: did not get dfab clock\n", __func__);
2048 return -EFAULT;
2049 }
2050
2051 rc = clk_set_rate(dfab_clk, 64000000);
2052 if (rc)
2053 pr_err("%s: unable to set dfab clock rate\n", __func__);
2054
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002055 bam_mux_rx_workqueue = create_singlethread_workqueue("bam_dmux_rx");
2056 if (!bam_mux_rx_workqueue)
2057 return -ENOMEM;
2058
2059 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
2060 if (!bam_mux_tx_workqueue) {
2061 destroy_workqueue(bam_mux_rx_workqueue);
2062 return -ENOMEM;
2063 }
2064
Jeff Hugo7960abd2011-08-02 15:39:38 -06002065 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002066 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06002067 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
2068 "bam_dmux_ch_%d", rc);
2069 /* bus 2, ie a2 stream 2 */
2070 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
2071 if (!bam_ch[rc].pdev) {
2072 pr_err("%s: platform device alloc failed\n", __func__);
2073 destroy_workqueue(bam_mux_rx_workqueue);
2074 destroy_workqueue(bam_mux_tx_workqueue);
2075 return -ENOMEM;
2076 }
2077 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002078
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002079 init_completion(&ul_wakeup_ack_completion);
2080 init_completion(&bam_connection_completion);
Eric Holmberg006057d2012-01-11 10:10:42 -07002081 init_completion(&dfab_unvote_completion);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002082 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
Eric Holmberg604ab252012-01-15 00:01:18 -07002083 INIT_DELAYED_WORK(&msm9615_bam_init_work, msm9615_bam_init);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002084 wake_lock_init(&bam_wakelock, WAKE_LOCK_SUSPEND, "bam_dmux_wakelock");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002085
Jeff Hugoade1f842011-08-03 15:53:59 -06002086 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL,
2087 bam_dmux_smsm_cb, NULL);
2088
2089 if (rc) {
2090 destroy_workqueue(bam_mux_rx_workqueue);
2091 destroy_workqueue(bam_mux_tx_workqueue);
2092 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
2093 return -ENOMEM;
2094 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002095
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002096 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL_ACK,
2097 bam_dmux_smsm_ack_cb, NULL);
2098
2099 if (rc) {
2100 destroy_workqueue(bam_mux_rx_workqueue);
2101 destroy_workqueue(bam_mux_tx_workqueue);
2102 smsm_state_cb_deregister(SMSM_MODEM_STATE,
2103 SMSM_A2_POWER_CONTROL,
2104 bam_dmux_smsm_cb, NULL);
2105 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
2106 rc);
2107 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
2108 platform_device_put(bam_ch[rc].pdev);
2109 return -ENOMEM;
2110 }
2111
Eric Holmbergfd1e2ae2011-11-15 18:28:17 -07002112 if (smsm_get_state(SMSM_MODEM_STATE) & SMSM_A2_POWER_CONTROL)
2113 bam_dmux_smsm_cb(NULL, 0, smsm_get_state(SMSM_MODEM_STATE));
2114
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002115 return 0;
2116}
2117
2118static struct platform_driver bam_dmux_driver = {
2119 .probe = bam_dmux_probe,
2120 .driver = {
2121 .name = "BAM_RMNT",
2122 .owner = THIS_MODULE,
2123 },
2124};
2125
2126static int __init bam_dmux_init(void)
2127{
Eric Holmberg878923a2012-01-10 14:28:19 -07002128 int ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002129#ifdef CONFIG_DEBUG_FS
2130 struct dentry *dent;
2131
2132 dent = debugfs_create_dir("bam_dmux", 0);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07002133 if (!IS_ERR(dent)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002134 debug_create("tbl", 0444, dent, debug_tbl);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07002135 debug_create("ul_pkt_cnt", 0444, dent, debug_ul_pkt_cnt);
2136 debug_create("stats", 0444, dent, debug_stats);
Eric Holmberge4ac80b2012-01-12 09:21:59 -07002137 debug_create_multiple("log", 0444, dent, debug_log);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07002138 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002139#endif
Eric Holmberg878923a2012-01-10 14:28:19 -07002140 ret = kfifo_alloc(&bam_dmux_state_log, PAGE_SIZE, GFP_KERNEL);
2141 if (ret) {
2142 pr_err("%s: failed to allocate log %d\n", __func__, ret);
2143 bam_dmux_state_logging_disabled = 1;
2144 }
2145
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002146 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002147 return platform_driver_register(&bam_dmux_driver);
2148}
2149
Jeff Hugoade1f842011-08-03 15:53:59 -06002150late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002151MODULE_DESCRIPTION("MSM BAM DMUX");
2152MODULE_LICENSE("GPL v2");