mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrew Lunn <andrew@lunn.ch>
To: Parthiban Veerasooran <Parthiban.Veerasooran@microchip.com>
Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
	pabeni@redhat.com, robh+dt@kernel.org,
	krzysztof.kozlowski+dt@linaro.org, conor+dt@kernel.org,
	corbet@lwn.net, steen.hegelund@microchip.com,
	rdunlap@infradead.org, horms@kernel.org, casper.casan@gmail.com,
	netdev@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org,
	horatiu.vultur@microchip.com, Woojung.Huh@microchip.com,
	Nicolas.Ferre@microchip.com, UNGLinuxDriver@microchip.com,
	Thorsten.Kummermehr@microchip.com
Subject: Re: [PATCH net-next v2 7/9] net: ethernet: oa_tc6: implement data transaction interface
Date: Tue, 24 Oct 2023 04:07:02 +0200	[thread overview]
Message-ID: <9b7a5ed9-840f-4346-a168-e538a8477714@lunn.ch> (raw)
In-Reply-To: <20231023154649.45931-8-Parthiban.Veerasooran@microchip.com>

> +static u16 oa_tc6_prepare_empty_chunk(struct oa_tc6 *tc6, u8 *buf, u8 cp_count)
> +{
> +	u32 hdr;
> +
> +	/* Prepare empty chunks used for getting interrupt information or if
> +	 * receive data available.
> +	 */
> +	for (u8 i = 0; i < cp_count; i++) {
> +		hdr = FIELD_PREP(DATA_HDR_DNC, 1);
> +		hdr |= FIELD_PREP(DATA_HDR_P, oa_tc6_get_parity(hdr));
> +		*(__be32 *)&buf[i * (tc6->cps + TC6_HDR_SIZE)] = cpu_to_be32(hdr);
> +		memset(&buf[TC6_HDR_SIZE + (i * (tc6->cps + TC6_HDR_SIZE))], 0,
> +		       tc6->cps);
> +	}

This is not simple, and its the sort of code which makes me wounder if
its gone off the end of the buffer. It would be good to find somebody
internally within Microchip to review this code.

> +static void oa_tc6_rx_eth_ready(struct oa_tc6 *tc6)
> +{
> +	struct sk_buff *skb;
> +
> +	/* Send the received ethernet packet to network layer */
> +	skb = netdev_alloc_skb(tc6->netdev, tc6->rxd_bytes + NET_IP_ALIGN);
> +	if (!skb) {
> +		tc6->netdev->stats.rx_dropped++;
> +		netdev_dbg(tc6->netdev, "Out of memory for rx'd frame");

You can just return here, and skip the else. Less indentation is
better, it generally makes the code more readable.

> +	} else {
> +		skb_reserve(skb, NET_IP_ALIGN);
> +		memcpy(skb_put(skb, tc6->rxd_bytes), &tc6->eth_rx_buf[0],
> +		       tc6->rxd_bytes);
> +		skb->protocol = eth_type_trans(skb, tc6->netdev);
> +		tc6->netdev->stats.rx_packets++;
> +		tc6->netdev->stats.rx_bytes += tc6->rxd_bytes;
> +		/* 0 for NET_RX_SUCCESS and 1 for NET_RX_DROP */
> +		if (netif_rx(skb))
> +			tc6->netdev->stats.rx_dropped++;

Rather than have a comment do:

		if (netif_rx(skb) == NET_RX_DROP)
			tc6->netdev->stats.rx_dropped++;


> +static void oa_tc6_rx_eth_complete2(struct oa_tc6 *tc6, u8 *payload, u32 ftr)
> +{
> +	u16 ebo;

What does ftr and ebo mean? Its really hard to read this code because
the names are not really meaningful.

> +
> +	if (FIELD_GET(DATA_FTR_EV, ftr))
> +		ebo = FIELD_GET(DATA_FTR_EBO, ftr) + 1;
> +	else
> +		ebo = tc6->cps;
> +
> +	memcpy(&tc6->eth_rx_buf[tc6->rxd_bytes], &payload[0], ebo);
> +	tc6->rxd_bytes += ebo;
> +	if (FIELD_GET(DATA_FTR_EV, ftr)) {
> +		/* If EV set then send the received ethernet frame to n/w */
> +		oa_tc6_rx_eth_ready(tc6);
> +		tc6->rxd_bytes = 0;
> +		tc6->rx_eth_started = false;
> +	}
> +}
> +
> +static void oa_tc6_rx_eth_complete1(struct oa_tc6 *tc6, u8 *payload, u32 ftr)
> +{
> +	u16 ebo;
> +	u16 sbo;
> +
> +	sbo = FIELD_GET(DATA_FTR_SWO, ftr) * 4;
> +	ebo = FIELD_GET(DATA_FTR_EBO, ftr) + 1;
> +
> +	if (ebo <= sbo) {
> +		memcpy(&tc6->eth_rx_buf[tc6->rxd_bytes], &payload[0], ebo);
> +		tc6->rxd_bytes += ebo;
> +		oa_tc6_rx_eth_ready(tc6);
> +		tc6->rxd_bytes = 0;
> +		memcpy(&tc6->eth_rx_buf[tc6->rxd_bytes], &payload[sbo],
> +		       tc6->cps - sbo);
> +		tc6->rxd_bytes += (tc6->cps - sbo);
> +	} else {
> +		memcpy(&tc6->eth_rx_buf[tc6->rxd_bytes], &payload[sbo],
> +		       ebo - sbo);
> +		tc6->rxd_bytes += (ebo - sbo);
> +		oa_tc6_rx_eth_ready(tc6);
> +		tc6->rxd_bytes = 0;
> +	}
> +}
> +
> +static void oa_tc6_start_rx_eth(struct oa_tc6 *tc6, u8 *payload, u32 ftr)
> +{
> +	u16 sbo;
> +
> +	tc6->rxd_bytes = 0;
> +	tc6->rx_eth_started = true;
> +	sbo = FIELD_GET(DATA_FTR_SWO, ftr) * 4;
> +	memcpy(&tc6->eth_rx_buf[tc6->rxd_bytes], &payload[sbo], tc6->cps - sbo);
> +	tc6->rxd_bytes += (tc6->cps - sbo);
> +}
> +
> +static u32 oa_tc6_get_footer(struct oa_tc6 *tc6, u8 *buf, u8 cp_num)
> +{
> +	__be32 ftr;
> +
> +	ftr = *(__be32 *)&buf[tc6->cps + (cp_num * (tc6->cps + TC6_FTR_SIZE))];
> +
> +	return be32_to_cpu(ftr);
> +}
> +
> +static void oa_tc6_update_txc_rca(struct oa_tc6 *tc6, u32 ftr)
> +{
> +	tc6->txc = FIELD_GET(DATA_FTR_TXC, ftr);
> +	tc6->rca = FIELD_GET(DATA_FTR_RCA, ftr);
> +}
> +
> +static int oa_tc6_check_ftr_errors(struct oa_tc6 *tc6, u32 ftr)
> +{
> +	/* Check for footer parity error */
> +	if (oa_tc6_get_parity(ftr)) {
> +		net_err_ratelimited("%s: Footer parity error\n",
> +				    tc6->netdev->name);
> +		return FTR_ERR;
> +	}
> +	/* If EXST set in the footer then read STS0 register to get the
> +	 * status information.
> +	 */
> +	if (FIELD_GET(DATA_FTR_EXST, ftr)) {
> +		if (oa_tc6_process_exst(tc6))
> +			net_err_ratelimited("%s: Failed to process EXST\n",
> +					    tc6->netdev->name);
> +		return FTR_ERR;
> +	}
> +	if (FIELD_GET(DATA_FTR_HDRB, ftr)) {
> +		net_err_ratelimited("%s: Footer eeceived header bad\n",
> +				    tc6->netdev->name);
> +		return FTR_ERR;
> +	}
> +	if (!FIELD_GET(DATA_FTR_SYNC, ftr)) {
> +		net_err_ratelimited("%s: Footer configuration unsync\n",
> +				    tc6->netdev->name);
> +		return FTR_ERR;
> +	}
> +	return FTR_OK;
> +}
> +
> +static void oa_tc6_drop_rx_eth(struct oa_tc6 *tc6)
> +{
> +	tc6->rxd_bytes = 0;
> +	tc6->rx_eth_started = false;
> +	tc6->netdev->stats.rx_dropped++;
> +	net_err_ratelimited("%s: Footer frame drop\n",
> +			    tc6->netdev->name);
> +}
> +
> +static int oa_tc6_process_rx_chunks(struct oa_tc6 *tc6, u8 *buf, u16 len)
> +{
> +	u8 cp_count;
> +	u8 *payload;
> +	u32 ftr;
> +	int ret;
> +
> +	/* Calculate the number of chunks received */
> +	cp_count = len / (tc6->cps + TC6_FTR_SIZE);
> +
> +	for (u8 i = 0; i < cp_count; i++) {
> +		/* Get the footer and payload */
> +		ftr = oa_tc6_get_footer(tc6, buf, i);
> +		payload = &buf[(i * (tc6->cps + TC6_FTR_SIZE))];

This would be more readable:

	/* Calculate the number of chunks received */
	chunks = len / (tc6->cps + TC6_FTR_SIZE);

	for (u8 chunk = 0; chunk < chunks; chunk++) {
		/* Get the footer and payload */
		ftr = oa_tc6_get_footer(tc6, buf, chunk);
		payload = &buf[(chunk * (tc6->cps + TC6_FTR_SIZE))];

etc.

And maybe move most of this code into a function
oa_tc6_process_rx_chunk(). With lots of small functions with good
names, you need less comments.


> +		/* Check for data valid */
> +		if (FIELD_GET(DATA_FTR_DV, ftr)) {
> +			/* Check whether both start valid and end valid are in a
> +			 * single chunk payload means a single chunk payload may
> +			 * contain an entire ethernet frame.
> +			 */
> +			if (FIELD_GET(DATA_FTR_SV, ftr) &&
> +			    FIELD_GET(DATA_FTR_EV, ftr)) {


		if (FIELD_GET(DATA_FOOTER_START_VALID, footer) &&
		    FIELD_GET(DATA_FOOTER_END_VALID, footer)) {

Don't you think that is more readable?

> +static void oa_tc6_prepare_tx_chunks(struct oa_tc6 *tc6, u8 *buf,
> +				     struct sk_buff *skb)
> +{
> +	bool frame_started = false;
> +	u16 copied_bytes = 0;
> +	u16 copy_len;
> +	u32 hdr;
> +
> +	/* Calculate the number tx credit counts needed to transport the tx
> +	 * ethernet frame.
> +	 */
> +	tc6->txc_needed = (skb->len / tc6->cps) + ((skb->len % tc6->cps) ? 1 : 0);

Why call it a credit here, but a chunk when receiving?

> +static int oa_tc6_perform_spi_xfer(struct oa_tc6 *tc6)
> +{
> +	bool do_tx_again;
> +	u16 total_len;
> +	u16 rca_len;
> +	u16 tx_len;
> +	int ret;
> +
> +	do {
> +		do_tx_again = false;
> +		rca_len = 0;
> +		tx_len = 0;
> +
> +		/* In case of an interrupt, perform an empty chunk transfer to
> +		 * know the purpose of the interrupt. Interrupt may occur in
> +		 * case of RCA (Receive Chunk Available) and TXC (Transmit
> +		 * Credit Count). Both will occur if they are not indicated
> +		 * through the previous footer.
> +		 */
> +		if (tc6->int_flag) {
> +			tc6->int_flag = false;
> +			total_len = oa_tc6_prepare_empty_chunk(tc6,
> +							       tc6->spi_tx_buf,
> +							       1);
> +		} else {
> +			/* Calculate the transfer length */
> +			if (tc6->tx_flag && tc6->txc) {
> +				tx_len = oa_tc6_calculate_tx_len(tc6);
> +				memcpy(&tc6->spi_tx_buf[0],
> +				       &tc6->eth_tx_buf[tc6->tx_pos], tx_len);
> +			}
> +
> +			if (tc6->rca)
> +				rca_len = oa_tc6_calculate_rca_len(tc6, tx_len);
> +
> +			total_len = tx_len + rca_len;
> +		}
> +		ret = oa_tc6_spi_transfer(tc6->spi, tc6->spi_tx_buf,
> +					  tc6->spi_rx_buf, total_len);
> +		if (ret)
> +			return ret;
> +		/* Process the rxd chunks to get the ethernet frame or status */
> +		ret = oa_tc6_process_rx_chunks(tc6, tc6->spi_rx_buf, total_len);
> +		if (ret)
> +			return ret;
> +		if (tc6->tx_flag) {
> +			tc6->tx_pos += tx_len;
> +			tc6->txc_needed = tc6->txc_needed -
> +					  (tx_len / (tc6->cps + TC6_HDR_SIZE));
> +			/* If the complete ethernet frame is transmitted then
> +			 * return the skb and update the details to n/w layer.
> +			 */
> +			if (!tc6->txc_needed)
> +				oa_tc6_tx_eth_complete(tc6);
> +			else if (tc6->txc)
> +				/* If txc is available again and updated from
> +				 * the previous footer then perform tx again.
> +				 */
> +				do_tx_again = true;
> +		}
> +
> +		/* If rca is updated from the previous footer then perform empty
> +		 * tx to receive ethernet frame.
> +		 */
> +		if (tc6->rca)
> +			do_tx_again = true;
> +	} while (do_tx_again);

The coding standard say:

Functions should be short and sweet, and do just one thing. They
should fit on one or two screenfuls of text (the ISO/ANSI screen size
is 80x24, as we all know), and do one thing and do that well.

This is too long, and does too many things.

     Andrew

  reply	other threads:[~2023-10-24  2:07 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-23 15:46 [PATCH net-next v2 0/9] Add support for OPEN Alliance 10BASE-T1x MACPHY Serial Interface Parthiban Veerasooran
2023-10-23 15:46 ` [PATCH net-next v2 1/9] net: ethernet: implement OPEN Alliance control transaction interface Parthiban Veerasooran
2023-10-23 21:28   ` Andrew Lunn
2023-10-25 11:16     ` Parthiban.Veerasooran
2023-10-26 19:46       ` Andrew Lunn
2023-10-27  7:15         ` Parthiban.Veerasooran
2023-10-23 23:09   ` kernel test robot
2023-10-25 19:09   ` kernel test robot
2023-10-23 15:46 ` [PATCH net-next v2 2/9] net: ethernet: oa_tc6: implement mac-phy software reset Parthiban Veerasooran
2023-10-23 22:43   ` Andrew Lunn
2023-10-25 11:39     ` Parthiban.Veerasooran
2023-10-26 20:01       ` Andrew Lunn
2023-10-27  7:00         ` Parthiban.Veerasooran
2023-10-25 11:39     ` Parthiban.Veerasooran
2023-10-23 15:46 ` [PATCH net-next v2 3/9] net: ethernet: oa_tc6: implement OA TC6 configuration function Parthiban Veerasooran
2023-10-23 22:58   ` Andrew Lunn
2023-10-25 12:02     ` Parthiban.Veerasooran
2023-10-26 20:06       ` Andrew Lunn
2023-10-27  7:10         ` Parthiban.Veerasooran
2023-10-23 15:46 ` [PATCH net-next v2 4/9] dt-bindings: net: add OPEN Alliance 10BASE-T1x MAC-PHY Serial Interface Parthiban Veerasooran
2023-10-23 17:40   ` Rob Herring
2023-10-25 12:28     ` Parthiban.Veerasooran
2023-10-24  0:37   ` Andrew Lunn
2023-10-27  9:12     ` Parthiban.Veerasooran
     [not found]       ` <d6377de8-603f-4ac1-a691-b79c46a5057b@lunn.ch>
2023-10-30 10:09         ` Parthiban.Veerasooran
2023-10-24  7:44   ` Krzysztof Kozlowski
2023-10-31 12:59     ` Parthiban.Veerasooran
2023-10-23 15:46 ` [PATCH net-next v2 5/9] net: ethernet: oa_tc6: implement internal PHY initialization Parthiban Veerasooran
2023-10-24  0:56   ` Andrew Lunn
2023-10-31  4:20     ` Parthiban.Veerasooran
2023-10-31 12:48       ` Andrew Lunn
2023-11-01  4:53         ` Parthiban.Veerasooran
2023-10-23 15:46 ` [PATCH net-next v2 6/9] dt-bindings: net: oa-tc6: add PHY register access capability Parthiban Veerasooran
2023-10-24  1:21   ` Andrew Lunn
2023-10-31  4:22     ` Parthiban.Veerasooran
2023-10-23 15:46 ` [PATCH net-next v2 7/9] net: ethernet: oa_tc6: implement data transaction interface Parthiban Veerasooran
2023-10-24  2:07   ` Andrew Lunn [this message]
2023-10-31  8:26     ` Parthiban.Veerasooran
2023-10-23 15:46 ` [PATCH net-next v2 8/9] microchip: lan865x: add driver support for Microchip's LAN865X MACPHY Parthiban Veerasooran
2023-10-24  2:33   ` Andrew Lunn
2023-10-31 11:04     ` Parthiban.Veerasooran
2023-10-31 12:53       ` Andrew Lunn
2023-11-01  4:51         ` Parthiban.Veerasooran
2023-10-24 11:57   ` Krzysztof Kozlowski
2023-10-31 10:25     ` Parthiban.Veerasooran
2023-11-02 12:20   ` Ramón Nordin Rodriguez
2023-11-02 12:39     ` Andrew Lunn
2023-11-02 13:40       ` Ramón Nordin Rodriguez
2023-11-03 14:59     ` Parthiban.Veerasooran
2023-11-06  8:59       ` Ramón Nordin Rodriguez
2023-10-23 15:46 ` [PATCH net-next v2 9/9] dt-bindings: net: add Microchip's LAN865X 10BASE-T1S MACPHY Parthiban Veerasooran
2023-10-23 17:40   ` Rob Herring
2023-10-30 12:41     ` Parthiban.Veerasooran
2023-10-24  8:03   ` Krzysztof Kozlowski
2023-10-30 13:16     ` Parthiban.Veerasooran
2023-10-30 14:45       ` Krzysztof Kozlowski
2023-11-02 13:31         ` Parthiban.Veerasooran
2024-03-24 11:55 ` [PATCH net-next v2 0/9] Add support for OPEN Alliance 10BASE-T1x MACPHY Serial Interface Benjamin Bigler
2024-03-25 13:24   ` Parthiban.Veerasooran
2024-03-25 14:01     ` Andrew Lunn
2024-03-26  9:43       ` Parthiban.Veerasooran
2024-04-03 21:40     ` Benjamin Bigler
2024-04-08 13:41       ` Parthiban.Veerasooran

Reply instructions:

You may reply publicly 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=9b7a5ed9-840f-4346-a168-e538a8477714@lunn.ch \
    --to=andrew@lunn.ch \
    --cc=Nicolas.Ferre@microchip.com \
    --cc=Parthiban.Veerasooran@microchip.com \
    --cc=Thorsten.Kummermehr@microchip.com \
    --cc=UNGLinuxDriver@microchip.com \
    --cc=Woojung.Huh@microchip.com \
    --cc=casper.casan@gmail.com \
    --cc=conor+dt@kernel.org \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=devicetree@vger.kernel.org \
    --cc=edumazet@google.com \
    --cc=horatiu.vultur@microchip.com \
    --cc=horms@kernel.org \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=kuba@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=rdunlap@infradead.org \
    --cc=robh+dt@kernel.org \
    --cc=steen.hegelund@microchip.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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®