summaryrefslogtreecommitdiff
path: root/drivers/vdpa/solidrun/snet_main.c
blob: 99428a04068d2d4f1c3507455a63dd522187b745 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
// SPDX-License-Identifier: GPL-2.0-only
/*
 * SolidRun DPU driver for control plane
 *
 * Copyright (C) 2022-2023 SolidRun
 *
 * Author: Alvaro Karsz <alvaro.karsz@solid-run.com>
 *
 */
#include <linux/iopoll.h>

#include "snet_vdpa.h"

/* SNET DPU device ID */
#define SNET_DEVICE_ID          0x1000
/* SNET signature */
#define SNET_SIGNATURE          0xD0D06363
/* Max. config version that we can work with */
#define SNET_CFG_VERSION        0x2
/* Queue align */
#define SNET_QUEUE_ALIGNMENT    PAGE_SIZE
/* Kick value to notify that new data is available */
#define SNET_KICK_VAL           0x1
#define SNET_CONFIG_OFF         0x0
/* How long we are willing to wait for a SNET device */
#define SNET_DETECT_TIMEOUT	5000000
/* How long should we wait for the DPU to read our config */
#define SNET_READ_CFG_TIMEOUT	3000000
/* Size of configs written to the DPU */
#define SNET_GENERAL_CFG_LEN	36
#define SNET_GENERAL_CFG_VQ_LEN	40

static struct snet *vdpa_to_snet(struct vdpa_device *vdpa)
{
	return container_of(vdpa, struct snet, vdpa);
}

static irqreturn_t snet_cfg_irq_hndlr(int irq, void *data)
{
	struct snet *snet = data;
	/* Call callback if any */
	if (likely(snet->cb.callback))
		return snet->cb.callback(snet->cb.private);

	return IRQ_HANDLED;
}

static irqreturn_t snet_vq_irq_hndlr(int irq, void *data)
{
	struct snet_vq *vq = data;
	/* Call callback if any */
	if (likely(vq->cb.callback))
		return vq->cb.callback(vq->cb.private);

	return IRQ_HANDLED;
}

static void snet_free_irqs(struct snet *snet)
{
	struct psnet *psnet = snet->psnet;
	struct pci_dev *pdev;
	u32 i;

	/* Which Device allcoated the IRQs? */
	if (PSNET_FLAG_ON(psnet, SNET_CFG_FLAG_IRQ_PF))
		pdev = snet->pdev->physfn;
	else
		pdev = snet->pdev;

	/* Free config's IRQ */
	if (snet->cfg_irq != -1) {
		devm_free_irq(&pdev->dev, snet->cfg_irq, snet);
		snet->cfg_irq = -1;
	}
	/* Free VQ IRQs */
	for (i = 0; i < snet->cfg->vq_num; i++) {
		if (snet->vqs[i] && snet->vqs[i]->irq != -1) {
			devm_free_irq(&pdev->dev, snet->vqs[i]->irq, snet->vqs[i]);
			snet->vqs[i]->irq = -1;
		}
	}

	/* IRQ vectors are freed when the pci remove callback is called */
}

static int snet_set_vq_address(struct vdpa_device *vdev, u16 idx, u64 desc_area,
			       u64 driver_area, u64 device_area)
{
	struct snet *snet = vdpa_to_snet(vdev);
	/* save received parameters in vqueue sturct */
	snet->vqs[idx]->desc_area = desc_area;
	snet->vqs[idx]->driver_area = driver_area;
	snet->vqs[idx]->device_area = device_area;

	return 0;
}

static void snet_set_vq_num(struct vdpa_device *vdev, u16 idx, u32 num)
{
	struct snet *snet = vdpa_to_snet(vdev);
	/* save num in vqueue */
	snet->vqs[idx]->num = num;
}

static void snet_kick_vq(struct vdpa_device *vdev, u16 idx)
{
	struct snet *snet = vdpa_to_snet(vdev);
	/* not ready - ignore */
	if (unlikely(!snet->vqs[idx]->ready))
		return;

	iowrite32(SNET_KICK_VAL, snet->vqs[idx]->kick_ptr);
}

static void snet_kick_vq_with_data(struct vdpa_device *vdev, u32 data)
{
	struct snet *snet = vdpa_to_snet(vdev);
	u16 idx = data & 0xFFFF;

	/* not ready - ignore */
	if (unlikely(!snet->vqs[idx]->ready))
		return;

	iowrite32((data & 0xFFFF0000) | SNET_KICK_VAL, snet->vqs[idx]->kick_ptr);
}

static void snet_set_vq_cb(struct vdpa_device *vdev, u16 idx, struct vdpa_callback *cb)
{
	struct snet *snet = vdpa_to_snet(vdev);

	snet->vqs[idx]->cb.callback = cb->callback;
	snet->vqs[idx]->cb.private = cb->private;
}

static void snet_set_vq_ready(struct vdpa_device *vdev, u16 idx, bool ready)
{
	struct snet *snet = vdpa_to_snet(vdev);

	snet->vqs[idx]->ready = ready;
}

static bool snet_get_vq_ready(struct vdpa_device *vdev, u16 idx)
{
	struct snet *snet = vdpa_to_snet(vdev);

	return snet->vqs[idx]->ready;
}

static bool snet_vq_state_is_initial(struct snet *snet, const struct vdpa_vq_state *state)
{
	if (SNET_HAS_FEATURE(snet, VIRTIO_F_RING_PACKED)) {
		const struct vdpa_vq_state_packed *p = &state->packed;

		if (p->last_avail_counter == 1 && p->last_used_counter == 1 &&
		    p->last_avail_idx == 0 && p->last_used_idx == 0)
			return true;
	} else {
		const struct vdpa_vq_state_split *s = &state->split;

		if (s->avail_index == 0)
			return true;
	}

	return false;
}

static int snet_set_vq_state(struct vdpa_device *vdev, u16 idx, const struct vdpa_vq_state *state)
{
	struct snet *snet = vdpa_to_snet(vdev);

	/* We can set any state for config version 2+ */
	if (SNET_CFG_VER(snet, 2)) {
		memcpy(&snet->vqs[idx]->vq_state, state, sizeof(*state));
		return 0;
	}

	/* Older config - we can't set the VQ state.
	 * Return 0 only if this is the initial state we use in the DPU.
	 */
	if (snet_vq_state_is_initial(snet, state))
		return 0;

	return -EOPNOTSUPP;
}

static int snet_get_vq_state(struct vdpa_device *vdev, u16 idx, struct vdpa_vq_state *state)
{
	struct snet *snet = vdpa_to_snet(vdev);

	return snet_read_vq_state(snet, idx, state);
}

static int snet_get_vq_irq(struct vdpa_device *vdev, u16 idx)
{
	struct snet *snet = vdpa_to_snet(vdev);

	return snet->vqs[idx]->irq;
}

static u32 snet_get_vq_align(struct vdpa_device *vdev)
{
	return (u32)SNET_QUEUE_ALIGNMENT;
}

static int snet_reset_dev(struct snet *snet)
{
	struct pci_dev *pdev = snet->pdev;
	int ret = 0;
	u32 i;

	/* If status is 0, nothing to do */
	if (!snet->status)
		return 0;

	/* If DPU started, destroy it */
	if (snet->status & VIRTIO_CONFIG_S_DRIVER_OK)
		ret = snet_destroy_dev(snet);

	/* Clear VQs */
	for (i = 0; i < snet->cfg->vq_num; i++) {
		if (!snet->vqs[i])
			continue;
		snet->vqs[i]->cb.callback = NULL;
		snet->vqs[i]->cb.private = NULL;
		snet->vqs[i]->desc_area = 0;
		snet->vqs[i]->device_area = 0;
		snet->vqs[i]->driver_area = 0;
		snet->vqs[i]->ready = false;
	}

	/* Clear config callback */
	snet->cb.callback = NULL;
	snet->cb.private = NULL;
	/* Free IRQs */
	snet_free_irqs(snet);
	/* Reset status */
	snet->status = 0;
	snet->dpu_ready = false;

	if (ret)
		SNET_WARN(pdev, "Incomplete reset to SNET[%u] device, err: %d\n", snet->sid, ret);
	else
		SNET_DBG(pdev, "Reset SNET[%u] device\n", snet->sid);

	return 0;
}

static int snet_reset(struct vdpa_device *vdev)
{
	struct snet *snet = vdpa_to_snet(vdev);

	return snet_reset_dev(snet);
}

static size_t snet_get_config_size(struct vdpa_device *vdev)
{
	struct snet *snet = vdpa_to_snet(vdev);

	return (size_t)snet->cfg->cfg_size;
}

static u64 snet_get_features(struct vdpa_device *vdev)
{
	struct snet *snet = vdpa_to_snet(vdev);

	return snet->cfg->features;
}

static int snet_set_drv_features(struct vdpa_device *vdev, u64 features)
{
	struct snet *snet = vdpa_to_snet(vdev);

	snet->negotiated_features = snet->cfg->features & features;
	return 0;
}

static u64 snet_get_drv_features(struct vdpa_device *vdev)
{
	struct snet *snet = vdpa_to_snet(vdev);

	return snet->negotiated_features;
}

static u16 snet_get_vq_num_max(struct vdpa_device *vdev)
{
	struct snet *snet = vdpa_to_snet(vdev);

	return (u16)snet->cfg->vq_size;
}

static void snet_set_config_cb(struct vdpa_device *vdev, struct vdpa_callback *cb)
{
	struct snet *snet = vdpa_to_snet(vdev);

	snet->cb.callback = cb->callback;
	snet->cb.private = cb->private;
}

static u32 snet_get_device_id(struct vdpa_device *vdev)
{
	struct snet *snet = vdpa_to_snet(vdev);

	return snet->cfg->virtio_id;
}

static u32 snet_get_vendor_id(struct vdpa_device *vdev)
{
	return (u32)PCI_VENDOR_ID_SOLIDRUN;
}

static u8 snet_get_status(struct vdpa_device *vdev)
{
	struct snet *snet = vdpa_to_snet(vdev);

	return snet->status;
}

static int snet_write_conf(struct snet *snet)
{
	u32 off, i, tmp;
	int ret;

	/* No need to write the config twice */
	if (snet->dpu_ready)
		return true;

	/* Snet data :
	 *
	 * General data: SNET_GENERAL_CFG_LEN bytes long
	 *  0             0x4       0x8        0xC               0x10      0x14        0x1C     0x24
	 *  | MAGIC NUMBER | CFG VER | SNET SID | NUMBER OF QUEUES | IRQ IDX | FEATURES |  RSVD  |
	 *
	 * For every VQ: SNET_GENERAL_CFG_VQ_LEN bytes long
	 * 0                          0x4        0x8
	 * |  VQ SID  AND  QUEUE SIZE | IRQ Index |
	 * |             DESC AREA                |
	 * |            DEVICE AREA               |
	 * |            DRIVER AREA               |
	 * |    VQ STATE (CFG 2+)     |   RSVD    |
	 *
	 * Magic number should be written last, this is the DPU indication that the data is ready
	 */

	/* Init offset */
	off = snet->psnet->cfg.host_cfg_off;

	/* Ignore magic number for now */
	off += 4;
	snet_write32(snet, off, snet->psnet->negotiated_cfg_ver);
	off += 4;
	snet_write32(snet, off, snet->sid);
	off += 4;
	snet_write32(snet, off, snet->cfg->vq_num);
	off += 4;
	snet_write32(snet, off, snet->cfg_irq_idx);
	off += 4;
	snet_write64(snet, off, snet->negotiated_features);
	off += 8;
	/* Ignore reserved */
	off += 8;
	/* Write VQs */
	for (i = 0 ; i < snet->cfg->vq_num ; i++) {
		tmp = (i << 16) | (snet->vqs[i]->num & 0xFFFF);
		snet_write32(snet, off, tmp);
		off += 4;
		snet_write32(snet, off, snet->vqs[i]->irq_idx);
		off += 4;
		snet_write64(snet, off, snet->vqs[i]->desc_area);
		off += 8;
		snet_write64(snet, off, snet->vqs[i]->device_area);
		off += 8;
		snet_write64(snet, off, snet->vqs[i]->driver_area);
		off += 8;
		/* Write VQ state if config version is 2+ */
		if (SNET_CFG_VER(snet, 2))
			snet_write32(snet, off, *(u32 *)&snet->vqs[i]->vq_state);
		off += 4;

		/* Ignore reserved */
		off += 4;
	}

	/* Write magic number - data is ready */
	snet_write32(snet, snet->psnet->cfg.host_cfg_off, SNET_SIGNATURE);

	/* The DPU will ACK the config by clearing the signature */
	ret = readx_poll_timeout(ioread32, snet->bar + snet->psnet->cfg.host_cfg_off,
				 tmp, !tmp, 10, SNET_READ_CFG_TIMEOUT);
	if (ret) {
		SNET_ERR(snet->pdev, "Timeout waiting for the DPU to read the config\n");
		return false;
	}

	/* set DPU flag */
	snet->dpu_ready = true;

	return true;
}

static int snet_request_irqs(struct pci_dev *pdev, struct snet *snet)
{
	int ret, i, irq;

	/* Request config IRQ */
	irq = pci_irq_vector(pdev, snet->cfg_irq_idx);
	ret = devm_request_irq(&pdev->dev, irq, snet_cfg_irq_hndlr, 0,
			       snet->cfg_irq_name, snet);
	if (ret) {
		SNET_ERR(pdev, "Failed to request IRQ\n");
		return ret;
	}
	snet->cfg_irq = irq;

	/* Request IRQ for every VQ */
	for (i = 0; i < snet->cfg->vq_num; i++) {
		irq = pci_irq_vector(pdev, snet->vqs[i]->irq_idx);
		ret = devm_request_irq(&pdev->dev, irq, snet_vq_irq_hndlr, 0,
				       snet->vqs[i]->irq_name, snet->vqs[i]);
		if (ret) {
			SNET_ERR(pdev, "Failed to request IRQ\n");
			return ret;
		}
		snet->vqs[i]->irq = irq;
	}
	return 0;
}

static void snet_set_status(struct vdpa_device *vdev, u8 status)
{
	struct snet *snet = vdpa_to_snet(vdev);
	struct psnet *psnet = snet->psnet;
	struct pci_dev *pdev = snet->pdev;
	int ret;
	bool pf_irqs;

	if (status == snet->status)
		return;

	if ((status & VIRTIO_CONFIG_S_DRIVER_OK) &&
	    !(snet->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
		/* Request IRQs */
		pf_irqs = PSNET_FLAG_ON(psnet, SNET_CFG_FLAG_IRQ_PF);
		ret = snet_request_irqs(pf_irqs ? pdev->physfn : pdev, snet);
		if (ret)
			goto set_err;

		/* Write config to the DPU */
		if (snet_write_conf(snet)) {
			SNET_INFO(pdev, "Create SNET[%u] device\n", snet->sid);
		} else {
			snet_free_irqs(snet);
			goto set_err;
		}
	}

	/* Save the new status */
	snet->status = status;
	return;

set_err:
	snet->status |= VIRTIO_CONFIG_S_FAILED;
}

static void snet_get_config(struct vdpa_device *vdev, unsigned int offset,
			    void *buf, unsigned int len)
{
	struct snet *snet = vdpa_to_snet(vdev);
	void __iomem *cfg_ptr = snet->cfg->virtio_cfg + offset;
	u8 *buf_ptr = buf;
	u32 i;

	/* check for offset error */
	if (offset + len > snet->cfg->cfg_size)
		return;

	/* Write into buffer */
	for (i = 0; i < len; i++)
		*buf_ptr++ = ioread8(cfg_ptr + i);
}

static void snet_set_config(struct vdpa_device *vdev, unsigned int offset,
			    const void *buf, unsigned int len)
{
	struct snet *snet = vdpa_to_snet(vdev);
	void __iomem *cfg_ptr = snet->cfg->virtio_cfg + offset;
	const u8 *buf_ptr = buf;
	u32 i;

	/* check for offset error */
	if (offset + len > snet->cfg->cfg_size)
		return;

	/* Write into PCI BAR */
	for (i = 0; i < len; i++)
		iowrite8(*buf_ptr++, cfg_ptr + i);
}

static int snet_suspend(struct vdpa_device *vdev)
{
	struct snet *snet = vdpa_to_snet(vdev);
	int ret;

	ret = snet_suspend_dev(snet);
	if (ret)
		SNET_ERR(snet->pdev, "SNET[%u] suspend failed, err: %d\n", snet->sid, ret);
	else
		SNET_DBG(snet->pdev, "Suspend SNET[%u] device\n", snet->sid);

	return ret;
}

static int snet_resume(struct vdpa_device *vdev)
{
	struct snet *snet = vdpa_to_snet(vdev);
	int ret;

	ret = snet_resume_dev(snet);
	if (ret)
		SNET_ERR(snet->pdev, "SNET[%u] resume failed, err: %d\n", snet->sid, ret);
	else
		SNET_DBG(snet->pdev, "Resume SNET[%u] device\n", snet->sid);

	return ret;
}

static const struct vdpa_config_ops snet_config_ops = {
	.set_vq_address         = snet_set_vq_address,
	.set_vq_num             = snet_set_vq_num,
	.kick_vq                = snet_kick_vq,
	.kick_vq_with_data	= snet_kick_vq_with_data,
	.set_vq_cb              = snet_set_vq_cb,
	.set_vq_ready           = snet_set_vq_ready,
	.get_vq_ready           = snet_get_vq_ready,
	.set_vq_state           = snet_set_vq_state,
	.get_vq_state           = snet_get_vq_state,
	.get_vq_irq		= snet_get_vq_irq,
	.get_vq_align           = snet_get_vq_align,
	.reset                  = snet_reset,
	.get_config_size        = snet_get_config_size,
	.get_device_features    = snet_get_features,
	.set_driver_features    = snet_set_drv_features,
	.get_driver_features    = snet_get_drv_features,
	.get_vq_num_min         = snet_get_vq_num_max,
	.get_vq_num_max         = snet_get_vq_num_max,
	.set_config_cb          = snet_set_config_cb,
	.get_device_id          = snet_get_device_id,
	.get_vendor_id          = snet_get_vendor_id,
	.get_status             = snet_get_status,
	.set_status             = snet_set_status,
	.get_config             = snet_get_config,
	.set_config             = snet_set_config,
	.suspend		= snet_suspend,
	.resume			= snet_resume,
};

static int psnet_open_pf_bar(struct pci_dev *pdev, struct psnet *psnet)
{
	char name[50];
	int ret, i, mask = 0;
	/* We don't know which BAR will be used to communicate..
	 * We will map every bar with len > 0.
	 *
	 * Later, we will discover the BAR and unmap all other BARs.
	 */
	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
		if (pci_resource_len(pdev, i))
			mask |= (1 << i);
	}

	/* No BAR can be used.. */
	if (!mask) {
		SNET_ERR(pdev, "Failed to find a PCI BAR\n");
		return -ENODEV;
	}

	snprintf(name, sizeof(name), "psnet[%s]-bars", pci_name(pdev));
	ret = pcim_iomap_regions(pdev, mask, name);
	if (ret) {
		SNET_ERR(pdev, "Failed to request and map PCI BARs\n");
		return ret;
	}

	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
		if (mask & (1 << i))
			psnet->bars[i] = pcim_iomap_table(pdev)[i];
	}

	return 0;
}

static int snet_open_vf_bar(struct pci_dev *pdev, struct snet *snet)
{
	char name[50];
	int ret;

	snprintf(name, sizeof(name), "snet[%s]-bar", pci_name(pdev));
	/* Request and map BAR */
	ret = pcim_iomap_regions(pdev, BIT(snet->psnet->cfg.vf_bar), name);
	if (ret) {
		SNET_ERR(pdev, "Failed to request and map PCI BAR for a VF\n");
		return ret;
	}

	snet->bar = pcim_iomap_table(pdev)[snet->psnet->cfg.vf_bar];

	return 0;
}

static void snet_free_cfg(struct snet_cfg *cfg)
{
	u32 i;

	if (!cfg->devs)
		return;

	/* Free devices */
	for (i = 0; i < cfg->devices_num; i++) {
		if (!cfg->devs[i])
			break;

		kfree(cfg->devs[i]);
	}
	/* Free pointers to devices */
	kfree(cfg->devs);
}

/* Detect which BAR is used for communication with the device. */
static int psnet_detect_bar(struct psnet *psnet, u32 off)
{
	unsigned long exit_time;
	int i;

	exit_time = jiffies + usecs_to_jiffies(SNET_DETECT_TIMEOUT);

	/* SNET DPU will write SNET's signature when the config is ready. */
	while (time_before(jiffies, exit_time)) {
		for (i = 0; i < PCI_STD_NUM_BARS; i++) {
			/* Is this BAR mapped? */
			if (!psnet->bars[i])
				continue;

			if (ioread32(psnet->bars[i] + off) == SNET_SIGNATURE)
				return i;
		}
		usleep_range(1000, 10000);
	}

	return -ENODEV;
}

static void psnet_unmap_unused_bars(struct pci_dev *pdev, struct psnet *psnet)
{
	int i, mask = 0;

	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
		if (psnet->bars[i] && i != psnet->barno)
			mask |= (1 << i);
	}

	if (mask)
		pcim_iounmap_regions(pdev, mask);
}

/* Read SNET config from PCI BAR */
static int psnet_read_cfg(struct pci_dev *pdev, struct psnet *psnet)
{
	struct snet_cfg *cfg = &psnet->cfg;
	u32 i, off;
	int barno;

	/* Move to where the config starts */
	off = SNET_CONFIG_OFF;

	/* Find BAR used for communication */
	barno = psnet_detect_bar(psnet, off);
	if (barno < 0) {
		SNET_ERR(pdev, "SNET config is not ready.\n");
		return barno;
	}

	/* Save used BAR number and unmap all other BARs */
	psnet->barno = barno;
	SNET_DBG(pdev, "Using BAR number %d\n", barno);

	psnet_unmap_unused_bars(pdev, psnet);

	/* load config from BAR */
	cfg->key = psnet_read32(psnet, off);
	off += 4;
	cfg->cfg_size = psnet_read32(psnet, off);
	off += 4;
	cfg->cfg_ver = psnet_read32(psnet, off);
	off += 4;
	/* The negotiated config version is the lower one between this driver's config
	 * and the DPU's.
	 */
	psnet->negotiated_cfg_ver = min_t(u32, cfg->cfg_ver, SNET_CFG_VERSION);
	SNET_DBG(pdev, "SNET config version %u\n", psnet->negotiated_cfg_ver);

	cfg->vf_num = psnet_read32(psnet, off);
	off += 4;
	cfg->vf_bar = psnet_read32(psnet, off);
	off += 4;
	cfg->host_cfg_off = psnet_read32(psnet, off);
	off += 4;
	cfg->max_size_host_cfg = psnet_read32(psnet, off);
	off += 4;
	cfg->virtio_cfg_off = psnet_read32(psnet, off);
	off += 4;
	cfg->kick_off = psnet_read32(psnet, off);
	off += 4;
	cfg->hwmon_off = psnet_read32(psnet, off);
	off += 4;
	cfg->ctrl_off = psnet_read32(psnet, off);
	off += 4;
	cfg->flags = psnet_read32(psnet, off);
	off += 4;
	/* Ignore Reserved */
	off += sizeof(cfg->rsvd);

	cfg->devices_num = psnet_read32(psnet, off);
	off += 4;
	/* Allocate memory to hold pointer to the devices */
	cfg->devs = kcalloc(cfg->devices_num, sizeof(void *), GFP_KERNEL);
	if (!cfg->devs)
		return -ENOMEM;

	/* Load device configuration from BAR */
	for (i = 0; i < cfg->devices_num; i++) {
		cfg->devs[i] = kzalloc(sizeof(*cfg->devs[i]), GFP_KERNEL);
		if (!cfg->devs[i]) {
			snet_free_cfg(cfg);
			return -ENOMEM;
		}
		/* Read device config */
		cfg->devs[i]->virtio_id = psnet_read32(psnet, off);
		off += 4;
		cfg->devs[i]->vq_num = psnet_read32(psnet, off);
		off += 4;
		cfg->devs[i]->vq_size = psnet_read32(psnet, off);
		off += 4;
		cfg->devs[i]->vfid = psnet_read32(psnet, off);
		off += 4;
		cfg->devs[i]->features = psnet_read64(psnet, off);
		off += 8;
		/* Ignore Reserved */
		off += sizeof(cfg->devs[i]->rsvd);

		cfg->devs[i]->cfg_size = psnet_read32(psnet, off);
		off += 4;

		/* Is the config witten to the DPU going to be too big? */
		if (SNET_GENERAL_CFG_LEN + SNET_GENERAL_CFG_VQ_LEN * cfg->devs[i]->vq_num >
		    cfg->max_size_host_cfg) {
			SNET_ERR(pdev, "Failed to read SNET config, the config is too big..\n");
			snet_free_cfg(cfg);
			return -EINVAL;
		}
	}
	return 0;
}

static int psnet_alloc_irq_vector(struct pci_dev *pdev, struct psnet *psnet)
{
	int ret = 0;
	u32 i, irq_num = 0;

	/* Let's count how many IRQs we need, 1 for every VQ + 1 for config change */
	for (i = 0; i < psnet->cfg.devices_num; i++)
		irq_num += psnet->cfg.devs[i]->vq_num + 1;

	ret = pci_alloc_irq_vectors(pdev, irq_num, irq_num, PCI_IRQ_MSIX);
	if (ret != irq_num) {
		SNET_ERR(pdev, "Failed to allocate IRQ vectors\n");
		return ret;
	}
	SNET_DBG(pdev, "Allocated %u IRQ vectors from physical function\n", irq_num);

	return 0;
}

static int snet_alloc_irq_vector(struct pci_dev *pdev, struct snet_dev_cfg *snet_cfg)
{
	int ret = 0;
	u32 irq_num;

	/* We want 1 IRQ for every VQ + 1 for config change events */
	irq_num = snet_cfg->vq_num + 1;

	ret = pci_alloc_irq_vectors(pdev, irq_num, irq_num, PCI_IRQ_MSIX);
	if (ret <= 0) {
		SNET_ERR(pdev, "Failed to allocate IRQ vectors\n");
		return ret;
	}

	return 0;
}

static void snet_free_vqs(struct snet *snet)
{
	u32 i;

	if (!snet->vqs)
		return;

	for (i = 0 ; i < snet->cfg->vq_num ; i++) {
		if (!snet->vqs[i])
			break;

		kfree(snet->vqs[i]);
	}
	kfree(snet->vqs);
}

static int snet_build_vqs(struct snet *snet)
{
	u32 i;
	/* Allocate the VQ pointers array */
	snet->vqs = kcalloc(snet->cfg->vq_num, sizeof(void *), GFP_KERNEL);
	if (!snet->vqs)
		return -ENOMEM;

	/* Allocate the VQs */
	for (i = 0; i < snet->cfg->vq_num; i++) {
		snet->vqs[i] = kzalloc(sizeof(*snet->vqs[i]), GFP_KERNEL);
		if (!snet->vqs[i]) {
			snet_free_vqs(snet);
			return -ENOMEM;
		}
		/* Reset IRQ num */
		snet->vqs[i]->irq = -1;
		/* VQ serial ID */
		snet->vqs[i]->sid = i;
		/* Kick address - every VQ gets 4B */
		snet->vqs[i]->kick_ptr = snet->bar + snet->psnet->cfg.kick_off +
					 snet->vqs[i]->sid * 4;
		/* Clear kick address for this VQ */
		iowrite32(0, snet->vqs[i]->kick_ptr);
	}
	return 0;
}

static int psnet_get_next_irq_num(struct psnet *psnet)
{
	int irq;

	spin_lock(&psnet->lock);
	irq = psnet->next_irq++;
	spin_unlock(&psnet->lock);

	return irq;
}

static void snet_reserve_irq_idx(struct pci_dev *pdev, struct snet *snet)
{
	struct psnet *psnet = snet->psnet;
	int  i;

	/* one IRQ for every VQ, and one for config changes */
	snet->cfg_irq_idx = psnet_get_next_irq_num(psnet);
	snprintf(snet->cfg_irq_name, SNET_NAME_SIZE, "snet[%s]-cfg[%d]",
		 pci_name(pdev), snet->cfg_irq_idx);

	for (i = 0; i < snet->cfg->vq_num; i++) {
		/* Get next free IRQ ID */
		snet->vqs[i]->irq_idx = psnet_get_next_irq_num(psnet);
		/* Write IRQ name */
		snprintf(snet->vqs[i]->irq_name, SNET_NAME_SIZE, "snet[%s]-vq[%d]",
			 pci_name(pdev), snet->vqs[i]->irq_idx);
	}
}

/* Find a device config based on virtual function id */
static struct snet_dev_cfg *snet_find_dev_cfg(struct snet_cfg *cfg, u32 vfid)
{
	u32 i;

	for (i = 0; i < cfg->devices_num; i++) {
		if (cfg->devs[i]->vfid == vfid)
			return cfg->devs[i];
	}
	/* Oppss.. no config found.. */
	return NULL;
}

/* Probe function for a physical PCI function */
static int snet_vdpa_probe_pf(struct pci_dev *pdev)
{
	struct psnet *psnet;
	int ret = 0;
	bool pf_irqs = false;

	ret = pcim_enable_device(pdev);
	if (ret) {
		SNET_ERR(pdev, "Failed to enable PCI device\n");
		return ret;
	}

	/* Allocate a PCI physical function device */
	psnet = kzalloc(sizeof(*psnet), GFP_KERNEL);
	if (!psnet)
		return -ENOMEM;

	/* Init PSNET spinlock */
	spin_lock_init(&psnet->lock);

	pci_set_master(pdev);
	pci_set_drvdata(pdev, psnet);

	/* Open SNET MAIN BAR */
	ret = psnet_open_pf_bar(pdev, psnet);
	if (ret)
		goto free_psnet;

	/* Try to read SNET's config from PCI BAR */
	ret = psnet_read_cfg(pdev, psnet);
	if (ret)
		goto free_psnet;

	/* If SNET_CFG_FLAG_IRQ_PF flag is set, we should use
	 * PF MSI-X vectors
	 */
	pf_irqs = PSNET_FLAG_ON(psnet, SNET_CFG_FLAG_IRQ_PF);

	if (pf_irqs) {
		ret = psnet_alloc_irq_vector(pdev, psnet);
		if (ret)
			goto free_cfg;
	}

	SNET_DBG(pdev, "Enable %u virtual functions\n", psnet->cfg.vf_num);
	ret = pci_enable_sriov(pdev, psnet->cfg.vf_num);
	if (ret) {
		SNET_ERR(pdev, "Failed to enable SR-IOV\n");
		goto free_irq;
	}

	/* Create HW monitor device */
	if (PSNET_FLAG_ON(psnet, SNET_CFG_FLAG_HWMON)) {
#if IS_ENABLED(CONFIG_HWMON)
		psnet_create_hwmon(pdev);
#else
		SNET_WARN(pdev, "Can't start HWMON, CONFIG_HWMON is not enabled\n");
#endif
	}

	return 0;

free_irq:
	if (pf_irqs)
		pci_free_irq_vectors(pdev);
free_cfg:
	snet_free_cfg(&psnet->cfg);
free_psnet:
	kfree(psnet);
	return ret;
}

/* Probe function for a virtual PCI function */
static int snet_vdpa_probe_vf(struct pci_dev *pdev)
{
	struct pci_dev *pdev_pf = pdev->physfn;
	struct psnet *psnet = pci_get_drvdata(pdev_pf);
	struct snet_dev_cfg *dev_cfg;
	struct snet *snet;
	u32 vfid;
	int ret;
	bool pf_irqs = false;

	/* Get virtual function id.
	 * (the DPU counts the VFs from 1)
	 */
	ret = pci_iov_vf_id(pdev);
	if (ret < 0) {
		SNET_ERR(pdev, "Failed to find a VF id\n");
		return ret;
	}
	vfid = ret + 1;

	/* Find the snet_dev_cfg based on vfid */
	dev_cfg = snet_find_dev_cfg(&psnet->cfg, vfid);
	if (!dev_cfg) {
		SNET_WARN(pdev, "Failed to find a VF config..\n");
		return -ENODEV;
	}

	/* Which PCI device should allocate the IRQs?
	 * If the SNET_CFG_FLAG_IRQ_PF flag set, the PF device allocates the IRQs
	 */
	pf_irqs = PSNET_FLAG_ON(psnet, SNET_CFG_FLAG_IRQ_PF);

	ret = pcim_enable_device(pdev);
	if (ret) {
		SNET_ERR(pdev, "Failed to enable PCI VF device\n");
		return ret;
	}

	/* Request for MSI-X IRQs */
	if (!pf_irqs) {
		ret = snet_alloc_irq_vector(pdev, dev_cfg);
		if (ret)
			return ret;
	}

	/* Allocate vdpa device */
	snet = vdpa_alloc_device(struct snet, vdpa, &pdev->dev, &snet_config_ops, 1, 1, NULL,
				 false);
	if (!snet) {
		SNET_ERR(pdev, "Failed to allocate a vdpa device\n");
		ret = -ENOMEM;
		goto free_irqs;
	}

	/* Init control mutex and spinlock */
	mutex_init(&snet->ctrl_lock);
	spin_lock_init(&snet->ctrl_spinlock);

	/* Save pci device pointer */
	snet->pdev = pdev;
	snet->psnet = psnet;
	snet->cfg = dev_cfg;
	snet->dpu_ready = false;
	snet->sid = vfid;
	/* Reset IRQ value */
	snet->cfg_irq = -1;

	ret = snet_open_vf_bar(pdev, snet);
	if (ret)
		goto put_device;

	/* Create a VirtIO config pointer */
	snet->cfg->virtio_cfg = snet->bar + snet->psnet->cfg.virtio_cfg_off;

	/* Clear control registers */
	snet_ctrl_clear(snet);

	pci_set_master(pdev);
	pci_set_drvdata(pdev, snet);

	ret = snet_build_vqs(snet);
	if (ret)
		goto put_device;

	/* Reserve IRQ indexes,
	 * The IRQs may be requested and freed multiple times,
	 * but the indexes won't change.
	 */
	snet_reserve_irq_idx(pf_irqs ? pdev_pf : pdev, snet);

	/*set DMA device*/
	snet->vdpa.dma_dev = &pdev->dev;

	/* Register VDPA device */
	ret = vdpa_register_device(&snet->vdpa, snet->cfg->vq_num);
	if (ret) {
		SNET_ERR(pdev, "Failed to register vdpa device\n");
		goto free_vqs;
	}

	return 0;

free_vqs:
	snet_free_vqs(snet);
put_device:
	put_device(&snet->vdpa.dev);
free_irqs:
	if (!pf_irqs)
		pci_free_irq_vectors(pdev);
	return ret;
}

static int snet_vdpa_probe(struct pci_dev *pdev, const struct pci_device_id *id)
{
	if (pdev->is_virtfn)
		return snet_vdpa_probe_vf(pdev);
	else
		return snet_vdpa_probe_pf(pdev);
}

static void snet_vdpa_remove_pf(struct pci_dev *pdev)
{
	struct psnet *psnet = pci_get_drvdata(pdev);

	pci_disable_sriov(pdev);
	/* If IRQs are allocated from the PF, we should free the IRQs */
	if (PSNET_FLAG_ON(psnet, SNET_CFG_FLAG_IRQ_PF))
		pci_free_irq_vectors(pdev);

	snet_free_cfg(&psnet->cfg);
	kfree(psnet);
}

static void snet_vdpa_remove_vf(struct pci_dev *pdev)
{
	struct snet *snet = pci_get_drvdata(pdev);
	struct psnet *psnet = snet->psnet;

	vdpa_unregister_device(&snet->vdpa);
	snet_free_vqs(snet);
	/* If IRQs are allocated from the VF, we should free the IRQs */
	if (!PSNET_FLAG_ON(psnet, SNET_CFG_FLAG_IRQ_PF))
		pci_free_irq_vectors(pdev);
}

static void snet_vdpa_remove(struct pci_dev *pdev)
{
	if (pdev->is_virtfn)
		snet_vdpa_remove_vf(pdev);
	else
		snet_vdpa_remove_pf(pdev);
}

static struct pci_device_id snet_driver_pci_ids[] = {
	{ PCI_DEVICE_SUB(PCI_VENDOR_ID_SOLIDRUN, SNET_DEVICE_ID,
			 PCI_VENDOR_ID_SOLIDRUN, SNET_DEVICE_ID) },
	{ 0 },
};

MODULE_DEVICE_TABLE(pci, snet_driver_pci_ids);

static struct pci_driver snet_vdpa_driver = {
	.name		= "snet-vdpa-driver",
	.id_table	= snet_driver_pci_ids,
	.probe		= snet_vdpa_probe,
	.remove		= snet_vdpa_remove,
};

module_pci_driver(snet_vdpa_driver);

MODULE_AUTHOR("Alvaro Karsz <alvaro.karsz@solid-run.com>");
MODULE_DESCRIPTION("SolidRun vDPA driver");
MODULE_LICENSE("GPL v2");