xref: /linux/tools/testing/nvdimm/test/nfit.c (revision 19357a685e870eeb825cbb7fd3104082ab041987)
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"
300fb5c8dfSDan Williams #include "../watermark.h"
316bc75619SDan Williams 
326bc75619SDan Williams /*
336bc75619SDan Williams  * Generate an NFIT table to describe the following topology:
346bc75619SDan Williams  *
356bc75619SDan Williams  * BUS0: Interleaved PMEM regions, and aliasing with BLK regions
366bc75619SDan Williams  *
376bc75619SDan Williams  *                     (a)                       (b)            DIMM   BLK-REGION
386bc75619SDan Williams  *           +----------+--------------+----------+---------+
396bc75619SDan Williams  * +------+  |  blk2.0  |     pm0.0    |  blk2.1  |  pm1.0  |    0      region2
406bc75619SDan Williams  * | imc0 +--+- - - - - region0 - - - -+----------+         +
416bc75619SDan Williams  * +--+---+  |  blk3.0  |     pm0.0    |  blk3.1  |  pm1.0  |    1      region3
426bc75619SDan Williams  *    |      +----------+--------------v----------v         v
436bc75619SDan Williams  * +--+---+                            |                    |
446bc75619SDan Williams  * | cpu0 |                                    region1
456bc75619SDan Williams  * +--+---+                            |                    |
466bc75619SDan Williams  *    |      +-------------------------^----------^         ^
476bc75619SDan Williams  * +--+---+  |                 blk4.0             |  pm1.0  |    2      region4
486bc75619SDan Williams  * | imc1 +--+-------------------------+----------+         +
496bc75619SDan Williams  * +------+  |                 blk5.0             |  pm1.0  |    3      region5
506bc75619SDan Williams  *           +-------------------------+----------+-+-------+
516bc75619SDan Williams  *
5220985164SVishal Verma  * +--+---+
5320985164SVishal Verma  * | cpu1 |
5420985164SVishal Verma  * +--+---+                   (Hotplug DIMM)
5520985164SVishal Verma  *    |      +----------------------------------------------+
5620985164SVishal Verma  * +--+---+  |                 blk6.0/pm7.0                 |    4      region6/7
5720985164SVishal Verma  * | imc0 +--+----------------------------------------------+
5820985164SVishal Verma  * +------+
5920985164SVishal Verma  *
6020985164SVishal Verma  *
616bc75619SDan Williams  * *) In this layout we have four dimms and two memory controllers in one
626bc75619SDan Williams  *    socket.  Each unique interface (BLK or PMEM) to DPA space
636bc75619SDan Williams  *    is identified by a region device with a dynamically assigned id.
646bc75619SDan Williams  *
656bc75619SDan Williams  * *) The first portion of dimm0 and dimm1 are interleaved as REGION0.
666bc75619SDan Williams  *    A single PMEM namespace "pm0.0" is created using half of the
676bc75619SDan Williams  *    REGION0 SPA-range.  REGION0 spans dimm0 and dimm1.  PMEM namespace
686bc75619SDan Williams  *    allocate from from the bottom of a region.  The unallocated
696bc75619SDan Williams  *    portion of REGION0 aliases with REGION2 and REGION3.  That
706bc75619SDan Williams  *    unallacted capacity is reclaimed as BLK namespaces ("blk2.0" and
716bc75619SDan Williams  *    "blk3.0") starting at the base of each DIMM to offset (a) in those
726bc75619SDan Williams  *    DIMMs.  "pm0.0", "blk2.0" and "blk3.0" are free-form readable
736bc75619SDan Williams  *    names that can be assigned to a namespace.
746bc75619SDan Williams  *
756bc75619SDan Williams  * *) In the last portion of dimm0 and dimm1 we have an interleaved
766bc75619SDan Williams  *    SPA range, REGION1, that spans those two dimms as well as dimm2
776bc75619SDan Williams  *    and dimm3.  Some of REGION1 allocated to a PMEM namespace named
786bc75619SDan Williams  *    "pm1.0" the rest is reclaimed in 4 BLK namespaces (for each
796bc75619SDan Williams  *    dimm in the interleave set), "blk2.1", "blk3.1", "blk4.0", and
806bc75619SDan Williams  *    "blk5.0".
816bc75619SDan Williams  *
826bc75619SDan Williams  * *) The portion of dimm2 and dimm3 that do not participate in the
836bc75619SDan Williams  *    REGION1 interleaved SPA range (i.e. the DPA address below offset
846bc75619SDan Williams  *    (b) are also included in the "blk4.0" and "blk5.0" namespaces.
856bc75619SDan Williams  *    Note, that BLK namespaces need not be contiguous in DPA-space, and
866bc75619SDan Williams  *    can consume aliased capacity from multiple interleave sets.
876bc75619SDan Williams  *
886bc75619SDan Williams  * BUS1: Legacy NVDIMM (single contiguous range)
896bc75619SDan Williams  *
906bc75619SDan Williams  *  region2
916bc75619SDan Williams  * +---------------------+
926bc75619SDan Williams  * |---------------------|
936bc75619SDan Williams  * ||       pm2.0       ||
946bc75619SDan Williams  * |---------------------|
956bc75619SDan Williams  * +---------------------+
966bc75619SDan Williams  *
976bc75619SDan Williams  * *) A NFIT-table may describe a simple system-physical-address range
986bc75619SDan Williams  *    with no BLK aliasing.  This type of region may optionally
996bc75619SDan Williams  *    reference an NVDIMM.
1006bc75619SDan Williams  */
1016bc75619SDan Williams enum {
10220985164SVishal Verma 	NUM_PM  = 3,
10320985164SVishal Verma 	NUM_DCR = 5,
10485d3fa02SDan Williams 	NUM_HINTS = 8,
1056bc75619SDan Williams 	NUM_BDW = NUM_DCR,
1066bc75619SDan Williams 	NUM_SPA = NUM_PM + NUM_DCR + NUM_BDW,
1079741a559SRoss Zwisler 	NUM_MEM = NUM_DCR + NUM_BDW + 2 /* spa0 iset */
1089741a559SRoss Zwisler 		+ 4 /* spa1 iset */ + 1 /* spa11 iset */,
1096bc75619SDan Williams 	DIMM_SIZE = SZ_32M,
1106bc75619SDan Williams 	LABEL_SIZE = SZ_128K,
1117bfe97c7SDan Williams 	SPA_VCD_SIZE = SZ_4M,
1126bc75619SDan Williams 	SPA0_SIZE = DIMM_SIZE,
1136bc75619SDan Williams 	SPA1_SIZE = DIMM_SIZE*2,
1146bc75619SDan Williams 	SPA2_SIZE = DIMM_SIZE,
1156bc75619SDan Williams 	BDW_SIZE = 64 << 8,
1166bc75619SDan Williams 	DCR_SIZE = 12,
1176bc75619SDan Williams 	NUM_NFITS = 2, /* permit testing multiple NFITs per system */
1186bc75619SDan Williams };
1196bc75619SDan Williams 
1206bc75619SDan Williams struct nfit_test_dcr {
1216bc75619SDan Williams 	__le64 bdw_addr;
1226bc75619SDan Williams 	__le32 bdw_status;
1236bc75619SDan Williams 	__u8 aperature[BDW_SIZE];
1246bc75619SDan Williams };
1256bc75619SDan Williams 
1266bc75619SDan Williams #define NFIT_DIMM_HANDLE(node, socket, imc, chan, dimm) \
1276bc75619SDan Williams 	(((node & 0xfff) << 16) | ((socket & 0xf) << 12) \
1286bc75619SDan Williams 	 | ((imc & 0xf) << 8) | ((chan & 0xf) << 4) | (dimm & 0xf))
1296bc75619SDan Williams 
130dafb1048SDan Williams static u32 handle[] = {
1316bc75619SDan Williams 	[0] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 0),
1326bc75619SDan Williams 	[1] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 1),
1336bc75619SDan Williams 	[2] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 0),
1346bc75619SDan Williams 	[3] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 1),
13520985164SVishal Verma 	[4] = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
136dafb1048SDan Williams 	[5] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 0),
137ac40b675SDan Williams 	[6] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 1),
1386bc75619SDan Williams };
1396bc75619SDan Williams 
14073606afdSDan Williams static unsigned long dimm_fail_cmd_flags[NUM_DCR];
14155c72ab6SDan Williams static int dimm_fail_cmd_code[NUM_DCR];
14273606afdSDan Williams 
143bfbaa952SDave Jiang struct nfit_test_fw {
144bfbaa952SDave Jiang 	enum intel_fw_update_state state;
145bfbaa952SDave Jiang 	u32 context;
146bfbaa952SDave Jiang 	u64 version;
147bfbaa952SDave Jiang 	u32 size_received;
148bfbaa952SDave Jiang 	u64 end_time;
149bfbaa952SDave Jiang };
150bfbaa952SDave Jiang 
1516bc75619SDan Williams struct nfit_test {
1526bc75619SDan Williams 	struct acpi_nfit_desc acpi_desc;
1536bc75619SDan Williams 	struct platform_device pdev;
1546bc75619SDan Williams 	struct list_head resources;
1556bc75619SDan Williams 	void *nfit_buf;
1566bc75619SDan Williams 	dma_addr_t nfit_dma;
1576bc75619SDan Williams 	size_t nfit_size;
1581526f9e2SRoss Zwisler 	size_t nfit_filled;
159dafb1048SDan Williams 	int dcr_idx;
1606bc75619SDan Williams 	int num_dcr;
1616bc75619SDan Williams 	int num_pm;
1626bc75619SDan Williams 	void **dimm;
1636bc75619SDan Williams 	dma_addr_t *dimm_dma;
1649d27a87eSDan Williams 	void **flush;
1659d27a87eSDan Williams 	dma_addr_t *flush_dma;
1666bc75619SDan Williams 	void **label;
1676bc75619SDan Williams 	dma_addr_t *label_dma;
1686bc75619SDan Williams 	void **spa_set;
1696bc75619SDan Williams 	dma_addr_t *spa_set_dma;
1706bc75619SDan Williams 	struct nfit_test_dcr **dcr;
1716bc75619SDan Williams 	dma_addr_t *dcr_dma;
1726bc75619SDan Williams 	int (*alloc)(struct nfit_test *t);
1736bc75619SDan Williams 	void (*setup)(struct nfit_test *t);
17420985164SVishal Verma 	int setup_hotplug;
175c14a868aSDan Williams 	union acpi_object **_fit;
176c14a868aSDan Williams 	dma_addr_t _fit_dma;
177f471f1a7SDan Williams 	struct ars_state {
178f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status;
179f471f1a7SDan Williams 		unsigned long deadline;
180f471f1a7SDan Williams 		spinlock_t lock;
181f471f1a7SDan Williams 	} ars_state;
182231bf117SDan Williams 	struct device *dimm_dev[NUM_DCR];
183ed07c433SDan Williams 	struct nd_intel_smart *smart;
184ed07c433SDan Williams 	struct nd_intel_smart_threshold *smart_threshold;
1859fb1a190SDave Jiang 	struct badrange badrange;
1869fb1a190SDave Jiang 	struct work_struct work;
187bfbaa952SDave Jiang 	struct nfit_test_fw *fw;
1886bc75619SDan Williams };
1896bc75619SDan Williams 
1909fb1a190SDave Jiang static struct workqueue_struct *nfit_wq;
1919fb1a190SDave Jiang 
1926bc75619SDan Williams static struct nfit_test *to_nfit_test(struct device *dev)
1936bc75619SDan Williams {
1946bc75619SDan Williams 	struct platform_device *pdev = to_platform_device(dev);
1956bc75619SDan Williams 
1966bc75619SDan Williams 	return container_of(pdev, struct nfit_test, pdev);
1976bc75619SDan Williams }
1986bc75619SDan Williams 
199bfbaa952SDave Jiang static int nd_intel_test_get_fw_info(struct nfit_test *t,
200bfbaa952SDave Jiang 		struct nd_intel_fw_info *nd_cmd, unsigned int buf_len,
201bfbaa952SDave Jiang 		int idx)
202bfbaa952SDave Jiang {
203bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
204bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
205bfbaa952SDave Jiang 
206bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p, buf_len: %u, idx: %d\n",
207bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
208bfbaa952SDave Jiang 
209bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
210bfbaa952SDave Jiang 		return -EINVAL;
211bfbaa952SDave Jiang 
212bfbaa952SDave Jiang 	nd_cmd->status = 0;
213bfbaa952SDave Jiang 	nd_cmd->storage_size = INTEL_FW_STORAGE_SIZE;
214bfbaa952SDave Jiang 	nd_cmd->max_send_len = INTEL_FW_MAX_SEND_LEN;
215bfbaa952SDave Jiang 	nd_cmd->query_interval = INTEL_FW_QUERY_INTERVAL;
216bfbaa952SDave Jiang 	nd_cmd->max_query_time = INTEL_FW_QUERY_MAX_TIME;
217bfbaa952SDave Jiang 	nd_cmd->update_cap = 0;
218bfbaa952SDave Jiang 	nd_cmd->fis_version = INTEL_FW_FIS_VERSION;
219bfbaa952SDave Jiang 	nd_cmd->run_version = 0;
220bfbaa952SDave Jiang 	nd_cmd->updated_version = fw->version;
221bfbaa952SDave Jiang 
222bfbaa952SDave Jiang 	return 0;
223bfbaa952SDave Jiang }
224bfbaa952SDave Jiang 
225bfbaa952SDave Jiang static int nd_intel_test_start_update(struct nfit_test *t,
226bfbaa952SDave Jiang 		struct nd_intel_fw_start *nd_cmd, unsigned int buf_len,
227bfbaa952SDave Jiang 		int idx)
228bfbaa952SDave Jiang {
229bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
230bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
231bfbaa952SDave Jiang 
232bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
233bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
234bfbaa952SDave Jiang 
235bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
236bfbaa952SDave Jiang 		return -EINVAL;
237bfbaa952SDave Jiang 
238bfbaa952SDave Jiang 	if (fw->state != FW_STATE_NEW) {
239bfbaa952SDave Jiang 		/* extended status, FW update in progress */
240bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
241bfbaa952SDave Jiang 		return 0;
242bfbaa952SDave Jiang 	}
243bfbaa952SDave Jiang 
244bfbaa952SDave Jiang 	fw->state = FW_STATE_IN_PROGRESS;
245bfbaa952SDave Jiang 	fw->context++;
246bfbaa952SDave Jiang 	fw->size_received = 0;
247bfbaa952SDave Jiang 	nd_cmd->status = 0;
248bfbaa952SDave Jiang 	nd_cmd->context = fw->context;
249bfbaa952SDave Jiang 
250bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context issued: %#x\n", __func__, nd_cmd->context);
251bfbaa952SDave Jiang 
252bfbaa952SDave Jiang 	return 0;
253bfbaa952SDave Jiang }
254bfbaa952SDave Jiang 
255bfbaa952SDave Jiang static int nd_intel_test_send_data(struct nfit_test *t,
256bfbaa952SDave Jiang 		struct nd_intel_fw_send_data *nd_cmd, unsigned int buf_len,
257bfbaa952SDave Jiang 		int idx)
258bfbaa952SDave Jiang {
259bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
260bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
261bfbaa952SDave Jiang 	u32 *status = (u32 *)&nd_cmd->data[nd_cmd->length];
262bfbaa952SDave Jiang 
263bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
264bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
265bfbaa952SDave Jiang 
266bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
267bfbaa952SDave Jiang 		return -EINVAL;
268bfbaa952SDave Jiang 
269bfbaa952SDave Jiang 
270bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->status: %#x\n", __func__, *status);
271bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[0]: %#x\n", __func__, nd_cmd->data[0]);
272bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[%u]: %#x\n", __func__, nd_cmd->length-1,
273bfbaa952SDave Jiang 			nd_cmd->data[nd_cmd->length-1]);
274bfbaa952SDave Jiang 
275bfbaa952SDave Jiang 	if (fw->state != FW_STATE_IN_PROGRESS) {
276bfbaa952SDave Jiang 		dev_dbg(dev, "%s: not in IN_PROGRESS state\n", __func__);
277bfbaa952SDave Jiang 		*status = 0x5;
278bfbaa952SDave Jiang 		return 0;
279bfbaa952SDave Jiang 	}
280bfbaa952SDave Jiang 
281bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
282bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
283bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
284bfbaa952SDave Jiang 		*status = 0x10007;
285bfbaa952SDave Jiang 		return 0;
286bfbaa952SDave Jiang 	}
287bfbaa952SDave Jiang 
288bfbaa952SDave Jiang 	/*
289bfbaa952SDave Jiang 	 * check offset + len > size of fw storage
290bfbaa952SDave Jiang 	 * check length is > max send length
291bfbaa952SDave Jiang 	 */
292bfbaa952SDave Jiang 	if (nd_cmd->offset + nd_cmd->length > INTEL_FW_STORAGE_SIZE ||
293bfbaa952SDave Jiang 			nd_cmd->length > INTEL_FW_MAX_SEND_LEN) {
294bfbaa952SDave Jiang 		*status = 0x3;
295bfbaa952SDave Jiang 		dev_dbg(dev, "%s: buffer boundary violation\n", __func__);
296bfbaa952SDave Jiang 		return 0;
297bfbaa952SDave Jiang 	}
298bfbaa952SDave Jiang 
299bfbaa952SDave Jiang 	fw->size_received += nd_cmd->length;
300bfbaa952SDave Jiang 	dev_dbg(dev, "%s: copying %u bytes, %u bytes so far\n",
301bfbaa952SDave Jiang 			__func__, nd_cmd->length, fw->size_received);
302bfbaa952SDave Jiang 	*status = 0;
303bfbaa952SDave Jiang 	return 0;
304bfbaa952SDave Jiang }
305bfbaa952SDave Jiang 
306bfbaa952SDave Jiang static int nd_intel_test_finish_fw(struct nfit_test *t,
307bfbaa952SDave Jiang 		struct nd_intel_fw_finish_update *nd_cmd,
308bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
309bfbaa952SDave Jiang {
310bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
311bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
312bfbaa952SDave Jiang 
313bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
314bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
315bfbaa952SDave Jiang 
316bfbaa952SDave Jiang 	if (fw->state == FW_STATE_UPDATED) {
317bfbaa952SDave Jiang 		/* update already done, need cold boot */
318bfbaa952SDave Jiang 		nd_cmd->status = 0x20007;
319bfbaa952SDave Jiang 		return 0;
320bfbaa952SDave Jiang 	}
321bfbaa952SDave Jiang 
322bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context: %#x  ctrl_flags: %#x\n",
323bfbaa952SDave Jiang 			__func__, nd_cmd->context, nd_cmd->ctrl_flags);
324bfbaa952SDave Jiang 
325bfbaa952SDave Jiang 	switch (nd_cmd->ctrl_flags) {
326bfbaa952SDave Jiang 	case 0: /* finish */
327bfbaa952SDave Jiang 		if (nd_cmd->context != fw->context) {
328bfbaa952SDave Jiang 			dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
329bfbaa952SDave Jiang 					__func__, nd_cmd->context,
330bfbaa952SDave Jiang 					fw->context);
331bfbaa952SDave Jiang 			nd_cmd->status = 0x10007;
332bfbaa952SDave Jiang 			return 0;
333bfbaa952SDave Jiang 		}
334bfbaa952SDave Jiang 		nd_cmd->status = 0;
335bfbaa952SDave Jiang 		fw->state = FW_STATE_VERIFY;
336bfbaa952SDave Jiang 		/* set 1 second of time for firmware "update" */
337bfbaa952SDave Jiang 		fw->end_time = jiffies + HZ;
338bfbaa952SDave Jiang 		break;
339bfbaa952SDave Jiang 
340bfbaa952SDave Jiang 	case 1: /* abort */
341bfbaa952SDave Jiang 		fw->size_received = 0;
342bfbaa952SDave Jiang 		/* successfully aborted status */
343bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
344bfbaa952SDave Jiang 		fw->state = FW_STATE_NEW;
345bfbaa952SDave Jiang 		dev_dbg(dev, "%s: abort successful\n", __func__);
346bfbaa952SDave Jiang 		break;
347bfbaa952SDave Jiang 
348bfbaa952SDave Jiang 	default: /* bad control flag */
349bfbaa952SDave Jiang 		dev_warn(dev, "%s: unknown control flag: %#x\n",
350bfbaa952SDave Jiang 				__func__, nd_cmd->ctrl_flags);
351bfbaa952SDave Jiang 		return -EINVAL;
352bfbaa952SDave Jiang 	}
353bfbaa952SDave Jiang 
354bfbaa952SDave Jiang 	return 0;
355bfbaa952SDave Jiang }
356bfbaa952SDave Jiang 
357bfbaa952SDave Jiang static int nd_intel_test_finish_query(struct nfit_test *t,
358bfbaa952SDave Jiang 		struct nd_intel_fw_finish_query *nd_cmd,
359bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
360bfbaa952SDave Jiang {
361bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
362bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
363bfbaa952SDave Jiang 
364bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
365bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
366bfbaa952SDave Jiang 
367bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
368bfbaa952SDave Jiang 		return -EINVAL;
369bfbaa952SDave Jiang 
370bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
371bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
372bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
373bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
374bfbaa952SDave Jiang 		return 0;
375bfbaa952SDave Jiang 	}
376bfbaa952SDave Jiang 
377bfbaa952SDave Jiang 	dev_dbg(dev, "%s context: %#x\n", __func__, nd_cmd->context);
378bfbaa952SDave Jiang 
379bfbaa952SDave Jiang 	switch (fw->state) {
380bfbaa952SDave Jiang 	case FW_STATE_NEW:
381bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
382bfbaa952SDave Jiang 		nd_cmd->status = 0;
383bfbaa952SDave Jiang 		dev_dbg(dev, "%s: new state\n", __func__);
384bfbaa952SDave Jiang 		break;
385bfbaa952SDave Jiang 
386bfbaa952SDave Jiang 	case FW_STATE_IN_PROGRESS:
387bfbaa952SDave Jiang 		/* sequencing error */
388bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
389bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
390bfbaa952SDave Jiang 		dev_dbg(dev, "%s: sequence error\n", __func__);
391bfbaa952SDave Jiang 		break;
392bfbaa952SDave Jiang 
393bfbaa952SDave Jiang 	case FW_STATE_VERIFY:
394bfbaa952SDave Jiang 		if (time_is_after_jiffies64(fw->end_time)) {
395bfbaa952SDave Jiang 			nd_cmd->updated_fw_rev = 0;
396bfbaa952SDave Jiang 			nd_cmd->status = 0x20007;
397bfbaa952SDave Jiang 			dev_dbg(dev, "%s: still verifying\n", __func__);
398bfbaa952SDave Jiang 			break;
399bfbaa952SDave Jiang 		}
400bfbaa952SDave Jiang 
401bfbaa952SDave Jiang 		dev_dbg(dev, "%s: transition out verify\n", __func__);
402bfbaa952SDave Jiang 		fw->state = FW_STATE_UPDATED;
403bfbaa952SDave Jiang 		/* we are going to fall through if it's "done" */
404bfbaa952SDave Jiang 	case FW_STATE_UPDATED:
405bfbaa952SDave Jiang 		nd_cmd->status = 0;
406bfbaa952SDave Jiang 		/* bogus test version */
407bfbaa952SDave Jiang 		fw->version = nd_cmd->updated_fw_rev =
408bfbaa952SDave Jiang 			INTEL_FW_FAKE_VERSION;
409bfbaa952SDave Jiang 		dev_dbg(dev, "%s: updated\n", __func__);
410bfbaa952SDave Jiang 		break;
411bfbaa952SDave Jiang 
412bfbaa952SDave Jiang 	default: /* we should never get here */
413bfbaa952SDave Jiang 		return -EINVAL;
414bfbaa952SDave Jiang 	}
415bfbaa952SDave Jiang 
416bfbaa952SDave Jiang 	return 0;
417bfbaa952SDave Jiang }
418bfbaa952SDave Jiang 
41939c686b8SVishal Verma static int nfit_test_cmd_get_config_size(struct nd_cmd_get_config_size *nd_cmd,
4206bc75619SDan Williams 		unsigned int buf_len)
4216bc75619SDan Williams {
4226bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4236bc75619SDan Williams 		return -EINVAL;
42439c686b8SVishal Verma 
4256bc75619SDan Williams 	nd_cmd->status = 0;
4266bc75619SDan Williams 	nd_cmd->config_size = LABEL_SIZE;
4276bc75619SDan Williams 	nd_cmd->max_xfer = SZ_4K;
42839c686b8SVishal Verma 
42939c686b8SVishal Verma 	return 0;
4306bc75619SDan Williams }
43139c686b8SVishal Verma 
43239c686b8SVishal Verma static int nfit_test_cmd_get_config_data(struct nd_cmd_get_config_data_hdr
43339c686b8SVishal Verma 		*nd_cmd, unsigned int buf_len, void *label)
43439c686b8SVishal Verma {
4356bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
43639c686b8SVishal Verma 	int rc;
4376bc75619SDan Williams 
4386bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4396bc75619SDan Williams 		return -EINVAL;
4406bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4416bc75619SDan Williams 		return -EINVAL;
4426bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) > buf_len)
4436bc75619SDan Williams 		return -EINVAL;
4446bc75619SDan Williams 
4456bc75619SDan Williams 	nd_cmd->status = 0;
4466bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
44739c686b8SVishal Verma 	memcpy(nd_cmd->out_buf, label + offset, len);
4486bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - len;
44939c686b8SVishal Verma 
45039c686b8SVishal Verma 	return rc;
4516bc75619SDan Williams }
45239c686b8SVishal Verma 
45339c686b8SVishal Verma static int nfit_test_cmd_set_config_data(struct nd_cmd_set_config_hdr *nd_cmd,
45439c686b8SVishal Verma 		unsigned int buf_len, void *label)
45539c686b8SVishal Verma {
4566bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
4576bc75619SDan Williams 	u32 *status;
45839c686b8SVishal Verma 	int rc;
4596bc75619SDan Williams 
4606bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4616bc75619SDan Williams 		return -EINVAL;
4626bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4636bc75619SDan Williams 		return -EINVAL;
4646bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) + 4 > buf_len)
4656bc75619SDan Williams 		return -EINVAL;
4666bc75619SDan Williams 
46739c686b8SVishal Verma 	status = (void *)nd_cmd + nd_cmd->in_length + sizeof(*nd_cmd);
4686bc75619SDan Williams 	*status = 0;
4696bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
47039c686b8SVishal Verma 	memcpy(label + offset, nd_cmd->in_buf, len);
4716bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - (len + 4);
47239c686b8SVishal Verma 
47339c686b8SVishal Verma 	return rc;
4746bc75619SDan Williams }
47539c686b8SVishal Verma 
476d4f32367SDan Williams #define NFIT_TEST_CLEAR_ERR_UNIT 256
477747ffe11SDan Williams 
47839c686b8SVishal Verma static int nfit_test_cmd_ars_cap(struct nd_cmd_ars_cap *nd_cmd,
47939c686b8SVishal Verma 		unsigned int buf_len)
48039c686b8SVishal Verma {
4819fb1a190SDave Jiang 	int ars_recs;
4829fb1a190SDave Jiang 
48339c686b8SVishal Verma 	if (buf_len < sizeof(*nd_cmd))
48439c686b8SVishal Verma 		return -EINVAL;
48539c686b8SVishal Verma 
4869fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
4879fb1a190SDave Jiang 	ars_recs = SZ_4K / sizeof(struct nd_ars_record);
4889fb1a190SDave Jiang 
489747ffe11SDan Williams 	nd_cmd->max_ars_out = sizeof(struct nd_cmd_ars_status)
4909fb1a190SDave Jiang 		+ ars_recs * sizeof(struct nd_ars_record);
49139c686b8SVishal Verma 	nd_cmd->status = (ND_ARS_PERSISTENT | ND_ARS_VOLATILE) << 16;
492d4f32367SDan Williams 	nd_cmd->clear_err_unit = NFIT_TEST_CLEAR_ERR_UNIT;
49339c686b8SVishal Verma 
49439c686b8SVishal Verma 	return 0;
49539c686b8SVishal Verma }
49639c686b8SVishal Verma 
4979fb1a190SDave Jiang static void post_ars_status(struct ars_state *ars_state,
4989fb1a190SDave Jiang 		struct badrange *badrange, u64 addr, u64 len)
49939c686b8SVishal Verma {
500f471f1a7SDan Williams 	struct nd_cmd_ars_status *ars_status;
501f471f1a7SDan Williams 	struct nd_ars_record *ars_record;
5029fb1a190SDave Jiang 	struct badrange_entry *be;
5039fb1a190SDave Jiang 	u64 end = addr + len - 1;
5049fb1a190SDave Jiang 	int i = 0;
505f471f1a7SDan Williams 
506f471f1a7SDan Williams 	ars_state->deadline = jiffies + 1*HZ;
507f471f1a7SDan Williams 	ars_status = ars_state->ars_status;
508f471f1a7SDan Williams 	ars_status->status = 0;
509f471f1a7SDan Williams 	ars_status->address = addr;
510f471f1a7SDan Williams 	ars_status->length = len;
511f471f1a7SDan Williams 	ars_status->type = ND_ARS_PERSISTENT;
5129fb1a190SDave Jiang 
5139fb1a190SDave Jiang 	spin_lock(&badrange->lock);
5149fb1a190SDave Jiang 	list_for_each_entry(be, &badrange->list, list) {
5159fb1a190SDave Jiang 		u64 be_end = be->start + be->length - 1;
5169fb1a190SDave Jiang 		u64 rstart, rend;
5179fb1a190SDave Jiang 
5189fb1a190SDave Jiang 		/* skip entries outside the range */
5199fb1a190SDave Jiang 		if (be_end < addr || be->start > end)
5209fb1a190SDave Jiang 			continue;
5219fb1a190SDave Jiang 
5229fb1a190SDave Jiang 		rstart = (be->start < addr) ? addr : be->start;
5239fb1a190SDave Jiang 		rend = (be_end < end) ? be_end : end;
5249fb1a190SDave Jiang 		ars_record = &ars_status->records[i];
525f471f1a7SDan Williams 		ars_record->handle = 0;
5269fb1a190SDave Jiang 		ars_record->err_address = rstart;
5279fb1a190SDave Jiang 		ars_record->length = rend - rstart + 1;
5289fb1a190SDave Jiang 		i++;
5299fb1a190SDave Jiang 	}
5309fb1a190SDave Jiang 	spin_unlock(&badrange->lock);
5319fb1a190SDave Jiang 	ars_status->num_records = i;
5329fb1a190SDave Jiang 	ars_status->out_length = sizeof(struct nd_cmd_ars_status)
5339fb1a190SDave Jiang 		+ i * sizeof(struct nd_ars_record);
534f471f1a7SDan Williams }
535f471f1a7SDan Williams 
5369fb1a190SDave Jiang static int nfit_test_cmd_ars_start(struct nfit_test *t,
5379fb1a190SDave Jiang 		struct ars_state *ars_state,
538f471f1a7SDan Williams 		struct nd_cmd_ars_start *ars_start, unsigned int buf_len,
539f471f1a7SDan Williams 		int *cmd_rc)
540f471f1a7SDan Williams {
541f471f1a7SDan Williams 	if (buf_len < sizeof(*ars_start))
54239c686b8SVishal Verma 		return -EINVAL;
54339c686b8SVishal Verma 
544f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
545f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
546f471f1a7SDan Williams 		ars_start->status = NFIT_ARS_START_BUSY;
547f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
548f471f1a7SDan Williams 	} else {
549f471f1a7SDan Williams 		ars_start->status = 0;
550f471f1a7SDan Williams 		ars_start->scrub_time = 1;
5519fb1a190SDave Jiang 		post_ars_status(ars_state, &t->badrange, ars_start->address,
552f471f1a7SDan Williams 				ars_start->length);
553f471f1a7SDan Williams 		*cmd_rc = 0;
554f471f1a7SDan Williams 	}
555f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
55639c686b8SVishal Verma 
55739c686b8SVishal Verma 	return 0;
55839c686b8SVishal Verma }
55939c686b8SVishal Verma 
560f471f1a7SDan Williams static int nfit_test_cmd_ars_status(struct ars_state *ars_state,
561f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status, unsigned int buf_len,
562f471f1a7SDan Williams 		int *cmd_rc)
56339c686b8SVishal Verma {
564f471f1a7SDan Williams 	if (buf_len < ars_state->ars_status->out_length)
56539c686b8SVishal Verma 		return -EINVAL;
56639c686b8SVishal Verma 
567f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
568f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
569f471f1a7SDan Williams 		memset(ars_status, 0, buf_len);
570f471f1a7SDan Williams 		ars_status->status = NFIT_ARS_STATUS_BUSY;
571f471f1a7SDan Williams 		ars_status->out_length = sizeof(*ars_status);
572f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
573f471f1a7SDan Williams 	} else {
574f471f1a7SDan Williams 		memcpy(ars_status, ars_state->ars_status,
575f471f1a7SDan Williams 				ars_state->ars_status->out_length);
576f471f1a7SDan Williams 		*cmd_rc = 0;
577f471f1a7SDan Williams 	}
578f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
57939c686b8SVishal Verma 	return 0;
58039c686b8SVishal Verma }
58139c686b8SVishal Verma 
5825e096ef3SVishal Verma static int nfit_test_cmd_clear_error(struct nfit_test *t,
5835e096ef3SVishal Verma 		struct nd_cmd_clear_error *clear_err,
584d4f32367SDan Williams 		unsigned int buf_len, int *cmd_rc)
585d4f32367SDan Williams {
586d4f32367SDan Williams 	const u64 mask = NFIT_TEST_CLEAR_ERR_UNIT - 1;
587d4f32367SDan Williams 	if (buf_len < sizeof(*clear_err))
588d4f32367SDan Williams 		return -EINVAL;
589d4f32367SDan Williams 
590d4f32367SDan Williams 	if ((clear_err->address & mask) || (clear_err->length & mask))
591d4f32367SDan Williams 		return -EINVAL;
592d4f32367SDan Williams 
5935e096ef3SVishal Verma 	badrange_forget(&t->badrange, clear_err->address, clear_err->length);
594d4f32367SDan Williams 	clear_err->status = 0;
595d4f32367SDan Williams 	clear_err->cleared = clear_err->length;
596d4f32367SDan Williams 	*cmd_rc = 0;
597d4f32367SDan Williams 	return 0;
598d4f32367SDan Williams }
599d4f32367SDan Williams 
60010246dc8SYasunori Goto struct region_search_spa {
60110246dc8SYasunori Goto 	u64 addr;
60210246dc8SYasunori Goto 	struct nd_region *region;
60310246dc8SYasunori Goto };
60410246dc8SYasunori Goto 
60510246dc8SYasunori Goto static int is_region_device(struct device *dev)
60610246dc8SYasunori Goto {
60710246dc8SYasunori Goto 	return !strncmp(dev->kobj.name, "region", 6);
60810246dc8SYasunori Goto }
60910246dc8SYasunori Goto 
61010246dc8SYasunori Goto static int nfit_test_search_region_spa(struct device *dev, void *data)
61110246dc8SYasunori Goto {
61210246dc8SYasunori Goto 	struct region_search_spa *ctx = data;
61310246dc8SYasunori Goto 	struct nd_region *nd_region;
61410246dc8SYasunori Goto 	resource_size_t ndr_end;
61510246dc8SYasunori Goto 
61610246dc8SYasunori Goto 	if (!is_region_device(dev))
61710246dc8SYasunori Goto 		return 0;
61810246dc8SYasunori Goto 
61910246dc8SYasunori Goto 	nd_region = to_nd_region(dev);
62010246dc8SYasunori Goto 	ndr_end = nd_region->ndr_start + nd_region->ndr_size;
62110246dc8SYasunori Goto 
62210246dc8SYasunori Goto 	if (ctx->addr >= nd_region->ndr_start && ctx->addr < ndr_end) {
62310246dc8SYasunori Goto 		ctx->region = nd_region;
62410246dc8SYasunori Goto 		return 1;
62510246dc8SYasunori Goto 	}
62610246dc8SYasunori Goto 
62710246dc8SYasunori Goto 	return 0;
62810246dc8SYasunori Goto }
62910246dc8SYasunori Goto 
63010246dc8SYasunori Goto static int nfit_test_search_spa(struct nvdimm_bus *bus,
63110246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa)
63210246dc8SYasunori Goto {
63310246dc8SYasunori Goto 	int ret;
63410246dc8SYasunori Goto 	struct nd_region *nd_region = NULL;
63510246dc8SYasunori Goto 	struct nvdimm *nvdimm = NULL;
63610246dc8SYasunori Goto 	struct nd_mapping *nd_mapping = NULL;
63710246dc8SYasunori Goto 	struct region_search_spa ctx = {
63810246dc8SYasunori Goto 		.addr = spa->spa,
63910246dc8SYasunori Goto 		.region = NULL,
64010246dc8SYasunori Goto 	};
64110246dc8SYasunori Goto 	u64 dpa;
64210246dc8SYasunori Goto 
64310246dc8SYasunori Goto 	ret = device_for_each_child(&bus->dev, &ctx,
64410246dc8SYasunori Goto 				nfit_test_search_region_spa);
64510246dc8SYasunori Goto 
64610246dc8SYasunori Goto 	if (!ret)
64710246dc8SYasunori Goto 		return -ENODEV;
64810246dc8SYasunori Goto 
64910246dc8SYasunori Goto 	nd_region = ctx.region;
65010246dc8SYasunori Goto 
65110246dc8SYasunori Goto 	dpa = ctx.addr - nd_region->ndr_start;
65210246dc8SYasunori Goto 
65310246dc8SYasunori Goto 	/*
65410246dc8SYasunori Goto 	 * last dimm is selected for test
65510246dc8SYasunori Goto 	 */
65610246dc8SYasunori Goto 	nd_mapping = &nd_region->mapping[nd_region->ndr_mappings - 1];
65710246dc8SYasunori Goto 	nvdimm = nd_mapping->nvdimm;
65810246dc8SYasunori Goto 
65910246dc8SYasunori Goto 	spa->devices[0].nfit_device_handle = handle[nvdimm->id];
66010246dc8SYasunori Goto 	spa->num_nvdimms = 1;
66110246dc8SYasunori Goto 	spa->devices[0].dpa = dpa;
66210246dc8SYasunori Goto 
66310246dc8SYasunori Goto 	return 0;
66410246dc8SYasunori Goto }
66510246dc8SYasunori Goto 
66610246dc8SYasunori Goto static int nfit_test_cmd_translate_spa(struct nvdimm_bus *bus,
66710246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa, unsigned int buf_len)
66810246dc8SYasunori Goto {
66910246dc8SYasunori Goto 	if (buf_len < spa->translate_length)
67010246dc8SYasunori Goto 		return -EINVAL;
67110246dc8SYasunori Goto 
67210246dc8SYasunori Goto 	if (nfit_test_search_spa(bus, spa) < 0 || !spa->num_nvdimms)
67310246dc8SYasunori Goto 		spa->status = 2;
67410246dc8SYasunori Goto 
67510246dc8SYasunori Goto 	return 0;
67610246dc8SYasunori Goto }
67710246dc8SYasunori Goto 
678ed07c433SDan Williams static int nfit_test_cmd_smart(struct nd_intel_smart *smart, unsigned int buf_len,
679ed07c433SDan Williams 		struct nd_intel_smart *smart_data)
680baa51277SDan Williams {
681baa51277SDan Williams 	if (buf_len < sizeof(*smart))
682baa51277SDan Williams 		return -EINVAL;
683ed07c433SDan Williams 	memcpy(smart, smart_data, sizeof(*smart));
684baa51277SDan Williams 	return 0;
685baa51277SDan Williams }
686baa51277SDan Williams 
687cdd77d3eSDan Williams static int nfit_test_cmd_smart_threshold(
688ed07c433SDan Williams 		struct nd_intel_smart_threshold *out,
689ed07c433SDan Williams 		unsigned int buf_len,
690ed07c433SDan Williams 		struct nd_intel_smart_threshold *smart_t)
691baa51277SDan Williams {
692baa51277SDan Williams 	if (buf_len < sizeof(*smart_t))
693baa51277SDan Williams 		return -EINVAL;
694ed07c433SDan Williams 	memcpy(out, smart_t, sizeof(*smart_t));
695ed07c433SDan Williams 	return 0;
696ed07c433SDan Williams }
697ed07c433SDan Williams 
698ed07c433SDan Williams static void smart_notify(struct device *bus_dev,
699ed07c433SDan Williams 		struct device *dimm_dev, struct nd_intel_smart *smart,
700ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh)
701ed07c433SDan Williams {
702ed07c433SDan Williams 	dev_dbg(dimm_dev, "%s: alarm: %#x spares: %d (%d) mtemp: %d (%d) ctemp: %d (%d)\n",
703ed07c433SDan Williams 			__func__, thresh->alarm_control, thresh->spares,
704ed07c433SDan Williams 			smart->spares, thresh->media_temperature,
705ed07c433SDan Williams 			smart->media_temperature, thresh->ctrl_temperature,
706ed07c433SDan Williams 			smart->ctrl_temperature);
707ed07c433SDan Williams 	if (((thresh->alarm_control & ND_INTEL_SMART_SPARE_TRIP)
708ed07c433SDan Williams 				&& smart->spares
709ed07c433SDan Williams 				<= thresh->spares)
710ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_TEMP_TRIP)
711ed07c433SDan Williams 				&& smart->media_temperature
712ed07c433SDan Williams 				>= thresh->media_temperature)
713ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_CTEMP_TRIP)
714ed07c433SDan Williams 				&& smart->ctrl_temperature
7154cf260fcSVishal Verma 				>= thresh->ctrl_temperature)
7164cf260fcSVishal Verma 			|| (smart->health != ND_INTEL_SMART_NON_CRITICAL_HEALTH)
7174cf260fcSVishal Verma 			|| (smart->shutdown_state != 0)) {
718ed07c433SDan Williams 		device_lock(bus_dev);
719ed07c433SDan Williams 		__acpi_nvdimm_notify(dimm_dev, 0x81);
720ed07c433SDan Williams 		device_unlock(bus_dev);
721ed07c433SDan Williams 	}
722ed07c433SDan Williams }
723ed07c433SDan Williams 
724ed07c433SDan Williams static int nfit_test_cmd_smart_set_threshold(
725ed07c433SDan Williams 		struct nd_intel_smart_set_threshold *in,
726ed07c433SDan Williams 		unsigned int buf_len,
727ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh,
728ed07c433SDan Williams 		struct nd_intel_smart *smart,
729ed07c433SDan Williams 		struct device *bus_dev, struct device *dimm_dev)
730ed07c433SDan Williams {
731ed07c433SDan Williams 	unsigned int size;
732ed07c433SDan Williams 
733ed07c433SDan Williams 	size = sizeof(*in) - 4;
734ed07c433SDan Williams 	if (buf_len < size)
735ed07c433SDan Williams 		return -EINVAL;
736ed07c433SDan Williams 	memcpy(thresh->data, in, size);
737ed07c433SDan Williams 	in->status = 0;
738ed07c433SDan Williams 	smart_notify(bus_dev, dimm_dev, smart, thresh);
739ed07c433SDan Williams 
740baa51277SDan Williams 	return 0;
741baa51277SDan Williams }
742baa51277SDan Williams 
7434cf260fcSVishal Verma static int nfit_test_cmd_smart_inject(
7444cf260fcSVishal Verma 		struct nd_intel_smart_inject *inj,
7454cf260fcSVishal Verma 		unsigned int buf_len,
7464cf260fcSVishal Verma 		struct nd_intel_smart_threshold *thresh,
7474cf260fcSVishal Verma 		struct nd_intel_smart *smart,
7484cf260fcSVishal Verma 		struct device *bus_dev, struct device *dimm_dev)
7494cf260fcSVishal Verma {
7504cf260fcSVishal Verma 	if (buf_len != sizeof(*inj))
7514cf260fcSVishal Verma 		return -EINVAL;
7524cf260fcSVishal Verma 
7534cf260fcSVishal Verma 	if (inj->mtemp_enable)
7544cf260fcSVishal Verma 		smart->media_temperature = inj->media_temperature;
7554cf260fcSVishal Verma 	if (inj->spare_enable)
7564cf260fcSVishal Verma 		smart->spares = inj->spares;
7574cf260fcSVishal Verma 	if (inj->fatal_enable)
7584cf260fcSVishal Verma 		smart->health = ND_INTEL_SMART_FATAL_HEALTH;
7594cf260fcSVishal Verma 	if (inj->unsafe_shutdown_enable) {
7604cf260fcSVishal Verma 		smart->shutdown_state = 1;
7614cf260fcSVishal Verma 		smart->shutdown_count++;
7624cf260fcSVishal Verma 	}
7634cf260fcSVishal Verma 	inj->status = 0;
7644cf260fcSVishal Verma 	smart_notify(bus_dev, dimm_dev, smart, thresh);
7654cf260fcSVishal Verma 
7664cf260fcSVishal Verma 	return 0;
7674cf260fcSVishal Verma }
7684cf260fcSVishal Verma 
7699fb1a190SDave Jiang static void uc_error_notify(struct work_struct *work)
7709fb1a190SDave Jiang {
7719fb1a190SDave Jiang 	struct nfit_test *t = container_of(work, typeof(*t), work);
7729fb1a190SDave Jiang 
7739fb1a190SDave Jiang 	__acpi_nfit_notify(&t->pdev.dev, t, NFIT_NOTIFY_UC_MEMORY_ERROR);
7749fb1a190SDave Jiang }
7759fb1a190SDave Jiang 
7769fb1a190SDave Jiang static int nfit_test_cmd_ars_error_inject(struct nfit_test *t,
7779fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj *err_inj, unsigned int buf_len)
7789fb1a190SDave Jiang {
7799fb1a190SDave Jiang 	int rc;
7809fb1a190SDave Jiang 
78141cb3301SVishal Verma 	if (buf_len != sizeof(*err_inj)) {
7829fb1a190SDave Jiang 		rc = -EINVAL;
7839fb1a190SDave Jiang 		goto err;
7849fb1a190SDave Jiang 	}
7859fb1a190SDave Jiang 
7869fb1a190SDave Jiang 	if (err_inj->err_inj_spa_range_length <= 0) {
7879fb1a190SDave Jiang 		rc = -EINVAL;
7889fb1a190SDave Jiang 		goto err;
7899fb1a190SDave Jiang 	}
7909fb1a190SDave Jiang 
7919fb1a190SDave Jiang 	rc =  badrange_add(&t->badrange, err_inj->err_inj_spa_range_base,
7929fb1a190SDave Jiang 			err_inj->err_inj_spa_range_length);
7939fb1a190SDave Jiang 	if (rc < 0)
7949fb1a190SDave Jiang 		goto err;
7959fb1a190SDave Jiang 
7969fb1a190SDave Jiang 	if (err_inj->err_inj_options & (1 << ND_ARS_ERR_INJ_OPT_NOTIFY))
7979fb1a190SDave Jiang 		queue_work(nfit_wq, &t->work);
7989fb1a190SDave Jiang 
7999fb1a190SDave Jiang 	err_inj->status = 0;
8009fb1a190SDave Jiang 	return 0;
8019fb1a190SDave Jiang 
8029fb1a190SDave Jiang err:
8039fb1a190SDave Jiang 	err_inj->status = NFIT_ARS_INJECT_INVALID;
8049fb1a190SDave Jiang 	return rc;
8059fb1a190SDave Jiang }
8069fb1a190SDave Jiang 
8079fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_clear(struct nfit_test *t,
8089fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_clr *err_clr, unsigned int buf_len)
8099fb1a190SDave Jiang {
8109fb1a190SDave Jiang 	int rc;
8119fb1a190SDave Jiang 
81241cb3301SVishal Verma 	if (buf_len != sizeof(*err_clr)) {
8139fb1a190SDave Jiang 		rc = -EINVAL;
8149fb1a190SDave Jiang 		goto err;
8159fb1a190SDave Jiang 	}
8169fb1a190SDave Jiang 
8179fb1a190SDave Jiang 	if (err_clr->err_inj_clr_spa_range_length <= 0) {
8189fb1a190SDave Jiang 		rc = -EINVAL;
8199fb1a190SDave Jiang 		goto err;
8209fb1a190SDave Jiang 	}
8219fb1a190SDave Jiang 
8229fb1a190SDave Jiang 	badrange_forget(&t->badrange, err_clr->err_inj_clr_spa_range_base,
8239fb1a190SDave Jiang 			err_clr->err_inj_clr_spa_range_length);
8249fb1a190SDave Jiang 
8259fb1a190SDave Jiang 	err_clr->status = 0;
8269fb1a190SDave Jiang 	return 0;
8279fb1a190SDave Jiang 
8289fb1a190SDave Jiang err:
8299fb1a190SDave Jiang 	err_clr->status = NFIT_ARS_INJECT_INVALID;
8309fb1a190SDave Jiang 	return rc;
8319fb1a190SDave Jiang }
8329fb1a190SDave Jiang 
8339fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_status(struct nfit_test *t,
8349fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_stat *err_stat,
8359fb1a190SDave Jiang 		unsigned int buf_len)
8369fb1a190SDave Jiang {
8379fb1a190SDave Jiang 	struct badrange_entry *be;
8389fb1a190SDave Jiang 	int max = SZ_4K / sizeof(struct nd_error_stat_query_record);
8399fb1a190SDave Jiang 	int i = 0;
8409fb1a190SDave Jiang 
8419fb1a190SDave Jiang 	err_stat->status = 0;
8429fb1a190SDave Jiang 	spin_lock(&t->badrange.lock);
8439fb1a190SDave Jiang 	list_for_each_entry(be, &t->badrange.list, list) {
8449fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_base = be->start;
8459fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_length = be->length;
8469fb1a190SDave Jiang 		i++;
8479fb1a190SDave Jiang 		if (i > max)
8489fb1a190SDave Jiang 			break;
8499fb1a190SDave Jiang 	}
8509fb1a190SDave Jiang 	spin_unlock(&t->badrange.lock);
8519fb1a190SDave Jiang 	err_stat->inj_err_rec_count = i;
8529fb1a190SDave Jiang 
8539fb1a190SDave Jiang 	return 0;
8549fb1a190SDave Jiang }
8559fb1a190SDave Jiang 
856674d8bdeSDave Jiang static int nd_intel_test_cmd_set_lss_status(struct nfit_test *t,
857674d8bdeSDave Jiang 		struct nd_intel_lss *nd_cmd, unsigned int buf_len)
858674d8bdeSDave Jiang {
859674d8bdeSDave Jiang 	struct device *dev = &t->pdev.dev;
860674d8bdeSDave Jiang 
861674d8bdeSDave Jiang 	if (buf_len < sizeof(*nd_cmd))
862674d8bdeSDave Jiang 		return -EINVAL;
863674d8bdeSDave Jiang 
864674d8bdeSDave Jiang 	switch (nd_cmd->enable) {
865674d8bdeSDave Jiang 	case 0:
866674d8bdeSDave Jiang 		nd_cmd->status = 0;
867674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status disabled\n",
868674d8bdeSDave Jiang 				__func__);
869674d8bdeSDave Jiang 		break;
870674d8bdeSDave Jiang 	case 1:
871674d8bdeSDave Jiang 		nd_cmd->status = 0;
872674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status enabled\n",
873674d8bdeSDave Jiang 				__func__);
874674d8bdeSDave Jiang 		break;
875674d8bdeSDave Jiang 	default:
876674d8bdeSDave Jiang 		dev_warn(dev, "Unknown enable value: %#x\n", nd_cmd->enable);
877674d8bdeSDave Jiang 		nd_cmd->status = 0x3;
878674d8bdeSDave Jiang 		break;
879674d8bdeSDave Jiang 	}
880674d8bdeSDave Jiang 
881674d8bdeSDave Jiang 
882674d8bdeSDave Jiang 	return 0;
883674d8bdeSDave Jiang }
884674d8bdeSDave Jiang 
885bfbaa952SDave Jiang static int get_dimm(struct nfit_mem *nfit_mem, unsigned int func)
886bfbaa952SDave Jiang {
887bfbaa952SDave Jiang 	int i;
888bfbaa952SDave Jiang 
889bfbaa952SDave Jiang 	/* lookup per-dimm data */
890bfbaa952SDave Jiang 	for (i = 0; i < ARRAY_SIZE(handle); i++)
891bfbaa952SDave Jiang 		if (__to_nfit_memdev(nfit_mem)->device_handle == handle[i])
892bfbaa952SDave Jiang 			break;
893bfbaa952SDave Jiang 	if (i >= ARRAY_SIZE(handle))
894bfbaa952SDave Jiang 		return -ENXIO;
895bfbaa952SDave Jiang 
89655c72ab6SDan Williams 	if ((1 << func) & dimm_fail_cmd_flags[i]) {
89755c72ab6SDan Williams 		if (dimm_fail_cmd_code[i])
89855c72ab6SDan Williams 			return dimm_fail_cmd_code[i];
899bfbaa952SDave Jiang 		return -EIO;
90055c72ab6SDan Williams 	}
901bfbaa952SDave Jiang 
902bfbaa952SDave Jiang 	return i;
903bfbaa952SDave Jiang }
904bfbaa952SDave Jiang 
90539c686b8SVishal Verma static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
90639c686b8SVishal Verma 		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
907aef25338SDan Williams 		unsigned int buf_len, int *cmd_rc)
90839c686b8SVishal Verma {
90939c686b8SVishal Verma 	struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
91039c686b8SVishal Verma 	struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
9116634fb06SDan Williams 	unsigned int func = cmd;
912f471f1a7SDan Williams 	int i, rc = 0, __cmd_rc;
913f471f1a7SDan Williams 
914f471f1a7SDan Williams 	if (!cmd_rc)
915f471f1a7SDan Williams 		cmd_rc = &__cmd_rc;
916f471f1a7SDan Williams 	*cmd_rc = 0;
91739c686b8SVishal Verma 
91839c686b8SVishal Verma 	if (nvdimm) {
91939c686b8SVishal Verma 		struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
920e3654ecaSDan Williams 		unsigned long cmd_mask = nvdimm_cmd_mask(nvdimm);
92139c686b8SVishal Verma 
9226634fb06SDan Williams 		if (!nfit_mem)
9236634fb06SDan Williams 			return -ENOTTY;
9246634fb06SDan Williams 
9256634fb06SDan Williams 		if (cmd == ND_CMD_CALL) {
9266634fb06SDan Williams 			struct nd_cmd_pkg *call_pkg = buf;
9276634fb06SDan Williams 
9286634fb06SDan Williams 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
9296634fb06SDan Williams 			buf = (void *) call_pkg->nd_payload;
9306634fb06SDan Williams 			func = call_pkg->nd_command;
9316634fb06SDan Williams 			if (call_pkg->nd_family != nfit_mem->family)
9326634fb06SDan Williams 				return -ENOTTY;
933bfbaa952SDave Jiang 
934bfbaa952SDave Jiang 			i = get_dimm(nfit_mem, func);
935bfbaa952SDave Jiang 			if (i < 0)
936bfbaa952SDave Jiang 				return i;
937bfbaa952SDave Jiang 
938bfbaa952SDave Jiang 			switch (func) {
939674d8bdeSDave Jiang 			case ND_INTEL_ENABLE_LSS_STATUS:
940674d8bdeSDave Jiang 				return nd_intel_test_cmd_set_lss_status(t,
941674d8bdeSDave Jiang 						buf, buf_len);
942bfbaa952SDave Jiang 			case ND_INTEL_FW_GET_INFO:
943bfbaa952SDave Jiang 				return nd_intel_test_get_fw_info(t, buf,
944bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
945bfbaa952SDave Jiang 			case ND_INTEL_FW_START_UPDATE:
946bfbaa952SDave Jiang 				return nd_intel_test_start_update(t, buf,
947bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
948bfbaa952SDave Jiang 			case ND_INTEL_FW_SEND_DATA:
949bfbaa952SDave Jiang 				return nd_intel_test_send_data(t, buf,
950bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
951bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_UPDATE:
952bfbaa952SDave Jiang 				return nd_intel_test_finish_fw(t, buf,
953bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
954bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_QUERY:
955bfbaa952SDave Jiang 				return nd_intel_test_finish_query(t, buf,
956bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
957bfbaa952SDave Jiang 			case ND_INTEL_SMART:
958bfbaa952SDave Jiang 				return nfit_test_cmd_smart(buf, buf_len,
959bfbaa952SDave Jiang 						&t->smart[i - t->dcr_idx]);
960bfbaa952SDave Jiang 			case ND_INTEL_SMART_THRESHOLD:
961bfbaa952SDave Jiang 				return nfit_test_cmd_smart_threshold(buf,
962bfbaa952SDave Jiang 						buf_len,
963bfbaa952SDave Jiang 						&t->smart_threshold[i -
964bfbaa952SDave Jiang 							t->dcr_idx]);
965bfbaa952SDave Jiang 			case ND_INTEL_SMART_SET_THRESHOLD:
966bfbaa952SDave Jiang 				return nfit_test_cmd_smart_set_threshold(buf,
967bfbaa952SDave Jiang 						buf_len,
968bfbaa952SDave Jiang 						&t->smart_threshold[i -
969bfbaa952SDave Jiang 							t->dcr_idx],
970bfbaa952SDave Jiang 						&t->smart[i - t->dcr_idx],
971bfbaa952SDave Jiang 						&t->pdev.dev, t->dimm_dev[i]);
9724cf260fcSVishal Verma 			case ND_INTEL_SMART_INJECT:
9734cf260fcSVishal Verma 				return nfit_test_cmd_smart_inject(buf,
9744cf260fcSVishal Verma 						buf_len,
9754cf260fcSVishal Verma 						&t->smart_threshold[i -
9764cf260fcSVishal Verma 							t->dcr_idx],
9774cf260fcSVishal Verma 						&t->smart[i - t->dcr_idx],
9784cf260fcSVishal Verma 						&t->pdev.dev, t->dimm_dev[i]);
979bfbaa952SDave Jiang 			default:
980bfbaa952SDave Jiang 				return -ENOTTY;
981bfbaa952SDave Jiang 			}
9826634fb06SDan Williams 		}
9836634fb06SDan Williams 
9846634fb06SDan Williams 		if (!test_bit(cmd, &cmd_mask)
9856634fb06SDan Williams 				|| !test_bit(func, &nfit_mem->dsm_mask))
98639c686b8SVishal Verma 			return -ENOTTY;
98739c686b8SVishal Verma 
988bfbaa952SDave Jiang 		i = get_dimm(nfit_mem, func);
989bfbaa952SDave Jiang 		if (i < 0)
990bfbaa952SDave Jiang 			return i;
99173606afdSDan Williams 
9926634fb06SDan Williams 		switch (func) {
99339c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_SIZE:
99439c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_size(buf, buf_len);
99539c686b8SVishal Verma 			break;
99639c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_DATA:
99739c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_data(buf, buf_len,
998dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
99939c686b8SVishal Verma 			break;
100039c686b8SVishal Verma 		case ND_CMD_SET_CONFIG_DATA:
100139c686b8SVishal Verma 			rc = nfit_test_cmd_set_config_data(buf, buf_len,
1002dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
100339c686b8SVishal Verma 			break;
10046bc75619SDan Williams 		default:
10056bc75619SDan Williams 			return -ENOTTY;
10066bc75619SDan Williams 		}
100739c686b8SVishal Verma 	} else {
1008f471f1a7SDan Williams 		struct ars_state *ars_state = &t->ars_state;
100910246dc8SYasunori Goto 		struct nd_cmd_pkg *call_pkg = buf;
101010246dc8SYasunori Goto 
101110246dc8SYasunori Goto 		if (!nd_desc)
101210246dc8SYasunori Goto 			return -ENOTTY;
101310246dc8SYasunori Goto 
101410246dc8SYasunori Goto 		if (cmd == ND_CMD_CALL) {
101510246dc8SYasunori Goto 			func = call_pkg->nd_command;
101610246dc8SYasunori Goto 
101710246dc8SYasunori Goto 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
101810246dc8SYasunori Goto 			buf = (void *) call_pkg->nd_payload;
101910246dc8SYasunori Goto 
102010246dc8SYasunori Goto 			switch (func) {
102110246dc8SYasunori Goto 			case NFIT_CMD_TRANSLATE_SPA:
102210246dc8SYasunori Goto 				rc = nfit_test_cmd_translate_spa(
102310246dc8SYasunori Goto 					acpi_desc->nvdimm_bus, buf, buf_len);
102410246dc8SYasunori Goto 				return rc;
10259fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_SET:
10269fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_error_inject(t, buf,
10279fb1a190SDave Jiang 					buf_len);
10289fb1a190SDave Jiang 				return rc;
10299fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_CLEAR:
10309fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_clear(t, buf,
10319fb1a190SDave Jiang 					buf_len);
10329fb1a190SDave Jiang 				return rc;
10339fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_GET:
10349fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_status(t, buf,
10359fb1a190SDave Jiang 					buf_len);
10369fb1a190SDave Jiang 				return rc;
103710246dc8SYasunori Goto 			default:
103810246dc8SYasunori Goto 				return -ENOTTY;
103910246dc8SYasunori Goto 			}
104010246dc8SYasunori Goto 		}
1041f471f1a7SDan Williams 
1042e3654ecaSDan Williams 		if (!nd_desc || !test_bit(cmd, &nd_desc->cmd_mask))
104339c686b8SVishal Verma 			return -ENOTTY;
104439c686b8SVishal Verma 
10456634fb06SDan Williams 		switch (func) {
104639c686b8SVishal Verma 		case ND_CMD_ARS_CAP:
104739c686b8SVishal Verma 			rc = nfit_test_cmd_ars_cap(buf, buf_len);
104839c686b8SVishal Verma 			break;
104939c686b8SVishal Verma 		case ND_CMD_ARS_START:
10509fb1a190SDave Jiang 			rc = nfit_test_cmd_ars_start(t, ars_state, buf,
10519fb1a190SDave Jiang 					buf_len, cmd_rc);
105239c686b8SVishal Verma 			break;
105339c686b8SVishal Verma 		case ND_CMD_ARS_STATUS:
1054f471f1a7SDan Williams 			rc = nfit_test_cmd_ars_status(ars_state, buf, buf_len,
1055f471f1a7SDan Williams 					cmd_rc);
105639c686b8SVishal Verma 			break;
1057d4f32367SDan Williams 		case ND_CMD_CLEAR_ERROR:
10585e096ef3SVishal Verma 			rc = nfit_test_cmd_clear_error(t, buf, buf_len, cmd_rc);
1059d4f32367SDan Williams 			break;
106039c686b8SVishal Verma 		default:
106139c686b8SVishal Verma 			return -ENOTTY;
106239c686b8SVishal Verma 		}
106339c686b8SVishal Verma 	}
10646bc75619SDan Williams 
10656bc75619SDan Williams 	return rc;
10666bc75619SDan Williams }
10676bc75619SDan Williams 
10686bc75619SDan Williams static DEFINE_SPINLOCK(nfit_test_lock);
10696bc75619SDan Williams static struct nfit_test *instances[NUM_NFITS];
10706bc75619SDan Williams 
10716bc75619SDan Williams static void release_nfit_res(void *data)
10726bc75619SDan Williams {
10736bc75619SDan Williams 	struct nfit_test_resource *nfit_res = data;
10746bc75619SDan Williams 
10756bc75619SDan Williams 	spin_lock(&nfit_test_lock);
10766bc75619SDan Williams 	list_del(&nfit_res->list);
10776bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
10786bc75619SDan Williams 
10796bc75619SDan Williams 	vfree(nfit_res->buf);
10806bc75619SDan Williams 	kfree(nfit_res);
10816bc75619SDan Williams }
10826bc75619SDan Williams 
10836bc75619SDan Williams static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
10846bc75619SDan Williams 		void *buf)
10856bc75619SDan Williams {
10866bc75619SDan Williams 	struct device *dev = &t->pdev.dev;
10876bc75619SDan Williams 	struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
10886bc75619SDan Williams 			GFP_KERNEL);
10896bc75619SDan Williams 	int rc;
10906bc75619SDan Williams 
1091bd4cd745SDan Williams 	if (!buf || !nfit_res)
10926bc75619SDan Williams 		goto err;
10936bc75619SDan Williams 	rc = devm_add_action(dev, release_nfit_res, nfit_res);
10946bc75619SDan Williams 	if (rc)
10956bc75619SDan Williams 		goto err;
10966bc75619SDan Williams 	INIT_LIST_HEAD(&nfit_res->list);
10976bc75619SDan Williams 	memset(buf, 0, size);
10986bc75619SDan Williams 	nfit_res->dev = dev;
10996bc75619SDan Williams 	nfit_res->buf = buf;
1100bd4cd745SDan Williams 	nfit_res->res.start = *dma;
1101bd4cd745SDan Williams 	nfit_res->res.end = *dma + size - 1;
1102bd4cd745SDan Williams 	nfit_res->res.name = "NFIT";
1103bd4cd745SDan Williams 	spin_lock_init(&nfit_res->lock);
1104bd4cd745SDan Williams 	INIT_LIST_HEAD(&nfit_res->requests);
11056bc75619SDan Williams 	spin_lock(&nfit_test_lock);
11066bc75619SDan Williams 	list_add(&nfit_res->list, &t->resources);
11076bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
11086bc75619SDan Williams 
11096bc75619SDan Williams 	return nfit_res->buf;
11106bc75619SDan Williams  err:
1111ee8520feSDan Williams 	if (buf)
11126bc75619SDan Williams 		vfree(buf);
11136bc75619SDan Williams 	kfree(nfit_res);
11146bc75619SDan Williams 	return NULL;
11156bc75619SDan Williams }
11166bc75619SDan Williams 
11176bc75619SDan Williams static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
11186bc75619SDan Williams {
11196bc75619SDan Williams 	void *buf = vmalloc(size);
11206bc75619SDan Williams 
11216bc75619SDan Williams 	*dma = (unsigned long) buf;
11226bc75619SDan Williams 	return __test_alloc(t, size, dma, buf);
11236bc75619SDan Williams }
11246bc75619SDan Williams 
11256bc75619SDan Williams static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
11266bc75619SDan Williams {
11276bc75619SDan Williams 	int i;
11286bc75619SDan Williams 
11296bc75619SDan Williams 	for (i = 0; i < ARRAY_SIZE(instances); i++) {
11306bc75619SDan Williams 		struct nfit_test_resource *n, *nfit_res = NULL;
11316bc75619SDan Williams 		struct nfit_test *t = instances[i];
11326bc75619SDan Williams 
11336bc75619SDan Williams 		if (!t)
11346bc75619SDan Williams 			continue;
11356bc75619SDan Williams 		spin_lock(&nfit_test_lock);
11366bc75619SDan Williams 		list_for_each_entry(n, &t->resources, list) {
1137bd4cd745SDan Williams 			if (addr >= n->res.start && (addr < n->res.start
1138bd4cd745SDan Williams 						+ resource_size(&n->res))) {
11396bc75619SDan Williams 				nfit_res = n;
11406bc75619SDan Williams 				break;
11416bc75619SDan Williams 			} else if (addr >= (unsigned long) n->buf
11426bc75619SDan Williams 					&& (addr < (unsigned long) n->buf
1143bd4cd745SDan Williams 						+ resource_size(&n->res))) {
11446bc75619SDan Williams 				nfit_res = n;
11456bc75619SDan Williams 				break;
11466bc75619SDan Williams 			}
11476bc75619SDan Williams 		}
11486bc75619SDan Williams 		spin_unlock(&nfit_test_lock);
11496bc75619SDan Williams 		if (nfit_res)
11506bc75619SDan Williams 			return nfit_res;
11516bc75619SDan Williams 	}
11526bc75619SDan Williams 
11536bc75619SDan Williams 	return NULL;
11546bc75619SDan Williams }
11556bc75619SDan Williams 
1156f471f1a7SDan Williams static int ars_state_init(struct device *dev, struct ars_state *ars_state)
1157f471f1a7SDan Williams {
11589fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
1159f471f1a7SDan Williams 	ars_state->ars_status = devm_kzalloc(dev,
11609fb1a190SDave Jiang 			sizeof(struct nd_cmd_ars_status) + SZ_4K, GFP_KERNEL);
1161f471f1a7SDan Williams 	if (!ars_state->ars_status)
1162f471f1a7SDan Williams 		return -ENOMEM;
1163f471f1a7SDan Williams 	spin_lock_init(&ars_state->lock);
1164f471f1a7SDan Williams 	return 0;
1165f471f1a7SDan Williams }
1166f471f1a7SDan Williams 
1167231bf117SDan Williams static void put_dimms(void *data)
1168231bf117SDan Williams {
1169718fda67SDan Williams 	struct nfit_test *t = data;
1170231bf117SDan Williams 	int i;
1171231bf117SDan Williams 
1172718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++)
1173718fda67SDan Williams 		if (t->dimm_dev[i])
1174718fda67SDan Williams 			device_unregister(t->dimm_dev[i]);
1175231bf117SDan Williams }
1176231bf117SDan Williams 
1177231bf117SDan Williams static struct class *nfit_test_dimm;
1178231bf117SDan Williams 
117973606afdSDan Williams static int dimm_name_to_id(struct device *dev)
118073606afdSDan Williams {
118173606afdSDan Williams 	int dimm;
118273606afdSDan Williams 
1183718fda67SDan Williams 	if (sscanf(dev_name(dev), "test_dimm%d", &dimm) != 1)
118473606afdSDan Williams 		return -ENXIO;
118573606afdSDan Williams 	return dimm;
118673606afdSDan Williams }
118773606afdSDan Williams 
118873606afdSDan Williams static ssize_t handle_show(struct device *dev, struct device_attribute *attr,
118973606afdSDan Williams 		char *buf)
119073606afdSDan Williams {
119173606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
119273606afdSDan Williams 
119373606afdSDan Williams 	if (dimm < 0)
119473606afdSDan Williams 		return dimm;
119573606afdSDan Williams 
1196*19357a68SDan Williams 	return sprintf(buf, "%#x\n", handle[dimm]);
119773606afdSDan Williams }
119873606afdSDan Williams DEVICE_ATTR_RO(handle);
119973606afdSDan Williams 
120073606afdSDan Williams static ssize_t fail_cmd_show(struct device *dev, struct device_attribute *attr,
120173606afdSDan Williams 		char *buf)
120273606afdSDan Williams {
120373606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
120473606afdSDan Williams 
120573606afdSDan Williams 	if (dimm < 0)
120673606afdSDan Williams 		return dimm;
120773606afdSDan Williams 
120873606afdSDan Williams 	return sprintf(buf, "%#lx\n", dimm_fail_cmd_flags[dimm]);
120973606afdSDan Williams }
121073606afdSDan Williams 
121173606afdSDan Williams static ssize_t fail_cmd_store(struct device *dev, struct device_attribute *attr,
121273606afdSDan Williams 		const char *buf, size_t size)
121373606afdSDan Williams {
121473606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
121573606afdSDan Williams 	unsigned long val;
121673606afdSDan Williams 	ssize_t rc;
121773606afdSDan Williams 
121873606afdSDan Williams 	if (dimm < 0)
121973606afdSDan Williams 		return dimm;
122073606afdSDan Williams 
122173606afdSDan Williams 	rc = kstrtol(buf, 0, &val);
122273606afdSDan Williams 	if (rc)
122373606afdSDan Williams 		return rc;
122473606afdSDan Williams 
122573606afdSDan Williams 	dimm_fail_cmd_flags[dimm] = val;
122673606afdSDan Williams 	return size;
122773606afdSDan Williams }
122873606afdSDan Williams static DEVICE_ATTR_RW(fail_cmd);
122973606afdSDan Williams 
123055c72ab6SDan Williams static ssize_t fail_cmd_code_show(struct device *dev, struct device_attribute *attr,
123155c72ab6SDan Williams 		char *buf)
123255c72ab6SDan Williams {
123355c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
123455c72ab6SDan Williams 
123555c72ab6SDan Williams 	if (dimm < 0)
123655c72ab6SDan Williams 		return dimm;
123755c72ab6SDan Williams 
123855c72ab6SDan Williams 	return sprintf(buf, "%d\n", dimm_fail_cmd_code[dimm]);
123955c72ab6SDan Williams }
124055c72ab6SDan Williams 
124155c72ab6SDan Williams static ssize_t fail_cmd_code_store(struct device *dev, struct device_attribute *attr,
124255c72ab6SDan Williams 		const char *buf, size_t size)
124355c72ab6SDan Williams {
124455c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
124555c72ab6SDan Williams 	unsigned long val;
124655c72ab6SDan Williams 	ssize_t rc;
124755c72ab6SDan Williams 
124855c72ab6SDan Williams 	if (dimm < 0)
124955c72ab6SDan Williams 		return dimm;
125055c72ab6SDan Williams 
125155c72ab6SDan Williams 	rc = kstrtol(buf, 0, &val);
125255c72ab6SDan Williams 	if (rc)
125355c72ab6SDan Williams 		return rc;
125455c72ab6SDan Williams 
125555c72ab6SDan Williams 	dimm_fail_cmd_code[dimm] = val;
125655c72ab6SDan Williams 	return size;
125755c72ab6SDan Williams }
125855c72ab6SDan Williams static DEVICE_ATTR_RW(fail_cmd_code);
125955c72ab6SDan Williams 
126073606afdSDan Williams static struct attribute *nfit_test_dimm_attributes[] = {
126173606afdSDan Williams 	&dev_attr_fail_cmd.attr,
126255c72ab6SDan Williams 	&dev_attr_fail_cmd_code.attr,
126373606afdSDan Williams 	&dev_attr_handle.attr,
126473606afdSDan Williams 	NULL,
126573606afdSDan Williams };
126673606afdSDan Williams 
126773606afdSDan Williams static struct attribute_group nfit_test_dimm_attribute_group = {
126873606afdSDan Williams 	.attrs = nfit_test_dimm_attributes,
126973606afdSDan Williams };
127073606afdSDan Williams 
127173606afdSDan Williams static const struct attribute_group *nfit_test_dimm_attribute_groups[] = {
127273606afdSDan Williams 	&nfit_test_dimm_attribute_group,
127373606afdSDan Williams 	NULL,
127473606afdSDan Williams };
127573606afdSDan Williams 
1276718fda67SDan Williams static int nfit_test_dimm_init(struct nfit_test *t)
1277718fda67SDan Williams {
1278718fda67SDan Williams 	int i;
1279718fda67SDan Williams 
1280718fda67SDan Williams 	if (devm_add_action_or_reset(&t->pdev.dev, put_dimms, t))
1281718fda67SDan Williams 		return -ENOMEM;
1282718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1283718fda67SDan Williams 		t->dimm_dev[i] = device_create_with_groups(nfit_test_dimm,
1284718fda67SDan Williams 				&t->pdev.dev, 0, NULL,
1285718fda67SDan Williams 				nfit_test_dimm_attribute_groups,
1286718fda67SDan Williams 				"test_dimm%d", i + t->dcr_idx);
1287718fda67SDan Williams 		if (!t->dimm_dev[i])
1288718fda67SDan Williams 			return -ENOMEM;
1289718fda67SDan Williams 	}
1290718fda67SDan Williams 	return 0;
1291718fda67SDan Williams }
1292718fda67SDan Williams 
1293ed07c433SDan Williams static void smart_init(struct nfit_test *t)
1294ed07c433SDan Williams {
1295ed07c433SDan Williams 	int i;
1296ed07c433SDan Williams 	const struct nd_intel_smart_threshold smart_t_data = {
1297ed07c433SDan Williams 		.alarm_control = ND_INTEL_SMART_SPARE_TRIP
1298ed07c433SDan Williams 			| ND_INTEL_SMART_TEMP_TRIP,
1299ed07c433SDan Williams 		.media_temperature = 40 * 16,
1300ed07c433SDan Williams 		.ctrl_temperature = 30 * 16,
1301ed07c433SDan Williams 		.spares = 5,
1302ed07c433SDan Williams 	};
1303ed07c433SDan Williams 	const struct nd_intel_smart smart_data = {
1304ed07c433SDan Williams 		.flags = ND_INTEL_SMART_HEALTH_VALID
1305ed07c433SDan Williams 			| ND_INTEL_SMART_SPARES_VALID
1306ed07c433SDan Williams 			| ND_INTEL_SMART_ALARM_VALID
1307ed07c433SDan Williams 			| ND_INTEL_SMART_USED_VALID
1308ed07c433SDan Williams 			| ND_INTEL_SMART_SHUTDOWN_VALID
1309ed07c433SDan Williams 			| ND_INTEL_SMART_MTEMP_VALID,
1310ed07c433SDan Williams 		.health = ND_INTEL_SMART_NON_CRITICAL_HEALTH,
1311ed07c433SDan Williams 		.media_temperature = 23 * 16,
1312f6adcca0SVishal Verma 		.ctrl_temperature = 25 * 16,
1313ed07c433SDan Williams 		.pmic_temperature = 40 * 16,
1314ed07c433SDan Williams 		.spares = 75,
1315ed07c433SDan Williams 		.alarm_flags = ND_INTEL_SMART_SPARE_TRIP
1316ed07c433SDan Williams 			| ND_INTEL_SMART_TEMP_TRIP,
1317ed07c433SDan Williams 		.ait_status = 1,
1318ed07c433SDan Williams 		.life_used = 5,
1319ed07c433SDan Williams 		.shutdown_state = 0,
1320ed07c433SDan Williams 		.vendor_size = 0,
1321ed07c433SDan Williams 		.shutdown_count = 100,
1322ed07c433SDan Williams 	};
1323ed07c433SDan Williams 
1324ed07c433SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1325ed07c433SDan Williams 		memcpy(&t->smart[i], &smart_data, sizeof(smart_data));
1326ed07c433SDan Williams 		memcpy(&t->smart_threshold[i], &smart_t_data,
1327ed07c433SDan Williams 				sizeof(smart_t_data));
1328ed07c433SDan Williams 	}
1329ed07c433SDan Williams }
1330ed07c433SDan Williams 
13316bc75619SDan Williams static int nfit_test0_alloc(struct nfit_test *t)
13326bc75619SDan Williams {
13336b577c9dSLinda Knippers 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * NUM_SPA
13346bc75619SDan Williams 			+ sizeof(struct acpi_nfit_memory_map) * NUM_MEM
13356bc75619SDan Williams 			+ sizeof(struct acpi_nfit_control_region) * NUM_DCR
13363b87356fSDan Williams 			+ offsetof(struct acpi_nfit_control_region,
13373b87356fSDan Williams 					window_size) * NUM_DCR
13389d27a87eSDan Williams 			+ sizeof(struct acpi_nfit_data_region) * NUM_BDW
133985d3fa02SDan Williams 			+ (sizeof(struct acpi_nfit_flush_address)
1340f81e1d35SDave Jiang 					+ sizeof(u64) * NUM_HINTS) * NUM_DCR
1341f81e1d35SDave Jiang 			+ sizeof(struct acpi_nfit_capabilities);
13426bc75619SDan Williams 	int i;
13436bc75619SDan Williams 
13446bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
13456bc75619SDan Williams 	if (!t->nfit_buf)
13466bc75619SDan Williams 		return -ENOMEM;
13476bc75619SDan Williams 	t->nfit_size = nfit_size;
13486bc75619SDan Williams 
1349ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[0]);
13506bc75619SDan Williams 	if (!t->spa_set[0])
13516bc75619SDan Williams 		return -ENOMEM;
13526bc75619SDan Williams 
1353ee8520feSDan Williams 	t->spa_set[1] = test_alloc(t, SPA1_SIZE, &t->spa_set_dma[1]);
13546bc75619SDan Williams 	if (!t->spa_set[1])
13556bc75619SDan Williams 		return -ENOMEM;
13566bc75619SDan Williams 
1357ee8520feSDan Williams 	t->spa_set[2] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[2]);
135820985164SVishal Verma 	if (!t->spa_set[2])
135920985164SVishal Verma 		return -ENOMEM;
136020985164SVishal Verma 
1361dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
13626bc75619SDan Williams 		t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
13636bc75619SDan Williams 		if (!t->dimm[i])
13646bc75619SDan Williams 			return -ENOMEM;
13656bc75619SDan Williams 
13666bc75619SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
13676bc75619SDan Williams 		if (!t->label[i])
13686bc75619SDan Williams 			return -ENOMEM;
13696bc75619SDan Williams 		sprintf(t->label[i], "label%d", i);
13709d27a87eSDan Williams 
13719d15ce9cSDan Williams 		t->flush[i] = test_alloc(t, max(PAGE_SIZE,
13729d15ce9cSDan Williams 					sizeof(u64) * NUM_HINTS),
137385d3fa02SDan Williams 				&t->flush_dma[i]);
13749d27a87eSDan Williams 		if (!t->flush[i])
13759d27a87eSDan Williams 			return -ENOMEM;
13766bc75619SDan Williams 	}
13776bc75619SDan Williams 
1378dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
13796bc75619SDan Williams 		t->dcr[i] = test_alloc(t, LABEL_SIZE, &t->dcr_dma[i]);
13806bc75619SDan Williams 		if (!t->dcr[i])
13816bc75619SDan Williams 			return -ENOMEM;
13826bc75619SDan Williams 	}
13836bc75619SDan Williams 
1384c14a868aSDan Williams 	t->_fit = test_alloc(t, sizeof(union acpi_object **), &t->_fit_dma);
1385c14a868aSDan Williams 	if (!t->_fit)
1386c14a868aSDan Williams 		return -ENOMEM;
1387c14a868aSDan Williams 
1388718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1389231bf117SDan Williams 		return -ENOMEM;
1390ed07c433SDan Williams 	smart_init(t);
1391f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
13926bc75619SDan Williams }
13936bc75619SDan Williams 
13946bc75619SDan Williams static int nfit_test1_alloc(struct nfit_test *t)
13956bc75619SDan Williams {
13967bfe97c7SDan Williams 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * 2
1397ac40b675SDan Williams 		+ sizeof(struct acpi_nfit_memory_map) * 2
1398ac40b675SDan Williams 		+ offsetof(struct acpi_nfit_control_region, window_size) * 2;
1399dafb1048SDan Williams 	int i;
14006bc75619SDan Williams 
14016bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
14026bc75619SDan Williams 	if (!t->nfit_buf)
14036bc75619SDan Williams 		return -ENOMEM;
14046bc75619SDan Williams 	t->nfit_size = nfit_size;
14056bc75619SDan Williams 
1406ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA2_SIZE, &t->spa_set_dma[0]);
14076bc75619SDan Williams 	if (!t->spa_set[0])
14086bc75619SDan Williams 		return -ENOMEM;
14096bc75619SDan Williams 
1410dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1411dafb1048SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
1412dafb1048SDan Williams 		if (!t->label[i])
1413dafb1048SDan Williams 			return -ENOMEM;
1414dafb1048SDan Williams 		sprintf(t->label[i], "label%d", i);
1415dafb1048SDan Williams 	}
1416dafb1048SDan Williams 
14177bfe97c7SDan Williams 	t->spa_set[1] = test_alloc(t, SPA_VCD_SIZE, &t->spa_set_dma[1]);
14187bfe97c7SDan Williams 	if (!t->spa_set[1])
14197bfe97c7SDan Williams 		return -ENOMEM;
14207bfe97c7SDan Williams 
1421718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1422718fda67SDan Williams 		return -ENOMEM;
1423ed07c433SDan Williams 	smart_init(t);
1424f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
14256bc75619SDan Williams }
14266bc75619SDan Williams 
14275dc68e55SDan Williams static void dcr_common_init(struct acpi_nfit_control_region *dcr)
14285dc68e55SDan Williams {
14295dc68e55SDan Williams 	dcr->vendor_id = 0xabcd;
14305dc68e55SDan Williams 	dcr->device_id = 0;
14315dc68e55SDan Williams 	dcr->revision_id = 1;
14325dc68e55SDan Williams 	dcr->valid_fields = 1;
14335dc68e55SDan Williams 	dcr->manufacturing_location = 0xa;
14345dc68e55SDan Williams 	dcr->manufacturing_date = cpu_to_be16(2016);
14355dc68e55SDan Williams }
14365dc68e55SDan Williams 
14376bc75619SDan Williams static void nfit_test0_setup(struct nfit_test *t)
14386bc75619SDan Williams {
143985d3fa02SDan Williams 	const int flush_hint_size = sizeof(struct acpi_nfit_flush_address)
144085d3fa02SDan Williams 		+ (sizeof(u64) * NUM_HINTS);
14416bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
14426bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
14436bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
14446bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
14456bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
14466bc75619SDan Williams 	struct acpi_nfit_data_region *bdw;
14479d27a87eSDan Williams 	struct acpi_nfit_flush_address *flush;
1448f81e1d35SDave Jiang 	struct acpi_nfit_capabilities *pcap;
1449d7d8464dSRoss Zwisler 	unsigned int offset = 0, i;
14506bc75619SDan Williams 
14516bc75619SDan Williams 	/*
14526bc75619SDan Williams 	 * spa0 (interleave first half of dimm0 and dimm1, note storage
14536bc75619SDan Williams 	 * does not actually alias the related block-data-window
14546bc75619SDan Williams 	 * regions)
14556bc75619SDan Williams 	 */
14566b577c9dSLinda Knippers 	spa = nfit_buf;
14576bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14586bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14596bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
14606bc75619SDan Williams 	spa->range_index = 0+1;
14616bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
14626bc75619SDan Williams 	spa->length = SPA0_SIZE;
1463d7d8464dSRoss Zwisler 	offset += spa->header.length;
14646bc75619SDan Williams 
14656bc75619SDan Williams 	/*
14666bc75619SDan Williams 	 * spa1 (interleave last half of the 4 DIMMS, note storage
14676bc75619SDan Williams 	 * does not actually alias the related block-data-window
14686bc75619SDan Williams 	 * regions)
14696bc75619SDan Williams 	 */
1470d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
14716bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14726bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14736bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
14746bc75619SDan Williams 	spa->range_index = 1+1;
14756bc75619SDan Williams 	spa->address = t->spa_set_dma[1];
14766bc75619SDan Williams 	spa->length = SPA1_SIZE;
1477d7d8464dSRoss Zwisler 	offset += spa->header.length;
14786bc75619SDan Williams 
14796bc75619SDan Williams 	/* spa2 (dcr0) dimm0 */
1480d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
14816bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14826bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14836bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
14846bc75619SDan Williams 	spa->range_index = 2+1;
14856bc75619SDan Williams 	spa->address = t->dcr_dma[0];
14866bc75619SDan Williams 	spa->length = DCR_SIZE;
1487d7d8464dSRoss Zwisler 	offset += spa->header.length;
14886bc75619SDan Williams 
14896bc75619SDan Williams 	/* spa3 (dcr1) dimm1 */
1490d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
14916bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14926bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14936bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
14946bc75619SDan Williams 	spa->range_index = 3+1;
14956bc75619SDan Williams 	spa->address = t->dcr_dma[1];
14966bc75619SDan Williams 	spa->length = DCR_SIZE;
1497d7d8464dSRoss Zwisler 	offset += spa->header.length;
14986bc75619SDan Williams 
14996bc75619SDan Williams 	/* spa4 (dcr2) dimm2 */
1500d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15016bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15026bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15036bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
15046bc75619SDan Williams 	spa->range_index = 4+1;
15056bc75619SDan Williams 	spa->address = t->dcr_dma[2];
15066bc75619SDan Williams 	spa->length = DCR_SIZE;
1507d7d8464dSRoss Zwisler 	offset += spa->header.length;
15086bc75619SDan Williams 
15096bc75619SDan Williams 	/* spa5 (dcr3) dimm3 */
1510d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15116bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15126bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15136bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
15146bc75619SDan Williams 	spa->range_index = 5+1;
15156bc75619SDan Williams 	spa->address = t->dcr_dma[3];
15166bc75619SDan Williams 	spa->length = DCR_SIZE;
1517d7d8464dSRoss Zwisler 	offset += spa->header.length;
15186bc75619SDan Williams 
15196bc75619SDan Williams 	/* spa6 (bdw for dcr0) dimm0 */
1520d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15216bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15226bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15236bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
15246bc75619SDan Williams 	spa->range_index = 6+1;
15256bc75619SDan Williams 	spa->address = t->dimm_dma[0];
15266bc75619SDan Williams 	spa->length = DIMM_SIZE;
1527d7d8464dSRoss Zwisler 	offset += spa->header.length;
15286bc75619SDan Williams 
15296bc75619SDan Williams 	/* spa7 (bdw for dcr1) dimm1 */
1530d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15316bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15326bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15336bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
15346bc75619SDan Williams 	spa->range_index = 7+1;
15356bc75619SDan Williams 	spa->address = t->dimm_dma[1];
15366bc75619SDan Williams 	spa->length = DIMM_SIZE;
1537d7d8464dSRoss Zwisler 	offset += spa->header.length;
15386bc75619SDan Williams 
15396bc75619SDan Williams 	/* spa8 (bdw for dcr2) dimm2 */
1540d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15416bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15426bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15436bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
15446bc75619SDan Williams 	spa->range_index = 8+1;
15456bc75619SDan Williams 	spa->address = t->dimm_dma[2];
15466bc75619SDan Williams 	spa->length = DIMM_SIZE;
1547d7d8464dSRoss Zwisler 	offset += spa->header.length;
15486bc75619SDan Williams 
15496bc75619SDan Williams 	/* spa9 (bdw for dcr3) dimm3 */
1550d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15516bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15526bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15536bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
15546bc75619SDan Williams 	spa->range_index = 9+1;
15556bc75619SDan Williams 	spa->address = t->dimm_dma[3];
15566bc75619SDan Williams 	spa->length = DIMM_SIZE;
1557d7d8464dSRoss Zwisler 	offset += spa->header.length;
15586bc75619SDan Williams 
15596bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
15606bc75619SDan Williams 	memdev = nfit_buf + offset;
15616bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
15626bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
15636bc75619SDan Williams 	memdev->device_handle = handle[0];
15646bc75619SDan Williams 	memdev->physical_id = 0;
15656bc75619SDan Williams 	memdev->region_id = 0;
15666bc75619SDan Williams 	memdev->range_index = 0+1;
15673b87356fSDan Williams 	memdev->region_index = 4+1;
15686bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1569df06a2d5SDan Williams 	memdev->region_offset = 1;
15706bc75619SDan Williams 	memdev->address = 0;
15716bc75619SDan Williams 	memdev->interleave_index = 0;
15726bc75619SDan Williams 	memdev->interleave_ways = 2;
1573d7d8464dSRoss Zwisler 	offset += memdev->header.length;
15746bc75619SDan Williams 
15756bc75619SDan Williams 	/* mem-region1 (spa0, dimm1) */
1576d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
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 = 0+1;
15833b87356fSDan Williams 	memdev->region_index = 5+1;
15846bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1585df06a2d5SDan Williams 	memdev->region_offset = (1 << 8);
15866bc75619SDan Williams 	memdev->address = 0;
15876bc75619SDan Williams 	memdev->interleave_index = 0;
15886bc75619SDan Williams 	memdev->interleave_ways = 2;
1589ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1590d7d8464dSRoss Zwisler 	offset += memdev->header.length;
15916bc75619SDan Williams 
15926bc75619SDan Williams 	/* mem-region2 (spa1, dimm0) */
1593d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
15946bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
15956bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
15966bc75619SDan Williams 	memdev->device_handle = handle[0];
15976bc75619SDan Williams 	memdev->physical_id = 0;
15986bc75619SDan Williams 	memdev->region_id = 1;
15996bc75619SDan Williams 	memdev->range_index = 1+1;
16003b87356fSDan Williams 	memdev->region_index = 4+1;
16016bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1602df06a2d5SDan Williams 	memdev->region_offset = (1 << 16);
16036bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
16046bc75619SDan Williams 	memdev->interleave_index = 0;
16056bc75619SDan Williams 	memdev->interleave_ways = 4;
1606ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1607d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16086bc75619SDan Williams 
16096bc75619SDan Williams 	/* mem-region3 (spa1, dimm1) */
1610d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16116bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16126bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16136bc75619SDan Williams 	memdev->device_handle = handle[1];
16146bc75619SDan Williams 	memdev->physical_id = 1;
16156bc75619SDan Williams 	memdev->region_id = 1;
16166bc75619SDan Williams 	memdev->range_index = 1+1;
16173b87356fSDan Williams 	memdev->region_index = 5+1;
16186bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1619df06a2d5SDan Williams 	memdev->region_offset = (1 << 24);
16206bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
16216bc75619SDan Williams 	memdev->interleave_index = 0;
16226bc75619SDan Williams 	memdev->interleave_ways = 4;
1623d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16246bc75619SDan Williams 
16256bc75619SDan Williams 	/* mem-region4 (spa1, dimm2) */
1626d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16276bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16286bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16296bc75619SDan Williams 	memdev->device_handle = handle[2];
16306bc75619SDan Williams 	memdev->physical_id = 2;
16316bc75619SDan Williams 	memdev->region_id = 0;
16326bc75619SDan Williams 	memdev->range_index = 1+1;
16333b87356fSDan Williams 	memdev->region_index = 6+1;
16346bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1635df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 32);
16366bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
16376bc75619SDan Williams 	memdev->interleave_index = 0;
16386bc75619SDan Williams 	memdev->interleave_ways = 4;
1639ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1640d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16416bc75619SDan Williams 
16426bc75619SDan Williams 	/* mem-region5 (spa1, dimm3) */
1643d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16446bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16456bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16466bc75619SDan Williams 	memdev->device_handle = handle[3];
16476bc75619SDan Williams 	memdev->physical_id = 3;
16486bc75619SDan Williams 	memdev->region_id = 0;
16496bc75619SDan Williams 	memdev->range_index = 1+1;
16503b87356fSDan Williams 	memdev->region_index = 7+1;
16516bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1652df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 40);
16536bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
16546bc75619SDan Williams 	memdev->interleave_index = 0;
16556bc75619SDan Williams 	memdev->interleave_ways = 4;
1656d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16576bc75619SDan Williams 
16586bc75619SDan Williams 	/* mem-region6 (spa/dcr0, dimm0) */
1659d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16606bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16616bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16626bc75619SDan Williams 	memdev->device_handle = handle[0];
16636bc75619SDan Williams 	memdev->physical_id = 0;
16646bc75619SDan Williams 	memdev->region_id = 0;
16656bc75619SDan Williams 	memdev->range_index = 2+1;
16666bc75619SDan Williams 	memdev->region_index = 0+1;
16676bc75619SDan Williams 	memdev->region_size = 0;
16686bc75619SDan Williams 	memdev->region_offset = 0;
16696bc75619SDan Williams 	memdev->address = 0;
16706bc75619SDan Williams 	memdev->interleave_index = 0;
16716bc75619SDan Williams 	memdev->interleave_ways = 1;
1672d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16736bc75619SDan Williams 
16746bc75619SDan Williams 	/* mem-region7 (spa/dcr1, dimm1) */
1675d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16766bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16776bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16786bc75619SDan Williams 	memdev->device_handle = handle[1];
16796bc75619SDan Williams 	memdev->physical_id = 1;
16806bc75619SDan Williams 	memdev->region_id = 0;
16816bc75619SDan Williams 	memdev->range_index = 3+1;
16826bc75619SDan Williams 	memdev->region_index = 1+1;
16836bc75619SDan Williams 	memdev->region_size = 0;
16846bc75619SDan Williams 	memdev->region_offset = 0;
16856bc75619SDan Williams 	memdev->address = 0;
16866bc75619SDan Williams 	memdev->interleave_index = 0;
16876bc75619SDan Williams 	memdev->interleave_ways = 1;
1688d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16896bc75619SDan Williams 
16906bc75619SDan Williams 	/* mem-region8 (spa/dcr2, dimm2) */
1691d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16926bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16936bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16946bc75619SDan Williams 	memdev->device_handle = handle[2];
16956bc75619SDan Williams 	memdev->physical_id = 2;
16966bc75619SDan Williams 	memdev->region_id = 0;
16976bc75619SDan Williams 	memdev->range_index = 4+1;
16986bc75619SDan Williams 	memdev->region_index = 2+1;
16996bc75619SDan Williams 	memdev->region_size = 0;
17006bc75619SDan Williams 	memdev->region_offset = 0;
17016bc75619SDan Williams 	memdev->address = 0;
17026bc75619SDan Williams 	memdev->interleave_index = 0;
17036bc75619SDan Williams 	memdev->interleave_ways = 1;
1704d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17056bc75619SDan Williams 
17066bc75619SDan Williams 	/* mem-region9 (spa/dcr3, dimm3) */
1707d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17086bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17096bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17106bc75619SDan Williams 	memdev->device_handle = handle[3];
17116bc75619SDan Williams 	memdev->physical_id = 3;
17126bc75619SDan Williams 	memdev->region_id = 0;
17136bc75619SDan Williams 	memdev->range_index = 5+1;
17146bc75619SDan Williams 	memdev->region_index = 3+1;
17156bc75619SDan Williams 	memdev->region_size = 0;
17166bc75619SDan Williams 	memdev->region_offset = 0;
17176bc75619SDan Williams 	memdev->address = 0;
17186bc75619SDan Williams 	memdev->interleave_index = 0;
17196bc75619SDan Williams 	memdev->interleave_ways = 1;
1720d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17216bc75619SDan Williams 
17226bc75619SDan Williams 	/* mem-region10 (spa/bdw0, dimm0) */
1723d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17246bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17256bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17266bc75619SDan Williams 	memdev->device_handle = handle[0];
17276bc75619SDan Williams 	memdev->physical_id = 0;
17286bc75619SDan Williams 	memdev->region_id = 0;
17296bc75619SDan Williams 	memdev->range_index = 6+1;
17306bc75619SDan Williams 	memdev->region_index = 0+1;
17316bc75619SDan Williams 	memdev->region_size = 0;
17326bc75619SDan Williams 	memdev->region_offset = 0;
17336bc75619SDan Williams 	memdev->address = 0;
17346bc75619SDan Williams 	memdev->interleave_index = 0;
17356bc75619SDan Williams 	memdev->interleave_ways = 1;
1736d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17376bc75619SDan Williams 
17386bc75619SDan Williams 	/* mem-region11 (spa/bdw1, dimm1) */
1739d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17406bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17416bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17426bc75619SDan Williams 	memdev->device_handle = handle[1];
17436bc75619SDan Williams 	memdev->physical_id = 1;
17446bc75619SDan Williams 	memdev->region_id = 0;
17456bc75619SDan Williams 	memdev->range_index = 7+1;
17466bc75619SDan Williams 	memdev->region_index = 1+1;
17476bc75619SDan Williams 	memdev->region_size = 0;
17486bc75619SDan Williams 	memdev->region_offset = 0;
17496bc75619SDan Williams 	memdev->address = 0;
17506bc75619SDan Williams 	memdev->interleave_index = 0;
17516bc75619SDan Williams 	memdev->interleave_ways = 1;
1752d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17536bc75619SDan Williams 
17546bc75619SDan Williams 	/* mem-region12 (spa/bdw2, dimm2) */
1755d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17566bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17576bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17586bc75619SDan Williams 	memdev->device_handle = handle[2];
17596bc75619SDan Williams 	memdev->physical_id = 2;
17606bc75619SDan Williams 	memdev->region_id = 0;
17616bc75619SDan Williams 	memdev->range_index = 8+1;
17626bc75619SDan Williams 	memdev->region_index = 2+1;
17636bc75619SDan Williams 	memdev->region_size = 0;
17646bc75619SDan Williams 	memdev->region_offset = 0;
17656bc75619SDan Williams 	memdev->address = 0;
17666bc75619SDan Williams 	memdev->interleave_index = 0;
17676bc75619SDan Williams 	memdev->interleave_ways = 1;
1768d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17696bc75619SDan Williams 
17706bc75619SDan Williams 	/* mem-region13 (spa/dcr3, dimm3) */
1771d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17726bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17736bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17746bc75619SDan Williams 	memdev->device_handle = handle[3];
17756bc75619SDan Williams 	memdev->physical_id = 3;
17766bc75619SDan Williams 	memdev->region_id = 0;
17776bc75619SDan Williams 	memdev->range_index = 9+1;
17786bc75619SDan Williams 	memdev->region_index = 3+1;
17796bc75619SDan Williams 	memdev->region_size = 0;
17806bc75619SDan Williams 	memdev->region_offset = 0;
17816bc75619SDan Williams 	memdev->address = 0;
17826bc75619SDan Williams 	memdev->interleave_index = 0;
17836bc75619SDan Williams 	memdev->interleave_ways = 1;
1784ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1785d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17866bc75619SDan Williams 
17873b87356fSDan Williams 	/* dcr-descriptor0: blk */
17886bc75619SDan Williams 	dcr = nfit_buf + offset;
17896bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1790d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
17916bc75619SDan Williams 	dcr->region_index = 0+1;
17925dc68e55SDan Williams 	dcr_common_init(dcr);
17936bc75619SDan Williams 	dcr->serial_number = ~handle[0];
1794be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
17956bc75619SDan Williams 	dcr->windows = 1;
17966bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
17976bc75619SDan Williams 	dcr->command_offset = 0;
17986bc75619SDan Williams 	dcr->command_size = 8;
17996bc75619SDan Williams 	dcr->status_offset = 8;
18006bc75619SDan Williams 	dcr->status_size = 4;
1801d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18026bc75619SDan Williams 
18033b87356fSDan Williams 	/* dcr-descriptor1: blk */
1804d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
18056bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1806d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
18076bc75619SDan Williams 	dcr->region_index = 1+1;
18085dc68e55SDan Williams 	dcr_common_init(dcr);
18096bc75619SDan Williams 	dcr->serial_number = ~handle[1];
1810be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
18116bc75619SDan Williams 	dcr->windows = 1;
18126bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
18136bc75619SDan Williams 	dcr->command_offset = 0;
18146bc75619SDan Williams 	dcr->command_size = 8;
18156bc75619SDan Williams 	dcr->status_offset = 8;
18166bc75619SDan Williams 	dcr->status_size = 4;
1817d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18186bc75619SDan Williams 
18193b87356fSDan Williams 	/* dcr-descriptor2: blk */
1820d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
18216bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1822d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
18236bc75619SDan Williams 	dcr->region_index = 2+1;
18245dc68e55SDan Williams 	dcr_common_init(dcr);
18256bc75619SDan Williams 	dcr->serial_number = ~handle[2];
1826be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
18276bc75619SDan Williams 	dcr->windows = 1;
18286bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
18296bc75619SDan Williams 	dcr->command_offset = 0;
18306bc75619SDan Williams 	dcr->command_size = 8;
18316bc75619SDan Williams 	dcr->status_offset = 8;
18326bc75619SDan Williams 	dcr->status_size = 4;
1833d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18346bc75619SDan Williams 
18353b87356fSDan Williams 	/* dcr-descriptor3: blk */
1836d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
18376bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1838d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
18396bc75619SDan Williams 	dcr->region_index = 3+1;
18405dc68e55SDan Williams 	dcr_common_init(dcr);
18416bc75619SDan Williams 	dcr->serial_number = ~handle[3];
1842be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
18436bc75619SDan Williams 	dcr->windows = 1;
18446bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
18456bc75619SDan Williams 	dcr->command_offset = 0;
18466bc75619SDan Williams 	dcr->command_size = 8;
18476bc75619SDan Williams 	dcr->status_offset = 8;
18486bc75619SDan Williams 	dcr->status_size = 4;
1849d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18506bc75619SDan Williams 
18513b87356fSDan Williams 	/* dcr-descriptor0: pmem */
18523b87356fSDan Williams 	dcr = nfit_buf + offset;
18533b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
18543b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
18553b87356fSDan Williams 			window_size);
18563b87356fSDan Williams 	dcr->region_index = 4+1;
18575dc68e55SDan Williams 	dcr_common_init(dcr);
18583b87356fSDan Williams 	dcr->serial_number = ~handle[0];
18593b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
18603b87356fSDan Williams 	dcr->windows = 0;
1861d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18623b87356fSDan Williams 
18633b87356fSDan Williams 	/* dcr-descriptor1: pmem */
1864d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
18653b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
18663b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
18673b87356fSDan Williams 			window_size);
18683b87356fSDan Williams 	dcr->region_index = 5+1;
18695dc68e55SDan Williams 	dcr_common_init(dcr);
18703b87356fSDan Williams 	dcr->serial_number = ~handle[1];
18713b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
18723b87356fSDan Williams 	dcr->windows = 0;
1873d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18743b87356fSDan Williams 
18753b87356fSDan Williams 	/* dcr-descriptor2: pmem */
1876d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
18773b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
18783b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
18793b87356fSDan Williams 			window_size);
18803b87356fSDan Williams 	dcr->region_index = 6+1;
18815dc68e55SDan Williams 	dcr_common_init(dcr);
18823b87356fSDan Williams 	dcr->serial_number = ~handle[2];
18833b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
18843b87356fSDan Williams 	dcr->windows = 0;
1885d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18863b87356fSDan Williams 
18873b87356fSDan Williams 	/* dcr-descriptor3: pmem */
1888d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
18893b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
18903b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
18913b87356fSDan Williams 			window_size);
18923b87356fSDan Williams 	dcr->region_index = 7+1;
18935dc68e55SDan Williams 	dcr_common_init(dcr);
18943b87356fSDan Williams 	dcr->serial_number = ~handle[3];
18953b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
18963b87356fSDan Williams 	dcr->windows = 0;
1897d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18983b87356fSDan Williams 
18996bc75619SDan Williams 	/* bdw0 (spa/dcr0, dimm0) */
19006bc75619SDan Williams 	bdw = nfit_buf + offset;
19016bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1902d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
19036bc75619SDan Williams 	bdw->region_index = 0+1;
19046bc75619SDan Williams 	bdw->windows = 1;
19056bc75619SDan Williams 	bdw->offset = 0;
19066bc75619SDan Williams 	bdw->size = BDW_SIZE;
19076bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
19086bc75619SDan Williams 	bdw->start_address = 0;
1909d7d8464dSRoss Zwisler 	offset += bdw->header.length;
19106bc75619SDan Williams 
19116bc75619SDan Williams 	/* bdw1 (spa/dcr1, dimm1) */
1912d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
19136bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1914d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
19156bc75619SDan Williams 	bdw->region_index = 1+1;
19166bc75619SDan Williams 	bdw->windows = 1;
19176bc75619SDan Williams 	bdw->offset = 0;
19186bc75619SDan Williams 	bdw->size = BDW_SIZE;
19196bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
19206bc75619SDan Williams 	bdw->start_address = 0;
1921d7d8464dSRoss Zwisler 	offset += bdw->header.length;
19226bc75619SDan Williams 
19236bc75619SDan Williams 	/* bdw2 (spa/dcr2, dimm2) */
1924d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
19256bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1926d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
19276bc75619SDan Williams 	bdw->region_index = 2+1;
19286bc75619SDan Williams 	bdw->windows = 1;
19296bc75619SDan Williams 	bdw->offset = 0;
19306bc75619SDan Williams 	bdw->size = BDW_SIZE;
19316bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
19326bc75619SDan Williams 	bdw->start_address = 0;
1933d7d8464dSRoss Zwisler 	offset += bdw->header.length;
19346bc75619SDan Williams 
19356bc75619SDan Williams 	/* bdw3 (spa/dcr3, dimm3) */
1936d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
19376bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1938d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
19396bc75619SDan Williams 	bdw->region_index = 3+1;
19406bc75619SDan Williams 	bdw->windows = 1;
19416bc75619SDan Williams 	bdw->offset = 0;
19426bc75619SDan Williams 	bdw->size = BDW_SIZE;
19436bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
19446bc75619SDan Williams 	bdw->start_address = 0;
1945d7d8464dSRoss Zwisler 	offset += bdw->header.length;
19466bc75619SDan Williams 
19479d27a87eSDan Williams 	/* flush0 (dimm0) */
19489d27a87eSDan Williams 	flush = nfit_buf + offset;
19499d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
195085d3fa02SDan Williams 	flush->header.length = flush_hint_size;
19519d27a87eSDan Williams 	flush->device_handle = handle[0];
195285d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
195385d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
195485d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[0] + i * sizeof(u64);
1955d7d8464dSRoss Zwisler 	offset += flush->header.length;
19569d27a87eSDan Williams 
19579d27a87eSDan Williams 	/* flush1 (dimm1) */
1958d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
19599d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
196085d3fa02SDan Williams 	flush->header.length = flush_hint_size;
19619d27a87eSDan Williams 	flush->device_handle = handle[1];
196285d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
196385d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
196485d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[1] + i * sizeof(u64);
1965d7d8464dSRoss Zwisler 	offset += flush->header.length;
19669d27a87eSDan Williams 
19679d27a87eSDan Williams 	/* flush2 (dimm2) */
1968d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
19699d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
197085d3fa02SDan Williams 	flush->header.length = flush_hint_size;
19719d27a87eSDan Williams 	flush->device_handle = handle[2];
197285d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
197385d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
197485d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[2] + i * sizeof(u64);
1975d7d8464dSRoss Zwisler 	offset += flush->header.length;
19769d27a87eSDan Williams 
19779d27a87eSDan Williams 	/* flush3 (dimm3) */
1978d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
19799d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
198085d3fa02SDan Williams 	flush->header.length = flush_hint_size;
19819d27a87eSDan Williams 	flush->device_handle = handle[3];
198285d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
198385d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
198485d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[3] + i * sizeof(u64);
1985d7d8464dSRoss Zwisler 	offset += flush->header.length;
19869d27a87eSDan Williams 
1987f81e1d35SDave Jiang 	/* platform capabilities */
1988d7d8464dSRoss Zwisler 	pcap = nfit_buf + offset;
1989f81e1d35SDave Jiang 	pcap->header.type = ACPI_NFIT_TYPE_CAPABILITIES;
1990f81e1d35SDave Jiang 	pcap->header.length = sizeof(*pcap);
1991f81e1d35SDave Jiang 	pcap->highest_capability = 1;
1992f81e1d35SDave Jiang 	pcap->capabilities = ACPI_NFIT_CAPABILITY_CACHE_FLUSH |
1993f81e1d35SDave Jiang 		ACPI_NFIT_CAPABILITY_MEM_FLUSH;
1994d7d8464dSRoss Zwisler 	offset += pcap->header.length;
1995f81e1d35SDave Jiang 
199620985164SVishal Verma 	if (t->setup_hotplug) {
19973b87356fSDan Williams 		/* dcr-descriptor4: blk */
199820985164SVishal Verma 		dcr = nfit_buf + offset;
199920985164SVishal Verma 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2000d7d8464dSRoss Zwisler 		dcr->header.length = sizeof(*dcr);
20013b87356fSDan Williams 		dcr->region_index = 8+1;
20025dc68e55SDan Williams 		dcr_common_init(dcr);
200320985164SVishal Verma 		dcr->serial_number = ~handle[4];
2004be26f9aeSDan Williams 		dcr->code = NFIT_FIC_BLK;
200520985164SVishal Verma 		dcr->windows = 1;
200620985164SVishal Verma 		dcr->window_size = DCR_SIZE;
200720985164SVishal Verma 		dcr->command_offset = 0;
200820985164SVishal Verma 		dcr->command_size = 8;
200920985164SVishal Verma 		dcr->status_offset = 8;
201020985164SVishal Verma 		dcr->status_size = 4;
2011d7d8464dSRoss Zwisler 		offset += dcr->header.length;
201220985164SVishal Verma 
20133b87356fSDan Williams 		/* dcr-descriptor4: pmem */
20143b87356fSDan Williams 		dcr = nfit_buf + offset;
20153b87356fSDan Williams 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
20163b87356fSDan Williams 		dcr->header.length = offsetof(struct acpi_nfit_control_region,
20173b87356fSDan Williams 				window_size);
20183b87356fSDan Williams 		dcr->region_index = 9+1;
20195dc68e55SDan Williams 		dcr_common_init(dcr);
20203b87356fSDan Williams 		dcr->serial_number = ~handle[4];
20213b87356fSDan Williams 		dcr->code = NFIT_FIC_BYTEN;
20223b87356fSDan Williams 		dcr->windows = 0;
2023d7d8464dSRoss Zwisler 		offset += dcr->header.length;
20243b87356fSDan Williams 
202520985164SVishal Verma 		/* bdw4 (spa/dcr4, dimm4) */
202620985164SVishal Verma 		bdw = nfit_buf + offset;
202720985164SVishal Verma 		bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2028d7d8464dSRoss Zwisler 		bdw->header.length = sizeof(*bdw);
20293b87356fSDan Williams 		bdw->region_index = 8+1;
203020985164SVishal Verma 		bdw->windows = 1;
203120985164SVishal Verma 		bdw->offset = 0;
203220985164SVishal Verma 		bdw->size = BDW_SIZE;
203320985164SVishal Verma 		bdw->capacity = DIMM_SIZE;
203420985164SVishal Verma 		bdw->start_address = 0;
2035d7d8464dSRoss Zwisler 		offset += bdw->header.length;
203620985164SVishal Verma 
203720985164SVishal Verma 		/* spa10 (dcr4) dimm4 */
203820985164SVishal Verma 		spa = nfit_buf + offset;
203920985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
204020985164SVishal Verma 		spa->header.length = sizeof(*spa);
204120985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
204220985164SVishal Verma 		spa->range_index = 10+1;
204320985164SVishal Verma 		spa->address = t->dcr_dma[4];
204420985164SVishal Verma 		spa->length = DCR_SIZE;
2045d7d8464dSRoss Zwisler 		offset += spa->header.length;
204620985164SVishal Verma 
204720985164SVishal Verma 		/*
204820985164SVishal Verma 		 * spa11 (single-dimm interleave for hotplug, note storage
204920985164SVishal Verma 		 * does not actually alias the related block-data-window
205020985164SVishal Verma 		 * regions)
205120985164SVishal Verma 		 */
2052d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
205320985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
205420985164SVishal Verma 		spa->header.length = sizeof(*spa);
205520985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
205620985164SVishal Verma 		spa->range_index = 11+1;
205720985164SVishal Verma 		spa->address = t->spa_set_dma[2];
205820985164SVishal Verma 		spa->length = SPA0_SIZE;
2059d7d8464dSRoss Zwisler 		offset += spa->header.length;
206020985164SVishal Verma 
206120985164SVishal Verma 		/* spa12 (bdw for dcr4) dimm4 */
2062d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
206320985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
206420985164SVishal Verma 		spa->header.length = sizeof(*spa);
206520985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
206620985164SVishal Verma 		spa->range_index = 12+1;
206720985164SVishal Verma 		spa->address = t->dimm_dma[4];
206820985164SVishal Verma 		spa->length = DIMM_SIZE;
2069d7d8464dSRoss Zwisler 		offset += spa->header.length;
207020985164SVishal Verma 
207120985164SVishal Verma 		/* mem-region14 (spa/dcr4, dimm4) */
207220985164SVishal Verma 		memdev = nfit_buf + offset;
207320985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
207420985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
207520985164SVishal Verma 		memdev->device_handle = handle[4];
207620985164SVishal Verma 		memdev->physical_id = 4;
207720985164SVishal Verma 		memdev->region_id = 0;
207820985164SVishal Verma 		memdev->range_index = 10+1;
20793b87356fSDan Williams 		memdev->region_index = 8+1;
208020985164SVishal Verma 		memdev->region_size = 0;
208120985164SVishal Verma 		memdev->region_offset = 0;
208220985164SVishal Verma 		memdev->address = 0;
208320985164SVishal Verma 		memdev->interleave_index = 0;
208420985164SVishal Verma 		memdev->interleave_ways = 1;
2085d7d8464dSRoss Zwisler 		offset += memdev->header.length;
208620985164SVishal Verma 
2087d7d8464dSRoss Zwisler 		/* mem-region15 (spa11, dimm4) */
2088d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
208920985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
209020985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
209120985164SVishal Verma 		memdev->device_handle = handle[4];
209220985164SVishal Verma 		memdev->physical_id = 4;
209320985164SVishal Verma 		memdev->region_id = 0;
209420985164SVishal Verma 		memdev->range_index = 11+1;
20953b87356fSDan Williams 		memdev->region_index = 9+1;
209620985164SVishal Verma 		memdev->region_size = SPA0_SIZE;
2097df06a2d5SDan Williams 		memdev->region_offset = (1ULL << 48);
209820985164SVishal Verma 		memdev->address = 0;
209920985164SVishal Verma 		memdev->interleave_index = 0;
210020985164SVishal Verma 		memdev->interleave_ways = 1;
2101ac40b675SDan Williams 		memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2102d7d8464dSRoss Zwisler 		offset += memdev->header.length;
210320985164SVishal Verma 
21043b87356fSDan Williams 		/* mem-region16 (spa/bdw4, dimm4) */
2105d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
210620985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
210720985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
210820985164SVishal Verma 		memdev->device_handle = handle[4];
210920985164SVishal Verma 		memdev->physical_id = 4;
211020985164SVishal Verma 		memdev->region_id = 0;
211120985164SVishal Verma 		memdev->range_index = 12+1;
21123b87356fSDan Williams 		memdev->region_index = 8+1;
211320985164SVishal Verma 		memdev->region_size = 0;
211420985164SVishal Verma 		memdev->region_offset = 0;
211520985164SVishal Verma 		memdev->address = 0;
211620985164SVishal Verma 		memdev->interleave_index = 0;
211720985164SVishal Verma 		memdev->interleave_ways = 1;
2118d7d8464dSRoss Zwisler 		offset += memdev->header.length;
211920985164SVishal Verma 
212020985164SVishal Verma 		/* flush3 (dimm4) */
212120985164SVishal Verma 		flush = nfit_buf + offset;
212220985164SVishal Verma 		flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
212385d3fa02SDan Williams 		flush->header.length = flush_hint_size;
212420985164SVishal Verma 		flush->device_handle = handle[4];
212585d3fa02SDan Williams 		flush->hint_count = NUM_HINTS;
212685d3fa02SDan Williams 		for (i = 0; i < NUM_HINTS; i++)
212785d3fa02SDan Williams 			flush->hint_address[i] = t->flush_dma[4]
212885d3fa02SDan Williams 				+ i * sizeof(u64);
2129d7d8464dSRoss Zwisler 		offset += flush->header.length;
21309741a559SRoss Zwisler 
21319741a559SRoss Zwisler 		/* sanity check to make sure we've filled the buffer */
21329741a559SRoss Zwisler 		WARN_ON(offset != t->nfit_size);
213320985164SVishal Verma 	}
213420985164SVishal Verma 
21351526f9e2SRoss Zwisler 	t->nfit_filled = offset;
21361526f9e2SRoss Zwisler 
21379fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
21389fb1a190SDave Jiang 			SPA0_SIZE);
2139f471f1a7SDan Williams 
21406bc75619SDan Williams 	acpi_desc = &t->acpi_desc;
2141e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
2142e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2143e3654ecaSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2144ed07c433SDan Williams 	set_bit(ND_INTEL_SMART, &acpi_desc->dimm_cmd_force_en);
2145ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
2146ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_SET_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
21474cf260fcSVishal Verma 	set_bit(ND_INTEL_SMART_INJECT, &acpi_desc->dimm_cmd_force_en);
2148e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2149e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2150e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2151e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
215210246dc8SYasunori Goto 	set_bit(ND_CMD_CALL, &acpi_desc->bus_cmd_force_en);
215310246dc8SYasunori Goto 	set_bit(NFIT_CMD_TRANSLATE_SPA, &acpi_desc->bus_nfit_cmd_force_en);
21549fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_SET, &acpi_desc->bus_nfit_cmd_force_en);
21559fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_CLEAR, &acpi_desc->bus_nfit_cmd_force_en);
21569fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_GET, &acpi_desc->bus_nfit_cmd_force_en);
2157bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_GET_INFO, &acpi_desc->dimm_cmd_force_en);
2158bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_START_UPDATE, &acpi_desc->dimm_cmd_force_en);
2159bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_SEND_DATA, &acpi_desc->dimm_cmd_force_en);
2160bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_UPDATE, &acpi_desc->dimm_cmd_force_en);
2161bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_QUERY, &acpi_desc->dimm_cmd_force_en);
2162674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
21636bc75619SDan Williams }
21646bc75619SDan Williams 
21656bc75619SDan Williams static void nfit_test1_setup(struct nfit_test *t)
21666bc75619SDan Williams {
21676b577c9dSLinda Knippers 	size_t offset;
21686bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
21696bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
21706bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
21716bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
2172d26f73f0SDan Williams 	struct acpi_nfit_desc *acpi_desc;
21736bc75619SDan Williams 
21746b577c9dSLinda Knippers 	offset = 0;
21756bc75619SDan Williams 	/* spa0 (flat range with no bdw aliasing) */
21766bc75619SDan Williams 	spa = nfit_buf + offset;
21776bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
21786bc75619SDan Williams 	spa->header.length = sizeof(*spa);
21796bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
21806bc75619SDan Williams 	spa->range_index = 0+1;
21816bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
21826bc75619SDan Williams 	spa->length = SPA2_SIZE;
2183d7d8464dSRoss Zwisler 	offset += spa->header.length;
21846bc75619SDan Williams 
21857bfe97c7SDan Williams 	/* virtual cd region */
2186d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
21877bfe97c7SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
21887bfe97c7SDan Williams 	spa->header.length = sizeof(*spa);
21897bfe97c7SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_VCD), 16);
21907bfe97c7SDan Williams 	spa->range_index = 0;
21917bfe97c7SDan Williams 	spa->address = t->spa_set_dma[1];
21927bfe97c7SDan Williams 	spa->length = SPA_VCD_SIZE;
2193d7d8464dSRoss Zwisler 	offset += spa->header.length;
21947bfe97c7SDan Williams 
21956bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
21966bc75619SDan Williams 	memdev = nfit_buf + offset;
21976bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21986bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
2199dafb1048SDan Williams 	memdev->device_handle = handle[5];
22006bc75619SDan Williams 	memdev->physical_id = 0;
22016bc75619SDan Williams 	memdev->region_id = 0;
22026bc75619SDan Williams 	memdev->range_index = 0+1;
22036bc75619SDan Williams 	memdev->region_index = 0+1;
22046bc75619SDan Williams 	memdev->region_size = SPA2_SIZE;
22056bc75619SDan Williams 	memdev->region_offset = 0;
22066bc75619SDan Williams 	memdev->address = 0;
22076bc75619SDan Williams 	memdev->interleave_index = 0;
22086bc75619SDan Williams 	memdev->interleave_ways = 1;
220958138820SDan Williams 	memdev->flags = ACPI_NFIT_MEM_SAVE_FAILED | ACPI_NFIT_MEM_RESTORE_FAILED
221058138820SDan Williams 		| ACPI_NFIT_MEM_FLUSH_FAILED | ACPI_NFIT_MEM_HEALTH_OBSERVED
2211f4295796SDan Williams 		| ACPI_NFIT_MEM_NOT_ARMED;
2212d7d8464dSRoss Zwisler 	offset += memdev->header.length;
22136bc75619SDan Williams 
22146bc75619SDan Williams 	/* dcr-descriptor0 */
22156bc75619SDan Williams 	dcr = nfit_buf + offset;
22166bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22173b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22183b87356fSDan Williams 			window_size);
22196bc75619SDan Williams 	dcr->region_index = 0+1;
22205dc68e55SDan Williams 	dcr_common_init(dcr);
2221dafb1048SDan Williams 	dcr->serial_number = ~handle[5];
2222be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BYTE;
22236bc75619SDan Williams 	dcr->windows = 0;
2224ac40b675SDan Williams 	offset += dcr->header.length;
2225d7d8464dSRoss Zwisler 
2226ac40b675SDan Williams 	memdev = nfit_buf + offset;
2227ac40b675SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
2228ac40b675SDan Williams 	memdev->header.length = sizeof(*memdev);
2229ac40b675SDan Williams 	memdev->device_handle = handle[6];
2230ac40b675SDan Williams 	memdev->physical_id = 0;
2231ac40b675SDan Williams 	memdev->region_id = 0;
2232ac40b675SDan Williams 	memdev->range_index = 0;
2233ac40b675SDan Williams 	memdev->region_index = 0+2;
2234ac40b675SDan Williams 	memdev->region_size = SPA2_SIZE;
2235ac40b675SDan Williams 	memdev->region_offset = 0;
2236ac40b675SDan Williams 	memdev->address = 0;
2237ac40b675SDan Williams 	memdev->interleave_index = 0;
2238ac40b675SDan Williams 	memdev->interleave_ways = 1;
2239ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_MAP_FAILED;
2240d7d8464dSRoss Zwisler 	offset += memdev->header.length;
2241ac40b675SDan Williams 
2242ac40b675SDan Williams 	/* dcr-descriptor1 */
2243ac40b675SDan Williams 	dcr = nfit_buf + offset;
2244ac40b675SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2245ac40b675SDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
2246ac40b675SDan Williams 			window_size);
2247ac40b675SDan Williams 	dcr->region_index = 0+2;
2248ac40b675SDan Williams 	dcr_common_init(dcr);
2249ac40b675SDan Williams 	dcr->serial_number = ~handle[6];
2250ac40b675SDan Williams 	dcr->code = NFIT_FIC_BYTE;
2251ac40b675SDan Williams 	dcr->windows = 0;
2252d7d8464dSRoss Zwisler 	offset += dcr->header.length;
2253ac40b675SDan Williams 
22549741a559SRoss Zwisler 	/* sanity check to make sure we've filled the buffer */
22559741a559SRoss Zwisler 	WARN_ON(offset != t->nfit_size);
22569741a559SRoss Zwisler 
22571526f9e2SRoss Zwisler 	t->nfit_filled = offset;
22581526f9e2SRoss Zwisler 
22599fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
22609fb1a190SDave Jiang 			SPA2_SIZE);
2261f471f1a7SDan Williams 
2262d26f73f0SDan Williams 	acpi_desc = &t->acpi_desc;
2263e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2264e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2265e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2266e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
2267674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
22686bc75619SDan Williams }
22696bc75619SDan Williams 
22706bc75619SDan Williams static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
22716bc75619SDan Williams 		void *iobuf, u64 len, int rw)
22726bc75619SDan Williams {
22736bc75619SDan Williams 	struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
22746bc75619SDan Williams 	struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
22756bc75619SDan Williams 	struct nd_region *nd_region = &ndbr->nd_region;
22766bc75619SDan Williams 	unsigned int lane;
22776bc75619SDan Williams 
22786bc75619SDan Williams 	lane = nd_region_acquire_lane(nd_region);
22796bc75619SDan Williams 	if (rw)
228067a3e8feSRoss Zwisler 		memcpy(mmio->addr.base + dpa, iobuf, len);
228167a3e8feSRoss Zwisler 	else {
228267a3e8feSRoss Zwisler 		memcpy(iobuf, mmio->addr.base + dpa, len);
228367a3e8feSRoss Zwisler 
22845deb67f7SRobin Murphy 		/* give us some some coverage of the arch_invalidate_pmem() API */
22855deb67f7SRobin Murphy 		arch_invalidate_pmem(mmio->addr.base + dpa, len);
228667a3e8feSRoss Zwisler 	}
22876bc75619SDan Williams 	nd_region_release_lane(nd_region, lane);
22886bc75619SDan Williams 
22896bc75619SDan Williams 	return 0;
22906bc75619SDan Williams }
22916bc75619SDan Williams 
2292a7de92daSDan Williams static unsigned long nfit_ctl_handle;
2293a7de92daSDan Williams 
2294a7de92daSDan Williams union acpi_object *result;
2295a7de92daSDan Williams 
2296a7de92daSDan Williams static union acpi_object *nfit_test_evaluate_dsm(acpi_handle handle,
229794116f81SAndy Shevchenko 		const guid_t *guid, u64 rev, u64 func, union acpi_object *argv4)
2298a7de92daSDan Williams {
2299a7de92daSDan Williams 	if (handle != &nfit_ctl_handle)
2300a7de92daSDan Williams 		return ERR_PTR(-ENXIO);
2301a7de92daSDan Williams 
2302a7de92daSDan Williams 	return result;
2303a7de92daSDan Williams }
2304a7de92daSDan Williams 
2305a7de92daSDan Williams static int setup_result(void *buf, size_t size)
2306a7de92daSDan Williams {
2307a7de92daSDan Williams 	result = kmalloc(sizeof(union acpi_object) + size, GFP_KERNEL);
2308a7de92daSDan Williams 	if (!result)
2309a7de92daSDan Williams 		return -ENOMEM;
2310a7de92daSDan Williams 	result->package.type = ACPI_TYPE_BUFFER,
2311a7de92daSDan Williams 	result->buffer.pointer = (void *) (result + 1);
2312a7de92daSDan Williams 	result->buffer.length = size;
2313a7de92daSDan Williams 	memcpy(result->buffer.pointer, buf, size);
2314a7de92daSDan Williams 	memset(buf, 0, size);
2315a7de92daSDan Williams 	return 0;
2316a7de92daSDan Williams }
2317a7de92daSDan Williams 
2318a7de92daSDan Williams static int nfit_ctl_test(struct device *dev)
2319a7de92daSDan Williams {
2320a7de92daSDan Williams 	int rc, cmd_rc;
2321a7de92daSDan Williams 	struct nvdimm *nvdimm;
2322a7de92daSDan Williams 	struct acpi_device *adev;
2323a7de92daSDan Williams 	struct nfit_mem *nfit_mem;
2324a7de92daSDan Williams 	struct nd_ars_record *record;
2325a7de92daSDan Williams 	struct acpi_nfit_desc *acpi_desc;
2326a7de92daSDan Williams 	const u64 test_val = 0x0123456789abcdefULL;
2327a7de92daSDan Williams 	unsigned long mask, cmd_size, offset;
2328a7de92daSDan Williams 	union {
2329a7de92daSDan Williams 		struct nd_cmd_get_config_size cfg_size;
2330fb2a1748SDan Williams 		struct nd_cmd_clear_error clear_err;
2331a7de92daSDan Williams 		struct nd_cmd_ars_status ars_stat;
2332a7de92daSDan Williams 		struct nd_cmd_ars_cap ars_cap;
2333a7de92daSDan Williams 		char buf[sizeof(struct nd_cmd_ars_status)
2334a7de92daSDan Williams 			+ sizeof(struct nd_ars_record)];
2335a7de92daSDan Williams 	} cmds;
2336a7de92daSDan Williams 
2337a7de92daSDan Williams 	adev = devm_kzalloc(dev, sizeof(*adev), GFP_KERNEL);
2338a7de92daSDan Williams 	if (!adev)
2339a7de92daSDan Williams 		return -ENOMEM;
2340a7de92daSDan Williams 	*adev = (struct acpi_device) {
2341a7de92daSDan Williams 		.handle = &nfit_ctl_handle,
2342a7de92daSDan Williams 		.dev = {
2343a7de92daSDan Williams 			.init_name = "test-adev",
2344a7de92daSDan Williams 		},
2345a7de92daSDan Williams 	};
2346a7de92daSDan Williams 
2347a7de92daSDan Williams 	acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2348a7de92daSDan Williams 	if (!acpi_desc)
2349a7de92daSDan Williams 		return -ENOMEM;
2350a7de92daSDan Williams 	*acpi_desc = (struct acpi_nfit_desc) {
2351a7de92daSDan Williams 		.nd_desc = {
2352a7de92daSDan Williams 			.cmd_mask = 1UL << ND_CMD_ARS_CAP
2353a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_START
2354a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_STATUS
235510246dc8SYasunori Goto 				| 1UL << ND_CMD_CLEAR_ERROR
235610246dc8SYasunori Goto 				| 1UL << ND_CMD_CALL,
2357a7de92daSDan Williams 			.module = THIS_MODULE,
2358a7de92daSDan Williams 			.provider_name = "ACPI.NFIT",
2359a7de92daSDan Williams 			.ndctl = acpi_nfit_ctl,
23609fb1a190SDave Jiang 			.bus_dsm_mask = 1UL << NFIT_CMD_TRANSLATE_SPA
23619fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_SET
23629fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_CLEAR
23639fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_GET,
2364a7de92daSDan Williams 		},
2365a7de92daSDan Williams 		.dev = &adev->dev,
2366a7de92daSDan Williams 	};
2367a7de92daSDan Williams 
2368a7de92daSDan Williams 	nfit_mem = devm_kzalloc(dev, sizeof(*nfit_mem), GFP_KERNEL);
2369a7de92daSDan Williams 	if (!nfit_mem)
2370a7de92daSDan Williams 		return -ENOMEM;
2371a7de92daSDan Williams 
2372a7de92daSDan Williams 	mask = 1UL << ND_CMD_SMART | 1UL << ND_CMD_SMART_THRESHOLD
2373a7de92daSDan Williams 		| 1UL << ND_CMD_DIMM_FLAGS | 1UL << ND_CMD_GET_CONFIG_SIZE
2374a7de92daSDan Williams 		| 1UL << ND_CMD_GET_CONFIG_DATA | 1UL << ND_CMD_SET_CONFIG_DATA
2375a7de92daSDan Williams 		| 1UL << ND_CMD_VENDOR;
2376a7de92daSDan Williams 	*nfit_mem = (struct nfit_mem) {
2377a7de92daSDan Williams 		.adev = adev,
2378a7de92daSDan Williams 		.family = NVDIMM_FAMILY_INTEL,
2379a7de92daSDan Williams 		.dsm_mask = mask,
2380a7de92daSDan Williams 	};
2381a7de92daSDan Williams 
2382a7de92daSDan Williams 	nvdimm = devm_kzalloc(dev, sizeof(*nvdimm), GFP_KERNEL);
2383a7de92daSDan Williams 	if (!nvdimm)
2384a7de92daSDan Williams 		return -ENOMEM;
2385a7de92daSDan Williams 	*nvdimm = (struct nvdimm) {
2386a7de92daSDan Williams 		.provider_data = nfit_mem,
2387a7de92daSDan Williams 		.cmd_mask = mask,
2388a7de92daSDan Williams 		.dev = {
2389a7de92daSDan Williams 			.init_name = "test-dimm",
2390a7de92daSDan Williams 		},
2391a7de92daSDan Williams 	};
2392a7de92daSDan Williams 
2393a7de92daSDan Williams 
2394a7de92daSDan Williams 	/* basic checkout of a typical 'get config size' command */
2395a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2396a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2397a7de92daSDan Williams 		.status = 0,
2398a7de92daSDan Williams 		.config_size = SZ_128K,
2399a7de92daSDan Williams 		.max_xfer = SZ_4K,
2400a7de92daSDan Williams 	};
2401a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2402a7de92daSDan Williams 	if (rc)
2403a7de92daSDan Williams 		return rc;
2404a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2405a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2406a7de92daSDan Williams 
2407a7de92daSDan Williams 	if (rc < 0 || cmd_rc || cmds.cfg_size.status != 0
2408a7de92daSDan Williams 			|| cmds.cfg_size.config_size != SZ_128K
2409a7de92daSDan Williams 			|| cmds.cfg_size.max_xfer != SZ_4K) {
2410a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2411a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2412a7de92daSDan Williams 		return -EIO;
2413a7de92daSDan Williams 	}
2414a7de92daSDan Williams 
2415a7de92daSDan Williams 
2416a7de92daSDan Williams 	/* test ars_status with zero output */
2417a7de92daSDan Williams 	cmd_size = offsetof(struct nd_cmd_ars_status, address);
2418a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2419a7de92daSDan Williams 		.out_length = 0,
2420a7de92daSDan Williams 	};
2421a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2422a7de92daSDan Williams 	if (rc)
2423a7de92daSDan Williams 		return rc;
2424a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2425a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2426a7de92daSDan Williams 
2427a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2428a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2429a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2430a7de92daSDan Williams 		return -EIO;
2431a7de92daSDan Williams 	}
2432a7de92daSDan Williams 
2433a7de92daSDan Williams 
2434a7de92daSDan Williams 	/* test ars_cap with benign extended status */
2435a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_cap);
2436a7de92daSDan Williams 	cmds.ars_cap = (struct nd_cmd_ars_cap) {
2437a7de92daSDan Williams 		.status = ND_ARS_PERSISTENT << 16,
2438a7de92daSDan Williams 	};
2439a7de92daSDan Williams 	offset = offsetof(struct nd_cmd_ars_cap, status);
2440a7de92daSDan Williams 	rc = setup_result(cmds.buf + offset, cmd_size - offset);
2441a7de92daSDan Williams 	if (rc)
2442a7de92daSDan Williams 		return rc;
2443a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_CAP,
2444a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2445a7de92daSDan Williams 
2446a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2447a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2448a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2449a7de92daSDan Williams 		return -EIO;
2450a7de92daSDan Williams 	}
2451a7de92daSDan Williams 
2452a7de92daSDan Williams 
2453a7de92daSDan Williams 	/* test ars_status with 'status' trimmed from 'out_length' */
2454a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2455a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2456a7de92daSDan Williams 		.out_length = cmd_size - 4,
2457a7de92daSDan Williams 	};
2458a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2459a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2460a7de92daSDan Williams 		.length = test_val,
2461a7de92daSDan Williams 	};
2462a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2463a7de92daSDan Williams 	if (rc)
2464a7de92daSDan Williams 		return rc;
2465a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2466a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2467a7de92daSDan Williams 
2468a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2469a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2470a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2471a7de92daSDan Williams 		return -EIO;
2472a7de92daSDan Williams 	}
2473a7de92daSDan Williams 
2474a7de92daSDan Williams 
2475a7de92daSDan Williams 	/* test ars_status with 'Output (Size)' including 'status' */
2476a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2477a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2478a7de92daSDan Williams 		.out_length = cmd_size,
2479a7de92daSDan Williams 	};
2480a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2481a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2482a7de92daSDan Williams 		.length = test_val,
2483a7de92daSDan Williams 	};
2484a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2485a7de92daSDan Williams 	if (rc)
2486a7de92daSDan Williams 		return rc;
2487a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2488a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2489a7de92daSDan Williams 
2490a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2491a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2492a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2493a7de92daSDan Williams 		return -EIO;
2494a7de92daSDan Williams 	}
2495a7de92daSDan Williams 
2496a7de92daSDan Williams 
2497a7de92daSDan Williams 	/* test extended status for get_config_size results in failure */
2498a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2499a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2500a7de92daSDan Williams 		.status = 1 << 16,
2501a7de92daSDan Williams 	};
2502a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2503a7de92daSDan Williams 	if (rc)
2504a7de92daSDan Williams 		return rc;
2505a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2506a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2507a7de92daSDan Williams 
2508a7de92daSDan Williams 	if (rc < 0 || cmd_rc >= 0) {
2509a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2510a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2511a7de92daSDan Williams 		return -EIO;
2512a7de92daSDan Williams 	}
2513a7de92daSDan Williams 
2514fb2a1748SDan Williams 	/* test clear error */
2515fb2a1748SDan Williams 	cmd_size = sizeof(cmds.clear_err);
2516fb2a1748SDan Williams 	cmds.clear_err = (struct nd_cmd_clear_error) {
2517fb2a1748SDan Williams 		.length = 512,
2518fb2a1748SDan Williams 		.cleared = 512,
2519fb2a1748SDan Williams 	};
2520fb2a1748SDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2521fb2a1748SDan Williams 	if (rc)
2522fb2a1748SDan Williams 		return rc;
2523fb2a1748SDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_CLEAR_ERROR,
2524fb2a1748SDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2525fb2a1748SDan Williams 	if (rc < 0 || cmd_rc) {
2526fb2a1748SDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2527fb2a1748SDan Williams 				__func__, __LINE__, rc, cmd_rc);
2528fb2a1748SDan Williams 		return -EIO;
2529fb2a1748SDan Williams 	}
2530fb2a1748SDan Williams 
2531a7de92daSDan Williams 	return 0;
2532a7de92daSDan Williams }
2533a7de92daSDan Williams 
25346bc75619SDan Williams static int nfit_test_probe(struct platform_device *pdev)
25356bc75619SDan Williams {
25366bc75619SDan Williams 	struct nvdimm_bus_descriptor *nd_desc;
25376bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
25386bc75619SDan Williams 	struct device *dev = &pdev->dev;
25396bc75619SDan Williams 	struct nfit_test *nfit_test;
2540231bf117SDan Williams 	struct nfit_mem *nfit_mem;
2541c14a868aSDan Williams 	union acpi_object *obj;
25426bc75619SDan Williams 	int rc;
25436bc75619SDan Williams 
2544a7de92daSDan Williams 	if (strcmp(dev_name(&pdev->dev), "nfit_test.0") == 0) {
2545a7de92daSDan Williams 		rc = nfit_ctl_test(&pdev->dev);
2546a7de92daSDan Williams 		if (rc)
2547a7de92daSDan Williams 			return rc;
2548a7de92daSDan Williams 	}
2549a7de92daSDan Williams 
25506bc75619SDan Williams 	nfit_test = to_nfit_test(&pdev->dev);
25516bc75619SDan Williams 
25526bc75619SDan Williams 	/* common alloc */
25536bc75619SDan Williams 	if (nfit_test->num_dcr) {
25546bc75619SDan Williams 		int num = nfit_test->num_dcr;
25556bc75619SDan Williams 
25566bc75619SDan Williams 		nfit_test->dimm = devm_kcalloc(dev, num, sizeof(void *),
25576bc75619SDan Williams 				GFP_KERNEL);
25586bc75619SDan Williams 		nfit_test->dimm_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
25596bc75619SDan Williams 				GFP_KERNEL);
25609d27a87eSDan Williams 		nfit_test->flush = devm_kcalloc(dev, num, sizeof(void *),
25619d27a87eSDan Williams 				GFP_KERNEL);
25629d27a87eSDan Williams 		nfit_test->flush_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
25639d27a87eSDan Williams 				GFP_KERNEL);
25646bc75619SDan Williams 		nfit_test->label = devm_kcalloc(dev, num, sizeof(void *),
25656bc75619SDan Williams 				GFP_KERNEL);
25666bc75619SDan Williams 		nfit_test->label_dma = devm_kcalloc(dev, num,
25676bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
25686bc75619SDan Williams 		nfit_test->dcr = devm_kcalloc(dev, num,
25696bc75619SDan Williams 				sizeof(struct nfit_test_dcr *), GFP_KERNEL);
25706bc75619SDan Williams 		nfit_test->dcr_dma = devm_kcalloc(dev, num,
25716bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
2572ed07c433SDan Williams 		nfit_test->smart = devm_kcalloc(dev, num,
2573ed07c433SDan Williams 				sizeof(struct nd_intel_smart), GFP_KERNEL);
2574ed07c433SDan Williams 		nfit_test->smart_threshold = devm_kcalloc(dev, num,
2575ed07c433SDan Williams 				sizeof(struct nd_intel_smart_threshold),
2576ed07c433SDan Williams 				GFP_KERNEL);
2577bfbaa952SDave Jiang 		nfit_test->fw = devm_kcalloc(dev, num,
2578bfbaa952SDave Jiang 				sizeof(struct nfit_test_fw), GFP_KERNEL);
25796bc75619SDan Williams 		if (nfit_test->dimm && nfit_test->dimm_dma && nfit_test->label
25806bc75619SDan Williams 				&& nfit_test->label_dma && nfit_test->dcr
25819d27a87eSDan Williams 				&& nfit_test->dcr_dma && nfit_test->flush
2582bfbaa952SDave Jiang 				&& nfit_test->flush_dma
2583bfbaa952SDave Jiang 				&& nfit_test->fw)
25846bc75619SDan Williams 			/* pass */;
25856bc75619SDan Williams 		else
25866bc75619SDan Williams 			return -ENOMEM;
25876bc75619SDan Williams 	}
25886bc75619SDan Williams 
25896bc75619SDan Williams 	if (nfit_test->num_pm) {
25906bc75619SDan Williams 		int num = nfit_test->num_pm;
25916bc75619SDan Williams 
25926bc75619SDan Williams 		nfit_test->spa_set = devm_kcalloc(dev, num, sizeof(void *),
25936bc75619SDan Williams 				GFP_KERNEL);
25946bc75619SDan Williams 		nfit_test->spa_set_dma = devm_kcalloc(dev, num,
25956bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
25966bc75619SDan Williams 		if (nfit_test->spa_set && nfit_test->spa_set_dma)
25976bc75619SDan Williams 			/* pass */;
25986bc75619SDan Williams 		else
25996bc75619SDan Williams 			return -ENOMEM;
26006bc75619SDan Williams 	}
26016bc75619SDan Williams 
26026bc75619SDan Williams 	/* per-nfit specific alloc */
26036bc75619SDan Williams 	if (nfit_test->alloc(nfit_test))
26046bc75619SDan Williams 		return -ENOMEM;
26056bc75619SDan Williams 
26066bc75619SDan Williams 	nfit_test->setup(nfit_test);
26076bc75619SDan Williams 	acpi_desc = &nfit_test->acpi_desc;
2608a61fe6f7SDan Williams 	acpi_nfit_desc_init(acpi_desc, &pdev->dev);
26096bc75619SDan Williams 	acpi_desc->blk_do_io = nfit_test_blk_do_io;
26106bc75619SDan Williams 	nd_desc = &acpi_desc->nd_desc;
2611a61fe6f7SDan Williams 	nd_desc->provider_name = NULL;
2612bc9775d8SDan Williams 	nd_desc->module = THIS_MODULE;
2613a61fe6f7SDan Williams 	nd_desc->ndctl = nfit_test_ctl;
26146bc75619SDan Williams 
2615e7a11b44SDan Williams 	rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_buf,
26161526f9e2SRoss Zwisler 			nfit_test->nfit_filled);
261758cd71b4SDan Williams 	if (rc)
261820985164SVishal Verma 		return rc;
261920985164SVishal Verma 
2620fbabd829SDan Williams 	rc = devm_add_action_or_reset(&pdev->dev, acpi_nfit_shutdown, acpi_desc);
2621fbabd829SDan Williams 	if (rc)
2622fbabd829SDan Williams 		return rc;
2623fbabd829SDan Williams 
262420985164SVishal Verma 	if (nfit_test->setup != nfit_test0_setup)
262520985164SVishal Verma 		return 0;
262620985164SVishal Verma 
262720985164SVishal Verma 	nfit_test->setup_hotplug = 1;
262820985164SVishal Verma 	nfit_test->setup(nfit_test);
262920985164SVishal Verma 
2630c14a868aSDan Williams 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
2631c14a868aSDan Williams 	if (!obj)
2632c14a868aSDan Williams 		return -ENOMEM;
2633c14a868aSDan Williams 	obj->type = ACPI_TYPE_BUFFER;
2634c14a868aSDan Williams 	obj->buffer.length = nfit_test->nfit_size;
2635c14a868aSDan Williams 	obj->buffer.pointer = nfit_test->nfit_buf;
2636c14a868aSDan Williams 	*(nfit_test->_fit) = obj;
2637c14a868aSDan Williams 	__acpi_nfit_notify(&pdev->dev, nfit_test, 0x80);
2638231bf117SDan Williams 
2639231bf117SDan Williams 	/* associate dimm devices with nfit_mem data for notification testing */
2640231bf117SDan Williams 	mutex_lock(&acpi_desc->init_mutex);
2641231bf117SDan Williams 	list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
2642231bf117SDan Williams 		u32 nfit_handle = __to_nfit_memdev(nfit_mem)->device_handle;
2643231bf117SDan Williams 		int i;
2644231bf117SDan Williams 
2645231bf117SDan Williams 		for (i = 0; i < NUM_DCR; i++)
2646231bf117SDan Williams 			if (nfit_handle == handle[i])
2647231bf117SDan Williams 				dev_set_drvdata(nfit_test->dimm_dev[i],
2648231bf117SDan Williams 						nfit_mem);
2649231bf117SDan Williams 	}
2650231bf117SDan Williams 	mutex_unlock(&acpi_desc->init_mutex);
26516bc75619SDan Williams 
26526bc75619SDan Williams 	return 0;
26536bc75619SDan Williams }
26546bc75619SDan Williams 
26556bc75619SDan Williams static int nfit_test_remove(struct platform_device *pdev)
26566bc75619SDan Williams {
26576bc75619SDan Williams 	return 0;
26586bc75619SDan Williams }
26596bc75619SDan Williams 
26606bc75619SDan Williams static void nfit_test_release(struct device *dev)
26616bc75619SDan Williams {
26626bc75619SDan Williams 	struct nfit_test *nfit_test = to_nfit_test(dev);
26636bc75619SDan Williams 
26646bc75619SDan Williams 	kfree(nfit_test);
26656bc75619SDan Williams }
26666bc75619SDan Williams 
26676bc75619SDan Williams static const struct platform_device_id nfit_test_id[] = {
26686bc75619SDan Williams 	{ KBUILD_MODNAME },
26696bc75619SDan Williams 	{ },
26706bc75619SDan Williams };
26716bc75619SDan Williams 
26726bc75619SDan Williams static struct platform_driver nfit_test_driver = {
26736bc75619SDan Williams 	.probe = nfit_test_probe,
26746bc75619SDan Williams 	.remove = nfit_test_remove,
26756bc75619SDan Williams 	.driver = {
26766bc75619SDan Williams 		.name = KBUILD_MODNAME,
26776bc75619SDan Williams 	},
26786bc75619SDan Williams 	.id_table = nfit_test_id,
26796bc75619SDan Williams };
26806bc75619SDan Williams 
26816bc75619SDan Williams static __init int nfit_test_init(void)
26826bc75619SDan Williams {
26836bc75619SDan Williams 	int rc, i;
26846bc75619SDan Williams 
26850fb5c8dfSDan Williams 	pmem_test();
26860fb5c8dfSDan Williams 	libnvdimm_test();
26870fb5c8dfSDan Williams 	acpi_nfit_test();
26880fb5c8dfSDan Williams 	device_dax_test();
26890fb5c8dfSDan Williams 
2690a7de92daSDan Williams 	nfit_test_setup(nfit_test_lookup, nfit_test_evaluate_dsm);
2691231bf117SDan Williams 
26929fb1a190SDave Jiang 	nfit_wq = create_singlethread_workqueue("nfit");
26939fb1a190SDave Jiang 	if (!nfit_wq)
26949fb1a190SDave Jiang 		return -ENOMEM;
26959fb1a190SDave Jiang 
2696a7de92daSDan Williams 	nfit_test_dimm = class_create(THIS_MODULE, "nfit_test_dimm");
2697a7de92daSDan Williams 	if (IS_ERR(nfit_test_dimm)) {
2698a7de92daSDan Williams 		rc = PTR_ERR(nfit_test_dimm);
2699a7de92daSDan Williams 		goto err_register;
2700a7de92daSDan Williams 	}
27016bc75619SDan Williams 
27026bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++) {
27036bc75619SDan Williams 		struct nfit_test *nfit_test;
27046bc75619SDan Williams 		struct platform_device *pdev;
27056bc75619SDan Williams 
27066bc75619SDan Williams 		nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
27076bc75619SDan Williams 		if (!nfit_test) {
27086bc75619SDan Williams 			rc = -ENOMEM;
27096bc75619SDan Williams 			goto err_register;
27106bc75619SDan Williams 		}
27116bc75619SDan Williams 		INIT_LIST_HEAD(&nfit_test->resources);
27129fb1a190SDave Jiang 		badrange_init(&nfit_test->badrange);
27136bc75619SDan Williams 		switch (i) {
27146bc75619SDan Williams 		case 0:
27156bc75619SDan Williams 			nfit_test->num_pm = NUM_PM;
2716dafb1048SDan Williams 			nfit_test->dcr_idx = 0;
27176bc75619SDan Williams 			nfit_test->num_dcr = NUM_DCR;
27186bc75619SDan Williams 			nfit_test->alloc = nfit_test0_alloc;
27196bc75619SDan Williams 			nfit_test->setup = nfit_test0_setup;
27206bc75619SDan Williams 			break;
27216bc75619SDan Williams 		case 1:
2722a117699cSYasunori Goto 			nfit_test->num_pm = 2;
2723dafb1048SDan Williams 			nfit_test->dcr_idx = NUM_DCR;
2724ac40b675SDan Williams 			nfit_test->num_dcr = 2;
27256bc75619SDan Williams 			nfit_test->alloc = nfit_test1_alloc;
27266bc75619SDan Williams 			nfit_test->setup = nfit_test1_setup;
27276bc75619SDan Williams 			break;
27286bc75619SDan Williams 		default:
27296bc75619SDan Williams 			rc = -EINVAL;
27306bc75619SDan Williams 			goto err_register;
27316bc75619SDan Williams 		}
27326bc75619SDan Williams 		pdev = &nfit_test->pdev;
27336bc75619SDan Williams 		pdev->name = KBUILD_MODNAME;
27346bc75619SDan Williams 		pdev->id = i;
27356bc75619SDan Williams 		pdev->dev.release = nfit_test_release;
27366bc75619SDan Williams 		rc = platform_device_register(pdev);
27376bc75619SDan Williams 		if (rc) {
27386bc75619SDan Williams 			put_device(&pdev->dev);
27396bc75619SDan Williams 			goto err_register;
27406bc75619SDan Williams 		}
27418b06b884SDan Williams 		get_device(&pdev->dev);
27426bc75619SDan Williams 
27436bc75619SDan Williams 		rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
27446bc75619SDan Williams 		if (rc)
27456bc75619SDan Williams 			goto err_register;
27466bc75619SDan Williams 
27476bc75619SDan Williams 		instances[i] = nfit_test;
27489fb1a190SDave Jiang 		INIT_WORK(&nfit_test->work, uc_error_notify);
27496bc75619SDan Williams 	}
27506bc75619SDan Williams 
27516bc75619SDan Williams 	rc = platform_driver_register(&nfit_test_driver);
27526bc75619SDan Williams 	if (rc)
27536bc75619SDan Williams 		goto err_register;
27546bc75619SDan Williams 	return 0;
27556bc75619SDan Williams 
27566bc75619SDan Williams  err_register:
27579fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
27586bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
27596bc75619SDan Williams 		if (instances[i])
27606bc75619SDan Williams 			platform_device_unregister(&instances[i]->pdev);
27616bc75619SDan Williams 	nfit_test_teardown();
27628b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
27638b06b884SDan Williams 		if (instances[i])
27648b06b884SDan Williams 			put_device(&instances[i]->pdev.dev);
27658b06b884SDan Williams 
27666bc75619SDan Williams 	return rc;
27676bc75619SDan Williams }
27686bc75619SDan Williams 
27696bc75619SDan Williams static __exit void nfit_test_exit(void)
27706bc75619SDan Williams {
27716bc75619SDan Williams 	int i;
27726bc75619SDan Williams 
27739fb1a190SDave Jiang 	flush_workqueue(nfit_wq);
27749fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
27756bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
27766bc75619SDan Williams 		platform_device_unregister(&instances[i]->pdev);
27778b06b884SDan Williams 	platform_driver_unregister(&nfit_test_driver);
27786bc75619SDan Williams 	nfit_test_teardown();
27798b06b884SDan Williams 
27808b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
27818b06b884SDan Williams 		put_device(&instances[i]->pdev.dev);
2782231bf117SDan Williams 	class_destroy(nfit_test_dimm);
27836bc75619SDan Williams }
27846bc75619SDan Williams 
27856bc75619SDan Williams module_init(nfit_test_init);
27866bc75619SDan Williams module_exit(nfit_test_exit);
27876bc75619SDan Williams MODULE_LICENSE("GPL v2");
27886bc75619SDan Williams MODULE_AUTHOR("Intel Corporation");
2789