Skip to contentSkip to conversation

byteorder: add get/put endian helpers for the aligned case

PATCH Superseded Harvey Harrison 2 participants 3 messages May 29, 2008 – May 30, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v4 3/6 byteorder: add get/put endian helpers for the aligned case

Add a set of endian helpers for the pattern:
*(__le16 *)ptr = cpu_to_le16(val);

put_le16(val, (__le16 *)ptr);

The argument order follows that of the get/put_unaligned_{endian} helpers.

The get_ helpers are exactly equivalent to the existing {endian}_to_cpup functions, but have been added anyway for symmetry in the apis.

Myrinet had a put_be32 helper that has been changed to myri_put_be32 to avoid namespace collisions.

Private helpers in the usb-gadget drivers have been removed and switched over to the common ones. Note that the put helpers here had the args in the opposite order from the common version.

---
 drivers/net/myri10ge/myri10ge.c   |   16 +++---
 drivers/usb/gadget/file_storage.c |   78 +++++++++---------------------
 include/linux/byteorder.h         |   96 ++++++++++++++++++++++++++++++++++--
 include/linux/byteorder/generic.h |   72 +++++++++++++++++++++++++--
 4 files changed, 188 insertions(+), 74 deletions(-)
drivers/net/myri10ge/myri10ge.c +8 -8
index 36be6ef..5d035d0 100644
@@ -331,7 +331,7 @@ MODULE_PARM_DESC(myri10ge_wcfifo, "Enable WC Fifo when WC is enabled");
 static void myri10ge_set_multicast_list(struct net_device *dev);
 static int myri10ge_sw_tso(struct sk_buff *skb, struct net_device *dev);
 
-static inline void put_be32(__be32 val, __be32 __iomem * p)
+static inline void myri_put_be32(__be32 val, __be32 __iomem * p)
 {
 	__raw_writel((__force __u32) val, (__force void __iomem *)p);
 }
@@ -897,7 +897,7 @@ static int myri10ge_reset(struct myri10ge_priv *mgp)
 		dev_err(&mgp->pdev->dev, "failed set interrupt parameters\n");
 		return status;
 	}
-	put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
+	myri_put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
 
 	memset(mgp->ss.rx_done.entry, 0, bytes);
 
@@ -930,7 +930,7 @@ myri10ge_submit_8rx(struct mcp_kreq_ether_recv __iomem * dst,
 	myri10ge_pio_copy(dst + 4, src + 4, 4 * sizeof(*src));
 	mb();
 	src->addr_low = low;
-	put_be32(low, &dst->addr_low);
+	myri_put_be32(low, &dst->addr_low);
 	mb();
 }
 
@@ -1288,7 +1288,7 @@ static int myri10ge_poll(struct napi_struct *napi, int budget)
 
 	if (work_done < budget) {
 		netif_rx_complete(netdev, napi);
-		put_be32(htonl(3), ss->irq_claim);
+		myri_put_be32(htonl(3), ss->irq_claim);
 	}
 	return work_done;
 }
@@ -1312,7 +1312,7 @@ static irqreturn_t myri10ge_intr(int irq, void *arg)
 		netif_rx_schedule(ss->dev, &ss->napi);
 
 	if (!mgp->msi_enabled) {
-		put_be32(0, mgp->irq_deassert);
+		myri_put_be32(0, mgp->irq_deassert);
 		if (!myri10ge_deassert_wait)
 			stats->valid = 0;
 		mb();
@@ -1341,7 +1341,7 @@ static irqreturn_t myri10ge_intr(int irq, void *arg)
 
 	myri10ge_check_statblock(mgp);
 
-	put_be32(htonl(3), ss->irq_claim + 1);
+	myri_put_be32(htonl(3), ss->irq_claim + 1);
 	return (IRQ_HANDLED);
 }
 
@@ -1410,7 +1410,7 @@ myri10ge_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
 	struct myri10ge_priv *mgp = netdev_priv(netdev);
 
 	mgp->intr_coal_delay = coal->rx_coalesce_usecs;
-	put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
+	myri_put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
 	return 0;
 }
 
@@ -2201,7 +2201,7 @@ myri10ge_submit_req(struct myri10ge_tx_buf *tx, struct mcp_kreq_ether_send *src,
 
 	/* re-write the last 32-bits with the valid flags */
 	src->flags = last_flags;
-	put_be32(*((__be32 *) src + 3), (__be32 __iomem *) dst + 3);
+	myri_put_be32(*((__be32 *) src + 3), (__be32 __iomem *) dst + 3);
 	tx->req += cnt;
 	mb();
 }
drivers/usb/gadget/file_storage.c +24 -54

195 lines not on this page · raw message

include/linux/byteorder.h +90 -6

124 lines not on this page · raw message

include/linux/byteorder/generic.h +66 -6

100 lines not on this page · raw message

Signature · 1 line
1.5.6.rc0.277.g804cf
#1 lore raw
Quoted 17 lines · Harvey Harrison

Add a set of endian helpers for the pattern:
*(__le16 *)ptr = cpu_to_le16(val);

put_le16(val, (__le16 *)ptr);

The argument order follows that of the get/put_unaligned_{endian} helpers.

The get_ helpers are exactly equivalent to the existing {endian}_to_cpup functions, but have been added anyway for symmetry in the apis.

Myrinet had a put_be32 helper that has been changed to myri_put_be32 to avoid namespace collisions.

Private helpers in the usb-gadget drivers have been removed and switched over to the common ones. Note that the put helpers here had the args in the opposite order from the common version.

...

drivers/usb/gadget/file_storage.c static int do_read(struct fsg_dev *fsg) · line 1526

Quoted 11 lines
drivers/usb/gadget/file_storage.c +2 -2
@@ -1551,9 +1521,9 @@ static int do_read(struct fsg_dev *fsg)
 	/* Get the starting Logical Block Address and check that it's
 	 * not too big */
 	if (fsg->cmnd[0] == SC_READ_6)
-		lba = (fsg->cmnd[1] << 16) | get_be16(&fsg->cmnd[2]);
+		lba = (fsg->cmnd[1] << 16) | get_be16((__be16 *)&fsg->cmnd[2]);
 	else {
-		lba = get_be32(&fsg->cmnd[2]);
+		lba = get_be32((__be32 *)&fsg->cmnd[2]);

You need to be more careful here. Since fsg->cmnd is aligned on a 32-bit boundary, fsg->cmnd[2] is not so aligned. So this needs to be an unaligned transfer, although the first one above is correct.

drivers/usb/gadget/file_storage.c static int do_write(struct fsg_dev *fsg) · line 1659

Quoted 9 lines
drivers/usb/gadget/file_storage.c +2 -2
@@ -1684,9 +1654,9 @@ static int do_write(struct fsg_dev *fsg)
 	/* Get the starting Logical Block Address and check that it's
 	 * not too big */
 	if (fsg->cmnd[0] == SC_WRITE_6)
-		lba = (fsg->cmnd[1] << 16) | get_be16(&fsg->cmnd[2]);
+		lba = (fsg->cmnd[1] << 16) | get_be16((__be16 *)&fsg->cmnd[2]);
 	else {
-		lba = get_be32(&fsg->cmnd[2]);
+		lba = get_be32((__be32 *)&fsg->cmnd[2]);

Same here.

drivers/usb/gadget/file_storage.c static int do_verify(struct fsg_dev *fsg) · line 1893

Quoted 6 lines
drivers/usb/gadget/file_storage.c +1 -1
@@ -1920,7 +1890,7 @@ static int do_verify(struct fsg_dev *fsg)
 
 	/* Get the starting Logical Block Address and check that it's
 	 * not too big */
-	lba = get_be32(&fsg->cmnd[2]);
+	lba = get_be32((__be32 *)&fsg->cmnd[2]);

And here.

drivers/usb/gadget/file_storage.c static int do_verify(struct fsg_dev *fsg) · line 1906

Quoted 6 lines
drivers/usb/gadget/file_storage.c +1 -1
@@ -1933,7 +1903,7 @@ static int do_verify(struct fsg_dev *fsg)
 		return -EINVAL;
 	}
 
-	verification_length = get_be16(&fsg->cmnd[7]);
+	verification_length = get_be16((__be16 *)&fsg->cmnd[7]);

Likewise, fsg->cmnd[7] is at an odd address, so it isn't properly aligned for a 16-bit access.

drivers/usb/gadget/file_storage.c static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) · line 2051

Quoted 6 lines
drivers/usb/gadget/file_storage.c +1 -1
@@ -2078,7 +2048,7 @@ static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
 	memset(buf, 0, 18);
 	buf[0] = valid | 0x70;			// Valid, current error
 	buf[2] = SK(sd);
-	put_be32(&buf[3], sdinfo);		// Sense information
+	put_be32(sdinfo, (__be32 *)&buf[3]);	// Sense information

Wrong again.

drivers/usb/gadget/file_storage.c static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) · line 2062

Quoted 6 lines
drivers/usb/gadget/file_storage.c +1 -1
@@ -2089,7 +2059,7 @@ static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
 static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
 {
 	struct lun	*curlun = fsg->curlun;
-	u32		lba = get_be32(&fsg->cmnd[2]);
+	u32		lba = get_be32((__be32 *)&fsg->cmnd[2]);

And again.

drivers/usb/gadget/file_storage.c static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh) · line 2073

Quoted 8 lines
drivers/usb/gadget/file_storage.c +2 -2
@@ -2099,8 +2069,8 @@ static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
 		return -EINVAL;
 	}
 
-	put_be32(&buf[0], curlun->num_sectors - 1);	// Max logical block
-	put_be32(&buf[4], 512);				// Block length
+	put_be32(curlun->num_sectors - 1, (__be32 *)&buf[0]);	// Max logical block
+	put_be32(512, (__be32 *)&buf[4]);			// Block length

These are correct, since buf[0] and buf[4] are properly aligned for 32-bit accesses.

drivers/usb/gadget/file_storage.c static int do_scsi_command(struct fsg_dev *fsg) · line 2748

Quoted 6 lines
drivers/usb/gadget/file_storage.c +1 -1
@@ -2775,7 +2745,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
 		break;
 
 	case SC_MODE_SELECT_10:
-		fsg->data_size_from_cmnd = get_be16(&fsg->cmnd[7]);
+		fsg->data_size_from_cmnd = get_be16((__be16 *)&fsg->cmnd[7]);

This is wrong.

drivers/usb/gadget/file_storage.c static int do_scsi_command(struct fsg_dev *fsg) · line 2764

Quoted 6 lines
drivers/usb/gadget/file_storage.c +1 -1
@@ -2791,7 +2761,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
 		break;
 
 	case SC_MODE_SENSE_10:
-		fsg->data_size_from_cmnd = get_be16(&fsg->cmnd[7]);
+		fsg->data_size_from_cmnd = get_be16((__be16 *)&fsg->cmnd[7]);

So is this.

drivers/usb/gadget/file_storage.c static int do_scsi_command(struct fsg_dev *fsg) · line 2789

Quoted 6 lines
drivers/usb/gadget/file_storage.c +1 -1
@@ -2816,7 +2786,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
 		break;
 
 	case SC_READ_10:
-		fsg->data_size_from_cmnd = get_be16(&fsg->cmnd[7]) << 9;
+		fsg->data_size_from_cmnd = get_be16((__be16 *)&fsg->cmnd[7]) << 9;

And this.

drivers/usb/gadget/file_storage.c static int do_scsi_command(struct fsg_dev *fsg) · line 2797

Quoted 6 lines
drivers/usb/gadget/file_storage.c +1 -1
@@ -2824,7 +2794,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
 		break;
 
 	case SC_READ_12:
-		fsg->data_size_from_cmnd = get_be32(&fsg->cmnd[6]) << 9;
+		fsg->data_size_from_cmnd = get_be32((__be32 *)&fsg->cmnd[6]) << 9;

And this.

drivers/usb/gadget/file_storage.c static int do_scsi_command(struct fsg_dev *fsg) · line 2813

Quoted 6 lines
drivers/usb/gadget/file_storage.c +1 -1
@@ -2840,7 +2810,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
 		break;
 
 	case SC_READ_FORMAT_CAPACITIES:
-		fsg->data_size_from_cmnd = get_be16(&fsg->cmnd[7]);
+		fsg->data_size_from_cmnd = get_be16((__be16 *)&fsg->cmnd[7]);

And this.

drivers/usb/gadget/file_storage.c static int do_scsi_command(struct fsg_dev *fsg) · line 2871

Quoted 6 lines
drivers/usb/gadget/file_storage.c +1 -1
@@ -2898,7 +2868,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
 		break;
 
 	case SC_WRITE_10:
-		fsg->data_size_from_cmnd = get_be16(&fsg->cmnd[7]) << 9;
+		fsg->data_size_from_cmnd = get_be16((__be16 *)&fsg->cmnd[7]) << 9;

And this.

drivers/usb/gadget/file_storage.c static int do_scsi_command(struct fsg_dev *fsg) · line 2879

Quoted 6 lines
drivers/usb/gadget/file_storage.c +1 -1
@@ -2906,7 +2876,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
 		break;
 
 	case SC_WRITE_12:
-		fsg->data_size_from_cmnd = get_be32(&fsg->cmnd[6]) << 9;
+		fsg->data_size_from_cmnd = get_be32((__be32 *)&fsg->cmnd[6]) << 9;

And this.

It does take a certain amount of concentration to check which sort of helper is needed for each access.

Alan Stern

#2 lore raw
Quoted 13 lines · Alan Stern
Quoted 11 lines · Harvey Harrison

Add a set of endian helpers for the pattern:
*(__le16 *)ptr = cpu_to_le16(val);

put_le16(val, (__le16 *)ptr);

The argument order follows that of the get/put_unaligned_{endian} helpers.

The get_ helpers are exactly equivalent to the existing {endian}_to_cpup functions, but have been added anyway for symmetry in the apis.

Quoted 7 lines
Quoted 6 lines

Myrinet had a put_be32 helper that has been changed to myri_put_be32 to avoid namespace collisions.

Private helpers in the usb-gadget drivers have been removed and switched over to the common ones. Note that the put helpers here had the args in the opposite order from the common version.

Sorry about that, it completely went out of my head that the usb-helper was ok for unaligned access...I do know better.

I'll send a patch that move the unaligned ones over to the unaligned access api and the aligned cases to be32_to_cpup and resend shortly.

Harvey