Linux-Watchdog Archive on lore.kernel.org
 help / color / Atom feed
From: Andrey Smirnov <andrew.smirnov@gmail.com>
To: linux-watchdog@vger.kernel.org
Cc: Andrey Smirnov <andrew.smirnov@gmail.com>,
	Chris Healy <cphealy@gmail.com>,
	Guenter Roeck <linux@roeck-us.net>,
	Rick Ramstetter <rick@anteaterllc.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 06/22] watchdog: ziirave_wdt: Simplify ziirave_firm_write_pkt()
Date: Mon, 12 Aug 2019 13:08:50 -0700
Message-ID: <20190812200906.31344-7-andrew.smirnov@gmail.com> (raw)
In-Reply-To: <20190812200906.31344-1-andrew.smirnov@gmail.com>

There no reason why ziirave_firm_write_pkt() has to take firmware
data via 'struct ihex_binrec' and it can just take address, data pointer
and data length as individual arguments. Make this change to allow us
to drastically simplify handling page crossing case by removing all of
the extra code required to split 'struct ihex_binrec' into two.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
Cc: Chris Healy <cphealy@gmail.com>
Cc: Guenter Roeck <linux@roeck-us.net>
Cc: Rick Ramstetter <rick@anteaterllc.com>
Cc: linux-watchdog@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
---
 drivers/watchdog/ziirave_wdt.c | 116 +++++++++++++--------------------
 1 file changed, 44 insertions(+), 72 deletions(-)

diff --git a/drivers/watchdog/ziirave_wdt.c b/drivers/watchdog/ziirave_wdt.c
index 75c066602c00..b2d5ff0c22fe 100644
--- a/drivers/watchdog/ziirave_wdt.c
+++ b/drivers/watchdog/ziirave_wdt.c
@@ -51,6 +51,7 @@ static char *ziirave_reasons[] = {"power cycle", "hw watchdog", NULL, NULL,
 #define ZIIRAVE_FIRM_PKT_DATA_SIZE	16
 #define ZIIRAVE_FIRM_FLASH_MEMORY_START	0x1600
 #define ZIIRAVE_FIRM_FLASH_MEMORY_END	0x2bbf
+#define ZIIRAVE_FIRM_PAGE_SIZE		128
 
 /* Received and ready for next Download packet. */
 #define ZIIRAVE_FIRM_DOWNLOAD_ACK	1
@@ -244,27 +245,26 @@ static int ziirave_firm_write_byte(struct watchdog_device *wdd, u8 command,
  *     Data0 .. Data15: Array of 16 bytes of data.
  *     Checksum: Checksum byte to verify data integrity.
  */
-static int ziirave_firm_write_pkt(struct watchdog_device *wdd,
-				  const struct ihex_binrec *rec)
+static int __ziirave_firm_write_pkt(struct watchdog_device *wdd,
+				    u32 addr, const u8 *data, u8 len)
 {
+	const u16 addr16 = (u16)addr / 2;
 	struct i2c_client *client = to_i2c_client(wdd->parent);
 	u8 i, checksum = 0, packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE];
 	int ret;
-	u16 addr;
 
 	memset(packet, 0, ARRAY_SIZE(packet));
 
 	/* Packet length */
-	packet[0] = (u8)be16_to_cpu(rec->len);
+	packet[0] = len;
 	/* Packet address */
-	addr = (be32_to_cpu(rec->addr) & 0xffff) >> 1;
-	packet[1] = addr & 0xff;
-	packet[2] = (addr & 0xff00) >> 8;
+	packet[1] = addr16 & 0xff;
+	packet[2] = (addr16 & 0xff00) >> 8;
 
 	/* Packet data */
-	if (be16_to_cpu(rec->len) > ZIIRAVE_FIRM_PKT_DATA_SIZE)
+	if (len > ZIIRAVE_FIRM_PKT_DATA_SIZE)
 		return -EMSGSIZE;
-	memcpy(packet + 3, rec->data, be16_to_cpu(rec->len));
+	memcpy(packet + 3, data, len);
 
 	/* Packet checksum */
 	for (i = 0; i < ZIIRAVE_FIRM_PKT_TOTAL_SIZE - 1; i++)
@@ -276,11 +276,35 @@ static int ziirave_firm_write_pkt(struct watchdog_device *wdd,
 	if (ret)
 		dev_err(&client->dev,
 		      "Failed to write firmware packet at address 0x%04x: %d\n",
-		      addr, ret);
+		      addr16, ret);
 
 	return ret;
 }
 
+static int ziirave_firm_write_pkt(struct watchdog_device *wdd,
+				  u32 addr, const u8 *data, u8 len)
+{
+	const u8 max_write_len = ZIIRAVE_FIRM_PAGE_SIZE -
+		(addr - ALIGN_DOWN(addr, ZIIRAVE_FIRM_PAGE_SIZE));
+	int ret;
+
+	if (len > max_write_len) {
+		/*
+		 * If data crossed page boundary we need to split this
+		 * write in two
+		 */
+		ret = __ziirave_firm_write_pkt(wdd, addr, data, max_write_len);
+		if (ret)
+			return ret;
+
+		addr += max_write_len;
+		data += max_write_len;
+		len  -= max_write_len;
+	}
+
+	return __ziirave_firm_write_pkt(wdd, addr, data, len);
+}
+
 static int ziirave_firm_verify(struct watchdog_device *wdd,
 			       const struct firmware *fw)
 {
@@ -333,9 +357,8 @@ static int ziirave_firm_upload(struct watchdog_device *wdd,
 			       const struct firmware *fw)
 {
 	struct i2c_client *client = to_i2c_client(wdd->parent);
-	int ret, words_till_page_break;
 	const struct ihex_binrec *rec;
-	struct ihex_binrec *rec_new;
+	int ret;
 
 	ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_JUMP_TO_BOOTLOADER, 1,
 				      false);
@@ -366,68 +389,17 @@ static int ziirave_firm_upload(struct watchdog_device *wdd,
 			return -EMSGSIZE;
 		}
 
-		/* Calculate words till page break */
-		words_till_page_break = (64 - ((be32_to_cpu(rec->addr) >> 1) &
-					 0x3f));
-		if ((be16_to_cpu(rec->len) >> 1) > words_till_page_break) {
-			/*
-			 * Data in passes page boundary, so we need to split in
-			 * two blocks of data. Create a packet with the first
-			 * block of data.
-			 */
-			rec_new = kzalloc(sizeof(struct ihex_binrec) +
-					  (words_till_page_break << 1),
-					  GFP_KERNEL);
-			if (!rec_new)
-				return -ENOMEM;
-
-			rec_new->len = cpu_to_be16(words_till_page_break << 1);
-			rec_new->addr = rec->addr;
-			memcpy(rec_new->data, rec->data,
-			       be16_to_cpu(rec_new->len));
-
-			ret = ziirave_firm_write_pkt(wdd, rec_new);
-			kfree(rec_new);
-			if (ret)
-				return ret;
-
-			/* Create a packet with the second block of data */
-			rec_new = kzalloc(sizeof(struct ihex_binrec) +
-					  be16_to_cpu(rec->len) -
-					  (words_till_page_break << 1),
-					  GFP_KERNEL);
-			if (!rec_new)
-				return -ENOMEM;
-
-			/* Remaining bytes */
-			rec_new->len = rec->len -
-				       cpu_to_be16(words_till_page_break << 1);
-
-			rec_new->addr = cpu_to_be32(be32_to_cpu(rec->addr) +
-					(words_till_page_break << 1));
-
-			memcpy(rec_new->data,
-			       rec->data + (words_till_page_break << 1),
-			       be16_to_cpu(rec_new->len));
-
-			ret = ziirave_firm_write_pkt(wdd, rec_new);
-			kfree(rec_new);
-			if (ret)
-				return ret;
-		} else {
-			ret = ziirave_firm_write_pkt(wdd, rec);
-			if (ret)
-				return ret;
-		}
+		ret = ziirave_firm_write_pkt(wdd, be32_to_cpu(rec->addr),
+					     rec->data, be16_to_cpu(rec->len));
+		if (ret)
+			return ret;
 	}
 
-	/* For end of download, the length field will be set to 0 */
-	rec_new = kzalloc(sizeof(struct ihex_binrec) + 1, GFP_KERNEL);
-	if (!rec_new)
-		return -ENOMEM;
-
-	ret = ziirave_firm_write_pkt(wdd, rec_new);
-	kfree(rec_new);
+	/*
+	 * Finish firmware download process by sending a zero length
+	 * payload
+	 */
+	ret = ziirave_firm_write_pkt(wdd, 0, NULL, 0);
 	if (ret) {
 		dev_err(&client->dev, "Failed to send EMPTY packet: %d\n", ret);
 		return ret;
-- 
2.21.0


  parent reply index

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-12 20:08 [PATCH v2 00/22] Ziirave_wdt driver fixes Andrey Smirnov
2019-08-12 20:08 ` [PATCH v2 01/22] watchdog: ziirave_wdt: Add missing newline Andrey Smirnov
2019-08-12 20:08 ` [PATCH v2 02/22] watchdog: ziirave_wdt: Be verbose about errors in probe() Andrey Smirnov
2019-08-12 20:08 ` [PATCH v2 03/22] watchdog: ziirave_wdt: Be more verbose during firmware update Andrey Smirnov
2019-08-12 20:08 ` [PATCH v2 04/22] watchdog: ziirave_wdt: Don't bail out on unexpected timeout value Andrey Smirnov
2019-08-15 18:12   ` Guenter Roeck
2019-08-12 20:08 ` [PATCH v2 05/22] watchdog: ziirave_wdt: Log bootloader/firmware info during probe Andrey Smirnov
2019-08-12 20:08 ` Andrey Smirnov [this message]
2019-08-15 18:14   ` [PATCH v2 06/22] watchdog: ziirave_wdt: Simplify ziirave_firm_write_pkt() Guenter Roeck
2019-08-12 20:08 ` [PATCH v2 07/22] watchdog: ziirave_wdt: Check packet length only once Andrey Smirnov
2019-08-15 18:14   ` Guenter Roeck
2019-08-12 20:08 ` [PATCH v2 08/22] watchdog: ziirave_wdt: Skip zeros when calculating checksum Andrey Smirnov
2019-08-15 18:16   ` Guenter Roeck
2019-08-12 20:08 ` [PATCH v2 09/22] watchdog: ziirave_wdt: Fix incorrect use of ARRAY_SIZE Andrey Smirnov
2019-08-15 18:17   ` Guenter Roeck
2019-08-12 20:08 ` [PATCH v2 10/22] watchdog: ziirave_wdt: Zero out only what's necessary Andrey Smirnov
2019-08-15 18:17   ` Guenter Roeck
2019-08-12 20:08 ` [PATCH v2 11/22] watchdog: ziirave_wdt: Make use of put_unaligned_le16 Andrey Smirnov
2019-08-15 18:18   ` Guenter Roeck
2019-08-12 20:08 ` [PATCH v2 12/22] watchdog: ziirave_wdt: Don't check if ihex record length is zero Andrey Smirnov
2019-08-15 18:19   ` Guenter Roeck
2019-08-12 20:08 ` [PATCH v2 13/22] watchdog: ziirave_wdt: Don't read out more than 'len' firmware bytes Andrey Smirnov
2019-08-15 18:19   ` Guenter Roeck
2019-08-12 20:08 ` [PATCH v2 14/22] watchdog: ziirave_wdt: Don't try to program readonly flash Andrey Smirnov
2019-08-15 18:21   ` Guenter Roeck
2019-08-12 20:08 ` [PATCH v2 15/22] watchdog: ziirave_wdt: Fix misleading error message Andrey Smirnov
2019-08-15 18:22   ` Guenter Roeck
2019-08-12 20:09 ` [PATCH v2 16/22] watchdog: ziirave_wdt: Fix JUMP_TO_BOOTLOADER payload Andrey Smirnov
2019-08-15 18:22   ` Guenter Roeck
2019-08-12 20:09 ` [PATCH v2 17/22] watchdog: ziirave_wdt: Fix DOWNLOAD_END payload Andrey Smirnov
2019-08-15 18:23   ` Guenter Roeck
2019-08-12 20:09 ` [PATCH v2 18/22] watchdog: ziirave_wdt: Fix RESET_PROCESSOR payload Andrey Smirnov
2019-08-15 18:23   ` Guenter Roeck
2019-08-12 20:09 ` [PATCH v2 19/22] watchdog: ziirave_wdt: Drop status polling code Andrey Smirnov
2019-08-15 18:24   ` Guenter Roeck
2019-08-12 20:09 ` [PATCH v2 20/22] watchdog: ziirave_wdt: Fix DOWNLOAD_START payload Andrey Smirnov
2019-08-15 18:24   ` Guenter Roeck
2019-08-12 20:09 ` [PATCH v2 21/22] watchdog: ziirave_wdt: Drop ziirave_firm_write_block_data() Andrey Smirnov
2019-08-15 18:25   ` Guenter Roeck
2019-08-12 20:09 ` [PATCH v2 22/22] watchdog: ziirave_wdt: Update checked I2C functionality mask Andrey Smirnov
2019-08-15 18:25   ` Guenter Roeck

Reply instructions:

You may reply publically to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190812200906.31344-7-andrew.smirnov@gmail.com \
    --to=andrew.smirnov@gmail.com \
    --cc=cphealy@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-watchdog@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=rick@anteaterllc.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux-Watchdog Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-watchdog/0 linux-watchdog/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-watchdog linux-watchdog/ https://lore.kernel.org/linux-watchdog \
		linux-watchdog@vger.kernel.org linux-watchdog@archiver.kernel.org
	public-inbox-index linux-watchdog


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-watchdog


AGPL code for this site: git clone https://public-inbox.org/ public-inbox