Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp5059168imu; Tue, 25 Dec 2018 16:46:42 -0800 (PST) X-Google-Smtp-Source: ALg8bN4ERblRpAj7IMtI+jPj9UglX9PI5XZh5xsA4pJO+GYi5wxg0REdlXcBkru8vzxv3vS5XTy8 X-Received: by 2002:a63:8f45:: with SMTP id r5mr16881376pgn.222.1545785202385; Tue, 25 Dec 2018 16:46:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545785202; cv=none; d=google.com; s=arc-20160816; b=sOUM8MkuhTlLLX4T0Sv/TrWU6lT6yWnewpby3pEJvT7PVwhWVjNmO65kybLF6TZLJe 4adY23ZPV07RJRhguIas43GNXRzfyVFHJEsVN3jxZRU1rRXbQv3tzwunzs/xsG27mNsL vFMbUiu2vAPadagVZ+P1pLzq00T/ifGGmkc0SrTsp64kqXGOgQAxQNbk+q4JcGj3nkX/ iBPrLqWWckif+GsHEw06aYWjFARdk6CjUzjRyrI71kGckzhWnpdLYNPdjfiSZuidq+jx 8cVjBXxVWDyy7pGIwCU0CBzCOCLknPHUflCdimGPIky3Tsuv18T2l3+NuLpMnsfZPFby ioJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:date:subject:from:references:in-reply-to :message-id:cc:to; bh=0/GMliJ6VQoTNYoWyoNM1W+rsIIyW6/dcZGXM/V7gdc=; b=u98thufb2ebqnji7DkgPuhrdxgpEVhHcSKu6lTUoc9DiKg4MC2eRoSG8zLqUyacou+ bW34ZwksTWhSChOqVlD7p7ijZHFZShHl7D+0wuFBcxMQ8w6x1c+4LDs79NQl2dL9RgKk sd7TnIAi01y2n7F9g0X5dpsBqmnmtvJ1QxmsyfXBBH102A0iW87citgHxAo4bLK0hvAq IDvS8kIA8o54h1LQrYui7z4LnSt5MLcZx8Tx2V4KlgG9EiYZ45a3wWc3wYq4WSR+7Ubw 3mZCN1Eo89xHxqRQfEpM/d4Pc/ZADe03rpEozATnploueVUzPWlMwrw4i6cpdByzZXqB oZtQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c8si17331097pgc.65.2018.12.25.16.46.06; Tue, 25 Dec 2018 16:46:42 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729629AbeLZAoE (ORCPT + 99 others); Tue, 25 Dec 2018 19:44:04 -0500 Received: from kvm5.telegraphics.com.au ([98.124.60.144]:57332 "EHLO kvm5.telegraphics.com.au" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725987AbeLZAoA (ORCPT ); Tue, 25 Dec 2018 19:44:00 -0500 Received: by kvm5.telegraphics.com.au (Postfix, from userid 502) id 002362951B; Tue, 25 Dec 2018 19:43:55 -0500 (EST) To: Arnd Bergmann , Greg Kroah-Hartman Cc: linux-kernel@vger.kernel.org, linux-m68k@lists.linux-m68k.org, linuxppc-dev@lists.ozlabs.org Message-Id: In-Reply-To: References: From: Finn Thain Subject: [PATCH v8 07/25] char/nvram: Allow the set_checksum and initialize ioctls to be omitted Date: Wed, 26 Dec 2018 11:37:59 +1100 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The drivers/char/nvram module has previously only supported RTC "CMOS" NVRAM, for which it provides appropriate checksum ioctls. Make these ioctls optional so the module can be re-used with other kinds of NVRAM. The ops struct methods that implement the ioctls now return error codes so that a multi-platform kernel binary can do the right thing when running on hardware without suitable NVRAM. Signed-off-by: Finn Thain --- drivers/char/nvram.c | 70 ++++++++++++++++++++++++------------------- include/linux/nvram.h | 2 ++ 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/drivers/char/nvram.c b/drivers/char/nvram.c index 00897daa0643..33ef3b02d365 100644 --- a/drivers/char/nvram.c +++ b/drivers/char/nvram.c @@ -153,16 +153,25 @@ static void __nvram_set_checksum(void) __nvram_write_byte(sum & 0xff, PC_CKS_LOC + 1); } -#if 0 -void nvram_set_checksum(void) +static long nvram_set_checksum(void) { - unsigned long flags; + spin_lock_irq(&rtc_lock); + __nvram_set_checksum(); + spin_unlock_irq(&rtc_lock); + return 0; +} - spin_lock_irqsave(&rtc_lock, flags); +static long nvram_initialize(void) +{ + ssize_t i; + + spin_lock_irq(&rtc_lock); + for (i = 0; i < NVRAM_BYTES; ++i) + __nvram_write_byte(0, i); __nvram_set_checksum(); - spin_unlock_irqrestore(&rtc_lock, flags); + spin_unlock_irq(&rtc_lock); + return 0; } -#endif /* 0 */ static ssize_t nvram_get_size(void) { @@ -173,6 +182,8 @@ const struct nvram_ops arch_nvram_ops = { .read_byte = nvram_read_byte, .write_byte = nvram_write_byte, .get_size = nvram_get_size, + .set_checksum = nvram_set_checksum, + .initialize = nvram_initialize, }; EXPORT_SYMBOL(arch_nvram_ops); #endif /* CONFIG_X86 */ @@ -258,51 +269,50 @@ static ssize_t nvram_misc_write(struct file *file, const char __user *buf, static long nvram_misc_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { - int i; + long ret = -ENOTTY; switch (cmd) { - case NVRAM_INIT: /* initialize NVRAM contents and checksum */ if (!capable(CAP_SYS_ADMIN)) return -EACCES; - mutex_lock(&nvram_mutex); - spin_lock_irq(&rtc_lock); - - for (i = 0; i < NVRAM_BYTES; ++i) - __nvram_write_byte(0, i); - __nvram_set_checksum(); - - spin_unlock_irq(&rtc_lock); - mutex_unlock(&nvram_mutex); - return 0; - + if (arch_nvram_ops.initialize != NULL) { + mutex_lock(&nvram_mutex); + ret = arch_nvram_ops.initialize(); + mutex_unlock(&nvram_mutex); + } + break; case NVRAM_SETCKS: /* just set checksum, contents unchanged (maybe useful after * checksum garbaged somehow...) */ if (!capable(CAP_SYS_ADMIN)) return -EACCES; - mutex_lock(&nvram_mutex); - spin_lock_irq(&rtc_lock); - __nvram_set_checksum(); - spin_unlock_irq(&rtc_lock); - mutex_unlock(&nvram_mutex); - return 0; - - default: - return -ENOTTY; + if (arch_nvram_ops.set_checksum != NULL) { + mutex_lock(&nvram_mutex); + ret = arch_nvram_ops.set_checksum(); + mutex_unlock(&nvram_mutex); + } + break; } + return ret; } static int nvram_misc_open(struct inode *inode, struct file *file) { spin_lock(&nvram_state_lock); + /* Prevent multiple readers/writers if desired. */ if ((nvram_open_cnt && (file->f_flags & O_EXCL)) || - (nvram_open_mode & NVRAM_EXCL) || - ((file->f_mode & FMODE_WRITE) && (nvram_open_mode & NVRAM_WRITE))) { + (nvram_open_mode & NVRAM_EXCL)) { + spin_unlock(&nvram_state_lock); + return -EBUSY; + } + + /* Prevent multiple writers if the set_checksum ioctl is implemented. */ + if ((arch_nvram_ops.set_checksum != NULL) && + (file->f_mode & FMODE_WRITE) && (nvram_open_mode & NVRAM_WRITE)) { spin_unlock(&nvram_state_lock); return -EBUSY; } diff --git a/include/linux/nvram.h b/include/linux/nvram.h index d1bdee50d6a8..b7bfaec60a43 100644 --- a/include/linux/nvram.h +++ b/include/linux/nvram.h @@ -18,6 +18,8 @@ struct nvram_ops { unsigned char (*read_byte)(int); void (*write_byte)(unsigned char, int); ssize_t (*get_size)(void); + long (*set_checksum)(void); + long (*initialize)(void); }; extern const struct nvram_ops arch_nvram_ops; -- 2.19.2