Skip to contentSkip to conversation

arcmsr: simplify ioctl data read/write

PATCH Applied Ching Huang 3 participants 5 messages Sep 24, 2014 – Sep 26, 2014 SCSI

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/2 arcmsr: simplify ioctl data read/write

From: Ching Huang <[email protected]>

This patch is relative to:
http://git.infradead.org/users/hch/scsi-queue.git/tree/drivers-for-3.18:/drivers/scsi/arcmsr

change in v5:
1. rename firstindex to getIndex, lastindex to putIndex for readability
2. define ARCMSR_API_DATA_BUFLEN as 1032
3. simplify ioctl data read by marcro CIRC_CNT_TO_END and CIRC_CNT

Signed-off-by Ching Huang <ching [email protected]>

---

drivers/scsi/arcmsr/arcmsr_attr.c +44 -83
diff -uprN a/drivers/scsi/arcmsr/arcmsr_attr.c b/drivers/scsi/arcmsr/arcmsr_attr.c
@@ -50,6 +50,7 @@
 #include <linux/errno.h>
 #include <linux/delay.h>
 #include <linux/pci.h>
+#include <linux/circ_buf.h>
 
 #include <scsi/scsi_cmnd.h>
 #include <scsi/scsi_device.h>
@@ -68,7 +69,7 @@ static ssize_t arcmsr_sysfs_iop_message_
 	struct device *dev = container_of(kobj,struct device,kobj);
 	struct Scsi_Host *host = class_to_shost(dev);
 	struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata;
-	uint8_t *pQbuffer,*ptmpQbuffer;
+	uint8_t *ptmpQbuffer;
 	int32_t allxfer_len = 0;
 	unsigned long flags;
 
@@ -78,57 +79,22 @@ static ssize_t arcmsr_sysfs_iop_message_
 	/* do message unit read. */
 	ptmpQbuffer = (uint8_t *)buf;
 	spin_lock_irqsave(&acb->rqbuffer_lock, flags);
-	if (acb->rqbuf_firstindex != acb->rqbuf_lastindex) {
-		pQbuffer = &acb->rqbuffer[acb->rqbuf_firstindex];
-		if (acb->rqbuf_firstindex > acb->rqbuf_lastindex) {
-			if ((ARCMSR_MAX_QBUFFER - acb->rqbuf_firstindex) >= 1032) {
-				memcpy(ptmpQbuffer, pQbuffer, 1032);
-				acb->rqbuf_firstindex += 1032;
-				acb->rqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
-				allxfer_len = 1032;
-			} else {
-				if (((ARCMSR_MAX_QBUFFER - acb->rqbuf_firstindex)
-					+ acb->rqbuf_lastindex) > 1032) {
-					memcpy(ptmpQbuffer, pQbuffer,
-						ARCMSR_MAX_QBUFFER
-						- acb->rqbuf_firstindex);
-					ptmpQbuffer += ARCMSR_MAX_QBUFFER
-						- acb->rqbuf_firstindex;
-					memcpy(ptmpQbuffer, acb->rqbuffer, 1032
-						- (ARCMSR_MAX_QBUFFER -
-						acb->rqbuf_firstindex));
-					acb->rqbuf_firstindex = 1032 -
-						(ARCMSR_MAX_QBUFFER -
-						acb->rqbuf_firstindex);
-					allxfer_len = 1032;
-				} else {
-					memcpy(ptmpQbuffer, pQbuffer,
-						ARCMSR_MAX_QBUFFER -
-						acb->rqbuf_firstindex);
-					ptmpQbuffer += ARCMSR_MAX_QBUFFER -
-						acb->rqbuf_firstindex;
-					memcpy(ptmpQbuffer, acb->rqbuffer,
-						acb->rqbuf_lastindex);
-					allxfer_len = ARCMSR_MAX_QBUFFER -
-						acb->rqbuf_firstindex +
-						acb->rqbuf_lastindex;
-					acb->rqbuf_firstindex =
-						acb->rqbuf_lastindex;
-				}
-			}
-		} else {
-			if ((acb->rqbuf_lastindex - acb->rqbuf_firstindex) > 1032) {
-				memcpy(ptmpQbuffer, pQbuffer, 1032);
-				acb->rqbuf_firstindex += 1032;
-				allxfer_len = 1032;
-			} else {
-				memcpy(ptmpQbuffer, pQbuffer, acb->rqbuf_lastindex
-					- acb->rqbuf_firstindex);
-				allxfer_len = acb->rqbuf_lastindex -
-					acb->rqbuf_firstindex;
-				acb->rqbuf_firstindex = acb->rqbuf_lastindex;
-			}
+	if (acb->rqbuf_getIndex != acb->rqbuf_putIndex) {
+		unsigned int tail = acb->rqbuf_getIndex;
+		unsigned int head = acb->rqbuf_putIndex;
+		unsigned int cnt_to_end = CIRC_CNT_TO_END(head, tail, ARCMSR_MAX_QBUFFER);
+
+		allxfer_len = CIRC_CNT(head, tail, ARCMSR_MAX_QBUFFER);
+		if (allxfer_len > ARCMSR_API_DATA_BUFLEN)
+			allxfer_len = ARCMSR_API_DATA_BUFLEN;
+
+		if (allxfer_len <= cnt_to_end)
+			memcpy(ptmpQbuffer, acb->rqbuffer + tail, allxfer_len);
+		else {
+			memcpy(ptmpQbuffer, acb->rqbuffer + tail, cnt_to_end);
+			memcpy(ptmpQbuffer + cnt_to_end, acb->rqbuffer, allxfer_len - cnt_to_end);
 		}
+		acb->rqbuf_getIndex = (acb->rqbuf_getIndex + allxfer_len) % ARCMSR_MAX_QBUFFER;
 	}
 	if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
 		struct QBUFFER __iomem *prbuffer;
@@ -150,47 +116,42 @@ static ssize_t arcmsr_sysfs_iop_message_
 	struct device *dev = container_of(kobj,struct device,kobj);
 	struct Scsi_Host *host = class_to_shost(dev);
 	struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata;
-	int32_t my_empty_len, user_len, wqbuf_firstindex, wqbuf_lastindex;
+	int32_t user_len, cnt2end;
 	uint8_t *pQbuffer, *ptmpuserbuffer;
 	unsigned long flags;
 
 	if (!capable(CAP_SYS_ADMIN))
 		return -EACCES;
-	if (count > 1032)
+	if (count > ARCMSR_API_DATA_BUFLEN)
 		return -EINVAL;
 	/* do message unit write. */
 	ptmpuserbuffer = (uint8_t *)buf;
 	user_len = (int32_t)count;
 	spin_lock_irqsave(&acb->wqbuffer_lock, flags);
-	wqbuf_lastindex = acb->wqbuf_lastindex;
-	wqbuf_firstindex = acb->wqbuf_firstindex;
-	if (wqbuf_lastindex != wqbuf_firstindex) {
+	if (acb->wqbuf_putIndex != acb->wqbuf_getIndex) {
 		arcmsr_write_ioctldata2iop(acb);
 		spin_unlock_irqrestore(&acb->wqbuffer_lock, flags);
 		return 0;	/*need retry*/
 	} else {
-		my_empty_len = (wqbuf_firstindex-wqbuf_lastindex - 1)
-			&(ARCMSR_MAX_QBUFFER - 1);
-		if (my_empty_len >= user_len) {
-			while (user_len > 0) {
-				pQbuffer = &acb->wqbuffer[acb->wqbuf_lastindex];
-				memcpy(pQbuffer, ptmpuserbuffer, 1);
-				acb->wqbuf_lastindex++;
-				acb->wqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
-				ptmpuserbuffer++;
-				user_len--;
-			}
-			if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_CLEARED) {
-				acb->acb_flags &=
-					~ACB_F_MESSAGE_WQBUFFER_CLEARED;
-				arcmsr_write_ioctldata2iop(acb);
-			}
-			spin_unlock_irqrestore(&acb->wqbuffer_lock, flags);
-			return count;
-		} else {
-			spin_unlock_irqrestore(&acb->wqbuffer_lock, flags);
-			return 0;	/*need retry*/
+		pQbuffer = &acb->wqbuffer[acb->wqbuf_putIndex];
+		cnt2end = ARCMSR_MAX_QBUFFER - acb->wqbuf_putIndex;
+		if (user_len > cnt2end) {
+			memcpy(pQbuffer, ptmpuserbuffer, cnt2end);
+			ptmpuserbuffer += cnt2end;
+			user_len -= cnt2end;
+			acb->wqbuf_putIndex = 0;
+			pQbuffer = acb->wqbuffer;
 		}
+		memcpy(pQbuffer, ptmpuserbuffer, user_len);
+		acb->wqbuf_putIndex += user_len;
+		acb->wqbuf_putIndex %= ARCMSR_MAX_QBUFFER;
+		if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_CLEARED) {
+			acb->acb_flags &=
+				~ACB_F_MESSAGE_WQBUFFER_CLEARED;
+			arcmsr_write_ioctldata2iop(acb);
+		}
+		spin_unlock_irqrestore(&acb->wqbuffer_lock, flags);
+		return count;
 	}
 }
 
@@ -215,12 +176,12 @@ static ssize_t arcmsr_sysfs_iop_message_
 		| ACB_F_MESSAGE_RQBUFFER_CLEARED
 		| ACB_F_MESSAGE_WQBUFFER_READED);
 	spin_lock_irqsave(&acb->rqbuffer_lock, flags);
-	acb->rqbuf_firstindex = 0;
-	acb->rqbuf_lastindex = 0;
+	acb->rqbuf_getIndex = 0;
+	acb->rqbuf_putIndex = 0;
 	spin_unlock_irqrestore(&acb->rqbuffer_lock, flags);
 	spin_lock_irqsave(&acb->wqbuffer_lock, flags);
-	acb->wqbuf_firstindex = 0;
-	acb->wqbuf_lastindex = 0;
+	acb->wqbuf_getIndex = 0;
+	acb->wqbuf_putIndex = 0;
 	spin_unlock_irqrestore(&acb->wqbuffer_lock, flags);
 	pQbuffer = acb->rqbuffer;
 	memset(pQbuffer, 0, sizeof (struct QBUFFER));
@@ -234,7 +195,7 @@ static struct bin_attribute arcmsr_sysfs
 		.name = "mu_read",
 		.mode = S_IRUSR ,
 	},
-	.size = 1032,
+	.size = ARCMSR_API_DATA_BUFLEN,
 	.read = arcmsr_sysfs_iop_message_read,
 };
 
@@ -243,7 +204,7 @@ static struct bin_attribute arcmsr_sysfs
 		.name = "mu_write",
 		.mode = S_IWUSR,
 	},
-	.size = 1032,
+	.size = ARCMSR_API_DATA_BUFLEN,
 	.write = arcmsr_sysfs_iop_message_write,
 };
 
drivers/scsi/arcmsr/arcmsr.h +7 -6
diff -uprN a/drivers/scsi/arcmsr/arcmsr.h b/drivers/scsi/arcmsr/arcmsr.h
@@ -52,7 +52,7 @@ struct device_attribute;
 	#define ARCMSR_MAX_FREECCB_NUM	320
 #define ARCMSR_MAX_OUTSTANDING_CMD	255
 #endif
-#define ARCMSR_DRIVER_VERSION		"v1.30.00.04-20140428"
+#define ARCMSR_DRIVER_VERSION		"v1.30.00.04-20140919"
 #define ARCMSR_SCSI_INITIATOR_ID						255
 #define ARCMSR_MAX_XFER_SECTORS							512
 #define ARCMSR_MAX_XFER_SECTORS_B						4096
@@ -107,10 +107,11 @@ struct CMD_MESSAGE
 **        IOP Message Transfer Data for user space
 *******************************************************************************
 */
+#define	ARCMSR_API_DATA_BUFLEN	1032
 struct CMD_MESSAGE_FIELD
 {
     struct CMD_MESSAGE			cmdmessage;
-    uint8_t				messagedatabuffer[1032];
+    uint8_t				messagedatabuffer[ARCMSR_API_DATA_BUFLEN];
 };
 /* IOP message transfer */
 #define ARCMSR_MESSAGE_FAIL			0x0001
@@ -678,15 +679,15 @@ struct AdapterControlBlock
 	unsigned int				uncache_size;
 	uint8_t				rqbuffer[ARCMSR_MAX_QBUFFER];
 	/* data collection buffer for read from 80331 */
-	int32_t				rqbuf_firstindex;
+	int32_t				rqbuf_getIndex;
 	/* first of read buffer  */
-	int32_t				rqbuf_lastindex;
+	int32_t				rqbuf_putIndex;
 	/* last of read buffer   */
 	uint8_t				wqbuffer[ARCMSR_MAX_QBUFFER];
 	/* data collection buffer for write to 80331  */
-	int32_t				wqbuf_firstindex;
+	int32_t				wqbuf_getIndex;
 	/* first of write buffer */
-	int32_t				wqbuf_lastindex;
+	int32_t				wqbuf_putIndex;
 	/* last of write buffer  */
 	uint8_t				devstate[ARCMSR_MAX_TARGETID][ARCMSR_MAX_TARGETLUN];
 	/* id0 ..... id15, lun0...lun7 */
drivers/scsi/arcmsr/arcmsr_hba.c +68 -140

327 lines not on this page · raw message

Reviewed-by on v5 1/2
#1 lore raw
Quoted 13 lines · Ching Huang

From: Ching Huang <[email protected]>

This patch is relative to:
http://git.infradead.org/users/hch/scsi-queue.git/tree/drivers-for-3.18:/drivers/scsi/arcmsr

change in v5:
1. rename firstindex to getIndex, lastindex to putIndex for readability
2. define ARCMSR_API_DATA_BUFLEN as 1032
3. simplify ioctl data read by marcro CIRC_CNT_TO_END and CIRC_CNT

Signed-off-by Ching Huang <ching [email protected]>

---

...

drivers/scsi/arcmsr/arcmsr_attr.c static ssize_t arcmsr_sysfs_iop_message_ · line 148

Quoted 13 lines
drivers/scsi/arcmsr/arcmsr_attr.c +12 -0
+		pQbuffer = &acb->wqbuffer[acb->wqbuf_putIndex];
+		cnt2end = ARCMSR_MAX_QBUFFER - acb->wqbuf_putIndex;
+		if (user_len > cnt2end) {
+			memcpy(pQbuffer, ptmpuserbuffer, cnt2end);
+			ptmpuserbuffer += cnt2end;
+			user_len -= cnt2end;
+			acb->wqbuf_putIndex = 0;
+			pQbuffer = acb->wqbuffer;
 		}
+		memcpy(pQbuffer, ptmpuserbuffer, user_len);
+		acb->wqbuf_putIndex += user_len;
+		acb->wqbuf_putIndex %= ARCMSR_MAX_QBUFFER;
+		if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_CLEARED) {

This test^ is most likely useless, it looks like you set the ACB_F_MESSAGE_WQBUFFER_CLEARED every time you have added some data to the buffer and clear it when the buffer gets empty. I think you could get rid of
the ACB_F_MESSAGE_WQBUFFER_CLEARED completely. Also the ACB_F_MESSAGE_RQBUFFER_CLEARED doesn't seems to be ever evaluated.
I'm not sure with the ACB_F_MESSAGE_WQBUFFER_READED, but that one probably is also a candidate for removal.
...

drivers/scsi/arcmsr/arcmsr.h struct AdapterControlBlock · line 682

Quoted 6 lines
drivers/scsi/arcmsr/arcmsr.h +1 -1
@@ -678,15 +679,15 @@ struct AdapterControlBlock
 	unsigned int				uncache_size;
 	uint8_t				rqbuffer[ARCMSR_MAX_QBUFFER];
 	/* data collection buffer for read from 80331 */
-	int32_t				rqbuf_firstindex;
+	int32_t				rqbuf_getIndex;

What is the reason for using an exact size int32 (instead of a plain int) here?

Quoted 4 lines
drivers/scsi/arcmsr/arcmsr.h +1 -1
 	/* first of read buffer  */
-	int32_t				rqbuf_lastindex;
+	int32_t				rqbuf_putIndex;
 	/* last of read buffer   */

drivers/scsi/arcmsr/arcmsr.h struct AdapterControlBlock · line 693

Quoted 10 lines
drivers/scsi/arcmsr/arcmsr.h +2 -2
 	uint8_t				wqbuffer[ARCMSR_MAX_QBUFFER];
 	/* data collection buffer for write to 80331  */
-	int32_t				wqbuf_firstindex;
+	int32_t				wqbuf_getIndex;
 	/* first of write buffer */
-	int32_t				wqbuf_lastindex;
+	int32_t				wqbuf_putIndex;
 	/* last of write buffer  */
 	uint8_t				devstate[ARCMSR_MAX_TARGETID][ARCMSR_MAX_TARGETLUN];
 	/* id0 ..... id15, lun0...lun7 */

The comments I've added are not directly related to this patch, but you may still address them in a new patch
so -

#2 lore raw
Quoted 27 lines · Tomas Henzl
Quoted 13 lines · Ching Huang

From: Ching Huang <[email protected]>

This patch is relative to:
http://git.infradead.org/users/hch/scsi-queue.git/tree/drivers-for-3.18:/drivers/scsi/arcmsr

change in v5:
1. rename firstindex to getIndex, lastindex to putIndex for readability
2. define ARCMSR_API_DATA_BUFLEN as 1032
3. simplify ioctl data read by marcro CIRC_CNT_TO_END and CIRC_CNT

Signed-off-by Ching Huang <ching [email protected]>

---

...

Quoted 12 lines
drivers/scsi/arcmsr/arcmsr_attr.c +11 -0
+		pQbuffer = &acb->wqbuffer[acb->wqbuf_putIndex];
+		cnt2end = ARCMSR_MAX_QBUFFER - acb->wqbuf_putIndex;
+		if (user_len > cnt2end) {
+			memcpy(pQbuffer, ptmpuserbuffer, cnt2end);
+			ptmpuserbuffer += cnt2end;
+			user_len -= cnt2end;
+			acb->wqbuf_putIndex = 0;
+			pQbuffer = acb->wqbuffer;
 		}
+		memcpy(pQbuffer, ptmpuserbuffer, user_len);
+		acb->wqbuf_putIndex += user_len;
+		acb->wqbuf_putIndex %= ARCMSR_MAX_QBUFFER;
Quoted 8 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+		if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_CLEARED) {

This test^ is most likely useless, it looks like you set the ACB_F_MESSAGE_WQBUFFER_CLEARED every time you have added some data to the buffer and clear it when the buffer gets empty. I think you could get rid of
the ACB_F_MESSAGE_WQBUFFER_CLEARED completely. Also the ACB_F_MESSAGE_RQBUFFER_CLEARED doesn't seems to be ever evaluated.
I'm not sure with the ACB_F_MESSAGE_WQBUFFER_READED, but that one probably is also a candidate for removal.

You are right. ACB_F_MESSAGE_WQBUFFER_CLEARED, ACB_F_MESSAGE_RQBUFFER_CLEARED, ACB_F_MESSAGE_WQBUFFER_READED are seem useless.

Quoted 8 lines

...

drivers/scsi/arcmsr/arcmsr.h struct AdapterControlBlock · line 682

Quoted 6 lines
drivers/scsi/arcmsr/arcmsr.h +1 -1
@@ -678,15 +679,15 @@ struct AdapterControlBlock
 	unsigned int				uncache_size;
 	uint8_t				rqbuffer[ARCMSR_MAX_QBUFFER];
 	/* data collection buffer for read from 80331 */
-	int32_t				rqbuf_firstindex;
+	int32_t				rqbuf_getIndex;

What is the reason for using an exact size int32 (instead of a plain int) here?

There is not special reason have to int32, int is OK.

Quoted 22 lines
Quoted 4 lines
drivers/scsi/arcmsr/arcmsr.h +1 -1
 	/* first of read buffer  */
-	int32_t				rqbuf_lastindex;
+	int32_t				rqbuf_putIndex;
 	/* last of read buffer   */

drivers/scsi/arcmsr/arcmsr.h struct AdapterControlBlock · line 693

Quoted 10 lines
drivers/scsi/arcmsr/arcmsr.h +2 -2
 	uint8_t				wqbuffer[ARCMSR_MAX_QBUFFER];
 	/* data collection buffer for write to 80331  */
-	int32_t				wqbuf_firstindex;
+	int32_t				wqbuf_getIndex;
 	/* first of write buffer */
-	int32_t				wqbuf_lastindex;
+	int32_t				wqbuf_putIndex;
 	/* last of write buffer  */
 	uint8_t				devstate[ARCMSR_MAX_TARGETID][ARCMSR_MAX_TARGETLUN];
 	/* id0 ..... id15, lun0...lun7 */

The comments I've added are not directly related to this patch, but you may still address them in a new patch
so -

#4 lore raw
Quoted 1 line · Christoph Hellwig

Thanks, applied to drivers-for-3.18.

Thanks for all of your review, especially Tomas.