xref: /openbmc/linux/tools/testing/nvdimm/test/nfit.c (revision ecaa4a97b3908be0bf3ad12181ae8c44d1816d40)
16bc75619SDan Williams /*
26bc75619SDan Williams  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
36bc75619SDan Williams  *
46bc75619SDan Williams  * This program is free software; you can redistribute it and/or modify
56bc75619SDan Williams  * it under the terms of version 2 of the GNU General Public License as
66bc75619SDan Williams  * published by the Free Software Foundation.
76bc75619SDan Williams  *
86bc75619SDan Williams  * This program is distributed in the hope that it will be useful, but
96bc75619SDan Williams  * WITHOUT ANY WARRANTY; without even the implied warranty of
106bc75619SDan Williams  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116bc75619SDan Williams  * General Public License for more details.
126bc75619SDan Williams  */
136bc75619SDan Williams #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
146bc75619SDan Williams #include <linux/platform_device.h>
156bc75619SDan Williams #include <linux/dma-mapping.h>
16d8d378faSDan Williams #include <linux/workqueue.h>
176bc75619SDan Williams #include <linux/libnvdimm.h>
186bc75619SDan Williams #include <linux/vmalloc.h>
196bc75619SDan Williams #include <linux/device.h>
206bc75619SDan Williams #include <linux/module.h>
2120985164SVishal Verma #include <linux/mutex.h>
226bc75619SDan Williams #include <linux/ndctl.h>
236bc75619SDan Williams #include <linux/sizes.h>
2420985164SVishal Verma #include <linux/list.h>
256bc75619SDan Williams #include <linux/slab.h>
26a7de92daSDan Williams #include <nd-core.h>
270ead1118SDan Williams #include <intel.h>
286bc75619SDan Williams #include <nfit.h>
296bc75619SDan Williams #include <nd.h>
306bc75619SDan Williams #include "nfit_test.h"
310fb5c8dfSDan Williams #include "../watermark.h"
326bc75619SDan Williams 
335d8beee2SDan Williams #include <asm/mcsafe_test.h>
345d8beee2SDan Williams 
356bc75619SDan Williams /*
366bc75619SDan Williams  * Generate an NFIT table to describe the following topology:
376bc75619SDan Williams  *
386bc75619SDan Williams  * BUS0: Interleaved PMEM regions, and aliasing with BLK regions
396bc75619SDan Williams  *
406bc75619SDan Williams  *                     (a)                       (b)            DIMM   BLK-REGION
416bc75619SDan Williams  *           +----------+--------------+----------+---------+
426bc75619SDan Williams  * +------+  |  blk2.0  |     pm0.0    |  blk2.1  |  pm1.0  |    0      region2
436bc75619SDan Williams  * | imc0 +--+- - - - - region0 - - - -+----------+         +
446bc75619SDan Williams  * +--+---+  |  blk3.0  |     pm0.0    |  blk3.1  |  pm1.0  |    1      region3
456bc75619SDan Williams  *    |      +----------+--------------v----------v         v
466bc75619SDan Williams  * +--+---+                            |                    |
476bc75619SDan Williams  * | cpu0 |                                    region1
486bc75619SDan Williams  * +--+---+                            |                    |
496bc75619SDan Williams  *    |      +-------------------------^----------^         ^
506bc75619SDan Williams  * +--+---+  |                 blk4.0             |  pm1.0  |    2      region4
516bc75619SDan Williams  * | imc1 +--+-------------------------+----------+         +
526bc75619SDan Williams  * +------+  |                 blk5.0             |  pm1.0  |    3      region5
536bc75619SDan Williams  *           +-------------------------+----------+-+-------+
546bc75619SDan Williams  *
5520985164SVishal Verma  * +--+---+
5620985164SVishal Verma  * | cpu1 |
5720985164SVishal Verma  * +--+---+                   (Hotplug DIMM)
5820985164SVishal Verma  *    |      +----------------------------------------------+
5920985164SVishal Verma  * +--+---+  |                 blk6.0/pm7.0                 |    4      region6/7
6020985164SVishal Verma  * | imc0 +--+----------------------------------------------+
6120985164SVishal Verma  * +------+
6220985164SVishal Verma  *
6320985164SVishal Verma  *
646bc75619SDan Williams  * *) In this layout we have four dimms and two memory controllers in one
656bc75619SDan Williams  *    socket.  Each unique interface (BLK or PMEM) to DPA space
666bc75619SDan Williams  *    is identified by a region device with a dynamically assigned id.
676bc75619SDan Williams  *
686bc75619SDan Williams  * *) The first portion of dimm0 and dimm1 are interleaved as REGION0.
696bc75619SDan Williams  *    A single PMEM namespace "pm0.0" is created using half of the
706bc75619SDan Williams  *    REGION0 SPA-range.  REGION0 spans dimm0 and dimm1.  PMEM namespace
716bc75619SDan Williams  *    allocate from from the bottom of a region.  The unallocated
726bc75619SDan Williams  *    portion of REGION0 aliases with REGION2 and REGION3.  That
736bc75619SDan Williams  *    unallacted capacity is reclaimed as BLK namespaces ("blk2.0" and
746bc75619SDan Williams  *    "blk3.0") starting at the base of each DIMM to offset (a) in those
756bc75619SDan Williams  *    DIMMs.  "pm0.0", "blk2.0" and "blk3.0" are free-form readable
766bc75619SDan Williams  *    names that can be assigned to a namespace.
776bc75619SDan Williams  *
786bc75619SDan Williams  * *) In the last portion of dimm0 and dimm1 we have an interleaved
796bc75619SDan Williams  *    SPA range, REGION1, that spans those two dimms as well as dimm2
806bc75619SDan Williams  *    and dimm3.  Some of REGION1 allocated to a PMEM namespace named
816bc75619SDan Williams  *    "pm1.0" the rest is reclaimed in 4 BLK namespaces (for each
826bc75619SDan Williams  *    dimm in the interleave set), "blk2.1", "blk3.1", "blk4.0", and
836bc75619SDan Williams  *    "blk5.0".
846bc75619SDan Williams  *
856bc75619SDan Williams  * *) The portion of dimm2 and dimm3 that do not participate in the
866bc75619SDan Williams  *    REGION1 interleaved SPA range (i.e. the DPA address below offset
876bc75619SDan Williams  *    (b) are also included in the "blk4.0" and "blk5.0" namespaces.
886bc75619SDan Williams  *    Note, that BLK namespaces need not be contiguous in DPA-space, and
896bc75619SDan Williams  *    can consume aliased capacity from multiple interleave sets.
906bc75619SDan Williams  *
916bc75619SDan Williams  * BUS1: Legacy NVDIMM (single contiguous range)
926bc75619SDan Williams  *
936bc75619SDan Williams  *  region2
946bc75619SDan Williams  * +---------------------+
956bc75619SDan Williams  * |---------------------|
966bc75619SDan Williams  * ||       pm2.0       ||
976bc75619SDan Williams  * |---------------------|
986bc75619SDan Williams  * +---------------------+
996bc75619SDan Williams  *
1006bc75619SDan Williams  * *) A NFIT-table may describe a simple system-physical-address range
1016bc75619SDan Williams  *    with no BLK aliasing.  This type of region may optionally
1026bc75619SDan Williams  *    reference an NVDIMM.
1036bc75619SDan Williams  */
1046bc75619SDan Williams enum {
10520985164SVishal Verma 	NUM_PM  = 3,
10620985164SVishal Verma 	NUM_DCR = 5,
10785d3fa02SDan Williams 	NUM_HINTS = 8,
1086bc75619SDan Williams 	NUM_BDW = NUM_DCR,
1096bc75619SDan Williams 	NUM_SPA = NUM_PM + NUM_DCR + NUM_BDW,
1109741a559SRoss Zwisler 	NUM_MEM = NUM_DCR + NUM_BDW + 2 /* spa0 iset */
1119741a559SRoss Zwisler 		+ 4 /* spa1 iset */ + 1 /* spa11 iset */,
1126bc75619SDan Williams 	DIMM_SIZE = SZ_32M,
1136bc75619SDan Williams 	LABEL_SIZE = SZ_128K,
1147bfe97c7SDan Williams 	SPA_VCD_SIZE = SZ_4M,
1156bc75619SDan Williams 	SPA0_SIZE = DIMM_SIZE,
1166bc75619SDan Williams 	SPA1_SIZE = DIMM_SIZE*2,
1176bc75619SDan Williams 	SPA2_SIZE = DIMM_SIZE,
1186bc75619SDan Williams 	BDW_SIZE = 64 << 8,
1196bc75619SDan Williams 	DCR_SIZE = 12,
1206bc75619SDan Williams 	NUM_NFITS = 2, /* permit testing multiple NFITs per system */
1216bc75619SDan Williams };
1226bc75619SDan Williams 
1236bc75619SDan Williams struct nfit_test_dcr {
1246bc75619SDan Williams 	__le64 bdw_addr;
1256bc75619SDan Williams 	__le32 bdw_status;
1266bc75619SDan Williams 	__u8 aperature[BDW_SIZE];
1276bc75619SDan Williams };
1286bc75619SDan Williams 
1296bc75619SDan Williams #define NFIT_DIMM_HANDLE(node, socket, imc, chan, dimm) \
1306bc75619SDan Williams 	(((node & 0xfff) << 16) | ((socket & 0xf) << 12) \
1316bc75619SDan Williams 	 | ((imc & 0xf) << 8) | ((chan & 0xf) << 4) | (dimm & 0xf))
1326bc75619SDan Williams 
133dafb1048SDan Williams static u32 handle[] = {
1346bc75619SDan Williams 	[0] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 0),
1356bc75619SDan Williams 	[1] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 1),
1366bc75619SDan Williams 	[2] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 0),
1376bc75619SDan Williams 	[3] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 1),
13820985164SVishal Verma 	[4] = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
139dafb1048SDan Williams 	[5] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 0),
140ac40b675SDan Williams 	[6] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 1),
1416bc75619SDan Williams };
1426bc75619SDan Williams 
143af31b04bSMasayoshi Mizuma static unsigned long dimm_fail_cmd_flags[ARRAY_SIZE(handle)];
144af31b04bSMasayoshi Mizuma static int dimm_fail_cmd_code[ARRAY_SIZE(handle)];
1453c13e2acSDave Jiang struct nfit_test_sec {
1463c13e2acSDave Jiang 	u8 state;
147*ecaa4a97SDave Jiang 	u8 ext_state;
1483c13e2acSDave Jiang 	u8 passphrase[32];
149*ecaa4a97SDave Jiang 	u8 master_passphrase[32];
150926f7480SDave Jiang 	u64 overwrite_end_time;
1513c13e2acSDave Jiang } dimm_sec_info[NUM_DCR];
15273606afdSDan Williams 
153b4d4702fSVishal Verma static const struct nd_intel_smart smart_def = {
154b4d4702fSVishal Verma 	.flags = ND_INTEL_SMART_HEALTH_VALID
155b4d4702fSVishal Verma 		| ND_INTEL_SMART_SPARES_VALID
156b4d4702fSVishal Verma 		| ND_INTEL_SMART_ALARM_VALID
157b4d4702fSVishal Verma 		| ND_INTEL_SMART_USED_VALID
158b4d4702fSVishal Verma 		| ND_INTEL_SMART_SHUTDOWN_VALID
159f1101766SDan Williams 		| ND_INTEL_SMART_SHUTDOWN_COUNT_VALID
160b4d4702fSVishal Verma 		| ND_INTEL_SMART_MTEMP_VALID
161b4d4702fSVishal Verma 		| ND_INTEL_SMART_CTEMP_VALID,
162b4d4702fSVishal Verma 	.health = ND_INTEL_SMART_NON_CRITICAL_HEALTH,
163b4d4702fSVishal Verma 	.media_temperature = 23 * 16,
164b4d4702fSVishal Verma 	.ctrl_temperature = 25 * 16,
165b4d4702fSVishal Verma 	.pmic_temperature = 40 * 16,
166b4d4702fSVishal Verma 	.spares = 75,
167b4d4702fSVishal Verma 	.alarm_flags = ND_INTEL_SMART_SPARE_TRIP
168b4d4702fSVishal Verma 		| ND_INTEL_SMART_TEMP_TRIP,
169b4d4702fSVishal Verma 	.ait_status = 1,
170b4d4702fSVishal Verma 	.life_used = 5,
171b4d4702fSVishal Verma 	.shutdown_state = 0,
172f1101766SDan Williams 	.shutdown_count = 42,
173b4d4702fSVishal Verma 	.vendor_size = 0,
174b4d4702fSVishal Verma };
175b4d4702fSVishal Verma 
176bfbaa952SDave Jiang struct nfit_test_fw {
177bfbaa952SDave Jiang 	enum intel_fw_update_state state;
178bfbaa952SDave Jiang 	u32 context;
179bfbaa952SDave Jiang 	u64 version;
180bfbaa952SDave Jiang 	u32 size_received;
181bfbaa952SDave Jiang 	u64 end_time;
182bfbaa952SDave Jiang };
183bfbaa952SDave Jiang 
1846bc75619SDan Williams struct nfit_test {
1856bc75619SDan Williams 	struct acpi_nfit_desc acpi_desc;
1866bc75619SDan Williams 	struct platform_device pdev;
1876bc75619SDan Williams 	struct list_head resources;
1886bc75619SDan Williams 	void *nfit_buf;
1896bc75619SDan Williams 	dma_addr_t nfit_dma;
1906bc75619SDan Williams 	size_t nfit_size;
1911526f9e2SRoss Zwisler 	size_t nfit_filled;
192dafb1048SDan Williams 	int dcr_idx;
1936bc75619SDan Williams 	int num_dcr;
1946bc75619SDan Williams 	int num_pm;
1956bc75619SDan Williams 	void **dimm;
1966bc75619SDan Williams 	dma_addr_t *dimm_dma;
1979d27a87eSDan Williams 	void **flush;
1989d27a87eSDan Williams 	dma_addr_t *flush_dma;
1996bc75619SDan Williams 	void **label;
2006bc75619SDan Williams 	dma_addr_t *label_dma;
2016bc75619SDan Williams 	void **spa_set;
2026bc75619SDan Williams 	dma_addr_t *spa_set_dma;
2036bc75619SDan Williams 	struct nfit_test_dcr **dcr;
2046bc75619SDan Williams 	dma_addr_t *dcr_dma;
2056bc75619SDan Williams 	int (*alloc)(struct nfit_test *t);
2066bc75619SDan Williams 	void (*setup)(struct nfit_test *t);
20720985164SVishal Verma 	int setup_hotplug;
208c14a868aSDan Williams 	union acpi_object **_fit;
209c14a868aSDan Williams 	dma_addr_t _fit_dma;
210f471f1a7SDan Williams 	struct ars_state {
211f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status;
212f471f1a7SDan Williams 		unsigned long deadline;
213f471f1a7SDan Williams 		spinlock_t lock;
214f471f1a7SDan Williams 	} ars_state;
215af31b04bSMasayoshi Mizuma 	struct device *dimm_dev[ARRAY_SIZE(handle)];
216ed07c433SDan Williams 	struct nd_intel_smart *smart;
217ed07c433SDan Williams 	struct nd_intel_smart_threshold *smart_threshold;
2189fb1a190SDave Jiang 	struct badrange badrange;
2199fb1a190SDave Jiang 	struct work_struct work;
220bfbaa952SDave Jiang 	struct nfit_test_fw *fw;
2216bc75619SDan Williams };
2226bc75619SDan Williams 
2239fb1a190SDave Jiang static struct workqueue_struct *nfit_wq;
2249fb1a190SDave Jiang 
2256bc75619SDan Williams static struct nfit_test *to_nfit_test(struct device *dev)
2266bc75619SDan Williams {
2276bc75619SDan Williams 	struct platform_device *pdev = to_platform_device(dev);
2286bc75619SDan Williams 
2296bc75619SDan Williams 	return container_of(pdev, struct nfit_test, pdev);
2306bc75619SDan Williams }
2316bc75619SDan Williams 
232bfbaa952SDave Jiang static int nd_intel_test_get_fw_info(struct nfit_test *t,
233bfbaa952SDave Jiang 		struct nd_intel_fw_info *nd_cmd, unsigned int buf_len,
234bfbaa952SDave Jiang 		int idx)
235bfbaa952SDave Jiang {
236bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
237bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
238bfbaa952SDave Jiang 
239bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p, buf_len: %u, idx: %d\n",
240bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
241bfbaa952SDave Jiang 
242bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
243bfbaa952SDave Jiang 		return -EINVAL;
244bfbaa952SDave Jiang 
245bfbaa952SDave Jiang 	nd_cmd->status = 0;
246bfbaa952SDave Jiang 	nd_cmd->storage_size = INTEL_FW_STORAGE_SIZE;
247bfbaa952SDave Jiang 	nd_cmd->max_send_len = INTEL_FW_MAX_SEND_LEN;
248bfbaa952SDave Jiang 	nd_cmd->query_interval = INTEL_FW_QUERY_INTERVAL;
249bfbaa952SDave Jiang 	nd_cmd->max_query_time = INTEL_FW_QUERY_MAX_TIME;
250bfbaa952SDave Jiang 	nd_cmd->update_cap = 0;
251bfbaa952SDave Jiang 	nd_cmd->fis_version = INTEL_FW_FIS_VERSION;
252bfbaa952SDave Jiang 	nd_cmd->run_version = 0;
253bfbaa952SDave Jiang 	nd_cmd->updated_version = fw->version;
254bfbaa952SDave Jiang 
255bfbaa952SDave Jiang 	return 0;
256bfbaa952SDave Jiang }
257bfbaa952SDave Jiang 
258bfbaa952SDave Jiang static int nd_intel_test_start_update(struct nfit_test *t,
259bfbaa952SDave Jiang 		struct nd_intel_fw_start *nd_cmd, unsigned int buf_len,
260bfbaa952SDave Jiang 		int idx)
261bfbaa952SDave Jiang {
262bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
263bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
264bfbaa952SDave Jiang 
265bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
266bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
267bfbaa952SDave Jiang 
268bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
269bfbaa952SDave Jiang 		return -EINVAL;
270bfbaa952SDave Jiang 
271bfbaa952SDave Jiang 	if (fw->state != FW_STATE_NEW) {
272bfbaa952SDave Jiang 		/* extended status, FW update in progress */
273bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
274bfbaa952SDave Jiang 		return 0;
275bfbaa952SDave Jiang 	}
276bfbaa952SDave Jiang 
277bfbaa952SDave Jiang 	fw->state = FW_STATE_IN_PROGRESS;
278bfbaa952SDave Jiang 	fw->context++;
279bfbaa952SDave Jiang 	fw->size_received = 0;
280bfbaa952SDave Jiang 	nd_cmd->status = 0;
281bfbaa952SDave Jiang 	nd_cmd->context = fw->context;
282bfbaa952SDave Jiang 
283bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context issued: %#x\n", __func__, nd_cmd->context);
284bfbaa952SDave Jiang 
285bfbaa952SDave Jiang 	return 0;
286bfbaa952SDave Jiang }
287bfbaa952SDave Jiang 
288bfbaa952SDave Jiang static int nd_intel_test_send_data(struct nfit_test *t,
289bfbaa952SDave Jiang 		struct nd_intel_fw_send_data *nd_cmd, unsigned int buf_len,
290bfbaa952SDave Jiang 		int idx)
291bfbaa952SDave Jiang {
292bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
293bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
294bfbaa952SDave Jiang 	u32 *status = (u32 *)&nd_cmd->data[nd_cmd->length];
295bfbaa952SDave Jiang 
296bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
297bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
298bfbaa952SDave Jiang 
299bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
300bfbaa952SDave Jiang 		return -EINVAL;
301bfbaa952SDave Jiang 
302bfbaa952SDave Jiang 
303bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->status: %#x\n", __func__, *status);
304bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[0]: %#x\n", __func__, nd_cmd->data[0]);
305bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[%u]: %#x\n", __func__, nd_cmd->length-1,
306bfbaa952SDave Jiang 			nd_cmd->data[nd_cmd->length-1]);
307bfbaa952SDave Jiang 
308bfbaa952SDave Jiang 	if (fw->state != FW_STATE_IN_PROGRESS) {
309bfbaa952SDave Jiang 		dev_dbg(dev, "%s: not in IN_PROGRESS state\n", __func__);
310bfbaa952SDave Jiang 		*status = 0x5;
311bfbaa952SDave Jiang 		return 0;
312bfbaa952SDave Jiang 	}
313bfbaa952SDave Jiang 
314bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
315bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
316bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
317bfbaa952SDave Jiang 		*status = 0x10007;
318bfbaa952SDave Jiang 		return 0;
319bfbaa952SDave Jiang 	}
320bfbaa952SDave Jiang 
321bfbaa952SDave Jiang 	/*
322bfbaa952SDave Jiang 	 * check offset + len > size of fw storage
323bfbaa952SDave Jiang 	 * check length is > max send length
324bfbaa952SDave Jiang 	 */
325bfbaa952SDave Jiang 	if (nd_cmd->offset + nd_cmd->length > INTEL_FW_STORAGE_SIZE ||
326bfbaa952SDave Jiang 			nd_cmd->length > INTEL_FW_MAX_SEND_LEN) {
327bfbaa952SDave Jiang 		*status = 0x3;
328bfbaa952SDave Jiang 		dev_dbg(dev, "%s: buffer boundary violation\n", __func__);
329bfbaa952SDave Jiang 		return 0;
330bfbaa952SDave Jiang 	}
331bfbaa952SDave Jiang 
332bfbaa952SDave Jiang 	fw->size_received += nd_cmd->length;
333bfbaa952SDave Jiang 	dev_dbg(dev, "%s: copying %u bytes, %u bytes so far\n",
334bfbaa952SDave Jiang 			__func__, nd_cmd->length, fw->size_received);
335bfbaa952SDave Jiang 	*status = 0;
336bfbaa952SDave Jiang 	return 0;
337bfbaa952SDave Jiang }
338bfbaa952SDave Jiang 
339bfbaa952SDave Jiang static int nd_intel_test_finish_fw(struct nfit_test *t,
340bfbaa952SDave Jiang 		struct nd_intel_fw_finish_update *nd_cmd,
341bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
342bfbaa952SDave Jiang {
343bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
344bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
345bfbaa952SDave Jiang 
346bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
347bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
348bfbaa952SDave Jiang 
349bfbaa952SDave Jiang 	if (fw->state == FW_STATE_UPDATED) {
350bfbaa952SDave Jiang 		/* update already done, need cold boot */
351bfbaa952SDave Jiang 		nd_cmd->status = 0x20007;
352bfbaa952SDave Jiang 		return 0;
353bfbaa952SDave Jiang 	}
354bfbaa952SDave Jiang 
355bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context: %#x  ctrl_flags: %#x\n",
356bfbaa952SDave Jiang 			__func__, nd_cmd->context, nd_cmd->ctrl_flags);
357bfbaa952SDave Jiang 
358bfbaa952SDave Jiang 	switch (nd_cmd->ctrl_flags) {
359bfbaa952SDave Jiang 	case 0: /* finish */
360bfbaa952SDave Jiang 		if (nd_cmd->context != fw->context) {
361bfbaa952SDave Jiang 			dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
362bfbaa952SDave Jiang 					__func__, nd_cmd->context,
363bfbaa952SDave Jiang 					fw->context);
364bfbaa952SDave Jiang 			nd_cmd->status = 0x10007;
365bfbaa952SDave Jiang 			return 0;
366bfbaa952SDave Jiang 		}
367bfbaa952SDave Jiang 		nd_cmd->status = 0;
368bfbaa952SDave Jiang 		fw->state = FW_STATE_VERIFY;
369bfbaa952SDave Jiang 		/* set 1 second of time for firmware "update" */
370bfbaa952SDave Jiang 		fw->end_time = jiffies + HZ;
371bfbaa952SDave Jiang 		break;
372bfbaa952SDave Jiang 
373bfbaa952SDave Jiang 	case 1: /* abort */
374bfbaa952SDave Jiang 		fw->size_received = 0;
375bfbaa952SDave Jiang 		/* successfully aborted status */
376bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
377bfbaa952SDave Jiang 		fw->state = FW_STATE_NEW;
378bfbaa952SDave Jiang 		dev_dbg(dev, "%s: abort successful\n", __func__);
379bfbaa952SDave Jiang 		break;
380bfbaa952SDave Jiang 
381bfbaa952SDave Jiang 	default: /* bad control flag */
382bfbaa952SDave Jiang 		dev_warn(dev, "%s: unknown control flag: %#x\n",
383bfbaa952SDave Jiang 				__func__, nd_cmd->ctrl_flags);
384bfbaa952SDave Jiang 		return -EINVAL;
385bfbaa952SDave Jiang 	}
386bfbaa952SDave Jiang 
387bfbaa952SDave Jiang 	return 0;
388bfbaa952SDave Jiang }
389bfbaa952SDave Jiang 
390bfbaa952SDave Jiang static int nd_intel_test_finish_query(struct nfit_test *t,
391bfbaa952SDave Jiang 		struct nd_intel_fw_finish_query *nd_cmd,
392bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
393bfbaa952SDave Jiang {
394bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
395bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
396bfbaa952SDave Jiang 
397bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
398bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
399bfbaa952SDave Jiang 
400bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
401bfbaa952SDave Jiang 		return -EINVAL;
402bfbaa952SDave Jiang 
403bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
404bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
405bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
406bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
407bfbaa952SDave Jiang 		return 0;
408bfbaa952SDave Jiang 	}
409bfbaa952SDave Jiang 
410bfbaa952SDave Jiang 	dev_dbg(dev, "%s context: %#x\n", __func__, nd_cmd->context);
411bfbaa952SDave Jiang 
412bfbaa952SDave Jiang 	switch (fw->state) {
413bfbaa952SDave Jiang 	case FW_STATE_NEW:
414bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
415bfbaa952SDave Jiang 		nd_cmd->status = 0;
416bfbaa952SDave Jiang 		dev_dbg(dev, "%s: new state\n", __func__);
417bfbaa952SDave Jiang 		break;
418bfbaa952SDave Jiang 
419bfbaa952SDave Jiang 	case FW_STATE_IN_PROGRESS:
420bfbaa952SDave Jiang 		/* sequencing error */
421bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
422bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
423bfbaa952SDave Jiang 		dev_dbg(dev, "%s: sequence error\n", __func__);
424bfbaa952SDave Jiang 		break;
425bfbaa952SDave Jiang 
426bfbaa952SDave Jiang 	case FW_STATE_VERIFY:
427bfbaa952SDave Jiang 		if (time_is_after_jiffies64(fw->end_time)) {
428bfbaa952SDave Jiang 			nd_cmd->updated_fw_rev = 0;
429bfbaa952SDave Jiang 			nd_cmd->status = 0x20007;
430bfbaa952SDave Jiang 			dev_dbg(dev, "%s: still verifying\n", __func__);
431bfbaa952SDave Jiang 			break;
432bfbaa952SDave Jiang 		}
433bfbaa952SDave Jiang 
434bfbaa952SDave Jiang 		dev_dbg(dev, "%s: transition out verify\n", __func__);
435bfbaa952SDave Jiang 		fw->state = FW_STATE_UPDATED;
436bfbaa952SDave Jiang 		/* we are going to fall through if it's "done" */
437bfbaa952SDave Jiang 	case FW_STATE_UPDATED:
438bfbaa952SDave Jiang 		nd_cmd->status = 0;
439bfbaa952SDave Jiang 		/* bogus test version */
440bfbaa952SDave Jiang 		fw->version = nd_cmd->updated_fw_rev =
441bfbaa952SDave Jiang 			INTEL_FW_FAKE_VERSION;
442bfbaa952SDave Jiang 		dev_dbg(dev, "%s: updated\n", __func__);
443bfbaa952SDave Jiang 		break;
444bfbaa952SDave Jiang 
445bfbaa952SDave Jiang 	default: /* we should never get here */
446bfbaa952SDave Jiang 		return -EINVAL;
447bfbaa952SDave Jiang 	}
448bfbaa952SDave Jiang 
449bfbaa952SDave Jiang 	return 0;
450bfbaa952SDave Jiang }
451bfbaa952SDave Jiang 
45239c686b8SVishal Verma static int nfit_test_cmd_get_config_size(struct nd_cmd_get_config_size *nd_cmd,
4536bc75619SDan Williams 		unsigned int buf_len)
4546bc75619SDan Williams {
4556bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4566bc75619SDan Williams 		return -EINVAL;
45739c686b8SVishal Verma 
4586bc75619SDan Williams 	nd_cmd->status = 0;
4596bc75619SDan Williams 	nd_cmd->config_size = LABEL_SIZE;
4606bc75619SDan Williams 	nd_cmd->max_xfer = SZ_4K;
46139c686b8SVishal Verma 
46239c686b8SVishal Verma 	return 0;
4636bc75619SDan Williams }
46439c686b8SVishal Verma 
46539c686b8SVishal Verma static int nfit_test_cmd_get_config_data(struct nd_cmd_get_config_data_hdr
46639c686b8SVishal Verma 		*nd_cmd, unsigned int buf_len, void *label)
46739c686b8SVishal Verma {
4686bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
46939c686b8SVishal Verma 	int rc;
4706bc75619SDan Williams 
4716bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4726bc75619SDan Williams 		return -EINVAL;
4736bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4746bc75619SDan Williams 		return -EINVAL;
4756bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) > buf_len)
4766bc75619SDan Williams 		return -EINVAL;
4776bc75619SDan Williams 
4786bc75619SDan Williams 	nd_cmd->status = 0;
4796bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
48039c686b8SVishal Verma 	memcpy(nd_cmd->out_buf, label + offset, len);
4816bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - len;
48239c686b8SVishal Verma 
48339c686b8SVishal Verma 	return rc;
4846bc75619SDan Williams }
48539c686b8SVishal Verma 
48639c686b8SVishal Verma static int nfit_test_cmd_set_config_data(struct nd_cmd_set_config_hdr *nd_cmd,
48739c686b8SVishal Verma 		unsigned int buf_len, void *label)
48839c686b8SVishal Verma {
4896bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
4906bc75619SDan Williams 	u32 *status;
49139c686b8SVishal Verma 	int rc;
4926bc75619SDan Williams 
4936bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4946bc75619SDan Williams 		return -EINVAL;
4956bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4966bc75619SDan Williams 		return -EINVAL;
4976bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) + 4 > buf_len)
4986bc75619SDan Williams 		return -EINVAL;
4996bc75619SDan Williams 
50039c686b8SVishal Verma 	status = (void *)nd_cmd + nd_cmd->in_length + sizeof(*nd_cmd);
5016bc75619SDan Williams 	*status = 0;
5026bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
50339c686b8SVishal Verma 	memcpy(label + offset, nd_cmd->in_buf, len);
5046bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - (len + 4);
50539c686b8SVishal Verma 
50639c686b8SVishal Verma 	return rc;
5076bc75619SDan Williams }
50839c686b8SVishal Verma 
509d4f32367SDan Williams #define NFIT_TEST_CLEAR_ERR_UNIT 256
510747ffe11SDan Williams 
51139c686b8SVishal Verma static int nfit_test_cmd_ars_cap(struct nd_cmd_ars_cap *nd_cmd,
51239c686b8SVishal Verma 		unsigned int buf_len)
51339c686b8SVishal Verma {
5149fb1a190SDave Jiang 	int ars_recs;
5159fb1a190SDave Jiang 
51639c686b8SVishal Verma 	if (buf_len < sizeof(*nd_cmd))
51739c686b8SVishal Verma 		return -EINVAL;
51839c686b8SVishal Verma 
5199fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
5209fb1a190SDave Jiang 	ars_recs = SZ_4K / sizeof(struct nd_ars_record);
5219fb1a190SDave Jiang 
522747ffe11SDan Williams 	nd_cmd->max_ars_out = sizeof(struct nd_cmd_ars_status)
5239fb1a190SDave Jiang 		+ ars_recs * sizeof(struct nd_ars_record);
52439c686b8SVishal Verma 	nd_cmd->status = (ND_ARS_PERSISTENT | ND_ARS_VOLATILE) << 16;
525d4f32367SDan Williams 	nd_cmd->clear_err_unit = NFIT_TEST_CLEAR_ERR_UNIT;
52639c686b8SVishal Verma 
52739c686b8SVishal Verma 	return 0;
52839c686b8SVishal Verma }
52939c686b8SVishal Verma 
5309fb1a190SDave Jiang static void post_ars_status(struct ars_state *ars_state,
5319fb1a190SDave Jiang 		struct badrange *badrange, u64 addr, u64 len)
53239c686b8SVishal Verma {
533f471f1a7SDan Williams 	struct nd_cmd_ars_status *ars_status;
534f471f1a7SDan Williams 	struct nd_ars_record *ars_record;
5359fb1a190SDave Jiang 	struct badrange_entry *be;
5369fb1a190SDave Jiang 	u64 end = addr + len - 1;
5379fb1a190SDave Jiang 	int i = 0;
538f471f1a7SDan Williams 
539f471f1a7SDan Williams 	ars_state->deadline = jiffies + 1*HZ;
540f471f1a7SDan Williams 	ars_status = ars_state->ars_status;
541f471f1a7SDan Williams 	ars_status->status = 0;
542f471f1a7SDan Williams 	ars_status->address = addr;
543f471f1a7SDan Williams 	ars_status->length = len;
544f471f1a7SDan Williams 	ars_status->type = ND_ARS_PERSISTENT;
5459fb1a190SDave Jiang 
5469fb1a190SDave Jiang 	spin_lock(&badrange->lock);
5479fb1a190SDave Jiang 	list_for_each_entry(be, &badrange->list, list) {
5489fb1a190SDave Jiang 		u64 be_end = be->start + be->length - 1;
5499fb1a190SDave Jiang 		u64 rstart, rend;
5509fb1a190SDave Jiang 
5519fb1a190SDave Jiang 		/* skip entries outside the range */
5529fb1a190SDave Jiang 		if (be_end < addr || be->start > end)
5539fb1a190SDave Jiang 			continue;
5549fb1a190SDave Jiang 
5559fb1a190SDave Jiang 		rstart = (be->start < addr) ? addr : be->start;
5569fb1a190SDave Jiang 		rend = (be_end < end) ? be_end : end;
5579fb1a190SDave Jiang 		ars_record = &ars_status->records[i];
558f471f1a7SDan Williams 		ars_record->handle = 0;
5599fb1a190SDave Jiang 		ars_record->err_address = rstart;
5609fb1a190SDave Jiang 		ars_record->length = rend - rstart + 1;
5619fb1a190SDave Jiang 		i++;
5629fb1a190SDave Jiang 	}
5639fb1a190SDave Jiang 	spin_unlock(&badrange->lock);
5649fb1a190SDave Jiang 	ars_status->num_records = i;
5659fb1a190SDave Jiang 	ars_status->out_length = sizeof(struct nd_cmd_ars_status)
5669fb1a190SDave Jiang 		+ i * sizeof(struct nd_ars_record);
567f471f1a7SDan Williams }
568f471f1a7SDan Williams 
5699fb1a190SDave Jiang static int nfit_test_cmd_ars_start(struct nfit_test *t,
5709fb1a190SDave Jiang 		struct ars_state *ars_state,
571f471f1a7SDan Williams 		struct nd_cmd_ars_start *ars_start, unsigned int buf_len,
572f471f1a7SDan Williams 		int *cmd_rc)
573f471f1a7SDan Williams {
574f471f1a7SDan Williams 	if (buf_len < sizeof(*ars_start))
57539c686b8SVishal Verma 		return -EINVAL;
57639c686b8SVishal Verma 
577f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
578f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
579f471f1a7SDan Williams 		ars_start->status = NFIT_ARS_START_BUSY;
580f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
581f471f1a7SDan Williams 	} else {
582f471f1a7SDan Williams 		ars_start->status = 0;
583f471f1a7SDan Williams 		ars_start->scrub_time = 1;
5849fb1a190SDave Jiang 		post_ars_status(ars_state, &t->badrange, ars_start->address,
585f471f1a7SDan Williams 				ars_start->length);
586f471f1a7SDan Williams 		*cmd_rc = 0;
587f471f1a7SDan Williams 	}
588f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
58939c686b8SVishal Verma 
59039c686b8SVishal Verma 	return 0;
59139c686b8SVishal Verma }
59239c686b8SVishal Verma 
593f471f1a7SDan Williams static int nfit_test_cmd_ars_status(struct ars_state *ars_state,
594f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status, unsigned int buf_len,
595f471f1a7SDan Williams 		int *cmd_rc)
59639c686b8SVishal Verma {
597f471f1a7SDan Williams 	if (buf_len < ars_state->ars_status->out_length)
59839c686b8SVishal Verma 		return -EINVAL;
59939c686b8SVishal Verma 
600f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
601f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
602f471f1a7SDan Williams 		memset(ars_status, 0, buf_len);
603f471f1a7SDan Williams 		ars_status->status = NFIT_ARS_STATUS_BUSY;
604f471f1a7SDan Williams 		ars_status->out_length = sizeof(*ars_status);
605f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
606f471f1a7SDan Williams 	} else {
607f471f1a7SDan Williams 		memcpy(ars_status, ars_state->ars_status,
608f471f1a7SDan Williams 				ars_state->ars_status->out_length);
609f471f1a7SDan Williams 		*cmd_rc = 0;
610f471f1a7SDan Williams 	}
611f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
61239c686b8SVishal Verma 	return 0;
61339c686b8SVishal Verma }
61439c686b8SVishal Verma 
6155e096ef3SVishal Verma static int nfit_test_cmd_clear_error(struct nfit_test *t,
6165e096ef3SVishal Verma 		struct nd_cmd_clear_error *clear_err,
617d4f32367SDan Williams 		unsigned int buf_len, int *cmd_rc)
618d4f32367SDan Williams {
619d4f32367SDan Williams 	const u64 mask = NFIT_TEST_CLEAR_ERR_UNIT - 1;
620d4f32367SDan Williams 	if (buf_len < sizeof(*clear_err))
621d4f32367SDan Williams 		return -EINVAL;
622d4f32367SDan Williams 
623d4f32367SDan Williams 	if ((clear_err->address & mask) || (clear_err->length & mask))
624d4f32367SDan Williams 		return -EINVAL;
625d4f32367SDan Williams 
6265e096ef3SVishal Verma 	badrange_forget(&t->badrange, clear_err->address, clear_err->length);
627d4f32367SDan Williams 	clear_err->status = 0;
628d4f32367SDan Williams 	clear_err->cleared = clear_err->length;
629d4f32367SDan Williams 	*cmd_rc = 0;
630d4f32367SDan Williams 	return 0;
631d4f32367SDan Williams }
632d4f32367SDan Williams 
63310246dc8SYasunori Goto struct region_search_spa {
63410246dc8SYasunori Goto 	u64 addr;
63510246dc8SYasunori Goto 	struct nd_region *region;
63610246dc8SYasunori Goto };
63710246dc8SYasunori Goto 
63810246dc8SYasunori Goto static int is_region_device(struct device *dev)
63910246dc8SYasunori Goto {
64010246dc8SYasunori Goto 	return !strncmp(dev->kobj.name, "region", 6);
64110246dc8SYasunori Goto }
64210246dc8SYasunori Goto 
64310246dc8SYasunori Goto static int nfit_test_search_region_spa(struct device *dev, void *data)
64410246dc8SYasunori Goto {
64510246dc8SYasunori Goto 	struct region_search_spa *ctx = data;
64610246dc8SYasunori Goto 	struct nd_region *nd_region;
64710246dc8SYasunori Goto 	resource_size_t ndr_end;
64810246dc8SYasunori Goto 
64910246dc8SYasunori Goto 	if (!is_region_device(dev))
65010246dc8SYasunori Goto 		return 0;
65110246dc8SYasunori Goto 
65210246dc8SYasunori Goto 	nd_region = to_nd_region(dev);
65310246dc8SYasunori Goto 	ndr_end = nd_region->ndr_start + nd_region->ndr_size;
65410246dc8SYasunori Goto 
65510246dc8SYasunori Goto 	if (ctx->addr >= nd_region->ndr_start && ctx->addr < ndr_end) {
65610246dc8SYasunori Goto 		ctx->region = nd_region;
65710246dc8SYasunori Goto 		return 1;
65810246dc8SYasunori Goto 	}
65910246dc8SYasunori Goto 
66010246dc8SYasunori Goto 	return 0;
66110246dc8SYasunori Goto }
66210246dc8SYasunori Goto 
66310246dc8SYasunori Goto static int nfit_test_search_spa(struct nvdimm_bus *bus,
66410246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa)
66510246dc8SYasunori Goto {
66610246dc8SYasunori Goto 	int ret;
66710246dc8SYasunori Goto 	struct nd_region *nd_region = NULL;
66810246dc8SYasunori Goto 	struct nvdimm *nvdimm = NULL;
66910246dc8SYasunori Goto 	struct nd_mapping *nd_mapping = NULL;
67010246dc8SYasunori Goto 	struct region_search_spa ctx = {
67110246dc8SYasunori Goto 		.addr = spa->spa,
67210246dc8SYasunori Goto 		.region = NULL,
67310246dc8SYasunori Goto 	};
67410246dc8SYasunori Goto 	u64 dpa;
67510246dc8SYasunori Goto 
67610246dc8SYasunori Goto 	ret = device_for_each_child(&bus->dev, &ctx,
67710246dc8SYasunori Goto 				nfit_test_search_region_spa);
67810246dc8SYasunori Goto 
67910246dc8SYasunori Goto 	if (!ret)
68010246dc8SYasunori Goto 		return -ENODEV;
68110246dc8SYasunori Goto 
68210246dc8SYasunori Goto 	nd_region = ctx.region;
68310246dc8SYasunori Goto 
68410246dc8SYasunori Goto 	dpa = ctx.addr - nd_region->ndr_start;
68510246dc8SYasunori Goto 
68610246dc8SYasunori Goto 	/*
68710246dc8SYasunori Goto 	 * last dimm is selected for test
68810246dc8SYasunori Goto 	 */
68910246dc8SYasunori Goto 	nd_mapping = &nd_region->mapping[nd_region->ndr_mappings - 1];
69010246dc8SYasunori Goto 	nvdimm = nd_mapping->nvdimm;
69110246dc8SYasunori Goto 
69210246dc8SYasunori Goto 	spa->devices[0].nfit_device_handle = handle[nvdimm->id];
69310246dc8SYasunori Goto 	spa->num_nvdimms = 1;
69410246dc8SYasunori Goto 	spa->devices[0].dpa = dpa;
69510246dc8SYasunori Goto 
69610246dc8SYasunori Goto 	return 0;
69710246dc8SYasunori Goto }
69810246dc8SYasunori Goto 
69910246dc8SYasunori Goto static int nfit_test_cmd_translate_spa(struct nvdimm_bus *bus,
70010246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa, unsigned int buf_len)
70110246dc8SYasunori Goto {
70210246dc8SYasunori Goto 	if (buf_len < spa->translate_length)
70310246dc8SYasunori Goto 		return -EINVAL;
70410246dc8SYasunori Goto 
70510246dc8SYasunori Goto 	if (nfit_test_search_spa(bus, spa) < 0 || !spa->num_nvdimms)
70610246dc8SYasunori Goto 		spa->status = 2;
70710246dc8SYasunori Goto 
70810246dc8SYasunori Goto 	return 0;
70910246dc8SYasunori Goto }
71010246dc8SYasunori Goto 
711ed07c433SDan Williams static int nfit_test_cmd_smart(struct nd_intel_smart *smart, unsigned int buf_len,
712ed07c433SDan Williams 		struct nd_intel_smart *smart_data)
713baa51277SDan Williams {
714baa51277SDan Williams 	if (buf_len < sizeof(*smart))
715baa51277SDan Williams 		return -EINVAL;
716ed07c433SDan Williams 	memcpy(smart, smart_data, sizeof(*smart));
717baa51277SDan Williams 	return 0;
718baa51277SDan Williams }
719baa51277SDan Williams 
720cdd77d3eSDan Williams static int nfit_test_cmd_smart_threshold(
721ed07c433SDan Williams 		struct nd_intel_smart_threshold *out,
722ed07c433SDan Williams 		unsigned int buf_len,
723ed07c433SDan Williams 		struct nd_intel_smart_threshold *smart_t)
724baa51277SDan Williams {
725baa51277SDan Williams 	if (buf_len < sizeof(*smart_t))
726baa51277SDan Williams 		return -EINVAL;
727ed07c433SDan Williams 	memcpy(out, smart_t, sizeof(*smart_t));
728ed07c433SDan Williams 	return 0;
729ed07c433SDan Williams }
730ed07c433SDan Williams 
731ed07c433SDan Williams static void smart_notify(struct device *bus_dev,
732ed07c433SDan Williams 		struct device *dimm_dev, struct nd_intel_smart *smart,
733ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh)
734ed07c433SDan Williams {
735ed07c433SDan Williams 	dev_dbg(dimm_dev, "%s: alarm: %#x spares: %d (%d) mtemp: %d (%d) ctemp: %d (%d)\n",
736ed07c433SDan Williams 			__func__, thresh->alarm_control, thresh->spares,
737ed07c433SDan Williams 			smart->spares, thresh->media_temperature,
738ed07c433SDan Williams 			smart->media_temperature, thresh->ctrl_temperature,
739ed07c433SDan Williams 			smart->ctrl_temperature);
740ed07c433SDan Williams 	if (((thresh->alarm_control & ND_INTEL_SMART_SPARE_TRIP)
741ed07c433SDan Williams 				&& smart->spares
742ed07c433SDan Williams 				<= thresh->spares)
743ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_TEMP_TRIP)
744ed07c433SDan Williams 				&& smart->media_temperature
745ed07c433SDan Williams 				>= thresh->media_temperature)
746ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_CTEMP_TRIP)
747ed07c433SDan Williams 				&& smart->ctrl_temperature
7484cf260fcSVishal Verma 				>= thresh->ctrl_temperature)
7494cf260fcSVishal Verma 			|| (smart->health != ND_INTEL_SMART_NON_CRITICAL_HEALTH)
7504cf260fcSVishal Verma 			|| (smart->shutdown_state != 0)) {
751ed07c433SDan Williams 		device_lock(bus_dev);
752ed07c433SDan Williams 		__acpi_nvdimm_notify(dimm_dev, 0x81);
753ed07c433SDan Williams 		device_unlock(bus_dev);
754ed07c433SDan Williams 	}
755ed07c433SDan Williams }
756ed07c433SDan Williams 
757ed07c433SDan Williams static int nfit_test_cmd_smart_set_threshold(
758ed07c433SDan Williams 		struct nd_intel_smart_set_threshold *in,
759ed07c433SDan Williams 		unsigned int buf_len,
760ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh,
761ed07c433SDan Williams 		struct nd_intel_smart *smart,
762ed07c433SDan Williams 		struct device *bus_dev, struct device *dimm_dev)
763ed07c433SDan Williams {
764ed07c433SDan Williams 	unsigned int size;
765ed07c433SDan Williams 
766ed07c433SDan Williams 	size = sizeof(*in) - 4;
767ed07c433SDan Williams 	if (buf_len < size)
768ed07c433SDan Williams 		return -EINVAL;
769ed07c433SDan Williams 	memcpy(thresh->data, in, size);
770ed07c433SDan Williams 	in->status = 0;
771ed07c433SDan Williams 	smart_notify(bus_dev, dimm_dev, smart, thresh);
772ed07c433SDan Williams 
773baa51277SDan Williams 	return 0;
774baa51277SDan Williams }
775baa51277SDan Williams 
7764cf260fcSVishal Verma static int nfit_test_cmd_smart_inject(
7774cf260fcSVishal Verma 		struct nd_intel_smart_inject *inj,
7784cf260fcSVishal Verma 		unsigned int buf_len,
7794cf260fcSVishal Verma 		struct nd_intel_smart_threshold *thresh,
7804cf260fcSVishal Verma 		struct nd_intel_smart *smart,
7814cf260fcSVishal Verma 		struct device *bus_dev, struct device *dimm_dev)
7824cf260fcSVishal Verma {
7834cf260fcSVishal Verma 	if (buf_len != sizeof(*inj))
7844cf260fcSVishal Verma 		return -EINVAL;
7854cf260fcSVishal Verma 
786b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_MTEMP) {
7874cf260fcSVishal Verma 		if (inj->mtemp_enable)
7884cf260fcSVishal Verma 			smart->media_temperature = inj->media_temperature;
789b4d4702fSVishal Verma 		else
790b4d4702fSVishal Verma 			smart->media_temperature = smart_def.media_temperature;
791b4d4702fSVishal Verma 	}
792b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_SPARE) {
7934cf260fcSVishal Verma 		if (inj->spare_enable)
7944cf260fcSVishal Verma 			smart->spares = inj->spares;
795b4d4702fSVishal Verma 		else
796b4d4702fSVishal Verma 			smart->spares = smart_def.spares;
797b4d4702fSVishal Verma 	}
798b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_FATAL) {
7994cf260fcSVishal Verma 		if (inj->fatal_enable)
8004cf260fcSVishal Verma 			smart->health = ND_INTEL_SMART_FATAL_HEALTH;
801b4d4702fSVishal Verma 		else
802b4d4702fSVishal Verma 			smart->health = ND_INTEL_SMART_NON_CRITICAL_HEALTH;
803b4d4702fSVishal Verma 	}
804b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_SHUTDOWN) {
8054cf260fcSVishal Verma 		if (inj->unsafe_shutdown_enable) {
8064cf260fcSVishal Verma 			smart->shutdown_state = 1;
8074cf260fcSVishal Verma 			smart->shutdown_count++;
808b4d4702fSVishal Verma 		} else
809b4d4702fSVishal Verma 			smart->shutdown_state = 0;
8104cf260fcSVishal Verma 	}
8114cf260fcSVishal Verma 	inj->status = 0;
8124cf260fcSVishal Verma 	smart_notify(bus_dev, dimm_dev, smart, thresh);
8134cf260fcSVishal Verma 
8144cf260fcSVishal Verma 	return 0;
8154cf260fcSVishal Verma }
8164cf260fcSVishal Verma 
8179fb1a190SDave Jiang static void uc_error_notify(struct work_struct *work)
8189fb1a190SDave Jiang {
8199fb1a190SDave Jiang 	struct nfit_test *t = container_of(work, typeof(*t), work);
8209fb1a190SDave Jiang 
8219fb1a190SDave Jiang 	__acpi_nfit_notify(&t->pdev.dev, t, NFIT_NOTIFY_UC_MEMORY_ERROR);
8229fb1a190SDave Jiang }
8239fb1a190SDave Jiang 
8249fb1a190SDave Jiang static int nfit_test_cmd_ars_error_inject(struct nfit_test *t,
8259fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj *err_inj, unsigned int buf_len)
8269fb1a190SDave Jiang {
8279fb1a190SDave Jiang 	int rc;
8289fb1a190SDave Jiang 
82941cb3301SVishal Verma 	if (buf_len != sizeof(*err_inj)) {
8309fb1a190SDave Jiang 		rc = -EINVAL;
8319fb1a190SDave Jiang 		goto err;
8329fb1a190SDave Jiang 	}
8339fb1a190SDave Jiang 
8349fb1a190SDave Jiang 	if (err_inj->err_inj_spa_range_length <= 0) {
8359fb1a190SDave Jiang 		rc = -EINVAL;
8369fb1a190SDave Jiang 		goto err;
8379fb1a190SDave Jiang 	}
8389fb1a190SDave Jiang 
8399fb1a190SDave Jiang 	rc =  badrange_add(&t->badrange, err_inj->err_inj_spa_range_base,
8409fb1a190SDave Jiang 			err_inj->err_inj_spa_range_length);
8419fb1a190SDave Jiang 	if (rc < 0)
8429fb1a190SDave Jiang 		goto err;
8439fb1a190SDave Jiang 
8449fb1a190SDave Jiang 	if (err_inj->err_inj_options & (1 << ND_ARS_ERR_INJ_OPT_NOTIFY))
8459fb1a190SDave Jiang 		queue_work(nfit_wq, &t->work);
8469fb1a190SDave Jiang 
8479fb1a190SDave Jiang 	err_inj->status = 0;
8489fb1a190SDave Jiang 	return 0;
8499fb1a190SDave Jiang 
8509fb1a190SDave Jiang err:
8519fb1a190SDave Jiang 	err_inj->status = NFIT_ARS_INJECT_INVALID;
8529fb1a190SDave Jiang 	return rc;
8539fb1a190SDave Jiang }
8549fb1a190SDave Jiang 
8559fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_clear(struct nfit_test *t,
8569fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_clr *err_clr, unsigned int buf_len)
8579fb1a190SDave Jiang {
8589fb1a190SDave Jiang 	int rc;
8599fb1a190SDave Jiang 
86041cb3301SVishal Verma 	if (buf_len != sizeof(*err_clr)) {
8619fb1a190SDave Jiang 		rc = -EINVAL;
8629fb1a190SDave Jiang 		goto err;
8639fb1a190SDave Jiang 	}
8649fb1a190SDave Jiang 
8659fb1a190SDave Jiang 	if (err_clr->err_inj_clr_spa_range_length <= 0) {
8669fb1a190SDave Jiang 		rc = -EINVAL;
8679fb1a190SDave Jiang 		goto err;
8689fb1a190SDave Jiang 	}
8699fb1a190SDave Jiang 
8709fb1a190SDave Jiang 	badrange_forget(&t->badrange, err_clr->err_inj_clr_spa_range_base,
8719fb1a190SDave Jiang 			err_clr->err_inj_clr_spa_range_length);
8729fb1a190SDave Jiang 
8739fb1a190SDave Jiang 	err_clr->status = 0;
8749fb1a190SDave Jiang 	return 0;
8759fb1a190SDave Jiang 
8769fb1a190SDave Jiang err:
8779fb1a190SDave Jiang 	err_clr->status = NFIT_ARS_INJECT_INVALID;
8789fb1a190SDave Jiang 	return rc;
8799fb1a190SDave Jiang }
8809fb1a190SDave Jiang 
8819fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_status(struct nfit_test *t,
8829fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_stat *err_stat,
8839fb1a190SDave Jiang 		unsigned int buf_len)
8849fb1a190SDave Jiang {
8859fb1a190SDave Jiang 	struct badrange_entry *be;
8869fb1a190SDave Jiang 	int max = SZ_4K / sizeof(struct nd_error_stat_query_record);
8879fb1a190SDave Jiang 	int i = 0;
8889fb1a190SDave Jiang 
8899fb1a190SDave Jiang 	err_stat->status = 0;
8909fb1a190SDave Jiang 	spin_lock(&t->badrange.lock);
8919fb1a190SDave Jiang 	list_for_each_entry(be, &t->badrange.list, list) {
8929fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_base = be->start;
8939fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_length = be->length;
8949fb1a190SDave Jiang 		i++;
8959fb1a190SDave Jiang 		if (i > max)
8969fb1a190SDave Jiang 			break;
8979fb1a190SDave Jiang 	}
8989fb1a190SDave Jiang 	spin_unlock(&t->badrange.lock);
8999fb1a190SDave Jiang 	err_stat->inj_err_rec_count = i;
9009fb1a190SDave Jiang 
9019fb1a190SDave Jiang 	return 0;
9029fb1a190SDave Jiang }
9039fb1a190SDave Jiang 
904674d8bdeSDave Jiang static int nd_intel_test_cmd_set_lss_status(struct nfit_test *t,
905674d8bdeSDave Jiang 		struct nd_intel_lss *nd_cmd, unsigned int buf_len)
906674d8bdeSDave Jiang {
907674d8bdeSDave Jiang 	struct device *dev = &t->pdev.dev;
908674d8bdeSDave Jiang 
909674d8bdeSDave Jiang 	if (buf_len < sizeof(*nd_cmd))
910674d8bdeSDave Jiang 		return -EINVAL;
911674d8bdeSDave Jiang 
912674d8bdeSDave Jiang 	switch (nd_cmd->enable) {
913674d8bdeSDave Jiang 	case 0:
914674d8bdeSDave Jiang 		nd_cmd->status = 0;
915674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status disabled\n",
916674d8bdeSDave Jiang 				__func__);
917674d8bdeSDave Jiang 		break;
918674d8bdeSDave Jiang 	case 1:
919674d8bdeSDave Jiang 		nd_cmd->status = 0;
920674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status enabled\n",
921674d8bdeSDave Jiang 				__func__);
922674d8bdeSDave Jiang 		break;
923674d8bdeSDave Jiang 	default:
924674d8bdeSDave Jiang 		dev_warn(dev, "Unknown enable value: %#x\n", nd_cmd->enable);
925674d8bdeSDave Jiang 		nd_cmd->status = 0x3;
926674d8bdeSDave Jiang 		break;
927674d8bdeSDave Jiang 	}
928674d8bdeSDave Jiang 
929674d8bdeSDave Jiang 
930674d8bdeSDave Jiang 	return 0;
931674d8bdeSDave Jiang }
932674d8bdeSDave Jiang 
93339611e83SDan Williams static int override_return_code(int dimm, unsigned int func, int rc)
93439611e83SDan Williams {
93539611e83SDan Williams 	if ((1 << func) & dimm_fail_cmd_flags[dimm]) {
93639611e83SDan Williams 		if (dimm_fail_cmd_code[dimm])
93739611e83SDan Williams 			return dimm_fail_cmd_code[dimm];
93839611e83SDan Williams 		return -EIO;
93939611e83SDan Williams 	}
94039611e83SDan Williams 	return rc;
94139611e83SDan Williams }
94239611e83SDan Williams 
9433c13e2acSDave Jiang static int nd_intel_test_cmd_security_status(struct nfit_test *t,
9443c13e2acSDave Jiang 		struct nd_intel_get_security_state *nd_cmd,
9453c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
9463c13e2acSDave Jiang {
9473c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
9483c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
9493c13e2acSDave Jiang 
9503c13e2acSDave Jiang 	nd_cmd->status = 0;
9513c13e2acSDave Jiang 	nd_cmd->state = sec->state;
952*ecaa4a97SDave Jiang 	nd_cmd->extended_state = sec->ext_state;
9533c13e2acSDave Jiang 	dev_dbg(dev, "security state (%#x) returned\n", nd_cmd->state);
9543c13e2acSDave Jiang 
9553c13e2acSDave Jiang 	return 0;
9563c13e2acSDave Jiang }
9573c13e2acSDave Jiang 
9583c13e2acSDave Jiang static int nd_intel_test_cmd_unlock_unit(struct nfit_test *t,
9593c13e2acSDave Jiang 		struct nd_intel_unlock_unit *nd_cmd,
9603c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
9613c13e2acSDave Jiang {
9623c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
9633c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
9643c13e2acSDave Jiang 
9653c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_LOCKED) ||
9663c13e2acSDave Jiang 			(sec->state & ND_INTEL_SEC_STATE_FROZEN)) {
9673c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
9683c13e2acSDave Jiang 		dev_dbg(dev, "unlock unit: invalid state: %#x\n",
9693c13e2acSDave Jiang 				sec->state);
9703c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
9713c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
9723c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
9733c13e2acSDave Jiang 		dev_dbg(dev, "unlock unit: invalid passphrase\n");
9743c13e2acSDave Jiang 	} else {
9753c13e2acSDave Jiang 		nd_cmd->status = 0;
9763c13e2acSDave Jiang 		sec->state = ND_INTEL_SEC_STATE_ENABLED;
9773c13e2acSDave Jiang 		dev_dbg(dev, "Unit unlocked\n");
9783c13e2acSDave Jiang 	}
9793c13e2acSDave Jiang 
9803c13e2acSDave Jiang 	dev_dbg(dev, "unlocking status returned: %#x\n", nd_cmd->status);
9813c13e2acSDave Jiang 	return 0;
9823c13e2acSDave Jiang }
9833c13e2acSDave Jiang 
9843c13e2acSDave Jiang static int nd_intel_test_cmd_set_pass(struct nfit_test *t,
9853c13e2acSDave Jiang 		struct nd_intel_set_passphrase *nd_cmd,
9863c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
9873c13e2acSDave Jiang {
9883c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
9893c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
9903c13e2acSDave Jiang 
9913c13e2acSDave Jiang 	if (sec->state & ND_INTEL_SEC_STATE_FROZEN) {
9923c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
9933c13e2acSDave Jiang 		dev_dbg(dev, "set passphrase: wrong security state\n");
9943c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->old_pass, sec->passphrase,
9953c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
9963c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
9973c13e2acSDave Jiang 		dev_dbg(dev, "set passphrase: wrong passphrase\n");
9983c13e2acSDave Jiang 	} else {
9993c13e2acSDave Jiang 		memcpy(sec->passphrase, nd_cmd->new_pass,
10003c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE);
10013c13e2acSDave Jiang 		sec->state |= ND_INTEL_SEC_STATE_ENABLED;
10023c13e2acSDave Jiang 		nd_cmd->status = 0;
10033c13e2acSDave Jiang 		dev_dbg(dev, "passphrase updated\n");
10043c13e2acSDave Jiang 	}
10053c13e2acSDave Jiang 
10063c13e2acSDave Jiang 	return 0;
10073c13e2acSDave Jiang }
10083c13e2acSDave Jiang 
10093c13e2acSDave Jiang static int nd_intel_test_cmd_freeze_lock(struct nfit_test *t,
10103c13e2acSDave Jiang 		struct nd_intel_freeze_lock *nd_cmd,
10113c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
10123c13e2acSDave Jiang {
10133c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
10143c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
10153c13e2acSDave Jiang 
10163c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED)) {
10173c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
10183c13e2acSDave Jiang 		dev_dbg(dev, "freeze lock: wrong security state\n");
10193c13e2acSDave Jiang 	} else {
10203c13e2acSDave Jiang 		sec->state |= ND_INTEL_SEC_STATE_FROZEN;
10213c13e2acSDave Jiang 		nd_cmd->status = 0;
10223c13e2acSDave Jiang 		dev_dbg(dev, "security frozen\n");
10233c13e2acSDave Jiang 	}
10243c13e2acSDave Jiang 
10253c13e2acSDave Jiang 	return 0;
10263c13e2acSDave Jiang }
10273c13e2acSDave Jiang 
10283c13e2acSDave Jiang static int nd_intel_test_cmd_disable_pass(struct nfit_test *t,
10293c13e2acSDave Jiang 		struct nd_intel_disable_passphrase *nd_cmd,
10303c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
10313c13e2acSDave Jiang {
10323c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
10333c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
10343c13e2acSDave Jiang 
10353c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED) ||
10363c13e2acSDave Jiang 			(sec->state & ND_INTEL_SEC_STATE_FROZEN)) {
10373c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
10383c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: wrong security state\n");
10393c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
10403c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
10413c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
10423c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: wrong passphrase\n");
10433c13e2acSDave Jiang 	} else {
10443c13e2acSDave Jiang 		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
10453c13e2acSDave Jiang 		sec->state = 0;
10463c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: done\n");
10473c13e2acSDave Jiang 	}
10483c13e2acSDave Jiang 
10493c13e2acSDave Jiang 	return 0;
10503c13e2acSDave Jiang }
10513c13e2acSDave Jiang 
10523c13e2acSDave Jiang static int nd_intel_test_cmd_secure_erase(struct nfit_test *t,
10533c13e2acSDave Jiang 		struct nd_intel_secure_erase *nd_cmd,
10543c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
10553c13e2acSDave Jiang {
10563c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
10573c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
10583c13e2acSDave Jiang 
10593c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED) ||
10603c13e2acSDave Jiang 			(sec->state & ND_INTEL_SEC_STATE_FROZEN)) {
10613c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
10623c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: wrong security state\n");
10633c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
10643c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
10653c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
10663c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: wrong passphrase\n");
10673c13e2acSDave Jiang 	} else {
10683c13e2acSDave Jiang 		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
1069*ecaa4a97SDave Jiang 		memset(sec->master_passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
10703c13e2acSDave Jiang 		sec->state = 0;
1071*ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
10723c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: done\n");
10733c13e2acSDave Jiang 	}
10743c13e2acSDave Jiang 
10753c13e2acSDave Jiang 	return 0;
10763c13e2acSDave Jiang }
10773c13e2acSDave Jiang 
1078926f7480SDave Jiang static int nd_intel_test_cmd_overwrite(struct nfit_test *t,
1079926f7480SDave Jiang 		struct nd_intel_overwrite *nd_cmd,
1080926f7480SDave Jiang 		unsigned int buf_len, int dimm)
1081926f7480SDave Jiang {
1082926f7480SDave Jiang 	struct device *dev = &t->pdev.dev;
1083926f7480SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1084926f7480SDave Jiang 
1085926f7480SDave Jiang 	if ((sec->state & ND_INTEL_SEC_STATE_ENABLED) &&
1086926f7480SDave Jiang 			memcmp(nd_cmd->passphrase, sec->passphrase,
1087926f7480SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
1088926f7480SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
1089926f7480SDave Jiang 		dev_dbg(dev, "overwrite: wrong passphrase\n");
1090926f7480SDave Jiang 		return 0;
1091926f7480SDave Jiang 	}
1092926f7480SDave Jiang 
1093926f7480SDave Jiang 	memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
1094926f7480SDave Jiang 	sec->state = ND_INTEL_SEC_STATE_OVERWRITE;
1095926f7480SDave Jiang 	dev_dbg(dev, "overwrite progressing.\n");
1096926f7480SDave Jiang 	sec->overwrite_end_time = get_jiffies_64() + 5 * HZ;
1097926f7480SDave Jiang 
1098926f7480SDave Jiang 	return 0;
1099926f7480SDave Jiang }
1100926f7480SDave Jiang 
1101926f7480SDave Jiang static int nd_intel_test_cmd_query_overwrite(struct nfit_test *t,
1102926f7480SDave Jiang 		struct nd_intel_query_overwrite *nd_cmd,
1103926f7480SDave Jiang 		unsigned int buf_len, int dimm)
1104926f7480SDave Jiang {
1105926f7480SDave Jiang 	struct device *dev = &t->pdev.dev;
1106926f7480SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1107926f7480SDave Jiang 
1108926f7480SDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_OVERWRITE)) {
1109926f7480SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_OQUERY_SEQUENCE_ERR;
1110926f7480SDave Jiang 		return 0;
1111926f7480SDave Jiang 	}
1112926f7480SDave Jiang 
1113926f7480SDave Jiang 	if (time_is_before_jiffies64(sec->overwrite_end_time)) {
1114926f7480SDave Jiang 		sec->overwrite_end_time = 0;
1115926f7480SDave Jiang 		sec->state = 0;
1116*ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1117926f7480SDave Jiang 		dev_dbg(dev, "overwrite is complete\n");
1118926f7480SDave Jiang 	} else
1119926f7480SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_OQUERY_INPROGRESS;
1120926f7480SDave Jiang 	return 0;
1121926f7480SDave Jiang }
1122926f7480SDave Jiang 
1123*ecaa4a97SDave Jiang static int nd_intel_test_cmd_master_set_pass(struct nfit_test *t,
1124*ecaa4a97SDave Jiang 		struct nd_intel_set_master_passphrase *nd_cmd,
1125*ecaa4a97SDave Jiang 		unsigned int buf_len, int dimm)
1126*ecaa4a97SDave Jiang {
1127*ecaa4a97SDave Jiang 	struct device *dev = &t->pdev.dev;
1128*ecaa4a97SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1129*ecaa4a97SDave Jiang 
1130*ecaa4a97SDave Jiang 	if (!(sec->ext_state & ND_INTEL_SEC_ESTATE_ENABLED)) {
1131*ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_NOT_SUPPORTED;
1132*ecaa4a97SDave Jiang 		dev_dbg(dev, "master set passphrase: in wrong state\n");
1133*ecaa4a97SDave Jiang 	} else if (sec->ext_state & ND_INTEL_SEC_ESTATE_PLIMIT) {
1134*ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
1135*ecaa4a97SDave Jiang 		dev_dbg(dev, "master set passphrase: in wrong security state\n");
1136*ecaa4a97SDave Jiang 	} else if (memcmp(nd_cmd->old_pass, sec->master_passphrase,
1137*ecaa4a97SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
1138*ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
1139*ecaa4a97SDave Jiang 		dev_dbg(dev, "master set passphrase: wrong passphrase\n");
1140*ecaa4a97SDave Jiang 	} else {
1141*ecaa4a97SDave Jiang 		memcpy(sec->master_passphrase, nd_cmd->new_pass,
1142*ecaa4a97SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE);
1143*ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1144*ecaa4a97SDave Jiang 		dev_dbg(dev, "master passphrase: updated\n");
1145*ecaa4a97SDave Jiang 	}
1146*ecaa4a97SDave Jiang 
1147*ecaa4a97SDave Jiang 	return 0;
1148*ecaa4a97SDave Jiang }
1149*ecaa4a97SDave Jiang 
1150*ecaa4a97SDave Jiang static int nd_intel_test_cmd_master_secure_erase(struct nfit_test *t,
1151*ecaa4a97SDave Jiang 		struct nd_intel_master_secure_erase *nd_cmd,
1152*ecaa4a97SDave Jiang 		unsigned int buf_len, int dimm)
1153*ecaa4a97SDave Jiang {
1154*ecaa4a97SDave Jiang 	struct device *dev = &t->pdev.dev;
1155*ecaa4a97SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1156*ecaa4a97SDave Jiang 
1157*ecaa4a97SDave Jiang 	if (!(sec->ext_state & ND_INTEL_SEC_ESTATE_ENABLED)) {
1158*ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_NOT_SUPPORTED;
1159*ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: in wrong state\n");
1160*ecaa4a97SDave Jiang 	} else if (sec->ext_state & ND_INTEL_SEC_ESTATE_PLIMIT) {
1161*ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
1162*ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: in wrong security state\n");
1163*ecaa4a97SDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->master_passphrase,
1164*ecaa4a97SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
1165*ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
1166*ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: wrong passphrase\n");
1167*ecaa4a97SDave Jiang 	} else {
1168*ecaa4a97SDave Jiang 		/* we do not erase master state passphrase ever */
1169*ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1170*ecaa4a97SDave Jiang 		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
1171*ecaa4a97SDave Jiang 		sec->state = 0;
1172*ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: done\n");
1173*ecaa4a97SDave Jiang 	}
1174*ecaa4a97SDave Jiang 
1175*ecaa4a97SDave Jiang 	return 0;
1176*ecaa4a97SDave Jiang }
1177*ecaa4a97SDave Jiang 
1178*ecaa4a97SDave Jiang 
1179bfbaa952SDave Jiang static int get_dimm(struct nfit_mem *nfit_mem, unsigned int func)
1180bfbaa952SDave Jiang {
1181bfbaa952SDave Jiang 	int i;
1182bfbaa952SDave Jiang 
1183bfbaa952SDave Jiang 	/* lookup per-dimm data */
1184bfbaa952SDave Jiang 	for (i = 0; i < ARRAY_SIZE(handle); i++)
1185bfbaa952SDave Jiang 		if (__to_nfit_memdev(nfit_mem)->device_handle == handle[i])
1186bfbaa952SDave Jiang 			break;
1187bfbaa952SDave Jiang 	if (i >= ARRAY_SIZE(handle))
1188bfbaa952SDave Jiang 		return -ENXIO;
1189bfbaa952SDave Jiang 	return i;
1190bfbaa952SDave Jiang }
1191bfbaa952SDave Jiang 
119239c686b8SVishal Verma static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
119339c686b8SVishal Verma 		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
1194aef25338SDan Williams 		unsigned int buf_len, int *cmd_rc)
119539c686b8SVishal Verma {
119639c686b8SVishal Verma 	struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
119739c686b8SVishal Verma 	struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
11986634fb06SDan Williams 	unsigned int func = cmd;
1199f471f1a7SDan Williams 	int i, rc = 0, __cmd_rc;
1200f471f1a7SDan Williams 
1201f471f1a7SDan Williams 	if (!cmd_rc)
1202f471f1a7SDan Williams 		cmd_rc = &__cmd_rc;
1203f471f1a7SDan Williams 	*cmd_rc = 0;
120439c686b8SVishal Verma 
120539c686b8SVishal Verma 	if (nvdimm) {
120639c686b8SVishal Verma 		struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1207e3654ecaSDan Williams 		unsigned long cmd_mask = nvdimm_cmd_mask(nvdimm);
120839c686b8SVishal Verma 
12096634fb06SDan Williams 		if (!nfit_mem)
12106634fb06SDan Williams 			return -ENOTTY;
12116634fb06SDan Williams 
12126634fb06SDan Williams 		if (cmd == ND_CMD_CALL) {
12136634fb06SDan Williams 			struct nd_cmd_pkg *call_pkg = buf;
12146634fb06SDan Williams 
12156634fb06SDan Williams 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
12166634fb06SDan Williams 			buf = (void *) call_pkg->nd_payload;
12176634fb06SDan Williams 			func = call_pkg->nd_command;
12186634fb06SDan Williams 			if (call_pkg->nd_family != nfit_mem->family)
12196634fb06SDan Williams 				return -ENOTTY;
1220bfbaa952SDave Jiang 
1221bfbaa952SDave Jiang 			i = get_dimm(nfit_mem, func);
1222bfbaa952SDave Jiang 			if (i < 0)
1223bfbaa952SDave Jiang 				return i;
1224bfbaa952SDave Jiang 
1225bfbaa952SDave Jiang 			switch (func) {
12263c13e2acSDave Jiang 			case NVDIMM_INTEL_GET_SECURITY_STATE:
12273c13e2acSDave Jiang 				rc = nd_intel_test_cmd_security_status(t,
12283c13e2acSDave Jiang 						buf, buf_len, i);
12293c13e2acSDave Jiang 				break;
12303c13e2acSDave Jiang 			case NVDIMM_INTEL_UNLOCK_UNIT:
12313c13e2acSDave Jiang 				rc = nd_intel_test_cmd_unlock_unit(t,
12323c13e2acSDave Jiang 						buf, buf_len, i);
12333c13e2acSDave Jiang 				break;
12343c13e2acSDave Jiang 			case NVDIMM_INTEL_SET_PASSPHRASE:
12353c13e2acSDave Jiang 				rc = nd_intel_test_cmd_set_pass(t,
12363c13e2acSDave Jiang 						buf, buf_len, i);
12373c13e2acSDave Jiang 				break;
12383c13e2acSDave Jiang 			case NVDIMM_INTEL_DISABLE_PASSPHRASE:
12393c13e2acSDave Jiang 				rc = nd_intel_test_cmd_disable_pass(t,
12403c13e2acSDave Jiang 						buf, buf_len, i);
12413c13e2acSDave Jiang 				break;
12423c13e2acSDave Jiang 			case NVDIMM_INTEL_FREEZE_LOCK:
12433c13e2acSDave Jiang 				rc = nd_intel_test_cmd_freeze_lock(t,
12443c13e2acSDave Jiang 						buf, buf_len, i);
12453c13e2acSDave Jiang 				break;
12463c13e2acSDave Jiang 			case NVDIMM_INTEL_SECURE_ERASE:
12473c13e2acSDave Jiang 				rc = nd_intel_test_cmd_secure_erase(t,
12483c13e2acSDave Jiang 						buf, buf_len, i);
12493c13e2acSDave Jiang 				break;
1250926f7480SDave Jiang 			case NVDIMM_INTEL_OVERWRITE:
1251926f7480SDave Jiang 				rc = nd_intel_test_cmd_overwrite(t,
1252926f7480SDave Jiang 						buf, buf_len, i - t->dcr_idx);
1253926f7480SDave Jiang 				break;
1254926f7480SDave Jiang 			case NVDIMM_INTEL_QUERY_OVERWRITE:
1255926f7480SDave Jiang 				rc = nd_intel_test_cmd_query_overwrite(t,
1256926f7480SDave Jiang 						buf, buf_len, i - t->dcr_idx);
1257926f7480SDave Jiang 				break;
1258*ecaa4a97SDave Jiang 			case NVDIMM_INTEL_SET_MASTER_PASSPHRASE:
1259*ecaa4a97SDave Jiang 				rc = nd_intel_test_cmd_master_set_pass(t,
1260*ecaa4a97SDave Jiang 						buf, buf_len, i);
1261*ecaa4a97SDave Jiang 				break;
1262*ecaa4a97SDave Jiang 			case NVDIMM_INTEL_MASTER_SECURE_ERASE:
1263*ecaa4a97SDave Jiang 				rc = nd_intel_test_cmd_master_secure_erase(t,
1264*ecaa4a97SDave Jiang 						buf, buf_len, i);
1265*ecaa4a97SDave Jiang 				break;
1266674d8bdeSDave Jiang 			case ND_INTEL_ENABLE_LSS_STATUS:
126739611e83SDan Williams 				rc = nd_intel_test_cmd_set_lss_status(t,
1268674d8bdeSDave Jiang 						buf, buf_len);
126939611e83SDan Williams 				break;
1270bfbaa952SDave Jiang 			case ND_INTEL_FW_GET_INFO:
127139611e83SDan Williams 				rc = nd_intel_test_get_fw_info(t, buf,
1272bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
127339611e83SDan Williams 				break;
1274bfbaa952SDave Jiang 			case ND_INTEL_FW_START_UPDATE:
127539611e83SDan Williams 				rc = nd_intel_test_start_update(t, buf,
1276bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
127739611e83SDan Williams 				break;
1278bfbaa952SDave Jiang 			case ND_INTEL_FW_SEND_DATA:
127939611e83SDan Williams 				rc = nd_intel_test_send_data(t, buf,
1280bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
128139611e83SDan Williams 				break;
1282bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_UPDATE:
128339611e83SDan Williams 				rc = nd_intel_test_finish_fw(t, buf,
1284bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
128539611e83SDan Williams 				break;
1286bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_QUERY:
128739611e83SDan Williams 				rc = nd_intel_test_finish_query(t, buf,
1288bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
128939611e83SDan Williams 				break;
1290bfbaa952SDave Jiang 			case ND_INTEL_SMART:
129139611e83SDan Williams 				rc = nfit_test_cmd_smart(buf, buf_len,
1292bfbaa952SDave Jiang 						&t->smart[i - t->dcr_idx]);
129339611e83SDan Williams 				break;
1294bfbaa952SDave Jiang 			case ND_INTEL_SMART_THRESHOLD:
129539611e83SDan Williams 				rc = nfit_test_cmd_smart_threshold(buf,
1296bfbaa952SDave Jiang 						buf_len,
1297bfbaa952SDave Jiang 						&t->smart_threshold[i -
1298bfbaa952SDave Jiang 							t->dcr_idx]);
129939611e83SDan Williams 				break;
1300bfbaa952SDave Jiang 			case ND_INTEL_SMART_SET_THRESHOLD:
130139611e83SDan Williams 				rc = nfit_test_cmd_smart_set_threshold(buf,
1302bfbaa952SDave Jiang 						buf_len,
1303bfbaa952SDave Jiang 						&t->smart_threshold[i -
1304bfbaa952SDave Jiang 							t->dcr_idx],
1305bfbaa952SDave Jiang 						&t->smart[i - t->dcr_idx],
1306bfbaa952SDave Jiang 						&t->pdev.dev, t->dimm_dev[i]);
130739611e83SDan Williams 				break;
13084cf260fcSVishal Verma 			case ND_INTEL_SMART_INJECT:
130939611e83SDan Williams 				rc = nfit_test_cmd_smart_inject(buf,
13104cf260fcSVishal Verma 						buf_len,
13114cf260fcSVishal Verma 						&t->smart_threshold[i -
13124cf260fcSVishal Verma 							t->dcr_idx],
13134cf260fcSVishal Verma 						&t->smart[i - t->dcr_idx],
13144cf260fcSVishal Verma 						&t->pdev.dev, t->dimm_dev[i]);
131539611e83SDan Williams 				break;
1316bfbaa952SDave Jiang 			default:
1317bfbaa952SDave Jiang 				return -ENOTTY;
1318bfbaa952SDave Jiang 			}
131939611e83SDan Williams 			return override_return_code(i, func, rc);
13206634fb06SDan Williams 		}
13216634fb06SDan Williams 
13226634fb06SDan Williams 		if (!test_bit(cmd, &cmd_mask)
13236634fb06SDan Williams 				|| !test_bit(func, &nfit_mem->dsm_mask))
132439c686b8SVishal Verma 			return -ENOTTY;
132539c686b8SVishal Verma 
1326bfbaa952SDave Jiang 		i = get_dimm(nfit_mem, func);
1327bfbaa952SDave Jiang 		if (i < 0)
1328bfbaa952SDave Jiang 			return i;
132973606afdSDan Williams 
13306634fb06SDan Williams 		switch (func) {
133139c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_SIZE:
133239c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_size(buf, buf_len);
133339c686b8SVishal Verma 			break;
133439c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_DATA:
133539c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_data(buf, buf_len,
1336dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
133739c686b8SVishal Verma 			break;
133839c686b8SVishal Verma 		case ND_CMD_SET_CONFIG_DATA:
133939c686b8SVishal Verma 			rc = nfit_test_cmd_set_config_data(buf, buf_len,
1340dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
134139c686b8SVishal Verma 			break;
13426bc75619SDan Williams 		default:
13436bc75619SDan Williams 			return -ENOTTY;
13446bc75619SDan Williams 		}
134539611e83SDan Williams 		return override_return_code(i, func, rc);
134639c686b8SVishal Verma 	} else {
1347f471f1a7SDan Williams 		struct ars_state *ars_state = &t->ars_state;
134810246dc8SYasunori Goto 		struct nd_cmd_pkg *call_pkg = buf;
134910246dc8SYasunori Goto 
135010246dc8SYasunori Goto 		if (!nd_desc)
135110246dc8SYasunori Goto 			return -ENOTTY;
135210246dc8SYasunori Goto 
135310246dc8SYasunori Goto 		if (cmd == ND_CMD_CALL) {
135410246dc8SYasunori Goto 			func = call_pkg->nd_command;
135510246dc8SYasunori Goto 
135610246dc8SYasunori Goto 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
135710246dc8SYasunori Goto 			buf = (void *) call_pkg->nd_payload;
135810246dc8SYasunori Goto 
135910246dc8SYasunori Goto 			switch (func) {
136010246dc8SYasunori Goto 			case NFIT_CMD_TRANSLATE_SPA:
136110246dc8SYasunori Goto 				rc = nfit_test_cmd_translate_spa(
136210246dc8SYasunori Goto 					acpi_desc->nvdimm_bus, buf, buf_len);
136310246dc8SYasunori Goto 				return rc;
13649fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_SET:
13659fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_error_inject(t, buf,
13669fb1a190SDave Jiang 					buf_len);
13679fb1a190SDave Jiang 				return rc;
13689fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_CLEAR:
13699fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_clear(t, buf,
13709fb1a190SDave Jiang 					buf_len);
13719fb1a190SDave Jiang 				return rc;
13729fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_GET:
13739fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_status(t, buf,
13749fb1a190SDave Jiang 					buf_len);
13759fb1a190SDave Jiang 				return rc;
137610246dc8SYasunori Goto 			default:
137710246dc8SYasunori Goto 				return -ENOTTY;
137810246dc8SYasunori Goto 			}
137910246dc8SYasunori Goto 		}
1380f471f1a7SDan Williams 
1381e3654ecaSDan Williams 		if (!nd_desc || !test_bit(cmd, &nd_desc->cmd_mask))
138239c686b8SVishal Verma 			return -ENOTTY;
138339c686b8SVishal Verma 
13846634fb06SDan Williams 		switch (func) {
138539c686b8SVishal Verma 		case ND_CMD_ARS_CAP:
138639c686b8SVishal Verma 			rc = nfit_test_cmd_ars_cap(buf, buf_len);
138739c686b8SVishal Verma 			break;
138839c686b8SVishal Verma 		case ND_CMD_ARS_START:
13899fb1a190SDave Jiang 			rc = nfit_test_cmd_ars_start(t, ars_state, buf,
13909fb1a190SDave Jiang 					buf_len, cmd_rc);
139139c686b8SVishal Verma 			break;
139239c686b8SVishal Verma 		case ND_CMD_ARS_STATUS:
1393f471f1a7SDan Williams 			rc = nfit_test_cmd_ars_status(ars_state, buf, buf_len,
1394f471f1a7SDan Williams 					cmd_rc);
139539c686b8SVishal Verma 			break;
1396d4f32367SDan Williams 		case ND_CMD_CLEAR_ERROR:
13975e096ef3SVishal Verma 			rc = nfit_test_cmd_clear_error(t, buf, buf_len, cmd_rc);
1398d4f32367SDan Williams 			break;
139939c686b8SVishal Verma 		default:
140039c686b8SVishal Verma 			return -ENOTTY;
140139c686b8SVishal Verma 		}
140239c686b8SVishal Verma 	}
14036bc75619SDan Williams 
14046bc75619SDan Williams 	return rc;
14056bc75619SDan Williams }
14066bc75619SDan Williams 
14076bc75619SDan Williams static DEFINE_SPINLOCK(nfit_test_lock);
14086bc75619SDan Williams static struct nfit_test *instances[NUM_NFITS];
14096bc75619SDan Williams 
14106bc75619SDan Williams static void release_nfit_res(void *data)
14116bc75619SDan Williams {
14126bc75619SDan Williams 	struct nfit_test_resource *nfit_res = data;
14136bc75619SDan Williams 
14146bc75619SDan Williams 	spin_lock(&nfit_test_lock);
14156bc75619SDan Williams 	list_del(&nfit_res->list);
14166bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
14176bc75619SDan Williams 
14186bc75619SDan Williams 	vfree(nfit_res->buf);
14196bc75619SDan Williams 	kfree(nfit_res);
14206bc75619SDan Williams }
14216bc75619SDan Williams 
14226bc75619SDan Williams static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
14236bc75619SDan Williams 		void *buf)
14246bc75619SDan Williams {
14256bc75619SDan Williams 	struct device *dev = &t->pdev.dev;
14266bc75619SDan Williams 	struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
14276bc75619SDan Williams 			GFP_KERNEL);
14286bc75619SDan Williams 	int rc;
14296bc75619SDan Williams 
1430bd4cd745SDan Williams 	if (!buf || !nfit_res)
14316bc75619SDan Williams 		goto err;
14326bc75619SDan Williams 	rc = devm_add_action(dev, release_nfit_res, nfit_res);
14336bc75619SDan Williams 	if (rc)
14346bc75619SDan Williams 		goto err;
14356bc75619SDan Williams 	INIT_LIST_HEAD(&nfit_res->list);
14366bc75619SDan Williams 	memset(buf, 0, size);
14376bc75619SDan Williams 	nfit_res->dev = dev;
14386bc75619SDan Williams 	nfit_res->buf = buf;
1439bd4cd745SDan Williams 	nfit_res->res.start = *dma;
1440bd4cd745SDan Williams 	nfit_res->res.end = *dma + size - 1;
1441bd4cd745SDan Williams 	nfit_res->res.name = "NFIT";
1442bd4cd745SDan Williams 	spin_lock_init(&nfit_res->lock);
1443bd4cd745SDan Williams 	INIT_LIST_HEAD(&nfit_res->requests);
14446bc75619SDan Williams 	spin_lock(&nfit_test_lock);
14456bc75619SDan Williams 	list_add(&nfit_res->list, &t->resources);
14466bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
14476bc75619SDan Williams 
14486bc75619SDan Williams 	return nfit_res->buf;
14496bc75619SDan Williams  err:
1450ee8520feSDan Williams 	if (buf)
14516bc75619SDan Williams 		vfree(buf);
14526bc75619SDan Williams 	kfree(nfit_res);
14536bc75619SDan Williams 	return NULL;
14546bc75619SDan Williams }
14556bc75619SDan Williams 
14566bc75619SDan Williams static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
14576bc75619SDan Williams {
14586bc75619SDan Williams 	void *buf = vmalloc(size);
14596bc75619SDan Williams 
14606bc75619SDan Williams 	*dma = (unsigned long) buf;
14616bc75619SDan Williams 	return __test_alloc(t, size, dma, buf);
14626bc75619SDan Williams }
14636bc75619SDan Williams 
14646bc75619SDan Williams static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
14656bc75619SDan Williams {
14666bc75619SDan Williams 	int i;
14676bc75619SDan Williams 
14686bc75619SDan Williams 	for (i = 0; i < ARRAY_SIZE(instances); i++) {
14696bc75619SDan Williams 		struct nfit_test_resource *n, *nfit_res = NULL;
14706bc75619SDan Williams 		struct nfit_test *t = instances[i];
14716bc75619SDan Williams 
14726bc75619SDan Williams 		if (!t)
14736bc75619SDan Williams 			continue;
14746bc75619SDan Williams 		spin_lock(&nfit_test_lock);
14756bc75619SDan Williams 		list_for_each_entry(n, &t->resources, list) {
1476bd4cd745SDan Williams 			if (addr >= n->res.start && (addr < n->res.start
1477bd4cd745SDan Williams 						+ resource_size(&n->res))) {
14786bc75619SDan Williams 				nfit_res = n;
14796bc75619SDan Williams 				break;
14806bc75619SDan Williams 			} else if (addr >= (unsigned long) n->buf
14816bc75619SDan Williams 					&& (addr < (unsigned long) n->buf
1482bd4cd745SDan Williams 						+ resource_size(&n->res))) {
14836bc75619SDan Williams 				nfit_res = n;
14846bc75619SDan Williams 				break;
14856bc75619SDan Williams 			}
14866bc75619SDan Williams 		}
14876bc75619SDan Williams 		spin_unlock(&nfit_test_lock);
14886bc75619SDan Williams 		if (nfit_res)
14896bc75619SDan Williams 			return nfit_res;
14906bc75619SDan Williams 	}
14916bc75619SDan Williams 
14926bc75619SDan Williams 	return NULL;
14936bc75619SDan Williams }
14946bc75619SDan Williams 
1495f471f1a7SDan Williams static int ars_state_init(struct device *dev, struct ars_state *ars_state)
1496f471f1a7SDan Williams {
14979fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
1498f471f1a7SDan Williams 	ars_state->ars_status = devm_kzalloc(dev,
14999fb1a190SDave Jiang 			sizeof(struct nd_cmd_ars_status) + SZ_4K, GFP_KERNEL);
1500f471f1a7SDan Williams 	if (!ars_state->ars_status)
1501f471f1a7SDan Williams 		return -ENOMEM;
1502f471f1a7SDan Williams 	spin_lock_init(&ars_state->lock);
1503f471f1a7SDan Williams 	return 0;
1504f471f1a7SDan Williams }
1505f471f1a7SDan Williams 
1506231bf117SDan Williams static void put_dimms(void *data)
1507231bf117SDan Williams {
1508718fda67SDan Williams 	struct nfit_test *t = data;
1509231bf117SDan Williams 	int i;
1510231bf117SDan Williams 
1511718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++)
1512718fda67SDan Williams 		if (t->dimm_dev[i])
1513718fda67SDan Williams 			device_unregister(t->dimm_dev[i]);
1514231bf117SDan Williams }
1515231bf117SDan Williams 
1516231bf117SDan Williams static struct class *nfit_test_dimm;
1517231bf117SDan Williams 
151873606afdSDan Williams static int dimm_name_to_id(struct device *dev)
151973606afdSDan Williams {
152073606afdSDan Williams 	int dimm;
152173606afdSDan Williams 
1522718fda67SDan Williams 	if (sscanf(dev_name(dev), "test_dimm%d", &dimm) != 1)
152373606afdSDan Williams 		return -ENXIO;
152473606afdSDan Williams 	return dimm;
152573606afdSDan Williams }
152673606afdSDan Williams 
152773606afdSDan Williams static ssize_t handle_show(struct device *dev, struct device_attribute *attr,
152873606afdSDan Williams 		char *buf)
152973606afdSDan Williams {
153073606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
153173606afdSDan Williams 
153273606afdSDan Williams 	if (dimm < 0)
153373606afdSDan Williams 		return dimm;
153473606afdSDan Williams 
153519357a68SDan Williams 	return sprintf(buf, "%#x\n", handle[dimm]);
153673606afdSDan Williams }
153773606afdSDan Williams DEVICE_ATTR_RO(handle);
153873606afdSDan Williams 
153973606afdSDan Williams static ssize_t fail_cmd_show(struct device *dev, struct device_attribute *attr,
154073606afdSDan Williams 		char *buf)
154173606afdSDan Williams {
154273606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
154373606afdSDan Williams 
154473606afdSDan Williams 	if (dimm < 0)
154573606afdSDan Williams 		return dimm;
154673606afdSDan Williams 
154773606afdSDan Williams 	return sprintf(buf, "%#lx\n", dimm_fail_cmd_flags[dimm]);
154873606afdSDan Williams }
154973606afdSDan Williams 
155073606afdSDan Williams static ssize_t fail_cmd_store(struct device *dev, struct device_attribute *attr,
155173606afdSDan Williams 		const char *buf, size_t size)
155273606afdSDan Williams {
155373606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
155473606afdSDan Williams 	unsigned long val;
155573606afdSDan Williams 	ssize_t rc;
155673606afdSDan Williams 
155773606afdSDan Williams 	if (dimm < 0)
155873606afdSDan Williams 		return dimm;
155973606afdSDan Williams 
156073606afdSDan Williams 	rc = kstrtol(buf, 0, &val);
156173606afdSDan Williams 	if (rc)
156273606afdSDan Williams 		return rc;
156373606afdSDan Williams 
156473606afdSDan Williams 	dimm_fail_cmd_flags[dimm] = val;
156573606afdSDan Williams 	return size;
156673606afdSDan Williams }
156773606afdSDan Williams static DEVICE_ATTR_RW(fail_cmd);
156873606afdSDan Williams 
156955c72ab6SDan Williams static ssize_t fail_cmd_code_show(struct device *dev, struct device_attribute *attr,
157055c72ab6SDan Williams 		char *buf)
157155c72ab6SDan Williams {
157255c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
157355c72ab6SDan Williams 
157455c72ab6SDan Williams 	if (dimm < 0)
157555c72ab6SDan Williams 		return dimm;
157655c72ab6SDan Williams 
157755c72ab6SDan Williams 	return sprintf(buf, "%d\n", dimm_fail_cmd_code[dimm]);
157855c72ab6SDan Williams }
157955c72ab6SDan Williams 
158055c72ab6SDan Williams static ssize_t fail_cmd_code_store(struct device *dev, struct device_attribute *attr,
158155c72ab6SDan Williams 		const char *buf, size_t size)
158255c72ab6SDan Williams {
158355c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
158455c72ab6SDan Williams 	unsigned long val;
158555c72ab6SDan Williams 	ssize_t rc;
158655c72ab6SDan Williams 
158755c72ab6SDan Williams 	if (dimm < 0)
158855c72ab6SDan Williams 		return dimm;
158955c72ab6SDan Williams 
159055c72ab6SDan Williams 	rc = kstrtol(buf, 0, &val);
159155c72ab6SDan Williams 	if (rc)
159255c72ab6SDan Williams 		return rc;
159355c72ab6SDan Williams 
159455c72ab6SDan Williams 	dimm_fail_cmd_code[dimm] = val;
159555c72ab6SDan Williams 	return size;
159655c72ab6SDan Williams }
159755c72ab6SDan Williams static DEVICE_ATTR_RW(fail_cmd_code);
159855c72ab6SDan Williams 
15993c13e2acSDave Jiang static ssize_t lock_dimm_store(struct device *dev,
16003c13e2acSDave Jiang 		struct device_attribute *attr, const char *buf, size_t size)
16013c13e2acSDave Jiang {
16023c13e2acSDave Jiang 	int dimm = dimm_name_to_id(dev);
16033c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
16043c13e2acSDave Jiang 
16053c13e2acSDave Jiang 	sec->state = ND_INTEL_SEC_STATE_ENABLED | ND_INTEL_SEC_STATE_LOCKED;
16063c13e2acSDave Jiang 	return size;
16073c13e2acSDave Jiang }
16083c13e2acSDave Jiang static DEVICE_ATTR_WO(lock_dimm);
16093c13e2acSDave Jiang 
161073606afdSDan Williams static struct attribute *nfit_test_dimm_attributes[] = {
161173606afdSDan Williams 	&dev_attr_fail_cmd.attr,
161255c72ab6SDan Williams 	&dev_attr_fail_cmd_code.attr,
161373606afdSDan Williams 	&dev_attr_handle.attr,
16143c13e2acSDave Jiang 	&dev_attr_lock_dimm.attr,
161573606afdSDan Williams 	NULL,
161673606afdSDan Williams };
161773606afdSDan Williams 
161873606afdSDan Williams static struct attribute_group nfit_test_dimm_attribute_group = {
161973606afdSDan Williams 	.attrs = nfit_test_dimm_attributes,
162073606afdSDan Williams };
162173606afdSDan Williams 
162273606afdSDan Williams static const struct attribute_group *nfit_test_dimm_attribute_groups[] = {
162373606afdSDan Williams 	&nfit_test_dimm_attribute_group,
162473606afdSDan Williams 	NULL,
162573606afdSDan Williams };
162673606afdSDan Williams 
1627718fda67SDan Williams static int nfit_test_dimm_init(struct nfit_test *t)
1628718fda67SDan Williams {
1629718fda67SDan Williams 	int i;
1630718fda67SDan Williams 
1631718fda67SDan Williams 	if (devm_add_action_or_reset(&t->pdev.dev, put_dimms, t))
1632718fda67SDan Williams 		return -ENOMEM;
1633718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1634718fda67SDan Williams 		t->dimm_dev[i] = device_create_with_groups(nfit_test_dimm,
1635718fda67SDan Williams 				&t->pdev.dev, 0, NULL,
1636718fda67SDan Williams 				nfit_test_dimm_attribute_groups,
1637718fda67SDan Williams 				"test_dimm%d", i + t->dcr_idx);
1638718fda67SDan Williams 		if (!t->dimm_dev[i])
1639718fda67SDan Williams 			return -ENOMEM;
1640718fda67SDan Williams 	}
1641718fda67SDan Williams 	return 0;
1642718fda67SDan Williams }
1643718fda67SDan Williams 
1644*ecaa4a97SDave Jiang static void security_init(struct nfit_test *t)
1645*ecaa4a97SDave Jiang {
1646*ecaa4a97SDave Jiang 	int i;
1647*ecaa4a97SDave Jiang 
1648*ecaa4a97SDave Jiang 	for (i = 0; i < t->num_dcr; i++) {
1649*ecaa4a97SDave Jiang 		struct nfit_test_sec *sec = &dimm_sec_info[i];
1650*ecaa4a97SDave Jiang 
1651*ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1652*ecaa4a97SDave Jiang 	}
1653*ecaa4a97SDave Jiang }
1654*ecaa4a97SDave Jiang 
1655ed07c433SDan Williams static void smart_init(struct nfit_test *t)
1656ed07c433SDan Williams {
1657ed07c433SDan Williams 	int i;
1658ed07c433SDan Williams 	const struct nd_intel_smart_threshold smart_t_data = {
1659ed07c433SDan Williams 		.alarm_control = ND_INTEL_SMART_SPARE_TRIP
1660ed07c433SDan Williams 			| ND_INTEL_SMART_TEMP_TRIP,
1661ed07c433SDan Williams 		.media_temperature = 40 * 16,
1662ed07c433SDan Williams 		.ctrl_temperature = 30 * 16,
1663ed07c433SDan Williams 		.spares = 5,
1664ed07c433SDan Williams 	};
1665ed07c433SDan Williams 
1666ed07c433SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1667b4d4702fSVishal Verma 		memcpy(&t->smart[i], &smart_def, sizeof(smart_def));
1668ed07c433SDan Williams 		memcpy(&t->smart_threshold[i], &smart_t_data,
1669ed07c433SDan Williams 				sizeof(smart_t_data));
1670ed07c433SDan Williams 	}
1671ed07c433SDan Williams }
1672ed07c433SDan Williams 
16736bc75619SDan Williams static int nfit_test0_alloc(struct nfit_test *t)
16746bc75619SDan Williams {
16756b577c9dSLinda Knippers 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * NUM_SPA
16766bc75619SDan Williams 			+ sizeof(struct acpi_nfit_memory_map) * NUM_MEM
16776bc75619SDan Williams 			+ sizeof(struct acpi_nfit_control_region) * NUM_DCR
16783b87356fSDan Williams 			+ offsetof(struct acpi_nfit_control_region,
16793b87356fSDan Williams 					window_size) * NUM_DCR
16809d27a87eSDan Williams 			+ sizeof(struct acpi_nfit_data_region) * NUM_BDW
168185d3fa02SDan Williams 			+ (sizeof(struct acpi_nfit_flush_address)
1682f81e1d35SDave Jiang 					+ sizeof(u64) * NUM_HINTS) * NUM_DCR
1683f81e1d35SDave Jiang 			+ sizeof(struct acpi_nfit_capabilities);
16846bc75619SDan Williams 	int i;
16856bc75619SDan Williams 
16866bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
16876bc75619SDan Williams 	if (!t->nfit_buf)
16886bc75619SDan Williams 		return -ENOMEM;
16896bc75619SDan Williams 	t->nfit_size = nfit_size;
16906bc75619SDan Williams 
1691ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[0]);
16926bc75619SDan Williams 	if (!t->spa_set[0])
16936bc75619SDan Williams 		return -ENOMEM;
16946bc75619SDan Williams 
1695ee8520feSDan Williams 	t->spa_set[1] = test_alloc(t, SPA1_SIZE, &t->spa_set_dma[1]);
16966bc75619SDan Williams 	if (!t->spa_set[1])
16976bc75619SDan Williams 		return -ENOMEM;
16986bc75619SDan Williams 
1699ee8520feSDan Williams 	t->spa_set[2] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[2]);
170020985164SVishal Verma 	if (!t->spa_set[2])
170120985164SVishal Verma 		return -ENOMEM;
170220985164SVishal Verma 
1703dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
17046bc75619SDan Williams 		t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
17056bc75619SDan Williams 		if (!t->dimm[i])
17066bc75619SDan Williams 			return -ENOMEM;
17076bc75619SDan Williams 
17086bc75619SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
17096bc75619SDan Williams 		if (!t->label[i])
17106bc75619SDan Williams 			return -ENOMEM;
17116bc75619SDan Williams 		sprintf(t->label[i], "label%d", i);
17129d27a87eSDan Williams 
17139d15ce9cSDan Williams 		t->flush[i] = test_alloc(t, max(PAGE_SIZE,
17149d15ce9cSDan Williams 					sizeof(u64) * NUM_HINTS),
171585d3fa02SDan Williams 				&t->flush_dma[i]);
17169d27a87eSDan Williams 		if (!t->flush[i])
17179d27a87eSDan Williams 			return -ENOMEM;
17186bc75619SDan Williams 	}
17196bc75619SDan Williams 
1720dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
17216bc75619SDan Williams 		t->dcr[i] = test_alloc(t, LABEL_SIZE, &t->dcr_dma[i]);
17226bc75619SDan Williams 		if (!t->dcr[i])
17236bc75619SDan Williams 			return -ENOMEM;
17246bc75619SDan Williams 	}
17256bc75619SDan Williams 
1726c14a868aSDan Williams 	t->_fit = test_alloc(t, sizeof(union acpi_object **), &t->_fit_dma);
1727c14a868aSDan Williams 	if (!t->_fit)
1728c14a868aSDan Williams 		return -ENOMEM;
1729c14a868aSDan Williams 
1730718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1731231bf117SDan Williams 		return -ENOMEM;
1732ed07c433SDan Williams 	smart_init(t);
1733*ecaa4a97SDave Jiang 	security_init(t);
1734f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
17356bc75619SDan Williams }
17366bc75619SDan Williams 
17376bc75619SDan Williams static int nfit_test1_alloc(struct nfit_test *t)
17386bc75619SDan Williams {
17397bfe97c7SDan Williams 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * 2
1740ac40b675SDan Williams 		+ sizeof(struct acpi_nfit_memory_map) * 2
1741ac40b675SDan Williams 		+ offsetof(struct acpi_nfit_control_region, window_size) * 2;
1742dafb1048SDan Williams 	int i;
17436bc75619SDan Williams 
17446bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
17456bc75619SDan Williams 	if (!t->nfit_buf)
17466bc75619SDan Williams 		return -ENOMEM;
17476bc75619SDan Williams 	t->nfit_size = nfit_size;
17486bc75619SDan Williams 
1749ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA2_SIZE, &t->spa_set_dma[0]);
17506bc75619SDan Williams 	if (!t->spa_set[0])
17516bc75619SDan Williams 		return -ENOMEM;
17526bc75619SDan Williams 
1753dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1754dafb1048SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
1755dafb1048SDan Williams 		if (!t->label[i])
1756dafb1048SDan Williams 			return -ENOMEM;
1757dafb1048SDan Williams 		sprintf(t->label[i], "label%d", i);
1758dafb1048SDan Williams 	}
1759dafb1048SDan Williams 
17607bfe97c7SDan Williams 	t->spa_set[1] = test_alloc(t, SPA_VCD_SIZE, &t->spa_set_dma[1]);
17617bfe97c7SDan Williams 	if (!t->spa_set[1])
17627bfe97c7SDan Williams 		return -ENOMEM;
17637bfe97c7SDan Williams 
1764718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1765718fda67SDan Williams 		return -ENOMEM;
1766ed07c433SDan Williams 	smart_init(t);
1767f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
17686bc75619SDan Williams }
17696bc75619SDan Williams 
17705dc68e55SDan Williams static void dcr_common_init(struct acpi_nfit_control_region *dcr)
17715dc68e55SDan Williams {
17725dc68e55SDan Williams 	dcr->vendor_id = 0xabcd;
17735dc68e55SDan Williams 	dcr->device_id = 0;
17745dc68e55SDan Williams 	dcr->revision_id = 1;
17755dc68e55SDan Williams 	dcr->valid_fields = 1;
17765dc68e55SDan Williams 	dcr->manufacturing_location = 0xa;
17775dc68e55SDan Williams 	dcr->manufacturing_date = cpu_to_be16(2016);
17785dc68e55SDan Williams }
17795dc68e55SDan Williams 
17806bc75619SDan Williams static void nfit_test0_setup(struct nfit_test *t)
17816bc75619SDan Williams {
178285d3fa02SDan Williams 	const int flush_hint_size = sizeof(struct acpi_nfit_flush_address)
178385d3fa02SDan Williams 		+ (sizeof(u64) * NUM_HINTS);
17846bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
17856bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
17866bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
17876bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
17886bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
17896bc75619SDan Williams 	struct acpi_nfit_data_region *bdw;
17909d27a87eSDan Williams 	struct acpi_nfit_flush_address *flush;
1791f81e1d35SDave Jiang 	struct acpi_nfit_capabilities *pcap;
1792d7d8464dSRoss Zwisler 	unsigned int offset = 0, i;
17936bc75619SDan Williams 
17946bc75619SDan Williams 	/*
17956bc75619SDan Williams 	 * spa0 (interleave first half of dimm0 and dimm1, note storage
17966bc75619SDan Williams 	 * does not actually alias the related block-data-window
17976bc75619SDan Williams 	 * regions)
17986bc75619SDan Williams 	 */
17996b577c9dSLinda Knippers 	spa = nfit_buf;
18006bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18016bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18026bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
18036bc75619SDan Williams 	spa->range_index = 0+1;
18046bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
18056bc75619SDan Williams 	spa->length = SPA0_SIZE;
1806d7d8464dSRoss Zwisler 	offset += spa->header.length;
18076bc75619SDan Williams 
18086bc75619SDan Williams 	/*
18096bc75619SDan Williams 	 * spa1 (interleave last half of the 4 DIMMS, note storage
18106bc75619SDan Williams 	 * does not actually alias the related block-data-window
18116bc75619SDan Williams 	 * regions)
18126bc75619SDan Williams 	 */
1813d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18146bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18156bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18166bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
18176bc75619SDan Williams 	spa->range_index = 1+1;
18186bc75619SDan Williams 	spa->address = t->spa_set_dma[1];
18196bc75619SDan Williams 	spa->length = SPA1_SIZE;
1820d7d8464dSRoss Zwisler 	offset += spa->header.length;
18216bc75619SDan Williams 
18226bc75619SDan Williams 	/* spa2 (dcr0) dimm0 */
1823d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18246bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18256bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18266bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
18276bc75619SDan Williams 	spa->range_index = 2+1;
18286bc75619SDan Williams 	spa->address = t->dcr_dma[0];
18296bc75619SDan Williams 	spa->length = DCR_SIZE;
1830d7d8464dSRoss Zwisler 	offset += spa->header.length;
18316bc75619SDan Williams 
18326bc75619SDan Williams 	/* spa3 (dcr1) dimm1 */
1833d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18346bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18356bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18366bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
18376bc75619SDan Williams 	spa->range_index = 3+1;
18386bc75619SDan Williams 	spa->address = t->dcr_dma[1];
18396bc75619SDan Williams 	spa->length = DCR_SIZE;
1840d7d8464dSRoss Zwisler 	offset += spa->header.length;
18416bc75619SDan Williams 
18426bc75619SDan Williams 	/* spa4 (dcr2) dimm2 */
1843d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18446bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18456bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18466bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
18476bc75619SDan Williams 	spa->range_index = 4+1;
18486bc75619SDan Williams 	spa->address = t->dcr_dma[2];
18496bc75619SDan Williams 	spa->length = DCR_SIZE;
1850d7d8464dSRoss Zwisler 	offset += spa->header.length;
18516bc75619SDan Williams 
18526bc75619SDan Williams 	/* spa5 (dcr3) dimm3 */
1853d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18546bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18556bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18566bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
18576bc75619SDan Williams 	spa->range_index = 5+1;
18586bc75619SDan Williams 	spa->address = t->dcr_dma[3];
18596bc75619SDan Williams 	spa->length = DCR_SIZE;
1860d7d8464dSRoss Zwisler 	offset += spa->header.length;
18616bc75619SDan Williams 
18626bc75619SDan Williams 	/* spa6 (bdw for dcr0) dimm0 */
1863d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18646bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18656bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18666bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
18676bc75619SDan Williams 	spa->range_index = 6+1;
18686bc75619SDan Williams 	spa->address = t->dimm_dma[0];
18696bc75619SDan Williams 	spa->length = DIMM_SIZE;
1870d7d8464dSRoss Zwisler 	offset += spa->header.length;
18716bc75619SDan Williams 
18726bc75619SDan Williams 	/* spa7 (bdw for dcr1) dimm1 */
1873d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18746bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18756bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18766bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
18776bc75619SDan Williams 	spa->range_index = 7+1;
18786bc75619SDan Williams 	spa->address = t->dimm_dma[1];
18796bc75619SDan Williams 	spa->length = DIMM_SIZE;
1880d7d8464dSRoss Zwisler 	offset += spa->header.length;
18816bc75619SDan Williams 
18826bc75619SDan Williams 	/* spa8 (bdw for dcr2) dimm2 */
1883d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18846bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18856bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18866bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
18876bc75619SDan Williams 	spa->range_index = 8+1;
18886bc75619SDan Williams 	spa->address = t->dimm_dma[2];
18896bc75619SDan Williams 	spa->length = DIMM_SIZE;
1890d7d8464dSRoss Zwisler 	offset += spa->header.length;
18916bc75619SDan Williams 
18926bc75619SDan Williams 	/* spa9 (bdw for dcr3) dimm3 */
1893d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
18946bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
18956bc75619SDan Williams 	spa->header.length = sizeof(*spa);
18966bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
18976bc75619SDan Williams 	spa->range_index = 9+1;
18986bc75619SDan Williams 	spa->address = t->dimm_dma[3];
18996bc75619SDan Williams 	spa->length = DIMM_SIZE;
1900d7d8464dSRoss Zwisler 	offset += spa->header.length;
19016bc75619SDan Williams 
19026bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
19036bc75619SDan Williams 	memdev = nfit_buf + offset;
19046bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19056bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19066bc75619SDan Williams 	memdev->device_handle = handle[0];
19076bc75619SDan Williams 	memdev->physical_id = 0;
19086bc75619SDan Williams 	memdev->region_id = 0;
19096bc75619SDan Williams 	memdev->range_index = 0+1;
19103b87356fSDan Williams 	memdev->region_index = 4+1;
19116bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1912df06a2d5SDan Williams 	memdev->region_offset = 1;
19136bc75619SDan Williams 	memdev->address = 0;
19146bc75619SDan Williams 	memdev->interleave_index = 0;
19156bc75619SDan Williams 	memdev->interleave_ways = 2;
1916d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19176bc75619SDan Williams 
19186bc75619SDan Williams 	/* mem-region1 (spa0, dimm1) */
1919d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19206bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19216bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19226bc75619SDan Williams 	memdev->device_handle = handle[1];
19236bc75619SDan Williams 	memdev->physical_id = 1;
19246bc75619SDan Williams 	memdev->region_id = 0;
19256bc75619SDan Williams 	memdev->range_index = 0+1;
19263b87356fSDan Williams 	memdev->region_index = 5+1;
19276bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1928df06a2d5SDan Williams 	memdev->region_offset = (1 << 8);
19296bc75619SDan Williams 	memdev->address = 0;
19306bc75619SDan Williams 	memdev->interleave_index = 0;
19316bc75619SDan Williams 	memdev->interleave_ways = 2;
1932ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1933d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19346bc75619SDan Williams 
19356bc75619SDan Williams 	/* mem-region2 (spa1, dimm0) */
1936d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19376bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19386bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19396bc75619SDan Williams 	memdev->device_handle = handle[0];
19406bc75619SDan Williams 	memdev->physical_id = 0;
19416bc75619SDan Williams 	memdev->region_id = 1;
19426bc75619SDan Williams 	memdev->range_index = 1+1;
19433b87356fSDan Williams 	memdev->region_index = 4+1;
19446bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1945df06a2d5SDan Williams 	memdev->region_offset = (1 << 16);
19466bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
19476bc75619SDan Williams 	memdev->interleave_index = 0;
19486bc75619SDan Williams 	memdev->interleave_ways = 4;
1949ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1950d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19516bc75619SDan Williams 
19526bc75619SDan Williams 	/* mem-region3 (spa1, dimm1) */
1953d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19546bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19556bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19566bc75619SDan Williams 	memdev->device_handle = handle[1];
19576bc75619SDan Williams 	memdev->physical_id = 1;
19586bc75619SDan Williams 	memdev->region_id = 1;
19596bc75619SDan Williams 	memdev->range_index = 1+1;
19603b87356fSDan Williams 	memdev->region_index = 5+1;
19616bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1962df06a2d5SDan Williams 	memdev->region_offset = (1 << 24);
19636bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
19646bc75619SDan Williams 	memdev->interleave_index = 0;
19656bc75619SDan Williams 	memdev->interleave_ways = 4;
1966d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19676bc75619SDan Williams 
19686bc75619SDan Williams 	/* mem-region4 (spa1, dimm2) */
1969d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19706bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19716bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19726bc75619SDan Williams 	memdev->device_handle = handle[2];
19736bc75619SDan Williams 	memdev->physical_id = 2;
19746bc75619SDan Williams 	memdev->region_id = 0;
19756bc75619SDan Williams 	memdev->range_index = 1+1;
19763b87356fSDan Williams 	memdev->region_index = 6+1;
19776bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1978df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 32);
19796bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
19806bc75619SDan Williams 	memdev->interleave_index = 0;
19816bc75619SDan Williams 	memdev->interleave_ways = 4;
1982ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1983d7d8464dSRoss Zwisler 	offset += memdev->header.length;
19846bc75619SDan Williams 
19856bc75619SDan Williams 	/* mem-region5 (spa1, dimm3) */
1986d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
19876bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
19886bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
19896bc75619SDan Williams 	memdev->device_handle = handle[3];
19906bc75619SDan Williams 	memdev->physical_id = 3;
19916bc75619SDan Williams 	memdev->region_id = 0;
19926bc75619SDan Williams 	memdev->range_index = 1+1;
19933b87356fSDan Williams 	memdev->region_index = 7+1;
19946bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1995df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 40);
19966bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
19976bc75619SDan Williams 	memdev->interleave_index = 0;
19986bc75619SDan Williams 	memdev->interleave_ways = 4;
1999d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20006bc75619SDan Williams 
20016bc75619SDan Williams 	/* mem-region6 (spa/dcr0, dimm0) */
2002d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20036bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20046bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20056bc75619SDan Williams 	memdev->device_handle = handle[0];
20066bc75619SDan Williams 	memdev->physical_id = 0;
20076bc75619SDan Williams 	memdev->region_id = 0;
20086bc75619SDan Williams 	memdev->range_index = 2+1;
20096bc75619SDan Williams 	memdev->region_index = 0+1;
20106bc75619SDan Williams 	memdev->region_size = 0;
20116bc75619SDan Williams 	memdev->region_offset = 0;
20126bc75619SDan Williams 	memdev->address = 0;
20136bc75619SDan Williams 	memdev->interleave_index = 0;
20146bc75619SDan Williams 	memdev->interleave_ways = 1;
2015d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20166bc75619SDan Williams 
20176bc75619SDan Williams 	/* mem-region7 (spa/dcr1, dimm1) */
2018d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20196bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20206bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20216bc75619SDan Williams 	memdev->device_handle = handle[1];
20226bc75619SDan Williams 	memdev->physical_id = 1;
20236bc75619SDan Williams 	memdev->region_id = 0;
20246bc75619SDan Williams 	memdev->range_index = 3+1;
20256bc75619SDan Williams 	memdev->region_index = 1+1;
20266bc75619SDan Williams 	memdev->region_size = 0;
20276bc75619SDan Williams 	memdev->region_offset = 0;
20286bc75619SDan Williams 	memdev->address = 0;
20296bc75619SDan Williams 	memdev->interleave_index = 0;
20306bc75619SDan Williams 	memdev->interleave_ways = 1;
2031d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20326bc75619SDan Williams 
20336bc75619SDan Williams 	/* mem-region8 (spa/dcr2, dimm2) */
2034d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20356bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20366bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20376bc75619SDan Williams 	memdev->device_handle = handle[2];
20386bc75619SDan Williams 	memdev->physical_id = 2;
20396bc75619SDan Williams 	memdev->region_id = 0;
20406bc75619SDan Williams 	memdev->range_index = 4+1;
20416bc75619SDan Williams 	memdev->region_index = 2+1;
20426bc75619SDan Williams 	memdev->region_size = 0;
20436bc75619SDan Williams 	memdev->region_offset = 0;
20446bc75619SDan Williams 	memdev->address = 0;
20456bc75619SDan Williams 	memdev->interleave_index = 0;
20466bc75619SDan Williams 	memdev->interleave_ways = 1;
2047d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20486bc75619SDan Williams 
20496bc75619SDan Williams 	/* mem-region9 (spa/dcr3, dimm3) */
2050d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20516bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20526bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20536bc75619SDan Williams 	memdev->device_handle = handle[3];
20546bc75619SDan Williams 	memdev->physical_id = 3;
20556bc75619SDan Williams 	memdev->region_id = 0;
20566bc75619SDan Williams 	memdev->range_index = 5+1;
20576bc75619SDan Williams 	memdev->region_index = 3+1;
20586bc75619SDan Williams 	memdev->region_size = 0;
20596bc75619SDan Williams 	memdev->region_offset = 0;
20606bc75619SDan Williams 	memdev->address = 0;
20616bc75619SDan Williams 	memdev->interleave_index = 0;
20626bc75619SDan Williams 	memdev->interleave_ways = 1;
2063d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20646bc75619SDan Williams 
20656bc75619SDan Williams 	/* mem-region10 (spa/bdw0, dimm0) */
2066d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20676bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20686bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20696bc75619SDan Williams 	memdev->device_handle = handle[0];
20706bc75619SDan Williams 	memdev->physical_id = 0;
20716bc75619SDan Williams 	memdev->region_id = 0;
20726bc75619SDan Williams 	memdev->range_index = 6+1;
20736bc75619SDan Williams 	memdev->region_index = 0+1;
20746bc75619SDan Williams 	memdev->region_size = 0;
20756bc75619SDan Williams 	memdev->region_offset = 0;
20766bc75619SDan Williams 	memdev->address = 0;
20776bc75619SDan Williams 	memdev->interleave_index = 0;
20786bc75619SDan Williams 	memdev->interleave_ways = 1;
2079d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20806bc75619SDan Williams 
20816bc75619SDan Williams 	/* mem-region11 (spa/bdw1, dimm1) */
2082d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20836bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20846bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
20856bc75619SDan Williams 	memdev->device_handle = handle[1];
20866bc75619SDan Williams 	memdev->physical_id = 1;
20876bc75619SDan Williams 	memdev->region_id = 0;
20886bc75619SDan Williams 	memdev->range_index = 7+1;
20896bc75619SDan Williams 	memdev->region_index = 1+1;
20906bc75619SDan Williams 	memdev->region_size = 0;
20916bc75619SDan Williams 	memdev->region_offset = 0;
20926bc75619SDan Williams 	memdev->address = 0;
20936bc75619SDan Williams 	memdev->interleave_index = 0;
20946bc75619SDan Williams 	memdev->interleave_ways = 1;
2095d7d8464dSRoss Zwisler 	offset += memdev->header.length;
20966bc75619SDan Williams 
20976bc75619SDan Williams 	/* mem-region12 (spa/bdw2, dimm2) */
2098d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
20996bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21006bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21016bc75619SDan Williams 	memdev->device_handle = handle[2];
21026bc75619SDan Williams 	memdev->physical_id = 2;
21036bc75619SDan Williams 	memdev->region_id = 0;
21046bc75619SDan Williams 	memdev->range_index = 8+1;
21056bc75619SDan Williams 	memdev->region_index = 2+1;
21066bc75619SDan Williams 	memdev->region_size = 0;
21076bc75619SDan Williams 	memdev->region_offset = 0;
21086bc75619SDan Williams 	memdev->address = 0;
21096bc75619SDan Williams 	memdev->interleave_index = 0;
21106bc75619SDan Williams 	memdev->interleave_ways = 1;
2111d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21126bc75619SDan Williams 
21136bc75619SDan Williams 	/* mem-region13 (spa/dcr3, dimm3) */
2114d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21156bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21166bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21176bc75619SDan Williams 	memdev->device_handle = handle[3];
21186bc75619SDan Williams 	memdev->physical_id = 3;
21196bc75619SDan Williams 	memdev->region_id = 0;
21206bc75619SDan Williams 	memdev->range_index = 9+1;
21216bc75619SDan Williams 	memdev->region_index = 3+1;
21226bc75619SDan Williams 	memdev->region_size = 0;
21236bc75619SDan Williams 	memdev->region_offset = 0;
21246bc75619SDan Williams 	memdev->address = 0;
21256bc75619SDan Williams 	memdev->interleave_index = 0;
21266bc75619SDan Williams 	memdev->interleave_ways = 1;
2127ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2128d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21296bc75619SDan Williams 
21303b87356fSDan Williams 	/* dcr-descriptor0: blk */
21316bc75619SDan Williams 	dcr = nfit_buf + offset;
21326bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2133d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
21346bc75619SDan Williams 	dcr->region_index = 0+1;
21355dc68e55SDan Williams 	dcr_common_init(dcr);
21366bc75619SDan Williams 	dcr->serial_number = ~handle[0];
2137be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
21386bc75619SDan Williams 	dcr->windows = 1;
21396bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
21406bc75619SDan Williams 	dcr->command_offset = 0;
21416bc75619SDan Williams 	dcr->command_size = 8;
21426bc75619SDan Williams 	dcr->status_offset = 8;
21436bc75619SDan Williams 	dcr->status_size = 4;
2144d7d8464dSRoss Zwisler 	offset += dcr->header.length;
21456bc75619SDan Williams 
21463b87356fSDan Williams 	/* dcr-descriptor1: blk */
2147d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
21486bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2149d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
21506bc75619SDan Williams 	dcr->region_index = 1+1;
21515dc68e55SDan Williams 	dcr_common_init(dcr);
21526bc75619SDan Williams 	dcr->serial_number = ~handle[1];
2153be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
21546bc75619SDan Williams 	dcr->windows = 1;
21556bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
21566bc75619SDan Williams 	dcr->command_offset = 0;
21576bc75619SDan Williams 	dcr->command_size = 8;
21586bc75619SDan Williams 	dcr->status_offset = 8;
21596bc75619SDan Williams 	dcr->status_size = 4;
2160d7d8464dSRoss Zwisler 	offset += dcr->header.length;
21616bc75619SDan Williams 
21623b87356fSDan Williams 	/* dcr-descriptor2: blk */
2163d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
21646bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2165d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
21666bc75619SDan Williams 	dcr->region_index = 2+1;
21675dc68e55SDan Williams 	dcr_common_init(dcr);
21686bc75619SDan Williams 	dcr->serial_number = ~handle[2];
2169be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
21706bc75619SDan Williams 	dcr->windows = 1;
21716bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
21726bc75619SDan Williams 	dcr->command_offset = 0;
21736bc75619SDan Williams 	dcr->command_size = 8;
21746bc75619SDan Williams 	dcr->status_offset = 8;
21756bc75619SDan Williams 	dcr->status_size = 4;
2176d7d8464dSRoss Zwisler 	offset += dcr->header.length;
21776bc75619SDan Williams 
21783b87356fSDan Williams 	/* dcr-descriptor3: blk */
2179d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
21806bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2181d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
21826bc75619SDan Williams 	dcr->region_index = 3+1;
21835dc68e55SDan Williams 	dcr_common_init(dcr);
21846bc75619SDan Williams 	dcr->serial_number = ~handle[3];
2185be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
21866bc75619SDan Williams 	dcr->windows = 1;
21876bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
21886bc75619SDan Williams 	dcr->command_offset = 0;
21896bc75619SDan Williams 	dcr->command_size = 8;
21906bc75619SDan Williams 	dcr->status_offset = 8;
21916bc75619SDan Williams 	dcr->status_size = 4;
2192d7d8464dSRoss Zwisler 	offset += dcr->header.length;
21936bc75619SDan Williams 
21943b87356fSDan Williams 	/* dcr-descriptor0: pmem */
21953b87356fSDan Williams 	dcr = nfit_buf + offset;
21963b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
21973b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
21983b87356fSDan Williams 			window_size);
21993b87356fSDan Williams 	dcr->region_index = 4+1;
22005dc68e55SDan Williams 	dcr_common_init(dcr);
22013b87356fSDan Williams 	dcr->serial_number = ~handle[0];
22023b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
22033b87356fSDan Williams 	dcr->windows = 0;
2204d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22053b87356fSDan Williams 
22063b87356fSDan Williams 	/* dcr-descriptor1: pmem */
2207d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
22083b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22093b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22103b87356fSDan Williams 			window_size);
22113b87356fSDan Williams 	dcr->region_index = 5+1;
22125dc68e55SDan Williams 	dcr_common_init(dcr);
22133b87356fSDan Williams 	dcr->serial_number = ~handle[1];
22143b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
22153b87356fSDan Williams 	dcr->windows = 0;
2216d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22173b87356fSDan Williams 
22183b87356fSDan Williams 	/* dcr-descriptor2: pmem */
2219d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
22203b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22213b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22223b87356fSDan Williams 			window_size);
22233b87356fSDan Williams 	dcr->region_index = 6+1;
22245dc68e55SDan Williams 	dcr_common_init(dcr);
22253b87356fSDan Williams 	dcr->serial_number = ~handle[2];
22263b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
22273b87356fSDan Williams 	dcr->windows = 0;
2228d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22293b87356fSDan Williams 
22303b87356fSDan Williams 	/* dcr-descriptor3: pmem */
2231d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
22323b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22333b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22343b87356fSDan Williams 			window_size);
22353b87356fSDan Williams 	dcr->region_index = 7+1;
22365dc68e55SDan Williams 	dcr_common_init(dcr);
22373b87356fSDan Williams 	dcr->serial_number = ~handle[3];
22383b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
22393b87356fSDan Williams 	dcr->windows = 0;
2240d7d8464dSRoss Zwisler 	offset += dcr->header.length;
22413b87356fSDan Williams 
22426bc75619SDan Williams 	/* bdw0 (spa/dcr0, dimm0) */
22436bc75619SDan Williams 	bdw = nfit_buf + offset;
22446bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2245d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
22466bc75619SDan Williams 	bdw->region_index = 0+1;
22476bc75619SDan Williams 	bdw->windows = 1;
22486bc75619SDan Williams 	bdw->offset = 0;
22496bc75619SDan Williams 	bdw->size = BDW_SIZE;
22506bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
22516bc75619SDan Williams 	bdw->start_address = 0;
2252d7d8464dSRoss Zwisler 	offset += bdw->header.length;
22536bc75619SDan Williams 
22546bc75619SDan Williams 	/* bdw1 (spa/dcr1, dimm1) */
2255d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
22566bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2257d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
22586bc75619SDan Williams 	bdw->region_index = 1+1;
22596bc75619SDan Williams 	bdw->windows = 1;
22606bc75619SDan Williams 	bdw->offset = 0;
22616bc75619SDan Williams 	bdw->size = BDW_SIZE;
22626bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
22636bc75619SDan Williams 	bdw->start_address = 0;
2264d7d8464dSRoss Zwisler 	offset += bdw->header.length;
22656bc75619SDan Williams 
22666bc75619SDan Williams 	/* bdw2 (spa/dcr2, dimm2) */
2267d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
22686bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2269d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
22706bc75619SDan Williams 	bdw->region_index = 2+1;
22716bc75619SDan Williams 	bdw->windows = 1;
22726bc75619SDan Williams 	bdw->offset = 0;
22736bc75619SDan Williams 	bdw->size = BDW_SIZE;
22746bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
22756bc75619SDan Williams 	bdw->start_address = 0;
2276d7d8464dSRoss Zwisler 	offset += bdw->header.length;
22776bc75619SDan Williams 
22786bc75619SDan Williams 	/* bdw3 (spa/dcr3, dimm3) */
2279d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
22806bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2281d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
22826bc75619SDan Williams 	bdw->region_index = 3+1;
22836bc75619SDan Williams 	bdw->windows = 1;
22846bc75619SDan Williams 	bdw->offset = 0;
22856bc75619SDan Williams 	bdw->size = BDW_SIZE;
22866bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
22876bc75619SDan Williams 	bdw->start_address = 0;
2288d7d8464dSRoss Zwisler 	offset += bdw->header.length;
22896bc75619SDan Williams 
22909d27a87eSDan Williams 	/* flush0 (dimm0) */
22919d27a87eSDan Williams 	flush = nfit_buf + offset;
22929d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
229385d3fa02SDan Williams 	flush->header.length = flush_hint_size;
22949d27a87eSDan Williams 	flush->device_handle = handle[0];
229585d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
229685d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
229785d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[0] + i * sizeof(u64);
2298d7d8464dSRoss Zwisler 	offset += flush->header.length;
22999d27a87eSDan Williams 
23009d27a87eSDan Williams 	/* flush1 (dimm1) */
2301d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
23029d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
230385d3fa02SDan Williams 	flush->header.length = flush_hint_size;
23049d27a87eSDan Williams 	flush->device_handle = handle[1];
230585d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
230685d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
230785d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[1] + i * sizeof(u64);
2308d7d8464dSRoss Zwisler 	offset += flush->header.length;
23099d27a87eSDan Williams 
23109d27a87eSDan Williams 	/* flush2 (dimm2) */
2311d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
23129d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
231385d3fa02SDan Williams 	flush->header.length = flush_hint_size;
23149d27a87eSDan Williams 	flush->device_handle = handle[2];
231585d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
231685d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
231785d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[2] + i * sizeof(u64);
2318d7d8464dSRoss Zwisler 	offset += flush->header.length;
23199d27a87eSDan Williams 
23209d27a87eSDan Williams 	/* flush3 (dimm3) */
2321d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
23229d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
232385d3fa02SDan Williams 	flush->header.length = flush_hint_size;
23249d27a87eSDan Williams 	flush->device_handle = handle[3];
232585d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
232685d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
232785d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[3] + i * sizeof(u64);
2328d7d8464dSRoss Zwisler 	offset += flush->header.length;
23299d27a87eSDan Williams 
2330f81e1d35SDave Jiang 	/* platform capabilities */
2331d7d8464dSRoss Zwisler 	pcap = nfit_buf + offset;
2332f81e1d35SDave Jiang 	pcap->header.type = ACPI_NFIT_TYPE_CAPABILITIES;
2333f81e1d35SDave Jiang 	pcap->header.length = sizeof(*pcap);
2334f81e1d35SDave Jiang 	pcap->highest_capability = 1;
23351273c253SVishal Verma 	pcap->capabilities = ACPI_NFIT_CAPABILITY_MEM_FLUSH;
2336d7d8464dSRoss Zwisler 	offset += pcap->header.length;
2337f81e1d35SDave Jiang 
233820985164SVishal Verma 	if (t->setup_hotplug) {
23393b87356fSDan Williams 		/* dcr-descriptor4: blk */
234020985164SVishal Verma 		dcr = nfit_buf + offset;
234120985164SVishal Verma 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2342d7d8464dSRoss Zwisler 		dcr->header.length = sizeof(*dcr);
23433b87356fSDan Williams 		dcr->region_index = 8+1;
23445dc68e55SDan Williams 		dcr_common_init(dcr);
234520985164SVishal Verma 		dcr->serial_number = ~handle[4];
2346be26f9aeSDan Williams 		dcr->code = NFIT_FIC_BLK;
234720985164SVishal Verma 		dcr->windows = 1;
234820985164SVishal Verma 		dcr->window_size = DCR_SIZE;
234920985164SVishal Verma 		dcr->command_offset = 0;
235020985164SVishal Verma 		dcr->command_size = 8;
235120985164SVishal Verma 		dcr->status_offset = 8;
235220985164SVishal Verma 		dcr->status_size = 4;
2353d7d8464dSRoss Zwisler 		offset += dcr->header.length;
235420985164SVishal Verma 
23553b87356fSDan Williams 		/* dcr-descriptor4: pmem */
23563b87356fSDan Williams 		dcr = nfit_buf + offset;
23573b87356fSDan Williams 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
23583b87356fSDan Williams 		dcr->header.length = offsetof(struct acpi_nfit_control_region,
23593b87356fSDan Williams 				window_size);
23603b87356fSDan Williams 		dcr->region_index = 9+1;
23615dc68e55SDan Williams 		dcr_common_init(dcr);
23623b87356fSDan Williams 		dcr->serial_number = ~handle[4];
23633b87356fSDan Williams 		dcr->code = NFIT_FIC_BYTEN;
23643b87356fSDan Williams 		dcr->windows = 0;
2365d7d8464dSRoss Zwisler 		offset += dcr->header.length;
23663b87356fSDan Williams 
236720985164SVishal Verma 		/* bdw4 (spa/dcr4, dimm4) */
236820985164SVishal Verma 		bdw = nfit_buf + offset;
236920985164SVishal Verma 		bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2370d7d8464dSRoss Zwisler 		bdw->header.length = sizeof(*bdw);
23713b87356fSDan Williams 		bdw->region_index = 8+1;
237220985164SVishal Verma 		bdw->windows = 1;
237320985164SVishal Verma 		bdw->offset = 0;
237420985164SVishal Verma 		bdw->size = BDW_SIZE;
237520985164SVishal Verma 		bdw->capacity = DIMM_SIZE;
237620985164SVishal Verma 		bdw->start_address = 0;
2377d7d8464dSRoss Zwisler 		offset += bdw->header.length;
237820985164SVishal Verma 
237920985164SVishal Verma 		/* spa10 (dcr4) dimm4 */
238020985164SVishal Verma 		spa = nfit_buf + offset;
238120985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
238220985164SVishal Verma 		spa->header.length = sizeof(*spa);
238320985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
238420985164SVishal Verma 		spa->range_index = 10+1;
238520985164SVishal Verma 		spa->address = t->dcr_dma[4];
238620985164SVishal Verma 		spa->length = DCR_SIZE;
2387d7d8464dSRoss Zwisler 		offset += spa->header.length;
238820985164SVishal Verma 
238920985164SVishal Verma 		/*
239020985164SVishal Verma 		 * spa11 (single-dimm interleave for hotplug, note storage
239120985164SVishal Verma 		 * does not actually alias the related block-data-window
239220985164SVishal Verma 		 * regions)
239320985164SVishal Verma 		 */
2394d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
239520985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
239620985164SVishal Verma 		spa->header.length = sizeof(*spa);
239720985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
239820985164SVishal Verma 		spa->range_index = 11+1;
239920985164SVishal Verma 		spa->address = t->spa_set_dma[2];
240020985164SVishal Verma 		spa->length = SPA0_SIZE;
2401d7d8464dSRoss Zwisler 		offset += spa->header.length;
240220985164SVishal Verma 
240320985164SVishal Verma 		/* spa12 (bdw for dcr4) dimm4 */
2404d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
240520985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
240620985164SVishal Verma 		spa->header.length = sizeof(*spa);
240720985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
240820985164SVishal Verma 		spa->range_index = 12+1;
240920985164SVishal Verma 		spa->address = t->dimm_dma[4];
241020985164SVishal Verma 		spa->length = DIMM_SIZE;
2411d7d8464dSRoss Zwisler 		offset += spa->header.length;
241220985164SVishal Verma 
241320985164SVishal Verma 		/* mem-region14 (spa/dcr4, dimm4) */
241420985164SVishal Verma 		memdev = nfit_buf + offset;
241520985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
241620985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
241720985164SVishal Verma 		memdev->device_handle = handle[4];
241820985164SVishal Verma 		memdev->physical_id = 4;
241920985164SVishal Verma 		memdev->region_id = 0;
242020985164SVishal Verma 		memdev->range_index = 10+1;
24213b87356fSDan Williams 		memdev->region_index = 8+1;
242220985164SVishal Verma 		memdev->region_size = 0;
242320985164SVishal Verma 		memdev->region_offset = 0;
242420985164SVishal Verma 		memdev->address = 0;
242520985164SVishal Verma 		memdev->interleave_index = 0;
242620985164SVishal Verma 		memdev->interleave_ways = 1;
2427d7d8464dSRoss Zwisler 		offset += memdev->header.length;
242820985164SVishal Verma 
2429d7d8464dSRoss Zwisler 		/* mem-region15 (spa11, dimm4) */
2430d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
243120985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
243220985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
243320985164SVishal Verma 		memdev->device_handle = handle[4];
243420985164SVishal Verma 		memdev->physical_id = 4;
243520985164SVishal Verma 		memdev->region_id = 0;
243620985164SVishal Verma 		memdev->range_index = 11+1;
24373b87356fSDan Williams 		memdev->region_index = 9+1;
243820985164SVishal Verma 		memdev->region_size = SPA0_SIZE;
2439df06a2d5SDan Williams 		memdev->region_offset = (1ULL << 48);
244020985164SVishal Verma 		memdev->address = 0;
244120985164SVishal Verma 		memdev->interleave_index = 0;
244220985164SVishal Verma 		memdev->interleave_ways = 1;
2443ac40b675SDan Williams 		memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2444d7d8464dSRoss Zwisler 		offset += memdev->header.length;
244520985164SVishal Verma 
24463b87356fSDan Williams 		/* mem-region16 (spa/bdw4, dimm4) */
2447d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
244820985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
244920985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
245020985164SVishal Verma 		memdev->device_handle = handle[4];
245120985164SVishal Verma 		memdev->physical_id = 4;
245220985164SVishal Verma 		memdev->region_id = 0;
245320985164SVishal Verma 		memdev->range_index = 12+1;
24543b87356fSDan Williams 		memdev->region_index = 8+1;
245520985164SVishal Verma 		memdev->region_size = 0;
245620985164SVishal Verma 		memdev->region_offset = 0;
245720985164SVishal Verma 		memdev->address = 0;
245820985164SVishal Verma 		memdev->interleave_index = 0;
245920985164SVishal Verma 		memdev->interleave_ways = 1;
2460d7d8464dSRoss Zwisler 		offset += memdev->header.length;
246120985164SVishal Verma 
246220985164SVishal Verma 		/* flush3 (dimm4) */
246320985164SVishal Verma 		flush = nfit_buf + offset;
246420985164SVishal Verma 		flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
246585d3fa02SDan Williams 		flush->header.length = flush_hint_size;
246620985164SVishal Verma 		flush->device_handle = handle[4];
246785d3fa02SDan Williams 		flush->hint_count = NUM_HINTS;
246885d3fa02SDan Williams 		for (i = 0; i < NUM_HINTS; i++)
246985d3fa02SDan Williams 			flush->hint_address[i] = t->flush_dma[4]
247085d3fa02SDan Williams 				+ i * sizeof(u64);
2471d7d8464dSRoss Zwisler 		offset += flush->header.length;
24729741a559SRoss Zwisler 
24739741a559SRoss Zwisler 		/* sanity check to make sure we've filled the buffer */
24749741a559SRoss Zwisler 		WARN_ON(offset != t->nfit_size);
247520985164SVishal Verma 	}
247620985164SVishal Verma 
24771526f9e2SRoss Zwisler 	t->nfit_filled = offset;
24781526f9e2SRoss Zwisler 
24799fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
24809fb1a190SDave Jiang 			SPA0_SIZE);
2481f471f1a7SDan Williams 
24826bc75619SDan Williams 	acpi_desc = &t->acpi_desc;
2483e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
2484e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2485e3654ecaSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2486ed07c433SDan Williams 	set_bit(ND_INTEL_SMART, &acpi_desc->dimm_cmd_force_en);
2487ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
2488ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_SET_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
24894cf260fcSVishal Verma 	set_bit(ND_INTEL_SMART_INJECT, &acpi_desc->dimm_cmd_force_en);
2490e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2491e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2492e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2493e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
249410246dc8SYasunori Goto 	set_bit(ND_CMD_CALL, &acpi_desc->bus_cmd_force_en);
249510246dc8SYasunori Goto 	set_bit(NFIT_CMD_TRANSLATE_SPA, &acpi_desc->bus_nfit_cmd_force_en);
24969fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_SET, &acpi_desc->bus_nfit_cmd_force_en);
24979fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_CLEAR, &acpi_desc->bus_nfit_cmd_force_en);
24989fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_GET, &acpi_desc->bus_nfit_cmd_force_en);
2499bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_GET_INFO, &acpi_desc->dimm_cmd_force_en);
2500bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_START_UPDATE, &acpi_desc->dimm_cmd_force_en);
2501bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_SEND_DATA, &acpi_desc->dimm_cmd_force_en);
2502bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_UPDATE, &acpi_desc->dimm_cmd_force_en);
2503bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_QUERY, &acpi_desc->dimm_cmd_force_en);
2504674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
25053c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_GET_SECURITY_STATE,
25063c13e2acSDave Jiang 			&acpi_desc->dimm_cmd_force_en);
25073c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_SET_PASSPHRASE, &acpi_desc->dimm_cmd_force_en);
25083c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_DISABLE_PASSPHRASE,
25093c13e2acSDave Jiang 			&acpi_desc->dimm_cmd_force_en);
25103c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_UNLOCK_UNIT, &acpi_desc->dimm_cmd_force_en);
25113c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_FREEZE_LOCK, &acpi_desc->dimm_cmd_force_en);
25123c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_SECURE_ERASE, &acpi_desc->dimm_cmd_force_en);
2513926f7480SDave Jiang 	set_bit(NVDIMM_INTEL_OVERWRITE, &acpi_desc->dimm_cmd_force_en);
2514926f7480SDave Jiang 	set_bit(NVDIMM_INTEL_QUERY_OVERWRITE, &acpi_desc->dimm_cmd_force_en);
2515*ecaa4a97SDave Jiang 	set_bit(NVDIMM_INTEL_SET_MASTER_PASSPHRASE,
2516*ecaa4a97SDave Jiang 			&acpi_desc->dimm_cmd_force_en);
2517*ecaa4a97SDave Jiang 	set_bit(NVDIMM_INTEL_MASTER_SECURE_ERASE,
2518*ecaa4a97SDave Jiang 			&acpi_desc->dimm_cmd_force_en);
25196bc75619SDan Williams }
25206bc75619SDan Williams 
25216bc75619SDan Williams static void nfit_test1_setup(struct nfit_test *t)
25226bc75619SDan Williams {
25236b577c9dSLinda Knippers 	size_t offset;
25246bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
25256bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
25266bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
25276bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
2528d26f73f0SDan Williams 	struct acpi_nfit_desc *acpi_desc;
25296bc75619SDan Williams 
25306b577c9dSLinda Knippers 	offset = 0;
25316bc75619SDan Williams 	/* spa0 (flat range with no bdw aliasing) */
25326bc75619SDan Williams 	spa = nfit_buf + offset;
25336bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
25346bc75619SDan Williams 	spa->header.length = sizeof(*spa);
25356bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
25366bc75619SDan Williams 	spa->range_index = 0+1;
25376bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
25386bc75619SDan Williams 	spa->length = SPA2_SIZE;
2539d7d8464dSRoss Zwisler 	offset += spa->header.length;
25406bc75619SDan Williams 
25417bfe97c7SDan Williams 	/* virtual cd region */
2542d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
25437bfe97c7SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
25447bfe97c7SDan Williams 	spa->header.length = sizeof(*spa);
25457bfe97c7SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_VCD), 16);
25467bfe97c7SDan Williams 	spa->range_index = 0;
25477bfe97c7SDan Williams 	spa->address = t->spa_set_dma[1];
25487bfe97c7SDan Williams 	spa->length = SPA_VCD_SIZE;
2549d7d8464dSRoss Zwisler 	offset += spa->header.length;
25507bfe97c7SDan Williams 
25516bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
25526bc75619SDan Williams 	memdev = nfit_buf + offset;
25536bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
25546bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
2555dafb1048SDan Williams 	memdev->device_handle = handle[5];
25566bc75619SDan Williams 	memdev->physical_id = 0;
25576bc75619SDan Williams 	memdev->region_id = 0;
25586bc75619SDan Williams 	memdev->range_index = 0+1;
25596bc75619SDan Williams 	memdev->region_index = 0+1;
25606bc75619SDan Williams 	memdev->region_size = SPA2_SIZE;
25616bc75619SDan Williams 	memdev->region_offset = 0;
25626bc75619SDan Williams 	memdev->address = 0;
25636bc75619SDan Williams 	memdev->interleave_index = 0;
25646bc75619SDan Williams 	memdev->interleave_ways = 1;
256558138820SDan Williams 	memdev->flags = ACPI_NFIT_MEM_SAVE_FAILED | ACPI_NFIT_MEM_RESTORE_FAILED
256658138820SDan Williams 		| ACPI_NFIT_MEM_FLUSH_FAILED | ACPI_NFIT_MEM_HEALTH_OBSERVED
2567f4295796SDan Williams 		| ACPI_NFIT_MEM_NOT_ARMED;
2568d7d8464dSRoss Zwisler 	offset += memdev->header.length;
25696bc75619SDan Williams 
25706bc75619SDan Williams 	/* dcr-descriptor0 */
25716bc75619SDan Williams 	dcr = nfit_buf + offset;
25726bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
25733b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
25743b87356fSDan Williams 			window_size);
25756bc75619SDan Williams 	dcr->region_index = 0+1;
25765dc68e55SDan Williams 	dcr_common_init(dcr);
2577dafb1048SDan Williams 	dcr->serial_number = ~handle[5];
2578be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BYTE;
25796bc75619SDan Williams 	dcr->windows = 0;
2580ac40b675SDan Williams 	offset += dcr->header.length;
2581d7d8464dSRoss Zwisler 
2582ac40b675SDan Williams 	memdev = nfit_buf + offset;
2583ac40b675SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
2584ac40b675SDan Williams 	memdev->header.length = sizeof(*memdev);
2585ac40b675SDan Williams 	memdev->device_handle = handle[6];
2586ac40b675SDan Williams 	memdev->physical_id = 0;
2587ac40b675SDan Williams 	memdev->region_id = 0;
2588ac40b675SDan Williams 	memdev->range_index = 0;
2589ac40b675SDan Williams 	memdev->region_index = 0+2;
2590ac40b675SDan Williams 	memdev->region_size = SPA2_SIZE;
2591ac40b675SDan Williams 	memdev->region_offset = 0;
2592ac40b675SDan Williams 	memdev->address = 0;
2593ac40b675SDan Williams 	memdev->interleave_index = 0;
2594ac40b675SDan Williams 	memdev->interleave_ways = 1;
2595ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_MAP_FAILED;
2596d7d8464dSRoss Zwisler 	offset += memdev->header.length;
2597ac40b675SDan Williams 
2598ac40b675SDan Williams 	/* dcr-descriptor1 */
2599ac40b675SDan Williams 	dcr = nfit_buf + offset;
2600ac40b675SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2601ac40b675SDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
2602ac40b675SDan Williams 			window_size);
2603ac40b675SDan Williams 	dcr->region_index = 0+2;
2604ac40b675SDan Williams 	dcr_common_init(dcr);
2605ac40b675SDan Williams 	dcr->serial_number = ~handle[6];
2606ac40b675SDan Williams 	dcr->code = NFIT_FIC_BYTE;
2607ac40b675SDan Williams 	dcr->windows = 0;
2608d7d8464dSRoss Zwisler 	offset += dcr->header.length;
2609ac40b675SDan Williams 
26109741a559SRoss Zwisler 	/* sanity check to make sure we've filled the buffer */
26119741a559SRoss Zwisler 	WARN_ON(offset != t->nfit_size);
26129741a559SRoss Zwisler 
26131526f9e2SRoss Zwisler 	t->nfit_filled = offset;
26141526f9e2SRoss Zwisler 
26159fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
26169fb1a190SDave Jiang 			SPA2_SIZE);
2617f471f1a7SDan Williams 
2618d26f73f0SDan Williams 	acpi_desc = &t->acpi_desc;
2619e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2620e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2621e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2622e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
2623674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
26249484e12dSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
26259484e12dSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
26269484e12dSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
26276bc75619SDan Williams }
26286bc75619SDan Williams 
26296bc75619SDan Williams static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
26306bc75619SDan Williams 		void *iobuf, u64 len, int rw)
26316bc75619SDan Williams {
26326bc75619SDan Williams 	struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
26336bc75619SDan Williams 	struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
26346bc75619SDan Williams 	struct nd_region *nd_region = &ndbr->nd_region;
26356bc75619SDan Williams 	unsigned int lane;
26366bc75619SDan Williams 
26376bc75619SDan Williams 	lane = nd_region_acquire_lane(nd_region);
26386bc75619SDan Williams 	if (rw)
263967a3e8feSRoss Zwisler 		memcpy(mmio->addr.base + dpa, iobuf, len);
264067a3e8feSRoss Zwisler 	else {
264167a3e8feSRoss Zwisler 		memcpy(iobuf, mmio->addr.base + dpa, len);
264267a3e8feSRoss Zwisler 
26435deb67f7SRobin Murphy 		/* give us some some coverage of the arch_invalidate_pmem() API */
26445deb67f7SRobin Murphy 		arch_invalidate_pmem(mmio->addr.base + dpa, len);
264567a3e8feSRoss Zwisler 	}
26466bc75619SDan Williams 	nd_region_release_lane(nd_region, lane);
26476bc75619SDan Williams 
26486bc75619SDan Williams 	return 0;
26496bc75619SDan Williams }
26506bc75619SDan Williams 
2651a7de92daSDan Williams static unsigned long nfit_ctl_handle;
2652a7de92daSDan Williams 
2653a7de92daSDan Williams union acpi_object *result;
2654a7de92daSDan Williams 
2655a7de92daSDan Williams static union acpi_object *nfit_test_evaluate_dsm(acpi_handle handle,
265694116f81SAndy Shevchenko 		const guid_t *guid, u64 rev, u64 func, union acpi_object *argv4)
2657a7de92daSDan Williams {
2658a7de92daSDan Williams 	if (handle != &nfit_ctl_handle)
2659a7de92daSDan Williams 		return ERR_PTR(-ENXIO);
2660a7de92daSDan Williams 
2661a7de92daSDan Williams 	return result;
2662a7de92daSDan Williams }
2663a7de92daSDan Williams 
2664a7de92daSDan Williams static int setup_result(void *buf, size_t size)
2665a7de92daSDan Williams {
2666a7de92daSDan Williams 	result = kmalloc(sizeof(union acpi_object) + size, GFP_KERNEL);
2667a7de92daSDan Williams 	if (!result)
2668a7de92daSDan Williams 		return -ENOMEM;
2669a7de92daSDan Williams 	result->package.type = ACPI_TYPE_BUFFER,
2670a7de92daSDan Williams 	result->buffer.pointer = (void *) (result + 1);
2671a7de92daSDan Williams 	result->buffer.length = size;
2672a7de92daSDan Williams 	memcpy(result->buffer.pointer, buf, size);
2673a7de92daSDan Williams 	memset(buf, 0, size);
2674a7de92daSDan Williams 	return 0;
2675a7de92daSDan Williams }
2676a7de92daSDan Williams 
2677a7de92daSDan Williams static int nfit_ctl_test(struct device *dev)
2678a7de92daSDan Williams {
2679a7de92daSDan Williams 	int rc, cmd_rc;
2680a7de92daSDan Williams 	struct nvdimm *nvdimm;
2681a7de92daSDan Williams 	struct acpi_device *adev;
2682a7de92daSDan Williams 	struct nfit_mem *nfit_mem;
2683a7de92daSDan Williams 	struct nd_ars_record *record;
2684a7de92daSDan Williams 	struct acpi_nfit_desc *acpi_desc;
2685a7de92daSDan Williams 	const u64 test_val = 0x0123456789abcdefULL;
2686a7de92daSDan Williams 	unsigned long mask, cmd_size, offset;
2687a7de92daSDan Williams 	union {
2688a7de92daSDan Williams 		struct nd_cmd_get_config_size cfg_size;
2689fb2a1748SDan Williams 		struct nd_cmd_clear_error clear_err;
2690a7de92daSDan Williams 		struct nd_cmd_ars_status ars_stat;
2691a7de92daSDan Williams 		struct nd_cmd_ars_cap ars_cap;
2692a7de92daSDan Williams 		char buf[sizeof(struct nd_cmd_ars_status)
2693a7de92daSDan Williams 			+ sizeof(struct nd_ars_record)];
2694a7de92daSDan Williams 	} cmds;
2695a7de92daSDan Williams 
2696a7de92daSDan Williams 	adev = devm_kzalloc(dev, sizeof(*adev), GFP_KERNEL);
2697a7de92daSDan Williams 	if (!adev)
2698a7de92daSDan Williams 		return -ENOMEM;
2699a7de92daSDan Williams 	*adev = (struct acpi_device) {
2700a7de92daSDan Williams 		.handle = &nfit_ctl_handle,
2701a7de92daSDan Williams 		.dev = {
2702a7de92daSDan Williams 			.init_name = "test-adev",
2703a7de92daSDan Williams 		},
2704a7de92daSDan Williams 	};
2705a7de92daSDan Williams 
2706a7de92daSDan Williams 	acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2707a7de92daSDan Williams 	if (!acpi_desc)
2708a7de92daSDan Williams 		return -ENOMEM;
2709a7de92daSDan Williams 	*acpi_desc = (struct acpi_nfit_desc) {
2710a7de92daSDan Williams 		.nd_desc = {
2711a7de92daSDan Williams 			.cmd_mask = 1UL << ND_CMD_ARS_CAP
2712a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_START
2713a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_STATUS
271410246dc8SYasunori Goto 				| 1UL << ND_CMD_CLEAR_ERROR
271510246dc8SYasunori Goto 				| 1UL << ND_CMD_CALL,
2716a7de92daSDan Williams 			.module = THIS_MODULE,
2717a7de92daSDan Williams 			.provider_name = "ACPI.NFIT",
2718a7de92daSDan Williams 			.ndctl = acpi_nfit_ctl,
27199fb1a190SDave Jiang 			.bus_dsm_mask = 1UL << NFIT_CMD_TRANSLATE_SPA
27209fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_SET
27219fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_CLEAR
27229fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_GET,
2723a7de92daSDan Williams 		},
2724a7de92daSDan Williams 		.dev = &adev->dev,
2725a7de92daSDan Williams 	};
2726a7de92daSDan Williams 
2727a7de92daSDan Williams 	nfit_mem = devm_kzalloc(dev, sizeof(*nfit_mem), GFP_KERNEL);
2728a7de92daSDan Williams 	if (!nfit_mem)
2729a7de92daSDan Williams 		return -ENOMEM;
2730a7de92daSDan Williams 
2731a7de92daSDan Williams 	mask = 1UL << ND_CMD_SMART | 1UL << ND_CMD_SMART_THRESHOLD
2732a7de92daSDan Williams 		| 1UL << ND_CMD_DIMM_FLAGS | 1UL << ND_CMD_GET_CONFIG_SIZE
2733a7de92daSDan Williams 		| 1UL << ND_CMD_GET_CONFIG_DATA | 1UL << ND_CMD_SET_CONFIG_DATA
2734a7de92daSDan Williams 		| 1UL << ND_CMD_VENDOR;
2735a7de92daSDan Williams 	*nfit_mem = (struct nfit_mem) {
2736a7de92daSDan Williams 		.adev = adev,
2737a7de92daSDan Williams 		.family = NVDIMM_FAMILY_INTEL,
2738a7de92daSDan Williams 		.dsm_mask = mask,
2739a7de92daSDan Williams 	};
2740a7de92daSDan Williams 
2741a7de92daSDan Williams 	nvdimm = devm_kzalloc(dev, sizeof(*nvdimm), GFP_KERNEL);
2742a7de92daSDan Williams 	if (!nvdimm)
2743a7de92daSDan Williams 		return -ENOMEM;
2744a7de92daSDan Williams 	*nvdimm = (struct nvdimm) {
2745a7de92daSDan Williams 		.provider_data = nfit_mem,
2746a7de92daSDan Williams 		.cmd_mask = mask,
2747a7de92daSDan Williams 		.dev = {
2748a7de92daSDan Williams 			.init_name = "test-dimm",
2749a7de92daSDan Williams 		},
2750a7de92daSDan Williams 	};
2751a7de92daSDan Williams 
2752a7de92daSDan Williams 
2753a7de92daSDan Williams 	/* basic checkout of a typical 'get config size' command */
2754a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2755a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2756a7de92daSDan Williams 		.status = 0,
2757a7de92daSDan Williams 		.config_size = SZ_128K,
2758a7de92daSDan Williams 		.max_xfer = SZ_4K,
2759a7de92daSDan Williams 	};
2760a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2761a7de92daSDan Williams 	if (rc)
2762a7de92daSDan Williams 		return rc;
2763a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2764a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2765a7de92daSDan Williams 
2766a7de92daSDan Williams 	if (rc < 0 || cmd_rc || cmds.cfg_size.status != 0
2767a7de92daSDan Williams 			|| cmds.cfg_size.config_size != SZ_128K
2768a7de92daSDan Williams 			|| cmds.cfg_size.max_xfer != SZ_4K) {
2769a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2770a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2771a7de92daSDan Williams 		return -EIO;
2772a7de92daSDan Williams 	}
2773a7de92daSDan Williams 
2774a7de92daSDan Williams 
2775a7de92daSDan Williams 	/* test ars_status with zero output */
2776a7de92daSDan Williams 	cmd_size = offsetof(struct nd_cmd_ars_status, address);
2777a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2778a7de92daSDan Williams 		.out_length = 0,
2779a7de92daSDan Williams 	};
2780a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2781a7de92daSDan Williams 	if (rc)
2782a7de92daSDan Williams 		return rc;
2783a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2784a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2785a7de92daSDan Williams 
2786a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2787a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2788a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2789a7de92daSDan Williams 		return -EIO;
2790a7de92daSDan Williams 	}
2791a7de92daSDan Williams 
2792a7de92daSDan Williams 
2793a7de92daSDan Williams 	/* test ars_cap with benign extended status */
2794a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_cap);
2795a7de92daSDan Williams 	cmds.ars_cap = (struct nd_cmd_ars_cap) {
2796a7de92daSDan Williams 		.status = ND_ARS_PERSISTENT << 16,
2797a7de92daSDan Williams 	};
2798a7de92daSDan Williams 	offset = offsetof(struct nd_cmd_ars_cap, status);
2799a7de92daSDan Williams 	rc = setup_result(cmds.buf + offset, cmd_size - offset);
2800a7de92daSDan Williams 	if (rc)
2801a7de92daSDan Williams 		return rc;
2802a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_CAP,
2803a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2804a7de92daSDan Williams 
2805a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2806a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2807a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2808a7de92daSDan Williams 		return -EIO;
2809a7de92daSDan Williams 	}
2810a7de92daSDan Williams 
2811a7de92daSDan Williams 
2812a7de92daSDan Williams 	/* test ars_status with 'status' trimmed from 'out_length' */
2813a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2814a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2815a7de92daSDan Williams 		.out_length = cmd_size - 4,
2816a7de92daSDan Williams 	};
2817a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2818a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2819a7de92daSDan Williams 		.length = test_val,
2820a7de92daSDan Williams 	};
2821a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2822a7de92daSDan Williams 	if (rc)
2823a7de92daSDan Williams 		return rc;
2824a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2825a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2826a7de92daSDan Williams 
2827a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2828a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2829a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2830a7de92daSDan Williams 		return -EIO;
2831a7de92daSDan Williams 	}
2832a7de92daSDan Williams 
2833a7de92daSDan Williams 
2834a7de92daSDan Williams 	/* test ars_status with 'Output (Size)' including 'status' */
2835a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2836a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2837a7de92daSDan Williams 		.out_length = cmd_size,
2838a7de92daSDan Williams 	};
2839a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2840a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2841a7de92daSDan Williams 		.length = test_val,
2842a7de92daSDan Williams 	};
2843a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2844a7de92daSDan Williams 	if (rc)
2845a7de92daSDan Williams 		return rc;
2846a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2847a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2848a7de92daSDan Williams 
2849a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2850a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2851a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2852a7de92daSDan Williams 		return -EIO;
2853a7de92daSDan Williams 	}
2854a7de92daSDan Williams 
2855a7de92daSDan Williams 
2856a7de92daSDan Williams 	/* test extended status for get_config_size results in failure */
2857a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2858a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2859a7de92daSDan Williams 		.status = 1 << 16,
2860a7de92daSDan Williams 	};
2861a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2862a7de92daSDan Williams 	if (rc)
2863a7de92daSDan Williams 		return rc;
2864a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2865a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2866a7de92daSDan Williams 
2867a7de92daSDan Williams 	if (rc < 0 || cmd_rc >= 0) {
2868a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2869a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2870a7de92daSDan Williams 		return -EIO;
2871a7de92daSDan Williams 	}
2872a7de92daSDan Williams 
2873fb2a1748SDan Williams 	/* test clear error */
2874fb2a1748SDan Williams 	cmd_size = sizeof(cmds.clear_err);
2875fb2a1748SDan Williams 	cmds.clear_err = (struct nd_cmd_clear_error) {
2876fb2a1748SDan Williams 		.length = 512,
2877fb2a1748SDan Williams 		.cleared = 512,
2878fb2a1748SDan Williams 	};
2879fb2a1748SDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2880fb2a1748SDan Williams 	if (rc)
2881fb2a1748SDan Williams 		return rc;
2882fb2a1748SDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_CLEAR_ERROR,
2883fb2a1748SDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2884fb2a1748SDan Williams 	if (rc < 0 || cmd_rc) {
2885fb2a1748SDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2886fb2a1748SDan Williams 				__func__, __LINE__, rc, cmd_rc);
2887fb2a1748SDan Williams 		return -EIO;
2888fb2a1748SDan Williams 	}
2889fb2a1748SDan Williams 
2890a7de92daSDan Williams 	return 0;
2891a7de92daSDan Williams }
2892a7de92daSDan Williams 
28936bc75619SDan Williams static int nfit_test_probe(struct platform_device *pdev)
28946bc75619SDan Williams {
28956bc75619SDan Williams 	struct nvdimm_bus_descriptor *nd_desc;
28966bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
28976bc75619SDan Williams 	struct device *dev = &pdev->dev;
28986bc75619SDan Williams 	struct nfit_test *nfit_test;
2899231bf117SDan Williams 	struct nfit_mem *nfit_mem;
2900c14a868aSDan Williams 	union acpi_object *obj;
29016bc75619SDan Williams 	int rc;
29026bc75619SDan Williams 
2903a7de92daSDan Williams 	if (strcmp(dev_name(&pdev->dev), "nfit_test.0") == 0) {
2904a7de92daSDan Williams 		rc = nfit_ctl_test(&pdev->dev);
2905a7de92daSDan Williams 		if (rc)
2906a7de92daSDan Williams 			return rc;
2907a7de92daSDan Williams 	}
2908a7de92daSDan Williams 
29096bc75619SDan Williams 	nfit_test = to_nfit_test(&pdev->dev);
29106bc75619SDan Williams 
29116bc75619SDan Williams 	/* common alloc */
29126bc75619SDan Williams 	if (nfit_test->num_dcr) {
29136bc75619SDan Williams 		int num = nfit_test->num_dcr;
29146bc75619SDan Williams 
29156bc75619SDan Williams 		nfit_test->dimm = devm_kcalloc(dev, num, sizeof(void *),
29166bc75619SDan Williams 				GFP_KERNEL);
29176bc75619SDan Williams 		nfit_test->dimm_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
29186bc75619SDan Williams 				GFP_KERNEL);
29199d27a87eSDan Williams 		nfit_test->flush = devm_kcalloc(dev, num, sizeof(void *),
29209d27a87eSDan Williams 				GFP_KERNEL);
29219d27a87eSDan Williams 		nfit_test->flush_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
29229d27a87eSDan Williams 				GFP_KERNEL);
29236bc75619SDan Williams 		nfit_test->label = devm_kcalloc(dev, num, sizeof(void *),
29246bc75619SDan Williams 				GFP_KERNEL);
29256bc75619SDan Williams 		nfit_test->label_dma = devm_kcalloc(dev, num,
29266bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
29276bc75619SDan Williams 		nfit_test->dcr = devm_kcalloc(dev, num,
29286bc75619SDan Williams 				sizeof(struct nfit_test_dcr *), GFP_KERNEL);
29296bc75619SDan Williams 		nfit_test->dcr_dma = devm_kcalloc(dev, num,
29306bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
2931ed07c433SDan Williams 		nfit_test->smart = devm_kcalloc(dev, num,
2932ed07c433SDan Williams 				sizeof(struct nd_intel_smart), GFP_KERNEL);
2933ed07c433SDan Williams 		nfit_test->smart_threshold = devm_kcalloc(dev, num,
2934ed07c433SDan Williams 				sizeof(struct nd_intel_smart_threshold),
2935ed07c433SDan Williams 				GFP_KERNEL);
2936bfbaa952SDave Jiang 		nfit_test->fw = devm_kcalloc(dev, num,
2937bfbaa952SDave Jiang 				sizeof(struct nfit_test_fw), GFP_KERNEL);
29386bc75619SDan Williams 		if (nfit_test->dimm && nfit_test->dimm_dma && nfit_test->label
29396bc75619SDan Williams 				&& nfit_test->label_dma && nfit_test->dcr
29409d27a87eSDan Williams 				&& nfit_test->dcr_dma && nfit_test->flush
2941bfbaa952SDave Jiang 				&& nfit_test->flush_dma
2942bfbaa952SDave Jiang 				&& nfit_test->fw)
29436bc75619SDan Williams 			/* pass */;
29446bc75619SDan Williams 		else
29456bc75619SDan Williams 			return -ENOMEM;
29466bc75619SDan Williams 	}
29476bc75619SDan Williams 
29486bc75619SDan Williams 	if (nfit_test->num_pm) {
29496bc75619SDan Williams 		int num = nfit_test->num_pm;
29506bc75619SDan Williams 
29516bc75619SDan Williams 		nfit_test->spa_set = devm_kcalloc(dev, num, sizeof(void *),
29526bc75619SDan Williams 				GFP_KERNEL);
29536bc75619SDan Williams 		nfit_test->spa_set_dma = devm_kcalloc(dev, num,
29546bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
29556bc75619SDan Williams 		if (nfit_test->spa_set && nfit_test->spa_set_dma)
29566bc75619SDan Williams 			/* pass */;
29576bc75619SDan Williams 		else
29586bc75619SDan Williams 			return -ENOMEM;
29596bc75619SDan Williams 	}
29606bc75619SDan Williams 
29616bc75619SDan Williams 	/* per-nfit specific alloc */
29626bc75619SDan Williams 	if (nfit_test->alloc(nfit_test))
29636bc75619SDan Williams 		return -ENOMEM;
29646bc75619SDan Williams 
29656bc75619SDan Williams 	nfit_test->setup(nfit_test);
29666bc75619SDan Williams 	acpi_desc = &nfit_test->acpi_desc;
2967a61fe6f7SDan Williams 	acpi_nfit_desc_init(acpi_desc, &pdev->dev);
29686bc75619SDan Williams 	acpi_desc->blk_do_io = nfit_test_blk_do_io;
29696bc75619SDan Williams 	nd_desc = &acpi_desc->nd_desc;
2970a61fe6f7SDan Williams 	nd_desc->provider_name = NULL;
2971bc9775d8SDan Williams 	nd_desc->module = THIS_MODULE;
2972a61fe6f7SDan Williams 	nd_desc->ndctl = nfit_test_ctl;
29736bc75619SDan Williams 
2974e7a11b44SDan Williams 	rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_buf,
29751526f9e2SRoss Zwisler 			nfit_test->nfit_filled);
297658cd71b4SDan Williams 	if (rc)
297720985164SVishal Verma 		return rc;
297820985164SVishal Verma 
2979fbabd829SDan Williams 	rc = devm_add_action_or_reset(&pdev->dev, acpi_nfit_shutdown, acpi_desc);
2980fbabd829SDan Williams 	if (rc)
2981fbabd829SDan Williams 		return rc;
2982fbabd829SDan Williams 
298320985164SVishal Verma 	if (nfit_test->setup != nfit_test0_setup)
298420985164SVishal Verma 		return 0;
298520985164SVishal Verma 
298620985164SVishal Verma 	nfit_test->setup_hotplug = 1;
298720985164SVishal Verma 	nfit_test->setup(nfit_test);
298820985164SVishal Verma 
2989c14a868aSDan Williams 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
2990c14a868aSDan Williams 	if (!obj)
2991c14a868aSDan Williams 		return -ENOMEM;
2992c14a868aSDan Williams 	obj->type = ACPI_TYPE_BUFFER;
2993c14a868aSDan Williams 	obj->buffer.length = nfit_test->nfit_size;
2994c14a868aSDan Williams 	obj->buffer.pointer = nfit_test->nfit_buf;
2995c14a868aSDan Williams 	*(nfit_test->_fit) = obj;
2996c14a868aSDan Williams 	__acpi_nfit_notify(&pdev->dev, nfit_test, 0x80);
2997231bf117SDan Williams 
2998231bf117SDan Williams 	/* associate dimm devices with nfit_mem data for notification testing */
2999231bf117SDan Williams 	mutex_lock(&acpi_desc->init_mutex);
3000231bf117SDan Williams 	list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
3001231bf117SDan Williams 		u32 nfit_handle = __to_nfit_memdev(nfit_mem)->device_handle;
3002231bf117SDan Williams 		int i;
3003231bf117SDan Williams 
3004af31b04bSMasayoshi Mizuma 		for (i = 0; i < ARRAY_SIZE(handle); i++)
3005231bf117SDan Williams 			if (nfit_handle == handle[i])
3006231bf117SDan Williams 				dev_set_drvdata(nfit_test->dimm_dev[i],
3007231bf117SDan Williams 						nfit_mem);
3008231bf117SDan Williams 	}
3009231bf117SDan Williams 	mutex_unlock(&acpi_desc->init_mutex);
30106bc75619SDan Williams 
30116bc75619SDan Williams 	return 0;
30126bc75619SDan Williams }
30136bc75619SDan Williams 
30146bc75619SDan Williams static int nfit_test_remove(struct platform_device *pdev)
30156bc75619SDan Williams {
30166bc75619SDan Williams 	return 0;
30176bc75619SDan Williams }
30186bc75619SDan Williams 
30196bc75619SDan Williams static void nfit_test_release(struct device *dev)
30206bc75619SDan Williams {
30216bc75619SDan Williams 	struct nfit_test *nfit_test = to_nfit_test(dev);
30226bc75619SDan Williams 
30236bc75619SDan Williams 	kfree(nfit_test);
30246bc75619SDan Williams }
30256bc75619SDan Williams 
30266bc75619SDan Williams static const struct platform_device_id nfit_test_id[] = {
30276bc75619SDan Williams 	{ KBUILD_MODNAME },
30286bc75619SDan Williams 	{ },
30296bc75619SDan Williams };
30306bc75619SDan Williams 
30316bc75619SDan Williams static struct platform_driver nfit_test_driver = {
30326bc75619SDan Williams 	.probe = nfit_test_probe,
30336bc75619SDan Williams 	.remove = nfit_test_remove,
30346bc75619SDan Williams 	.driver = {
30356bc75619SDan Williams 		.name = KBUILD_MODNAME,
30366bc75619SDan Williams 	},
30376bc75619SDan Williams 	.id_table = nfit_test_id,
30386bc75619SDan Williams };
30396bc75619SDan Williams 
30405d8beee2SDan Williams static char mcsafe_buf[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
30415d8beee2SDan Williams 
30425d8beee2SDan Williams enum INJECT {
30435d8beee2SDan Williams 	INJECT_NONE,
30445d8beee2SDan Williams 	INJECT_SRC,
30455d8beee2SDan Williams 	INJECT_DST,
30465d8beee2SDan Williams };
30475d8beee2SDan Williams 
30485d8beee2SDan Williams static void mcsafe_test_init(char *dst, char *src, size_t size)
30495d8beee2SDan Williams {
30505d8beee2SDan Williams 	size_t i;
30515d8beee2SDan Williams 
30525d8beee2SDan Williams 	memset(dst, 0xff, size);
30535d8beee2SDan Williams 	for (i = 0; i < size; i++)
30545d8beee2SDan Williams 		src[i] = (char) i;
30555d8beee2SDan Williams }
30565d8beee2SDan Williams 
30575d8beee2SDan Williams static bool mcsafe_test_validate(unsigned char *dst, unsigned char *src,
30585d8beee2SDan Williams 		size_t size, unsigned long rem)
30595d8beee2SDan Williams {
30605d8beee2SDan Williams 	size_t i;
30615d8beee2SDan Williams 
30625d8beee2SDan Williams 	for (i = 0; i < size - rem; i++)
30635d8beee2SDan Williams 		if (dst[i] != (unsigned char) i) {
30645d8beee2SDan Williams 			pr_info_once("%s:%d: offset: %zd got: %#x expect: %#x\n",
30655d8beee2SDan Williams 					__func__, __LINE__, i, dst[i],
30665d8beee2SDan Williams 					(unsigned char) i);
30675d8beee2SDan Williams 			return false;
30685d8beee2SDan Williams 		}
30695d8beee2SDan Williams 	for (i = size - rem; i < size; i++)
30705d8beee2SDan Williams 		if (dst[i] != 0xffU) {
30715d8beee2SDan Williams 			pr_info_once("%s:%d: offset: %zd got: %#x expect: 0xff\n",
30725d8beee2SDan Williams 					__func__, __LINE__, i, dst[i]);
30735d8beee2SDan Williams 			return false;
30745d8beee2SDan Williams 		}
30755d8beee2SDan Williams 	return true;
30765d8beee2SDan Williams }
30775d8beee2SDan Williams 
30785d8beee2SDan Williams void mcsafe_test(void)
30795d8beee2SDan Williams {
30805d8beee2SDan Williams 	char *inject_desc[] = { "none", "source", "destination" };
30815d8beee2SDan Williams 	enum INJECT inj;
30825d8beee2SDan Williams 
30835d8beee2SDan Williams 	if (IS_ENABLED(CONFIG_MCSAFE_TEST)) {
30845d8beee2SDan Williams 		pr_info("%s: run...\n", __func__);
30855d8beee2SDan Williams 	} else {
30865d8beee2SDan Williams 		pr_info("%s: disabled, skip.\n", __func__);
30875d8beee2SDan Williams 		return;
30885d8beee2SDan Williams 	}
30895d8beee2SDan Williams 
30905d8beee2SDan Williams 	for (inj = INJECT_NONE; inj <= INJECT_DST; inj++) {
30915d8beee2SDan Williams 		int i;
30925d8beee2SDan Williams 
30935d8beee2SDan Williams 		pr_info("%s: inject: %s\n", __func__, inject_desc[inj]);
30945d8beee2SDan Williams 		for (i = 0; i < 512; i++) {
30955d8beee2SDan Williams 			unsigned long expect, rem;
30965d8beee2SDan Williams 			void *src, *dst;
30975d8beee2SDan Williams 			bool valid;
30985d8beee2SDan Williams 
30995d8beee2SDan Williams 			switch (inj) {
31005d8beee2SDan Williams 			case INJECT_NONE:
31015d8beee2SDan Williams 				mcsafe_inject_src(NULL);
31025d8beee2SDan Williams 				mcsafe_inject_dst(NULL);
31035d8beee2SDan Williams 				dst = &mcsafe_buf[2048];
31045d8beee2SDan Williams 				src = &mcsafe_buf[1024 - i];
31055d8beee2SDan Williams 				expect = 0;
31065d8beee2SDan Williams 				break;
31075d8beee2SDan Williams 			case INJECT_SRC:
31085d8beee2SDan Williams 				mcsafe_inject_src(&mcsafe_buf[1024]);
31095d8beee2SDan Williams 				mcsafe_inject_dst(NULL);
31105d8beee2SDan Williams 				dst = &mcsafe_buf[2048];
31115d8beee2SDan Williams 				src = &mcsafe_buf[1024 - i];
31125d8beee2SDan Williams 				expect = 512 - i;
31135d8beee2SDan Williams 				break;
31145d8beee2SDan Williams 			case INJECT_DST:
31155d8beee2SDan Williams 				mcsafe_inject_src(NULL);
31165d8beee2SDan Williams 				mcsafe_inject_dst(&mcsafe_buf[2048]);
31175d8beee2SDan Williams 				dst = &mcsafe_buf[2048 - i];
31185d8beee2SDan Williams 				src = &mcsafe_buf[1024];
31195d8beee2SDan Williams 				expect = 512 - i;
31205d8beee2SDan Williams 				break;
31215d8beee2SDan Williams 			}
31225d8beee2SDan Williams 
31235d8beee2SDan Williams 			mcsafe_test_init(dst, src, 512);
31245d8beee2SDan Williams 			rem = __memcpy_mcsafe(dst, src, 512);
31255d8beee2SDan Williams 			valid = mcsafe_test_validate(dst, src, 512, expect);
31265d8beee2SDan Williams 			if (rem == expect && valid)
31275d8beee2SDan Williams 				continue;
31285d8beee2SDan Williams 			pr_info("%s: copy(%#lx, %#lx, %d) off: %d rem: %ld %s expect: %ld\n",
31295d8beee2SDan Williams 					__func__,
31305d8beee2SDan Williams 					((unsigned long) dst) & ~PAGE_MASK,
31315d8beee2SDan Williams 					((unsigned long ) src) & ~PAGE_MASK,
31325d8beee2SDan Williams 					512, i, rem, valid ? "valid" : "bad",
31335d8beee2SDan Williams 					expect);
31345d8beee2SDan Williams 		}
31355d8beee2SDan Williams 	}
31365d8beee2SDan Williams 
31375d8beee2SDan Williams 	mcsafe_inject_src(NULL);
31385d8beee2SDan Williams 	mcsafe_inject_dst(NULL);
31395d8beee2SDan Williams }
31405d8beee2SDan Williams 
31416bc75619SDan Williams static __init int nfit_test_init(void)
31426bc75619SDan Williams {
31436bc75619SDan Williams 	int rc, i;
31446bc75619SDan Williams 
31450fb5c8dfSDan Williams 	pmem_test();
31460fb5c8dfSDan Williams 	libnvdimm_test();
31470fb5c8dfSDan Williams 	acpi_nfit_test();
31480fb5c8dfSDan Williams 	device_dax_test();
31495d8beee2SDan Williams 	mcsafe_test();
31500fb5c8dfSDan Williams 
3151a7de92daSDan Williams 	nfit_test_setup(nfit_test_lookup, nfit_test_evaluate_dsm);
3152231bf117SDan Williams 
31539fb1a190SDave Jiang 	nfit_wq = create_singlethread_workqueue("nfit");
31549fb1a190SDave Jiang 	if (!nfit_wq)
31559fb1a190SDave Jiang 		return -ENOMEM;
31569fb1a190SDave Jiang 
3157a7de92daSDan Williams 	nfit_test_dimm = class_create(THIS_MODULE, "nfit_test_dimm");
3158a7de92daSDan Williams 	if (IS_ERR(nfit_test_dimm)) {
3159a7de92daSDan Williams 		rc = PTR_ERR(nfit_test_dimm);
3160a7de92daSDan Williams 		goto err_register;
3161a7de92daSDan Williams 	}
31626bc75619SDan Williams 
31636bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++) {
31646bc75619SDan Williams 		struct nfit_test *nfit_test;
31656bc75619SDan Williams 		struct platform_device *pdev;
31666bc75619SDan Williams 
31676bc75619SDan Williams 		nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
31686bc75619SDan Williams 		if (!nfit_test) {
31696bc75619SDan Williams 			rc = -ENOMEM;
31706bc75619SDan Williams 			goto err_register;
31716bc75619SDan Williams 		}
31726bc75619SDan Williams 		INIT_LIST_HEAD(&nfit_test->resources);
31739fb1a190SDave Jiang 		badrange_init(&nfit_test->badrange);
31746bc75619SDan Williams 		switch (i) {
31756bc75619SDan Williams 		case 0:
31766bc75619SDan Williams 			nfit_test->num_pm = NUM_PM;
3177dafb1048SDan Williams 			nfit_test->dcr_idx = 0;
31786bc75619SDan Williams 			nfit_test->num_dcr = NUM_DCR;
31796bc75619SDan Williams 			nfit_test->alloc = nfit_test0_alloc;
31806bc75619SDan Williams 			nfit_test->setup = nfit_test0_setup;
31816bc75619SDan Williams 			break;
31826bc75619SDan Williams 		case 1:
3183a117699cSYasunori Goto 			nfit_test->num_pm = 2;
3184dafb1048SDan Williams 			nfit_test->dcr_idx = NUM_DCR;
3185ac40b675SDan Williams 			nfit_test->num_dcr = 2;
31866bc75619SDan Williams 			nfit_test->alloc = nfit_test1_alloc;
31876bc75619SDan Williams 			nfit_test->setup = nfit_test1_setup;
31886bc75619SDan Williams 			break;
31896bc75619SDan Williams 		default:
31906bc75619SDan Williams 			rc = -EINVAL;
31916bc75619SDan Williams 			goto err_register;
31926bc75619SDan Williams 		}
31936bc75619SDan Williams 		pdev = &nfit_test->pdev;
31946bc75619SDan Williams 		pdev->name = KBUILD_MODNAME;
31956bc75619SDan Williams 		pdev->id = i;
31966bc75619SDan Williams 		pdev->dev.release = nfit_test_release;
31976bc75619SDan Williams 		rc = platform_device_register(pdev);
31986bc75619SDan Williams 		if (rc) {
31996bc75619SDan Williams 			put_device(&pdev->dev);
32006bc75619SDan Williams 			goto err_register;
32016bc75619SDan Williams 		}
32028b06b884SDan Williams 		get_device(&pdev->dev);
32036bc75619SDan Williams 
32046bc75619SDan Williams 		rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
32056bc75619SDan Williams 		if (rc)
32066bc75619SDan Williams 			goto err_register;
32076bc75619SDan Williams 
32086bc75619SDan Williams 		instances[i] = nfit_test;
32099fb1a190SDave Jiang 		INIT_WORK(&nfit_test->work, uc_error_notify);
32106bc75619SDan Williams 	}
32116bc75619SDan Williams 
32126bc75619SDan Williams 	rc = platform_driver_register(&nfit_test_driver);
32136bc75619SDan Williams 	if (rc)
32146bc75619SDan Williams 		goto err_register;
32156bc75619SDan Williams 	return 0;
32166bc75619SDan Williams 
32176bc75619SDan Williams  err_register:
32189fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
32196bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
32206bc75619SDan Williams 		if (instances[i])
32216bc75619SDan Williams 			platform_device_unregister(&instances[i]->pdev);
32226bc75619SDan Williams 	nfit_test_teardown();
32238b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
32248b06b884SDan Williams 		if (instances[i])
32258b06b884SDan Williams 			put_device(&instances[i]->pdev.dev);
32268b06b884SDan Williams 
32276bc75619SDan Williams 	return rc;
32286bc75619SDan Williams }
32296bc75619SDan Williams 
32306bc75619SDan Williams static __exit void nfit_test_exit(void)
32316bc75619SDan Williams {
32326bc75619SDan Williams 	int i;
32336bc75619SDan Williams 
32349fb1a190SDave Jiang 	flush_workqueue(nfit_wq);
32359fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
32366bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
32376bc75619SDan Williams 		platform_device_unregister(&instances[i]->pdev);
32388b06b884SDan Williams 	platform_driver_unregister(&nfit_test_driver);
32396bc75619SDan Williams 	nfit_test_teardown();
32408b06b884SDan Williams 
32418b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
32428b06b884SDan Williams 		put_device(&instances[i]->pdev.dev);
3243231bf117SDan Williams 	class_destroy(nfit_test_dimm);
32446bc75619SDan Williams }
32456bc75619SDan Williams 
32466bc75619SDan Williams module_init(nfit_test_init);
32476bc75619SDan Williams module_exit(nfit_test_exit);
32486bc75619SDan Williams MODULE_LICENSE("GPL v2");
32496bc75619SDan Williams MODULE_AUTHOR("Intel Corporation");
3250