[PATCH 09/11] staging: et131x: simplify tx dma code

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



combine two dma_alloc_coherent to one

Signed-off-by: ZHAO Gang <gamerh2o@xxxxxxxxx>
---
 drivers/staging/et131x/et131x.c | 70 +++++++++++------------------------------
 1 file changed, 19 insertions(+), 51 deletions(-)

diff --git a/drivers/staging/et131x/et131x.c b/drivers/staging/et131x/et131x.c
index a3d1b53..3fa1763 100644
--- a/drivers/staging/et131x/et131x.c
+++ b/drivers/staging/et131x/et131x.c
@@ -2708,12 +2708,7 @@ static void et131x_handle_recv_interrupt(struct et131x_adapter *adapter)
 		adapter->rx_ring.unfinished_receives = false;
 }
 
-/* et131x_tx_dma_memory_alloc
- * @adapter: pointer to our private adapter structure
- *
- * Returns 0 on success and errno on failure (as defined in errno.h).
- *
- * Allocates memory that will be visible both to the device and to the CPU.
+/* Allocates memory that will be visible both to the device and to the CPU.
  * The OS will pass us packets, pointers to which we will insert in the Tx
  * Descriptor queue. The device will read this queue to find the packets in
  * memory. The device will update the "status" in memory each time it xmits a
@@ -2721,75 +2716,48 @@ static void et131x_handle_recv_interrupt(struct et131x_adapter *adapter)
  */
 static int et131x_tx_dma_memory_alloc(struct et131x_adapter *adapter)
 {
-	int desc_size = 0;
+	int desc_size;
 	struct tx_ring *tx_ring = &adapter->tx_ring;
 
 	/* Allocate memory for the TCB's (Transmit Control Block) */
-	adapter->tx_ring.tcb_ring = kcalloc(NUM_TCB, sizeof(struct tcb),
-					    GFP_ATOMIC | GFP_DMA);
-	if (!adapter->tx_ring.tcb_ring)
+	tx_ring->tcb_ring = kcalloc(NUM_TCB, sizeof(struct tcb), GFP_KERNEL);
+	if (!tx_ring->tcb_ring)
 		return -ENOMEM;
 
 	desc_size = (sizeof(struct tx_desc) * NUM_DESC_PER_RING_TX);
-	tx_ring->tx_desc_ring =
-		(struct tx_desc *)dma_alloc_coherent(&adapter->pdev->dev,
-						     desc_size,
-						     &tx_ring->tx_desc_ring_pa,
-						     GFP_KERNEL);
-	if (!adapter->tx_ring.tx_desc_ring) {
+	/* Allocate dma memory for Tx descriptors and Tx status block */
+	tx_ring->tx_desc_ring = dma_alloc_coherent(&adapter->pdev->dev,
+						   desc_size + sizeof(u32),
+						   &tx_ring->tx_desc_ring_pa,
+						   GFP_KERNEL);
+
+	if (!tx_ring->tx_desc_ring) {
+		kfree(tx_ring->tcb_ring);
 		dev_err(&adapter->pdev->dev,
 			"Cannot alloc memory for Tx Ring\n");
-		return -ENOMEM;
-	}
 
-	/* Save physical address
-	 *
-	 * NOTE: dma_alloc_coherent(), used above to alloc DMA regions,
-	 * ALWAYS returns SAC (32-bit) addresses. If DAC (64-bit) addresses
-	 * are ever returned, make sure the high part is retrieved here before
-	 * storing the adjusted address.
-	 */
-	/* Allocate memory for the Tx status block */
-	tx_ring->tx_status = dma_alloc_coherent(&adapter->pdev->dev,
-						sizeof(u32),
-						&tx_ring->tx_status_pa,
-						GFP_KERNEL);
-	if (!adapter->tx_ring.tx_status_pa) {
-		dev_err(&adapter->pdev->dev,
-				  "Cannot alloc memory for Tx status block\n");
 		return -ENOMEM;
 	}
+
+	tx_ring->tx_status = (void *)tx_ring->tx_desc_ring + desc_size;
+	tx_ring->tx_status_pa = tx_ring->tx_desc_ring_pa + desc_size;
+
 	return 0;
 }
 
-/* et131x_tx_dma_memory_free - Free all memory allocated within this module
- * @adapter: pointer to our private adapter structure
- *
- * Returns 0 on success and errno on failure (as defined in errno.h).
- */
 static void et131x_tx_dma_memory_free(struct et131x_adapter *adapter)
 {
-	int desc_size = 0;
+	int desc_size;
 
 	if (adapter->tx_ring.tx_desc_ring) {
-		/* Free memory relating to Tx rings here */
+		/* Free Tx descriptors and Tx status block  memory */
 		desc_size = (sizeof(struct tx_desc) * NUM_DESC_PER_RING_TX);
 		dma_free_coherent(&adapter->pdev->dev,
-				  desc_size,
+				  desc_size + sizeof(u32),
 				  adapter->tx_ring.tx_desc_ring,
 				  adapter->tx_ring.tx_desc_ring_pa);
-		adapter->tx_ring.tx_desc_ring = NULL;
 	}
 
-	/* Free memory for the Tx status block */
-	if (adapter->tx_ring.tx_status) {
-		dma_free_coherent(&adapter->pdev->dev,
-				  sizeof(u32),
-				  adapter->tx_ring.tx_status,
-				  adapter->tx_ring.tx_status_pa);
-
-		adapter->tx_ring.tx_status = NULL;
-	}
 	/* Free the memory for the tcb structures */
 	kfree(adapter->tx_ring.tcb_ring);
 }
-- 
1.8.3.1

_______________________________________________
devel mailing list
devel@xxxxxxxxxxxxxxxxxxxxxx
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel




[Index of Archives]     [Linux Driver Backports]     [DMA Engine]     [Linux GPIO]     [Linux SPI]     [Video for Linux]     [Linux USB Devel]     [Linux Coverity]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]
  Powered by Linux