@@ -52,14 +52,10 @@ struct sifive_fu540_macb_mgmt {
5252#define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
5353#define MIN_RX_RING_SIZE 64
5454#define MAX_RX_RING_SIZE 8192
55- #define RX_RING_BYTES (bp ) (macb_dma_desc_get_size(bp) \
56- * (bp)->rx_ring_size)
5755
5856#define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
5957#define MIN_TX_RING_SIZE 64
6058#define MAX_TX_RING_SIZE 4096
61- #define TX_RING_BYTES (bp ) (macb_dma_desc_get_size(bp) \
62- * (bp)->tx_ring_size)
6359
6460/* level of occupied TX descriptors under which we wake up TX process */
6561#define MACB_TX_WAKEUP_THRESH (bp ) (3 * (bp)->tx_ring_size / 4)
@@ -279,9 +275,9 @@ static void macb_set_hwaddr(struct macb *bp)
279275 u32 bottom ;
280276 u16 top ;
281277
282- bottom = cpu_to_le32 ( * (( u32 * ) bp -> dev -> dev_addr ) );
278+ bottom = get_unaligned_le32 ( bp -> dev -> dev_addr );
283279 macb_or_gem_writel (bp , SA1B , bottom );
284- top = cpu_to_le16 ( * (( u16 * )( bp -> dev -> dev_addr + 4 )) );
280+ top = get_unaligned_le16 ( bp -> dev -> dev_addr + 4 );
285281 macb_or_gem_writel (bp , SA1T , top );
286282
287283 if (gem_has_ptp (bp )) {
@@ -496,19 +492,19 @@ static void macb_init_buffers(struct macb *bp)
496492 struct macb_queue * queue ;
497493 unsigned int q ;
498494
499- for (q = 0 , queue = bp -> queues ; q < bp -> num_queues ; ++ q , ++ queue ) {
500- queue_writel (queue , RBQP , lower_32_bits (queue -> rx_ring_dma ));
501495#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
502- if (bp -> hw_dma_cap & HW_DMA_CAP_64B )
503- queue_writel (queue , RBQPH ,
504- upper_32_bits (queue -> rx_ring_dma ));
496+ /* Single register for all queues' high 32 bits. */
497+ if (bp -> hw_dma_cap & HW_DMA_CAP_64B ) {
498+ macb_writel (bp , RBQPH ,
499+ upper_32_bits (bp -> queues [0 ].rx_ring_dma ));
500+ macb_writel (bp , TBQPH ,
501+ upper_32_bits (bp -> queues [0 ].tx_ring_dma ));
502+ }
505503#endif
504+
505+ for (q = 0 , queue = bp -> queues ; q < bp -> num_queues ; ++ q , ++ queue ) {
506+ queue_writel (queue , RBQP , lower_32_bits (queue -> rx_ring_dma ));
506507 queue_writel (queue , TBQP , lower_32_bits (queue -> tx_ring_dma ));
507- #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
508- if (bp -> hw_dma_cap & HW_DMA_CAP_64B )
509- queue_writel (queue , TBQPH ,
510- upper_32_bits (queue -> tx_ring_dma ));
511- #endif
512508 }
513509}
514510
@@ -1167,10 +1163,6 @@ static void macb_tx_error_task(struct work_struct *work)
11671163
11681164 /* Reinitialize the TX desc queue */
11691165 queue_writel (queue , TBQP , lower_32_bits (queue -> tx_ring_dma ));
1170- #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1171- if (bp -> hw_dma_cap & HW_DMA_CAP_64B )
1172- queue_writel (queue , TBQPH , upper_32_bits (queue -> tx_ring_dma ));
1173- #endif
11741166 /* Make TX ring reflect state of hardware */
11751167 queue -> tx_head = 0 ;
11761168 queue -> tx_tail = 0 ;
@@ -2475,35 +2467,42 @@ static void macb_free_rx_buffers(struct macb *bp)
24752467 }
24762468}
24772469
2470+ static unsigned int macb_tx_ring_size_per_queue (struct macb * bp )
2471+ {
2472+ return macb_dma_desc_get_size (bp ) * bp -> tx_ring_size + bp -> tx_bd_rd_prefetch ;
2473+ }
2474+
2475+ static unsigned int macb_rx_ring_size_per_queue (struct macb * bp )
2476+ {
2477+ return macb_dma_desc_get_size (bp ) * bp -> rx_ring_size + bp -> rx_bd_rd_prefetch ;
2478+ }
2479+
24782480static void macb_free_consistent (struct macb * bp )
24792481{
2482+ struct device * dev = & bp -> pdev -> dev ;
24802483 struct macb_queue * queue ;
24812484 unsigned int q ;
2482- int size ;
2485+ size_t size ;
24832486
24842487 if (bp -> rx_ring_tieoff ) {
2485- dma_free_coherent (& bp -> pdev -> dev , macb_dma_desc_get_size (bp ),
2488+ dma_free_coherent (dev , macb_dma_desc_get_size (bp ),
24862489 bp -> rx_ring_tieoff , bp -> rx_ring_tieoff_dma );
24872490 bp -> rx_ring_tieoff = NULL ;
24882491 }
24892492
24902493 bp -> macbgem_ops .mog_free_rx_buffers (bp );
24912494
2495+ size = bp -> num_queues * macb_tx_ring_size_per_queue (bp );
2496+ dma_free_coherent (dev , size , bp -> queues [0 ].tx_ring , bp -> queues [0 ].tx_ring_dma );
2497+
2498+ size = bp -> num_queues * macb_rx_ring_size_per_queue (bp );
2499+ dma_free_coherent (dev , size , bp -> queues [0 ].rx_ring , bp -> queues [0 ].rx_ring_dma );
2500+
24922501 for (q = 0 , queue = bp -> queues ; q < bp -> num_queues ; ++ q , ++ queue ) {
24932502 kfree (queue -> tx_skb );
24942503 queue -> tx_skb = NULL ;
2495- if (queue -> tx_ring ) {
2496- size = TX_RING_BYTES (bp ) + bp -> tx_bd_rd_prefetch ;
2497- dma_free_coherent (& bp -> pdev -> dev , size ,
2498- queue -> tx_ring , queue -> tx_ring_dma );
2499- queue -> tx_ring = NULL ;
2500- }
2501- if (queue -> rx_ring ) {
2502- size = RX_RING_BYTES (bp ) + bp -> rx_bd_rd_prefetch ;
2503- dma_free_coherent (& bp -> pdev -> dev , size ,
2504- queue -> rx_ring , queue -> rx_ring_dma );
2505- queue -> rx_ring = NULL ;
2506- }
2504+ queue -> tx_ring = NULL ;
2505+ queue -> rx_ring = NULL ;
25072506 }
25082507}
25092508
@@ -2545,35 +2544,45 @@ static int macb_alloc_rx_buffers(struct macb *bp)
25452544
25462545static int macb_alloc_consistent (struct macb * bp )
25472546{
2547+ struct device * dev = & bp -> pdev -> dev ;
2548+ dma_addr_t tx_dma , rx_dma ;
25482549 struct macb_queue * queue ;
25492550 unsigned int q ;
2550- int size ;
2551+ void * tx , * rx ;
2552+ size_t size ;
2553+
2554+ /*
2555+ * Upper 32-bits of Tx/Rx DMA descriptor for each queues much match!
2556+ * We cannot enforce this guarantee, the best we can do is do a single
2557+ * allocation and hope it will land into alloc_pages() that guarantees
2558+ * natural alignment of physical addresses.
2559+ */
2560+
2561+ size = bp -> num_queues * macb_tx_ring_size_per_queue (bp );
2562+ tx = dma_alloc_coherent (dev , size , & tx_dma , GFP_KERNEL );
2563+ if (!tx || upper_32_bits (tx_dma ) != upper_32_bits (tx_dma + size - 1 ))
2564+ goto out_err ;
2565+ netdev_dbg (bp -> dev , "Allocated %zu bytes for %u TX rings at %08lx (mapped %p)\n" ,
2566+ size , bp -> num_queues , (unsigned long )tx_dma , tx );
2567+
2568+ size = bp -> num_queues * macb_rx_ring_size_per_queue (bp );
2569+ rx = dma_alloc_coherent (dev , size , & rx_dma , GFP_KERNEL );
2570+ if (!rx || upper_32_bits (rx_dma ) != upper_32_bits (rx_dma + size - 1 ))
2571+ goto out_err ;
2572+ netdev_dbg (bp -> dev , "Allocated %zu bytes for %u RX rings at %08lx (mapped %p)\n" ,
2573+ size , bp -> num_queues , (unsigned long )rx_dma , rx );
25512574
25522575 for (q = 0 , queue = bp -> queues ; q < bp -> num_queues ; ++ q , ++ queue ) {
2553- size = TX_RING_BYTES (bp ) + bp -> tx_bd_rd_prefetch ;
2554- queue -> tx_ring = dma_alloc_coherent (& bp -> pdev -> dev , size ,
2555- & queue -> tx_ring_dma ,
2556- GFP_KERNEL );
2557- if (!queue -> tx_ring )
2558- goto out_err ;
2559- netdev_dbg (bp -> dev ,
2560- "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n" ,
2561- q , size , (unsigned long )queue -> tx_ring_dma ,
2562- queue -> tx_ring );
2576+ queue -> tx_ring = tx + macb_tx_ring_size_per_queue (bp ) * q ;
2577+ queue -> tx_ring_dma = tx_dma + macb_tx_ring_size_per_queue (bp ) * q ;
2578+
2579+ queue -> rx_ring = rx + macb_rx_ring_size_per_queue (bp ) * q ;
2580+ queue -> rx_ring_dma = rx_dma + macb_rx_ring_size_per_queue (bp ) * q ;
25632581
25642582 size = bp -> tx_ring_size * sizeof (struct macb_tx_skb );
25652583 queue -> tx_skb = kmalloc (size , GFP_KERNEL );
25662584 if (!queue -> tx_skb )
25672585 goto out_err ;
2568-
2569- size = RX_RING_BYTES (bp ) + bp -> rx_bd_rd_prefetch ;
2570- queue -> rx_ring = dma_alloc_coherent (& bp -> pdev -> dev , size ,
2571- & queue -> rx_ring_dma , GFP_KERNEL );
2572- if (!queue -> rx_ring )
2573- goto out_err ;
2574- netdev_dbg (bp -> dev ,
2575- "Allocated RX ring of %d bytes at %08lx (mapped %p)\n" ,
2576- size , (unsigned long )queue -> rx_ring_dma , queue -> rx_ring );
25772586 }
25782587 if (bp -> macbgem_ops .mog_alloc_rx_buffers (bp ))
25792588 goto out_err ;
@@ -4528,12 +4537,6 @@ static int macb_init(struct platform_device *pdev)
45284537 queue -> TBQP = GEM_TBQP (hw_q - 1 );
45294538 queue -> RBQP = GEM_RBQP (hw_q - 1 );
45304539 queue -> RBQS = GEM_RBQS (hw_q - 1 );
4531- #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4532- if (bp -> hw_dma_cap & HW_DMA_CAP_64B ) {
4533- queue -> TBQPH = GEM_TBQPH (hw_q - 1 );
4534- queue -> RBQPH = GEM_RBQPH (hw_q - 1 );
4535- }
4536- #endif
45374540 } else {
45384541 /* queue0 uses legacy registers */
45394542 queue -> ISR = MACB_ISR ;
@@ -4542,12 +4545,6 @@ static int macb_init(struct platform_device *pdev)
45424545 queue -> IMR = MACB_IMR ;
45434546 queue -> TBQP = MACB_TBQP ;
45444547 queue -> RBQP = MACB_RBQP ;
4545- #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4546- if (bp -> hw_dma_cap & HW_DMA_CAP_64B ) {
4547- queue -> TBQPH = MACB_TBQPH ;
4548- queue -> RBQPH = MACB_RBQPH ;
4549- }
4550- #endif
45514548 }
45524549
45534550 queue -> ENST_START_TIME = GEM_ENST_START_TIME (hw_q );
@@ -5701,6 +5698,11 @@ static int __maybe_unused macb_suspend(struct device *dev)
57015698 */
57025699 tmp = macb_readl (bp , NCR );
57035700 macb_writel (bp , NCR , tmp & ~(MACB_BIT (TE ) | MACB_BIT (RE )));
5701+ #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
5702+ if (!(bp -> caps & MACB_CAPS_QUEUE_DISABLE ))
5703+ macb_writel (bp , RBQPH ,
5704+ upper_32_bits (bp -> rx_ring_tieoff_dma ));
5705+ #endif
57045706 for (q = 0 , queue = bp -> queues ; q < bp -> num_queues ;
57055707 ++ q , ++ queue ) {
57065708 /* Disable RX queues */
@@ -5710,10 +5712,6 @@ static int __maybe_unused macb_suspend(struct device *dev)
57105712 /* Tie off RX queues */
57115713 queue_writel (queue , RBQP ,
57125714 lower_32_bits (bp -> rx_ring_tieoff_dma ));
5713- #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
5714- queue_writel (queue , RBQPH ,
5715- upper_32_bits (bp -> rx_ring_tieoff_dma ));
5716- #endif
57175715 }
57185716 /* Disable all interrupts */
57195717 queue_writel (queue , IDR , -1 );
0 commit comments