Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp12362497pxu; Fri, 1 Jan 2021 19:07:55 -0800 (PST) X-Google-Smtp-Source: ABdhPJzTdK+iQUO7TF5duEw7lykf+gu19oBcBlpUsNGB9cG5O24AKp5egC1tlxJflqYOn0nh5CUt X-Received: by 2002:a05:6402:a5b:: with SMTP id bt27mr62545387edb.222.1609556875049; Fri, 01 Jan 2021 19:07:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609556875; cv=none; d=google.com; s=arc-20160816; b=LZm3+OGhjJgS5lqWolLvnHRb7XBVyg34TvtHqBmH3ol7jQJoRiB2qIlIz8hA0Kqzrf feKi8j7KtCMzvBky2Fli1ClAfoZilv3SoPdj5MPcN2FQQeyRH8485uCMQgcJUK2s7iP6 UaH+OvAzTDGbrbWgWt3nHLONuzf5EcwZiBjhQ1+xkgf0P93dVcoUajc9nbEA0IPaph9u oPg8hQM6az7KgMSLcX/XhOsGs6c4mtOGQ6ZqKLjDridb1ykp84x8cKOwvB8ZtS3lekkQ CDBxQDZO5w9vcljypZkwG677hfo/41SzxmD1T+FtvU/ezMkfCMBTAwAaiMGi5VlIXhus Onog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from:ironport-sdr :ironport-sdr; bh=INx962dVjbCl9tbagFcESUKvqYjo4DnEcBENYhnBGLk=; b=b7ptCeMj94mIIaHI3NBZxnUSAJlzA8ahGVepGGvZQqN6nYDguD1snyAlgGKL09kiof 9A5WziWRfHY0vRU1xl7mUnyI9WFt1hy1/vXDytBaJII5ecJJdmfFAWz2jOAKVhh3h7mr ljjROkIJDfwp9yu9o0AE632GhqWf0TIX3AMuPnn68stTw8vkoPuBHxHwaMxW4R0ukb56 q3skfEBonwi66l5agBiuOjKu8faYPV59h5TIUIsbml/e2FNPlP/VsT+CVgxzjP25FH3E 2YjhrGXU7AH9U/pWrapAFK7O6pcY4mb7lAMS9BX3yFFEbnrVrwx5+4Up5U/Im+0d08U4 kW5w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r4si29293024edb.431.2021.01.01.19.07.14; Fri, 01 Jan 2021 19:07:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727207AbhABDFN (ORCPT + 99 others); Fri, 1 Jan 2021 22:05:13 -0500 Received: from mga11.intel.com ([192.55.52.93]:35271 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727133AbhABDFN (ORCPT ); Fri, 1 Jan 2021 22:05:13 -0500 IronPort-SDR: vVERRXup/ZnysZn4LstdiLINtKbAr4/jn/Hnt6S8QUC/tJdVjBIqjKu+K713o1bpHaXhGPInGR 5q63ahJLhcbg== X-IronPort-AV: E=McAfee;i="6000,8403,9851"; a="173297445" X-IronPort-AV: E=Sophos;i="5.78,468,1599548400"; d="scan'208";a="173297445" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Jan 2021 19:04:32 -0800 IronPort-SDR: VGzmKTwRMOJvIvSjAiyTHEPh+kBB0ZmvyEyO2MUStDs0VzkdkpK+OYxui9GmHdIa/inYzXXslf Yr2GbaJPSVMQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.78,468,1599548400"; d="scan'208";a="349083059" Received: from yilunxu-optiplex-7050.sh.intel.com ([10.239.159.141]) by fmsmga008.fm.intel.com with ESMTP; 01 Jan 2021 19:04:30 -0800 From: Xu Yilun To: broonie@kernel.org, linux-spi@vger.kernel.org Cc: trix@redhat.com, lgoncalv@redhat.com, yilun.xu@intel.com, hao.wu@intel.com, matthew.gerlach@linux.intel.com, russell.h.weight@intel.com, linux-kernel@vger.kernel.org Subject: [PATCH v2] spi: fix the divide by 0 error when calculating xfer waiting time Date: Sat, 2 Jan 2021 10:59:46 +0800 Message-Id: <1609556386-19422-1-git-send-email-yilun.xu@intel.com> X-Mailer: git-send-email 2.7.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The xfer waiting time is the result of xfer->len / xfer->speed_hz. This patch makes the assumption of 1khz xfer speed if the xfer->speed_hz is not assigned and stays 0. This avoids the divide by 0 issue and ensures a reasonable tolerant waiting time. Signed-off-by: Xu Yilun --- v2: use the normal conditional statement instead of the ternery operator change the default xfer speed from 1khz to 100khz --- drivers/spi/spi.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 51d7c00..aacae88 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -1108,6 +1108,7 @@ static int spi_transfer_wait(struct spi_controller *ctlr, { struct spi_statistics *statm = &ctlr->statistics; struct spi_statistics *stats = &msg->spi->statistics; + u32 speed_hz = xfer->speed_hz; unsigned long long ms; if (spi_controller_is_slave(ctlr)) { @@ -1116,8 +1117,11 @@ static int spi_transfer_wait(struct spi_controller *ctlr, return -EINTR; } } else { + if (!speed_hz) + speed_hz = 100000; + ms = 8LL * 1000LL * xfer->len; - do_div(ms, xfer->speed_hz); + do_div(ms, speed_hz); ms += ms + 200; /* some tolerance */ if (ms > UINT_MAX) -- 2.7.4