forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rio_cm.c
2376 lines (1999 loc) · 53.9 KB
/
rio_cm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* rio_cm - RapidIO Channelized Messaging Driver
*
* Copyright 2013-2016 Integrated Device Technology, Inc.
* Copyright (c) 2015, Prodrive Technologies
* Copyright (c) 2015, RapidIO Trade Association
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/dma-mapping.h>
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/rio.h>
#include <linux/rio_drv.h>
#include <linux/slab.h>
#include <linux/idr.h>
#include <linux/interrupt.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/poll.h>
#include <linux/reboot.h>
#include <linux/bitops.h>
#include <linux/printk.h>
#include <linux/rio_cm_cdev.h>
#define DRV_NAME "rio_cm"
#define DRV_VERSION "1.0.0"
#define DRV_AUTHOR "Alexandre Bounine <[email protected]>"
#define DRV_DESC "RapidIO Channelized Messaging Driver"
#define DEV_NAME "rio_cm"
/* Debug output filtering masks */
enum {
DBG_NONE = 0,
DBG_INIT = BIT(0), /* driver init */
DBG_EXIT = BIT(1), /* driver exit */
DBG_MPORT = BIT(2), /* mport add/remove */
DBG_RDEV = BIT(3), /* RapidIO device add/remove */
DBG_CHOP = BIT(4), /* channel operations */
DBG_WAIT = BIT(5), /* waiting for events */
DBG_TX = BIT(6), /* message TX */
DBG_TX_EVENT = BIT(7), /* message TX event */
DBG_RX_DATA = BIT(8), /* inbound data messages */
DBG_RX_CMD = BIT(9), /* inbound REQ/ACK/NACK messages */
DBG_ALL = ~0,
};
#ifdef DEBUG
#define riocm_debug(level, fmt, arg...) \
do { \
if (DBG_##level & dbg_level) \
pr_debug(DRV_NAME ": %s " fmt "\n", \
__func__, ##arg); \
} while (0)
#else
#define riocm_debug(level, fmt, arg...) \
no_printk(KERN_DEBUG pr_fmt(DRV_NAME fmt "\n"), ##arg)
#endif
#define riocm_warn(fmt, arg...) \
pr_warn(DRV_NAME ": %s WARNING " fmt "\n", __func__, ##arg)
#define riocm_error(fmt, arg...) \
pr_err(DRV_NAME ": %s ERROR " fmt "\n", __func__, ##arg)
static int cmbox = 1;
module_param(cmbox, int, S_IRUGO);
MODULE_PARM_DESC(cmbox, "RapidIO Mailbox number (default 1)");
static int chstart = 256;
module_param(chstart, int, S_IRUGO);
MODULE_PARM_DESC(chstart,
"Start channel number for dynamic allocation (default 256)");
#ifdef DEBUG
static u32 dbg_level = DBG_NONE;
module_param(dbg_level, uint, S_IWUSR | S_IRUGO);
MODULE_PARM_DESC(dbg_level, "Debugging output level (default 0 = none)");
#endif
MODULE_AUTHOR(DRV_AUTHOR);
MODULE_DESCRIPTION(DRV_DESC);
MODULE_LICENSE("GPL");
MODULE_VERSION(DRV_VERSION);
#define RIOCM_TX_RING_SIZE 128
#define RIOCM_RX_RING_SIZE 128
#define RIOCM_CONNECT_TO 3 /* connect response TO (in sec) */
#define RIOCM_MAX_CHNUM 0xffff /* Use full range of u16 field */
#define RIOCM_CHNUM_AUTO 0
#define RIOCM_MAX_EP_COUNT 0x10000 /* Max number of endpoints */
enum rio_cm_state {
RIO_CM_IDLE,
RIO_CM_CONNECT,
RIO_CM_CONNECTED,
RIO_CM_DISCONNECT,
RIO_CM_CHAN_BOUND,
RIO_CM_LISTEN,
RIO_CM_DESTROYING,
};
enum rio_cm_pkt_type {
RIO_CM_SYS = 0xaa,
RIO_CM_CHAN = 0x55,
};
enum rio_cm_chop {
CM_CONN_REQ,
CM_CONN_ACK,
CM_CONN_CLOSE,
CM_DATA_MSG,
};
struct rio_ch_base_bhdr {
u32 src_id;
u32 dst_id;
#define RIO_HDR_LETTER_MASK 0xffff0000
#define RIO_HDR_MBOX_MASK 0x0000ffff
u8 src_mbox;
u8 dst_mbox;
u8 type;
} __attribute__((__packed__));
struct rio_ch_chan_hdr {
struct rio_ch_base_bhdr bhdr;
u8 ch_op;
u16 dst_ch;
u16 src_ch;
u16 msg_len;
u16 rsrvd;
} __attribute__((__packed__));
struct tx_req {
struct list_head node;
struct rio_dev *rdev;
void *buffer;
size_t len;
};
struct cm_dev {
struct list_head list;
struct rio_mport *mport;
void *rx_buf[RIOCM_RX_RING_SIZE];
int rx_slots;
struct mutex rx_lock;
void *tx_buf[RIOCM_TX_RING_SIZE];
int tx_slot;
int tx_cnt;
int tx_ack_slot;
struct list_head tx_reqs;
spinlock_t tx_lock;
struct list_head peers;
u32 npeers;
struct workqueue_struct *rx_wq;
struct work_struct rx_work;
};
struct chan_rx_ring {
void *buf[RIOCM_RX_RING_SIZE];
int head;
int tail;
int count;
/* Tracking RX buffers reported to upper level */
void *inuse[RIOCM_RX_RING_SIZE];
int inuse_cnt;
};
struct rio_channel {
u16 id; /* local channel ID */
struct kref ref; /* channel refcount */
struct file *filp;
struct cm_dev *cmdev; /* associated CM device object */
struct rio_dev *rdev; /* remote RapidIO device */
enum rio_cm_state state;
int error;
spinlock_t lock;
void *context;
u32 loc_destid; /* local destID */
u32 rem_destid; /* remote destID */
u16 rem_channel; /* remote channel ID */
struct list_head accept_queue;
struct list_head ch_node;
struct completion comp;
struct completion comp_close;
struct chan_rx_ring rx_ring;
};
struct cm_peer {
struct list_head node;
struct rio_dev *rdev;
};
struct rio_cm_work {
struct work_struct work;
struct cm_dev *cm;
void *data;
};
struct conn_req {
struct list_head node;
u32 destid; /* requester destID */
u16 chan; /* requester channel ID */
struct cm_dev *cmdev;
};
/*
* A channel_dev structure represents a CM_CDEV
* @cdev Character device
* @dev Associated device object
*/
struct channel_dev {
struct cdev cdev;
struct device *dev;
};
static struct rio_channel *riocm_ch_alloc(u16 ch_num);
static void riocm_ch_free(struct kref *ref);
static int riocm_post_send(struct cm_dev *cm, struct rio_dev *rdev,
void *buffer, size_t len);
static int riocm_ch_close(struct rio_channel *ch);
static DEFINE_SPINLOCK(idr_lock);
static DEFINE_IDR(ch_idr);
static LIST_HEAD(cm_dev_list);
static DECLARE_RWSEM(rdev_sem);
static struct class *dev_class;
static unsigned int dev_major;
static unsigned int dev_minor_base;
static dev_t dev_number;
static struct channel_dev riocm_cdev;
#define is_msg_capable(src_ops, dst_ops) \
((src_ops & RIO_SRC_OPS_DATA_MSG) && \
(dst_ops & RIO_DST_OPS_DATA_MSG))
#define dev_cm_capable(dev) \
is_msg_capable(dev->src_ops, dev->dst_ops)
static int riocm_cmp(struct rio_channel *ch, enum rio_cm_state cmp)
{
int ret;
spin_lock_bh(&ch->lock);
ret = (ch->state == cmp);
spin_unlock_bh(&ch->lock);
return ret;
}
static int riocm_cmp_exch(struct rio_channel *ch,
enum rio_cm_state cmp, enum rio_cm_state exch)
{
int ret;
spin_lock_bh(&ch->lock);
ret = (ch->state == cmp);
if (ret)
ch->state = exch;
spin_unlock_bh(&ch->lock);
return ret;
}
static enum rio_cm_state riocm_exch(struct rio_channel *ch,
enum rio_cm_state exch)
{
enum rio_cm_state old;
spin_lock_bh(&ch->lock);
old = ch->state;
ch->state = exch;
spin_unlock_bh(&ch->lock);
return old;
}
static struct rio_channel *riocm_get_channel(u16 nr)
{
struct rio_channel *ch;
spin_lock_bh(&idr_lock);
ch = idr_find(&ch_idr, nr);
if (ch)
kref_get(&ch->ref);
spin_unlock_bh(&idr_lock);
return ch;
}
static void riocm_put_channel(struct rio_channel *ch)
{
kref_put(&ch->ref, riocm_ch_free);
}
static void *riocm_rx_get_msg(struct cm_dev *cm)
{
void *msg;
int i;
msg = rio_get_inb_message(cm->mport, cmbox);
if (msg) {
for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
if (cm->rx_buf[i] == msg) {
cm->rx_buf[i] = NULL;
cm->rx_slots++;
break;
}
}
if (i == RIOCM_RX_RING_SIZE)
riocm_warn("no record for buffer 0x%p", msg);
}
return msg;
}
/*
* riocm_rx_fill - fills a ring of receive buffers for given cm device
* @cm: cm_dev object
* @nent: max number of entries to fill
*
* Returns: none
*/
static void riocm_rx_fill(struct cm_dev *cm, int nent)
{
int i;
if (cm->rx_slots == 0)
return;
for (i = 0; i < RIOCM_RX_RING_SIZE && cm->rx_slots && nent; i++) {
if (cm->rx_buf[i] == NULL) {
cm->rx_buf[i] = kmalloc(RIO_MAX_MSG_SIZE, GFP_KERNEL);
if (cm->rx_buf[i] == NULL)
break;
rio_add_inb_buffer(cm->mport, cmbox, cm->rx_buf[i]);
cm->rx_slots--;
nent--;
}
}
}
/*
* riocm_rx_free - frees all receive buffers associated with given cm device
* @cm: cm_dev object
*
* Returns: none
*/
static void riocm_rx_free(struct cm_dev *cm)
{
int i;
for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
if (cm->rx_buf[i] != NULL) {
kfree(cm->rx_buf[i]);
cm->rx_buf[i] = NULL;
}
}
}
/*
* riocm_req_handler - connection request handler
* @cm: cm_dev object
* @req_data: pointer to the request packet
*
* Returns: 0 if success, or
* -EINVAL if channel is not in correct state,
* -ENODEV if cannot find a channel with specified ID,
* -ENOMEM if unable to allocate memory to store the request
*/
static int riocm_req_handler(struct cm_dev *cm, void *req_data)
{
struct rio_channel *ch;
struct conn_req *req;
struct rio_ch_chan_hdr *hh = req_data;
u16 chnum;
chnum = ntohs(hh->dst_ch);
ch = riocm_get_channel(chnum);
if (!ch)
return -ENODEV;
if (ch->state != RIO_CM_LISTEN) {
riocm_debug(RX_CMD, "channel %d is not in listen state", chnum);
riocm_put_channel(ch);
return -EINVAL;
}
req = kzalloc(sizeof(*req), GFP_KERNEL);
if (!req) {
riocm_put_channel(ch);
return -ENOMEM;
}
req->destid = ntohl(hh->bhdr.src_id);
req->chan = ntohs(hh->src_ch);
req->cmdev = cm;
spin_lock_bh(&ch->lock);
list_add_tail(&req->node, &ch->accept_queue);
spin_unlock_bh(&ch->lock);
complete(&ch->comp);
riocm_put_channel(ch);
return 0;
}
/*
* riocm_resp_handler - response to connection request handler
* @resp_data: pointer to the response packet
*
* Returns: 0 if success, or
* -EINVAL if channel is not in correct state,
* -ENODEV if cannot find a channel with specified ID,
*/
static int riocm_resp_handler(void *resp_data)
{
struct rio_channel *ch;
struct rio_ch_chan_hdr *hh = resp_data;
u16 chnum;
chnum = ntohs(hh->dst_ch);
ch = riocm_get_channel(chnum);
if (!ch)
return -ENODEV;
if (ch->state != RIO_CM_CONNECT) {
riocm_put_channel(ch);
return -EINVAL;
}
riocm_exch(ch, RIO_CM_CONNECTED);
ch->rem_channel = ntohs(hh->src_ch);
complete(&ch->comp);
riocm_put_channel(ch);
return 0;
}
/*
* riocm_close_handler - channel close request handler
* @req_data: pointer to the request packet
*
* Returns: 0 if success, or
* -ENODEV if cannot find a channel with specified ID,
* + error codes returned by riocm_ch_close.
*/
static int riocm_close_handler(void *data)
{
struct rio_channel *ch;
struct rio_ch_chan_hdr *hh = data;
int ret;
riocm_debug(RX_CMD, "for ch=%d", ntohs(hh->dst_ch));
spin_lock_bh(&idr_lock);
ch = idr_find(&ch_idr, ntohs(hh->dst_ch));
if (!ch) {
spin_unlock_bh(&idr_lock);
return -ENODEV;
}
idr_remove(&ch_idr, ch->id);
spin_unlock_bh(&idr_lock);
riocm_exch(ch, RIO_CM_DISCONNECT);
ret = riocm_ch_close(ch);
if (ret)
riocm_debug(RX_CMD, "riocm_ch_close() returned %d", ret);
return 0;
}
/*
* rio_cm_handler - function that services request (non-data) packets
* @cm: cm_dev object
* @data: pointer to the packet
*/
static void rio_cm_handler(struct cm_dev *cm, void *data)
{
struct rio_ch_chan_hdr *hdr;
if (!rio_mport_is_running(cm->mport))
goto out;
hdr = data;
riocm_debug(RX_CMD, "OP=%x for ch=%d from %d",
hdr->ch_op, ntohs(hdr->dst_ch), ntohs(hdr->src_ch));
switch (hdr->ch_op) {
case CM_CONN_REQ:
riocm_req_handler(cm, data);
break;
case CM_CONN_ACK:
riocm_resp_handler(data);
break;
case CM_CONN_CLOSE:
riocm_close_handler(data);
break;
default:
riocm_error("Invalid packet header");
break;
}
out:
kfree(data);
}
/*
* rio_rx_data_handler - received data packet handler
* @cm: cm_dev object
* @buf: data packet
*
* Returns: 0 if success, or
* -ENODEV if cannot find a channel with specified ID,
* -EIO if channel is not in CONNECTED state,
* -ENOMEM if channel RX queue is full (packet discarded)
*/
static int rio_rx_data_handler(struct cm_dev *cm, void *buf)
{
struct rio_ch_chan_hdr *hdr;
struct rio_channel *ch;
hdr = buf;
riocm_debug(RX_DATA, "for ch=%d", ntohs(hdr->dst_ch));
ch = riocm_get_channel(ntohs(hdr->dst_ch));
if (!ch) {
/* Discard data message for non-existing channel */
kfree(buf);
return -ENODEV;
}
/* Place pointer to the buffer into channel's RX queue */
spin_lock(&ch->lock);
if (ch->state != RIO_CM_CONNECTED) {
/* Channel is not ready to receive data, discard a packet */
riocm_debug(RX_DATA, "ch=%d is in wrong state=%d",
ch->id, ch->state);
spin_unlock(&ch->lock);
kfree(buf);
riocm_put_channel(ch);
return -EIO;
}
if (ch->rx_ring.count == RIOCM_RX_RING_SIZE) {
/* If RX ring is full, discard a packet */
riocm_debug(RX_DATA, "ch=%d is full", ch->id);
spin_unlock(&ch->lock);
kfree(buf);
riocm_put_channel(ch);
return -ENOMEM;
}
ch->rx_ring.buf[ch->rx_ring.head] = buf;
ch->rx_ring.head++;
ch->rx_ring.count++;
ch->rx_ring.head %= RIOCM_RX_RING_SIZE;
complete(&ch->comp);
spin_unlock(&ch->lock);
riocm_put_channel(ch);
return 0;
}
/*
* rio_ibmsg_handler - inbound message packet handler
*/
static void rio_ibmsg_handler(struct work_struct *work)
{
struct cm_dev *cm = container_of(work, struct cm_dev, rx_work);
void *data;
struct rio_ch_chan_hdr *hdr;
if (!rio_mport_is_running(cm->mport))
return;
while (1) {
mutex_lock(&cm->rx_lock);
data = riocm_rx_get_msg(cm);
if (data)
riocm_rx_fill(cm, 1);
mutex_unlock(&cm->rx_lock);
if (data == NULL)
break;
hdr = data;
if (hdr->bhdr.type != RIO_CM_CHAN) {
/* For now simply discard packets other than channel */
riocm_error("Unsupported TYPE code (0x%x). Msg dropped",
hdr->bhdr.type);
kfree(data);
continue;
}
/* Process a channel message */
if (hdr->ch_op == CM_DATA_MSG)
rio_rx_data_handler(cm, data);
else
rio_cm_handler(cm, data);
}
}
static void riocm_inb_msg_event(struct rio_mport *mport, void *dev_id,
int mbox, int slot)
{
struct cm_dev *cm = dev_id;
if (rio_mport_is_running(cm->mport) && !work_pending(&cm->rx_work))
queue_work(cm->rx_wq, &cm->rx_work);
}
/*
* rio_txcq_handler - TX completion handler
* @cm: cm_dev object
* @slot: TX queue slot
*
* TX completion handler also ensures that pending request packets are placed
* into transmit queue as soon as a free slot becomes available. This is done
* to give higher priority to request packets during high intensity data flow.
*/
static void rio_txcq_handler(struct cm_dev *cm, int slot)
{
int ack_slot;
/* ATTN: Add TX completion notification if/when direct buffer
* transfer is implemented. At this moment only correct tracking
* of tx_count is important.
*/
riocm_debug(TX_EVENT, "for mport_%d slot %d tx_cnt %d",
cm->mport->id, slot, cm->tx_cnt);
spin_lock(&cm->tx_lock);
ack_slot = cm->tx_ack_slot;
if (ack_slot == slot)
riocm_debug(TX_EVENT, "slot == ack_slot");
while (cm->tx_cnt && ((ack_slot != slot) ||
(cm->tx_cnt == RIOCM_TX_RING_SIZE))) {
cm->tx_buf[ack_slot] = NULL;
++ack_slot;
ack_slot &= (RIOCM_TX_RING_SIZE - 1);
cm->tx_cnt--;
}
if (cm->tx_cnt < 0 || cm->tx_cnt > RIOCM_TX_RING_SIZE)
riocm_error("tx_cnt %d out of sync", cm->tx_cnt);
WARN_ON((cm->tx_cnt < 0) || (cm->tx_cnt > RIOCM_TX_RING_SIZE));
cm->tx_ack_slot = ack_slot;
/*
* If there are pending requests, insert them into transmit queue
*/
if (!list_empty(&cm->tx_reqs) && (cm->tx_cnt < RIOCM_TX_RING_SIZE)) {
struct tx_req *req, *_req;
int rc;
list_for_each_entry_safe(req, _req, &cm->tx_reqs, node) {
list_del(&req->node);
cm->tx_buf[cm->tx_slot] = req->buffer;
rc = rio_add_outb_message(cm->mport, req->rdev, cmbox,
req->buffer, req->len);
kfree(req->buffer);
kfree(req);
++cm->tx_cnt;
++cm->tx_slot;
cm->tx_slot &= (RIOCM_TX_RING_SIZE - 1);
if (cm->tx_cnt == RIOCM_TX_RING_SIZE)
break;
}
}
spin_unlock(&cm->tx_lock);
}
static void riocm_outb_msg_event(struct rio_mport *mport, void *dev_id,
int mbox, int slot)
{
struct cm_dev *cm = dev_id;
if (cm && rio_mport_is_running(cm->mport))
rio_txcq_handler(cm, slot);
}
static int riocm_queue_req(struct cm_dev *cm, struct rio_dev *rdev,
void *buffer, size_t len)
{
unsigned long flags;
struct tx_req *treq;
treq = kzalloc(sizeof(*treq), GFP_KERNEL);
if (treq == NULL)
return -ENOMEM;
treq->rdev = rdev;
treq->buffer = buffer;
treq->len = len;
spin_lock_irqsave(&cm->tx_lock, flags);
list_add_tail(&treq->node, &cm->tx_reqs);
spin_unlock_irqrestore(&cm->tx_lock, flags);
return 0;
}
/*
* riocm_post_send - helper function that places packet into msg TX queue
* @cm: cm_dev object
* @rdev: target RapidIO device object (required by outbound msg interface)
* @buffer: pointer to a packet buffer to send
* @len: length of data to transfer
* @req: request priority flag
*
* Returns: 0 if success, or error code otherwise.
*/
static int riocm_post_send(struct cm_dev *cm, struct rio_dev *rdev,
void *buffer, size_t len)
{
int rc;
unsigned long flags;
spin_lock_irqsave(&cm->tx_lock, flags);
if (cm->mport == NULL) {
rc = -ENODEV;
goto err_out;
}
if (cm->tx_cnt == RIOCM_TX_RING_SIZE) {
riocm_debug(TX, "Tx Queue is full");
rc = -EBUSY;
goto err_out;
}
cm->tx_buf[cm->tx_slot] = buffer;
rc = rio_add_outb_message(cm->mport, rdev, cmbox, buffer, len);
riocm_debug(TX, "Add buf@%p destid=%x tx_slot=%d tx_cnt=%d",
buffer, rdev->destid, cm->tx_slot, cm->tx_cnt);
++cm->tx_cnt;
++cm->tx_slot;
cm->tx_slot &= (RIOCM_TX_RING_SIZE - 1);
err_out:
spin_unlock_irqrestore(&cm->tx_lock, flags);
return rc;
}
/*
* riocm_ch_send - sends a data packet to a remote device
* @ch_id: local channel ID
* @buf: pointer to a data buffer to send (including CM header)
* @len: length of data to transfer (including CM header)
*
* ATTN: ASSUMES THAT THE HEADER SPACE IS RESERVED PART OF THE DATA PACKET
*
* Returns: 0 if success, or
* -EINVAL if one or more input parameters is/are not valid,
* -ENODEV if cannot find a channel with specified ID,
* -EAGAIN if a channel is not in CONNECTED state,
* + error codes returned by HW send routine.
*/
static int riocm_ch_send(u16 ch_id, void *buf, int len)
{
struct rio_channel *ch;
struct rio_ch_chan_hdr *hdr;
int ret;
if (buf == NULL || ch_id == 0 || len == 0 || len > RIO_MAX_MSG_SIZE)
return -EINVAL;
ch = riocm_get_channel(ch_id);
if (!ch) {
riocm_error("%s(%d) ch_%d not found", current->comm,
task_pid_nr(current), ch_id);
return -ENODEV;
}
if (!riocm_cmp(ch, RIO_CM_CONNECTED)) {
ret = -EAGAIN;
goto err_out;
}
/*
* Fill buffer header section with corresponding channel data
*/
hdr = buf;
hdr->bhdr.src_id = htonl(ch->loc_destid);
hdr->bhdr.dst_id = htonl(ch->rem_destid);
hdr->bhdr.src_mbox = cmbox;
hdr->bhdr.dst_mbox = cmbox;
hdr->bhdr.type = RIO_CM_CHAN;
hdr->ch_op = CM_DATA_MSG;
hdr->dst_ch = htons(ch->rem_channel);
hdr->src_ch = htons(ch->id);
hdr->msg_len = htons((u16)len);
/* ATTN: the function call below relies on the fact that underlying
* HW-specific add_outb_message() routine copies TX data into its own
* internal transfer buffer (true for all RIONET compatible mport
* drivers). Must be reviewed if mport driver uses the buffer directly.
*/
ret = riocm_post_send(ch->cmdev, ch->rdev, buf, len);
if (ret)
riocm_debug(TX, "ch %d send_err=%d", ch->id, ret);
err_out:
riocm_put_channel(ch);
return ret;
}
static int riocm_ch_free_rxbuf(struct rio_channel *ch, void *buf)
{
int i, ret = -EINVAL;
spin_lock_bh(&ch->lock);
for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
if (ch->rx_ring.inuse[i] == buf) {
ch->rx_ring.inuse[i] = NULL;
ch->rx_ring.inuse_cnt--;
ret = 0;
break;
}
}
spin_unlock_bh(&ch->lock);
if (!ret)
kfree(buf);
return ret;
}
/*
* riocm_ch_receive - fetch a data packet received for the specified channel
* @ch: local channel ID
* @buf: pointer to a packet buffer
* @timeout: timeout to wait for incoming packet (in jiffies)
*
* Returns: 0 and valid buffer pointer if success, or NULL pointer and one of:
* -EAGAIN if a channel is not in CONNECTED state,
* -ENOMEM if in-use tracking queue is full,
* -ETIME if wait timeout expired,
* -EINTR if wait was interrupted.
*/
static int riocm_ch_receive(struct rio_channel *ch, void **buf, long timeout)
{
void *rxmsg = NULL;
int i, ret = 0;
long wret;
if (!riocm_cmp(ch, RIO_CM_CONNECTED)) {
ret = -EAGAIN;
goto out;
}
if (ch->rx_ring.inuse_cnt == RIOCM_RX_RING_SIZE) {
/* If we do not have entries to track buffers given to upper
* layer, reject request.
*/
ret = -ENOMEM;
goto out;
}
wret = wait_for_completion_interruptible_timeout(&ch->comp, timeout);
riocm_debug(WAIT, "wait on %d returned %ld", ch->id, wret);
if (!wret)
ret = -ETIME;
else if (wret == -ERESTARTSYS)
ret = -EINTR;
else
ret = riocm_cmp(ch, RIO_CM_CONNECTED) ? 0 : -ECONNRESET;
if (ret)
goto out;
spin_lock_bh(&ch->lock);
rxmsg = ch->rx_ring.buf[ch->rx_ring.tail];
ch->rx_ring.buf[ch->rx_ring.tail] = NULL;
ch->rx_ring.count--;
ch->rx_ring.tail++;
ch->rx_ring.tail %= RIOCM_RX_RING_SIZE;
ret = -ENOMEM;
for (i = 0; i < RIOCM_RX_RING_SIZE; i++) {
if (ch->rx_ring.inuse[i] == NULL) {
ch->rx_ring.inuse[i] = rxmsg;
ch->rx_ring.inuse_cnt++;
ret = 0;
break;
}
}
if (ret) {
/* We have no entry to store pending message: drop it */
kfree(rxmsg);
rxmsg = NULL;
}
spin_unlock_bh(&ch->lock);
out:
*buf = rxmsg;
return ret;
}
/*
* riocm_ch_connect - sends a connect request to a remote device
* @loc_ch: local channel ID
* @cm: CM device to send connect request
* @peer: target RapidIO device
* @rem_ch: remote channel ID
*
* Returns: 0 if success, or
* -EINVAL if the channel is not in IDLE state,
* -EAGAIN if no connection request available immediately,
* -ETIME if ACK response timeout expired,
* -EINTR if wait for response was interrupted.
*/
static int riocm_ch_connect(u16 loc_ch, struct cm_dev *cm,
struct cm_peer *peer, u16 rem_ch)
{
struct rio_channel *ch = NULL;
struct rio_ch_chan_hdr *hdr;
int ret;
long wret;
ch = riocm_get_channel(loc_ch);
if (!ch)
return -ENODEV;
if (!riocm_cmp_exch(ch, RIO_CM_IDLE, RIO_CM_CONNECT)) {
ret = -EINVAL;
goto conn_done;
}
ch->cmdev = cm;
ch->rdev = peer->rdev;
ch->context = NULL;
ch->loc_destid = cm->mport->host_deviceid;
ch->rem_channel = rem_ch;
/*
* Send connect request to the remote RapidIO device
*/
hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
if (hdr == NULL) {
ret = -ENOMEM;
goto conn_done;
}
hdr->bhdr.src_id = htonl(ch->loc_destid);
hdr->bhdr.dst_id = htonl(peer->rdev->destid);
hdr->bhdr.src_mbox = cmbox;
hdr->bhdr.dst_mbox = cmbox;
hdr->bhdr.type = RIO_CM_CHAN;
hdr->ch_op = CM_CONN_REQ;
hdr->dst_ch = htons(rem_ch);
hdr->src_ch = htons(loc_ch);
/* ATTN: the function call below relies on the fact that underlying
* HW-specific add_outb_message() routine copies TX data into its
* internal transfer buffer. Must be reviewed if mport driver uses
* this buffer directly.
*/
ret = riocm_post_send(cm, peer->rdev, hdr, sizeof(*hdr));
if (ret != -EBUSY) {
kfree(hdr);
} else {
ret = riocm_queue_req(cm, peer->rdev, hdr, sizeof(*hdr));
if (ret)
kfree(hdr);
}
if (ret) {
riocm_cmp_exch(ch, RIO_CM_CONNECT, RIO_CM_IDLE);