Root/drivers/staging/csr/csr_wifi_hip_card_sdio.c

1/*****************************************************************************
2
3            (c) Cambridge Silicon Radio Limited 2012
4            All rights reserved and confidential information of CSR
5
6            Refer to LICENSE.txt included with this source for details
7            on the license terms.
8
9*****************************************************************************/
10
11/*
12 * ---------------------------------------------------------------------------
13 * FILE: csr_wifi_hip_card_sdio.c
14 *
15 * PURPOSE: Implementation of the Card API for SDIO.
16 *
17 * NOTES:
18 * CardInit() is called from the SDIO probe callback when a card is
19 * inserted. This performs the basic SDIO initialisation, enabling i/o
20 * etc.
21 *
22 * ---------------------------------------------------------------------------
23 */
24#include <linux/slab.h>
25#include "csr_wifi_hip_unifi.h"
26#include "csr_wifi_hip_conversions.h"
27#include "csr_wifi_hip_unifiversion.h"
28#include "csr_wifi_hip_card.h"
29#include "csr_wifi_hip_card_sdio.h"
30#include "csr_wifi_hip_chiphelper.h"
31
32
33/* Time to wait between attempts to read MAILBOX0 */
34#define MAILBOX1_TIMEOUT 10 /* in millisecs */
35#define MAILBOX1_ATTEMPTS 200 /* 2 seconds */
36
37#define MAILBOX2_TIMEOUT 5 /* in millisecs */
38#define MAILBOX2_ATTEMPTS 10 /* 50ms */
39
40#define RESET_SETTLE_DELAY 25 /* in millisecs */
41
42static CsrResult card_init_slots(card_t *card);
43static CsrResult card_hw_init(card_t *card);
44static CsrResult firmware_present_in_flash(card_t *card);
45static void bootstrap_chip_hw(card_t *card);
46static CsrResult unifi_reset_hardware(card_t *card);
47static CsrResult unifi_hip_init(card_t *card);
48static CsrResult card_access_panic(card_t *card);
49static CsrResult unifi_read_chip_version(card_t *card);
50
51/*
52 * ---------------------------------------------------------------------------
53 * unifi_alloc_card
54 *
55 * Allocate and initialise the card context structure.
56 *
57 * Arguments:
58 * sdio Pointer to SDIO context pointer to pass to low
59 * level i/o functions.
60 * ospriv Pointer to O/S private struct to pass when calling
61 * callbacks to the higher level system.
62 *
63 * Returns:
64 * Pointer to card struct, which represents the driver context or
65 * NULL if the allocation failed.
66 * ---------------------------------------------------------------------------
67 */
68card_t* unifi_alloc_card(CsrSdioFunction *sdio, void *ospriv)
69{
70    card_t *card;
71    u32 i;
72
73    func_enter();
74
75
76    card = kzalloc(sizeof(card_t), GFP_KERNEL);
77    if (card == NULL)
78    {
79        return NULL;
80    }
81
82    card->sdio_if = sdio;
83    card->ospriv = ospriv;
84
85    card->unifi_interrupt_seq = 1;
86
87    /* Make these invalid. */
88    card->proc_select = (u32)(-1);
89    card->dmem_page = (u32)(-1);
90    card->pmem_page = (u32)(-1);
91
92    card->bh_reason_host = 0;
93    card->bh_reason_unifi = 0;
94
95    for (i = 0; i < sizeof(card->tx_q_paused_flag) / sizeof(card->tx_q_paused_flag[0]); i++)
96    {
97        card->tx_q_paused_flag[i] = 0;
98    }
99    card->memory_resources_allocated = 0;
100
101    card->low_power_mode = UNIFI_LOW_POWER_DISABLED;
102    card->periodic_wake_mode = UNIFI_PERIODIC_WAKE_HOST_DISABLED;
103
104    card->host_state = UNIFI_HOST_STATE_AWAKE;
105    card->intmode = CSR_WIFI_INTMODE_DEFAULT;
106
107    /*
108     * Memory resources for buffers are allocated when the chip is initialised
109     * because we need configuration information from the firmware.
110     */
111
112    /*
113     * Initialise wait queues and lists
114     */
115    card->fh_command_queue.q_body = card->fh_command_q_body;
116    card->fh_command_queue.q_length = UNIFI_SOFT_COMMAND_Q_LENGTH;
117
118    for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
119    {
120        card->fh_traffic_queue[i].q_body = card->fh_traffic_q_body[i];
121        card->fh_traffic_queue[i].q_length = UNIFI_SOFT_TRAFFIC_Q_LENGTH;
122    }
123
124
125    /* Initialise mini-coredump pointers in case no coredump buffers
126     * are requested by the OS layer.
127     */
128    card->request_coredump_on_reset = 0;
129    card->dump_next_write = NULL;
130    card->dump_cur_read = NULL;
131    card->dump_buf = NULL;
132
133#ifdef UNIFI_DEBUG
134    /* Determine offset of LSB in pointer for later alignment sanity check.
135     * Synergy integer types have specific widths, which cause compiler
136     * warnings when casting pointer types, e.g. on 64-bit systems.
137     */
138    {
139        u32 val = 0x01234567;
140
141        if (*((u8 *)&val) == 0x01)
142        {
143            card->lsb = sizeof(void *) - 1; /* BE */
144        }
145        else
146        {
147            card->lsb = 0; /* LE */
148        }
149    }
150#endif
151    func_exit();
152    return card;
153} /* unifi_alloc_card() */
154
155
156/*
157 * ---------------------------------------------------------------------------
158 * unifi_init_card
159 *
160 * Reset the hardware and perform HIP initialization
161 *
162 * Arguments:
163 * card Pointer to card struct
164 *
165 * Returns:
166 * CsrResult code
167 * CSR_RESULT_SUCCESS if successful
168 * ---------------------------------------------------------------------------
169 */
170CsrResult unifi_init_card(card_t *card, s32 led_mask)
171{
172    CsrResult r;
173
174    func_enter();
175
176    if (card == NULL)
177    {
178        func_exit_r(CSR_WIFI_HIP_RESULT_INVALID_VALUE);
179        return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
180    }
181
182    r = unifi_init(card);
183    if (r != CSR_RESULT_SUCCESS)
184    {
185        func_exit_r(r);
186        return r;
187    }
188
189    r = unifi_hip_init(card);
190    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
191    {
192        func_exit_r(r);
193        return r;
194    }
195    if (r != CSR_RESULT_SUCCESS)
196    {
197        unifi_error(card->ospriv, "Failed to start host protocol.\n");
198        func_exit_r(r);
199        return r;
200    }
201
202    func_exit();
203    return CSR_RESULT_SUCCESS;
204}
205
206
207/*
208 * ---------------------------------------------------------------------------
209 * unifi_init
210 *
211 * Init the hardware.
212 *
213 * Arguments:
214 * card Pointer to card struct
215 *
216 * Returns:
217 * CsrResult code
218 * CSR_RESULT_SUCCESS if successful
219 * ---------------------------------------------------------------------------
220 */
221CsrResult unifi_init(card_t *card)
222{
223    CsrResult r;
224    CsrResult csrResult;
225
226    func_enter();
227
228    if (card == NULL)
229    {
230        func_exit_r(CSR_WIFI_HIP_RESULT_INVALID_VALUE);
231        return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
232    }
233
234    /*
235     * Disable the SDIO interrupts while initialising UniFi.
236     * Re-enable them when f/w is running.
237     */
238    csrResult = CsrSdioInterruptDisable(card->sdio_if);
239    if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
240    {
241        return CSR_WIFI_HIP_RESULT_NO_DEVICE;
242    }
243
244    /*
245     * UniFi's PLL may start with a slow clock (~ 1 MHz) so initially
246     * set the SDIO bus clock to a similar value or SDIO accesses may
247     * fail.
248     */
249    csrResult = CsrSdioMaxBusClockFrequencySet(card->sdio_if, UNIFI_SDIO_CLOCK_SAFE_HZ);
250    if (csrResult != CSR_RESULT_SUCCESS)
251    {
252        r = ConvertCsrSdioToCsrHipResult(card, csrResult);
253        func_exit_r(r);
254        return r;
255    }
256    card->sdio_clock_speed = UNIFI_SDIO_CLOCK_SAFE_HZ;
257
258    /*
259     * Reset UniFi. Note, this only resets the WLAN function part of the chip,
260     * the SDIO interface is not reset.
261     */
262    unifi_trace(card->ospriv, UDBG1, "Resetting UniFi\n");
263    r = unifi_reset_hardware(card);
264    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
265    {
266        return r;
267    }
268    if (r != CSR_RESULT_SUCCESS)
269    {
270        unifi_error(card->ospriv, "Failed to reset UniFi\n");
271        func_exit_r(r);
272        return r;
273    }
274
275    /* Reset the power save mode, to be active until the MLME-reset is complete */
276    r = unifi_configure_low_power_mode(card,
277                                       UNIFI_LOW_POWER_DISABLED, UNIFI_PERIODIC_WAKE_HOST_DISABLED);
278    if (r != CSR_RESULT_SUCCESS)
279    {
280        unifi_error(card->ospriv, "Failed to set power save mode\n");
281        func_exit_r(r);
282        return r;
283    }
284
285    /*
286     * Set initial value of page registers.
287     * The page registers will be maintained by unifi_read...() and
288     * unifi_write...().
289     */
290    card->proc_select = (u32)(-1);
291    card->dmem_page = (u32)(-1);
292    card->pmem_page = (u32)(-1);
293    r = unifi_write_direct16(card, ChipHelper_HOST_WINDOW3_PAGE(card->helper) * 2, 0);
294    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
295    {
296        return r;
297    }
298    if (r != CSR_RESULT_SUCCESS)
299    {
300        unifi_error(card->ospriv, "Failed to write SHARED_DMEM_PAGE\n");
301        func_exit_r(r);
302        return r;
303    }
304    r = unifi_write_direct16(card, ChipHelper_HOST_WINDOW2_PAGE(card->helper) * 2, 0);
305    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
306    {
307        return r;
308    }
309    if (r != CSR_RESULT_SUCCESS)
310    {
311        unifi_error(card->ospriv, "Failed to write PROG_MEM2_PAGE\n");
312        func_exit_r(r);
313        return r;
314    }
315
316    /*
317     * If the driver has reset UniFi due to previous SDIO failure, this may
318     * have been due to a chip watchdog reset. In this case, the driver may
319     * have requested a mini-coredump which needs to be captured now the
320     * SDIO interface is alive.
321     */
322    (void)unifi_coredump_handle_request(card);
323
324    /*
325     * Probe to see if the UniFi has ROM/flash to boot from. CSR6xxx should do.
326     */
327    r = firmware_present_in_flash(card);
328    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
329    {
330        return r;
331    }
332    if (r == CSR_WIFI_HIP_RESULT_NOT_FOUND)
333    {
334        unifi_error(card->ospriv, "No firmware found\n");
335    }
336    else if (r != CSR_RESULT_SUCCESS)
337    {
338        unifi_error(card->ospriv, "Probe for Flash failed\n");
339    }
340
341    func_exit_r(r);
342    return r;
343} /* unifi_init() */
344
345
346/*
347 * ---------------------------------------------------------------------------
348 * unifi_download
349 *
350 * Load the firmware.
351 *
352 * Arguments:
353 * card Pointer to card struct
354 * led_mask Loader LED mask
355 *
356 * Returns:
357 * CSR_RESULT_SUCCESS on success
358 * CsrResult error code on failure.
359 * ---------------------------------------------------------------------------
360 */
361CsrResult unifi_download(card_t *card, s32 led_mask)
362{
363    CsrResult r;
364    void *dlpriv;
365
366    func_enter();
367
368    if (card == NULL)
369    {
370        func_exit_r(CSR_WIFI_HIP_RESULT_INVALID_VALUE);
371        return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
372    }
373
374    /* Set the loader led mask */
375    card->loader_led_mask = led_mask;
376
377    /* Get the firmware file information */
378    unifi_trace(card->ospriv, UDBG1, "downloading firmware...\n");
379
380    dlpriv = unifi_dl_fw_read_start(card, UNIFI_FW_STA);
381    if (dlpriv == NULL)
382    {
383        func_exit_r(CSR_WIFI_HIP_RESULT_NOT_FOUND);
384        return CSR_WIFI_HIP_RESULT_NOT_FOUND;
385    }
386
387    /* Download the firmware. */
388    r = unifi_dl_firmware(card, dlpriv);
389    if (r != CSR_RESULT_SUCCESS)
390    {
391        unifi_error(card->ospriv, "Failed to download firmware\n");
392        func_exit_r(r);
393        return r;
394    }
395
396    /* Free the firmware file information. */
397    unifi_fw_read_stop(card->ospriv, dlpriv);
398
399    func_exit();
400
401    return CSR_RESULT_SUCCESS;
402} /* unifi_download() */
403
404
405/*
406 * ---------------------------------------------------------------------------
407 * unifi_hip_init
408 *
409 * This function performs the f/w initialisation sequence as described
410 * in the Unifi Host Interface Protocol Specification.
411 * It allocates memory for host-side slot data and signal queues.
412 *
413 * Arguments:
414 * card Pointer to card struct
415 *
416 * Returns:
417 * CSR_RESULT_SUCCESS on success or else a CSR error code
418 *
419 * Notes:
420 * The firmware must have been downloaded.
421 * ---------------------------------------------------------------------------
422 */
423static CsrResult unifi_hip_init(card_t *card)
424{
425    CsrResult r;
426    CsrResult csrResult;
427
428    func_enter();
429
430    r = card_hw_init(card);
431    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
432    {
433        return r;
434    }
435    if (r != CSR_RESULT_SUCCESS)
436    {
437        unifi_error(card->ospriv, "Failed to establish communication with UniFi\n");
438        func_exit_r(r);
439        return r;
440    }
441#ifdef CSR_PRE_ALLOC_NET_DATA
442    /* if there is any preallocated netdata left from the prev session free it now */
443    prealloc_netdata_free(card);
444#endif
445    /*
446     * Allocate memory for host-side slot data and signal queues.
447     * We need the config info read from the firmware to know how much
448     * memory to allocate.
449     */
450    r = card_init_slots(card);
451    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
452    {
453        return r;
454    }
455    if (r != CSR_RESULT_SUCCESS)
456    {
457        unifi_error(card->ospriv, "Init slots failed: %d\n", r);
458        func_exit_r(r);
459        return r;
460    }
461
462    unifi_trace(card->ospriv, UDBG2, "Sending first UniFi interrupt\n");
463
464    r = unifi_set_host_state(card, UNIFI_HOST_STATE_AWAKE);
465    if (r != CSR_RESULT_SUCCESS)
466    {
467        func_exit_r(r);
468        return r;
469    }
470
471    /* Enable the SDIO interrupts now that the f/w is running. */
472    csrResult = CsrSdioInterruptEnable(card->sdio_if);
473    if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
474    {
475        return CSR_WIFI_HIP_RESULT_NO_DEVICE;
476    }
477
478    /* Signal the UniFi to start handling messages */
479    r = CardGenInt(card);
480    if (r != CSR_RESULT_SUCCESS)
481    {
482        func_exit_r(r);
483        return r;
484    }
485
486    func_exit();
487
488    return CSR_RESULT_SUCCESS;
489} /* unifi_hip_init() */
490
491
492/*
493 * ---------------------------------------------------------------------------
494 * _build_sdio_config_data
495 *
496 * Unpack the SDIO configuration information from a buffer read from
497 * UniFi into a host structure.
498 * The data is byte-swapped for a big-endian host if necessary by the
499 * UNPACK... macros.
500 *
501 * Arguments:
502 * card Pointer to card struct
503 * cfg_data Destination structure to unpack into.
504 * cfg_data_buf Source buffer to read from. This should be the raw
505 * data read from UniFi.
506 *
507 * Returns:
508 * None.
509 * ---------------------------------------------------------------------------
510 */
511static void _build_sdio_config_data(sdio_config_data_t *cfg_data,
512                                    const u8 *cfg_data_buf)
513{
514    s16 offset = 0;
515
516    cfg_data->version = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
517    offset += SIZEOF_UINT16;
518
519    cfg_data->sdio_ctrl_offset = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
520    offset += SIZEOF_UINT16;
521
522    cfg_data->fromhost_sigbuf_handle = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
523    offset += SIZEOF_UINT16;
524
525    cfg_data->tohost_sigbuf_handle = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
526    offset += SIZEOF_UINT16;
527
528    cfg_data->num_fromhost_sig_frags = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
529    offset += SIZEOF_UINT16;
530
531    cfg_data->num_tohost_sig_frags = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
532    offset += SIZEOF_UINT16;
533
534    cfg_data->num_fromhost_data_slots = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
535    offset += SIZEOF_UINT16;
536
537    cfg_data->num_tohost_data_slots = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
538    offset += SIZEOF_UINT16;
539
540    cfg_data->data_slot_size = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
541    offset += SIZEOF_UINT16;
542
543    cfg_data->initialised = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
544    offset += SIZEOF_UINT16;
545
546    cfg_data->overlay_size = CSR_GET_UINT32_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
547    offset += SIZEOF_UINT32;
548
549    cfg_data->data_slot_round = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
550    offset += SIZEOF_UINT16;
551
552    cfg_data->sig_frag_size = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
553    offset += SIZEOF_UINT16;
554
555    cfg_data->tohost_signal_padding = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
556} /* _build_sdio_config_data() */
557
558
559/*
560 * - Function ----------------------------------------------------------------
561 * card_hw_init()
562 *
563 * Perform the initialisation procedure described in the UniFi Host
564 * Interface Protocol document (section 3.3.8) and read the run-time
565 * configuration information from the UniFi. This is stuff like number
566 * of bulk data slots etc.
567 *
568 * The card enumeration and SD initialisation has already been done by
569 * the SDIO library, see card_sdio_init().
570 *
571 * The initialisation is done when firmware is ready, i.e. this may need
572 * to be called after a f/w download operation.
573 *
574 * The initialisation procedure goes like this:
575 * - Wait for UniFi to start-up by polling SHARED_MAILBOX1
576 * - Find the symbol table and look up SLT_SDIO_SLOT_CONFIG
577 * - Read the config structure
578 * - Check the "SDIO initialised" flag, if not zero do a h/w reset and
579 * start again
580 * - Decide the number of bulk data slots to allocate, allocate them and
581 * set "SDIO initialised" flag (and generate an interrupt) to say so.
582 *
583 * Arguments:
584 * card Pointer to card struct
585 *
586 * Returns:
587 * CSR_RESULT_SUCEESS on success,
588 * a CSR error code on failure
589 *
590 * Notes:
591 * All data in the f/w is stored in a little endian format, without any
592 * padding bytes. Every read from this memory has to be transformed in
593 * host (cpu specific) format, before it is stored in driver's parameters
594 * or/and structures. Athough unifi_card_read16() and unifi_read32() do perform
595 * the convertion internally, unifi_readn() does not.
596 * ---------------------------------------------------------------------------
597 */
598static CsrResult card_hw_init(card_t *card)
599{
600    u32 slut_address;
601    u16 initialised;
602    u16 finger_print;
603    symbol_t slut;
604    sdio_config_data_t *cfg_data;
605    u8 cfg_data_buf[SDIO_CONFIG_DATA_SIZE];
606    CsrResult r;
607    void *dlpriv;
608    s16 major, minor;
609    s16 search_4slut_again;
610    CsrResult csrResult;
611
612    func_enter();
613
614    /*
615     * The device revision from the TPLMID_MANF and TPLMID_CARD fields
616     * of the CIS are available as
617     * card->sdio_if->pDevice->ManfID
618     * card->sdio_if->pDevice->AppID
619     */
620
621    /*
622     * Run in a loop so we can patch.
623     */
624    do
625    {
626        /* Reset these each time around the loop. */
627        search_4slut_again = 0;
628        cfg_data = NULL;
629
630        r = card_wait_for_firmware_to_start(card, &slut_address);
631        if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
632        {
633            return r;
634        }
635        if (r != CSR_RESULT_SUCCESS)
636        {
637            unifi_error(card->ospriv, "Firmware hasn't started\n");
638            func_exit_r(r);
639            return r;
640        }
641        unifi_trace(card->ospriv, UDBG4, "SLUT addr 0x%lX\n", slut_address);
642
643        /*
644         * Firmware has started, but doesn't know full clock configuration yet
645         * as some of the information may be in the MIB. Therefore we set an
646         * initial SDIO clock speed, faster than UNIFI_SDIO_CLOCK_SAFE_HZ, for
647         * the patch download and subsequent firmware initialisation, and
648         * full speed UNIFI_SDIO_CLOCK_MAX_HZ will be set once the f/w tells us
649         * that it is ready.
650         */
651        csrResult = CsrSdioMaxBusClockFrequencySet(card->sdio_if, UNIFI_SDIO_CLOCK_INIT_HZ);
652        if (csrResult != CSR_RESULT_SUCCESS)
653        {
654            r = ConvertCsrSdioToCsrHipResult(card, csrResult);
655            func_exit_r(r);
656            return r;
657        }
658        card->sdio_clock_speed = UNIFI_SDIO_CLOCK_INIT_HZ;
659
660        /*
661         * Check the SLUT fingerprint.
662         * The slut_address is a generic pointer so we must use unifi_card_read16().
663         */
664        unifi_trace(card->ospriv, UDBG4, "Looking for SLUT finger print\n");
665        finger_print = 0;
666        r = unifi_card_read16(card, slut_address, &finger_print);
667        if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
668        {
669            return r;
670        }
671        if (r != CSR_RESULT_SUCCESS)
672        {
673            unifi_error(card->ospriv, "Failed to read SLUT finger print\n");
674            func_exit_r(r);
675            return r;
676        }
677
678        if (finger_print != SLUT_FINGERPRINT)
679        {
680            unifi_error(card->ospriv, "Failed to find Symbol lookup table fingerprint\n");
681            func_exit_r(CSR_RESULT_FAILURE);
682            return CSR_RESULT_FAILURE;
683        }
684
685        /* Symbol table starts imedately after the fingerprint */
686        slut_address += 2;
687
688        /* Search the table until either the end marker is found, or the
689         * loading of patch firmware invalidates the current table.
690         */
691        while (!search_4slut_again)
692        {
693            u16 s;
694            u32 l;
695
696            r = unifi_card_read16(card, slut_address, &s);
697            if (r != CSR_RESULT_SUCCESS)
698            {
699                func_exit_r(r);
700                return r;
701            }
702            slut_address += 2;
703
704            if (s == CSR_SLT_END)
705            {
706                unifi_trace(card->ospriv, UDBG3, " found CSR_SLT_END\n");
707                break;
708            }
709
710            r = unifi_read32(card, slut_address, &l);
711            if (r != CSR_RESULT_SUCCESS)
712            {
713                func_exit_r(r);
714                return r;
715            }
716            slut_address += 4;
717
718            slut.id = s;
719            slut.obj = l;
720
721            unifi_trace(card->ospriv, UDBG3, " found SLUT id %02d.%08lx\n", slut.id, slut.obj);
722            switch (slut.id)
723            {
724                case CSR_SLT_SDIO_SLOT_CONFIG:
725                    cfg_data = &card->config_data;
726                    /*
727                     * unifi_card_readn reads n bytes from the card, where data is stored
728                     * in a little endian format, without any padding bytes. So, we
729                     * can not just pass the cfg_data pointer or use the
730                     * sizeof(sdio_config_data_t) since the structure in the host can
731                     * be big endian formatted or have padding bytes for alignment.
732                     * We use a char buffer to read the data from the card.
733                     */
734                    r = unifi_card_readn(card, slut.obj, cfg_data_buf, SDIO_CONFIG_DATA_SIZE);
735                    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
736                    {
737                        return r;
738                    }
739                    if (r != CSR_RESULT_SUCCESS)
740                    {
741                        unifi_error(card->ospriv, "Failed to read config data\n");
742                        func_exit_r(r);
743                        return r;
744                    }
745                    /* .. and then we copy the data to the host structure */
746                    _build_sdio_config_data(cfg_data, cfg_data_buf);
747
748                    /* Make sure the from host data slots are what we expect
749                        we reserve 2 for commands and there should be at least
750                        1 left for each access category */
751                    if ((cfg_data->num_fromhost_data_slots < UNIFI_RESERVED_COMMAND_SLOTS)
752                        || (cfg_data->num_fromhost_data_slots - UNIFI_RESERVED_COMMAND_SLOTS) / UNIFI_NO_OF_TX_QS == 0)
753                    {
754                        unifi_error(card->ospriv, "From host data slots %d\n", cfg_data->num_fromhost_data_slots);
755                        unifi_error(card->ospriv, "need to be (queues * x + 2) (UNIFI_RESERVED_COMMAND_SLOTS for commands)\n");
756                        func_exit_r(CSR_RESULT_FAILURE);
757                        return CSR_RESULT_FAILURE;
758                    }
759
760                    /* Configure SDIO to-block-size padding */
761                    if (card->sdio_io_block_pad)
762                    {
763                    /*
764                     * Firmware limits the maximum padding size via data_slot_round.
765                     * Therefore when padding to whole block sizes, the block size
766                     * must be configured correctly by adjusting CSR_WIFI_HIP_SDIO_BLOCK_SIZE.
767                     */
768                        if (cfg_data->data_slot_round < card->sdio_io_block_size)
769                        {
770                            unifi_error(card->ospriv,
771                                        "Configuration error: Block size of %d exceeds f/w data_slot_round of %d\n",
772                                        card->sdio_io_block_size, cfg_data->data_slot_round);
773                            return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
774                        }
775
776                        /*
777                         * To force the To-Host signals to be rounded up to the SDIO block
778                         * size, we need to write the To-Host Signal Padding Fragments
779                         * field of the SDIO configuration in UniFi.
780                         */
781                        if ((card->sdio_io_block_size % cfg_data->sig_frag_size) != 0)
782                        {
783                            unifi_error(card->ospriv, "Configuration error: Can not pad to-host signals.\n");
784                            func_exit_r(CSR_WIFI_HIP_RESULT_INVALID_VALUE);
785                            return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
786                        }
787                        cfg_data->tohost_signal_padding = (u16) (card->sdio_io_block_size / cfg_data->sig_frag_size);
788                        unifi_info(card->ospriv, "SDIO block size %d requires %d padding chunks\n",
789                                   card->sdio_io_block_size, cfg_data->tohost_signal_padding);
790                        r = unifi_card_write16(card, slut.obj + SDIO_TO_HOST_SIG_PADDING_OFFSET, cfg_data->tohost_signal_padding);
791                        if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
792                        {
793                            return r;
794                        }
795                        if (r != CSR_RESULT_SUCCESS)
796                        {
797                            unifi_error(card->ospriv, "Failed to write To-Host Signal Padding Fragments\n");
798                            func_exit_r(r);
799                            return r;
800                        }
801                    }
802
803                    /* Reconstruct the Generic Pointer address of the
804                     * SDIO Control Data Struct.
805                     */
806                    card->sdio_ctrl_addr = cfg_data->sdio_ctrl_offset | (UNIFI_SH_DMEM << 24);
807                    card->init_flag_addr = slut.obj + SDIO_INIT_FLAG_OFFSET;
808                    break;
809
810                case CSR_SLT_BUILD_ID_NUMBER:
811                {
812                    u32 n;
813                    r = unifi_read32(card, slut.obj, &n);
814                    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
815                    {
816                        return r;
817                    }
818                    if (r != CSR_RESULT_SUCCESS)
819                    {
820                        unifi_error(card->ospriv, "Failed to read build id\n");
821                        func_exit_r(r);
822                        return r;
823                    }
824                    card->build_id = n;
825                }
826                break;
827
828                case CSR_SLT_BUILD_ID_STRING:
829                    r = unifi_readnz(card, slut.obj, card->build_id_string,
830                                     sizeof(card->build_id_string));
831                    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
832                    {
833                        return r;
834                    }
835                    if (r != CSR_RESULT_SUCCESS)
836                    {
837                        unifi_error(card->ospriv, "Failed to read build string\n");
838                        func_exit_r(r);
839                        return r;
840                    }
841                    break;
842
843                case CSR_SLT_PERSISTENT_STORE_DB:
844                    break;
845
846                case CSR_SLT_BOOT_LOADER_CONTROL:
847
848                    /* This command copies most of the station firmware
849                     * image from ROM into program RAM. It also clears
850                     * out the zerod data and sets up the initialised
851                     * data. */
852                    r = unifi_do_loader_op(card, slut.obj + 6, UNIFI_BOOT_LOADER_LOAD_STA);
853                    if (r != CSR_RESULT_SUCCESS)
854                    {
855                        unifi_error(card->ospriv, "Failed to write loader load image command\n");
856                        func_exit_r(r);
857                        return r;
858                    }
859
860                    dlpriv = unifi_dl_fw_read_start(card, UNIFI_FW_STA);
861
862                    /* dlpriv might be NULL, we still need to do the do_loader_op step. */
863                    if (dlpriv != NULL)
864                    {
865                    /* Download the firmware. */
866                        r = unifi_dl_patch(card, dlpriv, slut.obj);
867
868                    /* Free the firmware file information. */
869                        unifi_fw_read_stop(card->ospriv, dlpriv);
870
871                        if (r != CSR_RESULT_SUCCESS)
872                        {
873                            unifi_error(card->ospriv, "Failed to patch firmware\n");
874                            func_exit_r(r);
875                            return r;
876                        }
877                    }
878
879                    /* This command starts the firmware image that we want (the
880                    * station by default) with any patches required applied. */
881                    r = unifi_do_loader_op(card, slut.obj + 6, UNIFI_BOOT_LOADER_RESTART);
882                    if (r != CSR_RESULT_SUCCESS)
883                    {
884                        unifi_error(card->ospriv, "Failed to write loader restart command\n");
885                        func_exit_r(r);
886                        return r;
887                    }
888
889                    /* The now running patch f/w defines a new SLUT data structure -
890                     * the current one is no longer valid. We must drop out of the
891                     * processing loop and enumerate the new SLUT (which may appear
892                     * at a different offset).
893                     */
894                    search_4slut_again = 1;
895                    break;
896
897                case CSR_SLT_PANIC_DATA_PHY:
898                    card->panic_data_phy_addr = slut.obj;
899                    break;
900
901                case CSR_SLT_PANIC_DATA_MAC:
902                    card->panic_data_mac_addr = slut.obj;
903                    break;
904
905                default:
906                    /* do nothing */
907                    break;
908            }
909        } /* while */
910    } while (search_4slut_again);
911
912    /* Did we find the Config Data ? */
913    if (cfg_data == NULL)
914    {
915        unifi_error(card->ospriv, "Failed to find SDIO_SLOT_CONFIG Symbol\n");
916        func_exit_r(CSR_RESULT_FAILURE);
917        return CSR_RESULT_FAILURE;
918    }
919
920    /*
921     * Has ths card already been initialised?
922     * If so, return an error so we do a h/w reset and start again.
923     */
924    r = unifi_card_read16(card, card->init_flag_addr, &initialised);
925    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
926    {
927        return r;
928    }
929    if (r != CSR_RESULT_SUCCESS)
930    {
931        unifi_error(card->ospriv, "Failed to read init flag at %08lx\n",
932                    card->init_flag_addr);
933        func_exit_r(r);
934        return r;
935    }
936    if (initialised != 0)
937    {
938        func_exit_r(CSR_RESULT_FAILURE);
939        return CSR_RESULT_FAILURE;
940    }
941
942
943    /*
944     * Now check the UniFi firmware version
945     */
946    major = (cfg_data->version >> 8) & 0xFF;
947    minor = cfg_data->version & 0xFF;
948    unifi_info(card->ospriv, "UniFi f/w protocol version %d.%d (driver %d.%d)\n",
949               major, minor,
950               UNIFI_HIP_MAJOR_VERSION, UNIFI_HIP_MINOR_VERSION);
951
952    unifi_info(card->ospriv, "Firmware build %u: %s\n",
953               card->build_id, card->build_id_string);
954
955    if (major != UNIFI_HIP_MAJOR_VERSION)
956    {
957        unifi_error(card->ospriv, "UniFi f/w protocol major version (%d) is different from driver (v%d.%d)\n",
958                    major, UNIFI_HIP_MAJOR_VERSION, UNIFI_HIP_MINOR_VERSION);
959#ifndef CSR_WIFI_DISABLE_HIP_VERSION_CHECK
960        func_exit_r(CSR_RESULT_FAILURE);
961        return CSR_RESULT_FAILURE;
962#endif
963    }
964    if (minor < UNIFI_HIP_MINOR_VERSION)
965    {
966        unifi_error(card->ospriv, "UniFi f/w protocol version (v%d.%d) is older than minimum required by driver (v%d.%d).\n",
967                    major, minor,
968                    UNIFI_HIP_MAJOR_VERSION, UNIFI_HIP_MINOR_VERSION);
969#ifndef CSR_WIFI_DISABLE_HIP_VERSION_CHECK
970        func_exit_r(CSR_RESULT_FAILURE);
971        return CSR_RESULT_FAILURE;
972#endif
973    }
974
975    /* Read panic codes from a previous firmware panic. If the firmware has
976     * not panicked since power was applied (e.g. power-off hard reset)
977     * the stored panic codes will not be updated.
978     */
979    unifi_read_panic(card);
980
981    func_exit();
982    return CSR_RESULT_SUCCESS;
983} /* card_hw_init() */
984
985
986/*
987 * ---------------------------------------------------------------------------
988 * card_wait_for_unifi_to_reset
989 *
990 * Waits for a reset to complete by polling the WLAN function enable
991 * bit (which is cleared on reset).
992 *
993 * Arguments:
994 * card Pointer to card struct
995 *
996 * Returns:
997 * CSR_RESULT_SUCCESS on success, CSR error code on failure.
998 * ---------------------------------------------------------------------------
999 */
1000static CsrResult card_wait_for_unifi_to_reset(card_t *card)
1001{
1002    s16 i;
1003    CsrResult r;
1004    u8 io_enable;
1005    CsrResult csrResult;
1006
1007    func_enter();
1008
1009    r = CSR_RESULT_SUCCESS;
1010    for (i = 0; i < MAILBOX2_ATTEMPTS; i++)
1011    {
1012        unifi_trace(card->ospriv, UDBG1, "waiting for reset to complete, attempt %d\n", i);
1013        if (card->chip_id > SDIO_CARD_ID_UNIFI_2)
1014        {
1015            /* It's quite likely that this read will timeout for the
1016             * first few tries - especially if we have reset via
1017             * DBG_RESET.
1018             */
1019#if defined (CSR_WIFI_HIP_DEBUG_OFFLINE) && defined (CSR_WIFI_HIP_SDIO_TRACE)
1020            unifi_debug_log_to_buf("m0@%02X=", SDIO_IO_READY);
1021#endif
1022            csrResult = CsrSdioF0Read8(card->sdio_if, SDIO_IO_READY, &io_enable);
1023#if defined (CSR_WIFI_HIP_DEBUG_OFFLINE) && defined (CSR_WIFI_HIP_SDIO_TRACE)
1024            if (csrResult != CSR_RESULT_SUCCESS)
1025            {
1026                unifi_debug_log_to_buf("error=%X\n", csrResult);
1027            }
1028            else
1029            {
1030                unifi_debug_log_to_buf("%X\n", io_enable);
1031            }
1032#endif
1033            if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
1034            {
1035                return CSR_WIFI_HIP_RESULT_NO_DEVICE;
1036            }
1037            r = CSR_RESULT_SUCCESS;
1038            if (csrResult != CSR_RESULT_SUCCESS)
1039            {
1040                r = ConvertCsrSdioToCsrHipResult(card, csrResult);
1041            }
1042        }
1043        else
1044        {
1045            r = sdio_read_f0(card, SDIO_IO_ENABLE, &io_enable);
1046        }
1047        if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1048        {
1049            return r;
1050        }
1051        if (r == CSR_RESULT_SUCCESS)
1052        {
1053            u16 mbox2;
1054            s16 enabled = io_enable & (1 << card->function);
1055
1056            if (!enabled)
1057            {
1058                unifi_trace(card->ospriv, UDBG1,
1059                            "Reset complete (function %d is disabled) in ~ %u msecs\n",
1060                            card->function, i * MAILBOX2_TIMEOUT);
1061
1062                /* Enable WLAN function and verify MAILBOX2 is zero'd */
1063                csrResult = CsrSdioFunctionEnable(card->sdio_if);
1064                if (csrResult != CSR_RESULT_SUCCESS)
1065                {
1066                    r = ConvertCsrSdioToCsrHipResult(card, csrResult);
1067                    unifi_error(card->ospriv, "CsrSdioFunctionEnable failed %d\n", r);
1068                    break;
1069                }
1070            }
1071
1072            r = unifi_read_direct16(card, ChipHelper_SDIO_HIP_HANDSHAKE(card->helper) * 2, &mbox2);
1073            if (r != CSR_RESULT_SUCCESS)
1074            {
1075                unifi_error(card->ospriv, "read HIP_HANDSHAKE failed %d\n", r);
1076                break;
1077            }
1078            if (mbox2 != 0)
1079            {
1080                unifi_error(card->ospriv, "MAILBOX2 non-zero after reset (mbox2 = %04x)\n", mbox2);
1081                r = CSR_RESULT_FAILURE;
1082            }
1083            break;
1084        }
1085        else
1086        {
1087            if (card->chip_id > SDIO_CARD_ID_UNIFI_2)
1088            {
1089                /* We ignore read failures for the first few reads,
1090                 * they are probably benign. */
1091                if (i > MAILBOX2_ATTEMPTS / 4)
1092                {
1093                    unifi_trace(card->ospriv, UDBG1, "Failed to read CCCR IO Ready register while polling for reset\n");
1094                }
1095            }
1096            else
1097            {
1098                unifi_trace(card->ospriv, UDBG1, "Failed to read CCCR IO Enable register while polling for reset\n");
1099            }
1100        }
1101        CsrThreadSleep(MAILBOX2_TIMEOUT);
1102    }
1103
1104    if (r == CSR_RESULT_SUCCESS && i == MAILBOX2_ATTEMPTS)
1105    {
1106        unifi_trace(card->ospriv, UDBG1, "Timeout waiting for UniFi to complete reset\n");
1107        r = CSR_RESULT_FAILURE;
1108    }
1109
1110    func_exit();
1111    return r;
1112} /* card_wait_for_unifi_to_reset() */
1113
1114
1115/*
1116 * ---------------------------------------------------------------------------
1117 * card_wait_for_unifi_to_disable
1118 *
1119 * Waits for the function to become disabled by polling the
1120 * IO_READY bit.
1121 *
1122 * Arguments:
1123 * card Pointer to card struct
1124 *
1125 * Returns:
1126 * CSR_RESULT_SUCCESS on success, CSR error code on failure.
1127 *
1128 * Notes: This function can only be used with
1129 * card->chip_id > SDIO_CARD_ID_UNIFI_2
1130 * ---------------------------------------------------------------------------
1131 */
1132static CsrResult card_wait_for_unifi_to_disable(card_t *card)
1133{
1134    s16 i;
1135    CsrResult r;
1136    u8 io_enable;
1137    CsrResult csrResult;
1138
1139    func_enter();
1140
1141    if (card->chip_id <= SDIO_CARD_ID_UNIFI_2)
1142    {
1143        unifi_error(card->ospriv,
1144                    "Function reset method not supported for chip_id=%d\n",
1145                    card->chip_id);
1146        func_exit();
1147        return CSR_RESULT_FAILURE;
1148    }
1149
1150    r = CSR_RESULT_SUCCESS;
1151    for (i = 0; i < MAILBOX2_ATTEMPTS; i++)
1152    {
1153        unifi_trace(card->ospriv, UDBG1, "waiting for disable to complete, attempt %d\n", i);
1154
1155        /*
1156         * It's quite likely that this read will timeout for the
1157         * first few tries - especially if we have reset via
1158         * DBG_RESET.
1159         */
1160#if defined (CSR_WIFI_HIP_DEBUG_OFFLINE) && defined (CSR_WIFI_HIP_SDIO_TRACE)
1161        unifi_debug_log_to_buf("r0@%02X=", SDIO_IO_READY);
1162#endif
1163        csrResult = CsrSdioF0Read8(card->sdio_if, SDIO_IO_READY, &io_enable);
1164#if defined (CSR_WIFI_HIP_DEBUG_OFFLINE) && defined (CSR_WIFI_HIP_SDIO_TRACE)
1165        if (csrResult != CSR_RESULT_SUCCESS)
1166        {
1167            unifi_debug_log_to_buf("error=%X\n", csrResult);
1168        }
1169        else
1170        {
1171            unifi_debug_log_to_buf("%X\n", io_enable);
1172        }
1173#endif
1174        if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
1175        {
1176            return CSR_WIFI_HIP_RESULT_NO_DEVICE;
1177        }
1178        if (csrResult == CSR_RESULT_SUCCESS)
1179        {
1180            s16 enabled = io_enable & (1 << card->function);
1181            r = CSR_RESULT_SUCCESS;
1182            if (!enabled)
1183            {
1184                unifi_trace(card->ospriv, UDBG1,
1185                            "Disable complete (function %d is disabled) in ~ %u msecs\n",
1186                            card->function, i * MAILBOX2_TIMEOUT);
1187
1188                break;
1189            }
1190        }
1191        else
1192        {
1193            /*
1194             * We ignore read failures for the first few reads,
1195             * they are probably benign.
1196             */
1197            r = ConvertCsrSdioToCsrHipResult(card, csrResult);
1198            if (i > (MAILBOX2_ATTEMPTS / 4))
1199            {
1200                unifi_trace(card->ospriv, UDBG1,
1201                            "Failed to read CCCR IO Ready register while polling for disable\n");
1202            }
1203        }
1204        CsrThreadSleep(MAILBOX2_TIMEOUT);
1205    }
1206
1207    if ((r == CSR_RESULT_SUCCESS) && (i == MAILBOX2_ATTEMPTS))
1208    {
1209        unifi_trace(card->ospriv, UDBG1, "Timeout waiting for UniFi to complete disable\n");
1210        r = CSR_RESULT_FAILURE;
1211    }
1212
1213    func_exit();
1214    return r;
1215} /* card_wait_for_unifi_to_reset() */
1216
1217
1218/*
1219 * ---------------------------------------------------------------------------
1220 * card_wait_for_firmware_to_start
1221 *
1222 * Polls the MAILBOX1 register for a non-zero value.
1223 * Then reads MAILBOX0 and forms the two values into a 32-bit address
1224 * which is returned to the caller.
1225 *
1226 * Arguments:
1227 * card Pointer to card struct
1228 * paddr Pointer to receive the UniFi address formed
1229 * by concatenating MAILBOX1 and MAILBOX0.
1230 *
1231 * Returns:
1232 * CSR_RESULT_SUCCESS on success, CSR error code on failure.
1233 * ---------------------------------------------------------------------------
1234 */
1235CsrResult card_wait_for_firmware_to_start(card_t *card, u32 *paddr)
1236{
1237    s32 i;
1238    u16 mbox0, mbox1;
1239    CsrResult r;
1240
1241    func_enter();
1242
1243    /*
1244     * Wait for UniFi to initialise its data structures by polling
1245     * the SHARED_MAILBOX1 register.
1246     * Experience shows this is typically 120ms.
1247     */
1248    CsrThreadSleep(MAILBOX1_TIMEOUT);
1249
1250    mbox1 = 0;
1251    unifi_trace(card->ospriv, UDBG1, "waiting for MAILBOX1 to be non-zero...\n");
1252    for (i = 0; i < MAILBOX1_ATTEMPTS; i++)
1253    {
1254        r = unifi_read_direct16(card, ChipHelper_MAILBOX1(card->helper) * 2, &mbox1);
1255        if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1256        {
1257            return r;
1258        }
1259        if (r != CSR_RESULT_SUCCESS)
1260        {
1261            /* These reads can fail if UniFi isn't up yet, so try again */
1262            unifi_warning(card->ospriv, "Failed to read UniFi Mailbox1 register\n");
1263        }
1264
1265        if ((r == CSR_RESULT_SUCCESS) && (mbox1 != 0))
1266        {
1267            unifi_trace(card->ospriv, UDBG1, "MAILBOX1 ready (0x%04X) in %u millisecs\n",
1268                        mbox1, i * MAILBOX1_TIMEOUT);
1269
1270            /* Read the MAILBOX1 again in case we caught the value as it
1271             * changed. */
1272            r = unifi_read_direct16(card, ChipHelper_MAILBOX1(card->helper) * 2, &mbox1);
1273            if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1274            {
1275                return r;
1276            }
1277            if (r != CSR_RESULT_SUCCESS)
1278            {
1279                unifi_error(card->ospriv, "Failed to read UniFi Mailbox1 register for second time\n");
1280                func_exit_r(r);
1281                return r;
1282            }
1283            unifi_trace(card->ospriv, UDBG1, "MAILBOX1 value=0x%04X\n", mbox1);
1284
1285            break;
1286        }
1287
1288        CsrThreadSleep(MAILBOX1_TIMEOUT);
1289        if ((i % 100) == 99)
1290        {
1291            unifi_trace(card->ospriv, UDBG2, "MAILBOX1 not ready (0x%X), still trying...\n", mbox1);
1292        }
1293    }
1294
1295    if ((r == CSR_RESULT_SUCCESS) && (mbox1 == 0))
1296    {
1297        unifi_trace(card->ospriv, UDBG1, "Timeout waiting for firmware to start, Mailbox1 still 0 after %d ms\n",
1298                    MAILBOX1_ATTEMPTS * MAILBOX1_TIMEOUT);
1299        func_exit_r(CSR_RESULT_FAILURE);
1300        return CSR_RESULT_FAILURE;
1301    }
1302
1303
1304    /*
1305     * Complete the reset handshake by setting MAILBOX2 to 0xFFFF
1306     */
1307    r = unifi_write_direct16(card, ChipHelper_SDIO_HIP_HANDSHAKE(card->helper) * 2, 0xFFFF);
1308    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1309    {
1310        return r;
1311    }
1312    if (r != CSR_RESULT_SUCCESS)
1313    {
1314        unifi_error(card->ospriv, "Failed to write f/w startup handshake to MAILBOX2\n");
1315        func_exit_r(r);
1316        return r;
1317    }
1318
1319
1320    /*
1321     * Read the Symbol Look Up Table (SLUT) offset.
1322     * Top 16 bits are in mbox1, read the lower 16 bits from mbox0.
1323     */
1324    mbox0 = 0;
1325    r = unifi_read_direct16(card, ChipHelper_MAILBOX0(card->helper) * 2, &mbox0);
1326    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1327    {
1328        return r;
1329    }
1330    if (r != CSR_RESULT_SUCCESS)
1331    {
1332        unifi_error(card->ospriv, "Failed to read UniFi Mailbox0 register\n");
1333        func_exit_r(r);
1334        return r;
1335    }
1336
1337    *paddr = (((u32)mbox1 << 16) | mbox0);
1338
1339    func_exit();
1340    return CSR_RESULT_SUCCESS;
1341} /* card_wait_for_firmware_to_start() */
1342
1343
1344/*
1345 * ---------------------------------------------------------------------------
1346 * unifi_capture_panic
1347 *
1348 * Attempt to capture panic codes from the firmware. This may involve
1349 * warm reset of the chip to regain access following a watchdog reset.
1350 *
1351 * Arguments:
1352 * card Pointer to card struct
1353 *
1354 * Returns:
1355 * CSR_RESULT_SUCCESS if panic codes were captured, or none available
1356 * CSR_RESULT_FAILURE if the driver could not access function 1
1357 * ---------------------------------------------------------------------------
1358 */
1359CsrResult unifi_capture_panic(card_t *card)
1360{
1361    func_enter();
1362
1363    /* The firmware must have previously initialised to read the panic addresses
1364     * from the SLUT
1365     */
1366    if (!card->panic_data_phy_addr || !card->panic_data_mac_addr)
1367    {
1368        func_exit();
1369        return CSR_RESULT_SUCCESS;
1370    }
1371
1372    /* Ensure we can access function 1 following a panic/watchdog reset */
1373    if (card_access_panic(card) == CSR_RESULT_SUCCESS)
1374    {
1375        /* Read the panic codes */
1376        unifi_read_panic(card);
1377    }
1378    else
1379    {
1380        unifi_info(card->ospriv, "Unable to read panic codes");
1381    }
1382
1383    func_exit();
1384    return CSR_RESULT_SUCCESS;
1385}
1386
1387
1388/*
1389 * ---------------------------------------------------------------------------
1390 * card_access_panic
1391 * Attempt to read the WLAN SDIO function in order to read panic codes
1392 * and perform various reset steps to regain access if the read fails.
1393 *
1394 * Arguments:
1395 * card Pointer to card struct
1396 *
1397 * Returns:
1398 * CSR_RESULT_SUCCESS if panic codes can be read
1399 * CSR error code if panic codes can not be read
1400 * ---------------------------------------------------------------------------
1401 */
1402static CsrResult card_access_panic(card_t *card)
1403{
1404    u16 data_u16 = 0;
1405    s32 i;
1406    CsrResult r, sr;
1407
1408    func_enter();
1409
1410    /* A chip version of zero means that the version never got succesfully read
1411     * during reset. In this case give up because it will not be possible to
1412     * verify the chip version.
1413     */
1414    if (!card->chip_version)
1415    {
1416        unifi_info(card->ospriv, "Unknown chip version\n");
1417        return CSR_RESULT_FAILURE;
1418    }
1419
1420    /* Ensure chip is awake or access to function 1 will fail */
1421    r = unifi_set_host_state(card, UNIFI_HOST_STATE_AWAKE);
1422    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1423    {
1424        return r;
1425    }
1426    if (r != CSR_RESULT_SUCCESS)
1427    {
1428        unifi_error(card->ospriv, "unifi_set_host_state() failed %d\n", r);
1429        return CSR_RESULT_FAILURE; /* Card is probably unpowered */
1430    }
1431    CsrThreadSleep(20);
1432
1433    for (i = 0; i < 3; i++)
1434    {
1435        sr = CsrSdioRead16(card->sdio_if, CHIP_HELPER_UNIFI_GBL_CHIP_VERSION * 2, &data_u16);
1436        if (sr != CSR_RESULT_SUCCESS || data_u16 != card->chip_version)
1437        {
1438            unifi_info(card->ospriv, "Failed to read valid chip version sr=%d (0x%04x want 0x%04x) try %d\n",
1439                       sr, data_u16, card->chip_version, i);
1440
1441            /* Set clock speed low */
1442            sr = CsrSdioMaxBusClockFrequencySet(card->sdio_if, UNIFI_SDIO_CLOCK_SAFE_HZ);
1443            if (sr != CSR_RESULT_SUCCESS)
1444            {
1445                unifi_error(card->ospriv, "CsrSdioMaxBusClockFrequencySet() failed1 %d\n", sr);
1446                r = ConvertCsrSdioToCsrHipResult(card, sr);
1447            }
1448            card->sdio_clock_speed = UNIFI_SDIO_CLOCK_SAFE_HZ;
1449
1450            /* First try re-enabling function in case a f/w watchdog reset disabled it */
1451            if (i == 0)
1452            {
1453                unifi_info(card->ospriv, "Try function enable\n");
1454                sr = CsrSdioFunctionEnable(card->sdio_if);
1455                if (sr != CSR_RESULT_SUCCESS)
1456                {
1457                    r = ConvertCsrSdioToCsrHipResult(card, sr);
1458                    unifi_error(card->ospriv, "CsrSdioFunctionEnable failed %d (HIP %d)\n", sr, r);
1459                }
1460                continue;
1461            }
1462
1463            /* Second try, set awake */
1464            unifi_info(card->ospriv, "Try set awake\n");
1465
1466            /* Ensure chip is awake */
1467            r = unifi_set_host_state(card, UNIFI_HOST_STATE_AWAKE);
1468            if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1469            {
1470                return r;
1471            }
1472            if (r != CSR_RESULT_SUCCESS)
1473            {
1474                unifi_error(card->ospriv, "unifi_set_host_state() failed2 %d\n", r);
1475            }
1476
1477            /* Set clock speed low in case setting the host state raised it, which
1478             * would only happen if host state was previously TORPID
1479             */
1480            sr = CsrSdioMaxBusClockFrequencySet(card->sdio_if, UNIFI_SDIO_CLOCK_SAFE_HZ);
1481            if (sr != CSR_RESULT_SUCCESS)
1482            {
1483                unifi_error(card->ospriv, "CsrSdioMaxBusClockFrequencySet() failed2 %d\n", sr);
1484            }
1485            card->sdio_clock_speed = UNIFI_SDIO_CLOCK_SAFE_HZ;
1486
1487            if (i == 1)
1488            {
1489                continue;
1490            }
1491
1492            /* Perform a s/w reset to preserve as much as the card state as possible,
1493             * (mainly the preserve RAM). The context will be lost for coredump - but as we
1494             * were unable to access the WLAN function for panic, the coredump would have
1495             * also failed without a reset.
1496             */
1497            unifi_info(card->ospriv, "Try s/w reset\n");
1498
1499            r = unifi_card_hard_reset(card);
1500            if (r != CSR_RESULT_SUCCESS)
1501            {
1502                unifi_error(card->ospriv, "unifi_card_hard_reset() failed %d\n", r);
1503            }
1504        }
1505        else
1506        {
1507            if (i > 0)
1508            {
1509                unifi_info(card->ospriv, "Read chip version 0x%x after %d retries\n", data_u16, i);
1510            }
1511            break;
1512        }
1513    }
1514
1515    r = ConvertCsrSdioToCsrHipResult(card, sr);
1516    func_exit_r(r);
1517    return r;
1518}
1519
1520
1521/*
1522 * ---------------------------------------------------------------------------
1523 * unifi_read_panic
1524 * Reads, saves and prints panic codes stored by the firmware in UniFi's
1525 * preserve RAM by the last panic that occurred since chip was powered.
1526 * Nothing is saved if the panic codes are read as zero.
1527 *
1528 * Arguments:
1529 * card Pointer to card struct
1530 *
1531 * Returns:
1532 * ---------------------------------------------------------------------------
1533 */
1534void unifi_read_panic(card_t *card)
1535{
1536    CsrResult r;
1537    u16 p_code, p_arg;
1538
1539    func_enter();
1540
1541    /* The firmware must have previously initialised to read the panic addresses
1542     * from the SLUT
1543     */
1544    if (!card->panic_data_phy_addr || !card->panic_data_mac_addr)
1545    {
1546        return;
1547    }
1548
1549    /* Get the panic data from PHY */
1550    r = unifi_card_read16(card, card->panic_data_phy_addr, &p_code);
1551    if (r != CSR_RESULT_SUCCESS)
1552    {
1553        unifi_error(card->ospriv, "capture_panic: unifi_read16 %08x failed %d\n", card->panic_data_phy_addr, r);
1554        p_code = 0;
1555    }
1556    if (p_code)
1557    {
1558        r = unifi_card_read16(card, card->panic_data_phy_addr + 2, &p_arg);
1559        if (r != CSR_RESULT_SUCCESS)
1560        {
1561            unifi_error(card->ospriv, "capture_panic: unifi_read16 %08x failed %d\n", card->panic_data_phy_addr + 2, r);
1562        }
1563        unifi_error(card->ospriv, "Last UniFi PHY PANIC %04x arg %04x\n", p_code, p_arg);
1564        card->last_phy_panic_code = p_code;
1565        card->last_phy_panic_arg = p_arg;
1566    }
1567
1568    /* Get the panic data from MAC */
1569    r = unifi_card_read16(card, card->panic_data_mac_addr, &p_code);
1570    if (r != CSR_RESULT_SUCCESS)
1571    {
1572        unifi_error(card->ospriv, "capture_panic: unifi_read16 %08x failed %d\n", card->panic_data_mac_addr, r);
1573        p_code = 0;
1574    }
1575    if (p_code)
1576    {
1577        r = unifi_card_read16(card, card->panic_data_mac_addr + 2, &p_arg);
1578        if (r != CSR_RESULT_SUCCESS)
1579        {
1580            unifi_error(card->ospriv, "capture_panic: unifi_read16 %08x failed %d\n", card->panic_data_mac_addr + 2, r);
1581        }
1582        unifi_error(card->ospriv, "Last UniFi MAC PANIC %04x arg %04x\n", p_code, p_arg);
1583        card->last_mac_panic_code = p_code;
1584        card->last_mac_panic_arg = p_arg;
1585    }
1586
1587    func_exit();
1588}
1589
1590
1591/*
1592 * ---------------------------------------------------------------------------
1593 * card_allocate_memory_resources
1594 *
1595 * Allocates memory for the from-host, to-host bulk data slots,
1596 * soft queue buffers and bulk data buffers.
1597 *
1598 * Arguments:
1599 * card Pointer to card struct
1600 *
1601 * Returns:
1602 * CSR_RESULT_SUCCESS on success, CSR error code on failure.
1603 * ---------------------------------------------------------------------------
1604 */
1605static CsrResult card_allocate_memory_resources(card_t *card)
1606{
1607    s16 n, i, k, r;
1608    sdio_config_data_t *cfg_data;
1609
1610    func_enter();
1611
1612    /* Reset any state carried forward from a previous life */
1613    card->fh_command_queue.q_rd_ptr = 0;
1614    card->fh_command_queue.q_wr_ptr = 0;
1615    (void)CsrSnprintf(card->fh_command_queue.name, UNIFI_QUEUE_NAME_MAX_LENGTH,
1616                      "fh_cmd_q");
1617    for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
1618    {
1619        card->fh_traffic_queue[i].q_rd_ptr = 0;
1620        card->fh_traffic_queue[i].q_wr_ptr = 0;
1621        (void)CsrSnprintf(card->fh_traffic_queue[i].name,
1622                          UNIFI_QUEUE_NAME_MAX_LENGTH, "fh_data_q%d", i);
1623    }
1624#ifndef CSR_WIFI_HIP_TA_DISABLE
1625    unifi_ta_sampling_init(card);
1626#endif
1627    /* Convenience short-cut */
1628    cfg_data = &card->config_data;
1629
1630    /*
1631     * Allocate memory for the from-host and to-host signal buffers.
1632     */
1633    card->fh_buffer.buf = kmalloc(UNIFI_FH_BUF_SIZE, GFP_KERNEL);
1634    if (card->fh_buffer.buf == NULL)
1635    {
1636        unifi_error(card->ospriv, "Failed to allocate memory for F-H signals\n");
1637        func_exit_r(CSR_WIFI_HIP_RESULT_NO_MEMORY);
1638        return CSR_WIFI_HIP_RESULT_NO_MEMORY;
1639    }
1640    card->fh_buffer.bufsize = UNIFI_FH_BUF_SIZE;
1641    card->fh_buffer.ptr = card->fh_buffer.buf;
1642    card->fh_buffer.count = 0;
1643
1644    card->th_buffer.buf = kmalloc(UNIFI_FH_BUF_SIZE, GFP_KERNEL);
1645    if (card->th_buffer.buf == NULL)
1646    {
1647        unifi_error(card->ospriv, "Failed to allocate memory for T-H signals\n");
1648        func_exit_r(CSR_WIFI_HIP_RESULT_NO_MEMORY);
1649        return CSR_WIFI_HIP_RESULT_NO_MEMORY;
1650    }
1651    card->th_buffer.bufsize = UNIFI_FH_BUF_SIZE;
1652    card->th_buffer.ptr = card->th_buffer.buf;
1653    card->th_buffer.count = 0;
1654
1655
1656    /*
1657     * Allocate memory for the from-host and to-host bulk data slots.
1658     * This is done as separate kmallocs because lots of smaller
1659     * allocations are more likely to succeed than one huge one.
1660     */
1661
1662    /* Allocate memory for the array of pointers */
1663    n = cfg_data->num_fromhost_data_slots;
1664
1665    unifi_trace(card->ospriv, UDBG3, "Alloc from-host resources, %d slots.\n", n);
1666    card->from_host_data = kmalloc(n * sizeof(slot_desc_t), GFP_KERNEL);
1667    if (card->from_host_data == NULL)
1668    {
1669        unifi_error(card->ospriv, "Failed to allocate memory for F-H bulk data array\n");
1670        func_exit_r(CSR_WIFI_HIP_RESULT_NO_MEMORY);
1671        return CSR_WIFI_HIP_RESULT_NO_MEMORY;
1672    }
1673
1674    /* Initialise from-host bulk data slots */
1675    for (i = 0; i < n; i++)
1676    {
1677        UNIFI_INIT_BULK_DATA(&card->from_host_data[i].bd);
1678    }
1679
1680    /* Allocate memory for the array used for slot host tag mapping */
1681    card->fh_slot_host_tag_record = kmalloc(n * sizeof(u32), GFP_KERNEL);
1682
1683    if (card->fh_slot_host_tag_record == NULL)
1684    {
1685        unifi_error(card->ospriv, "Failed to allocate memory for F-H slot host tag mapping array\n");
1686        func_exit_r(CSR_WIFI_HIP_RESULT_NO_MEMORY);
1687        return CSR_WIFI_HIP_RESULT_NO_MEMORY;
1688    }
1689
1690    /* Initialise host tag entries for from-host bulk data slots */
1691    for (i = 0; i < n; i++)
1692    {
1693        card->fh_slot_host_tag_record[i] = CSR_WIFI_HIP_RESERVED_HOST_TAG;
1694    }
1695
1696
1697    /* Allocate memory for the array of pointers */
1698    n = cfg_data->num_tohost_data_slots;
1699
1700    unifi_trace(card->ospriv, UDBG3, "Alloc to-host resources, %d slots.\n", n);
1701    card->to_host_data = kmalloc(n * sizeof(bulk_data_desc_t), GFP_KERNEL);
1702    if (card->to_host_data == NULL)
1703    {
1704        unifi_error(card->ospriv, "Failed to allocate memory for T-H bulk data array\n");
1705        func_exit_r(CSR_WIFI_HIP_RESULT_NO_MEMORY);
1706        return CSR_WIFI_HIP_RESULT_NO_MEMORY;
1707    }
1708
1709    /* Initialise to-host bulk data slots */
1710    for (i = 0; i < n; i++)
1711    {
1712        UNIFI_INIT_BULK_DATA(&card->to_host_data[i]);
1713    }
1714
1715    /*
1716     * Initialise buffers for soft Q
1717     */
1718    for (i = 0; i < UNIFI_SOFT_COMMAND_Q_LENGTH; i++)
1719    {
1720        for (r = 0; r < UNIFI_MAX_DATA_REFERENCES; r++)
1721        {
1722            UNIFI_INIT_BULK_DATA(&card->fh_command_q_body[i].bulkdata[r]);
1723        }
1724    }
1725
1726    for (k = 0; k < UNIFI_NO_OF_TX_QS; k++)
1727    {
1728        for (i = 0; i < UNIFI_SOFT_TRAFFIC_Q_LENGTH; i++)
1729        {
1730            for (r = 0; r < UNIFI_MAX_DATA_REFERENCES; r++)
1731            {
1732                UNIFI_INIT_BULK_DATA(&card->fh_traffic_q_body[k][i].bulkdata[r]);
1733            }
1734        }
1735    }
1736
1737    card->memory_resources_allocated = 1;
1738
1739    func_exit();
1740    return CSR_RESULT_SUCCESS;
1741} /* card_allocate_memory_resources() */
1742
1743
1744/*
1745 * ---------------------------------------------------------------------------
1746 * unifi_free_bulk_data
1747 *
1748 * Free the data associated to a bulk data structure.
1749 *
1750 * Arguments:
1751 * card Pointer to card struct
1752 * bulk_data_slot Pointer to bulk data structure
1753 *
1754 * Returns:
1755 * None.
1756 *
1757 * ---------------------------------------------------------------------------
1758 */
1759static void unifi_free_bulk_data(card_t *card, bulk_data_desc_t *bulk_data_slot)
1760{
1761    if (bulk_data_slot->data_length != 0)
1762    {
1763        unifi_net_data_free(card->ospriv, bulk_data_slot);
1764    }
1765} /* unifi_free_bulk_data() */
1766
1767
1768/*
1769 * ---------------------------------------------------------------------------
1770 * card_free_memory_resources
1771 *
1772 * Frees memory allocated for the from-host, to-host bulk data slots,
1773 * soft queue buffers and bulk data buffers.
1774 *
1775 * Arguments:
1776 * card Pointer to card struct
1777 *
1778 * Returns:
1779 * None.
1780 * ---------------------------------------------------------------------------
1781 */
1782static void card_free_memory_resources(card_t *card)
1783{
1784    func_enter();
1785
1786    unifi_trace(card->ospriv, UDBG1, "Freeing card memory resources.\n");
1787
1788    /* Clear our internal queues */
1789    unifi_cancel_pending_signals(card);
1790
1791
1792    kfree(card->to_host_data);
1793    card->to_host_data = NULL;
1794
1795    kfree(card->from_host_data);
1796    card->from_host_data = NULL;
1797
1798    /* free the memory for slot host tag mapping array */
1799    kfree(card->fh_slot_host_tag_record);
1800    card->fh_slot_host_tag_record = NULL;
1801
1802    kfree(card->fh_buffer.buf);
1803    card->fh_buffer.ptr = card->fh_buffer.buf = NULL;
1804    card->fh_buffer.bufsize = 0;
1805    card->fh_buffer.count = 0;
1806
1807    kfree(card->th_buffer.buf);
1808    card->th_buffer.ptr = card->th_buffer.buf = NULL;
1809    card->th_buffer.bufsize = 0;
1810    card->th_buffer.count = 0;
1811
1812
1813    card->memory_resources_allocated = 0;
1814
1815    func_exit();
1816} /* card_free_memory_resources() */
1817
1818
1819static void card_init_soft_queues(card_t *card)
1820{
1821    s16 i;
1822
1823    func_enter();
1824
1825    unifi_trace(card->ospriv, UDBG1, "Initialising internal signal queues.\n");
1826    /* Reset any state carried forward from a previous life */
1827    card->fh_command_queue.q_rd_ptr = 0;
1828    card->fh_command_queue.q_wr_ptr = 0;
1829    (void)CsrSnprintf(card->fh_command_queue.name, UNIFI_QUEUE_NAME_MAX_LENGTH,
1830                      "fh_cmd_q");
1831    for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
1832    {
1833        card->fh_traffic_queue[i].q_rd_ptr = 0;
1834        card->fh_traffic_queue[i].q_wr_ptr = 0;
1835        (void)CsrSnprintf(card->fh_traffic_queue[i].name,
1836                          UNIFI_QUEUE_NAME_MAX_LENGTH, "fh_data_q%d", i);
1837    }
1838#ifndef CSR_WIFI_HIP_TA_DISABLE
1839    unifi_ta_sampling_init(card);
1840#endif
1841    func_exit();
1842}
1843
1844
1845/*
1846 * ---------------------------------------------------------------------------
1847 * unifi_cancel_pending_signals
1848 *
1849 * Free the signals and associated bulk data, pending in the core.
1850 *
1851 * Arguments:
1852 * card Pointer to card struct
1853 *
1854 * Returns:
1855 * None.
1856 * ---------------------------------------------------------------------------
1857 */
1858void unifi_cancel_pending_signals(card_t *card)
1859{
1860    s16 i, n, r;
1861    func_enter();
1862
1863    unifi_trace(card->ospriv, UDBG1, "Canceling pending signals.\n");
1864
1865    if (card->to_host_data)
1866    {
1867        /*
1868         * Free any bulk data buffers allocated for the t-h slots
1869         * This will clear all buffers that did not make it to
1870         * unifi_receive_event() before cancel was request.
1871         */
1872        n = card->config_data.num_tohost_data_slots;
1873        unifi_trace(card->ospriv, UDBG3, "Freeing to-host resources, %d slots.\n", n);
1874        for (i = 0; i < n; i++)
1875        {
1876            unifi_free_bulk_data(card, &card->to_host_data[i]);
1877        }
1878    }
1879
1880    /*
1881     * If any of the from-host bulk data has reached the card->from_host_data
1882     * but not UniFi, we need to free the buffers here.
1883     */
1884    if (card->from_host_data)
1885    {
1886        /* Free any bulk data buffers allocated for the f-h slots */
1887        n = card->config_data.num_fromhost_data_slots;
1888        unifi_trace(card->ospriv, UDBG3, "Freeing from-host resources, %d slots.\n", n);
1889        for (i = 0; i < n; i++)
1890        {
1891            unifi_free_bulk_data(card, &card->from_host_data[i].bd);
1892        }
1893
1894        for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
1895        {
1896            card->dynamic_slot_data.from_host_used_slots[i] = 0;
1897            card->dynamic_slot_data.from_host_max_slots[i] = 0;
1898            card->dynamic_slot_data.from_host_reserved_slots[i] = 0;
1899        }
1900    }
1901
1902    /*
1903     * Free any bulk data buffers allocated in the soft queues.
1904     * This covers the case where a bulk data pointer has reached the soft queue
1905     * but not the card->from_host_data.
1906     */
1907    unifi_trace(card->ospriv, UDBG3, "Freeing cmd q resources.\n");
1908    for (i = 0; i < UNIFI_SOFT_COMMAND_Q_LENGTH; i++)
1909    {
1910        for (r = 0; r < UNIFI_MAX_DATA_REFERENCES; r++)
1911        {
1912            unifi_free_bulk_data(card, &card->fh_command_q_body[i].bulkdata[r]);
1913        }
1914    }
1915
1916    unifi_trace(card->ospriv, UDBG3, "Freeing traffic q resources.\n");
1917    for (n = 0; n < UNIFI_NO_OF_TX_QS; n++)
1918    {
1919        for (i = 0; i < UNIFI_SOFT_TRAFFIC_Q_LENGTH; i++)
1920        {
1921            for (r = 0; r < UNIFI_MAX_DATA_REFERENCES; r++)
1922            {
1923                unifi_free_bulk_data(card, &card->fh_traffic_q_body[n][i].bulkdata[r]);
1924            }
1925        }
1926    }
1927
1928    card_init_soft_queues(card);
1929
1930    func_exit();
1931} /* unifi_cancel_pending_signals() */
1932
1933
1934/*
1935 * ---------------------------------------------------------------------------
1936 * unifi_free_card
1937 *
1938 * Free the memory allocated for the card structure and buffers.
1939 *
1940 * Notes:
1941 * The porting layer is responsible for freeing any mini-coredump buffers
1942 * allocated when it called unifi_coredump_init(), by calling
1943 * unifi_coredump_free() before calling this function.
1944 *
1945 * Arguments:
1946 * card Pointer to card struct
1947 *
1948 * Returns:
1949 * None.
1950 * ---------------------------------------------------------------------------
1951 */
1952void unifi_free_card(card_t *card)
1953{
1954    func_enter();
1955#ifdef CSR_PRE_ALLOC_NET_DATA
1956    prealloc_netdata_free(card);
1957#endif
1958    /* Free any memory allocated. */
1959    card_free_memory_resources(card);
1960
1961    /* Warn if caller didn't free coredump buffers */
1962    if (card->dump_buf)
1963    {
1964        unifi_error(card->ospriv, "Caller should call unifi_coredump_free()\n");
1965        unifi_coredump_free(card); /* free anyway to prevent memory leak */
1966    }
1967
1968    kfree(card);
1969
1970    func_exit();
1971} /* unifi_free_card() */
1972
1973
1974/*
1975 * ---------------------------------------------------------------------------
1976 * card_init_slots
1977 *
1978 * Allocate memory for host-side slot data and signal queues.
1979 *
1980 * Arguments:
1981 * card Pointer to card object
1982 *
1983 * Returns:
1984 * CSR error code.
1985 * ---------------------------------------------------------------------------
1986 */
1987static CsrResult card_init_slots(card_t *card)
1988{
1989    CsrResult r;
1990    u8 i;
1991
1992    func_enter();
1993
1994    /* Allocate the buffers we need, only once. */
1995    if (card->memory_resources_allocated == 1)
1996    {
1997        card_free_memory_resources(card);
1998    }
1999    else
2000    {
2001        /* Initialise our internal command and traffic queues */
2002        card_init_soft_queues(card);
2003    }
2004
2005    r = card_allocate_memory_resources(card);
2006    if (r != CSR_RESULT_SUCCESS)
2007    {
2008        unifi_error(card->ospriv, "Failed to allocate card memory resources.\n");
2009        card_free_memory_resources(card);
2010        func_exit_r(r);
2011        return r;
2012    }
2013
2014    if (card->sdio_ctrl_addr == 0)
2015    {
2016        unifi_error(card->ospriv, "Failed to find config struct!\n");
2017        func_exit_r(CSR_WIFI_HIP_RESULT_INVALID_VALUE);
2018        return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
2019    }
2020
2021    /*
2022     * Set initial counts.
2023     */
2024
2025    card->from_host_data_head = 0;
2026
2027    /* Get initial signal counts from UniFi, in case it has not been reset. */
2028    {
2029        u16 s;
2030
2031        /* Get the from-host-signals-written count */
2032        r = unifi_card_read16(card, card->sdio_ctrl_addr + 0, &s);
2033        if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2034        {
2035            return r;
2036        }
2037        if (r != CSR_RESULT_SUCCESS)
2038        {
2039            unifi_error(card->ospriv, "Failed to read from-host sig written count\n");
2040            func_exit_r(r);
2041            return r;
2042        }
2043        card->from_host_signals_w = (s16)s;
2044
2045        /* Get the to-host-signals-written count */
2046        r = unifi_card_read16(card, card->sdio_ctrl_addr + 6, &s);
2047        if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2048        {
2049            return r;
2050        }
2051        if (r != CSR_RESULT_SUCCESS)
2052        {
2053            unifi_error(card->ospriv, "Failed to read to-host sig read count\n");
2054            func_exit_r(r);
2055            return r;
2056        }
2057        card->to_host_signals_r = (s16)s;
2058    }
2059
2060    /* Set Initialised flag. */
2061    r = unifi_card_write16(card, card->init_flag_addr, 0x0001);
2062    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2063    {
2064        return r;
2065    }
2066    if (r != CSR_RESULT_SUCCESS)
2067    {
2068        unifi_error(card->ospriv, "Failed to write initialised flag\n");
2069        func_exit_r(r);
2070        return r;
2071    }
2072
2073    /* Dynamic queue reservation */
2074    memset(&card->dynamic_slot_data, 0, sizeof(card_dynamic_slot_t));
2075
2076    for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
2077    {
2078        card->dynamic_slot_data.from_host_max_slots[i] = card->config_data.num_fromhost_data_slots -
2079                                                         UNIFI_RESERVED_COMMAND_SLOTS;
2080        card->dynamic_slot_data.queue_stable[i] = FALSE;
2081    }
2082
2083    card->dynamic_slot_data.packets_interval = UNIFI_PACKETS_INTERVAL;
2084
2085    func_exit();
2086    return CSR_RESULT_SUCCESS;
2087} /* card_init_slots() */
2088
2089
2090/*
2091 * ---------------------------------------------------------------------------
2092 * unifi_set_udi_hook
2093 *
2094 * Registers the udi hook that reports the sent signals to the core.
2095 *
2096 * Arguments:
2097 * card Pointer to the card context struct
2098 * udi_fn Pointer to the callback function.
2099 *
2100 * Returns:
2101 * CSR_WIFI_HIP_RESULT_INVALID_VALUE if the card pointer is invalid,
2102 * CSR_RESULT_SUCCESS on success.
2103 * ---------------------------------------------------------------------------
2104 */
2105CsrResult unifi_set_udi_hook(card_t *card, udi_func_t udi_fn)
2106{
2107    if (card == NULL)
2108    {
2109        return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
2110    }
2111
2112    if (card->udi_hook == NULL)
2113    {
2114        card->udi_hook = udi_fn;
2115    }
2116
2117    return CSR_RESULT_SUCCESS;
2118} /* unifi_set_udi_hook() */
2119
2120
2121/*
2122 * ---------------------------------------------------------------------------
2123 * unifi_remove_udi_hook
2124 *
2125 * Removes the udi hook that reports the sent signals from the core.
2126 *
2127 * Arguments:
2128 * card Pointer to the card context struct
2129 * udi_fn Pointer to the callback function.
2130 *
2131 * Returns:
2132 * CSR_WIFI_HIP_RESULT_INVALID_VALUE if the card pointer is invalid,
2133 * CSR_RESULT_SUCCESS on success.
2134 * ---------------------------------------------------------------------------
2135 */
2136CsrResult unifi_remove_udi_hook(card_t *card, udi_func_t udi_fn)
2137{
2138    if (card == NULL)
2139    {
2140        return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
2141    }
2142
2143    if (card->udi_hook == udi_fn)
2144    {
2145        card->udi_hook = NULL;
2146    }
2147
2148    return CSR_RESULT_SUCCESS;
2149} /* unifi_remove_udi_hook() */
2150
2151
2152static void CardReassignDynamicReservation(card_t *card)
2153{
2154    u8 i;
2155
2156    func_enter();
2157
2158    unifi_trace(card->ospriv, UDBG5, "Packets Txed %d %d %d %d\n",
2159                card->dynamic_slot_data.packets_txed[0],
2160                card->dynamic_slot_data.packets_txed[1],
2161                card->dynamic_slot_data.packets_txed[2],
2162                card->dynamic_slot_data.packets_txed[3]);
2163
2164    /* Clear reservation and recalculate max slots */
2165    for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
2166    {
2167        card->dynamic_slot_data.queue_stable[i] = FALSE;
2168        card->dynamic_slot_data.from_host_reserved_slots[i] = 0;
2169        card->dynamic_slot_data.from_host_max_slots[i] = card->config_data.num_fromhost_data_slots -
2170                                                         UNIFI_RESERVED_COMMAND_SLOTS;
2171        card->dynamic_slot_data.packets_txed[i] = 0;
2172
2173        unifi_trace(card->ospriv, UDBG5, "CardReassignDynamicReservation: queue %d reserved %d Max %d\n", i,
2174                    card->dynamic_slot_data.from_host_reserved_slots[i],
2175                    card->dynamic_slot_data.from_host_max_slots[i]);
2176    }
2177
2178    card->dynamic_slot_data.total_packets_txed = 0;
2179    func_exit();
2180}
2181
2182
2183/* Algorithm to dynamically reserve slots. The logic is based mainly on the outstanding queue
2184 * length. Slots are reserved for particular queues during an interval and cleared after the interval.
2185 * Each queue has three associated variables.. a) used slots - the number of slots currently occupied
2186 * by the queue b) reserved slots - number of slots reserved specifically for the queue c) max slots - total
2187 * slots that this queue can actually use (may be higher than reserved slots and is dependent on reserved slots
2188 * for other queues).
2189 * This function is called when there are no slots available for a queue. It checks to see if there are enough
2190 * unreserved slots sufficient for this request. If available these slots are reserved for the queue.
2191 * If there are not enough unreserved slots, a fair share for each queue is calculated based on the total slots
2192 * and the number of active queues (any queue with existing reservation is considered active). Queues needing
2193 * less than their fair share are allowed to have the previously reserved slots. The remaining slots are
2194 * distributed evenly among queues that need more than the fair share
2195 *
2196 * A better scheme would take current bandwidth per AC into consideration when reserving slots. An
2197 * implementation scheme could consider the relative time/service period for slots in an AC. If the firmware
2198 * services other ACs faster than a particular AC (packets wait in the slots longer) then it is fair to reserve
2199 * less slots for the AC
2200 */
2201static void CardCheckDynamicReservation(card_t *card, unifi_TrafficQueue queue)
2202{
2203    u16 q_len, active_queues = 0, excess_queue_slots, div_extra_slots,
2204              queue_fair_share, reserved_slots = 0, q, excess_need_queues = 0, unmovable_slots = 0;
2205    s32 i;
2206    q_t *sigq;
2207    u16 num_data_slots = card->config_data.num_fromhost_data_slots - UNIFI_RESERVED_COMMAND_SLOTS;
2208
2209    func_enter();
2210
2211    /* Calculate the pending queue length */
2212    sigq = &card->fh_traffic_queue[queue];
2213    q_len = CSR_WIFI_HIP_Q_SLOTS_USED(sigq);
2214
2215    if (q_len <= card->dynamic_slot_data.from_host_reserved_slots[queue])
2216    {
2217        unifi_trace(card->ospriv, UDBG5, "queue %d q_len %d already has that many reserved slots, exiting\n", queue, q_len);
2218        func_exit();
2219        return;
2220    }
2221
2222    /* Upper limit */
2223    if (q_len > num_data_slots)
2224    {
2225        q_len = num_data_slots;
2226    }
2227
2228    for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
2229    {
2230        if (i != (s32)queue)
2231        {
2232            reserved_slots += card->dynamic_slot_data.from_host_reserved_slots[i];
2233        }
2234        if ((i == (s32)queue) || (card->dynamic_slot_data.from_host_reserved_slots[i] > 0))
2235        {
2236            active_queues++;
2237        }
2238    }
2239
2240    unifi_trace(card->ospriv, UDBG5, "CardCheckDynamicReservation: queue %d q_len %d\n", queue, q_len);
2241    unifi_trace(card->ospriv, UDBG5, "Active queues %d reserved slots on other queues %d\n",
2242                active_queues, reserved_slots);
2243
2244    if (reserved_slots + q_len <= num_data_slots)
2245    {
2246        card->dynamic_slot_data.from_host_reserved_slots[queue] = q_len;
2247        if (q_len == num_data_slots)
2248        {
2249            /* This is the common case when just 1 stream is going */
2250            card->dynamic_slot_data.queue_stable[queue] = TRUE;
2251        }
2252    }
2253    else
2254    {
2255        queue_fair_share = num_data_slots / active_queues;
2256        unifi_trace(card->ospriv, UDBG5, "queue fair share %d\n", queue_fair_share);
2257
2258        /* Evenly distribute slots among active queues */
2259        /* Find out the queues that need excess of fair share. Also find slots allocated
2260         * to queues less than their fair share, these slots cannot be reallocated (unmovable slots) */
2261
2262        card->dynamic_slot_data.from_host_reserved_slots[queue] = q_len;
2263
2264        for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
2265        {
2266            if (card->dynamic_slot_data.from_host_reserved_slots[i] > queue_fair_share)
2267            {
2268                excess_need_queues++;
2269            }
2270            else
2271            {
2272                unmovable_slots += card->dynamic_slot_data.from_host_reserved_slots[i];
2273            }
2274        }
2275
2276        unifi_trace(card->ospriv, UDBG5, "Excess need queues %d\n", excess_need_queues);
2277
2278        /* Now find the slots per excess demand queue */
2279        excess_queue_slots = (num_data_slots - unmovable_slots) / excess_need_queues;
2280        div_extra_slots = (num_data_slots - unmovable_slots) - excess_queue_slots * excess_need_queues;
2281        for (i = UNIFI_NO_OF_TX_QS - 1; i >= 0; i--)
2282        {
2283            if (card->dynamic_slot_data.from_host_reserved_slots[i] > excess_queue_slots)
2284            {
2285                card->dynamic_slot_data.from_host_reserved_slots[i] = excess_queue_slots;
2286                if (div_extra_slots > 0)
2287                {
2288                    card->dynamic_slot_data.from_host_reserved_slots[i]++;
2289                    div_extra_slots--;
2290                }
2291                /* No more slots will be allocated to this queue during the current interval */
2292                card->dynamic_slot_data.queue_stable[i] = TRUE;
2293                unifi_trace(card->ospriv, UDBG5, "queue stable %d\n", i);
2294            }
2295        }
2296    }
2297
2298    /* Redistribute max slots */
2299    for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
2300    {
2301        reserved_slots = 0;
2302        for (q = 0; q < UNIFI_NO_OF_TX_QS; q++)
2303        {
2304            if (i != q)
2305            {
2306                reserved_slots += card->dynamic_slot_data.from_host_reserved_slots[q];
2307            }
2308        }
2309
2310        card->dynamic_slot_data.from_host_max_slots[i] = num_data_slots - reserved_slots;
2311        unifi_trace(card->ospriv, UDBG5, "queue %d reserved %d Max %d\n", i,
2312                    card->dynamic_slot_data.from_host_reserved_slots[i],
2313                    card->dynamic_slot_data.from_host_max_slots[i]);
2314    }
2315
2316    func_exit();
2317}
2318
2319
2320/*
2321 * ---------------------------------------------------------------------------
2322 * CardClearFromHostDataSlot
2323 *
2324 * Clear a the given data slot, making it available again.
2325 *
2326 * Arguments:
2327 * card Pointer to Card object
2328 * slot Index of the signal slot to clear.
2329 *
2330 * Returns:
2331 * None.
2332 * ---------------------------------------------------------------------------
2333 */
2334void CardClearFromHostDataSlot(card_t *card, const s16 slot)
2335{
2336    u8 queue = card->from_host_data[slot].queue;
2337    const void *os_data_ptr = card->from_host_data[slot].bd.os_data_ptr;
2338
2339    func_enter();
2340
2341    if (card->from_host_data[slot].bd.data_length == 0)
2342    {
2343        unifi_warning(card->ospriv,
2344                      "Surprise: request to clear an already free FH data slot: %d\n",
2345                      slot);
2346        func_exit();
2347        return;
2348    }
2349
2350    if (os_data_ptr == NULL)
2351    {
2352        unifi_warning(card->ospriv,
2353                      "Clearing FH data slot %d: has null payload, len=%d\n",
2354                      slot, card->from_host_data[slot].bd.data_length);
2355    }
2356
2357    /* Free card->from_host_data[slot].bd.os_net_ptr here. */
2358    /* Mark slot as free by setting length to 0. */
2359    unifi_free_bulk_data(card, &card->from_host_data[slot].bd);
2360    if (queue < UNIFI_NO_OF_TX_QS)
2361    {
2362        if (card->dynamic_slot_data.from_host_used_slots[queue] == 0)
2363        {
2364            unifi_error(card->ospriv, "Goofed up used slots q = %d used slots = %d\n",
2365                        queue,
2366                        card->dynamic_slot_data.from_host_used_slots[queue]);
2367        }
2368        else
2369        {
2370            card->dynamic_slot_data.from_host_used_slots[queue]--;
2371        }
2372        card->dynamic_slot_data.packets_txed[queue]++;
2373        card->dynamic_slot_data.total_packets_txed++;
2374        if (card->dynamic_slot_data.total_packets_txed >= card->dynamic_slot_data.packets_interval)
2375        {
2376            CardReassignDynamicReservation(card);
2377        }
2378    }
2379
2380    unifi_trace(card->ospriv, UDBG4, "CardClearFromHostDataSlot: slot %d recycled %p\n", slot, os_data_ptr);
2381
2382    func_exit();
2383} /* CardClearFromHostDataSlot() */
2384
2385
2386#ifdef CSR_WIFI_REQUEUE_PACKET_TO_HAL
2387/*
2388 * ---------------------------------------------------------------------------
2389 * CardClearFromHostDataSlotWithoutFreeingBulkData
2390 *
2391 * Clear the given data slot with out freeing the bulk data.
2392 *
2393 * Arguments:
2394 * card Pointer to Card object
2395 * slot Index of the signal slot to clear.
2396 *
2397 * Returns:
2398 * None.
2399 * ---------------------------------------------------------------------------
2400 */
2401void CardClearFromHostDataSlotWithoutFreeingBulkData(card_t *card, const s16 slot)
2402{
2403    u8 queue = card->from_host_data[slot].queue;
2404
2405    /* Initialise the from_host data slot so it can be re-used,
2406     * Set length field in from_host_data array to 0.
2407     */
2408    UNIFI_INIT_BULK_DATA(&card->from_host_data[slot].bd);
2409
2410    queue = card->from_host_data[slot].queue;
2411
2412    if (queue < UNIFI_NO_OF_TX_QS)
2413    {
2414        if (card->dynamic_slot_data.from_host_used_slots[queue] == 0)
2415        {
2416            unifi_error(card->ospriv, "Goofed up used slots q = %d used slots = %d\n",
2417                        queue,
2418                        card->dynamic_slot_data.from_host_used_slots[queue]);
2419        }
2420        else
2421        {
2422            card->dynamic_slot_data.from_host_used_slots[queue]--;
2423        }
2424        card->dynamic_slot_data.packets_txed[queue]++;
2425        card->dynamic_slot_data.total_packets_txed++;
2426        if (card->dynamic_slot_data.total_packets_txed >=
2427            card->dynamic_slot_data.packets_interval)
2428        {
2429            CardReassignDynamicReservation(card);
2430        }
2431    }
2432} /* CardClearFromHostDataSlotWithoutFreeingBulkData() */
2433
2434
2435#endif
2436
2437u16 CardGetDataSlotSize(card_t *card)
2438{
2439    return card->config_data.data_slot_size;
2440} /* CardGetDataSlotSize() */
2441
2442
2443/*
2444 * ---------------------------------------------------------------------------
2445 * CardGetFreeFromHostDataSlots
2446 *
2447 * Retrieve the number of from-host bulk data slots available.
2448 *
2449 * Arguments:
2450 * card Pointer to the card context struct
2451 *
2452 * Returns:
2453 * Number of free from-host bulk data slots.
2454 * ---------------------------------------------------------------------------
2455 */
2456u16 CardGetFreeFromHostDataSlots(card_t *card)
2457{
2458    u16 i, n = 0;
2459
2460    func_enter();
2461
2462    /* First two slots reserved for MLME */
2463    for (i = 0; i < card->config_data.num_fromhost_data_slots; i++)
2464    {
2465        if (card->from_host_data[i].bd.data_length == 0)
2466        {
2467            /* Free slot */
2468            n++;
2469        }
2470    }
2471
2472    func_exit();
2473    return n;
2474} /* CardGetFreeFromHostDataSlots() */
2475
2476
2477/*
2478 * ---------------------------------------------------------------------------
2479 * CardAreAllFromHostDataSlotsEmpty
2480 *
2481 * Returns the state of from-host bulk data slots.
2482 *
2483 * Arguments:
2484 * card Pointer to the card context struct
2485 *
2486 * Returns:
2487 * 1 The from-host bulk data slots are all empty (available).
2488 * 0 Some or all the from-host bulk data slots are in use.
2489 * ---------------------------------------------------------------------------
2490 */
2491u16 CardAreAllFromHostDataSlotsEmpty(card_t *card)
2492{
2493    u16 i;
2494
2495    for (i = 0; i < card->config_data.num_fromhost_data_slots; i++)
2496    {
2497        if (card->from_host_data[i].bd.data_length != 0)
2498        {
2499            return 0;
2500        }
2501    }
2502
2503    return 1;
2504} /* CardGetFreeFromHostDataSlots() */
2505
2506
2507static CsrResult unifi_identify_hw(card_t *card)
2508{
2509    func_enter();
2510
2511    card->chip_id = card->sdio_if->sdioId.cardId;
2512    card->function = card->sdio_if->sdioId.sdioFunction;
2513    card->sdio_io_block_size = card->sdio_if->blockSize;
2514
2515    /* If SDIO controller doesn't support byte mode CMD53, pad transfers to block sizes */
2516    card->sdio_io_block_pad = (card->sdio_if->features & CSR_SDIO_FEATURE_BYTE_MODE)?FALSE : TRUE;
2517
2518    /*
2519     * Setup the chip helper so that we can access the registers (and
2520     * also tell what sub-type of HIP we should use).
2521     */
2522    card->helper = ChipHelper_GetVersionSdio((u8)card->chip_id);
2523    if (!card->helper)
2524    {
2525        unifi_error(card->ospriv, "Null ChipHelper\n");
2526    }
2527
2528    unifi_info(card->ospriv, "Chip ID 0x%02X Function %u Block Size %u Name %s(%s)\n",
2529               card->chip_id, card->function, card->sdio_io_block_size,
2530               ChipHelper_MarketingName(card->helper),
2531               ChipHelper_FriendlyName(card->helper));
2532
2533    func_exit();
2534    return CSR_RESULT_SUCCESS;
2535} /* unifi_identify_hw() */
2536
2537
2538static CsrResult unifi_prepare_hw(card_t *card)
2539{
2540    CsrResult r;
2541    CsrResult csrResult;
2542    enum unifi_host_state old_state = card->host_state;
2543
2544    func_enter();
2545
2546    r = unifi_identify_hw(card);
2547    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2548    {
2549        return r;
2550    }
2551    if (r != CSR_RESULT_SUCCESS)
2552    {
2553        unifi_error(card->ospriv, "Failed to identify hw\n");
2554        func_exit_r(r);
2555        return r;
2556    }
2557
2558    unifi_trace(card->ospriv, UDBG1,
2559                "%s mode SDIO\n", card->sdio_io_block_pad?"Block" : "Byte");
2560    /*
2561     * Chip must be a awake or blocks that are asleep may not get
2562     * reset. We can only do this after we have read the chip_id.
2563     */
2564    r = unifi_set_host_state(card, UNIFI_HOST_STATE_AWAKE);
2565    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2566    {
2567        return r;
2568    }
2569
2570    if (old_state == UNIFI_HOST_STATE_TORPID)
2571    {
2572        /* Ensure the initial clock rate is set; if a reset occured when the chip was
2573         * TORPID, unifi_set_host_state() may have raised it to MAX.
2574         */
2575        csrResult = CsrSdioMaxBusClockFrequencySet(card->sdio_if, UNIFI_SDIO_CLOCK_INIT_HZ);
2576        if (csrResult != CSR_RESULT_SUCCESS)
2577        {
2578            r = ConvertCsrSdioToCsrHipResult(card, csrResult);
2579            func_exit_r(r);
2580            return r;
2581        }
2582        card->sdio_clock_speed = UNIFI_SDIO_CLOCK_INIT_HZ;
2583    }
2584
2585    /*
2586     * The WLAN function must be enabled to access MAILBOX2 and DEBUG_RST
2587     * registers.
2588     */
2589    csrResult = CsrSdioFunctionEnable(card->sdio_if);
2590    if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
2591    {
2592        return CSR_WIFI_HIP_RESULT_NO_DEVICE;
2593    }
2594    if (csrResult != CSR_RESULT_SUCCESS)
2595    {
2596        r = ConvertCsrSdioToCsrHipResult(card, csrResult);
2597        /* Can't enable WLAN function. Try resetting the SDIO block. */
2598        unifi_error(card->ospriv, "Failed to re-enable function %d.\n", card->function);
2599        func_exit_r(r);
2600        return r;
2601    }
2602
2603    /*
2604     * Poke some registers to make sure the PLL has started,
2605     * otherwise memory accesses are likely to fail.
2606     */
2607    bootstrap_chip_hw(card);
2608
2609    /* Try to read the chip version from register. */
2610    r = unifi_read_chip_version(card);
2611    if (r != CSR_RESULT_SUCCESS)
2612    {
2613        func_exit_r(r);
2614        return r;
2615    }
2616
2617    func_exit();
2618    return CSR_RESULT_SUCCESS;
2619} /* unifi_prepare_hw() */
2620
2621
2622static CsrResult unifi_read_chip_version(card_t *card)
2623{
2624    u32 gbl_chip_version;
2625    CsrResult r;
2626    u16 ver;
2627
2628    func_enter();
2629
2630    gbl_chip_version = ChipHelper_GBL_CHIP_VERSION(card->helper);
2631
2632    /* Try to read the chip version from register. */
2633    if (gbl_chip_version != 0)
2634    {
2635        r = unifi_read_direct16(card, gbl_chip_version * 2, &ver);
2636        if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2637        {
2638            return r;
2639        }
2640        if (r != CSR_RESULT_SUCCESS)
2641        {
2642            unifi_error(card->ospriv, "Failed to read GBL_CHIP_VERSION\n");
2643            func_exit_r(r);
2644            return r;
2645        }
2646        card->chip_version = ver;
2647    }
2648    else
2649    {
2650        unifi_info(card->ospriv, "Unknown Chip ID, cannot locate GBL_CHIP_VERSION\n");
2651        r = CSR_RESULT_FAILURE;
2652    }
2653
2654    unifi_info(card->ospriv, "Chip Version 0x%04X\n", card->chip_version);
2655
2656    func_exit_r(r);
2657    return r;
2658} /* unifi_read_chip_version() */
2659
2660
2661/*
2662 * ---------------------------------------------------------------------------
2663 * unifi_reset_hardware
2664 *
2665 * Execute the UniFi reset sequence.
2666 *
2667 * Note: This may fail if the chip is going TORPID so retry at
2668 * least once.
2669 *
2670 * Arguments:
2671 * card - pointer to card context structure
2672 *
2673 * Returns:
2674 * CSR_RESULT_SUCCESS on success, CSR error otherwise.
2675 *
2676 * Notes:
2677 * Some platforms (e.g. Windows Vista) do not allow access to registers
2678 * that are necessary for a software soft reset.
2679 * ---------------------------------------------------------------------------
2680 */
2681static CsrResult unifi_reset_hardware(card_t *card)
2682{
2683    CsrResult r;
2684    u16 new_block_size = UNIFI_IO_BLOCK_SIZE;
2685    CsrResult csrResult;
2686
2687    func_enter();
2688
2689    /* Errors returned by unifi_prepare_hw() are not critical at this point */
2690    r = unifi_prepare_hw(card);
2691    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2692    {
2693        return r;
2694    }
2695
2696    /* First try SDIO controller reset, which may power cycle the UniFi, assert
2697     * its reset line, or not be implemented depending on the platform.
2698     */
2699    unifi_info(card->ospriv, "Calling CsrSdioHardReset\n");
2700    csrResult = CsrSdioHardReset(card->sdio_if);
2701    if (csrResult == CSR_RESULT_SUCCESS)
2702    {
2703        unifi_info(card->ospriv, "CsrSdioHardReset succeeded on reseting UniFi\n");
2704        r = unifi_prepare_hw(card);
2705        if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2706        {
2707            return r;
2708        }
2709        if (r != CSR_RESULT_SUCCESS)
2710        {
2711            unifi_error(card->ospriv, "unifi_prepare_hw failed after hard reset\n");
2712            func_exit_r(r);
2713            return r;
2714        }
2715    }
2716    else if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
2717    {
2718        return CSR_WIFI_HIP_RESULT_NO_DEVICE;
2719    }
2720    else
2721    {
2722        /* Falling back to software hard reset methods */
2723        unifi_info(card->ospriv, "Falling back to software hard reset\n");
2724        r = unifi_card_hard_reset(card);
2725        if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2726        {
2727            return r;
2728        }
2729        if (r != CSR_RESULT_SUCCESS)
2730        {
2731            unifi_error(card->ospriv, "software hard reset failed\n");
2732            func_exit_r(r);
2733            return r;
2734        }
2735
2736        /* If we fell back to unifi_card_hard_reset() methods, chip version may
2737         * not have been read. (Note in the unlikely event that it is zero,
2738         * it will be harmlessly read again)
2739         */
2740        if (card->chip_version == 0)
2741        {
2742            r = unifi_read_chip_version(card);
2743            if (r != CSR_RESULT_SUCCESS)
2744            {
2745                func_exit_r(r);
2746                return r;
2747            }
2748        }
2749    }
2750
2751#ifdef CSR_WIFI_HIP_SDIO_BLOCK_SIZE
2752    new_block_size = CSR_WIFI_HIP_SDIO_BLOCK_SIZE;
2753#endif
2754
2755    /* After hard reset, we need to restore the SDIO block size */
2756    csrResult = CsrSdioBlockSizeSet(card->sdio_if, new_block_size);
2757    r = ConvertCsrSdioToCsrHipResult(card, csrResult);
2758
2759    /* Warn if a different block size was achieved by the transport */
2760    if (card->sdio_if->blockSize != new_block_size)
2761    {
2762        unifi_info(card->ospriv,
2763                   "Actually got block size %d\n", card->sdio_if->blockSize);
2764    }
2765
2766    /* sdio_io_block_size always needs be updated from the achieved block size,
2767     * as it is used by the OS layer to allocate memory in unifi_net_malloc().
2768     * Controllers which don't support block mode (e.g. CSPI) will report a
2769     * block size of zero.
2770     */
2771    if (card->sdio_if->blockSize == 0)
2772    {
2773        unifi_info(card->ospriv, "Block size 0, block mode not available\n");
2774
2775        /* Set sdio_io_block_size to 1 so that unifi_net_data_malloc() has a
2776         * sensible rounding value. Elsewhere padding will already be
2777         * disabled because the controller supports byte mode.
2778         */
2779        card->sdio_io_block_size = 1;
2780
2781        /* Controller features must declare support for byte mode */
2782        if (!(card->sdio_if->features & CSR_SDIO_FEATURE_BYTE_MODE))
2783        {
2784            unifi_error(card->ospriv, "Requires byte mode\n");
2785            r = CSR_WIFI_HIP_RESULT_INVALID_VALUE;
2786        }
2787    }
2788    else
2789    {
2790        /* Padding will be enabled if CSR_SDIO_FEATURE_BYTE_MODE isn't set */
2791        card->sdio_io_block_size = card->sdio_if->blockSize;
2792    }
2793
2794
2795    func_exit_r(r);
2796    return r;
2797} /* unifi_reset_hardware() */
2798
2799
2800/*
2801 * ---------------------------------------------------------------------------
2802 * card_reset_method_io_enable
2803 *
2804 * Issue a hard reset to the hw writing the IO_ENABLE.
2805 *
2806 * Arguments:
2807 * card Pointer to Card object
2808 *
2809 * Returns:
2810 * 0 on success,
2811 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
2812 * CSR_RESULT_FAILURE if an SDIO error occurred or if a response
2813 * was not seen in the expected time
2814 * ---------------------------------------------------------------------------
2815 */
2816static CsrResult card_reset_method_io_enable(card_t *card)
2817{
2818    CsrResult r;
2819    CsrResult csrResult;
2820
2821    func_enter();
2822
2823    /*
2824     * This resets only function 1, so should be used in
2825     * preference to the method below (CSR_FUNC_EN)
2826     */
2827    unifi_trace(card->ospriv, UDBG1, "Hard reset (IO_ENABLE)\n");
2828
2829    csrResult = CsrSdioFunctionDisable(card->sdio_if);
2830    if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
2831    {
2832        return CSR_WIFI_HIP_RESULT_NO_DEVICE;
2833    }
2834    if (csrResult != CSR_RESULT_SUCCESS)
2835    {
2836        r = ConvertCsrSdioToCsrHipResult(card, csrResult);
2837        unifi_warning(card->ospriv, "SDIO error writing IO_ENABLE: %d\n", r);
2838    }
2839    else
2840    {
2841        /* Delay here to let the reset take affect. */
2842        CsrThreadSleep(RESET_SETTLE_DELAY);
2843
2844        r = card_wait_for_unifi_to_disable(card);
2845        if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2846        {
2847            return r;
2848        }
2849
2850        if (r == CSR_RESULT_SUCCESS)
2851        {
2852            r = card_wait_for_unifi_to_reset(card);
2853            if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2854            {
2855                return r;
2856            }
2857        }
2858    }
2859
2860    if (r != CSR_RESULT_SUCCESS)
2861    {
2862        unifi_trace(card->ospriv, UDBG1, "Hard reset (CSR_FUNC_EN)\n");
2863
2864        r = sdio_write_f0(card, SDIO_CSR_FUNC_EN, 0);
2865        if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2866        {
2867            return r;
2868        }
2869        if (r != CSR_RESULT_SUCCESS)
2870        {
2871            unifi_warning(card->ospriv, "SDIO error writing SDIO_CSR_FUNC_EN: %d\n", r);
2872            func_exit_r(r);
2873            return r;
2874        }
2875        else
2876        {
2877            /* Delay here to let the reset take affect. */
2878            CsrThreadSleep(RESET_SETTLE_DELAY);
2879
2880            r = card_wait_for_unifi_to_reset(card);
2881            if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2882            {
2883                return r;
2884            }
2885        }
2886    }
2887
2888    if (r != CSR_RESULT_SUCCESS)
2889    {
2890        unifi_warning(card->ospriv, "card_reset_method_io_enable failed to reset UniFi\n");
2891    }
2892
2893    func_exit();
2894    return r;
2895} /* card_reset_method_io_enable() */
2896
2897
2898/*
2899 * ---------------------------------------------------------------------------
2900 * card_reset_method_dbg_reset
2901 *
2902 * Issue a hard reset to the hw writing the DBG_RESET.
2903 *
2904 * Arguments:
2905 * card Pointer to Card object
2906 *
2907 * Returns:
2908 * CSR_RESULT_SUCCESS on success,
2909 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
2910 * CSR_RESULT_FAILURE if an SDIO error occurred or if a response
2911 * was not seen in the expected time
2912 * ---------------------------------------------------------------------------
2913 */
2914static CsrResult card_reset_method_dbg_reset(card_t *card)
2915{
2916    CsrResult r;
2917
2918    func_enter();
2919
2920    /*
2921     * Prepare UniFi for h/w reset
2922     */
2923    if (card->host_state == UNIFI_HOST_STATE_TORPID)
2924    {
2925        r = unifi_set_host_state(card, UNIFI_HOST_STATE_DROWSY);
2926        if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2927        {
2928            return r;
2929        }
2930        if (r != CSR_RESULT_SUCCESS)
2931        {
2932            unifi_error(card->ospriv, "Failed to set UNIFI_HOST_STATE_DROWSY\n");
2933            func_exit_r(r);
2934            return r;
2935        }
2936        CsrThreadSleep(5);
2937    }
2938
2939    r = unifi_card_stop_processor(card, UNIFI_PROC_BOTH);
2940    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2941    {
2942        return r;
2943    }
2944    if (r != CSR_RESULT_SUCCESS)
2945    {
2946        unifi_error(card->ospriv, "Can't stop processors\n");
2947        func_exit();
2948        return r;
2949    }
2950
2951    unifi_trace(card->ospriv, UDBG1, "Hard reset (DBG_RESET)\n");
2952
2953    /*
2954     * This register write may fail. The debug reset resets
2955     * parts of the Function 0 sections of the chip, and
2956     * therefore the response cannot be sent back to the host.
2957     */
2958    r = unifi_write_direct_8_or_16(card, ChipHelper_DBG_RESET(card->helper) * 2, 1);
2959    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2960    {
2961        return r;
2962    }
2963    if (r != CSR_RESULT_SUCCESS)
2964    {
2965        unifi_warning(card->ospriv, "SDIO error writing DBG_RESET: %d\n", r);
2966        func_exit_r(r);
2967        return r;
2968    }
2969
2970    /* Delay here to let the reset take affect. */
2971    CsrThreadSleep(RESET_SETTLE_DELAY);
2972
2973    r = card_wait_for_unifi_to_reset(card);
2974    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2975    {
2976        return r;
2977    }
2978    if (r != CSR_RESULT_SUCCESS)
2979    {
2980        unifi_warning(card->ospriv, "card_reset_method_dbg_reset failed to reset UniFi\n");
2981    }
2982
2983    func_exit();
2984    return r;
2985} /* card_reset_method_dbg_reset() */
2986
2987
2988/*
2989 * ---------------------------------------------------------------------------
2990 * unifi_card_hard_reset
2991 *
2992 * Issue reset to hardware, by writing to registers on the card.
2993 * Power to the card is preserved.
2994 *
2995 * Arguments:
2996 * card Pointer to Card object
2997 *
2998 * Returns:
2999 * CSR_RESULT_SUCCESS on success,
3000 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3001 * CSR_RESULT_FAILURE if an SDIO error occurred or if a response
3002 * was not seen in the expected time
3003 * ---------------------------------------------------------------------------
3004 */
3005CsrResult unifi_card_hard_reset(card_t *card)
3006{
3007    CsrResult r;
3008    const struct chip_helper_reset_values *init_data;
3009    u32 chunks;
3010
3011    func_enter();
3012
3013    /* Clear cache of page registers */
3014    card->proc_select = (u32)(-1);
3015    card->dmem_page = (u32)(-1);
3016    card->pmem_page = (u32)(-1);
3017
3018    /*
3019     * We need to have a valid card->helper before we use software hard reset.
3020     * If unifi_identify_hw() fails to get the card ID, it probably means
3021     * that there is no way to talk to the h/w.
3022     */
3023    r = unifi_identify_hw(card);
3024    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3025    {
3026        return r;
3027    }
3028    if (r != CSR_RESULT_SUCCESS)
3029    {
3030        unifi_error(card->ospriv, "unifi_card_hard_reset failed to identify h/w\n");
3031        func_exit();
3032        return r;
3033    }
3034
3035    /* Search for some reset code. */
3036    chunks = ChipHelper_HostResetSequence(card->helper, &init_data);
3037    if (chunks != 0)
3038    {
3039        unifi_error(card->ospriv,
3040                    "Hard reset (Code download) is unsupported\n");
3041
3042        func_exit_r(CSR_RESULT_FAILURE);
3043        return CSR_RESULT_FAILURE;
3044    }
3045
3046    if (card->chip_id > SDIO_CARD_ID_UNIFI_2)
3047    {
3048        /* The HIP spec considers this a bus-specific reset.
3049         * This resets only function 1, so should be used in
3050         * preference to the method below (CSR_FUNC_EN)
3051         * If this method fails, it means that the f/w is probably
3052         * not running. In this case, try the DBG_RESET method.
3053         */
3054        r = card_reset_method_io_enable(card);
3055        if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3056        {
3057            return r;
3058        }
3059        if (r == CSR_RESULT_SUCCESS)
3060        {
3061            func_exit();
3062            return r;
3063        }
3064    }
3065
3066    /* Software hard reset */
3067    r = card_reset_method_dbg_reset(card);
3068
3069    func_exit_r(r);
3070    return r;
3071} /* unifi_card_hard_reset() */
3072
3073
3074/*
3075 * ---------------------------------------------------------------------------
3076 *
3077 * CardGenInt
3078 *
3079 * Prod the card.
3080 * This function causes an internal interrupt to be raised in the
3081 * UniFi chip. It is used to signal the firmware that some action has
3082 * been completed.
3083 * The UniFi Host Interface asks that the value used increments for
3084 * debugging purposes.
3085 *
3086 * Arguments:
3087 * card Pointer to Card object
3088 *
3089 * Returns:
3090 * CSR_RESULT_SUCCESS on success,
3091 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3092 * CSR_RESULT_FAILURE if an SDIO error occurred or if a response
3093 * was not seen in the expected time
3094 * ---------------------------------------------------------------------------
3095 */
3096CsrResult CardGenInt(card_t *card)
3097{
3098    CsrResult r;
3099
3100    func_enter();
3101
3102    if (card->chip_id > SDIO_CARD_ID_UNIFI_2)
3103    {
3104        r = sdio_write_f0(card, SDIO_CSR_FROM_HOST_SCRATCH0,
3105                          (u8)card->unifi_interrupt_seq);
3106    }
3107    else
3108    {
3109        r = unifi_write_direct_8_or_16(card,
3110                                       ChipHelper_SHARED_IO_INTERRUPT(card->helper) * 2,
3111                                       (u8)card->unifi_interrupt_seq);
3112    }
3113    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3114    {
3115        return r;
3116    }
3117    if (r != CSR_RESULT_SUCCESS)
3118    {
3119        unifi_error(card->ospriv, "SDIO error writing UNIFI_SHARED_IO_INTERRUPT: %d\n", r);
3120        func_exit_r(r);
3121        return r;
3122    }
3123
3124    card->unifi_interrupt_seq++;
3125
3126    func_exit();
3127    return CSR_RESULT_SUCCESS;
3128} /* CardGenInt() */
3129
3130
3131/*
3132 * ---------------------------------------------------------------------------
3133 * CardEnableInt
3134 *
3135 * Enable the outgoing SDIO interrupt from UniFi to the host.
3136 *
3137 * Arguments:
3138 * card Pointer to Card object
3139 *
3140 * Returns:
3141 * CSR_RESULT_SUCCESS on success,
3142 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3143 * CSR_RESULT_FAILURE if an SDIO error occurred,
3144 * ---------------------------------------------------------------------------
3145 */
3146CsrResult CardEnableInt(card_t *card)
3147{
3148    CsrResult r;
3149    u8 int_enable;
3150
3151    r = sdio_read_f0(card, SDIO_INT_ENABLE, &int_enable);
3152    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3153    {
3154        return r;
3155    }
3156    if (r != CSR_RESULT_SUCCESS)
3157    {
3158        unifi_error(card->ospriv, "SDIO error reading SDIO_INT_ENABLE\n");
3159        return r;
3160    }
3161
3162    int_enable |= (1 << card->function) | UNIFI_SD_INT_ENABLE_IENM;
3163
3164    r = sdio_write_f0(card, SDIO_INT_ENABLE, int_enable);
3165    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3166    {
3167        return r;
3168    }
3169    if (r != CSR_RESULT_SUCCESS)
3170    {
3171        unifi_error(card->ospriv, "SDIO error writing SDIO_INT_ENABLE\n");
3172        return r;
3173    }
3174
3175    return CSR_RESULT_SUCCESS;
3176} /* CardEnableInt() */
3177
3178
3179/*
3180 * ---------------------------------------------------------------------------
3181 * CardDisableInt
3182 *
3183 * Disable the outgoing SDIO interrupt from UniFi to the host.
3184 *
3185 * Arguments:
3186 * card Pointer to Card object
3187 *
3188 * Returns:
3189 * CSR_RESULT_SUCCESS on success,
3190 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3191 * CSR_RESULT_FAILURE if an SDIO error occurred,
3192 * ---------------------------------------------------------------------------
3193 */
3194CsrResult CardDisableInt(card_t *card)
3195{
3196    CsrResult r;
3197    u8 int_enable;
3198
3199    r = sdio_read_f0(card, SDIO_INT_ENABLE, &int_enable);
3200    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3201    {
3202        return r;
3203    }
3204    if (r != CSR_RESULT_SUCCESS)
3205    {
3206        unifi_error(card->ospriv, "SDIO error reading SDIO_INT_ENABLE\n");
3207        return r;
3208    }
3209
3210    int_enable &= ~(1 << card->function);
3211
3212    r = sdio_write_f0(card, SDIO_INT_ENABLE, int_enable);
3213    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3214    {
3215        return r;
3216    }
3217    if (r != CSR_RESULT_SUCCESS)
3218    {
3219        unifi_error(card->ospriv, "SDIO error writing SDIO_INT_ENABLE\n");
3220        return r;
3221    }
3222
3223    return CSR_RESULT_SUCCESS;
3224} /* CardDisableInt() */
3225
3226
3227/*
3228 * ---------------------------------------------------------------------------
3229 * CardPendingInt
3230 *
3231 * Determine whether UniFi is currently asserting the SDIO interrupt
3232 * request.
3233 *
3234 * Arguments:
3235 * card Pointer to Card object
3236 * pintr Pointer to location to write interrupt status,
3237 * TRUE if interrupt pending,
3238 * FALSE if no interrupt pending.
3239 * Returns:
3240 * CSR_RESULT_SUCCESS interrupt status read successfully
3241 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3242 * CSR_RESULT_FAILURE if an SDIO error occurred,
3243 * ---------------------------------------------------------------------------
3244 */
3245CsrResult CardPendingInt(card_t *card, u8 *pintr)
3246{
3247    CsrResult r;
3248    u8 pending;
3249
3250    *pintr = FALSE;
3251
3252    r = sdio_read_f0(card, SDIO_INT_PENDING, &pending);
3253    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3254    {
3255        return r;
3256    }
3257    if (r != CSR_RESULT_SUCCESS)
3258    {
3259        unifi_error(card->ospriv, "SDIO error reading SDIO_INT_PENDING\n");
3260        return r;
3261    }
3262
3263    *pintr = (pending & (1 << card->function))?TRUE : FALSE;
3264
3265    return CSR_RESULT_SUCCESS;
3266} /* CardPendingInt() */
3267
3268
3269/*
3270 * ---------------------------------------------------------------------------
3271 * CardClearInt
3272 *
3273 * Clear the UniFi SDIO interrupt request.
3274 *
3275 * Arguments:
3276 * card Pointer to Card object
3277 *
3278 * Returns:
3279 * CSR_RESULT_SUCCESS if pending interrupt was cleared, or no pending interrupt.
3280 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3281 * CSR_RESULT_FAILURE if an SDIO error occurred,
3282 * ---------------------------------------------------------------------------
3283 */
3284CsrResult CardClearInt(card_t *card)
3285{
3286    CsrResult r;
3287    u8 intr;
3288
3289    if (card->chip_id > SDIO_CARD_ID_UNIFI_2)
3290    {
3291        /* CardPendingInt() sets intr, if there is a pending interrupt */
3292        r = CardPendingInt(card, &intr);
3293        if (intr == FALSE)
3294        {
3295            return r;
3296        }
3297
3298        r = sdio_write_f0(card, SDIO_CSR_HOST_INT_CLEAR, 1);
3299        if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3300        {
3301            return r;
3302        }
3303        if (r != CSR_RESULT_SUCCESS)
3304        {
3305            unifi_error(card->ospriv, "SDIO error writing SDIO_CSR_HOST_INT_CLEAR\n");
3306        }
3307    }
3308    else
3309    {
3310        r = unifi_write_direct_8_or_16(card,
3311                                       ChipHelper_SDIO_HOST_INT(card->helper) * 2,
3312                                       0);
3313        if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3314        {
3315            return r;
3316        }
3317        if (r != CSR_RESULT_SUCCESS)
3318        {
3319            unifi_error(card->ospriv, "SDIO error writing UNIFI_SDIO_HOST_INT\n");
3320        }
3321    }
3322
3323    return r;
3324} /* CardClearInt() */
3325
3326
3327/*
3328 * ---------------------------------------------------------------------------
3329 * CardIntEnabled
3330 *
3331 * Determine whether UniFi is currently asserting the SDIO interrupt
3332 * request.
3333 *
3334 * Arguments:
3335 * card Pointer to Card object
3336 * enabled Pointer to location to write interrupt enable status,
3337 * TRUE if interrupts enabled,
3338 * FALSE if interupts disabled.
3339 *
3340 * Returns:
3341 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3342 * CSR_RESULT_FAILURE if an SDIO error occurred,
3343 * ---------------------------------------------------------------------------
3344 */
3345CsrResult CardIntEnabled(card_t *card, u8 *enabled)
3346{
3347    CsrResult r;
3348    u8 int_enable;
3349
3350    r = sdio_read_f0(card, SDIO_INT_ENABLE, &int_enable);
3351    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3352    {
3353        return r;
3354    }
3355    if (r != CSR_RESULT_SUCCESS)
3356    {
3357        unifi_error(card->ospriv, "SDIO error reading SDIO_INT_ENABLE\n");
3358        return r;
3359    }
3360
3361    *enabled = (int_enable & (1 << card->function))?TRUE : FALSE;
3362
3363    return CSR_RESULT_SUCCESS;
3364} /* CardIntEnabled() */
3365
3366
3367/*
3368 * ---------------------------------------------------------------------------
3369 * CardWriteBulkData
3370 * Allocate slot in the pending bulkdata arrays and assign it to a signal's
3371 * bulkdata reference. The slot is then ready for UniFi's bulkdata commands
3372 * to transfer the data to/from the host.
3373 *
3374 * Arguments:
3375 * card Pointer to Card object
3376 * csptr Pending signal pointer, including bulkdata ref
3377 * queue Traffic queue that this signal is using
3378 *
3379 * Returns:
3380 * CSR_RESULT_SUCCESS if a free slot was assigned
3381 * CSR_RESULT_FAILURE if no slot was available
3382 * ---------------------------------------------------------------------------
3383 */
3384CsrResult CardWriteBulkData(card_t *card, card_signal_t *csptr, unifi_TrafficQueue queue)
3385{
3386    u16 i, slots[UNIFI_MAX_DATA_REFERENCES], j = 0;
3387    u8 *packed_sigptr, num_slots_required = 0;
3388    bulk_data_desc_t *bulkdata = csptr->bulkdata;
3389    s16 h, nslots;
3390
3391    func_enter();
3392
3393    /* Count the number of slots required */
3394    for (i = 0; i < UNIFI_MAX_DATA_REFERENCES; i++)
3395    {
3396        if (bulkdata[i].data_length != 0)
3397        {
3398            num_slots_required++;
3399        }
3400    }
3401
3402    /* Get the slot numbers */
3403    if (num_slots_required != 0)
3404    {
3405        /* Last 2 slots for MLME */
3406        if (queue == UNIFI_TRAFFIC_Q_MLME)
3407        {
3408            h = card->config_data.num_fromhost_data_slots - UNIFI_RESERVED_COMMAND_SLOTS;
3409            for (i = 0; i < card->config_data.num_fromhost_data_slots; i++)
3410            {
3411                if (card->from_host_data[h].bd.data_length == 0)
3412                {
3413                    /* Free data slot, claim it */
3414                    slots[j++] = h;
3415                    if (j == num_slots_required)
3416                    {
3417                        break;
3418                    }
3419                }
3420
3421                if (++h >= card->config_data.num_fromhost_data_slots)
3422                {
3423                    h = 0;
3424                }
3425            }
3426        }
3427        else
3428        {
3429            if (card->dynamic_slot_data.from_host_used_slots[queue]
3430                < card->dynamic_slot_data.from_host_max_slots[queue])
3431            {
3432                /* Data commands get a free slot only after a few checks */
3433                nslots = card->config_data.num_fromhost_data_slots - UNIFI_RESERVED_COMMAND_SLOTS;
3434
3435                h = card->from_host_data_head;
3436
3437                for (i = 0; i < nslots; i++)
3438                {
3439                    if (card->from_host_data[h].bd.data_length == 0)
3440                    {
3441                        /* Free data slot, claim it */
3442                        slots[j++] = h;
3443                        if (j == num_slots_required)
3444                        {
3445                            break;
3446                        }
3447                    }
3448
3449                    if (++h >= nslots)
3450                    {
3451                        h = 0;
3452                    }
3453                }
3454                card->from_host_data_head = h;
3455            }
3456        }
3457
3458        /* Required number of slots are not available, bail out */
3459        if (j != num_slots_required)
3460        {
3461            unifi_trace(card->ospriv, UDBG5, "CardWriteBulkData: didn't find free slot/s\n");
3462
3463            /* If we haven't already reached the stable state we can ask for reservation */
3464            if ((queue != UNIFI_TRAFFIC_Q_MLME) && (card->dynamic_slot_data.queue_stable[queue] == FALSE))
3465            {
3466                CardCheckDynamicReservation(card, queue);
3467            }
3468
3469            for (i = 0; i < card->config_data.num_fromhost_data_slots; i++)
3470            {
3471                unifi_trace(card->ospriv, UDBG5, "fh data slot %d: %d\n", i, card->from_host_data[i].bd.data_length);
3472            }
3473            func_exit();
3474            return CSR_RESULT_FAILURE;
3475        }
3476    }
3477
3478    packed_sigptr = csptr->sigbuf;
3479
3480    /* Fill in the slots with data */
3481    j = 0;
3482    for (i = 0; i < UNIFI_MAX_DATA_REFERENCES; i++)
3483    {
3484        if (bulkdata[i].data_length == 0)
3485        {
3486            /* Zero-out the DATAREF in the signal */
3487            SET_PACKED_DATAREF_SLOT(packed_sigptr, i, 0);
3488            SET_PACKED_DATAREF_LEN(packed_sigptr, i, 0);
3489        }
3490        else
3491        {
3492            /*
3493             * Fill in the slot number in the SIGNAL structure but
3494             * preserve the offset already in there
3495             */
3496            SET_PACKED_DATAREF_SLOT(packed_sigptr, i, slots[j] | (((u16)packed_sigptr[SIZEOF_SIGNAL_HEADER + (i * SIZEOF_DATAREF) + 1]) << 8));
3497            SET_PACKED_DATAREF_LEN(packed_sigptr, i, bulkdata[i].data_length);
3498
3499            /* Do not copy the data, just store the information to them */
3500            card->from_host_data[slots[j]].bd.os_data_ptr = bulkdata[i].os_data_ptr;
3501            card->from_host_data[slots[j]].bd.os_net_buf_ptr = bulkdata[i].os_net_buf_ptr;
3502            card->from_host_data[slots[j]].bd.data_length = bulkdata[i].data_length;
3503            card->from_host_data[slots[j]].bd.net_buf_length = bulkdata[i].net_buf_length;
3504            card->from_host_data[slots[j]].queue = queue;
3505
3506            unifi_trace(card->ospriv, UDBG4, "CardWriteBulkData sig=0x%x, fh slot %d = %p\n",
3507                        GET_SIGNAL_ID(packed_sigptr), i, bulkdata[i].os_data_ptr);
3508
3509            /* Sanity-check that the bulk data desc being assigned to the slot
3510             * actually has a payload.
3511             */
3512            if (!bulkdata[i].os_data_ptr)
3513            {
3514                unifi_error(card->ospriv, "Assign null os_data_ptr (len=%d) fh slot %d, i=%d, q=%d, sig=0x%x",
3515                            bulkdata[i].data_length, slots[j], i, queue, GET_SIGNAL_ID(packed_sigptr));
3516            }
3517
3518            j++;
3519            if (queue < UNIFI_NO_OF_TX_QS)
3520            {
3521                card->dynamic_slot_data.from_host_used_slots[queue]++;
3522            }
3523        }
3524    }
3525
3526    func_exit();
3527
3528    return CSR_RESULT_SUCCESS;
3529} /* CardWriteBulkData() */
3530
3531
3532/*
3533 * ---------------------------------------------------------------------------
3534 * card_find_data_slot
3535 *
3536 * Dereference references to bulk data slots into pointers to real data.
3537 *
3538 * Arguments:
3539 * card Pointer to the card struct.
3540 * slot Slot number from a signal structure
3541 *
3542 * Returns:
3543 * Pointer to entry in bulk_data_slot array.
3544 * ---------------------------------------------------------------------------
3545 */
3546bulk_data_desc_t* card_find_data_slot(card_t *card, s16 slot)
3547{
3548    s16 sn;
3549    bulk_data_desc_t *bd;
3550
3551    sn = slot & 0x7FFF;
3552
3553    /* ?? check sanity of slot number ?? */
3554
3555    if (slot & SLOT_DIR_TO_HOST)
3556    {
3557        bd = &card->to_host_data[sn];
3558    }
3559    else
3560    {
3561        bd = &card->from_host_data[sn].bd;
3562    }
3563
3564    return bd;
3565} /* card_find_data_slot() */
3566
3567
3568/*
3569 * ---------------------------------------------------------------------------
3570 * firmware_present_in_flash
3571 *
3572 * Probe for external Flash that looks like it might contain firmware.
3573 *
3574 * If Flash is not present, reads always return 0x0008.
3575 * If Flash is present, but empty, reads return 0xFFFF.
3576 * Anything else is considered to be firmware.
3577 *
3578 * Arguments:
3579 * card Pointer to card struct
3580 *
3581 * Returns:
3582 * CSR_RESULT_SUCCESS firmware is present in ROM or flash
3583 * CSR_WIFI_HIP_RESULT_NOT_FOUND firmware is not present in ROM or flash
3584 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3585 * CSR_RESULT_FAILURE if an SDIO error occurred
3586 * ---------------------------------------------------------------------------
3587 */
3588static CsrResult firmware_present_in_flash(card_t *card)
3589{
3590    CsrResult r;
3591    u16 m1, m5;
3592
3593    if (ChipHelper_HasRom(card->helper))
3594    {
3595        return CSR_RESULT_SUCCESS;
3596    }
3597    if (!ChipHelper_HasFlash(card->helper))
3598    {
3599        return CSR_WIFI_HIP_RESULT_NOT_FOUND;
3600    }
3601
3602    /*
3603     * Examine the Flash locations that are the power-on default reset
3604     * vectors of the XAP processors.
3605     * These are words 1 and 5 in Flash.
3606     */
3607    r = unifi_card_read16(card, UNIFI_MAKE_GP(EXT_FLASH, 2), &m1);
3608    if (r != CSR_RESULT_SUCCESS)
3609    {
3610        return r;
3611    }
3612
3613    r = unifi_card_read16(card, UNIFI_MAKE_GP(EXT_FLASH, 10), &m5);
3614    if (r != CSR_RESULT_SUCCESS)
3615    {
3616        return r;
3617    }
3618
3619    /* Check for uninitialised/missing flash */
3620    if ((m1 == 0x0008) || (m1 == 0xFFFF) ||
3621        (m1 == 0x0004) || (m5 == 0x0004) ||
3622        (m5 == 0x0008) || (m5 == 0xFFFF))
3623    {
3624        return CSR_WIFI_HIP_RESULT_NOT_FOUND;
3625    }
3626
3627    return CSR_RESULT_SUCCESS;
3628} /* firmware_present_in_flash() */
3629
3630
3631/*
3632 * ---------------------------------------------------------------------------
3633 * bootstrap_chip_hw
3634 *
3635 * Perform chip specific magic to "Get It Working" TM. This will
3636 * increase speed of PLLs in analogue and maybe enable some
3637 * on-chip regulators.
3638 *
3639 * Arguments:
3640 * card Pointer to card struct
3641 *
3642 * Returns:
3643 * None.
3644 * ---------------------------------------------------------------------------
3645 */
3646static void bootstrap_chip_hw(card_t *card)
3647{
3648    const struct chip_helper_init_values *vals;
3649    u32 i, len;
3650    void *sdio = card->sdio_if;
3651    CsrResult csrResult;
3652
3653    len = ChipHelper_ClockStartupSequence(card->helper, &vals);
3654    if (len != 0)
3655    {
3656        for (i = 0; i < len; i++)
3657        {
3658            csrResult = CsrSdioWrite16(sdio, vals[i].addr * 2, vals[i].value);
3659            if (csrResult != CSR_RESULT_SUCCESS)
3660            {
3661                unifi_warning(card->ospriv, "Failed to write bootstrap value %d\n", i);
3662                /* Might not be fatal */
3663            }
3664
3665            CsrThreadSleep(1);
3666        }
3667    }
3668} /* bootstrap_chip_hw() */
3669
3670
3671/*
3672 * ---------------------------------------------------------------------------
3673 * unifi_card_stop_processor
3674 *
3675 * Stop the UniFi XAP processors.
3676 *
3677 * Arguments:
3678 * card Pointer to card struct
3679 * which One of UNIFI_PROC_MAC, UNIFI_PROC_PHY, UNIFI_PROC_BOTH
3680 *
3681 * Returns:
3682 * CSR_RESULT_SUCCESS if successful, or CSR error code
3683 * ---------------------------------------------------------------------------
3684 */
3685CsrResult unifi_card_stop_processor(card_t *card, enum unifi_dbg_processors_select which)
3686{
3687    CsrResult r = CSR_RESULT_SUCCESS;
3688    u8 status;
3689    s16 retry = 100;
3690
3691    while (retry--)
3692    {
3693        /* Select both XAPs */
3694        r = unifi_set_proc_select(card, which);
3695        if (r != CSR_RESULT_SUCCESS)
3696        {
3697            break;
3698        }
3699
3700        /* Stop processors */
3701        r = unifi_write_direct16(card, ChipHelper_DBG_EMU_CMD(card->helper) * 2, 2);
3702        if (r != CSR_RESULT_SUCCESS)
3703        {
3704            break;
3705        }
3706
3707        /* Read status */
3708        r = unifi_read_direct_8_or_16(card,
3709                                      ChipHelper_DBG_HOST_STOP_STATUS(card->helper) * 2,
3710                                      &status);
3711        if (r != CSR_RESULT_SUCCESS)
3712        {
3713            break;
3714        }
3715
3716        if ((status & 1) == 1)
3717        {
3718            /* Success! */
3719            return CSR_RESULT_SUCCESS;
3720        }
3721
3722        /* Processors didn't stop, try again */
3723    }
3724
3725    if (r != CSR_RESULT_SUCCESS)
3726    {
3727        /* An SDIO error occurred */
3728        unifi_error(card->ospriv, "Failed to stop processors: SDIO error\n");
3729    }
3730    else
3731    {
3732        /* If we reach here, we didn't the status in time. */
3733        unifi_error(card->ospriv, "Failed to stop processors: timeout waiting for stopped status\n");
3734        r = CSR_RESULT_FAILURE;
3735    }
3736
3737    return r;
3738} /* unifi_card_stop_processor() */
3739
3740
3741/*
3742 * ---------------------------------------------------------------------------
3743 * card_start_processor
3744 *
3745 * Start the UniFi XAP processors.
3746 *
3747 * Arguments:
3748 * card Pointer to card struct
3749 * which One of UNIFI_PROC_MAC, UNIFI_PROC_PHY, UNIFI_PROC_BOTH
3750 *
3751 * Returns:
3752 * CSR_RESULT_SUCCESS or CSR error code
3753 * ---------------------------------------------------------------------------
3754 */
3755CsrResult card_start_processor(card_t *card, enum unifi_dbg_processors_select which)
3756{
3757    CsrResult r;
3758
3759    /* Select both XAPs */
3760    r = unifi_set_proc_select(card, which);
3761    if (r != CSR_RESULT_SUCCESS)
3762    {
3763        unifi_error(card->ospriv, "unifi_set_proc_select failed: %d.\n", r);
3764        return r;
3765    }
3766
3767
3768    r = unifi_write_direct_8_or_16(card,
3769                                   ChipHelper_DBG_EMU_CMD(card->helper) * 2, 8);
3770    if (r != CSR_RESULT_SUCCESS)
3771    {
3772        return r;
3773    }
3774
3775    r = unifi_write_direct_8_or_16(card,
3776                                   ChipHelper_DBG_EMU_CMD(card->helper) * 2, 0);
3777    if (r != CSR_RESULT_SUCCESS)
3778    {
3779        return r;
3780    }
3781
3782    return CSR_RESULT_SUCCESS;
3783} /* card_start_processor() */
3784
3785
3786/*
3787 * ---------------------------------------------------------------------------
3788 * unifi_set_interrupt_mode
3789 *
3790 * Configure the interrupt processing mode used by the HIP
3791 *
3792 * Arguments:
3793 * card Pointer to card struct
3794 * mode Interrupt mode to apply
3795 *
3796 * Returns:
3797 * None
3798 * ---------------------------------------------------------------------------
3799 */
3800void unifi_set_interrupt_mode(card_t *card, u32 mode)
3801{
3802    if (mode == CSR_WIFI_INTMODE_RUN_BH_ONCE)
3803    {
3804        unifi_info(card->ospriv, "Scheduled interrupt mode");
3805    }
3806    card->intmode = mode;
3807} /* unifi_set_interrupt_mode() */
3808
3809
3810/*
3811 * ---------------------------------------------------------------------------
3812 * unifi_start_processors
3813 *
3814 * Start all UniFi XAP processors.
3815 *
3816 * Arguments:
3817 * card Pointer to card struct
3818 *
3819 * Returns:
3820 * CSR_RESULT_SUCCESS on success, CSR error code on error
3821 * ---------------------------------------------------------------------------
3822 */
3823CsrResult unifi_start_processors(card_t *card)
3824{
3825    return card_start_processor(card, UNIFI_PROC_BOTH);
3826} /* unifi_start_processors() */
3827
3828
3829/*
3830 * ---------------------------------------------------------------------------
3831 * unifi_request_max_sdio_clock
3832 *
3833 * Requests that the maximum SDIO clock rate is set at the next suitable
3834 * opportunity (e.g. when the BH next runs, so as not to interfere with
3835 * any current operation).
3836 *
3837 * Arguments:
3838 * card Pointer to card struct
3839 *
3840 * Returns:
3841 * None
3842 * ---------------------------------------------------------------------------
3843 */
3844void unifi_request_max_sdio_clock(card_t *card)
3845{
3846    card->request_max_clock = 1;
3847} /* unifi_request_max_sdio_clock() */
3848
3849
3850/*
3851 * ---------------------------------------------------------------------------
3852 * unifi_set_host_state
3853 *
3854 * Set the host deep-sleep state.
3855 *
3856 * If transitioning to TORPID, the SDIO driver will be notified
3857 * that the SD bus will be unused (idle) and conversely, when
3858 * transitioning from TORPID that the bus will be used (active).
3859 *
3860 * Arguments:
3861 * card Pointer to card struct
3862 * state New deep-sleep state.
3863 *
3864 * Returns:
3865 * CSR_RESULT_SUCCESS on success
3866 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3867 * CSR_RESULT_FAILURE if an SDIO error occurred
3868 *
3869 * Notes:
3870 * We need to reduce the SDIO clock speed before trying to wake up the
3871 * chip. Actually, in the implementation below we reduce the clock speed
3872 * not just before we try to wake up the chip, but when we put the chip to
3873 * deep sleep. This means that if the f/w wakes up on its' own, we waste
3874 * a reduce/increace cycle. However, trying to eliminate this overhead is
3875 * proved difficult, as the current state machine in the HIP lib does at
3876 * least a CMD52 to disable the interrupts before we configure the host
3877 * state.
3878 * ---------------------------------------------------------------------------
3879 */
3880CsrResult unifi_set_host_state(card_t *card, enum unifi_host_state state)
3881{
3882    CsrResult r = CSR_RESULT_SUCCESS;
3883    CsrResult csrResult;
3884    static const char *const states[] = {
3885        "AWAKE", "DROWSY", "TORPID"
3886    };
3887    static const u8 state_csr_host_wakeup[] = {
3888        1, 3, 0
3889    };
3890    static const u8 state_io_abort[] = {
3891        0, 2, 3
3892    };
3893
3894    unifi_trace(card->ospriv, UDBG4, "State %s to %s\n",
3895                states[card->host_state], states[state]);
3896
3897    if (card->host_state == UNIFI_HOST_STATE_TORPID)
3898    {
3899        CsrSdioFunctionActive(card->sdio_if);
3900    }
3901
3902    /* Write the new state to UniFi. */
3903    if (card->chip_id > SDIO_CARD_ID_UNIFI_2)
3904    {
3905        r = sdio_write_f0(card, SDIO_CSR_HOST_WAKEUP,
3906                          (u8)((card->function << 4) | state_csr_host_wakeup[state]));
3907    }
3908    else
3909    {
3910        r = sdio_write_f0(card, SDIO_IO_ABORT, state_io_abort[state]);
3911    }
3912
3913    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3914    {
3915        return r;
3916    }
3917    if (r != CSR_RESULT_SUCCESS)
3918    {
3919        unifi_error(card->ospriv, "Failed to write UniFi deep sleep state\n");
3920    }
3921    else
3922    {
3923        /*
3924         * If the chip was in state TORPID then we can now increase
3925         * the maximum bus clock speed.
3926         */
3927        if (card->host_state == UNIFI_HOST_STATE_TORPID)
3928        {
3929            csrResult = CsrSdioMaxBusClockFrequencySet(card->sdio_if,
3930                                                       UNIFI_SDIO_CLOCK_MAX_HZ);
3931            r = ConvertCsrSdioToCsrHipResult(card, csrResult);
3932            /* Non-fatal error */
3933            if (r != CSR_RESULT_SUCCESS && r != CSR_WIFI_HIP_RESULT_NO_DEVICE)
3934            {
3935                unifi_warning(card->ospriv,
3936                              "Failed to increase the SDIO clock speed\n");
3937            }
3938            else
3939            {
3940                card->sdio_clock_speed = UNIFI_SDIO_CLOCK_MAX_HZ;
3941            }
3942        }
3943
3944        /*
3945         * Cache the current state in the card structure to avoid
3946         * unnecessary SDIO reads.
3947         */
3948        card->host_state = state;
3949
3950        if (state == UNIFI_HOST_STATE_TORPID)
3951        {
3952            /*
3953             * If the chip is now in state TORPID then we must now decrease
3954             * the maximum bus clock speed.
3955             */
3956            csrResult = CsrSdioMaxBusClockFrequencySet(card->sdio_if,
3957                                                       UNIFI_SDIO_CLOCK_SAFE_HZ);
3958            r = ConvertCsrSdioToCsrHipResult(card, csrResult);
3959            if (r != CSR_RESULT_SUCCESS && r != CSR_WIFI_HIP_RESULT_NO_DEVICE)
3960            {
3961                unifi_warning(card->ospriv,
3962                              "Failed to decrease the SDIO clock speed\n");
3963            }
3964            else
3965            {
3966                card->sdio_clock_speed = UNIFI_SDIO_CLOCK_SAFE_HZ;
3967            }
3968            CsrSdioFunctionIdle(card->sdio_if);
3969        }
3970    }
3971
3972    return r;
3973} /* unifi_set_host_state() */
3974
3975
3976/*
3977 * ---------------------------------------------------------------------------
3978 * unifi_card_info
3979 *
3980 * Update the card information data structure
3981 *
3982 * Arguments:
3983 * card Pointer to card struct
3984 * card_info Pointer to info structure to update
3985 *
3986 * Returns:
3987 * None
3988 * ---------------------------------------------------------------------------
3989 */
3990void unifi_card_info(card_t *card, card_info_t *card_info)
3991{
3992    card_info->chip_id = card->chip_id;
3993    card_info->chip_version = card->chip_version;
3994    card_info->fw_build = card->build_id;
3995    card_info->fw_hip_version = card->config_data.version;
3996    card_info->sdio_block_size = card->sdio_io_block_size;
3997} /* unifi_card_info() */
3998
3999
4000/*
4001 * ---------------------------------------------------------------------------
4002 * unifi_check_io_status
4003 *
4004 * Check UniFi for spontaneous reset and pending interrupt.
4005 *
4006 * Arguments:
4007 * card Pointer to card struct
4008 * status Pointer to location to write chip status:
4009 * 0 if UniFi is running, and no interrupt pending
4010 * 1 if UniFi has spontaneously reset
4011 * 2 if there is a pending interrupt
4012 * Returns:
4013 * CSR_RESULT_SUCCESS if OK, or CSR error
4014 * ---------------------------------------------------------------------------
4015 */
4016CsrResult unifi_check_io_status(card_t *card, s32 *status)
4017{
4018    u8 io_en;
4019    CsrResult r;
4020    u8 pending;
4021
4022    *status = 0;
4023
4024    r = sdio_read_f0(card, SDIO_IO_ENABLE, &io_en);
4025    if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
4026    {
4027        return r;
4028    }
4029    if (r != CSR_RESULT_SUCCESS)
4030    {
4031        unifi_error(card->ospriv, "Failed to read SDIO_IO_ENABLE to check for spontaneous reset\n");
4032        return r;
4033    }
4034
4035    if ((io_en & (1 << card->function)) == 0)
4036    {
4037        s32 fw_count;
4038        *status = 1;
4039        unifi_error(card->ospriv, "UniFi has spontaneously reset.\n");
4040
4041        /*
4042         * These reads are very likely to fail. We want to know if the function is really
4043         * disabled or the SDIO driver just returns rubbish.
4044         */
4045        fw_count = unifi_read_shared_count(card, card->sdio_ctrl_addr + 4);
4046        if (fw_count < 0)
4047        {
4048            unifi_error(card->ospriv, "Failed to read to-host sig written count\n");
4049        }
4050        else
4051        {
4052            unifi_error(card->ospriv, "thsw: %u (driver thinks is %u)\n",
4053                        fw_count, card->to_host_signals_w);
4054        }
4055        fw_count = unifi_read_shared_count(card, card->sdio_ctrl_addr + 2);
4056        if (fw_count < 0)
4057        {
4058            unifi_error(card->ospriv, "Failed to read from-host sig read count\n");
4059        }
4060        else
4061        {
4062            unifi_error(card->ospriv, "fhsr: %u (driver thinks is %u)\n",
4063                        fw_count, card->from_host_signals_r);
4064        }
4065
4066        return r;
4067    }
4068
4069    unifi_info(card->ospriv, "UniFi function %d is enabled.\n", card->function);
4070
4071    /* See if we missed an SDIO interrupt */
4072    r = CardPendingInt(card, &pending);
4073    if (pending)
4074    {
4075        unifi_error(card->ospriv, "There is an unhandled pending interrupt.\n");
4076        *status = 2;
4077        return r;
4078    }
4079
4080    return r;
4081} /* unifi_check_io_status() */
4082
4083
4084void unifi_get_hip_qos_info(card_t *card, unifi_HipQosInfo *hipqosinfo)
4085{
4086    s32 count_fhr;
4087    s16 t;
4088    u32 occupied_fh;
4089
4090    q_t *sigq;
4091    u16 nslots, i;
4092
4093    memset(hipqosinfo, 0, sizeof(unifi_HipQosInfo));
4094
4095    nslots = card->config_data.num_fromhost_data_slots;
4096
4097    for (i = 0; i < nslots; i++)
4098    {
4099        if (card->from_host_data[i].bd.data_length == 0)
4100        {
4101            hipqosinfo->free_fh_bulkdata_slots++;
4102        }
4103    }
4104
4105    for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
4106    {
4107        sigq = &card->fh_traffic_queue[i];
4108        t = sigq->q_wr_ptr - sigq->q_rd_ptr;
4109        if (t < 0)
4110        {
4111            t += sigq->q_length;
4112        }
4113        hipqosinfo->free_fh_sig_queue_slots[i] = (sigq->q_length - t) - 1;
4114    }
4115
4116    count_fhr = unifi_read_shared_count(card, card->sdio_ctrl_addr + 2);
4117    if (count_fhr < 0)
4118    {
4119        unifi_error(card->ospriv, "Failed to read from-host sig read count - %d\n", count_fhr);
4120        hipqosinfo->free_fh_fw_slots = 0xfa;
4121        return;
4122    }
4123
4124    occupied_fh = (card->from_host_signals_w - count_fhr) % 128;
4125
4126    hipqosinfo->free_fh_fw_slots = (u16)(card->config_data.num_fromhost_sig_frags - occupied_fh);
4127}
4128
4129
4130
4131CsrResult ConvertCsrSdioToCsrHipResult(card_t *card, CsrResult csrResult)
4132{
4133    CsrResult r = CSR_RESULT_FAILURE;
4134
4135    switch (csrResult)
4136    {
4137        case CSR_RESULT_SUCCESS:
4138            r = CSR_RESULT_SUCCESS;
4139            break;
4140        /* Timeout errors */
4141        case CSR_SDIO_RESULT_TIMEOUT:
4142        /* Integrity errors */
4143        case CSR_SDIO_RESULT_CRC_ERROR:
4144            r = CSR_RESULT_FAILURE;
4145            break;
4146        case CSR_SDIO_RESULT_NO_DEVICE:
4147            r = CSR_WIFI_HIP_RESULT_NO_DEVICE;
4148            break;
4149        case CSR_SDIO_RESULT_INVALID_VALUE:
4150            r = CSR_WIFI_HIP_RESULT_INVALID_VALUE;
4151            break;
4152        case CSR_RESULT_FAILURE:
4153            r = CSR_RESULT_FAILURE;
4154            break;
4155        default:
4156            unifi_warning(card->ospriv, "Unrecognised csrResult error code: %d\n", csrResult);
4157            break;
4158    }
4159
4160    return r;
4161} /* ConvertCsrSdioToCsrHipResult() */
4162
4163
4164

Archive Download this file



interactive