All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@linaro.org>
To: Aditya Garg <gargaditya08@live.com>
Cc: "gregkh@linuxfoundation.org" <gregkh@linuxfoundation.org>,
	"bhelgaas@google.com" <bhelgaas@google.com>,
	"joro@8bytes.org" <joro@8bytes.org>,
	"will@kernel.org" <will@kernel.org>,
	"robin.murphy@arm.com" <robin.murphy@arm.com>,
	"andriy.shevchenko@linux.intel.com"
	<andriy.shevchenko@linux.intel.com>,
	"linux-staging@lists.linux.dev" <linux-staging@lists.linux.dev>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	"linux-pci@vger.kernel.org" <linux-pci@vger.kernel.org>,
	"iommu@lists.linux.dev" <iommu@lists.linux.dev>,
	Aun-Ali Zaidi <admin@kodeit.net>, "paul@mrarm.io" <paul@mrarm.io>,
	Orlando Chamberlain <orlandoch.dev@gmail.com>
Subject: Re: [PATCH RFC] staging: Add driver to communicate with the T2 Security Chip
Date: Mon, 10 Mar 2025 11:14:48 +0300	[thread overview]
Message-ID: <f483ac12-c731-4dc8-9de5-059e07ff2c85@stanley.mountain> (raw)
In-Reply-To: <1A12CB39-B4FD-4859-9CD7-115314D97C75@live.com>

On Sun, Mar 09, 2025 at 08:40:31AM +0000, Aditya Garg wrote:
> diff --git a/drivers/staging/apple-bce/apple_bce.c b/drivers/staging/apple-bce/apple_bce.c
> new file mode 100644
> index 000000000..c66e0c8d5
> --- /dev/null
> +++ b/drivers/staging/apple-bce/apple_bce.c
> @@ -0,0 +1,448 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +
> +#include "apple_bce.h"
> +#include <linux/module.h>
> +#include <linux/crc32.h>
> +#include "audio/audio.h"
> +#include <linux/version.h>
> +
> +static dev_t bce_chrdev;
> +static struct class *bce_class;
> +
> +struct apple_bce_device *global_bce;
> +
> +static int bce_create_command_queues(struct apple_bce_device *bce);
> +static void bce_free_command_queues(struct apple_bce_device *bce);
> +static irqreturn_t bce_handle_mb_irq(int irq, void *dev);
> +static irqreturn_t bce_handle_dma_irq(int irq, void *dev);
> +static int bce_fw_version_handshake(struct apple_bce_device *bce);
> +static int bce_register_command_queue(struct apple_bce_device *bce, struct bce_queue_memcfg *cfg, int is_sq);
> +
> +static int apple_bce_probe(struct pci_dev *dev, const struct pci_device_id *id)
> +{
> +	struct apple_bce_device *bce = NULL;
> +	int status = 0;
> +	int nvec;
> +
> +	pr_info("apple-bce: capturing our device\n");
> +
> +	if (pci_enable_device(dev))
> +		return -ENODEV;

	ret = pci_enable_device(dev);
	if (ret)
		return ret;

> +	if (pci_request_regions(dev, "apple-bce")) {

Same everywhere.  Propagate the error code.

> +		status = -ENODEV;
> +		goto fail;

Instead of "goto fail;" it's better to use a full unwind ladder
with better label names.
https://staticthinking.wordpress.com/2022/04/28/free-the-last-thing-style/

> +	}
> +	pci_set_master(dev);
> +	nvec = pci_alloc_irq_vectors(dev, 1, 8, PCI_IRQ_MSI);
> +	if (nvec < 5) {
> +		status = -EINVAL;
> +		goto fail;
> +	}
> +
> +	bce = kzalloc(sizeof(struct apple_bce_device), GFP_KERNEL);

	bce = kzalloc(sizeof(*bce), GFP_KERNEL);

> +	if (!bce) {
> +		status = -ENOMEM;
> +		goto fail;
> +	}
> +
> +	bce->pci = dev;
> +	pci_set_drvdata(dev, bce);
> +
> +	bce->devt = bce_chrdev;
> +	bce->dev = device_create(bce_class, &dev->dev, bce->devt, NULL, "apple-bce");
> +	if (IS_ERR_OR_NULL(bce->dev)) {
> +		status = PTR_ERR(bce_class);


device_create() can't return NULL.
https://staticthinking.wordpress.com/2022/08/01/mixing-error-pointers-and-null/

> +		goto fail;
> +	}
> +
> +	bce->reg_mem_mb = pci_iomap(dev, 4, 0);
> +	bce->reg_mem_dma = pci_iomap(dev, 2, 0);
> +
> +	if (IS_ERR_OR_NULL(bce->reg_mem_mb) || IS_ERR_OR_NULL(bce->reg_mem_dma)) {
> +		dev_warn(&dev->dev, "apple-bce: Failed to pci_iomap required regions\n");
> +		goto fail;
> +	}
> +
> +	bce_mailbox_init(&bce->mbox, bce->reg_mem_mb);
> +	bce_timestamp_init(&bce->timestamp, bce->reg_mem_mb);
> +
> +	spin_lock_init(&bce->queues_lock);
> +	ida_init(&bce->queue_ida);
> +
> +	if ((status = pci_request_irq(dev, 0, bce_handle_mb_irq, NULL, dev, "bce_mbox")))

I think checkpatch will complain about this.  Do it as.

	status = pci_request_irq(dev, 0, bce_handle_mb_irq, NULL, dev, "bce_mbox");
	if (status)

> +		goto fail;
> +	if ((status = pci_request_irq(dev, 4, NULL, bce_handle_dma_irq, dev, "bce_dma")))
> +		goto fail_interrupt_0;
> +
> +	if ((status = dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(37)))) {
> +		dev_warn(&dev->dev, "dma: Setting mask failed\n");
> +		goto fail_interrupt;
> +	}
> +
> +	/* Gets the function 0's interface. This is needed because Apple only accepts DMA on our function if function 0
> +	   is a bus master, so we need to work around this. */
> +	bce->pci0 = pci_get_slot(dev->bus, PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
> +#ifndef WITHOUT_NVME_PATCH

Delete dead code?

> +	if ((status = pci_enable_device_mem(bce->pci0))) {
> +		dev_warn(&dev->dev, "apple-bce: failed to enable function 0\n");
> +		goto fail_dev0;
> +	}
> +#endif
> +	pci_set_master(bce->pci0);
> +
> +	bce_timestamp_start(&bce->timestamp, true);
> +
> +	if ((status = bce_fw_version_handshake(bce)))
> +		goto fail_ts;
> +	pr_info("apple-bce: handshake done\n");
> +
> +	if ((status = bce_create_command_queues(bce))) {
> +		pr_info("apple-bce: Creating command queues failed\n");
> +		goto fail_ts;
> +	}
> +
> +	global_bce = bce;

Do this right before the "return 0;"?

> +
> +	bce_vhci_create(bce, &bce->vhci);

Check for errors.

> +
> +	return 0;
> +
> +fail_ts:
> +	bce_timestamp_stop(&bce->timestamp);
> +#ifndef WITHOUT_NVME_PATCH
> +	pci_disable_device(bce->pci0);
> +fail_dev0:
> +#endif
> +	pci_dev_put(bce->pci0);
> +fail_interrupt:
> +	pci_free_irq(dev, 4, dev);
> +fail_interrupt_0:
> +	pci_free_irq(dev, 0, dev);
> +fail:
> +	if (bce && bce->dev) {
> +		device_destroy(bce_class, bce->devt);
> +
> +		if (!IS_ERR_OR_NULL(bce->reg_mem_mb))
> +			pci_iounmap(dev, bce->reg_mem_mb);
> +		if (!IS_ERR_OR_NULL(bce->reg_mem_dma))
> +			pci_iounmap(dev, bce->reg_mem_dma);
> +
> +		kfree(bce);
> +	}
> +
> +	pci_free_irq_vectors(dev);
> +	pci_release_regions(dev);
> +	pci_disable_device(dev);
> +
> +	if (!status)
> +		status = -EINVAL;

This is like saying "if the code is buggy then fix it" but it's better to
just not introduce bugs.  Which sounds difficult and unreliable, but it's
even more difficult and unreliable to predict which bugs people will add
and fix them correctly.

> +	return status;
> +}

regards,
dan carpenter


  parent reply	other threads:[~2025-03-10  8:14 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-09  8:40 [PATCH RFC] staging: Add driver to communicate with the T2 Security Chip Aditya Garg
2025-03-09  8:44 ` Aditya Garg
2025-03-09  8:49   ` gregkh
2025-03-09  9:00     ` Aditya Garg
2025-03-09  9:13       ` gregkh
2025-03-09  8:52 ` gregkh
2025-03-09  8:54   ` gregkh
2025-03-09  9:05     ` Aditya Garg
2025-03-09  9:15       ` gregkh
2025-03-09  9:03   ` Aditya Garg
2025-03-09  9:14     ` gregkh
2025-03-09  9:28       ` Aditya Garg
2025-03-09  9:37         ` gregkh
2025-03-09  9:41           ` Aditya Garg
2025-03-09  9:48             ` gregkh
2025-03-09  9:52               ` Aditya Garg
2025-03-09  9:55                 ` gregkh
2025-03-09 10:12                   ` Aditya Garg
2025-03-09 10:22                     ` gregkh
2025-03-09 10:33                       ` Aditya Garg
2025-03-10  8:14 ` Dan Carpenter [this message]
2025-03-10  8:45   ` Aditya Garg
2025-03-10 13:49 ` Robin Murphy
2025-03-10 13:54   ` andriy.shevchenko
2025-03-10 13:59     ` Aditya Garg

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=f483ac12-c731-4dc8-9de5-059e07ff2c85@stanley.mountain \
    --to=dan.carpenter@linaro.org \
    --cc=admin@kodeit.net \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=bhelgaas@google.com \
    --cc=gargaditya08@live.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=iommu@lists.linux.dev \
    --cc=joro@8bytes.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=linux-staging@lists.linux.dev \
    --cc=orlandoch.dev@gmail.com \
    --cc=paul@mrarm.io \
    --cc=robin.murphy@arm.com \
    --cc=will@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.