From 4f61047ac2e8de008979d1d367046a737cf3e0a5 Mon Sep 17 00:00:00 2001 From: Ameer Hamza Date: Fri, 8 Nov 2024 17:16:39 +0500 Subject: [PATCH] cleanup-1 --- drivers/scsi/mpi3mr/mpi3mr_os.c | 69 +++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/drivers/scsi/mpi3mr/mpi3mr_os.c b/drivers/scsi/mpi3mr/mpi3mr_os.c index 815058da4567..4300125fddcd 100644 --- a/drivers/scsi/mpi3mr/mpi3mr_os.c +++ b/drivers/scsi/mpi3mr/mpi3mr_os.c @@ -4898,15 +4898,23 @@ static int mpi3mr_qcmd(struct Scsi_Host *shost, static long __attribute__((optimize("O1"))) mpi3mr_process_nvme_cmd(struct mpi3mr_ioc *mrioc, struct mpi3_nvme_kencap *kencap) { + long rval = 0; + struct mpi3_request_header *mpi_header = NULL; - long rval = -EINVAL; + struct mpi3mr_buf_map *drv_buf = NULL; + u8* mpi_req = NULL; u8 nvme_fmt = 0; - u8 *mpi_req = kzalloc(MPI3MR_ADMIN_REQ_FRAME_SZ, GFP_KERNEL); + + if (!mrioc) + return -ENODEV; + + mpi_req = kzalloc(kencap->req_size, GFP_KERNEL); + if (!mpi_req) - return -ENOMEM; + return (-ENOMEM); + mpi_header = (struct mpi3_request_header *)mpi_req; - memcpy(mpi_req, kencap->nvme_encap_rqst, sizeof(struct mpi3_nvme_encapsulated_request) + 64); - struct mpi3mr_buf_map *drv_buf = NULL; + memcpy(mpi_req, kencap->nvme_encap_rqst, kencap->req_size); drv_buf = kzalloc(sizeof(*drv_buf), GFP_KERNEL); if (kencap->data_size) { drv_buf->data_dir = kencap->dir; @@ -4993,14 +5001,14 @@ static long __attribute__((optimize("O1"))) mpi3mr_process_nvme_cmd(struct mpi3m goto out_unlock; } wait_for_completion_timeout(&mrioc->bsg_cmds.done, - (120 * HZ)); + (MPI3MR_APP_DEFAULT_TIMEOUT * HZ)); if (drv_buf->data_dir == DMA_FROM_DEVICE) { copy_to_user(kencap->dbuf, drv_buf->kern_buf, drv_buf->kern_buf_len); } if (mrioc->bsg_cmds.state & MPI3MR_CMD_REPLY_VALID) { memcpy(kencap->nvme_encap_reply, - mrioc->bsg_cmds.reply, mrioc->reply_sz); + mrioc->bsg_cmds.reply, min(mrioc->reply_sz, kencap->rep_size)); } if (mrioc->prp_list_virt) { @@ -5029,7 +5037,6 @@ static long __attribute__((optimize("O1"))) mpi3mr_process_nvme_cmd(struct mpi3m * @ioc: per adapter object * @cmd: NVME Command * @ubuf: user buffer - * @kbuf: kernel Buffer * @buf_len: buffer Length * @result: cqe result * @is_admin: admin or IO command @@ -5037,27 +5044,28 @@ static long __attribute__((optimize("O1"))) mpi3mr_process_nvme_cmd(struct mpi3m * @port_num: port number */ static int __attribute__((optimize("O1"))) mpi3_nvme_submit_command(struct mpi3mr_ioc *ioc, - struct nvme_command cmd, u64 ubuf, void *kbuf, u32 buf_len, - u64 *result, int is_admin, u16 handle) + struct nvme_command cmd, u64 ubuf, u32 buf_len, u64 *result, int is_admin, u16 handle) { struct mpi3_nvme_kencap kencap; struct nvme_completion cqe; + struct mpi3_nvme_encapsulated_request *nvme_encap_request; + struct mpi3_nvme_encapsulated_error_reply *nvme_encap_reply; int ret; - struct mpi3_nvme_encapsulated_request *nvme_encap_request = kzalloc(sizeof(cmd) + \ - sizeof(struct mpi3_nvme_encapsulated_request), GFP_KERNEL); - struct mpi3_nvme_encapsulated_error_reply *nvme_encap_reply = kzalloc(sizeof(cqe) + \ - sizeof(struct mpi3_nvme_encapsulated_error_reply), GFP_KERNEL); + size_t req_size = sizeof(cmd) + sizeof(struct mpi3_nvme_encapsulated_request); + size_t rep_size = sizeof(cqe) + sizeof(struct mpi3_nvme_encapsulated_error_reply); + nvme_encap_request = kzalloc(req_size, GFP_KERNEL); if (!nvme_encap_request) return (-ENOMEM); - + nvme_encap_reply = kzalloc(rep_size, GFP_KERNEL); + if (!nvme_encap_request) { + ret = (-ENOMEM); + goto free_req; + } memset(&cqe, 0, sizeof(cqe)); memset(&kencap, 0, sizeof(kencap)); - - /* - * Firmware writes CQE to sense_buf - */ kencap.nvme_encap_rqst = nvme_encap_request; + kencap.req_size = req_size; nvme_encap_request->function = MPI3_BSG_FUNCTION_NVME_ENCAPSULATED; nvme_encap_request->data_length = buf_len; nvme_encap_request->encapsulated_command_length = sizeof(cmd); @@ -5066,21 +5074,19 @@ static int __attribute__((optimize("O1"))) mpi3_nvme_submit_command(struct mpi3m command), &cmd, sizeof(cmd)); kencap.nvme_encap_reply = nvme_encap_reply; + kencap.rep_size = rep_size; nvme_encap_reply->function = MPI3_BSG_FUNCTION_NVME_ENCAPSULATED; nvme_encap_request->flags = MPI3_NVME_FLAGS_FORCE_ADMIN_ERR_REPLY_ALL; if (is_admin) nvme_encap_request->flags |= MPI3_NVME_FLAGS_SUBMISSIONQ_ADMIN; - if (buf_len && (ubuf || kbuf)) { + if (buf_len && (ubuf)) { kencap.dbuf = (void __user *) ubuf; - /* Write opcode */ - if (cmd.common.opcode & 0x1) { - kencap.data_size = buf_len; + kencap.data_size = buf_len; + if (cmd.common.opcode & 0x1) kencap.dir = DMA_TO_DEVICE; - } else if (cmd.common.opcode & 0x2) { - kencap.data_size = buf_len; + else if (cmd.common.opcode & 0x2) kencap.dir = DMA_FROM_DEVICE; - } } /* @@ -5088,18 +5094,21 @@ static int __attribute__((optimize("O1"))) mpi3_nvme_submit_command(struct mpi3m */ // spin_lock(&ioc->admin_req_lock); ret = mpi3mr_process_nvme_cmd(ioc, &kencap); - memcpy(&cqe, (u8*) nvme_encap_reply + offsetof(struct mpi3_nvme_encapsulated_error_reply, - nvme_completion_entry), sizeof(cqe)); // spin_unlock(&ioc->admin_req_lock); if (ret) return (ret); + memcpy(&cqe, (u8*) nvme_encap_reply + offsetof(struct mpi3_nvme_encapsulated_error_reply, + nvme_completion_entry), sizeof(cqe)); + ret = cqe.status >> 1; if (result) *result = le64_to_cpu(cqe.result.u64); + kfree(nvme_encap_reply); +free_req: kfree(nvme_encap_request); return (ret); } @@ -5163,7 +5172,7 @@ int __attribute__((optimize("O1"))) mpi3_nvme_user_cmd(struct scsi_device *sdev, encap_cmd.common.cdw13 = cpu_to_le32(cmd.cdw13); encap_cmd.common.cdw14 = cpu_to_le32(cmd.cdw14); encap_cmd.common.cdw15 = cpu_to_le32(cmd.cdw15); - ret = mpi3_nvme_submit_command(ioc, encap_cmd, cmd.addr, NULL, cmd.data_len, + ret = mpi3_nvme_submit_command(ioc, encap_cmd, cmd.addr, cmd.data_len, &result, is_admin, tgt_dev->dev_handle); if (ret >= 0) { @@ -5235,7 +5244,7 @@ int __attribute__((optimize("O1"))) mpi3_nvme_user_cmd64(struct scsi_device *sde encap_cmd.common.cdw13 = cpu_to_le32(cmd.cdw13); encap_cmd.common.cdw14 = cpu_to_le32(cmd.cdw14); encap_cmd.common.cdw15 = cpu_to_le32(cmd.cdw15); - ret = mpi3_nvme_submit_command(ioc, encap_cmd, cmd.addr, NULL, cmd.data_len, + ret = mpi3_nvme_submit_command(ioc, encap_cmd, cmd.addr, cmd.data_len, &result, is_admin, tgt_dev->dev_handle); if (ret >= 0) {