xref: /linux/tools/testing/nvdimm/test/nfit.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
15b497af4SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
26bc75619SDan Williams /*
36bc75619SDan Williams  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
46bc75619SDan Williams  */
56bc75619SDan Williams #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
66bc75619SDan Williams #include <linux/platform_device.h>
76bc75619SDan Williams #include <linux/dma-mapping.h>
8d8d378faSDan Williams #include <linux/workqueue.h>
96bc75619SDan Williams #include <linux/libnvdimm.h>
10e3f5df76SDan Williams #include <linux/genalloc.h>
116bc75619SDan Williams #include <linux/vmalloc.h>
126bc75619SDan Williams #include <linux/device.h>
136bc75619SDan Williams #include <linux/module.h>
1420985164SVishal Verma #include <linux/mutex.h>
156bc75619SDan Williams #include <linux/ndctl.h>
166bc75619SDan Williams #include <linux/sizes.h>
1720985164SVishal Verma #include <linux/list.h>
186bc75619SDan Williams #include <linux/slab.h>
19a7de92daSDan Williams #include <nd-core.h>
200ead1118SDan Williams #include <intel.h>
216bc75619SDan Williams #include <nfit.h>
226bc75619SDan Williams #include <nd.h>
236bc75619SDan Williams #include "nfit_test.h"
240fb5c8dfSDan Williams #include "../watermark.h"
256bc75619SDan Williams 
266bc75619SDan Williams /*
276bc75619SDan Williams  * Generate an NFIT table to describe the following topology:
286bc75619SDan Williams  *
296bc75619SDan Williams  * BUS0: Interleaved PMEM regions, and aliasing with BLK regions
306bc75619SDan Williams  *
316bc75619SDan Williams  *                     (a)                       (b)            DIMM   BLK-REGION
326bc75619SDan Williams  *           +----------+--------------+----------+---------+
336bc75619SDan Williams  * +------+  |  blk2.0  |     pm0.0    |  blk2.1  |  pm1.0  |    0      region2
346bc75619SDan Williams  * | imc0 +--+- - - - - region0 - - - -+----------+         +
356bc75619SDan Williams  * +--+---+  |  blk3.0  |     pm0.0    |  blk3.1  |  pm1.0  |    1      region3
366bc75619SDan Williams  *    |      +----------+--------------v----------v         v
376bc75619SDan Williams  * +--+---+                            |                    |
386bc75619SDan Williams  * | cpu0 |                                    region1
396bc75619SDan Williams  * +--+---+                            |                    |
406bc75619SDan Williams  *    |      +-------------------------^----------^         ^
416bc75619SDan Williams  * +--+---+  |                 blk4.0             |  pm1.0  |    2      region4
426bc75619SDan Williams  * | imc1 +--+-------------------------+----------+         +
436bc75619SDan Williams  * +------+  |                 blk5.0             |  pm1.0  |    3      region5
446bc75619SDan Williams  *           +-------------------------+----------+-+-------+
456bc75619SDan Williams  *
4620985164SVishal Verma  * +--+---+
4720985164SVishal Verma  * | cpu1 |
4820985164SVishal Verma  * +--+---+                   (Hotplug DIMM)
4920985164SVishal Verma  *    |      +----------------------------------------------+
5020985164SVishal Verma  * +--+---+  |                 blk6.0/pm7.0                 |    4      region6/7
5120985164SVishal Verma  * | imc0 +--+----------------------------------------------+
5220985164SVishal Verma  * +------+
5320985164SVishal Verma  *
5420985164SVishal Verma  *
556bc75619SDan Williams  * *) In this layout we have four dimms and two memory controllers in one
566bc75619SDan Williams  *    socket.  Each unique interface (BLK or PMEM) to DPA space
576bc75619SDan Williams  *    is identified by a region device with a dynamically assigned id.
586bc75619SDan Williams  *
596bc75619SDan Williams  * *) The first portion of dimm0 and dimm1 are interleaved as REGION0.
606bc75619SDan Williams  *    A single PMEM namespace "pm0.0" is created using half of the
616bc75619SDan Williams  *    REGION0 SPA-range.  REGION0 spans dimm0 and dimm1.  PMEM namespace
626bc75619SDan Williams  *    allocate from from the bottom of a region.  The unallocated
636bc75619SDan Williams  *    portion of REGION0 aliases with REGION2 and REGION3.  That
646bc75619SDan Williams  *    unallacted capacity is reclaimed as BLK namespaces ("blk2.0" and
656bc75619SDan Williams  *    "blk3.0") starting at the base of each DIMM to offset (a) in those
666bc75619SDan Williams  *    DIMMs.  "pm0.0", "blk2.0" and "blk3.0" are free-form readable
676bc75619SDan Williams  *    names that can be assigned to a namespace.
686bc75619SDan Williams  *
696bc75619SDan Williams  * *) In the last portion of dimm0 and dimm1 we have an interleaved
706bc75619SDan Williams  *    SPA range, REGION1, that spans those two dimms as well as dimm2
716bc75619SDan Williams  *    and dimm3.  Some of REGION1 allocated to a PMEM namespace named
726bc75619SDan Williams  *    "pm1.0" the rest is reclaimed in 4 BLK namespaces (for each
736bc75619SDan Williams  *    dimm in the interleave set), "blk2.1", "blk3.1", "blk4.0", and
746bc75619SDan Williams  *    "blk5.0".
756bc75619SDan Williams  *
766bc75619SDan Williams  * *) The portion of dimm2 and dimm3 that do not participate in the
776bc75619SDan Williams  *    REGION1 interleaved SPA range (i.e. the DPA address below offset
786bc75619SDan Williams  *    (b) are also included in the "blk4.0" and "blk5.0" namespaces.
796bc75619SDan Williams  *    Note, that BLK namespaces need not be contiguous in DPA-space, and
806bc75619SDan Williams  *    can consume aliased capacity from multiple interleave sets.
816bc75619SDan Williams  *
826bc75619SDan Williams  * BUS1: Legacy NVDIMM (single contiguous range)
836bc75619SDan Williams  *
846bc75619SDan Williams  *  region2
856bc75619SDan Williams  * +---------------------+
866bc75619SDan Williams  * |---------------------|
876bc75619SDan Williams  * ||       pm2.0       ||
886bc75619SDan Williams  * |---------------------|
896bc75619SDan Williams  * +---------------------+
906bc75619SDan Williams  *
916bc75619SDan Williams  * *) A NFIT-table may describe a simple system-physical-address range
926bc75619SDan Williams  *    with no BLK aliasing.  This type of region may optionally
936bc75619SDan Williams  *    reference an NVDIMM.
946bc75619SDan Williams  */
956bc75619SDan Williams enum {
9620985164SVishal Verma 	NUM_PM  = 3,
9720985164SVishal Verma 	NUM_DCR = 5,
9885d3fa02SDan Williams 	NUM_HINTS = 8,
996bc75619SDan Williams 	NUM_BDW = NUM_DCR,
1006bc75619SDan Williams 	NUM_SPA = NUM_PM + NUM_DCR + NUM_BDW,
1019741a559SRoss Zwisler 	NUM_MEM = NUM_DCR + NUM_BDW + 2 /* spa0 iset */
1029741a559SRoss Zwisler 		+ 4 /* spa1 iset */ + 1 /* spa11 iset */,
1036bc75619SDan Williams 	DIMM_SIZE = SZ_32M,
1046bc75619SDan Williams 	LABEL_SIZE = SZ_128K,
1057bfe97c7SDan Williams 	SPA_VCD_SIZE = SZ_4M,
1066bc75619SDan Williams 	SPA0_SIZE = DIMM_SIZE,
1076bc75619SDan Williams 	SPA1_SIZE = DIMM_SIZE*2,
1086bc75619SDan Williams 	SPA2_SIZE = DIMM_SIZE,
1096bc75619SDan Williams 	BDW_SIZE = 64 << 8,
1106bc75619SDan Williams 	DCR_SIZE = 12,
1116bc75619SDan Williams 	NUM_NFITS = 2, /* permit testing multiple NFITs per system */
1126bc75619SDan Williams };
1136bc75619SDan Williams 
1146bc75619SDan Williams struct nfit_test_dcr {
1156bc75619SDan Williams 	__le64 bdw_addr;
1166bc75619SDan Williams 	__le32 bdw_status;
1176bc75619SDan Williams 	__u8 aperature[BDW_SIZE];
1186bc75619SDan Williams };
1196bc75619SDan Williams 
1206bc75619SDan Williams #define NFIT_DIMM_HANDLE(node, socket, imc, chan, dimm) \
1216bc75619SDan Williams 	(((node & 0xfff) << 16) | ((socket & 0xf) << 12) \
1226bc75619SDan Williams 	 | ((imc & 0xf) << 8) | ((chan & 0xf) << 4) | (dimm & 0xf))
1236bc75619SDan Williams 
124dafb1048SDan Williams static u32 handle[] = {
1256bc75619SDan Williams 	[0] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 0),
1266bc75619SDan Williams 	[1] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 1),
1276bc75619SDan Williams 	[2] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 0),
1286bc75619SDan Williams 	[3] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 1),
12920985164SVishal Verma 	[4] = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
130dafb1048SDan Williams 	[5] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 0),
131ac40b675SDan Williams 	[6] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 1),
1326bc75619SDan Williams };
1336bc75619SDan Williams 
134af31b04bSMasayoshi Mizuma static unsigned long dimm_fail_cmd_flags[ARRAY_SIZE(handle)];
135af31b04bSMasayoshi Mizuma static int dimm_fail_cmd_code[ARRAY_SIZE(handle)];
1363c13e2acSDave Jiang struct nfit_test_sec {
1373c13e2acSDave Jiang 	u8 state;
138ecaa4a97SDave Jiang 	u8 ext_state;
1392170a0d5SDave Jiang 	u8 old_state;
1403c13e2acSDave Jiang 	u8 passphrase[32];
141ecaa4a97SDave Jiang 	u8 master_passphrase[32];
142926f7480SDave Jiang 	u64 overwrite_end_time;
1433c13e2acSDave Jiang } dimm_sec_info[NUM_DCR];
14473606afdSDan Williams 
145b4d4702fSVishal Verma static const struct nd_intel_smart smart_def = {
146b4d4702fSVishal Verma 	.flags = ND_INTEL_SMART_HEALTH_VALID
147b4d4702fSVishal Verma 		| ND_INTEL_SMART_SPARES_VALID
148b4d4702fSVishal Verma 		| ND_INTEL_SMART_ALARM_VALID
149b4d4702fSVishal Verma 		| ND_INTEL_SMART_USED_VALID
150b4d4702fSVishal Verma 		| ND_INTEL_SMART_SHUTDOWN_VALID
151f1101766SDan Williams 		| ND_INTEL_SMART_SHUTDOWN_COUNT_VALID
152b4d4702fSVishal Verma 		| ND_INTEL_SMART_MTEMP_VALID
153b4d4702fSVishal Verma 		| ND_INTEL_SMART_CTEMP_VALID,
154b4d4702fSVishal Verma 	.health = ND_INTEL_SMART_NON_CRITICAL_HEALTH,
155b4d4702fSVishal Verma 	.media_temperature = 23 * 16,
156b4d4702fSVishal Verma 	.ctrl_temperature = 25 * 16,
157b4d4702fSVishal Verma 	.pmic_temperature = 40 * 16,
158b4d4702fSVishal Verma 	.spares = 75,
159b4d4702fSVishal Verma 	.alarm_flags = ND_INTEL_SMART_SPARE_TRIP
160b4d4702fSVishal Verma 		| ND_INTEL_SMART_TEMP_TRIP,
161b4d4702fSVishal Verma 	.ait_status = 1,
162b4d4702fSVishal Verma 	.life_used = 5,
163b4d4702fSVishal Verma 	.shutdown_state = 0,
164f1101766SDan Williams 	.shutdown_count = 42,
165b4d4702fSVishal Verma 	.vendor_size = 0,
166b4d4702fSVishal Verma };
167b4d4702fSVishal Verma 
168bfbaa952SDave Jiang struct nfit_test_fw {
169bfbaa952SDave Jiang 	enum intel_fw_update_state state;
170bfbaa952SDave Jiang 	u32 context;
171bfbaa952SDave Jiang 	u64 version;
172bfbaa952SDave Jiang 	u32 size_received;
173bfbaa952SDave Jiang 	u64 end_time;
174916566aeSDan Williams 	bool armed;
175916566aeSDan Williams 	bool missed_activate;
176916566aeSDan Williams 	unsigned long last_activate;
177bfbaa952SDave Jiang };
178bfbaa952SDave Jiang 
1796bc75619SDan Williams struct nfit_test {
1806bc75619SDan Williams 	struct acpi_nfit_desc acpi_desc;
1816bc75619SDan Williams 	struct platform_device pdev;
1826bc75619SDan Williams 	struct list_head resources;
1836bc75619SDan Williams 	void *nfit_buf;
1846bc75619SDan Williams 	dma_addr_t nfit_dma;
1856bc75619SDan Williams 	size_t nfit_size;
1861526f9e2SRoss Zwisler 	size_t nfit_filled;
187dafb1048SDan Williams 	int dcr_idx;
1886bc75619SDan Williams 	int num_dcr;
1896bc75619SDan Williams 	int num_pm;
1906bc75619SDan Williams 	void **dimm;
1916bc75619SDan Williams 	dma_addr_t *dimm_dma;
1929d27a87eSDan Williams 	void **flush;
1939d27a87eSDan Williams 	dma_addr_t *flush_dma;
1946bc75619SDan Williams 	void **label;
1956bc75619SDan Williams 	dma_addr_t *label_dma;
1966bc75619SDan Williams 	void **spa_set;
1976bc75619SDan Williams 	dma_addr_t *spa_set_dma;
1986bc75619SDan Williams 	struct nfit_test_dcr **dcr;
1996bc75619SDan Williams 	dma_addr_t *dcr_dma;
2006bc75619SDan Williams 	int (*alloc)(struct nfit_test *t);
2016bc75619SDan Williams 	void (*setup)(struct nfit_test *t);
20220985164SVishal Verma 	int setup_hotplug;
203c14a868aSDan Williams 	union acpi_object **_fit;
204c14a868aSDan Williams 	dma_addr_t _fit_dma;
205f471f1a7SDan Williams 	struct ars_state {
206f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status;
207f471f1a7SDan Williams 		unsigned long deadline;
208f471f1a7SDan Williams 		spinlock_t lock;
209f471f1a7SDan Williams 	} ars_state;
210af31b04bSMasayoshi Mizuma 	struct device *dimm_dev[ARRAY_SIZE(handle)];
211ed07c433SDan Williams 	struct nd_intel_smart *smart;
212ed07c433SDan Williams 	struct nd_intel_smart_threshold *smart_threshold;
2139fb1a190SDave Jiang 	struct badrange badrange;
2149fb1a190SDave Jiang 	struct work_struct work;
215bfbaa952SDave Jiang 	struct nfit_test_fw *fw;
2166bc75619SDan Williams };
2176bc75619SDan Williams 
2189fb1a190SDave Jiang static struct workqueue_struct *nfit_wq;
2199fb1a190SDave Jiang 
220e3f5df76SDan Williams static struct gen_pool *nfit_pool;
221e3f5df76SDan Williams 
222037c8489SDave Jiang static const char zero_key[NVDIMM_PASSPHRASE_LEN];
223037c8489SDave Jiang 
to_nfit_test(struct device * dev)2246bc75619SDan Williams static struct nfit_test *to_nfit_test(struct device *dev)
2256bc75619SDan Williams {
2266bc75619SDan Williams 	struct platform_device *pdev = to_platform_device(dev);
2276bc75619SDan Williams 
2286bc75619SDan Williams 	return container_of(pdev, struct nfit_test, pdev);
2296bc75619SDan Williams }
2306bc75619SDan Williams 
nd_intel_test_get_fw_info(struct nfit_test * t,struct nd_intel_fw_info * nd_cmd,unsigned int buf_len,int idx)231bfbaa952SDave Jiang static int nd_intel_test_get_fw_info(struct nfit_test *t,
232bfbaa952SDave Jiang 		struct nd_intel_fw_info *nd_cmd, unsigned int buf_len,
233bfbaa952SDave Jiang 		int idx)
234bfbaa952SDave Jiang {
235bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
236bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
237bfbaa952SDave Jiang 
238bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p, buf_len: %u, idx: %d\n",
239bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
240bfbaa952SDave Jiang 
241bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
242bfbaa952SDave Jiang 		return -EINVAL;
243bfbaa952SDave Jiang 
244bfbaa952SDave Jiang 	nd_cmd->status = 0;
245bfbaa952SDave Jiang 	nd_cmd->storage_size = INTEL_FW_STORAGE_SIZE;
246bfbaa952SDave Jiang 	nd_cmd->max_send_len = INTEL_FW_MAX_SEND_LEN;
247bfbaa952SDave Jiang 	nd_cmd->query_interval = INTEL_FW_QUERY_INTERVAL;
248bfbaa952SDave Jiang 	nd_cmd->max_query_time = INTEL_FW_QUERY_MAX_TIME;
249bfbaa952SDave Jiang 	nd_cmd->update_cap = 0;
250bfbaa952SDave Jiang 	nd_cmd->fis_version = INTEL_FW_FIS_VERSION;
251bfbaa952SDave Jiang 	nd_cmd->run_version = 0;
252bfbaa952SDave Jiang 	nd_cmd->updated_version = fw->version;
253bfbaa952SDave Jiang 
254bfbaa952SDave Jiang 	return 0;
255bfbaa952SDave Jiang }
256bfbaa952SDave Jiang 
nd_intel_test_start_update(struct nfit_test * t,struct nd_intel_fw_start * nd_cmd,unsigned int buf_len,int idx)257bfbaa952SDave Jiang static int nd_intel_test_start_update(struct nfit_test *t,
258bfbaa952SDave Jiang 		struct nd_intel_fw_start *nd_cmd, unsigned int buf_len,
259bfbaa952SDave Jiang 		int idx)
260bfbaa952SDave Jiang {
261bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
262bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
263bfbaa952SDave Jiang 
264bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
265bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
266bfbaa952SDave Jiang 
267bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
268bfbaa952SDave Jiang 		return -EINVAL;
269bfbaa952SDave Jiang 
270bfbaa952SDave Jiang 	if (fw->state != FW_STATE_NEW) {
271bfbaa952SDave Jiang 		/* extended status, FW update in progress */
272bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
273bfbaa952SDave Jiang 		return 0;
274bfbaa952SDave Jiang 	}
275bfbaa952SDave Jiang 
276bfbaa952SDave Jiang 	fw->state = FW_STATE_IN_PROGRESS;
277bfbaa952SDave Jiang 	fw->context++;
278bfbaa952SDave Jiang 	fw->size_received = 0;
279bfbaa952SDave Jiang 	nd_cmd->status = 0;
280bfbaa952SDave Jiang 	nd_cmd->context = fw->context;
281bfbaa952SDave Jiang 
282bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context issued: %#x\n", __func__, nd_cmd->context);
283bfbaa952SDave Jiang 
284bfbaa952SDave Jiang 	return 0;
285bfbaa952SDave Jiang }
286bfbaa952SDave Jiang 
nd_intel_test_send_data(struct nfit_test * t,struct nd_intel_fw_send_data * nd_cmd,unsigned int buf_len,int idx)287bfbaa952SDave Jiang static int nd_intel_test_send_data(struct nfit_test *t,
288bfbaa952SDave Jiang 		struct nd_intel_fw_send_data *nd_cmd, unsigned int buf_len,
289bfbaa952SDave Jiang 		int idx)
290bfbaa952SDave Jiang {
291bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
292bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
293bfbaa952SDave Jiang 	u32 *status = (u32 *)&nd_cmd->data[nd_cmd->length];
294bfbaa952SDave Jiang 
295bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
296bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
297bfbaa952SDave Jiang 
298bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
299bfbaa952SDave Jiang 		return -EINVAL;
300bfbaa952SDave Jiang 
301bfbaa952SDave Jiang 
302bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->status: %#x\n", __func__, *status);
303bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[0]: %#x\n", __func__, nd_cmd->data[0]);
304bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[%u]: %#x\n", __func__, nd_cmd->length-1,
305bfbaa952SDave Jiang 			nd_cmd->data[nd_cmd->length-1]);
306bfbaa952SDave Jiang 
307bfbaa952SDave Jiang 	if (fw->state != FW_STATE_IN_PROGRESS) {
308bfbaa952SDave Jiang 		dev_dbg(dev, "%s: not in IN_PROGRESS state\n", __func__);
309bfbaa952SDave Jiang 		*status = 0x5;
310bfbaa952SDave Jiang 		return 0;
311bfbaa952SDave Jiang 	}
312bfbaa952SDave Jiang 
313bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
314bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
315bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
316bfbaa952SDave Jiang 		*status = 0x10007;
317bfbaa952SDave Jiang 		return 0;
318bfbaa952SDave Jiang 	}
319bfbaa952SDave Jiang 
320bfbaa952SDave Jiang 	/*
321bfbaa952SDave Jiang 	 * check offset + len > size of fw storage
322bfbaa952SDave Jiang 	 * check length is > max send length
323bfbaa952SDave Jiang 	 */
324bfbaa952SDave Jiang 	if (nd_cmd->offset + nd_cmd->length > INTEL_FW_STORAGE_SIZE ||
325bfbaa952SDave Jiang 			nd_cmd->length > INTEL_FW_MAX_SEND_LEN) {
326bfbaa952SDave Jiang 		*status = 0x3;
327bfbaa952SDave Jiang 		dev_dbg(dev, "%s: buffer boundary violation\n", __func__);
328bfbaa952SDave Jiang 		return 0;
329bfbaa952SDave Jiang 	}
330bfbaa952SDave Jiang 
331bfbaa952SDave Jiang 	fw->size_received += nd_cmd->length;
332bfbaa952SDave Jiang 	dev_dbg(dev, "%s: copying %u bytes, %u bytes so far\n",
333bfbaa952SDave Jiang 			__func__, nd_cmd->length, fw->size_received);
334bfbaa952SDave Jiang 	*status = 0;
335bfbaa952SDave Jiang 	return 0;
336bfbaa952SDave Jiang }
337bfbaa952SDave Jiang 
nd_intel_test_finish_fw(struct nfit_test * t,struct nd_intel_fw_finish_update * nd_cmd,unsigned int buf_len,int idx)338bfbaa952SDave Jiang static int nd_intel_test_finish_fw(struct nfit_test *t,
339bfbaa952SDave Jiang 		struct nd_intel_fw_finish_update *nd_cmd,
340bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
341bfbaa952SDave Jiang {
342bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
343bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
344bfbaa952SDave Jiang 
345bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
346bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
347bfbaa952SDave Jiang 
348bfbaa952SDave Jiang 	if (fw->state == FW_STATE_UPDATED) {
349916566aeSDan Williams 		/* update already done, need activation */
350bfbaa952SDave Jiang 		nd_cmd->status = 0x20007;
351bfbaa952SDave Jiang 		return 0;
352bfbaa952SDave Jiang 	}
353bfbaa952SDave Jiang 
354bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context: %#x  ctrl_flags: %#x\n",
355bfbaa952SDave Jiang 			__func__, nd_cmd->context, nd_cmd->ctrl_flags);
356bfbaa952SDave Jiang 
357bfbaa952SDave Jiang 	switch (nd_cmd->ctrl_flags) {
358bfbaa952SDave Jiang 	case 0: /* finish */
359bfbaa952SDave Jiang 		if (nd_cmd->context != fw->context) {
360bfbaa952SDave Jiang 			dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
361bfbaa952SDave Jiang 					__func__, nd_cmd->context,
362bfbaa952SDave Jiang 					fw->context);
363bfbaa952SDave Jiang 			nd_cmd->status = 0x10007;
364bfbaa952SDave Jiang 			return 0;
365bfbaa952SDave Jiang 		}
366bfbaa952SDave Jiang 		nd_cmd->status = 0;
367bfbaa952SDave Jiang 		fw->state = FW_STATE_VERIFY;
368bfbaa952SDave Jiang 		/* set 1 second of time for firmware "update" */
369bfbaa952SDave Jiang 		fw->end_time = jiffies + HZ;
370bfbaa952SDave Jiang 		break;
371bfbaa952SDave Jiang 
372bfbaa952SDave Jiang 	case 1: /* abort */
373bfbaa952SDave Jiang 		fw->size_received = 0;
374bfbaa952SDave Jiang 		/* successfully aborted status */
375bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
376bfbaa952SDave Jiang 		fw->state = FW_STATE_NEW;
377bfbaa952SDave Jiang 		dev_dbg(dev, "%s: abort successful\n", __func__);
378bfbaa952SDave Jiang 		break;
379bfbaa952SDave Jiang 
380bfbaa952SDave Jiang 	default: /* bad control flag */
381bfbaa952SDave Jiang 		dev_warn(dev, "%s: unknown control flag: %#x\n",
382bfbaa952SDave Jiang 				__func__, nd_cmd->ctrl_flags);
383bfbaa952SDave Jiang 		return -EINVAL;
384bfbaa952SDave Jiang 	}
385bfbaa952SDave Jiang 
386bfbaa952SDave Jiang 	return 0;
387bfbaa952SDave Jiang }
388bfbaa952SDave Jiang 
nd_intel_test_finish_query(struct nfit_test * t,struct nd_intel_fw_finish_query * nd_cmd,unsigned int buf_len,int idx)389bfbaa952SDave Jiang static int nd_intel_test_finish_query(struct nfit_test *t,
390bfbaa952SDave Jiang 		struct nd_intel_fw_finish_query *nd_cmd,
391bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
392bfbaa952SDave Jiang {
393bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
394bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
395bfbaa952SDave Jiang 
396bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
397bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
398bfbaa952SDave Jiang 
399bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
400bfbaa952SDave Jiang 		return -EINVAL;
401bfbaa952SDave Jiang 
402bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
403bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
404bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
405bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
406bfbaa952SDave Jiang 		return 0;
407bfbaa952SDave Jiang 	}
408bfbaa952SDave Jiang 
409bfbaa952SDave Jiang 	dev_dbg(dev, "%s context: %#x\n", __func__, nd_cmd->context);
410bfbaa952SDave Jiang 
411bfbaa952SDave Jiang 	switch (fw->state) {
412bfbaa952SDave Jiang 	case FW_STATE_NEW:
413bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
414bfbaa952SDave Jiang 		nd_cmd->status = 0;
415bfbaa952SDave Jiang 		dev_dbg(dev, "%s: new state\n", __func__);
416bfbaa952SDave Jiang 		break;
417bfbaa952SDave Jiang 
418bfbaa952SDave Jiang 	case FW_STATE_IN_PROGRESS:
419bfbaa952SDave Jiang 		/* sequencing error */
420bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
421bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
422bfbaa952SDave Jiang 		dev_dbg(dev, "%s: sequence error\n", __func__);
423bfbaa952SDave Jiang 		break;
424bfbaa952SDave Jiang 
425bfbaa952SDave Jiang 	case FW_STATE_VERIFY:
426bfbaa952SDave Jiang 		if (time_is_after_jiffies64(fw->end_time)) {
427bfbaa952SDave Jiang 			nd_cmd->updated_fw_rev = 0;
428bfbaa952SDave Jiang 			nd_cmd->status = 0x20007;
429bfbaa952SDave Jiang 			dev_dbg(dev, "%s: still verifying\n", __func__);
430bfbaa952SDave Jiang 			break;
431bfbaa952SDave Jiang 		}
432bfbaa952SDave Jiang 		dev_dbg(dev, "%s: transition out verify\n", __func__);
433bfbaa952SDave Jiang 		fw->state = FW_STATE_UPDATED;
434916566aeSDan Williams 		fw->missed_activate = false;
435f21453b0SDan Williams 		fallthrough;
436bfbaa952SDave Jiang 	case FW_STATE_UPDATED:
437bfbaa952SDave Jiang 		nd_cmd->status = 0;
438bfbaa952SDave Jiang 		/* bogus test version */
439bfbaa952SDave Jiang 		fw->version = nd_cmd->updated_fw_rev =
440bfbaa952SDave Jiang 			INTEL_FW_FAKE_VERSION;
441bfbaa952SDave Jiang 		dev_dbg(dev, "%s: updated\n", __func__);
442bfbaa952SDave Jiang 		break;
443bfbaa952SDave Jiang 
444bfbaa952SDave Jiang 	default: /* we should never get here */
445bfbaa952SDave Jiang 		return -EINVAL;
446bfbaa952SDave Jiang 	}
447bfbaa952SDave Jiang 
448bfbaa952SDave Jiang 	return 0;
449bfbaa952SDave Jiang }
450bfbaa952SDave Jiang 
nfit_test_cmd_get_config_size(struct nd_cmd_get_config_size * nd_cmd,unsigned int buf_len)45139c686b8SVishal Verma static int nfit_test_cmd_get_config_size(struct nd_cmd_get_config_size *nd_cmd,
4526bc75619SDan Williams 		unsigned int buf_len)
4536bc75619SDan Williams {
4546bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4556bc75619SDan Williams 		return -EINVAL;
45639c686b8SVishal Verma 
4576bc75619SDan Williams 	nd_cmd->status = 0;
4586bc75619SDan Williams 	nd_cmd->config_size = LABEL_SIZE;
4596bc75619SDan Williams 	nd_cmd->max_xfer = SZ_4K;
46039c686b8SVishal Verma 
46139c686b8SVishal Verma 	return 0;
4626bc75619SDan Williams }
46339c686b8SVishal Verma 
nfit_test_cmd_get_config_data(struct nd_cmd_get_config_data_hdr * nd_cmd,unsigned int buf_len,void * label)46439c686b8SVishal Verma static int nfit_test_cmd_get_config_data(struct nd_cmd_get_config_data_hdr
46539c686b8SVishal Verma 		*nd_cmd, unsigned int buf_len, void *label)
46639c686b8SVishal Verma {
4676bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
46839c686b8SVishal Verma 	int rc;
4696bc75619SDan Williams 
4706bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4716bc75619SDan Williams 		return -EINVAL;
4726bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4736bc75619SDan Williams 		return -EINVAL;
4746bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) > buf_len)
4756bc75619SDan Williams 		return -EINVAL;
4766bc75619SDan Williams 
4776bc75619SDan Williams 	nd_cmd->status = 0;
4786bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
47939c686b8SVishal Verma 	memcpy(nd_cmd->out_buf, label + offset, len);
4806bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - len;
48139c686b8SVishal Verma 
48239c686b8SVishal Verma 	return rc;
4836bc75619SDan Williams }
48439c686b8SVishal Verma 
nfit_test_cmd_set_config_data(struct nd_cmd_set_config_hdr * nd_cmd,unsigned int buf_len,void * label)48539c686b8SVishal Verma static int nfit_test_cmd_set_config_data(struct nd_cmd_set_config_hdr *nd_cmd,
48639c686b8SVishal Verma 		unsigned int buf_len, void *label)
48739c686b8SVishal Verma {
4886bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
4896bc75619SDan Williams 	u32 *status;
49039c686b8SVishal Verma 	int rc;
4916bc75619SDan Williams 
4926bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4936bc75619SDan Williams 		return -EINVAL;
4946bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4956bc75619SDan Williams 		return -EINVAL;
4966bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) + 4 > buf_len)
4976bc75619SDan Williams 		return -EINVAL;
4986bc75619SDan Williams 
49939c686b8SVishal Verma 	status = (void *)nd_cmd + nd_cmd->in_length + sizeof(*nd_cmd);
5006bc75619SDan Williams 	*status = 0;
5016bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
50239c686b8SVishal Verma 	memcpy(label + offset, nd_cmd->in_buf, len);
5036bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - (len + 4);
50439c686b8SVishal Verma 
50539c686b8SVishal Verma 	return rc;
5066bc75619SDan Williams }
50739c686b8SVishal Verma 
508d4f32367SDan Williams #define NFIT_TEST_CLEAR_ERR_UNIT 256
509747ffe11SDan Williams 
nfit_test_cmd_ars_cap(struct nd_cmd_ars_cap * nd_cmd,unsigned int buf_len)51039c686b8SVishal Verma static int nfit_test_cmd_ars_cap(struct nd_cmd_ars_cap *nd_cmd,
51139c686b8SVishal Verma 		unsigned int buf_len)
51239c686b8SVishal Verma {
5139fb1a190SDave Jiang 	int ars_recs;
5149fb1a190SDave Jiang 
51539c686b8SVishal Verma 	if (buf_len < sizeof(*nd_cmd))
51639c686b8SVishal Verma 		return -EINVAL;
51739c686b8SVishal Verma 
5189fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
5199fb1a190SDave Jiang 	ars_recs = SZ_4K / sizeof(struct nd_ars_record);
5209fb1a190SDave Jiang 
521747ffe11SDan Williams 	nd_cmd->max_ars_out = sizeof(struct nd_cmd_ars_status)
5229fb1a190SDave Jiang 		+ ars_recs * sizeof(struct nd_ars_record);
52339c686b8SVishal Verma 	nd_cmd->status = (ND_ARS_PERSISTENT | ND_ARS_VOLATILE) << 16;
524d4f32367SDan Williams 	nd_cmd->clear_err_unit = NFIT_TEST_CLEAR_ERR_UNIT;
52539c686b8SVishal Verma 
52639c686b8SVishal Verma 	return 0;
52739c686b8SVishal Verma }
52839c686b8SVishal Verma 
post_ars_status(struct ars_state * ars_state,struct badrange * badrange,u64 addr,u64 len)5299fb1a190SDave Jiang static void post_ars_status(struct ars_state *ars_state,
5309fb1a190SDave Jiang 		struct badrange *badrange, u64 addr, u64 len)
53139c686b8SVishal Verma {
532f471f1a7SDan Williams 	struct nd_cmd_ars_status *ars_status;
533f471f1a7SDan Williams 	struct nd_ars_record *ars_record;
5349fb1a190SDave Jiang 	struct badrange_entry *be;
5359fb1a190SDave Jiang 	u64 end = addr + len - 1;
5369fb1a190SDave Jiang 	int i = 0;
537f471f1a7SDan Williams 
538f471f1a7SDan Williams 	ars_state->deadline = jiffies + 1*HZ;
539f471f1a7SDan Williams 	ars_status = ars_state->ars_status;
540f471f1a7SDan Williams 	ars_status->status = 0;
541f471f1a7SDan Williams 	ars_status->address = addr;
542f471f1a7SDan Williams 	ars_status->length = len;
543f471f1a7SDan Williams 	ars_status->type = ND_ARS_PERSISTENT;
5449fb1a190SDave Jiang 
5459fb1a190SDave Jiang 	spin_lock(&badrange->lock);
5469fb1a190SDave Jiang 	list_for_each_entry(be, &badrange->list, list) {
5479fb1a190SDave Jiang 		u64 be_end = be->start + be->length - 1;
5489fb1a190SDave Jiang 		u64 rstart, rend;
5499fb1a190SDave Jiang 
5509fb1a190SDave Jiang 		/* skip entries outside the range */
5519fb1a190SDave Jiang 		if (be_end < addr || be->start > end)
5529fb1a190SDave Jiang 			continue;
5539fb1a190SDave Jiang 
5549fb1a190SDave Jiang 		rstart = (be->start < addr) ? addr : be->start;
5559fb1a190SDave Jiang 		rend = (be_end < end) ? be_end : end;
5569fb1a190SDave Jiang 		ars_record = &ars_status->records[i];
557f471f1a7SDan Williams 		ars_record->handle = 0;
5589fb1a190SDave Jiang 		ars_record->err_address = rstart;
5599fb1a190SDave Jiang 		ars_record->length = rend - rstart + 1;
5609fb1a190SDave Jiang 		i++;
5619fb1a190SDave Jiang 	}
5629fb1a190SDave Jiang 	spin_unlock(&badrange->lock);
5639fb1a190SDave Jiang 	ars_status->num_records = i;
5649fb1a190SDave Jiang 	ars_status->out_length = sizeof(struct nd_cmd_ars_status)
5659fb1a190SDave Jiang 		+ i * sizeof(struct nd_ars_record);
566f471f1a7SDan Williams }
567f471f1a7SDan Williams 
nfit_test_cmd_ars_start(struct nfit_test * t,struct ars_state * ars_state,struct nd_cmd_ars_start * ars_start,unsigned int buf_len,int * cmd_rc)5689fb1a190SDave Jiang static int nfit_test_cmd_ars_start(struct nfit_test *t,
5699fb1a190SDave Jiang 		struct ars_state *ars_state,
570f471f1a7SDan Williams 		struct nd_cmd_ars_start *ars_start, unsigned int buf_len,
571f471f1a7SDan Williams 		int *cmd_rc)
572f471f1a7SDan Williams {
573f471f1a7SDan Williams 	if (buf_len < sizeof(*ars_start))
57439c686b8SVishal Verma 		return -EINVAL;
57539c686b8SVishal Verma 
576f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
577f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
578f471f1a7SDan Williams 		ars_start->status = NFIT_ARS_START_BUSY;
579f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
580f471f1a7SDan Williams 	} else {
581f471f1a7SDan Williams 		ars_start->status = 0;
582f471f1a7SDan Williams 		ars_start->scrub_time = 1;
5839fb1a190SDave Jiang 		post_ars_status(ars_state, &t->badrange, ars_start->address,
584f471f1a7SDan Williams 				ars_start->length);
585f471f1a7SDan Williams 		*cmd_rc = 0;
586f471f1a7SDan Williams 	}
587f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
58839c686b8SVishal Verma 
58939c686b8SVishal Verma 	return 0;
59039c686b8SVishal Verma }
59139c686b8SVishal Verma 
nfit_test_cmd_ars_status(struct ars_state * ars_state,struct nd_cmd_ars_status * ars_status,unsigned int buf_len,int * cmd_rc)592f471f1a7SDan Williams static int nfit_test_cmd_ars_status(struct ars_state *ars_state,
593f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status, unsigned int buf_len,
594f471f1a7SDan Williams 		int *cmd_rc)
59539c686b8SVishal Verma {
596f471f1a7SDan Williams 	if (buf_len < ars_state->ars_status->out_length)
59739c686b8SVishal Verma 		return -EINVAL;
59839c686b8SVishal Verma 
599f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
600f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
601f471f1a7SDan Williams 		memset(ars_status, 0, buf_len);
602f471f1a7SDan Williams 		ars_status->status = NFIT_ARS_STATUS_BUSY;
603f471f1a7SDan Williams 		ars_status->out_length = sizeof(*ars_status);
604f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
605f471f1a7SDan Williams 	} else {
606f471f1a7SDan Williams 		memcpy(ars_status, ars_state->ars_status,
607f471f1a7SDan Williams 				ars_state->ars_status->out_length);
608f471f1a7SDan Williams 		*cmd_rc = 0;
609f471f1a7SDan Williams 	}
610f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
61139c686b8SVishal Verma 	return 0;
61239c686b8SVishal Verma }
61339c686b8SVishal Verma 
nfit_test_cmd_clear_error(struct nfit_test * t,struct nd_cmd_clear_error * clear_err,unsigned int buf_len,int * cmd_rc)6145e096ef3SVishal Verma static int nfit_test_cmd_clear_error(struct nfit_test *t,
6155e096ef3SVishal Verma 		struct nd_cmd_clear_error *clear_err,
616d4f32367SDan Williams 		unsigned int buf_len, int *cmd_rc)
617d4f32367SDan Williams {
618d4f32367SDan Williams 	const u64 mask = NFIT_TEST_CLEAR_ERR_UNIT - 1;
619d4f32367SDan Williams 	if (buf_len < sizeof(*clear_err))
620d4f32367SDan Williams 		return -EINVAL;
621d4f32367SDan Williams 
622d4f32367SDan Williams 	if ((clear_err->address & mask) || (clear_err->length & mask))
623d4f32367SDan Williams 		return -EINVAL;
624d4f32367SDan Williams 
6255e096ef3SVishal Verma 	badrange_forget(&t->badrange, clear_err->address, clear_err->length);
626d4f32367SDan Williams 	clear_err->status = 0;
627d4f32367SDan Williams 	clear_err->cleared = clear_err->length;
628d4f32367SDan Williams 	*cmd_rc = 0;
629d4f32367SDan Williams 	return 0;
630d4f32367SDan Williams }
631d4f32367SDan Williams 
63210246dc8SYasunori Goto struct region_search_spa {
63310246dc8SYasunori Goto 	u64 addr;
63410246dc8SYasunori Goto 	struct nd_region *region;
63510246dc8SYasunori Goto };
63610246dc8SYasunori Goto 
is_region_device(struct device * dev)63710246dc8SYasunori Goto static int is_region_device(struct device *dev)
63810246dc8SYasunori Goto {
63910246dc8SYasunori Goto 	return !strncmp(dev->kobj.name, "region", 6);
64010246dc8SYasunori Goto }
64110246dc8SYasunori Goto 
nfit_test_search_region_spa(struct device * dev,void * data)64210246dc8SYasunori Goto static int nfit_test_search_region_spa(struct device *dev, void *data)
64310246dc8SYasunori Goto {
64410246dc8SYasunori Goto 	struct region_search_spa *ctx = data;
64510246dc8SYasunori Goto 	struct nd_region *nd_region;
64610246dc8SYasunori Goto 	resource_size_t ndr_end;
64710246dc8SYasunori Goto 
64810246dc8SYasunori Goto 	if (!is_region_device(dev))
64910246dc8SYasunori Goto 		return 0;
65010246dc8SYasunori Goto 
65110246dc8SYasunori Goto 	nd_region = to_nd_region(dev);
65210246dc8SYasunori Goto 	ndr_end = nd_region->ndr_start + nd_region->ndr_size;
65310246dc8SYasunori Goto 
65410246dc8SYasunori Goto 	if (ctx->addr >= nd_region->ndr_start && ctx->addr < ndr_end) {
65510246dc8SYasunori Goto 		ctx->region = nd_region;
65610246dc8SYasunori Goto 		return 1;
65710246dc8SYasunori Goto 	}
65810246dc8SYasunori Goto 
65910246dc8SYasunori Goto 	return 0;
66010246dc8SYasunori Goto }
66110246dc8SYasunori Goto 
nfit_test_search_spa(struct nvdimm_bus * bus,struct nd_cmd_translate_spa * spa)66210246dc8SYasunori Goto static int nfit_test_search_spa(struct nvdimm_bus *bus,
66310246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa)
66410246dc8SYasunori Goto {
66510246dc8SYasunori Goto 	int ret;
66610246dc8SYasunori Goto 	struct nd_region *nd_region = NULL;
66710246dc8SYasunori Goto 	struct nvdimm *nvdimm = NULL;
66810246dc8SYasunori Goto 	struct nd_mapping *nd_mapping = NULL;
66910246dc8SYasunori Goto 	struct region_search_spa ctx = {
67010246dc8SYasunori Goto 		.addr = spa->spa,
67110246dc8SYasunori Goto 		.region = NULL,
67210246dc8SYasunori Goto 	};
67310246dc8SYasunori Goto 	u64 dpa;
67410246dc8SYasunori Goto 
67510246dc8SYasunori Goto 	ret = device_for_each_child(&bus->dev, &ctx,
67610246dc8SYasunori Goto 				nfit_test_search_region_spa);
67710246dc8SYasunori Goto 
67810246dc8SYasunori Goto 	if (!ret)
67910246dc8SYasunori Goto 		return -ENODEV;
68010246dc8SYasunori Goto 
68110246dc8SYasunori Goto 	nd_region = ctx.region;
68210246dc8SYasunori Goto 
68310246dc8SYasunori Goto 	dpa = ctx.addr - nd_region->ndr_start;
68410246dc8SYasunori Goto 
68510246dc8SYasunori Goto 	/*
68610246dc8SYasunori Goto 	 * last dimm is selected for test
68710246dc8SYasunori Goto 	 */
68810246dc8SYasunori Goto 	nd_mapping = &nd_region->mapping[nd_region->ndr_mappings - 1];
68910246dc8SYasunori Goto 	nvdimm = nd_mapping->nvdimm;
69010246dc8SYasunori Goto 
69110246dc8SYasunori Goto 	spa->devices[0].nfit_device_handle = handle[nvdimm->id];
69210246dc8SYasunori Goto 	spa->num_nvdimms = 1;
69310246dc8SYasunori Goto 	spa->devices[0].dpa = dpa;
69410246dc8SYasunori Goto 
69510246dc8SYasunori Goto 	return 0;
69610246dc8SYasunori Goto }
69710246dc8SYasunori Goto 
nfit_test_cmd_translate_spa(struct nvdimm_bus * bus,struct nd_cmd_translate_spa * spa,unsigned int buf_len)69810246dc8SYasunori Goto static int nfit_test_cmd_translate_spa(struct nvdimm_bus *bus,
69910246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa, unsigned int buf_len)
70010246dc8SYasunori Goto {
70110246dc8SYasunori Goto 	if (buf_len < spa->translate_length)
70210246dc8SYasunori Goto 		return -EINVAL;
70310246dc8SYasunori Goto 
70410246dc8SYasunori Goto 	if (nfit_test_search_spa(bus, spa) < 0 || !spa->num_nvdimms)
70510246dc8SYasunori Goto 		spa->status = 2;
70610246dc8SYasunori Goto 
70710246dc8SYasunori Goto 	return 0;
70810246dc8SYasunori Goto }
70910246dc8SYasunori Goto 
nfit_test_cmd_smart(struct nd_intel_smart * smart,unsigned int buf_len,struct nd_intel_smart * smart_data)710ed07c433SDan Williams static int nfit_test_cmd_smart(struct nd_intel_smart *smart, unsigned int buf_len,
711ed07c433SDan Williams 		struct nd_intel_smart *smart_data)
712baa51277SDan Williams {
713baa51277SDan Williams 	if (buf_len < sizeof(*smart))
714baa51277SDan Williams 		return -EINVAL;
715ed07c433SDan Williams 	memcpy(smart, smart_data, sizeof(*smart));
716baa51277SDan Williams 	return 0;
717baa51277SDan Williams }
718baa51277SDan Williams 
nfit_test_cmd_smart_threshold(struct nd_intel_smart_threshold * out,unsigned int buf_len,struct nd_intel_smart_threshold * smart_t)719cdd77d3eSDan Williams static int nfit_test_cmd_smart_threshold(
720ed07c433SDan Williams 		struct nd_intel_smart_threshold *out,
721ed07c433SDan Williams 		unsigned int buf_len,
722ed07c433SDan Williams 		struct nd_intel_smart_threshold *smart_t)
723baa51277SDan Williams {
724baa51277SDan Williams 	if (buf_len < sizeof(*smart_t))
725baa51277SDan Williams 		return -EINVAL;
726ed07c433SDan Williams 	memcpy(out, smart_t, sizeof(*smart_t));
727ed07c433SDan Williams 	return 0;
728ed07c433SDan Williams }
729ed07c433SDan Williams 
smart_notify(struct device * bus_dev,struct device * dimm_dev,struct nd_intel_smart * smart,struct nd_intel_smart_threshold * thresh)730ed07c433SDan Williams static void smart_notify(struct device *bus_dev,
731ed07c433SDan Williams 		struct device *dimm_dev, struct nd_intel_smart *smart,
732ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh)
733ed07c433SDan Williams {
734ed07c433SDan Williams 	dev_dbg(dimm_dev, "%s: alarm: %#x spares: %d (%d) mtemp: %d (%d) ctemp: %d (%d)\n",
735ed07c433SDan Williams 			__func__, thresh->alarm_control, thresh->spares,
736ed07c433SDan Williams 			smart->spares, thresh->media_temperature,
737ed07c433SDan Williams 			smart->media_temperature, thresh->ctrl_temperature,
738ed07c433SDan Williams 			smart->ctrl_temperature);
739ed07c433SDan Williams 	if (((thresh->alarm_control & ND_INTEL_SMART_SPARE_TRIP)
740ed07c433SDan Williams 				&& smart->spares
741ed07c433SDan Williams 				<= thresh->spares)
742ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_TEMP_TRIP)
743ed07c433SDan Williams 				&& smart->media_temperature
744ed07c433SDan Williams 				>= thresh->media_temperature)
745ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_CTEMP_TRIP)
746ed07c433SDan Williams 				&& smart->ctrl_temperature
7474cf260fcSVishal Verma 				>= thresh->ctrl_temperature)
7484cf260fcSVishal Verma 			|| (smart->health != ND_INTEL_SMART_NON_CRITICAL_HEALTH)
7494cf260fcSVishal Verma 			|| (smart->shutdown_state != 0)) {
750ed07c433SDan Williams 		device_lock(bus_dev);
751ed07c433SDan Williams 		__acpi_nvdimm_notify(dimm_dev, 0x81);
752ed07c433SDan Williams 		device_unlock(bus_dev);
753ed07c433SDan Williams 	}
754ed07c433SDan Williams }
755ed07c433SDan Williams 
nfit_test_cmd_smart_set_threshold(struct nd_intel_smart_set_threshold * in,unsigned int buf_len,struct nd_intel_smart_threshold * thresh,struct nd_intel_smart * smart,struct device * bus_dev,struct device * dimm_dev)756ed07c433SDan Williams static int nfit_test_cmd_smart_set_threshold(
757ed07c433SDan Williams 		struct nd_intel_smart_set_threshold *in,
758ed07c433SDan Williams 		unsigned int buf_len,
759ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh,
760ed07c433SDan Williams 		struct nd_intel_smart *smart,
761ed07c433SDan Williams 		struct device *bus_dev, struct device *dimm_dev)
762ed07c433SDan Williams {
763ed07c433SDan Williams 	unsigned int size;
764ed07c433SDan Williams 
765ed07c433SDan Williams 	size = sizeof(*in) - 4;
766ed07c433SDan Williams 	if (buf_len < size)
767ed07c433SDan Williams 		return -EINVAL;
768ed07c433SDan Williams 	memcpy(thresh->data, in, size);
769ed07c433SDan Williams 	in->status = 0;
770ed07c433SDan Williams 	smart_notify(bus_dev, dimm_dev, smart, thresh);
771ed07c433SDan Williams 
772baa51277SDan Williams 	return 0;
773baa51277SDan Williams }
774baa51277SDan Williams 
nfit_test_cmd_smart_inject(struct nd_intel_smart_inject * inj,unsigned int buf_len,struct nd_intel_smart_threshold * thresh,struct nd_intel_smart * smart,struct device * bus_dev,struct device * dimm_dev)7754cf260fcSVishal Verma static int nfit_test_cmd_smart_inject(
7764cf260fcSVishal Verma 		struct nd_intel_smart_inject *inj,
7774cf260fcSVishal Verma 		unsigned int buf_len,
7784cf260fcSVishal Verma 		struct nd_intel_smart_threshold *thresh,
7794cf260fcSVishal Verma 		struct nd_intel_smart *smart,
7804cf260fcSVishal Verma 		struct device *bus_dev, struct device *dimm_dev)
7814cf260fcSVishal Verma {
7824cf260fcSVishal Verma 	if (buf_len != sizeof(*inj))
7834cf260fcSVishal Verma 		return -EINVAL;
7844cf260fcSVishal Verma 
785b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_MTEMP) {
7864cf260fcSVishal Verma 		if (inj->mtemp_enable)
7874cf260fcSVishal Verma 			smart->media_temperature = inj->media_temperature;
788b4d4702fSVishal Verma 		else
789b4d4702fSVishal Verma 			smart->media_temperature = smart_def.media_temperature;
790b4d4702fSVishal Verma 	}
791b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_SPARE) {
7924cf260fcSVishal Verma 		if (inj->spare_enable)
7934cf260fcSVishal Verma 			smart->spares = inj->spares;
794b4d4702fSVishal Verma 		else
795b4d4702fSVishal Verma 			smart->spares = smart_def.spares;
796b4d4702fSVishal Verma 	}
797b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_FATAL) {
7984cf260fcSVishal Verma 		if (inj->fatal_enable)
7994cf260fcSVishal Verma 			smart->health = ND_INTEL_SMART_FATAL_HEALTH;
800b4d4702fSVishal Verma 		else
801b4d4702fSVishal Verma 			smart->health = ND_INTEL_SMART_NON_CRITICAL_HEALTH;
802b4d4702fSVishal Verma 	}
803b4d4702fSVishal Verma 	if (inj->flags & ND_INTEL_SMART_INJECT_SHUTDOWN) {
8044cf260fcSVishal Verma 		if (inj->unsafe_shutdown_enable) {
8054cf260fcSVishal Verma 			smart->shutdown_state = 1;
8064cf260fcSVishal Verma 			smart->shutdown_count++;
807b4d4702fSVishal Verma 		} else
808b4d4702fSVishal Verma 			smart->shutdown_state = 0;
8094cf260fcSVishal Verma 	}
8104cf260fcSVishal Verma 	inj->status = 0;
8114cf260fcSVishal Verma 	smart_notify(bus_dev, dimm_dev, smart, thresh);
8124cf260fcSVishal Verma 
8134cf260fcSVishal Verma 	return 0;
8144cf260fcSVishal Verma }
8154cf260fcSVishal Verma 
uc_error_notify(struct work_struct * work)8169fb1a190SDave Jiang static void uc_error_notify(struct work_struct *work)
8179fb1a190SDave Jiang {
8189fb1a190SDave Jiang 	struct nfit_test *t = container_of(work, typeof(*t), work);
8199fb1a190SDave Jiang 
8209fb1a190SDave Jiang 	__acpi_nfit_notify(&t->pdev.dev, t, NFIT_NOTIFY_UC_MEMORY_ERROR);
8219fb1a190SDave Jiang }
8229fb1a190SDave Jiang 
nfit_test_cmd_ars_error_inject(struct nfit_test * t,struct nd_cmd_ars_err_inj * err_inj,unsigned int buf_len)8239fb1a190SDave Jiang static int nfit_test_cmd_ars_error_inject(struct nfit_test *t,
8249fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj *err_inj, unsigned int buf_len)
8259fb1a190SDave Jiang {
8269fb1a190SDave Jiang 	int rc;
8279fb1a190SDave Jiang 
82841cb3301SVishal Verma 	if (buf_len != sizeof(*err_inj)) {
8299fb1a190SDave Jiang 		rc = -EINVAL;
8309fb1a190SDave Jiang 		goto err;
8319fb1a190SDave Jiang 	}
8329fb1a190SDave Jiang 
8339fb1a190SDave Jiang 	if (err_inj->err_inj_spa_range_length <= 0) {
8349fb1a190SDave Jiang 		rc = -EINVAL;
8359fb1a190SDave Jiang 		goto err;
8369fb1a190SDave Jiang 	}
8379fb1a190SDave Jiang 
8389fb1a190SDave Jiang 	rc =  badrange_add(&t->badrange, err_inj->err_inj_spa_range_base,
8399fb1a190SDave Jiang 			err_inj->err_inj_spa_range_length);
8409fb1a190SDave Jiang 	if (rc < 0)
8419fb1a190SDave Jiang 		goto err;
8429fb1a190SDave Jiang 
8439fb1a190SDave Jiang 	if (err_inj->err_inj_options & (1 << ND_ARS_ERR_INJ_OPT_NOTIFY))
8449fb1a190SDave Jiang 		queue_work(nfit_wq, &t->work);
8459fb1a190SDave Jiang 
8469fb1a190SDave Jiang 	err_inj->status = 0;
8479fb1a190SDave Jiang 	return 0;
8489fb1a190SDave Jiang 
8499fb1a190SDave Jiang err:
8509fb1a190SDave Jiang 	err_inj->status = NFIT_ARS_INJECT_INVALID;
8519fb1a190SDave Jiang 	return rc;
8529fb1a190SDave Jiang }
8539fb1a190SDave Jiang 
nfit_test_cmd_ars_inject_clear(struct nfit_test * t,struct nd_cmd_ars_err_inj_clr * err_clr,unsigned int buf_len)8549fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_clear(struct nfit_test *t,
8559fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_clr *err_clr, unsigned int buf_len)
8569fb1a190SDave Jiang {
8579fb1a190SDave Jiang 	int rc;
8589fb1a190SDave Jiang 
85941cb3301SVishal Verma 	if (buf_len != sizeof(*err_clr)) {
8609fb1a190SDave Jiang 		rc = -EINVAL;
8619fb1a190SDave Jiang 		goto err;
8629fb1a190SDave Jiang 	}
8639fb1a190SDave Jiang 
8649fb1a190SDave Jiang 	if (err_clr->err_inj_clr_spa_range_length <= 0) {
8659fb1a190SDave Jiang 		rc = -EINVAL;
8669fb1a190SDave Jiang 		goto err;
8679fb1a190SDave Jiang 	}
8689fb1a190SDave Jiang 
8699fb1a190SDave Jiang 	badrange_forget(&t->badrange, err_clr->err_inj_clr_spa_range_base,
8709fb1a190SDave Jiang 			err_clr->err_inj_clr_spa_range_length);
8719fb1a190SDave Jiang 
8729fb1a190SDave Jiang 	err_clr->status = 0;
8739fb1a190SDave Jiang 	return 0;
8749fb1a190SDave Jiang 
8759fb1a190SDave Jiang err:
8769fb1a190SDave Jiang 	err_clr->status = NFIT_ARS_INJECT_INVALID;
8779fb1a190SDave Jiang 	return rc;
8789fb1a190SDave Jiang }
8799fb1a190SDave Jiang 
nfit_test_cmd_ars_inject_status(struct nfit_test * t,struct nd_cmd_ars_err_inj_stat * err_stat,unsigned int buf_len)8809fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_status(struct nfit_test *t,
8819fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_stat *err_stat,
8829fb1a190SDave Jiang 		unsigned int buf_len)
8839fb1a190SDave Jiang {
8849fb1a190SDave Jiang 	struct badrange_entry *be;
8859fb1a190SDave Jiang 	int max = SZ_4K / sizeof(struct nd_error_stat_query_record);
8869fb1a190SDave Jiang 	int i = 0;
8879fb1a190SDave Jiang 
8889fb1a190SDave Jiang 	err_stat->status = 0;
8899fb1a190SDave Jiang 	spin_lock(&t->badrange.lock);
8909fb1a190SDave Jiang 	list_for_each_entry(be, &t->badrange.list, list) {
8919fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_base = be->start;
8929fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_length = be->length;
8939fb1a190SDave Jiang 		i++;
8949fb1a190SDave Jiang 		if (i > max)
8959fb1a190SDave Jiang 			break;
8969fb1a190SDave Jiang 	}
8979fb1a190SDave Jiang 	spin_unlock(&t->badrange.lock);
8989fb1a190SDave Jiang 	err_stat->inj_err_rec_count = i;
8999fb1a190SDave Jiang 
9009fb1a190SDave Jiang 	return 0;
9019fb1a190SDave Jiang }
9029fb1a190SDave Jiang 
nd_intel_test_cmd_set_lss_status(struct nfit_test * t,struct nd_intel_lss * nd_cmd,unsigned int buf_len)903674d8bdeSDave Jiang static int nd_intel_test_cmd_set_lss_status(struct nfit_test *t,
904674d8bdeSDave Jiang 		struct nd_intel_lss *nd_cmd, unsigned int buf_len)
905674d8bdeSDave Jiang {
906674d8bdeSDave Jiang 	struct device *dev = &t->pdev.dev;
907674d8bdeSDave Jiang 
908674d8bdeSDave Jiang 	if (buf_len < sizeof(*nd_cmd))
909674d8bdeSDave Jiang 		return -EINVAL;
910674d8bdeSDave Jiang 
911674d8bdeSDave Jiang 	switch (nd_cmd->enable) {
912674d8bdeSDave Jiang 	case 0:
913674d8bdeSDave Jiang 		nd_cmd->status = 0;
914674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status disabled\n",
915674d8bdeSDave Jiang 				__func__);
916674d8bdeSDave Jiang 		break;
917674d8bdeSDave Jiang 	case 1:
918674d8bdeSDave Jiang 		nd_cmd->status = 0;
919674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status enabled\n",
920674d8bdeSDave Jiang 				__func__);
921674d8bdeSDave Jiang 		break;
922674d8bdeSDave Jiang 	default:
923674d8bdeSDave Jiang 		dev_warn(dev, "Unknown enable value: %#x\n", nd_cmd->enable);
924674d8bdeSDave Jiang 		nd_cmd->status = 0x3;
925674d8bdeSDave Jiang 		break;
926674d8bdeSDave Jiang 	}
927674d8bdeSDave Jiang 
928674d8bdeSDave Jiang 
929674d8bdeSDave Jiang 	return 0;
930674d8bdeSDave Jiang }
931674d8bdeSDave Jiang 
override_return_code(int dimm,unsigned int func,int rc)93239611e83SDan Williams static int override_return_code(int dimm, unsigned int func, int rc)
93339611e83SDan Williams {
93439611e83SDan Williams 	if ((1 << func) & dimm_fail_cmd_flags[dimm]) {
93539611e83SDan Williams 		if (dimm_fail_cmd_code[dimm])
93639611e83SDan Williams 			return dimm_fail_cmd_code[dimm];
93739611e83SDan Williams 		return -EIO;
93839611e83SDan Williams 	}
93939611e83SDan Williams 	return rc;
94039611e83SDan Williams }
94139611e83SDan Williams 
nd_intel_test_cmd_security_status(struct nfit_test * t,struct nd_intel_get_security_state * nd_cmd,unsigned int buf_len,int dimm)9423c13e2acSDave Jiang static int nd_intel_test_cmd_security_status(struct nfit_test *t,
9433c13e2acSDave Jiang 		struct nd_intel_get_security_state *nd_cmd,
9443c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
9453c13e2acSDave Jiang {
9463c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
9473c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
9483c13e2acSDave Jiang 
9493c13e2acSDave Jiang 	nd_cmd->status = 0;
9503c13e2acSDave Jiang 	nd_cmd->state = sec->state;
951ecaa4a97SDave Jiang 	nd_cmd->extended_state = sec->ext_state;
9523c13e2acSDave Jiang 	dev_dbg(dev, "security state (%#x) returned\n", nd_cmd->state);
9533c13e2acSDave Jiang 
9543c13e2acSDave Jiang 	return 0;
9553c13e2acSDave Jiang }
9563c13e2acSDave Jiang 
nd_intel_test_cmd_unlock_unit(struct nfit_test * t,struct nd_intel_unlock_unit * nd_cmd,unsigned int buf_len,int dimm)9573c13e2acSDave Jiang static int nd_intel_test_cmd_unlock_unit(struct nfit_test *t,
9583c13e2acSDave Jiang 		struct nd_intel_unlock_unit *nd_cmd,
9593c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
9603c13e2acSDave Jiang {
9613c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
9623c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
9633c13e2acSDave Jiang 
9643c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_LOCKED) ||
9653c13e2acSDave Jiang 			(sec->state & ND_INTEL_SEC_STATE_FROZEN)) {
9663c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
9673c13e2acSDave Jiang 		dev_dbg(dev, "unlock unit: invalid state: %#x\n",
9683c13e2acSDave Jiang 				sec->state);
9693c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
9703c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
9713c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
9723c13e2acSDave Jiang 		dev_dbg(dev, "unlock unit: invalid passphrase\n");
9733c13e2acSDave Jiang 	} else {
9743c13e2acSDave Jiang 		nd_cmd->status = 0;
9753c13e2acSDave Jiang 		sec->state = ND_INTEL_SEC_STATE_ENABLED;
9763c13e2acSDave Jiang 		dev_dbg(dev, "Unit unlocked\n");
9773c13e2acSDave Jiang 	}
9783c13e2acSDave Jiang 
9793c13e2acSDave Jiang 	dev_dbg(dev, "unlocking status returned: %#x\n", nd_cmd->status);
9803c13e2acSDave Jiang 	return 0;
9813c13e2acSDave Jiang }
9823c13e2acSDave Jiang 
nd_intel_test_cmd_set_pass(struct nfit_test * t,struct nd_intel_set_passphrase * nd_cmd,unsigned int buf_len,int dimm)9833c13e2acSDave Jiang static int nd_intel_test_cmd_set_pass(struct nfit_test *t,
9843c13e2acSDave Jiang 		struct nd_intel_set_passphrase *nd_cmd,
9853c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
9863c13e2acSDave Jiang {
9873c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
9883c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
9893c13e2acSDave Jiang 
9903c13e2acSDave Jiang 	if (sec->state & ND_INTEL_SEC_STATE_FROZEN) {
9913c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
9923c13e2acSDave Jiang 		dev_dbg(dev, "set passphrase: wrong security state\n");
9933c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->old_pass, sec->passphrase,
9943c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
9953c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
9963c13e2acSDave Jiang 		dev_dbg(dev, "set passphrase: wrong passphrase\n");
9973c13e2acSDave Jiang 	} else {
9983c13e2acSDave Jiang 		memcpy(sec->passphrase, nd_cmd->new_pass,
9993c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE);
10003c13e2acSDave Jiang 		sec->state |= ND_INTEL_SEC_STATE_ENABLED;
10013c13e2acSDave Jiang 		nd_cmd->status = 0;
10023c13e2acSDave Jiang 		dev_dbg(dev, "passphrase updated\n");
10033c13e2acSDave Jiang 	}
10043c13e2acSDave Jiang 
10053c13e2acSDave Jiang 	return 0;
10063c13e2acSDave Jiang }
10073c13e2acSDave Jiang 
nd_intel_test_cmd_freeze_lock(struct nfit_test * t,struct nd_intel_freeze_lock * nd_cmd,unsigned int buf_len,int dimm)10083c13e2acSDave Jiang static int nd_intel_test_cmd_freeze_lock(struct nfit_test *t,
10093c13e2acSDave Jiang 		struct nd_intel_freeze_lock *nd_cmd,
10103c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
10113c13e2acSDave Jiang {
10123c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
10133c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
10143c13e2acSDave Jiang 
10153c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED)) {
10163c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
10173c13e2acSDave Jiang 		dev_dbg(dev, "freeze lock: wrong security state\n");
10183c13e2acSDave Jiang 	} else {
10193c13e2acSDave Jiang 		sec->state |= ND_INTEL_SEC_STATE_FROZEN;
10203c13e2acSDave Jiang 		nd_cmd->status = 0;
10213c13e2acSDave Jiang 		dev_dbg(dev, "security frozen\n");
10223c13e2acSDave Jiang 	}
10233c13e2acSDave Jiang 
10243c13e2acSDave Jiang 	return 0;
10253c13e2acSDave Jiang }
10263c13e2acSDave Jiang 
nd_intel_test_cmd_disable_pass(struct nfit_test * t,struct nd_intel_disable_passphrase * nd_cmd,unsigned int buf_len,int dimm)10273c13e2acSDave Jiang static int nd_intel_test_cmd_disable_pass(struct nfit_test *t,
10283c13e2acSDave Jiang 		struct nd_intel_disable_passphrase *nd_cmd,
10293c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
10303c13e2acSDave Jiang {
10313c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
10323c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
10333c13e2acSDave Jiang 
10343c13e2acSDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED) ||
10353c13e2acSDave Jiang 			(sec->state & ND_INTEL_SEC_STATE_FROZEN)) {
10363c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
10373c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: wrong security state\n");
10383c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
10393c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
10403c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
10413c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: wrong passphrase\n");
10423c13e2acSDave Jiang 	} else {
10433c13e2acSDave Jiang 		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
10443c13e2acSDave Jiang 		sec->state = 0;
10453c13e2acSDave Jiang 		dev_dbg(dev, "disable passphrase: done\n");
10463c13e2acSDave Jiang 	}
10473c13e2acSDave Jiang 
10483c13e2acSDave Jiang 	return 0;
10493c13e2acSDave Jiang }
10503c13e2acSDave Jiang 
nd_intel_test_cmd_secure_erase(struct nfit_test * t,struct nd_intel_secure_erase * nd_cmd,unsigned int buf_len,int dimm)10513c13e2acSDave Jiang static int nd_intel_test_cmd_secure_erase(struct nfit_test *t,
10523c13e2acSDave Jiang 		struct nd_intel_secure_erase *nd_cmd,
10533c13e2acSDave Jiang 		unsigned int buf_len, int dimm)
10543c13e2acSDave Jiang {
10553c13e2acSDave Jiang 	struct device *dev = &t->pdev.dev;
10563c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
10573c13e2acSDave Jiang 
1058037c8489SDave Jiang 	if (sec->state & ND_INTEL_SEC_STATE_FROZEN) {
10593c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
10603c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: wrong security state\n");
10613c13e2acSDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->passphrase,
10623c13e2acSDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
10633c13e2acSDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
10643c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: wrong passphrase\n");
10653c13e2acSDave Jiang 	} else {
1066037c8489SDave Jiang 		if (!(sec->state & ND_INTEL_SEC_STATE_ENABLED)
1067037c8489SDave Jiang 				&& (memcmp(nd_cmd->passphrase, zero_key,
1068037c8489SDave Jiang 					ND_INTEL_PASSPHRASE_SIZE) != 0)) {
1069037c8489SDave Jiang 			dev_dbg(dev, "invalid zero key\n");
1070037c8489SDave Jiang 			return 0;
1071037c8489SDave Jiang 		}
10723c13e2acSDave Jiang 		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
1073ecaa4a97SDave Jiang 		memset(sec->master_passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
10743c13e2acSDave Jiang 		sec->state = 0;
1075ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
10763c13e2acSDave Jiang 		dev_dbg(dev, "secure erase: done\n");
10773c13e2acSDave Jiang 	}
10783c13e2acSDave Jiang 
10793c13e2acSDave Jiang 	return 0;
10803c13e2acSDave Jiang }
10813c13e2acSDave Jiang 
nd_intel_test_cmd_overwrite(struct nfit_test * t,struct nd_intel_overwrite * nd_cmd,unsigned int buf_len,int dimm)1082926f7480SDave Jiang static int nd_intel_test_cmd_overwrite(struct nfit_test *t,
1083926f7480SDave Jiang 		struct nd_intel_overwrite *nd_cmd,
1084926f7480SDave Jiang 		unsigned int buf_len, int dimm)
1085926f7480SDave Jiang {
1086926f7480SDave Jiang 	struct device *dev = &t->pdev.dev;
1087926f7480SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1088926f7480SDave Jiang 
1089926f7480SDave Jiang 	if ((sec->state & ND_INTEL_SEC_STATE_ENABLED) &&
1090926f7480SDave Jiang 			memcmp(nd_cmd->passphrase, sec->passphrase,
1091926f7480SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
1092926f7480SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
1093926f7480SDave Jiang 		dev_dbg(dev, "overwrite: wrong passphrase\n");
1094926f7480SDave Jiang 		return 0;
1095926f7480SDave Jiang 	}
1096926f7480SDave Jiang 
10972170a0d5SDave Jiang 	sec->old_state = sec->state;
1098926f7480SDave Jiang 	sec->state = ND_INTEL_SEC_STATE_OVERWRITE;
1099926f7480SDave Jiang 	dev_dbg(dev, "overwrite progressing.\n");
1100926f7480SDave Jiang 	sec->overwrite_end_time = get_jiffies_64() + 5 * HZ;
1101926f7480SDave Jiang 
1102926f7480SDave Jiang 	return 0;
1103926f7480SDave Jiang }
1104926f7480SDave Jiang 
nd_intel_test_cmd_query_overwrite(struct nfit_test * t,struct nd_intel_query_overwrite * nd_cmd,unsigned int buf_len,int dimm)1105926f7480SDave Jiang static int nd_intel_test_cmd_query_overwrite(struct nfit_test *t,
1106926f7480SDave Jiang 		struct nd_intel_query_overwrite *nd_cmd,
1107926f7480SDave Jiang 		unsigned int buf_len, int dimm)
1108926f7480SDave Jiang {
1109926f7480SDave Jiang 	struct device *dev = &t->pdev.dev;
1110926f7480SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1111926f7480SDave Jiang 
1112926f7480SDave Jiang 	if (!(sec->state & ND_INTEL_SEC_STATE_OVERWRITE)) {
1113926f7480SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_OQUERY_SEQUENCE_ERR;
1114926f7480SDave Jiang 		return 0;
1115926f7480SDave Jiang 	}
1116926f7480SDave Jiang 
1117926f7480SDave Jiang 	if (time_is_before_jiffies64(sec->overwrite_end_time)) {
1118926f7480SDave Jiang 		sec->overwrite_end_time = 0;
11192170a0d5SDave Jiang 		sec->state = sec->old_state;
11202170a0d5SDave Jiang 		sec->old_state = 0;
1121ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1122926f7480SDave Jiang 		dev_dbg(dev, "overwrite is complete\n");
1123926f7480SDave Jiang 	} else
1124926f7480SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_OQUERY_INPROGRESS;
1125926f7480SDave Jiang 	return 0;
1126926f7480SDave Jiang }
1127926f7480SDave Jiang 
nd_intel_test_cmd_master_set_pass(struct nfit_test * t,struct nd_intel_set_master_passphrase * nd_cmd,unsigned int buf_len,int dimm)1128ecaa4a97SDave Jiang static int nd_intel_test_cmd_master_set_pass(struct nfit_test *t,
1129ecaa4a97SDave Jiang 		struct nd_intel_set_master_passphrase *nd_cmd,
1130ecaa4a97SDave Jiang 		unsigned int buf_len, int dimm)
1131ecaa4a97SDave Jiang {
1132ecaa4a97SDave Jiang 	struct device *dev = &t->pdev.dev;
1133ecaa4a97SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1134ecaa4a97SDave Jiang 
1135ecaa4a97SDave Jiang 	if (!(sec->ext_state & ND_INTEL_SEC_ESTATE_ENABLED)) {
1136ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_NOT_SUPPORTED;
1137ecaa4a97SDave Jiang 		dev_dbg(dev, "master set passphrase: in wrong state\n");
1138ecaa4a97SDave Jiang 	} else if (sec->ext_state & ND_INTEL_SEC_ESTATE_PLIMIT) {
1139ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
1140ecaa4a97SDave Jiang 		dev_dbg(dev, "master set passphrase: in wrong security state\n");
1141ecaa4a97SDave Jiang 	} else if (memcmp(nd_cmd->old_pass, sec->master_passphrase,
1142ecaa4a97SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
1143ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
1144ecaa4a97SDave Jiang 		dev_dbg(dev, "master set passphrase: wrong passphrase\n");
1145ecaa4a97SDave Jiang 	} else {
1146ecaa4a97SDave Jiang 		memcpy(sec->master_passphrase, nd_cmd->new_pass,
1147ecaa4a97SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE);
1148ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1149ecaa4a97SDave Jiang 		dev_dbg(dev, "master passphrase: updated\n");
1150ecaa4a97SDave Jiang 	}
1151ecaa4a97SDave Jiang 
1152ecaa4a97SDave Jiang 	return 0;
1153ecaa4a97SDave Jiang }
1154ecaa4a97SDave Jiang 
nd_intel_test_cmd_master_secure_erase(struct nfit_test * t,struct nd_intel_master_secure_erase * nd_cmd,unsigned int buf_len,int dimm)1155ecaa4a97SDave Jiang static int nd_intel_test_cmd_master_secure_erase(struct nfit_test *t,
1156ecaa4a97SDave Jiang 		struct nd_intel_master_secure_erase *nd_cmd,
1157ecaa4a97SDave Jiang 		unsigned int buf_len, int dimm)
1158ecaa4a97SDave Jiang {
1159ecaa4a97SDave Jiang 	struct device *dev = &t->pdev.dev;
1160ecaa4a97SDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
1161ecaa4a97SDave Jiang 
1162ecaa4a97SDave Jiang 	if (!(sec->ext_state & ND_INTEL_SEC_ESTATE_ENABLED)) {
1163ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_NOT_SUPPORTED;
1164ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: in wrong state\n");
1165ecaa4a97SDave Jiang 	} else if (sec->ext_state & ND_INTEL_SEC_ESTATE_PLIMIT) {
1166ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_STATE;
1167ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: in wrong security state\n");
1168ecaa4a97SDave Jiang 	} else if (memcmp(nd_cmd->passphrase, sec->master_passphrase,
1169ecaa4a97SDave Jiang 				ND_INTEL_PASSPHRASE_SIZE) != 0) {
1170ecaa4a97SDave Jiang 		nd_cmd->status = ND_INTEL_STATUS_INVALID_PASS;
1171ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: wrong passphrase\n");
1172ecaa4a97SDave Jiang 	} else {
1173ecaa4a97SDave Jiang 		/* we do not erase master state passphrase ever */
1174ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1175ecaa4a97SDave Jiang 		memset(sec->passphrase, 0, ND_INTEL_PASSPHRASE_SIZE);
1176ecaa4a97SDave Jiang 		sec->state = 0;
1177ecaa4a97SDave Jiang 		dev_dbg(dev, "master secure erase: done\n");
1178ecaa4a97SDave Jiang 	}
1179ecaa4a97SDave Jiang 
1180ecaa4a97SDave Jiang 	return 0;
1181ecaa4a97SDave Jiang }
1182ecaa4a97SDave Jiang 
1183916566aeSDan Williams static unsigned long last_activate;
1184916566aeSDan Williams 
nvdimm_bus_intel_fw_activate_businfo(struct nfit_test * t,struct nd_intel_bus_fw_activate_businfo * nd_cmd,unsigned int buf_len)1185916566aeSDan Williams static int nvdimm_bus_intel_fw_activate_businfo(struct nfit_test *t,
1186916566aeSDan Williams 		struct nd_intel_bus_fw_activate_businfo *nd_cmd,
1187916566aeSDan Williams 		unsigned int buf_len)
1188916566aeSDan Williams {
1189916566aeSDan Williams 	int i, armed = 0;
1190916566aeSDan Williams 	int state;
1191916566aeSDan Williams 	u64 tmo;
1192916566aeSDan Williams 
1193916566aeSDan Williams 	for (i = 0; i < NUM_DCR; i++) {
1194916566aeSDan Williams 		struct nfit_test_fw *fw = &t->fw[i];
1195916566aeSDan Williams 
1196916566aeSDan Williams 		if (fw->armed)
1197916566aeSDan Williams 			armed++;
1198916566aeSDan Williams 	}
1199916566aeSDan Williams 
1200916566aeSDan Williams 	/*
1201916566aeSDan Williams 	 * Emulate 3 second activation max, and 1 second incremental
1202916566aeSDan Williams 	 * quiesce time per dimm requiring multiple activates to get all
1203916566aeSDan Williams 	 * DIMMs updated.
1204916566aeSDan Williams 	 */
1205916566aeSDan Williams 	if (armed)
1206916566aeSDan Williams 		state = ND_INTEL_FWA_ARMED;
1207916566aeSDan Williams 	else if (!last_activate || time_after(jiffies, last_activate + 3 * HZ))
1208916566aeSDan Williams 		state = ND_INTEL_FWA_IDLE;
1209916566aeSDan Williams 	else
1210916566aeSDan Williams 		state = ND_INTEL_FWA_BUSY;
1211916566aeSDan Williams 
1212916566aeSDan Williams 	tmo = armed * USEC_PER_SEC;
1213916566aeSDan Williams 	*nd_cmd = (struct nd_intel_bus_fw_activate_businfo) {
1214916566aeSDan Williams 		.capability = ND_INTEL_BUS_FWA_CAP_FWQUIESCE
1215916566aeSDan Williams 			| ND_INTEL_BUS_FWA_CAP_OSQUIESCE
1216916566aeSDan Williams 			| ND_INTEL_BUS_FWA_CAP_RESET,
1217916566aeSDan Williams 		.state = state,
1218916566aeSDan Williams 		.activate_tmo = tmo,
1219916566aeSDan Williams 		.cpu_quiesce_tmo = tmo,
1220916566aeSDan Williams 		.io_quiesce_tmo = tmo,
1221916566aeSDan Williams 		.max_quiesce_tmo = 3 * USEC_PER_SEC,
1222916566aeSDan Williams 	};
1223916566aeSDan Williams 
1224916566aeSDan Williams 	return 0;
1225916566aeSDan Williams }
1226916566aeSDan Williams 
nvdimm_bus_intel_fw_activate(struct nfit_test * t,struct nd_intel_bus_fw_activate * nd_cmd,unsigned int buf_len)1227916566aeSDan Williams static int nvdimm_bus_intel_fw_activate(struct nfit_test *t,
1228916566aeSDan Williams 		struct nd_intel_bus_fw_activate *nd_cmd,
1229916566aeSDan Williams 		unsigned int buf_len)
1230916566aeSDan Williams {
1231916566aeSDan Williams 	struct nd_intel_bus_fw_activate_businfo info;
1232916566aeSDan Williams 	u32 status = 0;
1233916566aeSDan Williams 	int i;
1234916566aeSDan Williams 
1235916566aeSDan Williams 	nvdimm_bus_intel_fw_activate_businfo(t, &info, sizeof(info));
1236916566aeSDan Williams 	if (info.state == ND_INTEL_FWA_BUSY)
1237916566aeSDan Williams 		status = ND_INTEL_BUS_FWA_STATUS_BUSY;
1238916566aeSDan Williams 	else if (info.activate_tmo > info.max_quiesce_tmo)
1239916566aeSDan Williams 		status = ND_INTEL_BUS_FWA_STATUS_TMO;
1240916566aeSDan Williams 	else if (info.state == ND_INTEL_FWA_IDLE)
1241916566aeSDan Williams 		status = ND_INTEL_BUS_FWA_STATUS_NOARM;
1242916566aeSDan Williams 
1243916566aeSDan Williams 	dev_dbg(&t->pdev.dev, "status: %d\n", status);
1244916566aeSDan Williams 	nd_cmd->status = status;
1245916566aeSDan Williams 	if (status && status != ND_INTEL_BUS_FWA_STATUS_TMO)
1246916566aeSDan Williams 		return 0;
1247916566aeSDan Williams 
1248916566aeSDan Williams 	last_activate = jiffies;
1249916566aeSDan Williams 	for (i = 0; i < NUM_DCR; i++) {
1250916566aeSDan Williams 		struct nfit_test_fw *fw = &t->fw[i];
1251916566aeSDan Williams 
1252916566aeSDan Williams 		if (!fw->armed)
1253916566aeSDan Williams 			continue;
1254916566aeSDan Williams 		if (fw->state != FW_STATE_UPDATED)
1255916566aeSDan Williams 			fw->missed_activate = true;
1256916566aeSDan Williams 		else
1257916566aeSDan Williams 			fw->state = FW_STATE_NEW;
1258916566aeSDan Williams 		fw->armed = false;
1259916566aeSDan Williams 		fw->last_activate = last_activate;
1260916566aeSDan Williams 	}
1261916566aeSDan Williams 
1262916566aeSDan Williams 	return 0;
1263916566aeSDan Williams }
1264916566aeSDan Williams 
nd_intel_test_cmd_fw_activate_dimminfo(struct nfit_test * t,struct nd_intel_fw_activate_dimminfo * nd_cmd,unsigned int buf_len,int dimm)1265916566aeSDan Williams static int nd_intel_test_cmd_fw_activate_dimminfo(struct nfit_test *t,
1266916566aeSDan Williams 		struct nd_intel_fw_activate_dimminfo *nd_cmd,
1267916566aeSDan Williams 		unsigned int buf_len, int dimm)
1268916566aeSDan Williams {
1269916566aeSDan Williams 	struct nd_intel_bus_fw_activate_businfo info;
1270916566aeSDan Williams 	struct nfit_test_fw *fw = &t->fw[dimm];
1271916566aeSDan Williams 	u32 result, state;
1272916566aeSDan Williams 
1273916566aeSDan Williams 	nvdimm_bus_intel_fw_activate_businfo(t, &info, sizeof(info));
1274916566aeSDan Williams 
1275916566aeSDan Williams 	if (info.state == ND_INTEL_FWA_BUSY)
1276916566aeSDan Williams 		state = ND_INTEL_FWA_BUSY;
1277916566aeSDan Williams 	else if (info.state == ND_INTEL_FWA_IDLE)
1278916566aeSDan Williams 		state = ND_INTEL_FWA_IDLE;
1279916566aeSDan Williams 	else if (fw->armed)
1280916566aeSDan Williams 		state = ND_INTEL_FWA_ARMED;
1281916566aeSDan Williams 	else
1282916566aeSDan Williams 		state = ND_INTEL_FWA_IDLE;
1283916566aeSDan Williams 
1284916566aeSDan Williams 	result = ND_INTEL_DIMM_FWA_NONE;
1285916566aeSDan Williams 	if (last_activate && fw->last_activate == last_activate &&
1286916566aeSDan Williams 			state == ND_INTEL_FWA_IDLE) {
1287916566aeSDan Williams 		if (fw->missed_activate)
1288916566aeSDan Williams 			result = ND_INTEL_DIMM_FWA_NOTSTAGED;
1289916566aeSDan Williams 		else
1290916566aeSDan Williams 			result = ND_INTEL_DIMM_FWA_SUCCESS;
1291916566aeSDan Williams 	}
1292916566aeSDan Williams 
1293916566aeSDan Williams 	*nd_cmd = (struct nd_intel_fw_activate_dimminfo) {
1294916566aeSDan Williams 		.result = result,
1295916566aeSDan Williams 		.state = state,
1296916566aeSDan Williams 	};
1297916566aeSDan Williams 
1298916566aeSDan Williams 	return 0;
1299916566aeSDan Williams }
1300916566aeSDan Williams 
nd_intel_test_cmd_fw_activate_arm(struct nfit_test * t,struct nd_intel_fw_activate_arm * nd_cmd,unsigned int buf_len,int dimm)1301916566aeSDan Williams static int nd_intel_test_cmd_fw_activate_arm(struct nfit_test *t,
1302916566aeSDan Williams 		struct nd_intel_fw_activate_arm *nd_cmd,
1303916566aeSDan Williams 		unsigned int buf_len, int dimm)
1304916566aeSDan Williams {
1305916566aeSDan Williams 	struct nfit_test_fw *fw = &t->fw[dimm];
1306916566aeSDan Williams 
1307916566aeSDan Williams 	fw->armed = nd_cmd->activate_arm == ND_INTEL_DIMM_FWA_ARM;
1308916566aeSDan Williams 	nd_cmd->status = 0;
1309916566aeSDan Williams 	return 0;
1310916566aeSDan Williams }
1311ecaa4a97SDave Jiang 
get_dimm(struct nfit_mem * nfit_mem,unsigned int func)1312bfbaa952SDave Jiang static int get_dimm(struct nfit_mem *nfit_mem, unsigned int func)
1313bfbaa952SDave Jiang {
1314bfbaa952SDave Jiang 	int i;
1315bfbaa952SDave Jiang 
1316bfbaa952SDave Jiang 	/* lookup per-dimm data */
1317bfbaa952SDave Jiang 	for (i = 0; i < ARRAY_SIZE(handle); i++)
1318bfbaa952SDave Jiang 		if (__to_nfit_memdev(nfit_mem)->device_handle == handle[i])
1319bfbaa952SDave Jiang 			break;
1320bfbaa952SDave Jiang 	if (i >= ARRAY_SIZE(handle))
1321bfbaa952SDave Jiang 		return -ENXIO;
1322bfbaa952SDave Jiang 	return i;
1323bfbaa952SDave Jiang }
1324bfbaa952SDave Jiang 
nfit_ctl_dbg(struct acpi_nfit_desc * acpi_desc,struct nvdimm * nvdimm,unsigned int cmd,void * buf,unsigned int len)13250d47c4dfSDan Williams static void nfit_ctl_dbg(struct acpi_nfit_desc *acpi_desc,
13260d47c4dfSDan Williams 		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
13270d47c4dfSDan Williams 		unsigned int len)
13280d47c4dfSDan Williams {
13290d47c4dfSDan Williams 	struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
13300d47c4dfSDan Williams 	unsigned int func = cmd;
13310d47c4dfSDan Williams 	unsigned int family = 0;
13320d47c4dfSDan Williams 
13330d47c4dfSDan Williams 	if (cmd == ND_CMD_CALL) {
13340d47c4dfSDan Williams 		struct nd_cmd_pkg *pkg = buf;
13350d47c4dfSDan Williams 
13360d47c4dfSDan Williams 		len = pkg->nd_size_in;
13370d47c4dfSDan Williams 		family = pkg->nd_family;
13380d47c4dfSDan Williams 		buf = pkg->nd_payload;
13390d47c4dfSDan Williams 		func = pkg->nd_command;
13400d47c4dfSDan Williams 	}
13410d47c4dfSDan Williams 	dev_dbg(&t->pdev.dev, "%s family: %d cmd: %d: func: %d input length: %d\n",
13420d47c4dfSDan Williams 			nvdimm ? nvdimm_name(nvdimm) : "bus", family, cmd, func,
13430d47c4dfSDan Williams 			len);
13440d47c4dfSDan Williams 	print_hex_dump_debug("nvdimm in  ", DUMP_PREFIX_OFFSET, 16, 4,
13450d47c4dfSDan Williams 			buf, min(len, 256u), true);
13460d47c4dfSDan Williams }
13470d47c4dfSDan Williams 
nfit_test_ctl(struct nvdimm_bus_descriptor * nd_desc,struct nvdimm * nvdimm,unsigned int cmd,void * buf,unsigned int buf_len,int * cmd_rc)134839c686b8SVishal Verma static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
134939c686b8SVishal Verma 		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
1350aef25338SDan Williams 		unsigned int buf_len, int *cmd_rc)
135139c686b8SVishal Verma {
135239c686b8SVishal Verma 	struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
135339c686b8SVishal Verma 	struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
13546634fb06SDan Williams 	unsigned int func = cmd;
1355f471f1a7SDan Williams 	int i, rc = 0, __cmd_rc;
1356f471f1a7SDan Williams 
1357f471f1a7SDan Williams 	if (!cmd_rc)
1358f471f1a7SDan Williams 		cmd_rc = &__cmd_rc;
1359f471f1a7SDan Williams 	*cmd_rc = 0;
136039c686b8SVishal Verma 
13610d47c4dfSDan Williams 	nfit_ctl_dbg(acpi_desc, nvdimm, cmd, buf, buf_len);
13620d47c4dfSDan Williams 
136339c686b8SVishal Verma 	if (nvdimm) {
136439c686b8SVishal Verma 		struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1365e3654ecaSDan Williams 		unsigned long cmd_mask = nvdimm_cmd_mask(nvdimm);
136639c686b8SVishal Verma 
13676634fb06SDan Williams 		if (!nfit_mem)
13686634fb06SDan Williams 			return -ENOTTY;
13696634fb06SDan Williams 
13706634fb06SDan Williams 		if (cmd == ND_CMD_CALL) {
13716634fb06SDan Williams 			struct nd_cmd_pkg *call_pkg = buf;
13726634fb06SDan Williams 
13736634fb06SDan Williams 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
13746634fb06SDan Williams 			buf = (void *) call_pkg->nd_payload;
13756634fb06SDan Williams 			func = call_pkg->nd_command;
13766634fb06SDan Williams 			if (call_pkg->nd_family != nfit_mem->family)
13776634fb06SDan Williams 				return -ENOTTY;
1378bfbaa952SDave Jiang 
1379bfbaa952SDave Jiang 			i = get_dimm(nfit_mem, func);
1380bfbaa952SDave Jiang 			if (i < 0)
1381bfbaa952SDave Jiang 				return i;
138224770658SDan Williams 			if (i >= NUM_DCR) {
138324770658SDan Williams 				dev_WARN_ONCE(&t->pdev.dev, 1,
138424770658SDan Williams 						"ND_CMD_CALL only valid for nfit_test0\n");
138524770658SDan Williams 				return -EINVAL;
138624770658SDan Williams 			}
1387bfbaa952SDave Jiang 
1388bfbaa952SDave Jiang 			switch (func) {
13893c13e2acSDave Jiang 			case NVDIMM_INTEL_GET_SECURITY_STATE:
13903c13e2acSDave Jiang 				rc = nd_intel_test_cmd_security_status(t,
13913c13e2acSDave Jiang 						buf, buf_len, i);
13923c13e2acSDave Jiang 				break;
13933c13e2acSDave Jiang 			case NVDIMM_INTEL_UNLOCK_UNIT:
13943c13e2acSDave Jiang 				rc = nd_intel_test_cmd_unlock_unit(t,
13953c13e2acSDave Jiang 						buf, buf_len, i);
13963c13e2acSDave Jiang 				break;
13973c13e2acSDave Jiang 			case NVDIMM_INTEL_SET_PASSPHRASE:
13983c13e2acSDave Jiang 				rc = nd_intel_test_cmd_set_pass(t,
13993c13e2acSDave Jiang 						buf, buf_len, i);
14003c13e2acSDave Jiang 				break;
14013c13e2acSDave Jiang 			case NVDIMM_INTEL_DISABLE_PASSPHRASE:
14023c13e2acSDave Jiang 				rc = nd_intel_test_cmd_disable_pass(t,
14033c13e2acSDave Jiang 						buf, buf_len, i);
14043c13e2acSDave Jiang 				break;
14053c13e2acSDave Jiang 			case NVDIMM_INTEL_FREEZE_LOCK:
14063c13e2acSDave Jiang 				rc = nd_intel_test_cmd_freeze_lock(t,
14073c13e2acSDave Jiang 						buf, buf_len, i);
14083c13e2acSDave Jiang 				break;
14093c13e2acSDave Jiang 			case NVDIMM_INTEL_SECURE_ERASE:
14103c13e2acSDave Jiang 				rc = nd_intel_test_cmd_secure_erase(t,
14113c13e2acSDave Jiang 						buf, buf_len, i);
14123c13e2acSDave Jiang 				break;
1413926f7480SDave Jiang 			case NVDIMM_INTEL_OVERWRITE:
1414926f7480SDave Jiang 				rc = nd_intel_test_cmd_overwrite(t,
141524770658SDan Williams 						buf, buf_len, i);
1416926f7480SDave Jiang 				break;
1417926f7480SDave Jiang 			case NVDIMM_INTEL_QUERY_OVERWRITE:
1418926f7480SDave Jiang 				rc = nd_intel_test_cmd_query_overwrite(t,
141924770658SDan Williams 						buf, buf_len, i);
1420926f7480SDave Jiang 				break;
1421ecaa4a97SDave Jiang 			case NVDIMM_INTEL_SET_MASTER_PASSPHRASE:
1422ecaa4a97SDave Jiang 				rc = nd_intel_test_cmd_master_set_pass(t,
1423ecaa4a97SDave Jiang 						buf, buf_len, i);
1424ecaa4a97SDave Jiang 				break;
1425ecaa4a97SDave Jiang 			case NVDIMM_INTEL_MASTER_SECURE_ERASE:
1426ecaa4a97SDave Jiang 				rc = nd_intel_test_cmd_master_secure_erase(t,
1427ecaa4a97SDave Jiang 						buf, buf_len, i);
1428ecaa4a97SDave Jiang 				break;
1429916566aeSDan Williams 			case NVDIMM_INTEL_FW_ACTIVATE_DIMMINFO:
1430916566aeSDan Williams 				rc = nd_intel_test_cmd_fw_activate_dimminfo(
1431916566aeSDan Williams 					t, buf, buf_len, i);
1432916566aeSDan Williams 				break;
1433916566aeSDan Williams 			case NVDIMM_INTEL_FW_ACTIVATE_ARM:
1434916566aeSDan Williams 				rc = nd_intel_test_cmd_fw_activate_arm(
1435916566aeSDan Williams 					t, buf, buf_len, i);
1436916566aeSDan Williams 				break;
1437674d8bdeSDave Jiang 			case ND_INTEL_ENABLE_LSS_STATUS:
143839611e83SDan Williams 				rc = nd_intel_test_cmd_set_lss_status(t,
1439674d8bdeSDave Jiang 						buf, buf_len);
144039611e83SDan Williams 				break;
1441bfbaa952SDave Jiang 			case ND_INTEL_FW_GET_INFO:
144239611e83SDan Williams 				rc = nd_intel_test_get_fw_info(t, buf,
144324770658SDan Williams 						buf_len, i);
144439611e83SDan Williams 				break;
1445bfbaa952SDave Jiang 			case ND_INTEL_FW_START_UPDATE:
144639611e83SDan Williams 				rc = nd_intel_test_start_update(t, buf,
144724770658SDan Williams 						buf_len, i);
144839611e83SDan Williams 				break;
1449bfbaa952SDave Jiang 			case ND_INTEL_FW_SEND_DATA:
145039611e83SDan Williams 				rc = nd_intel_test_send_data(t, buf,
145124770658SDan Williams 						buf_len, i);
145239611e83SDan Williams 				break;
1453bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_UPDATE:
145439611e83SDan Williams 				rc = nd_intel_test_finish_fw(t, buf,
145524770658SDan Williams 						buf_len, i);
145639611e83SDan Williams 				break;
1457bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_QUERY:
145839611e83SDan Williams 				rc = nd_intel_test_finish_query(t, buf,
145924770658SDan Williams 						buf_len, i);
146039611e83SDan Williams 				break;
1461bfbaa952SDave Jiang 			case ND_INTEL_SMART:
146239611e83SDan Williams 				rc = nfit_test_cmd_smart(buf, buf_len,
146324770658SDan Williams 						&t->smart[i]);
146439611e83SDan Williams 				break;
1465bfbaa952SDave Jiang 			case ND_INTEL_SMART_THRESHOLD:
146639611e83SDan Williams 				rc = nfit_test_cmd_smart_threshold(buf,
1467bfbaa952SDave Jiang 						buf_len,
146824770658SDan Williams 						&t->smart_threshold[i]);
146939611e83SDan Williams 				break;
1470bfbaa952SDave Jiang 			case ND_INTEL_SMART_SET_THRESHOLD:
147139611e83SDan Williams 				rc = nfit_test_cmd_smart_set_threshold(buf,
1472bfbaa952SDave Jiang 						buf_len,
147324770658SDan Williams 						&t->smart_threshold[i],
147424770658SDan Williams 						&t->smart[i],
1475bfbaa952SDave Jiang 						&t->pdev.dev, t->dimm_dev[i]);
147639611e83SDan Williams 				break;
14774cf260fcSVishal Verma 			case ND_INTEL_SMART_INJECT:
147839611e83SDan Williams 				rc = nfit_test_cmd_smart_inject(buf,
14794cf260fcSVishal Verma 						buf_len,
148024770658SDan Williams 						&t->smart_threshold[i],
148124770658SDan Williams 						&t->smart[i],
14824cf260fcSVishal Verma 						&t->pdev.dev, t->dimm_dev[i]);
148339611e83SDan Williams 				break;
1484bfbaa952SDave Jiang 			default:
1485bfbaa952SDave Jiang 				return -ENOTTY;
1486bfbaa952SDave Jiang 			}
148739611e83SDan Williams 			return override_return_code(i, func, rc);
14886634fb06SDan Williams 		}
14896634fb06SDan Williams 
14906634fb06SDan Williams 		if (!test_bit(cmd, &cmd_mask)
14916634fb06SDan Williams 				|| !test_bit(func, &nfit_mem->dsm_mask))
149239c686b8SVishal Verma 			return -ENOTTY;
149339c686b8SVishal Verma 
1494bfbaa952SDave Jiang 		i = get_dimm(nfit_mem, func);
1495bfbaa952SDave Jiang 		if (i < 0)
1496bfbaa952SDave Jiang 			return i;
149773606afdSDan Williams 
14986634fb06SDan Williams 		switch (func) {
149939c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_SIZE:
150039c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_size(buf, buf_len);
150139c686b8SVishal Verma 			break;
150239c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_DATA:
150339c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_data(buf, buf_len,
1504dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
150539c686b8SVishal Verma 			break;
150639c686b8SVishal Verma 		case ND_CMD_SET_CONFIG_DATA:
150739c686b8SVishal Verma 			rc = nfit_test_cmd_set_config_data(buf, buf_len,
1508dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
150939c686b8SVishal Verma 			break;
15106bc75619SDan Williams 		default:
15116bc75619SDan Williams 			return -ENOTTY;
15126bc75619SDan Williams 		}
151339611e83SDan Williams 		return override_return_code(i, func, rc);
151439c686b8SVishal Verma 	} else {
1515f471f1a7SDan Williams 		struct ars_state *ars_state = &t->ars_state;
151610246dc8SYasunori Goto 		struct nd_cmd_pkg *call_pkg = buf;
151710246dc8SYasunori Goto 
151810246dc8SYasunori Goto 		if (!nd_desc)
151910246dc8SYasunori Goto 			return -ENOTTY;
152010246dc8SYasunori Goto 
1521916566aeSDan Williams 		if (cmd == ND_CMD_CALL && call_pkg->nd_family
1522916566aeSDan Williams 				== NVDIMM_BUS_FAMILY_NFIT) {
152310246dc8SYasunori Goto 			func = call_pkg->nd_command;
152410246dc8SYasunori Goto 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
152510246dc8SYasunori Goto 			buf = (void *) call_pkg->nd_payload;
152610246dc8SYasunori Goto 
152710246dc8SYasunori Goto 			switch (func) {
152810246dc8SYasunori Goto 			case NFIT_CMD_TRANSLATE_SPA:
152910246dc8SYasunori Goto 				rc = nfit_test_cmd_translate_spa(
153010246dc8SYasunori Goto 					acpi_desc->nvdimm_bus, buf, buf_len);
153110246dc8SYasunori Goto 				return rc;
15329fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_SET:
15339fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_error_inject(t, buf,
15349fb1a190SDave Jiang 					buf_len);
15359fb1a190SDave Jiang 				return rc;
15369fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_CLEAR:
15379fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_clear(t, buf,
15389fb1a190SDave Jiang 					buf_len);
15399fb1a190SDave Jiang 				return rc;
15409fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_GET:
15419fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_status(t, buf,
15429fb1a190SDave Jiang 					buf_len);
15439fb1a190SDave Jiang 				return rc;
154410246dc8SYasunori Goto 			default:
154510246dc8SYasunori Goto 				return -ENOTTY;
154610246dc8SYasunori Goto 			}
1547916566aeSDan Williams 		} else if (cmd == ND_CMD_CALL && call_pkg->nd_family
1548916566aeSDan Williams 				== NVDIMM_BUS_FAMILY_INTEL) {
1549916566aeSDan Williams 			func = call_pkg->nd_command;
1550916566aeSDan Williams 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
1551916566aeSDan Williams 			buf = (void *) call_pkg->nd_payload;
1552916566aeSDan Williams 
1553916566aeSDan Williams 			switch (func) {
1554916566aeSDan Williams 			case NVDIMM_BUS_INTEL_FW_ACTIVATE_BUSINFO:
1555916566aeSDan Williams 				rc = nvdimm_bus_intel_fw_activate_businfo(t,
1556916566aeSDan Williams 						buf, buf_len);
1557916566aeSDan Williams 				return rc;
1558916566aeSDan Williams 			case NVDIMM_BUS_INTEL_FW_ACTIVATE:
1559916566aeSDan Williams 				rc = nvdimm_bus_intel_fw_activate(t, buf,
1560916566aeSDan Williams 						buf_len);
1561916566aeSDan Williams 				return rc;
1562916566aeSDan Williams 			default:
1563916566aeSDan Williams 				return -ENOTTY;
156410246dc8SYasunori Goto 			}
1565916566aeSDan Williams 		} else if (cmd == ND_CMD_CALL)
1566916566aeSDan Williams 			return -ENOTTY;
1567f471f1a7SDan Williams 
1568e3654ecaSDan Williams 		if (!nd_desc || !test_bit(cmd, &nd_desc->cmd_mask))
156939c686b8SVishal Verma 			return -ENOTTY;
157039c686b8SVishal Verma 
15716634fb06SDan Williams 		switch (func) {
157239c686b8SVishal Verma 		case ND_CMD_ARS_CAP:
157339c686b8SVishal Verma 			rc = nfit_test_cmd_ars_cap(buf, buf_len);
157439c686b8SVishal Verma 			break;
157539c686b8SVishal Verma 		case ND_CMD_ARS_START:
15769fb1a190SDave Jiang 			rc = nfit_test_cmd_ars_start(t, ars_state, buf,
15779fb1a190SDave Jiang 					buf_len, cmd_rc);
157839c686b8SVishal Verma 			break;
157939c686b8SVishal Verma 		case ND_CMD_ARS_STATUS:
1580f471f1a7SDan Williams 			rc = nfit_test_cmd_ars_status(ars_state, buf, buf_len,
1581f471f1a7SDan Williams 					cmd_rc);
158239c686b8SVishal Verma 			break;
1583d4f32367SDan Williams 		case ND_CMD_CLEAR_ERROR:
15845e096ef3SVishal Verma 			rc = nfit_test_cmd_clear_error(t, buf, buf_len, cmd_rc);
1585d4f32367SDan Williams 			break;
158639c686b8SVishal Verma 		default:
158739c686b8SVishal Verma 			return -ENOTTY;
158839c686b8SVishal Verma 		}
158939c686b8SVishal Verma 	}
15906bc75619SDan Williams 
15916bc75619SDan Williams 	return rc;
15926bc75619SDan Williams }
15936bc75619SDan Williams 
15946bc75619SDan Williams static DEFINE_SPINLOCK(nfit_test_lock);
15956bc75619SDan Williams static struct nfit_test *instances[NUM_NFITS];
15966bc75619SDan Williams 
release_nfit_res(void * data)15976bc75619SDan Williams static void release_nfit_res(void *data)
15986bc75619SDan Williams {
15996bc75619SDan Williams 	struct nfit_test_resource *nfit_res = data;
16006bc75619SDan Williams 
16016bc75619SDan Williams 	spin_lock(&nfit_test_lock);
16026bc75619SDan Williams 	list_del(&nfit_res->list);
16036bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
16046bc75619SDan Williams 
1605e3f5df76SDan Williams 	if (resource_size(&nfit_res->res) >= DIMM_SIZE)
1606e3f5df76SDan Williams 		gen_pool_free(nfit_pool, nfit_res->res.start,
1607e3f5df76SDan Williams 				resource_size(&nfit_res->res));
16086bc75619SDan Williams 	vfree(nfit_res->buf);
16096bc75619SDan Williams 	kfree(nfit_res);
16106bc75619SDan Williams }
16116bc75619SDan Williams 
__test_alloc(struct nfit_test * t,size_t size,dma_addr_t * dma,void * buf)16126bc75619SDan Williams static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
16136bc75619SDan Williams 		void *buf)
16146bc75619SDan Williams {
16156bc75619SDan Williams 	struct device *dev = &t->pdev.dev;
16166bc75619SDan Williams 	struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
16176bc75619SDan Williams 			GFP_KERNEL);
16186bc75619SDan Williams 	int rc;
16196bc75619SDan Williams 
1620e3f5df76SDan Williams 	if (!buf || !nfit_res || !*dma)
16216bc75619SDan Williams 		goto err;
16226bc75619SDan Williams 	rc = devm_add_action(dev, release_nfit_res, nfit_res);
16236bc75619SDan Williams 	if (rc)
16246bc75619SDan Williams 		goto err;
16256bc75619SDan Williams 	INIT_LIST_HEAD(&nfit_res->list);
16266bc75619SDan Williams 	memset(buf, 0, size);
16276bc75619SDan Williams 	nfit_res->dev = dev;
16286bc75619SDan Williams 	nfit_res->buf = buf;
1629bd4cd745SDan Williams 	nfit_res->res.start = *dma;
1630bd4cd745SDan Williams 	nfit_res->res.end = *dma + size - 1;
1631bd4cd745SDan Williams 	nfit_res->res.name = "NFIT";
1632bd4cd745SDan Williams 	spin_lock_init(&nfit_res->lock);
1633bd4cd745SDan Williams 	INIT_LIST_HEAD(&nfit_res->requests);
16346bc75619SDan Williams 	spin_lock(&nfit_test_lock);
16356bc75619SDan Williams 	list_add(&nfit_res->list, &t->resources);
16366bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
16376bc75619SDan Williams 
16386bc75619SDan Williams 	return nfit_res->buf;
16396bc75619SDan Williams  err:
1640e3f5df76SDan Williams 	if (*dma && size >= DIMM_SIZE)
1641e3f5df76SDan Williams 		gen_pool_free(nfit_pool, *dma, size);
1642ee8520feSDan Williams 	if (buf)
16436bc75619SDan Williams 		vfree(buf);
16446bc75619SDan Williams 	kfree(nfit_res);
16456bc75619SDan Williams 	return NULL;
16466bc75619SDan Williams }
16476bc75619SDan Williams 
test_alloc(struct nfit_test * t,size_t size,dma_addr_t * dma)16486bc75619SDan Williams static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
16496bc75619SDan Williams {
1650e3f5df76SDan Williams 	struct genpool_data_align data = {
1651e3f5df76SDan Williams 		.align = SZ_128M,
1652e3f5df76SDan Williams 	};
16536bc75619SDan Williams 	void *buf = vmalloc(size);
16546bc75619SDan Williams 
1655e3f5df76SDan Williams 	if (size >= DIMM_SIZE)
1656e3f5df76SDan Williams 		*dma = gen_pool_alloc_algo(nfit_pool, size,
1657e3f5df76SDan Williams 				gen_pool_first_fit_align, &data);
1658e3f5df76SDan Williams 	else
16596bc75619SDan Williams 		*dma = (unsigned long) buf;
16606bc75619SDan Williams 	return __test_alloc(t, size, dma, buf);
16616bc75619SDan Williams }
16626bc75619SDan Williams 
nfit_test_lookup(resource_size_t addr)16636bc75619SDan Williams static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
16646bc75619SDan Williams {
16656bc75619SDan Williams 	int i;
16666bc75619SDan Williams 
16676bc75619SDan Williams 	for (i = 0; i < ARRAY_SIZE(instances); i++) {
16686bc75619SDan Williams 		struct nfit_test_resource *n, *nfit_res = NULL;
16696bc75619SDan Williams 		struct nfit_test *t = instances[i];
16706bc75619SDan Williams 
16716bc75619SDan Williams 		if (!t)
16726bc75619SDan Williams 			continue;
16736bc75619SDan Williams 		spin_lock(&nfit_test_lock);
16746bc75619SDan Williams 		list_for_each_entry(n, &t->resources, list) {
1675bd4cd745SDan Williams 			if (addr >= n->res.start && (addr < n->res.start
1676bd4cd745SDan Williams 						+ resource_size(&n->res))) {
16776bc75619SDan Williams 				nfit_res = n;
16786bc75619SDan Williams 				break;
16796bc75619SDan Williams 			} else if (addr >= (unsigned long) n->buf
16806bc75619SDan Williams 					&& (addr < (unsigned long) n->buf
1681bd4cd745SDan Williams 						+ resource_size(&n->res))) {
16826bc75619SDan Williams 				nfit_res = n;
16836bc75619SDan Williams 				break;
16846bc75619SDan Williams 			}
16856bc75619SDan Williams 		}
16866bc75619SDan Williams 		spin_unlock(&nfit_test_lock);
16876bc75619SDan Williams 		if (nfit_res)
16886bc75619SDan Williams 			return nfit_res;
16896bc75619SDan Williams 	}
16906bc75619SDan Williams 
16916bc75619SDan Williams 	return NULL;
16926bc75619SDan Williams }
16936bc75619SDan Williams 
ars_state_init(struct device * dev,struct ars_state * ars_state)1694f471f1a7SDan Williams static int ars_state_init(struct device *dev, struct ars_state *ars_state)
1695f471f1a7SDan Williams {
16969fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
1697f471f1a7SDan Williams 	ars_state->ars_status = devm_kzalloc(dev,
16989fb1a190SDave Jiang 			sizeof(struct nd_cmd_ars_status) + SZ_4K, GFP_KERNEL);
1699f471f1a7SDan Williams 	if (!ars_state->ars_status)
1700f471f1a7SDan Williams 		return -ENOMEM;
1701f471f1a7SDan Williams 	spin_lock_init(&ars_state->lock);
1702f471f1a7SDan Williams 	return 0;
1703f471f1a7SDan Williams }
1704f471f1a7SDan Williams 
put_dimms(void * data)1705231bf117SDan Williams static void put_dimms(void *data)
1706231bf117SDan Williams {
1707718fda67SDan Williams 	struct nfit_test *t = data;
1708231bf117SDan Williams 	int i;
1709231bf117SDan Williams 
1710718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++)
1711718fda67SDan Williams 		if (t->dimm_dev[i])
1712718fda67SDan Williams 			device_unregister(t->dimm_dev[i]);
1713231bf117SDan Williams }
1714231bf117SDan Williams 
1715dd6cad2dSGreg Kroah-Hartman static const struct class nfit_test_dimm = {
1716dd6cad2dSGreg Kroah-Hartman 	.name = "nfit_test_dimm",
1717dd6cad2dSGreg Kroah-Hartman };
1718231bf117SDan Williams 
dimm_name_to_id(struct device * dev)171973606afdSDan Williams static int dimm_name_to_id(struct device *dev)
172073606afdSDan Williams {
172173606afdSDan Williams 	int dimm;
172273606afdSDan Williams 
1723718fda67SDan Williams 	if (sscanf(dev_name(dev), "test_dimm%d", &dimm) != 1)
172473606afdSDan Williams 		return -ENXIO;
172573606afdSDan Williams 	return dimm;
172673606afdSDan Williams }
172773606afdSDan Williams 
handle_show(struct device * dev,struct device_attribute * attr,char * buf)172873606afdSDan Williams static ssize_t handle_show(struct device *dev, struct device_attribute *attr,
172973606afdSDan Williams 		char *buf)
173073606afdSDan Williams {
173173606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
173273606afdSDan Williams 
173373606afdSDan Williams 	if (dimm < 0)
173473606afdSDan Williams 		return dimm;
173573606afdSDan Williams 
173619357a68SDan Williams 	return sprintf(buf, "%#x\n", handle[dimm]);
173773606afdSDan Williams }
173873606afdSDan Williams DEVICE_ATTR_RO(handle);
173973606afdSDan Williams 
fail_cmd_show(struct device * dev,struct device_attribute * attr,char * buf)174073606afdSDan Williams static ssize_t fail_cmd_show(struct device *dev, struct device_attribute *attr,
174173606afdSDan Williams 		char *buf)
174273606afdSDan Williams {
174373606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
174473606afdSDan Williams 
174573606afdSDan Williams 	if (dimm < 0)
174673606afdSDan Williams 		return dimm;
174773606afdSDan Williams 
174873606afdSDan Williams 	return sprintf(buf, "%#lx\n", dimm_fail_cmd_flags[dimm]);
174973606afdSDan Williams }
175073606afdSDan Williams 
fail_cmd_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)175173606afdSDan Williams static ssize_t fail_cmd_store(struct device *dev, struct device_attribute *attr,
175273606afdSDan Williams 		const char *buf, size_t size)
175373606afdSDan Williams {
175473606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
175573606afdSDan Williams 	unsigned long val;
175673606afdSDan Williams 	ssize_t rc;
175773606afdSDan Williams 
175873606afdSDan Williams 	if (dimm < 0)
175973606afdSDan Williams 		return dimm;
176073606afdSDan Williams 
176173606afdSDan Williams 	rc = kstrtol(buf, 0, &val);
176273606afdSDan Williams 	if (rc)
176373606afdSDan Williams 		return rc;
176473606afdSDan Williams 
176573606afdSDan Williams 	dimm_fail_cmd_flags[dimm] = val;
176673606afdSDan Williams 	return size;
176773606afdSDan Williams }
176873606afdSDan Williams static DEVICE_ATTR_RW(fail_cmd);
176973606afdSDan Williams 
fail_cmd_code_show(struct device * dev,struct device_attribute * attr,char * buf)177055c72ab6SDan Williams static ssize_t fail_cmd_code_show(struct device *dev, struct device_attribute *attr,
177155c72ab6SDan Williams 		char *buf)
177255c72ab6SDan Williams {
177355c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
177455c72ab6SDan Williams 
177555c72ab6SDan Williams 	if (dimm < 0)
177655c72ab6SDan Williams 		return dimm;
177755c72ab6SDan Williams 
177855c72ab6SDan Williams 	return sprintf(buf, "%d\n", dimm_fail_cmd_code[dimm]);
177955c72ab6SDan Williams }
178055c72ab6SDan Williams 
fail_cmd_code_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)178155c72ab6SDan Williams static ssize_t fail_cmd_code_store(struct device *dev, struct device_attribute *attr,
178255c72ab6SDan Williams 		const char *buf, size_t size)
178355c72ab6SDan Williams {
178455c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
178555c72ab6SDan Williams 	unsigned long val;
178655c72ab6SDan Williams 	ssize_t rc;
178755c72ab6SDan Williams 
178855c72ab6SDan Williams 	if (dimm < 0)
178955c72ab6SDan Williams 		return dimm;
179055c72ab6SDan Williams 
179155c72ab6SDan Williams 	rc = kstrtol(buf, 0, &val);
179255c72ab6SDan Williams 	if (rc)
179355c72ab6SDan Williams 		return rc;
179455c72ab6SDan Williams 
179555c72ab6SDan Williams 	dimm_fail_cmd_code[dimm] = val;
179655c72ab6SDan Williams 	return size;
179755c72ab6SDan Williams }
179855c72ab6SDan Williams static DEVICE_ATTR_RW(fail_cmd_code);
179955c72ab6SDan Williams 
lock_dimm_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)18003c13e2acSDave Jiang static ssize_t lock_dimm_store(struct device *dev,
18013c13e2acSDave Jiang 		struct device_attribute *attr, const char *buf, size_t size)
18023c13e2acSDave Jiang {
18033c13e2acSDave Jiang 	int dimm = dimm_name_to_id(dev);
18043c13e2acSDave Jiang 	struct nfit_test_sec *sec = &dimm_sec_info[dimm];
18053c13e2acSDave Jiang 
18063c13e2acSDave Jiang 	sec->state = ND_INTEL_SEC_STATE_ENABLED | ND_INTEL_SEC_STATE_LOCKED;
18073c13e2acSDave Jiang 	return size;
18083c13e2acSDave Jiang }
18093c13e2acSDave Jiang static DEVICE_ATTR_WO(lock_dimm);
18103c13e2acSDave Jiang 
181173606afdSDan Williams static struct attribute *nfit_test_dimm_attributes[] = {
181273606afdSDan Williams 	&dev_attr_fail_cmd.attr,
181355c72ab6SDan Williams 	&dev_attr_fail_cmd_code.attr,
181473606afdSDan Williams 	&dev_attr_handle.attr,
18153c13e2acSDave Jiang 	&dev_attr_lock_dimm.attr,
181673606afdSDan Williams 	NULL,
181773606afdSDan Williams };
181873606afdSDan Williams 
181973606afdSDan Williams static struct attribute_group nfit_test_dimm_attribute_group = {
182073606afdSDan Williams 	.attrs = nfit_test_dimm_attributes,
182173606afdSDan Williams };
182273606afdSDan Williams 
182373606afdSDan Williams static const struct attribute_group *nfit_test_dimm_attribute_groups[] = {
182473606afdSDan Williams 	&nfit_test_dimm_attribute_group,
182573606afdSDan Williams 	NULL,
182673606afdSDan Williams };
182773606afdSDan Williams 
nfit_test_dimm_init(struct nfit_test * t)1828718fda67SDan Williams static int nfit_test_dimm_init(struct nfit_test *t)
1829718fda67SDan Williams {
1830718fda67SDan Williams 	int i;
1831718fda67SDan Williams 
1832718fda67SDan Williams 	if (devm_add_action_or_reset(&t->pdev.dev, put_dimms, t))
1833718fda67SDan Williams 		return -ENOMEM;
1834718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1835dd6cad2dSGreg Kroah-Hartman 		t->dimm_dev[i] = device_create_with_groups(&nfit_test_dimm,
1836718fda67SDan Williams 				&t->pdev.dev, 0, NULL,
1837718fda67SDan Williams 				nfit_test_dimm_attribute_groups,
1838718fda67SDan Williams 				"test_dimm%d", i + t->dcr_idx);
1839718fda67SDan Williams 		if (!t->dimm_dev[i])
1840718fda67SDan Williams 			return -ENOMEM;
1841718fda67SDan Williams 	}
1842718fda67SDan Williams 	return 0;
1843718fda67SDan Williams }
1844718fda67SDan Williams 
nfit_security_init(struct nfit_test * t)1845e8cf229eSDan Williams static void nfit_security_init(struct nfit_test *t)
1846ecaa4a97SDave Jiang {
1847ecaa4a97SDave Jiang 	int i;
1848ecaa4a97SDave Jiang 
1849ecaa4a97SDave Jiang 	for (i = 0; i < t->num_dcr; i++) {
1850ecaa4a97SDave Jiang 		struct nfit_test_sec *sec = &dimm_sec_info[i];
1851ecaa4a97SDave Jiang 
1852ecaa4a97SDave Jiang 		sec->ext_state = ND_INTEL_SEC_ESTATE_ENABLED;
1853ecaa4a97SDave Jiang 	}
1854ecaa4a97SDave Jiang }
1855ecaa4a97SDave Jiang 
smart_init(struct nfit_test * t)1856ed07c433SDan Williams static void smart_init(struct nfit_test *t)
1857ed07c433SDan Williams {
1858ed07c433SDan Williams 	int i;
1859ed07c433SDan Williams 	const struct nd_intel_smart_threshold smart_t_data = {
1860ed07c433SDan Williams 		.alarm_control = ND_INTEL_SMART_SPARE_TRIP
1861ed07c433SDan Williams 			| ND_INTEL_SMART_TEMP_TRIP,
1862ed07c433SDan Williams 		.media_temperature = 40 * 16,
1863ed07c433SDan Williams 		.ctrl_temperature = 30 * 16,
1864ed07c433SDan Williams 		.spares = 5,
1865ed07c433SDan Williams 	};
1866ed07c433SDan Williams 
1867ed07c433SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1868b4d4702fSVishal Verma 		memcpy(&t->smart[i], &smart_def, sizeof(smart_def));
1869ed07c433SDan Williams 		memcpy(&t->smart_threshold[i], &smart_t_data,
1870ed07c433SDan Williams 				sizeof(smart_t_data));
1871ed07c433SDan Williams 	}
1872ed07c433SDan Williams }
1873ed07c433SDan Williams 
sizeof_spa(struct acpi_nfit_system_address * spa)1874e9cfd259SDan Williams static size_t sizeof_spa(struct acpi_nfit_system_address *spa)
1875e9cfd259SDan Williams {
1876e9cfd259SDan Williams 	/* until spa location cookie support is added... */
1877e9cfd259SDan Williams 	return sizeof(*spa) - 8;
1878e9cfd259SDan Williams }
1879e9cfd259SDan Williams 
nfit_test0_alloc(struct nfit_test * t)18806bc75619SDan Williams static int nfit_test0_alloc(struct nfit_test *t)
18816bc75619SDan Williams {
1882e9cfd259SDan Williams 	struct acpi_nfit_system_address *spa = NULL;
188374522feaSKees Cook 	struct acpi_nfit_flush_address *flush;
1884e9cfd259SDan Williams 	size_t nfit_size = sizeof_spa(spa) * NUM_SPA
18856bc75619SDan Williams 			+ sizeof(struct acpi_nfit_memory_map) * NUM_MEM
18866bc75619SDan Williams 			+ sizeof(struct acpi_nfit_control_region) * NUM_DCR
18873b87356fSDan Williams 			+ offsetof(struct acpi_nfit_control_region,
18883b87356fSDan Williams 					window_size) * NUM_DCR
18899d27a87eSDan Williams 			+ sizeof(struct acpi_nfit_data_region) * NUM_BDW
189074522feaSKees Cook 			+ struct_size(flush, hint_address, NUM_HINTS) * NUM_DCR
1891f81e1d35SDave Jiang 			+ sizeof(struct acpi_nfit_capabilities);
18926bc75619SDan Williams 	int i;
18936bc75619SDan Williams 
18946bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
18956bc75619SDan Williams 	if (!t->nfit_buf)
18966bc75619SDan Williams 		return -ENOMEM;
18976bc75619SDan Williams 	t->nfit_size = nfit_size;
18986bc75619SDan Williams 
1899ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[0]);
19006bc75619SDan Williams 	if (!t->spa_set[0])
19016bc75619SDan Williams 		return -ENOMEM;
19026bc75619SDan Williams 
1903ee8520feSDan Williams 	t->spa_set[1] = test_alloc(t, SPA1_SIZE, &t->spa_set_dma[1]);
19046bc75619SDan Williams 	if (!t->spa_set[1])
19056bc75619SDan Williams 		return -ENOMEM;
19066bc75619SDan Williams 
1907ee8520feSDan Williams 	t->spa_set[2] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[2]);
190820985164SVishal Verma 	if (!t->spa_set[2])
190920985164SVishal Verma 		return -ENOMEM;
191020985164SVishal Verma 
1911dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
19126bc75619SDan Williams 		t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
19136bc75619SDan Williams 		if (!t->dimm[i])
19146bc75619SDan Williams 			return -ENOMEM;
19156bc75619SDan Williams 
19166bc75619SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
19176bc75619SDan Williams 		if (!t->label[i])
19186bc75619SDan Williams 			return -ENOMEM;
19196bc75619SDan Williams 		sprintf(t->label[i], "label%d", i);
19209d27a87eSDan Williams 
19219d15ce9cSDan Williams 		t->flush[i] = test_alloc(t, max(PAGE_SIZE,
19229d15ce9cSDan Williams 					sizeof(u64) * NUM_HINTS),
192385d3fa02SDan Williams 				&t->flush_dma[i]);
19249d27a87eSDan Williams 		if (!t->flush[i])
19259d27a87eSDan Williams 			return -ENOMEM;
19266bc75619SDan Williams 	}
19276bc75619SDan Williams 
1928dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
19296bc75619SDan Williams 		t->dcr[i] = test_alloc(t, LABEL_SIZE, &t->dcr_dma[i]);
19306bc75619SDan Williams 		if (!t->dcr[i])
19316bc75619SDan Williams 			return -ENOMEM;
19326bc75619SDan Williams 	}
19336bc75619SDan Williams 
1934c14a868aSDan Williams 	t->_fit = test_alloc(t, sizeof(union acpi_object **), &t->_fit_dma);
1935c14a868aSDan Williams 	if (!t->_fit)
1936c14a868aSDan Williams 		return -ENOMEM;
1937c14a868aSDan Williams 
1938718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1939231bf117SDan Williams 		return -ENOMEM;
1940ed07c433SDan Williams 	smart_init(t);
1941e8cf229eSDan Williams 	nfit_security_init(t);
1942f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
19436bc75619SDan Williams }
19446bc75619SDan Williams 
nfit_test1_alloc(struct nfit_test * t)19456bc75619SDan Williams static int nfit_test1_alloc(struct nfit_test *t)
19466bc75619SDan Williams {
1947e9cfd259SDan Williams 	struct acpi_nfit_system_address *spa = NULL;
1948e9cfd259SDan Williams 	size_t nfit_size = sizeof_spa(spa) * 2
1949ac40b675SDan Williams 		+ sizeof(struct acpi_nfit_memory_map) * 2
1950ac40b675SDan Williams 		+ offsetof(struct acpi_nfit_control_region, window_size) * 2;
1951dafb1048SDan Williams 	int i;
19526bc75619SDan Williams 
19536bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
19546bc75619SDan Williams 	if (!t->nfit_buf)
19556bc75619SDan Williams 		return -ENOMEM;
19566bc75619SDan Williams 	t->nfit_size = nfit_size;
19576bc75619SDan Williams 
1958ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA2_SIZE, &t->spa_set_dma[0]);
19596bc75619SDan Williams 	if (!t->spa_set[0])
19606bc75619SDan Williams 		return -ENOMEM;
19616bc75619SDan Williams 
1962dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1963dafb1048SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
1964dafb1048SDan Williams 		if (!t->label[i])
1965dafb1048SDan Williams 			return -ENOMEM;
1966dafb1048SDan Williams 		sprintf(t->label[i], "label%d", i);
1967dafb1048SDan Williams 	}
1968dafb1048SDan Williams 
19697bfe97c7SDan Williams 	t->spa_set[1] = test_alloc(t, SPA_VCD_SIZE, &t->spa_set_dma[1]);
19707bfe97c7SDan Williams 	if (!t->spa_set[1])
19717bfe97c7SDan Williams 		return -ENOMEM;
19727bfe97c7SDan Williams 
1973718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1974718fda67SDan Williams 		return -ENOMEM;
1975ed07c433SDan Williams 	smart_init(t);
1976f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
19776bc75619SDan Williams }
19786bc75619SDan Williams 
dcr_common_init(struct acpi_nfit_control_region * dcr)19795dc68e55SDan Williams static void dcr_common_init(struct acpi_nfit_control_region *dcr)
19805dc68e55SDan Williams {
19815dc68e55SDan Williams 	dcr->vendor_id = 0xabcd;
19825dc68e55SDan Williams 	dcr->device_id = 0;
19835dc68e55SDan Williams 	dcr->revision_id = 1;
19845dc68e55SDan Williams 	dcr->valid_fields = 1;
19855dc68e55SDan Williams 	dcr->manufacturing_location = 0xa;
19865dc68e55SDan Williams 	dcr->manufacturing_date = cpu_to_be16(2016);
19875dc68e55SDan Williams }
19885dc68e55SDan Williams 
nfit_test0_setup(struct nfit_test * t)19896bc75619SDan Williams static void nfit_test0_setup(struct nfit_test *t)
19906bc75619SDan Williams {
199185d3fa02SDan Williams 	const int flush_hint_size = sizeof(struct acpi_nfit_flush_address)
199285d3fa02SDan Williams 		+ (sizeof(u64) * NUM_HINTS);
19936bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
19946bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
19956bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
19966bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
19976bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
19986bc75619SDan Williams 	struct acpi_nfit_data_region *bdw;
19999d27a87eSDan Williams 	struct acpi_nfit_flush_address *flush;
2000f81e1d35SDave Jiang 	struct acpi_nfit_capabilities *pcap;
2001d7d8464dSRoss Zwisler 	unsigned int offset = 0, i;
2002916566aeSDan Williams 	unsigned long *acpi_mask;
20036bc75619SDan Williams 
20046bc75619SDan Williams 	/*
20056bc75619SDan Williams 	 * spa0 (interleave first half of dimm0 and dimm1, note storage
20066bc75619SDan Williams 	 * does not actually alias the related block-data-window
20076bc75619SDan Williams 	 * regions)
20086bc75619SDan Williams 	 */
20096b577c9dSLinda Knippers 	spa = nfit_buf;
20106bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
2011e9cfd259SDan Williams 	spa->header.length = sizeof_spa(spa);
20126bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
20136bc75619SDan Williams 	spa->range_index = 0+1;
20146bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
20156bc75619SDan Williams 	spa->length = SPA0_SIZE;
2016d7d8464dSRoss Zwisler 	offset += spa->header.length;
20176bc75619SDan Williams 
20186bc75619SDan Williams 	/*
20196bc75619SDan Williams 	 * spa1 (interleave last half of the 4 DIMMS, note storage
20206bc75619SDan Williams 	 * does not actually alias the related block-data-window
20216bc75619SDan Williams 	 * regions)
20226bc75619SDan Williams 	 */
2023d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
20246bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
2025e9cfd259SDan Williams 	spa->header.length = sizeof_spa(spa);
20266bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
20276bc75619SDan Williams 	spa->range_index = 1+1;
20286bc75619SDan Williams 	spa->address = t->spa_set_dma[1];
20296bc75619SDan Williams 	spa->length = SPA1_SIZE;
2030d7d8464dSRoss Zwisler 	offset += spa->header.length;
20316bc75619SDan Williams 
20326bc75619SDan Williams 	/* spa2 (dcr0) dimm0 */
2033d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
20346bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
2035e9cfd259SDan Williams 	spa->header.length = sizeof_spa(spa);
20366bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
20376bc75619SDan Williams 	spa->range_index = 2+1;
20386bc75619SDan Williams 	spa->address = t->dcr_dma[0];
20396bc75619SDan Williams 	spa->length = DCR_SIZE;
2040d7d8464dSRoss Zwisler 	offset += spa->header.length;
20416bc75619SDan Williams 
20426bc75619SDan Williams 	/* spa3 (dcr1) dimm1 */
2043d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
20446bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
2045e9cfd259SDan Williams 	spa->header.length = sizeof_spa(spa);
20466bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
20476bc75619SDan Williams 	spa->range_index = 3+1;
20486bc75619SDan Williams 	spa->address = t->dcr_dma[1];
20496bc75619SDan Williams 	spa->length = DCR_SIZE;
2050d7d8464dSRoss Zwisler 	offset += spa->header.length;
20516bc75619SDan Williams 
20526bc75619SDan Williams 	/* spa4 (dcr2) dimm2 */
2053d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
20546bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
2055e9cfd259SDan Williams 	spa->header.length = sizeof_spa(spa);
20566bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
20576bc75619SDan Williams 	spa->range_index = 4+1;
20586bc75619SDan Williams 	spa->address = t->dcr_dma[2];
20596bc75619SDan Williams 	spa->length = DCR_SIZE;
2060d7d8464dSRoss Zwisler 	offset += spa->header.length;
20616bc75619SDan Williams 
20626bc75619SDan Williams 	/* spa5 (dcr3) dimm3 */
2063d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
20646bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
2065e9cfd259SDan Williams 	spa->header.length = sizeof_spa(spa);
20666bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
20676bc75619SDan Williams 	spa->range_index = 5+1;
20686bc75619SDan Williams 	spa->address = t->dcr_dma[3];
20696bc75619SDan Williams 	spa->length = DCR_SIZE;
2070d7d8464dSRoss Zwisler 	offset += spa->header.length;
20716bc75619SDan Williams 
20726bc75619SDan Williams 	/* spa6 (bdw for dcr0) dimm0 */
2073d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
20746bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
2075e9cfd259SDan Williams 	spa->header.length = sizeof_spa(spa);
20766bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
20776bc75619SDan Williams 	spa->range_index = 6+1;
20786bc75619SDan Williams 	spa->address = t->dimm_dma[0];
20796bc75619SDan Williams 	spa->length = DIMM_SIZE;
2080d7d8464dSRoss Zwisler 	offset += spa->header.length;
20816bc75619SDan Williams 
20826bc75619SDan Williams 	/* spa7 (bdw for dcr1) dimm1 */
2083d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
20846bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
2085e9cfd259SDan Williams 	spa->header.length = sizeof_spa(spa);
20866bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
20876bc75619SDan Williams 	spa->range_index = 7+1;
20886bc75619SDan Williams 	spa->address = t->dimm_dma[1];
20896bc75619SDan Williams 	spa->length = DIMM_SIZE;
2090d7d8464dSRoss Zwisler 	offset += spa->header.length;
20916bc75619SDan Williams 
20926bc75619SDan Williams 	/* spa8 (bdw for dcr2) dimm2 */
2093d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
20946bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
2095e9cfd259SDan Williams 	spa->header.length = sizeof_spa(spa);
20966bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
20976bc75619SDan Williams 	spa->range_index = 8+1;
20986bc75619SDan Williams 	spa->address = t->dimm_dma[2];
20996bc75619SDan Williams 	spa->length = DIMM_SIZE;
2100d7d8464dSRoss Zwisler 	offset += spa->header.length;
21016bc75619SDan Williams 
21026bc75619SDan Williams 	/* spa9 (bdw for dcr3) dimm3 */
2103d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
21046bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
2105e9cfd259SDan Williams 	spa->header.length = sizeof_spa(spa);
21066bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
21076bc75619SDan Williams 	spa->range_index = 9+1;
21086bc75619SDan Williams 	spa->address = t->dimm_dma[3];
21096bc75619SDan Williams 	spa->length = DIMM_SIZE;
2110d7d8464dSRoss Zwisler 	offset += spa->header.length;
21116bc75619SDan Williams 
21126bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
21136bc75619SDan Williams 	memdev = nfit_buf + offset;
21146bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21156bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21166bc75619SDan Williams 	memdev->device_handle = handle[0];
21176bc75619SDan Williams 	memdev->physical_id = 0;
21186bc75619SDan Williams 	memdev->region_id = 0;
21196bc75619SDan Williams 	memdev->range_index = 0+1;
21203b87356fSDan Williams 	memdev->region_index = 4+1;
21216bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
2122df06a2d5SDan Williams 	memdev->region_offset = 1;
21236bc75619SDan Williams 	memdev->address = 0;
21246bc75619SDan Williams 	memdev->interleave_index = 0;
21256bc75619SDan Williams 	memdev->interleave_ways = 2;
2126d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21276bc75619SDan Williams 
21286bc75619SDan Williams 	/* mem-region1 (spa0, dimm1) */
2129d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21306bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21316bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21326bc75619SDan Williams 	memdev->device_handle = handle[1];
21336bc75619SDan Williams 	memdev->physical_id = 1;
21346bc75619SDan Williams 	memdev->region_id = 0;
21356bc75619SDan Williams 	memdev->range_index = 0+1;
21363b87356fSDan Williams 	memdev->region_index = 5+1;
21376bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
2138df06a2d5SDan Williams 	memdev->region_offset = (1 << 8);
21396bc75619SDan Williams 	memdev->address = 0;
21406bc75619SDan Williams 	memdev->interleave_index = 0;
21416bc75619SDan Williams 	memdev->interleave_ways = 2;
2142ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2143d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21446bc75619SDan Williams 
21456bc75619SDan Williams 	/* mem-region2 (spa1, dimm0) */
2146d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21476bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21486bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21496bc75619SDan Williams 	memdev->device_handle = handle[0];
21506bc75619SDan Williams 	memdev->physical_id = 0;
21516bc75619SDan Williams 	memdev->region_id = 1;
21526bc75619SDan Williams 	memdev->range_index = 1+1;
21533b87356fSDan Williams 	memdev->region_index = 4+1;
21546bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
2155df06a2d5SDan Williams 	memdev->region_offset = (1 << 16);
21566bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
21576bc75619SDan Williams 	memdev->interleave_index = 0;
21586bc75619SDan Williams 	memdev->interleave_ways = 4;
2159ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2160d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21616bc75619SDan Williams 
21626bc75619SDan Williams 	/* mem-region3 (spa1, dimm1) */
2163d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21646bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21656bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21666bc75619SDan Williams 	memdev->device_handle = handle[1];
21676bc75619SDan Williams 	memdev->physical_id = 1;
21686bc75619SDan Williams 	memdev->region_id = 1;
21696bc75619SDan Williams 	memdev->range_index = 1+1;
21703b87356fSDan Williams 	memdev->region_index = 5+1;
21716bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
2172df06a2d5SDan Williams 	memdev->region_offset = (1 << 24);
21736bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
21746bc75619SDan Williams 	memdev->interleave_index = 0;
21756bc75619SDan Williams 	memdev->interleave_ways = 4;
2176d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21776bc75619SDan Williams 
21786bc75619SDan Williams 	/* mem-region4 (spa1, dimm2) */
2179d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21806bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21816bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21826bc75619SDan Williams 	memdev->device_handle = handle[2];
21836bc75619SDan Williams 	memdev->physical_id = 2;
21846bc75619SDan Williams 	memdev->region_id = 0;
21856bc75619SDan Williams 	memdev->range_index = 1+1;
21863b87356fSDan Williams 	memdev->region_index = 6+1;
21876bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
2188df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 32);
21896bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
21906bc75619SDan Williams 	memdev->interleave_index = 0;
21916bc75619SDan Williams 	memdev->interleave_ways = 4;
2192ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2193d7d8464dSRoss Zwisler 	offset += memdev->header.length;
21946bc75619SDan Williams 
21956bc75619SDan Williams 	/* mem-region5 (spa1, dimm3) */
2196d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
21976bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21986bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
21996bc75619SDan Williams 	memdev->device_handle = handle[3];
22006bc75619SDan Williams 	memdev->physical_id = 3;
22016bc75619SDan Williams 	memdev->region_id = 0;
22026bc75619SDan Williams 	memdev->range_index = 1+1;
22033b87356fSDan Williams 	memdev->region_index = 7+1;
22046bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
2205df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 40);
22066bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
22076bc75619SDan Williams 	memdev->interleave_index = 0;
22086bc75619SDan Williams 	memdev->interleave_ways = 4;
2209d7d8464dSRoss Zwisler 	offset += memdev->header.length;
22106bc75619SDan Williams 
22116bc75619SDan Williams 	/* mem-region6 (spa/dcr0, dimm0) */
2212d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
22136bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
22146bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
22156bc75619SDan Williams 	memdev->device_handle = handle[0];
22166bc75619SDan Williams 	memdev->physical_id = 0;
22176bc75619SDan Williams 	memdev->region_id = 0;
22186bc75619SDan Williams 	memdev->range_index = 2+1;
22196bc75619SDan Williams 	memdev->region_index = 0+1;
22206bc75619SDan Williams 	memdev->region_size = 0;
22216bc75619SDan Williams 	memdev->region_offset = 0;
22226bc75619SDan Williams 	memdev->address = 0;
22236bc75619SDan Williams 	memdev->interleave_index = 0;
22246bc75619SDan Williams 	memdev->interleave_ways = 1;
2225d7d8464dSRoss Zwisler 	offset += memdev->header.length;
22266bc75619SDan Williams 
22276bc75619SDan Williams 	/* mem-region7 (spa/dcr1, dimm1) */
2228d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
22296bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
22306bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
22316bc75619SDan Williams 	memdev->device_handle = handle[1];
22326bc75619SDan Williams 	memdev->physical_id = 1;
22336bc75619SDan Williams 	memdev->region_id = 0;
22346bc75619SDan Williams 	memdev->range_index = 3+1;
22356bc75619SDan Williams 	memdev->region_index = 1+1;
22366bc75619SDan Williams 	memdev->region_size = 0;
22376bc75619SDan Williams 	memdev->region_offset = 0;
22386bc75619SDan Williams 	memdev->address = 0;
22396bc75619SDan Williams 	memdev->interleave_index = 0;
22406bc75619SDan Williams 	memdev->interleave_ways = 1;
2241d7d8464dSRoss Zwisler 	offset += memdev->header.length;
22426bc75619SDan Williams 
22436bc75619SDan Williams 	/* mem-region8 (spa/dcr2, dimm2) */
2244d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
22456bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
22466bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
22476bc75619SDan Williams 	memdev->device_handle = handle[2];
22486bc75619SDan Williams 	memdev->physical_id = 2;
22496bc75619SDan Williams 	memdev->region_id = 0;
22506bc75619SDan Williams 	memdev->range_index = 4+1;
22516bc75619SDan Williams 	memdev->region_index = 2+1;
22526bc75619SDan Williams 	memdev->region_size = 0;
22536bc75619SDan Williams 	memdev->region_offset = 0;
22546bc75619SDan Williams 	memdev->address = 0;
22556bc75619SDan Williams 	memdev->interleave_index = 0;
22566bc75619SDan Williams 	memdev->interleave_ways = 1;
2257d7d8464dSRoss Zwisler 	offset += memdev->header.length;
22586bc75619SDan Williams 
22596bc75619SDan Williams 	/* mem-region9 (spa/dcr3, dimm3) */
2260d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
22616bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
22626bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
22636bc75619SDan Williams 	memdev->device_handle = handle[3];
22646bc75619SDan Williams 	memdev->physical_id = 3;
22656bc75619SDan Williams 	memdev->region_id = 0;
22666bc75619SDan Williams 	memdev->range_index = 5+1;
22676bc75619SDan Williams 	memdev->region_index = 3+1;
22686bc75619SDan Williams 	memdev->region_size = 0;
22696bc75619SDan Williams 	memdev->region_offset = 0;
22706bc75619SDan Williams 	memdev->address = 0;
22716bc75619SDan Williams 	memdev->interleave_index = 0;
22726bc75619SDan Williams 	memdev->interleave_ways = 1;
2273d7d8464dSRoss Zwisler 	offset += memdev->header.length;
22746bc75619SDan Williams 
22756bc75619SDan Williams 	/* mem-region10 (spa/bdw0, dimm0) */
2276d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
22776bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
22786bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
22796bc75619SDan Williams 	memdev->device_handle = handle[0];
22806bc75619SDan Williams 	memdev->physical_id = 0;
22816bc75619SDan Williams 	memdev->region_id = 0;
22826bc75619SDan Williams 	memdev->range_index = 6+1;
22836bc75619SDan Williams 	memdev->region_index = 0+1;
22846bc75619SDan Williams 	memdev->region_size = 0;
22856bc75619SDan Williams 	memdev->region_offset = 0;
22866bc75619SDan Williams 	memdev->address = 0;
22876bc75619SDan Williams 	memdev->interleave_index = 0;
22886bc75619SDan Williams 	memdev->interleave_ways = 1;
2289d7d8464dSRoss Zwisler 	offset += memdev->header.length;
22906bc75619SDan Williams 
22916bc75619SDan Williams 	/* mem-region11 (spa/bdw1, dimm1) */
2292d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
22936bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
22946bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
22956bc75619SDan Williams 	memdev->device_handle = handle[1];
22966bc75619SDan Williams 	memdev->physical_id = 1;
22976bc75619SDan Williams 	memdev->region_id = 0;
22986bc75619SDan Williams 	memdev->range_index = 7+1;
22996bc75619SDan Williams 	memdev->region_index = 1+1;
23006bc75619SDan Williams 	memdev->region_size = 0;
23016bc75619SDan Williams 	memdev->region_offset = 0;
23026bc75619SDan Williams 	memdev->address = 0;
23036bc75619SDan Williams 	memdev->interleave_index = 0;
23046bc75619SDan Williams 	memdev->interleave_ways = 1;
2305d7d8464dSRoss Zwisler 	offset += memdev->header.length;
23066bc75619SDan Williams 
23076bc75619SDan Williams 	/* mem-region12 (spa/bdw2, dimm2) */
2308d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
23096bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
23106bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
23116bc75619SDan Williams 	memdev->device_handle = handle[2];
23126bc75619SDan Williams 	memdev->physical_id = 2;
23136bc75619SDan Williams 	memdev->region_id = 0;
23146bc75619SDan Williams 	memdev->range_index = 8+1;
23156bc75619SDan Williams 	memdev->region_index = 2+1;
23166bc75619SDan Williams 	memdev->region_size = 0;
23176bc75619SDan Williams 	memdev->region_offset = 0;
23186bc75619SDan Williams 	memdev->address = 0;
23196bc75619SDan Williams 	memdev->interleave_index = 0;
23206bc75619SDan Williams 	memdev->interleave_ways = 1;
2321d7d8464dSRoss Zwisler 	offset += memdev->header.length;
23226bc75619SDan Williams 
23236bc75619SDan Williams 	/* mem-region13 (spa/dcr3, dimm3) */
2324d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
23256bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
23266bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
23276bc75619SDan Williams 	memdev->device_handle = handle[3];
23286bc75619SDan Williams 	memdev->physical_id = 3;
23296bc75619SDan Williams 	memdev->region_id = 0;
23306bc75619SDan Williams 	memdev->range_index = 9+1;
23316bc75619SDan Williams 	memdev->region_index = 3+1;
23326bc75619SDan Williams 	memdev->region_size = 0;
23336bc75619SDan Williams 	memdev->region_offset = 0;
23346bc75619SDan Williams 	memdev->address = 0;
23356bc75619SDan Williams 	memdev->interleave_index = 0;
23366bc75619SDan Williams 	memdev->interleave_ways = 1;
2337ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2338d7d8464dSRoss Zwisler 	offset += memdev->header.length;
23396bc75619SDan Williams 
23403b87356fSDan Williams 	/* dcr-descriptor0: blk */
23416bc75619SDan Williams 	dcr = nfit_buf + offset;
23426bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2343d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
23446bc75619SDan Williams 	dcr->region_index = 0+1;
23455dc68e55SDan Williams 	dcr_common_init(dcr);
23466bc75619SDan Williams 	dcr->serial_number = ~handle[0];
2347be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
23486bc75619SDan Williams 	dcr->windows = 1;
23496bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
23506bc75619SDan Williams 	dcr->command_offset = 0;
23516bc75619SDan Williams 	dcr->command_size = 8;
23526bc75619SDan Williams 	dcr->status_offset = 8;
23536bc75619SDan Williams 	dcr->status_size = 4;
2354d7d8464dSRoss Zwisler 	offset += dcr->header.length;
23556bc75619SDan Williams 
23563b87356fSDan Williams 	/* dcr-descriptor1: blk */
2357d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
23586bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2359d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
23606bc75619SDan Williams 	dcr->region_index = 1+1;
23615dc68e55SDan Williams 	dcr_common_init(dcr);
23626bc75619SDan Williams 	dcr->serial_number = ~handle[1];
2363be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
23646bc75619SDan Williams 	dcr->windows = 1;
23656bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
23666bc75619SDan Williams 	dcr->command_offset = 0;
23676bc75619SDan Williams 	dcr->command_size = 8;
23686bc75619SDan Williams 	dcr->status_offset = 8;
23696bc75619SDan Williams 	dcr->status_size = 4;
2370d7d8464dSRoss Zwisler 	offset += dcr->header.length;
23716bc75619SDan Williams 
23723b87356fSDan Williams 	/* dcr-descriptor2: blk */
2373d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
23746bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2375d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
23766bc75619SDan Williams 	dcr->region_index = 2+1;
23775dc68e55SDan Williams 	dcr_common_init(dcr);
23786bc75619SDan Williams 	dcr->serial_number = ~handle[2];
2379be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
23806bc75619SDan Williams 	dcr->windows = 1;
23816bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
23826bc75619SDan Williams 	dcr->command_offset = 0;
23836bc75619SDan Williams 	dcr->command_size = 8;
23846bc75619SDan Williams 	dcr->status_offset = 8;
23856bc75619SDan Williams 	dcr->status_size = 4;
2386d7d8464dSRoss Zwisler 	offset += dcr->header.length;
23876bc75619SDan Williams 
23883b87356fSDan Williams 	/* dcr-descriptor3: blk */
2389d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
23906bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2391d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
23926bc75619SDan Williams 	dcr->region_index = 3+1;
23935dc68e55SDan Williams 	dcr_common_init(dcr);
23946bc75619SDan Williams 	dcr->serial_number = ~handle[3];
2395be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
23966bc75619SDan Williams 	dcr->windows = 1;
23976bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
23986bc75619SDan Williams 	dcr->command_offset = 0;
23996bc75619SDan Williams 	dcr->command_size = 8;
24006bc75619SDan Williams 	dcr->status_offset = 8;
24016bc75619SDan Williams 	dcr->status_size = 4;
2402d7d8464dSRoss Zwisler 	offset += dcr->header.length;
24036bc75619SDan Williams 
24043b87356fSDan Williams 	/* dcr-descriptor0: pmem */
24053b87356fSDan Williams 	dcr = nfit_buf + offset;
24063b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
24073b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
24083b87356fSDan Williams 			window_size);
24093b87356fSDan Williams 	dcr->region_index = 4+1;
24105dc68e55SDan Williams 	dcr_common_init(dcr);
24113b87356fSDan Williams 	dcr->serial_number = ~handle[0];
24123b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
24133b87356fSDan Williams 	dcr->windows = 0;
2414d7d8464dSRoss Zwisler 	offset += dcr->header.length;
24153b87356fSDan Williams 
24163b87356fSDan Williams 	/* dcr-descriptor1: pmem */
2417d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
24183b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
24193b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
24203b87356fSDan Williams 			window_size);
24213b87356fSDan Williams 	dcr->region_index = 5+1;
24225dc68e55SDan Williams 	dcr_common_init(dcr);
24233b87356fSDan Williams 	dcr->serial_number = ~handle[1];
24243b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
24253b87356fSDan Williams 	dcr->windows = 0;
2426d7d8464dSRoss Zwisler 	offset += dcr->header.length;
24273b87356fSDan Williams 
24283b87356fSDan Williams 	/* dcr-descriptor2: pmem */
2429d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
24303b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
24313b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
24323b87356fSDan Williams 			window_size);
24333b87356fSDan Williams 	dcr->region_index = 6+1;
24345dc68e55SDan Williams 	dcr_common_init(dcr);
24353b87356fSDan Williams 	dcr->serial_number = ~handle[2];
24363b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
24373b87356fSDan Williams 	dcr->windows = 0;
2438d7d8464dSRoss Zwisler 	offset += dcr->header.length;
24393b87356fSDan Williams 
24403b87356fSDan Williams 	/* dcr-descriptor3: pmem */
2441d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
24423b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
24433b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
24443b87356fSDan Williams 			window_size);
24453b87356fSDan Williams 	dcr->region_index = 7+1;
24465dc68e55SDan Williams 	dcr_common_init(dcr);
24473b87356fSDan Williams 	dcr->serial_number = ~handle[3];
24483b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
24493b87356fSDan Williams 	dcr->windows = 0;
2450d7d8464dSRoss Zwisler 	offset += dcr->header.length;
24513b87356fSDan Williams 
24526bc75619SDan Williams 	/* bdw0 (spa/dcr0, dimm0) */
24536bc75619SDan Williams 	bdw = nfit_buf + offset;
24546bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2455d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
24566bc75619SDan Williams 	bdw->region_index = 0+1;
24576bc75619SDan Williams 	bdw->windows = 1;
24586bc75619SDan Williams 	bdw->offset = 0;
24596bc75619SDan Williams 	bdw->size = BDW_SIZE;
24606bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
24616bc75619SDan Williams 	bdw->start_address = 0;
2462d7d8464dSRoss Zwisler 	offset += bdw->header.length;
24636bc75619SDan Williams 
24646bc75619SDan Williams 	/* bdw1 (spa/dcr1, dimm1) */
2465d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
24666bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2467d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
24686bc75619SDan Williams 	bdw->region_index = 1+1;
24696bc75619SDan Williams 	bdw->windows = 1;
24706bc75619SDan Williams 	bdw->offset = 0;
24716bc75619SDan Williams 	bdw->size = BDW_SIZE;
24726bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
24736bc75619SDan Williams 	bdw->start_address = 0;
2474d7d8464dSRoss Zwisler 	offset += bdw->header.length;
24756bc75619SDan Williams 
24766bc75619SDan Williams 	/* bdw2 (spa/dcr2, dimm2) */
2477d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
24786bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2479d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
24806bc75619SDan Williams 	bdw->region_index = 2+1;
24816bc75619SDan Williams 	bdw->windows = 1;
24826bc75619SDan Williams 	bdw->offset = 0;
24836bc75619SDan Williams 	bdw->size = BDW_SIZE;
24846bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
24856bc75619SDan Williams 	bdw->start_address = 0;
2486d7d8464dSRoss Zwisler 	offset += bdw->header.length;
24876bc75619SDan Williams 
24886bc75619SDan Williams 	/* bdw3 (spa/dcr3, dimm3) */
2489d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
24906bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2491d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
24926bc75619SDan Williams 	bdw->region_index = 3+1;
24936bc75619SDan Williams 	bdw->windows = 1;
24946bc75619SDan Williams 	bdw->offset = 0;
24956bc75619SDan Williams 	bdw->size = BDW_SIZE;
24966bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
24976bc75619SDan Williams 	bdw->start_address = 0;
2498d7d8464dSRoss Zwisler 	offset += bdw->header.length;
24996bc75619SDan Williams 
25009d27a87eSDan Williams 	/* flush0 (dimm0) */
25019d27a87eSDan Williams 	flush = nfit_buf + offset;
25029d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
250385d3fa02SDan Williams 	flush->header.length = flush_hint_size;
25049d27a87eSDan Williams 	flush->device_handle = handle[0];
250585d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
250685d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
250785d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[0] + i * sizeof(u64);
2508d7d8464dSRoss Zwisler 	offset += flush->header.length;
25099d27a87eSDan Williams 
25109d27a87eSDan Williams 	/* flush1 (dimm1) */
2511d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
25129d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
251385d3fa02SDan Williams 	flush->header.length = flush_hint_size;
25149d27a87eSDan Williams 	flush->device_handle = handle[1];
251585d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
251685d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
251785d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[1] + i * sizeof(u64);
2518d7d8464dSRoss Zwisler 	offset += flush->header.length;
25199d27a87eSDan Williams 
25209d27a87eSDan Williams 	/* flush2 (dimm2) */
2521d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
25229d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
252385d3fa02SDan Williams 	flush->header.length = flush_hint_size;
25249d27a87eSDan Williams 	flush->device_handle = handle[2];
252585d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
252685d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
252785d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[2] + i * sizeof(u64);
2528d7d8464dSRoss Zwisler 	offset += flush->header.length;
25299d27a87eSDan Williams 
25309d27a87eSDan Williams 	/* flush3 (dimm3) */
2531d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
25329d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
253385d3fa02SDan Williams 	flush->header.length = flush_hint_size;
25349d27a87eSDan Williams 	flush->device_handle = handle[3];
253585d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
253685d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
253785d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[3] + i * sizeof(u64);
2538d7d8464dSRoss Zwisler 	offset += flush->header.length;
25399d27a87eSDan Williams 
2540f81e1d35SDave Jiang 	/* platform capabilities */
2541d7d8464dSRoss Zwisler 	pcap = nfit_buf + offset;
2542f81e1d35SDave Jiang 	pcap->header.type = ACPI_NFIT_TYPE_CAPABILITIES;
2543f81e1d35SDave Jiang 	pcap->header.length = sizeof(*pcap);
2544f81e1d35SDave Jiang 	pcap->highest_capability = 1;
25451273c253SVishal Verma 	pcap->capabilities = ACPI_NFIT_CAPABILITY_MEM_FLUSH;
2546d7d8464dSRoss Zwisler 	offset += pcap->header.length;
2547f81e1d35SDave Jiang 
254820985164SVishal Verma 	if (t->setup_hotplug) {
25493b87356fSDan Williams 		/* dcr-descriptor4: blk */
255020985164SVishal Verma 		dcr = nfit_buf + offset;
255120985164SVishal Verma 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2552d7d8464dSRoss Zwisler 		dcr->header.length = sizeof(*dcr);
25533b87356fSDan Williams 		dcr->region_index = 8+1;
25545dc68e55SDan Williams 		dcr_common_init(dcr);
255520985164SVishal Verma 		dcr->serial_number = ~handle[4];
2556be26f9aeSDan Williams 		dcr->code = NFIT_FIC_BLK;
255720985164SVishal Verma 		dcr->windows = 1;
255820985164SVishal Verma 		dcr->window_size = DCR_SIZE;
255920985164SVishal Verma 		dcr->command_offset = 0;
256020985164SVishal Verma 		dcr->command_size = 8;
256120985164SVishal Verma 		dcr->status_offset = 8;
256220985164SVishal Verma 		dcr->status_size = 4;
2563d7d8464dSRoss Zwisler 		offset += dcr->header.length;
256420985164SVishal Verma 
25653b87356fSDan Williams 		/* dcr-descriptor4: pmem */
25663b87356fSDan Williams 		dcr = nfit_buf + offset;
25673b87356fSDan Williams 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
25683b87356fSDan Williams 		dcr->header.length = offsetof(struct acpi_nfit_control_region,
25693b87356fSDan Williams 				window_size);
25703b87356fSDan Williams 		dcr->region_index = 9+1;
25715dc68e55SDan Williams 		dcr_common_init(dcr);
25723b87356fSDan Williams 		dcr->serial_number = ~handle[4];
25733b87356fSDan Williams 		dcr->code = NFIT_FIC_BYTEN;
25743b87356fSDan Williams 		dcr->windows = 0;
2575d7d8464dSRoss Zwisler 		offset += dcr->header.length;
25763b87356fSDan Williams 
257720985164SVishal Verma 		/* bdw4 (spa/dcr4, dimm4) */
257820985164SVishal Verma 		bdw = nfit_buf + offset;
257920985164SVishal Verma 		bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2580d7d8464dSRoss Zwisler 		bdw->header.length = sizeof(*bdw);
25813b87356fSDan Williams 		bdw->region_index = 8+1;
258220985164SVishal Verma 		bdw->windows = 1;
258320985164SVishal Verma 		bdw->offset = 0;
258420985164SVishal Verma 		bdw->size = BDW_SIZE;
258520985164SVishal Verma 		bdw->capacity = DIMM_SIZE;
258620985164SVishal Verma 		bdw->start_address = 0;
2587d7d8464dSRoss Zwisler 		offset += bdw->header.length;
258820985164SVishal Verma 
258920985164SVishal Verma 		/* spa10 (dcr4) dimm4 */
259020985164SVishal Verma 		spa = nfit_buf + offset;
259120985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
2592e9cfd259SDan Williams 		spa->header.length = sizeof_spa(spa);
259320985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
259420985164SVishal Verma 		spa->range_index = 10+1;
259520985164SVishal Verma 		spa->address = t->dcr_dma[4];
259620985164SVishal Verma 		spa->length = DCR_SIZE;
2597d7d8464dSRoss Zwisler 		offset += spa->header.length;
259820985164SVishal Verma 
259920985164SVishal Verma 		/*
260020985164SVishal Verma 		 * spa11 (single-dimm interleave for hotplug, note storage
260120985164SVishal Verma 		 * does not actually alias the related block-data-window
260220985164SVishal Verma 		 * regions)
260320985164SVishal Verma 		 */
2604d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
260520985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
2606e9cfd259SDan Williams 		spa->header.length = sizeof_spa(spa);
260720985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
260820985164SVishal Verma 		spa->range_index = 11+1;
260920985164SVishal Verma 		spa->address = t->spa_set_dma[2];
261020985164SVishal Verma 		spa->length = SPA0_SIZE;
2611d7d8464dSRoss Zwisler 		offset += spa->header.length;
261220985164SVishal Verma 
261320985164SVishal Verma 		/* spa12 (bdw for dcr4) dimm4 */
2614d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
261520985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
2616e9cfd259SDan Williams 		spa->header.length = sizeof_spa(spa);
261720985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
261820985164SVishal Verma 		spa->range_index = 12+1;
261920985164SVishal Verma 		spa->address = t->dimm_dma[4];
262020985164SVishal Verma 		spa->length = DIMM_SIZE;
2621d7d8464dSRoss Zwisler 		offset += spa->header.length;
262220985164SVishal Verma 
262320985164SVishal Verma 		/* mem-region14 (spa/dcr4, dimm4) */
262420985164SVishal Verma 		memdev = nfit_buf + offset;
262520985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
262620985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
262720985164SVishal Verma 		memdev->device_handle = handle[4];
262820985164SVishal Verma 		memdev->physical_id = 4;
262920985164SVishal Verma 		memdev->region_id = 0;
263020985164SVishal Verma 		memdev->range_index = 10+1;
26313b87356fSDan Williams 		memdev->region_index = 8+1;
263220985164SVishal Verma 		memdev->region_size = 0;
263320985164SVishal Verma 		memdev->region_offset = 0;
263420985164SVishal Verma 		memdev->address = 0;
263520985164SVishal Verma 		memdev->interleave_index = 0;
263620985164SVishal Verma 		memdev->interleave_ways = 1;
2637d7d8464dSRoss Zwisler 		offset += memdev->header.length;
263820985164SVishal Verma 
2639d7d8464dSRoss Zwisler 		/* mem-region15 (spa11, dimm4) */
2640d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
264120985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
264220985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
264320985164SVishal Verma 		memdev->device_handle = handle[4];
264420985164SVishal Verma 		memdev->physical_id = 4;
264520985164SVishal Verma 		memdev->region_id = 0;
264620985164SVishal Verma 		memdev->range_index = 11+1;
26473b87356fSDan Williams 		memdev->region_index = 9+1;
264820985164SVishal Verma 		memdev->region_size = SPA0_SIZE;
2649df06a2d5SDan Williams 		memdev->region_offset = (1ULL << 48);
265020985164SVishal Verma 		memdev->address = 0;
265120985164SVishal Verma 		memdev->interleave_index = 0;
265220985164SVishal Verma 		memdev->interleave_ways = 1;
2653ac40b675SDan Williams 		memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2654d7d8464dSRoss Zwisler 		offset += memdev->header.length;
265520985164SVishal Verma 
26563b87356fSDan Williams 		/* mem-region16 (spa/bdw4, dimm4) */
2657d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
265820985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
265920985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
266020985164SVishal Verma 		memdev->device_handle = handle[4];
266120985164SVishal Verma 		memdev->physical_id = 4;
266220985164SVishal Verma 		memdev->region_id = 0;
266320985164SVishal Verma 		memdev->range_index = 12+1;
26643b87356fSDan Williams 		memdev->region_index = 8+1;
266520985164SVishal Verma 		memdev->region_size = 0;
266620985164SVishal Verma 		memdev->region_offset = 0;
266720985164SVishal Verma 		memdev->address = 0;
266820985164SVishal Verma 		memdev->interleave_index = 0;
266920985164SVishal Verma 		memdev->interleave_ways = 1;
2670d7d8464dSRoss Zwisler 		offset += memdev->header.length;
267120985164SVishal Verma 
267220985164SVishal Verma 		/* flush3 (dimm4) */
267320985164SVishal Verma 		flush = nfit_buf + offset;
267420985164SVishal Verma 		flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
267585d3fa02SDan Williams 		flush->header.length = flush_hint_size;
267620985164SVishal Verma 		flush->device_handle = handle[4];
267785d3fa02SDan Williams 		flush->hint_count = NUM_HINTS;
267885d3fa02SDan Williams 		for (i = 0; i < NUM_HINTS; i++)
267985d3fa02SDan Williams 			flush->hint_address[i] = t->flush_dma[4]
268085d3fa02SDan Williams 				+ i * sizeof(u64);
2681d7d8464dSRoss Zwisler 		offset += flush->header.length;
26829741a559SRoss Zwisler 
26839741a559SRoss Zwisler 		/* sanity check to make sure we've filled the buffer */
26849741a559SRoss Zwisler 		WARN_ON(offset != t->nfit_size);
268520985164SVishal Verma 	}
268620985164SVishal Verma 
26871526f9e2SRoss Zwisler 	t->nfit_filled = offset;
26881526f9e2SRoss Zwisler 
26899fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
26909fb1a190SDave Jiang 			SPA0_SIZE);
2691f471f1a7SDan Williams 
26926bc75619SDan Williams 	acpi_desc = &t->acpi_desc;
2693e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
2694e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2695e3654ecaSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2696ed07c433SDan Williams 	set_bit(ND_INTEL_SMART, &acpi_desc->dimm_cmd_force_en);
2697ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
2698ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_SET_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
26994cf260fcSVishal Verma 	set_bit(ND_INTEL_SMART_INJECT, &acpi_desc->dimm_cmd_force_en);
2700e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2701e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2702e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2703e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
270410246dc8SYasunori Goto 	set_bit(ND_CMD_CALL, &acpi_desc->bus_cmd_force_en);
2705d46e6a21SDan Williams 	set_bit(NFIT_CMD_TRANSLATE_SPA, &acpi_desc->bus_dsm_mask);
2706d46e6a21SDan Williams 	set_bit(NFIT_CMD_ARS_INJECT_SET, &acpi_desc->bus_dsm_mask);
2707d46e6a21SDan Williams 	set_bit(NFIT_CMD_ARS_INJECT_CLEAR, &acpi_desc->bus_dsm_mask);
2708d46e6a21SDan Williams 	set_bit(NFIT_CMD_ARS_INJECT_GET, &acpi_desc->bus_dsm_mask);
2709bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_GET_INFO, &acpi_desc->dimm_cmd_force_en);
2710bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_START_UPDATE, &acpi_desc->dimm_cmd_force_en);
2711bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_SEND_DATA, &acpi_desc->dimm_cmd_force_en);
2712bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_UPDATE, &acpi_desc->dimm_cmd_force_en);
2713bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_QUERY, &acpi_desc->dimm_cmd_force_en);
2714674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
27153c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_GET_SECURITY_STATE,
27163c13e2acSDave Jiang 			&acpi_desc->dimm_cmd_force_en);
27173c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_SET_PASSPHRASE, &acpi_desc->dimm_cmd_force_en);
27183c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_DISABLE_PASSPHRASE,
27193c13e2acSDave Jiang 			&acpi_desc->dimm_cmd_force_en);
27203c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_UNLOCK_UNIT, &acpi_desc->dimm_cmd_force_en);
27213c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_FREEZE_LOCK, &acpi_desc->dimm_cmd_force_en);
27223c13e2acSDave Jiang 	set_bit(NVDIMM_INTEL_SECURE_ERASE, &acpi_desc->dimm_cmd_force_en);
2723926f7480SDave Jiang 	set_bit(NVDIMM_INTEL_OVERWRITE, &acpi_desc->dimm_cmd_force_en);
2724926f7480SDave Jiang 	set_bit(NVDIMM_INTEL_QUERY_OVERWRITE, &acpi_desc->dimm_cmd_force_en);
2725ecaa4a97SDave Jiang 	set_bit(NVDIMM_INTEL_SET_MASTER_PASSPHRASE,
2726ecaa4a97SDave Jiang 			&acpi_desc->dimm_cmd_force_en);
2727ecaa4a97SDave Jiang 	set_bit(NVDIMM_INTEL_MASTER_SECURE_ERASE,
2728ecaa4a97SDave Jiang 			&acpi_desc->dimm_cmd_force_en);
2729916566aeSDan Williams 	set_bit(NVDIMM_INTEL_FW_ACTIVATE_DIMMINFO, &acpi_desc->dimm_cmd_force_en);
2730916566aeSDan Williams 	set_bit(NVDIMM_INTEL_FW_ACTIVATE_ARM, &acpi_desc->dimm_cmd_force_en);
2731916566aeSDan Williams 
2732916566aeSDan Williams 	acpi_mask = &acpi_desc->family_dsm_mask[NVDIMM_BUS_FAMILY_INTEL];
2733916566aeSDan Williams 	set_bit(NVDIMM_BUS_INTEL_FW_ACTIVATE_BUSINFO, acpi_mask);
2734916566aeSDan Williams 	set_bit(NVDIMM_BUS_INTEL_FW_ACTIVATE, acpi_mask);
27356bc75619SDan Williams }
27366bc75619SDan Williams 
nfit_test1_setup(struct nfit_test * t)27376bc75619SDan Williams static void nfit_test1_setup(struct nfit_test *t)
27386bc75619SDan Williams {
27396b577c9dSLinda Knippers 	size_t offset;
27406bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
27416bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
27426bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
27436bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
2744d26f73f0SDan Williams 	struct acpi_nfit_desc *acpi_desc;
27456bc75619SDan Williams 
27466b577c9dSLinda Knippers 	offset = 0;
27476bc75619SDan Williams 	/* spa0 (flat range with no bdw aliasing) */
27486bc75619SDan Williams 	spa = nfit_buf + offset;
27496bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
2750e9cfd259SDan Williams 	spa->header.length = sizeof_spa(spa);
27516bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
27526bc75619SDan Williams 	spa->range_index = 0+1;
27536bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
27546bc75619SDan Williams 	spa->length = SPA2_SIZE;
2755d7d8464dSRoss Zwisler 	offset += spa->header.length;
27566bc75619SDan Williams 
27577bfe97c7SDan Williams 	/* virtual cd region */
2758d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
27597bfe97c7SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
2760e9cfd259SDan Williams 	spa->header.length = sizeof_spa(spa);
27617bfe97c7SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_VCD), 16);
27627bfe97c7SDan Williams 	spa->range_index = 0;
27637bfe97c7SDan Williams 	spa->address = t->spa_set_dma[1];
27647bfe97c7SDan Williams 	spa->length = SPA_VCD_SIZE;
2765d7d8464dSRoss Zwisler 	offset += spa->header.length;
27667bfe97c7SDan Williams 
27676bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
27686bc75619SDan Williams 	memdev = nfit_buf + offset;
27696bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
27706bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
2771dafb1048SDan Williams 	memdev->device_handle = handle[5];
27726bc75619SDan Williams 	memdev->physical_id = 0;
27736bc75619SDan Williams 	memdev->region_id = 0;
27746bc75619SDan Williams 	memdev->range_index = 0+1;
27756bc75619SDan Williams 	memdev->region_index = 0+1;
27766bc75619SDan Williams 	memdev->region_size = SPA2_SIZE;
27776bc75619SDan Williams 	memdev->region_offset = 0;
27786bc75619SDan Williams 	memdev->address = 0;
27796bc75619SDan Williams 	memdev->interleave_index = 0;
27806bc75619SDan Williams 	memdev->interleave_ways = 1;
278158138820SDan Williams 	memdev->flags = ACPI_NFIT_MEM_SAVE_FAILED | ACPI_NFIT_MEM_RESTORE_FAILED
278258138820SDan Williams 		| ACPI_NFIT_MEM_FLUSH_FAILED | ACPI_NFIT_MEM_HEALTH_OBSERVED
2783f4295796SDan Williams 		| ACPI_NFIT_MEM_NOT_ARMED;
2784d7d8464dSRoss Zwisler 	offset += memdev->header.length;
27856bc75619SDan Williams 
27866bc75619SDan Williams 	/* dcr-descriptor0 */
27876bc75619SDan Williams 	dcr = nfit_buf + offset;
27886bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
27893b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
27903b87356fSDan Williams 			window_size);
27916bc75619SDan Williams 	dcr->region_index = 0+1;
27925dc68e55SDan Williams 	dcr_common_init(dcr);
2793dafb1048SDan Williams 	dcr->serial_number = ~handle[5];
2794be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BYTE;
27956bc75619SDan Williams 	dcr->windows = 0;
2796ac40b675SDan Williams 	offset += dcr->header.length;
2797d7d8464dSRoss Zwisler 
2798ac40b675SDan Williams 	memdev = nfit_buf + offset;
2799ac40b675SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
2800ac40b675SDan Williams 	memdev->header.length = sizeof(*memdev);
2801ac40b675SDan Williams 	memdev->device_handle = handle[6];
2802ac40b675SDan Williams 	memdev->physical_id = 0;
2803ac40b675SDan Williams 	memdev->region_id = 0;
2804ac40b675SDan Williams 	memdev->range_index = 0;
2805ac40b675SDan Williams 	memdev->region_index = 0+2;
2806ac40b675SDan Williams 	memdev->region_size = SPA2_SIZE;
2807ac40b675SDan Williams 	memdev->region_offset = 0;
2808ac40b675SDan Williams 	memdev->address = 0;
2809ac40b675SDan Williams 	memdev->interleave_index = 0;
2810ac40b675SDan Williams 	memdev->interleave_ways = 1;
2811ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_MAP_FAILED;
2812d7d8464dSRoss Zwisler 	offset += memdev->header.length;
2813ac40b675SDan Williams 
2814ac40b675SDan Williams 	/* dcr-descriptor1 */
2815ac40b675SDan Williams 	dcr = nfit_buf + offset;
2816ac40b675SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2817ac40b675SDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
2818ac40b675SDan Williams 			window_size);
2819ac40b675SDan Williams 	dcr->region_index = 0+2;
2820ac40b675SDan Williams 	dcr_common_init(dcr);
2821ac40b675SDan Williams 	dcr->serial_number = ~handle[6];
2822ac40b675SDan Williams 	dcr->code = NFIT_FIC_BYTE;
2823ac40b675SDan Williams 	dcr->windows = 0;
2824d7d8464dSRoss Zwisler 	offset += dcr->header.length;
2825ac40b675SDan Williams 
28269741a559SRoss Zwisler 	/* sanity check to make sure we've filled the buffer */
28279741a559SRoss Zwisler 	WARN_ON(offset != t->nfit_size);
28289741a559SRoss Zwisler 
28291526f9e2SRoss Zwisler 	t->nfit_filled = offset;
28301526f9e2SRoss Zwisler 
28319fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
28329fb1a190SDave Jiang 			SPA2_SIZE);
2833f471f1a7SDan Williams 
2834d26f73f0SDan Williams 	acpi_desc = &t->acpi_desc;
2835e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2836e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2837e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2838e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
2839674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
28409484e12dSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
28419484e12dSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
28429484e12dSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
28436bc75619SDan Williams }
28446bc75619SDan Williams 
2845a7de92daSDan Williams static unsigned long nfit_ctl_handle;
2846a7de92daSDan Williams 
2847a7de92daSDan Williams union acpi_object *result;
2848a7de92daSDan Williams 
nfit_test_evaluate_dsm(acpi_handle handle,const guid_t * guid,u64 rev,u64 func,union acpi_object * argv4)2849a7de92daSDan Williams static union acpi_object *nfit_test_evaluate_dsm(acpi_handle handle,
285094116f81SAndy Shevchenko 		const guid_t *guid, u64 rev, u64 func, union acpi_object *argv4)
2851a7de92daSDan Williams {
2852a7de92daSDan Williams 	if (handle != &nfit_ctl_handle)
2853a7de92daSDan Williams 		return ERR_PTR(-ENXIO);
2854a7de92daSDan Williams 
2855a7de92daSDan Williams 	return result;
2856a7de92daSDan Williams }
2857a7de92daSDan Williams 
setup_result(void * buf,size_t size)2858a7de92daSDan Williams static int setup_result(void *buf, size_t size)
2859a7de92daSDan Williams {
2860a7de92daSDan Williams 	result = kmalloc(sizeof(union acpi_object) + size, GFP_KERNEL);
2861a7de92daSDan Williams 	if (!result)
2862a7de92daSDan Williams 		return -ENOMEM;
2863a7de92daSDan Williams 	result->package.type = ACPI_TYPE_BUFFER,
2864a7de92daSDan Williams 	result->buffer.pointer = (void *) (result + 1);
2865a7de92daSDan Williams 	result->buffer.length = size;
2866a7de92daSDan Williams 	memcpy(result->buffer.pointer, buf, size);
2867a7de92daSDan Williams 	memset(buf, 0, size);
2868a7de92daSDan Williams 	return 0;
2869a7de92daSDan Williams }
2870a7de92daSDan Williams 
nfit_ctl_test(struct device * dev)2871a7de92daSDan Williams static int nfit_ctl_test(struct device *dev)
2872a7de92daSDan Williams {
2873a7de92daSDan Williams 	int rc, cmd_rc;
2874a7de92daSDan Williams 	struct nvdimm *nvdimm;
2875a7de92daSDan Williams 	struct acpi_device *adev;
2876a7de92daSDan Williams 	struct nfit_mem *nfit_mem;
2877a7de92daSDan Williams 	struct nd_ars_record *record;
2878a7de92daSDan Williams 	struct acpi_nfit_desc *acpi_desc;
2879a7de92daSDan Williams 	const u64 test_val = 0x0123456789abcdefULL;
2880a7de92daSDan Williams 	unsigned long mask, cmd_size, offset;
2881abfd4d9cSDan Williams 	struct nfit_ctl_test_cmd {
2882abfd4d9cSDan Williams 		struct nd_cmd_pkg pkg;
2883a7de92daSDan Williams 		union {
2884a7de92daSDan Williams 			struct nd_cmd_get_config_size cfg_size;
2885fb2a1748SDan Williams 			struct nd_cmd_clear_error clear_err;
2886a7de92daSDan Williams 			struct nd_cmd_ars_status ars_stat;
2887a7de92daSDan Williams 			struct nd_cmd_ars_cap ars_cap;
2888916566aeSDan Williams 			struct nd_intel_bus_fw_activate_businfo fwa_info;
2889a7de92daSDan Williams 			char buf[sizeof(struct nd_cmd_ars_status)
2890a7de92daSDan Williams 				+ sizeof(struct nd_ars_record)];
2891abfd4d9cSDan Williams 		};
2892abfd4d9cSDan Williams 	} cmd;
2893a7de92daSDan Williams 
2894a7de92daSDan Williams 	adev = devm_kzalloc(dev, sizeof(*adev), GFP_KERNEL);
2895a7de92daSDan Williams 	if (!adev)
2896a7de92daSDan Williams 		return -ENOMEM;
2897a7de92daSDan Williams 	*adev = (struct acpi_device) {
2898a7de92daSDan Williams 		.handle = &nfit_ctl_handle,
2899a7de92daSDan Williams 		.dev = {
2900a7de92daSDan Williams 			.init_name = "test-adev",
2901a7de92daSDan Williams 		},
2902a7de92daSDan Williams 	};
2903a7de92daSDan Williams 
2904a7de92daSDan Williams 	acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2905a7de92daSDan Williams 	if (!acpi_desc)
2906a7de92daSDan Williams 		return -ENOMEM;
2907a7de92daSDan Williams 	*acpi_desc = (struct acpi_nfit_desc) {
2908a7de92daSDan Williams 		.nd_desc = {
2909a7de92daSDan Williams 			.cmd_mask = 1UL << ND_CMD_ARS_CAP
2910a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_START
2911a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_STATUS
291210246dc8SYasunori Goto 				| 1UL << ND_CMD_CLEAR_ERROR
291310246dc8SYasunori Goto 				| 1UL << ND_CMD_CALL,
2914a7de92daSDan Williams 			.module = THIS_MODULE,
2915a7de92daSDan Williams 			.provider_name = "ACPI.NFIT",
2916a7de92daSDan Williams 			.ndctl = acpi_nfit_ctl,
2917916566aeSDan Williams 			.bus_family_mask = 1UL << NVDIMM_BUS_FAMILY_NFIT
2918916566aeSDan Williams 				| 1UL << NVDIMM_BUS_FAMILY_INTEL,
2919d46e6a21SDan Williams 		},
29209fb1a190SDave Jiang 		.bus_dsm_mask = 1UL << NFIT_CMD_TRANSLATE_SPA
29219fb1a190SDave Jiang 			| 1UL << NFIT_CMD_ARS_INJECT_SET
29229fb1a190SDave Jiang 			| 1UL << NFIT_CMD_ARS_INJECT_CLEAR
29239fb1a190SDave Jiang 			| 1UL << NFIT_CMD_ARS_INJECT_GET,
2924916566aeSDan Williams 		.family_dsm_mask[NVDIMM_BUS_FAMILY_INTEL] =
2925916566aeSDan Williams 			NVDIMM_BUS_INTEL_FW_ACTIVATE_CMDMASK,
2926a7de92daSDan Williams 		.dev = &adev->dev,
2927a7de92daSDan Williams 	};
2928a7de92daSDan Williams 
2929a7de92daSDan Williams 	nfit_mem = devm_kzalloc(dev, sizeof(*nfit_mem), GFP_KERNEL);
2930a7de92daSDan Williams 	if (!nfit_mem)
2931a7de92daSDan Williams 		return -ENOMEM;
2932a7de92daSDan Williams 
2933a7de92daSDan Williams 	mask = 1UL << ND_CMD_SMART | 1UL << ND_CMD_SMART_THRESHOLD
2934a7de92daSDan Williams 		| 1UL << ND_CMD_DIMM_FLAGS | 1UL << ND_CMD_GET_CONFIG_SIZE
2935a7de92daSDan Williams 		| 1UL << ND_CMD_GET_CONFIG_DATA | 1UL << ND_CMD_SET_CONFIG_DATA
2936a7de92daSDan Williams 		| 1UL << ND_CMD_VENDOR;
2937a7de92daSDan Williams 	*nfit_mem = (struct nfit_mem) {
2938a7de92daSDan Williams 		.adev = adev,
2939a7de92daSDan Williams 		.family = NVDIMM_FAMILY_INTEL,
2940a7de92daSDan Williams 		.dsm_mask = mask,
2941a7de92daSDan Williams 	};
2942a7de92daSDan Williams 
2943a7de92daSDan Williams 	nvdimm = devm_kzalloc(dev, sizeof(*nvdimm), GFP_KERNEL);
2944a7de92daSDan Williams 	if (!nvdimm)
2945a7de92daSDan Williams 		return -ENOMEM;
2946a7de92daSDan Williams 	*nvdimm = (struct nvdimm) {
2947a7de92daSDan Williams 		.provider_data = nfit_mem,
2948a7de92daSDan Williams 		.cmd_mask = mask,
2949a7de92daSDan Williams 		.dev = {
2950a7de92daSDan Williams 			.init_name = "test-dimm",
2951a7de92daSDan Williams 		},
2952a7de92daSDan Williams 	};
2953a7de92daSDan Williams 
2954a7de92daSDan Williams 
2955a7de92daSDan Williams 	/* basic checkout of a typical 'get config size' command */
2956abfd4d9cSDan Williams 	cmd_size = sizeof(cmd.cfg_size);
2957abfd4d9cSDan Williams 	cmd.cfg_size = (struct nd_cmd_get_config_size) {
2958a7de92daSDan Williams 		.status = 0,
2959a7de92daSDan Williams 		.config_size = SZ_128K,
2960a7de92daSDan Williams 		.max_xfer = SZ_4K,
2961a7de92daSDan Williams 	};
2962abfd4d9cSDan Williams 	rc = setup_result(cmd.buf, cmd_size);
2963a7de92daSDan Williams 	if (rc)
2964a7de92daSDan Williams 		return rc;
2965a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2966abfd4d9cSDan Williams 			cmd.buf, cmd_size, &cmd_rc);
2967a7de92daSDan Williams 
2968abfd4d9cSDan Williams 	if (rc < 0 || cmd_rc || cmd.cfg_size.status != 0
2969abfd4d9cSDan Williams 			|| cmd.cfg_size.config_size != SZ_128K
2970abfd4d9cSDan Williams 			|| cmd.cfg_size.max_xfer != SZ_4K) {
2971a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2972a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2973a7de92daSDan Williams 		return -EIO;
2974a7de92daSDan Williams 	}
2975a7de92daSDan Williams 
2976a7de92daSDan Williams 
2977a7de92daSDan Williams 	/* test ars_status with zero output */
2978a7de92daSDan Williams 	cmd_size = offsetof(struct nd_cmd_ars_status, address);
2979abfd4d9cSDan Williams 	cmd.ars_stat = (struct nd_cmd_ars_status) {
2980a7de92daSDan Williams 		.out_length = 0,
2981a7de92daSDan Williams 	};
2982abfd4d9cSDan Williams 	rc = setup_result(cmd.buf, cmd_size);
2983a7de92daSDan Williams 	if (rc)
2984a7de92daSDan Williams 		return rc;
2985a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2986abfd4d9cSDan Williams 			cmd.buf, cmd_size, &cmd_rc);
2987a7de92daSDan Williams 
2988a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2989a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2990a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2991a7de92daSDan Williams 		return -EIO;
2992a7de92daSDan Williams 	}
2993a7de92daSDan Williams 
2994a7de92daSDan Williams 
2995a7de92daSDan Williams 	/* test ars_cap with benign extended status */
2996abfd4d9cSDan Williams 	cmd_size = sizeof(cmd.ars_cap);
2997abfd4d9cSDan Williams 	cmd.ars_cap = (struct nd_cmd_ars_cap) {
2998a7de92daSDan Williams 		.status = ND_ARS_PERSISTENT << 16,
2999a7de92daSDan Williams 	};
3000a7de92daSDan Williams 	offset = offsetof(struct nd_cmd_ars_cap, status);
3001abfd4d9cSDan Williams 	rc = setup_result(cmd.buf + offset, cmd_size - offset);
3002a7de92daSDan Williams 	if (rc)
3003a7de92daSDan Williams 		return rc;
3004a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_CAP,
3005abfd4d9cSDan Williams 			cmd.buf, cmd_size, &cmd_rc);
3006a7de92daSDan Williams 
3007a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
3008a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
3009a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
3010a7de92daSDan Williams 		return -EIO;
3011a7de92daSDan Williams 	}
3012a7de92daSDan Williams 
3013a7de92daSDan Williams 
3014a7de92daSDan Williams 	/* test ars_status with 'status' trimmed from 'out_length' */
3015abfd4d9cSDan Williams 	cmd_size = sizeof(cmd.ars_stat) + sizeof(struct nd_ars_record);
3016abfd4d9cSDan Williams 	cmd.ars_stat = (struct nd_cmd_ars_status) {
3017a7de92daSDan Williams 		.out_length = cmd_size - 4,
3018a7de92daSDan Williams 	};
3019abfd4d9cSDan Williams 	record = &cmd.ars_stat.records[0];
3020a7de92daSDan Williams 	*record = (struct nd_ars_record) {
3021a7de92daSDan Williams 		.length = test_val,
3022a7de92daSDan Williams 	};
3023abfd4d9cSDan Williams 	rc = setup_result(cmd.buf, cmd_size);
3024a7de92daSDan Williams 	if (rc)
3025a7de92daSDan Williams 		return rc;
3026a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
3027abfd4d9cSDan Williams 			cmd.buf, cmd_size, &cmd_rc);
3028a7de92daSDan Williams 
3029a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
3030a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
3031a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
3032a7de92daSDan Williams 		return -EIO;
3033a7de92daSDan Williams 	}
3034a7de92daSDan Williams 
3035a7de92daSDan Williams 
3036a7de92daSDan Williams 	/* test ars_status with 'Output (Size)' including 'status' */
3037abfd4d9cSDan Williams 	cmd_size = sizeof(cmd.ars_stat) + sizeof(struct nd_ars_record);
3038abfd4d9cSDan Williams 	cmd.ars_stat = (struct nd_cmd_ars_status) {
3039a7de92daSDan Williams 		.out_length = cmd_size,
3040a7de92daSDan Williams 	};
3041abfd4d9cSDan Williams 	record = &cmd.ars_stat.records[0];
3042a7de92daSDan Williams 	*record = (struct nd_ars_record) {
3043a7de92daSDan Williams 		.length = test_val,
3044a7de92daSDan Williams 	};
3045abfd4d9cSDan Williams 	rc = setup_result(cmd.buf, cmd_size);
3046a7de92daSDan Williams 	if (rc)
3047a7de92daSDan Williams 		return rc;
3048a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
3049abfd4d9cSDan Williams 			cmd.buf, cmd_size, &cmd_rc);
3050a7de92daSDan Williams 
3051a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
3052a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
3053a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
3054a7de92daSDan Williams 		return -EIO;
3055a7de92daSDan Williams 	}
3056a7de92daSDan Williams 
3057a7de92daSDan Williams 
3058a7de92daSDan Williams 	/* test extended status for get_config_size results in failure */
3059abfd4d9cSDan Williams 	cmd_size = sizeof(cmd.cfg_size);
3060abfd4d9cSDan Williams 	cmd.cfg_size = (struct nd_cmd_get_config_size) {
3061a7de92daSDan Williams 		.status = 1 << 16,
3062a7de92daSDan Williams 	};
3063abfd4d9cSDan Williams 	rc = setup_result(cmd.buf, cmd_size);
3064a7de92daSDan Williams 	if (rc)
3065a7de92daSDan Williams 		return rc;
3066a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
3067abfd4d9cSDan Williams 			cmd.buf, cmd_size, &cmd_rc);
3068a7de92daSDan Williams 
3069a7de92daSDan Williams 	if (rc < 0 || cmd_rc >= 0) {
3070a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
3071a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
3072a7de92daSDan Williams 		return -EIO;
3073a7de92daSDan Williams 	}
3074a7de92daSDan Williams 
3075fb2a1748SDan Williams 	/* test clear error */
3076abfd4d9cSDan Williams 	cmd_size = sizeof(cmd.clear_err);
3077abfd4d9cSDan Williams 	cmd.clear_err = (struct nd_cmd_clear_error) {
3078fb2a1748SDan Williams 		.length = 512,
3079fb2a1748SDan Williams 		.cleared = 512,
3080fb2a1748SDan Williams 	};
3081abfd4d9cSDan Williams 	rc = setup_result(cmd.buf, cmd_size);
3082fb2a1748SDan Williams 	if (rc)
3083fb2a1748SDan Williams 		return rc;
3084fb2a1748SDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_CLEAR_ERROR,
3085abfd4d9cSDan Williams 			cmd.buf, cmd_size, &cmd_rc);
3086fb2a1748SDan Williams 	if (rc < 0 || cmd_rc) {
3087fb2a1748SDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
3088fb2a1748SDan Williams 				__func__, __LINE__, rc, cmd_rc);
3089fb2a1748SDan Williams 		return -EIO;
3090fb2a1748SDan Williams 	}
3091fb2a1748SDan Williams 
3092916566aeSDan Williams 	/* test firmware activate bus info */
3093916566aeSDan Williams 	cmd_size = sizeof(cmd.fwa_info);
3094916566aeSDan Williams 	cmd = (struct nfit_ctl_test_cmd) {
3095916566aeSDan Williams 		.pkg = {
3096916566aeSDan Williams 			.nd_command = NVDIMM_BUS_INTEL_FW_ACTIVATE_BUSINFO,
3097916566aeSDan Williams 			.nd_family = NVDIMM_BUS_FAMILY_INTEL,
3098916566aeSDan Williams 			.nd_size_out = cmd_size,
3099916566aeSDan Williams 			.nd_fw_size = cmd_size,
3100916566aeSDan Williams 		},
3101916566aeSDan Williams 		.fwa_info = {
3102916566aeSDan Williams 			.state = ND_INTEL_FWA_IDLE,
3103916566aeSDan Williams 			.capability = ND_INTEL_BUS_FWA_CAP_FWQUIESCE
3104916566aeSDan Williams 				| ND_INTEL_BUS_FWA_CAP_OSQUIESCE,
3105916566aeSDan Williams 			.activate_tmo = 1,
3106916566aeSDan Williams 			.cpu_quiesce_tmo = 1,
3107916566aeSDan Williams 			.io_quiesce_tmo = 1,
3108916566aeSDan Williams 			.max_quiesce_tmo = 1,
3109916566aeSDan Williams 		},
3110916566aeSDan Williams 	};
3111916566aeSDan Williams 	rc = setup_result(cmd.buf, cmd_size);
3112916566aeSDan Williams 	if (rc)
3113916566aeSDan Williams 		return rc;
3114916566aeSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_CALL,
3115916566aeSDan Williams 			&cmd, sizeof(cmd.pkg) + cmd_size, &cmd_rc);
3116916566aeSDan Williams 	if (rc < 0 || cmd_rc) {
3117916566aeSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
3118916566aeSDan Williams 				__func__, __LINE__, rc, cmd_rc);
3119916566aeSDan Williams 		return -EIO;
3120916566aeSDan Williams 	}
3121916566aeSDan Williams 
3122a7de92daSDan Williams 	return 0;
3123a7de92daSDan Williams }
3124a7de92daSDan Williams 
nfit_test_probe(struct platform_device * pdev)31256bc75619SDan Williams static int nfit_test_probe(struct platform_device *pdev)
31266bc75619SDan Williams {
31276bc75619SDan Williams 	struct nvdimm_bus_descriptor *nd_desc;
31286bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
31296bc75619SDan Williams 	struct device *dev = &pdev->dev;
31306bc75619SDan Williams 	struct nfit_test *nfit_test;
3131231bf117SDan Williams 	struct nfit_mem *nfit_mem;
3132c14a868aSDan Williams 	union acpi_object *obj;
31336bc75619SDan Williams 	int rc;
31346bc75619SDan Williams 
3135a7de92daSDan Williams 	if (strcmp(dev_name(&pdev->dev), "nfit_test.0") == 0) {
3136a7de92daSDan Williams 		rc = nfit_ctl_test(&pdev->dev);
3137a7de92daSDan Williams 		if (rc)
3138a7de92daSDan Williams 			return rc;
3139a7de92daSDan Williams 	}
3140a7de92daSDan Williams 
31416bc75619SDan Williams 	nfit_test = to_nfit_test(&pdev->dev);
31426bc75619SDan Williams 
31436bc75619SDan Williams 	/* common alloc */
31446bc75619SDan Williams 	if (nfit_test->num_dcr) {
31456bc75619SDan Williams 		int num = nfit_test->num_dcr;
31466bc75619SDan Williams 
31476bc75619SDan Williams 		nfit_test->dimm = devm_kcalloc(dev, num, sizeof(void *),
31486bc75619SDan Williams 				GFP_KERNEL);
31496bc75619SDan Williams 		nfit_test->dimm_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
31506bc75619SDan Williams 				GFP_KERNEL);
31519d27a87eSDan Williams 		nfit_test->flush = devm_kcalloc(dev, num, sizeof(void *),
31529d27a87eSDan Williams 				GFP_KERNEL);
31539d27a87eSDan Williams 		nfit_test->flush_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
31549d27a87eSDan Williams 				GFP_KERNEL);
31556bc75619SDan Williams 		nfit_test->label = devm_kcalloc(dev, num, sizeof(void *),
31566bc75619SDan Williams 				GFP_KERNEL);
31576bc75619SDan Williams 		nfit_test->label_dma = devm_kcalloc(dev, num,
31586bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
31596bc75619SDan Williams 		nfit_test->dcr = devm_kcalloc(dev, num,
31606bc75619SDan Williams 				sizeof(struct nfit_test_dcr *), GFP_KERNEL);
31616bc75619SDan Williams 		nfit_test->dcr_dma = devm_kcalloc(dev, num,
31626bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
3163ed07c433SDan Williams 		nfit_test->smart = devm_kcalloc(dev, num,
3164ed07c433SDan Williams 				sizeof(struct nd_intel_smart), GFP_KERNEL);
3165ed07c433SDan Williams 		nfit_test->smart_threshold = devm_kcalloc(dev, num,
3166ed07c433SDan Williams 				sizeof(struct nd_intel_smart_threshold),
3167ed07c433SDan Williams 				GFP_KERNEL);
3168bfbaa952SDave Jiang 		nfit_test->fw = devm_kcalloc(dev, num,
3169bfbaa952SDave Jiang 				sizeof(struct nfit_test_fw), GFP_KERNEL);
31706bc75619SDan Williams 		if (nfit_test->dimm && nfit_test->dimm_dma && nfit_test->label
31716bc75619SDan Williams 				&& nfit_test->label_dma && nfit_test->dcr
31729d27a87eSDan Williams 				&& nfit_test->dcr_dma && nfit_test->flush
3173bfbaa952SDave Jiang 				&& nfit_test->flush_dma
3174bfbaa952SDave Jiang 				&& nfit_test->fw)
31756bc75619SDan Williams 			/* pass */;
31766bc75619SDan Williams 		else
31776bc75619SDan Williams 			return -ENOMEM;
31786bc75619SDan Williams 	}
31796bc75619SDan Williams 
31806bc75619SDan Williams 	if (nfit_test->num_pm) {
31816bc75619SDan Williams 		int num = nfit_test->num_pm;
31826bc75619SDan Williams 
31836bc75619SDan Williams 		nfit_test->spa_set = devm_kcalloc(dev, num, sizeof(void *),
31846bc75619SDan Williams 				GFP_KERNEL);
31856bc75619SDan Williams 		nfit_test->spa_set_dma = devm_kcalloc(dev, num,
31866bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
31876bc75619SDan Williams 		if (nfit_test->spa_set && nfit_test->spa_set_dma)
31886bc75619SDan Williams 			/* pass */;
31896bc75619SDan Williams 		else
31906bc75619SDan Williams 			return -ENOMEM;
31916bc75619SDan Williams 	}
31926bc75619SDan Williams 
31936bc75619SDan Williams 	/* per-nfit specific alloc */
31946bc75619SDan Williams 	if (nfit_test->alloc(nfit_test))
31956bc75619SDan Williams 		return -ENOMEM;
31966bc75619SDan Williams 
31976bc75619SDan Williams 	nfit_test->setup(nfit_test);
31986bc75619SDan Williams 	acpi_desc = &nfit_test->acpi_desc;
3199a61fe6f7SDan Williams 	acpi_nfit_desc_init(acpi_desc, &pdev->dev);
32006bc75619SDan Williams 	nd_desc = &acpi_desc->nd_desc;
3201a61fe6f7SDan Williams 	nd_desc->provider_name = NULL;
3202bc9775d8SDan Williams 	nd_desc->module = THIS_MODULE;
3203a61fe6f7SDan Williams 	nd_desc->ndctl = nfit_test_ctl;
32046bc75619SDan Williams 
3205e7a11b44SDan Williams 	rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_buf,
32061526f9e2SRoss Zwisler 			nfit_test->nfit_filled);
320758cd71b4SDan Williams 	if (rc)
320820985164SVishal Verma 		return rc;
320920985164SVishal Verma 
3210fbabd829SDan Williams 	rc = devm_add_action_or_reset(&pdev->dev, acpi_nfit_shutdown, acpi_desc);
3211fbabd829SDan Williams 	if (rc)
3212fbabd829SDan Williams 		return rc;
3213fbabd829SDan Williams 
321420985164SVishal Verma 	if (nfit_test->setup != nfit_test0_setup)
321520985164SVishal Verma 		return 0;
321620985164SVishal Verma 
321720985164SVishal Verma 	nfit_test->setup_hotplug = 1;
321820985164SVishal Verma 	nfit_test->setup(nfit_test);
321920985164SVishal Verma 
3220c14a868aSDan Williams 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3221c14a868aSDan Williams 	if (!obj)
3222c14a868aSDan Williams 		return -ENOMEM;
3223c14a868aSDan Williams 	obj->type = ACPI_TYPE_BUFFER;
3224c14a868aSDan Williams 	obj->buffer.length = nfit_test->nfit_size;
3225c14a868aSDan Williams 	obj->buffer.pointer = nfit_test->nfit_buf;
3226c14a868aSDan Williams 	*(nfit_test->_fit) = obj;
3227c14a868aSDan Williams 	__acpi_nfit_notify(&pdev->dev, nfit_test, 0x80);
3228231bf117SDan Williams 
3229231bf117SDan Williams 	/* associate dimm devices with nfit_mem data for notification testing */
3230231bf117SDan Williams 	mutex_lock(&acpi_desc->init_mutex);
3231231bf117SDan Williams 	list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
3232231bf117SDan Williams 		u32 nfit_handle = __to_nfit_memdev(nfit_mem)->device_handle;
3233231bf117SDan Williams 		int i;
3234231bf117SDan Williams 
3235af31b04bSMasayoshi Mizuma 		for (i = 0; i < ARRAY_SIZE(handle); i++)
3236231bf117SDan Williams 			if (nfit_handle == handle[i])
3237231bf117SDan Williams 				dev_set_drvdata(nfit_test->dimm_dev[i],
3238231bf117SDan Williams 						nfit_mem);
3239231bf117SDan Williams 	}
3240231bf117SDan Williams 	mutex_unlock(&acpi_desc->init_mutex);
32416bc75619SDan Williams 
32426bc75619SDan Williams 	return 0;
32436bc75619SDan Williams }
32446bc75619SDan Williams 
nfit_test_release(struct device * dev)32456bc75619SDan Williams static void nfit_test_release(struct device *dev)
32466bc75619SDan Williams {
32476bc75619SDan Williams 	struct nfit_test *nfit_test = to_nfit_test(dev);
32486bc75619SDan Williams 
32496bc75619SDan Williams 	kfree(nfit_test);
32506bc75619SDan Williams }
32516bc75619SDan Williams 
32526bc75619SDan Williams static const struct platform_device_id nfit_test_id[] = {
32536bc75619SDan Williams 	{ KBUILD_MODNAME },
32546bc75619SDan Williams 	{ },
32556bc75619SDan Williams };
32566bc75619SDan Williams 
32576bc75619SDan Williams static struct platform_driver nfit_test_driver = {
32586bc75619SDan Williams 	.probe = nfit_test_probe,
32596bc75619SDan Williams 	.driver = {
32606bc75619SDan Williams 		.name = KBUILD_MODNAME,
32616bc75619SDan Williams 	},
32626bc75619SDan Williams 	.id_table = nfit_test_id,
32636bc75619SDan Williams };
32646bc75619SDan Williams 
nfit_test_init(void)32656bc75619SDan Williams static __init int nfit_test_init(void)
32666bc75619SDan Williams {
32676bc75619SDan Williams 	int rc, i;
32686bc75619SDan Williams 
32690fb5c8dfSDan Williams 	pmem_test();
32700fb5c8dfSDan Williams 	libnvdimm_test();
32710fb5c8dfSDan Williams 	acpi_nfit_test();
32720fb5c8dfSDan Williams 	device_dax_test();
327392f6f2d7SVishal Verma 	dax_pmem_test();
32740fb5c8dfSDan Williams 
3275a7de92daSDan Williams 	nfit_test_setup(nfit_test_lookup, nfit_test_evaluate_dsm);
3276231bf117SDan Williams 
32779fb1a190SDave Jiang 	nfit_wq = create_singlethread_workqueue("nfit");
32789fb1a190SDave Jiang 	if (!nfit_wq)
32799fb1a190SDave Jiang 		return -ENOMEM;
32809fb1a190SDave Jiang 
3281dd6cad2dSGreg Kroah-Hartman 	rc = class_register(&nfit_test_dimm);
3282dd6cad2dSGreg Kroah-Hartman 	if (rc)
3283a7de92daSDan Williams 		goto err_register;
32846bc75619SDan Williams 
3285e3f5df76SDan Williams 	nfit_pool = gen_pool_create(ilog2(SZ_4M), NUMA_NO_NODE);
3286e3f5df76SDan Williams 	if (!nfit_pool) {
3287e3f5df76SDan Williams 		rc = -ENOMEM;
3288e3f5df76SDan Williams 		goto err_register;
3289e3f5df76SDan Williams 	}
3290e3f5df76SDan Williams 
3291e3f5df76SDan Williams 	if (gen_pool_add(nfit_pool, SZ_4G, SZ_4G, NUMA_NO_NODE)) {
3292e3f5df76SDan Williams 		rc = -ENOMEM;
3293e3f5df76SDan Williams 		goto err_register;
3294e3f5df76SDan Williams 	}
3295e3f5df76SDan Williams 
32966bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++) {
32976bc75619SDan Williams 		struct nfit_test *nfit_test;
32986bc75619SDan Williams 		struct platform_device *pdev;
32996bc75619SDan Williams 
33006bc75619SDan Williams 		nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
33016bc75619SDan Williams 		if (!nfit_test) {
33026bc75619SDan Williams 			rc = -ENOMEM;
33036bc75619SDan Williams 			goto err_register;
33046bc75619SDan Williams 		}
33056bc75619SDan Williams 		INIT_LIST_HEAD(&nfit_test->resources);
33069fb1a190SDave Jiang 		badrange_init(&nfit_test->badrange);
33076bc75619SDan Williams 		switch (i) {
33086bc75619SDan Williams 		case 0:
33096bc75619SDan Williams 			nfit_test->num_pm = NUM_PM;
3310dafb1048SDan Williams 			nfit_test->dcr_idx = 0;
33116bc75619SDan Williams 			nfit_test->num_dcr = NUM_DCR;
33126bc75619SDan Williams 			nfit_test->alloc = nfit_test0_alloc;
33136bc75619SDan Williams 			nfit_test->setup = nfit_test0_setup;
33146bc75619SDan Williams 			break;
33156bc75619SDan Williams 		case 1:
3316a117699cSYasunori Goto 			nfit_test->num_pm = 2;
3317dafb1048SDan Williams 			nfit_test->dcr_idx = NUM_DCR;
3318ac40b675SDan Williams 			nfit_test->num_dcr = 2;
33196bc75619SDan Williams 			nfit_test->alloc = nfit_test1_alloc;
33206bc75619SDan Williams 			nfit_test->setup = nfit_test1_setup;
33216bc75619SDan Williams 			break;
33226bc75619SDan Williams 		default:
33236bc75619SDan Williams 			rc = -EINVAL;
33246bc75619SDan Williams 			goto err_register;
33256bc75619SDan Williams 		}
33266bc75619SDan Williams 		pdev = &nfit_test->pdev;
33276bc75619SDan Williams 		pdev->name = KBUILD_MODNAME;
33286bc75619SDan Williams 		pdev->id = i;
33296bc75619SDan Williams 		pdev->dev.release = nfit_test_release;
33306bc75619SDan Williams 		rc = platform_device_register(pdev);
33316bc75619SDan Williams 		if (rc) {
33326bc75619SDan Williams 			put_device(&pdev->dev);
33336bc75619SDan Williams 			goto err_register;
33346bc75619SDan Williams 		}
33358b06b884SDan Williams 		get_device(&pdev->dev);
33366bc75619SDan Williams 
33376bc75619SDan Williams 		rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
33386bc75619SDan Williams 		if (rc)
33396bc75619SDan Williams 			goto err_register;
33406bc75619SDan Williams 
33416bc75619SDan Williams 		instances[i] = nfit_test;
33429fb1a190SDave Jiang 		INIT_WORK(&nfit_test->work, uc_error_notify);
33436bc75619SDan Williams 	}
33446bc75619SDan Williams 
33456bc75619SDan Williams 	rc = platform_driver_register(&nfit_test_driver);
33466bc75619SDan Williams 	if (rc)
33476bc75619SDan Williams 		goto err_register;
33486bc75619SDan Williams 	return 0;
33496bc75619SDan Williams 
33506bc75619SDan Williams  err_register:
3351e3f5df76SDan Williams 	if (nfit_pool)
3352e3f5df76SDan Williams 		gen_pool_destroy(nfit_pool);
3353e3f5df76SDan Williams 
33549fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
33556bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
33566bc75619SDan Williams 		if (instances[i])
33576bc75619SDan Williams 			platform_device_unregister(&instances[i]->pdev);
33586bc75619SDan Williams 	nfit_test_teardown();
33598b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
33608b06b884SDan Williams 		if (instances[i])
33618b06b884SDan Williams 			put_device(&instances[i]->pdev.dev);
33628b06b884SDan Williams 
33636bc75619SDan Williams 	return rc;
33646bc75619SDan Williams }
33656bc75619SDan Williams 
nfit_test_exit(void)33666bc75619SDan Williams static __exit void nfit_test_exit(void)
33676bc75619SDan Williams {
33686bc75619SDan Williams 	int i;
33696bc75619SDan Williams 
33709fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
33716bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
33726bc75619SDan Williams 		platform_device_unregister(&instances[i]->pdev);
33738b06b884SDan Williams 	platform_driver_unregister(&nfit_test_driver);
33746bc75619SDan Williams 	nfit_test_teardown();
33758b06b884SDan Williams 
3376e3f5df76SDan Williams 	gen_pool_destroy(nfit_pool);
3377e3f5df76SDan Williams 
33788b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
33798b06b884SDan Williams 		put_device(&instances[i]->pdev.dev);
3380dd6cad2dSGreg Kroah-Hartman 	class_unregister(&nfit_test_dimm);
33816bc75619SDan Williams }
33826bc75619SDan Williams 
33836bc75619SDan Williams module_init(nfit_test_init);
33846bc75619SDan Williams module_exit(nfit_test_exit);
3385*b0d478e3SIra Weiny MODULE_DESCRIPTION("Test ACPI NFIT devices");
33866bc75619SDan Williams MODULE_LICENSE("GPL v2");
33876bc75619SDan Williams MODULE_AUTHOR("Intel Corporation");
3388