forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibmvmc.c
2421 lines (2092 loc) · 60.8 KB
/
ibmvmc.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+
/*
* IBM Power Systems Virtual Management Channel Support.
*
* Copyright (c) 2004, 2018 IBM Corp.
* Dave Engebretsen [email protected]
* Steven Royer [email protected]
* Adam Reznechek [email protected]
* Bryant G. Ly <[email protected]>
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/major.h>
#include <linux/string.h>
#include <linux/fcntl.h>
#include <linux/slab.h>
#include <linux/poll.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/spinlock.h>
#include <linux/percpu.h>
#include <linux/delay.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/miscdevice.h>
#include <linux/sched/signal.h>
#include <asm/byteorder.h>
#include <asm/irq.h>
#include <asm/vio.h>
#include "ibmvmc.h"
#define IBMVMC_DRIVER_VERSION "1.0"
/*
* Static global variables
*/
static DECLARE_WAIT_QUEUE_HEAD(ibmvmc_read_wait);
static const char ibmvmc_driver_name[] = "ibmvmc";
static struct ibmvmc_struct ibmvmc;
static struct ibmvmc_hmc hmcs[MAX_HMCS];
static struct crq_server_adapter ibmvmc_adapter;
static int ibmvmc_max_buf_pool_size = DEFAULT_BUF_POOL_SIZE;
static int ibmvmc_max_hmcs = DEFAULT_HMCS;
static int ibmvmc_max_mtu = DEFAULT_MTU;
static inline long h_copy_rdma(s64 length, u64 sliobn, u64 slioba,
u64 dliobn, u64 dlioba)
{
long rc = 0;
/* Ensure all writes to source memory are visible before hcall */
dma_wmb();
pr_debug("ibmvmc: h_copy_rdma(0x%llx, 0x%llx, 0x%llx, 0x%llx, 0x%llx\n",
length, sliobn, slioba, dliobn, dlioba);
rc = plpar_hcall_norets(H_COPY_RDMA, length, sliobn, slioba,
dliobn, dlioba);
pr_debug("ibmvmc: h_copy_rdma rc = 0x%lx\n", rc);
return rc;
}
static inline void h_free_crq(uint32_t unit_address)
{
long rc = 0;
do {
if (H_IS_LONG_BUSY(rc))
msleep(get_longbusy_msecs(rc));
rc = plpar_hcall_norets(H_FREE_CRQ, unit_address);
} while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
}
/**
* h_request_vmc: - request a hypervisor virtual management channel device
* @vmc_index: drc index of the vmc device created
*
* Requests the hypervisor create a new virtual management channel device,
* allowing this partition to send hypervisor virtualization control
* commands.
*
* Return:
* 0 - Success
* Non-zero - Failure
*/
static inline long h_request_vmc(u32 *vmc_index)
{
long rc = 0;
unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
do {
if (H_IS_LONG_BUSY(rc))
msleep(get_longbusy_msecs(rc));
/* Call to request the VMC device from phyp */
rc = plpar_hcall(H_REQUEST_VMC, retbuf);
pr_debug("ibmvmc: %s rc = 0x%lx\n", __func__, rc);
*vmc_index = retbuf[0];
} while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
return rc;
}
/* routines for managing a command/response queue */
/**
* ibmvmc_handle_event: - Interrupt handler for crq events
* @irq: number of irq to handle, not used
* @dev_instance: crq_server_adapter that received interrupt
*
* Disables interrupts and schedules ibmvmc_task
*
* Always returns IRQ_HANDLED
*/
static irqreturn_t ibmvmc_handle_event(int irq, void *dev_instance)
{
struct crq_server_adapter *adapter =
(struct crq_server_adapter *)dev_instance;
vio_disable_interrupts(to_vio_dev(adapter->dev));
tasklet_schedule(&adapter->work_task);
return IRQ_HANDLED;
}
/**
* ibmvmc_release_crq_queue - Release CRQ Queue
*
* @adapter: crq_server_adapter struct
*
* Return:
* 0 - Success
* Non-Zero - Failure
*/
static void ibmvmc_release_crq_queue(struct crq_server_adapter *adapter)
{
struct vio_dev *vdev = to_vio_dev(adapter->dev);
struct crq_queue *queue = &adapter->queue;
free_irq(vdev->irq, (void *)adapter);
tasklet_kill(&adapter->work_task);
if (adapter->reset_task)
kthread_stop(adapter->reset_task);
h_free_crq(vdev->unit_address);
dma_unmap_single(adapter->dev,
queue->msg_token,
queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
free_page((unsigned long)queue->msgs);
}
/**
* ibmvmc_reset_crq_queue - Reset CRQ Queue
*
* @adapter: crq_server_adapter struct
*
* This function calls h_free_crq and then calls H_REG_CRQ and does all the
* bookkeeping to get us back to where we can communicate.
*
* Return:
* 0 - Success
* Non-Zero - Failure
*/
static int ibmvmc_reset_crq_queue(struct crq_server_adapter *adapter)
{
struct vio_dev *vdev = to_vio_dev(adapter->dev);
struct crq_queue *queue = &adapter->queue;
int rc = 0;
/* Close the CRQ */
h_free_crq(vdev->unit_address);
/* Clean out the queue */
memset(queue->msgs, 0x00, PAGE_SIZE);
queue->cur = 0;
/* And re-open it again */
rc = plpar_hcall_norets(H_REG_CRQ,
vdev->unit_address,
queue->msg_token, PAGE_SIZE);
if (rc == 2)
/* Adapter is good, but other end is not ready */
dev_warn(adapter->dev, "Partner adapter not ready\n");
else if (rc != 0)
dev_err(adapter->dev, "couldn't register crq--rc 0x%x\n", rc);
return rc;
}
/**
* crq_queue_next_crq: - Returns the next entry in message queue
* @queue: crq_queue to use
*
* Returns pointer to next entry in queue, or NULL if there are no new
* entried in the CRQ.
*/
static struct ibmvmc_crq_msg *crq_queue_next_crq(struct crq_queue *queue)
{
struct ibmvmc_crq_msg *crq;
unsigned long flags;
spin_lock_irqsave(&queue->lock, flags);
crq = &queue->msgs[queue->cur];
if (crq->valid & 0x80) {
if (++queue->cur == queue->size)
queue->cur = 0;
/* Ensure the read of the valid bit occurs before reading any
* other bits of the CRQ entry
*/
dma_rmb();
} else {
crq = NULL;
}
spin_unlock_irqrestore(&queue->lock, flags);
return crq;
}
/**
* ibmvmc_send_crq - Send CRQ
*
* @adapter: crq_server_adapter struct
* @word1: Word1 Data field
* @word2: Word2 Data field
*
* Return:
* 0 - Success
* Non-Zero - Failure
*/
static long ibmvmc_send_crq(struct crq_server_adapter *adapter,
u64 word1, u64 word2)
{
struct vio_dev *vdev = to_vio_dev(adapter->dev);
long rc = 0;
dev_dbg(adapter->dev, "(0x%x, 0x%016llx, 0x%016llx)\n",
vdev->unit_address, word1, word2);
/*
* Ensure the command buffer is flushed to memory before handing it
* over to the other side to prevent it from fetching any stale data.
*/
dma_wmb();
rc = plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
dev_dbg(adapter->dev, "rc = 0x%lx\n", rc);
return rc;
}
/**
* alloc_dma_buffer - Create DMA Buffer
*
* @vdev: vio_dev struct
* @size: Size field
* @dma_handle: DMA address field
*
* Allocates memory for the command queue and maps remote memory into an
* ioba.
*
* Returns a pointer to the buffer
*/
static void *alloc_dma_buffer(struct vio_dev *vdev, size_t size,
dma_addr_t *dma_handle)
{
/* allocate memory */
void *buffer = kzalloc(size, GFP_ATOMIC);
if (!buffer) {
*dma_handle = 0;
return NULL;
}
/* DMA map */
*dma_handle = dma_map_single(&vdev->dev, buffer, size,
DMA_BIDIRECTIONAL);
if (dma_mapping_error(&vdev->dev, *dma_handle)) {
*dma_handle = 0;
kzfree(buffer);
return NULL;
}
return buffer;
}
/**
* free_dma_buffer - Free DMA Buffer
*
* @vdev: vio_dev struct
* @size: Size field
* @vaddr: Address field
* @dma_handle: DMA address field
*
* Releases memory for a command queue and unmaps mapped remote memory.
*/
static void free_dma_buffer(struct vio_dev *vdev, size_t size, void *vaddr,
dma_addr_t dma_handle)
{
/* DMA unmap */
dma_unmap_single(&vdev->dev, dma_handle, size, DMA_BIDIRECTIONAL);
/* deallocate memory */
kzfree(vaddr);
}
/**
* ibmvmc_get_valid_hmc_buffer - Retrieve Valid HMC Buffer
*
* @hmc_index: HMC Index Field
*
* Return:
* Pointer to ibmvmc_buffer
*/
static struct ibmvmc_buffer *ibmvmc_get_valid_hmc_buffer(u8 hmc_index)
{
struct ibmvmc_buffer *buffer;
struct ibmvmc_buffer *ret_buf = NULL;
unsigned long i;
if (hmc_index > ibmvmc.max_hmc_index)
return NULL;
buffer = hmcs[hmc_index].buffer;
for (i = 0; i < ibmvmc_max_buf_pool_size; i++) {
if (buffer[i].valid && buffer[i].free &&
buffer[i].owner == VMC_BUF_OWNER_ALPHA) {
buffer[i].free = 0;
ret_buf = &buffer[i];
break;
}
}
return ret_buf;
}
/**
* ibmvmc_get_free_hmc_buffer - Get Free HMC Buffer
*
* @adapter: crq_server_adapter struct
* @hmc_index: Hmc Index field
*
* Return:
* Pointer to ibmvmc_buffer
*/
static struct ibmvmc_buffer *ibmvmc_get_free_hmc_buffer(struct crq_server_adapter *adapter,
u8 hmc_index)
{
struct ibmvmc_buffer *buffer;
struct ibmvmc_buffer *ret_buf = NULL;
unsigned long i;
if (hmc_index > ibmvmc.max_hmc_index) {
dev_info(adapter->dev, "get_free_hmc_buffer: invalid hmc_index=0x%x\n",
hmc_index);
return NULL;
}
buffer = hmcs[hmc_index].buffer;
for (i = 0; i < ibmvmc_max_buf_pool_size; i++) {
if (buffer[i].free &&
buffer[i].owner == VMC_BUF_OWNER_ALPHA) {
buffer[i].free = 0;
ret_buf = &buffer[i];
break;
}
}
return ret_buf;
}
/**
* ibmvmc_free_hmc_buffer - Free an HMC Buffer
*
* @hmc: ibmvmc_hmc struct
* @buffer: ibmvmc_buffer struct
*
*/
static void ibmvmc_free_hmc_buffer(struct ibmvmc_hmc *hmc,
struct ibmvmc_buffer *buffer)
{
unsigned long flags;
spin_lock_irqsave(&hmc->lock, flags);
buffer->free = 1;
spin_unlock_irqrestore(&hmc->lock, flags);
}
/**
* ibmvmc_count_hmc_buffers - Count HMC Buffers
*
* @hmc_index: HMC Index field
* @valid: Valid number of buffers field
* @free: Free number of buffers field
*
*/
static void ibmvmc_count_hmc_buffers(u8 hmc_index, unsigned int *valid,
unsigned int *free)
{
struct ibmvmc_buffer *buffer;
unsigned long i;
unsigned long flags;
if (hmc_index > ibmvmc.max_hmc_index)
return;
if (!valid || !free)
return;
*valid = 0; *free = 0;
buffer = hmcs[hmc_index].buffer;
spin_lock_irqsave(&hmcs[hmc_index].lock, flags);
for (i = 0; i < ibmvmc_max_buf_pool_size; i++) {
if (buffer[i].valid) {
*valid = *valid + 1;
if (buffer[i].free)
*free = *free + 1;
}
}
spin_unlock_irqrestore(&hmcs[hmc_index].lock, flags);
}
/**
* ibmvmc_get_free_hmc - Get Free HMC
*
* Return:
* Pointer to an available HMC Connection
* Null otherwise
*/
static struct ibmvmc_hmc *ibmvmc_get_free_hmc(void)
{
unsigned long i;
unsigned long flags;
/*
* Find an available HMC connection.
*/
for (i = 0; i <= ibmvmc.max_hmc_index; i++) {
spin_lock_irqsave(&hmcs[i].lock, flags);
if (hmcs[i].state == ibmhmc_state_free) {
hmcs[i].index = i;
hmcs[i].state = ibmhmc_state_initial;
spin_unlock_irqrestore(&hmcs[i].lock, flags);
return &hmcs[i];
}
spin_unlock_irqrestore(&hmcs[i].lock, flags);
}
return NULL;
}
/**
* ibmvmc_return_hmc - Return an HMC Connection
*
* @hmc: ibmvmc_hmc struct
* @release_readers: Number of readers connected to session
*
* This function releases the HMC connections back into the pool.
*
* Return:
* 0 - Success
* Non-zero - Failure
*/
static int ibmvmc_return_hmc(struct ibmvmc_hmc *hmc, bool release_readers)
{
struct ibmvmc_buffer *buffer;
struct crq_server_adapter *adapter;
struct vio_dev *vdev;
unsigned long i;
unsigned long flags;
if (!hmc || !hmc->adapter)
return -EIO;
if (release_readers) {
if (hmc->file_session) {
struct ibmvmc_file_session *session = hmc->file_session;
session->valid = 0;
wake_up_interruptible(&ibmvmc_read_wait);
}
}
adapter = hmc->adapter;
vdev = to_vio_dev(adapter->dev);
spin_lock_irqsave(&hmc->lock, flags);
hmc->index = 0;
hmc->state = ibmhmc_state_free;
hmc->queue_head = 0;
hmc->queue_tail = 0;
buffer = hmc->buffer;
for (i = 0; i < ibmvmc_max_buf_pool_size; i++) {
if (buffer[i].valid) {
free_dma_buffer(vdev,
ibmvmc.max_mtu,
buffer[i].real_addr_local,
buffer[i].dma_addr_local);
dev_dbg(adapter->dev, "Forgot buffer id 0x%lx\n", i);
}
memset(&buffer[i], 0, sizeof(struct ibmvmc_buffer));
hmc->queue_outbound_msgs[i] = VMC_INVALID_BUFFER_ID;
}
spin_unlock_irqrestore(&hmc->lock, flags);
return 0;
}
/**
* ibmvmc_send_open - Interface Open
* @buffer: Pointer to ibmvmc_buffer struct
* @hmc: Pointer to ibmvmc_hmc struct
*
* This command is sent by the management partition as the result of a
* management partition device request. It causes the hypervisor to
* prepare a set of data buffers for the management application connection
* indicated HMC idx. A unique HMC Idx would be used if multiple management
* applications running concurrently were desired. Before responding to this
* command, the hypervisor must provide the management partition with at
* least one of these new buffers via the Add Buffer. This indicates whether
* the messages are inbound or outbound from the hypervisor.
*
* Return:
* 0 - Success
* Non-zero - Failure
*/
static int ibmvmc_send_open(struct ibmvmc_buffer *buffer,
struct ibmvmc_hmc *hmc)
{
struct ibmvmc_crq_msg crq_msg;
struct crq_server_adapter *adapter;
__be64 *crq_as_u64 = (__be64 *)&crq_msg;
int rc = 0;
if (!hmc || !hmc->adapter)
return -EIO;
adapter = hmc->adapter;
dev_dbg(adapter->dev, "send_open: 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
(unsigned long)buffer->size, (unsigned long)adapter->liobn,
(unsigned long)buffer->dma_addr_local,
(unsigned long)adapter->riobn,
(unsigned long)buffer->dma_addr_remote);
rc = h_copy_rdma(buffer->size,
adapter->liobn,
buffer->dma_addr_local,
adapter->riobn,
buffer->dma_addr_remote);
if (rc) {
dev_err(adapter->dev, "Error: In send_open, h_copy_rdma rc 0x%x\n",
rc);
return -EIO;
}
hmc->state = ibmhmc_state_opening;
crq_msg.valid = 0x80;
crq_msg.type = VMC_MSG_OPEN;
crq_msg.status = 0;
crq_msg.var1.rsvd = 0;
crq_msg.hmc_session = hmc->session;
crq_msg.hmc_index = hmc->index;
crq_msg.var2.buffer_id = cpu_to_be16(buffer->id);
crq_msg.rsvd = 0;
crq_msg.var3.rsvd = 0;
ibmvmc_send_crq(adapter, be64_to_cpu(crq_as_u64[0]),
be64_to_cpu(crq_as_u64[1]));
return rc;
}
/**
* ibmvmc_send_close - Interface Close
* @hmc: Pointer to ibmvmc_hmc struct
*
* This command is sent by the management partition to terminate a
* management application to hypervisor connection. When this command is
* sent, the management partition has quiesced all I/O operations to all
* buffers associated with this management application connection, and
* has freed any storage for these buffers.
*
* Return:
* 0 - Success
* Non-zero - Failure
*/
static int ibmvmc_send_close(struct ibmvmc_hmc *hmc)
{
struct ibmvmc_crq_msg crq_msg;
struct crq_server_adapter *adapter;
__be64 *crq_as_u64 = (__be64 *)&crq_msg;
int rc = 0;
if (!hmc || !hmc->adapter)
return -EIO;
adapter = hmc->adapter;
dev_info(adapter->dev, "CRQ send: close\n");
crq_msg.valid = 0x80;
crq_msg.type = VMC_MSG_CLOSE;
crq_msg.status = 0;
crq_msg.var1.rsvd = 0;
crq_msg.hmc_session = hmc->session;
crq_msg.hmc_index = hmc->index;
crq_msg.var2.rsvd = 0;
crq_msg.rsvd = 0;
crq_msg.var3.rsvd = 0;
ibmvmc_send_crq(adapter, be64_to_cpu(crq_as_u64[0]),
be64_to_cpu(crq_as_u64[1]));
return rc;
}
/**
* ibmvmc_send_capabilities - Send VMC Capabilities
*
* @adapter: crq_server_adapter struct
*
* The capabilities message is an administrative message sent after the CRQ
* initialization sequence of messages and is used to exchange VMC capabilities
* between the management partition and the hypervisor. The management
* partition must send this message and the hypervisor must respond with VMC
* capabilities Response message before HMC interface message can begin. Any
* HMC interface messages received before the exchange of capabilities has
* complete are dropped.
*
* Return:
* 0 - Success
*/
static int ibmvmc_send_capabilities(struct crq_server_adapter *adapter)
{
struct ibmvmc_admin_crq_msg crq_msg;
__be64 *crq_as_u64 = (__be64 *)&crq_msg;
dev_dbg(adapter->dev, "ibmvmc: CRQ send: capabilities\n");
crq_msg.valid = 0x80;
crq_msg.type = VMC_MSG_CAP;
crq_msg.status = 0;
crq_msg.rsvd[0] = 0;
crq_msg.rsvd[1] = 0;
crq_msg.max_hmc = ibmvmc_max_hmcs;
crq_msg.max_mtu = cpu_to_be32(ibmvmc_max_mtu);
crq_msg.pool_size = cpu_to_be16(ibmvmc_max_buf_pool_size);
crq_msg.crq_size = cpu_to_be16(adapter->queue.size);
crq_msg.version = cpu_to_be16(IBMVMC_PROTOCOL_VERSION);
ibmvmc_send_crq(adapter, be64_to_cpu(crq_as_u64[0]),
be64_to_cpu(crq_as_u64[1]));
ibmvmc.state = ibmvmc_state_capabilities;
return 0;
}
/**
* ibmvmc_send_add_buffer_resp - Add Buffer Response
*
* @adapter: crq_server_adapter struct
* @status: Status field
* @hmc_session: HMC Session field
* @hmc_index: HMC Index field
* @buffer_id: Buffer Id field
*
* This command is sent by the management partition to the hypervisor in
* response to the Add Buffer message. The Status field indicates the result of
* the command.
*
* Return:
* 0 - Success
*/
static int ibmvmc_send_add_buffer_resp(struct crq_server_adapter *adapter,
u8 status, u8 hmc_session,
u8 hmc_index, u16 buffer_id)
{
struct ibmvmc_crq_msg crq_msg;
__be64 *crq_as_u64 = (__be64 *)&crq_msg;
dev_dbg(adapter->dev, "CRQ send: add_buffer_resp\n");
crq_msg.valid = 0x80;
crq_msg.type = VMC_MSG_ADD_BUF_RESP;
crq_msg.status = status;
crq_msg.var1.rsvd = 0;
crq_msg.hmc_session = hmc_session;
crq_msg.hmc_index = hmc_index;
crq_msg.var2.buffer_id = cpu_to_be16(buffer_id);
crq_msg.rsvd = 0;
crq_msg.var3.rsvd = 0;
ibmvmc_send_crq(adapter, be64_to_cpu(crq_as_u64[0]),
be64_to_cpu(crq_as_u64[1]));
return 0;
}
/**
* ibmvmc_send_rem_buffer_resp - Remove Buffer Response
*
* @adapter: crq_server_adapter struct
* @status: Status field
* @hmc_session: HMC Session field
* @hmc_index: HMC Index field
* @buffer_id: Buffer Id field
*
* This command is sent by the management partition to the hypervisor in
* response to the Remove Buffer message. The Buffer ID field indicates
* which buffer the management partition selected to remove. The Status
* field indicates the result of the command.
*
* Return:
* 0 - Success
*/
static int ibmvmc_send_rem_buffer_resp(struct crq_server_adapter *adapter,
u8 status, u8 hmc_session,
u8 hmc_index, u16 buffer_id)
{
struct ibmvmc_crq_msg crq_msg;
__be64 *crq_as_u64 = (__be64 *)&crq_msg;
dev_dbg(adapter->dev, "CRQ send: rem_buffer_resp\n");
crq_msg.valid = 0x80;
crq_msg.type = VMC_MSG_REM_BUF_RESP;
crq_msg.status = status;
crq_msg.var1.rsvd = 0;
crq_msg.hmc_session = hmc_session;
crq_msg.hmc_index = hmc_index;
crq_msg.var2.buffer_id = cpu_to_be16(buffer_id);
crq_msg.rsvd = 0;
crq_msg.var3.rsvd = 0;
ibmvmc_send_crq(adapter, be64_to_cpu(crq_as_u64[0]),
be64_to_cpu(crq_as_u64[1]));
return 0;
}
/**
* ibmvmc_send_msg - Signal Message
*
* @adapter: crq_server_adapter struct
* @buffer: ibmvmc_buffer struct
* @hmc: ibmvmc_hmc struct
* @msg_length: message length field
*
* This command is sent between the management partition and the hypervisor
* in order to signal the arrival of an HMC protocol message. The command
* can be sent by both the management partition and the hypervisor. It is
* used for all traffic between the management application and the hypervisor,
* regardless of who initiated the communication.
*
* There is no response to this message.
*
* Return:
* 0 - Success
* Non-zero - Failure
*/
static int ibmvmc_send_msg(struct crq_server_adapter *adapter,
struct ibmvmc_buffer *buffer,
struct ibmvmc_hmc *hmc, int msg_len)
{
struct ibmvmc_crq_msg crq_msg;
__be64 *crq_as_u64 = (__be64 *)&crq_msg;
int rc = 0;
dev_dbg(adapter->dev, "CRQ send: rdma to HV\n");
rc = h_copy_rdma(msg_len,
adapter->liobn,
buffer->dma_addr_local,
adapter->riobn,
buffer->dma_addr_remote);
if (rc) {
dev_err(adapter->dev, "Error in send_msg, h_copy_rdma rc 0x%x\n",
rc);
return rc;
}
crq_msg.valid = 0x80;
crq_msg.type = VMC_MSG_SIGNAL;
crq_msg.status = 0;
crq_msg.var1.rsvd = 0;
crq_msg.hmc_session = hmc->session;
crq_msg.hmc_index = hmc->index;
crq_msg.var2.buffer_id = cpu_to_be16(buffer->id);
crq_msg.var3.msg_len = cpu_to_be32(msg_len);
dev_dbg(adapter->dev, "CRQ send: msg to HV 0x%llx 0x%llx\n",
be64_to_cpu(crq_as_u64[0]), be64_to_cpu(crq_as_u64[1]));
buffer->owner = VMC_BUF_OWNER_HV;
ibmvmc_send_crq(adapter, be64_to_cpu(crq_as_u64[0]),
be64_to_cpu(crq_as_u64[1]));
return rc;
}
/**
* ibmvmc_open - Open Session
*
* @inode: inode struct
* @file: file struct
*
* Return:
* 0 - Success
* Non-zero - Failure
*/
static int ibmvmc_open(struct inode *inode, struct file *file)
{
struct ibmvmc_file_session *session;
pr_debug("%s: inode = 0x%lx, file = 0x%lx, state = 0x%x\n", __func__,
(unsigned long)inode, (unsigned long)file,
ibmvmc.state);
session = kzalloc(sizeof(*session), GFP_KERNEL);
if (!session)
return -ENOMEM;
session->file = file;
file->private_data = session;
return 0;
}
/**
* ibmvmc_close - Close Session
*
* @inode: inode struct
* @file: file struct
*
* Return:
* 0 - Success
* Non-zero - Failure
*/
static int ibmvmc_close(struct inode *inode, struct file *file)
{
struct ibmvmc_file_session *session;
struct ibmvmc_hmc *hmc;
int rc = 0;
unsigned long flags;
pr_debug("%s: file = 0x%lx, state = 0x%x\n", __func__,
(unsigned long)file, ibmvmc.state);
session = file->private_data;
if (!session)
return -EIO;
hmc = session->hmc;
if (hmc) {
if (!hmc->adapter)
return -EIO;
if (ibmvmc.state == ibmvmc_state_failed) {
dev_warn(hmc->adapter->dev, "close: state_failed\n");
return -EIO;
}
spin_lock_irqsave(&hmc->lock, flags);
if (hmc->state >= ibmhmc_state_opening) {
rc = ibmvmc_send_close(hmc);
if (rc)
dev_warn(hmc->adapter->dev, "close: send_close failed.\n");
}
spin_unlock_irqrestore(&hmc->lock, flags);
}
kzfree(session);
return rc;
}
/**
* ibmvmc_read - Read
*
* @file: file struct
* @buf: Character buffer
* @nbytes: Size in bytes
* @ppos: Offset
*
* Return:
* 0 - Success
* Non-zero - Failure
*/
static ssize_t ibmvmc_read(struct file *file, char *buf, size_t nbytes,
loff_t *ppos)
{
struct ibmvmc_file_session *session;
struct ibmvmc_hmc *hmc;
struct crq_server_adapter *adapter;
struct ibmvmc_buffer *buffer;
ssize_t n;
ssize_t retval = 0;
unsigned long flags;
DEFINE_WAIT(wait);
pr_debug("ibmvmc: read: file = 0x%lx, buf = 0x%lx, nbytes = 0x%lx\n",
(unsigned long)file, (unsigned long)buf,
(unsigned long)nbytes);
if (nbytes == 0)
return 0;
if (nbytes > ibmvmc.max_mtu) {
pr_warn("ibmvmc: read: nbytes invalid 0x%x\n",
(unsigned int)nbytes);
return -EINVAL;
}
session = file->private_data;
if (!session) {
pr_warn("ibmvmc: read: no session\n");
return -EIO;
}
hmc = session->hmc;
if (!hmc) {
pr_warn("ibmvmc: read: no hmc\n");
return -EIO;
}
adapter = hmc->adapter;
if (!adapter) {
pr_warn("ibmvmc: read: no adapter\n");
return -EIO;
}
do {
prepare_to_wait(&ibmvmc_read_wait, &wait, TASK_INTERRUPTIBLE);
spin_lock_irqsave(&hmc->lock, flags);
if (hmc->queue_tail != hmc->queue_head)
/* Data is available */
break;
spin_unlock_irqrestore(&hmc->lock, flags);
if (!session->valid) {
retval = -EBADFD;
goto out;
}
if (file->f_flags & O_NONBLOCK) {
retval = -EAGAIN;
goto out;
}
schedule();
if (signal_pending(current)) {
retval = -ERESTARTSYS;
goto out;
}
} while (1);
buffer = &(hmc->buffer[hmc->queue_outbound_msgs[hmc->queue_tail]]);
hmc->queue_tail++;
if (hmc->queue_tail == ibmvmc_max_buf_pool_size)
hmc->queue_tail = 0;
spin_unlock_irqrestore(&hmc->lock, flags);
nbytes = min_t(size_t, nbytes, buffer->msg_len);
n = copy_to_user((void *)buf, buffer->real_addr_local, nbytes);
dev_dbg(adapter->dev, "read: copy to user nbytes = 0x%lx.\n", nbytes);
ibmvmc_free_hmc_buffer(hmc, buffer);
retval = nbytes;
if (n) {
dev_warn(adapter->dev, "read: copy to user failed.\n");
retval = -EFAULT;
}
out:
finish_wait(&ibmvmc_read_wait, &wait);
dev_dbg(adapter->dev, "read: out %ld\n", retval);
return retval;
}
/**
* ibmvmc_poll - Poll
*
* @file: file struct
* @wait: Poll Table