aboutsummaryrefslogtreecommitdiff
path: root/drivers/staging/csr/putest.c
blob: 5613cf0e16b0b10b842d09a9134633317335bd75 (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
/*
 * ***************************************************************************
 *  FILE:     putest.c
 *
 *  PURPOSE:    putest related functions.
 *
 *  Copyright (C) 2008-2009 by Cambridge Silicon Radio Ltd.
 *
 * Refer to LICENSE.txt included with this source code for details on
 * the license terms.
 *
 * ***************************************************************************
 */

#include <linux/vmalloc.h>
#include <linux/firmware.h>

#include "unifi_priv.h"
#include "csr_wifi_hip_chiphelper.h"

#define UNIFI_PROC_BOTH 3


int unifi_putest_cmd52_read(unifi_priv_t *priv, unsigned char *arg)
{
    struct unifi_putest_cmd52 cmd52_params;
    u8 *arg_pos;
    unsigned int cmd_param_size;
    int r;
    CsrResult csrResult;
    unsigned char ret_buffer[32];
    u8 *ret_buffer_pos;
    u8 retries;

    arg_pos = (u8*)(((unifi_putest_command_t*)arg) + 1);
    if (get_user(cmd_param_size, (int*)arg_pos)) {
        unifi_error(priv,
                    "unifi_putest_cmd52_read: Failed to get the argument\n");
        return -EFAULT;
    }

    if (cmd_param_size != sizeof(struct unifi_putest_cmd52)) {
        unifi_error(priv,
                    "unifi_putest_cmd52_read: cmd52 struct mismatch\n");
        return -EINVAL;
    }

    arg_pos += sizeof(unsigned int);
    if (copy_from_user(&cmd52_params,
                       (void*)arg_pos,
                       sizeof(struct unifi_putest_cmd52))) {
        unifi_error(priv,
                    "unifi_putest_cmd52_read: Failed to get the cmd52 params\n");
        return -EFAULT;
    }

    unifi_trace(priv, UDBG2, "cmd52r: func=%d addr=0x%x ",
                cmd52_params.funcnum, cmd52_params.addr);

    retries = 3;
    CsrSdioClaim(priv->sdio);
    do {
        if (cmd52_params.funcnum == 0) {
            csrResult = CsrSdioF0Read8(priv->sdio, cmd52_params.addr, &cmd52_params.data);
        } else {
            csrResult = CsrSdioRead8(priv->sdio, cmd52_params.addr, &cmd52_params.data);
        }
    } while (--retries && ((csrResult == CSR_SDIO_RESULT_CRC_ERROR) || (csrResult == CSR_SDIO_RESULT_TIMEOUT)));
    CsrSdioRelease(priv->sdio);

    if (csrResult != CSR_RESULT_SUCCESS) {
        unifi_error(priv,
                    "\nunifi_putest_cmd52_read: Read8() failed (csrResult=0x%x)\n", csrResult);
        return -EFAULT;
    }
    unifi_trace(priv, UDBG2, "data=%d\n", cmd52_params.data);

    /* Copy the info to the out buffer */
    *(unifi_putest_command_t*)ret_buffer = UNIFI_PUTEST_CMD52_READ;
    ret_buffer_pos = (u8*)(((unifi_putest_command_t*)ret_buffer) + 1);
    *(unsigned int*)ret_buffer_pos = sizeof(struct unifi_putest_cmd52);
    ret_buffer_pos += sizeof(unsigned int);
    memcpy(ret_buffer_pos, &cmd52_params, sizeof(struct unifi_putest_cmd52));
    ret_buffer_pos += sizeof(struct unifi_putest_cmd52);

    r = copy_to_user((void*)arg,
                     ret_buffer,
                     ret_buffer_pos - ret_buffer);
    if (r) {
        unifi_error(priv,
                    "unifi_putest_cmd52_read: Failed to return the data\n");
        return -EFAULT;
    }

    return 0;
}


int unifi_putest_cmd52_write(unifi_priv_t *priv, unsigned char *arg)
{
    struct unifi_putest_cmd52 cmd52_params;
    u8 *arg_pos;
    unsigned int cmd_param_size;
    CsrResult csrResult;
    u8 retries;

    arg_pos = (u8*)(((unifi_putest_command_t*)arg) + 1);
    if (get_user(cmd_param_size, (int*)arg_pos)) {
        unifi_error(priv,
                    "unifi_putest_cmd52_write: Failed to get the argument\n");
        return -EFAULT;
    }

    if (cmd_param_size != sizeof(struct unifi_putest_cmd52)) {
        unifi_error(priv,
                    "unifi_putest_cmd52_write: cmd52 struct mismatch\n");
        return -EINVAL;
    }

    arg_pos += sizeof(unsigned int);
    if (copy_from_user(&cmd52_params,
                       (void*)(arg_pos),
                       sizeof(struct unifi_putest_cmd52))) {
        unifi_error(priv,
                    "unifi_putest_cmd52_write: Failed to get the cmd52 params\n");
        return -EFAULT;
    }

    unifi_trace(priv, UDBG2, "cmd52w: func=%d addr=0x%x data=%d\n",
                cmd52_params.funcnum, cmd52_params.addr, cmd52_params.data);

    retries = 3;
    CsrSdioClaim(priv->sdio);
    do {
        if (cmd52_params.funcnum == 0) {
            csrResult = CsrSdioF0Write8(priv->sdio, cmd52_params.addr, cmd52_params.data);
        } else {
            csrResult = CsrSdioWrite8(priv->sdio, cmd52_params.addr, cmd52_params.data);
        }
    } while (--retries && ((csrResult == CSR_SDIO_RESULT_CRC_ERROR) || (csrResult == CSR_SDIO_RESULT_TIMEOUT)));
    CsrSdioRelease(priv->sdio);

    if (csrResult != CSR_RESULT_SUCCESS) {
        unifi_error(priv,
                    "unifi_putest_cmd52_write: Write8() failed (csrResult=0x%x)\n", csrResult);
        return -EFAULT;
    }

    return 0;
}

int unifi_putest_gp_read16(unifi_priv_t *priv, unsigned char *arg)
{
    struct unifi_putest_gp_rw16 gp_r16_params;
    u8 *arg_pos;
    unsigned int cmd_param_size;
    int r;
    CsrResult csrResult;
    unsigned char ret_buffer[32];
    u8 *ret_buffer_pos;

    arg_pos = (u8*)(((unifi_putest_command_t*)arg) + 1);
    if (get_user(cmd_param_size, (int*)arg_pos)) {
        unifi_error(priv,
                    "unifi_putest_gp_read16: Failed to get the argument\n");
        return -EFAULT;
    }

    if (cmd_param_size != sizeof(struct unifi_putest_gp_rw16)) {
        unifi_error(priv,
                    "unifi_putest_gp_read16: struct mismatch\n");
        return -EINVAL;
    }

    arg_pos += sizeof(unsigned int);
    if (copy_from_user(&gp_r16_params,
                       (void*)arg_pos,
                       sizeof(struct unifi_putest_gp_rw16))) {
        unifi_error(priv,
                    "unifi_putest_gp_read16: Failed to get the params\n");
        return -EFAULT;
    }
    CsrSdioClaim(priv->sdio);
    csrResult = unifi_card_read16(priv->card, gp_r16_params.addr, &gp_r16_params.data);
    CsrSdioRelease(priv->sdio);
    if (csrResult != CSR_RESULT_SUCCESS) {
        unifi_error(priv,
                    "unifi_putest_gp_read16: unifi_card_read16() GP=0x%x failed (csrResult=0x%x)\n", gp_r16_params.addr, csrResult);
        return -EFAULT;
    }

    unifi_trace(priv, UDBG2, "gp_r16: GP=0x%08x, data=0x%04x\n", gp_r16_params.addr, gp_r16_params.data);

    /* Copy the info to the out buffer */
    *(unifi_putest_command_t*)ret_buffer = UNIFI_PUTEST_GP_READ16;
    ret_buffer_pos = (u8*)(((unifi_putest_command_t*)ret_buffer) + 1);
    *(unsigned int*)ret_buffer_pos = sizeof(struct unifi_putest_gp_rw16);
    ret_buffer_pos += sizeof(unsigned int);
    memcpy(ret_buffer_pos, &gp_r16_params, sizeof(struct unifi_putest_gp_rw16));
    ret_buffer_pos += sizeof(struct unifi_putest_gp_rw16);

    r = copy_to_user((void*)arg,
                     ret_buffer,
                     ret_buffer_pos - ret_buffer);
    if (r) {
        unifi_error(priv,
                    "unifi_putest_gp_read16: Failed to return the data\n");
        return -EFAULT;
    }

    return 0;
}

int unifi_putest_gp_write16(unifi_priv_t *priv, unsigned char *arg)
{
    struct unifi_putest_gp_rw16 gp_w16_params;
    u8 *arg_pos;
    unsigned int cmd_param_size;
    CsrResult csrResult;

    arg_pos = (u8*)(((unifi_putest_command_t*)arg) + 1);
    if (get_user(cmd_param_size, (int*)arg_pos)) {
        unifi_error(priv,
                    "unifi_putest_gp_write16: Failed to get the argument\n");
        return -EFAULT;
    }

    if (cmd_param_size != sizeof(struct unifi_putest_gp_rw16)) {
        unifi_error(priv,
                    "unifi_putest_gp_write16: struct mismatch\n");
        return -EINVAL;
    }

    arg_pos += sizeof(unsigned int);
    if (copy_from_user(&gp_w16_params,
                       (void*)(arg_pos),
                       sizeof(struct unifi_putest_gp_rw16))) {
        unifi_error(priv,
                    "unifi_putest_gp_write16: Failed to get the params\n");
        return -EFAULT;
    }

    unifi_trace(priv, UDBG2, "gp_w16: GP=0x%08x, data=0x%04x\n", gp_w16_params.addr, gp_w16_params.data);
    CsrSdioClaim(priv->sdio);
    csrResult = unifi_card_write16(priv->card, gp_w16_params.addr, gp_w16_params.data);
    CsrSdioRelease(priv->sdio);
    if (csrResult != CSR_RESULT_SUCCESS) {
        unifi_error(priv,
                    "unifi_putest_gp_write16: unifi_card_write16() GP=%x failed (csrResult=0x%x)\n", gp_w16_params.addr, csrResult);
        return -EFAULT;
    }

    return 0;
}

int unifi_putest_set_sdio_clock(unifi_priv_t *priv, unsigned char *arg)
{
    int sdio_clock_speed;
    CsrResult csrResult;

    if (get_user(sdio_clock_speed, (int*)(((unifi_putest_command_t*)arg) + 1))) {
        unifi_error(priv,
                    "unifi_putest_set_sdio_clock: Failed to get the argument\n");
        return -EFAULT;
    }

    unifi_trace(priv, UDBG2, "set sdio clock: %d KHz\n", sdio_clock_speed);

    CsrSdioClaim(priv->sdio);
    csrResult = CsrSdioMaxBusClockFrequencySet(priv->sdio, sdio_clock_speed * 1000);
    CsrSdioRelease(priv->sdio);
    if (csrResult != CSR_RESULT_SUCCESS) {
        unifi_error(priv,
                    "unifi_putest_set_sdio_clock: Set clock failed (csrResult=0x%x)\n", csrResult);
        return -EFAULT;
    }

    return 0;
}


int unifi_putest_start(unifi_priv_t *priv, unsigned char *arg)
{
    int r;
    CsrResult csrResult;
    int already_in_test = priv->ptest_mode;

    /* Ensure that sme_sys_suspend() doesn't power down the chip because:
     *  1) Power is needed anyway for ptest.
     *  2) The app code uses the START ioctl as a reset, so it gets called
     *     multiple times. If the app stops the XAPs, but the power_down/up
     *     sequence doesn't actually power down the chip, there can be problems
     *     resetting, because part of the power_up sequence disables function 1
     */
    priv->ptest_mode = 1;

    /* Suspend the SME and UniFi */
    if (priv->sme_cli) {
        r = sme_sys_suspend(priv);
        if (r) {
            unifi_error(priv,
                        "unifi_putest_start: failed to suspend UniFi\n");
            return r;
        }
    }

    /* Application may have stopped the XAPs, but they are needed for reset */
    if (already_in_test) {
        CsrSdioClaim(priv->sdio);
        csrResult = unifi_start_processors(priv->card);
        CsrSdioRelease(priv->sdio);
        if (csrResult != CSR_RESULT_SUCCESS) {
            unifi_error(priv, "Failed to start XAPs. Hard reset required.\n");
        }
    } else {
        /* Ensure chip is powered for the case where there's no unifi_helper */
        CsrSdioClaim(priv->sdio);
        csrResult = CsrSdioPowerOn(priv->sdio);
        CsrSdioRelease(priv->sdio);
        if (csrResult != CSR_RESULT_SUCCESS) {
            unifi_error(priv, "CsrSdioPowerOn csrResult = %d\n", csrResult);
        }
    }
    CsrSdioClaim(priv->sdio);
    csrResult = unifi_init(priv->card);
    CsrSdioRelease(priv->sdio);
    if (csrResult != CSR_RESULT_SUCCESS) {
        unifi_error(priv,
                    "unifi_putest_start: failed to init UniFi\n");
        return CsrHipResultToStatus(csrResult);
    }

    return 0;
}


int unifi_putest_stop(unifi_priv_t *priv, unsigned char *arg)
{
    int r = 0;
    CsrResult csrResult;

    /* Application may have stopped the XAPs, but they are needed for reset */
    CsrSdioClaim(priv->sdio);
    csrResult = unifi_start_processors(priv->card);
    CsrSdioRelease(priv->sdio);
    if (csrResult != CSR_RESULT_SUCCESS) {
        unifi_error(priv, "Failed to start XAPs. Hard reset required.\n");
    }

    /* PUTEST_STOP is also used to resume the XAPs after SME coredump.
     * Don't power off the chip, leave that to the normal wifi-off which is
     * about to carry on. No need to resume the SME either, as it wasn't suspended.
     */
    if (priv->coredump_mode) {
        priv->coredump_mode = 0;
        return 0;
    }

    /* At this point function 1 is enabled and the XAPs are running, so it is
     * safe to let the card power down. Power is restored later, asynchronously,
     * during the wifi_on requested by the SME.
     */
    CsrSdioClaim(priv->sdio);
    CsrSdioPowerOff(priv->sdio);
    CsrSdioRelease(priv->sdio);

    /* Resume the SME and UniFi */
    if (priv->sme_cli) {
        r = sme_sys_resume(priv);
        if (r) {
            unifi_error(priv,
                        "unifi_putest_stop: failed to resume SME\n");
        }
    }
    priv->ptest_mode = 0;

    return r;
}


int unifi_putest_dl_fw(unifi_priv_t *priv, unsigned char *arg)
{
#define UF_PUTEST_MAX_FW_FILE_NAME      16
#define UNIFI_MAX_FW_PATH_LEN           32
    unsigned int fw_name_length;
    unsigned char fw_name[UF_PUTEST_MAX_FW_FILE_NAME+1];
    unsigned char *name_buffer;
    int postfix;
    char fw_path[UNIFI_MAX_FW_PATH_LEN];
    const struct firmware *fw_entry;
    struct dlpriv temp_fw_sta;
    int r;
    CsrResult csrResult;

    /* Get the f/w file name length */
    if (get_user(fw_name_length, (unsigned int*)(((unifi_putest_command_t*)arg) + 1))) {
        unifi_error(priv,
                    "unifi_putest_dl_fw: Failed to get the length argument\n");
        return -EFAULT;
    }

    unifi_trace(priv, UDBG2, "unifi_putest_dl_fw: file name size = %d\n", fw_name_length);

    /* Sanity check for the f/w file name length */
    if (fw_name_length > UF_PUTEST_MAX_FW_FILE_NAME) {
        unifi_error(priv,
                    "unifi_putest_dl_fw: F/W file name is too long\n");
        return -EINVAL;
    }

    /* Get the f/w file name */
    name_buffer = ((unsigned char*)arg) + sizeof(unifi_putest_command_t) + sizeof(unsigned int);
    if (copy_from_user(fw_name, (void*)name_buffer, fw_name_length)) {
        unifi_error(priv, "unifi_putest_dl_fw: Failed to get the file name\n");
        return -EFAULT;
    }
    fw_name[fw_name_length] = '\0';
    unifi_trace(priv, UDBG2, "unifi_putest_dl_fw: file = %s\n", fw_name);

    /* Keep the existing f/w to a temp, we need to restore it later */
    temp_fw_sta = priv->fw_sta;

    /* Get the putest f/w */
    postfix = priv->instance;
    scnprintf(fw_path, UNIFI_MAX_FW_PATH_LEN, "unifi-sdio-%d/%s",
              postfix, fw_name);
    r = request_firmware(&fw_entry, fw_path, priv->unifi_device);
    if (r == 0) {
        priv->fw_sta.fw_desc = (void *)fw_entry;
        priv->fw_sta.dl_data = fw_entry->data;
        priv->fw_sta.dl_len = fw_entry->size;
    } else {
        unifi_error(priv, "Firmware file not available\n");
        return -EINVAL;
    }

    /* Application may have stopped the XAPs, but they are needed for reset */
    CsrSdioClaim(priv->sdio);
    csrResult = unifi_start_processors(priv->card);
    CsrSdioRelease(priv->sdio);
    if (csrResult != CSR_RESULT_SUCCESS) {
        unifi_error(priv, "Failed to start XAPs. Hard reset required.\n");
    }

    /* Download the f/w. On UF6xxx this will cause the f/w file to convert
     * into patch format and download via the ROM boot loader
     */
    CsrSdioClaim(priv->sdio);
    csrResult = unifi_download(priv->card, 0x0c00);
    CsrSdioRelease(priv->sdio);
    if (csrResult != CSR_RESULT_SUCCESS) {
        unifi_error(priv,
                    "unifi_putest_dl_fw: failed to download the f/w\n");
        goto free_fw;
    }

    /* Free the putest f/w... */
free_fw:
    uf_release_firmware(priv, &priv->fw_sta);
    /* ... and restore the original f/w */
    priv->fw_sta = temp_fw_sta;

    return CsrHipResultToStatus(csrResult);
}


int unifi_putest_dl_fw_buff(unifi_priv_t *priv, unsigned char *arg)
{
    unsigned int fw_length;
    unsigned char *fw_buf = NULL;
    unsigned char *fw_user_ptr;
    struct dlpriv temp_fw_sta;
    CsrResult csrResult;

    /* Get the f/w buffer length */
    if (get_user(fw_length, (unsigned int*)(((unifi_putest_command_t*)arg) + 1))) {
        unifi_error(priv,
                    "unifi_putest_dl_fw_buff: Failed to get the length arg\n");
        return -EFAULT;
    }

    unifi_trace(priv, UDBG2, "unifi_putest_dl_fw_buff: size = %d\n", fw_length);

    /* Sanity check for the buffer length */
    if (fw_length == 0 || fw_length > 0xfffffff) {
        unifi_error(priv,
                    "unifi_putest_dl_fw_buff: buffer length bad %u\n", fw_length);
        return -EINVAL;
    }

    /* Buffer for kernel copy of the f/w image */
    fw_buf = kmalloc(fw_length, GFP_KERNEL);
    if (!fw_buf) {
        unifi_error(priv, "unifi_putest_dl_fw_buff: malloc fail\n");
        return -ENOMEM;
    }

    /* Get the f/w image */
    fw_user_ptr = ((unsigned char*)arg) + sizeof(unifi_putest_command_t) + sizeof(unsigned int);
    if (copy_from_user(fw_buf, (void*)fw_user_ptr, fw_length)) {
        unifi_error(priv, "unifi_putest_dl_fw_buff: Failed to get the buffer\n");
        kfree(fw_buf);
        return -EFAULT;
    }

    /* Save the existing f/w to a temp, we need to restore it later */
    temp_fw_sta = priv->fw_sta;

    /* Setting fw_desc NULL indicates to the core that no f/w file was loaded
     * via the kernel request_firmware() mechanism. This indicates to the core
     * that it shouldn't call release_firmware() after the download is done.
     */
    priv->fw_sta.fw_desc = NULL;            /* No OS f/w resource */
    priv->fw_sta.dl_data = fw_buf;
    priv->fw_sta.dl_len = fw_length;

    /* Application may have stopped the XAPs, but they are needed for reset */
    CsrSdioClaim(priv->sdio);
    csrResult = unifi_start_processors(priv->card);
    CsrSdioRelease(priv->sdio);
    if (csrResult != CSR_RESULT_SUCCESS) {
        unifi_error(priv, "Failed to start XAPs. Hard reset required.\n");
    }

    /* Download the f/w. On UF6xxx this will cause the f/w file to convert
     * into patch format and download via the ROM boot loader
     */
    CsrSdioClaim(priv->sdio);
    csrResult = unifi_download(priv->card, 0x0c00);
    CsrSdioRelease(priv->sdio);
    if (csrResult != CSR_RESULT_SUCCESS) {
        unifi_error(priv,
                    "unifi_putest_dl_fw_buff: failed to download the f/w\n");
        goto free_fw;
    }

free_fw:
    /* Finished with the putest f/w, so restore the station f/w */
    priv->fw_sta = temp_fw_sta;
    kfree(fw_buf);

    return CsrHipResultToStatus(csrResult);
}


int unifi_putest_coredump_prepare(unifi_priv_t *priv, unsigned char *arg)
{
    u16 data_u16;
    s32 i;
    CsrResult r;

    unifi_info(priv, "Preparing for SDIO coredump\n");
#if defined (CSR_WIFI_HIP_DEBUG_OFFLINE)
    unifi_debug_buf_dump();
#endif

    /* Sanity check that userspace hasn't called a PUTEST_START, because that
     * would have reset UniFi, potentially power cycling it and losing context
     */
    if (priv->ptest_mode) {
        unifi_error(priv, "PUTEST_START shouldn't be used before a coredump\n");
    }

    /* Flag that the userspace has requested coredump. Even if this preparation
     * fails, the SME will call PUTEST_STOP to tidy up.
     */
    priv->coredump_mode = 1;

    for (i = 0; i < 3; i++) {
        CsrSdioClaim(priv->sdio);
        r = CsrSdioRead16(priv->sdio, CHIP_HELPER_UNIFI_GBL_CHIP_VERSION*2, &data_u16);
        CsrSdioRelease(priv->sdio);
        if (r != CSR_RESULT_SUCCESS) {
            unifi_info(priv, "Failed to read chip version! Try %d\n", i);

            /* First try, re-enable function which may have been disabled by f/w panic */
            if (i == 0) {
                unifi_info(priv, "Try function enable\n");
                CsrSdioClaim(priv->sdio);
                r = CsrSdioFunctionEnable(priv->sdio);
                CsrSdioRelease(priv->sdio);
                if (r != CSR_RESULT_SUCCESS) {
                    unifi_error(priv, "CsrSdioFunctionEnable failed %d\n", r);
                }
                continue;
            }

            /* Subsequent tries, reset */

            /* Set clock speed low */
            CsrSdioClaim(priv->sdio);
            r = CsrSdioMaxBusClockFrequencySet(priv->sdio, UNIFI_SDIO_CLOCK_SAFE_HZ);
            CsrSdioRelease(priv->sdio);
            if (r != CSR_RESULT_SUCCESS) {
                unifi_error(priv, "CsrSdioMaxBusClockFrequencySet() failed %d\n", r);
            }

            /* Card software reset */
            CsrSdioClaim(priv->sdio);
            r = unifi_card_hard_reset(priv->card);
            CsrSdioRelease(priv->sdio);
            if (r != CSR_RESULT_SUCCESS) {
                unifi_error(priv, "unifi_card_hard_reset() failed %d\n", r);
            }
        } else {
            unifi_info(priv, "Read chip version of 0x%04x\n", data_u16);
            break;
        }
    }

    if (r != CSR_RESULT_SUCCESS) {
        unifi_error(priv, "Failed to prepare chip\n");
        return -EIO;
    }

    /* Stop the XAPs for coredump. The PUTEST_STOP must be called, e.g. at
     * Raw SDIO deinit, to resume them.
     */
    CsrSdioClaim(priv->sdio);
    r = unifi_card_stop_processor(priv->card, UNIFI_PROC_BOTH);
    CsrSdioRelease(priv->sdio);
    if (r != CSR_RESULT_SUCCESS) {
        unifi_error(priv, "Failed to stop processors\n");
    }

    return 0;
}

int unifi_putest_cmd52_block_read(unifi_priv_t *priv, unsigned char *arg)
{
    struct unifi_putest_block_cmd52_r block_cmd52;
    u8 *arg_pos;
    unsigned int cmd_param_size;
    CsrResult r;
    u8 *block_local_buffer;

    arg_pos = (u8*)(((unifi_putest_command_t*)arg) + 1);
    if (get_user(cmd_param_size, (int*)arg_pos)) {
        unifi_error(priv,
                    "cmd52r_block: Failed to get the argument\n");
        return -EFAULT;
    }

    if (cmd_param_size != sizeof(struct unifi_putest_block_cmd52_r)) {
        unifi_error(priv,
                    "cmd52r_block: cmd52 struct mismatch\n");
        return -EINVAL;
    }

    arg_pos += sizeof(unsigned int);
    if (copy_from_user(&block_cmd52,
                       (void*)arg_pos,
                       sizeof(struct unifi_putest_block_cmd52_r))) {
        unifi_error(priv,
                    "cmd52r_block: Failed to get the cmd52 params\n");
        return -EFAULT;
    }

    unifi_trace(priv, UDBG2, "cmd52r_block: func=%d addr=0x%x len=0x%x ",
                block_cmd52.funcnum, block_cmd52.addr, block_cmd52.length);

    block_local_buffer = vmalloc(block_cmd52.length);
    if (block_local_buffer == NULL) {
        unifi_error(priv, "cmd52r_block: Failed to allocate buffer\n");
        return -ENOMEM;
    }

    CsrSdioClaim(priv->sdio);
    r = unifi_card_readn(priv->card, block_cmd52.addr, block_local_buffer, block_cmd52.length);
    CsrSdioRelease(priv->sdio);
    if (r != CSR_RESULT_SUCCESS) {
        unifi_error(priv, "cmd52r_block: unifi_readn failed\n");
        return -EIO;
    }

    if (copy_to_user((void*)block_cmd52.data,
                     block_local_buffer,
                     block_cmd52.length)) {
        unifi_error(priv,
                    "cmd52r_block: Failed to return the data\n");
        return -EFAULT;
    }

    return 0;
}