-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathmcap_lib.c
722 lines (589 loc) · 17 KB
/
mcap_lib.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
/******************************************************************************
* Copyright (C) 2014-2015 Xilinx, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*
*
******************************************************************************/
/*****************************************************************************/
/**
*
* @file mcap_lib.c
* MCAP Interface Library functions
*
******************************************************************************/
#include "mcap_lib.h"
/* Library Specific Definitions */
#define MCAP_VENDOR_ID 0x10EE
#define MCAP_LOOP_COUNT 1000000
#define MCAP_SYNC_DWORD 0xFFFFFFFF
#define MCAP_SYNC_BYTE0 ((MCAP_SYNC_DWORD & 0xFF000000) >> 24)
#define MCAP_SYNC_BYTE1 ((MCAP_SYNC_DWORD & 0x00FF0000) >> 16)
#define MCAP_SYNC_BYTE2 ((MCAP_SYNC_DWORD & 0x0000FF00) >> 8)
#define MCAP_SYNC_BYTE3 ((MCAP_SYNC_DWORD & 0x000000FF) >> 0)
#define MCAP_RBT_FILE ".rbt"
#define MCAP_BIT_FILE ".bit"
#define MCAP_BIN_FILE ".bin"
static char *MCapFindTypeofFile(const char *s1, const char *s2)
{
size_t l1, l2;
l2 = strlen(s2);
if (!l2)
return (char *)s1;
l1 = strlen(s1);
while (l1 >= l2) {
l1--;
if (!strncasecmp(s1, s2, l2))
return (char *)s1;
s1++;
}
return NULL;
}
static u32 MCapProcessRBT(FILE *fptr, u32 *buf)
{
char *raw = NULL;
int i, read;
size_t linelen;
u32 count = 0, len = 0, result = 0;
while ((read = getline(&raw, &linelen, fptr)) != -1) {
if (raw[0] != '1' && raw[1] != '0')
continue;
for (i = 0; i < read - 1; i++) {
if (raw[i] == '1' || raw[i] == '0') {
result = (result << 1) | (raw[i] - 0x30);
count++;
if (count == 32) {
*buf++ = result;
len ++;
result = count = 0;
break;
}
}
}
}
return len;
}
static u32 MCapProcessBIT(FILE *fptr, u32 *buf, int sz)
{
int err;
u8 value, len = 0;
/*
* .bit files are not guaranteed to be aligned with
* the bitstream sync word on a 32-bit boundary. So,
* we need to check every byte here.
*/
while ((err = fread(&value, 1, 1, fptr)) == 1) {
len++; if (value == MCAP_SYNC_BYTE0)
if ((err = fread(&value, 1, 1, fptr)) == 1) {
len++; if (value == MCAP_SYNC_BYTE1)
if ((err = fread(&value, 1, 1, fptr)) == 1) {
len++; if (value == MCAP_SYNC_BYTE2)
if ((err = fread(&value, 1, 1, fptr)) == 1) {
len++; if (value == MCAP_SYNC_BYTE3)
break;
}
}
}
}
if (err != 1 && !feof(fptr)) {
pr_err("Failed to Read BIT file\n");
return 0;
}
if (err != 1 && feof(fptr)) {
pr_err("Failed to find SYNC Word in BIT file\n");
return 0;
}
*buf++ = __bswap_32(MCAP_SYNC_DWORD);
while ((err = fread(buf, sz - len, 1, fptr)) == 1)
;
if (err != 1 && !feof(fptr)) {
pr_err("Failed to Read BIT file\n");
return 0;
}
return (sz - len)/4 + 1;
}
static u32 MCapProcessBIN(FILE *fptr, u32 *buf, int sz)
{
int err;
err = fread(buf, sz, 1, fptr);
if (err != 1 && !feof(fptr)) {
pr_err("Failed to Read BIN file\n");
return 0;
}
return sz/4;
}
static int MCapDoBusWalk(struct mcap_dev *mdev)
{
struct pci_cap *c;
c = pci_find_cap(mdev->pdev, MCAP_EXT_CAP_ID, PCI_CAP_EXTENDED);
if (!c)
return -EMCAPBUSWALK;
mdev->reg_base = c->addr;
return 0;
}
static int MCapClearRequestByConfigure(struct mcap_dev *mdev, u32 *restore)
{
u32 set;
int loop = MCAP_LOOP_COUNT;
set = *restore = MCapRegRead(mdev, MCAP_CONTROL);
if (IsConfigureMCapReqSet(mdev)) {
/* Set 'Mode' and 'In Use by PCIe' bits */
set |= (MCAP_CTRL_MODE_MASK | MCAP_CTRL_IN_USE_MASK);
MCapRegWrite(mdev, MCAP_CONTROL, set);
do {
if (!(IsConfigureMCapReqSet(mdev)))
break;
} while (loop--);
if (!loop) {
pr_err("Failed to clear MCAP Request by config bit\n");
MCapRegWrite(mdev, MCAP_CONTROL, *restore);
return -EMCAPREQ;
}
}
pr_dbg("Request by Configure bit cleared!!\n");
return 0;
}
static int Checkforcompletion(struct mcap_dev *mdev)
{
unsigned long retry_count = 0;
u32 delay;
int sr, i;
sr = MCapRegRead(mdev, MCAP_STATUS);
while (!(sr & MCAP_STS_EOS_MASK)) {
usleep(2);
for (i=0 ; i < EMCAP_EOS_LOOP_COUNT; i++) {
MCapRegWrite(mdev, MCAP_DATA, EMCAP_NOOP_VAL);
}
sr = MCapRegRead(mdev, MCAP_STATUS);
retry_count++;
if (retry_count > EMCAP_EOS_RETRY_COUNT) {
pr_err("Error: The MCAP EOS bit did not assert after");
pr_err(" programming the specified programming file\n");
return -EMCAPREQ;
}
}
return 0;
}
static int MCapWritePartialBitStream(struct mcap_dev *mdev, u32 *data,
int len, u8 bswap)
{
u32 set, restore;
int err, count = 0, i;
if (!data || !len) {
pr_err("Invalid Arguments\n");
return -EMCAPWRITE;
}
err = MCapClearRequestByConfigure(mdev, &restore);
if (err)
return err;
if (IsErrSet(mdev) || IsRegReadComplete(mdev) ||
IsFifoOverflow(mdev)) {
pr_err("Failed to initialize configuring FPGA\n");
MCapRegWrite(mdev, MCAP_CONTROL, restore);
return -EMCAPWRITE;
}
/* Set 'Mode', 'In Use by PCIe' and 'Data Reg Protect' bits */
set = MCapRegRead(mdev, MCAP_CONTROL);
set |= MCAP_CTRL_MODE_MASK | MCAP_CTRL_IN_USE_MASK |
MCAP_CTRL_DATA_REG_PROT_MASK;
/* Clear 'Reset', 'Module Reset' and 'Register Read' bits */
set &= ~(MCAP_CTRL_RESET_MASK | MCAP_CTRL_MOD_RESET_MASK |
MCAP_CTRL_REG_READ_MASK | MCAP_CTRL_DESIGN_SWITCH_MASK);
MCapRegWrite(mdev, MCAP_CONTROL, set);
/* Write Data */
if (!bswap) {
for (count = 0; count < len; count++)
MCapRegWrite(mdev, MCAP_DATA, data[count]);
} else {
for (count = 0; count < len; count++)
MCapRegWrite(mdev, MCAP_DATA, __bswap_32(data[count]));
}
for (i = 0 ; i < EMCAP_EOS_LOOP_COUNT; i++) {
MCapRegWrite(mdev, MCAP_DATA, EMCAP_NOOP_VAL);
}
if (IsErrSet(mdev) || IsFifoOverflow(mdev)) {
pr_err("Failed to Write Bitstream\n");
MCapRegWrite(mdev, MCAP_CONTROL, restore);
MCapFullReset(mdev);
return -EMCAPWRITE;
}
if (!mdev->is_multiplebit) {
pr_info("Info: A partial reconfiguration clear file (-C) was");
pr_info(" loaded without a partial reconfiguration file (-p)");
pr_info(" as result the MCAP Control register was not restored");
pr_info(" to its original value\n\r");
}
return 0;
}
static int MCapWriteBitStream(struct mcap_dev *mdev, u32 *data,
int len, u8 bswap)
{
u32 set, restore;
int err, count = 0;
if (!data || !len) {
pr_err("Invalid Arguments\n");
return -EMCAPWRITE;
}
err = MCapClearRequestByConfigure(mdev, &restore);
if (err)
return err;
if (IsErrSet(mdev) || IsRegReadComplete(mdev) ||
IsFifoOverflow(mdev)) {
pr_err("Failed to initialize configuring FPGA\n");
MCapRegWrite(mdev, MCAP_CONTROL, restore);
return -EMCAPWRITE;
}
if (!mdev->is_multiplebit) {
/* Set 'Mode', 'In Use by PCIe' and 'Data Reg Protect' bits */
set = MCapRegRead(mdev, MCAP_CONTROL);
set |= MCAP_CTRL_MODE_MASK | MCAP_CTRL_IN_USE_MASK |
MCAP_CTRL_DATA_REG_PROT_MASK;
/* Clear 'Reset', 'Module Reset' and 'Register Read' bits */
set &= ~(MCAP_CTRL_RESET_MASK | MCAP_CTRL_MOD_RESET_MASK |
MCAP_CTRL_REG_READ_MASK | MCAP_CTRL_DESIGN_SWITCH_MASK);
MCapRegWrite(mdev, MCAP_CONTROL, set);
}
/* Write Data */
if (!bswap) {
for (count = 0; count < len; count++)
MCapRegWrite(mdev, MCAP_DATA, data[count]);
} else {
for (count = 0; count < len; count++)
MCapRegWrite(mdev, MCAP_DATA, __bswap_32(data[count]));
}
/* Check for Completion */
err = Checkforcompletion(mdev);
if (err)
return -EMCAPCFG;
if (IsErrSet(mdev) || IsFifoOverflow(mdev)) {
pr_err("Failed to Write Bitstream\n");
MCapRegWrite(mdev, MCAP_CONTROL, restore);
MCapFullReset(mdev);
return -EMCAPWRITE;
}
/* Enable PCIe BAR reads/writes in the PCIe hardblock */
restore |= MCAP_CTRL_DESIGN_SWITCH_MASK;
MCapRegWrite(mdev, MCAP_CONTROL, restore);
return 0;
}
void MCapLibFree(struct mcap_dev *mdev)
{
if (mdev) {
pci_cleanup(mdev->pacc);
free(mdev);
}
}
struct mcap_dev *MCapLibInit(int device_id)
{
struct pci_dev *dev;
struct mcap_dev *mdev;
/* Allocate MCAP device */
mdev = malloc(sizeof(struct mcap_dev));
if (!mdev)
return NULL;
/* Get the pci_access structure */
mdev->pacc = pci_alloc();
mdev->is_multiplebit = 0;
/* Initialize the PCI library */
pci_init(mdev->pacc);
/* Get the list of devices */
pci_scan_bus(mdev->pacc);
for (dev = mdev->pacc->devices; dev; dev = dev->next) {
/* Fill in header info we need */
pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES |
PCI_FILL_CLASS);
if (dev->vendor_id == MCAP_VENDOR_ID &&
dev->device_id == device_id) {
pr_info("Xilinx MCAP device found\n");
mdev->pdev = dev;
} else {
continue;
}
}
if (!mdev->pdev) {
pr_err("Xilinx MCAP device not found .. Exiting ...\n");
goto free_resources;
}
/* Get the MCAP Register base */
if (MCapDoBusWalk(mdev)) {
pr_err("Unable to get the Register Base\n");
goto free_resources;
}
return mdev;
free_resources:
MCapLibFree(mdev);
return NULL;
}
int MCapReset(struct mcap_dev *mdev)
{
u32 set, restore;
int err;
err = MCapClearRequestByConfigure(mdev, &restore);
if (err)
return err;
/* Set 'Mode', 'In Use by PCIe' and 'Reset' bits */
set = MCapRegRead(mdev, MCAP_CONTROL);
set |= MCAP_CTRL_MODE_MASK | MCAP_CTRL_IN_USE_MASK |
MCAP_CTRL_RESET_MASK;
MCapRegWrite(mdev, MCAP_CONTROL, set);
if (IsErrSet(mdev) || !(IsResetSet(mdev))) {
pr_err("Failed to Reset\n");
MCapRegWrite(mdev, MCAP_CONTROL, restore);
return -EMCAPRESET;
}
MCapRegWrite(mdev, MCAP_CONTROL, restore);
pr_info("Reset Done!!\n");
return 0;
}
int MCapModuleReset(struct mcap_dev *mdev)
{
u32 set, restore;
int err;
err = MCapClearRequestByConfigure(mdev, &restore);
if (err)
return err;
/* Set 'Mode', 'In Use by PCIe' and 'Module Reset' bits */
set = MCapRegRead(mdev, MCAP_CONTROL);
set |= MCAP_CTRL_MODE_MASK | MCAP_CTRL_IN_USE_MASK |
MCAP_CTRL_MOD_RESET_MASK;
MCapRegWrite(mdev, MCAP_CONTROL, set);
if (IsErrSet(mdev) || !(IsModuleResetSet(mdev))) {
pr_err("Failed to Reset Module\n");
MCapRegWrite(mdev, MCAP_CONTROL, restore);
return -EMCAPMODRESET;
}
MCapRegWrite(mdev, MCAP_CONTROL, restore);
pr_info("Module Reset Done!!\n");
return 0;
}
int MCapFullReset(struct mcap_dev *mdev)
{
u32 set, restore;
int err;
err = MCapClearRequestByConfigure(mdev, &restore);
if (err)
return err;
/* Set 'Mode', 'In Use by PCIe' and 'Module Reset' bits */
set = MCapRegRead(mdev, MCAP_CONTROL);
set |= MCAP_CTRL_MODE_MASK | MCAP_CTRL_IN_USE_MASK |
MCAP_CTRL_RESET_MASK | MCAP_CTRL_MOD_RESET_MASK;
MCapRegWrite(mdev, MCAP_CONTROL, set);
if (IsErrSet(mdev) || !(IsModuleResetSet(mdev)) ||
!(IsResetSet(mdev))) {
pr_err("Failed to Full Reset\n");
MCapRegWrite(mdev, MCAP_CONTROL, restore);
return -EMCAPFULLRESET;
}
MCapRegWrite(mdev, MCAP_CONTROL, restore);
pr_info("Full Reset Done!!\n");
return 0;
}
static int MCapReadDataRegisters(struct mcap_dev *mdev, u32 *data)
{
u32 set, restore, read_cnt;
int err;
if (!data) {
pr_err("Invalid Arguments\n");
return -EMCAPREAD;
}
err = MCapClearRequestByConfigure(mdev, &restore);
if (err)
return err;
/* Set 'Mode', 'In Use by PCIe' and 'Data Reg Protect' bits */
set = MCapRegRead(mdev, MCAP_CONTROL);
set |= MCAP_CTRL_MODE_MASK | MCAP_CTRL_IN_USE_MASK |
MCAP_CTRL_REG_READ_MASK;
/* Clear 'Reset', 'Module Reset' and 'Register Read' bits */
set &= ~(MCAP_CTRL_RESET_MASK | MCAP_CTRL_MOD_RESET_MASK);
MCapRegWrite(mdev, MCAP_CONTROL, set);
read_cnt = GetRegReadCount(mdev);
if (!(read_cnt) || !(IsRegReadComplete(mdev))) {
MCapRegWrite(mdev, MCAP_CONTROL, restore);
return EMCAPREAD;
}
if (IsErrSet(mdev) || IsFifoOverflow(mdev)) {
pr_err("Read Register Set Configuration Failed\n");
MCapRegWrite(mdev, MCAP_CONTROL, restore);
return -EMCAPREAD;
}
switch (read_cnt) {
case 7: case 6: case 5: case 4:
data[3] = MCapRegRead(mdev, MCAP_READ_DATA_3);
/* Fall-through */
case 3:
data[2] = MCapRegRead(mdev, MCAP_READ_DATA_2);
/* Fall-through */
case 2:
data[1] = MCapRegRead(mdev, MCAP_READ_DATA_1);
/* Fall-through */
case 1:
data[0] = MCapRegRead(mdev, MCAP_READ_DATA_0);
break;
}
MCapRegWrite(mdev, MCAP_CONTROL, restore);
pr_dbg("Read Data Registers Complete!\n");
return 0;
}
void MCapDumpReadRegs(struct mcap_dev *mdev)
{
u32 data[4];
u32 status;
status = MCapReadDataRegisters(mdev, data);
if (status == EMCAPREAD)
return;
if (status) {
pr_err("Failed Reading Registers.. This may be");
pr_err(" due to inappropriate FPGA configuration.");
pr_err(" Make sure you downloaded the correct bitstream\n");
return;
}
pr_info("Register Read Data 0:\t0x%08x\n", data[0]);
pr_info("Register Read Data 1:\t0x%08x\n", data[1]);
pr_info("Register Read Data 2:\t0x%08x\n", data[2]);
pr_info("Register Read Data 3:\t0x%08x\n", data[3]);
}
void MCapDumpRegs(struct mcap_dev *mdev)
{
pr_info("Extended Capability:\t0x%08x\n",
MCapRegRead(mdev, MCAP_EXT_CAP_HEADER));
pr_info("Vendor Specific Header:\t0x%08x\n",
MCapRegRead(mdev, MCAP_VEND_SPEC_HEADER));
pr_info("FPGA JTAG ID:\t\t0x%08x\n",
MCapRegRead(mdev, MCAP_FPGA_JTAG_ID));
pr_info("FPGA Bitstream Version:0x%08x\n",
MCapRegRead(mdev, MCAP_FPGA_BIT_VERSION));
pr_info("Status:\t\t\t0x%08x\n",
MCapRegRead(mdev, MCAP_STATUS));
pr_info("Control:\t\t0x%08x\n",
MCapRegRead(mdev, MCAP_CONTROL));
pr_info("Data:\t\t\t0x%08x\n",
MCapRegRead(mdev, MCAP_DATA));
MCapDumpReadRegs(mdev);
}
int MCapConfigureFPGA(struct mcap_dev *mdev, char *file_path, u32 bitfile_type)
{
FILE *fptr;
u32 *data;
u32 binsz, wrdatasz;
int err = 0;
u8 bswap = 0;
/* Get the size */
fptr = fopen(file_path, "rb");
if (fptr == NULL)
return -EMCAPCFG;
fseek(fptr, 0L, SEEK_END);
binsz = ftell(fptr);
fseek(fptr, 0L, SEEK_SET);
/* Allocate the buffer */
data = malloc(binsz);
if (data == NULL)
return -EMCAPCFG;
/* Process files and Read the data */
if (MCapFindTypeofFile(file_path, MCAP_RBT_FILE)) {
/* Read the RBT file */
wrdatasz = MCapProcessRBT(fptr, data);
} else if (MCapFindTypeofFile(file_path, MCAP_BIT_FILE)) {
/* Read the BIT file */
wrdatasz = MCapProcessBIT(fptr, data, binsz);
bswap = 1;
} else if (MCapFindTypeofFile(file_path, MCAP_BIN_FILE)) {
/* Read the BIN file */
wrdatasz = MCapProcessBIN(fptr, data, binsz);
bswap = 1;
} else {
pr_err("Unknown File Format.. This may be");
pr_err(" due to .bit/.bin/.rbt files does not exist at the.");
pr_err(" specified location, Please cross check the");
pr_err(" path is correct or not\n");
goto free_resources;
}
/* Program FPGA */
if (bitfile_type == EMCAP_PARTIALCONFIG_FILE) {
err = MCapWritePartialBitStream(mdev, data, wrdatasz, bswap);
if (err)
return -EMCAPCFG;
pr_info("FPGA Partial Configuration Done!!\n");
} else if (bitfile_type == EMCAP_CONFIG_FILE) {
err = MCapWriteBitStream(mdev, data, wrdatasz, bswap);
if (err)
return -EMCAPCFG;
pr_info("FPGA Configuration Done!!\n");
}
free_resources:
if (data)
free(data);
fclose(fptr);
return err;
}
int MCapAccessConfigSpace(struct mcap_dev *mdev, int argc, char **argv)
{
unsigned long wrval, rdval;
int pos, access_type;
pos = (int) strtol(argv[4], NULL, 16);
access_type = tolower(argv[5][0]);
if (argc == 6) {
switch (access_type) {
case 'b':
rdval = pci_read_byte(mdev->pdev, pos);
break;
case 'h':
rdval = pci_read_word(mdev->pdev, pos);
break;
case 'w':
rdval = pci_read_long(mdev->pdev, pos);
break;
default:
return -EMCAPCFGACC;
}
pr_info("Read 0x%08lx @ 0x%x\n", rdval, pos);
}
if (argc > 6) {
wrval = strtoul(argv[6], 0, 0);
switch (access_type) {
case 'b':
pci_write_byte(mdev->pdev, pos, wrval);
break;
case 'h':
pci_write_word(mdev->pdev, pos, wrval);
break;
case 'w':
pci_write_long(mdev->pdev, pos, wrval);
break;
default:
return -EMCAPCFGACC;
}
pr_info("Written 0x%08lx @ 0x%x\n", wrval, pos);
}
return 0;
}
int MCapShowDevice(struct mcap_dev *mdev, int verbose)
{
char command[80];
u16 vendor_id, device_id;
vendor_id = mdev->pdev->vendor_id;
device_id = mdev->pdev->device_id;
if (verbose == 1)
sprintf(command, "lspci -vd %x:%x", vendor_id, device_id);
if (verbose >= 2)
sprintf(command, "lspci -vvd %x:%x", vendor_id, device_id);
if (!verbose)
sprintf(command, "lspci -d %x:%x", vendor_id, device_id);
return system(command);
}