diff --git a/drivers/net/wireless/apple/centauri/t6050/centauri_wifi.c b/drivers/net/wireless/apple/centauri/t6050/centauri_wifi.c index 5012fd467803..7c46f3bef4a9 100644 --- a/drivers/net/wireless/apple/centauri/t6050/centauri_wifi.c +++ b/drivers/net/wireless/apple/centauri/t6050/centauri_wifi.c @@ -45,6 +45,12 @@ * (shift 7) on every socket it transmits for, and so does this driver. */ #define DATA_TX_PACING_SHIFT 7 +/* + * The MSI vectors data_ring_open() gives the data rings: TX ring 25 and its + * completion ring 76 signal on one, the RX rings 12, 14 and 15 on the other. + */ +#define DATA_TX_VECTOR 3 +#define DATA_RX_VECTOR 8 static_assert(DATA_TX_BUFFER >= CEN_ALPHA_SCRATCH_OFFSET + CEN_ALPHA_SCRATCH_SIZE); @@ -67,6 +73,15 @@ struct centauri_wifi_disconnect_receipt { int command_error, error; }; +/* + * One data direction's frames since start, for the power save guard. A window + * counted under another ps_gen (gen) starts over. + */ +struct centauri_wifi_ps_window { + unsigned long start; + unsigned int frames, gen; +}; + struct centauri_t6050_wifi { struct centauri_t6050 *control; struct cen_alpha_resources a; @@ -78,7 +93,8 @@ struct centauri_t6050_wifi { struct work_struct link_work; bool irq_ready; struct cfg80211_scan_request *scan_request; - struct mutex lock; /* Serializes firmware commands and ring consumers. */ + /* Serializes firmware commands and the control and event rings. */ + struct mutex lock; u16 main_head, command_head, sequence; u32 rx_seen[5]; @@ -105,6 +121,37 @@ struct centauri_t6050_wifi { u8 bssid[ETH_ALEN], connect_payload[1024], request_ie[256]; u8 ssid[IEEE80211_MAX_SSID_LEN]; size_t connect_size, request_ie_size, ssid_len; + /* + * The data rings run outside wifi->lock, so that a frame never waits + * for a firmware command: frames go out from net_transmit() and come + * in from rx_napi, whose poll also takes the TX completions. + * + * Lock order: wifi->lock, then data_tx_lock or data_rx_lock. The two + * data locks never nest, and wifi->lock is never taken under either. + * data_tx_queue.lock nests inside data_tx_lock, which nests inside the + * stack's TX queue lock (net_transmit()). Hard interrupts take + * none of them; process context takes the data locks with bottom + * halves off. + * + * data_rx_lock: RX post ring 12, RX completion ring 15 and data_rx_*. + * data_tx_lock: TX ring 25, TX completion ring 76, data_tx_queue and + * the other data_tx_* fields. + */ + spinlock_t data_rx_lock, data_tx_lock; + /* + * Set while no data path may touch the rings (see data_quiesce()). + * Written under wifi->lock. + */ + bool data_paused; + /* rx_napi is enabled: the poll owns RX, not the link worker. */ + bool rx_napi_on; + /* + * The Linux interrupts of DATA_TX_VECTOR and DATA_RX_VECTOR, or 0. + * Set before irq_ready and not changed after it. + */ + int data_irq[2]; + /* Ring 25 or 76 moved and the TX doorbell is still owed. */ + bool data_tx_doorbell; u32 data_rx_received, data_rx_reposted, data_rx_bad; struct sk_buff_head data_tx_queue; u16 data_tx_length[DATA_TX_SLOTS]; @@ -124,7 +171,7 @@ struct centauri_t6050_wifi { u8 tx_defaults_applied; struct ieee80211_supported_band bands[3]; struct ieee80211_channel channels[CEN_CHANNELS]; - /* The station's HE capabilities per band: 2.4, 5 and 6 GHz. */ + /* The station's HE and EHT capabilities per band: 2.4, 5 and 6 GHz. */ struct ieee80211_sband_iftype_data iftype_data[3]; /* The firmware's own reports, read at bring-up (system commands 19 and 8). */ u8 hw_channels[REPLY_SIZE], system_caps[REPLY_SIZE]; @@ -135,9 +182,26 @@ struct centauri_t6050_wifi { u8 avoid_ssid[IEEE80211_MAX_SSID_LEN]; size_t avoid_ssid_len; unsigned long avoid_until; - /* Received frames wait here for rx_napi, whose poll hands them to GRO. */ + /* Drains the data RX ring into GRO and reaps the TX completions. */ struct napi_struct rx_napi; - struct sk_buff_head rx_gro_queue; + /* + * Power save as cfg80211 last set it (command 7), and whether ps_work + * holds it off for a burst of traffic. Written under wifi->lock. + */ + bool ps_user, ps_suppressed; + /* ps_note() saw a burst of traffic; ps_worker() takes it. */ + atomic_t ps_burst_requested; + /* + * Changes, under wifi->lock, whenever cfg80211 changes power save or a + * connect ends: a hold ps_work started (ps_hold_gen), or frames a + * window counted, under another value are stale. + */ + unsigned int ps_gen, ps_hold_gen; + /* Frames seen in the current window, under data_rx_lock and data_tx_lock. */ + struct centauri_wifi_ps_window ps_rx, ps_tx; + /* The last sample of ps_work and the last time it found traffic. */ + unsigned long ps_frames, ps_sampled_at, ps_busy_at; + struct delayed_work ps_work; }; static int query_link_status(struct centauri_t6050_wifi *wifi); @@ -497,43 +561,55 @@ static bool data_rx_frame(const u8 *packet, unsigned int capacity, return *offset <= capacity && *frame_length <= capacity - *offset; } -static int rx_poll(struct napi_struct *napi, int budget) +/* + * Whether a data path may touch the rings of @bit in data_rings_open: the + * transport is up and nothing is taking it down. Called with the ring's data + * lock held, so that data_quiesce() waits for the caller. A data interrupt + * does not wake the link worker, so when the firmware has stopped while + * nothing took the transport down, wake it here to report the fault. + */ +static bool data_ready(struct centauri_t6050_wifi *wifi, unsigned int bit) { - struct centauri_t6050_wifi *wifi = - container_of(napi, struct centauri_t6050_wifi, rx_napi); - struct sk_buff *skb; - int done = 0; - - while (done < budget && (skb = skb_dequeue(&wifi->rx_gro_queue))) { - napi_gro_receive(napi, skb); - done++; - } - if (done < budget && napi_complete_done(napi, done) && - !skb_queue_empty(&wifi->rx_gro_queue)) - napi_schedule(napi); - return done; + if (READ_ONCE(wifi->data_paused) || READ_ONCE(wifi->stopping) || + READ_ONCE(wifi->transport_paused) || + READ_ONCE(wifi->host_sleep_sent) || + !(READ_ONCE(wifi->data_rings_open) & BIT(bit))) + return false; + if (readl(wifi->a.bar + 8) == 2) + return true; + if (!READ_ONCE(wifi->command_failed)) + schedule_work(&wifi->link_work); + return false; } -static void drain_data_rx(struct centauri_t6050_wifi *wifi) +/* + * Take up to @budget frames off the data RX completion ring and post their + * buffers back. With @napi the frames go to @frames for GRO; without (the + * interface is down) they are dropped. Caller holds data_rx_lock. + */ +static int data_rx_collect(struct centauri_t6050_wifi *wifi, + struct napi_struct *napi, int budget, + struct sk_buff_head *frames) { u16 head, tail, post; - unsigned int processed = 0; - struct sk_buff_head frames; - - __skb_queue_head_init(&frames); + int processed = 0; + bool deliver; - if (!(wifi->data_rings_open & BIT(4)) || readl(wifi->a.bar + 8) != 2) - return; + lockdep_assert_held(&wifi->data_rx_lock); + if (budget <= 0 || !data_ready(wifi, 4)) + return 0; head = index_read(wifi, 0, 207); tail = index_read(wifi, 2, 207); post = index_read(wifi, 3, 12); if (head >= DATA_RX_SLOTS || tail >= DATA_RX_SLOTS || post >= DATA_RX_SLOTS) { wifi->data_rx_bad++; - return; + return 0; } + deliver = napi && READ_ONCE(wifi->connected) && + netif_running(wifi->netdev); dma_rmb(); - while (tail != head && processed < (DATA_RX_SLOTS - 1)) { + while (tail != head && processed < budget) { u8 *completion = wifi->a.window + 0x240000 + tail * 32; u32 header = get_unaligned_le32(completion); u32 info = get_unaligned_le32(completion + 4); @@ -555,15 +631,14 @@ static void drain_data_rx(struct centauri_t6050_wifi *wifi) packet = wifi->a.window + buffer; wifi->data_rx_received++; - if (wifi->connected && netif_running(wifi->netdev)) { + if (deliver) { u32 footer = get_unaligned_le32(completion + 12); unsigned int offset, frame_length; struct sk_buff *skb = NULL; if (data_rx_frame(packet, 4096, length, footer, &offset, &frame_length)) - skb = netdev_alloc_skb_ip_align(wifi->netdev, - frame_length); + skb = napi_alloc_skb(napi, frame_length); if (skb) { skb_put_data(skb, packet + offset, frame_length); @@ -572,7 +647,7 @@ static void drain_data_rx(struct centauri_t6050_wifi *wifi) skb->ip_summed = CHECKSUM_NONE; wifi->netdev->stats.rx_packets++; wifi->netdev->stats.rx_bytes += frame_length; - __skb_queue_tail(&frames, skb); + __skb_queue_tail(frames, skb); } else { wifi->netdev->stats.rx_dropped++; } @@ -595,19 +670,14 @@ static void drain_data_rx(struct centauri_t6050_wifi *wifi) dma_wmb(); writel(1, wifi->a.bar + 0x1050); } - /* - * Hand the batch to the NAPI context, so that GRO coalesces a flow's - * segments (and TCP acknowledges each coalesced run once, not every - * other segment) before they reach the stack. - */ - if (!skb_queue_empty(&frames)) { - spin_lock_bh(&wifi->rx_gro_queue.lock); - skb_queue_splice_tail_init(&frames, &wifi->rx_gro_queue); - spin_unlock_bh(&wifi->rx_gro_queue.lock); - local_bh_disable(); - napi_schedule(&wifi->rx_napi); - local_bh_enable(); - } + return processed; +} + +/* Whether the data RX completion ring holds frames; a hint, without a lock. */ +static bool data_rx_pending(struct centauri_t6050_wifi *wifi) +{ + return (READ_ONCE(wifi->data_rings_open) & BIT(4)) && + index_read(wifi, 0, 207) != index_read(wifi, 2, 207); } static bool data_tx_completion_span(u16 cookie, u16 wire_count, u8 flags, @@ -623,14 +693,60 @@ static bool data_tx_completion_span(u16 cookie, u16 wire_count, u8 flags, return true; } -static void drain_data_tx(struct centauri_t6050_wifi *wifi) +/* + * Whether a TX path may post frames now: not once a disconnect event is in, + * even before the link worker takes the link down. The next connect clears + * the event. + */ +static bool data_tx_open(struct centauri_t6050_wifi *wifi) +{ + return READ_ONCE(wifi->connected) && !READ_ONCE(wifi->stopping) && + !READ_ONCE(wifi->tx_paused) && + !READ_ONCE(wifi->command_failed) && + !READ_ONCE(wifi->disconnect_event) && + netif_running(wifi->netdev); +} + +/* + * Ring the TX doorbell if a ring moved since the last one. Caller holds + * data_tx_lock and has seen the transport take it (data_ready()). + */ +static void data_tx_ring(struct centauri_t6050_wifi *wifi) +{ + lockdep_assert_held(&wifi->data_tx_lock); + if (!wifi->data_tx_doorbell) + return; + wifi->data_tx_doorbell = false; + dma_wmb(); + writel(1, wifi->a.bar + 0x1030); +} + +/* + * Ring a doorbell a TX batch still owes. One owed while the transport cannot + * take it (a fault, a stop, a pause) is dropped. Caller holds data_tx_lock. + */ +static void data_tx_doorbell(struct centauri_t6050_wifi *wifi) +{ + if (wifi->data_tx_doorbell && + (READ_ONCE(wifi->command_failed) || !data_ready(wifi, 5))) + wifi->data_tx_doorbell = false; + data_tx_ring(wifi); +} + +/* + * Reap the TX completions, then post the queued frames that fit. With @defer + * the doorbell waits for the next run, which the stack promises when it says + * more frames follow (netdev_xmit_more()). Caller holds data_tx_lock. Every + * step is a memory or MMIO access; nothing here sleeps. + */ +static void data_tx_run(struct centauri_t6050_wifi *wifi, bool defer) { u16 head, tail, post; unsigned int budget = DATA_TX_SLOTS; bool posted = false, completed = false; - if (wifi->command_failed || !(wifi->data_rings_open & BIT(5)) || - readl(wifi->a.bar + 8) != 2) + lockdep_assert_held(&wifi->data_tx_lock); + if (READ_ONCE(wifi->command_failed) || !data_ready(wifi, 5)) return; head = index_read(wifi, 0, 192); tail = index_read(wifi, 2, 192); @@ -685,9 +801,7 @@ static void drain_data_tx(struct centauri_t6050_wifi *wifi) if (completed) index_write(wifi, 2, 192, tail); budget = DATA_TX_SLOTS - 1; - while (wifi->connected && !READ_ONCE(wifi->stopping) && - !READ_ONCE(wifi->tx_paused) && - netif_running(wifi->netdev) && budget--) { + while (data_tx_open(wifi) && budget--) { u16 next = (post + 1) % DATA_TX_SLOTS; u32 buffer = DATA_TX_BUFFER + post * BUFFER_SIZE; u8 *descriptor = wifi->a.window + 0x540000 + post * 32; @@ -701,7 +815,8 @@ static void drain_data_tx(struct centauri_t6050_wifi *wifi) * Keep at most DATA_TX_INFLIGHT_MAX frames with the firmware. A * completion that never comes would hold its frame's place for * good, so the cap gives way once no completion has arrived for - * half a second (beacon reports keep this path running). + * half a second (beacon reports keep the link worker, and with + * it this path, running). */ if (inflight >= DATA_TX_INFLIGHT_MAX && !skb_queue_empty(&wifi->data_tx_queue)) { @@ -745,16 +860,202 @@ static void drain_data_tx(struct centauri_t6050_wifi *wifi) dma_wmb(); if (posted) index_write(wifi, 3, 25, post); - dma_wmb(); - writel(1, wifi->a.bar + 0x1030); + wifi->data_tx_doorbell = true; } - if (wifi->connected && !READ_ONCE(wifi->stopping) && - !READ_ONCE(wifi->tx_paused) && - netif_running(wifi->netdev) && + if (!defer) + data_tx_ring(wifi); + /* Under data_tx_lock, so net_transmit() cannot stop it after this. */ + if (data_tx_open(wifi) && skb_queue_len(&wifi->data_tx_queue) < DATA_TX_QUEUE_LIMIT) netif_wake_queue(wifi->netdev); } +/* + * Power save costs throughput and latency while frames flow: hold it off + * (ps_worker()) as soon as one direction carries PS_BUSY_FRAMES in a + * PS_WINDOW, and give it back after PS_IDLE_TIME below PS_IDLE_FRAMES a + * second. Nothing is held off while cfg80211 has power save off. + */ +#define PS_WINDOW (HZ / 4) +#define PS_BUSY_FRAMES 64 +#define PS_IDLE_FRAMES 32 +#define PS_IDLE_TIME (2 * HZ) +#define PS_SAMPLE (HZ / 2) + +/* Count frames of one direction. Caller holds that direction's lock. */ +static void ps_note(struct centauri_t6050_wifi *wifi, + struct centauri_wifi_ps_window *window, unsigned int frames) +{ + unsigned int gen = READ_ONCE(wifi->ps_gen); + + if (window->gen != gen || + time_after(jiffies, window->start + PS_WINDOW)) { + window->start = jiffies; + window->frames = 0; + window->gen = gen; + } + window->frames += frames; + if (window->frames >= PS_BUSY_FRAMES && READ_ONCE(wifi->ps_user) && + !READ_ONCE(wifi->ps_suppressed)) { + window->frames = 0; + atomic_set(&wifi->ps_burst_requested, 1); + /* Now, ahead of a stale sample still waiting. */ + mod_delayed_work(system_percpu_wq, &wifi->ps_work, 0); + } +} + +/* + * Power save changed in cfg80211, or a connect ended: a burst seen, frames + * counted toward one or a hold started before no longer count. A data path + * still counting under the old state ends under its lock before the burst + * flag clears, so it cannot flag one after. Caller holds wifi->lock. + */ +static void ps_invalidate(struct centauri_t6050_wifi *wifi) +{ + WRITE_ONCE(wifi->ps_gen, wifi->ps_gen + 1); + spin_lock_bh(&wifi->data_tx_lock); + wifi->ps_tx.frames = 0; + spin_unlock_bh(&wifi->data_tx_lock); + spin_lock_bh(&wifi->data_rx_lock); + wifi->ps_rx.frames = 0; + spin_unlock_bh(&wifi->data_rx_lock); + atomic_set(&wifi->ps_burst_requested, 0); +} + +static int rx_poll(struct napi_struct *napi, int budget) +{ + struct centauri_t6050_wifi *wifi = + container_of(napi, struct centauri_t6050_wifi, rx_napi); + struct sk_buff_head frames; + struct sk_buff *skb; + int done; + + /* + * Budget 0 is netpoll, possibly with interrupts off over a holder of + * the lock: reap TX only if the lock is free, and leave RX alone. + */ + if (!budget) { + if (spin_trylock(&wifi->data_tx_lock)) { + data_tx_run(wifi, false); + spin_unlock(&wifi->data_tx_lock); + } + return 0; + } + /* NAPI runs with bottom halves off: the plain lock calls suffice. */ + spin_lock(&wifi->data_tx_lock); + data_tx_run(wifi, false); + spin_unlock(&wifi->data_tx_lock); + + __skb_queue_head_init(&frames); + spin_lock(&wifi->data_rx_lock); + done = data_rx_collect(wifi, napi, budget, &frames); + if (done) + ps_note(wifi, &wifi->ps_rx, done); + spin_unlock(&wifi->data_rx_lock); + /* + * Outside the lock: a frame's delivery may transmit (a TCP ACK), which + * takes data_tx_lock. + */ + while ((skb = __skb_dequeue(&frames))) + napi_gro_receive(napi, skb); + if (done < budget) + napi_complete_done(napi, done); + return done; +} + +static void data_rx_schedule(struct centauri_t6050_wifi *wifi) +{ + local_bh_disable(); + napi_schedule(&wifi->rx_napi); + local_bh_enable(); +} + +/* + * Service the data rings from process context, with or without wifi->lock: + * the link worker and the command waits. TX runs here as well as in the + * fast paths, so that the in-flight cap can give way while nothing is sent. + * While the interface is down (no rx_napi), received frames are dropped here + * and their buffers go back to the firmware. + */ +static void data_kick(struct centauri_t6050_wifi *wifi) +{ + struct sk_buff_head none; + + spin_lock_bh(&wifi->data_tx_lock); + data_tx_run(wifi, false); + spin_unlock_bh(&wifi->data_tx_lock); + + if (READ_ONCE(wifi->rx_napi_on)) { + if (data_rx_pending(wifi)) + data_rx_schedule(wifi); + return; + } + __skb_queue_head_init(&none); + spin_lock_bh(&wifi->data_rx_lock); + data_rx_collect(wifi, NULL, DATA_RX_SLOTS - 1, &none); + spin_unlock_bh(&wifi->data_rx_lock); +} + +/* + * Stop every data path from touching the rings: a path that checked + * data_ready() before the flag was set finishes under its lock first. A TX + * doorbell still owed rings before the flag closes the transport to it. + * Caller holds wifi->lock. + */ +static void data_quiesce(struct centauri_t6050_wifi *wifi) +{ + spin_lock_bh(&wifi->data_tx_lock); + data_tx_doorbell(wifi); + WRITE_ONCE(wifi->data_paused, true); + spin_unlock_bh(&wifi->data_tx_lock); + spin_lock_bh(&wifi->data_rx_lock); + spin_unlock_bh(&wifi->data_rx_lock); +} + +/* Undo data_quiesce(). Caller holds wifi->lock. */ +static void data_unquiesce(struct centauri_t6050_wifi *wifi) +{ + WRITE_ONCE(wifi->data_paused, false); + if (READ_ONCE(wifi->rx_napi_on)) + data_rx_schedule(wifi); +} + +/* + * Stop the queue and drop the frames waiting for a TX slot, in one critical + * section, so that a TX batch that saw the link open cannot wake the queue + * after this. A registration that failed before the netdev exists has no + * queue to stop and no frame to drop. + */ +static void data_tx_flush(struct centauri_t6050_wifi *wifi) +{ + if (!wifi->netdev) + return; + spin_lock_bh(&wifi->data_tx_lock); + netif_stop_queue(wifi->netdev); + skb_queue_purge(&wifi->data_tx_queue); + spin_unlock_bh(&wifi->data_tx_lock); +} + +/* + * The RX path counts under data_rx_lock and the TX path under data_tx_lock: + * read each direction's packets and bytes under its own lock, so that both + * come from the same point. + */ +static void data_counters_read(struct centauri_t6050_wifi *wifi, + struct net_device_stats *counters) +{ + const struct net_device_stats *live = &wifi->netdev->stats; + + spin_lock_bh(&wifi->data_rx_lock); + counters->rx_packets = live->rx_packets; + counters->rx_bytes = live->rx_bytes; + spin_unlock_bh(&wifi->data_rx_lock); + spin_lock_bh(&wifi->data_tx_lock); + counters->tx_packets = live->tx_packets; + counters->tx_bytes = live->tx_bytes; + spin_unlock_bh(&wifi->data_tx_lock); +} + /* The interface status reply: 8 fixed bytes, then TLVs. */ static bool link_signal_read(const u8 *packet, size_t size, s8 *signal) { @@ -800,7 +1101,8 @@ static void drain(struct centauri_t6050_wifi *wifi) wire_command = get_unaligned_le32(packet + 0x14); command = wire_command & ~BIT(30); wifi->rx_seen[ring]++; - wifi->rx_next[ring] = (slot + 1) % ring_size[ring]; + WRITE_ONCE(wifi->rx_next[ring], + (slot + 1) % ring_size[ring]); if (size < 24 || size > BUFFER_SIZE) goto refill; if (ring == 4) { @@ -818,8 +1120,26 @@ static void drain(struct centauri_t6050_wifi *wifi) } if (command == 0x80000003 || command == 0x80000004) { - wifi->disconnect_event = true; + /* + * Whoever drains it, the link worker + * takes the link down: a data + * interrupt does not wake it. + */ + bool wake = wifi->connected && + !wifi->disconnect_event && + !READ_ONCE(wifi->stopping); + + /* + * Under the TX lock: a running TX + * batch ends before the event closes + * the ring to the next one. + */ + spin_lock_bh(&wifi->data_tx_lock); + WRITE_ONCE(wifi->disconnect_event, true); + spin_unlock_bh(&wifi->data_tx_lock); wifi->disconnect_status = status; + if (wake) + schedule_work(&wifi->link_work); } if (command == 0x80000004) wifi->disconnect_pending = false; @@ -850,17 +1170,30 @@ static void drain(struct centauri_t6050_wifi *wifi) completions(wifi); if (posted) writel(1, wifi->a.bar + 0x1004); - drain_data_rx(wifi); - drain_data_tx(wifi); + data_kick(wifi); +} + +/* + * Close TX for good: under the TX lock, so a running TX batch ends before the + * queue stops. Caller holds wifi->lock. + */ +static void data_tx_fail(struct centauri_t6050_wifi *wifi) +{ + spin_lock_bh(&wifi->data_tx_lock); + WRITE_ONCE(wifi->command_failed, true); + if (wifi->netdev) + netif_stop_queue(wifi->netdev); + /* No doorbell after the fault, not even one a batch still owes. */ + wifi->data_tx_doorbell = false; + spin_unlock_bh(&wifi->data_tx_lock); } /* Caller holds wifi->lock. Unknown completion keeps DMA payloads owned. */ static void command_fault(struct centauri_t6050_wifi *wifi) { - wifi->command_failed = true; + data_tx_fail(wifi); wifi->configured = false; wifi->data_failed = true; - netif_stop_queue(wifi->netdev); if (smp_load_acquire(&wifi->irq_ready)) schedule_work(&wifi->link_work); } @@ -1097,8 +1430,10 @@ static void channels_apply(struct centauri_t6050_wifi *wifi) channel->flags |= IEEE80211_CHAN_NO_HT40; if (!channel_entry(list, size, band, 2, channel->hw_value)) channel->flags |= IEEE80211_CHAN_NO_80MHZ; + /* 320 MHz spans two 160 MHz channels: none without 160. */ if (!channel_entry(list, size, band, 3, channel->hw_value)) - channel->flags |= IEEE80211_CHAN_NO_160MHZ; + channel->flags |= IEEE80211_CHAN_NO_160MHZ | + IEEE80211_CHAN_NO_320MHZ; if (!(channel->flags & IEEE80211_CHAN_DISABLED)) counted[band]++; } @@ -1120,21 +1455,150 @@ static void country_set(struct centauri_t6050_wifi *wifi, const u8 country[3]) } /* - * The station's HT, VHT and HE capabilities as the firmware reports them + * The size of the PPE thresholds of an EHT capabilities element whose MCS map + * field holds @mcs bytes, or -1 when the element is not exactly the size its + * fields give. + */ +static int eht_elem_ppe_size(const u8 *elem, unsigned int mcs) +{ + const struct ieee80211_eht_cap_elem_fixed *fixed = + (const void *)(elem + 3); + unsigned int len = elem[1] - 1, ppe = 0; + + if (len < sizeof(*fixed) + mcs) + return -1; + if (fixed->phy_cap_info[5] & IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) { + if (len < sizeof(*fixed) + mcs + 2) + return -1; + ppe = ieee80211_eht_ppe_size(get_unaligned_le16(elem + 3 + + sizeof(*fixed) + + mcs), + fixed->phy_cap_info); + } + return len == sizeof(*fixed) + mcs + ppe ? ppe : -1; +} + +/* + * Read an EHT capabilities element against the HE capabilities it came with. + * The size of its MCS map field follows the 5 and 6 GHz channel widths (80, + * 160 and 320 MHz, or a 20 MHz-only map without any). Without a 5 or 6 GHz + * width it follows 40 MHz on 2.4 GHz instead, when HE lists that, whose map + * has the layout of the 80 MHz one. Only an element of exactly the size its + * fields give, with widths that build on each other and with spatial streams + * both ways in its base MCS range, is taken. Returns the size of the MCS map field, or 0 when + * the element is not usable. + */ +static unsigned int eht_caps_from_elem(const struct ieee80211_he_cap_elem *he, + const u8 *elem, + struct ieee80211_sta_eht_cap *eht) +{ + const struct ieee80211_eht_cap_elem_fixed *fixed = + (const void *)(elem + 3); + const u8 *map = elem + 3 + sizeof(*fixed); + struct ieee80211_he_cap_elem he_5ghz = *he; + unsigned int mcs; + u8 width; + int ppe; + + he_5ghz.phy_cap_info[0] &= + ~(IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G | + IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_RU_MAPPING_IN_2G); + width = he_5ghz.phy_cap_info[0]; + if ((width & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G && + !(width & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) || + (fixed->phy_cap_info[0] & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ && + !(width & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G))) + return 0; + mcs = ieee80211_eht_mcs_nss_size(&he_5ghz, fixed, false); + /* No 5 or 6 GHz width: 40 MHz on 2.4 GHz picks the 80 MHz layout. */ + if (mcs == 4 && + he->phy_cap_info[0] & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G) + mcs = 3; + ppe = eht_elem_ppe_size(elem, mcs); + /* MCS 0-7 in the 20 MHz-only map, MCS 0-9 in the others. */ + if (ppe < 0 || ppe > sizeof(eht->eht_ppe_thres) || + !(map[0] & IEEE80211_EHT_MCS_NSS_RX) || + !(map[0] & IEEE80211_EHT_MCS_NSS_TX)) + return 0; + + memset(eht, 0, sizeof(*eht)); + eht->has_eht = true; + eht->eht_cap_elem = *fixed; + memcpy(&eht->eht_mcs_nss_supp, map, mcs); + memcpy(eht->eht_ppe_thres, map + mcs, ppe); + return mcs; +} + +/* + * Fit the firmware's EHT capabilities to band @band (0 for 2.4 GHz, 1 for 5, + * 2 for 6): 160 MHz only where the band's HE capabilities list it, the MCS + * maps of the widths left, the 6 GHz duplicate mode on 6 GHz only and the + * maximum MPDU length on 2.4 GHz only (5 and 6 GHz take it from VHT and HE). + * No band gets 320 MHz: the channel list (channels_apply()) describes widths + * up to 160 MHz, so it cannot say which 6 GHz channels take 320 MHz. A band + * whose map layout the element does not carry (20 MHz only against a wider + * one) gets no EHT. + */ +static void eht_band_caps(struct ieee80211_sband_iftype_data *data, + const struct ieee80211_sta_eht_cap *base, + unsigned int base_mcs, unsigned int band) +{ + struct ieee80211_sta_eht_cap *eht = &data->eht_cap; + u8 *phy = eht->eht_cap_elem.phy_cap_info; + bool narrow, base_narrow = base_mcs == 4; + unsigned int mcs; + + *eht = *base; + phy[0] &= ~IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ; + phy[1] &= ~IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_320MHZ_MASK; + phy[2] &= ~IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_320MHZ_MASK; + phy[3] &= ~IEEE80211_EHT_PHY_CAP3_SOUNDING_DIM_320MHZ_MASK; + phy[6] &= ~IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_320MHZ; + phy[7] &= ~(IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_320MHZ | + IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_320MHZ); + if (band != 2) + phy[6] &= ~IEEE80211_EHT_PHY_CAP6_EHT_DUP_6GHZ_SUPP; + if (band != 0) + eht->eht_cap_elem.mac_cap_info[0] &= + ~IEEE80211_EHT_MAC_CAP0_MAX_MPDU_LEN_MASK; + if (!(data->he_cap.he_cap_elem.phy_cap_info[0] & + IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)) { + phy[1] &= ~IEEE80211_EHT_PHY_CAP1_BEAMFORMEE_SS_160MHZ_MASK; + phy[2] &= ~IEEE80211_EHT_PHY_CAP2_SOUNDING_DIM_160MHZ_MASK; + phy[6] &= ~IEEE80211_EHT_PHY_CAP6_MCS15_SUPP_160MHZ; + phy[7] &= ~(IEEE80211_EHT_PHY_CAP7_NON_OFDMA_UL_MU_MIMO_160MHZ | + IEEE80211_EHT_PHY_CAP7_MU_BEAMFORMER_160MHZ); + } + mcs = ieee80211_eht_mcs_nss_size(&data->he_cap.he_cap_elem, + &eht->eht_cap_elem, false); + narrow = mcs == 4; + if (narrow != base_narrow || mcs > base_mcs) { + memset(eht, 0, sizeof(*eht)); + return; + } + memset((u8 *)&eht->eht_mcs_nss_supp + mcs, 0, + sizeof(eht->eht_mcs_nss_supp) - mcs); +} + +/* + * The station's HT, VHT, HE and EHT capabilities as the firmware reports them * (system command 8): the HT element at offset 87 of TLV 262, the VHT element - * (TLV 0xbf) and the HE capabilities element (TLV 0xff). HT applies to 2.4 and - * 5 GHz, VHT to 5 GHz and HE to all three bands, without 40 MHz on 2.4 GHz - * where the firmware does not list it and without 160 MHz there. The 6 GHz - * band capabilities come from the VHT element. A band keeps what it has when - * its element is missing; without HE, 6 GHz is not registered. + * (TLV 0xbf) and the HE and EHT capabilities elements (TLV 0xff each). HT + * applies to 2.4 and 5 GHz, VHT to 5 GHz and HE to all three bands, without + * 40 MHz on 2.4 GHz where the firmware does not list it and without 160 MHz + * there. EHT follows HE, without 320 MHz. The 6 GHz band capabilities come + * from the VHT element. A band keeps what it has when its element is + * missing; without HE, 6 GHz is not registered and EHT is ignored. */ static bool band_caps_from_firmware(struct centauri_t6050_wifi *wifi) { const u8 *caps = wifi->system_caps; const u8 *ht_elem = NULL, *vht_elem = NULL, *he_elem = NULL; + const u8 *eht_elem = NULL; size_t size = wifi->system_caps_blob.size, pos = 0, mcs, ppe; struct ieee80211_sta_he_cap base_he = {}; - unsigned int band; + struct ieee80211_sta_eht_cap base_eht = {}; + unsigned int band, eht_mcs = 0; u32 vht_cap = 0; u8 ampdu_density = 0; @@ -1160,6 +1624,11 @@ static bool band_caps_from_firmware(struct centauri_t6050_wifi *wifi) value[2] == WLAN_EID_EXT_HE_CAPABILITY && ieee80211_he_capa_size_ok(value + 3, value[1] - 1)) he_elem = value; + if (tag == WLAN_EID_EXTENSION && + length >= 3 + sizeof(struct ieee80211_eht_cap_elem_fixed) && + value[0] == WLAN_EID_EXTENSION && value[1] == length - 2 && + value[2] == WLAN_EID_EXT_EHT_CAPABILITY) + eht_elem = value; pos += 4 + length; } @@ -1210,6 +1679,9 @@ static bool band_caps_from_firmware(struct centauri_t6050_wifi *wifi) he_elem + 3 + sizeof(base_he.he_cap_elem) + mcs, min(ppe, sizeof(base_he.ppe_thres))); base_he.has_he = true; + if (eht_elem) + eht_mcs = eht_caps_from_elem(&base_he.he_cap_elem, eht_elem, + &base_eht); for (band = 0; band < ARRAY_SIZE(wifi->iftype_data); band++) { struct ieee80211_sband_iftype_data *data = @@ -1255,6 +1727,8 @@ static bool band_caps_from_firmware(struct centauri_t6050_wifi *wifi) IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS : 0) | (vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN ? IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS : 0)); + if (eht_mcs) + eht_band_caps(data, &base_eht, eht_mcs, band); _ieee80211_set_sband_iftype_data(&wifi->bands[band], data, 1); } return true; @@ -1608,7 +2082,7 @@ static void cancel_connect_locked(struct centauri_t6050_wifi *wifi) if (!wifi->connecting) return; wifi->connecting = false; - wifi->connected = false; + WRITE_ONCE(wifi->connected, false); memzero_explicit(wifi->connect_payload, sizeof(wifi->connect_payload)); cfg80211_connect_timeout(wifi->netdev, wifi->bssid, wifi->request_ie, wifi->request_ie_size, GFP_KERNEL, @@ -2039,7 +2513,8 @@ static int wifi_connect(struct wiphy *wiphy, struct net_device *netdev, WRITE_ONCE(wifi->connect_abort, false); wifi->connecting = true; wifi->connect_status = 0; - wifi->connect_event = wifi->disconnect_event = false; + wifi->connect_event = false; + WRITE_ONCE(wifi->disconnect_event, false); schedule_work(&wifi->connect_work); out: mutex_unlock(&wifi->lock); @@ -2214,7 +2689,7 @@ static void connect_worker(struct work_struct *work) host_setup_commands(wifi); drain(wifi); wifi->connect_event = false; - wifi->disconnect_event = false; + WRITE_ONCE(wifi->disconnect_event, false); slot = wifi->command_head; ret = command(wifi, 131072, wifi->connect_payload, wifi->connect_size); memzero_explicit(wifi->connect_payload, sizeof(wifi->connect_payload)); @@ -2280,14 +2755,20 @@ static void connect_worker(struct work_struct *work) ret == -ENOTCONN)) join_avoid_6ghz(wifi); wifi->connecting = false; - wifi->connected = status == WLAN_STATUS_SUCCESS; + /* + * The station counters start before the data paths see the link, so + * that no frame counted after the publish lands in the baseline. + */ + if (status == WLAN_STATUS_SUCCESS) + data_counters_read(wifi, &wifi->station_start); + ps_invalidate(wifi); + WRITE_ONCE(wifi->connected, status == WLAN_STATUS_SUCCESS); wifi->station_signal_valid = false; if (wifi->connected) { if (wifi->connect_channel->band == NL80211_BAND_6GHZ) wifi->avoid_valid = false; wifi->connected_at = jiffies; wifi->station_signal_at = jiffies - HZ; - wifi->station_start = wifi->netdev->stats; } if (command_payload_consumed(wifi, slot)) @@ -2318,17 +2799,54 @@ static void connect_worker(struct work_struct *work) if (wifi->data_rings_open & BIT(5)) netif_wake_queue(wifi->netdev); schedule_work(&wifi->link_work); + /* A restore the firmware refused while the link was down. */ + if (wifi->ps_suppressed) + mod_delayed_work(system_percpu_wq, &wifi->ps_work, 0); } mutex_unlock(&wifi->lock); } +/* + * Whether a control, event or scan ring holds a message the link worker has + * not taken: a hint, read without wifi->lock. + */ +static bool control_pending(struct centauri_t6050_wifi *wifi) +{ + unsigned int ring; + + for (ring = 2; ring <= 4; ring++) { + u16 slot = READ_ONCE(wifi->rx_next[ring]); + const u8 *descriptor = wifi->a.window + TR_BASE + + (ring - 1) * RING_SPAN + slot * 16; + + if (READ_ONCE(descriptor[15]) & 4) + return true; + } + return false; +} + irqreturn_t centauri_t6050_alpha_irq(int irq, void *data) { struct centauri_t6050 *c = data; struct centauri_t6050_wifi *wifi = smp_load_acquire(&c->wifi); + bool data_only; - if (wifi && smp_load_acquire(&wifi->irq_ready) && - !READ_ONCE(wifi->stopping) && !READ_ONCE(wifi->transport_paused)) + /* Pairs with the release in register: data_irq[] is set before. */ + if (!wifi || !smp_load_acquire(&wifi->irq_ready) || + READ_ONCE(wifi->stopping) || READ_ONCE(wifi->transport_paused)) + return IRQ_HANDLED; + /* + * The data vectors go straight to rx_napi, whose poll takes both the + * TX completions and the RX ring. The link worker runs for every + * other vector, for a control message seen on a data vector, and + * whenever rx_napi cannot take the data rings. + */ + data_only = irq == wifi->data_irq[0] || irq == wifi->data_irq[1]; + if (READ_ONCE(wifi->rx_napi_on) && !READ_ONCE(wifi->data_paused)) + napi_schedule(&wifi->rx_napi); + else + data_only = false; + if (!data_only || control_pending(wifi)) schedule_work(&wifi->link_work); return IRQ_HANDLED; } @@ -2346,9 +2864,14 @@ static void link_worker(struct work_struct *work) return; } was_connected = wifi->connected; - drain(wifi); + /* + * Drain only while Alpha runs: with the firmware stopped, the data + * paths in drain() find it so (data_ready()) and wake this worker + * again, without end while no fault closes the transport. + */ stage = readl(wifi->a.bar + 8); if (stage == 2) { + drain(wifi); wifi->stage_fault_logged = false; } else if (!READ_ONCE(wifi->stopping) && !wifi->host_sleep_sent && !wifi->command_failed) { @@ -2364,10 +2887,10 @@ static void link_worker(struct work_struct *work) disconnected = wifi->command_failed || wifi->disconnect_event || READ_ONCE(wifi->stopping) || stage != 2; if (disconnected) - wifi->connected = false; + WRITE_ONCE(wifi->connected, false); if (disconnected && was_connected) { netif_stop_queue(wifi->netdev); - skb_queue_purge(&wifi->data_tx_queue); + data_tx_flush(wifi); netif_carrier_off(wifi->netdev); cfg80211_disconnected(wifi->netdev, WLAN_REASON_UNSPECIFIED, NULL, 0, false, GFP_KERNEL); @@ -2411,12 +2934,14 @@ static int wait_disconnect(struct centauri_t6050_wifi *wifi) static bool data_tx_in_flight(struct centauri_t6050_wifi *wifi) { + bool busy = false; unsigned int i; - for (i = 0; i < DATA_TX_SLOTS; i++) - if (wifi->data_tx_busy[i]) - return true; - return false; + spin_lock_bh(&wifi->data_tx_lock); + for (i = 0; i < DATA_TX_SLOTS && !busy; i++) + busy = wifi->data_tx_busy[i]; + spin_unlock_bh(&wifi->data_tx_lock); + return busy; } static void drain_tx_in_flight(struct centauri_t6050_wifi *wifi) @@ -2478,9 +3003,9 @@ static int wifi_disconnect(struct wiphy *wiphy, struct net_device *netdev, wifi->pm_disconnect = receipt; connected = wifi->connected; - wifi->connected = false; + WRITE_ONCE(wifi->connected, false); netif_stop_queue(netdev); - skb_queue_purge(&wifi->data_tx_queue); + data_tx_flush(wifi); if (!READ_ONCE(wifi->stopping)) drain_tx_in_flight(wifi); netif_carrier_off(netdev); @@ -2574,7 +3099,8 @@ void centauri_t6050_wifi_mark_failed(struct centauri_t6050 *c) return; mutex_lock(&wifi->lock); WRITE_ONCE(wifi->stopping, true); - wifi->command_failed = true; + data_tx_fail(wifi); + data_quiesce(wifi); mutex_unlock(&wifi->lock); } @@ -2728,6 +3254,7 @@ static int alpha_resume_locked(struct centauri_t6050_wifi *wifi) } WRITE_ONCE(wifi->transport_paused, false); WRITE_ONCE(wifi->tx_paused, false); + data_unquiesce(wifi); wifi->station_signal_valid = false; if (wifi->connected && !READ_ONCE(wifi->stopping)) { if (!wifi->disconnect_event && netif_running(wifi->netdev) && @@ -2738,14 +3265,87 @@ static int alpha_resume_locked(struct centauri_t6050_wifi *wifi) return 0; } -int centauri_t6050_wifi_suspend(struct centauri_t6050 *c) +/* + * Hold power save off when ps_note() reports a burst, and give it back once + * the link has been idle for PS_IDLE_TIME, is gone, or the hold went stale + * (ps_invalidate()). Runs every PS_SAMPLE while it holds power save off, and + * otherwise only for a burst; a run without one is a sample, which never + * starts a hold. A refused restore keeps the hold and is tried again at the + * next sample while the link lasts, or at the next association once it is + * gone. Suspend stops it; resume, or a suspend that fails, starts it again + * (ps_restart()). + */ +static void ps_worker(struct work_struct *work) +{ + struct centauri_t6050_wifi *wifi = + container_of(to_delayed_work(work), struct centauri_t6050_wifi, + ps_work); + struct net_device_stats counters; + unsigned long frames, now, elapsed; + bool burst, rearm = false; + int ret; + u8 mode; + + data_counters_read(wifi, &counters); + frames = counters.rx_packets + counters.tx_packets; + now = jiffies; + mutex_lock(&wifi->lock); + burst = atomic_xchg(&wifi->ps_burst_requested, 0); + if (READ_ONCE(wifi->stopping) || wifi->command_failed || + !wifi->configured || READ_ONCE(wifi->tx_paused) || + READ_ONCE(wifi->transport_paused) || wifi->host_sleep_sent) + goto out; + if (!wifi->ps_suppressed) { + mode = 3; + if (!burst || !wifi->ps_user || !wifi->connected || + command(wifi, 7, &mode, sizeof(mode))) + goto out; + WRITE_ONCE(wifi->ps_suppressed, true); + wifi->ps_hold_gen = wifi->ps_gen; + wifi->ps_busy_at = now; + rearm = true; + } else { + elapsed = now - wifi->ps_sampled_at; + if ((frames - wifi->ps_frames) * HZ >= PS_IDLE_FRAMES * elapsed) + wifi->ps_busy_at = now; + rearm = wifi->ps_user && wifi->connected && + wifi->ps_hold_gen == wifi->ps_gen && + time_before(now, wifi->ps_busy_at + PS_IDLE_TIME); + if (!rearm) { + mode = 0; + ret = wifi->ps_user ? + command(wifi, 7, &mode, sizeof(mode)) : 0; + /* + * A refused restore keeps the hold: the next sample + * tries again, or the next association without a link. + */ + if (!ret || wifi->command_failed || + READ_ONCE(wifi->stopping)) + WRITE_ONCE(wifi->ps_suppressed, false); + else + rearm = wifi->connected; + } + } + wifi->ps_frames = frames; + wifi->ps_sampled_at = now; +out: + mutex_unlock(&wifi->lock); + if (rearm) + schedule_delayed_work(&wifi->ps_work, PS_SAMPLE); +} + +static void ps_restart(struct centauri_t6050_wifi *wifi) +{ + if (READ_ONCE(wifi->ps_suppressed)) + schedule_delayed_work(&wifi->ps_work, 0); +} + +static int alpha_suspend(struct centauri_t6050 *c, + struct centauri_t6050_wifi *wifi) { - struct centauri_t6050_wifi *wifi = c->wifi; unsigned int i; int ret; - if (!wifi) - return 0; mutex_lock(&wifi->lock); if (!wifi->configured || wifi->command_failed || READ_ONCE(wifi->stopping) || readl(wifi->a.bar + 8) != 2 || readl(wifi->a.bar + 0xc) != 2) { @@ -2758,6 +3358,8 @@ int centauri_t6050_wifi_suspend(struct centauri_t6050 *c) netif_tx_disable(wifi->netdev); cancel_scan(wifi); cancel_work_sync(&wifi->connect_work); + /* tx_paused keeps a late ps_work away from the firmware. */ + cancel_delayed_work_sync(&wifi->ps_work); mutex_lock(&wifi->lock); if (!wifi->configured || wifi->command_failed || READ_ONCE(wifi->stopping) || @@ -2775,6 +3377,8 @@ int centauri_t6050_wifi_suspend(struct centauri_t6050 *c) ret); goto out_unpause; } + /* From here on only this thread talks to the firmware. */ + data_quiesce(wifi); ret = alpha_host_power(wifi, true); if (ret) goto out_unpause; @@ -2785,9 +3389,23 @@ int centauri_t6050_wifi_suspend(struct centauri_t6050 *c) for (i = 0; i < c->alpha_irqs.count; i++) synchronize_irq(pci_irq_vector(c->alpha_irqs.pdev, i)); cancel_work_sync(&wifi->link_work); + /* + * A burst the data paths saw before data_quiesce() is stale after + * resume; ps_restart() starts ps_work again for a hold. + */ + cancel_delayed_work_sync(&wifi->ps_work); + atomic_set(&wifi->ps_burst_requested, 0); /* Match the regulatory notifier's lock order for suspend unwind. */ rtnl_lock(); + /* + * The interrupts are quiet, so nothing schedules rx_napi any more: + * let a poll still running finish before the transport sleeps. Under + * RTNL the interface cannot go up or down, and a disabled NAPI would + * never look idle, so rx_napi_on decides. + */ + if (READ_ONCE(wifi->rx_napi_on)) + napi_synchronize(&wifi->rx_napi); mutex_lock(&wifi->lock); ret = alpha_transport_state(wifi, true); if (!ret) { @@ -2808,14 +3426,27 @@ int centauri_t6050_wifi_suspend(struct centauri_t6050 *c) out_unpause: WRITE_ONCE(wifi->tx_paused, false); - if (wifi->connected && !wifi->disconnect_event && - netif_running(wifi->netdev) && - (wifi->data_rings_open & BIT(5))) + data_unquiesce(wifi); + /* Not after a command fault in the sleep commands above. */ + if (data_tx_open(wifi) && (wifi->data_rings_open & BIT(5))) netif_wake_queue(wifi->netdev); mutex_unlock(&wifi->lock); return ret; } +int centauri_t6050_wifi_suspend(struct centauri_t6050 *c) +{ + struct centauri_t6050_wifi *wifi = c->wifi; + int ret; + + if (!wifi) + return 0; + ret = alpha_suspend(c, wifi); + if (ret) + ps_restart(wifi); + return ret; +} + int centauri_t6050_wifi_resume(struct centauri_t6050 *c) { struct centauri_t6050_wifi *wifi = c->wifi; @@ -2828,6 +3459,8 @@ int centauri_t6050_wifi_resume(struct centauri_t6050 *c) ret = alpha_resume_locked(wifi); mutex_unlock(&wifi->lock); rtnl_unlock(); + if (!ret) + ps_restart(wifi); return ret; } @@ -2870,8 +3503,23 @@ static int wifi_power_mgmt(struct wiphy *wiphy, struct net_device *netdev, int ret; mutex_lock(&wifi->lock); - ret = wifi->configured ? command(wifi, 7, &mode, sizeof(mode)) : - -ENETDOWN; + /* Under the mutex: once stopping, no command and no fault from one. */ + if (!wifi->configured || READ_ONCE(wifi->stopping)) + ret = -ENETDOWN; + else if (enabled && wifi->ps_suppressed) + /* ps_worker() turns it on once the traffic stops. */ + ret = 0; + else + ret = command(wifi, 7, &mode, sizeof(mode)); + if (!ret) { + bool changed = wifi->ps_user != enabled; + + WRITE_ONCE(wifi->ps_user, enabled); + if (!enabled) + WRITE_ONCE(wifi->ps_suppressed, false); + if (changed) + ps_invalidate(wifi); + } mutex_unlock(&wifi->lock); return ret; } @@ -2904,7 +3552,7 @@ static void wifi_regulatory(struct wiphy *wiphy, static void fill_station(struct centauri_t6050_wifi *wifi, struct station_info *sinfo) { - const struct net_device_stats *stats = &wifi->netdev->stats; + struct net_device_stats now; if (!wifi->scan_request && time_after_eq(jiffies, wifi->station_signal_at + HZ)) { @@ -2918,10 +3566,11 @@ static void fill_station(struct centauri_t6050_wifi *wifi, struct station_info * BIT_ULL(NL80211_STA_INFO_RX_PACKETS) | BIT_ULL(NL80211_STA_INFO_TX_PACKETS); sinfo->connected_time = (jiffies - wifi->connected_at) / HZ; - sinfo->rx_bytes = stats->rx_bytes - wifi->station_start.rx_bytes; - sinfo->tx_bytes = stats->tx_bytes - wifi->station_start.tx_bytes; - sinfo->rx_packets = stats->rx_packets - wifi->station_start.rx_packets; - sinfo->tx_packets = stats->tx_packets - wifi->station_start.tx_packets; + data_counters_read(wifi, &now); + sinfo->rx_bytes = now.rx_bytes - wifi->station_start.rx_bytes; + sinfo->tx_bytes = now.tx_bytes - wifi->station_start.tx_bytes; + sinfo->rx_packets = now.rx_packets - wifi->station_start.rx_packets; + sinfo->tx_packets = now.tx_packets - wifi->station_start.tx_packets; if (wifi->station_signal_valid) { sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL); sinfo->signal = wifi->station_signal; @@ -2986,14 +3635,19 @@ static int net_open(struct net_device *netdev) netif_carrier_off(netdev); netif_stop_queue(netdev); - skb_queue_purge(&wifi->rx_gro_queue); napi_enable(&wifi->rx_napi); + WRITE_ONCE(wifi->rx_napi_on, true); mutex_lock(&wifi->lock); ret = wifi->configured ? data_start(wifi) : -ENETDOWN; mutex_unlock(&wifi->lock); - if (ret) + if (ret) { + WRITE_ONCE(wifi->rx_napi_on, false); napi_disable(&wifi->rx_napi); - return ret; + return ret; + } + /* Frames that arrived while the interface was down. */ + data_rx_schedule(wifi); + return 0; } static int net_stop(struct net_device *netdev) @@ -3003,36 +3657,81 @@ static int net_stop(struct net_device *netdev) cancel_scan(wifi); netif_stop_queue(netdev); - skb_queue_purge(&wifi->data_tx_queue); + data_tx_flush(wifi); + /* The link worker drops what arrives from now on. */ + WRITE_ONCE(wifi->rx_napi_on, false); napi_disable(&wifi->rx_napi); - skb_queue_purge(&wifi->rx_gro_queue); + /* A poll cut short by the disable can leave frames: repost them. */ + data_kick(wifi); return 0; } +/* + * Take data_tx_lock from the transmit path. The stack calls it with bottom + * halves off. Netpoll can call it with interrupts off, on a CPU whose process + * context holds the lock (spin_lock_bh): never spin there. + */ +static bool data_tx_lock_xmit(struct centauri_t6050_wifi *wifi) +{ + if (unlikely(irqs_disabled())) + return spin_trylock(&wifi->data_tx_lock); + spin_lock(&wifi->data_tx_lock); + return true; +} + static netdev_tx_t net_transmit(struct sk_buff *skb, struct net_device *netdev) { struct centauri_t6050_wifi *wifi = *(struct centauri_t6050_wifi **)netdev_priv(netdev); + bool more = netdev_xmit_more(); if (READ_ONCE(wifi->stopping) || !READ_ONCE(wifi->connected) || - skb->len < ETH_HLEN || skb->len > BUFFER_SIZE) { - netdev->stats.tx_dropped++; - dev_kfree_skb_any(skb); - return NETDEV_TX_OK; - } + skb->len < ETH_HLEN || skb->len > BUFFER_SIZE) + goto drop; if (READ_ONCE(wifi->tx_paused)) { netif_stop_queue(netdev); return NETDEV_TX_BUSY; } + if (!data_tx_lock_xmit(wifi)) + return NETDEV_TX_BUSY; + /* A command fault sets command_failed under this lock: no enqueue after it. */ + if (!READ_ONCE(wifi->connected) || READ_ONCE(wifi->command_failed)) { + spin_unlock(&wifi->data_tx_lock); + goto drop; + } if (skb_queue_len(&wifi->data_tx_queue) >= DATA_TX_QUEUE_LIMIT) { netif_stop_queue(netdev); + /* The stack retries later: ring for what is already posted. */ + data_tx_doorbell(wifi); + spin_unlock(&wifi->data_tx_lock); return NETDEV_TX_BUSY; } sk_pacing_shift_update(skb->sk, DATA_TX_PACING_SHIFT); + /* + * Behind the frames already waiting, then post what fits: the frame + * stays queued only while the ring or the in-flight cap is full, and + * the completions that free a place post it. One doorbell covers a + * batch: the stack says when more frames follow. + */ skb_queue_tail(&wifi->data_tx_queue, skb); - if (skb_queue_len(&wifi->data_tx_queue) >= DATA_TX_QUEUE_LIMIT) + ps_note(wifi, &wifi->ps_tx, 1); + data_tx_run(wifi, more); + if (skb_queue_len(&wifi->data_tx_queue) >= DATA_TX_QUEUE_LIMIT) { netif_stop_queue(netdev); - schedule_work(&wifi->link_work); + /* No frame follows a stopped queue. */ + data_tx_doorbell(wifi); + } + spin_unlock(&wifi->data_tx_lock); + return NETDEV_TX_OK; + +drop: + netdev->stats.tx_dropped++; + dev_kfree_skb_any(skb); + /* The last frame of a batch still owes the doorbell of those before. */ + if (!more && data_tx_lock_xmit(wifi)) { + data_tx_doorbell(wifi); + spin_unlock(&wifi->data_tx_lock); + } return NETDEV_TX_OK; } @@ -3109,6 +3808,7 @@ static int data_ring_open(struct centauri_t6050_wifi *wifi, unsigned int id) u8 message[0x34 + 10] = { 0 }; unsigned int vector, offset, size, bit, doorbell, irq; bool transfer = false; + spinlock_t *lock; /* the data lock that guards ring id */ int ret; lockdep_assert_held(&wifi->lock); @@ -3120,21 +3820,21 @@ static int data_ring_open(struct centauri_t6050_wifi *wifi, unsigned int id) size = 2048; bit = 0; doorbell = 18; - irq = 3; + irq = DATA_TX_VECTOR; } else if (id == 14) { vector = 206; offset = 0x220000; size = 256; bit = 1; doorbell = 11; - irq = 8; + irq = DATA_RX_VECTOR; } else if (id == 15) { vector = 207; offset = 0x240000; size = DATA_RX_SLOTS; bit = 2; doorbell = 11; - irq = 8; + irq = DATA_RX_VECTOR; } else if (id == 24 || id == 25 || id == 12) { transfer = true; vector = id; @@ -3142,7 +3842,7 @@ static int data_ring_open(struct centauri_t6050_wifi *wifi, unsigned int id) size = id == 12 ? DATA_RX_SLOTS : DATA_TX_SLOTS; bit = id == 12 ? 4 : id == 24 ? 3 : 5; doorbell = id == 12 ? 11 : 18; - irq = id == 12 ? 8 : 3; + irq = id == 12 ? DATA_RX_VECTOR : DATA_TX_VECTOR; } else { return -EINVAL; } @@ -3187,7 +3887,6 @@ static int data_ring_open(struct centauri_t6050_wifi *wifi, unsigned int id) wifi->data_failed = true; goto out; } - wifi->data_rings_open |= BIT(bit); if (transfer && id == 12) { unsigned int slot; @@ -3206,6 +3905,14 @@ static int data_ring_open(struct centauri_t6050_wifi *wifi, unsigned int id) dma_wmb(); writel(1, wifi->a.bar + 0x1050); } + /* + * Publish the ring to the data paths only now, under their lock, so + * that they never see it before it is set up. + */ + lock = bit == 2 || bit == 4 ? &wifi->data_rx_lock : &wifi->data_tx_lock; + spin_lock_bh(lock); + WRITE_ONCE(wifi->data_rings_open, wifi->data_rings_open | BIT(bit)); + spin_unlock_bh(lock); out: return ret; } @@ -3243,20 +3950,30 @@ static int query_link_status(struct centauri_t6050_wifi *wifi) static int data_stats_show(struct seq_file *m, void *unused) { struct centauri_t6050_wifi *wifi = m->private; - - mutex_lock(&wifi->lock); + u32 rx[3], tx[9]; + + spin_lock_bh(&wifi->data_rx_lock); + rx[0] = wifi->data_rx_received; + rx[1] = wifi->data_rx_reposted; + rx[2] = wifi->data_rx_bad; + spin_unlock_bh(&wifi->data_rx_lock); + spin_lock_bh(&wifi->data_tx_lock); + tx[0] = wifi->data_tx_submitted; + tx[1] = wifi->data_tx_completed; + tx[2] = wifi->data_tx_submitted - wifi->data_tx_completed; + tx[3] = wifi->data_tx_failed; + tx[4] = wifi->data_tx_bad; + tx[5] = wifi->data_tx_coalesced; + tx[6] = skb_queue_len(&wifi->data_tx_queue); + tx[7] = wifi->data_tx_capped; + tx[8] = wifi->data_tx_cap_bypassed; + spin_unlock_bh(&wifi->data_tx_lock); seq_printf(m, "rx received %u reposted %u bad %u\n" "tx submitted %u completed %u in_flight %u failed %u bad %u coalesced %u\n" "tx queued %u capped %u cap_bypassed %u\n", - wifi->data_rx_received, wifi->data_rx_reposted, - wifi->data_rx_bad, wifi->data_tx_submitted, - wifi->data_tx_completed, - wifi->data_tx_submitted - wifi->data_tx_completed, - wifi->data_tx_failed, wifi->data_tx_bad, - wifi->data_tx_coalesced, skb_queue_len(&wifi->data_tx_queue), - wifi->data_tx_capped, wifi->data_tx_cap_bypassed); - mutex_unlock(&wifi->lock); + rx[0], rx[1], rx[2], tx[0], tx[1], tx[2], tx[3], tx[4], + tx[5], tx[6], tx[7], tx[8]); return 0; } DEFINE_SHOW_ATTRIBUTE(data_stats); @@ -3272,11 +3989,13 @@ int centauri_t6050_wifi_register(struct centauri_t6050 *c) return -ENOMEM; wifi->control = c; mutex_init(&wifi->lock); + spin_lock_init(&wifi->data_rx_lock); + spin_lock_init(&wifi->data_tx_lock); skb_queue_head_init(&wifi->data_tx_queue); - skb_queue_head_init(&wifi->rx_gro_queue); INIT_WORK(&wifi->scan_work, scan_worker); INIT_WORK(&wifi->connect_work, connect_worker); INIT_WORK(&wifi->link_work, link_worker); + INIT_DELAYED_WORK(&wifi->ps_work, ps_worker); smp_store_release(&c->wifi, wifi); ret = centauri_t6050_alpha_resources(c, &wifi->a); if (ret) @@ -3378,10 +4097,24 @@ int centauri_t6050_wifi_register(struct centauri_t6050 *c) SET_NETDEV_DEV(wifi->netdev, &c->pdev->dev); strscpy(wifi->netdev->name, "wlan%d", IFNAMSIZ); wifi->netdev->max_mtu = ETH_DATA_LEN; + /* + * TX copies each frame into its ring slot with skb_copy_bits(), which + * reads paged data as well as linear, so paged frames need no + * linearizing first. No segmentation offload: a slot holds one frame + * of at most BUFFER_SIZE bytes. + */ + wifi->netdev->hw_features = NETIF_F_SG; + wifi->netdev->features = NETIF_F_SG; ret = register_netdev(wifi->netdev); if (ret) goto failed; + if (c->alpha_irqs.count > DATA_RX_VECTOR) { + wifi->data_irq[0] = pci_irq_vector(c->alpha_irqs.pdev, + DATA_TX_VECTOR); + wifi->data_irq[1] = pci_irq_vector(c->alpha_irqs.pdev, + DATA_RX_VECTOR); + } smp_store_release(&wifi->irq_ready, true); schedule_work(&wifi->link_work); @@ -3412,13 +4145,22 @@ void centauri_t6050_wifi_unregister(struct centauri_t6050 *c) cancel_work_sync(&wifi->link_work); mutex_lock(&wifi->lock); cancel_connect_locked(wifi); + /* stopping already closes data_ready(); wait out a path past it. */ + data_quiesce(wifi); mutex_unlock(&wifi->lock); - skb_queue_purge(&wifi->data_tx_queue); + data_tx_flush(wifi); if (wifi->netdev && wifi->netdev->reg_state == NETREG_REGISTERED) unregister_netdev(wifi->netdev); - skb_queue_purge(&wifi->rx_gro_queue); if (wifi->registered) wiphy_unregister(wifi->wiphy); + /* + * The netdev and wiphy callbacks are gone, and the data paths and the + * interrupt are closed: nothing is left to queue these (a command + * fault queues the link worker). Stopping ends a run without queuing + * either again; ps_work goes first, as its commands can fault. + */ + cancel_delayed_work_sync(&wifi->ps_work); + cancel_work_sync(&wifi->link_work); if (wifi->wiphy) wiphy_free(wifi->wiphy); if (wifi->netdev) diff --git a/tools/asahi/centauri-scan-tests.py b/tools/asahi/centauri-scan-tests.py index 914bcf0dc0c1..4019a18bad91 100755 --- a/tools/asahi/centauri-scan-tests.py +++ b/tools/asahi/centauri-scan-tests.py @@ -122,6 +122,11 @@ def extract_fn(source, name): static void fill_station(struct centauri_t6050_wifi *wifi, struct station_info *sinfo); +static void data_counters_read(struct centauri_t6050_wifi *wifi, + struct net_device_stats *counters) +{ + *counters = wifi->netdev->stats; +} static void mutex_lock(int *l) { assert(!*l); *l = 1; } static void mutex_unlock(int *l) { assert(*l); *l = 0; } diff --git a/tools/asahi/centauri-wifi-pm-tests.py b/tools/asahi/centauri-wifi-pm-tests.py index 4e21cf3aebec..f5498e37aedb 100644 --- a/tools/asahi/centauri-wifi-pm-tests.py +++ b/tools/asahi/centauri-wifi-pm-tests.py @@ -71,8 +71,9 @@ def block(source, name): struct centauri_t6050_wifi { struct centauri_t6050 *control; struct { u8 *window, *bar; uint64_t window_dma; } a; - int lock, connect_work, link_work, data_tx_queue; + int lock, connect_work, link_work, data_tx_queue, data_tx_lock; bool command_failed, stage_fault_logged, stopping, connect_abort, disconnect_pending, connecting, connected; + bool data_paused; bool reply_valid, pm_prepared, pm_wowlan_armed; bool data_tx_busy[DATA_TX_SLOTS]; bool host_events_fallback_logged; @@ -138,6 +139,8 @@ def block(source, name): return command_reply(wifi, id, payload, size, NULL, NULL, NULL); } static void command_fault(struct centauri_t6050_wifi *wifi) { wifi->command_failed = true; } +static void data_tx_fail(struct centauri_t6050_wifi *wifi) +{ assert(wifi->lock); wifi->command_failed = true; } static void centauri_t6050_fault_report(struct centauri_t6050 *c, const char *why) { (void)c; (void)why; fault_reports++; } static void drain(struct centauri_t6050_wifi *wifi) @@ -179,7 +182,13 @@ def block(source, name): static void cancel_connect_locked(struct centauri_t6050_wifi *wifi) { assert(wifi->lock); } static void netif_stop_queue(struct net_device *netdev) { (void)netdev; } static void netif_carrier_off(struct net_device *netdev) { (void)netdev; } -static void skb_queue_purge(int *queue) { (void)queue; } +static void spin_lock_bh(int *lock) { assert(!*lock); *lock = 1; } +static void spin_unlock_bh(int *lock) { assert(*lock); *lock = 0; } +static void data_tx_flush(struct centauri_t6050_wifi *wifi) { (void)wifi; } +static void data_quiesce(struct centauri_t6050_wifi *wifi) +{ assert(wifi->lock); wifi->data_paused = true; } +static void data_unquiesce(struct centauri_t6050_wifi *wifi) +{ assert(wifi->lock); wifi->data_paused = false; } static void cfg80211_disconnected(struct net_device *netdev, u16 reason, void *ie, int size, bool local, int flags) { (void)netdev; (void)reason; (void)ie; (void)size; (void)local; (void)flags; notifications++; }