1 /*****************************************************************************
3 (c) Cambridge Silicon Radio Limited 2012
4 All rights reserved and confidential information of CSR
6 Refer to LICENSE.txt included with this source for details
9 *****************************************************************************/
12 * ---------------------------------------------------------------------------
13 * FILE: csr_wifi_hip_card_sdio.c
15 * PURPOSE: Implementation of the Card API for SDIO.
18 * CardInit() is called from the SDIO probe callback when a card is
19 * inserted. This performs the basic SDIO initialisation, enabling i/o
22 * ---------------------------------------------------------------------------
24 #include "csr_wifi_hip_unifi.h"
25 #include "csr_wifi_hip_conversions.h"
26 #include "csr_wifi_hip_unifiversion.h"
27 #include "csr_wifi_hip_card.h"
28 #include "csr_wifi_hip_card_sdio.h"
29 #include "csr_wifi_hip_chiphelper.h"
32 /* Time to wait between attempts to read MAILBOX0 */
33 #define MAILBOX1_TIMEOUT 10 /* in millisecs */
34 #define MAILBOX1_ATTEMPTS 200 /* 2 seconds */
36 #define MAILBOX2_TIMEOUT 5 /* in millisecs */
37 #define MAILBOX2_ATTEMPTS 10 /* 50ms */
39 #define RESET_SETTLE_DELAY 25 /* in millisecs */
41 static CsrResult card_init_slots(card_t *card);
42 static CsrResult card_hw_init(card_t *card);
43 static CsrResult firmware_present_in_flash(card_t *card);
44 static void bootstrap_chip_hw(card_t *card);
45 static CsrResult unifi_reset_hardware(card_t *card);
46 static CsrResult unifi_hip_init(card_t *card);
47 static CsrResult card_access_panic(card_t *card);
48 static CsrResult unifi_read_chip_version(card_t *card);
51 * ---------------------------------------------------------------------------
54 * Allocate and initialise the card context structure.
57 * sdio Pointer to SDIO context pointer to pass to low
58 * level i/o functions.
59 * ospriv Pointer to O/S private struct to pass when calling
60 * callbacks to the higher level system.
63 * Pointer to card struct, which represents the driver context or
64 * NULL if the allocation failed.
65 * ---------------------------------------------------------------------------
67 card_t* unifi_alloc_card(CsrSdioFunction *sdio, void *ospriv)
75 card = (card_t *)CsrMemAlloc(sizeof(card_t));
80 CsrMemSet(card, 0, sizeof(card_t));
84 card->ospriv = ospriv;
86 card->unifi_interrupt_seq = 1;
88 /* Make these invalid. */
89 card->proc_select = (CsrUint32)(-1);
90 card->dmem_page = (CsrUint32)(-1);
91 card->pmem_page = (CsrUint32)(-1);
93 card->bh_reason_host = 0;
94 card->bh_reason_unifi = 0;
96 for (i = 0; i < sizeof(card->tx_q_paused_flag) / sizeof(card->tx_q_paused_flag[0]); i++)
98 card->tx_q_paused_flag[i] = 0;
100 card->memory_resources_allocated = 0;
102 card->low_power_mode = UNIFI_LOW_POWER_DISABLED;
103 card->periodic_wake_mode = UNIFI_PERIODIC_WAKE_HOST_DISABLED;
105 card->host_state = UNIFI_HOST_STATE_AWAKE;
106 card->intmode = CSR_WIFI_INTMODE_DEFAULT;
109 * Memory resources for buffers are allocated when the chip is initialised
110 * because we need configuration information from the firmware.
114 * Initialise wait queues and lists
116 card->fh_command_queue.q_body = card->fh_command_q_body;
117 card->fh_command_queue.q_length = UNIFI_SOFT_COMMAND_Q_LENGTH;
119 for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
121 card->fh_traffic_queue[i].q_body = card->fh_traffic_q_body[i];
122 card->fh_traffic_queue[i].q_length = UNIFI_SOFT_TRAFFIC_Q_LENGTH;
126 /* Initialise mini-coredump pointers in case no coredump buffers
127 * are requested by the OS layer.
129 card->request_coredump_on_reset = 0;
130 card->dump_next_write = NULL;
131 card->dump_cur_read = NULL;
132 card->dump_buf = NULL;
135 /* Determine offset of LSB in pointer for later alignment sanity check.
136 * Synergy integer types have specific widths, which cause compiler
137 * warnings when casting pointer types, e.g. on 64-bit systems.
140 CsrUint32 val = 0x01234567;
142 if (*((CsrUint8 *)&val) == 0x01)
144 card->lsb = sizeof(void *) - 1; /* BE */
148 card->lsb = 0; /* LE */
154 } /* unifi_alloc_card() */
158 * ---------------------------------------------------------------------------
161 * Reset the hardware and perform HIP initialization
164 * card Pointer to card struct
168 * CSR_RESULT_SUCCESS if successful
169 * ---------------------------------------------------------------------------
171 CsrResult unifi_init_card(card_t *card, CsrInt32 led_mask)
179 func_exit_r(CSR_WIFI_HIP_RESULT_INVALID_VALUE);
180 return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
183 r = unifi_init(card);
184 if (r != CSR_RESULT_SUCCESS)
190 r = unifi_hip_init(card);
191 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
196 if (r != CSR_RESULT_SUCCESS)
198 unifi_error(card->ospriv, "Failed to start host protocol.\n");
204 return CSR_RESULT_SUCCESS;
209 * ---------------------------------------------------------------------------
215 * card Pointer to card struct
219 * CSR_RESULT_SUCCESS if successful
220 * ---------------------------------------------------------------------------
222 CsrResult unifi_init(card_t *card)
231 func_exit_r(CSR_WIFI_HIP_RESULT_INVALID_VALUE);
232 return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
236 * Disable the SDIO interrupts while initialising UniFi.
237 * Re-enable them when f/w is running.
239 csrResult = CsrSdioInterruptDisable(card->sdio_if);
240 if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
242 return CSR_WIFI_HIP_RESULT_NO_DEVICE;
246 * UniFi's PLL may start with a slow clock (~ 1 MHz) so initially
247 * set the SDIO bus clock to a similar value or SDIO accesses may
250 csrResult = CsrSdioMaxBusClockFrequencySet(card->sdio_if, UNIFI_SDIO_CLOCK_SAFE_HZ);
251 if (csrResult != CSR_RESULT_SUCCESS)
253 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
257 card->sdio_clock_speed = UNIFI_SDIO_CLOCK_SAFE_HZ;
260 * Reset UniFi. Note, this only resets the WLAN function part of the chip,
261 * the SDIO interface is not reset.
263 unifi_trace(card->ospriv, UDBG1, "Resetting UniFi\n");
264 r = unifi_reset_hardware(card);
265 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
269 if (r != CSR_RESULT_SUCCESS)
271 unifi_error(card->ospriv, "Failed to reset UniFi\n");
276 /* Reset the power save mode, to be active until the MLME-reset is complete */
277 r = unifi_configure_low_power_mode(card,
278 UNIFI_LOW_POWER_DISABLED, UNIFI_PERIODIC_WAKE_HOST_DISABLED);
279 if (r != CSR_RESULT_SUCCESS)
281 unifi_error(card->ospriv, "Failed to set power save mode\n");
287 * Set initial value of page registers.
288 * The page registers will be maintained by unifi_read...() and
291 card->proc_select = (CsrUint32)(-1);
292 card->dmem_page = (CsrUint32)(-1);
293 card->pmem_page = (CsrUint32)(-1);
294 r = unifi_write_direct16(card, ChipHelper_HOST_WINDOW3_PAGE(card->helper) * 2, 0);
295 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
299 if (r != CSR_RESULT_SUCCESS)
301 unifi_error(card->ospriv, "Failed to write SHARED_DMEM_PAGE\n");
305 r = unifi_write_direct16(card, ChipHelper_HOST_WINDOW2_PAGE(card->helper) * 2, 0);
306 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
310 if (r != CSR_RESULT_SUCCESS)
312 unifi_error(card->ospriv, "Failed to write PROG_MEM2_PAGE\n");
318 * If the driver has reset UniFi due to previous SDIO failure, this may
319 * have been due to a chip watchdog reset. In this case, the driver may
320 * have requested a mini-coredump which needs to be captured now the
321 * SDIO interface is alive.
323 (void)unifi_coredump_handle_request(card);
326 * Probe to see if the UniFi has ROM/flash to boot from. CSR6xxx should do.
328 r = firmware_present_in_flash(card);
329 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
333 if (r == CSR_WIFI_HIP_RESULT_NOT_FOUND)
335 unifi_error(card->ospriv, "No firmware found\n");
337 else if (r != CSR_RESULT_SUCCESS)
339 unifi_error(card->ospriv, "Probe for Flash failed\n");
348 * ---------------------------------------------------------------------------
354 * card Pointer to card struct
355 * led_mask Loader LED mask
358 * CSR_RESULT_SUCCESS on success
359 * CsrResult error code on failure.
360 * ---------------------------------------------------------------------------
362 CsrResult unifi_download(card_t *card, CsrInt32 led_mask)
371 func_exit_r(CSR_WIFI_HIP_RESULT_INVALID_VALUE);
372 return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
375 /* Set the loader led mask */
376 card->loader_led_mask = led_mask;
378 /* Get the firmware file information */
379 unifi_trace(card->ospriv, UDBG1, "downloading firmware...\n");
381 dlpriv = unifi_dl_fw_read_start(card, UNIFI_FW_STA);
384 func_exit_r(CSR_WIFI_HIP_RESULT_NOT_FOUND);
385 return CSR_WIFI_HIP_RESULT_NOT_FOUND;
388 /* Download the firmware. */
389 r = unifi_dl_firmware(card, dlpriv);
390 if (r != CSR_RESULT_SUCCESS)
392 unifi_error(card->ospriv, "Failed to download firmware\n");
397 /* Free the firmware file information. */
398 unifi_fw_read_stop(card->ospriv, dlpriv);
402 return CSR_RESULT_SUCCESS;
403 } /* unifi_download() */
407 * ---------------------------------------------------------------------------
410 * This function performs the f/w initialisation sequence as described
411 * in the Unifi Host Interface Protocol Specification.
412 * It allocates memory for host-side slot data and signal queues.
415 * card Pointer to card struct
418 * CSR_RESULT_SUCCESS on success or else a CSR error code
421 * The firmware must have been downloaded.
422 * ---------------------------------------------------------------------------
424 static CsrResult unifi_hip_init(card_t *card)
431 r = card_hw_init(card);
432 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
436 if (r != CSR_RESULT_SUCCESS)
438 unifi_error(card->ospriv, "Failed to establish communication with UniFi\n");
442 #ifdef CSR_PRE_ALLOC_NET_DATA
443 /* if there is any preallocated netdata left from the prev session free it now */
444 prealloc_netdata_free(card);
447 * Allocate memory for host-side slot data and signal queues.
448 * We need the config info read from the firmware to know how much
449 * memory to allocate.
451 r = card_init_slots(card);
452 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
456 if (r != CSR_RESULT_SUCCESS)
458 unifi_error(card->ospriv, "Init slots failed: %d\n", r);
463 unifi_trace(card->ospriv, UDBG2, "Sending first UniFi interrupt\n");
465 r = unifi_set_host_state(card, UNIFI_HOST_STATE_AWAKE);
466 if (r != CSR_RESULT_SUCCESS)
472 /* Enable the SDIO interrupts now that the f/w is running. */
473 csrResult = CsrSdioInterruptEnable(card->sdio_if);
474 if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
476 return CSR_WIFI_HIP_RESULT_NO_DEVICE;
479 /* Signal the UniFi to start handling messages */
480 r = CardGenInt(card);
481 if (r != CSR_RESULT_SUCCESS)
489 return CSR_RESULT_SUCCESS;
490 } /* unifi_hip_init() */
494 * ---------------------------------------------------------------------------
495 * _build_sdio_config_data
497 * Unpack the SDIO configuration information from a buffer read from
498 * UniFi into a host structure.
499 * The data is byte-swapped for a big-endian host if necessary by the
503 * card Pointer to card struct
504 * cfg_data Destination structure to unpack into.
505 * cfg_data_buf Source buffer to read from. This should be the raw
506 * data read from UniFi.
510 * ---------------------------------------------------------------------------
512 static void _build_sdio_config_data(sdio_config_data_t *cfg_data,
513 const CsrUint8 *cfg_data_buf)
517 cfg_data->version = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
518 offset += SIZEOF_UINT16;
520 cfg_data->sdio_ctrl_offset = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
521 offset += SIZEOF_UINT16;
523 cfg_data->fromhost_sigbuf_handle = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
524 offset += SIZEOF_UINT16;
526 cfg_data->tohost_sigbuf_handle = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
527 offset += SIZEOF_UINT16;
529 cfg_data->num_fromhost_sig_frags = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
530 offset += SIZEOF_UINT16;
532 cfg_data->num_tohost_sig_frags = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
533 offset += SIZEOF_UINT16;
535 cfg_data->num_fromhost_data_slots = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
536 offset += SIZEOF_UINT16;
538 cfg_data->num_tohost_data_slots = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
539 offset += SIZEOF_UINT16;
541 cfg_data->data_slot_size = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
542 offset += SIZEOF_UINT16;
544 cfg_data->initialised = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
545 offset += SIZEOF_UINT16;
547 cfg_data->overlay_size = CSR_GET_UINT32_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
548 offset += SIZEOF_UINT32;
550 cfg_data->data_slot_round = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
551 offset += SIZEOF_UINT16;
553 cfg_data->sig_frag_size = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
554 offset += SIZEOF_UINT16;
556 cfg_data->tohost_signal_padding = CSR_GET_UINT16_FROM_LITTLE_ENDIAN(cfg_data_buf + offset);
557 } /* _build_sdio_config_data() */
561 * - Function ----------------------------------------------------------------
564 * Perform the initialisation procedure described in the UniFi Host
565 * Interface Protocol document (section 3.3.8) and read the run-time
566 * configuration information from the UniFi. This is stuff like number
567 * of bulk data slots etc.
569 * The card enumeration and SD initialisation has already been done by
570 * the SDIO library, see card_sdio_init().
572 * The initialisation is done when firmware is ready, i.e. this may need
573 * to be called after a f/w download operation.
575 * The initialisation procedure goes like this:
576 * - Wait for UniFi to start-up by polling SHARED_MAILBOX1
577 * - Find the symbol table and look up SLT_SDIO_SLOT_CONFIG
578 * - Read the config structure
579 * - Check the "SDIO initialised" flag, if not zero do a h/w reset and
581 * - Decide the number of bulk data slots to allocate, allocate them and
582 * set "SDIO initialised" flag (and generate an interrupt) to say so.
585 * card Pointer to card struct
588 * CSR_RESULT_SUCEESS on success,
589 * a CSR error code on failure
592 * All data in the f/w is stored in a little endian format, without any
593 * padding bytes. Every read from this memory has to be transformed in
594 * host (cpu specific) format, before it is stored in driver's parameters
595 * or/and structures. Athough unifi_card_read16() and unifi_read32() do perform
596 * the convertion internally, unifi_readn() does not.
597 * ---------------------------------------------------------------------------
599 static CsrResult card_hw_init(card_t *card)
601 CsrUint32 slut_address;
602 CsrUint16 initialised;
603 CsrUint16 finger_print;
605 sdio_config_data_t *cfg_data;
606 CsrUint8 cfg_data_buf[SDIO_CONFIG_DATA_SIZE];
609 CsrInt16 major, minor;
610 CsrInt16 search_4slut_again;
616 * The device revision from the TPLMID_MANF and TPLMID_CARD fields
617 * of the CIS are available as
618 * card->sdio_if->pDevice->ManfID
619 * card->sdio_if->pDevice->AppID
623 * Run in a loop so we can patch.
627 /* Reset these each time around the loop. */
628 search_4slut_again = 0;
631 r = card_wait_for_firmware_to_start(card, &slut_address);
632 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
636 if (r != CSR_RESULT_SUCCESS)
638 unifi_error(card->ospriv, "Firmware hasn't started\n");
642 unifi_trace(card->ospriv, UDBG4, "SLUT addr 0x%lX\n", slut_address);
645 * Firmware has started, but doesn't know full clock configuration yet
646 * as some of the information may be in the MIB. Therefore we set an
647 * initial SDIO clock speed, faster than UNIFI_SDIO_CLOCK_SAFE_HZ, for
648 * the patch download and subsequent firmware initialisation, and
649 * full speed UNIFI_SDIO_CLOCK_MAX_HZ will be set once the f/w tells us
652 csrResult = CsrSdioMaxBusClockFrequencySet(card->sdio_if, UNIFI_SDIO_CLOCK_INIT_HZ);
653 if (csrResult != CSR_RESULT_SUCCESS)
655 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
659 card->sdio_clock_speed = UNIFI_SDIO_CLOCK_INIT_HZ;
662 * Check the SLUT fingerprint.
663 * The slut_address is a generic pointer so we must use unifi_card_read16().
665 unifi_trace(card->ospriv, UDBG4, "Looking for SLUT finger print\n");
667 r = unifi_card_read16(card, slut_address, &finger_print);
668 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
672 if (r != CSR_RESULT_SUCCESS)
674 unifi_error(card->ospriv, "Failed to read SLUT finger print\n");
679 if (finger_print != SLUT_FINGERPRINT)
681 unifi_error(card->ospriv, "Failed to find Symbol lookup table fingerprint\n");
682 func_exit_r(CSR_RESULT_FAILURE);
683 return CSR_RESULT_FAILURE;
686 /* Symbol table starts imedately after the fingerprint */
689 /* Search the table until either the end marker is found, or the
690 * loading of patch firmware invalidates the current table.
692 while (!search_4slut_again)
697 r = unifi_card_read16(card, slut_address, &s);
698 if (r != CSR_RESULT_SUCCESS)
705 if (s == CSR_SLT_END)
707 unifi_trace(card->ospriv, UDBG3, " found CSR_SLT_END\n");
711 r = unifi_read32(card, slut_address, &l);
712 if (r != CSR_RESULT_SUCCESS)
722 unifi_trace(card->ospriv, UDBG3, " found SLUT id %02d.%08lx\n", slut.id, slut.obj);
725 case CSR_SLT_SDIO_SLOT_CONFIG:
726 cfg_data = &card->config_data;
728 * unifi_card_readn reads n bytes from the card, where data is stored
729 * in a little endian format, without any padding bytes. So, we
730 * can not just pass the cfg_data pointer or use the
731 * sizeof(sdio_config_data_t) since the structure in the host can
732 * be big endian formatted or have padding bytes for alignment.
733 * We use a char buffer to read the data from the card.
735 r = unifi_card_readn(card, slut.obj, cfg_data_buf, SDIO_CONFIG_DATA_SIZE);
736 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
740 if (r != CSR_RESULT_SUCCESS)
742 unifi_error(card->ospriv, "Failed to read config data\n");
746 /* .. and then we copy the data to the host structure */
747 _build_sdio_config_data(cfg_data, cfg_data_buf);
749 /* Make sure the from host data slots are what we expect
750 we reserve 2 for commands and there should be at least
751 1 left for each access category */
752 if ((cfg_data->num_fromhost_data_slots < UNIFI_RESERVED_COMMAND_SLOTS)
753 || (cfg_data->num_fromhost_data_slots - UNIFI_RESERVED_COMMAND_SLOTS) / UNIFI_NO_OF_TX_QS == 0)
755 unifi_error(card->ospriv, "From host data slots %d\n", cfg_data->num_fromhost_data_slots);
756 unifi_error(card->ospriv, "need to be (queues * x + 2) (UNIFI_RESERVED_COMMAND_SLOTS for commands)\n");
757 func_exit_r(CSR_RESULT_FAILURE);
758 return CSR_RESULT_FAILURE;
761 /* Configure SDIO to-block-size padding */
762 if (card->sdio_io_block_pad)
765 * Firmware limits the maximum padding size via data_slot_round.
766 * Therefore when padding to whole block sizes, the block size
767 * must be configured correctly by adjusting CSR_WIFI_HIP_SDIO_BLOCK_SIZE.
769 if (cfg_data->data_slot_round < card->sdio_io_block_size)
771 unifi_error(card->ospriv,
772 "Configuration error: Block size of %d exceeds f/w data_slot_round of %d\n",
773 card->sdio_io_block_size, cfg_data->data_slot_round);
774 return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
778 * To force the To-Host signals to be rounded up to the SDIO block
779 * size, we need to write the To-Host Signal Padding Fragments
780 * field of the SDIO configuration in UniFi.
782 if ((card->sdio_io_block_size % cfg_data->sig_frag_size) != 0)
784 unifi_error(card->ospriv, "Configuration error: Can not pad to-host signals.\n");
785 func_exit_r(CSR_WIFI_HIP_RESULT_INVALID_VALUE);
786 return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
788 cfg_data->tohost_signal_padding = (CsrUint16) (card->sdio_io_block_size / cfg_data->sig_frag_size);
789 unifi_info(card->ospriv, "SDIO block size %d requires %d padding chunks\n",
790 card->sdio_io_block_size, cfg_data->tohost_signal_padding);
791 r = unifi_card_write16(card, slut.obj + SDIO_TO_HOST_SIG_PADDING_OFFSET, cfg_data->tohost_signal_padding);
792 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
796 if (r != CSR_RESULT_SUCCESS)
798 unifi_error(card->ospriv, "Failed to write To-Host Signal Padding Fragments\n");
804 /* Reconstruct the Generic Pointer address of the
805 * SDIO Control Data Struct.
807 card->sdio_ctrl_addr = cfg_data->sdio_ctrl_offset | (UNIFI_SH_DMEM << 24);
808 card->init_flag_addr = slut.obj + SDIO_INIT_FLAG_OFFSET;
811 case CSR_SLT_BUILD_ID_NUMBER:
814 r = unifi_read32(card, slut.obj, &n);
815 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
819 if (r != CSR_RESULT_SUCCESS)
821 unifi_error(card->ospriv, "Failed to read build id\n");
829 case CSR_SLT_BUILD_ID_STRING:
830 r = unifi_readnz(card, slut.obj, card->build_id_string,
831 sizeof(card->build_id_string));
832 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
836 if (r != CSR_RESULT_SUCCESS)
838 unifi_error(card->ospriv, "Failed to read build string\n");
844 case CSR_SLT_PERSISTENT_STORE_DB:
847 case CSR_SLT_BOOT_LOADER_CONTROL:
849 /* This command copies most of the station firmware
850 * image from ROM into program RAM. It also clears
851 * out the zerod data and sets up the initialised
853 r = unifi_do_loader_op(card, slut.obj + 6, UNIFI_BOOT_LOADER_LOAD_STA);
854 if (r != CSR_RESULT_SUCCESS)
856 unifi_error(card->ospriv, "Failed to write loader load image command\n");
861 dlpriv = unifi_dl_fw_read_start(card, UNIFI_FW_STA);
863 /* dlpriv might be NULL, we still need to do the do_loader_op step. */
866 /* Download the firmware. */
867 r = unifi_dl_patch(card, dlpriv, slut.obj);
869 /* Free the firmware file information. */
870 unifi_fw_read_stop(card->ospriv, dlpriv);
872 if (r != CSR_RESULT_SUCCESS)
874 unifi_error(card->ospriv, "Failed to patch firmware\n");
880 /* This command starts the firmware image that we want (the
881 * station by default) with any patches required applied. */
882 r = unifi_do_loader_op(card, slut.obj + 6, UNIFI_BOOT_LOADER_RESTART);
883 if (r != CSR_RESULT_SUCCESS)
885 unifi_error(card->ospriv, "Failed to write loader restart command\n");
890 /* The now running patch f/w defines a new SLUT data structure -
891 * the current one is no longer valid. We must drop out of the
892 * processing loop and enumerate the new SLUT (which may appear
893 * at a different offset).
895 search_4slut_again = 1;
898 case CSR_SLT_PANIC_DATA_PHY:
899 card->panic_data_phy_addr = slut.obj;
902 case CSR_SLT_PANIC_DATA_MAC:
903 card->panic_data_mac_addr = slut.obj;
911 } while (search_4slut_again);
913 /* Did we find the Config Data ? */
914 if (cfg_data == NULL)
916 unifi_error(card->ospriv, "Failed to find SDIO_SLOT_CONFIG Symbol\n");
917 func_exit_r(CSR_RESULT_FAILURE);
918 return CSR_RESULT_FAILURE;
922 * Has ths card already been initialised?
923 * If so, return an error so we do a h/w reset and start again.
925 r = unifi_card_read16(card, card->init_flag_addr, &initialised);
926 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
930 if (r != CSR_RESULT_SUCCESS)
932 unifi_error(card->ospriv, "Failed to read init flag at %08lx\n",
933 card->init_flag_addr);
937 if (initialised != 0)
939 func_exit_r(CSR_RESULT_FAILURE);
940 return CSR_RESULT_FAILURE;
945 * Now check the UniFi firmware version
947 major = (cfg_data->version >> 8) & 0xFF;
948 minor = cfg_data->version & 0xFF;
949 unifi_info(card->ospriv, "UniFi f/w protocol version %d.%d (driver %d.%d)\n",
951 UNIFI_HIP_MAJOR_VERSION, UNIFI_HIP_MINOR_VERSION);
953 unifi_info(card->ospriv, "Firmware build %u: %s\n",
954 card->build_id, card->build_id_string);
956 if (major != UNIFI_HIP_MAJOR_VERSION)
958 unifi_error(card->ospriv, "UniFi f/w protocol major version (%d) is different from driver (v%d.%d)\n",
959 major, UNIFI_HIP_MAJOR_VERSION, UNIFI_HIP_MINOR_VERSION);
960 #ifndef CSR_WIFI_DISABLE_HIP_VERSION_CHECK
961 func_exit_r(CSR_RESULT_FAILURE);
962 return CSR_RESULT_FAILURE;
965 if (minor < UNIFI_HIP_MINOR_VERSION)
967 unifi_error(card->ospriv, "UniFi f/w protocol version (v%d.%d) is older than minimum required by driver (v%d.%d).\n",
969 UNIFI_HIP_MAJOR_VERSION, UNIFI_HIP_MINOR_VERSION);
970 #ifndef CSR_WIFI_DISABLE_HIP_VERSION_CHECK
971 func_exit_r(CSR_RESULT_FAILURE);
972 return CSR_RESULT_FAILURE;
976 /* Read panic codes from a previous firmware panic. If the firmware has
977 * not panicked since power was applied (e.g. power-off hard reset)
978 * the stored panic codes will not be updated.
980 unifi_read_panic(card);
983 return CSR_RESULT_SUCCESS;
984 } /* card_hw_init() */
988 * ---------------------------------------------------------------------------
989 * card_wait_for_unifi_to_reset
991 * Waits for a reset to complete by polling the WLAN function enable
992 * bit (which is cleared on reset).
995 * card Pointer to card struct
998 * CSR_RESULT_SUCCESS on success, CSR error code on failure.
999 * ---------------------------------------------------------------------------
1001 static CsrResult card_wait_for_unifi_to_reset(card_t *card)
1006 CsrResult csrResult;
1010 r = CSR_RESULT_SUCCESS;
1011 for (i = 0; i < MAILBOX2_ATTEMPTS; i++)
1013 unifi_trace(card->ospriv, UDBG1, "waiting for reset to complete, attempt %d\n", i);
1014 if (card->chip_id > SDIO_CARD_ID_UNIFI_2)
1016 /* It's quite likely that this read will timeout for the
1017 * first few tries - especially if we have reset via
1020 #if defined (CSR_WIFI_HIP_DEBUG_OFFLINE) && defined (CSR_WIFI_HIP_SDIO_TRACE)
1021 unifi_debug_log_to_buf("m0@%02X=", SDIO_IO_READY);
1023 csrResult = CsrSdioF0Read8(card->sdio_if, SDIO_IO_READY, &io_enable);
1024 #if defined (CSR_WIFI_HIP_DEBUG_OFFLINE) && defined (CSR_WIFI_HIP_SDIO_TRACE)
1025 if (csrResult != CSR_RESULT_SUCCESS)
1027 unifi_debug_log_to_buf("error=%X\n", csrResult);
1031 unifi_debug_log_to_buf("%X\n", io_enable);
1034 if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
1036 return CSR_WIFI_HIP_RESULT_NO_DEVICE;
1038 r = CSR_RESULT_SUCCESS;
1039 if (csrResult != CSR_RESULT_SUCCESS)
1041 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
1046 r = sdio_read_f0(card, SDIO_IO_ENABLE, &io_enable);
1048 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1052 if (r == CSR_RESULT_SUCCESS)
1055 CsrInt16 enabled = io_enable & (1 << card->function);
1059 unifi_trace(card->ospriv, UDBG1,
1060 "Reset complete (function %d is disabled) in ~ %u msecs\n",
1061 card->function, i * MAILBOX2_TIMEOUT);
1063 /* Enable WLAN function and verify MAILBOX2 is zero'd */
1064 csrResult = CsrSdioFunctionEnable(card->sdio_if);
1065 if (csrResult != CSR_RESULT_SUCCESS)
1067 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
1068 unifi_error(card->ospriv, "CsrSdioFunctionEnable failed %d\n", r);
1073 r = unifi_read_direct16(card, ChipHelper_SDIO_HIP_HANDSHAKE(card->helper) * 2, &mbox2);
1074 if (r != CSR_RESULT_SUCCESS)
1076 unifi_error(card->ospriv, "read HIP_HANDSHAKE failed %d\n", r);
1081 unifi_error(card->ospriv, "MAILBOX2 non-zero after reset (mbox2 = %04x)\n", mbox2);
1082 r = CSR_RESULT_FAILURE;
1088 if (card->chip_id > SDIO_CARD_ID_UNIFI_2)
1090 /* We ignore read failures for the first few reads,
1091 * they are probably benign. */
1092 if (i > MAILBOX2_ATTEMPTS / 4)
1094 unifi_trace(card->ospriv, UDBG1, "Failed to read CCCR IO Ready register while polling for reset\n");
1099 unifi_trace(card->ospriv, UDBG1, "Failed to read CCCR IO Enable register while polling for reset\n");
1102 CsrThreadSleep(MAILBOX2_TIMEOUT);
1105 if (r == CSR_RESULT_SUCCESS && i == MAILBOX2_ATTEMPTS)
1107 unifi_trace(card->ospriv, UDBG1, "Timeout waiting for UniFi to complete reset\n");
1108 r = CSR_RESULT_FAILURE;
1113 } /* card_wait_for_unifi_to_reset() */
1117 * ---------------------------------------------------------------------------
1118 * card_wait_for_unifi_to_disable
1120 * Waits for the function to become disabled by polling the
1124 * card Pointer to card struct
1127 * CSR_RESULT_SUCCESS on success, CSR error code on failure.
1129 * Notes: This function can only be used with
1130 * card->chip_id > SDIO_CARD_ID_UNIFI_2
1131 * ---------------------------------------------------------------------------
1133 static CsrResult card_wait_for_unifi_to_disable(card_t *card)
1138 CsrResult csrResult;
1142 if (card->chip_id <= SDIO_CARD_ID_UNIFI_2)
1144 unifi_error(card->ospriv,
1145 "Function reset method not supported for chip_id=%d\n",
1148 return CSR_RESULT_FAILURE;
1151 r = CSR_RESULT_SUCCESS;
1152 for (i = 0; i < MAILBOX2_ATTEMPTS; i++)
1154 unifi_trace(card->ospriv, UDBG1, "waiting for disable to complete, attempt %d\n", i);
1157 * It's quite likely that this read will timeout for the
1158 * first few tries - especially if we have reset via
1161 #if defined (CSR_WIFI_HIP_DEBUG_OFFLINE) && defined (CSR_WIFI_HIP_SDIO_TRACE)
1162 unifi_debug_log_to_buf("r0@%02X=", SDIO_IO_READY);
1164 csrResult = CsrSdioF0Read8(card->sdio_if, SDIO_IO_READY, &io_enable);
1165 #if defined (CSR_WIFI_HIP_DEBUG_OFFLINE) && defined (CSR_WIFI_HIP_SDIO_TRACE)
1166 if (csrResult != CSR_RESULT_SUCCESS)
1168 unifi_debug_log_to_buf("error=%X\n", csrResult);
1172 unifi_debug_log_to_buf("%X\n", io_enable);
1175 if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
1177 return CSR_WIFI_HIP_RESULT_NO_DEVICE;
1179 if (csrResult == CSR_RESULT_SUCCESS)
1181 CsrInt16 enabled = io_enable & (1 << card->function);
1182 r = CSR_RESULT_SUCCESS;
1185 unifi_trace(card->ospriv, UDBG1,
1186 "Disable complete (function %d is disabled) in ~ %u msecs\n",
1187 card->function, i * MAILBOX2_TIMEOUT);
1195 * We ignore read failures for the first few reads,
1196 * they are probably benign.
1198 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
1199 if (i > (MAILBOX2_ATTEMPTS / 4))
1201 unifi_trace(card->ospriv, UDBG1,
1202 "Failed to read CCCR IO Ready register while polling for disable\n");
1205 CsrThreadSleep(MAILBOX2_TIMEOUT);
1208 if ((r == CSR_RESULT_SUCCESS) && (i == MAILBOX2_ATTEMPTS))
1210 unifi_trace(card->ospriv, UDBG1, "Timeout waiting for UniFi to complete disable\n");
1211 r = CSR_RESULT_FAILURE;
1216 } /* card_wait_for_unifi_to_reset() */
1220 * ---------------------------------------------------------------------------
1221 * card_wait_for_firmware_to_start
1223 * Polls the MAILBOX1 register for a non-zero value.
1224 * Then reads MAILBOX0 and forms the two values into a 32-bit address
1225 * which is returned to the caller.
1228 * card Pointer to card struct
1229 * paddr Pointer to receive the UniFi address formed
1230 * by concatenating MAILBOX1 and MAILBOX0.
1233 * CSR_RESULT_SUCCESS on success, CSR error code on failure.
1234 * ---------------------------------------------------------------------------
1236 CsrResult card_wait_for_firmware_to_start(card_t *card, CsrUint32 *paddr)
1239 CsrUint16 mbox0, mbox1;
1245 * Wait for UniFi to initialise its data structures by polling
1246 * the SHARED_MAILBOX1 register.
1247 * Experience shows this is typically 120ms.
1249 CsrThreadSleep(MAILBOX1_TIMEOUT);
1252 unifi_trace(card->ospriv, UDBG1, "waiting for MAILBOX1 to be non-zero...\n");
1253 for (i = 0; i < MAILBOX1_ATTEMPTS; i++)
1255 r = unifi_read_direct16(card, ChipHelper_MAILBOX1(card->helper) * 2, &mbox1);
1256 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1260 if (r != CSR_RESULT_SUCCESS)
1262 /* These reads can fail if UniFi isn't up yet, so try again */
1263 unifi_warning(card->ospriv, "Failed to read UniFi Mailbox1 register\n");
1266 if ((r == CSR_RESULT_SUCCESS) && (mbox1 != 0))
1268 unifi_trace(card->ospriv, UDBG1, "MAILBOX1 ready (0x%04X) in %u millisecs\n",
1269 mbox1, i * MAILBOX1_TIMEOUT);
1271 /* Read the MAILBOX1 again in case we caught the value as it
1273 r = unifi_read_direct16(card, ChipHelper_MAILBOX1(card->helper) * 2, &mbox1);
1274 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1278 if (r != CSR_RESULT_SUCCESS)
1280 unifi_error(card->ospriv, "Failed to read UniFi Mailbox1 register for second time\n");
1284 unifi_trace(card->ospriv, UDBG1, "MAILBOX1 value=0x%04X\n", mbox1);
1289 CsrThreadSleep(MAILBOX1_TIMEOUT);
1290 if ((i % 100) == 99)
1292 unifi_trace(card->ospriv, UDBG2, "MAILBOX1 not ready (0x%X), still trying...\n", mbox1);
1296 if ((r == CSR_RESULT_SUCCESS) && (mbox1 == 0))
1298 unifi_trace(card->ospriv, UDBG1, "Timeout waiting for firmware to start, Mailbox1 still 0 after %d ms\n",
1299 MAILBOX1_ATTEMPTS * MAILBOX1_TIMEOUT);
1300 func_exit_r(CSR_RESULT_FAILURE);
1301 return CSR_RESULT_FAILURE;
1306 * Complete the reset handshake by setting MAILBOX2 to 0xFFFF
1308 r = unifi_write_direct16(card, ChipHelper_SDIO_HIP_HANDSHAKE(card->helper) * 2, 0xFFFF);
1309 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1313 if (r != CSR_RESULT_SUCCESS)
1315 unifi_error(card->ospriv, "Failed to write f/w startup handshake to MAILBOX2\n");
1322 * Read the Symbol Look Up Table (SLUT) offset.
1323 * Top 16 bits are in mbox1, read the lower 16 bits from mbox0.
1326 r = unifi_read_direct16(card, ChipHelper_MAILBOX0(card->helper) * 2, &mbox0);
1327 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1331 if (r != CSR_RESULT_SUCCESS)
1333 unifi_error(card->ospriv, "Failed to read UniFi Mailbox0 register\n");
1338 *paddr = (((CsrUint32)mbox1 << 16) | mbox0);
1341 return CSR_RESULT_SUCCESS;
1342 } /* card_wait_for_firmware_to_start() */
1346 * ---------------------------------------------------------------------------
1347 * unifi_capture_panic
1349 * Attempt to capture panic codes from the firmware. This may involve
1350 * warm reset of the chip to regain access following a watchdog reset.
1353 * card Pointer to card struct
1356 * CSR_RESULT_SUCCESS if panic codes were captured, or none available
1357 * CSR_RESULT_FAILURE if the driver could not access function 1
1358 * ---------------------------------------------------------------------------
1360 CsrResult unifi_capture_panic(card_t *card)
1364 /* The firmware must have previously initialised to read the panic addresses
1367 if (!card->panic_data_phy_addr || !card->panic_data_mac_addr)
1370 return CSR_RESULT_SUCCESS;
1373 /* Ensure we can access function 1 following a panic/watchdog reset */
1374 if (card_access_panic(card) == CSR_RESULT_SUCCESS)
1376 /* Read the panic codes */
1377 unifi_read_panic(card);
1381 unifi_info(card->ospriv, "Unable to read panic codes");
1385 return CSR_RESULT_SUCCESS;
1390 * ---------------------------------------------------------------------------
1392 * Attempt to read the WLAN SDIO function in order to read panic codes
1393 * and perform various reset steps to regain access if the read fails.
1396 * card Pointer to card struct
1399 * CSR_RESULT_SUCCESS if panic codes can be read
1400 * CSR error code if panic codes can not be read
1401 * ---------------------------------------------------------------------------
1403 static CsrResult card_access_panic(card_t *card)
1405 CsrUint16 data_u16 = 0;
1411 /* A chip version of zero means that the version never got succesfully read
1412 * during reset. In this case give up because it will not be possible to
1413 * verify the chip version.
1415 if (!card->chip_version)
1417 unifi_info(card->ospriv, "Unknown chip version\n");
1418 return CSR_RESULT_FAILURE;
1421 /* Ensure chip is awake or access to function 1 will fail */
1422 r = unifi_set_host_state(card, UNIFI_HOST_STATE_AWAKE);
1423 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1427 if (r != CSR_RESULT_SUCCESS)
1429 unifi_error(card->ospriv, "unifi_set_host_state() failed %d\n", r);
1430 return CSR_RESULT_FAILURE; /* Card is probably unpowered */
1434 for (i = 0; i < 3; i++)
1436 sr = CsrSdioRead16(card->sdio_if, CHIP_HELPER_UNIFI_GBL_CHIP_VERSION * 2, &data_u16);
1437 if (sr != CSR_RESULT_SUCCESS || data_u16 != card->chip_version)
1439 unifi_info(card->ospriv, "Failed to read valid chip version sr=%d (0x%04x want 0x%04x) try %d\n",
1440 sr, data_u16, card->chip_version, i);
1442 /* Set clock speed low */
1443 sr = CsrSdioMaxBusClockFrequencySet(card->sdio_if, UNIFI_SDIO_CLOCK_SAFE_HZ);
1444 if (sr != CSR_RESULT_SUCCESS)
1446 unifi_error(card->ospriv, "CsrSdioMaxBusClockFrequencySet() failed1 %d\n", sr);
1447 r = ConvertCsrSdioToCsrHipResult(card, sr);
1449 card->sdio_clock_speed = UNIFI_SDIO_CLOCK_SAFE_HZ;
1451 /* First try re-enabling function in case a f/w watchdog reset disabled it */
1454 unifi_info(card->ospriv, "Try function enable\n");
1455 sr = CsrSdioFunctionEnable(card->sdio_if);
1456 if (sr != CSR_RESULT_SUCCESS)
1458 r = ConvertCsrSdioToCsrHipResult(card, sr);
1459 unifi_error(card->ospriv, "CsrSdioFunctionEnable failed %d (HIP %d)\n", sr, r);
1464 /* Second try, set awake */
1465 unifi_info(card->ospriv, "Try set awake\n");
1467 /* Ensure chip is awake */
1468 r = unifi_set_host_state(card, UNIFI_HOST_STATE_AWAKE);
1469 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
1473 if (r != CSR_RESULT_SUCCESS)
1475 unifi_error(card->ospriv, "unifi_set_host_state() failed2 %d\n", r);
1478 /* Set clock speed low in case setting the host state raised it, which
1479 * would only happen if host state was previously TORPID
1481 sr = CsrSdioMaxBusClockFrequencySet(card->sdio_if, UNIFI_SDIO_CLOCK_SAFE_HZ);
1482 if (sr != CSR_RESULT_SUCCESS)
1484 unifi_error(card->ospriv, "CsrSdioMaxBusClockFrequencySet() failed2 %d\n", sr);
1486 card->sdio_clock_speed = UNIFI_SDIO_CLOCK_SAFE_HZ;
1493 /* Perform a s/w reset to preserve as much as the card state as possible,
1494 * (mainly the preserve RAM). The context will be lost for coredump - but as we
1495 * were unable to access the WLAN function for panic, the coredump would have
1496 * also failed without a reset.
1498 unifi_info(card->ospriv, "Try s/w reset\n");
1500 r = unifi_card_hard_reset(card);
1501 if (r != CSR_RESULT_SUCCESS)
1503 unifi_error(card->ospriv, "unifi_card_hard_reset() failed %d\n", r);
1510 unifi_info(card->ospriv, "Read chip version 0x%x after %d retries\n", data_u16, i);
1516 r = ConvertCsrSdioToCsrHipResult(card, sr);
1523 * ---------------------------------------------------------------------------
1525 * Reads, saves and prints panic codes stored by the firmware in UniFi's
1526 * preserve RAM by the last panic that occurred since chip was powered.
1527 * Nothing is saved if the panic codes are read as zero.
1530 * card Pointer to card struct
1533 * ---------------------------------------------------------------------------
1535 void unifi_read_panic(card_t *card)
1538 CsrUint16 p_code, p_arg;
1542 /* The firmware must have previously initialised to read the panic addresses
1545 if (!card->panic_data_phy_addr || !card->panic_data_mac_addr)
1550 /* Get the panic data from PHY */
1551 r = unifi_card_read16(card, card->panic_data_phy_addr, &p_code);
1552 if (r != CSR_RESULT_SUCCESS)
1554 unifi_error(card->ospriv, "capture_panic: unifi_read16 %08x failed %d\n", card->panic_data_phy_addr, r);
1559 r = unifi_card_read16(card, card->panic_data_phy_addr + 2, &p_arg);
1560 if (r != CSR_RESULT_SUCCESS)
1562 unifi_error(card->ospriv, "capture_panic: unifi_read16 %08x failed %d\n", card->panic_data_phy_addr + 2, r);
1564 unifi_error(card->ospriv, "Last UniFi PHY PANIC %04x arg %04x\n", p_code, p_arg);
1565 card->last_phy_panic_code = p_code;
1566 card->last_phy_panic_arg = p_arg;
1569 /* Get the panic data from MAC */
1570 r = unifi_card_read16(card, card->panic_data_mac_addr, &p_code);
1571 if (r != CSR_RESULT_SUCCESS)
1573 unifi_error(card->ospriv, "capture_panic: unifi_read16 %08x failed %d\n", card->panic_data_mac_addr, r);
1578 r = unifi_card_read16(card, card->panic_data_mac_addr + 2, &p_arg);
1579 if (r != CSR_RESULT_SUCCESS)
1581 unifi_error(card->ospriv, "capture_panic: unifi_read16 %08x failed %d\n", card->panic_data_mac_addr + 2, r);
1583 unifi_error(card->ospriv, "Last UniFi MAC PANIC %04x arg %04x\n", p_code, p_arg);
1584 card->last_mac_panic_code = p_code;
1585 card->last_mac_panic_arg = p_arg;
1593 * ---------------------------------------------------------------------------
1594 * card_allocate_memory_resources
1596 * Allocates memory for the from-host, to-host bulk data slots,
1597 * soft queue buffers and bulk data buffers.
1600 * card Pointer to card struct
1603 * CSR_RESULT_SUCCESS on success, CSR error code on failure.
1604 * ---------------------------------------------------------------------------
1606 static CsrResult card_allocate_memory_resources(card_t *card)
1608 CsrInt16 n, i, k, r;
1609 sdio_config_data_t *cfg_data;
1613 /* Reset any state carried forward from a previous life */
1614 card->fh_command_queue.q_rd_ptr = 0;
1615 card->fh_command_queue.q_wr_ptr = 0;
1616 (void)CsrSnprintf(card->fh_command_queue.name, UNIFI_QUEUE_NAME_MAX_LENGTH,
1618 for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
1620 card->fh_traffic_queue[i].q_rd_ptr = 0;
1621 card->fh_traffic_queue[i].q_wr_ptr = 0;
1622 (void)CsrSnprintf(card->fh_traffic_queue[i].name,
1623 UNIFI_QUEUE_NAME_MAX_LENGTH, "fh_data_q%d", i);
1625 #ifndef CSR_WIFI_HIP_TA_DISABLE
1626 unifi_ta_sampling_init(card);
1628 /* Convenience short-cut */
1629 cfg_data = &card->config_data;
1632 * Allocate memory for the from-host and to-host signal buffers.
1634 card->fh_buffer.buf = CsrMemAllocDma(UNIFI_FH_BUF_SIZE);
1635 if (card->fh_buffer.buf == NULL)
1637 unifi_error(card->ospriv, "Failed to allocate memory for F-H signals\n");
1638 func_exit_r(CSR_WIFI_HIP_RESULT_NO_MEMORY);
1639 return CSR_WIFI_HIP_RESULT_NO_MEMORY;
1641 card->fh_buffer.bufsize = UNIFI_FH_BUF_SIZE;
1642 card->fh_buffer.ptr = card->fh_buffer.buf;
1643 card->fh_buffer.count = 0;
1645 card->th_buffer.buf = CsrMemAllocDma(UNIFI_FH_BUF_SIZE);
1646 if (card->th_buffer.buf == NULL)
1648 unifi_error(card->ospriv, "Failed to allocate memory for T-H signals\n");
1649 func_exit_r(CSR_WIFI_HIP_RESULT_NO_MEMORY);
1650 return CSR_WIFI_HIP_RESULT_NO_MEMORY;
1652 card->th_buffer.bufsize = UNIFI_FH_BUF_SIZE;
1653 card->th_buffer.ptr = card->th_buffer.buf;
1654 card->th_buffer.count = 0;
1658 * Allocate memory for the from-host and to-host bulk data slots.
1659 * This is done as separate CsrPmemAllocs because lots of smaller
1660 * allocations are more likely to succeed than one huge one.
1663 /* Allocate memory for the array of pointers */
1664 n = cfg_data->num_fromhost_data_slots;
1666 unifi_trace(card->ospriv, UDBG3, "Alloc from-host resources, %d slots.\n", n);
1667 card->from_host_data =
1668 (slot_desc_t *)CsrMemAlloc(n * sizeof(slot_desc_t));
1669 if (card->from_host_data == NULL)
1671 unifi_error(card->ospriv, "Failed to allocate memory for F-H bulk data array\n");
1672 func_exit_r(CSR_WIFI_HIP_RESULT_NO_MEMORY);
1673 return CSR_WIFI_HIP_RESULT_NO_MEMORY;
1676 /* Initialise from-host bulk data slots */
1677 for (i = 0; i < n; i++)
1679 UNIFI_INIT_BULK_DATA(&card->from_host_data[i].bd);
1682 /* Allocate memory for the array used for slot host tag mapping */
1683 card->fh_slot_host_tag_record =
1684 (CsrUint32 *)CsrMemAlloc(n * sizeof(CsrUint32));
1686 if (card->fh_slot_host_tag_record == NULL)
1688 unifi_error(card->ospriv, "Failed to allocate memory for F-H slot host tag mapping array\n");
1689 func_exit_r(CSR_WIFI_HIP_RESULT_NO_MEMORY);
1690 return CSR_WIFI_HIP_RESULT_NO_MEMORY;
1693 /* Initialise host tag entries for from-host bulk data slots */
1694 for (i = 0; i < n; i++)
1696 card->fh_slot_host_tag_record[i] = CSR_WIFI_HIP_RESERVED_HOST_TAG;
1700 /* Allocate memory for the array of pointers */
1701 n = cfg_data->num_tohost_data_slots;
1703 unifi_trace(card->ospriv, UDBG3, "Alloc to-host resources, %d slots.\n", n);
1704 card->to_host_data =
1705 (bulk_data_desc_t *)CsrMemAlloc(n * sizeof(bulk_data_desc_t));
1706 if (card->to_host_data == NULL)
1708 unifi_error(card->ospriv, "Failed to allocate memory for T-H bulk data array\n");
1709 func_exit_r(CSR_WIFI_HIP_RESULT_NO_MEMORY);
1710 return CSR_WIFI_HIP_RESULT_NO_MEMORY;
1713 /* Initialise to-host bulk data slots */
1714 for (i = 0; i < n; i++)
1716 UNIFI_INIT_BULK_DATA(&card->to_host_data[i]);
1720 * Initialise buffers for soft Q
1722 for (i = 0; i < UNIFI_SOFT_COMMAND_Q_LENGTH; i++)
1724 for (r = 0; r < UNIFI_MAX_DATA_REFERENCES; r++)
1726 UNIFI_INIT_BULK_DATA(&card->fh_command_q_body[i].bulkdata[r]);
1730 for (k = 0; k < UNIFI_NO_OF_TX_QS; k++)
1732 for (i = 0; i < UNIFI_SOFT_TRAFFIC_Q_LENGTH; i++)
1734 for (r = 0; r < UNIFI_MAX_DATA_REFERENCES; r++)
1736 UNIFI_INIT_BULK_DATA(&card->fh_traffic_q_body[k][i].bulkdata[r]);
1741 card->memory_resources_allocated = 1;
1744 return CSR_RESULT_SUCCESS;
1745 } /* card_allocate_memory_resources() */
1749 * ---------------------------------------------------------------------------
1750 * unifi_free_bulk_data
1752 * Free the data associated to a bulk data structure.
1755 * card Pointer to card struct
1756 * bulk_data_slot Pointer to bulk data structure
1761 * ---------------------------------------------------------------------------
1763 static void unifi_free_bulk_data(card_t *card, bulk_data_desc_t *bulk_data_slot)
1765 if (bulk_data_slot->data_length != 0)
1767 unifi_net_data_free(card->ospriv, bulk_data_slot);
1769 } /* unifi_free_bulk_data() */
1773 * ---------------------------------------------------------------------------
1774 * card_free_memory_resources
1776 * Frees memory allocated for the from-host, to-host bulk data slots,
1777 * soft queue buffers and bulk data buffers.
1780 * card Pointer to card struct
1784 * ---------------------------------------------------------------------------
1786 static void card_free_memory_resources(card_t *card)
1790 unifi_trace(card->ospriv, UDBG1, "Freeing card memory resources.\n");
1792 /* Clear our internal queues */
1793 unifi_cancel_pending_signals(card);
1796 if (card->to_host_data)
1798 CsrMemFree(card->to_host_data);
1799 card->to_host_data = NULL;
1802 if (card->from_host_data)
1804 CsrMemFree(card->from_host_data);
1805 card->from_host_data = NULL;
1808 /* free the memory for slot host tag mapping array */
1809 if (card->fh_slot_host_tag_record)
1811 CsrMemFree(card->fh_slot_host_tag_record);
1812 card->fh_slot_host_tag_record = NULL;
1815 if (card->fh_buffer.buf)
1817 CsrMemFreeDma(card->fh_buffer.buf);
1819 card->fh_buffer.ptr = card->fh_buffer.buf = NULL;
1820 card->fh_buffer.bufsize = 0;
1821 card->fh_buffer.count = 0;
1823 if (card->th_buffer.buf)
1825 CsrMemFreeDma(card->th_buffer.buf);
1827 card->th_buffer.ptr = card->th_buffer.buf = NULL;
1828 card->th_buffer.bufsize = 0;
1829 card->th_buffer.count = 0;
1832 card->memory_resources_allocated = 0;
1835 } /* card_free_memory_resources() */
1838 static void card_init_soft_queues(card_t *card)
1844 unifi_trace(card->ospriv, UDBG1, "Initialising internal signal queues.\n");
1845 /* Reset any state carried forward from a previous life */
1846 card->fh_command_queue.q_rd_ptr = 0;
1847 card->fh_command_queue.q_wr_ptr = 0;
1848 (void)CsrSnprintf(card->fh_command_queue.name, UNIFI_QUEUE_NAME_MAX_LENGTH,
1850 for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
1852 card->fh_traffic_queue[i].q_rd_ptr = 0;
1853 card->fh_traffic_queue[i].q_wr_ptr = 0;
1854 (void)CsrSnprintf(card->fh_traffic_queue[i].name,
1855 UNIFI_QUEUE_NAME_MAX_LENGTH, "fh_data_q%d", i);
1857 #ifndef CSR_WIFI_HIP_TA_DISABLE
1858 unifi_ta_sampling_init(card);
1865 * ---------------------------------------------------------------------------
1866 * unifi_cancel_pending_signals
1868 * Free the signals and associated bulk data, pending in the core.
1871 * card Pointer to card struct
1875 * ---------------------------------------------------------------------------
1877 void unifi_cancel_pending_signals(card_t *card)
1882 unifi_trace(card->ospriv, UDBG1, "Canceling pending signals.\n");
1884 if (card->to_host_data)
1887 * Free any bulk data buffers allocated for the t-h slots
1888 * This will clear all buffers that did not make it to
1889 * unifi_receive_event() before cancel was request.
1891 n = card->config_data.num_tohost_data_slots;
1892 unifi_trace(card->ospriv, UDBG3, "Freeing to-host resources, %d slots.\n", n);
1893 for (i = 0; i < n; i++)
1895 unifi_free_bulk_data(card, &card->to_host_data[i]);
1900 * If any of the from-host bulk data has reached the card->from_host_data
1901 * but not UniFi, we need to free the buffers here.
1903 if (card->from_host_data)
1905 /* Free any bulk data buffers allocated for the f-h slots */
1906 n = card->config_data.num_fromhost_data_slots;
1907 unifi_trace(card->ospriv, UDBG3, "Freeing from-host resources, %d slots.\n", n);
1908 for (i = 0; i < n; i++)
1910 unifi_free_bulk_data(card, &card->from_host_data[i].bd);
1913 for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
1915 card->dynamic_slot_data.from_host_used_slots[i] = 0;
1916 card->dynamic_slot_data.from_host_max_slots[i] = 0;
1917 card->dynamic_slot_data.from_host_reserved_slots[i] = 0;
1922 * Free any bulk data buffers allocated in the soft queues.
1923 * This covers the case where a bulk data pointer has reached the soft queue
1924 * but not the card->from_host_data.
1926 unifi_trace(card->ospriv, UDBG3, "Freeing cmd q resources.\n");
1927 for (i = 0; i < UNIFI_SOFT_COMMAND_Q_LENGTH; i++)
1929 for (r = 0; r < UNIFI_MAX_DATA_REFERENCES; r++)
1931 unifi_free_bulk_data(card, &card->fh_command_q_body[i].bulkdata[r]);
1935 unifi_trace(card->ospriv, UDBG3, "Freeing traffic q resources.\n");
1936 for (n = 0; n < UNIFI_NO_OF_TX_QS; n++)
1938 for (i = 0; i < UNIFI_SOFT_TRAFFIC_Q_LENGTH; i++)
1940 for (r = 0; r < UNIFI_MAX_DATA_REFERENCES; r++)
1942 unifi_free_bulk_data(card, &card->fh_traffic_q_body[n][i].bulkdata[r]);
1947 card_init_soft_queues(card);
1950 } /* unifi_cancel_pending_signals() */
1954 * ---------------------------------------------------------------------------
1957 * Free the memory allocated for the card structure and buffers.
1960 * The porting layer is responsible for freeing any mini-coredump buffers
1961 * allocated when it called unifi_coredump_init(), by calling
1962 * unifi_coredump_free() before calling this function.
1965 * card Pointer to card struct
1969 * ---------------------------------------------------------------------------
1971 void unifi_free_card(card_t *card)
1974 #ifdef CSR_PRE_ALLOC_NET_DATA
1975 prealloc_netdata_free(card);
1977 /* Free any memory allocated. */
1978 card_free_memory_resources(card);
1980 /* Warn if caller didn't free coredump buffers */
1983 unifi_error(card->ospriv, "Caller should call unifi_coredump_free()\n");
1984 unifi_coredump_free(card); /* free anyway to prevent memory leak */
1990 } /* unifi_free_card() */
1994 * ---------------------------------------------------------------------------
1997 * Allocate memory for host-side slot data and signal queues.
2000 * card Pointer to card object
2004 * ---------------------------------------------------------------------------
2006 static CsrResult card_init_slots(card_t *card)
2013 /* Allocate the buffers we need, only once. */
2014 if (card->memory_resources_allocated == 1)
2016 card_free_memory_resources(card);
2020 /* Initialise our internal command and traffic queues */
2021 card_init_soft_queues(card);
2024 r = card_allocate_memory_resources(card);
2025 if (r != CSR_RESULT_SUCCESS)
2027 unifi_error(card->ospriv, "Failed to allocate card memory resources.\n");
2028 card_free_memory_resources(card);
2033 if (card->sdio_ctrl_addr == 0)
2035 unifi_error(card->ospriv, "Failed to find config struct!\n");
2036 func_exit_r(CSR_WIFI_HIP_RESULT_INVALID_VALUE);
2037 return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
2041 * Set initial counts.
2044 card->from_host_data_head = 0;
2046 /* Get initial signal counts from UniFi, in case it has not been reset. */
2050 /* Get the from-host-signals-written count */
2051 r = unifi_card_read16(card, card->sdio_ctrl_addr + 0, &s);
2052 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2056 if (r != CSR_RESULT_SUCCESS)
2058 unifi_error(card->ospriv, "Failed to read from-host sig written count\n");
2062 card->from_host_signals_w = (CsrInt16)s;
2064 /* Get the to-host-signals-written count */
2065 r = unifi_card_read16(card, card->sdio_ctrl_addr + 6, &s);
2066 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2070 if (r != CSR_RESULT_SUCCESS)
2072 unifi_error(card->ospriv, "Failed to read to-host sig read count\n");
2076 card->to_host_signals_r = (CsrInt16)s;
2079 /* Set Initialised flag. */
2080 r = unifi_card_write16(card, card->init_flag_addr, 0x0001);
2081 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2085 if (r != CSR_RESULT_SUCCESS)
2087 unifi_error(card->ospriv, "Failed to write initialised flag\n");
2092 /* Dynamic queue reservation */
2093 CsrMemSet(&card->dynamic_slot_data, 0, sizeof(card_dynamic_slot_t));
2095 for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
2097 card->dynamic_slot_data.from_host_max_slots[i] = card->config_data.num_fromhost_data_slots -
2098 UNIFI_RESERVED_COMMAND_SLOTS;
2099 card->dynamic_slot_data.queue_stable[i] = FALSE;
2102 card->dynamic_slot_data.packets_interval = UNIFI_PACKETS_INTERVAL;
2105 return CSR_RESULT_SUCCESS;
2106 } /* card_init_slots() */
2110 * ---------------------------------------------------------------------------
2111 * unifi_set_udi_hook
2113 * Registers the udi hook that reports the sent signals to the core.
2116 * card Pointer to the card context struct
2117 * udi_fn Pointer to the callback function.
2120 * CSR_WIFI_HIP_RESULT_INVALID_VALUE if the card pointer is invalid,
2121 * CSR_RESULT_SUCCESS on success.
2122 * ---------------------------------------------------------------------------
2124 CsrResult unifi_set_udi_hook(card_t *card, udi_func_t udi_fn)
2128 return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
2131 if (card->udi_hook == NULL)
2133 card->udi_hook = udi_fn;
2136 return CSR_RESULT_SUCCESS;
2137 } /* unifi_set_udi_hook() */
2141 * ---------------------------------------------------------------------------
2142 * unifi_remove_udi_hook
2144 * Removes the udi hook that reports the sent signals from the core.
2147 * card Pointer to the card context struct
2148 * udi_fn Pointer to the callback function.
2151 * CSR_WIFI_HIP_RESULT_INVALID_VALUE if the card pointer is invalid,
2152 * CSR_RESULT_SUCCESS on success.
2153 * ---------------------------------------------------------------------------
2155 CsrResult unifi_remove_udi_hook(card_t *card, udi_func_t udi_fn)
2159 return CSR_WIFI_HIP_RESULT_INVALID_VALUE;
2162 if (card->udi_hook == udi_fn)
2164 card->udi_hook = NULL;
2167 return CSR_RESULT_SUCCESS;
2168 } /* unifi_remove_udi_hook() */
2171 static void CardReassignDynamicReservation(card_t *card)
2177 unifi_trace(card->ospriv, UDBG5, "Packets Txed %d %d %d %d\n",
2178 card->dynamic_slot_data.packets_txed[0],
2179 card->dynamic_slot_data.packets_txed[1],
2180 card->dynamic_slot_data.packets_txed[2],
2181 card->dynamic_slot_data.packets_txed[3]);
2183 /* Clear reservation and recalculate max slots */
2184 for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
2186 card->dynamic_slot_data.queue_stable[i] = FALSE;
2187 card->dynamic_slot_data.from_host_reserved_slots[i] = 0;
2188 card->dynamic_slot_data.from_host_max_slots[i] = card->config_data.num_fromhost_data_slots -
2189 UNIFI_RESERVED_COMMAND_SLOTS;
2190 card->dynamic_slot_data.packets_txed[i] = 0;
2192 unifi_trace(card->ospriv, UDBG5, "CardReassignDynamicReservation: queue %d reserved %d Max %d\n", i,
2193 card->dynamic_slot_data.from_host_reserved_slots[i],
2194 card->dynamic_slot_data.from_host_max_slots[i]);
2197 card->dynamic_slot_data.total_packets_txed = 0;
2202 /* Algorithm to dynamically reserve slots. The logic is based mainly on the outstanding queue
2203 * length. Slots are reserved for particular queues during an interval and cleared after the interval.
2204 * Each queue has three associated variables.. a) used slots - the number of slots currently occupied
2205 * by the queue b) reserved slots - number of slots reserved specifically for the queue c) max slots - total
2206 * slots that this queue can actually use (may be higher than reserved slots and is dependent on reserved slots
2207 * for other queues).
2208 * This function is called when there are no slots available for a queue. It checks to see if there are enough
2209 * unreserved slots sufficient for this request. If available these slots are reserved for the queue.
2210 * If there are not enough unreserved slots, a fair share for each queue is calculated based on the total slots
2211 * and the number of active queues (any queue with existing reservation is considered active). Queues needing
2212 * less than their fair share are allowed to have the previously reserved slots. The remaining slots are
2213 * distributed evenly among queues that need more than the fair share
2215 * A better scheme would take current bandwidth per AC into consideration when reserving slots. An
2216 * implementation scheme could consider the relative time/service period for slots in an AC. If the firmware
2217 * services other ACs faster than a particular AC (packets wait in the slots longer) then it is fair to reserve
2218 * less slots for the AC
2220 static void CardCheckDynamicReservation(card_t *card, unifi_TrafficQueue queue)
2222 CsrUint16 q_len, active_queues = 0, excess_queue_slots, div_extra_slots,
2223 queue_fair_share, reserved_slots = 0, q, excess_need_queues = 0, unmovable_slots = 0;
2226 CsrUint16 num_data_slots = card->config_data.num_fromhost_data_slots - UNIFI_RESERVED_COMMAND_SLOTS;
2230 /* Calculate the pending queue length */
2231 sigq = &card->fh_traffic_queue[queue];
2232 q_len = CSR_WIFI_HIP_Q_SLOTS_USED(sigq);
2234 if (q_len <= card->dynamic_slot_data.from_host_reserved_slots[queue])
2236 unifi_trace(card->ospriv, UDBG5, "queue %d q_len %d already has that many reserved slots, exiting\n", queue, q_len);
2242 if (q_len > num_data_slots)
2244 q_len = num_data_slots;
2247 for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
2249 if (i != (CsrInt32)queue)
2251 reserved_slots += card->dynamic_slot_data.from_host_reserved_slots[i];
2253 if ((i == (CsrInt32)queue) || (card->dynamic_slot_data.from_host_reserved_slots[i] > 0))
2259 unifi_trace(card->ospriv, UDBG5, "CardCheckDynamicReservation: queue %d q_len %d\n", queue, q_len);
2260 unifi_trace(card->ospriv, UDBG5, "Active queues %d reserved slots on other queues %d\n",
2261 active_queues, reserved_slots);
2263 if (reserved_slots + q_len <= num_data_slots)
2265 card->dynamic_slot_data.from_host_reserved_slots[queue] = q_len;
2266 if (q_len == num_data_slots)
2268 /* This is the common case when just 1 stream is going */
2269 card->dynamic_slot_data.queue_stable[queue] = TRUE;
2274 queue_fair_share = num_data_slots / active_queues;
2275 unifi_trace(card->ospriv, UDBG5, "queue fair share %d\n", queue_fair_share);
2277 /* Evenly distribute slots among active queues */
2278 /* Find out the queues that need excess of fair share. Also find slots allocated
2279 * to queues less than their fair share, these slots cannot be reallocated (unmovable slots) */
2281 card->dynamic_slot_data.from_host_reserved_slots[queue] = q_len;
2283 for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
2285 if (card->dynamic_slot_data.from_host_reserved_slots[i] > queue_fair_share)
2287 excess_need_queues++;
2291 unmovable_slots += card->dynamic_slot_data.from_host_reserved_slots[i];
2295 unifi_trace(card->ospriv, UDBG5, "Excess need queues %d\n", excess_need_queues);
2297 /* Now find the slots per excess demand queue */
2298 excess_queue_slots = (num_data_slots - unmovable_slots) / excess_need_queues;
2299 div_extra_slots = (num_data_slots - unmovable_slots) - excess_queue_slots * excess_need_queues;
2300 for (i = UNIFI_NO_OF_TX_QS - 1; i >= 0; i--)
2302 if (card->dynamic_slot_data.from_host_reserved_slots[i] > excess_queue_slots)
2304 card->dynamic_slot_data.from_host_reserved_slots[i] = excess_queue_slots;
2305 if (div_extra_slots > 0)
2307 card->dynamic_slot_data.from_host_reserved_slots[i]++;
2310 /* No more slots will be allocated to this queue during the current interval */
2311 card->dynamic_slot_data.queue_stable[i] = TRUE;
2312 unifi_trace(card->ospriv, UDBG5, "queue stable %d\n", i);
2317 /* Redistribute max slots */
2318 for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
2321 for (q = 0; q < UNIFI_NO_OF_TX_QS; q++)
2325 reserved_slots += card->dynamic_slot_data.from_host_reserved_slots[q];
2329 card->dynamic_slot_data.from_host_max_slots[i] = num_data_slots - reserved_slots;
2330 unifi_trace(card->ospriv, UDBG5, "queue %d reserved %d Max %d\n", i,
2331 card->dynamic_slot_data.from_host_reserved_slots[i],
2332 card->dynamic_slot_data.from_host_max_slots[i]);
2340 * ---------------------------------------------------------------------------
2341 * CardClearFromHostDataSlot
2343 * Clear a the given data slot, making it available again.
2346 * card Pointer to Card object
2347 * slot Index of the signal slot to clear.
2351 * ---------------------------------------------------------------------------
2353 void CardClearFromHostDataSlot(card_t *card, const CsrInt16 slot)
2355 CsrUint8 queue = card->from_host_data[slot].queue;
2356 const void *os_data_ptr = card->from_host_data[slot].bd.os_data_ptr;
2360 if (card->from_host_data[slot].bd.data_length == 0)
2362 unifi_warning(card->ospriv,
2363 "Surprise: request to clear an already free FH data slot: %d\n",
2369 if (os_data_ptr == NULL)
2371 unifi_warning(card->ospriv,
2372 "Clearing FH data slot %d: has null payload, len=%d\n",
2373 slot, card->from_host_data[slot].bd.data_length);
2376 /* Free card->from_host_data[slot].bd.os_net_ptr here. */
2377 /* Mark slot as free by setting length to 0. */
2378 unifi_free_bulk_data(card, &card->from_host_data[slot].bd);
2379 if (queue < UNIFI_NO_OF_TX_QS)
2381 if (card->dynamic_slot_data.from_host_used_slots[queue] == 0)
2383 unifi_error(card->ospriv, "Goofed up used slots q = %d used slots = %d\n",
2385 card->dynamic_slot_data.from_host_used_slots[queue]);
2389 card->dynamic_slot_data.from_host_used_slots[queue]--;
2391 card->dynamic_slot_data.packets_txed[queue]++;
2392 card->dynamic_slot_data.total_packets_txed++;
2393 if (card->dynamic_slot_data.total_packets_txed >= card->dynamic_slot_data.packets_interval)
2395 CardReassignDynamicReservation(card);
2399 unifi_trace(card->ospriv, UDBG4, "CardClearFromHostDataSlot: slot %d recycled %p\n", slot, os_data_ptr);
2402 } /* CardClearFromHostDataSlot() */
2405 #ifdef CSR_WIFI_REQUEUE_PACKET_TO_HAL
2407 * ---------------------------------------------------------------------------
2408 * CardClearFromHostDataSlotWithoutFreeingBulkData
2410 * Clear the given data slot with out freeing the bulk data.
2413 * card Pointer to Card object
2414 * slot Index of the signal slot to clear.
2418 * ---------------------------------------------------------------------------
2420 void CardClearFromHostDataSlotWithoutFreeingBulkData(card_t *card, const CsrInt16 slot)
2422 CsrUint8 queue = card->from_host_data[slot].queue;
2424 /* Initialise the from_host data slot so it can be re-used,
2425 * Set length field in from_host_data array to 0.
2427 UNIFI_INIT_BULK_DATA(&card->from_host_data[slot].bd);
2429 queue = card->from_host_data[slot].queue;
2431 if (queue < UNIFI_NO_OF_TX_QS)
2433 if (card->dynamic_slot_data.from_host_used_slots[queue] == 0)
2435 unifi_error(card->ospriv, "Goofed up used slots q = %d used slots = %d\n",
2437 card->dynamic_slot_data.from_host_used_slots[queue]);
2441 card->dynamic_slot_data.from_host_used_slots[queue]--;
2443 card->dynamic_slot_data.packets_txed[queue]++;
2444 card->dynamic_slot_data.total_packets_txed++;
2445 if (card->dynamic_slot_data.total_packets_txed >=
2446 card->dynamic_slot_data.packets_interval)
2448 CardReassignDynamicReservation(card);
2451 } /* CardClearFromHostDataSlotWithoutFreeingBulkData() */
2456 CsrUint16 CardGetDataSlotSize(card_t *card)
2458 return card->config_data.data_slot_size;
2459 } /* CardGetDataSlotSize() */
2463 * ---------------------------------------------------------------------------
2464 * CardGetFreeFromHostDataSlots
2466 * Retrieve the number of from-host bulk data slots available.
2469 * card Pointer to the card context struct
2472 * Number of free from-host bulk data slots.
2473 * ---------------------------------------------------------------------------
2475 CsrUint16 CardGetFreeFromHostDataSlots(card_t *card)
2481 /* First two slots reserved for MLME */
2482 for (i = 0; i < card->config_data.num_fromhost_data_slots; i++)
2484 if (card->from_host_data[i].bd.data_length == 0)
2493 } /* CardGetFreeFromHostDataSlots() */
2497 * ---------------------------------------------------------------------------
2498 * CardAreAllFromHostDataSlotsEmpty
2500 * Returns the state of from-host bulk data slots.
2503 * card Pointer to the card context struct
2506 * 1 The from-host bulk data slots are all empty (available).
2507 * 0 Some or all the from-host bulk data slots are in use.
2508 * ---------------------------------------------------------------------------
2510 CsrUint16 CardAreAllFromHostDataSlotsEmpty(card_t *card)
2514 for (i = 0; i < card->config_data.num_fromhost_data_slots; i++)
2516 if (card->from_host_data[i].bd.data_length != 0)
2523 } /* CardGetFreeFromHostDataSlots() */
2526 static CsrResult unifi_identify_hw(card_t *card)
2530 card->chip_id = card->sdio_if->sdioId.cardId;
2531 card->function = card->sdio_if->sdioId.sdioFunction;
2532 card->sdio_io_block_size = card->sdio_if->blockSize;
2534 /* If SDIO controller doesn't support byte mode CMD53, pad transfers to block sizes */
2535 card->sdio_io_block_pad = (card->sdio_if->features & CSR_SDIO_FEATURE_BYTE_MODE)?FALSE : TRUE;
2538 * Setup the chip helper so that we can access the registers (and
2539 * also tell what sub-type of HIP we should use).
2541 card->helper = ChipHelper_GetVersionSdio((CsrUint8)card->chip_id);
2544 unifi_error(card->ospriv, "Null ChipHelper\n");
2547 unifi_info(card->ospriv, "Chip ID 0x%02X Function %u Block Size %u Name %s(%s)\n",
2548 card->chip_id, card->function, card->sdio_io_block_size,
2549 ChipHelper_MarketingName(card->helper),
2550 ChipHelper_FriendlyName(card->helper));
2553 return CSR_RESULT_SUCCESS;
2554 } /* unifi_identify_hw() */
2557 static CsrResult unifi_prepare_hw(card_t *card)
2560 CsrResult csrResult;
2561 enum unifi_host_state old_state = card->host_state;
2565 r = unifi_identify_hw(card);
2566 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2570 if (r != CSR_RESULT_SUCCESS)
2572 unifi_error(card->ospriv, "Failed to identify hw\n");
2577 unifi_trace(card->ospriv, UDBG1,
2578 "%s mode SDIO\n", card->sdio_io_block_pad?"Block" : "Byte");
2580 * Chip must be a awake or blocks that are asleep may not get
2581 * reset. We can only do this after we have read the chip_id.
2583 r = unifi_set_host_state(card, UNIFI_HOST_STATE_AWAKE);
2584 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2589 if (old_state == UNIFI_HOST_STATE_TORPID)
2591 /* Ensure the initial clock rate is set; if a reset occured when the chip was
2592 * TORPID, unifi_set_host_state() may have raised it to MAX.
2594 csrResult = CsrSdioMaxBusClockFrequencySet(card->sdio_if, UNIFI_SDIO_CLOCK_INIT_HZ);
2595 if (csrResult != CSR_RESULT_SUCCESS)
2597 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
2601 card->sdio_clock_speed = UNIFI_SDIO_CLOCK_INIT_HZ;
2605 * The WLAN function must be enabled to access MAILBOX2 and DEBUG_RST
2608 csrResult = CsrSdioFunctionEnable(card->sdio_if);
2609 if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
2611 return CSR_WIFI_HIP_RESULT_NO_DEVICE;
2613 if (csrResult != CSR_RESULT_SUCCESS)
2615 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
2616 /* Can't enable WLAN function. Try resetting the SDIO block. */
2617 unifi_error(card->ospriv, "Failed to re-enable function %d.\n", card->function);
2623 * Poke some registers to make sure the PLL has started,
2624 * otherwise memory accesses are likely to fail.
2626 bootstrap_chip_hw(card);
2628 /* Try to read the chip version from register. */
2629 r = unifi_read_chip_version(card);
2630 if (r != CSR_RESULT_SUCCESS)
2637 return CSR_RESULT_SUCCESS;
2638 } /* unifi_prepare_hw() */
2641 static CsrResult unifi_read_chip_version(card_t *card)
2643 CsrUint32 gbl_chip_version;
2649 gbl_chip_version = ChipHelper_GBL_CHIP_VERSION(card->helper);
2651 /* Try to read the chip version from register. */
2652 if (gbl_chip_version != 0)
2654 r = unifi_read_direct16(card, gbl_chip_version * 2, &ver);
2655 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2659 if (r != CSR_RESULT_SUCCESS)
2661 unifi_error(card->ospriv, "Failed to read GBL_CHIP_VERSION\n");
2665 card->chip_version = ver;
2669 unifi_info(card->ospriv, "Unknown Chip ID, cannot locate GBL_CHIP_VERSION\n");
2670 r = CSR_RESULT_FAILURE;
2673 unifi_info(card->ospriv, "Chip Version 0x%04X\n", card->chip_version);
2677 } /* unifi_read_chip_version() */
2681 * ---------------------------------------------------------------------------
2682 * unifi_reset_hardware
2684 * Execute the UniFi reset sequence.
2686 * Note: This may fail if the chip is going TORPID so retry at
2690 * card - pointer to card context structure
2693 * CSR_RESULT_SUCCESS on success, CSR error otherwise.
2696 * Some platforms (e.g. Windows Vista) do not allow access to registers
2697 * that are necessary for a software soft reset.
2698 * ---------------------------------------------------------------------------
2700 static CsrResult unifi_reset_hardware(card_t *card)
2703 CsrUint16 new_block_size = UNIFI_IO_BLOCK_SIZE;
2704 CsrResult csrResult;
2708 /* Errors returned by unifi_prepare_hw() are not critical at this point */
2709 r = unifi_prepare_hw(card);
2710 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2715 /* First try SDIO controller reset, which may power cycle the UniFi, assert
2716 * its reset line, or not be implemented depending on the platform.
2718 unifi_info(card->ospriv, "Calling CsrSdioHardReset\n");
2719 csrResult = CsrSdioHardReset(card->sdio_if);
2720 if (csrResult == CSR_RESULT_SUCCESS)
2722 unifi_info(card->ospriv, "CsrSdioHardReset succeeded on reseting UniFi\n");
2723 r = unifi_prepare_hw(card);
2724 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2728 if (r != CSR_RESULT_SUCCESS)
2730 unifi_error(card->ospriv, "unifi_prepare_hw failed after hard reset\n");
2735 else if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
2737 return CSR_WIFI_HIP_RESULT_NO_DEVICE;
2741 /* Falling back to software hard reset methods */
2742 unifi_info(card->ospriv, "Falling back to software hard reset\n");
2743 r = unifi_card_hard_reset(card);
2744 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2748 if (r != CSR_RESULT_SUCCESS)
2750 unifi_error(card->ospriv, "software hard reset failed\n");
2755 /* If we fell back to unifi_card_hard_reset() methods, chip version may
2756 * not have been read. (Note in the unlikely event that it is zero,
2757 * it will be harmlessly read again)
2759 if (card->chip_version == 0)
2761 r = unifi_read_chip_version(card);
2762 if (r != CSR_RESULT_SUCCESS)
2770 #ifdef CSR_WIFI_HIP_SDIO_BLOCK_SIZE
2771 new_block_size = CSR_WIFI_HIP_SDIO_BLOCK_SIZE;
2774 /* After hard reset, we need to restore the SDIO block size */
2775 csrResult = CsrSdioBlockSizeSet(card->sdio_if, new_block_size);
2776 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
2778 /* Warn if a different block size was achieved by the transport */
2779 if (card->sdio_if->blockSize != new_block_size)
2781 unifi_info(card->ospriv,
2782 "Actually got block size %d\n", card->sdio_if->blockSize);
2785 /* sdio_io_block_size always needs be updated from the achieved block size,
2786 * as it is used by the OS layer to allocate memory in unifi_net_malloc().
2787 * Controllers which don't support block mode (e.g. CSPI) will report a
2788 * block size of zero.
2790 if (card->sdio_if->blockSize == 0)
2792 unifi_info(card->ospriv, "Block size 0, block mode not available\n");
2794 /* Set sdio_io_block_size to 1 so that unifi_net_data_malloc() has a
2795 * sensible rounding value. Elsewhere padding will already be
2796 * disabled because the controller supports byte mode.
2798 card->sdio_io_block_size = 1;
2800 /* Controller features must declare support for byte mode */
2801 if (!(card->sdio_if->features & CSR_SDIO_FEATURE_BYTE_MODE))
2803 unifi_error(card->ospriv, "Requires byte mode\n");
2804 r = CSR_WIFI_HIP_RESULT_INVALID_VALUE;
2809 /* Padding will be enabled if CSR_SDIO_FEATURE_BYTE_MODE isn't set */
2810 card->sdio_io_block_size = card->sdio_if->blockSize;
2816 } /* unifi_reset_hardware() */
2820 * ---------------------------------------------------------------------------
2821 * card_reset_method_io_enable
2823 * Issue a hard reset to the hw writing the IO_ENABLE.
2826 * card Pointer to Card object
2830 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
2831 * CSR_RESULT_FAILURE if an SDIO error occurred or if a response
2832 * was not seen in the expected time
2833 * ---------------------------------------------------------------------------
2835 static CsrResult card_reset_method_io_enable(card_t *card)
2838 CsrResult csrResult;
2843 * This resets only function 1, so should be used in
2844 * preference to the method below (CSR_FUNC_EN)
2846 unifi_trace(card->ospriv, UDBG1, "Hard reset (IO_ENABLE)\n");
2848 csrResult = CsrSdioFunctionDisable(card->sdio_if);
2849 if (csrResult == CSR_SDIO_RESULT_NO_DEVICE)
2851 return CSR_WIFI_HIP_RESULT_NO_DEVICE;
2853 if (csrResult != CSR_RESULT_SUCCESS)
2855 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
2856 unifi_warning(card->ospriv, "SDIO error writing IO_ENABLE: %d\n", r);
2860 /* Delay here to let the reset take affect. */
2861 CsrThreadSleep(RESET_SETTLE_DELAY);
2863 r = card_wait_for_unifi_to_disable(card);
2864 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2869 if (r == CSR_RESULT_SUCCESS)
2871 r = card_wait_for_unifi_to_reset(card);
2872 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2879 if (r != CSR_RESULT_SUCCESS)
2881 unifi_trace(card->ospriv, UDBG1, "Hard reset (CSR_FUNC_EN)\n");
2883 r = sdio_write_f0(card, SDIO_CSR_FUNC_EN, 0);
2884 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2888 if (r != CSR_RESULT_SUCCESS)
2890 unifi_warning(card->ospriv, "SDIO error writing SDIO_CSR_FUNC_EN: %d\n", r);
2896 /* Delay here to let the reset take affect. */
2897 CsrThreadSleep(RESET_SETTLE_DELAY);
2899 r = card_wait_for_unifi_to_reset(card);
2900 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2907 if (r != CSR_RESULT_SUCCESS)
2909 unifi_warning(card->ospriv, "card_reset_method_io_enable failed to reset UniFi\n");
2914 } /* card_reset_method_io_enable() */
2918 * ---------------------------------------------------------------------------
2919 * card_reset_method_dbg_reset
2921 * Issue a hard reset to the hw writing the DBG_RESET.
2924 * card Pointer to Card object
2927 * CSR_RESULT_SUCCESS on success,
2928 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
2929 * CSR_RESULT_FAILURE if an SDIO error occurred or if a response
2930 * was not seen in the expected time
2931 * ---------------------------------------------------------------------------
2933 static CsrResult card_reset_method_dbg_reset(card_t *card)
2940 * Prepare UniFi for h/w reset
2942 if (card->host_state == UNIFI_HOST_STATE_TORPID)
2944 r = unifi_set_host_state(card, UNIFI_HOST_STATE_DROWSY);
2945 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2949 if (r != CSR_RESULT_SUCCESS)
2951 unifi_error(card->ospriv, "Failed to set UNIFI_HOST_STATE_DROWSY\n");
2958 r = unifi_card_stop_processor(card, UNIFI_PROC_BOTH);
2959 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2963 if (r != CSR_RESULT_SUCCESS)
2965 unifi_error(card->ospriv, "Can't stop processors\n");
2970 unifi_trace(card->ospriv, UDBG1, "Hard reset (DBG_RESET)\n");
2973 * This register write may fail. The debug reset resets
2974 * parts of the Function 0 sections of the chip, and
2975 * therefore the response cannot be sent back to the host.
2977 r = unifi_write_direct_8_or_16(card, ChipHelper_DBG_RESET(card->helper) * 2, 1);
2978 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2982 if (r != CSR_RESULT_SUCCESS)
2984 unifi_warning(card->ospriv, "SDIO error writing DBG_RESET: %d\n", r);
2989 /* Delay here to let the reset take affect. */
2990 CsrThreadSleep(RESET_SETTLE_DELAY);
2992 r = card_wait_for_unifi_to_reset(card);
2993 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
2997 if (r != CSR_RESULT_SUCCESS)
2999 unifi_warning(card->ospriv, "card_reset_method_dbg_reset failed to reset UniFi\n");
3004 } /* card_reset_method_dbg_reset() */
3008 * ---------------------------------------------------------------------------
3009 * unifi_card_hard_reset
3011 * Issue reset to hardware, by writing to registers on the card.
3012 * Power to the card is preserved.
3015 * card Pointer to Card object
3018 * CSR_RESULT_SUCCESS on success,
3019 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3020 * CSR_RESULT_FAILURE if an SDIO error occurred or if a response
3021 * was not seen in the expected time
3022 * ---------------------------------------------------------------------------
3024 CsrResult unifi_card_hard_reset(card_t *card)
3027 const struct chip_helper_reset_values *init_data;
3032 /* Clear cache of page registers */
3033 card->proc_select = (CsrUint32)(-1);
3034 card->dmem_page = (CsrUint32)(-1);
3035 card->pmem_page = (CsrUint32)(-1);
3038 * We need to have a valid card->helper before we use software hard reset.
3039 * If unifi_identify_hw() fails to get the card ID, it probably means
3040 * that there is no way to talk to the h/w.
3042 r = unifi_identify_hw(card);
3043 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3047 if (r != CSR_RESULT_SUCCESS)
3049 unifi_error(card->ospriv, "unifi_card_hard_reset failed to identify h/w\n");
3054 /* Search for some reset code. */
3055 chunks = ChipHelper_HostResetSequence(card->helper, &init_data);
3058 unifi_error(card->ospriv,
3059 "Hard reset (Code download) is unsupported\n");
3061 func_exit_r(CSR_RESULT_FAILURE);
3062 return CSR_RESULT_FAILURE;
3065 if (card->chip_id > SDIO_CARD_ID_UNIFI_2)
3067 /* The HIP spec considers this a bus-specific reset.
3068 * This resets only function 1, so should be used in
3069 * preference to the method below (CSR_FUNC_EN)
3070 * If this method fails, it means that the f/w is probably
3071 * not running. In this case, try the DBG_RESET method.
3073 r = card_reset_method_io_enable(card);
3074 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3078 if (r == CSR_RESULT_SUCCESS)
3085 /* Software hard reset */
3086 r = card_reset_method_dbg_reset(card);
3090 } /* unifi_card_hard_reset() */
3094 * ---------------------------------------------------------------------------
3099 * This function causes an internal interrupt to be raised in the
3100 * UniFi chip. It is used to signal the firmware that some action has
3102 * The UniFi Host Interface asks that the value used increments for
3103 * debugging purposes.
3106 * card Pointer to Card object
3109 * CSR_RESULT_SUCCESS on success,
3110 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3111 * CSR_RESULT_FAILURE if an SDIO error occurred or if a response
3112 * was not seen in the expected time
3113 * ---------------------------------------------------------------------------
3115 CsrResult CardGenInt(card_t *card)
3121 if (card->chip_id > SDIO_CARD_ID_UNIFI_2)
3123 r = sdio_write_f0(card, SDIO_CSR_FROM_HOST_SCRATCH0,
3124 (CsrUint8)card->unifi_interrupt_seq);
3128 r = unifi_write_direct_8_or_16(card,
3129 ChipHelper_SHARED_IO_INTERRUPT(card->helper) * 2,
3130 (CsrUint8)card->unifi_interrupt_seq);
3132 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3136 if (r != CSR_RESULT_SUCCESS)
3138 unifi_error(card->ospriv, "SDIO error writing UNIFI_SHARED_IO_INTERRUPT: %d\n", r);
3143 card->unifi_interrupt_seq++;
3146 return CSR_RESULT_SUCCESS;
3147 } /* CardGenInt() */
3151 * ---------------------------------------------------------------------------
3154 * Enable the outgoing SDIO interrupt from UniFi to the host.
3157 * card Pointer to Card object
3160 * CSR_RESULT_SUCCESS on success,
3161 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3162 * CSR_RESULT_FAILURE if an SDIO error occurred,
3163 * ---------------------------------------------------------------------------
3165 CsrResult CardEnableInt(card_t *card)
3168 CsrUint8 int_enable;
3170 r = sdio_read_f0(card, SDIO_INT_ENABLE, &int_enable);
3171 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3175 if (r != CSR_RESULT_SUCCESS)
3177 unifi_error(card->ospriv, "SDIO error reading SDIO_INT_ENABLE\n");
3181 int_enable |= (1 << card->function) | UNIFI_SD_INT_ENABLE_IENM;
3183 r = sdio_write_f0(card, SDIO_INT_ENABLE, int_enable);
3184 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3188 if (r != CSR_RESULT_SUCCESS)
3190 unifi_error(card->ospriv, "SDIO error writing SDIO_INT_ENABLE\n");
3194 return CSR_RESULT_SUCCESS;
3195 } /* CardEnableInt() */
3199 * ---------------------------------------------------------------------------
3202 * Disable the outgoing SDIO interrupt from UniFi to the host.
3205 * card Pointer to Card object
3208 * CSR_RESULT_SUCCESS on success,
3209 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3210 * CSR_RESULT_FAILURE if an SDIO error occurred,
3211 * ---------------------------------------------------------------------------
3213 CsrResult CardDisableInt(card_t *card)
3216 CsrUint8 int_enable;
3218 r = sdio_read_f0(card, SDIO_INT_ENABLE, &int_enable);
3219 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3223 if (r != CSR_RESULT_SUCCESS)
3225 unifi_error(card->ospriv, "SDIO error reading SDIO_INT_ENABLE\n");
3229 int_enable &= ~(1 << card->function);
3231 r = sdio_write_f0(card, SDIO_INT_ENABLE, int_enable);
3232 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3236 if (r != CSR_RESULT_SUCCESS)
3238 unifi_error(card->ospriv, "SDIO error writing SDIO_INT_ENABLE\n");
3242 return CSR_RESULT_SUCCESS;
3243 } /* CardDisableInt() */
3247 * ---------------------------------------------------------------------------
3250 * Determine whether UniFi is currently asserting the SDIO interrupt
3254 * card Pointer to Card object
3255 * pintr Pointer to location to write interrupt status,
3256 * TRUE if interrupt pending,
3257 * FALSE if no interrupt pending.
3259 * CSR_RESULT_SUCCESS interrupt status read successfully
3260 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3261 * CSR_RESULT_FAILURE if an SDIO error occurred,
3262 * ---------------------------------------------------------------------------
3264 CsrResult CardPendingInt(card_t *card, CsrBool *pintr)
3271 r = sdio_read_f0(card, SDIO_INT_PENDING, &pending);
3272 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3276 if (r != CSR_RESULT_SUCCESS)
3278 unifi_error(card->ospriv, "SDIO error reading SDIO_INT_PENDING\n");
3282 *pintr = (pending & (1 << card->function))?TRUE : FALSE;
3284 return CSR_RESULT_SUCCESS;
3285 } /* CardPendingInt() */
3289 * ---------------------------------------------------------------------------
3292 * Clear the UniFi SDIO interrupt request.
3295 * card Pointer to Card object
3298 * CSR_RESULT_SUCCESS if pending interrupt was cleared, or no pending interrupt.
3299 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3300 * CSR_RESULT_FAILURE if an SDIO error occurred,
3301 * ---------------------------------------------------------------------------
3303 CsrResult CardClearInt(card_t *card)
3308 if (card->chip_id > SDIO_CARD_ID_UNIFI_2)
3310 /* CardPendingInt() sets intr, if there is a pending interrupt */
3311 r = CardPendingInt(card, &intr);
3317 r = sdio_write_f0(card, SDIO_CSR_HOST_INT_CLEAR, 1);
3318 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3322 if (r != CSR_RESULT_SUCCESS)
3324 unifi_error(card->ospriv, "SDIO error writing SDIO_CSR_HOST_INT_CLEAR\n");
3329 r = unifi_write_direct_8_or_16(card,
3330 ChipHelper_SDIO_HOST_INT(card->helper) * 2,
3332 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3336 if (r != CSR_RESULT_SUCCESS)
3338 unifi_error(card->ospriv, "SDIO error writing UNIFI_SDIO_HOST_INT\n");
3343 } /* CardClearInt() */
3347 * ---------------------------------------------------------------------------
3350 * Determine whether UniFi is currently asserting the SDIO interrupt
3354 * card Pointer to Card object
3355 * enabled Pointer to location to write interrupt enable status,
3356 * TRUE if interrupts enabled,
3357 * FALSE if interupts disabled.
3360 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3361 * CSR_RESULT_FAILURE if an SDIO error occurred,
3362 * ---------------------------------------------------------------------------
3364 CsrResult CardIntEnabled(card_t *card, CsrBool *enabled)
3367 CsrUint8 int_enable;
3369 r = sdio_read_f0(card, SDIO_INT_ENABLE, &int_enable);
3370 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3374 if (r != CSR_RESULT_SUCCESS)
3376 unifi_error(card->ospriv, "SDIO error reading SDIO_INT_ENABLE\n");
3380 *enabled = (int_enable & (1 << card->function))?TRUE : FALSE;
3382 return CSR_RESULT_SUCCESS;
3383 } /* CardIntEnabled() */
3387 * ---------------------------------------------------------------------------
3389 * Allocate slot in the pending bulkdata arrays and assign it to a signal's
3390 * bulkdata reference. The slot is then ready for UniFi's bulkdata commands
3391 * to transfer the data to/from the host.
3394 * card Pointer to Card object
3395 * csptr Pending signal pointer, including bulkdata ref
3396 * queue Traffic queue that this signal is using
3399 * CSR_RESULT_SUCCESS if a free slot was assigned
3400 * CSR_RESULT_FAILURE if no slot was available
3401 * ---------------------------------------------------------------------------
3403 CsrResult CardWriteBulkData(card_t *card, card_signal_t *csptr, unifi_TrafficQueue queue)
3405 CsrUint16 i, slots[UNIFI_MAX_DATA_REFERENCES], j = 0;
3406 CsrUint8 *packed_sigptr, num_slots_required = 0;
3407 bulk_data_desc_t *bulkdata = csptr->bulkdata;
3412 /* Count the number of slots required */
3413 for (i = 0; i < UNIFI_MAX_DATA_REFERENCES; i++)
3415 if (bulkdata[i].data_length != 0)
3417 num_slots_required++;
3421 /* Get the slot numbers */
3422 if (num_slots_required != 0)
3424 /* Last 2 slots for MLME */
3425 if (queue == UNIFI_TRAFFIC_Q_MLME)
3427 h = card->config_data.num_fromhost_data_slots - UNIFI_RESERVED_COMMAND_SLOTS;
3428 for (i = 0; i < card->config_data.num_fromhost_data_slots; i++)
3430 if (card->from_host_data[h].bd.data_length == 0)
3432 /* Free data slot, claim it */
3434 if (j == num_slots_required)
3440 if (++h >= card->config_data.num_fromhost_data_slots)
3448 if (card->dynamic_slot_data.from_host_used_slots[queue]
3449 < card->dynamic_slot_data.from_host_max_slots[queue])
3451 /* Data commands get a free slot only after a few checks */
3452 nslots = card->config_data.num_fromhost_data_slots - UNIFI_RESERVED_COMMAND_SLOTS;
3454 h = card->from_host_data_head;
3456 for (i = 0; i < nslots; i++)
3458 if (card->from_host_data[h].bd.data_length == 0)
3460 /* Free data slot, claim it */
3462 if (j == num_slots_required)
3473 card->from_host_data_head = h;
3477 /* Required number of slots are not available, bail out */
3478 if (j != num_slots_required)
3480 unifi_trace(card->ospriv, UDBG5, "CardWriteBulkData: didn't find free slot/s\n");
3482 /* If we haven't already reached the stable state we can ask for reservation */
3483 if ((queue != UNIFI_TRAFFIC_Q_MLME) && (card->dynamic_slot_data.queue_stable[queue] == FALSE))
3485 CardCheckDynamicReservation(card, queue);
3488 for (i = 0; i < card->config_data.num_fromhost_data_slots; i++)
3490 unifi_trace(card->ospriv, UDBG5, "fh data slot %d: %d\n", i, card->from_host_data[i].bd.data_length);
3493 return CSR_RESULT_FAILURE;
3497 packed_sigptr = csptr->sigbuf;
3499 /* Fill in the slots with data */
3501 for (i = 0; i < UNIFI_MAX_DATA_REFERENCES; i++)
3503 if (bulkdata[i].data_length == 0)
3505 /* Zero-out the DATAREF in the signal */
3506 SET_PACKED_DATAREF_SLOT(packed_sigptr, i, 0);
3507 SET_PACKED_DATAREF_LEN(packed_sigptr, i, 0);
3512 * Fill in the slot number in the SIGNAL structure but
3513 * preserve the offset already in there
3515 SET_PACKED_DATAREF_SLOT(packed_sigptr, i, slots[j] | (((CsrUint16)packed_sigptr[SIZEOF_SIGNAL_HEADER + (i * SIZEOF_DATAREF) + 1]) << 8));
3516 SET_PACKED_DATAREF_LEN(packed_sigptr, i, bulkdata[i].data_length);
3518 /* Do not copy the data, just store the information to them */
3519 card->from_host_data[slots[j]].bd.os_data_ptr = bulkdata[i].os_data_ptr;
3520 card->from_host_data[slots[j]].bd.os_net_buf_ptr = bulkdata[i].os_net_buf_ptr;
3521 card->from_host_data[slots[j]].bd.data_length = bulkdata[i].data_length;
3522 card->from_host_data[slots[j]].bd.net_buf_length = bulkdata[i].net_buf_length;
3523 card->from_host_data[slots[j]].queue = queue;
3525 unifi_trace(card->ospriv, UDBG4, "CardWriteBulkData sig=0x%x, fh slot %d = %p\n",
3526 GET_SIGNAL_ID(packed_sigptr), i, bulkdata[i].os_data_ptr);
3528 /* Sanity-check that the bulk data desc being assigned to the slot
3529 * actually has a payload.
3531 if (!bulkdata[i].os_data_ptr)
3533 unifi_error(card->ospriv, "Assign null os_data_ptr (len=%d) fh slot %d, i=%d, q=%d, sig=0x%x",
3534 bulkdata[i].data_length, slots[j], i, queue, GET_SIGNAL_ID(packed_sigptr));
3538 if (queue < UNIFI_NO_OF_TX_QS)
3540 card->dynamic_slot_data.from_host_used_slots[queue]++;
3547 return CSR_RESULT_SUCCESS;
3548 } /* CardWriteBulkData() */
3552 * ---------------------------------------------------------------------------
3553 * card_find_data_slot
3555 * Dereference references to bulk data slots into pointers to real data.
3558 * card Pointer to the card struct.
3559 * slot Slot number from a signal structure
3562 * Pointer to entry in bulk_data_slot array.
3563 * ---------------------------------------------------------------------------
3565 bulk_data_desc_t* card_find_data_slot(card_t *card, CsrInt16 slot)
3568 bulk_data_desc_t *bd;
3572 /* ?? check sanity of slot number ?? */
3574 if (slot & SLOT_DIR_TO_HOST)
3576 bd = &card->to_host_data[sn];
3580 bd = &card->from_host_data[sn].bd;
3584 } /* card_find_data_slot() */
3588 * ---------------------------------------------------------------------------
3589 * firmware_present_in_flash
3591 * Probe for external Flash that looks like it might contain firmware.
3593 * If Flash is not present, reads always return 0x0008.
3594 * If Flash is present, but empty, reads return 0xFFFF.
3595 * Anything else is considered to be firmware.
3598 * card Pointer to card struct
3601 * CSR_RESULT_SUCCESS firmware is present in ROM or flash
3602 * CSR_WIFI_HIP_RESULT_NOT_FOUND firmware is not present in ROM or flash
3603 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3604 * CSR_RESULT_FAILURE if an SDIO error occurred
3605 * ---------------------------------------------------------------------------
3607 static CsrResult firmware_present_in_flash(card_t *card)
3612 if (ChipHelper_HasRom(card->helper))
3614 return CSR_RESULT_SUCCESS;
3616 if (!ChipHelper_HasFlash(card->helper))
3618 return CSR_WIFI_HIP_RESULT_NOT_FOUND;
3622 * Examine the Flash locations that are the power-on default reset
3623 * vectors of the XAP processors.
3624 * These are words 1 and 5 in Flash.
3626 r = unifi_card_read16(card, UNIFI_MAKE_GP(EXT_FLASH, 2), &m1);
3627 if (r != CSR_RESULT_SUCCESS)
3632 r = unifi_card_read16(card, UNIFI_MAKE_GP(EXT_FLASH, 10), &m5);
3633 if (r != CSR_RESULT_SUCCESS)
3638 /* Check for uninitialised/missing flash */
3639 if ((m1 == 0x0008) || (m1 == 0xFFFF) ||
3640 (m1 == 0x0004) || (m5 == 0x0004) ||
3641 (m5 == 0x0008) || (m5 == 0xFFFF))
3643 return CSR_WIFI_HIP_RESULT_NOT_FOUND;
3646 return CSR_RESULT_SUCCESS;
3647 } /* firmware_present_in_flash() */
3651 * ---------------------------------------------------------------------------
3654 * Perform chip specific magic to "Get It Working" TM. This will
3655 * increase speed of PLLs in analogue and maybe enable some
3656 * on-chip regulators.
3659 * card Pointer to card struct
3663 * ---------------------------------------------------------------------------
3665 static void bootstrap_chip_hw(card_t *card)
3667 const struct chip_helper_init_values *vals;
3669 void *sdio = card->sdio_if;
3670 CsrResult csrResult;
3672 len = ChipHelper_ClockStartupSequence(card->helper, &vals);
3675 for (i = 0; i < len; i++)
3677 csrResult = CsrSdioWrite16(sdio, vals[i].addr * 2, vals[i].value);
3678 if (csrResult != CSR_RESULT_SUCCESS)
3680 unifi_warning(card->ospriv, "Failed to write bootstrap value %d\n", i);
3681 /* Might not be fatal */
3687 } /* bootstrap_chip_hw() */
3691 * ---------------------------------------------------------------------------
3692 * unifi_card_stop_processor
3694 * Stop the UniFi XAP processors.
3697 * card Pointer to card struct
3698 * which One of UNIFI_PROC_MAC, UNIFI_PROC_PHY, UNIFI_PROC_BOTH
3701 * CSR_RESULT_SUCCESS if successful, or CSR error code
3702 * ---------------------------------------------------------------------------
3704 CsrResult unifi_card_stop_processor(card_t *card, enum unifi_dbg_processors_select which)
3706 CsrResult r = CSR_RESULT_SUCCESS;
3708 CsrInt16 retry = 100;
3712 /* Select both XAPs */
3713 r = unifi_set_proc_select(card, which);
3714 if (r != CSR_RESULT_SUCCESS)
3719 /* Stop processors */
3720 r = unifi_write_direct16(card, ChipHelper_DBG_EMU_CMD(card->helper) * 2, 2);
3721 if (r != CSR_RESULT_SUCCESS)
3727 r = unifi_read_direct_8_or_16(card,
3728 ChipHelper_DBG_HOST_STOP_STATUS(card->helper) * 2,
3730 if (r != CSR_RESULT_SUCCESS)
3735 if ((status & 1) == 1)
3738 return CSR_RESULT_SUCCESS;
3741 /* Processors didn't stop, try again */
3744 if (r != CSR_RESULT_SUCCESS)
3746 /* An SDIO error occurred */
3747 unifi_error(card->ospriv, "Failed to stop processors: SDIO error\n");
3751 /* If we reach here, we didn't the status in time. */
3752 unifi_error(card->ospriv, "Failed to stop processors: timeout waiting for stopped status\n");
3753 r = CSR_RESULT_FAILURE;
3757 } /* unifi_card_stop_processor() */
3761 * ---------------------------------------------------------------------------
3762 * card_start_processor
3764 * Start the UniFi XAP processors.
3767 * card Pointer to card struct
3768 * which One of UNIFI_PROC_MAC, UNIFI_PROC_PHY, UNIFI_PROC_BOTH
3771 * CSR_RESULT_SUCCESS or CSR error code
3772 * ---------------------------------------------------------------------------
3774 CsrResult card_start_processor(card_t *card, enum unifi_dbg_processors_select which)
3778 /* Select both XAPs */
3779 r = unifi_set_proc_select(card, which);
3780 if (r != CSR_RESULT_SUCCESS)
3782 unifi_error(card->ospriv, "unifi_set_proc_select failed: %d.\n", r);
3787 r = unifi_write_direct_8_or_16(card,
3788 ChipHelper_DBG_EMU_CMD(card->helper) * 2, 8);
3789 if (r != CSR_RESULT_SUCCESS)
3794 r = unifi_write_direct_8_or_16(card,
3795 ChipHelper_DBG_EMU_CMD(card->helper) * 2, 0);
3796 if (r != CSR_RESULT_SUCCESS)
3801 return CSR_RESULT_SUCCESS;
3802 } /* card_start_processor() */
3806 * ---------------------------------------------------------------------------
3807 * unifi_set_interrupt_mode
3809 * Configure the interrupt processing mode used by the HIP
3812 * card Pointer to card struct
3813 * mode Interrupt mode to apply
3817 * ---------------------------------------------------------------------------
3819 void unifi_set_interrupt_mode(card_t *card, CsrUint32 mode)
3821 if (mode == CSR_WIFI_INTMODE_RUN_BH_ONCE)
3823 unifi_info(card->ospriv, "Scheduled interrupt mode");
3825 card->intmode = mode;
3826 } /* unifi_set_interrupt_mode() */
3830 * ---------------------------------------------------------------------------
3831 * unifi_start_processors
3833 * Start all UniFi XAP processors.
3836 * card Pointer to card struct
3839 * CSR_RESULT_SUCCESS on success, CSR error code on error
3840 * ---------------------------------------------------------------------------
3842 CsrResult unifi_start_processors(card_t *card)
3844 return card_start_processor(card, UNIFI_PROC_BOTH);
3845 } /* unifi_start_processors() */
3849 * ---------------------------------------------------------------------------
3850 * unifi_request_max_sdio_clock
3852 * Requests that the maximum SDIO clock rate is set at the next suitable
3853 * opportunity (e.g. when the BH next runs, so as not to interfere with
3854 * any current operation).
3857 * card Pointer to card struct
3861 * ---------------------------------------------------------------------------
3863 void unifi_request_max_sdio_clock(card_t *card)
3865 card->request_max_clock = 1;
3866 } /* unifi_request_max_sdio_clock() */
3870 * ---------------------------------------------------------------------------
3871 * unifi_set_host_state
3873 * Set the host deep-sleep state.
3875 * If transitioning to TORPID, the SDIO driver will be notified
3876 * that the SD bus will be unused (idle) and conversely, when
3877 * transitioning from TORPID that the bus will be used (active).
3880 * card Pointer to card struct
3881 * state New deep-sleep state.
3884 * CSR_RESULT_SUCCESS on success
3885 * CSR_WIFI_HIP_RESULT_NO_DEVICE if the card was ejected
3886 * CSR_RESULT_FAILURE if an SDIO error occurred
3889 * We need to reduce the SDIO clock speed before trying to wake up the
3890 * chip. Actually, in the implementation below we reduce the clock speed
3891 * not just before we try to wake up the chip, but when we put the chip to
3892 * deep sleep. This means that if the f/w wakes up on its' own, we waste
3893 * a reduce/increace cycle. However, trying to eliminate this overhead is
3894 * proved difficult, as the current state machine in the HIP lib does at
3895 * least a CMD52 to disable the interrupts before we configure the host
3897 * ---------------------------------------------------------------------------
3899 CsrResult unifi_set_host_state(card_t *card, enum unifi_host_state state)
3901 CsrResult r = CSR_RESULT_SUCCESS;
3902 CsrResult csrResult;
3903 static const CsrCharString *const states[] = {
3904 "AWAKE", "DROWSY", "TORPID"
3906 static const CsrUint8 state_csr_host_wakeup[] = {
3909 static const CsrUint8 state_io_abort[] = {
3913 unifi_trace(card->ospriv, UDBG4, "State %s to %s\n",
3914 states[card->host_state], states[state]);
3916 if (card->host_state == UNIFI_HOST_STATE_TORPID)
3918 CsrSdioFunctionActive(card->sdio_if);
3921 /* Write the new state to UniFi. */
3922 if (card->chip_id > SDIO_CARD_ID_UNIFI_2)
3924 r = sdio_write_f0(card, SDIO_CSR_HOST_WAKEUP,
3925 (CsrUint8)((card->function << 4) | state_csr_host_wakeup[state]));
3929 r = sdio_write_f0(card, SDIO_IO_ABORT, state_io_abort[state]);
3932 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
3936 if (r != CSR_RESULT_SUCCESS)
3938 unifi_error(card->ospriv, "Failed to write UniFi deep sleep state\n");
3943 * If the chip was in state TORPID then we can now increase
3944 * the maximum bus clock speed.
3946 if (card->host_state == UNIFI_HOST_STATE_TORPID)
3948 csrResult = CsrSdioMaxBusClockFrequencySet(card->sdio_if,
3949 UNIFI_SDIO_CLOCK_MAX_HZ);
3950 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
3951 /* Non-fatal error */
3952 if (r != CSR_RESULT_SUCCESS && r != CSR_WIFI_HIP_RESULT_NO_DEVICE)
3954 unifi_warning(card->ospriv,
3955 "Failed to increase the SDIO clock speed\n");
3959 card->sdio_clock_speed = UNIFI_SDIO_CLOCK_MAX_HZ;
3964 * Cache the current state in the card structure to avoid
3965 * unnecessary SDIO reads.
3967 card->host_state = state;
3969 if (state == UNIFI_HOST_STATE_TORPID)
3972 * If the chip is now in state TORPID then we must now decrease
3973 * the maximum bus clock speed.
3975 csrResult = CsrSdioMaxBusClockFrequencySet(card->sdio_if,
3976 UNIFI_SDIO_CLOCK_SAFE_HZ);
3977 r = ConvertCsrSdioToCsrHipResult(card, csrResult);
3978 if (r != CSR_RESULT_SUCCESS && r != CSR_WIFI_HIP_RESULT_NO_DEVICE)
3980 unifi_warning(card->ospriv,
3981 "Failed to decrease the SDIO clock speed\n");
3985 card->sdio_clock_speed = UNIFI_SDIO_CLOCK_SAFE_HZ;
3987 CsrSdioFunctionIdle(card->sdio_if);
3992 } /* unifi_set_host_state() */
3996 * ---------------------------------------------------------------------------
3999 * Update the card information data structure
4002 * card Pointer to card struct
4003 * card_info Pointer to info structure to update
4007 * ---------------------------------------------------------------------------
4009 void unifi_card_info(card_t *card, card_info_t *card_info)
4011 card_info->chip_id = card->chip_id;
4012 card_info->chip_version = card->chip_version;
4013 card_info->fw_build = card->build_id;
4014 card_info->fw_hip_version = card->config_data.version;
4015 card_info->sdio_block_size = card->sdio_io_block_size;
4016 } /* unifi_card_info() */
4020 * ---------------------------------------------------------------------------
4021 * unifi_check_io_status
4023 * Check UniFi for spontaneous reset and pending interrupt.
4026 * card Pointer to card struct
4027 * status Pointer to location to write chip status:
4028 * 0 if UniFi is running, and no interrupt pending
4029 * 1 if UniFi has spontaneously reset
4030 * 2 if there is a pending interrupt
4032 * CSR_RESULT_SUCCESS if OK, or CSR error
4033 * ---------------------------------------------------------------------------
4035 CsrResult unifi_check_io_status(card_t *card, CsrInt32 *status)
4043 r = sdio_read_f0(card, SDIO_IO_ENABLE, &io_en);
4044 if (r == CSR_WIFI_HIP_RESULT_NO_DEVICE)
4048 if (r != CSR_RESULT_SUCCESS)
4050 unifi_error(card->ospriv, "Failed to read SDIO_IO_ENABLE to check for spontaneous reset\n");
4054 if ((io_en & (1 << card->function)) == 0)
4058 unifi_error(card->ospriv, "UniFi has spontaneously reset.\n");
4061 * These reads are very likely to fail. We want to know if the function is really
4062 * disabled or the SDIO driver just returns rubbish.
4064 fw_count = unifi_read_shared_count(card, card->sdio_ctrl_addr + 4);
4067 unifi_error(card->ospriv, "Failed to read to-host sig written count\n");
4071 unifi_error(card->ospriv, "thsw: %u (driver thinks is %u)\n",
4072 fw_count, card->to_host_signals_w);
4074 fw_count = unifi_read_shared_count(card, card->sdio_ctrl_addr + 2);
4077 unifi_error(card->ospriv, "Failed to read from-host sig read count\n");
4081 unifi_error(card->ospriv, "fhsr: %u (driver thinks is %u)\n",
4082 fw_count, card->from_host_signals_r);
4088 unifi_info(card->ospriv, "UniFi function %d is enabled.\n", card->function);
4090 /* See if we missed an SDIO interrupt */
4091 r = CardPendingInt(card, &pending);
4094 unifi_error(card->ospriv, "There is an unhandled pending interrupt.\n");
4100 } /* unifi_check_io_status() */
4103 void unifi_get_hip_qos_info(card_t *card, unifi_HipQosInfo *hipqosinfo)
4107 CsrUint32 occupied_fh;
4110 CsrUint16 nslots, i;
4112 CsrMemSet(hipqosinfo, 0, sizeof(unifi_HipQosInfo));
4114 nslots = card->config_data.num_fromhost_data_slots;
4116 for (i = 0; i < nslots; i++)
4118 if (card->from_host_data[i].bd.data_length == 0)
4120 hipqosinfo->free_fh_bulkdata_slots++;
4124 for (i = 0; i < UNIFI_NO_OF_TX_QS; i++)
4126 sigq = &card->fh_traffic_queue[i];
4127 t = sigq->q_wr_ptr - sigq->q_rd_ptr;
4130 t += sigq->q_length;
4132 hipqosinfo->free_fh_sig_queue_slots[i] = (sigq->q_length - t) - 1;
4135 count_fhr = unifi_read_shared_count(card, card->sdio_ctrl_addr + 2);
4138 unifi_error(card->ospriv, "Failed to read from-host sig read count - %d\n", count_fhr);
4139 hipqosinfo->free_fh_fw_slots = 0xfa;
4143 occupied_fh = (card->from_host_signals_w - count_fhr) % 128;
4145 hipqosinfo->free_fh_fw_slots = (CsrUint16)(card->config_data.num_fromhost_sig_frags - occupied_fh);
4150 CsrResult ConvertCsrSdioToCsrHipResult(card_t *card, CsrResult csrResult)
4152 CsrResult r = CSR_RESULT_FAILURE;
4156 case CSR_RESULT_SUCCESS:
4157 r = CSR_RESULT_SUCCESS;
4159 /* Timeout errors */
4160 case CSR_SDIO_RESULT_TIMEOUT:
4161 /* Integrity errors */
4162 case CSR_SDIO_RESULT_CRC_ERROR:
4163 r = CSR_RESULT_FAILURE;
4165 case CSR_SDIO_RESULT_NO_DEVICE:
4166 r = CSR_WIFI_HIP_RESULT_NO_DEVICE;
4168 case CSR_SDIO_RESULT_INVALID_VALUE:
4169 r = CSR_WIFI_HIP_RESULT_INVALID_VALUE;
4171 case CSR_RESULT_FAILURE:
4172 r = CSR_RESULT_FAILURE;
4175 unifi_warning(card->ospriv, "Unrecognised csrResult error code: %d\n", csrResult);
4180 } /* ConvertCsrSdioToCsrHipResult() */