Skip to content

Commit 3fab2d2

Browse files
author
Paolo Abeni
committed
Merge tag 'linux-can-fixes-for-6.15-20250521' of git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can
Marc Kleine-Budde says: ==================== pull-request: can 2025-05-22 this is a pull request of 4 patches for net/main. The first 3 patches are by Axel Forsman and fix a ISR race condition in the kvaser_pciefd driver. The last patch is by Carlos Sanchez and fixes the reception of short error messages in the slcan driver. * tag 'linux-can-fixes-for-6.15-20250521' of git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can: can: slcan: allow reception of short error messages can: kvaser_pciefd: Continue parsing DMA buf after dropped RX can: kvaser_pciefd: Fix echo_skb race can: kvaser_pciefd: Force IRQ edge in case of nested IRQ ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Paolo Abeni <[email protected]>
2 parents ab94003 + ef0841e commit 3fab2d2

File tree

2 files changed

+122
-86
lines changed

2 files changed

+122
-86
lines changed

drivers/net/can/kvaser_pciefd.c

Lines changed: 102 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/netdevice.h>
1717
#include <linux/pci.h>
1818
#include <linux/timer.h>
19+
#include <net/netdev_queues.h>
1920

2021
MODULE_LICENSE("Dual BSD/GPL");
2122
MODULE_AUTHOR("Kvaser AB <[email protected]>");
@@ -410,10 +411,13 @@ struct kvaser_pciefd_can {
410411
void __iomem *reg_base;
411412
struct can_berr_counter bec;
412413
u8 cmd_seq;
414+
u8 tx_max_count;
415+
u8 tx_idx;
416+
u8 ack_idx;
413417
int err_rep_cnt;
414-
int echo_idx;
418+
unsigned int completed_tx_pkts;
419+
unsigned int completed_tx_bytes;
415420
spinlock_t lock; /* Locks sensitive registers (e.g. MODE) */
416-
spinlock_t echo_lock; /* Locks the message echo buffer */
417421
struct timer_list bec_poll_timer;
418422
struct completion start_comp, flush_comp;
419423
};
@@ -714,6 +718,9 @@ static int kvaser_pciefd_open(struct net_device *netdev)
714718
int ret;
715719
struct kvaser_pciefd_can *can = netdev_priv(netdev);
716720

721+
can->tx_idx = 0;
722+
can->ack_idx = 0;
723+
717724
ret = open_candev(netdev);
718725
if (ret)
719726
return ret;
@@ -745,21 +752,26 @@ static int kvaser_pciefd_stop(struct net_device *netdev)
745752
timer_delete(&can->bec_poll_timer);
746753
}
747754
can->can.state = CAN_STATE_STOPPED;
755+
netdev_reset_queue(netdev);
748756
close_candev(netdev);
749757

750758
return ret;
751759
}
752760

761+
static unsigned int kvaser_pciefd_tx_avail(const struct kvaser_pciefd_can *can)
762+
{
763+
return can->tx_max_count - (READ_ONCE(can->tx_idx) - READ_ONCE(can->ack_idx));
764+
}
765+
753766
static int kvaser_pciefd_prepare_tx_packet(struct kvaser_pciefd_tx_packet *p,
754-
struct kvaser_pciefd_can *can,
767+
struct can_priv *can, u8 seq,
755768
struct sk_buff *skb)
756769
{
757770
struct canfd_frame *cf = (struct canfd_frame *)skb->data;
758771
int packet_size;
759-
int seq = can->echo_idx;
760772

761773
memset(p, 0, sizeof(*p));
762-
if (can->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
774+
if (can->ctrlmode & CAN_CTRLMODE_ONE_SHOT)
763775
p->header[1] |= KVASER_PCIEFD_TPACKET_SMS;
764776

765777
if (cf->can_id & CAN_RTR_FLAG)
@@ -782,7 +794,7 @@ static int kvaser_pciefd_prepare_tx_packet(struct kvaser_pciefd_tx_packet *p,
782794
} else {
783795
p->header[1] |=
784796
FIELD_PREP(KVASER_PCIEFD_RPACKET_DLC_MASK,
785-
can_get_cc_dlc((struct can_frame *)cf, can->can.ctrlmode));
797+
can_get_cc_dlc((struct can_frame *)cf, can->ctrlmode));
786798
}
787799

788800
p->header[1] |= FIELD_PREP(KVASER_PCIEFD_PACKET_SEQ_MASK, seq);
@@ -797,22 +809,24 @@ static netdev_tx_t kvaser_pciefd_start_xmit(struct sk_buff *skb,
797809
struct net_device *netdev)
798810
{
799811
struct kvaser_pciefd_can *can = netdev_priv(netdev);
800-
unsigned long irq_flags;
801812
struct kvaser_pciefd_tx_packet packet;
813+
unsigned int seq = can->tx_idx & (can->can.echo_skb_max - 1);
814+
unsigned int frame_len;
802815
int nr_words;
803-
u8 count;
804816

805817
if (can_dev_dropped_skb(netdev, skb))
806818
return NETDEV_TX_OK;
819+
if (!netif_subqueue_maybe_stop(netdev, 0, kvaser_pciefd_tx_avail(can), 1, 1))
820+
return NETDEV_TX_BUSY;
807821

808-
nr_words = kvaser_pciefd_prepare_tx_packet(&packet, can, skb);
822+
nr_words = kvaser_pciefd_prepare_tx_packet(&packet, &can->can, seq, skb);
809823

810-
spin_lock_irqsave(&can->echo_lock, irq_flags);
811824
/* Prepare and save echo skb in internal slot */
812-
can_put_echo_skb(skb, netdev, can->echo_idx, 0);
813-
814-
/* Move echo index to the next slot */
815-
can->echo_idx = (can->echo_idx + 1) % can->can.echo_skb_max;
825+
WRITE_ONCE(can->can.echo_skb[seq], NULL);
826+
frame_len = can_skb_get_frame_len(skb);
827+
can_put_echo_skb(skb, netdev, seq, frame_len);
828+
netdev_sent_queue(netdev, frame_len);
829+
WRITE_ONCE(can->tx_idx, can->tx_idx + 1);
816830

817831
/* Write header to fifo */
818832
iowrite32(packet.header[0],
@@ -836,14 +850,7 @@ static netdev_tx_t kvaser_pciefd_start_xmit(struct sk_buff *skb,
836850
KVASER_PCIEFD_KCAN_FIFO_LAST_REG);
837851
}
838852

839-
count = FIELD_GET(KVASER_PCIEFD_KCAN_TX_NR_PACKETS_CURRENT_MASK,
840-
ioread32(can->reg_base + KVASER_PCIEFD_KCAN_TX_NR_PACKETS_REG));
841-
/* No room for a new message, stop the queue until at least one
842-
* successful transmit
843-
*/
844-
if (count >= can->can.echo_skb_max || can->can.echo_skb[can->echo_idx])
845-
netif_stop_queue(netdev);
846-
spin_unlock_irqrestore(&can->echo_lock, irq_flags);
853+
netif_subqueue_maybe_stop(netdev, 0, kvaser_pciefd_tx_avail(can), 1, 1);
847854

848855
return NETDEV_TX_OK;
849856
}
@@ -970,6 +977,8 @@ static int kvaser_pciefd_setup_can_ctrls(struct kvaser_pciefd *pcie)
970977
can->kv_pcie = pcie;
971978
can->cmd_seq = 0;
972979
can->err_rep_cnt = 0;
980+
can->completed_tx_pkts = 0;
981+
can->completed_tx_bytes = 0;
973982
can->bec.txerr = 0;
974983
can->bec.rxerr = 0;
975984

@@ -983,11 +992,10 @@ static int kvaser_pciefd_setup_can_ctrls(struct kvaser_pciefd *pcie)
983992
tx_nr_packets_max =
984993
FIELD_GET(KVASER_PCIEFD_KCAN_TX_NR_PACKETS_MAX_MASK,
985994
ioread32(can->reg_base + KVASER_PCIEFD_KCAN_TX_NR_PACKETS_REG));
995+
can->tx_max_count = min(KVASER_PCIEFD_CAN_TX_MAX_COUNT, tx_nr_packets_max - 1);
986996

987997
can->can.clock.freq = pcie->freq;
988-
can->can.echo_skb_max = min(KVASER_PCIEFD_CAN_TX_MAX_COUNT, tx_nr_packets_max - 1);
989-
can->echo_idx = 0;
990-
spin_lock_init(&can->echo_lock);
998+
can->can.echo_skb_max = roundup_pow_of_two(can->tx_max_count);
991999
spin_lock_init(&can->lock);
9921000

9931001
can->can.bittiming_const = &kvaser_pciefd_bittiming_const;
@@ -1201,7 +1209,7 @@ static int kvaser_pciefd_handle_data_packet(struct kvaser_pciefd *pcie,
12011209
skb = alloc_canfd_skb(priv->dev, &cf);
12021210
if (!skb) {
12031211
priv->dev->stats.rx_dropped++;
1204-
return -ENOMEM;
1212+
return 0;
12051213
}
12061214

12071215
cf->len = can_fd_dlc2len(dlc);
@@ -1213,7 +1221,7 @@ static int kvaser_pciefd_handle_data_packet(struct kvaser_pciefd *pcie,
12131221
skb = alloc_can_skb(priv->dev, (struct can_frame **)&cf);
12141222
if (!skb) {
12151223
priv->dev->stats.rx_dropped++;
1216-
return -ENOMEM;
1224+
return 0;
12171225
}
12181226
can_frame_set_cc_len((struct can_frame *)cf, dlc, priv->ctrlmode);
12191227
}
@@ -1231,7 +1239,9 @@ static int kvaser_pciefd_handle_data_packet(struct kvaser_pciefd *pcie,
12311239
priv->dev->stats.rx_packets++;
12321240
kvaser_pciefd_set_skb_timestamp(pcie, skb, p->timestamp);
12331241

1234-
return netif_rx(skb);
1242+
netif_rx(skb);
1243+
1244+
return 0;
12351245
}
12361246

12371247
static void kvaser_pciefd_change_state(struct kvaser_pciefd_can *can,
@@ -1510,19 +1520,21 @@ static int kvaser_pciefd_handle_ack_packet(struct kvaser_pciefd *pcie,
15101520
netdev_dbg(can->can.dev, "Packet was flushed\n");
15111521
} else {
15121522
int echo_idx = FIELD_GET(KVASER_PCIEFD_PACKET_SEQ_MASK, p->header[0]);
1513-
int len;
1514-
u8 count;
1523+
unsigned int len, frame_len = 0;
15151524
struct sk_buff *skb;
15161525

1526+
if (echo_idx != (can->ack_idx & (can->can.echo_skb_max - 1)))
1527+
return 0;
15171528
skb = can->can.echo_skb[echo_idx];
1518-
if (skb)
1519-
kvaser_pciefd_set_skb_timestamp(pcie, skb, p->timestamp);
1520-
len = can_get_echo_skb(can->can.dev, echo_idx, NULL);
1521-
count = FIELD_GET(KVASER_PCIEFD_KCAN_TX_NR_PACKETS_CURRENT_MASK,
1522-
ioread32(can->reg_base + KVASER_PCIEFD_KCAN_TX_NR_PACKETS_REG));
1529+
if (!skb)
1530+
return 0;
1531+
kvaser_pciefd_set_skb_timestamp(pcie, skb, p->timestamp);
1532+
len = can_get_echo_skb(can->can.dev, echo_idx, &frame_len);
15231533

1524-
if (count < can->can.echo_skb_max && netif_queue_stopped(can->can.dev))
1525-
netif_wake_queue(can->can.dev);
1534+
/* Pairs with barrier in kvaser_pciefd_start_xmit() */
1535+
smp_store_release(&can->ack_idx, can->ack_idx + 1);
1536+
can->completed_tx_pkts++;
1537+
can->completed_tx_bytes += frame_len;
15261538

15271539
if (!one_shot_fail) {
15281540
can->can.dev->stats.tx_bytes += len;
@@ -1638,32 +1650,51 @@ static int kvaser_pciefd_read_buffer(struct kvaser_pciefd *pcie, int dma_buf)
16381650
{
16391651
int pos = 0;
16401652
int res = 0;
1653+
unsigned int i;
16411654

16421655
do {
16431656
res = kvaser_pciefd_read_packet(pcie, &pos, dma_buf);
16441657
} while (!res && pos > 0 && pos < KVASER_PCIEFD_DMA_SIZE);
16451658

1659+
/* Report ACKs in this buffer to BQL en masse for correct periods */
1660+
for (i = 0; i < pcie->nr_channels; ++i) {
1661+
struct kvaser_pciefd_can *can = pcie->can[i];
1662+
1663+
if (!can->completed_tx_pkts)
1664+
continue;
1665+
netif_subqueue_completed_wake(can->can.dev, 0,
1666+
can->completed_tx_pkts,
1667+
can->completed_tx_bytes,
1668+
kvaser_pciefd_tx_avail(can), 1);
1669+
can->completed_tx_pkts = 0;
1670+
can->completed_tx_bytes = 0;
1671+
}
1672+
16461673
return res;
16471674
}
16481675

1649-
static u32 kvaser_pciefd_receive_irq(struct kvaser_pciefd *pcie)
1676+
static void kvaser_pciefd_receive_irq(struct kvaser_pciefd *pcie)
16501677
{
1678+
void __iomem *srb_cmd_reg = KVASER_PCIEFD_SRB_ADDR(pcie) + KVASER_PCIEFD_SRB_CMD_REG;
16511679
u32 irq = ioread32(KVASER_PCIEFD_SRB_ADDR(pcie) + KVASER_PCIEFD_SRB_IRQ_REG);
16521680

1653-
if (irq & KVASER_PCIEFD_SRB_IRQ_DPD0)
1681+
iowrite32(irq, KVASER_PCIEFD_SRB_ADDR(pcie) + KVASER_PCIEFD_SRB_IRQ_REG);
1682+
1683+
if (irq & KVASER_PCIEFD_SRB_IRQ_DPD0) {
16541684
kvaser_pciefd_read_buffer(pcie, 0);
1685+
iowrite32(KVASER_PCIEFD_SRB_CMD_RDB0, srb_cmd_reg); /* Rearm buffer */
1686+
}
16551687

1656-
if (irq & KVASER_PCIEFD_SRB_IRQ_DPD1)
1688+
if (irq & KVASER_PCIEFD_SRB_IRQ_DPD1) {
16571689
kvaser_pciefd_read_buffer(pcie, 1);
1690+
iowrite32(KVASER_PCIEFD_SRB_CMD_RDB1, srb_cmd_reg); /* Rearm buffer */
1691+
}
16581692

16591693
if (unlikely(irq & KVASER_PCIEFD_SRB_IRQ_DOF0 ||
16601694
irq & KVASER_PCIEFD_SRB_IRQ_DOF1 ||
16611695
irq & KVASER_PCIEFD_SRB_IRQ_DUF0 ||
16621696
irq & KVASER_PCIEFD_SRB_IRQ_DUF1))
16631697
dev_err(&pcie->pci->dev, "DMA IRQ error 0x%08X\n", irq);
1664-
1665-
iowrite32(irq, KVASER_PCIEFD_SRB_ADDR(pcie) + KVASER_PCIEFD_SRB_IRQ_REG);
1666-
return irq;
16671698
}
16681699

16691700
static void kvaser_pciefd_transmit_irq(struct kvaser_pciefd_can *can)
@@ -1691,29 +1722,22 @@ static irqreturn_t kvaser_pciefd_irq_handler(int irq, void *dev)
16911722
struct kvaser_pciefd *pcie = (struct kvaser_pciefd *)dev;
16921723
const struct kvaser_pciefd_irq_mask *irq_mask = pcie->driver_data->irq_mask;
16931724
u32 pci_irq = ioread32(KVASER_PCIEFD_PCI_IRQ_ADDR(pcie));
1694-
u32 srb_irq = 0;
1695-
u32 srb_release = 0;
16961725
int i;
16971726

16981727
if (!(pci_irq & irq_mask->all))
16991728
return IRQ_NONE;
17001729

1730+
iowrite32(0, KVASER_PCIEFD_PCI_IEN_ADDR(pcie));
1731+
17011732
if (pci_irq & irq_mask->kcan_rx0)
1702-
srb_irq = kvaser_pciefd_receive_irq(pcie);
1733+
kvaser_pciefd_receive_irq(pcie);
17031734

17041735
for (i = 0; i < pcie->nr_channels; i++) {
17051736
if (pci_irq & irq_mask->kcan_tx[i])
17061737
kvaser_pciefd_transmit_irq(pcie->can[i]);
17071738
}
17081739

1709-
if (srb_irq & KVASER_PCIEFD_SRB_IRQ_DPD0)
1710-
srb_release |= KVASER_PCIEFD_SRB_CMD_RDB0;
1711-
1712-
if (srb_irq & KVASER_PCIEFD_SRB_IRQ_DPD1)
1713-
srb_release |= KVASER_PCIEFD_SRB_CMD_RDB1;
1714-
1715-
if (srb_release)
1716-
iowrite32(srb_release, KVASER_PCIEFD_SRB_ADDR(pcie) + KVASER_PCIEFD_SRB_CMD_REG);
1740+
iowrite32(irq_mask->all, KVASER_PCIEFD_PCI_IEN_ADDR(pcie));
17171741

17181742
return IRQ_HANDLED;
17191743
}
@@ -1733,13 +1757,22 @@ static void kvaser_pciefd_teardown_can_ctrls(struct kvaser_pciefd *pcie)
17331757
}
17341758
}
17351759

1760+
static void kvaser_pciefd_disable_irq_srcs(struct kvaser_pciefd *pcie)
1761+
{
1762+
unsigned int i;
1763+
1764+
/* Masking PCI_IRQ is insufficient as running ISR will unmask it */
1765+
iowrite32(0, KVASER_PCIEFD_SRB_ADDR(pcie) + KVASER_PCIEFD_SRB_IEN_REG);
1766+
for (i = 0; i < pcie->nr_channels; ++i)
1767+
iowrite32(0, pcie->can[i]->reg_base + KVASER_PCIEFD_KCAN_IEN_REG);
1768+
}
1769+
17361770
static int kvaser_pciefd_probe(struct pci_dev *pdev,
17371771
const struct pci_device_id *id)
17381772
{
17391773
int ret;
17401774
struct kvaser_pciefd *pcie;
17411775
const struct kvaser_pciefd_irq_mask *irq_mask;
1742-
void __iomem *irq_en_base;
17431776

17441777
pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
17451778
if (!pcie)
@@ -1805,8 +1838,7 @@ static int kvaser_pciefd_probe(struct pci_dev *pdev,
18051838
KVASER_PCIEFD_SRB_ADDR(pcie) + KVASER_PCIEFD_SRB_IEN_REG);
18061839

18071840
/* Enable PCI interrupts */
1808-
irq_en_base = KVASER_PCIEFD_PCI_IEN_ADDR(pcie);
1809-
iowrite32(irq_mask->all, irq_en_base);
1841+
iowrite32(irq_mask->all, KVASER_PCIEFD_PCI_IEN_ADDR(pcie));
18101842
/* Ready the DMA buffers */
18111843
iowrite32(KVASER_PCIEFD_SRB_CMD_RDB0,
18121844
KVASER_PCIEFD_SRB_ADDR(pcie) + KVASER_PCIEFD_SRB_CMD_REG);
@@ -1820,8 +1852,7 @@ static int kvaser_pciefd_probe(struct pci_dev *pdev,
18201852
return 0;
18211853

18221854
err_free_irq:
1823-
/* Disable PCI interrupts */
1824-
iowrite32(0, irq_en_base);
1855+
kvaser_pciefd_disable_irq_srcs(pcie);
18251856
free_irq(pcie->pci->irq, pcie);
18261857

18271858
err_pci_free_irq_vectors:
@@ -1844,35 +1875,26 @@ static int kvaser_pciefd_probe(struct pci_dev *pdev,
18441875
return ret;
18451876
}
18461877

1847-
static void kvaser_pciefd_remove_all_ctrls(struct kvaser_pciefd *pcie)
1848-
{
1849-
int i;
1850-
1851-
for (i = 0; i < pcie->nr_channels; i++) {
1852-
struct kvaser_pciefd_can *can = pcie->can[i];
1853-
1854-
if (can) {
1855-
iowrite32(0, can->reg_base + KVASER_PCIEFD_KCAN_IEN_REG);
1856-
unregister_candev(can->can.dev);
1857-
timer_delete(&can->bec_poll_timer);
1858-
kvaser_pciefd_pwm_stop(can);
1859-
free_candev(can->can.dev);
1860-
}
1861-
}
1862-
}
1863-
18641878
static void kvaser_pciefd_remove(struct pci_dev *pdev)
18651879
{
18661880
struct kvaser_pciefd *pcie = pci_get_drvdata(pdev);
1881+
unsigned int i;
18671882

1868-
kvaser_pciefd_remove_all_ctrls(pcie);
1883+
for (i = 0; i < pcie->nr_channels; ++i) {
1884+
struct kvaser_pciefd_can *can = pcie->can[i];
18691885

1870-
/* Disable interrupts */
1871-
iowrite32(0, KVASER_PCIEFD_SRB_ADDR(pcie) + KVASER_PCIEFD_SRB_CTRL_REG);
1872-
iowrite32(0, KVASER_PCIEFD_PCI_IEN_ADDR(pcie));
1886+
unregister_candev(can->can.dev);
1887+
timer_delete(&can->bec_poll_timer);
1888+
kvaser_pciefd_pwm_stop(can);
1889+
}
18731890

1891+
kvaser_pciefd_disable_irq_srcs(pcie);
18741892
free_irq(pcie->pci->irq, pcie);
18751893
pci_free_irq_vectors(pcie->pci);
1894+
1895+
for (i = 0; i < pcie->nr_channels; ++i)
1896+
free_candev(pcie->can[i]->can.dev);
1897+
18761898
pci_iounmap(pdev, pcie->reg_base);
18771899
pci_release_regions(pdev);
18781900
pci_disable_device(pdev);

0 commit comments

Comments
 (0)