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