xref: /linux/tools/testing/nvdimm/test/nfit.c (revision 674d8bdec770d40288574534eab27d82bdf16b0e)
16bc75619SDan Williams /*
26bc75619SDan Williams  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
36bc75619SDan Williams  *
46bc75619SDan Williams  * This program is free software; you can redistribute it and/or modify
56bc75619SDan Williams  * it under the terms of version 2 of the GNU General Public License as
66bc75619SDan Williams  * published by the Free Software Foundation.
76bc75619SDan Williams  *
86bc75619SDan Williams  * This program is distributed in the hope that it will be useful, but
96bc75619SDan Williams  * WITHOUT ANY WARRANTY; without even the implied warranty of
106bc75619SDan Williams  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116bc75619SDan Williams  * General Public License for more details.
126bc75619SDan Williams  */
136bc75619SDan Williams #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
146bc75619SDan Williams #include <linux/platform_device.h>
156bc75619SDan Williams #include <linux/dma-mapping.h>
16d8d378faSDan Williams #include <linux/workqueue.h>
176bc75619SDan Williams #include <linux/libnvdimm.h>
186bc75619SDan Williams #include <linux/vmalloc.h>
196bc75619SDan Williams #include <linux/device.h>
206bc75619SDan Williams #include <linux/module.h>
2120985164SVishal Verma #include <linux/mutex.h>
226bc75619SDan Williams #include <linux/ndctl.h>
236bc75619SDan Williams #include <linux/sizes.h>
2420985164SVishal Verma #include <linux/list.h>
256bc75619SDan Williams #include <linux/slab.h>
26a7de92daSDan Williams #include <nd-core.h>
276bc75619SDan Williams #include <nfit.h>
286bc75619SDan Williams #include <nd.h>
296bc75619SDan Williams #include "nfit_test.h"
306bc75619SDan Williams 
316bc75619SDan Williams /*
326bc75619SDan Williams  * Generate an NFIT table to describe the following topology:
336bc75619SDan Williams  *
346bc75619SDan Williams  * BUS0: Interleaved PMEM regions, and aliasing with BLK regions
356bc75619SDan Williams  *
366bc75619SDan Williams  *                     (a)                       (b)            DIMM   BLK-REGION
376bc75619SDan Williams  *           +----------+--------------+----------+---------+
386bc75619SDan Williams  * +------+  |  blk2.0  |     pm0.0    |  blk2.1  |  pm1.0  |    0      region2
396bc75619SDan Williams  * | imc0 +--+- - - - - region0 - - - -+----------+         +
406bc75619SDan Williams  * +--+---+  |  blk3.0  |     pm0.0    |  blk3.1  |  pm1.0  |    1      region3
416bc75619SDan Williams  *    |      +----------+--------------v----------v         v
426bc75619SDan Williams  * +--+---+                            |                    |
436bc75619SDan Williams  * | cpu0 |                                    region1
446bc75619SDan Williams  * +--+---+                            |                    |
456bc75619SDan Williams  *    |      +-------------------------^----------^         ^
466bc75619SDan Williams  * +--+---+  |                 blk4.0             |  pm1.0  |    2      region4
476bc75619SDan Williams  * | imc1 +--+-------------------------+----------+         +
486bc75619SDan Williams  * +------+  |                 blk5.0             |  pm1.0  |    3      region5
496bc75619SDan Williams  *           +-------------------------+----------+-+-------+
506bc75619SDan Williams  *
5120985164SVishal Verma  * +--+---+
5220985164SVishal Verma  * | cpu1 |
5320985164SVishal Verma  * +--+---+                   (Hotplug DIMM)
5420985164SVishal Verma  *    |      +----------------------------------------------+
5520985164SVishal Verma  * +--+---+  |                 blk6.0/pm7.0                 |    4      region6/7
5620985164SVishal Verma  * | imc0 +--+----------------------------------------------+
5720985164SVishal Verma  * +------+
5820985164SVishal Verma  *
5920985164SVishal Verma  *
606bc75619SDan Williams  * *) In this layout we have four dimms and two memory controllers in one
616bc75619SDan Williams  *    socket.  Each unique interface (BLK or PMEM) to DPA space
626bc75619SDan Williams  *    is identified by a region device with a dynamically assigned id.
636bc75619SDan Williams  *
646bc75619SDan Williams  * *) The first portion of dimm0 and dimm1 are interleaved as REGION0.
656bc75619SDan Williams  *    A single PMEM namespace "pm0.0" is created using half of the
666bc75619SDan Williams  *    REGION0 SPA-range.  REGION0 spans dimm0 and dimm1.  PMEM namespace
676bc75619SDan Williams  *    allocate from from the bottom of a region.  The unallocated
686bc75619SDan Williams  *    portion of REGION0 aliases with REGION2 and REGION3.  That
696bc75619SDan Williams  *    unallacted capacity is reclaimed as BLK namespaces ("blk2.0" and
706bc75619SDan Williams  *    "blk3.0") starting at the base of each DIMM to offset (a) in those
716bc75619SDan Williams  *    DIMMs.  "pm0.0", "blk2.0" and "blk3.0" are free-form readable
726bc75619SDan Williams  *    names that can be assigned to a namespace.
736bc75619SDan Williams  *
746bc75619SDan Williams  * *) In the last portion of dimm0 and dimm1 we have an interleaved
756bc75619SDan Williams  *    SPA range, REGION1, that spans those two dimms as well as dimm2
766bc75619SDan Williams  *    and dimm3.  Some of REGION1 allocated to a PMEM namespace named
776bc75619SDan Williams  *    "pm1.0" the rest is reclaimed in 4 BLK namespaces (for each
786bc75619SDan Williams  *    dimm in the interleave set), "blk2.1", "blk3.1", "blk4.0", and
796bc75619SDan Williams  *    "blk5.0".
806bc75619SDan Williams  *
816bc75619SDan Williams  * *) The portion of dimm2 and dimm3 that do not participate in the
826bc75619SDan Williams  *    REGION1 interleaved SPA range (i.e. the DPA address below offset
836bc75619SDan Williams  *    (b) are also included in the "blk4.0" and "blk5.0" namespaces.
846bc75619SDan Williams  *    Note, that BLK namespaces need not be contiguous in DPA-space, and
856bc75619SDan Williams  *    can consume aliased capacity from multiple interleave sets.
866bc75619SDan Williams  *
876bc75619SDan Williams  * BUS1: Legacy NVDIMM (single contiguous range)
886bc75619SDan Williams  *
896bc75619SDan Williams  *  region2
906bc75619SDan Williams  * +---------------------+
916bc75619SDan Williams  * |---------------------|
926bc75619SDan Williams  * ||       pm2.0       ||
936bc75619SDan Williams  * |---------------------|
946bc75619SDan Williams  * +---------------------+
956bc75619SDan Williams  *
966bc75619SDan Williams  * *) A NFIT-table may describe a simple system-physical-address range
976bc75619SDan Williams  *    with no BLK aliasing.  This type of region may optionally
986bc75619SDan Williams  *    reference an NVDIMM.
996bc75619SDan Williams  */
1006bc75619SDan Williams enum {
10120985164SVishal Verma 	NUM_PM  = 3,
10220985164SVishal Verma 	NUM_DCR = 5,
10385d3fa02SDan Williams 	NUM_HINTS = 8,
1046bc75619SDan Williams 	NUM_BDW = NUM_DCR,
1056bc75619SDan Williams 	NUM_SPA = NUM_PM + NUM_DCR + NUM_BDW,
1066bc75619SDan Williams 	NUM_MEM = NUM_DCR + NUM_BDW + 2 /* spa0 iset */ + 4 /* spa1 iset */,
1076bc75619SDan Williams 	DIMM_SIZE = SZ_32M,
1086bc75619SDan Williams 	LABEL_SIZE = SZ_128K,
1097bfe97c7SDan Williams 	SPA_VCD_SIZE = SZ_4M,
1106bc75619SDan Williams 	SPA0_SIZE = DIMM_SIZE,
1116bc75619SDan Williams 	SPA1_SIZE = DIMM_SIZE*2,
1126bc75619SDan Williams 	SPA2_SIZE = DIMM_SIZE,
1136bc75619SDan Williams 	BDW_SIZE = 64 << 8,
1146bc75619SDan Williams 	DCR_SIZE = 12,
1156bc75619SDan Williams 	NUM_NFITS = 2, /* permit testing multiple NFITs per system */
1166bc75619SDan Williams };
1176bc75619SDan Williams 
1186bc75619SDan Williams struct nfit_test_dcr {
1196bc75619SDan Williams 	__le64 bdw_addr;
1206bc75619SDan Williams 	__le32 bdw_status;
1216bc75619SDan Williams 	__u8 aperature[BDW_SIZE];
1226bc75619SDan Williams };
1236bc75619SDan Williams 
1246bc75619SDan Williams #define NFIT_DIMM_HANDLE(node, socket, imc, chan, dimm) \
1256bc75619SDan Williams 	(((node & 0xfff) << 16) | ((socket & 0xf) << 12) \
1266bc75619SDan Williams 	 | ((imc & 0xf) << 8) | ((chan & 0xf) << 4) | (dimm & 0xf))
1276bc75619SDan Williams 
128dafb1048SDan Williams static u32 handle[] = {
1296bc75619SDan Williams 	[0] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 0),
1306bc75619SDan Williams 	[1] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 1),
1316bc75619SDan Williams 	[2] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 0),
1326bc75619SDan Williams 	[3] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 1),
13320985164SVishal Verma 	[4] = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
134dafb1048SDan Williams 	[5] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 0),
135ac40b675SDan Williams 	[6] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 1),
1366bc75619SDan Williams };
1376bc75619SDan Williams 
13873606afdSDan Williams static unsigned long dimm_fail_cmd_flags[NUM_DCR];
13973606afdSDan Williams 
140bfbaa952SDave Jiang struct nfit_test_fw {
141bfbaa952SDave Jiang 	enum intel_fw_update_state state;
142bfbaa952SDave Jiang 	u32 context;
143bfbaa952SDave Jiang 	u64 version;
144bfbaa952SDave Jiang 	u32 size_received;
145bfbaa952SDave Jiang 	u64 end_time;
146bfbaa952SDave Jiang };
147bfbaa952SDave Jiang 
1486bc75619SDan Williams struct nfit_test {
1496bc75619SDan Williams 	struct acpi_nfit_desc acpi_desc;
1506bc75619SDan Williams 	struct platform_device pdev;
1516bc75619SDan Williams 	struct list_head resources;
1526bc75619SDan Williams 	void *nfit_buf;
1536bc75619SDan Williams 	dma_addr_t nfit_dma;
1546bc75619SDan Williams 	size_t nfit_size;
155dafb1048SDan Williams 	int dcr_idx;
1566bc75619SDan Williams 	int num_dcr;
1576bc75619SDan Williams 	int num_pm;
1586bc75619SDan Williams 	void **dimm;
1596bc75619SDan Williams 	dma_addr_t *dimm_dma;
1609d27a87eSDan Williams 	void **flush;
1619d27a87eSDan Williams 	dma_addr_t *flush_dma;
1626bc75619SDan Williams 	void **label;
1636bc75619SDan Williams 	dma_addr_t *label_dma;
1646bc75619SDan Williams 	void **spa_set;
1656bc75619SDan Williams 	dma_addr_t *spa_set_dma;
1666bc75619SDan Williams 	struct nfit_test_dcr **dcr;
1676bc75619SDan Williams 	dma_addr_t *dcr_dma;
1686bc75619SDan Williams 	int (*alloc)(struct nfit_test *t);
1696bc75619SDan Williams 	void (*setup)(struct nfit_test *t);
17020985164SVishal Verma 	int setup_hotplug;
171c14a868aSDan Williams 	union acpi_object **_fit;
172c14a868aSDan Williams 	dma_addr_t _fit_dma;
173f471f1a7SDan Williams 	struct ars_state {
174f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status;
175f471f1a7SDan Williams 		unsigned long deadline;
176f471f1a7SDan Williams 		spinlock_t lock;
177f471f1a7SDan Williams 	} ars_state;
178231bf117SDan Williams 	struct device *dimm_dev[NUM_DCR];
179ed07c433SDan Williams 	struct nd_intel_smart *smart;
180ed07c433SDan Williams 	struct nd_intel_smart_threshold *smart_threshold;
1819fb1a190SDave Jiang 	struct badrange badrange;
1829fb1a190SDave Jiang 	struct work_struct work;
183bfbaa952SDave Jiang 	struct nfit_test_fw *fw;
1846bc75619SDan Williams };
1856bc75619SDan Williams 
1869fb1a190SDave Jiang static struct workqueue_struct *nfit_wq;
1879fb1a190SDave Jiang 
1886bc75619SDan Williams static struct nfit_test *to_nfit_test(struct device *dev)
1896bc75619SDan Williams {
1906bc75619SDan Williams 	struct platform_device *pdev = to_platform_device(dev);
1916bc75619SDan Williams 
1926bc75619SDan Williams 	return container_of(pdev, struct nfit_test, pdev);
1936bc75619SDan Williams }
1946bc75619SDan Williams 
195bfbaa952SDave Jiang static int nd_intel_test_get_fw_info(struct nfit_test *t,
196bfbaa952SDave Jiang 		struct nd_intel_fw_info *nd_cmd, unsigned int buf_len,
197bfbaa952SDave Jiang 		int idx)
198bfbaa952SDave Jiang {
199bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
200bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
201bfbaa952SDave Jiang 
202bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p, buf_len: %u, idx: %d\n",
203bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
204bfbaa952SDave Jiang 
205bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
206bfbaa952SDave Jiang 		return -EINVAL;
207bfbaa952SDave Jiang 
208bfbaa952SDave Jiang 	nd_cmd->status = 0;
209bfbaa952SDave Jiang 	nd_cmd->storage_size = INTEL_FW_STORAGE_SIZE;
210bfbaa952SDave Jiang 	nd_cmd->max_send_len = INTEL_FW_MAX_SEND_LEN;
211bfbaa952SDave Jiang 	nd_cmd->query_interval = INTEL_FW_QUERY_INTERVAL;
212bfbaa952SDave Jiang 	nd_cmd->max_query_time = INTEL_FW_QUERY_MAX_TIME;
213bfbaa952SDave Jiang 	nd_cmd->update_cap = 0;
214bfbaa952SDave Jiang 	nd_cmd->fis_version = INTEL_FW_FIS_VERSION;
215bfbaa952SDave Jiang 	nd_cmd->run_version = 0;
216bfbaa952SDave Jiang 	nd_cmd->updated_version = fw->version;
217bfbaa952SDave Jiang 
218bfbaa952SDave Jiang 	return 0;
219bfbaa952SDave Jiang }
220bfbaa952SDave Jiang 
221bfbaa952SDave Jiang static int nd_intel_test_start_update(struct nfit_test *t,
222bfbaa952SDave Jiang 		struct nd_intel_fw_start *nd_cmd, unsigned int buf_len,
223bfbaa952SDave Jiang 		int idx)
224bfbaa952SDave Jiang {
225bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
226bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
227bfbaa952SDave Jiang 
228bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
229bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
230bfbaa952SDave Jiang 
231bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
232bfbaa952SDave Jiang 		return -EINVAL;
233bfbaa952SDave Jiang 
234bfbaa952SDave Jiang 	if (fw->state != FW_STATE_NEW) {
235bfbaa952SDave Jiang 		/* extended status, FW update in progress */
236bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
237bfbaa952SDave Jiang 		return 0;
238bfbaa952SDave Jiang 	}
239bfbaa952SDave Jiang 
240bfbaa952SDave Jiang 	fw->state = FW_STATE_IN_PROGRESS;
241bfbaa952SDave Jiang 	fw->context++;
242bfbaa952SDave Jiang 	fw->size_received = 0;
243bfbaa952SDave Jiang 	nd_cmd->status = 0;
244bfbaa952SDave Jiang 	nd_cmd->context = fw->context;
245bfbaa952SDave Jiang 
246bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context issued: %#x\n", __func__, nd_cmd->context);
247bfbaa952SDave Jiang 
248bfbaa952SDave Jiang 	return 0;
249bfbaa952SDave Jiang }
250bfbaa952SDave Jiang 
251bfbaa952SDave Jiang static int nd_intel_test_send_data(struct nfit_test *t,
252bfbaa952SDave Jiang 		struct nd_intel_fw_send_data *nd_cmd, unsigned int buf_len,
253bfbaa952SDave Jiang 		int idx)
254bfbaa952SDave Jiang {
255bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
256bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
257bfbaa952SDave Jiang 	u32 *status = (u32 *)&nd_cmd->data[nd_cmd->length];
258bfbaa952SDave Jiang 
259bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
260bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
261bfbaa952SDave Jiang 
262bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
263bfbaa952SDave Jiang 		return -EINVAL;
264bfbaa952SDave Jiang 
265bfbaa952SDave Jiang 
266bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->status: %#x\n", __func__, *status);
267bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[0]: %#x\n", __func__, nd_cmd->data[0]);
268bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[%u]: %#x\n", __func__, nd_cmd->length-1,
269bfbaa952SDave Jiang 			nd_cmd->data[nd_cmd->length-1]);
270bfbaa952SDave Jiang 
271bfbaa952SDave Jiang 	if (fw->state != FW_STATE_IN_PROGRESS) {
272bfbaa952SDave Jiang 		dev_dbg(dev, "%s: not in IN_PROGRESS state\n", __func__);
273bfbaa952SDave Jiang 		*status = 0x5;
274bfbaa952SDave Jiang 		return 0;
275bfbaa952SDave Jiang 	}
276bfbaa952SDave Jiang 
277bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
278bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
279bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
280bfbaa952SDave Jiang 		*status = 0x10007;
281bfbaa952SDave Jiang 		return 0;
282bfbaa952SDave Jiang 	}
283bfbaa952SDave Jiang 
284bfbaa952SDave Jiang 	/*
285bfbaa952SDave Jiang 	 * check offset + len > size of fw storage
286bfbaa952SDave Jiang 	 * check length is > max send length
287bfbaa952SDave Jiang 	 */
288bfbaa952SDave Jiang 	if (nd_cmd->offset + nd_cmd->length > INTEL_FW_STORAGE_SIZE ||
289bfbaa952SDave Jiang 			nd_cmd->length > INTEL_FW_MAX_SEND_LEN) {
290bfbaa952SDave Jiang 		*status = 0x3;
291bfbaa952SDave Jiang 		dev_dbg(dev, "%s: buffer boundary violation\n", __func__);
292bfbaa952SDave Jiang 		return 0;
293bfbaa952SDave Jiang 	}
294bfbaa952SDave Jiang 
295bfbaa952SDave Jiang 	fw->size_received += nd_cmd->length;
296bfbaa952SDave Jiang 	dev_dbg(dev, "%s: copying %u bytes, %u bytes so far\n",
297bfbaa952SDave Jiang 			__func__, nd_cmd->length, fw->size_received);
298bfbaa952SDave Jiang 	*status = 0;
299bfbaa952SDave Jiang 	return 0;
300bfbaa952SDave Jiang }
301bfbaa952SDave Jiang 
302bfbaa952SDave Jiang static int nd_intel_test_finish_fw(struct nfit_test *t,
303bfbaa952SDave Jiang 		struct nd_intel_fw_finish_update *nd_cmd,
304bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
305bfbaa952SDave Jiang {
306bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
307bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
308bfbaa952SDave Jiang 
309bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
310bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
311bfbaa952SDave Jiang 
312bfbaa952SDave Jiang 	if (fw->state == FW_STATE_UPDATED) {
313bfbaa952SDave Jiang 		/* update already done, need cold boot */
314bfbaa952SDave Jiang 		nd_cmd->status = 0x20007;
315bfbaa952SDave Jiang 		return 0;
316bfbaa952SDave Jiang 	}
317bfbaa952SDave Jiang 
318bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context: %#x  ctrl_flags: %#x\n",
319bfbaa952SDave Jiang 			__func__, nd_cmd->context, nd_cmd->ctrl_flags);
320bfbaa952SDave Jiang 
321bfbaa952SDave Jiang 	switch (nd_cmd->ctrl_flags) {
322bfbaa952SDave Jiang 	case 0: /* finish */
323bfbaa952SDave Jiang 		if (nd_cmd->context != fw->context) {
324bfbaa952SDave Jiang 			dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
325bfbaa952SDave Jiang 					__func__, nd_cmd->context,
326bfbaa952SDave Jiang 					fw->context);
327bfbaa952SDave Jiang 			nd_cmd->status = 0x10007;
328bfbaa952SDave Jiang 			return 0;
329bfbaa952SDave Jiang 		}
330bfbaa952SDave Jiang 		nd_cmd->status = 0;
331bfbaa952SDave Jiang 		fw->state = FW_STATE_VERIFY;
332bfbaa952SDave Jiang 		/* set 1 second of time for firmware "update" */
333bfbaa952SDave Jiang 		fw->end_time = jiffies + HZ;
334bfbaa952SDave Jiang 		break;
335bfbaa952SDave Jiang 
336bfbaa952SDave Jiang 	case 1: /* abort */
337bfbaa952SDave Jiang 		fw->size_received = 0;
338bfbaa952SDave Jiang 		/* successfully aborted status */
339bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
340bfbaa952SDave Jiang 		fw->state = FW_STATE_NEW;
341bfbaa952SDave Jiang 		dev_dbg(dev, "%s: abort successful\n", __func__);
342bfbaa952SDave Jiang 		break;
343bfbaa952SDave Jiang 
344bfbaa952SDave Jiang 	default: /* bad control flag */
345bfbaa952SDave Jiang 		dev_warn(dev, "%s: unknown control flag: %#x\n",
346bfbaa952SDave Jiang 				__func__, nd_cmd->ctrl_flags);
347bfbaa952SDave Jiang 		return -EINVAL;
348bfbaa952SDave Jiang 	}
349bfbaa952SDave Jiang 
350bfbaa952SDave Jiang 	return 0;
351bfbaa952SDave Jiang }
352bfbaa952SDave Jiang 
353bfbaa952SDave Jiang static int nd_intel_test_finish_query(struct nfit_test *t,
354bfbaa952SDave Jiang 		struct nd_intel_fw_finish_query *nd_cmd,
355bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
356bfbaa952SDave Jiang {
357bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
358bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
359bfbaa952SDave Jiang 
360bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
361bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
362bfbaa952SDave Jiang 
363bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
364bfbaa952SDave Jiang 		return -EINVAL;
365bfbaa952SDave Jiang 
366bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
367bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
368bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
369bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
370bfbaa952SDave Jiang 		return 0;
371bfbaa952SDave Jiang 	}
372bfbaa952SDave Jiang 
373bfbaa952SDave Jiang 	dev_dbg(dev, "%s context: %#x\n", __func__, nd_cmd->context);
374bfbaa952SDave Jiang 
375bfbaa952SDave Jiang 	switch (fw->state) {
376bfbaa952SDave Jiang 	case FW_STATE_NEW:
377bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
378bfbaa952SDave Jiang 		nd_cmd->status = 0;
379bfbaa952SDave Jiang 		dev_dbg(dev, "%s: new state\n", __func__);
380bfbaa952SDave Jiang 		break;
381bfbaa952SDave Jiang 
382bfbaa952SDave Jiang 	case FW_STATE_IN_PROGRESS:
383bfbaa952SDave Jiang 		/* sequencing error */
384bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
385bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
386bfbaa952SDave Jiang 		dev_dbg(dev, "%s: sequence error\n", __func__);
387bfbaa952SDave Jiang 		break;
388bfbaa952SDave Jiang 
389bfbaa952SDave Jiang 	case FW_STATE_VERIFY:
390bfbaa952SDave Jiang 		if (time_is_after_jiffies64(fw->end_time)) {
391bfbaa952SDave Jiang 			nd_cmd->updated_fw_rev = 0;
392bfbaa952SDave Jiang 			nd_cmd->status = 0x20007;
393bfbaa952SDave Jiang 			dev_dbg(dev, "%s: still verifying\n", __func__);
394bfbaa952SDave Jiang 			break;
395bfbaa952SDave Jiang 		}
396bfbaa952SDave Jiang 
397bfbaa952SDave Jiang 		dev_dbg(dev, "%s: transition out verify\n", __func__);
398bfbaa952SDave Jiang 		fw->state = FW_STATE_UPDATED;
399bfbaa952SDave Jiang 		/* we are going to fall through if it's "done" */
400bfbaa952SDave Jiang 	case FW_STATE_UPDATED:
401bfbaa952SDave Jiang 		nd_cmd->status = 0;
402bfbaa952SDave Jiang 		/* bogus test version */
403bfbaa952SDave Jiang 		fw->version = nd_cmd->updated_fw_rev =
404bfbaa952SDave Jiang 			INTEL_FW_FAKE_VERSION;
405bfbaa952SDave Jiang 		dev_dbg(dev, "%s: updated\n", __func__);
406bfbaa952SDave Jiang 		break;
407bfbaa952SDave Jiang 
408bfbaa952SDave Jiang 	default: /* we should never get here */
409bfbaa952SDave Jiang 		return -EINVAL;
410bfbaa952SDave Jiang 	}
411bfbaa952SDave Jiang 
412bfbaa952SDave Jiang 	return 0;
413bfbaa952SDave Jiang }
414bfbaa952SDave Jiang 
41539c686b8SVishal Verma static int nfit_test_cmd_get_config_size(struct nd_cmd_get_config_size *nd_cmd,
4166bc75619SDan Williams 		unsigned int buf_len)
4176bc75619SDan Williams {
4186bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4196bc75619SDan Williams 		return -EINVAL;
42039c686b8SVishal Verma 
4216bc75619SDan Williams 	nd_cmd->status = 0;
4226bc75619SDan Williams 	nd_cmd->config_size = LABEL_SIZE;
4236bc75619SDan Williams 	nd_cmd->max_xfer = SZ_4K;
42439c686b8SVishal Verma 
42539c686b8SVishal Verma 	return 0;
4266bc75619SDan Williams }
42739c686b8SVishal Verma 
42839c686b8SVishal Verma static int nfit_test_cmd_get_config_data(struct nd_cmd_get_config_data_hdr
42939c686b8SVishal Verma 		*nd_cmd, unsigned int buf_len, void *label)
43039c686b8SVishal Verma {
4316bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
43239c686b8SVishal Verma 	int rc;
4336bc75619SDan Williams 
4346bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4356bc75619SDan Williams 		return -EINVAL;
4366bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4376bc75619SDan Williams 		return -EINVAL;
4386bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) > buf_len)
4396bc75619SDan Williams 		return -EINVAL;
4406bc75619SDan Williams 
4416bc75619SDan Williams 	nd_cmd->status = 0;
4426bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
44339c686b8SVishal Verma 	memcpy(nd_cmd->out_buf, label + offset, len);
4446bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - len;
44539c686b8SVishal Verma 
44639c686b8SVishal Verma 	return rc;
4476bc75619SDan Williams }
44839c686b8SVishal Verma 
44939c686b8SVishal Verma static int nfit_test_cmd_set_config_data(struct nd_cmd_set_config_hdr *nd_cmd,
45039c686b8SVishal Verma 		unsigned int buf_len, void *label)
45139c686b8SVishal Verma {
4526bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
4536bc75619SDan Williams 	u32 *status;
45439c686b8SVishal Verma 	int rc;
4556bc75619SDan Williams 
4566bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4576bc75619SDan Williams 		return -EINVAL;
4586bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4596bc75619SDan Williams 		return -EINVAL;
4606bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) + 4 > buf_len)
4616bc75619SDan Williams 		return -EINVAL;
4626bc75619SDan Williams 
46339c686b8SVishal Verma 	status = (void *)nd_cmd + nd_cmd->in_length + sizeof(*nd_cmd);
4646bc75619SDan Williams 	*status = 0;
4656bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
46639c686b8SVishal Verma 	memcpy(label + offset, nd_cmd->in_buf, len);
4676bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - (len + 4);
46839c686b8SVishal Verma 
46939c686b8SVishal Verma 	return rc;
4706bc75619SDan Williams }
47139c686b8SVishal Verma 
472d4f32367SDan Williams #define NFIT_TEST_CLEAR_ERR_UNIT 256
473747ffe11SDan Williams 
47439c686b8SVishal Verma static int nfit_test_cmd_ars_cap(struct nd_cmd_ars_cap *nd_cmd,
47539c686b8SVishal Verma 		unsigned int buf_len)
47639c686b8SVishal Verma {
4779fb1a190SDave Jiang 	int ars_recs;
4789fb1a190SDave Jiang 
47939c686b8SVishal Verma 	if (buf_len < sizeof(*nd_cmd))
48039c686b8SVishal Verma 		return -EINVAL;
48139c686b8SVishal Verma 
4829fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
4839fb1a190SDave Jiang 	ars_recs = SZ_4K / sizeof(struct nd_ars_record);
4849fb1a190SDave Jiang 
485747ffe11SDan Williams 	nd_cmd->max_ars_out = sizeof(struct nd_cmd_ars_status)
4869fb1a190SDave Jiang 		+ ars_recs * sizeof(struct nd_ars_record);
48739c686b8SVishal Verma 	nd_cmd->status = (ND_ARS_PERSISTENT | ND_ARS_VOLATILE) << 16;
488d4f32367SDan Williams 	nd_cmd->clear_err_unit = NFIT_TEST_CLEAR_ERR_UNIT;
48939c686b8SVishal Verma 
49039c686b8SVishal Verma 	return 0;
49139c686b8SVishal Verma }
49239c686b8SVishal Verma 
4939fb1a190SDave Jiang static void post_ars_status(struct ars_state *ars_state,
4949fb1a190SDave Jiang 		struct badrange *badrange, u64 addr, u64 len)
49539c686b8SVishal Verma {
496f471f1a7SDan Williams 	struct nd_cmd_ars_status *ars_status;
497f471f1a7SDan Williams 	struct nd_ars_record *ars_record;
4989fb1a190SDave Jiang 	struct badrange_entry *be;
4999fb1a190SDave Jiang 	u64 end = addr + len - 1;
5009fb1a190SDave Jiang 	int i = 0;
501f471f1a7SDan Williams 
502f471f1a7SDan Williams 	ars_state->deadline = jiffies + 1*HZ;
503f471f1a7SDan Williams 	ars_status = ars_state->ars_status;
504f471f1a7SDan Williams 	ars_status->status = 0;
505f471f1a7SDan Williams 	ars_status->address = addr;
506f471f1a7SDan Williams 	ars_status->length = len;
507f471f1a7SDan Williams 	ars_status->type = ND_ARS_PERSISTENT;
5089fb1a190SDave Jiang 
5099fb1a190SDave Jiang 	spin_lock(&badrange->lock);
5109fb1a190SDave Jiang 	list_for_each_entry(be, &badrange->list, list) {
5119fb1a190SDave Jiang 		u64 be_end = be->start + be->length - 1;
5129fb1a190SDave Jiang 		u64 rstart, rend;
5139fb1a190SDave Jiang 
5149fb1a190SDave Jiang 		/* skip entries outside the range */
5159fb1a190SDave Jiang 		if (be_end < addr || be->start > end)
5169fb1a190SDave Jiang 			continue;
5179fb1a190SDave Jiang 
5189fb1a190SDave Jiang 		rstart = (be->start < addr) ? addr : be->start;
5199fb1a190SDave Jiang 		rend = (be_end < end) ? be_end : end;
5209fb1a190SDave Jiang 		ars_record = &ars_status->records[i];
521f471f1a7SDan Williams 		ars_record->handle = 0;
5229fb1a190SDave Jiang 		ars_record->err_address = rstart;
5239fb1a190SDave Jiang 		ars_record->length = rend - rstart + 1;
5249fb1a190SDave Jiang 		i++;
5259fb1a190SDave Jiang 	}
5269fb1a190SDave Jiang 	spin_unlock(&badrange->lock);
5279fb1a190SDave Jiang 	ars_status->num_records = i;
5289fb1a190SDave Jiang 	ars_status->out_length = sizeof(struct nd_cmd_ars_status)
5299fb1a190SDave Jiang 		+ i * sizeof(struct nd_ars_record);
530f471f1a7SDan Williams }
531f471f1a7SDan Williams 
5329fb1a190SDave Jiang static int nfit_test_cmd_ars_start(struct nfit_test *t,
5339fb1a190SDave Jiang 		struct ars_state *ars_state,
534f471f1a7SDan Williams 		struct nd_cmd_ars_start *ars_start, unsigned int buf_len,
535f471f1a7SDan Williams 		int *cmd_rc)
536f471f1a7SDan Williams {
537f471f1a7SDan Williams 	if (buf_len < sizeof(*ars_start))
53839c686b8SVishal Verma 		return -EINVAL;
53939c686b8SVishal Verma 
540f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
541f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
542f471f1a7SDan Williams 		ars_start->status = NFIT_ARS_START_BUSY;
543f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
544f471f1a7SDan Williams 	} else {
545f471f1a7SDan Williams 		ars_start->status = 0;
546f471f1a7SDan Williams 		ars_start->scrub_time = 1;
5479fb1a190SDave Jiang 		post_ars_status(ars_state, &t->badrange, ars_start->address,
548f471f1a7SDan Williams 				ars_start->length);
549f471f1a7SDan Williams 		*cmd_rc = 0;
550f471f1a7SDan Williams 	}
551f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
55239c686b8SVishal Verma 
55339c686b8SVishal Verma 	return 0;
55439c686b8SVishal Verma }
55539c686b8SVishal Verma 
556f471f1a7SDan Williams static int nfit_test_cmd_ars_status(struct ars_state *ars_state,
557f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status, unsigned int buf_len,
558f471f1a7SDan Williams 		int *cmd_rc)
55939c686b8SVishal Verma {
560f471f1a7SDan Williams 	if (buf_len < ars_state->ars_status->out_length)
56139c686b8SVishal Verma 		return -EINVAL;
56239c686b8SVishal Verma 
563f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
564f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
565f471f1a7SDan Williams 		memset(ars_status, 0, buf_len);
566f471f1a7SDan Williams 		ars_status->status = NFIT_ARS_STATUS_BUSY;
567f471f1a7SDan Williams 		ars_status->out_length = sizeof(*ars_status);
568f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
569f471f1a7SDan Williams 	} else {
570f471f1a7SDan Williams 		memcpy(ars_status, ars_state->ars_status,
571f471f1a7SDan Williams 				ars_state->ars_status->out_length);
572f471f1a7SDan Williams 		*cmd_rc = 0;
573f471f1a7SDan Williams 	}
574f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
57539c686b8SVishal Verma 	return 0;
57639c686b8SVishal Verma }
57739c686b8SVishal Verma 
5785e096ef3SVishal Verma static int nfit_test_cmd_clear_error(struct nfit_test *t,
5795e096ef3SVishal Verma 		struct nd_cmd_clear_error *clear_err,
580d4f32367SDan Williams 		unsigned int buf_len, int *cmd_rc)
581d4f32367SDan Williams {
582d4f32367SDan Williams 	const u64 mask = NFIT_TEST_CLEAR_ERR_UNIT - 1;
583d4f32367SDan Williams 	if (buf_len < sizeof(*clear_err))
584d4f32367SDan Williams 		return -EINVAL;
585d4f32367SDan Williams 
586d4f32367SDan Williams 	if ((clear_err->address & mask) || (clear_err->length & mask))
587d4f32367SDan Williams 		return -EINVAL;
588d4f32367SDan Williams 
5895e096ef3SVishal Verma 	badrange_forget(&t->badrange, clear_err->address, clear_err->length);
590d4f32367SDan Williams 	clear_err->status = 0;
591d4f32367SDan Williams 	clear_err->cleared = clear_err->length;
592d4f32367SDan Williams 	*cmd_rc = 0;
593d4f32367SDan Williams 	return 0;
594d4f32367SDan Williams }
595d4f32367SDan Williams 
59610246dc8SYasunori Goto struct region_search_spa {
59710246dc8SYasunori Goto 	u64 addr;
59810246dc8SYasunori Goto 	struct nd_region *region;
59910246dc8SYasunori Goto };
60010246dc8SYasunori Goto 
60110246dc8SYasunori Goto static int is_region_device(struct device *dev)
60210246dc8SYasunori Goto {
60310246dc8SYasunori Goto 	return !strncmp(dev->kobj.name, "region", 6);
60410246dc8SYasunori Goto }
60510246dc8SYasunori Goto 
60610246dc8SYasunori Goto static int nfit_test_search_region_spa(struct device *dev, void *data)
60710246dc8SYasunori Goto {
60810246dc8SYasunori Goto 	struct region_search_spa *ctx = data;
60910246dc8SYasunori Goto 	struct nd_region *nd_region;
61010246dc8SYasunori Goto 	resource_size_t ndr_end;
61110246dc8SYasunori Goto 
61210246dc8SYasunori Goto 	if (!is_region_device(dev))
61310246dc8SYasunori Goto 		return 0;
61410246dc8SYasunori Goto 
61510246dc8SYasunori Goto 	nd_region = to_nd_region(dev);
61610246dc8SYasunori Goto 	ndr_end = nd_region->ndr_start + nd_region->ndr_size;
61710246dc8SYasunori Goto 
61810246dc8SYasunori Goto 	if (ctx->addr >= nd_region->ndr_start && ctx->addr < ndr_end) {
61910246dc8SYasunori Goto 		ctx->region = nd_region;
62010246dc8SYasunori Goto 		return 1;
62110246dc8SYasunori Goto 	}
62210246dc8SYasunori Goto 
62310246dc8SYasunori Goto 	return 0;
62410246dc8SYasunori Goto }
62510246dc8SYasunori Goto 
62610246dc8SYasunori Goto static int nfit_test_search_spa(struct nvdimm_bus *bus,
62710246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa)
62810246dc8SYasunori Goto {
62910246dc8SYasunori Goto 	int ret;
63010246dc8SYasunori Goto 	struct nd_region *nd_region = NULL;
63110246dc8SYasunori Goto 	struct nvdimm *nvdimm = NULL;
63210246dc8SYasunori Goto 	struct nd_mapping *nd_mapping = NULL;
63310246dc8SYasunori Goto 	struct region_search_spa ctx = {
63410246dc8SYasunori Goto 		.addr = spa->spa,
63510246dc8SYasunori Goto 		.region = NULL,
63610246dc8SYasunori Goto 	};
63710246dc8SYasunori Goto 	u64 dpa;
63810246dc8SYasunori Goto 
63910246dc8SYasunori Goto 	ret = device_for_each_child(&bus->dev, &ctx,
64010246dc8SYasunori Goto 				nfit_test_search_region_spa);
64110246dc8SYasunori Goto 
64210246dc8SYasunori Goto 	if (!ret)
64310246dc8SYasunori Goto 		return -ENODEV;
64410246dc8SYasunori Goto 
64510246dc8SYasunori Goto 	nd_region = ctx.region;
64610246dc8SYasunori Goto 
64710246dc8SYasunori Goto 	dpa = ctx.addr - nd_region->ndr_start;
64810246dc8SYasunori Goto 
64910246dc8SYasunori Goto 	/*
65010246dc8SYasunori Goto 	 * last dimm is selected for test
65110246dc8SYasunori Goto 	 */
65210246dc8SYasunori Goto 	nd_mapping = &nd_region->mapping[nd_region->ndr_mappings - 1];
65310246dc8SYasunori Goto 	nvdimm = nd_mapping->nvdimm;
65410246dc8SYasunori Goto 
65510246dc8SYasunori Goto 	spa->devices[0].nfit_device_handle = handle[nvdimm->id];
65610246dc8SYasunori Goto 	spa->num_nvdimms = 1;
65710246dc8SYasunori Goto 	spa->devices[0].dpa = dpa;
65810246dc8SYasunori Goto 
65910246dc8SYasunori Goto 	return 0;
66010246dc8SYasunori Goto }
66110246dc8SYasunori Goto 
66210246dc8SYasunori Goto static int nfit_test_cmd_translate_spa(struct nvdimm_bus *bus,
66310246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa, unsigned int buf_len)
66410246dc8SYasunori Goto {
66510246dc8SYasunori Goto 	if (buf_len < spa->translate_length)
66610246dc8SYasunori Goto 		return -EINVAL;
66710246dc8SYasunori Goto 
66810246dc8SYasunori Goto 	if (nfit_test_search_spa(bus, spa) < 0 || !spa->num_nvdimms)
66910246dc8SYasunori Goto 		spa->status = 2;
67010246dc8SYasunori Goto 
67110246dc8SYasunori Goto 	return 0;
67210246dc8SYasunori Goto }
67310246dc8SYasunori Goto 
674ed07c433SDan Williams static int nfit_test_cmd_smart(struct nd_intel_smart *smart, unsigned int buf_len,
675ed07c433SDan Williams 		struct nd_intel_smart *smart_data)
676baa51277SDan Williams {
677baa51277SDan Williams 	if (buf_len < sizeof(*smart))
678baa51277SDan Williams 		return -EINVAL;
679ed07c433SDan Williams 	memcpy(smart, smart_data, sizeof(*smart));
680baa51277SDan Williams 	return 0;
681baa51277SDan Williams }
682baa51277SDan Williams 
683cdd77d3eSDan Williams static int nfit_test_cmd_smart_threshold(
684ed07c433SDan Williams 		struct nd_intel_smart_threshold *out,
685ed07c433SDan Williams 		unsigned int buf_len,
686ed07c433SDan Williams 		struct nd_intel_smart_threshold *smart_t)
687baa51277SDan Williams {
688baa51277SDan Williams 	if (buf_len < sizeof(*smart_t))
689baa51277SDan Williams 		return -EINVAL;
690ed07c433SDan Williams 	memcpy(out, smart_t, sizeof(*smart_t));
691ed07c433SDan Williams 	return 0;
692ed07c433SDan Williams }
693ed07c433SDan Williams 
694ed07c433SDan Williams static void smart_notify(struct device *bus_dev,
695ed07c433SDan Williams 		struct device *dimm_dev, struct nd_intel_smart *smart,
696ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh)
697ed07c433SDan Williams {
698ed07c433SDan Williams 	dev_dbg(dimm_dev, "%s: alarm: %#x spares: %d (%d) mtemp: %d (%d) ctemp: %d (%d)\n",
699ed07c433SDan Williams 			__func__, thresh->alarm_control, thresh->spares,
700ed07c433SDan Williams 			smart->spares, thresh->media_temperature,
701ed07c433SDan Williams 			smart->media_temperature, thresh->ctrl_temperature,
702ed07c433SDan Williams 			smart->ctrl_temperature);
703ed07c433SDan Williams 	if (((thresh->alarm_control & ND_INTEL_SMART_SPARE_TRIP)
704ed07c433SDan Williams 				&& smart->spares
705ed07c433SDan Williams 				<= thresh->spares)
706ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_TEMP_TRIP)
707ed07c433SDan Williams 				&& smart->media_temperature
708ed07c433SDan Williams 				>= thresh->media_temperature)
709ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_CTEMP_TRIP)
710ed07c433SDan Williams 				&& smart->ctrl_temperature
711ed07c433SDan Williams 				>= thresh->ctrl_temperature)) {
712ed07c433SDan Williams 		device_lock(bus_dev);
713ed07c433SDan Williams 		__acpi_nvdimm_notify(dimm_dev, 0x81);
714ed07c433SDan Williams 		device_unlock(bus_dev);
715ed07c433SDan Williams 	}
716ed07c433SDan Williams }
717ed07c433SDan Williams 
718ed07c433SDan Williams static int nfit_test_cmd_smart_set_threshold(
719ed07c433SDan Williams 		struct nd_intel_smart_set_threshold *in,
720ed07c433SDan Williams 		unsigned int buf_len,
721ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh,
722ed07c433SDan Williams 		struct nd_intel_smart *smart,
723ed07c433SDan Williams 		struct device *bus_dev, struct device *dimm_dev)
724ed07c433SDan Williams {
725ed07c433SDan Williams 	unsigned int size;
726ed07c433SDan Williams 
727ed07c433SDan Williams 	size = sizeof(*in) - 4;
728ed07c433SDan Williams 	if (buf_len < size)
729ed07c433SDan Williams 		return -EINVAL;
730ed07c433SDan Williams 	memcpy(thresh->data, in, size);
731ed07c433SDan Williams 	in->status = 0;
732ed07c433SDan Williams 	smart_notify(bus_dev, dimm_dev, smart, thresh);
733ed07c433SDan Williams 
734baa51277SDan Williams 	return 0;
735baa51277SDan Williams }
736baa51277SDan Williams 
7379fb1a190SDave Jiang static void uc_error_notify(struct work_struct *work)
7389fb1a190SDave Jiang {
7399fb1a190SDave Jiang 	struct nfit_test *t = container_of(work, typeof(*t), work);
7409fb1a190SDave Jiang 
7419fb1a190SDave Jiang 	__acpi_nfit_notify(&t->pdev.dev, t, NFIT_NOTIFY_UC_MEMORY_ERROR);
7429fb1a190SDave Jiang }
7439fb1a190SDave Jiang 
7449fb1a190SDave Jiang static int nfit_test_cmd_ars_error_inject(struct nfit_test *t,
7459fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj *err_inj, unsigned int buf_len)
7469fb1a190SDave Jiang {
7479fb1a190SDave Jiang 	int rc;
7489fb1a190SDave Jiang 
74941cb3301SVishal Verma 	if (buf_len != sizeof(*err_inj)) {
7509fb1a190SDave Jiang 		rc = -EINVAL;
7519fb1a190SDave Jiang 		goto err;
7529fb1a190SDave Jiang 	}
7539fb1a190SDave Jiang 
7549fb1a190SDave Jiang 	if (err_inj->err_inj_spa_range_length <= 0) {
7559fb1a190SDave Jiang 		rc = -EINVAL;
7569fb1a190SDave Jiang 		goto err;
7579fb1a190SDave Jiang 	}
7589fb1a190SDave Jiang 
7599fb1a190SDave Jiang 	rc =  badrange_add(&t->badrange, err_inj->err_inj_spa_range_base,
7609fb1a190SDave Jiang 			err_inj->err_inj_spa_range_length);
7619fb1a190SDave Jiang 	if (rc < 0)
7629fb1a190SDave Jiang 		goto err;
7639fb1a190SDave Jiang 
7649fb1a190SDave Jiang 	if (err_inj->err_inj_options & (1 << ND_ARS_ERR_INJ_OPT_NOTIFY))
7659fb1a190SDave Jiang 		queue_work(nfit_wq, &t->work);
7669fb1a190SDave Jiang 
7679fb1a190SDave Jiang 	err_inj->status = 0;
7689fb1a190SDave Jiang 	return 0;
7699fb1a190SDave Jiang 
7709fb1a190SDave Jiang err:
7719fb1a190SDave Jiang 	err_inj->status = NFIT_ARS_INJECT_INVALID;
7729fb1a190SDave Jiang 	return rc;
7739fb1a190SDave Jiang }
7749fb1a190SDave Jiang 
7759fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_clear(struct nfit_test *t,
7769fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_clr *err_clr, unsigned int buf_len)
7779fb1a190SDave Jiang {
7789fb1a190SDave Jiang 	int rc;
7799fb1a190SDave Jiang 
78041cb3301SVishal Verma 	if (buf_len != sizeof(*err_clr)) {
7819fb1a190SDave Jiang 		rc = -EINVAL;
7829fb1a190SDave Jiang 		goto err;
7839fb1a190SDave Jiang 	}
7849fb1a190SDave Jiang 
7859fb1a190SDave Jiang 	if (err_clr->err_inj_clr_spa_range_length <= 0) {
7869fb1a190SDave Jiang 		rc = -EINVAL;
7879fb1a190SDave Jiang 		goto err;
7889fb1a190SDave Jiang 	}
7899fb1a190SDave Jiang 
7909fb1a190SDave Jiang 	badrange_forget(&t->badrange, err_clr->err_inj_clr_spa_range_base,
7919fb1a190SDave Jiang 			err_clr->err_inj_clr_spa_range_length);
7929fb1a190SDave Jiang 
7939fb1a190SDave Jiang 	err_clr->status = 0;
7949fb1a190SDave Jiang 	return 0;
7959fb1a190SDave Jiang 
7969fb1a190SDave Jiang err:
7979fb1a190SDave Jiang 	err_clr->status = NFIT_ARS_INJECT_INVALID;
7989fb1a190SDave Jiang 	return rc;
7999fb1a190SDave Jiang }
8009fb1a190SDave Jiang 
8019fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_status(struct nfit_test *t,
8029fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_stat *err_stat,
8039fb1a190SDave Jiang 		unsigned int buf_len)
8049fb1a190SDave Jiang {
8059fb1a190SDave Jiang 	struct badrange_entry *be;
8069fb1a190SDave Jiang 	int max = SZ_4K / sizeof(struct nd_error_stat_query_record);
8079fb1a190SDave Jiang 	int i = 0;
8089fb1a190SDave Jiang 
8099fb1a190SDave Jiang 	err_stat->status = 0;
8109fb1a190SDave Jiang 	spin_lock(&t->badrange.lock);
8119fb1a190SDave Jiang 	list_for_each_entry(be, &t->badrange.list, list) {
8129fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_base = be->start;
8139fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_length = be->length;
8149fb1a190SDave Jiang 		i++;
8159fb1a190SDave Jiang 		if (i > max)
8169fb1a190SDave Jiang 			break;
8179fb1a190SDave Jiang 	}
8189fb1a190SDave Jiang 	spin_unlock(&t->badrange.lock);
8199fb1a190SDave Jiang 	err_stat->inj_err_rec_count = i;
8209fb1a190SDave Jiang 
8219fb1a190SDave Jiang 	return 0;
8229fb1a190SDave Jiang }
8239fb1a190SDave Jiang 
824*674d8bdeSDave Jiang static int nd_intel_test_cmd_set_lss_status(struct nfit_test *t,
825*674d8bdeSDave Jiang 		struct nd_intel_lss *nd_cmd, unsigned int buf_len)
826*674d8bdeSDave Jiang {
827*674d8bdeSDave Jiang 	struct device *dev = &t->pdev.dev;
828*674d8bdeSDave Jiang 
829*674d8bdeSDave Jiang 	if (buf_len < sizeof(*nd_cmd))
830*674d8bdeSDave Jiang 		return -EINVAL;
831*674d8bdeSDave Jiang 
832*674d8bdeSDave Jiang 	switch (nd_cmd->enable) {
833*674d8bdeSDave Jiang 	case 0:
834*674d8bdeSDave Jiang 		nd_cmd->status = 0;
835*674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status disabled\n",
836*674d8bdeSDave Jiang 				__func__);
837*674d8bdeSDave Jiang 		break;
838*674d8bdeSDave Jiang 	case 1:
839*674d8bdeSDave Jiang 		nd_cmd->status = 0;
840*674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status enabled\n",
841*674d8bdeSDave Jiang 				__func__);
842*674d8bdeSDave Jiang 		break;
843*674d8bdeSDave Jiang 	default:
844*674d8bdeSDave Jiang 		dev_warn(dev, "Unknown enable value: %#x\n", nd_cmd->enable);
845*674d8bdeSDave Jiang 		nd_cmd->status = 0x3;
846*674d8bdeSDave Jiang 		break;
847*674d8bdeSDave Jiang 	}
848*674d8bdeSDave Jiang 
849*674d8bdeSDave Jiang 
850*674d8bdeSDave Jiang 	return 0;
851*674d8bdeSDave Jiang }
852*674d8bdeSDave Jiang 
853bfbaa952SDave Jiang static int get_dimm(struct nfit_mem *nfit_mem, unsigned int func)
854bfbaa952SDave Jiang {
855bfbaa952SDave Jiang 	int i;
856bfbaa952SDave Jiang 
857bfbaa952SDave Jiang 	/* lookup per-dimm data */
858bfbaa952SDave Jiang 	for (i = 0; i < ARRAY_SIZE(handle); i++)
859bfbaa952SDave Jiang 		if (__to_nfit_memdev(nfit_mem)->device_handle == handle[i])
860bfbaa952SDave Jiang 			break;
861bfbaa952SDave Jiang 	if (i >= ARRAY_SIZE(handle))
862bfbaa952SDave Jiang 		return -ENXIO;
863bfbaa952SDave Jiang 
864bfbaa952SDave Jiang 	if ((1 << func) & dimm_fail_cmd_flags[i])
865bfbaa952SDave Jiang 		return -EIO;
866bfbaa952SDave Jiang 
867bfbaa952SDave Jiang 	return i;
868bfbaa952SDave Jiang }
869bfbaa952SDave Jiang 
87039c686b8SVishal Verma static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
87139c686b8SVishal Verma 		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
872aef25338SDan Williams 		unsigned int buf_len, int *cmd_rc)
87339c686b8SVishal Verma {
87439c686b8SVishal Verma 	struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
87539c686b8SVishal Verma 	struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
8766634fb06SDan Williams 	unsigned int func = cmd;
877f471f1a7SDan Williams 	int i, rc = 0, __cmd_rc;
878f471f1a7SDan Williams 
879f471f1a7SDan Williams 	if (!cmd_rc)
880f471f1a7SDan Williams 		cmd_rc = &__cmd_rc;
881f471f1a7SDan Williams 	*cmd_rc = 0;
88239c686b8SVishal Verma 
88339c686b8SVishal Verma 	if (nvdimm) {
88439c686b8SVishal Verma 		struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
885e3654ecaSDan Williams 		unsigned long cmd_mask = nvdimm_cmd_mask(nvdimm);
88639c686b8SVishal Verma 
8876634fb06SDan Williams 		if (!nfit_mem)
8886634fb06SDan Williams 			return -ENOTTY;
8896634fb06SDan Williams 
8906634fb06SDan Williams 		if (cmd == ND_CMD_CALL) {
8916634fb06SDan Williams 			struct nd_cmd_pkg *call_pkg = buf;
8926634fb06SDan Williams 
8936634fb06SDan Williams 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
8946634fb06SDan Williams 			buf = (void *) call_pkg->nd_payload;
8956634fb06SDan Williams 			func = call_pkg->nd_command;
8966634fb06SDan Williams 			if (call_pkg->nd_family != nfit_mem->family)
8976634fb06SDan Williams 				return -ENOTTY;
898bfbaa952SDave Jiang 
899bfbaa952SDave Jiang 			i = get_dimm(nfit_mem, func);
900bfbaa952SDave Jiang 			if (i < 0)
901bfbaa952SDave Jiang 				return i;
902bfbaa952SDave Jiang 
903bfbaa952SDave Jiang 			switch (func) {
904*674d8bdeSDave Jiang 			case ND_INTEL_ENABLE_LSS_STATUS:
905*674d8bdeSDave Jiang 				return nd_intel_test_cmd_set_lss_status(t,
906*674d8bdeSDave Jiang 						buf, buf_len);
907bfbaa952SDave Jiang 			case ND_INTEL_FW_GET_INFO:
908bfbaa952SDave Jiang 				return nd_intel_test_get_fw_info(t, buf,
909bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
910bfbaa952SDave Jiang 			case ND_INTEL_FW_START_UPDATE:
911bfbaa952SDave Jiang 				return nd_intel_test_start_update(t, buf,
912bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
913bfbaa952SDave Jiang 			case ND_INTEL_FW_SEND_DATA:
914bfbaa952SDave Jiang 				return nd_intel_test_send_data(t, buf,
915bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
916bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_UPDATE:
917bfbaa952SDave Jiang 				return nd_intel_test_finish_fw(t, buf,
918bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
919bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_QUERY:
920bfbaa952SDave Jiang 				return nd_intel_test_finish_query(t, buf,
921bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
922bfbaa952SDave Jiang 			case ND_INTEL_SMART:
923bfbaa952SDave Jiang 				return nfit_test_cmd_smart(buf, buf_len,
924bfbaa952SDave Jiang 						&t->smart[i - t->dcr_idx]);
925bfbaa952SDave Jiang 			case ND_INTEL_SMART_THRESHOLD:
926bfbaa952SDave Jiang 				return nfit_test_cmd_smart_threshold(buf,
927bfbaa952SDave Jiang 						buf_len,
928bfbaa952SDave Jiang 						&t->smart_threshold[i -
929bfbaa952SDave Jiang 							t->dcr_idx]);
930bfbaa952SDave Jiang 			case ND_INTEL_SMART_SET_THRESHOLD:
931bfbaa952SDave Jiang 				return nfit_test_cmd_smart_set_threshold(buf,
932bfbaa952SDave Jiang 						buf_len,
933bfbaa952SDave Jiang 						&t->smart_threshold[i -
934bfbaa952SDave Jiang 							t->dcr_idx],
935bfbaa952SDave Jiang 						&t->smart[i - t->dcr_idx],
936bfbaa952SDave Jiang 						&t->pdev.dev, t->dimm_dev[i]);
937bfbaa952SDave Jiang 			default:
938bfbaa952SDave Jiang 				return -ENOTTY;
939bfbaa952SDave Jiang 			}
9406634fb06SDan Williams 		}
9416634fb06SDan Williams 
9426634fb06SDan Williams 		if (!test_bit(cmd, &cmd_mask)
9436634fb06SDan Williams 				|| !test_bit(func, &nfit_mem->dsm_mask))
94439c686b8SVishal Verma 			return -ENOTTY;
94539c686b8SVishal Verma 
946bfbaa952SDave Jiang 		i = get_dimm(nfit_mem, func);
947bfbaa952SDave Jiang 		if (i < 0)
948bfbaa952SDave Jiang 			return i;
94973606afdSDan Williams 
9506634fb06SDan Williams 		switch (func) {
95139c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_SIZE:
95239c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_size(buf, buf_len);
95339c686b8SVishal Verma 			break;
95439c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_DATA:
95539c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_data(buf, buf_len,
956dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
95739c686b8SVishal Verma 			break;
95839c686b8SVishal Verma 		case ND_CMD_SET_CONFIG_DATA:
95939c686b8SVishal Verma 			rc = nfit_test_cmd_set_config_data(buf, buf_len,
960dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
96139c686b8SVishal Verma 			break;
9626bc75619SDan Williams 		default:
9636bc75619SDan Williams 			return -ENOTTY;
9646bc75619SDan Williams 		}
96539c686b8SVishal Verma 	} else {
966f471f1a7SDan Williams 		struct ars_state *ars_state = &t->ars_state;
96710246dc8SYasunori Goto 		struct nd_cmd_pkg *call_pkg = buf;
96810246dc8SYasunori Goto 
96910246dc8SYasunori Goto 		if (!nd_desc)
97010246dc8SYasunori Goto 			return -ENOTTY;
97110246dc8SYasunori Goto 
97210246dc8SYasunori Goto 		if (cmd == ND_CMD_CALL) {
97310246dc8SYasunori Goto 			func = call_pkg->nd_command;
97410246dc8SYasunori Goto 
97510246dc8SYasunori Goto 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
97610246dc8SYasunori Goto 			buf = (void *) call_pkg->nd_payload;
97710246dc8SYasunori Goto 
97810246dc8SYasunori Goto 			switch (func) {
97910246dc8SYasunori Goto 			case NFIT_CMD_TRANSLATE_SPA:
98010246dc8SYasunori Goto 				rc = nfit_test_cmd_translate_spa(
98110246dc8SYasunori Goto 					acpi_desc->nvdimm_bus, buf, buf_len);
98210246dc8SYasunori Goto 				return rc;
9839fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_SET:
9849fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_error_inject(t, buf,
9859fb1a190SDave Jiang 					buf_len);
9869fb1a190SDave Jiang 				return rc;
9879fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_CLEAR:
9889fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_clear(t, buf,
9899fb1a190SDave Jiang 					buf_len);
9909fb1a190SDave Jiang 				return rc;
9919fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_GET:
9929fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_status(t, buf,
9939fb1a190SDave Jiang 					buf_len);
9949fb1a190SDave Jiang 				return rc;
99510246dc8SYasunori Goto 			default:
99610246dc8SYasunori Goto 				return -ENOTTY;
99710246dc8SYasunori Goto 			}
99810246dc8SYasunori Goto 		}
999f471f1a7SDan Williams 
1000e3654ecaSDan Williams 		if (!nd_desc || !test_bit(cmd, &nd_desc->cmd_mask))
100139c686b8SVishal Verma 			return -ENOTTY;
100239c686b8SVishal Verma 
10036634fb06SDan Williams 		switch (func) {
100439c686b8SVishal Verma 		case ND_CMD_ARS_CAP:
100539c686b8SVishal Verma 			rc = nfit_test_cmd_ars_cap(buf, buf_len);
100639c686b8SVishal Verma 			break;
100739c686b8SVishal Verma 		case ND_CMD_ARS_START:
10089fb1a190SDave Jiang 			rc = nfit_test_cmd_ars_start(t, ars_state, buf,
10099fb1a190SDave Jiang 					buf_len, cmd_rc);
101039c686b8SVishal Verma 			break;
101139c686b8SVishal Verma 		case ND_CMD_ARS_STATUS:
1012f471f1a7SDan Williams 			rc = nfit_test_cmd_ars_status(ars_state, buf, buf_len,
1013f471f1a7SDan Williams 					cmd_rc);
101439c686b8SVishal Verma 			break;
1015d4f32367SDan Williams 		case ND_CMD_CLEAR_ERROR:
10165e096ef3SVishal Verma 			rc = nfit_test_cmd_clear_error(t, buf, buf_len, cmd_rc);
1017d4f32367SDan Williams 			break;
101839c686b8SVishal Verma 		default:
101939c686b8SVishal Verma 			return -ENOTTY;
102039c686b8SVishal Verma 		}
102139c686b8SVishal Verma 	}
10226bc75619SDan Williams 
10236bc75619SDan Williams 	return rc;
10246bc75619SDan Williams }
10256bc75619SDan Williams 
10266bc75619SDan Williams static DEFINE_SPINLOCK(nfit_test_lock);
10276bc75619SDan Williams static struct nfit_test *instances[NUM_NFITS];
10286bc75619SDan Williams 
10296bc75619SDan Williams static void release_nfit_res(void *data)
10306bc75619SDan Williams {
10316bc75619SDan Williams 	struct nfit_test_resource *nfit_res = data;
10326bc75619SDan Williams 
10336bc75619SDan Williams 	spin_lock(&nfit_test_lock);
10346bc75619SDan Williams 	list_del(&nfit_res->list);
10356bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
10366bc75619SDan Williams 
10376bc75619SDan Williams 	vfree(nfit_res->buf);
10386bc75619SDan Williams 	kfree(nfit_res);
10396bc75619SDan Williams }
10406bc75619SDan Williams 
10416bc75619SDan Williams static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
10426bc75619SDan Williams 		void *buf)
10436bc75619SDan Williams {
10446bc75619SDan Williams 	struct device *dev = &t->pdev.dev;
10456bc75619SDan Williams 	struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
10466bc75619SDan Williams 			GFP_KERNEL);
10476bc75619SDan Williams 	int rc;
10486bc75619SDan Williams 
1049bd4cd745SDan Williams 	if (!buf || !nfit_res)
10506bc75619SDan Williams 		goto err;
10516bc75619SDan Williams 	rc = devm_add_action(dev, release_nfit_res, nfit_res);
10526bc75619SDan Williams 	if (rc)
10536bc75619SDan Williams 		goto err;
10546bc75619SDan Williams 	INIT_LIST_HEAD(&nfit_res->list);
10556bc75619SDan Williams 	memset(buf, 0, size);
10566bc75619SDan Williams 	nfit_res->dev = dev;
10576bc75619SDan Williams 	nfit_res->buf = buf;
1058bd4cd745SDan Williams 	nfit_res->res.start = *dma;
1059bd4cd745SDan Williams 	nfit_res->res.end = *dma + size - 1;
1060bd4cd745SDan Williams 	nfit_res->res.name = "NFIT";
1061bd4cd745SDan Williams 	spin_lock_init(&nfit_res->lock);
1062bd4cd745SDan Williams 	INIT_LIST_HEAD(&nfit_res->requests);
10636bc75619SDan Williams 	spin_lock(&nfit_test_lock);
10646bc75619SDan Williams 	list_add(&nfit_res->list, &t->resources);
10656bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
10666bc75619SDan Williams 
10676bc75619SDan Williams 	return nfit_res->buf;
10686bc75619SDan Williams  err:
1069ee8520feSDan Williams 	if (buf)
10706bc75619SDan Williams 		vfree(buf);
10716bc75619SDan Williams 	kfree(nfit_res);
10726bc75619SDan Williams 	return NULL;
10736bc75619SDan Williams }
10746bc75619SDan Williams 
10756bc75619SDan Williams static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
10766bc75619SDan Williams {
10776bc75619SDan Williams 	void *buf = vmalloc(size);
10786bc75619SDan Williams 
10796bc75619SDan Williams 	*dma = (unsigned long) buf;
10806bc75619SDan Williams 	return __test_alloc(t, size, dma, buf);
10816bc75619SDan Williams }
10826bc75619SDan Williams 
10836bc75619SDan Williams static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
10846bc75619SDan Williams {
10856bc75619SDan Williams 	int i;
10866bc75619SDan Williams 
10876bc75619SDan Williams 	for (i = 0; i < ARRAY_SIZE(instances); i++) {
10886bc75619SDan Williams 		struct nfit_test_resource *n, *nfit_res = NULL;
10896bc75619SDan Williams 		struct nfit_test *t = instances[i];
10906bc75619SDan Williams 
10916bc75619SDan Williams 		if (!t)
10926bc75619SDan Williams 			continue;
10936bc75619SDan Williams 		spin_lock(&nfit_test_lock);
10946bc75619SDan Williams 		list_for_each_entry(n, &t->resources, list) {
1095bd4cd745SDan Williams 			if (addr >= n->res.start && (addr < n->res.start
1096bd4cd745SDan Williams 						+ resource_size(&n->res))) {
10976bc75619SDan Williams 				nfit_res = n;
10986bc75619SDan Williams 				break;
10996bc75619SDan Williams 			} else if (addr >= (unsigned long) n->buf
11006bc75619SDan Williams 					&& (addr < (unsigned long) n->buf
1101bd4cd745SDan Williams 						+ resource_size(&n->res))) {
11026bc75619SDan Williams 				nfit_res = n;
11036bc75619SDan Williams 				break;
11046bc75619SDan Williams 			}
11056bc75619SDan Williams 		}
11066bc75619SDan Williams 		spin_unlock(&nfit_test_lock);
11076bc75619SDan Williams 		if (nfit_res)
11086bc75619SDan Williams 			return nfit_res;
11096bc75619SDan Williams 	}
11106bc75619SDan Williams 
11116bc75619SDan Williams 	return NULL;
11126bc75619SDan Williams }
11136bc75619SDan Williams 
1114f471f1a7SDan Williams static int ars_state_init(struct device *dev, struct ars_state *ars_state)
1115f471f1a7SDan Williams {
11169fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
1117f471f1a7SDan Williams 	ars_state->ars_status = devm_kzalloc(dev,
11189fb1a190SDave Jiang 			sizeof(struct nd_cmd_ars_status) + SZ_4K, GFP_KERNEL);
1119f471f1a7SDan Williams 	if (!ars_state->ars_status)
1120f471f1a7SDan Williams 		return -ENOMEM;
1121f471f1a7SDan Williams 	spin_lock_init(&ars_state->lock);
1122f471f1a7SDan Williams 	return 0;
1123f471f1a7SDan Williams }
1124f471f1a7SDan Williams 
1125231bf117SDan Williams static void put_dimms(void *data)
1126231bf117SDan Williams {
1127231bf117SDan Williams 	struct device **dimm_dev = data;
1128231bf117SDan Williams 	int i;
1129231bf117SDan Williams 
1130231bf117SDan Williams 	for (i = 0; i < NUM_DCR; i++)
1131231bf117SDan Williams 		if (dimm_dev[i])
1132231bf117SDan Williams 			device_unregister(dimm_dev[i]);
1133231bf117SDan Williams }
1134231bf117SDan Williams 
1135231bf117SDan Williams static struct class *nfit_test_dimm;
1136231bf117SDan Williams 
113773606afdSDan Williams static int dimm_name_to_id(struct device *dev)
113873606afdSDan Williams {
113973606afdSDan Williams 	int dimm;
114073606afdSDan Williams 
114173606afdSDan Williams 	if (sscanf(dev_name(dev), "test_dimm%d", &dimm) != 1
114273606afdSDan Williams 			|| dimm >= NUM_DCR || dimm < 0)
114373606afdSDan Williams 		return -ENXIO;
114473606afdSDan Williams 	return dimm;
114573606afdSDan Williams }
114673606afdSDan Williams 
114773606afdSDan Williams 
114873606afdSDan Williams static ssize_t handle_show(struct device *dev, struct device_attribute *attr,
114973606afdSDan Williams 		char *buf)
115073606afdSDan Williams {
115173606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
115273606afdSDan Williams 
115373606afdSDan Williams 	if (dimm < 0)
115473606afdSDan Williams 		return dimm;
115573606afdSDan Williams 
115673606afdSDan Williams 	return sprintf(buf, "%#x", handle[dimm]);
115773606afdSDan Williams }
115873606afdSDan Williams DEVICE_ATTR_RO(handle);
115973606afdSDan Williams 
116073606afdSDan Williams static ssize_t fail_cmd_show(struct device *dev, struct device_attribute *attr,
116173606afdSDan Williams 		char *buf)
116273606afdSDan Williams {
116373606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
116473606afdSDan Williams 
116573606afdSDan Williams 	if (dimm < 0)
116673606afdSDan Williams 		return dimm;
116773606afdSDan Williams 
116873606afdSDan Williams 	return sprintf(buf, "%#lx\n", dimm_fail_cmd_flags[dimm]);
116973606afdSDan Williams }
117073606afdSDan Williams 
117173606afdSDan Williams static ssize_t fail_cmd_store(struct device *dev, struct device_attribute *attr,
117273606afdSDan Williams 		const char *buf, size_t size)
117373606afdSDan Williams {
117473606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
117573606afdSDan Williams 	unsigned long val;
117673606afdSDan Williams 	ssize_t rc;
117773606afdSDan Williams 
117873606afdSDan Williams 	if (dimm < 0)
117973606afdSDan Williams 		return dimm;
118073606afdSDan Williams 
118173606afdSDan Williams 	rc = kstrtol(buf, 0, &val);
118273606afdSDan Williams 	if (rc)
118373606afdSDan Williams 		return rc;
118473606afdSDan Williams 
118573606afdSDan Williams 	dimm_fail_cmd_flags[dimm] = val;
118673606afdSDan Williams 	return size;
118773606afdSDan Williams }
118873606afdSDan Williams static DEVICE_ATTR_RW(fail_cmd);
118973606afdSDan Williams 
119073606afdSDan Williams static struct attribute *nfit_test_dimm_attributes[] = {
119173606afdSDan Williams 	&dev_attr_fail_cmd.attr,
119273606afdSDan Williams 	&dev_attr_handle.attr,
119373606afdSDan Williams 	NULL,
119473606afdSDan Williams };
119573606afdSDan Williams 
119673606afdSDan Williams static struct attribute_group nfit_test_dimm_attribute_group = {
119773606afdSDan Williams 	.attrs = nfit_test_dimm_attributes,
119873606afdSDan Williams };
119973606afdSDan Williams 
120073606afdSDan Williams static const struct attribute_group *nfit_test_dimm_attribute_groups[] = {
120173606afdSDan Williams 	&nfit_test_dimm_attribute_group,
120273606afdSDan Williams 	NULL,
120373606afdSDan Williams };
120473606afdSDan Williams 
1205ed07c433SDan Williams static void smart_init(struct nfit_test *t)
1206ed07c433SDan Williams {
1207ed07c433SDan Williams 	int i;
1208ed07c433SDan Williams 	const struct nd_intel_smart_threshold smart_t_data = {
1209ed07c433SDan Williams 		.alarm_control = ND_INTEL_SMART_SPARE_TRIP
1210ed07c433SDan Williams 			| ND_INTEL_SMART_TEMP_TRIP,
1211ed07c433SDan Williams 		.media_temperature = 40 * 16,
1212ed07c433SDan Williams 		.ctrl_temperature = 30 * 16,
1213ed07c433SDan Williams 		.spares = 5,
1214ed07c433SDan Williams 	};
1215ed07c433SDan Williams 	const struct nd_intel_smart smart_data = {
1216ed07c433SDan Williams 		.flags = ND_INTEL_SMART_HEALTH_VALID
1217ed07c433SDan Williams 			| ND_INTEL_SMART_SPARES_VALID
1218ed07c433SDan Williams 			| ND_INTEL_SMART_ALARM_VALID
1219ed07c433SDan Williams 			| ND_INTEL_SMART_USED_VALID
1220ed07c433SDan Williams 			| ND_INTEL_SMART_SHUTDOWN_VALID
1221ed07c433SDan Williams 			| ND_INTEL_SMART_MTEMP_VALID,
1222ed07c433SDan Williams 		.health = ND_INTEL_SMART_NON_CRITICAL_HEALTH,
1223ed07c433SDan Williams 		.media_temperature = 23 * 16,
1224ed07c433SDan Williams 		.ctrl_temperature = 30 * 16,
1225ed07c433SDan Williams 		.pmic_temperature = 40 * 16,
1226ed07c433SDan Williams 		.spares = 75,
1227ed07c433SDan Williams 		.alarm_flags = ND_INTEL_SMART_SPARE_TRIP
1228ed07c433SDan Williams 			| ND_INTEL_SMART_TEMP_TRIP,
1229ed07c433SDan Williams 		.ait_status = 1,
1230ed07c433SDan Williams 		.life_used = 5,
1231ed07c433SDan Williams 		.shutdown_state = 0,
1232ed07c433SDan Williams 		.vendor_size = 0,
1233ed07c433SDan Williams 		.shutdown_count = 100,
1234ed07c433SDan Williams 	};
1235ed07c433SDan Williams 
1236ed07c433SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1237ed07c433SDan Williams 		memcpy(&t->smart[i], &smart_data, sizeof(smart_data));
1238ed07c433SDan Williams 		memcpy(&t->smart_threshold[i], &smart_t_data,
1239ed07c433SDan Williams 				sizeof(smart_t_data));
1240ed07c433SDan Williams 	}
1241ed07c433SDan Williams }
1242ed07c433SDan Williams 
12436bc75619SDan Williams static int nfit_test0_alloc(struct nfit_test *t)
12446bc75619SDan Williams {
12456b577c9dSLinda Knippers 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * NUM_SPA
12466bc75619SDan Williams 			+ sizeof(struct acpi_nfit_memory_map) * NUM_MEM
12476bc75619SDan Williams 			+ sizeof(struct acpi_nfit_control_region) * NUM_DCR
12483b87356fSDan Williams 			+ offsetof(struct acpi_nfit_control_region,
12493b87356fSDan Williams 					window_size) * NUM_DCR
12509d27a87eSDan Williams 			+ sizeof(struct acpi_nfit_data_region) * NUM_BDW
125185d3fa02SDan Williams 			+ (sizeof(struct acpi_nfit_flush_address)
125285d3fa02SDan Williams 					+ sizeof(u64) * NUM_HINTS) * NUM_DCR;
12536bc75619SDan Williams 	int i;
12546bc75619SDan Williams 
12556bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
12566bc75619SDan Williams 	if (!t->nfit_buf)
12576bc75619SDan Williams 		return -ENOMEM;
12586bc75619SDan Williams 	t->nfit_size = nfit_size;
12596bc75619SDan Williams 
1260ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[0]);
12616bc75619SDan Williams 	if (!t->spa_set[0])
12626bc75619SDan Williams 		return -ENOMEM;
12636bc75619SDan Williams 
1264ee8520feSDan Williams 	t->spa_set[1] = test_alloc(t, SPA1_SIZE, &t->spa_set_dma[1]);
12656bc75619SDan Williams 	if (!t->spa_set[1])
12666bc75619SDan Williams 		return -ENOMEM;
12676bc75619SDan Williams 
1268ee8520feSDan Williams 	t->spa_set[2] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[2]);
126920985164SVishal Verma 	if (!t->spa_set[2])
127020985164SVishal Verma 		return -ENOMEM;
127120985164SVishal Verma 
1272dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
12736bc75619SDan Williams 		t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
12746bc75619SDan Williams 		if (!t->dimm[i])
12756bc75619SDan Williams 			return -ENOMEM;
12766bc75619SDan Williams 
12776bc75619SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
12786bc75619SDan Williams 		if (!t->label[i])
12796bc75619SDan Williams 			return -ENOMEM;
12806bc75619SDan Williams 		sprintf(t->label[i], "label%d", i);
12819d27a87eSDan Williams 
12829d15ce9cSDan Williams 		t->flush[i] = test_alloc(t, max(PAGE_SIZE,
12839d15ce9cSDan Williams 					sizeof(u64) * NUM_HINTS),
128485d3fa02SDan Williams 				&t->flush_dma[i]);
12859d27a87eSDan Williams 		if (!t->flush[i])
12869d27a87eSDan Williams 			return -ENOMEM;
12876bc75619SDan Williams 	}
12886bc75619SDan Williams 
1289dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
12906bc75619SDan Williams 		t->dcr[i] = test_alloc(t, LABEL_SIZE, &t->dcr_dma[i]);
12916bc75619SDan Williams 		if (!t->dcr[i])
12926bc75619SDan Williams 			return -ENOMEM;
12936bc75619SDan Williams 	}
12946bc75619SDan Williams 
1295c14a868aSDan Williams 	t->_fit = test_alloc(t, sizeof(union acpi_object **), &t->_fit_dma);
1296c14a868aSDan Williams 	if (!t->_fit)
1297c14a868aSDan Williams 		return -ENOMEM;
1298c14a868aSDan Williams 
1299231bf117SDan Williams 	if (devm_add_action_or_reset(&t->pdev.dev, put_dimms, t->dimm_dev))
1300231bf117SDan Williams 		return -ENOMEM;
1301231bf117SDan Williams 	for (i = 0; i < NUM_DCR; i++) {
130273606afdSDan Williams 		t->dimm_dev[i] = device_create_with_groups(nfit_test_dimm,
130373606afdSDan Williams 				&t->pdev.dev, 0, NULL,
130473606afdSDan Williams 				nfit_test_dimm_attribute_groups,
130573606afdSDan Williams 				"test_dimm%d", i);
1306231bf117SDan Williams 		if (!t->dimm_dev[i])
1307231bf117SDan Williams 			return -ENOMEM;
1308231bf117SDan Williams 	}
1309231bf117SDan Williams 
1310ed07c433SDan Williams 	smart_init(t);
1311f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
13126bc75619SDan Williams }
13136bc75619SDan Williams 
13146bc75619SDan Williams static int nfit_test1_alloc(struct nfit_test *t)
13156bc75619SDan Williams {
13167bfe97c7SDan Williams 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * 2
1317ac40b675SDan Williams 		+ sizeof(struct acpi_nfit_memory_map) * 2
1318ac40b675SDan Williams 		+ offsetof(struct acpi_nfit_control_region, window_size) * 2;
1319dafb1048SDan Williams 	int i;
13206bc75619SDan Williams 
13216bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
13226bc75619SDan Williams 	if (!t->nfit_buf)
13236bc75619SDan Williams 		return -ENOMEM;
13246bc75619SDan Williams 	t->nfit_size = nfit_size;
13256bc75619SDan Williams 
1326ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA2_SIZE, &t->spa_set_dma[0]);
13276bc75619SDan Williams 	if (!t->spa_set[0])
13286bc75619SDan Williams 		return -ENOMEM;
13296bc75619SDan Williams 
1330dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1331dafb1048SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
1332dafb1048SDan Williams 		if (!t->label[i])
1333dafb1048SDan Williams 			return -ENOMEM;
1334dafb1048SDan Williams 		sprintf(t->label[i], "label%d", i);
1335dafb1048SDan Williams 	}
1336dafb1048SDan Williams 
13377bfe97c7SDan Williams 	t->spa_set[1] = test_alloc(t, SPA_VCD_SIZE, &t->spa_set_dma[1]);
13387bfe97c7SDan Williams 	if (!t->spa_set[1])
13397bfe97c7SDan Williams 		return -ENOMEM;
13407bfe97c7SDan Williams 
1341ed07c433SDan Williams 	smart_init(t);
1342f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
13436bc75619SDan Williams }
13446bc75619SDan Williams 
13455dc68e55SDan Williams static void dcr_common_init(struct acpi_nfit_control_region *dcr)
13465dc68e55SDan Williams {
13475dc68e55SDan Williams 	dcr->vendor_id = 0xabcd;
13485dc68e55SDan Williams 	dcr->device_id = 0;
13495dc68e55SDan Williams 	dcr->revision_id = 1;
13505dc68e55SDan Williams 	dcr->valid_fields = 1;
13515dc68e55SDan Williams 	dcr->manufacturing_location = 0xa;
13525dc68e55SDan Williams 	dcr->manufacturing_date = cpu_to_be16(2016);
13535dc68e55SDan Williams }
13545dc68e55SDan Williams 
13556bc75619SDan Williams static void nfit_test0_setup(struct nfit_test *t)
13566bc75619SDan Williams {
135785d3fa02SDan Williams 	const int flush_hint_size = sizeof(struct acpi_nfit_flush_address)
135885d3fa02SDan Williams 		+ (sizeof(u64) * NUM_HINTS);
13596bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
13606bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
13616bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
13626bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
13636bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
13646bc75619SDan Williams 	struct acpi_nfit_data_region *bdw;
13659d27a87eSDan Williams 	struct acpi_nfit_flush_address *flush;
136685d3fa02SDan Williams 	unsigned int offset, i;
13676bc75619SDan Williams 
13686bc75619SDan Williams 	/*
13696bc75619SDan Williams 	 * spa0 (interleave first half of dimm0 and dimm1, note storage
13706bc75619SDan Williams 	 * does not actually alias the related block-data-window
13716bc75619SDan Williams 	 * regions)
13726bc75619SDan Williams 	 */
13736b577c9dSLinda Knippers 	spa = nfit_buf;
13746bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
13756bc75619SDan Williams 	spa->header.length = sizeof(*spa);
13766bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
13776bc75619SDan Williams 	spa->range_index = 0+1;
13786bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
13796bc75619SDan Williams 	spa->length = SPA0_SIZE;
13806bc75619SDan Williams 
13816bc75619SDan Williams 	/*
13826bc75619SDan Williams 	 * spa1 (interleave last half of the 4 DIMMS, note storage
13836bc75619SDan Williams 	 * does not actually alias the related block-data-window
13846bc75619SDan Williams 	 * regions)
13856bc75619SDan Williams 	 */
13866b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa);
13876bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
13886bc75619SDan Williams 	spa->header.length = sizeof(*spa);
13896bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
13906bc75619SDan Williams 	spa->range_index = 1+1;
13916bc75619SDan Williams 	spa->address = t->spa_set_dma[1];
13926bc75619SDan Williams 	spa->length = SPA1_SIZE;
13936bc75619SDan Williams 
13946bc75619SDan Williams 	/* spa2 (dcr0) dimm0 */
13956b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 2;
13966bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
13976bc75619SDan Williams 	spa->header.length = sizeof(*spa);
13986bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
13996bc75619SDan Williams 	spa->range_index = 2+1;
14006bc75619SDan Williams 	spa->address = t->dcr_dma[0];
14016bc75619SDan Williams 	spa->length = DCR_SIZE;
14026bc75619SDan Williams 
14036bc75619SDan Williams 	/* spa3 (dcr1) dimm1 */
14046b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 3;
14056bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14066bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14076bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
14086bc75619SDan Williams 	spa->range_index = 3+1;
14096bc75619SDan Williams 	spa->address = t->dcr_dma[1];
14106bc75619SDan Williams 	spa->length = DCR_SIZE;
14116bc75619SDan Williams 
14126bc75619SDan Williams 	/* spa4 (dcr2) dimm2 */
14136b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 4;
14146bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14156bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14166bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
14176bc75619SDan Williams 	spa->range_index = 4+1;
14186bc75619SDan Williams 	spa->address = t->dcr_dma[2];
14196bc75619SDan Williams 	spa->length = DCR_SIZE;
14206bc75619SDan Williams 
14216bc75619SDan Williams 	/* spa5 (dcr3) dimm3 */
14226b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 5;
14236bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14246bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14256bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
14266bc75619SDan Williams 	spa->range_index = 5+1;
14276bc75619SDan Williams 	spa->address = t->dcr_dma[3];
14286bc75619SDan Williams 	spa->length = DCR_SIZE;
14296bc75619SDan Williams 
14306bc75619SDan Williams 	/* spa6 (bdw for dcr0) dimm0 */
14316b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 6;
14326bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14336bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14346bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
14356bc75619SDan Williams 	spa->range_index = 6+1;
14366bc75619SDan Williams 	spa->address = t->dimm_dma[0];
14376bc75619SDan Williams 	spa->length = DIMM_SIZE;
14386bc75619SDan Williams 
14396bc75619SDan Williams 	/* spa7 (bdw for dcr1) dimm1 */
14406b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 7;
14416bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14426bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14436bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
14446bc75619SDan Williams 	spa->range_index = 7+1;
14456bc75619SDan Williams 	spa->address = t->dimm_dma[1];
14466bc75619SDan Williams 	spa->length = DIMM_SIZE;
14476bc75619SDan Williams 
14486bc75619SDan Williams 	/* spa8 (bdw for dcr2) dimm2 */
14496b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 8;
14506bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14516bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14526bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
14536bc75619SDan Williams 	spa->range_index = 8+1;
14546bc75619SDan Williams 	spa->address = t->dimm_dma[2];
14556bc75619SDan Williams 	spa->length = DIMM_SIZE;
14566bc75619SDan Williams 
14576bc75619SDan Williams 	/* spa9 (bdw for dcr3) dimm3 */
14586b577c9dSLinda Knippers 	spa = nfit_buf + sizeof(*spa) * 9;
14596bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14606bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14616bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
14626bc75619SDan Williams 	spa->range_index = 9+1;
14636bc75619SDan Williams 	spa->address = t->dimm_dma[3];
14646bc75619SDan Williams 	spa->length = DIMM_SIZE;
14656bc75619SDan Williams 
14666b577c9dSLinda Knippers 	offset = sizeof(*spa) * 10;
14676bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
14686bc75619SDan Williams 	memdev = nfit_buf + offset;
14696bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
14706bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
14716bc75619SDan Williams 	memdev->device_handle = handle[0];
14726bc75619SDan Williams 	memdev->physical_id = 0;
14736bc75619SDan Williams 	memdev->region_id = 0;
14746bc75619SDan Williams 	memdev->range_index = 0+1;
14753b87356fSDan Williams 	memdev->region_index = 4+1;
14766bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1477df06a2d5SDan Williams 	memdev->region_offset = 1;
14786bc75619SDan Williams 	memdev->address = 0;
14796bc75619SDan Williams 	memdev->interleave_index = 0;
14806bc75619SDan Williams 	memdev->interleave_ways = 2;
14816bc75619SDan Williams 
14826bc75619SDan Williams 	/* mem-region1 (spa0, dimm1) */
14836bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map);
14846bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
14856bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
14866bc75619SDan Williams 	memdev->device_handle = handle[1];
14876bc75619SDan Williams 	memdev->physical_id = 1;
14886bc75619SDan Williams 	memdev->region_id = 0;
14896bc75619SDan Williams 	memdev->range_index = 0+1;
14903b87356fSDan Williams 	memdev->region_index = 5+1;
14916bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1492df06a2d5SDan Williams 	memdev->region_offset = (1 << 8);
14936bc75619SDan Williams 	memdev->address = 0;
14946bc75619SDan Williams 	memdev->interleave_index = 0;
14956bc75619SDan Williams 	memdev->interleave_ways = 2;
1496ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
14976bc75619SDan Williams 
14986bc75619SDan Williams 	/* mem-region2 (spa1, dimm0) */
14996bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 2;
15006bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
15016bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
15026bc75619SDan Williams 	memdev->device_handle = handle[0];
15036bc75619SDan Williams 	memdev->physical_id = 0;
15046bc75619SDan Williams 	memdev->region_id = 1;
15056bc75619SDan Williams 	memdev->range_index = 1+1;
15063b87356fSDan Williams 	memdev->region_index = 4+1;
15076bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1508df06a2d5SDan Williams 	memdev->region_offset = (1 << 16);
15096bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
15106bc75619SDan Williams 	memdev->interleave_index = 0;
15116bc75619SDan Williams 	memdev->interleave_ways = 4;
1512ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
15136bc75619SDan Williams 
15146bc75619SDan Williams 	/* mem-region3 (spa1, dimm1) */
15156bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 3;
15166bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
15176bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
15186bc75619SDan Williams 	memdev->device_handle = handle[1];
15196bc75619SDan Williams 	memdev->physical_id = 1;
15206bc75619SDan Williams 	memdev->region_id = 1;
15216bc75619SDan Williams 	memdev->range_index = 1+1;
15223b87356fSDan Williams 	memdev->region_index = 5+1;
15236bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1524df06a2d5SDan Williams 	memdev->region_offset = (1 << 24);
15256bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
15266bc75619SDan Williams 	memdev->interleave_index = 0;
15276bc75619SDan Williams 	memdev->interleave_ways = 4;
15286bc75619SDan Williams 
15296bc75619SDan Williams 	/* mem-region4 (spa1, dimm2) */
15306bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 4;
15316bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
15326bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
15336bc75619SDan Williams 	memdev->device_handle = handle[2];
15346bc75619SDan Williams 	memdev->physical_id = 2;
15356bc75619SDan Williams 	memdev->region_id = 0;
15366bc75619SDan Williams 	memdev->range_index = 1+1;
15373b87356fSDan Williams 	memdev->region_index = 6+1;
15386bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1539df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 32);
15406bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
15416bc75619SDan Williams 	memdev->interleave_index = 0;
15426bc75619SDan Williams 	memdev->interleave_ways = 4;
1543ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
15446bc75619SDan Williams 
15456bc75619SDan Williams 	/* mem-region5 (spa1, dimm3) */
15466bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 5;
15476bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
15486bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
15496bc75619SDan Williams 	memdev->device_handle = handle[3];
15506bc75619SDan Williams 	memdev->physical_id = 3;
15516bc75619SDan Williams 	memdev->region_id = 0;
15526bc75619SDan Williams 	memdev->range_index = 1+1;
15533b87356fSDan Williams 	memdev->region_index = 7+1;
15546bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1555df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 40);
15566bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
15576bc75619SDan Williams 	memdev->interleave_index = 0;
15586bc75619SDan Williams 	memdev->interleave_ways = 4;
15596bc75619SDan Williams 
15606bc75619SDan Williams 	/* mem-region6 (spa/dcr0, dimm0) */
15616bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 6;
15626bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
15636bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
15646bc75619SDan Williams 	memdev->device_handle = handle[0];
15656bc75619SDan Williams 	memdev->physical_id = 0;
15666bc75619SDan Williams 	memdev->region_id = 0;
15676bc75619SDan Williams 	memdev->range_index = 2+1;
15686bc75619SDan Williams 	memdev->region_index = 0+1;
15696bc75619SDan Williams 	memdev->region_size = 0;
15706bc75619SDan Williams 	memdev->region_offset = 0;
15716bc75619SDan Williams 	memdev->address = 0;
15726bc75619SDan Williams 	memdev->interleave_index = 0;
15736bc75619SDan Williams 	memdev->interleave_ways = 1;
15746bc75619SDan Williams 
15756bc75619SDan Williams 	/* mem-region7 (spa/dcr1, dimm1) */
15766bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 7;
15776bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
15786bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
15796bc75619SDan Williams 	memdev->device_handle = handle[1];
15806bc75619SDan Williams 	memdev->physical_id = 1;
15816bc75619SDan Williams 	memdev->region_id = 0;
15826bc75619SDan Williams 	memdev->range_index = 3+1;
15836bc75619SDan Williams 	memdev->region_index = 1+1;
15846bc75619SDan Williams 	memdev->region_size = 0;
15856bc75619SDan Williams 	memdev->region_offset = 0;
15866bc75619SDan Williams 	memdev->address = 0;
15876bc75619SDan Williams 	memdev->interleave_index = 0;
15886bc75619SDan Williams 	memdev->interleave_ways = 1;
15896bc75619SDan Williams 
15906bc75619SDan Williams 	/* mem-region8 (spa/dcr2, dimm2) */
15916bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 8;
15926bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
15936bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
15946bc75619SDan Williams 	memdev->device_handle = handle[2];
15956bc75619SDan Williams 	memdev->physical_id = 2;
15966bc75619SDan Williams 	memdev->region_id = 0;
15976bc75619SDan Williams 	memdev->range_index = 4+1;
15986bc75619SDan Williams 	memdev->region_index = 2+1;
15996bc75619SDan Williams 	memdev->region_size = 0;
16006bc75619SDan Williams 	memdev->region_offset = 0;
16016bc75619SDan Williams 	memdev->address = 0;
16026bc75619SDan Williams 	memdev->interleave_index = 0;
16036bc75619SDan Williams 	memdev->interleave_ways = 1;
16046bc75619SDan Williams 
16056bc75619SDan Williams 	/* mem-region9 (spa/dcr3, dimm3) */
16066bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 9;
16076bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16086bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16096bc75619SDan Williams 	memdev->device_handle = handle[3];
16106bc75619SDan Williams 	memdev->physical_id = 3;
16116bc75619SDan Williams 	memdev->region_id = 0;
16126bc75619SDan Williams 	memdev->range_index = 5+1;
16136bc75619SDan Williams 	memdev->region_index = 3+1;
16146bc75619SDan Williams 	memdev->region_size = 0;
16156bc75619SDan Williams 	memdev->region_offset = 0;
16166bc75619SDan Williams 	memdev->address = 0;
16176bc75619SDan Williams 	memdev->interleave_index = 0;
16186bc75619SDan Williams 	memdev->interleave_ways = 1;
16196bc75619SDan Williams 
16206bc75619SDan Williams 	/* mem-region10 (spa/bdw0, dimm0) */
16216bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 10;
16226bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16236bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16246bc75619SDan Williams 	memdev->device_handle = handle[0];
16256bc75619SDan Williams 	memdev->physical_id = 0;
16266bc75619SDan Williams 	memdev->region_id = 0;
16276bc75619SDan Williams 	memdev->range_index = 6+1;
16286bc75619SDan Williams 	memdev->region_index = 0+1;
16296bc75619SDan Williams 	memdev->region_size = 0;
16306bc75619SDan Williams 	memdev->region_offset = 0;
16316bc75619SDan Williams 	memdev->address = 0;
16326bc75619SDan Williams 	memdev->interleave_index = 0;
16336bc75619SDan Williams 	memdev->interleave_ways = 1;
16346bc75619SDan Williams 
16356bc75619SDan Williams 	/* mem-region11 (spa/bdw1, dimm1) */
16366bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 11;
16376bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16386bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16396bc75619SDan Williams 	memdev->device_handle = handle[1];
16406bc75619SDan Williams 	memdev->physical_id = 1;
16416bc75619SDan Williams 	memdev->region_id = 0;
16426bc75619SDan Williams 	memdev->range_index = 7+1;
16436bc75619SDan Williams 	memdev->region_index = 1+1;
16446bc75619SDan Williams 	memdev->region_size = 0;
16456bc75619SDan Williams 	memdev->region_offset = 0;
16466bc75619SDan Williams 	memdev->address = 0;
16476bc75619SDan Williams 	memdev->interleave_index = 0;
16486bc75619SDan Williams 	memdev->interleave_ways = 1;
16496bc75619SDan Williams 
16506bc75619SDan Williams 	/* mem-region12 (spa/bdw2, dimm2) */
16516bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 12;
16526bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16536bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16546bc75619SDan Williams 	memdev->device_handle = handle[2];
16556bc75619SDan Williams 	memdev->physical_id = 2;
16566bc75619SDan Williams 	memdev->region_id = 0;
16576bc75619SDan Williams 	memdev->range_index = 8+1;
16586bc75619SDan Williams 	memdev->region_index = 2+1;
16596bc75619SDan Williams 	memdev->region_size = 0;
16606bc75619SDan Williams 	memdev->region_offset = 0;
16616bc75619SDan Williams 	memdev->address = 0;
16626bc75619SDan Williams 	memdev->interleave_index = 0;
16636bc75619SDan Williams 	memdev->interleave_ways = 1;
16646bc75619SDan Williams 
16656bc75619SDan Williams 	/* mem-region13 (spa/dcr3, dimm3) */
16666bc75619SDan Williams 	memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 13;
16676bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16686bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16696bc75619SDan Williams 	memdev->device_handle = handle[3];
16706bc75619SDan Williams 	memdev->physical_id = 3;
16716bc75619SDan Williams 	memdev->region_id = 0;
16726bc75619SDan Williams 	memdev->range_index = 9+1;
16736bc75619SDan Williams 	memdev->region_index = 3+1;
16746bc75619SDan Williams 	memdev->region_size = 0;
16756bc75619SDan Williams 	memdev->region_offset = 0;
16766bc75619SDan Williams 	memdev->address = 0;
16776bc75619SDan Williams 	memdev->interleave_index = 0;
16786bc75619SDan Williams 	memdev->interleave_ways = 1;
1679ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
16806bc75619SDan Williams 
16816bc75619SDan Williams 	offset = offset + sizeof(struct acpi_nfit_memory_map) * 14;
16823b87356fSDan Williams 	/* dcr-descriptor0: blk */
16836bc75619SDan Williams 	dcr = nfit_buf + offset;
16846bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
16856bc75619SDan Williams 	dcr->header.length = sizeof(struct acpi_nfit_control_region);
16866bc75619SDan Williams 	dcr->region_index = 0+1;
16875dc68e55SDan Williams 	dcr_common_init(dcr);
16886bc75619SDan Williams 	dcr->serial_number = ~handle[0];
1689be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
16906bc75619SDan Williams 	dcr->windows = 1;
16916bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
16926bc75619SDan Williams 	dcr->command_offset = 0;
16936bc75619SDan Williams 	dcr->command_size = 8;
16946bc75619SDan Williams 	dcr->status_offset = 8;
16956bc75619SDan Williams 	dcr->status_size = 4;
16966bc75619SDan Williams 
16973b87356fSDan Williams 	/* dcr-descriptor1: blk */
16986bc75619SDan Williams 	dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region);
16996bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
17006bc75619SDan Williams 	dcr->header.length = sizeof(struct acpi_nfit_control_region);
17016bc75619SDan Williams 	dcr->region_index = 1+1;
17025dc68e55SDan Williams 	dcr_common_init(dcr);
17036bc75619SDan Williams 	dcr->serial_number = ~handle[1];
1704be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
17056bc75619SDan Williams 	dcr->windows = 1;
17066bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
17076bc75619SDan Williams 	dcr->command_offset = 0;
17086bc75619SDan Williams 	dcr->command_size = 8;
17096bc75619SDan Williams 	dcr->status_offset = 8;
17106bc75619SDan Williams 	dcr->status_size = 4;
17116bc75619SDan Williams 
17123b87356fSDan Williams 	/* dcr-descriptor2: blk */
17136bc75619SDan Williams 	dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region) * 2;
17146bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
17156bc75619SDan Williams 	dcr->header.length = sizeof(struct acpi_nfit_control_region);
17166bc75619SDan Williams 	dcr->region_index = 2+1;
17175dc68e55SDan Williams 	dcr_common_init(dcr);
17186bc75619SDan Williams 	dcr->serial_number = ~handle[2];
1719be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
17206bc75619SDan Williams 	dcr->windows = 1;
17216bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
17226bc75619SDan Williams 	dcr->command_offset = 0;
17236bc75619SDan Williams 	dcr->command_size = 8;
17246bc75619SDan Williams 	dcr->status_offset = 8;
17256bc75619SDan Williams 	dcr->status_size = 4;
17266bc75619SDan Williams 
17273b87356fSDan Williams 	/* dcr-descriptor3: blk */
17286bc75619SDan Williams 	dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region) * 3;
17296bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
17306bc75619SDan Williams 	dcr->header.length = sizeof(struct acpi_nfit_control_region);
17316bc75619SDan Williams 	dcr->region_index = 3+1;
17325dc68e55SDan Williams 	dcr_common_init(dcr);
17336bc75619SDan Williams 	dcr->serial_number = ~handle[3];
1734be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
17356bc75619SDan Williams 	dcr->windows = 1;
17366bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
17376bc75619SDan Williams 	dcr->command_offset = 0;
17386bc75619SDan Williams 	dcr->command_size = 8;
17396bc75619SDan Williams 	dcr->status_offset = 8;
17406bc75619SDan Williams 	dcr->status_size = 4;
17416bc75619SDan Williams 
17426bc75619SDan Williams 	offset = offset + sizeof(struct acpi_nfit_control_region) * 4;
17433b87356fSDan Williams 	/* dcr-descriptor0: pmem */
17443b87356fSDan Williams 	dcr = nfit_buf + offset;
17453b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
17463b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
17473b87356fSDan Williams 			window_size);
17483b87356fSDan Williams 	dcr->region_index = 4+1;
17495dc68e55SDan Williams 	dcr_common_init(dcr);
17503b87356fSDan Williams 	dcr->serial_number = ~handle[0];
17513b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
17523b87356fSDan Williams 	dcr->windows = 0;
17533b87356fSDan Williams 
17543b87356fSDan Williams 	/* dcr-descriptor1: pmem */
17553b87356fSDan Williams 	dcr = nfit_buf + offset + offsetof(struct acpi_nfit_control_region,
17563b87356fSDan Williams 			window_size);
17573b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
17583b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
17593b87356fSDan Williams 			window_size);
17603b87356fSDan Williams 	dcr->region_index = 5+1;
17615dc68e55SDan Williams 	dcr_common_init(dcr);
17623b87356fSDan Williams 	dcr->serial_number = ~handle[1];
17633b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
17643b87356fSDan Williams 	dcr->windows = 0;
17653b87356fSDan Williams 
17663b87356fSDan Williams 	/* dcr-descriptor2: pmem */
17673b87356fSDan Williams 	dcr = nfit_buf + offset + offsetof(struct acpi_nfit_control_region,
17683b87356fSDan Williams 			window_size) * 2;
17693b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
17703b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
17713b87356fSDan Williams 			window_size);
17723b87356fSDan Williams 	dcr->region_index = 6+1;
17735dc68e55SDan Williams 	dcr_common_init(dcr);
17743b87356fSDan Williams 	dcr->serial_number = ~handle[2];
17753b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
17763b87356fSDan Williams 	dcr->windows = 0;
17773b87356fSDan Williams 
17783b87356fSDan Williams 	/* dcr-descriptor3: pmem */
17793b87356fSDan Williams 	dcr = nfit_buf + offset + offsetof(struct acpi_nfit_control_region,
17803b87356fSDan Williams 			window_size) * 3;
17813b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
17823b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
17833b87356fSDan Williams 			window_size);
17843b87356fSDan Williams 	dcr->region_index = 7+1;
17855dc68e55SDan Williams 	dcr_common_init(dcr);
17863b87356fSDan Williams 	dcr->serial_number = ~handle[3];
17873b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
17883b87356fSDan Williams 	dcr->windows = 0;
17893b87356fSDan Williams 
17903b87356fSDan Williams 	offset = offset + offsetof(struct acpi_nfit_control_region,
17913b87356fSDan Williams 			window_size) * 4;
17926bc75619SDan Williams 	/* bdw0 (spa/dcr0, dimm0) */
17936bc75619SDan Williams 	bdw = nfit_buf + offset;
17946bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
17956bc75619SDan Williams 	bdw->header.length = sizeof(struct acpi_nfit_data_region);
17966bc75619SDan Williams 	bdw->region_index = 0+1;
17976bc75619SDan Williams 	bdw->windows = 1;
17986bc75619SDan Williams 	bdw->offset = 0;
17996bc75619SDan Williams 	bdw->size = BDW_SIZE;
18006bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
18016bc75619SDan Williams 	bdw->start_address = 0;
18026bc75619SDan Williams 
18036bc75619SDan Williams 	/* bdw1 (spa/dcr1, dimm1) */
18046bc75619SDan Williams 	bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region);
18056bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
18066bc75619SDan Williams 	bdw->header.length = sizeof(struct acpi_nfit_data_region);
18076bc75619SDan Williams 	bdw->region_index = 1+1;
18086bc75619SDan Williams 	bdw->windows = 1;
18096bc75619SDan Williams 	bdw->offset = 0;
18106bc75619SDan Williams 	bdw->size = BDW_SIZE;
18116bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
18126bc75619SDan Williams 	bdw->start_address = 0;
18136bc75619SDan Williams 
18146bc75619SDan Williams 	/* bdw2 (spa/dcr2, dimm2) */
18156bc75619SDan Williams 	bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region) * 2;
18166bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
18176bc75619SDan Williams 	bdw->header.length = sizeof(struct acpi_nfit_data_region);
18186bc75619SDan Williams 	bdw->region_index = 2+1;
18196bc75619SDan Williams 	bdw->windows = 1;
18206bc75619SDan Williams 	bdw->offset = 0;
18216bc75619SDan Williams 	bdw->size = BDW_SIZE;
18226bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
18236bc75619SDan Williams 	bdw->start_address = 0;
18246bc75619SDan Williams 
18256bc75619SDan Williams 	/* bdw3 (spa/dcr3, dimm3) */
18266bc75619SDan Williams 	bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region) * 3;
18276bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
18286bc75619SDan Williams 	bdw->header.length = sizeof(struct acpi_nfit_data_region);
18296bc75619SDan Williams 	bdw->region_index = 3+1;
18306bc75619SDan Williams 	bdw->windows = 1;
18316bc75619SDan Williams 	bdw->offset = 0;
18326bc75619SDan Williams 	bdw->size = BDW_SIZE;
18336bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
18346bc75619SDan Williams 	bdw->start_address = 0;
18356bc75619SDan Williams 
18369d27a87eSDan Williams 	offset = offset + sizeof(struct acpi_nfit_data_region) * 4;
18379d27a87eSDan Williams 	/* flush0 (dimm0) */
18389d27a87eSDan Williams 	flush = nfit_buf + offset;
18399d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
184085d3fa02SDan Williams 	flush->header.length = flush_hint_size;
18419d27a87eSDan Williams 	flush->device_handle = handle[0];
184285d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
184385d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
184485d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[0] + i * sizeof(u64);
18459d27a87eSDan Williams 
18469d27a87eSDan Williams 	/* flush1 (dimm1) */
184785d3fa02SDan Williams 	flush = nfit_buf + offset + flush_hint_size * 1;
18489d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
184985d3fa02SDan Williams 	flush->header.length = flush_hint_size;
18509d27a87eSDan Williams 	flush->device_handle = handle[1];
185185d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
185285d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
185385d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[1] + i * sizeof(u64);
18549d27a87eSDan Williams 
18559d27a87eSDan Williams 	/* flush2 (dimm2) */
185685d3fa02SDan Williams 	flush = nfit_buf + offset + flush_hint_size  * 2;
18579d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
185885d3fa02SDan Williams 	flush->header.length = flush_hint_size;
18599d27a87eSDan Williams 	flush->device_handle = handle[2];
186085d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
186185d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
186285d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[2] + i * sizeof(u64);
18639d27a87eSDan Williams 
18649d27a87eSDan Williams 	/* flush3 (dimm3) */
186585d3fa02SDan Williams 	flush = nfit_buf + offset + flush_hint_size * 3;
18669d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
186785d3fa02SDan Williams 	flush->header.length = flush_hint_size;
18689d27a87eSDan Williams 	flush->device_handle = handle[3];
186985d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
187085d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
187185d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[3] + i * sizeof(u64);
18729d27a87eSDan Williams 
187320985164SVishal Verma 	if (t->setup_hotplug) {
187485d3fa02SDan Williams 		offset = offset + flush_hint_size * 4;
18753b87356fSDan Williams 		/* dcr-descriptor4: blk */
187620985164SVishal Verma 		dcr = nfit_buf + offset;
187720985164SVishal Verma 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
187820985164SVishal Verma 		dcr->header.length = sizeof(struct acpi_nfit_control_region);
18793b87356fSDan Williams 		dcr->region_index = 8+1;
18805dc68e55SDan Williams 		dcr_common_init(dcr);
188120985164SVishal Verma 		dcr->serial_number = ~handle[4];
1882be26f9aeSDan Williams 		dcr->code = NFIT_FIC_BLK;
188320985164SVishal Verma 		dcr->windows = 1;
188420985164SVishal Verma 		dcr->window_size = DCR_SIZE;
188520985164SVishal Verma 		dcr->command_offset = 0;
188620985164SVishal Verma 		dcr->command_size = 8;
188720985164SVishal Verma 		dcr->status_offset = 8;
188820985164SVishal Verma 		dcr->status_size = 4;
188920985164SVishal Verma 
189020985164SVishal Verma 		offset = offset + sizeof(struct acpi_nfit_control_region);
18913b87356fSDan Williams 		/* dcr-descriptor4: pmem */
18923b87356fSDan Williams 		dcr = nfit_buf + offset;
18933b87356fSDan Williams 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
18943b87356fSDan Williams 		dcr->header.length = offsetof(struct acpi_nfit_control_region,
18953b87356fSDan Williams 				window_size);
18963b87356fSDan Williams 		dcr->region_index = 9+1;
18975dc68e55SDan Williams 		dcr_common_init(dcr);
18983b87356fSDan Williams 		dcr->serial_number = ~handle[4];
18993b87356fSDan Williams 		dcr->code = NFIT_FIC_BYTEN;
19003b87356fSDan Williams 		dcr->windows = 0;
19013b87356fSDan Williams 
19023b87356fSDan Williams 		offset = offset + offsetof(struct acpi_nfit_control_region,
19033b87356fSDan Williams 				window_size);
190420985164SVishal Verma 		/* bdw4 (spa/dcr4, dimm4) */
190520985164SVishal Verma 		bdw = nfit_buf + offset;
190620985164SVishal Verma 		bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
190720985164SVishal Verma 		bdw->header.length = sizeof(struct acpi_nfit_data_region);
19083b87356fSDan Williams 		bdw->region_index = 8+1;
190920985164SVishal Verma 		bdw->windows = 1;
191020985164SVishal Verma 		bdw->offset = 0;
191120985164SVishal Verma 		bdw->size = BDW_SIZE;
191220985164SVishal Verma 		bdw->capacity = DIMM_SIZE;
191320985164SVishal Verma 		bdw->start_address = 0;
191420985164SVishal Verma 
191520985164SVishal Verma 		offset = offset + sizeof(struct acpi_nfit_data_region);
191620985164SVishal Verma 		/* spa10 (dcr4) dimm4 */
191720985164SVishal Verma 		spa = nfit_buf + offset;
191820985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
191920985164SVishal Verma 		spa->header.length = sizeof(*spa);
192020985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
192120985164SVishal Verma 		spa->range_index = 10+1;
192220985164SVishal Verma 		spa->address = t->dcr_dma[4];
192320985164SVishal Verma 		spa->length = DCR_SIZE;
192420985164SVishal Verma 
192520985164SVishal Verma 		/*
192620985164SVishal Verma 		 * spa11 (single-dimm interleave for hotplug, note storage
192720985164SVishal Verma 		 * does not actually alias the related block-data-window
192820985164SVishal Verma 		 * regions)
192920985164SVishal Verma 		 */
193020985164SVishal Verma 		spa = nfit_buf + offset + sizeof(*spa);
193120985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
193220985164SVishal Verma 		spa->header.length = sizeof(*spa);
193320985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
193420985164SVishal Verma 		spa->range_index = 11+1;
193520985164SVishal Verma 		spa->address = t->spa_set_dma[2];
193620985164SVishal Verma 		spa->length = SPA0_SIZE;
193720985164SVishal Verma 
193820985164SVishal Verma 		/* spa12 (bdw for dcr4) dimm4 */
193920985164SVishal Verma 		spa = nfit_buf + offset + sizeof(*spa) * 2;
194020985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
194120985164SVishal Verma 		spa->header.length = sizeof(*spa);
194220985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
194320985164SVishal Verma 		spa->range_index = 12+1;
194420985164SVishal Verma 		spa->address = t->dimm_dma[4];
194520985164SVishal Verma 		spa->length = DIMM_SIZE;
194620985164SVishal Verma 
194720985164SVishal Verma 		offset = offset + sizeof(*spa) * 3;
194820985164SVishal Verma 		/* mem-region14 (spa/dcr4, dimm4) */
194920985164SVishal Verma 		memdev = nfit_buf + offset;
195020985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
195120985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
195220985164SVishal Verma 		memdev->device_handle = handle[4];
195320985164SVishal Verma 		memdev->physical_id = 4;
195420985164SVishal Verma 		memdev->region_id = 0;
195520985164SVishal Verma 		memdev->range_index = 10+1;
19563b87356fSDan Williams 		memdev->region_index = 8+1;
195720985164SVishal Verma 		memdev->region_size = 0;
195820985164SVishal Verma 		memdev->region_offset = 0;
195920985164SVishal Verma 		memdev->address = 0;
196020985164SVishal Verma 		memdev->interleave_index = 0;
196120985164SVishal Verma 		memdev->interleave_ways = 1;
196220985164SVishal Verma 
196320985164SVishal Verma 		/* mem-region15 (spa0, dimm4) */
196420985164SVishal Verma 		memdev = nfit_buf + offset +
196520985164SVishal Verma 				sizeof(struct acpi_nfit_memory_map);
196620985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
196720985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
196820985164SVishal Verma 		memdev->device_handle = handle[4];
196920985164SVishal Verma 		memdev->physical_id = 4;
197020985164SVishal Verma 		memdev->region_id = 0;
197120985164SVishal Verma 		memdev->range_index = 11+1;
19723b87356fSDan Williams 		memdev->region_index = 9+1;
197320985164SVishal Verma 		memdev->region_size = SPA0_SIZE;
1974df06a2d5SDan Williams 		memdev->region_offset = (1ULL << 48);
197520985164SVishal Verma 		memdev->address = 0;
197620985164SVishal Verma 		memdev->interleave_index = 0;
197720985164SVishal Verma 		memdev->interleave_ways = 1;
1978ac40b675SDan Williams 		memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
197920985164SVishal Verma 
19803b87356fSDan Williams 		/* mem-region16 (spa/bdw4, dimm4) */
198120985164SVishal Verma 		memdev = nfit_buf + offset +
198220985164SVishal Verma 				sizeof(struct acpi_nfit_memory_map) * 2;
198320985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
198420985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
198520985164SVishal Verma 		memdev->device_handle = handle[4];
198620985164SVishal Verma 		memdev->physical_id = 4;
198720985164SVishal Verma 		memdev->region_id = 0;
198820985164SVishal Verma 		memdev->range_index = 12+1;
19893b87356fSDan Williams 		memdev->region_index = 8+1;
199020985164SVishal Verma 		memdev->region_size = 0;
199120985164SVishal Verma 		memdev->region_offset = 0;
199220985164SVishal Verma 		memdev->address = 0;
199320985164SVishal Verma 		memdev->interleave_index = 0;
199420985164SVishal Verma 		memdev->interleave_ways = 1;
199520985164SVishal Verma 
199620985164SVishal Verma 		offset = offset + sizeof(struct acpi_nfit_memory_map) * 3;
199720985164SVishal Verma 		/* flush3 (dimm4) */
199820985164SVishal Verma 		flush = nfit_buf + offset;
199920985164SVishal Verma 		flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
200085d3fa02SDan Williams 		flush->header.length = flush_hint_size;
200120985164SVishal Verma 		flush->device_handle = handle[4];
200285d3fa02SDan Williams 		flush->hint_count = NUM_HINTS;
200385d3fa02SDan Williams 		for (i = 0; i < NUM_HINTS; i++)
200485d3fa02SDan Williams 			flush->hint_address[i] = t->flush_dma[4]
200585d3fa02SDan Williams 				+ i * sizeof(u64);
200620985164SVishal Verma 	}
200720985164SVishal Verma 
20089fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
20099fb1a190SDave Jiang 			SPA0_SIZE);
2010f471f1a7SDan Williams 
20116bc75619SDan Williams 	acpi_desc = &t->acpi_desc;
2012e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
2013e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2014e3654ecaSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2015ed07c433SDan Williams 	set_bit(ND_INTEL_SMART, &acpi_desc->dimm_cmd_force_en);
2016ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
2017ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_SET_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
2018e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2019e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2020e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2021e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
202210246dc8SYasunori Goto 	set_bit(ND_CMD_CALL, &acpi_desc->bus_cmd_force_en);
202310246dc8SYasunori Goto 	set_bit(NFIT_CMD_TRANSLATE_SPA, &acpi_desc->bus_nfit_cmd_force_en);
20249fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_SET, &acpi_desc->bus_nfit_cmd_force_en);
20259fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_CLEAR, &acpi_desc->bus_nfit_cmd_force_en);
20269fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_GET, &acpi_desc->bus_nfit_cmd_force_en);
2027bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_GET_INFO, &acpi_desc->dimm_cmd_force_en);
2028bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_START_UPDATE, &acpi_desc->dimm_cmd_force_en);
2029bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_SEND_DATA, &acpi_desc->dimm_cmd_force_en);
2030bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_UPDATE, &acpi_desc->dimm_cmd_force_en);
2031bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_QUERY, &acpi_desc->dimm_cmd_force_en);
2032*674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
20336bc75619SDan Williams }
20346bc75619SDan Williams 
20356bc75619SDan Williams static void nfit_test1_setup(struct nfit_test *t)
20366bc75619SDan Williams {
20376b577c9dSLinda Knippers 	size_t offset;
20386bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
20396bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
20406bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
20416bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
2042d26f73f0SDan Williams 	struct acpi_nfit_desc *acpi_desc;
20436bc75619SDan Williams 
20446b577c9dSLinda Knippers 	offset = 0;
20456bc75619SDan Williams 	/* spa0 (flat range with no bdw aliasing) */
20466bc75619SDan Williams 	spa = nfit_buf + offset;
20476bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
20486bc75619SDan Williams 	spa->header.length = sizeof(*spa);
20496bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
20506bc75619SDan Williams 	spa->range_index = 0+1;
20516bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
20526bc75619SDan Williams 	spa->length = SPA2_SIZE;
20536bc75619SDan Williams 
20547bfe97c7SDan Williams 	/* virtual cd region */
20557bfe97c7SDan Williams 	spa = nfit_buf + sizeof(*spa);
20567bfe97c7SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
20577bfe97c7SDan Williams 	spa->header.length = sizeof(*spa);
20587bfe97c7SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_VCD), 16);
20597bfe97c7SDan Williams 	spa->range_index = 0;
20607bfe97c7SDan Williams 	spa->address = t->spa_set_dma[1];
20617bfe97c7SDan Williams 	spa->length = SPA_VCD_SIZE;
20627bfe97c7SDan Williams 
20637bfe97c7SDan Williams 	offset += sizeof(*spa) * 2;
20646bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
20656bc75619SDan Williams 	memdev = nfit_buf + offset;
20666bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
20676bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
2068dafb1048SDan Williams 	memdev->device_handle = handle[5];
20696bc75619SDan Williams 	memdev->physical_id = 0;
20706bc75619SDan Williams 	memdev->region_id = 0;
20716bc75619SDan Williams 	memdev->range_index = 0+1;
20726bc75619SDan Williams 	memdev->region_index = 0+1;
20736bc75619SDan Williams 	memdev->region_size = SPA2_SIZE;
20746bc75619SDan Williams 	memdev->region_offset = 0;
20756bc75619SDan Williams 	memdev->address = 0;
20766bc75619SDan Williams 	memdev->interleave_index = 0;
20776bc75619SDan Williams 	memdev->interleave_ways = 1;
207858138820SDan Williams 	memdev->flags = ACPI_NFIT_MEM_SAVE_FAILED | ACPI_NFIT_MEM_RESTORE_FAILED
207958138820SDan Williams 		| ACPI_NFIT_MEM_FLUSH_FAILED | ACPI_NFIT_MEM_HEALTH_OBSERVED
2080f4295796SDan Williams 		| ACPI_NFIT_MEM_NOT_ARMED;
20816bc75619SDan Williams 
20826bc75619SDan Williams 	offset += sizeof(*memdev);
20836bc75619SDan Williams 	/* dcr-descriptor0 */
20846bc75619SDan Williams 	dcr = nfit_buf + offset;
20856bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
20863b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
20873b87356fSDan Williams 			window_size);
20886bc75619SDan Williams 	dcr->region_index = 0+1;
20895dc68e55SDan Williams 	dcr_common_init(dcr);
2090dafb1048SDan Williams 	dcr->serial_number = ~handle[5];
2091be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BYTE;
20926bc75619SDan Williams 	dcr->windows = 0;
2093d26f73f0SDan Williams 
2094ac40b675SDan Williams 	offset += dcr->header.length;
2095ac40b675SDan Williams 	memdev = nfit_buf + offset;
2096ac40b675SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
2097ac40b675SDan Williams 	memdev->header.length = sizeof(*memdev);
2098ac40b675SDan Williams 	memdev->device_handle = handle[6];
2099ac40b675SDan Williams 	memdev->physical_id = 0;
2100ac40b675SDan Williams 	memdev->region_id = 0;
2101ac40b675SDan Williams 	memdev->range_index = 0;
2102ac40b675SDan Williams 	memdev->region_index = 0+2;
2103ac40b675SDan Williams 	memdev->region_size = SPA2_SIZE;
2104ac40b675SDan Williams 	memdev->region_offset = 0;
2105ac40b675SDan Williams 	memdev->address = 0;
2106ac40b675SDan Williams 	memdev->interleave_index = 0;
2107ac40b675SDan Williams 	memdev->interleave_ways = 1;
2108ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_MAP_FAILED;
2109ac40b675SDan Williams 
2110ac40b675SDan Williams 	/* dcr-descriptor1 */
2111ac40b675SDan Williams 	offset += sizeof(*memdev);
2112ac40b675SDan Williams 	dcr = nfit_buf + offset;
2113ac40b675SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2114ac40b675SDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
2115ac40b675SDan Williams 			window_size);
2116ac40b675SDan Williams 	dcr->region_index = 0+2;
2117ac40b675SDan Williams 	dcr_common_init(dcr);
2118ac40b675SDan Williams 	dcr->serial_number = ~handle[6];
2119ac40b675SDan Williams 	dcr->code = NFIT_FIC_BYTE;
2120ac40b675SDan Williams 	dcr->windows = 0;
2121ac40b675SDan Williams 
21229fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
21239fb1a190SDave Jiang 			SPA2_SIZE);
2124f471f1a7SDan Williams 
2125d26f73f0SDan Williams 	acpi_desc = &t->acpi_desc;
2126e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2127e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2128e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2129e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
2130*674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
21316bc75619SDan Williams }
21326bc75619SDan Williams 
21336bc75619SDan Williams static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
21346bc75619SDan Williams 		void *iobuf, u64 len, int rw)
21356bc75619SDan Williams {
21366bc75619SDan Williams 	struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
21376bc75619SDan Williams 	struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
21386bc75619SDan Williams 	struct nd_region *nd_region = &ndbr->nd_region;
21396bc75619SDan Williams 	unsigned int lane;
21406bc75619SDan Williams 
21416bc75619SDan Williams 	lane = nd_region_acquire_lane(nd_region);
21426bc75619SDan Williams 	if (rw)
214367a3e8feSRoss Zwisler 		memcpy(mmio->addr.base + dpa, iobuf, len);
214467a3e8feSRoss Zwisler 	else {
214567a3e8feSRoss Zwisler 		memcpy(iobuf, mmio->addr.base + dpa, len);
214667a3e8feSRoss Zwisler 
21475deb67f7SRobin Murphy 		/* give us some some coverage of the arch_invalidate_pmem() API */
21485deb67f7SRobin Murphy 		arch_invalidate_pmem(mmio->addr.base + dpa, len);
214967a3e8feSRoss Zwisler 	}
21506bc75619SDan Williams 	nd_region_release_lane(nd_region, lane);
21516bc75619SDan Williams 
21526bc75619SDan Williams 	return 0;
21536bc75619SDan Williams }
21546bc75619SDan Williams 
2155a7de92daSDan Williams static unsigned long nfit_ctl_handle;
2156a7de92daSDan Williams 
2157a7de92daSDan Williams union acpi_object *result;
2158a7de92daSDan Williams 
2159a7de92daSDan Williams static union acpi_object *nfit_test_evaluate_dsm(acpi_handle handle,
216094116f81SAndy Shevchenko 		const guid_t *guid, u64 rev, u64 func, union acpi_object *argv4)
2161a7de92daSDan Williams {
2162a7de92daSDan Williams 	if (handle != &nfit_ctl_handle)
2163a7de92daSDan Williams 		return ERR_PTR(-ENXIO);
2164a7de92daSDan Williams 
2165a7de92daSDan Williams 	return result;
2166a7de92daSDan Williams }
2167a7de92daSDan Williams 
2168a7de92daSDan Williams static int setup_result(void *buf, size_t size)
2169a7de92daSDan Williams {
2170a7de92daSDan Williams 	result = kmalloc(sizeof(union acpi_object) + size, GFP_KERNEL);
2171a7de92daSDan Williams 	if (!result)
2172a7de92daSDan Williams 		return -ENOMEM;
2173a7de92daSDan Williams 	result->package.type = ACPI_TYPE_BUFFER,
2174a7de92daSDan Williams 	result->buffer.pointer = (void *) (result + 1);
2175a7de92daSDan Williams 	result->buffer.length = size;
2176a7de92daSDan Williams 	memcpy(result->buffer.pointer, buf, size);
2177a7de92daSDan Williams 	memset(buf, 0, size);
2178a7de92daSDan Williams 	return 0;
2179a7de92daSDan Williams }
2180a7de92daSDan Williams 
2181a7de92daSDan Williams static int nfit_ctl_test(struct device *dev)
2182a7de92daSDan Williams {
2183a7de92daSDan Williams 	int rc, cmd_rc;
2184a7de92daSDan Williams 	struct nvdimm *nvdimm;
2185a7de92daSDan Williams 	struct acpi_device *adev;
2186a7de92daSDan Williams 	struct nfit_mem *nfit_mem;
2187a7de92daSDan Williams 	struct nd_ars_record *record;
2188a7de92daSDan Williams 	struct acpi_nfit_desc *acpi_desc;
2189a7de92daSDan Williams 	const u64 test_val = 0x0123456789abcdefULL;
2190a7de92daSDan Williams 	unsigned long mask, cmd_size, offset;
2191a7de92daSDan Williams 	union {
2192a7de92daSDan Williams 		struct nd_cmd_get_config_size cfg_size;
2193fb2a1748SDan Williams 		struct nd_cmd_clear_error clear_err;
2194a7de92daSDan Williams 		struct nd_cmd_ars_status ars_stat;
2195a7de92daSDan Williams 		struct nd_cmd_ars_cap ars_cap;
2196a7de92daSDan Williams 		char buf[sizeof(struct nd_cmd_ars_status)
2197a7de92daSDan Williams 			+ sizeof(struct nd_ars_record)];
2198a7de92daSDan Williams 	} cmds;
2199a7de92daSDan Williams 
2200a7de92daSDan Williams 	adev = devm_kzalloc(dev, sizeof(*adev), GFP_KERNEL);
2201a7de92daSDan Williams 	if (!adev)
2202a7de92daSDan Williams 		return -ENOMEM;
2203a7de92daSDan Williams 	*adev = (struct acpi_device) {
2204a7de92daSDan Williams 		.handle = &nfit_ctl_handle,
2205a7de92daSDan Williams 		.dev = {
2206a7de92daSDan Williams 			.init_name = "test-adev",
2207a7de92daSDan Williams 		},
2208a7de92daSDan Williams 	};
2209a7de92daSDan Williams 
2210a7de92daSDan Williams 	acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2211a7de92daSDan Williams 	if (!acpi_desc)
2212a7de92daSDan Williams 		return -ENOMEM;
2213a7de92daSDan Williams 	*acpi_desc = (struct acpi_nfit_desc) {
2214a7de92daSDan Williams 		.nd_desc = {
2215a7de92daSDan Williams 			.cmd_mask = 1UL << ND_CMD_ARS_CAP
2216a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_START
2217a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_STATUS
221810246dc8SYasunori Goto 				| 1UL << ND_CMD_CLEAR_ERROR
221910246dc8SYasunori Goto 				| 1UL << ND_CMD_CALL,
2220a7de92daSDan Williams 			.module = THIS_MODULE,
2221a7de92daSDan Williams 			.provider_name = "ACPI.NFIT",
2222a7de92daSDan Williams 			.ndctl = acpi_nfit_ctl,
22239fb1a190SDave Jiang 			.bus_dsm_mask = 1UL << NFIT_CMD_TRANSLATE_SPA
22249fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_SET
22259fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_CLEAR
22269fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_GET,
2227a7de92daSDan Williams 		},
2228a7de92daSDan Williams 		.dev = &adev->dev,
2229a7de92daSDan Williams 	};
2230a7de92daSDan Williams 
2231a7de92daSDan Williams 	nfit_mem = devm_kzalloc(dev, sizeof(*nfit_mem), GFP_KERNEL);
2232a7de92daSDan Williams 	if (!nfit_mem)
2233a7de92daSDan Williams 		return -ENOMEM;
2234a7de92daSDan Williams 
2235a7de92daSDan Williams 	mask = 1UL << ND_CMD_SMART | 1UL << ND_CMD_SMART_THRESHOLD
2236a7de92daSDan Williams 		| 1UL << ND_CMD_DIMM_FLAGS | 1UL << ND_CMD_GET_CONFIG_SIZE
2237a7de92daSDan Williams 		| 1UL << ND_CMD_GET_CONFIG_DATA | 1UL << ND_CMD_SET_CONFIG_DATA
2238a7de92daSDan Williams 		| 1UL << ND_CMD_VENDOR;
2239a7de92daSDan Williams 	*nfit_mem = (struct nfit_mem) {
2240a7de92daSDan Williams 		.adev = adev,
2241a7de92daSDan Williams 		.family = NVDIMM_FAMILY_INTEL,
2242a7de92daSDan Williams 		.dsm_mask = mask,
2243a7de92daSDan Williams 	};
2244a7de92daSDan Williams 
2245a7de92daSDan Williams 	nvdimm = devm_kzalloc(dev, sizeof(*nvdimm), GFP_KERNEL);
2246a7de92daSDan Williams 	if (!nvdimm)
2247a7de92daSDan Williams 		return -ENOMEM;
2248a7de92daSDan Williams 	*nvdimm = (struct nvdimm) {
2249a7de92daSDan Williams 		.provider_data = nfit_mem,
2250a7de92daSDan Williams 		.cmd_mask = mask,
2251a7de92daSDan Williams 		.dev = {
2252a7de92daSDan Williams 			.init_name = "test-dimm",
2253a7de92daSDan Williams 		},
2254a7de92daSDan Williams 	};
2255a7de92daSDan Williams 
2256a7de92daSDan Williams 
2257a7de92daSDan Williams 	/* basic checkout of a typical 'get config size' command */
2258a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2259a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2260a7de92daSDan Williams 		.status = 0,
2261a7de92daSDan Williams 		.config_size = SZ_128K,
2262a7de92daSDan Williams 		.max_xfer = SZ_4K,
2263a7de92daSDan Williams 	};
2264a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2265a7de92daSDan Williams 	if (rc)
2266a7de92daSDan Williams 		return rc;
2267a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2268a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2269a7de92daSDan Williams 
2270a7de92daSDan Williams 	if (rc < 0 || cmd_rc || cmds.cfg_size.status != 0
2271a7de92daSDan Williams 			|| cmds.cfg_size.config_size != SZ_128K
2272a7de92daSDan Williams 			|| cmds.cfg_size.max_xfer != SZ_4K) {
2273a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2274a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2275a7de92daSDan Williams 		return -EIO;
2276a7de92daSDan Williams 	}
2277a7de92daSDan Williams 
2278a7de92daSDan Williams 
2279a7de92daSDan Williams 	/* test ars_status with zero output */
2280a7de92daSDan Williams 	cmd_size = offsetof(struct nd_cmd_ars_status, address);
2281a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2282a7de92daSDan Williams 		.out_length = 0,
2283a7de92daSDan Williams 	};
2284a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2285a7de92daSDan Williams 	if (rc)
2286a7de92daSDan Williams 		return rc;
2287a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2288a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2289a7de92daSDan Williams 
2290a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2291a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2292a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2293a7de92daSDan Williams 		return -EIO;
2294a7de92daSDan Williams 	}
2295a7de92daSDan Williams 
2296a7de92daSDan Williams 
2297a7de92daSDan Williams 	/* test ars_cap with benign extended status */
2298a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_cap);
2299a7de92daSDan Williams 	cmds.ars_cap = (struct nd_cmd_ars_cap) {
2300a7de92daSDan Williams 		.status = ND_ARS_PERSISTENT << 16,
2301a7de92daSDan Williams 	};
2302a7de92daSDan Williams 	offset = offsetof(struct nd_cmd_ars_cap, status);
2303a7de92daSDan Williams 	rc = setup_result(cmds.buf + offset, cmd_size - offset);
2304a7de92daSDan Williams 	if (rc)
2305a7de92daSDan Williams 		return rc;
2306a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_CAP,
2307a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2308a7de92daSDan Williams 
2309a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2310a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2311a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2312a7de92daSDan Williams 		return -EIO;
2313a7de92daSDan Williams 	}
2314a7de92daSDan Williams 
2315a7de92daSDan Williams 
2316a7de92daSDan Williams 	/* test ars_status with 'status' trimmed from 'out_length' */
2317a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2318a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2319a7de92daSDan Williams 		.out_length = cmd_size - 4,
2320a7de92daSDan Williams 	};
2321a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2322a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2323a7de92daSDan Williams 		.length = test_val,
2324a7de92daSDan Williams 	};
2325a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2326a7de92daSDan Williams 	if (rc)
2327a7de92daSDan Williams 		return rc;
2328a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2329a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2330a7de92daSDan Williams 
2331a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2332a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2333a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2334a7de92daSDan Williams 		return -EIO;
2335a7de92daSDan Williams 	}
2336a7de92daSDan Williams 
2337a7de92daSDan Williams 
2338a7de92daSDan Williams 	/* test ars_status with 'Output (Size)' including 'status' */
2339a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2340a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2341a7de92daSDan Williams 		.out_length = cmd_size,
2342a7de92daSDan Williams 	};
2343a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2344a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2345a7de92daSDan Williams 		.length = test_val,
2346a7de92daSDan Williams 	};
2347a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2348a7de92daSDan Williams 	if (rc)
2349a7de92daSDan Williams 		return rc;
2350a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2351a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2352a7de92daSDan Williams 
2353a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2354a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2355a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2356a7de92daSDan Williams 		return -EIO;
2357a7de92daSDan Williams 	}
2358a7de92daSDan Williams 
2359a7de92daSDan Williams 
2360a7de92daSDan Williams 	/* test extended status for get_config_size results in failure */
2361a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2362a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2363a7de92daSDan Williams 		.status = 1 << 16,
2364a7de92daSDan Williams 	};
2365a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2366a7de92daSDan Williams 	if (rc)
2367a7de92daSDan Williams 		return rc;
2368a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2369a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2370a7de92daSDan Williams 
2371a7de92daSDan Williams 	if (rc < 0 || cmd_rc >= 0) {
2372a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2373a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2374a7de92daSDan Williams 		return -EIO;
2375a7de92daSDan Williams 	}
2376a7de92daSDan Williams 
2377fb2a1748SDan Williams 	/* test clear error */
2378fb2a1748SDan Williams 	cmd_size = sizeof(cmds.clear_err);
2379fb2a1748SDan Williams 	cmds.clear_err = (struct nd_cmd_clear_error) {
2380fb2a1748SDan Williams 		.length = 512,
2381fb2a1748SDan Williams 		.cleared = 512,
2382fb2a1748SDan Williams 	};
2383fb2a1748SDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2384fb2a1748SDan Williams 	if (rc)
2385fb2a1748SDan Williams 		return rc;
2386fb2a1748SDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_CLEAR_ERROR,
2387fb2a1748SDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2388fb2a1748SDan Williams 	if (rc < 0 || cmd_rc) {
2389fb2a1748SDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2390fb2a1748SDan Williams 				__func__, __LINE__, rc, cmd_rc);
2391fb2a1748SDan Williams 		return -EIO;
2392fb2a1748SDan Williams 	}
2393fb2a1748SDan Williams 
2394a7de92daSDan Williams 	return 0;
2395a7de92daSDan Williams }
2396a7de92daSDan Williams 
23976bc75619SDan Williams static int nfit_test_probe(struct platform_device *pdev)
23986bc75619SDan Williams {
23996bc75619SDan Williams 	struct nvdimm_bus_descriptor *nd_desc;
24006bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
24016bc75619SDan Williams 	struct device *dev = &pdev->dev;
24026bc75619SDan Williams 	struct nfit_test *nfit_test;
2403231bf117SDan Williams 	struct nfit_mem *nfit_mem;
2404c14a868aSDan Williams 	union acpi_object *obj;
24056bc75619SDan Williams 	int rc;
24066bc75619SDan Williams 
2407a7de92daSDan Williams 	if (strcmp(dev_name(&pdev->dev), "nfit_test.0") == 0) {
2408a7de92daSDan Williams 		rc = nfit_ctl_test(&pdev->dev);
2409a7de92daSDan Williams 		if (rc)
2410a7de92daSDan Williams 			return rc;
2411a7de92daSDan Williams 	}
2412a7de92daSDan Williams 
24136bc75619SDan Williams 	nfit_test = to_nfit_test(&pdev->dev);
24146bc75619SDan Williams 
24156bc75619SDan Williams 	/* common alloc */
24166bc75619SDan Williams 	if (nfit_test->num_dcr) {
24176bc75619SDan Williams 		int num = nfit_test->num_dcr;
24186bc75619SDan Williams 
24196bc75619SDan Williams 		nfit_test->dimm = devm_kcalloc(dev, num, sizeof(void *),
24206bc75619SDan Williams 				GFP_KERNEL);
24216bc75619SDan Williams 		nfit_test->dimm_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
24226bc75619SDan Williams 				GFP_KERNEL);
24239d27a87eSDan Williams 		nfit_test->flush = devm_kcalloc(dev, num, sizeof(void *),
24249d27a87eSDan Williams 				GFP_KERNEL);
24259d27a87eSDan Williams 		nfit_test->flush_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
24269d27a87eSDan Williams 				GFP_KERNEL);
24276bc75619SDan Williams 		nfit_test->label = devm_kcalloc(dev, num, sizeof(void *),
24286bc75619SDan Williams 				GFP_KERNEL);
24296bc75619SDan Williams 		nfit_test->label_dma = devm_kcalloc(dev, num,
24306bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
24316bc75619SDan Williams 		nfit_test->dcr = devm_kcalloc(dev, num,
24326bc75619SDan Williams 				sizeof(struct nfit_test_dcr *), GFP_KERNEL);
24336bc75619SDan Williams 		nfit_test->dcr_dma = devm_kcalloc(dev, num,
24346bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
2435ed07c433SDan Williams 		nfit_test->smart = devm_kcalloc(dev, num,
2436ed07c433SDan Williams 				sizeof(struct nd_intel_smart), GFP_KERNEL);
2437ed07c433SDan Williams 		nfit_test->smart_threshold = devm_kcalloc(dev, num,
2438ed07c433SDan Williams 				sizeof(struct nd_intel_smart_threshold),
2439ed07c433SDan Williams 				GFP_KERNEL);
2440bfbaa952SDave Jiang 		nfit_test->fw = devm_kcalloc(dev, num,
2441bfbaa952SDave Jiang 				sizeof(struct nfit_test_fw), GFP_KERNEL);
24426bc75619SDan Williams 		if (nfit_test->dimm && nfit_test->dimm_dma && nfit_test->label
24436bc75619SDan Williams 				&& nfit_test->label_dma && nfit_test->dcr
24449d27a87eSDan Williams 				&& nfit_test->dcr_dma && nfit_test->flush
2445bfbaa952SDave Jiang 				&& nfit_test->flush_dma
2446bfbaa952SDave Jiang 				&& nfit_test->fw)
24476bc75619SDan Williams 			/* pass */;
24486bc75619SDan Williams 		else
24496bc75619SDan Williams 			return -ENOMEM;
24506bc75619SDan Williams 	}
24516bc75619SDan Williams 
24526bc75619SDan Williams 	if (nfit_test->num_pm) {
24536bc75619SDan Williams 		int num = nfit_test->num_pm;
24546bc75619SDan Williams 
24556bc75619SDan Williams 		nfit_test->spa_set = devm_kcalloc(dev, num, sizeof(void *),
24566bc75619SDan Williams 				GFP_KERNEL);
24576bc75619SDan Williams 		nfit_test->spa_set_dma = devm_kcalloc(dev, num,
24586bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
24596bc75619SDan Williams 		if (nfit_test->spa_set && nfit_test->spa_set_dma)
24606bc75619SDan Williams 			/* pass */;
24616bc75619SDan Williams 		else
24626bc75619SDan Williams 			return -ENOMEM;
24636bc75619SDan Williams 	}
24646bc75619SDan Williams 
24656bc75619SDan Williams 	/* per-nfit specific alloc */
24666bc75619SDan Williams 	if (nfit_test->alloc(nfit_test))
24676bc75619SDan Williams 		return -ENOMEM;
24686bc75619SDan Williams 
24696bc75619SDan Williams 	nfit_test->setup(nfit_test);
24706bc75619SDan Williams 	acpi_desc = &nfit_test->acpi_desc;
2471a61fe6f7SDan Williams 	acpi_nfit_desc_init(acpi_desc, &pdev->dev);
24726bc75619SDan Williams 	acpi_desc->blk_do_io = nfit_test_blk_do_io;
24736bc75619SDan Williams 	nd_desc = &acpi_desc->nd_desc;
2474a61fe6f7SDan Williams 	nd_desc->provider_name = NULL;
2475bc9775d8SDan Williams 	nd_desc->module = THIS_MODULE;
2476a61fe6f7SDan Williams 	nd_desc->ndctl = nfit_test_ctl;
24776bc75619SDan Williams 
2478e7a11b44SDan Williams 	rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_buf,
2479e7a11b44SDan Williams 			nfit_test->nfit_size);
248058cd71b4SDan Williams 	if (rc)
248120985164SVishal Verma 		return rc;
248220985164SVishal Verma 
2483fbabd829SDan Williams 	rc = devm_add_action_or_reset(&pdev->dev, acpi_nfit_shutdown, acpi_desc);
2484fbabd829SDan Williams 	if (rc)
2485fbabd829SDan Williams 		return rc;
2486fbabd829SDan Williams 
248720985164SVishal Verma 	if (nfit_test->setup != nfit_test0_setup)
248820985164SVishal Verma 		return 0;
248920985164SVishal Verma 
249020985164SVishal Verma 	nfit_test->setup_hotplug = 1;
249120985164SVishal Verma 	nfit_test->setup(nfit_test);
249220985164SVishal Verma 
2493c14a868aSDan Williams 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
2494c14a868aSDan Williams 	if (!obj)
2495c14a868aSDan Williams 		return -ENOMEM;
2496c14a868aSDan Williams 	obj->type = ACPI_TYPE_BUFFER;
2497c14a868aSDan Williams 	obj->buffer.length = nfit_test->nfit_size;
2498c14a868aSDan Williams 	obj->buffer.pointer = nfit_test->nfit_buf;
2499c14a868aSDan Williams 	*(nfit_test->_fit) = obj;
2500c14a868aSDan Williams 	__acpi_nfit_notify(&pdev->dev, nfit_test, 0x80);
2501231bf117SDan Williams 
2502231bf117SDan Williams 	/* associate dimm devices with nfit_mem data for notification testing */
2503231bf117SDan Williams 	mutex_lock(&acpi_desc->init_mutex);
2504231bf117SDan Williams 	list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
2505231bf117SDan Williams 		u32 nfit_handle = __to_nfit_memdev(nfit_mem)->device_handle;
2506231bf117SDan Williams 		int i;
2507231bf117SDan Williams 
2508231bf117SDan Williams 		for (i = 0; i < NUM_DCR; i++)
2509231bf117SDan Williams 			if (nfit_handle == handle[i])
2510231bf117SDan Williams 				dev_set_drvdata(nfit_test->dimm_dev[i],
2511231bf117SDan Williams 						nfit_mem);
2512231bf117SDan Williams 	}
2513231bf117SDan Williams 	mutex_unlock(&acpi_desc->init_mutex);
25146bc75619SDan Williams 
25156bc75619SDan Williams 	return 0;
25166bc75619SDan Williams }
25176bc75619SDan Williams 
25186bc75619SDan Williams static int nfit_test_remove(struct platform_device *pdev)
25196bc75619SDan Williams {
25206bc75619SDan Williams 	return 0;
25216bc75619SDan Williams }
25226bc75619SDan Williams 
25236bc75619SDan Williams static void nfit_test_release(struct device *dev)
25246bc75619SDan Williams {
25256bc75619SDan Williams 	struct nfit_test *nfit_test = to_nfit_test(dev);
25266bc75619SDan Williams 
25276bc75619SDan Williams 	kfree(nfit_test);
25286bc75619SDan Williams }
25296bc75619SDan Williams 
25306bc75619SDan Williams static const struct platform_device_id nfit_test_id[] = {
25316bc75619SDan Williams 	{ KBUILD_MODNAME },
25326bc75619SDan Williams 	{ },
25336bc75619SDan Williams };
25346bc75619SDan Williams 
25356bc75619SDan Williams static struct platform_driver nfit_test_driver = {
25366bc75619SDan Williams 	.probe = nfit_test_probe,
25376bc75619SDan Williams 	.remove = nfit_test_remove,
25386bc75619SDan Williams 	.driver = {
25396bc75619SDan Williams 		.name = KBUILD_MODNAME,
25406bc75619SDan Williams 	},
25416bc75619SDan Williams 	.id_table = nfit_test_id,
25426bc75619SDan Williams };
25436bc75619SDan Williams 
25446bc75619SDan Williams static __init int nfit_test_init(void)
25456bc75619SDan Williams {
25466bc75619SDan Williams 	int rc, i;
25476bc75619SDan Williams 
2548a7de92daSDan Williams 	nfit_test_setup(nfit_test_lookup, nfit_test_evaluate_dsm);
2549231bf117SDan Williams 
25509fb1a190SDave Jiang 	nfit_wq = create_singlethread_workqueue("nfit");
25519fb1a190SDave Jiang 	if (!nfit_wq)
25529fb1a190SDave Jiang 		return -ENOMEM;
25539fb1a190SDave Jiang 
2554a7de92daSDan Williams 	nfit_test_dimm = class_create(THIS_MODULE, "nfit_test_dimm");
2555a7de92daSDan Williams 	if (IS_ERR(nfit_test_dimm)) {
2556a7de92daSDan Williams 		rc = PTR_ERR(nfit_test_dimm);
2557a7de92daSDan Williams 		goto err_register;
2558a7de92daSDan Williams 	}
25596bc75619SDan Williams 
25606bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++) {
25616bc75619SDan Williams 		struct nfit_test *nfit_test;
25626bc75619SDan Williams 		struct platform_device *pdev;
25636bc75619SDan Williams 
25646bc75619SDan Williams 		nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
25656bc75619SDan Williams 		if (!nfit_test) {
25666bc75619SDan Williams 			rc = -ENOMEM;
25676bc75619SDan Williams 			goto err_register;
25686bc75619SDan Williams 		}
25696bc75619SDan Williams 		INIT_LIST_HEAD(&nfit_test->resources);
25709fb1a190SDave Jiang 		badrange_init(&nfit_test->badrange);
25716bc75619SDan Williams 		switch (i) {
25726bc75619SDan Williams 		case 0:
25736bc75619SDan Williams 			nfit_test->num_pm = NUM_PM;
2574dafb1048SDan Williams 			nfit_test->dcr_idx = 0;
25756bc75619SDan Williams 			nfit_test->num_dcr = NUM_DCR;
25766bc75619SDan Williams 			nfit_test->alloc = nfit_test0_alloc;
25776bc75619SDan Williams 			nfit_test->setup = nfit_test0_setup;
25786bc75619SDan Williams 			break;
25796bc75619SDan Williams 		case 1:
2580a117699cSYasunori Goto 			nfit_test->num_pm = 2;
2581dafb1048SDan Williams 			nfit_test->dcr_idx = NUM_DCR;
2582ac40b675SDan Williams 			nfit_test->num_dcr = 2;
25836bc75619SDan Williams 			nfit_test->alloc = nfit_test1_alloc;
25846bc75619SDan Williams 			nfit_test->setup = nfit_test1_setup;
25856bc75619SDan Williams 			break;
25866bc75619SDan Williams 		default:
25876bc75619SDan Williams 			rc = -EINVAL;
25886bc75619SDan Williams 			goto err_register;
25896bc75619SDan Williams 		}
25906bc75619SDan Williams 		pdev = &nfit_test->pdev;
25916bc75619SDan Williams 		pdev->name = KBUILD_MODNAME;
25926bc75619SDan Williams 		pdev->id = i;
25936bc75619SDan Williams 		pdev->dev.release = nfit_test_release;
25946bc75619SDan Williams 		rc = platform_device_register(pdev);
25956bc75619SDan Williams 		if (rc) {
25966bc75619SDan Williams 			put_device(&pdev->dev);
25976bc75619SDan Williams 			goto err_register;
25986bc75619SDan Williams 		}
25998b06b884SDan Williams 		get_device(&pdev->dev);
26006bc75619SDan Williams 
26016bc75619SDan Williams 		rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
26026bc75619SDan Williams 		if (rc)
26036bc75619SDan Williams 			goto err_register;
26046bc75619SDan Williams 
26056bc75619SDan Williams 		instances[i] = nfit_test;
26069fb1a190SDave Jiang 		INIT_WORK(&nfit_test->work, uc_error_notify);
26076bc75619SDan Williams 	}
26086bc75619SDan Williams 
26096bc75619SDan Williams 	rc = platform_driver_register(&nfit_test_driver);
26106bc75619SDan Williams 	if (rc)
26116bc75619SDan Williams 		goto err_register;
26126bc75619SDan Williams 	return 0;
26136bc75619SDan Williams 
26146bc75619SDan Williams  err_register:
26159fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
26166bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
26176bc75619SDan Williams 		if (instances[i])
26186bc75619SDan Williams 			platform_device_unregister(&instances[i]->pdev);
26196bc75619SDan Williams 	nfit_test_teardown();
26208b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
26218b06b884SDan Williams 		if (instances[i])
26228b06b884SDan Williams 			put_device(&instances[i]->pdev.dev);
26238b06b884SDan Williams 
26246bc75619SDan Williams 	return rc;
26256bc75619SDan Williams }
26266bc75619SDan Williams 
26276bc75619SDan Williams static __exit void nfit_test_exit(void)
26286bc75619SDan Williams {
26296bc75619SDan Williams 	int i;
26306bc75619SDan Williams 
26319fb1a190SDave Jiang 	flush_workqueue(nfit_wq);
26329fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
26336bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
26346bc75619SDan Williams 		platform_device_unregister(&instances[i]->pdev);
26358b06b884SDan Williams 	platform_driver_unregister(&nfit_test_driver);
26366bc75619SDan Williams 	nfit_test_teardown();
26378b06b884SDan Williams 
26388b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
26398b06b884SDan Williams 		put_device(&instances[i]->pdev.dev);
2640231bf117SDan Williams 	class_destroy(nfit_test_dimm);
26416bc75619SDan Williams }
26426bc75619SDan Williams 
26436bc75619SDan Williams module_init(nfit_test_init);
26446bc75619SDan Williams module_exit(nfit_test_exit);
26456bc75619SDan Williams MODULE_LICENSE("GPL v2");
26466bc75619SDan Williams MODULE_AUTHOR("Intel Corporation");
2647