xref: /openbmc/linux/tools/testing/nvdimm/test/nfit.c (revision 1273c253c32b9a073a4d8921ed079177ccc7c8af)
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 
325d8beee2SDan Williams #include <asm/mcsafe_test.h>
335d8beee2SDan Williams 
346bc75619SDan Williams /*
356bc75619SDan Williams  * Generate an NFIT table to describe the following topology:
366bc75619SDan Williams  *
376bc75619SDan Williams  * BUS0: Interleaved PMEM regions, and aliasing with BLK regions
386bc75619SDan Williams  *
396bc75619SDan Williams  *                     (a)                       (b)            DIMM   BLK-REGION
406bc75619SDan Williams  *           +----------+--------------+----------+---------+
416bc75619SDan Williams  * +------+  |  blk2.0  |     pm0.0    |  blk2.1  |  pm1.0  |    0      region2
426bc75619SDan Williams  * | imc0 +--+- - - - - region0 - - - -+----------+         +
436bc75619SDan Williams  * +--+---+  |  blk3.0  |     pm0.0    |  blk3.1  |  pm1.0  |    1      region3
446bc75619SDan Williams  *    |      +----------+--------------v----------v         v
456bc75619SDan Williams  * +--+---+                            |                    |
466bc75619SDan Williams  * | cpu0 |                                    region1
476bc75619SDan Williams  * +--+---+                            |                    |
486bc75619SDan Williams  *    |      +-------------------------^----------^         ^
496bc75619SDan Williams  * +--+---+  |                 blk4.0             |  pm1.0  |    2      region4
506bc75619SDan Williams  * | imc1 +--+-------------------------+----------+         +
516bc75619SDan Williams  * +------+  |                 blk5.0             |  pm1.0  |    3      region5
526bc75619SDan Williams  *           +-------------------------+----------+-+-------+
536bc75619SDan Williams  *
5420985164SVishal Verma  * +--+---+
5520985164SVishal Verma  * | cpu1 |
5620985164SVishal Verma  * +--+---+                   (Hotplug DIMM)
5720985164SVishal Verma  *    |      +----------------------------------------------+
5820985164SVishal Verma  * +--+---+  |                 blk6.0/pm7.0                 |    4      region6/7
5920985164SVishal Verma  * | imc0 +--+----------------------------------------------+
6020985164SVishal Verma  * +------+
6120985164SVishal Verma  *
6220985164SVishal Verma  *
636bc75619SDan Williams  * *) In this layout we have four dimms and two memory controllers in one
646bc75619SDan Williams  *    socket.  Each unique interface (BLK or PMEM) to DPA space
656bc75619SDan Williams  *    is identified by a region device with a dynamically assigned id.
666bc75619SDan Williams  *
676bc75619SDan Williams  * *) The first portion of dimm0 and dimm1 are interleaved as REGION0.
686bc75619SDan Williams  *    A single PMEM namespace "pm0.0" is created using half of the
696bc75619SDan Williams  *    REGION0 SPA-range.  REGION0 spans dimm0 and dimm1.  PMEM namespace
706bc75619SDan Williams  *    allocate from from the bottom of a region.  The unallocated
716bc75619SDan Williams  *    portion of REGION0 aliases with REGION2 and REGION3.  That
726bc75619SDan Williams  *    unallacted capacity is reclaimed as BLK namespaces ("blk2.0" and
736bc75619SDan Williams  *    "blk3.0") starting at the base of each DIMM to offset (a) in those
746bc75619SDan Williams  *    DIMMs.  "pm0.0", "blk2.0" and "blk3.0" are free-form readable
756bc75619SDan Williams  *    names that can be assigned to a namespace.
766bc75619SDan Williams  *
776bc75619SDan Williams  * *) In the last portion of dimm0 and dimm1 we have an interleaved
786bc75619SDan Williams  *    SPA range, REGION1, that spans those two dimms as well as dimm2
796bc75619SDan Williams  *    and dimm3.  Some of REGION1 allocated to a PMEM namespace named
806bc75619SDan Williams  *    "pm1.0" the rest is reclaimed in 4 BLK namespaces (for each
816bc75619SDan Williams  *    dimm in the interleave set), "blk2.1", "blk3.1", "blk4.0", and
826bc75619SDan Williams  *    "blk5.0".
836bc75619SDan Williams  *
846bc75619SDan Williams  * *) The portion of dimm2 and dimm3 that do not participate in the
856bc75619SDan Williams  *    REGION1 interleaved SPA range (i.e. the DPA address below offset
866bc75619SDan Williams  *    (b) are also included in the "blk4.0" and "blk5.0" namespaces.
876bc75619SDan Williams  *    Note, that BLK namespaces need not be contiguous in DPA-space, and
886bc75619SDan Williams  *    can consume aliased capacity from multiple interleave sets.
896bc75619SDan Williams  *
906bc75619SDan Williams  * BUS1: Legacy NVDIMM (single contiguous range)
916bc75619SDan Williams  *
926bc75619SDan Williams  *  region2
936bc75619SDan Williams  * +---------------------+
946bc75619SDan Williams  * |---------------------|
956bc75619SDan Williams  * ||       pm2.0       ||
966bc75619SDan Williams  * |---------------------|
976bc75619SDan Williams  * +---------------------+
986bc75619SDan Williams  *
996bc75619SDan Williams  * *) A NFIT-table may describe a simple system-physical-address range
1006bc75619SDan Williams  *    with no BLK aliasing.  This type of region may optionally
1016bc75619SDan Williams  *    reference an NVDIMM.
1026bc75619SDan Williams  */
1036bc75619SDan Williams enum {
10420985164SVishal Verma 	NUM_PM  = 3,
10520985164SVishal Verma 	NUM_DCR = 5,
10685d3fa02SDan Williams 	NUM_HINTS = 8,
1076bc75619SDan Williams 	NUM_BDW = NUM_DCR,
1086bc75619SDan Williams 	NUM_SPA = NUM_PM + NUM_DCR + NUM_BDW,
1099741a559SRoss Zwisler 	NUM_MEM = NUM_DCR + NUM_BDW + 2 /* spa0 iset */
1109741a559SRoss Zwisler 		+ 4 /* spa1 iset */ + 1 /* spa11 iset */,
1116bc75619SDan Williams 	DIMM_SIZE = SZ_32M,
1126bc75619SDan Williams 	LABEL_SIZE = SZ_128K,
1137bfe97c7SDan Williams 	SPA_VCD_SIZE = SZ_4M,
1146bc75619SDan Williams 	SPA0_SIZE = DIMM_SIZE,
1156bc75619SDan Williams 	SPA1_SIZE = DIMM_SIZE*2,
1166bc75619SDan Williams 	SPA2_SIZE = DIMM_SIZE,
1176bc75619SDan Williams 	BDW_SIZE = 64 << 8,
1186bc75619SDan Williams 	DCR_SIZE = 12,
1196bc75619SDan Williams 	NUM_NFITS = 2, /* permit testing multiple NFITs per system */
1206bc75619SDan Williams };
1216bc75619SDan Williams 
1226bc75619SDan Williams struct nfit_test_dcr {
1236bc75619SDan Williams 	__le64 bdw_addr;
1246bc75619SDan Williams 	__le32 bdw_status;
1256bc75619SDan Williams 	__u8 aperature[BDW_SIZE];
1266bc75619SDan Williams };
1276bc75619SDan Williams 
1286bc75619SDan Williams #define NFIT_DIMM_HANDLE(node, socket, imc, chan, dimm) \
1296bc75619SDan Williams 	(((node & 0xfff) << 16) | ((socket & 0xf) << 12) \
1306bc75619SDan Williams 	 | ((imc & 0xf) << 8) | ((chan & 0xf) << 4) | (dimm & 0xf))
1316bc75619SDan Williams 
132dafb1048SDan Williams static u32 handle[] = {
1336bc75619SDan Williams 	[0] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 0),
1346bc75619SDan Williams 	[1] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 1),
1356bc75619SDan Williams 	[2] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 0),
1366bc75619SDan Williams 	[3] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 1),
13720985164SVishal Verma 	[4] = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
138dafb1048SDan Williams 	[5] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 0),
139ac40b675SDan Williams 	[6] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 1),
1406bc75619SDan Williams };
1416bc75619SDan Williams 
14273606afdSDan Williams static unsigned long dimm_fail_cmd_flags[NUM_DCR];
14355c72ab6SDan Williams static int dimm_fail_cmd_code[NUM_DCR];
14473606afdSDan Williams 
145bfbaa952SDave Jiang struct nfit_test_fw {
146bfbaa952SDave Jiang 	enum intel_fw_update_state state;
147bfbaa952SDave Jiang 	u32 context;
148bfbaa952SDave Jiang 	u64 version;
149bfbaa952SDave Jiang 	u32 size_received;
150bfbaa952SDave Jiang 	u64 end_time;
151bfbaa952SDave Jiang };
152bfbaa952SDave Jiang 
1536bc75619SDan Williams struct nfit_test {
1546bc75619SDan Williams 	struct acpi_nfit_desc acpi_desc;
1556bc75619SDan Williams 	struct platform_device pdev;
1566bc75619SDan Williams 	struct list_head resources;
1576bc75619SDan Williams 	void *nfit_buf;
1586bc75619SDan Williams 	dma_addr_t nfit_dma;
1596bc75619SDan Williams 	size_t nfit_size;
1601526f9e2SRoss Zwisler 	size_t nfit_filled;
161dafb1048SDan Williams 	int dcr_idx;
1626bc75619SDan Williams 	int num_dcr;
1636bc75619SDan Williams 	int num_pm;
1646bc75619SDan Williams 	void **dimm;
1656bc75619SDan Williams 	dma_addr_t *dimm_dma;
1669d27a87eSDan Williams 	void **flush;
1679d27a87eSDan Williams 	dma_addr_t *flush_dma;
1686bc75619SDan Williams 	void **label;
1696bc75619SDan Williams 	dma_addr_t *label_dma;
1706bc75619SDan Williams 	void **spa_set;
1716bc75619SDan Williams 	dma_addr_t *spa_set_dma;
1726bc75619SDan Williams 	struct nfit_test_dcr **dcr;
1736bc75619SDan Williams 	dma_addr_t *dcr_dma;
1746bc75619SDan Williams 	int (*alloc)(struct nfit_test *t);
1756bc75619SDan Williams 	void (*setup)(struct nfit_test *t);
17620985164SVishal Verma 	int setup_hotplug;
177c14a868aSDan Williams 	union acpi_object **_fit;
178c14a868aSDan Williams 	dma_addr_t _fit_dma;
179f471f1a7SDan Williams 	struct ars_state {
180f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status;
181f471f1a7SDan Williams 		unsigned long deadline;
182f471f1a7SDan Williams 		spinlock_t lock;
183f471f1a7SDan Williams 	} ars_state;
184231bf117SDan Williams 	struct device *dimm_dev[NUM_DCR];
185ed07c433SDan Williams 	struct nd_intel_smart *smart;
186ed07c433SDan Williams 	struct nd_intel_smart_threshold *smart_threshold;
1879fb1a190SDave Jiang 	struct badrange badrange;
1889fb1a190SDave Jiang 	struct work_struct work;
189bfbaa952SDave Jiang 	struct nfit_test_fw *fw;
1906bc75619SDan Williams };
1916bc75619SDan Williams 
1929fb1a190SDave Jiang static struct workqueue_struct *nfit_wq;
1939fb1a190SDave Jiang 
1946bc75619SDan Williams static struct nfit_test *to_nfit_test(struct device *dev)
1956bc75619SDan Williams {
1966bc75619SDan Williams 	struct platform_device *pdev = to_platform_device(dev);
1976bc75619SDan Williams 
1986bc75619SDan Williams 	return container_of(pdev, struct nfit_test, pdev);
1996bc75619SDan Williams }
2006bc75619SDan Williams 
201bfbaa952SDave Jiang static int nd_intel_test_get_fw_info(struct nfit_test *t,
202bfbaa952SDave Jiang 		struct nd_intel_fw_info *nd_cmd, unsigned int buf_len,
203bfbaa952SDave Jiang 		int idx)
204bfbaa952SDave Jiang {
205bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
206bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
207bfbaa952SDave Jiang 
208bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p, buf_len: %u, idx: %d\n",
209bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
210bfbaa952SDave Jiang 
211bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
212bfbaa952SDave Jiang 		return -EINVAL;
213bfbaa952SDave Jiang 
214bfbaa952SDave Jiang 	nd_cmd->status = 0;
215bfbaa952SDave Jiang 	nd_cmd->storage_size = INTEL_FW_STORAGE_SIZE;
216bfbaa952SDave Jiang 	nd_cmd->max_send_len = INTEL_FW_MAX_SEND_LEN;
217bfbaa952SDave Jiang 	nd_cmd->query_interval = INTEL_FW_QUERY_INTERVAL;
218bfbaa952SDave Jiang 	nd_cmd->max_query_time = INTEL_FW_QUERY_MAX_TIME;
219bfbaa952SDave Jiang 	nd_cmd->update_cap = 0;
220bfbaa952SDave Jiang 	nd_cmd->fis_version = INTEL_FW_FIS_VERSION;
221bfbaa952SDave Jiang 	nd_cmd->run_version = 0;
222bfbaa952SDave Jiang 	nd_cmd->updated_version = fw->version;
223bfbaa952SDave Jiang 
224bfbaa952SDave Jiang 	return 0;
225bfbaa952SDave Jiang }
226bfbaa952SDave Jiang 
227bfbaa952SDave Jiang static int nd_intel_test_start_update(struct nfit_test *t,
228bfbaa952SDave Jiang 		struct nd_intel_fw_start *nd_cmd, unsigned int buf_len,
229bfbaa952SDave Jiang 		int idx)
230bfbaa952SDave Jiang {
231bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
232bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
233bfbaa952SDave Jiang 
234bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
235bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
236bfbaa952SDave Jiang 
237bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
238bfbaa952SDave Jiang 		return -EINVAL;
239bfbaa952SDave Jiang 
240bfbaa952SDave Jiang 	if (fw->state != FW_STATE_NEW) {
241bfbaa952SDave Jiang 		/* extended status, FW update in progress */
242bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
243bfbaa952SDave Jiang 		return 0;
244bfbaa952SDave Jiang 	}
245bfbaa952SDave Jiang 
246bfbaa952SDave Jiang 	fw->state = FW_STATE_IN_PROGRESS;
247bfbaa952SDave Jiang 	fw->context++;
248bfbaa952SDave Jiang 	fw->size_received = 0;
249bfbaa952SDave Jiang 	nd_cmd->status = 0;
250bfbaa952SDave Jiang 	nd_cmd->context = fw->context;
251bfbaa952SDave Jiang 
252bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context issued: %#x\n", __func__, nd_cmd->context);
253bfbaa952SDave Jiang 
254bfbaa952SDave Jiang 	return 0;
255bfbaa952SDave Jiang }
256bfbaa952SDave Jiang 
257bfbaa952SDave Jiang static int nd_intel_test_send_data(struct nfit_test *t,
258bfbaa952SDave Jiang 		struct nd_intel_fw_send_data *nd_cmd, unsigned int buf_len,
259bfbaa952SDave Jiang 		int idx)
260bfbaa952SDave Jiang {
261bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
262bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
263bfbaa952SDave Jiang 	u32 *status = (u32 *)&nd_cmd->data[nd_cmd->length];
264bfbaa952SDave Jiang 
265bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
266bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
267bfbaa952SDave Jiang 
268bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
269bfbaa952SDave Jiang 		return -EINVAL;
270bfbaa952SDave Jiang 
271bfbaa952SDave Jiang 
272bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->status: %#x\n", __func__, *status);
273bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[0]: %#x\n", __func__, nd_cmd->data[0]);
274bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[%u]: %#x\n", __func__, nd_cmd->length-1,
275bfbaa952SDave Jiang 			nd_cmd->data[nd_cmd->length-1]);
276bfbaa952SDave Jiang 
277bfbaa952SDave Jiang 	if (fw->state != FW_STATE_IN_PROGRESS) {
278bfbaa952SDave Jiang 		dev_dbg(dev, "%s: not in IN_PROGRESS state\n", __func__);
279bfbaa952SDave Jiang 		*status = 0x5;
280bfbaa952SDave Jiang 		return 0;
281bfbaa952SDave Jiang 	}
282bfbaa952SDave Jiang 
283bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
284bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
285bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
286bfbaa952SDave Jiang 		*status = 0x10007;
287bfbaa952SDave Jiang 		return 0;
288bfbaa952SDave Jiang 	}
289bfbaa952SDave Jiang 
290bfbaa952SDave Jiang 	/*
291bfbaa952SDave Jiang 	 * check offset + len > size of fw storage
292bfbaa952SDave Jiang 	 * check length is > max send length
293bfbaa952SDave Jiang 	 */
294bfbaa952SDave Jiang 	if (nd_cmd->offset + nd_cmd->length > INTEL_FW_STORAGE_SIZE ||
295bfbaa952SDave Jiang 			nd_cmd->length > INTEL_FW_MAX_SEND_LEN) {
296bfbaa952SDave Jiang 		*status = 0x3;
297bfbaa952SDave Jiang 		dev_dbg(dev, "%s: buffer boundary violation\n", __func__);
298bfbaa952SDave Jiang 		return 0;
299bfbaa952SDave Jiang 	}
300bfbaa952SDave Jiang 
301bfbaa952SDave Jiang 	fw->size_received += nd_cmd->length;
302bfbaa952SDave Jiang 	dev_dbg(dev, "%s: copying %u bytes, %u bytes so far\n",
303bfbaa952SDave Jiang 			__func__, nd_cmd->length, fw->size_received);
304bfbaa952SDave Jiang 	*status = 0;
305bfbaa952SDave Jiang 	return 0;
306bfbaa952SDave Jiang }
307bfbaa952SDave Jiang 
308bfbaa952SDave Jiang static int nd_intel_test_finish_fw(struct nfit_test *t,
309bfbaa952SDave Jiang 		struct nd_intel_fw_finish_update *nd_cmd,
310bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
311bfbaa952SDave Jiang {
312bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
313bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
314bfbaa952SDave Jiang 
315bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
316bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
317bfbaa952SDave Jiang 
318bfbaa952SDave Jiang 	if (fw->state == FW_STATE_UPDATED) {
319bfbaa952SDave Jiang 		/* update already done, need cold boot */
320bfbaa952SDave Jiang 		nd_cmd->status = 0x20007;
321bfbaa952SDave Jiang 		return 0;
322bfbaa952SDave Jiang 	}
323bfbaa952SDave Jiang 
324bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context: %#x  ctrl_flags: %#x\n",
325bfbaa952SDave Jiang 			__func__, nd_cmd->context, nd_cmd->ctrl_flags);
326bfbaa952SDave Jiang 
327bfbaa952SDave Jiang 	switch (nd_cmd->ctrl_flags) {
328bfbaa952SDave Jiang 	case 0: /* finish */
329bfbaa952SDave Jiang 		if (nd_cmd->context != fw->context) {
330bfbaa952SDave Jiang 			dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
331bfbaa952SDave Jiang 					__func__, nd_cmd->context,
332bfbaa952SDave Jiang 					fw->context);
333bfbaa952SDave Jiang 			nd_cmd->status = 0x10007;
334bfbaa952SDave Jiang 			return 0;
335bfbaa952SDave Jiang 		}
336bfbaa952SDave Jiang 		nd_cmd->status = 0;
337bfbaa952SDave Jiang 		fw->state = FW_STATE_VERIFY;
338bfbaa952SDave Jiang 		/* set 1 second of time for firmware "update" */
339bfbaa952SDave Jiang 		fw->end_time = jiffies + HZ;
340bfbaa952SDave Jiang 		break;
341bfbaa952SDave Jiang 
342bfbaa952SDave Jiang 	case 1: /* abort */
343bfbaa952SDave Jiang 		fw->size_received = 0;
344bfbaa952SDave Jiang 		/* successfully aborted status */
345bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
346bfbaa952SDave Jiang 		fw->state = FW_STATE_NEW;
347bfbaa952SDave Jiang 		dev_dbg(dev, "%s: abort successful\n", __func__);
348bfbaa952SDave Jiang 		break;
349bfbaa952SDave Jiang 
350bfbaa952SDave Jiang 	default: /* bad control flag */
351bfbaa952SDave Jiang 		dev_warn(dev, "%s: unknown control flag: %#x\n",
352bfbaa952SDave Jiang 				__func__, nd_cmd->ctrl_flags);
353bfbaa952SDave Jiang 		return -EINVAL;
354bfbaa952SDave Jiang 	}
355bfbaa952SDave Jiang 
356bfbaa952SDave Jiang 	return 0;
357bfbaa952SDave Jiang }
358bfbaa952SDave Jiang 
359bfbaa952SDave Jiang static int nd_intel_test_finish_query(struct nfit_test *t,
360bfbaa952SDave Jiang 		struct nd_intel_fw_finish_query *nd_cmd,
361bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
362bfbaa952SDave Jiang {
363bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
364bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
365bfbaa952SDave Jiang 
366bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
367bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
368bfbaa952SDave Jiang 
369bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
370bfbaa952SDave Jiang 		return -EINVAL;
371bfbaa952SDave Jiang 
372bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
373bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
374bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
375bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
376bfbaa952SDave Jiang 		return 0;
377bfbaa952SDave Jiang 	}
378bfbaa952SDave Jiang 
379bfbaa952SDave Jiang 	dev_dbg(dev, "%s context: %#x\n", __func__, nd_cmd->context);
380bfbaa952SDave Jiang 
381bfbaa952SDave Jiang 	switch (fw->state) {
382bfbaa952SDave Jiang 	case FW_STATE_NEW:
383bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
384bfbaa952SDave Jiang 		nd_cmd->status = 0;
385bfbaa952SDave Jiang 		dev_dbg(dev, "%s: new state\n", __func__);
386bfbaa952SDave Jiang 		break;
387bfbaa952SDave Jiang 
388bfbaa952SDave Jiang 	case FW_STATE_IN_PROGRESS:
389bfbaa952SDave Jiang 		/* sequencing error */
390bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
391bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
392bfbaa952SDave Jiang 		dev_dbg(dev, "%s: sequence error\n", __func__);
393bfbaa952SDave Jiang 		break;
394bfbaa952SDave Jiang 
395bfbaa952SDave Jiang 	case FW_STATE_VERIFY:
396bfbaa952SDave Jiang 		if (time_is_after_jiffies64(fw->end_time)) {
397bfbaa952SDave Jiang 			nd_cmd->updated_fw_rev = 0;
398bfbaa952SDave Jiang 			nd_cmd->status = 0x20007;
399bfbaa952SDave Jiang 			dev_dbg(dev, "%s: still verifying\n", __func__);
400bfbaa952SDave Jiang 			break;
401bfbaa952SDave Jiang 		}
402bfbaa952SDave Jiang 
403bfbaa952SDave Jiang 		dev_dbg(dev, "%s: transition out verify\n", __func__);
404bfbaa952SDave Jiang 		fw->state = FW_STATE_UPDATED;
405bfbaa952SDave Jiang 		/* we are going to fall through if it's "done" */
406bfbaa952SDave Jiang 	case FW_STATE_UPDATED:
407bfbaa952SDave Jiang 		nd_cmd->status = 0;
408bfbaa952SDave Jiang 		/* bogus test version */
409bfbaa952SDave Jiang 		fw->version = nd_cmd->updated_fw_rev =
410bfbaa952SDave Jiang 			INTEL_FW_FAKE_VERSION;
411bfbaa952SDave Jiang 		dev_dbg(dev, "%s: updated\n", __func__);
412bfbaa952SDave Jiang 		break;
413bfbaa952SDave Jiang 
414bfbaa952SDave Jiang 	default: /* we should never get here */
415bfbaa952SDave Jiang 		return -EINVAL;
416bfbaa952SDave Jiang 	}
417bfbaa952SDave Jiang 
418bfbaa952SDave Jiang 	return 0;
419bfbaa952SDave Jiang }
420bfbaa952SDave Jiang 
42139c686b8SVishal Verma static int nfit_test_cmd_get_config_size(struct nd_cmd_get_config_size *nd_cmd,
4226bc75619SDan Williams 		unsigned int buf_len)
4236bc75619SDan Williams {
4246bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4256bc75619SDan Williams 		return -EINVAL;
42639c686b8SVishal Verma 
4276bc75619SDan Williams 	nd_cmd->status = 0;
4286bc75619SDan Williams 	nd_cmd->config_size = LABEL_SIZE;
4296bc75619SDan Williams 	nd_cmd->max_xfer = SZ_4K;
43039c686b8SVishal Verma 
43139c686b8SVishal Verma 	return 0;
4326bc75619SDan Williams }
43339c686b8SVishal Verma 
43439c686b8SVishal Verma static int nfit_test_cmd_get_config_data(struct nd_cmd_get_config_data_hdr
43539c686b8SVishal Verma 		*nd_cmd, unsigned int buf_len, void *label)
43639c686b8SVishal Verma {
4376bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
43839c686b8SVishal Verma 	int rc;
4396bc75619SDan Williams 
4406bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4416bc75619SDan Williams 		return -EINVAL;
4426bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4436bc75619SDan Williams 		return -EINVAL;
4446bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) > buf_len)
4456bc75619SDan Williams 		return -EINVAL;
4466bc75619SDan Williams 
4476bc75619SDan Williams 	nd_cmd->status = 0;
4486bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
44939c686b8SVishal Verma 	memcpy(nd_cmd->out_buf, label + offset, len);
4506bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - len;
45139c686b8SVishal Verma 
45239c686b8SVishal Verma 	return rc;
4536bc75619SDan Williams }
45439c686b8SVishal Verma 
45539c686b8SVishal Verma static int nfit_test_cmd_set_config_data(struct nd_cmd_set_config_hdr *nd_cmd,
45639c686b8SVishal Verma 		unsigned int buf_len, void *label)
45739c686b8SVishal Verma {
4586bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
4596bc75619SDan Williams 	u32 *status;
46039c686b8SVishal Verma 	int rc;
4616bc75619SDan Williams 
4626bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4636bc75619SDan Williams 		return -EINVAL;
4646bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4656bc75619SDan Williams 		return -EINVAL;
4666bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) + 4 > buf_len)
4676bc75619SDan Williams 		return -EINVAL;
4686bc75619SDan Williams 
46939c686b8SVishal Verma 	status = (void *)nd_cmd + nd_cmd->in_length + sizeof(*nd_cmd);
4706bc75619SDan Williams 	*status = 0;
4716bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
47239c686b8SVishal Verma 	memcpy(label + offset, nd_cmd->in_buf, len);
4736bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - (len + 4);
47439c686b8SVishal Verma 
47539c686b8SVishal Verma 	return rc;
4766bc75619SDan Williams }
47739c686b8SVishal Verma 
478d4f32367SDan Williams #define NFIT_TEST_CLEAR_ERR_UNIT 256
479747ffe11SDan Williams 
48039c686b8SVishal Verma static int nfit_test_cmd_ars_cap(struct nd_cmd_ars_cap *nd_cmd,
48139c686b8SVishal Verma 		unsigned int buf_len)
48239c686b8SVishal Verma {
4839fb1a190SDave Jiang 	int ars_recs;
4849fb1a190SDave Jiang 
48539c686b8SVishal Verma 	if (buf_len < sizeof(*nd_cmd))
48639c686b8SVishal Verma 		return -EINVAL;
48739c686b8SVishal Verma 
4889fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
4899fb1a190SDave Jiang 	ars_recs = SZ_4K / sizeof(struct nd_ars_record);
4909fb1a190SDave Jiang 
491747ffe11SDan Williams 	nd_cmd->max_ars_out = sizeof(struct nd_cmd_ars_status)
4929fb1a190SDave Jiang 		+ ars_recs * sizeof(struct nd_ars_record);
49339c686b8SVishal Verma 	nd_cmd->status = (ND_ARS_PERSISTENT | ND_ARS_VOLATILE) << 16;
494d4f32367SDan Williams 	nd_cmd->clear_err_unit = NFIT_TEST_CLEAR_ERR_UNIT;
49539c686b8SVishal Verma 
49639c686b8SVishal Verma 	return 0;
49739c686b8SVishal Verma }
49839c686b8SVishal Verma 
4999fb1a190SDave Jiang static void post_ars_status(struct ars_state *ars_state,
5009fb1a190SDave Jiang 		struct badrange *badrange, u64 addr, u64 len)
50139c686b8SVishal Verma {
502f471f1a7SDan Williams 	struct nd_cmd_ars_status *ars_status;
503f471f1a7SDan Williams 	struct nd_ars_record *ars_record;
5049fb1a190SDave Jiang 	struct badrange_entry *be;
5059fb1a190SDave Jiang 	u64 end = addr + len - 1;
5069fb1a190SDave Jiang 	int i = 0;
507f471f1a7SDan Williams 
508f471f1a7SDan Williams 	ars_state->deadline = jiffies + 1*HZ;
509f471f1a7SDan Williams 	ars_status = ars_state->ars_status;
510f471f1a7SDan Williams 	ars_status->status = 0;
511f471f1a7SDan Williams 	ars_status->address = addr;
512f471f1a7SDan Williams 	ars_status->length = len;
513f471f1a7SDan Williams 	ars_status->type = ND_ARS_PERSISTENT;
5149fb1a190SDave Jiang 
5159fb1a190SDave Jiang 	spin_lock(&badrange->lock);
5169fb1a190SDave Jiang 	list_for_each_entry(be, &badrange->list, list) {
5179fb1a190SDave Jiang 		u64 be_end = be->start + be->length - 1;
5189fb1a190SDave Jiang 		u64 rstart, rend;
5199fb1a190SDave Jiang 
5209fb1a190SDave Jiang 		/* skip entries outside the range */
5219fb1a190SDave Jiang 		if (be_end < addr || be->start > end)
5229fb1a190SDave Jiang 			continue;
5239fb1a190SDave Jiang 
5249fb1a190SDave Jiang 		rstart = (be->start < addr) ? addr : be->start;
5259fb1a190SDave Jiang 		rend = (be_end < end) ? be_end : end;
5269fb1a190SDave Jiang 		ars_record = &ars_status->records[i];
527f471f1a7SDan Williams 		ars_record->handle = 0;
5289fb1a190SDave Jiang 		ars_record->err_address = rstart;
5299fb1a190SDave Jiang 		ars_record->length = rend - rstart + 1;
5309fb1a190SDave Jiang 		i++;
5319fb1a190SDave Jiang 	}
5329fb1a190SDave Jiang 	spin_unlock(&badrange->lock);
5339fb1a190SDave Jiang 	ars_status->num_records = i;
5349fb1a190SDave Jiang 	ars_status->out_length = sizeof(struct nd_cmd_ars_status)
5359fb1a190SDave Jiang 		+ i * sizeof(struct nd_ars_record);
536f471f1a7SDan Williams }
537f471f1a7SDan Williams 
5389fb1a190SDave Jiang static int nfit_test_cmd_ars_start(struct nfit_test *t,
5399fb1a190SDave Jiang 		struct ars_state *ars_state,
540f471f1a7SDan Williams 		struct nd_cmd_ars_start *ars_start, unsigned int buf_len,
541f471f1a7SDan Williams 		int *cmd_rc)
542f471f1a7SDan Williams {
543f471f1a7SDan Williams 	if (buf_len < sizeof(*ars_start))
54439c686b8SVishal Verma 		return -EINVAL;
54539c686b8SVishal Verma 
546f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
547f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
548f471f1a7SDan Williams 		ars_start->status = NFIT_ARS_START_BUSY;
549f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
550f471f1a7SDan Williams 	} else {
551f471f1a7SDan Williams 		ars_start->status = 0;
552f471f1a7SDan Williams 		ars_start->scrub_time = 1;
5539fb1a190SDave Jiang 		post_ars_status(ars_state, &t->badrange, ars_start->address,
554f471f1a7SDan Williams 				ars_start->length);
555f471f1a7SDan Williams 		*cmd_rc = 0;
556f471f1a7SDan Williams 	}
557f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
55839c686b8SVishal Verma 
55939c686b8SVishal Verma 	return 0;
56039c686b8SVishal Verma }
56139c686b8SVishal Verma 
562f471f1a7SDan Williams static int nfit_test_cmd_ars_status(struct ars_state *ars_state,
563f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status, unsigned int buf_len,
564f471f1a7SDan Williams 		int *cmd_rc)
56539c686b8SVishal Verma {
566f471f1a7SDan Williams 	if (buf_len < ars_state->ars_status->out_length)
56739c686b8SVishal Verma 		return -EINVAL;
56839c686b8SVishal Verma 
569f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
570f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
571f471f1a7SDan Williams 		memset(ars_status, 0, buf_len);
572f471f1a7SDan Williams 		ars_status->status = NFIT_ARS_STATUS_BUSY;
573f471f1a7SDan Williams 		ars_status->out_length = sizeof(*ars_status);
574f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
575f471f1a7SDan Williams 	} else {
576f471f1a7SDan Williams 		memcpy(ars_status, ars_state->ars_status,
577f471f1a7SDan Williams 				ars_state->ars_status->out_length);
578f471f1a7SDan Williams 		*cmd_rc = 0;
579f471f1a7SDan Williams 	}
580f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
58139c686b8SVishal Verma 	return 0;
58239c686b8SVishal Verma }
58339c686b8SVishal Verma 
5845e096ef3SVishal Verma static int nfit_test_cmd_clear_error(struct nfit_test *t,
5855e096ef3SVishal Verma 		struct nd_cmd_clear_error *clear_err,
586d4f32367SDan Williams 		unsigned int buf_len, int *cmd_rc)
587d4f32367SDan Williams {
588d4f32367SDan Williams 	const u64 mask = NFIT_TEST_CLEAR_ERR_UNIT - 1;
589d4f32367SDan Williams 	if (buf_len < sizeof(*clear_err))
590d4f32367SDan Williams 		return -EINVAL;
591d4f32367SDan Williams 
592d4f32367SDan Williams 	if ((clear_err->address & mask) || (clear_err->length & mask))
593d4f32367SDan Williams 		return -EINVAL;
594d4f32367SDan Williams 
5955e096ef3SVishal Verma 	badrange_forget(&t->badrange, clear_err->address, clear_err->length);
596d4f32367SDan Williams 	clear_err->status = 0;
597d4f32367SDan Williams 	clear_err->cleared = clear_err->length;
598d4f32367SDan Williams 	*cmd_rc = 0;
599d4f32367SDan Williams 	return 0;
600d4f32367SDan Williams }
601d4f32367SDan Williams 
60210246dc8SYasunori Goto struct region_search_spa {
60310246dc8SYasunori Goto 	u64 addr;
60410246dc8SYasunori Goto 	struct nd_region *region;
60510246dc8SYasunori Goto };
60610246dc8SYasunori Goto 
60710246dc8SYasunori Goto static int is_region_device(struct device *dev)
60810246dc8SYasunori Goto {
60910246dc8SYasunori Goto 	return !strncmp(dev->kobj.name, "region", 6);
61010246dc8SYasunori Goto }
61110246dc8SYasunori Goto 
61210246dc8SYasunori Goto static int nfit_test_search_region_spa(struct device *dev, void *data)
61310246dc8SYasunori Goto {
61410246dc8SYasunori Goto 	struct region_search_spa *ctx = data;
61510246dc8SYasunori Goto 	struct nd_region *nd_region;
61610246dc8SYasunori Goto 	resource_size_t ndr_end;
61710246dc8SYasunori Goto 
61810246dc8SYasunori Goto 	if (!is_region_device(dev))
61910246dc8SYasunori Goto 		return 0;
62010246dc8SYasunori Goto 
62110246dc8SYasunori Goto 	nd_region = to_nd_region(dev);
62210246dc8SYasunori Goto 	ndr_end = nd_region->ndr_start + nd_region->ndr_size;
62310246dc8SYasunori Goto 
62410246dc8SYasunori Goto 	if (ctx->addr >= nd_region->ndr_start && ctx->addr < ndr_end) {
62510246dc8SYasunori Goto 		ctx->region = nd_region;
62610246dc8SYasunori Goto 		return 1;
62710246dc8SYasunori Goto 	}
62810246dc8SYasunori Goto 
62910246dc8SYasunori Goto 	return 0;
63010246dc8SYasunori Goto }
63110246dc8SYasunori Goto 
63210246dc8SYasunori Goto static int nfit_test_search_spa(struct nvdimm_bus *bus,
63310246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa)
63410246dc8SYasunori Goto {
63510246dc8SYasunori Goto 	int ret;
63610246dc8SYasunori Goto 	struct nd_region *nd_region = NULL;
63710246dc8SYasunori Goto 	struct nvdimm *nvdimm = NULL;
63810246dc8SYasunori Goto 	struct nd_mapping *nd_mapping = NULL;
63910246dc8SYasunori Goto 	struct region_search_spa ctx = {
64010246dc8SYasunori Goto 		.addr = spa->spa,
64110246dc8SYasunori Goto 		.region = NULL,
64210246dc8SYasunori Goto 	};
64310246dc8SYasunori Goto 	u64 dpa;
64410246dc8SYasunori Goto 
64510246dc8SYasunori Goto 	ret = device_for_each_child(&bus->dev, &ctx,
64610246dc8SYasunori Goto 				nfit_test_search_region_spa);
64710246dc8SYasunori Goto 
64810246dc8SYasunori Goto 	if (!ret)
64910246dc8SYasunori Goto 		return -ENODEV;
65010246dc8SYasunori Goto 
65110246dc8SYasunori Goto 	nd_region = ctx.region;
65210246dc8SYasunori Goto 
65310246dc8SYasunori Goto 	dpa = ctx.addr - nd_region->ndr_start;
65410246dc8SYasunori Goto 
65510246dc8SYasunori Goto 	/*
65610246dc8SYasunori Goto 	 * last dimm is selected for test
65710246dc8SYasunori Goto 	 */
65810246dc8SYasunori Goto 	nd_mapping = &nd_region->mapping[nd_region->ndr_mappings - 1];
65910246dc8SYasunori Goto 	nvdimm = nd_mapping->nvdimm;
66010246dc8SYasunori Goto 
66110246dc8SYasunori Goto 	spa->devices[0].nfit_device_handle = handle[nvdimm->id];
66210246dc8SYasunori Goto 	spa->num_nvdimms = 1;
66310246dc8SYasunori Goto 	spa->devices[0].dpa = dpa;
66410246dc8SYasunori Goto 
66510246dc8SYasunori Goto 	return 0;
66610246dc8SYasunori Goto }
66710246dc8SYasunori Goto 
66810246dc8SYasunori Goto static int nfit_test_cmd_translate_spa(struct nvdimm_bus *bus,
66910246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa, unsigned int buf_len)
67010246dc8SYasunori Goto {
67110246dc8SYasunori Goto 	if (buf_len < spa->translate_length)
67210246dc8SYasunori Goto 		return -EINVAL;
67310246dc8SYasunori Goto 
67410246dc8SYasunori Goto 	if (nfit_test_search_spa(bus, spa) < 0 || !spa->num_nvdimms)
67510246dc8SYasunori Goto 		spa->status = 2;
67610246dc8SYasunori Goto 
67710246dc8SYasunori Goto 	return 0;
67810246dc8SYasunori Goto }
67910246dc8SYasunori Goto 
680ed07c433SDan Williams static int nfit_test_cmd_smart(struct nd_intel_smart *smart, unsigned int buf_len,
681ed07c433SDan Williams 		struct nd_intel_smart *smart_data)
682baa51277SDan Williams {
683baa51277SDan Williams 	if (buf_len < sizeof(*smart))
684baa51277SDan Williams 		return -EINVAL;
685ed07c433SDan Williams 	memcpy(smart, smart_data, sizeof(*smart));
686baa51277SDan Williams 	return 0;
687baa51277SDan Williams }
688baa51277SDan Williams 
689cdd77d3eSDan Williams static int nfit_test_cmd_smart_threshold(
690ed07c433SDan Williams 		struct nd_intel_smart_threshold *out,
691ed07c433SDan Williams 		unsigned int buf_len,
692ed07c433SDan Williams 		struct nd_intel_smart_threshold *smart_t)
693baa51277SDan Williams {
694baa51277SDan Williams 	if (buf_len < sizeof(*smart_t))
695baa51277SDan Williams 		return -EINVAL;
696ed07c433SDan Williams 	memcpy(out, smart_t, sizeof(*smart_t));
697ed07c433SDan Williams 	return 0;
698ed07c433SDan Williams }
699ed07c433SDan Williams 
700ed07c433SDan Williams static void smart_notify(struct device *bus_dev,
701ed07c433SDan Williams 		struct device *dimm_dev, struct nd_intel_smart *smart,
702ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh)
703ed07c433SDan Williams {
704ed07c433SDan Williams 	dev_dbg(dimm_dev, "%s: alarm: %#x spares: %d (%d) mtemp: %d (%d) ctemp: %d (%d)\n",
705ed07c433SDan Williams 			__func__, thresh->alarm_control, thresh->spares,
706ed07c433SDan Williams 			smart->spares, thresh->media_temperature,
707ed07c433SDan Williams 			smart->media_temperature, thresh->ctrl_temperature,
708ed07c433SDan Williams 			smart->ctrl_temperature);
709ed07c433SDan Williams 	if (((thresh->alarm_control & ND_INTEL_SMART_SPARE_TRIP)
710ed07c433SDan Williams 				&& smart->spares
711ed07c433SDan Williams 				<= thresh->spares)
712ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_TEMP_TRIP)
713ed07c433SDan Williams 				&& smart->media_temperature
714ed07c433SDan Williams 				>= thresh->media_temperature)
715ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_CTEMP_TRIP)
716ed07c433SDan Williams 				&& smart->ctrl_temperature
7174cf260fcSVishal Verma 				>= thresh->ctrl_temperature)
7184cf260fcSVishal Verma 			|| (smart->health != ND_INTEL_SMART_NON_CRITICAL_HEALTH)
7194cf260fcSVishal Verma 			|| (smart->shutdown_state != 0)) {
720ed07c433SDan Williams 		device_lock(bus_dev);
721ed07c433SDan Williams 		__acpi_nvdimm_notify(dimm_dev, 0x81);
722ed07c433SDan Williams 		device_unlock(bus_dev);
723ed07c433SDan Williams 	}
724ed07c433SDan Williams }
725ed07c433SDan Williams 
726ed07c433SDan Williams static int nfit_test_cmd_smart_set_threshold(
727ed07c433SDan Williams 		struct nd_intel_smart_set_threshold *in,
728ed07c433SDan Williams 		unsigned int buf_len,
729ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh,
730ed07c433SDan Williams 		struct nd_intel_smart *smart,
731ed07c433SDan Williams 		struct device *bus_dev, struct device *dimm_dev)
732ed07c433SDan Williams {
733ed07c433SDan Williams 	unsigned int size;
734ed07c433SDan Williams 
735ed07c433SDan Williams 	size = sizeof(*in) - 4;
736ed07c433SDan Williams 	if (buf_len < size)
737ed07c433SDan Williams 		return -EINVAL;
738ed07c433SDan Williams 	memcpy(thresh->data, in, size);
739ed07c433SDan Williams 	in->status = 0;
740ed07c433SDan Williams 	smart_notify(bus_dev, dimm_dev, smart, thresh);
741ed07c433SDan Williams 
742baa51277SDan Williams 	return 0;
743baa51277SDan Williams }
744baa51277SDan Williams 
7454cf260fcSVishal Verma static int nfit_test_cmd_smart_inject(
7464cf260fcSVishal Verma 		struct nd_intel_smart_inject *inj,
7474cf260fcSVishal Verma 		unsigned int buf_len,
7484cf260fcSVishal Verma 		struct nd_intel_smart_threshold *thresh,
7494cf260fcSVishal Verma 		struct nd_intel_smart *smart,
7504cf260fcSVishal Verma 		struct device *bus_dev, struct device *dimm_dev)
7514cf260fcSVishal Verma {
7524cf260fcSVishal Verma 	if (buf_len != sizeof(*inj))
7534cf260fcSVishal Verma 		return -EINVAL;
7544cf260fcSVishal Verma 
7554cf260fcSVishal Verma 	if (inj->mtemp_enable)
7564cf260fcSVishal Verma 		smart->media_temperature = inj->media_temperature;
7574cf260fcSVishal Verma 	if (inj->spare_enable)
7584cf260fcSVishal Verma 		smart->spares = inj->spares;
7594cf260fcSVishal Verma 	if (inj->fatal_enable)
7604cf260fcSVishal Verma 		smart->health = ND_INTEL_SMART_FATAL_HEALTH;
7614cf260fcSVishal Verma 	if (inj->unsafe_shutdown_enable) {
7624cf260fcSVishal Verma 		smart->shutdown_state = 1;
7634cf260fcSVishal Verma 		smart->shutdown_count++;
7644cf260fcSVishal Verma 	}
7654cf260fcSVishal Verma 	inj->status = 0;
7664cf260fcSVishal Verma 	smart_notify(bus_dev, dimm_dev, smart, thresh);
7674cf260fcSVishal Verma 
7684cf260fcSVishal Verma 	return 0;
7694cf260fcSVishal Verma }
7704cf260fcSVishal Verma 
7719fb1a190SDave Jiang static void uc_error_notify(struct work_struct *work)
7729fb1a190SDave Jiang {
7739fb1a190SDave Jiang 	struct nfit_test *t = container_of(work, typeof(*t), work);
7749fb1a190SDave Jiang 
7759fb1a190SDave Jiang 	__acpi_nfit_notify(&t->pdev.dev, t, NFIT_NOTIFY_UC_MEMORY_ERROR);
7769fb1a190SDave Jiang }
7779fb1a190SDave Jiang 
7789fb1a190SDave Jiang static int nfit_test_cmd_ars_error_inject(struct nfit_test *t,
7799fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj *err_inj, unsigned int buf_len)
7809fb1a190SDave Jiang {
7819fb1a190SDave Jiang 	int rc;
7829fb1a190SDave Jiang 
78341cb3301SVishal Verma 	if (buf_len != sizeof(*err_inj)) {
7849fb1a190SDave Jiang 		rc = -EINVAL;
7859fb1a190SDave Jiang 		goto err;
7869fb1a190SDave Jiang 	}
7879fb1a190SDave Jiang 
7889fb1a190SDave Jiang 	if (err_inj->err_inj_spa_range_length <= 0) {
7899fb1a190SDave Jiang 		rc = -EINVAL;
7909fb1a190SDave Jiang 		goto err;
7919fb1a190SDave Jiang 	}
7929fb1a190SDave Jiang 
7939fb1a190SDave Jiang 	rc =  badrange_add(&t->badrange, err_inj->err_inj_spa_range_base,
7949fb1a190SDave Jiang 			err_inj->err_inj_spa_range_length);
7959fb1a190SDave Jiang 	if (rc < 0)
7969fb1a190SDave Jiang 		goto err;
7979fb1a190SDave Jiang 
7989fb1a190SDave Jiang 	if (err_inj->err_inj_options & (1 << ND_ARS_ERR_INJ_OPT_NOTIFY))
7999fb1a190SDave Jiang 		queue_work(nfit_wq, &t->work);
8009fb1a190SDave Jiang 
8019fb1a190SDave Jiang 	err_inj->status = 0;
8029fb1a190SDave Jiang 	return 0;
8039fb1a190SDave Jiang 
8049fb1a190SDave Jiang err:
8059fb1a190SDave Jiang 	err_inj->status = NFIT_ARS_INJECT_INVALID;
8069fb1a190SDave Jiang 	return rc;
8079fb1a190SDave Jiang }
8089fb1a190SDave Jiang 
8099fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_clear(struct nfit_test *t,
8109fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_clr *err_clr, unsigned int buf_len)
8119fb1a190SDave Jiang {
8129fb1a190SDave Jiang 	int rc;
8139fb1a190SDave Jiang 
81441cb3301SVishal Verma 	if (buf_len != sizeof(*err_clr)) {
8159fb1a190SDave Jiang 		rc = -EINVAL;
8169fb1a190SDave Jiang 		goto err;
8179fb1a190SDave Jiang 	}
8189fb1a190SDave Jiang 
8199fb1a190SDave Jiang 	if (err_clr->err_inj_clr_spa_range_length <= 0) {
8209fb1a190SDave Jiang 		rc = -EINVAL;
8219fb1a190SDave Jiang 		goto err;
8229fb1a190SDave Jiang 	}
8239fb1a190SDave Jiang 
8249fb1a190SDave Jiang 	badrange_forget(&t->badrange, err_clr->err_inj_clr_spa_range_base,
8259fb1a190SDave Jiang 			err_clr->err_inj_clr_spa_range_length);
8269fb1a190SDave Jiang 
8279fb1a190SDave Jiang 	err_clr->status = 0;
8289fb1a190SDave Jiang 	return 0;
8299fb1a190SDave Jiang 
8309fb1a190SDave Jiang err:
8319fb1a190SDave Jiang 	err_clr->status = NFIT_ARS_INJECT_INVALID;
8329fb1a190SDave Jiang 	return rc;
8339fb1a190SDave Jiang }
8349fb1a190SDave Jiang 
8359fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_status(struct nfit_test *t,
8369fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_stat *err_stat,
8379fb1a190SDave Jiang 		unsigned int buf_len)
8389fb1a190SDave Jiang {
8399fb1a190SDave Jiang 	struct badrange_entry *be;
8409fb1a190SDave Jiang 	int max = SZ_4K / sizeof(struct nd_error_stat_query_record);
8419fb1a190SDave Jiang 	int i = 0;
8429fb1a190SDave Jiang 
8439fb1a190SDave Jiang 	err_stat->status = 0;
8449fb1a190SDave Jiang 	spin_lock(&t->badrange.lock);
8459fb1a190SDave Jiang 	list_for_each_entry(be, &t->badrange.list, list) {
8469fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_base = be->start;
8479fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_length = be->length;
8489fb1a190SDave Jiang 		i++;
8499fb1a190SDave Jiang 		if (i > max)
8509fb1a190SDave Jiang 			break;
8519fb1a190SDave Jiang 	}
8529fb1a190SDave Jiang 	spin_unlock(&t->badrange.lock);
8539fb1a190SDave Jiang 	err_stat->inj_err_rec_count = i;
8549fb1a190SDave Jiang 
8559fb1a190SDave Jiang 	return 0;
8569fb1a190SDave Jiang }
8579fb1a190SDave Jiang 
858674d8bdeSDave Jiang static int nd_intel_test_cmd_set_lss_status(struct nfit_test *t,
859674d8bdeSDave Jiang 		struct nd_intel_lss *nd_cmd, unsigned int buf_len)
860674d8bdeSDave Jiang {
861674d8bdeSDave Jiang 	struct device *dev = &t->pdev.dev;
862674d8bdeSDave Jiang 
863674d8bdeSDave Jiang 	if (buf_len < sizeof(*nd_cmd))
864674d8bdeSDave Jiang 		return -EINVAL;
865674d8bdeSDave Jiang 
866674d8bdeSDave Jiang 	switch (nd_cmd->enable) {
867674d8bdeSDave Jiang 	case 0:
868674d8bdeSDave Jiang 		nd_cmd->status = 0;
869674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status disabled\n",
870674d8bdeSDave Jiang 				__func__);
871674d8bdeSDave Jiang 		break;
872674d8bdeSDave Jiang 	case 1:
873674d8bdeSDave Jiang 		nd_cmd->status = 0;
874674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status enabled\n",
875674d8bdeSDave Jiang 				__func__);
876674d8bdeSDave Jiang 		break;
877674d8bdeSDave Jiang 	default:
878674d8bdeSDave Jiang 		dev_warn(dev, "Unknown enable value: %#x\n", nd_cmd->enable);
879674d8bdeSDave Jiang 		nd_cmd->status = 0x3;
880674d8bdeSDave Jiang 		break;
881674d8bdeSDave Jiang 	}
882674d8bdeSDave Jiang 
883674d8bdeSDave Jiang 
884674d8bdeSDave Jiang 	return 0;
885674d8bdeSDave Jiang }
886674d8bdeSDave Jiang 
887bfbaa952SDave Jiang static int get_dimm(struct nfit_mem *nfit_mem, unsigned int func)
888bfbaa952SDave Jiang {
889bfbaa952SDave Jiang 	int i;
890bfbaa952SDave Jiang 
891bfbaa952SDave Jiang 	/* lookup per-dimm data */
892bfbaa952SDave Jiang 	for (i = 0; i < ARRAY_SIZE(handle); i++)
893bfbaa952SDave Jiang 		if (__to_nfit_memdev(nfit_mem)->device_handle == handle[i])
894bfbaa952SDave Jiang 			break;
895bfbaa952SDave Jiang 	if (i >= ARRAY_SIZE(handle))
896bfbaa952SDave Jiang 		return -ENXIO;
897bfbaa952SDave Jiang 
89855c72ab6SDan Williams 	if ((1 << func) & dimm_fail_cmd_flags[i]) {
89955c72ab6SDan Williams 		if (dimm_fail_cmd_code[i])
90055c72ab6SDan Williams 			return dimm_fail_cmd_code[i];
901bfbaa952SDave Jiang 		return -EIO;
90255c72ab6SDan Williams 	}
903bfbaa952SDave Jiang 
904bfbaa952SDave Jiang 	return i;
905bfbaa952SDave Jiang }
906bfbaa952SDave Jiang 
90739c686b8SVishal Verma static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
90839c686b8SVishal Verma 		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
909aef25338SDan Williams 		unsigned int buf_len, int *cmd_rc)
91039c686b8SVishal Verma {
91139c686b8SVishal Verma 	struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
91239c686b8SVishal Verma 	struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
9136634fb06SDan Williams 	unsigned int func = cmd;
914f471f1a7SDan Williams 	int i, rc = 0, __cmd_rc;
915f471f1a7SDan Williams 
916f471f1a7SDan Williams 	if (!cmd_rc)
917f471f1a7SDan Williams 		cmd_rc = &__cmd_rc;
918f471f1a7SDan Williams 	*cmd_rc = 0;
91939c686b8SVishal Verma 
92039c686b8SVishal Verma 	if (nvdimm) {
92139c686b8SVishal Verma 		struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
922e3654ecaSDan Williams 		unsigned long cmd_mask = nvdimm_cmd_mask(nvdimm);
92339c686b8SVishal Verma 
9246634fb06SDan Williams 		if (!nfit_mem)
9256634fb06SDan Williams 			return -ENOTTY;
9266634fb06SDan Williams 
9276634fb06SDan Williams 		if (cmd == ND_CMD_CALL) {
9286634fb06SDan Williams 			struct nd_cmd_pkg *call_pkg = buf;
9296634fb06SDan Williams 
9306634fb06SDan Williams 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
9316634fb06SDan Williams 			buf = (void *) call_pkg->nd_payload;
9326634fb06SDan Williams 			func = call_pkg->nd_command;
9336634fb06SDan Williams 			if (call_pkg->nd_family != nfit_mem->family)
9346634fb06SDan Williams 				return -ENOTTY;
935bfbaa952SDave Jiang 
936bfbaa952SDave Jiang 			i = get_dimm(nfit_mem, func);
937bfbaa952SDave Jiang 			if (i < 0)
938bfbaa952SDave Jiang 				return i;
939bfbaa952SDave Jiang 
940bfbaa952SDave Jiang 			switch (func) {
941674d8bdeSDave Jiang 			case ND_INTEL_ENABLE_LSS_STATUS:
942674d8bdeSDave Jiang 				return nd_intel_test_cmd_set_lss_status(t,
943674d8bdeSDave Jiang 						buf, buf_len);
944bfbaa952SDave Jiang 			case ND_INTEL_FW_GET_INFO:
945bfbaa952SDave Jiang 				return nd_intel_test_get_fw_info(t, buf,
946bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
947bfbaa952SDave Jiang 			case ND_INTEL_FW_START_UPDATE:
948bfbaa952SDave Jiang 				return nd_intel_test_start_update(t, buf,
949bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
950bfbaa952SDave Jiang 			case ND_INTEL_FW_SEND_DATA:
951bfbaa952SDave Jiang 				return nd_intel_test_send_data(t, buf,
952bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
953bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_UPDATE:
954bfbaa952SDave Jiang 				return nd_intel_test_finish_fw(t, buf,
955bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
956bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_QUERY:
957bfbaa952SDave Jiang 				return nd_intel_test_finish_query(t, buf,
958bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
959bfbaa952SDave Jiang 			case ND_INTEL_SMART:
960bfbaa952SDave Jiang 				return nfit_test_cmd_smart(buf, buf_len,
961bfbaa952SDave Jiang 						&t->smart[i - t->dcr_idx]);
962bfbaa952SDave Jiang 			case ND_INTEL_SMART_THRESHOLD:
963bfbaa952SDave Jiang 				return nfit_test_cmd_smart_threshold(buf,
964bfbaa952SDave Jiang 						buf_len,
965bfbaa952SDave Jiang 						&t->smart_threshold[i -
966bfbaa952SDave Jiang 							t->dcr_idx]);
967bfbaa952SDave Jiang 			case ND_INTEL_SMART_SET_THRESHOLD:
968bfbaa952SDave Jiang 				return nfit_test_cmd_smart_set_threshold(buf,
969bfbaa952SDave Jiang 						buf_len,
970bfbaa952SDave Jiang 						&t->smart_threshold[i -
971bfbaa952SDave Jiang 							t->dcr_idx],
972bfbaa952SDave Jiang 						&t->smart[i - t->dcr_idx],
973bfbaa952SDave Jiang 						&t->pdev.dev, t->dimm_dev[i]);
9744cf260fcSVishal Verma 			case ND_INTEL_SMART_INJECT:
9754cf260fcSVishal Verma 				return nfit_test_cmd_smart_inject(buf,
9764cf260fcSVishal Verma 						buf_len,
9774cf260fcSVishal Verma 						&t->smart_threshold[i -
9784cf260fcSVishal Verma 							t->dcr_idx],
9794cf260fcSVishal Verma 						&t->smart[i - t->dcr_idx],
9804cf260fcSVishal Verma 						&t->pdev.dev, t->dimm_dev[i]);
981bfbaa952SDave Jiang 			default:
982bfbaa952SDave Jiang 				return -ENOTTY;
983bfbaa952SDave Jiang 			}
9846634fb06SDan Williams 		}
9856634fb06SDan Williams 
9866634fb06SDan Williams 		if (!test_bit(cmd, &cmd_mask)
9876634fb06SDan Williams 				|| !test_bit(func, &nfit_mem->dsm_mask))
98839c686b8SVishal Verma 			return -ENOTTY;
98939c686b8SVishal Verma 
990bfbaa952SDave Jiang 		i = get_dimm(nfit_mem, func);
991bfbaa952SDave Jiang 		if (i < 0)
992bfbaa952SDave Jiang 			return i;
99373606afdSDan Williams 
9946634fb06SDan Williams 		switch (func) {
99539c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_SIZE:
99639c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_size(buf, buf_len);
99739c686b8SVishal Verma 			break;
99839c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_DATA:
99939c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_data(buf, buf_len,
1000dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
100139c686b8SVishal Verma 			break;
100239c686b8SVishal Verma 		case ND_CMD_SET_CONFIG_DATA:
100339c686b8SVishal Verma 			rc = nfit_test_cmd_set_config_data(buf, buf_len,
1004dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
100539c686b8SVishal Verma 			break;
10066bc75619SDan Williams 		default:
10076bc75619SDan Williams 			return -ENOTTY;
10086bc75619SDan Williams 		}
100939c686b8SVishal Verma 	} else {
1010f471f1a7SDan Williams 		struct ars_state *ars_state = &t->ars_state;
101110246dc8SYasunori Goto 		struct nd_cmd_pkg *call_pkg = buf;
101210246dc8SYasunori Goto 
101310246dc8SYasunori Goto 		if (!nd_desc)
101410246dc8SYasunori Goto 			return -ENOTTY;
101510246dc8SYasunori Goto 
101610246dc8SYasunori Goto 		if (cmd == ND_CMD_CALL) {
101710246dc8SYasunori Goto 			func = call_pkg->nd_command;
101810246dc8SYasunori Goto 
101910246dc8SYasunori Goto 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
102010246dc8SYasunori Goto 			buf = (void *) call_pkg->nd_payload;
102110246dc8SYasunori Goto 
102210246dc8SYasunori Goto 			switch (func) {
102310246dc8SYasunori Goto 			case NFIT_CMD_TRANSLATE_SPA:
102410246dc8SYasunori Goto 				rc = nfit_test_cmd_translate_spa(
102510246dc8SYasunori Goto 					acpi_desc->nvdimm_bus, buf, buf_len);
102610246dc8SYasunori Goto 				return rc;
10279fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_SET:
10289fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_error_inject(t, buf,
10299fb1a190SDave Jiang 					buf_len);
10309fb1a190SDave Jiang 				return rc;
10319fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_CLEAR:
10329fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_clear(t, buf,
10339fb1a190SDave Jiang 					buf_len);
10349fb1a190SDave Jiang 				return rc;
10359fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_GET:
10369fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_status(t, buf,
10379fb1a190SDave Jiang 					buf_len);
10389fb1a190SDave Jiang 				return rc;
103910246dc8SYasunori Goto 			default:
104010246dc8SYasunori Goto 				return -ENOTTY;
104110246dc8SYasunori Goto 			}
104210246dc8SYasunori Goto 		}
1043f471f1a7SDan Williams 
1044e3654ecaSDan Williams 		if (!nd_desc || !test_bit(cmd, &nd_desc->cmd_mask))
104539c686b8SVishal Verma 			return -ENOTTY;
104639c686b8SVishal Verma 
10476634fb06SDan Williams 		switch (func) {
104839c686b8SVishal Verma 		case ND_CMD_ARS_CAP:
104939c686b8SVishal Verma 			rc = nfit_test_cmd_ars_cap(buf, buf_len);
105039c686b8SVishal Verma 			break;
105139c686b8SVishal Verma 		case ND_CMD_ARS_START:
10529fb1a190SDave Jiang 			rc = nfit_test_cmd_ars_start(t, ars_state, buf,
10539fb1a190SDave Jiang 					buf_len, cmd_rc);
105439c686b8SVishal Verma 			break;
105539c686b8SVishal Verma 		case ND_CMD_ARS_STATUS:
1056f471f1a7SDan Williams 			rc = nfit_test_cmd_ars_status(ars_state, buf, buf_len,
1057f471f1a7SDan Williams 					cmd_rc);
105839c686b8SVishal Verma 			break;
1059d4f32367SDan Williams 		case ND_CMD_CLEAR_ERROR:
10605e096ef3SVishal Verma 			rc = nfit_test_cmd_clear_error(t, buf, buf_len, cmd_rc);
1061d4f32367SDan Williams 			break;
106239c686b8SVishal Verma 		default:
106339c686b8SVishal Verma 			return -ENOTTY;
106439c686b8SVishal Verma 		}
106539c686b8SVishal Verma 	}
10666bc75619SDan Williams 
10676bc75619SDan Williams 	return rc;
10686bc75619SDan Williams }
10696bc75619SDan Williams 
10706bc75619SDan Williams static DEFINE_SPINLOCK(nfit_test_lock);
10716bc75619SDan Williams static struct nfit_test *instances[NUM_NFITS];
10726bc75619SDan Williams 
10736bc75619SDan Williams static void release_nfit_res(void *data)
10746bc75619SDan Williams {
10756bc75619SDan Williams 	struct nfit_test_resource *nfit_res = data;
10766bc75619SDan Williams 
10776bc75619SDan Williams 	spin_lock(&nfit_test_lock);
10786bc75619SDan Williams 	list_del(&nfit_res->list);
10796bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
10806bc75619SDan Williams 
10816bc75619SDan Williams 	vfree(nfit_res->buf);
10826bc75619SDan Williams 	kfree(nfit_res);
10836bc75619SDan Williams }
10846bc75619SDan Williams 
10856bc75619SDan Williams static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
10866bc75619SDan Williams 		void *buf)
10876bc75619SDan Williams {
10886bc75619SDan Williams 	struct device *dev = &t->pdev.dev;
10896bc75619SDan Williams 	struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
10906bc75619SDan Williams 			GFP_KERNEL);
10916bc75619SDan Williams 	int rc;
10926bc75619SDan Williams 
1093bd4cd745SDan Williams 	if (!buf || !nfit_res)
10946bc75619SDan Williams 		goto err;
10956bc75619SDan Williams 	rc = devm_add_action(dev, release_nfit_res, nfit_res);
10966bc75619SDan Williams 	if (rc)
10976bc75619SDan Williams 		goto err;
10986bc75619SDan Williams 	INIT_LIST_HEAD(&nfit_res->list);
10996bc75619SDan Williams 	memset(buf, 0, size);
11006bc75619SDan Williams 	nfit_res->dev = dev;
11016bc75619SDan Williams 	nfit_res->buf = buf;
1102bd4cd745SDan Williams 	nfit_res->res.start = *dma;
1103bd4cd745SDan Williams 	nfit_res->res.end = *dma + size - 1;
1104bd4cd745SDan Williams 	nfit_res->res.name = "NFIT";
1105bd4cd745SDan Williams 	spin_lock_init(&nfit_res->lock);
1106bd4cd745SDan Williams 	INIT_LIST_HEAD(&nfit_res->requests);
11076bc75619SDan Williams 	spin_lock(&nfit_test_lock);
11086bc75619SDan Williams 	list_add(&nfit_res->list, &t->resources);
11096bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
11106bc75619SDan Williams 
11116bc75619SDan Williams 	return nfit_res->buf;
11126bc75619SDan Williams  err:
1113ee8520feSDan Williams 	if (buf)
11146bc75619SDan Williams 		vfree(buf);
11156bc75619SDan Williams 	kfree(nfit_res);
11166bc75619SDan Williams 	return NULL;
11176bc75619SDan Williams }
11186bc75619SDan Williams 
11196bc75619SDan Williams static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
11206bc75619SDan Williams {
11216bc75619SDan Williams 	void *buf = vmalloc(size);
11226bc75619SDan Williams 
11236bc75619SDan Williams 	*dma = (unsigned long) buf;
11246bc75619SDan Williams 	return __test_alloc(t, size, dma, buf);
11256bc75619SDan Williams }
11266bc75619SDan Williams 
11276bc75619SDan Williams static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
11286bc75619SDan Williams {
11296bc75619SDan Williams 	int i;
11306bc75619SDan Williams 
11316bc75619SDan Williams 	for (i = 0; i < ARRAY_SIZE(instances); i++) {
11326bc75619SDan Williams 		struct nfit_test_resource *n, *nfit_res = NULL;
11336bc75619SDan Williams 		struct nfit_test *t = instances[i];
11346bc75619SDan Williams 
11356bc75619SDan Williams 		if (!t)
11366bc75619SDan Williams 			continue;
11376bc75619SDan Williams 		spin_lock(&nfit_test_lock);
11386bc75619SDan Williams 		list_for_each_entry(n, &t->resources, list) {
1139bd4cd745SDan Williams 			if (addr >= n->res.start && (addr < n->res.start
1140bd4cd745SDan Williams 						+ resource_size(&n->res))) {
11416bc75619SDan Williams 				nfit_res = n;
11426bc75619SDan Williams 				break;
11436bc75619SDan Williams 			} else if (addr >= (unsigned long) n->buf
11446bc75619SDan Williams 					&& (addr < (unsigned long) n->buf
1145bd4cd745SDan Williams 						+ resource_size(&n->res))) {
11466bc75619SDan Williams 				nfit_res = n;
11476bc75619SDan Williams 				break;
11486bc75619SDan Williams 			}
11496bc75619SDan Williams 		}
11506bc75619SDan Williams 		spin_unlock(&nfit_test_lock);
11516bc75619SDan Williams 		if (nfit_res)
11526bc75619SDan Williams 			return nfit_res;
11536bc75619SDan Williams 	}
11546bc75619SDan Williams 
11556bc75619SDan Williams 	return NULL;
11566bc75619SDan Williams }
11576bc75619SDan Williams 
1158f471f1a7SDan Williams static int ars_state_init(struct device *dev, struct ars_state *ars_state)
1159f471f1a7SDan Williams {
11609fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
1161f471f1a7SDan Williams 	ars_state->ars_status = devm_kzalloc(dev,
11629fb1a190SDave Jiang 			sizeof(struct nd_cmd_ars_status) + SZ_4K, GFP_KERNEL);
1163f471f1a7SDan Williams 	if (!ars_state->ars_status)
1164f471f1a7SDan Williams 		return -ENOMEM;
1165f471f1a7SDan Williams 	spin_lock_init(&ars_state->lock);
1166f471f1a7SDan Williams 	return 0;
1167f471f1a7SDan Williams }
1168f471f1a7SDan Williams 
1169231bf117SDan Williams static void put_dimms(void *data)
1170231bf117SDan Williams {
1171718fda67SDan Williams 	struct nfit_test *t = data;
1172231bf117SDan Williams 	int i;
1173231bf117SDan Williams 
1174718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++)
1175718fda67SDan Williams 		if (t->dimm_dev[i])
1176718fda67SDan Williams 			device_unregister(t->dimm_dev[i]);
1177231bf117SDan Williams }
1178231bf117SDan Williams 
1179231bf117SDan Williams static struct class *nfit_test_dimm;
1180231bf117SDan Williams 
118173606afdSDan Williams static int dimm_name_to_id(struct device *dev)
118273606afdSDan Williams {
118373606afdSDan Williams 	int dimm;
118473606afdSDan Williams 
1185718fda67SDan Williams 	if (sscanf(dev_name(dev), "test_dimm%d", &dimm) != 1)
118673606afdSDan Williams 		return -ENXIO;
118773606afdSDan Williams 	return dimm;
118873606afdSDan Williams }
118973606afdSDan Williams 
119073606afdSDan Williams static ssize_t handle_show(struct device *dev, struct device_attribute *attr,
119173606afdSDan Williams 		char *buf)
119273606afdSDan Williams {
119373606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
119473606afdSDan Williams 
119573606afdSDan Williams 	if (dimm < 0)
119673606afdSDan Williams 		return dimm;
119773606afdSDan Williams 
119819357a68SDan Williams 	return sprintf(buf, "%#x\n", handle[dimm]);
119973606afdSDan Williams }
120073606afdSDan Williams DEVICE_ATTR_RO(handle);
120173606afdSDan Williams 
120273606afdSDan Williams static ssize_t fail_cmd_show(struct device *dev, struct device_attribute *attr,
120373606afdSDan Williams 		char *buf)
120473606afdSDan Williams {
120573606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
120673606afdSDan Williams 
120773606afdSDan Williams 	if (dimm < 0)
120873606afdSDan Williams 		return dimm;
120973606afdSDan Williams 
121073606afdSDan Williams 	return sprintf(buf, "%#lx\n", dimm_fail_cmd_flags[dimm]);
121173606afdSDan Williams }
121273606afdSDan Williams 
121373606afdSDan Williams static ssize_t fail_cmd_store(struct device *dev, struct device_attribute *attr,
121473606afdSDan Williams 		const char *buf, size_t size)
121573606afdSDan Williams {
121673606afdSDan Williams 	int dimm = dimm_name_to_id(dev);
121773606afdSDan Williams 	unsigned long val;
121873606afdSDan Williams 	ssize_t rc;
121973606afdSDan Williams 
122073606afdSDan Williams 	if (dimm < 0)
122173606afdSDan Williams 		return dimm;
122273606afdSDan Williams 
122373606afdSDan Williams 	rc = kstrtol(buf, 0, &val);
122473606afdSDan Williams 	if (rc)
122573606afdSDan Williams 		return rc;
122673606afdSDan Williams 
122773606afdSDan Williams 	dimm_fail_cmd_flags[dimm] = val;
122873606afdSDan Williams 	return size;
122973606afdSDan Williams }
123073606afdSDan Williams static DEVICE_ATTR_RW(fail_cmd);
123173606afdSDan Williams 
123255c72ab6SDan Williams static ssize_t fail_cmd_code_show(struct device *dev, struct device_attribute *attr,
123355c72ab6SDan Williams 		char *buf)
123455c72ab6SDan Williams {
123555c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
123655c72ab6SDan Williams 
123755c72ab6SDan Williams 	if (dimm < 0)
123855c72ab6SDan Williams 		return dimm;
123955c72ab6SDan Williams 
124055c72ab6SDan Williams 	return sprintf(buf, "%d\n", dimm_fail_cmd_code[dimm]);
124155c72ab6SDan Williams }
124255c72ab6SDan Williams 
124355c72ab6SDan Williams static ssize_t fail_cmd_code_store(struct device *dev, struct device_attribute *attr,
124455c72ab6SDan Williams 		const char *buf, size_t size)
124555c72ab6SDan Williams {
124655c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
124755c72ab6SDan Williams 	unsigned long val;
124855c72ab6SDan Williams 	ssize_t rc;
124955c72ab6SDan Williams 
125055c72ab6SDan Williams 	if (dimm < 0)
125155c72ab6SDan Williams 		return dimm;
125255c72ab6SDan Williams 
125355c72ab6SDan Williams 	rc = kstrtol(buf, 0, &val);
125455c72ab6SDan Williams 	if (rc)
125555c72ab6SDan Williams 		return rc;
125655c72ab6SDan Williams 
125755c72ab6SDan Williams 	dimm_fail_cmd_code[dimm] = val;
125855c72ab6SDan Williams 	return size;
125955c72ab6SDan Williams }
126055c72ab6SDan Williams static DEVICE_ATTR_RW(fail_cmd_code);
126155c72ab6SDan Williams 
126273606afdSDan Williams static struct attribute *nfit_test_dimm_attributes[] = {
126373606afdSDan Williams 	&dev_attr_fail_cmd.attr,
126455c72ab6SDan Williams 	&dev_attr_fail_cmd_code.attr,
126573606afdSDan Williams 	&dev_attr_handle.attr,
126673606afdSDan Williams 	NULL,
126773606afdSDan Williams };
126873606afdSDan Williams 
126973606afdSDan Williams static struct attribute_group nfit_test_dimm_attribute_group = {
127073606afdSDan Williams 	.attrs = nfit_test_dimm_attributes,
127173606afdSDan Williams };
127273606afdSDan Williams 
127373606afdSDan Williams static const struct attribute_group *nfit_test_dimm_attribute_groups[] = {
127473606afdSDan Williams 	&nfit_test_dimm_attribute_group,
127573606afdSDan Williams 	NULL,
127673606afdSDan Williams };
127773606afdSDan Williams 
1278718fda67SDan Williams static int nfit_test_dimm_init(struct nfit_test *t)
1279718fda67SDan Williams {
1280718fda67SDan Williams 	int i;
1281718fda67SDan Williams 
1282718fda67SDan Williams 	if (devm_add_action_or_reset(&t->pdev.dev, put_dimms, t))
1283718fda67SDan Williams 		return -ENOMEM;
1284718fda67SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1285718fda67SDan Williams 		t->dimm_dev[i] = device_create_with_groups(nfit_test_dimm,
1286718fda67SDan Williams 				&t->pdev.dev, 0, NULL,
1287718fda67SDan Williams 				nfit_test_dimm_attribute_groups,
1288718fda67SDan Williams 				"test_dimm%d", i + t->dcr_idx);
1289718fda67SDan Williams 		if (!t->dimm_dev[i])
1290718fda67SDan Williams 			return -ENOMEM;
1291718fda67SDan Williams 	}
1292718fda67SDan Williams 	return 0;
1293718fda67SDan Williams }
1294718fda67SDan Williams 
1295ed07c433SDan Williams static void smart_init(struct nfit_test *t)
1296ed07c433SDan Williams {
1297ed07c433SDan Williams 	int i;
1298ed07c433SDan Williams 	const struct nd_intel_smart_threshold smart_t_data = {
1299ed07c433SDan Williams 		.alarm_control = ND_INTEL_SMART_SPARE_TRIP
1300ed07c433SDan Williams 			| ND_INTEL_SMART_TEMP_TRIP,
1301ed07c433SDan Williams 		.media_temperature = 40 * 16,
1302ed07c433SDan Williams 		.ctrl_temperature = 30 * 16,
1303ed07c433SDan Williams 		.spares = 5,
1304ed07c433SDan Williams 	};
1305ed07c433SDan Williams 	const struct nd_intel_smart smart_data = {
1306ed07c433SDan Williams 		.flags = ND_INTEL_SMART_HEALTH_VALID
1307ed07c433SDan Williams 			| ND_INTEL_SMART_SPARES_VALID
1308ed07c433SDan Williams 			| ND_INTEL_SMART_ALARM_VALID
1309ed07c433SDan Williams 			| ND_INTEL_SMART_USED_VALID
1310ed07c433SDan Williams 			| ND_INTEL_SMART_SHUTDOWN_VALID
1311ed07c433SDan Williams 			| ND_INTEL_SMART_MTEMP_VALID,
1312ed07c433SDan Williams 		.health = ND_INTEL_SMART_NON_CRITICAL_HEALTH,
1313ed07c433SDan Williams 		.media_temperature = 23 * 16,
1314f6adcca0SVishal Verma 		.ctrl_temperature = 25 * 16,
1315ed07c433SDan Williams 		.pmic_temperature = 40 * 16,
1316ed07c433SDan Williams 		.spares = 75,
1317ed07c433SDan Williams 		.alarm_flags = ND_INTEL_SMART_SPARE_TRIP
1318ed07c433SDan Williams 			| ND_INTEL_SMART_TEMP_TRIP,
1319ed07c433SDan Williams 		.ait_status = 1,
1320ed07c433SDan Williams 		.life_used = 5,
1321ed07c433SDan Williams 		.shutdown_state = 0,
1322ed07c433SDan Williams 		.vendor_size = 0,
1323ed07c433SDan Williams 		.shutdown_count = 100,
1324ed07c433SDan Williams 	};
1325ed07c433SDan Williams 
1326ed07c433SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1327ed07c433SDan Williams 		memcpy(&t->smart[i], &smart_data, sizeof(smart_data));
1328ed07c433SDan Williams 		memcpy(&t->smart_threshold[i], &smart_t_data,
1329ed07c433SDan Williams 				sizeof(smart_t_data));
1330ed07c433SDan Williams 	}
1331ed07c433SDan Williams }
1332ed07c433SDan Williams 
13336bc75619SDan Williams static int nfit_test0_alloc(struct nfit_test *t)
13346bc75619SDan Williams {
13356b577c9dSLinda Knippers 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * NUM_SPA
13366bc75619SDan Williams 			+ sizeof(struct acpi_nfit_memory_map) * NUM_MEM
13376bc75619SDan Williams 			+ sizeof(struct acpi_nfit_control_region) * NUM_DCR
13383b87356fSDan Williams 			+ offsetof(struct acpi_nfit_control_region,
13393b87356fSDan Williams 					window_size) * NUM_DCR
13409d27a87eSDan Williams 			+ sizeof(struct acpi_nfit_data_region) * NUM_BDW
134185d3fa02SDan Williams 			+ (sizeof(struct acpi_nfit_flush_address)
1342f81e1d35SDave Jiang 					+ sizeof(u64) * NUM_HINTS) * NUM_DCR
1343f81e1d35SDave Jiang 			+ sizeof(struct acpi_nfit_capabilities);
13446bc75619SDan Williams 	int i;
13456bc75619SDan Williams 
13466bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
13476bc75619SDan Williams 	if (!t->nfit_buf)
13486bc75619SDan Williams 		return -ENOMEM;
13496bc75619SDan Williams 	t->nfit_size = nfit_size;
13506bc75619SDan Williams 
1351ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[0]);
13526bc75619SDan Williams 	if (!t->spa_set[0])
13536bc75619SDan Williams 		return -ENOMEM;
13546bc75619SDan Williams 
1355ee8520feSDan Williams 	t->spa_set[1] = test_alloc(t, SPA1_SIZE, &t->spa_set_dma[1]);
13566bc75619SDan Williams 	if (!t->spa_set[1])
13576bc75619SDan Williams 		return -ENOMEM;
13586bc75619SDan Williams 
1359ee8520feSDan Williams 	t->spa_set[2] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[2]);
136020985164SVishal Verma 	if (!t->spa_set[2])
136120985164SVishal Verma 		return -ENOMEM;
136220985164SVishal Verma 
1363dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
13646bc75619SDan Williams 		t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
13656bc75619SDan Williams 		if (!t->dimm[i])
13666bc75619SDan Williams 			return -ENOMEM;
13676bc75619SDan Williams 
13686bc75619SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
13696bc75619SDan Williams 		if (!t->label[i])
13706bc75619SDan Williams 			return -ENOMEM;
13716bc75619SDan Williams 		sprintf(t->label[i], "label%d", i);
13729d27a87eSDan Williams 
13739d15ce9cSDan Williams 		t->flush[i] = test_alloc(t, max(PAGE_SIZE,
13749d15ce9cSDan Williams 					sizeof(u64) * NUM_HINTS),
137585d3fa02SDan Williams 				&t->flush_dma[i]);
13769d27a87eSDan Williams 		if (!t->flush[i])
13779d27a87eSDan Williams 			return -ENOMEM;
13786bc75619SDan Williams 	}
13796bc75619SDan Williams 
1380dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
13816bc75619SDan Williams 		t->dcr[i] = test_alloc(t, LABEL_SIZE, &t->dcr_dma[i]);
13826bc75619SDan Williams 		if (!t->dcr[i])
13836bc75619SDan Williams 			return -ENOMEM;
13846bc75619SDan Williams 	}
13856bc75619SDan Williams 
1386c14a868aSDan Williams 	t->_fit = test_alloc(t, sizeof(union acpi_object **), &t->_fit_dma);
1387c14a868aSDan Williams 	if (!t->_fit)
1388c14a868aSDan Williams 		return -ENOMEM;
1389c14a868aSDan Williams 
1390718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1391231bf117SDan Williams 		return -ENOMEM;
1392ed07c433SDan Williams 	smart_init(t);
1393f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
13946bc75619SDan Williams }
13956bc75619SDan Williams 
13966bc75619SDan Williams static int nfit_test1_alloc(struct nfit_test *t)
13976bc75619SDan Williams {
13987bfe97c7SDan Williams 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * 2
1399ac40b675SDan Williams 		+ sizeof(struct acpi_nfit_memory_map) * 2
1400ac40b675SDan Williams 		+ offsetof(struct acpi_nfit_control_region, window_size) * 2;
1401dafb1048SDan Williams 	int i;
14026bc75619SDan Williams 
14036bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
14046bc75619SDan Williams 	if (!t->nfit_buf)
14056bc75619SDan Williams 		return -ENOMEM;
14066bc75619SDan Williams 	t->nfit_size = nfit_size;
14076bc75619SDan Williams 
1408ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA2_SIZE, &t->spa_set_dma[0]);
14096bc75619SDan Williams 	if (!t->spa_set[0])
14106bc75619SDan Williams 		return -ENOMEM;
14116bc75619SDan Williams 
1412dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1413dafb1048SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
1414dafb1048SDan Williams 		if (!t->label[i])
1415dafb1048SDan Williams 			return -ENOMEM;
1416dafb1048SDan Williams 		sprintf(t->label[i], "label%d", i);
1417dafb1048SDan Williams 	}
1418dafb1048SDan Williams 
14197bfe97c7SDan Williams 	t->spa_set[1] = test_alloc(t, SPA_VCD_SIZE, &t->spa_set_dma[1]);
14207bfe97c7SDan Williams 	if (!t->spa_set[1])
14217bfe97c7SDan Williams 		return -ENOMEM;
14227bfe97c7SDan Williams 
1423718fda67SDan Williams 	if (nfit_test_dimm_init(t))
1424718fda67SDan Williams 		return -ENOMEM;
1425ed07c433SDan Williams 	smart_init(t);
1426f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
14276bc75619SDan Williams }
14286bc75619SDan Williams 
14295dc68e55SDan Williams static void dcr_common_init(struct acpi_nfit_control_region *dcr)
14305dc68e55SDan Williams {
14315dc68e55SDan Williams 	dcr->vendor_id = 0xabcd;
14325dc68e55SDan Williams 	dcr->device_id = 0;
14335dc68e55SDan Williams 	dcr->revision_id = 1;
14345dc68e55SDan Williams 	dcr->valid_fields = 1;
14355dc68e55SDan Williams 	dcr->manufacturing_location = 0xa;
14365dc68e55SDan Williams 	dcr->manufacturing_date = cpu_to_be16(2016);
14375dc68e55SDan Williams }
14385dc68e55SDan Williams 
14396bc75619SDan Williams static void nfit_test0_setup(struct nfit_test *t)
14406bc75619SDan Williams {
144185d3fa02SDan Williams 	const int flush_hint_size = sizeof(struct acpi_nfit_flush_address)
144285d3fa02SDan Williams 		+ (sizeof(u64) * NUM_HINTS);
14436bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
14446bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
14456bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
14466bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
14476bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
14486bc75619SDan Williams 	struct acpi_nfit_data_region *bdw;
14499d27a87eSDan Williams 	struct acpi_nfit_flush_address *flush;
1450f81e1d35SDave Jiang 	struct acpi_nfit_capabilities *pcap;
1451d7d8464dSRoss Zwisler 	unsigned int offset = 0, i;
14526bc75619SDan Williams 
14536bc75619SDan Williams 	/*
14546bc75619SDan Williams 	 * spa0 (interleave first half of dimm0 and dimm1, note storage
14556bc75619SDan Williams 	 * does not actually alias the related block-data-window
14566bc75619SDan Williams 	 * regions)
14576bc75619SDan Williams 	 */
14586b577c9dSLinda Knippers 	spa = nfit_buf;
14596bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14606bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14616bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
14626bc75619SDan Williams 	spa->range_index = 0+1;
14636bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
14646bc75619SDan Williams 	spa->length = SPA0_SIZE;
1465d7d8464dSRoss Zwisler 	offset += spa->header.length;
14666bc75619SDan Williams 
14676bc75619SDan Williams 	/*
14686bc75619SDan Williams 	 * spa1 (interleave last half of the 4 DIMMS, note storage
14696bc75619SDan Williams 	 * does not actually alias the related block-data-window
14706bc75619SDan Williams 	 * regions)
14716bc75619SDan Williams 	 */
1472d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
14736bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14746bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14756bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
14766bc75619SDan Williams 	spa->range_index = 1+1;
14776bc75619SDan Williams 	spa->address = t->spa_set_dma[1];
14786bc75619SDan Williams 	spa->length = SPA1_SIZE;
1479d7d8464dSRoss Zwisler 	offset += spa->header.length;
14806bc75619SDan Williams 
14816bc75619SDan Williams 	/* spa2 (dcr0) dimm0 */
1482d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
14836bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14846bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14856bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
14866bc75619SDan Williams 	spa->range_index = 2+1;
14876bc75619SDan Williams 	spa->address = t->dcr_dma[0];
14886bc75619SDan Williams 	spa->length = DCR_SIZE;
1489d7d8464dSRoss Zwisler 	offset += spa->header.length;
14906bc75619SDan Williams 
14916bc75619SDan Williams 	/* spa3 (dcr1) dimm1 */
1492d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
14936bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14946bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14956bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
14966bc75619SDan Williams 	spa->range_index = 3+1;
14976bc75619SDan Williams 	spa->address = t->dcr_dma[1];
14986bc75619SDan Williams 	spa->length = DCR_SIZE;
1499d7d8464dSRoss Zwisler 	offset += spa->header.length;
15006bc75619SDan Williams 
15016bc75619SDan Williams 	/* spa4 (dcr2) dimm2 */
1502d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15036bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15046bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15056bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
15066bc75619SDan Williams 	spa->range_index = 4+1;
15076bc75619SDan Williams 	spa->address = t->dcr_dma[2];
15086bc75619SDan Williams 	spa->length = DCR_SIZE;
1509d7d8464dSRoss Zwisler 	offset += spa->header.length;
15106bc75619SDan Williams 
15116bc75619SDan Williams 	/* spa5 (dcr3) dimm3 */
1512d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15136bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15146bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15156bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
15166bc75619SDan Williams 	spa->range_index = 5+1;
15176bc75619SDan Williams 	spa->address = t->dcr_dma[3];
15186bc75619SDan Williams 	spa->length = DCR_SIZE;
1519d7d8464dSRoss Zwisler 	offset += spa->header.length;
15206bc75619SDan Williams 
15216bc75619SDan Williams 	/* spa6 (bdw for dcr0) dimm0 */
1522d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15236bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15246bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15256bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
15266bc75619SDan Williams 	spa->range_index = 6+1;
15276bc75619SDan Williams 	spa->address = t->dimm_dma[0];
15286bc75619SDan Williams 	spa->length = DIMM_SIZE;
1529d7d8464dSRoss Zwisler 	offset += spa->header.length;
15306bc75619SDan Williams 
15316bc75619SDan Williams 	/* spa7 (bdw for dcr1) dimm1 */
1532d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15336bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15346bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15356bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
15366bc75619SDan Williams 	spa->range_index = 7+1;
15376bc75619SDan Williams 	spa->address = t->dimm_dma[1];
15386bc75619SDan Williams 	spa->length = DIMM_SIZE;
1539d7d8464dSRoss Zwisler 	offset += spa->header.length;
15406bc75619SDan Williams 
15416bc75619SDan Williams 	/* spa8 (bdw for dcr2) dimm2 */
1542d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15436bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15446bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15456bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
15466bc75619SDan Williams 	spa->range_index = 8+1;
15476bc75619SDan Williams 	spa->address = t->dimm_dma[2];
15486bc75619SDan Williams 	spa->length = DIMM_SIZE;
1549d7d8464dSRoss Zwisler 	offset += spa->header.length;
15506bc75619SDan Williams 
15516bc75619SDan Williams 	/* spa9 (bdw for dcr3) dimm3 */
1552d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15536bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15546bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15556bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
15566bc75619SDan Williams 	spa->range_index = 9+1;
15576bc75619SDan Williams 	spa->address = t->dimm_dma[3];
15586bc75619SDan Williams 	spa->length = DIMM_SIZE;
1559d7d8464dSRoss Zwisler 	offset += spa->header.length;
15606bc75619SDan Williams 
15616bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
15626bc75619SDan Williams 	memdev = nfit_buf + offset;
15636bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
15646bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
15656bc75619SDan Williams 	memdev->device_handle = handle[0];
15666bc75619SDan Williams 	memdev->physical_id = 0;
15676bc75619SDan Williams 	memdev->region_id = 0;
15686bc75619SDan Williams 	memdev->range_index = 0+1;
15693b87356fSDan Williams 	memdev->region_index = 4+1;
15706bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1571df06a2d5SDan Williams 	memdev->region_offset = 1;
15726bc75619SDan Williams 	memdev->address = 0;
15736bc75619SDan Williams 	memdev->interleave_index = 0;
15746bc75619SDan Williams 	memdev->interleave_ways = 2;
1575d7d8464dSRoss Zwisler 	offset += memdev->header.length;
15766bc75619SDan Williams 
15776bc75619SDan Williams 	/* mem-region1 (spa0, dimm1) */
1578d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
15796bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
15806bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
15816bc75619SDan Williams 	memdev->device_handle = handle[1];
15826bc75619SDan Williams 	memdev->physical_id = 1;
15836bc75619SDan Williams 	memdev->region_id = 0;
15846bc75619SDan Williams 	memdev->range_index = 0+1;
15853b87356fSDan Williams 	memdev->region_index = 5+1;
15866bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1587df06a2d5SDan Williams 	memdev->region_offset = (1 << 8);
15886bc75619SDan Williams 	memdev->address = 0;
15896bc75619SDan Williams 	memdev->interleave_index = 0;
15906bc75619SDan Williams 	memdev->interleave_ways = 2;
1591ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1592d7d8464dSRoss Zwisler 	offset += memdev->header.length;
15936bc75619SDan Williams 
15946bc75619SDan Williams 	/* mem-region2 (spa1, dimm0) */
1595d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
15966bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
15976bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
15986bc75619SDan Williams 	memdev->device_handle = handle[0];
15996bc75619SDan Williams 	memdev->physical_id = 0;
16006bc75619SDan Williams 	memdev->region_id = 1;
16016bc75619SDan Williams 	memdev->range_index = 1+1;
16023b87356fSDan Williams 	memdev->region_index = 4+1;
16036bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1604df06a2d5SDan Williams 	memdev->region_offset = (1 << 16);
16056bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
16066bc75619SDan Williams 	memdev->interleave_index = 0;
16076bc75619SDan Williams 	memdev->interleave_ways = 4;
1608ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1609d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16106bc75619SDan Williams 
16116bc75619SDan Williams 	/* mem-region3 (spa1, dimm1) */
1612d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16136bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16146bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16156bc75619SDan Williams 	memdev->device_handle = handle[1];
16166bc75619SDan Williams 	memdev->physical_id = 1;
16176bc75619SDan Williams 	memdev->region_id = 1;
16186bc75619SDan Williams 	memdev->range_index = 1+1;
16193b87356fSDan Williams 	memdev->region_index = 5+1;
16206bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1621df06a2d5SDan Williams 	memdev->region_offset = (1 << 24);
16226bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
16236bc75619SDan Williams 	memdev->interleave_index = 0;
16246bc75619SDan Williams 	memdev->interleave_ways = 4;
1625d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16266bc75619SDan Williams 
16276bc75619SDan Williams 	/* mem-region4 (spa1, dimm2) */
1628d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16296bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16306bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16316bc75619SDan Williams 	memdev->device_handle = handle[2];
16326bc75619SDan Williams 	memdev->physical_id = 2;
16336bc75619SDan Williams 	memdev->region_id = 0;
16346bc75619SDan Williams 	memdev->range_index = 1+1;
16353b87356fSDan Williams 	memdev->region_index = 6+1;
16366bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1637df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 32);
16386bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
16396bc75619SDan Williams 	memdev->interleave_index = 0;
16406bc75619SDan Williams 	memdev->interleave_ways = 4;
1641ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1642d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16436bc75619SDan Williams 
16446bc75619SDan Williams 	/* mem-region5 (spa1, dimm3) */
1645d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16466bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16476bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16486bc75619SDan Williams 	memdev->device_handle = handle[3];
16496bc75619SDan Williams 	memdev->physical_id = 3;
16506bc75619SDan Williams 	memdev->region_id = 0;
16516bc75619SDan Williams 	memdev->range_index = 1+1;
16523b87356fSDan Williams 	memdev->region_index = 7+1;
16536bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1654df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 40);
16556bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
16566bc75619SDan Williams 	memdev->interleave_index = 0;
16576bc75619SDan Williams 	memdev->interleave_ways = 4;
1658d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16596bc75619SDan Williams 
16606bc75619SDan Williams 	/* mem-region6 (spa/dcr0, dimm0) */
1661d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16626bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16636bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16646bc75619SDan Williams 	memdev->device_handle = handle[0];
16656bc75619SDan Williams 	memdev->physical_id = 0;
16666bc75619SDan Williams 	memdev->region_id = 0;
16676bc75619SDan Williams 	memdev->range_index = 2+1;
16686bc75619SDan Williams 	memdev->region_index = 0+1;
16696bc75619SDan Williams 	memdev->region_size = 0;
16706bc75619SDan Williams 	memdev->region_offset = 0;
16716bc75619SDan Williams 	memdev->address = 0;
16726bc75619SDan Williams 	memdev->interleave_index = 0;
16736bc75619SDan Williams 	memdev->interleave_ways = 1;
1674d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16756bc75619SDan Williams 
16766bc75619SDan Williams 	/* mem-region7 (spa/dcr1, dimm1) */
1677d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16786bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16796bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16806bc75619SDan Williams 	memdev->device_handle = handle[1];
16816bc75619SDan Williams 	memdev->physical_id = 1;
16826bc75619SDan Williams 	memdev->region_id = 0;
16836bc75619SDan Williams 	memdev->range_index = 3+1;
16846bc75619SDan Williams 	memdev->region_index = 1+1;
16856bc75619SDan Williams 	memdev->region_size = 0;
16866bc75619SDan Williams 	memdev->region_offset = 0;
16876bc75619SDan Williams 	memdev->address = 0;
16886bc75619SDan Williams 	memdev->interleave_index = 0;
16896bc75619SDan Williams 	memdev->interleave_ways = 1;
1690d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16916bc75619SDan Williams 
16926bc75619SDan Williams 	/* mem-region8 (spa/dcr2, dimm2) */
1693d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16946bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16956bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16966bc75619SDan Williams 	memdev->device_handle = handle[2];
16976bc75619SDan Williams 	memdev->physical_id = 2;
16986bc75619SDan Williams 	memdev->region_id = 0;
16996bc75619SDan Williams 	memdev->range_index = 4+1;
17006bc75619SDan Williams 	memdev->region_index = 2+1;
17016bc75619SDan Williams 	memdev->region_size = 0;
17026bc75619SDan Williams 	memdev->region_offset = 0;
17036bc75619SDan Williams 	memdev->address = 0;
17046bc75619SDan Williams 	memdev->interleave_index = 0;
17056bc75619SDan Williams 	memdev->interleave_ways = 1;
1706d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17076bc75619SDan Williams 
17086bc75619SDan Williams 	/* mem-region9 (spa/dcr3, dimm3) */
1709d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17106bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17116bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17126bc75619SDan Williams 	memdev->device_handle = handle[3];
17136bc75619SDan Williams 	memdev->physical_id = 3;
17146bc75619SDan Williams 	memdev->region_id = 0;
17156bc75619SDan Williams 	memdev->range_index = 5+1;
17166bc75619SDan Williams 	memdev->region_index = 3+1;
17176bc75619SDan Williams 	memdev->region_size = 0;
17186bc75619SDan Williams 	memdev->region_offset = 0;
17196bc75619SDan Williams 	memdev->address = 0;
17206bc75619SDan Williams 	memdev->interleave_index = 0;
17216bc75619SDan Williams 	memdev->interleave_ways = 1;
1722d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17236bc75619SDan Williams 
17246bc75619SDan Williams 	/* mem-region10 (spa/bdw0, dimm0) */
1725d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17266bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17276bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17286bc75619SDan Williams 	memdev->device_handle = handle[0];
17296bc75619SDan Williams 	memdev->physical_id = 0;
17306bc75619SDan Williams 	memdev->region_id = 0;
17316bc75619SDan Williams 	memdev->range_index = 6+1;
17326bc75619SDan Williams 	memdev->region_index = 0+1;
17336bc75619SDan Williams 	memdev->region_size = 0;
17346bc75619SDan Williams 	memdev->region_offset = 0;
17356bc75619SDan Williams 	memdev->address = 0;
17366bc75619SDan Williams 	memdev->interleave_index = 0;
17376bc75619SDan Williams 	memdev->interleave_ways = 1;
1738d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17396bc75619SDan Williams 
17406bc75619SDan Williams 	/* mem-region11 (spa/bdw1, dimm1) */
1741d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17426bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17436bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17446bc75619SDan Williams 	memdev->device_handle = handle[1];
17456bc75619SDan Williams 	memdev->physical_id = 1;
17466bc75619SDan Williams 	memdev->region_id = 0;
17476bc75619SDan Williams 	memdev->range_index = 7+1;
17486bc75619SDan Williams 	memdev->region_index = 1+1;
17496bc75619SDan Williams 	memdev->region_size = 0;
17506bc75619SDan Williams 	memdev->region_offset = 0;
17516bc75619SDan Williams 	memdev->address = 0;
17526bc75619SDan Williams 	memdev->interleave_index = 0;
17536bc75619SDan Williams 	memdev->interleave_ways = 1;
1754d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17556bc75619SDan Williams 
17566bc75619SDan Williams 	/* mem-region12 (spa/bdw2, dimm2) */
1757d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17586bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17596bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17606bc75619SDan Williams 	memdev->device_handle = handle[2];
17616bc75619SDan Williams 	memdev->physical_id = 2;
17626bc75619SDan Williams 	memdev->region_id = 0;
17636bc75619SDan Williams 	memdev->range_index = 8+1;
17646bc75619SDan Williams 	memdev->region_index = 2+1;
17656bc75619SDan Williams 	memdev->region_size = 0;
17666bc75619SDan Williams 	memdev->region_offset = 0;
17676bc75619SDan Williams 	memdev->address = 0;
17686bc75619SDan Williams 	memdev->interleave_index = 0;
17696bc75619SDan Williams 	memdev->interleave_ways = 1;
1770d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17716bc75619SDan Williams 
17726bc75619SDan Williams 	/* mem-region13 (spa/dcr3, dimm3) */
1773d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17746bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17756bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17766bc75619SDan Williams 	memdev->device_handle = handle[3];
17776bc75619SDan Williams 	memdev->physical_id = 3;
17786bc75619SDan Williams 	memdev->region_id = 0;
17796bc75619SDan Williams 	memdev->range_index = 9+1;
17806bc75619SDan Williams 	memdev->region_index = 3+1;
17816bc75619SDan Williams 	memdev->region_size = 0;
17826bc75619SDan Williams 	memdev->region_offset = 0;
17836bc75619SDan Williams 	memdev->address = 0;
17846bc75619SDan Williams 	memdev->interleave_index = 0;
17856bc75619SDan Williams 	memdev->interleave_ways = 1;
1786ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1787d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17886bc75619SDan Williams 
17893b87356fSDan Williams 	/* dcr-descriptor0: blk */
17906bc75619SDan Williams 	dcr = nfit_buf + offset;
17916bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1792d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
17936bc75619SDan Williams 	dcr->region_index = 0+1;
17945dc68e55SDan Williams 	dcr_common_init(dcr);
17956bc75619SDan Williams 	dcr->serial_number = ~handle[0];
1796be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
17976bc75619SDan Williams 	dcr->windows = 1;
17986bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
17996bc75619SDan Williams 	dcr->command_offset = 0;
18006bc75619SDan Williams 	dcr->command_size = 8;
18016bc75619SDan Williams 	dcr->status_offset = 8;
18026bc75619SDan Williams 	dcr->status_size = 4;
1803d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18046bc75619SDan Williams 
18053b87356fSDan Williams 	/* dcr-descriptor1: blk */
1806d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
18076bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1808d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
18096bc75619SDan Williams 	dcr->region_index = 1+1;
18105dc68e55SDan Williams 	dcr_common_init(dcr);
18116bc75619SDan Williams 	dcr->serial_number = ~handle[1];
1812be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
18136bc75619SDan Williams 	dcr->windows = 1;
18146bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
18156bc75619SDan Williams 	dcr->command_offset = 0;
18166bc75619SDan Williams 	dcr->command_size = 8;
18176bc75619SDan Williams 	dcr->status_offset = 8;
18186bc75619SDan Williams 	dcr->status_size = 4;
1819d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18206bc75619SDan Williams 
18213b87356fSDan Williams 	/* dcr-descriptor2: blk */
1822d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
18236bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1824d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
18256bc75619SDan Williams 	dcr->region_index = 2+1;
18265dc68e55SDan Williams 	dcr_common_init(dcr);
18276bc75619SDan Williams 	dcr->serial_number = ~handle[2];
1828be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
18296bc75619SDan Williams 	dcr->windows = 1;
18306bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
18316bc75619SDan Williams 	dcr->command_offset = 0;
18326bc75619SDan Williams 	dcr->command_size = 8;
18336bc75619SDan Williams 	dcr->status_offset = 8;
18346bc75619SDan Williams 	dcr->status_size = 4;
1835d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18366bc75619SDan Williams 
18373b87356fSDan Williams 	/* dcr-descriptor3: blk */
1838d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
18396bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1840d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
18416bc75619SDan Williams 	dcr->region_index = 3+1;
18425dc68e55SDan Williams 	dcr_common_init(dcr);
18436bc75619SDan Williams 	dcr->serial_number = ~handle[3];
1844be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
18456bc75619SDan Williams 	dcr->windows = 1;
18466bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
18476bc75619SDan Williams 	dcr->command_offset = 0;
18486bc75619SDan Williams 	dcr->command_size = 8;
18496bc75619SDan Williams 	dcr->status_offset = 8;
18506bc75619SDan Williams 	dcr->status_size = 4;
1851d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18526bc75619SDan Williams 
18533b87356fSDan Williams 	/* dcr-descriptor0: pmem */
18543b87356fSDan Williams 	dcr = nfit_buf + offset;
18553b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
18563b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
18573b87356fSDan Williams 			window_size);
18583b87356fSDan Williams 	dcr->region_index = 4+1;
18595dc68e55SDan Williams 	dcr_common_init(dcr);
18603b87356fSDan Williams 	dcr->serial_number = ~handle[0];
18613b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
18623b87356fSDan Williams 	dcr->windows = 0;
1863d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18643b87356fSDan Williams 
18653b87356fSDan Williams 	/* dcr-descriptor1: pmem */
1866d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
18673b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
18683b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
18693b87356fSDan Williams 			window_size);
18703b87356fSDan Williams 	dcr->region_index = 5+1;
18715dc68e55SDan Williams 	dcr_common_init(dcr);
18723b87356fSDan Williams 	dcr->serial_number = ~handle[1];
18733b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
18743b87356fSDan Williams 	dcr->windows = 0;
1875d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18763b87356fSDan Williams 
18773b87356fSDan Williams 	/* dcr-descriptor2: pmem */
1878d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
18793b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
18803b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
18813b87356fSDan Williams 			window_size);
18823b87356fSDan Williams 	dcr->region_index = 6+1;
18835dc68e55SDan Williams 	dcr_common_init(dcr);
18843b87356fSDan Williams 	dcr->serial_number = ~handle[2];
18853b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
18863b87356fSDan Williams 	dcr->windows = 0;
1887d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18883b87356fSDan Williams 
18893b87356fSDan Williams 	/* dcr-descriptor3: pmem */
1890d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
18913b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
18923b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
18933b87356fSDan Williams 			window_size);
18943b87356fSDan Williams 	dcr->region_index = 7+1;
18955dc68e55SDan Williams 	dcr_common_init(dcr);
18963b87356fSDan Williams 	dcr->serial_number = ~handle[3];
18973b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
18983b87356fSDan Williams 	dcr->windows = 0;
1899d7d8464dSRoss Zwisler 	offset += dcr->header.length;
19003b87356fSDan Williams 
19016bc75619SDan Williams 	/* bdw0 (spa/dcr0, dimm0) */
19026bc75619SDan Williams 	bdw = nfit_buf + offset;
19036bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1904d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
19056bc75619SDan Williams 	bdw->region_index = 0+1;
19066bc75619SDan Williams 	bdw->windows = 1;
19076bc75619SDan Williams 	bdw->offset = 0;
19086bc75619SDan Williams 	bdw->size = BDW_SIZE;
19096bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
19106bc75619SDan Williams 	bdw->start_address = 0;
1911d7d8464dSRoss Zwisler 	offset += bdw->header.length;
19126bc75619SDan Williams 
19136bc75619SDan Williams 	/* bdw1 (spa/dcr1, dimm1) */
1914d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
19156bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1916d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
19176bc75619SDan Williams 	bdw->region_index = 1+1;
19186bc75619SDan Williams 	bdw->windows = 1;
19196bc75619SDan Williams 	bdw->offset = 0;
19206bc75619SDan Williams 	bdw->size = BDW_SIZE;
19216bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
19226bc75619SDan Williams 	bdw->start_address = 0;
1923d7d8464dSRoss Zwisler 	offset += bdw->header.length;
19246bc75619SDan Williams 
19256bc75619SDan Williams 	/* bdw2 (spa/dcr2, dimm2) */
1926d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
19276bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1928d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
19296bc75619SDan Williams 	bdw->region_index = 2+1;
19306bc75619SDan Williams 	bdw->windows = 1;
19316bc75619SDan Williams 	bdw->offset = 0;
19326bc75619SDan Williams 	bdw->size = BDW_SIZE;
19336bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
19346bc75619SDan Williams 	bdw->start_address = 0;
1935d7d8464dSRoss Zwisler 	offset += bdw->header.length;
19366bc75619SDan Williams 
19376bc75619SDan Williams 	/* bdw3 (spa/dcr3, dimm3) */
1938d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
19396bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1940d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
19416bc75619SDan Williams 	bdw->region_index = 3+1;
19426bc75619SDan Williams 	bdw->windows = 1;
19436bc75619SDan Williams 	bdw->offset = 0;
19446bc75619SDan Williams 	bdw->size = BDW_SIZE;
19456bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
19466bc75619SDan Williams 	bdw->start_address = 0;
1947d7d8464dSRoss Zwisler 	offset += bdw->header.length;
19486bc75619SDan Williams 
19499d27a87eSDan Williams 	/* flush0 (dimm0) */
19509d27a87eSDan Williams 	flush = nfit_buf + offset;
19519d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
195285d3fa02SDan Williams 	flush->header.length = flush_hint_size;
19539d27a87eSDan Williams 	flush->device_handle = handle[0];
195485d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
195585d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
195685d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[0] + i * sizeof(u64);
1957d7d8464dSRoss Zwisler 	offset += flush->header.length;
19589d27a87eSDan Williams 
19599d27a87eSDan Williams 	/* flush1 (dimm1) */
1960d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
19619d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
196285d3fa02SDan Williams 	flush->header.length = flush_hint_size;
19639d27a87eSDan Williams 	flush->device_handle = handle[1];
196485d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
196585d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
196685d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[1] + i * sizeof(u64);
1967d7d8464dSRoss Zwisler 	offset += flush->header.length;
19689d27a87eSDan Williams 
19699d27a87eSDan Williams 	/* flush2 (dimm2) */
1970d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
19719d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
197285d3fa02SDan Williams 	flush->header.length = flush_hint_size;
19739d27a87eSDan Williams 	flush->device_handle = handle[2];
197485d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
197585d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
197685d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[2] + i * sizeof(u64);
1977d7d8464dSRoss Zwisler 	offset += flush->header.length;
19789d27a87eSDan Williams 
19799d27a87eSDan Williams 	/* flush3 (dimm3) */
1980d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
19819d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
198285d3fa02SDan Williams 	flush->header.length = flush_hint_size;
19839d27a87eSDan Williams 	flush->device_handle = handle[3];
198485d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
198585d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
198685d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[3] + i * sizeof(u64);
1987d7d8464dSRoss Zwisler 	offset += flush->header.length;
19889d27a87eSDan Williams 
1989f81e1d35SDave Jiang 	/* platform capabilities */
1990d7d8464dSRoss Zwisler 	pcap = nfit_buf + offset;
1991f81e1d35SDave Jiang 	pcap->header.type = ACPI_NFIT_TYPE_CAPABILITIES;
1992f81e1d35SDave Jiang 	pcap->header.length = sizeof(*pcap);
1993f81e1d35SDave Jiang 	pcap->highest_capability = 1;
1994*1273c253SVishal Verma 	pcap->capabilities = ACPI_NFIT_CAPABILITY_MEM_FLUSH;
1995d7d8464dSRoss Zwisler 	offset += pcap->header.length;
1996f81e1d35SDave Jiang 
199720985164SVishal Verma 	if (t->setup_hotplug) {
19983b87356fSDan Williams 		/* dcr-descriptor4: blk */
199920985164SVishal Verma 		dcr = nfit_buf + offset;
200020985164SVishal Verma 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2001d7d8464dSRoss Zwisler 		dcr->header.length = sizeof(*dcr);
20023b87356fSDan Williams 		dcr->region_index = 8+1;
20035dc68e55SDan Williams 		dcr_common_init(dcr);
200420985164SVishal Verma 		dcr->serial_number = ~handle[4];
2005be26f9aeSDan Williams 		dcr->code = NFIT_FIC_BLK;
200620985164SVishal Verma 		dcr->windows = 1;
200720985164SVishal Verma 		dcr->window_size = DCR_SIZE;
200820985164SVishal Verma 		dcr->command_offset = 0;
200920985164SVishal Verma 		dcr->command_size = 8;
201020985164SVishal Verma 		dcr->status_offset = 8;
201120985164SVishal Verma 		dcr->status_size = 4;
2012d7d8464dSRoss Zwisler 		offset += dcr->header.length;
201320985164SVishal Verma 
20143b87356fSDan Williams 		/* dcr-descriptor4: pmem */
20153b87356fSDan Williams 		dcr = nfit_buf + offset;
20163b87356fSDan Williams 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
20173b87356fSDan Williams 		dcr->header.length = offsetof(struct acpi_nfit_control_region,
20183b87356fSDan Williams 				window_size);
20193b87356fSDan Williams 		dcr->region_index = 9+1;
20205dc68e55SDan Williams 		dcr_common_init(dcr);
20213b87356fSDan Williams 		dcr->serial_number = ~handle[4];
20223b87356fSDan Williams 		dcr->code = NFIT_FIC_BYTEN;
20233b87356fSDan Williams 		dcr->windows = 0;
2024d7d8464dSRoss Zwisler 		offset += dcr->header.length;
20253b87356fSDan Williams 
202620985164SVishal Verma 		/* bdw4 (spa/dcr4, dimm4) */
202720985164SVishal Verma 		bdw = nfit_buf + offset;
202820985164SVishal Verma 		bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2029d7d8464dSRoss Zwisler 		bdw->header.length = sizeof(*bdw);
20303b87356fSDan Williams 		bdw->region_index = 8+1;
203120985164SVishal Verma 		bdw->windows = 1;
203220985164SVishal Verma 		bdw->offset = 0;
203320985164SVishal Verma 		bdw->size = BDW_SIZE;
203420985164SVishal Verma 		bdw->capacity = DIMM_SIZE;
203520985164SVishal Verma 		bdw->start_address = 0;
2036d7d8464dSRoss Zwisler 		offset += bdw->header.length;
203720985164SVishal Verma 
203820985164SVishal Verma 		/* spa10 (dcr4) dimm4 */
203920985164SVishal Verma 		spa = nfit_buf + offset;
204020985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
204120985164SVishal Verma 		spa->header.length = sizeof(*spa);
204220985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
204320985164SVishal Verma 		spa->range_index = 10+1;
204420985164SVishal Verma 		spa->address = t->dcr_dma[4];
204520985164SVishal Verma 		spa->length = DCR_SIZE;
2046d7d8464dSRoss Zwisler 		offset += spa->header.length;
204720985164SVishal Verma 
204820985164SVishal Verma 		/*
204920985164SVishal Verma 		 * spa11 (single-dimm interleave for hotplug, note storage
205020985164SVishal Verma 		 * does not actually alias the related block-data-window
205120985164SVishal Verma 		 * regions)
205220985164SVishal Verma 		 */
2053d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
205420985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
205520985164SVishal Verma 		spa->header.length = sizeof(*spa);
205620985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
205720985164SVishal Verma 		spa->range_index = 11+1;
205820985164SVishal Verma 		spa->address = t->spa_set_dma[2];
205920985164SVishal Verma 		spa->length = SPA0_SIZE;
2060d7d8464dSRoss Zwisler 		offset += spa->header.length;
206120985164SVishal Verma 
206220985164SVishal Verma 		/* spa12 (bdw for dcr4) dimm4 */
2063d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
206420985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
206520985164SVishal Verma 		spa->header.length = sizeof(*spa);
206620985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
206720985164SVishal Verma 		spa->range_index = 12+1;
206820985164SVishal Verma 		spa->address = t->dimm_dma[4];
206920985164SVishal Verma 		spa->length = DIMM_SIZE;
2070d7d8464dSRoss Zwisler 		offset += spa->header.length;
207120985164SVishal Verma 
207220985164SVishal Verma 		/* mem-region14 (spa/dcr4, dimm4) */
207320985164SVishal Verma 		memdev = nfit_buf + offset;
207420985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
207520985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
207620985164SVishal Verma 		memdev->device_handle = handle[4];
207720985164SVishal Verma 		memdev->physical_id = 4;
207820985164SVishal Verma 		memdev->region_id = 0;
207920985164SVishal Verma 		memdev->range_index = 10+1;
20803b87356fSDan Williams 		memdev->region_index = 8+1;
208120985164SVishal Verma 		memdev->region_size = 0;
208220985164SVishal Verma 		memdev->region_offset = 0;
208320985164SVishal Verma 		memdev->address = 0;
208420985164SVishal Verma 		memdev->interleave_index = 0;
208520985164SVishal Verma 		memdev->interleave_ways = 1;
2086d7d8464dSRoss Zwisler 		offset += memdev->header.length;
208720985164SVishal Verma 
2088d7d8464dSRoss Zwisler 		/* mem-region15 (spa11, dimm4) */
2089d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
209020985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
209120985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
209220985164SVishal Verma 		memdev->device_handle = handle[4];
209320985164SVishal Verma 		memdev->physical_id = 4;
209420985164SVishal Verma 		memdev->region_id = 0;
209520985164SVishal Verma 		memdev->range_index = 11+1;
20963b87356fSDan Williams 		memdev->region_index = 9+1;
209720985164SVishal Verma 		memdev->region_size = SPA0_SIZE;
2098df06a2d5SDan Williams 		memdev->region_offset = (1ULL << 48);
209920985164SVishal Verma 		memdev->address = 0;
210020985164SVishal Verma 		memdev->interleave_index = 0;
210120985164SVishal Verma 		memdev->interleave_ways = 1;
2102ac40b675SDan Williams 		memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2103d7d8464dSRoss Zwisler 		offset += memdev->header.length;
210420985164SVishal Verma 
21053b87356fSDan Williams 		/* mem-region16 (spa/bdw4, dimm4) */
2106d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
210720985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
210820985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
210920985164SVishal Verma 		memdev->device_handle = handle[4];
211020985164SVishal Verma 		memdev->physical_id = 4;
211120985164SVishal Verma 		memdev->region_id = 0;
211220985164SVishal Verma 		memdev->range_index = 12+1;
21133b87356fSDan Williams 		memdev->region_index = 8+1;
211420985164SVishal Verma 		memdev->region_size = 0;
211520985164SVishal Verma 		memdev->region_offset = 0;
211620985164SVishal Verma 		memdev->address = 0;
211720985164SVishal Verma 		memdev->interleave_index = 0;
211820985164SVishal Verma 		memdev->interleave_ways = 1;
2119d7d8464dSRoss Zwisler 		offset += memdev->header.length;
212020985164SVishal Verma 
212120985164SVishal Verma 		/* flush3 (dimm4) */
212220985164SVishal Verma 		flush = nfit_buf + offset;
212320985164SVishal Verma 		flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
212485d3fa02SDan Williams 		flush->header.length = flush_hint_size;
212520985164SVishal Verma 		flush->device_handle = handle[4];
212685d3fa02SDan Williams 		flush->hint_count = NUM_HINTS;
212785d3fa02SDan Williams 		for (i = 0; i < NUM_HINTS; i++)
212885d3fa02SDan Williams 			flush->hint_address[i] = t->flush_dma[4]
212985d3fa02SDan Williams 				+ i * sizeof(u64);
2130d7d8464dSRoss Zwisler 		offset += flush->header.length;
21319741a559SRoss Zwisler 
21329741a559SRoss Zwisler 		/* sanity check to make sure we've filled the buffer */
21339741a559SRoss Zwisler 		WARN_ON(offset != t->nfit_size);
213420985164SVishal Verma 	}
213520985164SVishal Verma 
21361526f9e2SRoss Zwisler 	t->nfit_filled = offset;
21371526f9e2SRoss Zwisler 
21389fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
21399fb1a190SDave Jiang 			SPA0_SIZE);
2140f471f1a7SDan Williams 
21416bc75619SDan Williams 	acpi_desc = &t->acpi_desc;
2142e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
2143e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2144e3654ecaSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2145ed07c433SDan Williams 	set_bit(ND_INTEL_SMART, &acpi_desc->dimm_cmd_force_en);
2146ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
2147ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_SET_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
21484cf260fcSVishal Verma 	set_bit(ND_INTEL_SMART_INJECT, &acpi_desc->dimm_cmd_force_en);
2149e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2150e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2151e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2152e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
215310246dc8SYasunori Goto 	set_bit(ND_CMD_CALL, &acpi_desc->bus_cmd_force_en);
215410246dc8SYasunori Goto 	set_bit(NFIT_CMD_TRANSLATE_SPA, &acpi_desc->bus_nfit_cmd_force_en);
21559fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_SET, &acpi_desc->bus_nfit_cmd_force_en);
21569fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_CLEAR, &acpi_desc->bus_nfit_cmd_force_en);
21579fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_GET, &acpi_desc->bus_nfit_cmd_force_en);
2158bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_GET_INFO, &acpi_desc->dimm_cmd_force_en);
2159bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_START_UPDATE, &acpi_desc->dimm_cmd_force_en);
2160bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_SEND_DATA, &acpi_desc->dimm_cmd_force_en);
2161bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_UPDATE, &acpi_desc->dimm_cmd_force_en);
2162bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_QUERY, &acpi_desc->dimm_cmd_force_en);
2163674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
21646bc75619SDan Williams }
21656bc75619SDan Williams 
21666bc75619SDan Williams static void nfit_test1_setup(struct nfit_test *t)
21676bc75619SDan Williams {
21686b577c9dSLinda Knippers 	size_t offset;
21696bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
21706bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
21716bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
21726bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
2173d26f73f0SDan Williams 	struct acpi_nfit_desc *acpi_desc;
21746bc75619SDan Williams 
21756b577c9dSLinda Knippers 	offset = 0;
21766bc75619SDan Williams 	/* spa0 (flat range with no bdw aliasing) */
21776bc75619SDan Williams 	spa = nfit_buf + offset;
21786bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
21796bc75619SDan Williams 	spa->header.length = sizeof(*spa);
21806bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
21816bc75619SDan Williams 	spa->range_index = 0+1;
21826bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
21836bc75619SDan Williams 	spa->length = SPA2_SIZE;
2184d7d8464dSRoss Zwisler 	offset += spa->header.length;
21856bc75619SDan Williams 
21867bfe97c7SDan Williams 	/* virtual cd region */
2187d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
21887bfe97c7SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
21897bfe97c7SDan Williams 	spa->header.length = sizeof(*spa);
21907bfe97c7SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_VCD), 16);
21917bfe97c7SDan Williams 	spa->range_index = 0;
21927bfe97c7SDan Williams 	spa->address = t->spa_set_dma[1];
21937bfe97c7SDan Williams 	spa->length = SPA_VCD_SIZE;
2194d7d8464dSRoss Zwisler 	offset += spa->header.length;
21957bfe97c7SDan Williams 
21966bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
21976bc75619SDan Williams 	memdev = nfit_buf + offset;
21986bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21996bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
2200dafb1048SDan Williams 	memdev->device_handle = handle[5];
22016bc75619SDan Williams 	memdev->physical_id = 0;
22026bc75619SDan Williams 	memdev->region_id = 0;
22036bc75619SDan Williams 	memdev->range_index = 0+1;
22046bc75619SDan Williams 	memdev->region_index = 0+1;
22056bc75619SDan Williams 	memdev->region_size = SPA2_SIZE;
22066bc75619SDan Williams 	memdev->region_offset = 0;
22076bc75619SDan Williams 	memdev->address = 0;
22086bc75619SDan Williams 	memdev->interleave_index = 0;
22096bc75619SDan Williams 	memdev->interleave_ways = 1;
221058138820SDan Williams 	memdev->flags = ACPI_NFIT_MEM_SAVE_FAILED | ACPI_NFIT_MEM_RESTORE_FAILED
221158138820SDan Williams 		| ACPI_NFIT_MEM_FLUSH_FAILED | ACPI_NFIT_MEM_HEALTH_OBSERVED
2212f4295796SDan Williams 		| ACPI_NFIT_MEM_NOT_ARMED;
2213d7d8464dSRoss Zwisler 	offset += memdev->header.length;
22146bc75619SDan Williams 
22156bc75619SDan Williams 	/* dcr-descriptor0 */
22166bc75619SDan Williams 	dcr = nfit_buf + offset;
22176bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22183b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22193b87356fSDan Williams 			window_size);
22206bc75619SDan Williams 	dcr->region_index = 0+1;
22215dc68e55SDan Williams 	dcr_common_init(dcr);
2222dafb1048SDan Williams 	dcr->serial_number = ~handle[5];
2223be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BYTE;
22246bc75619SDan Williams 	dcr->windows = 0;
2225ac40b675SDan Williams 	offset += dcr->header.length;
2226d7d8464dSRoss Zwisler 
2227ac40b675SDan Williams 	memdev = nfit_buf + offset;
2228ac40b675SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
2229ac40b675SDan Williams 	memdev->header.length = sizeof(*memdev);
2230ac40b675SDan Williams 	memdev->device_handle = handle[6];
2231ac40b675SDan Williams 	memdev->physical_id = 0;
2232ac40b675SDan Williams 	memdev->region_id = 0;
2233ac40b675SDan Williams 	memdev->range_index = 0;
2234ac40b675SDan Williams 	memdev->region_index = 0+2;
2235ac40b675SDan Williams 	memdev->region_size = SPA2_SIZE;
2236ac40b675SDan Williams 	memdev->region_offset = 0;
2237ac40b675SDan Williams 	memdev->address = 0;
2238ac40b675SDan Williams 	memdev->interleave_index = 0;
2239ac40b675SDan Williams 	memdev->interleave_ways = 1;
2240ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_MAP_FAILED;
2241d7d8464dSRoss Zwisler 	offset += memdev->header.length;
2242ac40b675SDan Williams 
2243ac40b675SDan Williams 	/* dcr-descriptor1 */
2244ac40b675SDan Williams 	dcr = nfit_buf + offset;
2245ac40b675SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2246ac40b675SDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
2247ac40b675SDan Williams 			window_size);
2248ac40b675SDan Williams 	dcr->region_index = 0+2;
2249ac40b675SDan Williams 	dcr_common_init(dcr);
2250ac40b675SDan Williams 	dcr->serial_number = ~handle[6];
2251ac40b675SDan Williams 	dcr->code = NFIT_FIC_BYTE;
2252ac40b675SDan Williams 	dcr->windows = 0;
2253d7d8464dSRoss Zwisler 	offset += dcr->header.length;
2254ac40b675SDan Williams 
22559741a559SRoss Zwisler 	/* sanity check to make sure we've filled the buffer */
22569741a559SRoss Zwisler 	WARN_ON(offset != t->nfit_size);
22579741a559SRoss Zwisler 
22581526f9e2SRoss Zwisler 	t->nfit_filled = offset;
22591526f9e2SRoss Zwisler 
22609fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
22619fb1a190SDave Jiang 			SPA2_SIZE);
2262f471f1a7SDan Williams 
2263d26f73f0SDan Williams 	acpi_desc = &t->acpi_desc;
2264e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2265e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2266e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2267e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
2268674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
22699484e12dSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
22709484e12dSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
22719484e12dSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
22726bc75619SDan Williams }
22736bc75619SDan Williams 
22746bc75619SDan Williams static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
22756bc75619SDan Williams 		void *iobuf, u64 len, int rw)
22766bc75619SDan Williams {
22776bc75619SDan Williams 	struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
22786bc75619SDan Williams 	struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
22796bc75619SDan Williams 	struct nd_region *nd_region = &ndbr->nd_region;
22806bc75619SDan Williams 	unsigned int lane;
22816bc75619SDan Williams 
22826bc75619SDan Williams 	lane = nd_region_acquire_lane(nd_region);
22836bc75619SDan Williams 	if (rw)
228467a3e8feSRoss Zwisler 		memcpy(mmio->addr.base + dpa, iobuf, len);
228567a3e8feSRoss Zwisler 	else {
228667a3e8feSRoss Zwisler 		memcpy(iobuf, mmio->addr.base + dpa, len);
228767a3e8feSRoss Zwisler 
22885deb67f7SRobin Murphy 		/* give us some some coverage of the arch_invalidate_pmem() API */
22895deb67f7SRobin Murphy 		arch_invalidate_pmem(mmio->addr.base + dpa, len);
229067a3e8feSRoss Zwisler 	}
22916bc75619SDan Williams 	nd_region_release_lane(nd_region, lane);
22926bc75619SDan Williams 
22936bc75619SDan Williams 	return 0;
22946bc75619SDan Williams }
22956bc75619SDan Williams 
2296a7de92daSDan Williams static unsigned long nfit_ctl_handle;
2297a7de92daSDan Williams 
2298a7de92daSDan Williams union acpi_object *result;
2299a7de92daSDan Williams 
2300a7de92daSDan Williams static union acpi_object *nfit_test_evaluate_dsm(acpi_handle handle,
230194116f81SAndy Shevchenko 		const guid_t *guid, u64 rev, u64 func, union acpi_object *argv4)
2302a7de92daSDan Williams {
2303a7de92daSDan Williams 	if (handle != &nfit_ctl_handle)
2304a7de92daSDan Williams 		return ERR_PTR(-ENXIO);
2305a7de92daSDan Williams 
2306a7de92daSDan Williams 	return result;
2307a7de92daSDan Williams }
2308a7de92daSDan Williams 
2309a7de92daSDan Williams static int setup_result(void *buf, size_t size)
2310a7de92daSDan Williams {
2311a7de92daSDan Williams 	result = kmalloc(sizeof(union acpi_object) + size, GFP_KERNEL);
2312a7de92daSDan Williams 	if (!result)
2313a7de92daSDan Williams 		return -ENOMEM;
2314a7de92daSDan Williams 	result->package.type = ACPI_TYPE_BUFFER,
2315a7de92daSDan Williams 	result->buffer.pointer = (void *) (result + 1);
2316a7de92daSDan Williams 	result->buffer.length = size;
2317a7de92daSDan Williams 	memcpy(result->buffer.pointer, buf, size);
2318a7de92daSDan Williams 	memset(buf, 0, size);
2319a7de92daSDan Williams 	return 0;
2320a7de92daSDan Williams }
2321a7de92daSDan Williams 
2322a7de92daSDan Williams static int nfit_ctl_test(struct device *dev)
2323a7de92daSDan Williams {
2324a7de92daSDan Williams 	int rc, cmd_rc;
2325a7de92daSDan Williams 	struct nvdimm *nvdimm;
2326a7de92daSDan Williams 	struct acpi_device *adev;
2327a7de92daSDan Williams 	struct nfit_mem *nfit_mem;
2328a7de92daSDan Williams 	struct nd_ars_record *record;
2329a7de92daSDan Williams 	struct acpi_nfit_desc *acpi_desc;
2330a7de92daSDan Williams 	const u64 test_val = 0x0123456789abcdefULL;
2331a7de92daSDan Williams 	unsigned long mask, cmd_size, offset;
2332a7de92daSDan Williams 	union {
2333a7de92daSDan Williams 		struct nd_cmd_get_config_size cfg_size;
2334fb2a1748SDan Williams 		struct nd_cmd_clear_error clear_err;
2335a7de92daSDan Williams 		struct nd_cmd_ars_status ars_stat;
2336a7de92daSDan Williams 		struct nd_cmd_ars_cap ars_cap;
2337a7de92daSDan Williams 		char buf[sizeof(struct nd_cmd_ars_status)
2338a7de92daSDan Williams 			+ sizeof(struct nd_ars_record)];
2339a7de92daSDan Williams 	} cmds;
2340a7de92daSDan Williams 
2341a7de92daSDan Williams 	adev = devm_kzalloc(dev, sizeof(*adev), GFP_KERNEL);
2342a7de92daSDan Williams 	if (!adev)
2343a7de92daSDan Williams 		return -ENOMEM;
2344a7de92daSDan Williams 	*adev = (struct acpi_device) {
2345a7de92daSDan Williams 		.handle = &nfit_ctl_handle,
2346a7de92daSDan Williams 		.dev = {
2347a7de92daSDan Williams 			.init_name = "test-adev",
2348a7de92daSDan Williams 		},
2349a7de92daSDan Williams 	};
2350a7de92daSDan Williams 
2351a7de92daSDan Williams 	acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2352a7de92daSDan Williams 	if (!acpi_desc)
2353a7de92daSDan Williams 		return -ENOMEM;
2354a7de92daSDan Williams 	*acpi_desc = (struct acpi_nfit_desc) {
2355a7de92daSDan Williams 		.nd_desc = {
2356a7de92daSDan Williams 			.cmd_mask = 1UL << ND_CMD_ARS_CAP
2357a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_START
2358a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_STATUS
235910246dc8SYasunori Goto 				| 1UL << ND_CMD_CLEAR_ERROR
236010246dc8SYasunori Goto 				| 1UL << ND_CMD_CALL,
2361a7de92daSDan Williams 			.module = THIS_MODULE,
2362a7de92daSDan Williams 			.provider_name = "ACPI.NFIT",
2363a7de92daSDan Williams 			.ndctl = acpi_nfit_ctl,
23649fb1a190SDave Jiang 			.bus_dsm_mask = 1UL << NFIT_CMD_TRANSLATE_SPA
23659fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_SET
23669fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_CLEAR
23679fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_GET,
2368a7de92daSDan Williams 		},
2369a7de92daSDan Williams 		.dev = &adev->dev,
2370a7de92daSDan Williams 	};
2371a7de92daSDan Williams 
2372a7de92daSDan Williams 	nfit_mem = devm_kzalloc(dev, sizeof(*nfit_mem), GFP_KERNEL);
2373a7de92daSDan Williams 	if (!nfit_mem)
2374a7de92daSDan Williams 		return -ENOMEM;
2375a7de92daSDan Williams 
2376a7de92daSDan Williams 	mask = 1UL << ND_CMD_SMART | 1UL << ND_CMD_SMART_THRESHOLD
2377a7de92daSDan Williams 		| 1UL << ND_CMD_DIMM_FLAGS | 1UL << ND_CMD_GET_CONFIG_SIZE
2378a7de92daSDan Williams 		| 1UL << ND_CMD_GET_CONFIG_DATA | 1UL << ND_CMD_SET_CONFIG_DATA
2379a7de92daSDan Williams 		| 1UL << ND_CMD_VENDOR;
2380a7de92daSDan Williams 	*nfit_mem = (struct nfit_mem) {
2381a7de92daSDan Williams 		.adev = adev,
2382a7de92daSDan Williams 		.family = NVDIMM_FAMILY_INTEL,
2383a7de92daSDan Williams 		.dsm_mask = mask,
2384a7de92daSDan Williams 	};
2385a7de92daSDan Williams 
2386a7de92daSDan Williams 	nvdimm = devm_kzalloc(dev, sizeof(*nvdimm), GFP_KERNEL);
2387a7de92daSDan Williams 	if (!nvdimm)
2388a7de92daSDan Williams 		return -ENOMEM;
2389a7de92daSDan Williams 	*nvdimm = (struct nvdimm) {
2390a7de92daSDan Williams 		.provider_data = nfit_mem,
2391a7de92daSDan Williams 		.cmd_mask = mask,
2392a7de92daSDan Williams 		.dev = {
2393a7de92daSDan Williams 			.init_name = "test-dimm",
2394a7de92daSDan Williams 		},
2395a7de92daSDan Williams 	};
2396a7de92daSDan Williams 
2397a7de92daSDan Williams 
2398a7de92daSDan Williams 	/* basic checkout of a typical 'get config size' command */
2399a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2400a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2401a7de92daSDan Williams 		.status = 0,
2402a7de92daSDan Williams 		.config_size = SZ_128K,
2403a7de92daSDan Williams 		.max_xfer = SZ_4K,
2404a7de92daSDan Williams 	};
2405a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2406a7de92daSDan Williams 	if (rc)
2407a7de92daSDan Williams 		return rc;
2408a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2409a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2410a7de92daSDan Williams 
2411a7de92daSDan Williams 	if (rc < 0 || cmd_rc || cmds.cfg_size.status != 0
2412a7de92daSDan Williams 			|| cmds.cfg_size.config_size != SZ_128K
2413a7de92daSDan Williams 			|| cmds.cfg_size.max_xfer != SZ_4K) {
2414a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2415a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2416a7de92daSDan Williams 		return -EIO;
2417a7de92daSDan Williams 	}
2418a7de92daSDan Williams 
2419a7de92daSDan Williams 
2420a7de92daSDan Williams 	/* test ars_status with zero output */
2421a7de92daSDan Williams 	cmd_size = offsetof(struct nd_cmd_ars_status, address);
2422a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2423a7de92daSDan Williams 		.out_length = 0,
2424a7de92daSDan Williams 	};
2425a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2426a7de92daSDan Williams 	if (rc)
2427a7de92daSDan Williams 		return rc;
2428a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2429a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2430a7de92daSDan Williams 
2431a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2432a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2433a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2434a7de92daSDan Williams 		return -EIO;
2435a7de92daSDan Williams 	}
2436a7de92daSDan Williams 
2437a7de92daSDan Williams 
2438a7de92daSDan Williams 	/* test ars_cap with benign extended status */
2439a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_cap);
2440a7de92daSDan Williams 	cmds.ars_cap = (struct nd_cmd_ars_cap) {
2441a7de92daSDan Williams 		.status = ND_ARS_PERSISTENT << 16,
2442a7de92daSDan Williams 	};
2443a7de92daSDan Williams 	offset = offsetof(struct nd_cmd_ars_cap, status);
2444a7de92daSDan Williams 	rc = setup_result(cmds.buf + offset, cmd_size - offset);
2445a7de92daSDan Williams 	if (rc)
2446a7de92daSDan Williams 		return rc;
2447a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_CAP,
2448a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2449a7de92daSDan Williams 
2450a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2451a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2452a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2453a7de92daSDan Williams 		return -EIO;
2454a7de92daSDan Williams 	}
2455a7de92daSDan Williams 
2456a7de92daSDan Williams 
2457a7de92daSDan Williams 	/* test ars_status with 'status' trimmed from 'out_length' */
2458a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2459a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2460a7de92daSDan Williams 		.out_length = cmd_size - 4,
2461a7de92daSDan Williams 	};
2462a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2463a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2464a7de92daSDan Williams 		.length = test_val,
2465a7de92daSDan Williams 	};
2466a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2467a7de92daSDan Williams 	if (rc)
2468a7de92daSDan Williams 		return rc;
2469a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2470a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2471a7de92daSDan Williams 
2472a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2473a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2474a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2475a7de92daSDan Williams 		return -EIO;
2476a7de92daSDan Williams 	}
2477a7de92daSDan Williams 
2478a7de92daSDan Williams 
2479a7de92daSDan Williams 	/* test ars_status with 'Output (Size)' including 'status' */
2480a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2481a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2482a7de92daSDan Williams 		.out_length = cmd_size,
2483a7de92daSDan Williams 	};
2484a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2485a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2486a7de92daSDan Williams 		.length = test_val,
2487a7de92daSDan Williams 	};
2488a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2489a7de92daSDan Williams 	if (rc)
2490a7de92daSDan Williams 		return rc;
2491a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2492a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2493a7de92daSDan Williams 
2494a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2495a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2496a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2497a7de92daSDan Williams 		return -EIO;
2498a7de92daSDan Williams 	}
2499a7de92daSDan Williams 
2500a7de92daSDan Williams 
2501a7de92daSDan Williams 	/* test extended status for get_config_size results in failure */
2502a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2503a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2504a7de92daSDan Williams 		.status = 1 << 16,
2505a7de92daSDan Williams 	};
2506a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2507a7de92daSDan Williams 	if (rc)
2508a7de92daSDan Williams 		return rc;
2509a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2510a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2511a7de92daSDan Williams 
2512a7de92daSDan Williams 	if (rc < 0 || cmd_rc >= 0) {
2513a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2514a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2515a7de92daSDan Williams 		return -EIO;
2516a7de92daSDan Williams 	}
2517a7de92daSDan Williams 
2518fb2a1748SDan Williams 	/* test clear error */
2519fb2a1748SDan Williams 	cmd_size = sizeof(cmds.clear_err);
2520fb2a1748SDan Williams 	cmds.clear_err = (struct nd_cmd_clear_error) {
2521fb2a1748SDan Williams 		.length = 512,
2522fb2a1748SDan Williams 		.cleared = 512,
2523fb2a1748SDan Williams 	};
2524fb2a1748SDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2525fb2a1748SDan Williams 	if (rc)
2526fb2a1748SDan Williams 		return rc;
2527fb2a1748SDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_CLEAR_ERROR,
2528fb2a1748SDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2529fb2a1748SDan Williams 	if (rc < 0 || cmd_rc) {
2530fb2a1748SDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2531fb2a1748SDan Williams 				__func__, __LINE__, rc, cmd_rc);
2532fb2a1748SDan Williams 		return -EIO;
2533fb2a1748SDan Williams 	}
2534fb2a1748SDan Williams 
2535a7de92daSDan Williams 	return 0;
2536a7de92daSDan Williams }
2537a7de92daSDan Williams 
25386bc75619SDan Williams static int nfit_test_probe(struct platform_device *pdev)
25396bc75619SDan Williams {
25406bc75619SDan Williams 	struct nvdimm_bus_descriptor *nd_desc;
25416bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
25426bc75619SDan Williams 	struct device *dev = &pdev->dev;
25436bc75619SDan Williams 	struct nfit_test *nfit_test;
2544231bf117SDan Williams 	struct nfit_mem *nfit_mem;
2545c14a868aSDan Williams 	union acpi_object *obj;
25466bc75619SDan Williams 	int rc;
25476bc75619SDan Williams 
2548a7de92daSDan Williams 	if (strcmp(dev_name(&pdev->dev), "nfit_test.0") == 0) {
2549a7de92daSDan Williams 		rc = nfit_ctl_test(&pdev->dev);
2550a7de92daSDan Williams 		if (rc)
2551a7de92daSDan Williams 			return rc;
2552a7de92daSDan Williams 	}
2553a7de92daSDan Williams 
25546bc75619SDan Williams 	nfit_test = to_nfit_test(&pdev->dev);
25556bc75619SDan Williams 
25566bc75619SDan Williams 	/* common alloc */
25576bc75619SDan Williams 	if (nfit_test->num_dcr) {
25586bc75619SDan Williams 		int num = nfit_test->num_dcr;
25596bc75619SDan Williams 
25606bc75619SDan Williams 		nfit_test->dimm = devm_kcalloc(dev, num, sizeof(void *),
25616bc75619SDan Williams 				GFP_KERNEL);
25626bc75619SDan Williams 		nfit_test->dimm_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
25636bc75619SDan Williams 				GFP_KERNEL);
25649d27a87eSDan Williams 		nfit_test->flush = devm_kcalloc(dev, num, sizeof(void *),
25659d27a87eSDan Williams 				GFP_KERNEL);
25669d27a87eSDan Williams 		nfit_test->flush_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
25679d27a87eSDan Williams 				GFP_KERNEL);
25686bc75619SDan Williams 		nfit_test->label = devm_kcalloc(dev, num, sizeof(void *),
25696bc75619SDan Williams 				GFP_KERNEL);
25706bc75619SDan Williams 		nfit_test->label_dma = devm_kcalloc(dev, num,
25716bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
25726bc75619SDan Williams 		nfit_test->dcr = devm_kcalloc(dev, num,
25736bc75619SDan Williams 				sizeof(struct nfit_test_dcr *), GFP_KERNEL);
25746bc75619SDan Williams 		nfit_test->dcr_dma = devm_kcalloc(dev, num,
25756bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
2576ed07c433SDan Williams 		nfit_test->smart = devm_kcalloc(dev, num,
2577ed07c433SDan Williams 				sizeof(struct nd_intel_smart), GFP_KERNEL);
2578ed07c433SDan Williams 		nfit_test->smart_threshold = devm_kcalloc(dev, num,
2579ed07c433SDan Williams 				sizeof(struct nd_intel_smart_threshold),
2580ed07c433SDan Williams 				GFP_KERNEL);
2581bfbaa952SDave Jiang 		nfit_test->fw = devm_kcalloc(dev, num,
2582bfbaa952SDave Jiang 				sizeof(struct nfit_test_fw), GFP_KERNEL);
25836bc75619SDan Williams 		if (nfit_test->dimm && nfit_test->dimm_dma && nfit_test->label
25846bc75619SDan Williams 				&& nfit_test->label_dma && nfit_test->dcr
25859d27a87eSDan Williams 				&& nfit_test->dcr_dma && nfit_test->flush
2586bfbaa952SDave Jiang 				&& nfit_test->flush_dma
2587bfbaa952SDave Jiang 				&& nfit_test->fw)
25886bc75619SDan Williams 			/* pass */;
25896bc75619SDan Williams 		else
25906bc75619SDan Williams 			return -ENOMEM;
25916bc75619SDan Williams 	}
25926bc75619SDan Williams 
25936bc75619SDan Williams 	if (nfit_test->num_pm) {
25946bc75619SDan Williams 		int num = nfit_test->num_pm;
25956bc75619SDan Williams 
25966bc75619SDan Williams 		nfit_test->spa_set = devm_kcalloc(dev, num, sizeof(void *),
25976bc75619SDan Williams 				GFP_KERNEL);
25986bc75619SDan Williams 		nfit_test->spa_set_dma = devm_kcalloc(dev, num,
25996bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
26006bc75619SDan Williams 		if (nfit_test->spa_set && nfit_test->spa_set_dma)
26016bc75619SDan Williams 			/* pass */;
26026bc75619SDan Williams 		else
26036bc75619SDan Williams 			return -ENOMEM;
26046bc75619SDan Williams 	}
26056bc75619SDan Williams 
26066bc75619SDan Williams 	/* per-nfit specific alloc */
26076bc75619SDan Williams 	if (nfit_test->alloc(nfit_test))
26086bc75619SDan Williams 		return -ENOMEM;
26096bc75619SDan Williams 
26106bc75619SDan Williams 	nfit_test->setup(nfit_test);
26116bc75619SDan Williams 	acpi_desc = &nfit_test->acpi_desc;
2612a61fe6f7SDan Williams 	acpi_nfit_desc_init(acpi_desc, &pdev->dev);
26136bc75619SDan Williams 	acpi_desc->blk_do_io = nfit_test_blk_do_io;
26146bc75619SDan Williams 	nd_desc = &acpi_desc->nd_desc;
2615a61fe6f7SDan Williams 	nd_desc->provider_name = NULL;
2616bc9775d8SDan Williams 	nd_desc->module = THIS_MODULE;
2617a61fe6f7SDan Williams 	nd_desc->ndctl = nfit_test_ctl;
26186bc75619SDan Williams 
2619e7a11b44SDan Williams 	rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_buf,
26201526f9e2SRoss Zwisler 			nfit_test->nfit_filled);
262158cd71b4SDan Williams 	if (rc)
262220985164SVishal Verma 		return rc;
262320985164SVishal Verma 
2624fbabd829SDan Williams 	rc = devm_add_action_or_reset(&pdev->dev, acpi_nfit_shutdown, acpi_desc);
2625fbabd829SDan Williams 	if (rc)
2626fbabd829SDan Williams 		return rc;
2627fbabd829SDan Williams 
262820985164SVishal Verma 	if (nfit_test->setup != nfit_test0_setup)
262920985164SVishal Verma 		return 0;
263020985164SVishal Verma 
263120985164SVishal Verma 	nfit_test->setup_hotplug = 1;
263220985164SVishal Verma 	nfit_test->setup(nfit_test);
263320985164SVishal Verma 
2634c14a868aSDan Williams 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
2635c14a868aSDan Williams 	if (!obj)
2636c14a868aSDan Williams 		return -ENOMEM;
2637c14a868aSDan Williams 	obj->type = ACPI_TYPE_BUFFER;
2638c14a868aSDan Williams 	obj->buffer.length = nfit_test->nfit_size;
2639c14a868aSDan Williams 	obj->buffer.pointer = nfit_test->nfit_buf;
2640c14a868aSDan Williams 	*(nfit_test->_fit) = obj;
2641c14a868aSDan Williams 	__acpi_nfit_notify(&pdev->dev, nfit_test, 0x80);
2642231bf117SDan Williams 
2643231bf117SDan Williams 	/* associate dimm devices with nfit_mem data for notification testing */
2644231bf117SDan Williams 	mutex_lock(&acpi_desc->init_mutex);
2645231bf117SDan Williams 	list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
2646231bf117SDan Williams 		u32 nfit_handle = __to_nfit_memdev(nfit_mem)->device_handle;
2647231bf117SDan Williams 		int i;
2648231bf117SDan Williams 
2649231bf117SDan Williams 		for (i = 0; i < NUM_DCR; i++)
2650231bf117SDan Williams 			if (nfit_handle == handle[i])
2651231bf117SDan Williams 				dev_set_drvdata(nfit_test->dimm_dev[i],
2652231bf117SDan Williams 						nfit_mem);
2653231bf117SDan Williams 	}
2654231bf117SDan Williams 	mutex_unlock(&acpi_desc->init_mutex);
26556bc75619SDan Williams 
26566bc75619SDan Williams 	return 0;
26576bc75619SDan Williams }
26586bc75619SDan Williams 
26596bc75619SDan Williams static int nfit_test_remove(struct platform_device *pdev)
26606bc75619SDan Williams {
26616bc75619SDan Williams 	return 0;
26626bc75619SDan Williams }
26636bc75619SDan Williams 
26646bc75619SDan Williams static void nfit_test_release(struct device *dev)
26656bc75619SDan Williams {
26666bc75619SDan Williams 	struct nfit_test *nfit_test = to_nfit_test(dev);
26676bc75619SDan Williams 
26686bc75619SDan Williams 	kfree(nfit_test);
26696bc75619SDan Williams }
26706bc75619SDan Williams 
26716bc75619SDan Williams static const struct platform_device_id nfit_test_id[] = {
26726bc75619SDan Williams 	{ KBUILD_MODNAME },
26736bc75619SDan Williams 	{ },
26746bc75619SDan Williams };
26756bc75619SDan Williams 
26766bc75619SDan Williams static struct platform_driver nfit_test_driver = {
26776bc75619SDan Williams 	.probe = nfit_test_probe,
26786bc75619SDan Williams 	.remove = nfit_test_remove,
26796bc75619SDan Williams 	.driver = {
26806bc75619SDan Williams 		.name = KBUILD_MODNAME,
26816bc75619SDan Williams 	},
26826bc75619SDan Williams 	.id_table = nfit_test_id,
26836bc75619SDan Williams };
26846bc75619SDan Williams 
26855d8beee2SDan Williams static char mcsafe_buf[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
26865d8beee2SDan Williams 
26875d8beee2SDan Williams enum INJECT {
26885d8beee2SDan Williams 	INJECT_NONE,
26895d8beee2SDan Williams 	INJECT_SRC,
26905d8beee2SDan Williams 	INJECT_DST,
26915d8beee2SDan Williams };
26925d8beee2SDan Williams 
26935d8beee2SDan Williams static void mcsafe_test_init(char *dst, char *src, size_t size)
26945d8beee2SDan Williams {
26955d8beee2SDan Williams 	size_t i;
26965d8beee2SDan Williams 
26975d8beee2SDan Williams 	memset(dst, 0xff, size);
26985d8beee2SDan Williams 	for (i = 0; i < size; i++)
26995d8beee2SDan Williams 		src[i] = (char) i;
27005d8beee2SDan Williams }
27015d8beee2SDan Williams 
27025d8beee2SDan Williams static bool mcsafe_test_validate(unsigned char *dst, unsigned char *src,
27035d8beee2SDan Williams 		size_t size, unsigned long rem)
27045d8beee2SDan Williams {
27055d8beee2SDan Williams 	size_t i;
27065d8beee2SDan Williams 
27075d8beee2SDan Williams 	for (i = 0; i < size - rem; i++)
27085d8beee2SDan Williams 		if (dst[i] != (unsigned char) i) {
27095d8beee2SDan Williams 			pr_info_once("%s:%d: offset: %zd got: %#x expect: %#x\n",
27105d8beee2SDan Williams 					__func__, __LINE__, i, dst[i],
27115d8beee2SDan Williams 					(unsigned char) i);
27125d8beee2SDan Williams 			return false;
27135d8beee2SDan Williams 		}
27145d8beee2SDan Williams 	for (i = size - rem; i < size; i++)
27155d8beee2SDan Williams 		if (dst[i] != 0xffU) {
27165d8beee2SDan Williams 			pr_info_once("%s:%d: offset: %zd got: %#x expect: 0xff\n",
27175d8beee2SDan Williams 					__func__, __LINE__, i, dst[i]);
27185d8beee2SDan Williams 			return false;
27195d8beee2SDan Williams 		}
27205d8beee2SDan Williams 	return true;
27215d8beee2SDan Williams }
27225d8beee2SDan Williams 
27235d8beee2SDan Williams void mcsafe_test(void)
27245d8beee2SDan Williams {
27255d8beee2SDan Williams 	char *inject_desc[] = { "none", "source", "destination" };
27265d8beee2SDan Williams 	enum INJECT inj;
27275d8beee2SDan Williams 
27285d8beee2SDan Williams 	if (IS_ENABLED(CONFIG_MCSAFE_TEST)) {
27295d8beee2SDan Williams 		pr_info("%s: run...\n", __func__);
27305d8beee2SDan Williams 	} else {
27315d8beee2SDan Williams 		pr_info("%s: disabled, skip.\n", __func__);
27325d8beee2SDan Williams 		return;
27335d8beee2SDan Williams 	}
27345d8beee2SDan Williams 
27355d8beee2SDan Williams 	for (inj = INJECT_NONE; inj <= INJECT_DST; inj++) {
27365d8beee2SDan Williams 		int i;
27375d8beee2SDan Williams 
27385d8beee2SDan Williams 		pr_info("%s: inject: %s\n", __func__, inject_desc[inj]);
27395d8beee2SDan Williams 		for (i = 0; i < 512; i++) {
27405d8beee2SDan Williams 			unsigned long expect, rem;
27415d8beee2SDan Williams 			void *src, *dst;
27425d8beee2SDan Williams 			bool valid;
27435d8beee2SDan Williams 
27445d8beee2SDan Williams 			switch (inj) {
27455d8beee2SDan Williams 			case INJECT_NONE:
27465d8beee2SDan Williams 				mcsafe_inject_src(NULL);
27475d8beee2SDan Williams 				mcsafe_inject_dst(NULL);
27485d8beee2SDan Williams 				dst = &mcsafe_buf[2048];
27495d8beee2SDan Williams 				src = &mcsafe_buf[1024 - i];
27505d8beee2SDan Williams 				expect = 0;
27515d8beee2SDan Williams 				break;
27525d8beee2SDan Williams 			case INJECT_SRC:
27535d8beee2SDan Williams 				mcsafe_inject_src(&mcsafe_buf[1024]);
27545d8beee2SDan Williams 				mcsafe_inject_dst(NULL);
27555d8beee2SDan Williams 				dst = &mcsafe_buf[2048];
27565d8beee2SDan Williams 				src = &mcsafe_buf[1024 - i];
27575d8beee2SDan Williams 				expect = 512 - i;
27585d8beee2SDan Williams 				break;
27595d8beee2SDan Williams 			case INJECT_DST:
27605d8beee2SDan Williams 				mcsafe_inject_src(NULL);
27615d8beee2SDan Williams 				mcsafe_inject_dst(&mcsafe_buf[2048]);
27625d8beee2SDan Williams 				dst = &mcsafe_buf[2048 - i];
27635d8beee2SDan Williams 				src = &mcsafe_buf[1024];
27645d8beee2SDan Williams 				expect = 512 - i;
27655d8beee2SDan Williams 				break;
27665d8beee2SDan Williams 			}
27675d8beee2SDan Williams 
27685d8beee2SDan Williams 			mcsafe_test_init(dst, src, 512);
27695d8beee2SDan Williams 			rem = __memcpy_mcsafe(dst, src, 512);
27705d8beee2SDan Williams 			valid = mcsafe_test_validate(dst, src, 512, expect);
27715d8beee2SDan Williams 			if (rem == expect && valid)
27725d8beee2SDan Williams 				continue;
27735d8beee2SDan Williams 			pr_info("%s: copy(%#lx, %#lx, %d) off: %d rem: %ld %s expect: %ld\n",
27745d8beee2SDan Williams 					__func__,
27755d8beee2SDan Williams 					((unsigned long) dst) & ~PAGE_MASK,
27765d8beee2SDan Williams 					((unsigned long ) src) & ~PAGE_MASK,
27775d8beee2SDan Williams 					512, i, rem, valid ? "valid" : "bad",
27785d8beee2SDan Williams 					expect);
27795d8beee2SDan Williams 		}
27805d8beee2SDan Williams 	}
27815d8beee2SDan Williams 
27825d8beee2SDan Williams 	mcsafe_inject_src(NULL);
27835d8beee2SDan Williams 	mcsafe_inject_dst(NULL);
27845d8beee2SDan Williams }
27855d8beee2SDan Williams 
27866bc75619SDan Williams static __init int nfit_test_init(void)
27876bc75619SDan Williams {
27886bc75619SDan Williams 	int rc, i;
27896bc75619SDan Williams 
27900fb5c8dfSDan Williams 	pmem_test();
27910fb5c8dfSDan Williams 	libnvdimm_test();
27920fb5c8dfSDan Williams 	acpi_nfit_test();
27930fb5c8dfSDan Williams 	device_dax_test();
27945d8beee2SDan Williams 	mcsafe_test();
27950fb5c8dfSDan Williams 
2796a7de92daSDan Williams 	nfit_test_setup(nfit_test_lookup, nfit_test_evaluate_dsm);
2797231bf117SDan Williams 
27989fb1a190SDave Jiang 	nfit_wq = create_singlethread_workqueue("nfit");
27999fb1a190SDave Jiang 	if (!nfit_wq)
28009fb1a190SDave Jiang 		return -ENOMEM;
28019fb1a190SDave Jiang 
2802a7de92daSDan Williams 	nfit_test_dimm = class_create(THIS_MODULE, "nfit_test_dimm");
2803a7de92daSDan Williams 	if (IS_ERR(nfit_test_dimm)) {
2804a7de92daSDan Williams 		rc = PTR_ERR(nfit_test_dimm);
2805a7de92daSDan Williams 		goto err_register;
2806a7de92daSDan Williams 	}
28076bc75619SDan Williams 
28086bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++) {
28096bc75619SDan Williams 		struct nfit_test *nfit_test;
28106bc75619SDan Williams 		struct platform_device *pdev;
28116bc75619SDan Williams 
28126bc75619SDan Williams 		nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
28136bc75619SDan Williams 		if (!nfit_test) {
28146bc75619SDan Williams 			rc = -ENOMEM;
28156bc75619SDan Williams 			goto err_register;
28166bc75619SDan Williams 		}
28176bc75619SDan Williams 		INIT_LIST_HEAD(&nfit_test->resources);
28189fb1a190SDave Jiang 		badrange_init(&nfit_test->badrange);
28196bc75619SDan Williams 		switch (i) {
28206bc75619SDan Williams 		case 0:
28216bc75619SDan Williams 			nfit_test->num_pm = NUM_PM;
2822dafb1048SDan Williams 			nfit_test->dcr_idx = 0;
28236bc75619SDan Williams 			nfit_test->num_dcr = NUM_DCR;
28246bc75619SDan Williams 			nfit_test->alloc = nfit_test0_alloc;
28256bc75619SDan Williams 			nfit_test->setup = nfit_test0_setup;
28266bc75619SDan Williams 			break;
28276bc75619SDan Williams 		case 1:
2828a117699cSYasunori Goto 			nfit_test->num_pm = 2;
2829dafb1048SDan Williams 			nfit_test->dcr_idx = NUM_DCR;
2830ac40b675SDan Williams 			nfit_test->num_dcr = 2;
28316bc75619SDan Williams 			nfit_test->alloc = nfit_test1_alloc;
28326bc75619SDan Williams 			nfit_test->setup = nfit_test1_setup;
28336bc75619SDan Williams 			break;
28346bc75619SDan Williams 		default:
28356bc75619SDan Williams 			rc = -EINVAL;
28366bc75619SDan Williams 			goto err_register;
28376bc75619SDan Williams 		}
28386bc75619SDan Williams 		pdev = &nfit_test->pdev;
28396bc75619SDan Williams 		pdev->name = KBUILD_MODNAME;
28406bc75619SDan Williams 		pdev->id = i;
28416bc75619SDan Williams 		pdev->dev.release = nfit_test_release;
28426bc75619SDan Williams 		rc = platform_device_register(pdev);
28436bc75619SDan Williams 		if (rc) {
28446bc75619SDan Williams 			put_device(&pdev->dev);
28456bc75619SDan Williams 			goto err_register;
28466bc75619SDan Williams 		}
28478b06b884SDan Williams 		get_device(&pdev->dev);
28486bc75619SDan Williams 
28496bc75619SDan Williams 		rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
28506bc75619SDan Williams 		if (rc)
28516bc75619SDan Williams 			goto err_register;
28526bc75619SDan Williams 
28536bc75619SDan Williams 		instances[i] = nfit_test;
28549fb1a190SDave Jiang 		INIT_WORK(&nfit_test->work, uc_error_notify);
28556bc75619SDan Williams 	}
28566bc75619SDan Williams 
28576bc75619SDan Williams 	rc = platform_driver_register(&nfit_test_driver);
28586bc75619SDan Williams 	if (rc)
28596bc75619SDan Williams 		goto err_register;
28606bc75619SDan Williams 	return 0;
28616bc75619SDan Williams 
28626bc75619SDan Williams  err_register:
28639fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
28646bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
28656bc75619SDan Williams 		if (instances[i])
28666bc75619SDan Williams 			platform_device_unregister(&instances[i]->pdev);
28676bc75619SDan Williams 	nfit_test_teardown();
28688b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
28698b06b884SDan Williams 		if (instances[i])
28708b06b884SDan Williams 			put_device(&instances[i]->pdev.dev);
28718b06b884SDan Williams 
28726bc75619SDan Williams 	return rc;
28736bc75619SDan Williams }
28746bc75619SDan Williams 
28756bc75619SDan Williams static __exit void nfit_test_exit(void)
28766bc75619SDan Williams {
28776bc75619SDan Williams 	int i;
28786bc75619SDan Williams 
28799fb1a190SDave Jiang 	flush_workqueue(nfit_wq);
28809fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
28816bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
28826bc75619SDan Williams 		platform_device_unregister(&instances[i]->pdev);
28838b06b884SDan Williams 	platform_driver_unregister(&nfit_test_driver);
28846bc75619SDan Williams 	nfit_test_teardown();
28858b06b884SDan Williams 
28868b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
28878b06b884SDan Williams 		put_device(&instances[i]->pdev.dev);
2888231bf117SDan Williams 	class_destroy(nfit_test_dimm);
28896bc75619SDan Williams }
28906bc75619SDan Williams 
28916bc75619SDan Williams module_init(nfit_test_init);
28926bc75619SDan Williams module_exit(nfit_test_exit);
28936bc75619SDan Williams MODULE_LICENSE("GPL v2");
28946bc75619SDan Williams MODULE_AUTHOR("Intel Corporation");
2895