GNU Linux-libre 4.9.284-gnu1
[releases.git] / drivers / staging / most / hdm-dim2 / dim2_hdm.c
1 /*
2  * dim2_hdm.c - MediaLB DIM2 Hardware Dependent Module
3  *
4  * Copyright (C) 2015-2016, Microchip Technology Germany II GmbH & Co. KG
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  * GNU General Public License for more details.
10  *
11  * This file is licensed under GPLv2.
12  */
13
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16 #include <linux/module.h>
17 #include <linux/printk.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/platform_device.h>
21 #include <linux/interrupt.h>
22 #include <linux/slab.h>
23 #include <linux/io.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/sched.h>
26 #include <linux/kthread.h>
27
28 #include <mostcore.h>
29 #include <networking.h>
30 #include "dim2_hal.h"
31 #include "dim2_hdm.h"
32 #include "dim2_errors.h"
33 #include "dim2_sysfs.h"
34
35 #define DMA_CHANNELS (32 - 1)  /* channel 0 is a system channel */
36
37 #define MAX_BUFFERS_PACKET      32
38 #define MAX_BUFFERS_STREAMING   32
39 #define MAX_BUF_SIZE_PACKET     2048
40 #define MAX_BUF_SIZE_STREAMING  (8 * 1024)
41
42 /* command line parameter to select clock speed */
43 static char *clock_speed;
44 module_param(clock_speed, charp, 0);
45 MODULE_PARM_DESC(clock_speed, "MediaLB Clock Speed");
46
47 /*
48  * The parameter representing the number of frames per sub-buffer for
49  * synchronous channels.  Valid values: [0 .. 6].
50  *
51  * The values 0, 1, 2, 3, 4, 5, 6 represent corresponding number of frames per
52  * sub-buffer 1, 2, 4, 8, 16, 32, 64.
53  */
54 static u8 fcnt = 4;  /* (1 << fcnt) frames per subbuffer */
55 module_param(fcnt, byte, 0);
56 MODULE_PARM_DESC(fcnt, "Num of frames per sub-buffer for sync channels as a power of 2");
57
58 static DEFINE_SPINLOCK(dim_lock);
59
60 static void dim2_tasklet_fn(unsigned long data);
61 static DECLARE_TASKLET(dim2_tasklet, dim2_tasklet_fn, 0);
62
63 /**
64  * struct hdm_channel - private structure to keep channel specific data
65  * @is_initialized: identifier to know whether the channel is initialized
66  * @ch: HAL specific channel data
67  * @pending_list: list to keep MBO's before starting transfer
68  * @started_list: list to keep MBO's after starting transfer
69  * @direction: channel direction (TX or RX)
70  * @data_type: channel data type
71  */
72 struct hdm_channel {
73         char name[sizeof "caNNN"];
74         bool is_initialized;
75         struct dim_channel ch;
76         struct list_head pending_list;  /* before dim_enqueue_buffer() */
77         struct list_head started_list;  /* after dim_enqueue_buffer() */
78         enum most_channel_direction direction;
79         enum most_channel_data_type data_type;
80 };
81
82 /**
83  * struct dim2_hdm - private structure to keep interface specific data
84  * @hch: an array of channel specific data
85  * @most_iface: most interface structure
86  * @capabilities: an array of channel capability data
87  * @io_base: I/O register base address
88  * @clk_speed: user selectable (through command line parameter) clock speed
89  * @netinfo_task: thread to deliver network status
90  * @netinfo_waitq: waitq for the thread to sleep
91  * @deliver_netinfo: to identify whether network status received
92  * @mac_addrs: INIC mac address
93  * @link_state: network link state
94  * @atx_idx: index of async tx channel
95  */
96 struct dim2_hdm {
97         struct hdm_channel hch[DMA_CHANNELS];
98         struct most_channel_capability capabilities[DMA_CHANNELS];
99         struct most_interface most_iface;
100         char name[16 + sizeof "dim2-"];
101         void __iomem *io_base;
102         int clk_speed;
103         struct task_struct *netinfo_task;
104         wait_queue_head_t netinfo_waitq;
105         int deliver_netinfo;
106         unsigned char mac_addrs[6];
107         unsigned char link_state;
108         int atx_idx;
109         struct medialb_bus bus;
110 };
111
112 #define iface_to_hdm(iface) container_of(iface, struct dim2_hdm, most_iface)
113
114 /* Macro to identify a network status message */
115 #define PACKET_IS_NET_INFO(p)  \
116         (((p)[1] == 0x18) && ((p)[2] == 0x05) && ((p)[3] == 0x0C) && \
117          ((p)[13] == 0x3C) && ((p)[14] == 0x00) && ((p)[15] == 0x0A))
118
119 bool dim2_sysfs_get_state_cb(void)
120 {
121         bool state;
122         unsigned long flags;
123
124         spin_lock_irqsave(&dim_lock, flags);
125         state = dim_get_lock_state();
126         spin_unlock_irqrestore(&dim_lock, flags);
127
128         return state;
129 }
130
131 /**
132  * dimcb_io_read - callback from HAL to read an I/O register
133  * @ptr32: register address
134  */
135 u32 dimcb_io_read(u32 __iomem *ptr32)
136 {
137         return readl(ptr32);
138 }
139
140 /**
141  * dimcb_io_write - callback from HAL to write value to an I/O register
142  * @ptr32: register address
143  * @value: value to write
144  */
145 void dimcb_io_write(u32 __iomem *ptr32, u32 value)
146 {
147         writel(value, ptr32);
148 }
149
150 /**
151  * dimcb_on_error - callback from HAL to report miscommunication between
152  * HDM and HAL
153  * @error_id: Error ID
154  * @error_message: Error message. Some text in a free format
155  */
156 void dimcb_on_error(u8 error_id, const char *error_message)
157 {
158         pr_err("dimcb_on_error: error_id - %d, error_message - %s\n", error_id,
159                error_message);
160 }
161
162 /**
163  * startup_dim - initialize the dim2 interface
164  * @pdev: platform device
165  *
166  * Get the value of command line parameter "clock_speed" if given or use the
167  * default value, enable the clock and PLL, and initialize the dim2 interface.
168  */
169 static int startup_dim(struct platform_device *pdev)
170 {
171         struct dim2_hdm *dev = platform_get_drvdata(pdev);
172         struct dim2_platform_data *pdata = pdev->dev.platform_data;
173         u8 hal_ret;
174
175         dev->clk_speed = -1;
176
177         if (clock_speed) {
178                 if (!strcmp(clock_speed, "256fs"))
179                         dev->clk_speed = CLK_256FS;
180                 else if (!strcmp(clock_speed, "512fs"))
181                         dev->clk_speed = CLK_512FS;
182                 else if (!strcmp(clock_speed, "1024fs"))
183                         dev->clk_speed = CLK_1024FS;
184                 else if (!strcmp(clock_speed, "2048fs"))
185                         dev->clk_speed = CLK_2048FS;
186                 else if (!strcmp(clock_speed, "3072fs"))
187                         dev->clk_speed = CLK_3072FS;
188                 else if (!strcmp(clock_speed, "4096fs"))
189                         dev->clk_speed = CLK_4096FS;
190                 else if (!strcmp(clock_speed, "6144fs"))
191                         dev->clk_speed = CLK_6144FS;
192                 else if (!strcmp(clock_speed, "8192fs"))
193                         dev->clk_speed = CLK_8192FS;
194         }
195
196         if (dev->clk_speed == -1) {
197                 pr_info("Bad or missing clock speed parameter, using default value: 3072fs\n");
198                 dev->clk_speed = CLK_3072FS;
199         } else {
200                 pr_info("Selected clock speed: %s\n", clock_speed);
201         }
202         if (pdata && pdata->init) {
203                 int ret = pdata->init(pdata, dev->io_base, dev->clk_speed);
204
205                 if (ret)
206                         return ret;
207         }
208
209         pr_info("sync: num of frames per sub-buffer: %u\n", fcnt);
210         hal_ret = dim_startup(dev->io_base, dev->clk_speed, fcnt);
211         if (hal_ret != DIM_NO_ERROR) {
212                 pr_err("dim_startup failed: %d\n", hal_ret);
213                 if (pdata && pdata->destroy)
214                         pdata->destroy(pdata);
215                 return -ENODEV;
216         }
217
218         return 0;
219 }
220
221 /**
222  * try_start_dim_transfer - try to transfer a buffer on a channel
223  * @hdm_ch: channel specific data
224  *
225  * Transfer a buffer from pending_list if the channel is ready
226  */
227 static int try_start_dim_transfer(struct hdm_channel *hdm_ch)
228 {
229         u16 buf_size;
230         struct list_head *head = &hdm_ch->pending_list;
231         struct mbo *mbo;
232         unsigned long flags;
233         struct dim_ch_state_t st;
234
235         BUG_ON(!hdm_ch);
236         BUG_ON(!hdm_ch->is_initialized);
237
238         spin_lock_irqsave(&dim_lock, flags);
239         if (list_empty(head)) {
240                 spin_unlock_irqrestore(&dim_lock, flags);
241                 return -EAGAIN;
242         }
243
244         if (!dim_get_channel_state(&hdm_ch->ch, &st)->ready) {
245                 spin_unlock_irqrestore(&dim_lock, flags);
246                 return -EAGAIN;
247         }
248
249         mbo = list_first_entry(head, struct mbo, list);
250         buf_size = mbo->buffer_length;
251
252         if (dim_dbr_space(&hdm_ch->ch) < buf_size) {
253                 spin_unlock_irqrestore(&dim_lock, flags);
254                 return -EAGAIN;
255         }
256
257         BUG_ON(mbo->bus_address == 0);
258         if (!dim_enqueue_buffer(&hdm_ch->ch, mbo->bus_address, buf_size)) {
259                 list_del(head->next);
260                 spin_unlock_irqrestore(&dim_lock, flags);
261                 mbo->processed_length = 0;
262                 mbo->status = MBO_E_INVAL;
263                 mbo->complete(mbo);
264                 return -EFAULT;
265         }
266
267         list_move_tail(head->next, &hdm_ch->started_list);
268         spin_unlock_irqrestore(&dim_lock, flags);
269
270         return 0;
271 }
272
273 /**
274  * deliver_netinfo_thread - thread to deliver network status to mostcore
275  * @data: private data
276  *
277  * Wait for network status and deliver it to mostcore once it is received
278  */
279 static int deliver_netinfo_thread(void *data)
280 {
281         struct dim2_hdm *dev = data;
282
283         while (!kthread_should_stop()) {
284                 wait_event_interruptible(dev->netinfo_waitq,
285                                          dev->deliver_netinfo ||
286                                          kthread_should_stop());
287
288                 if (dev->deliver_netinfo) {
289                         dev->deliver_netinfo--;
290                         most_deliver_netinfo(&dev->most_iface, dev->link_state,
291                                              dev->mac_addrs);
292                 }
293         }
294
295         return 0;
296 }
297
298 /**
299  * retrieve_netinfo - retrieve network status from received buffer
300  * @dev: private data
301  * @mbo: received MBO
302  *
303  * Parse the message in buffer and get node address, link state, MAC address.
304  * Wake up a thread to deliver this status to mostcore
305  */
306 static void retrieve_netinfo(struct dim2_hdm *dev, struct mbo *mbo)
307 {
308         u8 *data = mbo->virt_address;
309         u8 *mac = dev->mac_addrs;
310
311         pr_info("Node Address: 0x%03x\n", (u16)data[16] << 8 | data[17]);
312         dev->link_state = data[18];
313         pr_info("NIState: %d\n", dev->link_state);
314         memcpy(mac, data + 19, 6);
315         pr_info("MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
316                 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
317         dev->deliver_netinfo++;
318         wake_up_interruptible(&dev->netinfo_waitq);
319 }
320
321 /**
322  * service_done_flag - handle completed buffers
323  * @dev: private data
324  * @ch_idx: channel index
325  *
326  * Return back the completed buffers to mostcore, using completion callback
327  */
328 static void service_done_flag(struct dim2_hdm *dev, int ch_idx)
329 {
330         struct hdm_channel *hdm_ch = dev->hch + ch_idx;
331         struct dim_ch_state_t st;
332         struct list_head *head;
333         struct mbo *mbo;
334         int done_buffers;
335         unsigned long flags;
336         u8 *data;
337
338         BUG_ON(!hdm_ch);
339         BUG_ON(!hdm_ch->is_initialized);
340
341         spin_lock_irqsave(&dim_lock, flags);
342
343         done_buffers = dim_get_channel_state(&hdm_ch->ch, &st)->done_buffers;
344         if (!done_buffers) {
345                 spin_unlock_irqrestore(&dim_lock, flags);
346                 return;
347         }
348
349         if (!dim_detach_buffers(&hdm_ch->ch, done_buffers)) {
350                 spin_unlock_irqrestore(&dim_lock, flags);
351                 return;
352         }
353         spin_unlock_irqrestore(&dim_lock, flags);
354
355         head = &hdm_ch->started_list;
356
357         while (done_buffers) {
358                 spin_lock_irqsave(&dim_lock, flags);
359                 if (list_empty(head)) {
360                         spin_unlock_irqrestore(&dim_lock, flags);
361                         pr_crit("hard error: started_mbo list is empty whereas DIM2 has sent buffers\n");
362                         break;
363                 }
364
365                 mbo = list_first_entry(head, struct mbo, list);
366                 list_del(head->next);
367                 spin_unlock_irqrestore(&dim_lock, flags);
368
369                 data = mbo->virt_address;
370
371                 if (hdm_ch->data_type == MOST_CH_ASYNC &&
372                     hdm_ch->direction == MOST_CH_RX &&
373                     PACKET_IS_NET_INFO(data)) {
374                         retrieve_netinfo(dev, mbo);
375
376                         spin_lock_irqsave(&dim_lock, flags);
377                         list_add_tail(&mbo->list, &hdm_ch->pending_list);
378                         spin_unlock_irqrestore(&dim_lock, flags);
379                 } else {
380                         if (hdm_ch->data_type == MOST_CH_CONTROL ||
381                             hdm_ch->data_type == MOST_CH_ASYNC) {
382                                 u32 const data_size =
383                                         (u32)data[0] * 256 + data[1] + 2;
384
385                                 mbo->processed_length =
386                                         min_t(u32, data_size,
387                                               mbo->buffer_length);
388                         } else {
389                                 mbo->processed_length = mbo->buffer_length;
390                         }
391                         mbo->status = MBO_SUCCESS;
392                         mbo->complete(mbo);
393                 }
394
395                 done_buffers--;
396         }
397 }
398
399 static struct dim_channel **get_active_channels(struct dim2_hdm *dev,
400                                                 struct dim_channel **buffer)
401 {
402         int idx = 0;
403         int ch_idx;
404
405         for (ch_idx = 0; ch_idx < DMA_CHANNELS; ch_idx++) {
406                 if (dev->hch[ch_idx].is_initialized)
407                         buffer[idx++] = &dev->hch[ch_idx].ch;
408         }
409         buffer[idx++] = NULL;
410
411         return buffer;
412 }
413
414 static irqreturn_t dim2_mlb_isr(int irq, void *_dev)
415 {
416         struct dim2_hdm *dev = _dev;
417         unsigned long flags;
418
419         spin_lock_irqsave(&dim_lock, flags);
420         dim_service_mlb_int_irq();
421         spin_unlock_irqrestore(&dim_lock, flags);
422
423         if (dev->atx_idx >= 0 && dev->hch[dev->atx_idx].is_initialized)
424                 while (!try_start_dim_transfer(dev->hch + dev->atx_idx))
425                         continue;
426
427         return IRQ_HANDLED;
428 }
429
430 /**
431  * dim2_tasklet_fn - tasklet function
432  * @data: private data
433  *
434  * Service each initialized channel, if needed
435  */
436 static void dim2_tasklet_fn(unsigned long data)
437 {
438         struct dim2_hdm *dev = (struct dim2_hdm *)data;
439         unsigned long flags;
440         int ch_idx;
441
442         for (ch_idx = 0; ch_idx < DMA_CHANNELS; ch_idx++) {
443                 if (!dev->hch[ch_idx].is_initialized)
444                         continue;
445
446                 spin_lock_irqsave(&dim_lock, flags);
447                 dim_service_channel(&dev->hch[ch_idx].ch);
448                 spin_unlock_irqrestore(&dim_lock, flags);
449
450                 service_done_flag(dev, ch_idx);
451                 while (!try_start_dim_transfer(dev->hch + ch_idx))
452                         continue;
453         }
454 }
455
456 /**
457  * dim2_ahb_isr - interrupt service routine
458  * @irq: irq number
459  * @_dev: private data
460  *
461  * Acknowledge the interrupt and schedule a tasklet to service channels.
462  * Return IRQ_HANDLED.
463  */
464 static irqreturn_t dim2_ahb_isr(int irq, void *_dev)
465 {
466         struct dim2_hdm *dev = _dev;
467         struct dim_channel *buffer[DMA_CHANNELS + 1];
468         unsigned long flags;
469
470         spin_lock_irqsave(&dim_lock, flags);
471         dim_service_ahb_int_irq(get_active_channels(dev, buffer));
472         spin_unlock_irqrestore(&dim_lock, flags);
473
474         dim2_tasklet.data = (unsigned long)dev;
475         tasklet_schedule(&dim2_tasklet);
476         return IRQ_HANDLED;
477 }
478
479 /**
480  * complete_all_mbos - complete MBO's in a list
481  * @head: list head
482  *
483  * Delete all the entries in list and return back MBO's to mostcore using
484  * completion call back.
485  */
486 static void complete_all_mbos(struct list_head *head)
487 {
488         unsigned long flags;
489         struct mbo *mbo;
490
491         for (;;) {
492                 spin_lock_irqsave(&dim_lock, flags);
493                 if (list_empty(head)) {
494                         spin_unlock_irqrestore(&dim_lock, flags);
495                         break;
496                 }
497
498                 mbo = list_first_entry(head, struct mbo, list);
499                 list_del(head->next);
500                 spin_unlock_irqrestore(&dim_lock, flags);
501
502                 mbo->processed_length = 0;
503                 mbo->status = MBO_E_CLOSE;
504                 mbo->complete(mbo);
505         }
506 }
507
508 /**
509  * configure_channel - initialize a channel
510  * @iface: interface the channel belongs to
511  * @channel: channel to be configured
512  * @channel_config: structure that holds the configuration information
513  *
514  * Receives configuration information from mostcore and initialize
515  * the corresponding channel. Return 0 on success, negative on failure.
516  */
517 static int configure_channel(struct most_interface *most_iface, int ch_idx,
518                              struct most_channel_config *ccfg)
519 {
520         struct dim2_hdm *dev = iface_to_hdm(most_iface);
521         bool const is_tx = ccfg->direction == MOST_CH_TX;
522         u16 const sub_size = ccfg->subbuffer_size;
523         u16 const buf_size = ccfg->buffer_size;
524         u16 new_size;
525         unsigned long flags;
526         u8 hal_ret;
527         int const ch_addr = ch_idx * 2 + 2;
528         struct hdm_channel *const hdm_ch = dev->hch + ch_idx;
529
530         BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
531
532         if (hdm_ch->is_initialized)
533                 return -EPERM;
534
535         switch (ccfg->data_type) {
536         case MOST_CH_CONTROL:
537                 new_size = dim_norm_ctrl_async_buffer_size(buf_size);
538                 if (new_size == 0) {
539                         pr_err("%s: too small buffer size\n", hdm_ch->name);
540                         return -EINVAL;
541                 }
542                 ccfg->buffer_size = new_size;
543                 if (new_size != buf_size)
544                         pr_warn("%s: fixed buffer size (%d -> %d)\n",
545                                 hdm_ch->name, buf_size, new_size);
546                 spin_lock_irqsave(&dim_lock, flags);
547                 hal_ret = dim_init_control(&hdm_ch->ch, is_tx, ch_addr,
548                                            is_tx ? new_size * 2 : new_size);
549                 break;
550         case MOST_CH_ASYNC:
551                 new_size = dim_norm_ctrl_async_buffer_size(buf_size);
552                 if (new_size == 0) {
553                         pr_err("%s: too small buffer size\n", hdm_ch->name);
554                         return -EINVAL;
555                 }
556                 ccfg->buffer_size = new_size;
557                 if (new_size != buf_size)
558                         pr_warn("%s: fixed buffer size (%d -> %d)\n",
559                                 hdm_ch->name, buf_size, new_size);
560                 spin_lock_irqsave(&dim_lock, flags);
561                 hal_ret = dim_init_async(&hdm_ch->ch, is_tx, ch_addr,
562                                          is_tx ? new_size * 2 : new_size);
563                 break;
564         case MOST_CH_ISOC:
565                 new_size = dim_norm_isoc_buffer_size(buf_size, sub_size);
566                 if (new_size == 0) {
567                         pr_err("%s: invalid sub-buffer size or too small buffer size\n",
568                                hdm_ch->name);
569                         return -EINVAL;
570                 }
571                 ccfg->buffer_size = new_size;
572                 if (new_size != buf_size)
573                         pr_warn("%s: fixed buffer size (%d -> %d)\n",
574                                 hdm_ch->name, buf_size, new_size);
575                 spin_lock_irqsave(&dim_lock, flags);
576                 hal_ret = dim_init_isoc(&hdm_ch->ch, is_tx, ch_addr, sub_size);
577                 break;
578         case MOST_CH_SYNC:
579                 new_size = dim_norm_sync_buffer_size(buf_size, sub_size);
580                 if (new_size == 0) {
581                         pr_err("%s: invalid sub-buffer size or too small buffer size\n",
582                                hdm_ch->name);
583                         return -EINVAL;
584                 }
585                 ccfg->buffer_size = new_size;
586                 if (new_size != buf_size)
587                         pr_warn("%s: fixed buffer size (%d -> %d)\n",
588                                 hdm_ch->name, buf_size, new_size);
589                 spin_lock_irqsave(&dim_lock, flags);
590                 hal_ret = dim_init_sync(&hdm_ch->ch, is_tx, ch_addr, sub_size);
591                 break;
592         default:
593                 pr_err("%s: configure failed, bad channel type: %d\n",
594                        hdm_ch->name, ccfg->data_type);
595                 return -EINVAL;
596         }
597
598         if (hal_ret != DIM_NO_ERROR) {
599                 spin_unlock_irqrestore(&dim_lock, flags);
600                 pr_err("%s: configure failed (%d), type: %d, is_tx: %d\n",
601                        hdm_ch->name, hal_ret, ccfg->data_type, (int)is_tx);
602                 return -ENODEV;
603         }
604
605         hdm_ch->data_type = ccfg->data_type;
606         hdm_ch->direction = ccfg->direction;
607         hdm_ch->is_initialized = true;
608
609         if (hdm_ch->data_type == MOST_CH_ASYNC &&
610             hdm_ch->direction == MOST_CH_TX &&
611             dev->atx_idx < 0)
612                 dev->atx_idx = ch_idx;
613
614         spin_unlock_irqrestore(&dim_lock, flags);
615
616         return 0;
617 }
618
619 /**
620  * enqueue - enqueue a buffer for data transfer
621  * @iface: intended interface
622  * @channel: ID of the channel the buffer is intended for
623  * @mbo: pointer to the buffer object
624  *
625  * Push the buffer into pending_list and try to transfer one buffer from
626  * pending_list. Return 0 on success, negative on failure.
627  */
628 static int enqueue(struct most_interface *most_iface, int ch_idx,
629                    struct mbo *mbo)
630 {
631         struct dim2_hdm *dev = iface_to_hdm(most_iface);
632         struct hdm_channel *hdm_ch = dev->hch + ch_idx;
633         unsigned long flags;
634
635         BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
636
637         if (!hdm_ch->is_initialized)
638                 return -EPERM;
639
640         if (mbo->bus_address == 0)
641                 return -EFAULT;
642
643         spin_lock_irqsave(&dim_lock, flags);
644         list_add_tail(&mbo->list, &hdm_ch->pending_list);
645         spin_unlock_irqrestore(&dim_lock, flags);
646
647         (void)try_start_dim_transfer(hdm_ch);
648
649         return 0;
650 }
651
652 /**
653  * request_netinfo - triggers retrieving of network info
654  * @iface: pointer to the interface
655  * @channel_id: corresponding channel ID
656  *
657  * Send a command to INIC which triggers retrieving of network info by means of
658  * "Message exchange over MDP/MEP". Return 0 on success, negative on failure.
659  */
660 static void request_netinfo(struct most_interface *most_iface, int ch_idx)
661 {
662         struct dim2_hdm *dev = iface_to_hdm(most_iface);
663         struct mbo *mbo;
664         u8 *data;
665
666         if (dev->atx_idx < 0) {
667                 pr_err("Async Tx Not initialized\n");
668                 return;
669         }
670
671         mbo = most_get_mbo(&dev->most_iface, dev->atx_idx, NULL);
672         if (!mbo)
673                 return;
674
675         mbo->buffer_length = 5;
676
677         data = mbo->virt_address;
678
679         data[0] = 0x00; /* PML High byte */
680         data[1] = 0x03; /* PML Low byte */
681         data[2] = 0x02; /* PMHL */
682         data[3] = 0x08; /* FPH */
683         data[4] = 0x40; /* FMF (FIFO cmd msg - Triggers NAOverMDP) */
684
685         most_submit_mbo(mbo);
686 }
687
688 /**
689  * poison_channel - poison buffers of a channel
690  * @iface: pointer to the interface the channel to be poisoned belongs to
691  * @channel_id: corresponding channel ID
692  *
693  * Destroy a channel and complete all the buffers in both started_list &
694  * pending_list. Return 0 on success, negative on failure.
695  */
696 static int poison_channel(struct most_interface *most_iface, int ch_idx)
697 {
698         struct dim2_hdm *dev = iface_to_hdm(most_iface);
699         struct hdm_channel *hdm_ch = dev->hch + ch_idx;
700         unsigned long flags;
701         u8 hal_ret;
702         int ret = 0;
703
704         BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
705
706         if (!hdm_ch->is_initialized)
707                 return -EPERM;
708
709         tasklet_disable(&dim2_tasklet);
710         spin_lock_irqsave(&dim_lock, flags);
711         hal_ret = dim_destroy_channel(&hdm_ch->ch);
712         hdm_ch->is_initialized = false;
713         if (ch_idx == dev->atx_idx)
714                 dev->atx_idx = -1;
715         spin_unlock_irqrestore(&dim_lock, flags);
716         tasklet_enable(&dim2_tasklet);
717         if (hal_ret != DIM_NO_ERROR) {
718                 pr_err("HAL Failed to close channel %s\n", hdm_ch->name);
719                 ret = -EFAULT;
720         }
721
722         complete_all_mbos(&hdm_ch->started_list);
723         complete_all_mbos(&hdm_ch->pending_list);
724
725         return ret;
726 }
727
728 /*
729  * dim2_probe - dim2 probe handler
730  * @pdev: platform device structure
731  *
732  * Register the dim2 interface with mostcore and initialize it.
733  * Return 0 on success, negative on failure.
734  */
735 static int dim2_probe(struct platform_device *pdev)
736 {
737         struct dim2_hdm *dev;
738         struct resource *res;
739         int ret, i;
740         struct kobject *kobj;
741         int irq;
742
743         dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
744         if (!dev)
745                 return -ENOMEM;
746
747         dev->atx_idx = -1;
748
749         platform_set_drvdata(pdev, dev);
750         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
751         dev->io_base = devm_ioremap_resource(&pdev->dev, res);
752         if (IS_ERR(dev->io_base))
753                 return PTR_ERR(dev->io_base);
754
755         irq = platform_get_irq(pdev, 0);
756         if (irq < 0) {
757                 dev_err(&pdev->dev, "failed to get ahb0_int irq\n");
758                 return -ENODEV;
759         }
760
761         ret = devm_request_irq(&pdev->dev, irq, dim2_ahb_isr, 0,
762                                "dim2_ahb0_int", dev);
763         if (ret) {
764                 dev_err(&pdev->dev, "failed to request ahb0_int irq %d\n", irq);
765                 return ret;
766         }
767
768         irq = platform_get_irq(pdev, 1);
769         if (irq < 0) {
770                 dev_err(&pdev->dev, "failed to get mlb_int irq\n");
771                 return -ENODEV;
772         }
773
774         ret = devm_request_irq(&pdev->dev, irq, dim2_mlb_isr, 0,
775                                "dim2_mlb_int", dev);
776         if (ret) {
777                 dev_err(&pdev->dev, "failed to request mlb_int irq %d\n", irq);
778                 return ret;
779         }
780
781         init_waitqueue_head(&dev->netinfo_waitq);
782         dev->deliver_netinfo = 0;
783         dev->netinfo_task = kthread_run(&deliver_netinfo_thread, (void *)dev,
784                                         "dim2_netinfo");
785         if (IS_ERR(dev->netinfo_task))
786                 return PTR_ERR(dev->netinfo_task);
787
788         for (i = 0; i < DMA_CHANNELS; i++) {
789                 struct most_channel_capability *cap = dev->capabilities + i;
790                 struct hdm_channel *hdm_ch = dev->hch + i;
791
792                 INIT_LIST_HEAD(&hdm_ch->pending_list);
793                 INIT_LIST_HEAD(&hdm_ch->started_list);
794                 hdm_ch->is_initialized = false;
795                 snprintf(hdm_ch->name, sizeof(hdm_ch->name), "ca%d", i * 2 + 2);
796
797                 cap->name_suffix = hdm_ch->name;
798                 cap->direction = MOST_CH_RX | MOST_CH_TX;
799                 cap->data_type = MOST_CH_CONTROL | MOST_CH_ASYNC |
800                                  MOST_CH_ISOC | MOST_CH_SYNC;
801                 cap->num_buffers_packet = MAX_BUFFERS_PACKET;
802                 cap->buffer_size_packet = MAX_BUF_SIZE_PACKET;
803                 cap->num_buffers_streaming = MAX_BUFFERS_STREAMING;
804                 cap->buffer_size_streaming = MAX_BUF_SIZE_STREAMING;
805         }
806
807         {
808                 const char *fmt;
809
810                 if (sizeof(res->start) == sizeof(long long))
811                         fmt = "dim2-%016llx";
812                 else if (sizeof(res->start) == sizeof(long))
813                         fmt = "dim2-%016lx";
814                 else
815                         fmt = "dim2-%016x";
816
817                 snprintf(dev->name, sizeof(dev->name), fmt, res->start);
818         }
819
820         dev->most_iface.interface = ITYPE_MEDIALB_DIM2;
821         dev->most_iface.description = dev->name;
822         dev->most_iface.num_channels = DMA_CHANNELS;
823         dev->most_iface.channel_vector = dev->capabilities;
824         dev->most_iface.configure = configure_channel;
825         dev->most_iface.enqueue = enqueue;
826         dev->most_iface.poison_channel = poison_channel;
827         dev->most_iface.request_netinfo = request_netinfo;
828
829         kobj = most_register_interface(&dev->most_iface);
830         if (IS_ERR(kobj)) {
831                 ret = PTR_ERR(kobj);
832                 dev_err(&pdev->dev, "failed to register MOST interface\n");
833                 goto err_stop_thread;
834         }
835
836         ret = dim2_sysfs_probe(&dev->bus, kobj);
837         if (ret)
838                 goto err_unreg_iface;
839
840         ret = startup_dim(pdev);
841         if (ret) {
842                 dev_err(&pdev->dev, "failed to initialize DIM2\n");
843                 goto err_destroy_bus;
844         }
845
846         return 0;
847
848 err_destroy_bus:
849         dim2_sysfs_destroy(&dev->bus);
850 err_unreg_iface:
851         most_deregister_interface(&dev->most_iface);
852 err_stop_thread:
853         kthread_stop(dev->netinfo_task);
854
855         return ret;
856 }
857
858 /**
859  * dim2_remove - dim2 remove handler
860  * @pdev: platform device structure
861  *
862  * Unregister the interface from mostcore
863  */
864 static int dim2_remove(struct platform_device *pdev)
865 {
866         struct dim2_hdm *dev = platform_get_drvdata(pdev);
867         struct dim2_platform_data *pdata = pdev->dev.platform_data;
868         unsigned long flags;
869
870         spin_lock_irqsave(&dim_lock, flags);
871         dim_shutdown();
872         spin_unlock_irqrestore(&dim_lock, flags);
873
874         if (pdata && pdata->destroy)
875                 pdata->destroy(pdata);
876
877         dim2_sysfs_destroy(&dev->bus);
878         most_deregister_interface(&dev->most_iface);
879         kthread_stop(dev->netinfo_task);
880
881         /*
882          * break link to local platform_device_id struct
883          * to prevent crash by unload platform device module
884          */
885         pdev->id_entry = NULL;
886
887         return 0;
888 }
889
890 static struct platform_device_id dim2_id[] = {
891         { "medialb_dim2" },
892         { }, /* Terminating entry */
893 };
894
895 MODULE_DEVICE_TABLE(platform, dim2_id);
896
897 static struct platform_driver dim2_driver = {
898         .probe = dim2_probe,
899         .remove = dim2_remove,
900         .id_table = dim2_id,
901         .driver = {
902                 .name = "hdm_dim2",
903         },
904 };
905
906 module_platform_driver(dim2_driver);
907
908 MODULE_AUTHOR("Jain Roy Ambi <JainRoy.Ambi@microchip.com>");
909 MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
910 MODULE_DESCRIPTION("MediaLB DIM2 Hardware Dependent Module");
911 MODULE_LICENSE("GPL");