xref: /linux/tools/testing/nvdimm/test/nfit.c (revision 55c72ab62e47fc584131901baddb2752e949ebcd)
16bc75619SDan Williams /*
26bc75619SDan Williams  * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
36bc75619SDan Williams  *
46bc75619SDan Williams  * This program is free software; you can redistribute it and/or modify
56bc75619SDan Williams  * it under the terms of version 2 of the GNU General Public License as
66bc75619SDan Williams  * published by the Free Software Foundation.
76bc75619SDan Williams  *
86bc75619SDan Williams  * This program is distributed in the hope that it will be useful, but
96bc75619SDan Williams  * WITHOUT ANY WARRANTY; without even the implied warranty of
106bc75619SDan Williams  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116bc75619SDan Williams  * General Public License for more details.
126bc75619SDan Williams  */
136bc75619SDan Williams #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
146bc75619SDan Williams #include <linux/platform_device.h>
156bc75619SDan Williams #include <linux/dma-mapping.h>
16d8d378faSDan Williams #include <linux/workqueue.h>
176bc75619SDan Williams #include <linux/libnvdimm.h>
186bc75619SDan Williams #include <linux/vmalloc.h>
196bc75619SDan Williams #include <linux/device.h>
206bc75619SDan Williams #include <linux/module.h>
2120985164SVishal Verma #include <linux/mutex.h>
226bc75619SDan Williams #include <linux/ndctl.h>
236bc75619SDan Williams #include <linux/sizes.h>
2420985164SVishal Verma #include <linux/list.h>
256bc75619SDan Williams #include <linux/slab.h>
26a7de92daSDan Williams #include <nd-core.h>
276bc75619SDan Williams #include <nfit.h>
286bc75619SDan Williams #include <nd.h>
296bc75619SDan Williams #include "nfit_test.h"
300fb5c8dfSDan Williams #include "../watermark.h"
316bc75619SDan Williams 
326bc75619SDan Williams /*
336bc75619SDan Williams  * Generate an NFIT table to describe the following topology:
346bc75619SDan Williams  *
356bc75619SDan Williams  * BUS0: Interleaved PMEM regions, and aliasing with BLK regions
366bc75619SDan Williams  *
376bc75619SDan Williams  *                     (a)                       (b)            DIMM   BLK-REGION
386bc75619SDan Williams  *           +----------+--------------+----------+---------+
396bc75619SDan Williams  * +------+  |  blk2.0  |     pm0.0    |  blk2.1  |  pm1.0  |    0      region2
406bc75619SDan Williams  * | imc0 +--+- - - - - region0 - - - -+----------+         +
416bc75619SDan Williams  * +--+---+  |  blk3.0  |     pm0.0    |  blk3.1  |  pm1.0  |    1      region3
426bc75619SDan Williams  *    |      +----------+--------------v----------v         v
436bc75619SDan Williams  * +--+---+                            |                    |
446bc75619SDan Williams  * | cpu0 |                                    region1
456bc75619SDan Williams  * +--+---+                            |                    |
466bc75619SDan Williams  *    |      +-------------------------^----------^         ^
476bc75619SDan Williams  * +--+---+  |                 blk4.0             |  pm1.0  |    2      region4
486bc75619SDan Williams  * | imc1 +--+-------------------------+----------+         +
496bc75619SDan Williams  * +------+  |                 blk5.0             |  pm1.0  |    3      region5
506bc75619SDan Williams  *           +-------------------------+----------+-+-------+
516bc75619SDan Williams  *
5220985164SVishal Verma  * +--+---+
5320985164SVishal Verma  * | cpu1 |
5420985164SVishal Verma  * +--+---+                   (Hotplug DIMM)
5520985164SVishal Verma  *    |      +----------------------------------------------+
5620985164SVishal Verma  * +--+---+  |                 blk6.0/pm7.0                 |    4      region6/7
5720985164SVishal Verma  * | imc0 +--+----------------------------------------------+
5820985164SVishal Verma  * +------+
5920985164SVishal Verma  *
6020985164SVishal Verma  *
616bc75619SDan Williams  * *) In this layout we have four dimms and two memory controllers in one
626bc75619SDan Williams  *    socket.  Each unique interface (BLK or PMEM) to DPA space
636bc75619SDan Williams  *    is identified by a region device with a dynamically assigned id.
646bc75619SDan Williams  *
656bc75619SDan Williams  * *) The first portion of dimm0 and dimm1 are interleaved as REGION0.
666bc75619SDan Williams  *    A single PMEM namespace "pm0.0" is created using half of the
676bc75619SDan Williams  *    REGION0 SPA-range.  REGION0 spans dimm0 and dimm1.  PMEM namespace
686bc75619SDan Williams  *    allocate from from the bottom of a region.  The unallocated
696bc75619SDan Williams  *    portion of REGION0 aliases with REGION2 and REGION3.  That
706bc75619SDan Williams  *    unallacted capacity is reclaimed as BLK namespaces ("blk2.0" and
716bc75619SDan Williams  *    "blk3.0") starting at the base of each DIMM to offset (a) in those
726bc75619SDan Williams  *    DIMMs.  "pm0.0", "blk2.0" and "blk3.0" are free-form readable
736bc75619SDan Williams  *    names that can be assigned to a namespace.
746bc75619SDan Williams  *
756bc75619SDan Williams  * *) In the last portion of dimm0 and dimm1 we have an interleaved
766bc75619SDan Williams  *    SPA range, REGION1, that spans those two dimms as well as dimm2
776bc75619SDan Williams  *    and dimm3.  Some of REGION1 allocated to a PMEM namespace named
786bc75619SDan Williams  *    "pm1.0" the rest is reclaimed in 4 BLK namespaces (for each
796bc75619SDan Williams  *    dimm in the interleave set), "blk2.1", "blk3.1", "blk4.0", and
806bc75619SDan Williams  *    "blk5.0".
816bc75619SDan Williams  *
826bc75619SDan Williams  * *) The portion of dimm2 and dimm3 that do not participate in the
836bc75619SDan Williams  *    REGION1 interleaved SPA range (i.e. the DPA address below offset
846bc75619SDan Williams  *    (b) are also included in the "blk4.0" and "blk5.0" namespaces.
856bc75619SDan Williams  *    Note, that BLK namespaces need not be contiguous in DPA-space, and
866bc75619SDan Williams  *    can consume aliased capacity from multiple interleave sets.
876bc75619SDan Williams  *
886bc75619SDan Williams  * BUS1: Legacy NVDIMM (single contiguous range)
896bc75619SDan Williams  *
906bc75619SDan Williams  *  region2
916bc75619SDan Williams  * +---------------------+
926bc75619SDan Williams  * |---------------------|
936bc75619SDan Williams  * ||       pm2.0       ||
946bc75619SDan Williams  * |---------------------|
956bc75619SDan Williams  * +---------------------+
966bc75619SDan Williams  *
976bc75619SDan Williams  * *) A NFIT-table may describe a simple system-physical-address range
986bc75619SDan Williams  *    with no BLK aliasing.  This type of region may optionally
996bc75619SDan Williams  *    reference an NVDIMM.
1006bc75619SDan Williams  */
1016bc75619SDan Williams enum {
10220985164SVishal Verma 	NUM_PM  = 3,
10320985164SVishal Verma 	NUM_DCR = 5,
10485d3fa02SDan Williams 	NUM_HINTS = 8,
1056bc75619SDan Williams 	NUM_BDW = NUM_DCR,
1066bc75619SDan Williams 	NUM_SPA = NUM_PM + NUM_DCR + NUM_BDW,
1079741a559SRoss Zwisler 	NUM_MEM = NUM_DCR + NUM_BDW + 2 /* spa0 iset */
1089741a559SRoss Zwisler 		+ 4 /* spa1 iset */ + 1 /* spa11 iset */,
1096bc75619SDan Williams 	DIMM_SIZE = SZ_32M,
1106bc75619SDan Williams 	LABEL_SIZE = SZ_128K,
1117bfe97c7SDan Williams 	SPA_VCD_SIZE = SZ_4M,
1126bc75619SDan Williams 	SPA0_SIZE = DIMM_SIZE,
1136bc75619SDan Williams 	SPA1_SIZE = DIMM_SIZE*2,
1146bc75619SDan Williams 	SPA2_SIZE = DIMM_SIZE,
1156bc75619SDan Williams 	BDW_SIZE = 64 << 8,
1166bc75619SDan Williams 	DCR_SIZE = 12,
1176bc75619SDan Williams 	NUM_NFITS = 2, /* permit testing multiple NFITs per system */
1186bc75619SDan Williams };
1196bc75619SDan Williams 
1206bc75619SDan Williams struct nfit_test_dcr {
1216bc75619SDan Williams 	__le64 bdw_addr;
1226bc75619SDan Williams 	__le32 bdw_status;
1236bc75619SDan Williams 	__u8 aperature[BDW_SIZE];
1246bc75619SDan Williams };
1256bc75619SDan Williams 
1266bc75619SDan Williams #define NFIT_DIMM_HANDLE(node, socket, imc, chan, dimm) \
1276bc75619SDan Williams 	(((node & 0xfff) << 16) | ((socket & 0xf) << 12) \
1286bc75619SDan Williams 	 | ((imc & 0xf) << 8) | ((chan & 0xf) << 4) | (dimm & 0xf))
1296bc75619SDan Williams 
130dafb1048SDan Williams static u32 handle[] = {
1316bc75619SDan Williams 	[0] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 0),
1326bc75619SDan Williams 	[1] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 1),
1336bc75619SDan Williams 	[2] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 0),
1346bc75619SDan Williams 	[3] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 1),
13520985164SVishal Verma 	[4] = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
136dafb1048SDan Williams 	[5] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 0),
137ac40b675SDan Williams 	[6] = NFIT_DIMM_HANDLE(1, 0, 0, 0, 1),
1386bc75619SDan Williams };
1396bc75619SDan Williams 
14073606afdSDan Williams static unsigned long dimm_fail_cmd_flags[NUM_DCR];
141*55c72ab6SDan Williams static int dimm_fail_cmd_code[NUM_DCR];
14273606afdSDan Williams 
143bfbaa952SDave Jiang struct nfit_test_fw {
144bfbaa952SDave Jiang 	enum intel_fw_update_state state;
145bfbaa952SDave Jiang 	u32 context;
146bfbaa952SDave Jiang 	u64 version;
147bfbaa952SDave Jiang 	u32 size_received;
148bfbaa952SDave Jiang 	u64 end_time;
149bfbaa952SDave Jiang };
150bfbaa952SDave Jiang 
1516bc75619SDan Williams struct nfit_test {
1526bc75619SDan Williams 	struct acpi_nfit_desc acpi_desc;
1536bc75619SDan Williams 	struct platform_device pdev;
1546bc75619SDan Williams 	struct list_head resources;
1556bc75619SDan Williams 	void *nfit_buf;
1566bc75619SDan Williams 	dma_addr_t nfit_dma;
1576bc75619SDan Williams 	size_t nfit_size;
1581526f9e2SRoss Zwisler 	size_t nfit_filled;
159dafb1048SDan Williams 	int dcr_idx;
1606bc75619SDan Williams 	int num_dcr;
1616bc75619SDan Williams 	int num_pm;
1626bc75619SDan Williams 	void **dimm;
1636bc75619SDan Williams 	dma_addr_t *dimm_dma;
1649d27a87eSDan Williams 	void **flush;
1659d27a87eSDan Williams 	dma_addr_t *flush_dma;
1666bc75619SDan Williams 	void **label;
1676bc75619SDan Williams 	dma_addr_t *label_dma;
1686bc75619SDan Williams 	void **spa_set;
1696bc75619SDan Williams 	dma_addr_t *spa_set_dma;
1706bc75619SDan Williams 	struct nfit_test_dcr **dcr;
1716bc75619SDan Williams 	dma_addr_t *dcr_dma;
1726bc75619SDan Williams 	int (*alloc)(struct nfit_test *t);
1736bc75619SDan Williams 	void (*setup)(struct nfit_test *t);
17420985164SVishal Verma 	int setup_hotplug;
175c14a868aSDan Williams 	union acpi_object **_fit;
176c14a868aSDan Williams 	dma_addr_t _fit_dma;
177f471f1a7SDan Williams 	struct ars_state {
178f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status;
179f471f1a7SDan Williams 		unsigned long deadline;
180f471f1a7SDan Williams 		spinlock_t lock;
181f471f1a7SDan Williams 	} ars_state;
182231bf117SDan Williams 	struct device *dimm_dev[NUM_DCR];
183ed07c433SDan Williams 	struct nd_intel_smart *smart;
184ed07c433SDan Williams 	struct nd_intel_smart_threshold *smart_threshold;
1859fb1a190SDave Jiang 	struct badrange badrange;
1869fb1a190SDave Jiang 	struct work_struct work;
187bfbaa952SDave Jiang 	struct nfit_test_fw *fw;
1886bc75619SDan Williams };
1896bc75619SDan Williams 
1909fb1a190SDave Jiang static struct workqueue_struct *nfit_wq;
1919fb1a190SDave Jiang 
1926bc75619SDan Williams static struct nfit_test *to_nfit_test(struct device *dev)
1936bc75619SDan Williams {
1946bc75619SDan Williams 	struct platform_device *pdev = to_platform_device(dev);
1956bc75619SDan Williams 
1966bc75619SDan Williams 	return container_of(pdev, struct nfit_test, pdev);
1976bc75619SDan Williams }
1986bc75619SDan Williams 
199bfbaa952SDave Jiang static int nd_intel_test_get_fw_info(struct nfit_test *t,
200bfbaa952SDave Jiang 		struct nd_intel_fw_info *nd_cmd, unsigned int buf_len,
201bfbaa952SDave Jiang 		int idx)
202bfbaa952SDave Jiang {
203bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
204bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
205bfbaa952SDave Jiang 
206bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p, buf_len: %u, idx: %d\n",
207bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
208bfbaa952SDave Jiang 
209bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
210bfbaa952SDave Jiang 		return -EINVAL;
211bfbaa952SDave Jiang 
212bfbaa952SDave Jiang 	nd_cmd->status = 0;
213bfbaa952SDave Jiang 	nd_cmd->storage_size = INTEL_FW_STORAGE_SIZE;
214bfbaa952SDave Jiang 	nd_cmd->max_send_len = INTEL_FW_MAX_SEND_LEN;
215bfbaa952SDave Jiang 	nd_cmd->query_interval = INTEL_FW_QUERY_INTERVAL;
216bfbaa952SDave Jiang 	nd_cmd->max_query_time = INTEL_FW_QUERY_MAX_TIME;
217bfbaa952SDave Jiang 	nd_cmd->update_cap = 0;
218bfbaa952SDave Jiang 	nd_cmd->fis_version = INTEL_FW_FIS_VERSION;
219bfbaa952SDave Jiang 	nd_cmd->run_version = 0;
220bfbaa952SDave Jiang 	nd_cmd->updated_version = fw->version;
221bfbaa952SDave Jiang 
222bfbaa952SDave Jiang 	return 0;
223bfbaa952SDave Jiang }
224bfbaa952SDave Jiang 
225bfbaa952SDave Jiang static int nd_intel_test_start_update(struct nfit_test *t,
226bfbaa952SDave Jiang 		struct nd_intel_fw_start *nd_cmd, unsigned int buf_len,
227bfbaa952SDave Jiang 		int idx)
228bfbaa952SDave Jiang {
229bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
230bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
231bfbaa952SDave Jiang 
232bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
233bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
234bfbaa952SDave Jiang 
235bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
236bfbaa952SDave Jiang 		return -EINVAL;
237bfbaa952SDave Jiang 
238bfbaa952SDave Jiang 	if (fw->state != FW_STATE_NEW) {
239bfbaa952SDave Jiang 		/* extended status, FW update in progress */
240bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
241bfbaa952SDave Jiang 		return 0;
242bfbaa952SDave Jiang 	}
243bfbaa952SDave Jiang 
244bfbaa952SDave Jiang 	fw->state = FW_STATE_IN_PROGRESS;
245bfbaa952SDave Jiang 	fw->context++;
246bfbaa952SDave Jiang 	fw->size_received = 0;
247bfbaa952SDave Jiang 	nd_cmd->status = 0;
248bfbaa952SDave Jiang 	nd_cmd->context = fw->context;
249bfbaa952SDave Jiang 
250bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context issued: %#x\n", __func__, nd_cmd->context);
251bfbaa952SDave Jiang 
252bfbaa952SDave Jiang 	return 0;
253bfbaa952SDave Jiang }
254bfbaa952SDave Jiang 
255bfbaa952SDave Jiang static int nd_intel_test_send_data(struct nfit_test *t,
256bfbaa952SDave Jiang 		struct nd_intel_fw_send_data *nd_cmd, unsigned int buf_len,
257bfbaa952SDave Jiang 		int idx)
258bfbaa952SDave Jiang {
259bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
260bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
261bfbaa952SDave Jiang 	u32 *status = (u32 *)&nd_cmd->data[nd_cmd->length];
262bfbaa952SDave Jiang 
263bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
264bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
265bfbaa952SDave Jiang 
266bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
267bfbaa952SDave Jiang 		return -EINVAL;
268bfbaa952SDave Jiang 
269bfbaa952SDave Jiang 
270bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->status: %#x\n", __func__, *status);
271bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[0]: %#x\n", __func__, nd_cmd->data[0]);
272bfbaa952SDave Jiang 	dev_dbg(dev, "%s: cmd->data[%u]: %#x\n", __func__, nd_cmd->length-1,
273bfbaa952SDave Jiang 			nd_cmd->data[nd_cmd->length-1]);
274bfbaa952SDave Jiang 
275bfbaa952SDave Jiang 	if (fw->state != FW_STATE_IN_PROGRESS) {
276bfbaa952SDave Jiang 		dev_dbg(dev, "%s: not in IN_PROGRESS state\n", __func__);
277bfbaa952SDave Jiang 		*status = 0x5;
278bfbaa952SDave Jiang 		return 0;
279bfbaa952SDave Jiang 	}
280bfbaa952SDave Jiang 
281bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
282bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
283bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
284bfbaa952SDave Jiang 		*status = 0x10007;
285bfbaa952SDave Jiang 		return 0;
286bfbaa952SDave Jiang 	}
287bfbaa952SDave Jiang 
288bfbaa952SDave Jiang 	/*
289bfbaa952SDave Jiang 	 * check offset + len > size of fw storage
290bfbaa952SDave Jiang 	 * check length is > max send length
291bfbaa952SDave Jiang 	 */
292bfbaa952SDave Jiang 	if (nd_cmd->offset + nd_cmd->length > INTEL_FW_STORAGE_SIZE ||
293bfbaa952SDave Jiang 			nd_cmd->length > INTEL_FW_MAX_SEND_LEN) {
294bfbaa952SDave Jiang 		*status = 0x3;
295bfbaa952SDave Jiang 		dev_dbg(dev, "%s: buffer boundary violation\n", __func__);
296bfbaa952SDave Jiang 		return 0;
297bfbaa952SDave Jiang 	}
298bfbaa952SDave Jiang 
299bfbaa952SDave Jiang 	fw->size_received += nd_cmd->length;
300bfbaa952SDave Jiang 	dev_dbg(dev, "%s: copying %u bytes, %u bytes so far\n",
301bfbaa952SDave Jiang 			__func__, nd_cmd->length, fw->size_received);
302bfbaa952SDave Jiang 	*status = 0;
303bfbaa952SDave Jiang 	return 0;
304bfbaa952SDave Jiang }
305bfbaa952SDave Jiang 
306bfbaa952SDave Jiang static int nd_intel_test_finish_fw(struct nfit_test *t,
307bfbaa952SDave Jiang 		struct nd_intel_fw_finish_update *nd_cmd,
308bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
309bfbaa952SDave Jiang {
310bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
311bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
312bfbaa952SDave Jiang 
313bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
314bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
315bfbaa952SDave Jiang 
316bfbaa952SDave Jiang 	if (fw->state == FW_STATE_UPDATED) {
317bfbaa952SDave Jiang 		/* update already done, need cold boot */
318bfbaa952SDave Jiang 		nd_cmd->status = 0x20007;
319bfbaa952SDave Jiang 		return 0;
320bfbaa952SDave Jiang 	}
321bfbaa952SDave Jiang 
322bfbaa952SDave Jiang 	dev_dbg(dev, "%s: context: %#x  ctrl_flags: %#x\n",
323bfbaa952SDave Jiang 			__func__, nd_cmd->context, nd_cmd->ctrl_flags);
324bfbaa952SDave Jiang 
325bfbaa952SDave Jiang 	switch (nd_cmd->ctrl_flags) {
326bfbaa952SDave Jiang 	case 0: /* finish */
327bfbaa952SDave Jiang 		if (nd_cmd->context != fw->context) {
328bfbaa952SDave Jiang 			dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
329bfbaa952SDave Jiang 					__func__, nd_cmd->context,
330bfbaa952SDave Jiang 					fw->context);
331bfbaa952SDave Jiang 			nd_cmd->status = 0x10007;
332bfbaa952SDave Jiang 			return 0;
333bfbaa952SDave Jiang 		}
334bfbaa952SDave Jiang 		nd_cmd->status = 0;
335bfbaa952SDave Jiang 		fw->state = FW_STATE_VERIFY;
336bfbaa952SDave Jiang 		/* set 1 second of time for firmware "update" */
337bfbaa952SDave Jiang 		fw->end_time = jiffies + HZ;
338bfbaa952SDave Jiang 		break;
339bfbaa952SDave Jiang 
340bfbaa952SDave Jiang 	case 1: /* abort */
341bfbaa952SDave Jiang 		fw->size_received = 0;
342bfbaa952SDave Jiang 		/* successfully aborted status */
343bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
344bfbaa952SDave Jiang 		fw->state = FW_STATE_NEW;
345bfbaa952SDave Jiang 		dev_dbg(dev, "%s: abort successful\n", __func__);
346bfbaa952SDave Jiang 		break;
347bfbaa952SDave Jiang 
348bfbaa952SDave Jiang 	default: /* bad control flag */
349bfbaa952SDave Jiang 		dev_warn(dev, "%s: unknown control flag: %#x\n",
350bfbaa952SDave Jiang 				__func__, nd_cmd->ctrl_flags);
351bfbaa952SDave Jiang 		return -EINVAL;
352bfbaa952SDave Jiang 	}
353bfbaa952SDave Jiang 
354bfbaa952SDave Jiang 	return 0;
355bfbaa952SDave Jiang }
356bfbaa952SDave Jiang 
357bfbaa952SDave Jiang static int nd_intel_test_finish_query(struct nfit_test *t,
358bfbaa952SDave Jiang 		struct nd_intel_fw_finish_query *nd_cmd,
359bfbaa952SDave Jiang 		unsigned int buf_len, int idx)
360bfbaa952SDave Jiang {
361bfbaa952SDave Jiang 	struct device *dev = &t->pdev.dev;
362bfbaa952SDave Jiang 	struct nfit_test_fw *fw = &t->fw[idx];
363bfbaa952SDave Jiang 
364bfbaa952SDave Jiang 	dev_dbg(dev, "%s(nfit_test: %p nd_cmd: %p buf_len: %u idx: %d)\n",
365bfbaa952SDave Jiang 			__func__, t, nd_cmd, buf_len, idx);
366bfbaa952SDave Jiang 
367bfbaa952SDave Jiang 	if (buf_len < sizeof(*nd_cmd))
368bfbaa952SDave Jiang 		return -EINVAL;
369bfbaa952SDave Jiang 
370bfbaa952SDave Jiang 	if (nd_cmd->context != fw->context) {
371bfbaa952SDave Jiang 		dev_dbg(dev, "%s: incorrect context: in: %#x correct: %#x\n",
372bfbaa952SDave Jiang 				__func__, nd_cmd->context, fw->context);
373bfbaa952SDave Jiang 		nd_cmd->status = 0x10007;
374bfbaa952SDave Jiang 		return 0;
375bfbaa952SDave Jiang 	}
376bfbaa952SDave Jiang 
377bfbaa952SDave Jiang 	dev_dbg(dev, "%s context: %#x\n", __func__, nd_cmd->context);
378bfbaa952SDave Jiang 
379bfbaa952SDave Jiang 	switch (fw->state) {
380bfbaa952SDave Jiang 	case FW_STATE_NEW:
381bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
382bfbaa952SDave Jiang 		nd_cmd->status = 0;
383bfbaa952SDave Jiang 		dev_dbg(dev, "%s: new state\n", __func__);
384bfbaa952SDave Jiang 		break;
385bfbaa952SDave Jiang 
386bfbaa952SDave Jiang 	case FW_STATE_IN_PROGRESS:
387bfbaa952SDave Jiang 		/* sequencing error */
388bfbaa952SDave Jiang 		nd_cmd->status = 0x40007;
389bfbaa952SDave Jiang 		nd_cmd->updated_fw_rev = 0;
390bfbaa952SDave Jiang 		dev_dbg(dev, "%s: sequence error\n", __func__);
391bfbaa952SDave Jiang 		break;
392bfbaa952SDave Jiang 
393bfbaa952SDave Jiang 	case FW_STATE_VERIFY:
394bfbaa952SDave Jiang 		if (time_is_after_jiffies64(fw->end_time)) {
395bfbaa952SDave Jiang 			nd_cmd->updated_fw_rev = 0;
396bfbaa952SDave Jiang 			nd_cmd->status = 0x20007;
397bfbaa952SDave Jiang 			dev_dbg(dev, "%s: still verifying\n", __func__);
398bfbaa952SDave Jiang 			break;
399bfbaa952SDave Jiang 		}
400bfbaa952SDave Jiang 
401bfbaa952SDave Jiang 		dev_dbg(dev, "%s: transition out verify\n", __func__);
402bfbaa952SDave Jiang 		fw->state = FW_STATE_UPDATED;
403bfbaa952SDave Jiang 		/* we are going to fall through if it's "done" */
404bfbaa952SDave Jiang 	case FW_STATE_UPDATED:
405bfbaa952SDave Jiang 		nd_cmd->status = 0;
406bfbaa952SDave Jiang 		/* bogus test version */
407bfbaa952SDave Jiang 		fw->version = nd_cmd->updated_fw_rev =
408bfbaa952SDave Jiang 			INTEL_FW_FAKE_VERSION;
409bfbaa952SDave Jiang 		dev_dbg(dev, "%s: updated\n", __func__);
410bfbaa952SDave Jiang 		break;
411bfbaa952SDave Jiang 
412bfbaa952SDave Jiang 	default: /* we should never get here */
413bfbaa952SDave Jiang 		return -EINVAL;
414bfbaa952SDave Jiang 	}
415bfbaa952SDave Jiang 
416bfbaa952SDave Jiang 	return 0;
417bfbaa952SDave Jiang }
418bfbaa952SDave Jiang 
41939c686b8SVishal Verma static int nfit_test_cmd_get_config_size(struct nd_cmd_get_config_size *nd_cmd,
4206bc75619SDan Williams 		unsigned int buf_len)
4216bc75619SDan Williams {
4226bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4236bc75619SDan Williams 		return -EINVAL;
42439c686b8SVishal Verma 
4256bc75619SDan Williams 	nd_cmd->status = 0;
4266bc75619SDan Williams 	nd_cmd->config_size = LABEL_SIZE;
4276bc75619SDan Williams 	nd_cmd->max_xfer = SZ_4K;
42839c686b8SVishal Verma 
42939c686b8SVishal Verma 	return 0;
4306bc75619SDan Williams }
43139c686b8SVishal Verma 
43239c686b8SVishal Verma static int nfit_test_cmd_get_config_data(struct nd_cmd_get_config_data_hdr
43339c686b8SVishal Verma 		*nd_cmd, unsigned int buf_len, void *label)
43439c686b8SVishal Verma {
4356bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
43639c686b8SVishal Verma 	int rc;
4376bc75619SDan Williams 
4386bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4396bc75619SDan Williams 		return -EINVAL;
4406bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4416bc75619SDan Williams 		return -EINVAL;
4426bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) > buf_len)
4436bc75619SDan Williams 		return -EINVAL;
4446bc75619SDan Williams 
4456bc75619SDan Williams 	nd_cmd->status = 0;
4466bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
44739c686b8SVishal Verma 	memcpy(nd_cmd->out_buf, label + offset, len);
4486bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - len;
44939c686b8SVishal Verma 
45039c686b8SVishal Verma 	return rc;
4516bc75619SDan Williams }
45239c686b8SVishal Verma 
45339c686b8SVishal Verma static int nfit_test_cmd_set_config_data(struct nd_cmd_set_config_hdr *nd_cmd,
45439c686b8SVishal Verma 		unsigned int buf_len, void *label)
45539c686b8SVishal Verma {
4566bc75619SDan Williams 	unsigned int len, offset = nd_cmd->in_offset;
4576bc75619SDan Williams 	u32 *status;
45839c686b8SVishal Verma 	int rc;
4596bc75619SDan Williams 
4606bc75619SDan Williams 	if (buf_len < sizeof(*nd_cmd))
4616bc75619SDan Williams 		return -EINVAL;
4626bc75619SDan Williams 	if (offset >= LABEL_SIZE)
4636bc75619SDan Williams 		return -EINVAL;
4646bc75619SDan Williams 	if (nd_cmd->in_length + sizeof(*nd_cmd) + 4 > buf_len)
4656bc75619SDan Williams 		return -EINVAL;
4666bc75619SDan Williams 
46739c686b8SVishal Verma 	status = (void *)nd_cmd + nd_cmd->in_length + sizeof(*nd_cmd);
4686bc75619SDan Williams 	*status = 0;
4696bc75619SDan Williams 	len = min(nd_cmd->in_length, LABEL_SIZE - offset);
47039c686b8SVishal Verma 	memcpy(label + offset, nd_cmd->in_buf, len);
4716bc75619SDan Williams 	rc = buf_len - sizeof(*nd_cmd) - (len + 4);
47239c686b8SVishal Verma 
47339c686b8SVishal Verma 	return rc;
4746bc75619SDan Williams }
47539c686b8SVishal Verma 
476d4f32367SDan Williams #define NFIT_TEST_CLEAR_ERR_UNIT 256
477747ffe11SDan Williams 
47839c686b8SVishal Verma static int nfit_test_cmd_ars_cap(struct nd_cmd_ars_cap *nd_cmd,
47939c686b8SVishal Verma 		unsigned int buf_len)
48039c686b8SVishal Verma {
4819fb1a190SDave Jiang 	int ars_recs;
4829fb1a190SDave Jiang 
48339c686b8SVishal Verma 	if (buf_len < sizeof(*nd_cmd))
48439c686b8SVishal Verma 		return -EINVAL;
48539c686b8SVishal Verma 
4869fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
4879fb1a190SDave Jiang 	ars_recs = SZ_4K / sizeof(struct nd_ars_record);
4889fb1a190SDave Jiang 
489747ffe11SDan Williams 	nd_cmd->max_ars_out = sizeof(struct nd_cmd_ars_status)
4909fb1a190SDave Jiang 		+ ars_recs * sizeof(struct nd_ars_record);
49139c686b8SVishal Verma 	nd_cmd->status = (ND_ARS_PERSISTENT | ND_ARS_VOLATILE) << 16;
492d4f32367SDan Williams 	nd_cmd->clear_err_unit = NFIT_TEST_CLEAR_ERR_UNIT;
49339c686b8SVishal Verma 
49439c686b8SVishal Verma 	return 0;
49539c686b8SVishal Verma }
49639c686b8SVishal Verma 
4979fb1a190SDave Jiang static void post_ars_status(struct ars_state *ars_state,
4989fb1a190SDave Jiang 		struct badrange *badrange, u64 addr, u64 len)
49939c686b8SVishal Verma {
500f471f1a7SDan Williams 	struct nd_cmd_ars_status *ars_status;
501f471f1a7SDan Williams 	struct nd_ars_record *ars_record;
5029fb1a190SDave Jiang 	struct badrange_entry *be;
5039fb1a190SDave Jiang 	u64 end = addr + len - 1;
5049fb1a190SDave Jiang 	int i = 0;
505f471f1a7SDan Williams 
506f471f1a7SDan Williams 	ars_state->deadline = jiffies + 1*HZ;
507f471f1a7SDan Williams 	ars_status = ars_state->ars_status;
508f471f1a7SDan Williams 	ars_status->status = 0;
509f471f1a7SDan Williams 	ars_status->address = addr;
510f471f1a7SDan Williams 	ars_status->length = len;
511f471f1a7SDan Williams 	ars_status->type = ND_ARS_PERSISTENT;
5129fb1a190SDave Jiang 
5139fb1a190SDave Jiang 	spin_lock(&badrange->lock);
5149fb1a190SDave Jiang 	list_for_each_entry(be, &badrange->list, list) {
5159fb1a190SDave Jiang 		u64 be_end = be->start + be->length - 1;
5169fb1a190SDave Jiang 		u64 rstart, rend;
5179fb1a190SDave Jiang 
5189fb1a190SDave Jiang 		/* skip entries outside the range */
5199fb1a190SDave Jiang 		if (be_end < addr || be->start > end)
5209fb1a190SDave Jiang 			continue;
5219fb1a190SDave Jiang 
5229fb1a190SDave Jiang 		rstart = (be->start < addr) ? addr : be->start;
5239fb1a190SDave Jiang 		rend = (be_end < end) ? be_end : end;
5249fb1a190SDave Jiang 		ars_record = &ars_status->records[i];
525f471f1a7SDan Williams 		ars_record->handle = 0;
5269fb1a190SDave Jiang 		ars_record->err_address = rstart;
5279fb1a190SDave Jiang 		ars_record->length = rend - rstart + 1;
5289fb1a190SDave Jiang 		i++;
5299fb1a190SDave Jiang 	}
5309fb1a190SDave Jiang 	spin_unlock(&badrange->lock);
5319fb1a190SDave Jiang 	ars_status->num_records = i;
5329fb1a190SDave Jiang 	ars_status->out_length = sizeof(struct nd_cmd_ars_status)
5339fb1a190SDave Jiang 		+ i * sizeof(struct nd_ars_record);
534f471f1a7SDan Williams }
535f471f1a7SDan Williams 
5369fb1a190SDave Jiang static int nfit_test_cmd_ars_start(struct nfit_test *t,
5379fb1a190SDave Jiang 		struct ars_state *ars_state,
538f471f1a7SDan Williams 		struct nd_cmd_ars_start *ars_start, unsigned int buf_len,
539f471f1a7SDan Williams 		int *cmd_rc)
540f471f1a7SDan Williams {
541f471f1a7SDan Williams 	if (buf_len < sizeof(*ars_start))
54239c686b8SVishal Verma 		return -EINVAL;
54339c686b8SVishal Verma 
544f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
545f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
546f471f1a7SDan Williams 		ars_start->status = NFIT_ARS_START_BUSY;
547f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
548f471f1a7SDan Williams 	} else {
549f471f1a7SDan Williams 		ars_start->status = 0;
550f471f1a7SDan Williams 		ars_start->scrub_time = 1;
5519fb1a190SDave Jiang 		post_ars_status(ars_state, &t->badrange, ars_start->address,
552f471f1a7SDan Williams 				ars_start->length);
553f471f1a7SDan Williams 		*cmd_rc = 0;
554f471f1a7SDan Williams 	}
555f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
55639c686b8SVishal Verma 
55739c686b8SVishal Verma 	return 0;
55839c686b8SVishal Verma }
55939c686b8SVishal Verma 
560f471f1a7SDan Williams static int nfit_test_cmd_ars_status(struct ars_state *ars_state,
561f471f1a7SDan Williams 		struct nd_cmd_ars_status *ars_status, unsigned int buf_len,
562f471f1a7SDan Williams 		int *cmd_rc)
56339c686b8SVishal Verma {
564f471f1a7SDan Williams 	if (buf_len < ars_state->ars_status->out_length)
56539c686b8SVishal Verma 		return -EINVAL;
56639c686b8SVishal Verma 
567f471f1a7SDan Williams 	spin_lock(&ars_state->lock);
568f471f1a7SDan Williams 	if (time_before(jiffies, ars_state->deadline)) {
569f471f1a7SDan Williams 		memset(ars_status, 0, buf_len);
570f471f1a7SDan Williams 		ars_status->status = NFIT_ARS_STATUS_BUSY;
571f471f1a7SDan Williams 		ars_status->out_length = sizeof(*ars_status);
572f471f1a7SDan Williams 		*cmd_rc = -EBUSY;
573f471f1a7SDan Williams 	} else {
574f471f1a7SDan Williams 		memcpy(ars_status, ars_state->ars_status,
575f471f1a7SDan Williams 				ars_state->ars_status->out_length);
576f471f1a7SDan Williams 		*cmd_rc = 0;
577f471f1a7SDan Williams 	}
578f471f1a7SDan Williams 	spin_unlock(&ars_state->lock);
57939c686b8SVishal Verma 	return 0;
58039c686b8SVishal Verma }
58139c686b8SVishal Verma 
5825e096ef3SVishal Verma static int nfit_test_cmd_clear_error(struct nfit_test *t,
5835e096ef3SVishal Verma 		struct nd_cmd_clear_error *clear_err,
584d4f32367SDan Williams 		unsigned int buf_len, int *cmd_rc)
585d4f32367SDan Williams {
586d4f32367SDan Williams 	const u64 mask = NFIT_TEST_CLEAR_ERR_UNIT - 1;
587d4f32367SDan Williams 	if (buf_len < sizeof(*clear_err))
588d4f32367SDan Williams 		return -EINVAL;
589d4f32367SDan Williams 
590d4f32367SDan Williams 	if ((clear_err->address & mask) || (clear_err->length & mask))
591d4f32367SDan Williams 		return -EINVAL;
592d4f32367SDan Williams 
5935e096ef3SVishal Verma 	badrange_forget(&t->badrange, clear_err->address, clear_err->length);
594d4f32367SDan Williams 	clear_err->status = 0;
595d4f32367SDan Williams 	clear_err->cleared = clear_err->length;
596d4f32367SDan Williams 	*cmd_rc = 0;
597d4f32367SDan Williams 	return 0;
598d4f32367SDan Williams }
599d4f32367SDan Williams 
60010246dc8SYasunori Goto struct region_search_spa {
60110246dc8SYasunori Goto 	u64 addr;
60210246dc8SYasunori Goto 	struct nd_region *region;
60310246dc8SYasunori Goto };
60410246dc8SYasunori Goto 
60510246dc8SYasunori Goto static int is_region_device(struct device *dev)
60610246dc8SYasunori Goto {
60710246dc8SYasunori Goto 	return !strncmp(dev->kobj.name, "region", 6);
60810246dc8SYasunori Goto }
60910246dc8SYasunori Goto 
61010246dc8SYasunori Goto static int nfit_test_search_region_spa(struct device *dev, void *data)
61110246dc8SYasunori Goto {
61210246dc8SYasunori Goto 	struct region_search_spa *ctx = data;
61310246dc8SYasunori Goto 	struct nd_region *nd_region;
61410246dc8SYasunori Goto 	resource_size_t ndr_end;
61510246dc8SYasunori Goto 
61610246dc8SYasunori Goto 	if (!is_region_device(dev))
61710246dc8SYasunori Goto 		return 0;
61810246dc8SYasunori Goto 
61910246dc8SYasunori Goto 	nd_region = to_nd_region(dev);
62010246dc8SYasunori Goto 	ndr_end = nd_region->ndr_start + nd_region->ndr_size;
62110246dc8SYasunori Goto 
62210246dc8SYasunori Goto 	if (ctx->addr >= nd_region->ndr_start && ctx->addr < ndr_end) {
62310246dc8SYasunori Goto 		ctx->region = nd_region;
62410246dc8SYasunori Goto 		return 1;
62510246dc8SYasunori Goto 	}
62610246dc8SYasunori Goto 
62710246dc8SYasunori Goto 	return 0;
62810246dc8SYasunori Goto }
62910246dc8SYasunori Goto 
63010246dc8SYasunori Goto static int nfit_test_search_spa(struct nvdimm_bus *bus,
63110246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa)
63210246dc8SYasunori Goto {
63310246dc8SYasunori Goto 	int ret;
63410246dc8SYasunori Goto 	struct nd_region *nd_region = NULL;
63510246dc8SYasunori Goto 	struct nvdimm *nvdimm = NULL;
63610246dc8SYasunori Goto 	struct nd_mapping *nd_mapping = NULL;
63710246dc8SYasunori Goto 	struct region_search_spa ctx = {
63810246dc8SYasunori Goto 		.addr = spa->spa,
63910246dc8SYasunori Goto 		.region = NULL,
64010246dc8SYasunori Goto 	};
64110246dc8SYasunori Goto 	u64 dpa;
64210246dc8SYasunori Goto 
64310246dc8SYasunori Goto 	ret = device_for_each_child(&bus->dev, &ctx,
64410246dc8SYasunori Goto 				nfit_test_search_region_spa);
64510246dc8SYasunori Goto 
64610246dc8SYasunori Goto 	if (!ret)
64710246dc8SYasunori Goto 		return -ENODEV;
64810246dc8SYasunori Goto 
64910246dc8SYasunori Goto 	nd_region = ctx.region;
65010246dc8SYasunori Goto 
65110246dc8SYasunori Goto 	dpa = ctx.addr - nd_region->ndr_start;
65210246dc8SYasunori Goto 
65310246dc8SYasunori Goto 	/*
65410246dc8SYasunori Goto 	 * last dimm is selected for test
65510246dc8SYasunori Goto 	 */
65610246dc8SYasunori Goto 	nd_mapping = &nd_region->mapping[nd_region->ndr_mappings - 1];
65710246dc8SYasunori Goto 	nvdimm = nd_mapping->nvdimm;
65810246dc8SYasunori Goto 
65910246dc8SYasunori Goto 	spa->devices[0].nfit_device_handle = handle[nvdimm->id];
66010246dc8SYasunori Goto 	spa->num_nvdimms = 1;
66110246dc8SYasunori Goto 	spa->devices[0].dpa = dpa;
66210246dc8SYasunori Goto 
66310246dc8SYasunori Goto 	return 0;
66410246dc8SYasunori Goto }
66510246dc8SYasunori Goto 
66610246dc8SYasunori Goto static int nfit_test_cmd_translate_spa(struct nvdimm_bus *bus,
66710246dc8SYasunori Goto 		struct nd_cmd_translate_spa *spa, unsigned int buf_len)
66810246dc8SYasunori Goto {
66910246dc8SYasunori Goto 	if (buf_len < spa->translate_length)
67010246dc8SYasunori Goto 		return -EINVAL;
67110246dc8SYasunori Goto 
67210246dc8SYasunori Goto 	if (nfit_test_search_spa(bus, spa) < 0 || !spa->num_nvdimms)
67310246dc8SYasunori Goto 		spa->status = 2;
67410246dc8SYasunori Goto 
67510246dc8SYasunori Goto 	return 0;
67610246dc8SYasunori Goto }
67710246dc8SYasunori Goto 
678ed07c433SDan Williams static int nfit_test_cmd_smart(struct nd_intel_smart *smart, unsigned int buf_len,
679ed07c433SDan Williams 		struct nd_intel_smart *smart_data)
680baa51277SDan Williams {
681baa51277SDan Williams 	if (buf_len < sizeof(*smart))
682baa51277SDan Williams 		return -EINVAL;
683ed07c433SDan Williams 	memcpy(smart, smart_data, sizeof(*smart));
684baa51277SDan Williams 	return 0;
685baa51277SDan Williams }
686baa51277SDan Williams 
687cdd77d3eSDan Williams static int nfit_test_cmd_smart_threshold(
688ed07c433SDan Williams 		struct nd_intel_smart_threshold *out,
689ed07c433SDan Williams 		unsigned int buf_len,
690ed07c433SDan Williams 		struct nd_intel_smart_threshold *smart_t)
691baa51277SDan Williams {
692baa51277SDan Williams 	if (buf_len < sizeof(*smart_t))
693baa51277SDan Williams 		return -EINVAL;
694ed07c433SDan Williams 	memcpy(out, smart_t, sizeof(*smart_t));
695ed07c433SDan Williams 	return 0;
696ed07c433SDan Williams }
697ed07c433SDan Williams 
698ed07c433SDan Williams static void smart_notify(struct device *bus_dev,
699ed07c433SDan Williams 		struct device *dimm_dev, struct nd_intel_smart *smart,
700ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh)
701ed07c433SDan Williams {
702ed07c433SDan Williams 	dev_dbg(dimm_dev, "%s: alarm: %#x spares: %d (%d) mtemp: %d (%d) ctemp: %d (%d)\n",
703ed07c433SDan Williams 			__func__, thresh->alarm_control, thresh->spares,
704ed07c433SDan Williams 			smart->spares, thresh->media_temperature,
705ed07c433SDan Williams 			smart->media_temperature, thresh->ctrl_temperature,
706ed07c433SDan Williams 			smart->ctrl_temperature);
707ed07c433SDan Williams 	if (((thresh->alarm_control & ND_INTEL_SMART_SPARE_TRIP)
708ed07c433SDan Williams 				&& smart->spares
709ed07c433SDan Williams 				<= thresh->spares)
710ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_TEMP_TRIP)
711ed07c433SDan Williams 				&& smart->media_temperature
712ed07c433SDan Williams 				>= thresh->media_temperature)
713ed07c433SDan Williams 			|| ((thresh->alarm_control & ND_INTEL_SMART_CTEMP_TRIP)
714ed07c433SDan Williams 				&& smart->ctrl_temperature
7154cf260fcSVishal Verma 				>= thresh->ctrl_temperature)
7164cf260fcSVishal Verma 			|| (smart->health != ND_INTEL_SMART_NON_CRITICAL_HEALTH)
7174cf260fcSVishal Verma 			|| (smart->shutdown_state != 0)) {
718ed07c433SDan Williams 		device_lock(bus_dev);
719ed07c433SDan Williams 		__acpi_nvdimm_notify(dimm_dev, 0x81);
720ed07c433SDan Williams 		device_unlock(bus_dev);
721ed07c433SDan Williams 	}
722ed07c433SDan Williams }
723ed07c433SDan Williams 
724ed07c433SDan Williams static int nfit_test_cmd_smart_set_threshold(
725ed07c433SDan Williams 		struct nd_intel_smart_set_threshold *in,
726ed07c433SDan Williams 		unsigned int buf_len,
727ed07c433SDan Williams 		struct nd_intel_smart_threshold *thresh,
728ed07c433SDan Williams 		struct nd_intel_smart *smart,
729ed07c433SDan Williams 		struct device *bus_dev, struct device *dimm_dev)
730ed07c433SDan Williams {
731ed07c433SDan Williams 	unsigned int size;
732ed07c433SDan Williams 
733ed07c433SDan Williams 	size = sizeof(*in) - 4;
734ed07c433SDan Williams 	if (buf_len < size)
735ed07c433SDan Williams 		return -EINVAL;
736ed07c433SDan Williams 	memcpy(thresh->data, in, size);
737ed07c433SDan Williams 	in->status = 0;
738ed07c433SDan Williams 	smart_notify(bus_dev, dimm_dev, smart, thresh);
739ed07c433SDan Williams 
740baa51277SDan Williams 	return 0;
741baa51277SDan Williams }
742baa51277SDan Williams 
7434cf260fcSVishal Verma static int nfit_test_cmd_smart_inject(
7444cf260fcSVishal Verma 		struct nd_intel_smart_inject *inj,
7454cf260fcSVishal Verma 		unsigned int buf_len,
7464cf260fcSVishal Verma 		struct nd_intel_smart_threshold *thresh,
7474cf260fcSVishal Verma 		struct nd_intel_smart *smart,
7484cf260fcSVishal Verma 		struct device *bus_dev, struct device *dimm_dev)
7494cf260fcSVishal Verma {
7504cf260fcSVishal Verma 	if (buf_len != sizeof(*inj))
7514cf260fcSVishal Verma 		return -EINVAL;
7524cf260fcSVishal Verma 
7534cf260fcSVishal Verma 	if (inj->mtemp_enable)
7544cf260fcSVishal Verma 		smart->media_temperature = inj->media_temperature;
7554cf260fcSVishal Verma 	if (inj->spare_enable)
7564cf260fcSVishal Verma 		smart->spares = inj->spares;
7574cf260fcSVishal Verma 	if (inj->fatal_enable)
7584cf260fcSVishal Verma 		smart->health = ND_INTEL_SMART_FATAL_HEALTH;
7594cf260fcSVishal Verma 	if (inj->unsafe_shutdown_enable) {
7604cf260fcSVishal Verma 		smart->shutdown_state = 1;
7614cf260fcSVishal Verma 		smart->shutdown_count++;
7624cf260fcSVishal Verma 	}
7634cf260fcSVishal Verma 	inj->status = 0;
7644cf260fcSVishal Verma 	smart_notify(bus_dev, dimm_dev, smart, thresh);
7654cf260fcSVishal Verma 
7664cf260fcSVishal Verma 	return 0;
7674cf260fcSVishal Verma }
7684cf260fcSVishal Verma 
7699fb1a190SDave Jiang static void uc_error_notify(struct work_struct *work)
7709fb1a190SDave Jiang {
7719fb1a190SDave Jiang 	struct nfit_test *t = container_of(work, typeof(*t), work);
7729fb1a190SDave Jiang 
7739fb1a190SDave Jiang 	__acpi_nfit_notify(&t->pdev.dev, t, NFIT_NOTIFY_UC_MEMORY_ERROR);
7749fb1a190SDave Jiang }
7759fb1a190SDave Jiang 
7769fb1a190SDave Jiang static int nfit_test_cmd_ars_error_inject(struct nfit_test *t,
7779fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj *err_inj, unsigned int buf_len)
7789fb1a190SDave Jiang {
7799fb1a190SDave Jiang 	int rc;
7809fb1a190SDave Jiang 
78141cb3301SVishal Verma 	if (buf_len != sizeof(*err_inj)) {
7829fb1a190SDave Jiang 		rc = -EINVAL;
7839fb1a190SDave Jiang 		goto err;
7849fb1a190SDave Jiang 	}
7859fb1a190SDave Jiang 
7869fb1a190SDave Jiang 	if (err_inj->err_inj_spa_range_length <= 0) {
7879fb1a190SDave Jiang 		rc = -EINVAL;
7889fb1a190SDave Jiang 		goto err;
7899fb1a190SDave Jiang 	}
7909fb1a190SDave Jiang 
7919fb1a190SDave Jiang 	rc =  badrange_add(&t->badrange, err_inj->err_inj_spa_range_base,
7929fb1a190SDave Jiang 			err_inj->err_inj_spa_range_length);
7939fb1a190SDave Jiang 	if (rc < 0)
7949fb1a190SDave Jiang 		goto err;
7959fb1a190SDave Jiang 
7969fb1a190SDave Jiang 	if (err_inj->err_inj_options & (1 << ND_ARS_ERR_INJ_OPT_NOTIFY))
7979fb1a190SDave Jiang 		queue_work(nfit_wq, &t->work);
7989fb1a190SDave Jiang 
7999fb1a190SDave Jiang 	err_inj->status = 0;
8009fb1a190SDave Jiang 	return 0;
8019fb1a190SDave Jiang 
8029fb1a190SDave Jiang err:
8039fb1a190SDave Jiang 	err_inj->status = NFIT_ARS_INJECT_INVALID;
8049fb1a190SDave Jiang 	return rc;
8059fb1a190SDave Jiang }
8069fb1a190SDave Jiang 
8079fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_clear(struct nfit_test *t,
8089fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_clr *err_clr, unsigned int buf_len)
8099fb1a190SDave Jiang {
8109fb1a190SDave Jiang 	int rc;
8119fb1a190SDave Jiang 
81241cb3301SVishal Verma 	if (buf_len != sizeof(*err_clr)) {
8139fb1a190SDave Jiang 		rc = -EINVAL;
8149fb1a190SDave Jiang 		goto err;
8159fb1a190SDave Jiang 	}
8169fb1a190SDave Jiang 
8179fb1a190SDave Jiang 	if (err_clr->err_inj_clr_spa_range_length <= 0) {
8189fb1a190SDave Jiang 		rc = -EINVAL;
8199fb1a190SDave Jiang 		goto err;
8209fb1a190SDave Jiang 	}
8219fb1a190SDave Jiang 
8229fb1a190SDave Jiang 	badrange_forget(&t->badrange, err_clr->err_inj_clr_spa_range_base,
8239fb1a190SDave Jiang 			err_clr->err_inj_clr_spa_range_length);
8249fb1a190SDave Jiang 
8259fb1a190SDave Jiang 	err_clr->status = 0;
8269fb1a190SDave Jiang 	return 0;
8279fb1a190SDave Jiang 
8289fb1a190SDave Jiang err:
8299fb1a190SDave Jiang 	err_clr->status = NFIT_ARS_INJECT_INVALID;
8309fb1a190SDave Jiang 	return rc;
8319fb1a190SDave Jiang }
8329fb1a190SDave Jiang 
8339fb1a190SDave Jiang static int nfit_test_cmd_ars_inject_status(struct nfit_test *t,
8349fb1a190SDave Jiang 		struct nd_cmd_ars_err_inj_stat *err_stat,
8359fb1a190SDave Jiang 		unsigned int buf_len)
8369fb1a190SDave Jiang {
8379fb1a190SDave Jiang 	struct badrange_entry *be;
8389fb1a190SDave Jiang 	int max = SZ_4K / sizeof(struct nd_error_stat_query_record);
8399fb1a190SDave Jiang 	int i = 0;
8409fb1a190SDave Jiang 
8419fb1a190SDave Jiang 	err_stat->status = 0;
8429fb1a190SDave Jiang 	spin_lock(&t->badrange.lock);
8439fb1a190SDave Jiang 	list_for_each_entry(be, &t->badrange.list, list) {
8449fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_base = be->start;
8459fb1a190SDave Jiang 		err_stat->record[i].err_inj_stat_spa_range_length = be->length;
8469fb1a190SDave Jiang 		i++;
8479fb1a190SDave Jiang 		if (i > max)
8489fb1a190SDave Jiang 			break;
8499fb1a190SDave Jiang 	}
8509fb1a190SDave Jiang 	spin_unlock(&t->badrange.lock);
8519fb1a190SDave Jiang 	err_stat->inj_err_rec_count = i;
8529fb1a190SDave Jiang 
8539fb1a190SDave Jiang 	return 0;
8549fb1a190SDave Jiang }
8559fb1a190SDave Jiang 
856674d8bdeSDave Jiang static int nd_intel_test_cmd_set_lss_status(struct nfit_test *t,
857674d8bdeSDave Jiang 		struct nd_intel_lss *nd_cmd, unsigned int buf_len)
858674d8bdeSDave Jiang {
859674d8bdeSDave Jiang 	struct device *dev = &t->pdev.dev;
860674d8bdeSDave Jiang 
861674d8bdeSDave Jiang 	if (buf_len < sizeof(*nd_cmd))
862674d8bdeSDave Jiang 		return -EINVAL;
863674d8bdeSDave Jiang 
864674d8bdeSDave Jiang 	switch (nd_cmd->enable) {
865674d8bdeSDave Jiang 	case 0:
866674d8bdeSDave Jiang 		nd_cmd->status = 0;
867674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status disabled\n",
868674d8bdeSDave Jiang 				__func__);
869674d8bdeSDave Jiang 		break;
870674d8bdeSDave Jiang 	case 1:
871674d8bdeSDave Jiang 		nd_cmd->status = 0;
872674d8bdeSDave Jiang 		dev_dbg(dev, "%s: Latch System Shutdown Status enabled\n",
873674d8bdeSDave Jiang 				__func__);
874674d8bdeSDave Jiang 		break;
875674d8bdeSDave Jiang 	default:
876674d8bdeSDave Jiang 		dev_warn(dev, "Unknown enable value: %#x\n", nd_cmd->enable);
877674d8bdeSDave Jiang 		nd_cmd->status = 0x3;
878674d8bdeSDave Jiang 		break;
879674d8bdeSDave Jiang 	}
880674d8bdeSDave Jiang 
881674d8bdeSDave Jiang 
882674d8bdeSDave Jiang 	return 0;
883674d8bdeSDave Jiang }
884674d8bdeSDave Jiang 
885bfbaa952SDave Jiang static int get_dimm(struct nfit_mem *nfit_mem, unsigned int func)
886bfbaa952SDave Jiang {
887bfbaa952SDave Jiang 	int i;
888bfbaa952SDave Jiang 
889bfbaa952SDave Jiang 	/* lookup per-dimm data */
890bfbaa952SDave Jiang 	for (i = 0; i < ARRAY_SIZE(handle); i++)
891bfbaa952SDave Jiang 		if (__to_nfit_memdev(nfit_mem)->device_handle == handle[i])
892bfbaa952SDave Jiang 			break;
893bfbaa952SDave Jiang 	if (i >= ARRAY_SIZE(handle))
894bfbaa952SDave Jiang 		return -ENXIO;
895bfbaa952SDave Jiang 
896*55c72ab6SDan Williams 	if ((1 << func) & dimm_fail_cmd_flags[i]) {
897*55c72ab6SDan Williams 		if (dimm_fail_cmd_code[i])
898*55c72ab6SDan Williams 			return dimm_fail_cmd_code[i];
899bfbaa952SDave Jiang 		return -EIO;
900*55c72ab6SDan Williams 	}
901bfbaa952SDave Jiang 
902bfbaa952SDave Jiang 	return i;
903bfbaa952SDave Jiang }
904bfbaa952SDave Jiang 
90539c686b8SVishal Verma static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
90639c686b8SVishal Verma 		struct nvdimm *nvdimm, unsigned int cmd, void *buf,
907aef25338SDan Williams 		unsigned int buf_len, int *cmd_rc)
90839c686b8SVishal Verma {
90939c686b8SVishal Verma 	struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
91039c686b8SVishal Verma 	struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
9116634fb06SDan Williams 	unsigned int func = cmd;
912f471f1a7SDan Williams 	int i, rc = 0, __cmd_rc;
913f471f1a7SDan Williams 
914f471f1a7SDan Williams 	if (!cmd_rc)
915f471f1a7SDan Williams 		cmd_rc = &__cmd_rc;
916f471f1a7SDan Williams 	*cmd_rc = 0;
91739c686b8SVishal Verma 
91839c686b8SVishal Verma 	if (nvdimm) {
91939c686b8SVishal Verma 		struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
920e3654ecaSDan Williams 		unsigned long cmd_mask = nvdimm_cmd_mask(nvdimm);
92139c686b8SVishal Verma 
9226634fb06SDan Williams 		if (!nfit_mem)
9236634fb06SDan Williams 			return -ENOTTY;
9246634fb06SDan Williams 
9256634fb06SDan Williams 		if (cmd == ND_CMD_CALL) {
9266634fb06SDan Williams 			struct nd_cmd_pkg *call_pkg = buf;
9276634fb06SDan Williams 
9286634fb06SDan Williams 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
9296634fb06SDan Williams 			buf = (void *) call_pkg->nd_payload;
9306634fb06SDan Williams 			func = call_pkg->nd_command;
9316634fb06SDan Williams 			if (call_pkg->nd_family != nfit_mem->family)
9326634fb06SDan Williams 				return -ENOTTY;
933bfbaa952SDave Jiang 
934bfbaa952SDave Jiang 			i = get_dimm(nfit_mem, func);
935bfbaa952SDave Jiang 			if (i < 0)
936bfbaa952SDave Jiang 				return i;
937bfbaa952SDave Jiang 
938bfbaa952SDave Jiang 			switch (func) {
939674d8bdeSDave Jiang 			case ND_INTEL_ENABLE_LSS_STATUS:
940674d8bdeSDave Jiang 				return nd_intel_test_cmd_set_lss_status(t,
941674d8bdeSDave Jiang 						buf, buf_len);
942bfbaa952SDave Jiang 			case ND_INTEL_FW_GET_INFO:
943bfbaa952SDave Jiang 				return nd_intel_test_get_fw_info(t, buf,
944bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
945bfbaa952SDave Jiang 			case ND_INTEL_FW_START_UPDATE:
946bfbaa952SDave Jiang 				return nd_intel_test_start_update(t, buf,
947bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
948bfbaa952SDave Jiang 			case ND_INTEL_FW_SEND_DATA:
949bfbaa952SDave Jiang 				return nd_intel_test_send_data(t, buf,
950bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
951bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_UPDATE:
952bfbaa952SDave Jiang 				return nd_intel_test_finish_fw(t, buf,
953bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
954bfbaa952SDave Jiang 			case ND_INTEL_FW_FINISH_QUERY:
955bfbaa952SDave Jiang 				return nd_intel_test_finish_query(t, buf,
956bfbaa952SDave Jiang 						buf_len, i - t->dcr_idx);
957bfbaa952SDave Jiang 			case ND_INTEL_SMART:
958bfbaa952SDave Jiang 				return nfit_test_cmd_smart(buf, buf_len,
959bfbaa952SDave Jiang 						&t->smart[i - t->dcr_idx]);
960bfbaa952SDave Jiang 			case ND_INTEL_SMART_THRESHOLD:
961bfbaa952SDave Jiang 				return nfit_test_cmd_smart_threshold(buf,
962bfbaa952SDave Jiang 						buf_len,
963bfbaa952SDave Jiang 						&t->smart_threshold[i -
964bfbaa952SDave Jiang 							t->dcr_idx]);
965bfbaa952SDave Jiang 			case ND_INTEL_SMART_SET_THRESHOLD:
966bfbaa952SDave Jiang 				return nfit_test_cmd_smart_set_threshold(buf,
967bfbaa952SDave Jiang 						buf_len,
968bfbaa952SDave Jiang 						&t->smart_threshold[i -
969bfbaa952SDave Jiang 							t->dcr_idx],
970bfbaa952SDave Jiang 						&t->smart[i - t->dcr_idx],
971bfbaa952SDave Jiang 						&t->pdev.dev, t->dimm_dev[i]);
9724cf260fcSVishal Verma 			case ND_INTEL_SMART_INJECT:
9734cf260fcSVishal Verma 				return nfit_test_cmd_smart_inject(buf,
9744cf260fcSVishal Verma 						buf_len,
9754cf260fcSVishal Verma 						&t->smart_threshold[i -
9764cf260fcSVishal Verma 							t->dcr_idx],
9774cf260fcSVishal Verma 						&t->smart[i - t->dcr_idx],
9784cf260fcSVishal Verma 						&t->pdev.dev, t->dimm_dev[i]);
979bfbaa952SDave Jiang 			default:
980bfbaa952SDave Jiang 				return -ENOTTY;
981bfbaa952SDave Jiang 			}
9826634fb06SDan Williams 		}
9836634fb06SDan Williams 
9846634fb06SDan Williams 		if (!test_bit(cmd, &cmd_mask)
9856634fb06SDan Williams 				|| !test_bit(func, &nfit_mem->dsm_mask))
98639c686b8SVishal Verma 			return -ENOTTY;
98739c686b8SVishal Verma 
988bfbaa952SDave Jiang 		i = get_dimm(nfit_mem, func);
989bfbaa952SDave Jiang 		if (i < 0)
990bfbaa952SDave Jiang 			return i;
99173606afdSDan Williams 
9926634fb06SDan Williams 		switch (func) {
99339c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_SIZE:
99439c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_size(buf, buf_len);
99539c686b8SVishal Verma 			break;
99639c686b8SVishal Verma 		case ND_CMD_GET_CONFIG_DATA:
99739c686b8SVishal Verma 			rc = nfit_test_cmd_get_config_data(buf, buf_len,
998dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
99939c686b8SVishal Verma 			break;
100039c686b8SVishal Verma 		case ND_CMD_SET_CONFIG_DATA:
100139c686b8SVishal Verma 			rc = nfit_test_cmd_set_config_data(buf, buf_len,
1002dafb1048SDan Williams 				t->label[i - t->dcr_idx]);
100339c686b8SVishal Verma 			break;
10046bc75619SDan Williams 		default:
10056bc75619SDan Williams 			return -ENOTTY;
10066bc75619SDan Williams 		}
100739c686b8SVishal Verma 	} else {
1008f471f1a7SDan Williams 		struct ars_state *ars_state = &t->ars_state;
100910246dc8SYasunori Goto 		struct nd_cmd_pkg *call_pkg = buf;
101010246dc8SYasunori Goto 
101110246dc8SYasunori Goto 		if (!nd_desc)
101210246dc8SYasunori Goto 			return -ENOTTY;
101310246dc8SYasunori Goto 
101410246dc8SYasunori Goto 		if (cmd == ND_CMD_CALL) {
101510246dc8SYasunori Goto 			func = call_pkg->nd_command;
101610246dc8SYasunori Goto 
101710246dc8SYasunori Goto 			buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
101810246dc8SYasunori Goto 			buf = (void *) call_pkg->nd_payload;
101910246dc8SYasunori Goto 
102010246dc8SYasunori Goto 			switch (func) {
102110246dc8SYasunori Goto 			case NFIT_CMD_TRANSLATE_SPA:
102210246dc8SYasunori Goto 				rc = nfit_test_cmd_translate_spa(
102310246dc8SYasunori Goto 					acpi_desc->nvdimm_bus, buf, buf_len);
102410246dc8SYasunori Goto 				return rc;
10259fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_SET:
10269fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_error_inject(t, buf,
10279fb1a190SDave Jiang 					buf_len);
10289fb1a190SDave Jiang 				return rc;
10299fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_CLEAR:
10309fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_clear(t, buf,
10319fb1a190SDave Jiang 					buf_len);
10329fb1a190SDave Jiang 				return rc;
10339fb1a190SDave Jiang 			case NFIT_CMD_ARS_INJECT_GET:
10349fb1a190SDave Jiang 				rc = nfit_test_cmd_ars_inject_status(t, buf,
10359fb1a190SDave Jiang 					buf_len);
10369fb1a190SDave Jiang 				return rc;
103710246dc8SYasunori Goto 			default:
103810246dc8SYasunori Goto 				return -ENOTTY;
103910246dc8SYasunori Goto 			}
104010246dc8SYasunori Goto 		}
1041f471f1a7SDan Williams 
1042e3654ecaSDan Williams 		if (!nd_desc || !test_bit(cmd, &nd_desc->cmd_mask))
104339c686b8SVishal Verma 			return -ENOTTY;
104439c686b8SVishal Verma 
10456634fb06SDan Williams 		switch (func) {
104639c686b8SVishal Verma 		case ND_CMD_ARS_CAP:
104739c686b8SVishal Verma 			rc = nfit_test_cmd_ars_cap(buf, buf_len);
104839c686b8SVishal Verma 			break;
104939c686b8SVishal Verma 		case ND_CMD_ARS_START:
10509fb1a190SDave Jiang 			rc = nfit_test_cmd_ars_start(t, ars_state, buf,
10519fb1a190SDave Jiang 					buf_len, cmd_rc);
105239c686b8SVishal Verma 			break;
105339c686b8SVishal Verma 		case ND_CMD_ARS_STATUS:
1054f471f1a7SDan Williams 			rc = nfit_test_cmd_ars_status(ars_state, buf, buf_len,
1055f471f1a7SDan Williams 					cmd_rc);
105639c686b8SVishal Verma 			break;
1057d4f32367SDan Williams 		case ND_CMD_CLEAR_ERROR:
10585e096ef3SVishal Verma 			rc = nfit_test_cmd_clear_error(t, buf, buf_len, cmd_rc);
1059d4f32367SDan Williams 			break;
106039c686b8SVishal Verma 		default:
106139c686b8SVishal Verma 			return -ENOTTY;
106239c686b8SVishal Verma 		}
106339c686b8SVishal Verma 	}
10646bc75619SDan Williams 
10656bc75619SDan Williams 	return rc;
10666bc75619SDan Williams }
10676bc75619SDan Williams 
10686bc75619SDan Williams static DEFINE_SPINLOCK(nfit_test_lock);
10696bc75619SDan Williams static struct nfit_test *instances[NUM_NFITS];
10706bc75619SDan Williams 
10716bc75619SDan Williams static void release_nfit_res(void *data)
10726bc75619SDan Williams {
10736bc75619SDan Williams 	struct nfit_test_resource *nfit_res = data;
10746bc75619SDan Williams 
10756bc75619SDan Williams 	spin_lock(&nfit_test_lock);
10766bc75619SDan Williams 	list_del(&nfit_res->list);
10776bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
10786bc75619SDan Williams 
10796bc75619SDan Williams 	vfree(nfit_res->buf);
10806bc75619SDan Williams 	kfree(nfit_res);
10816bc75619SDan Williams }
10826bc75619SDan Williams 
10836bc75619SDan Williams static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
10846bc75619SDan Williams 		void *buf)
10856bc75619SDan Williams {
10866bc75619SDan Williams 	struct device *dev = &t->pdev.dev;
10876bc75619SDan Williams 	struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
10886bc75619SDan Williams 			GFP_KERNEL);
10896bc75619SDan Williams 	int rc;
10906bc75619SDan Williams 
1091bd4cd745SDan Williams 	if (!buf || !nfit_res)
10926bc75619SDan Williams 		goto err;
10936bc75619SDan Williams 	rc = devm_add_action(dev, release_nfit_res, nfit_res);
10946bc75619SDan Williams 	if (rc)
10956bc75619SDan Williams 		goto err;
10966bc75619SDan Williams 	INIT_LIST_HEAD(&nfit_res->list);
10976bc75619SDan Williams 	memset(buf, 0, size);
10986bc75619SDan Williams 	nfit_res->dev = dev;
10996bc75619SDan Williams 	nfit_res->buf = buf;
1100bd4cd745SDan Williams 	nfit_res->res.start = *dma;
1101bd4cd745SDan Williams 	nfit_res->res.end = *dma + size - 1;
1102bd4cd745SDan Williams 	nfit_res->res.name = "NFIT";
1103bd4cd745SDan Williams 	spin_lock_init(&nfit_res->lock);
1104bd4cd745SDan Williams 	INIT_LIST_HEAD(&nfit_res->requests);
11056bc75619SDan Williams 	spin_lock(&nfit_test_lock);
11066bc75619SDan Williams 	list_add(&nfit_res->list, &t->resources);
11076bc75619SDan Williams 	spin_unlock(&nfit_test_lock);
11086bc75619SDan Williams 
11096bc75619SDan Williams 	return nfit_res->buf;
11106bc75619SDan Williams  err:
1111ee8520feSDan Williams 	if (buf)
11126bc75619SDan Williams 		vfree(buf);
11136bc75619SDan Williams 	kfree(nfit_res);
11146bc75619SDan Williams 	return NULL;
11156bc75619SDan Williams }
11166bc75619SDan Williams 
11176bc75619SDan Williams static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
11186bc75619SDan Williams {
11196bc75619SDan Williams 	void *buf = vmalloc(size);
11206bc75619SDan Williams 
11216bc75619SDan Williams 	*dma = (unsigned long) buf;
11226bc75619SDan Williams 	return __test_alloc(t, size, dma, buf);
11236bc75619SDan Williams }
11246bc75619SDan Williams 
11256bc75619SDan Williams static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
11266bc75619SDan Williams {
11276bc75619SDan Williams 	int i;
11286bc75619SDan Williams 
11296bc75619SDan Williams 	for (i = 0; i < ARRAY_SIZE(instances); i++) {
11306bc75619SDan Williams 		struct nfit_test_resource *n, *nfit_res = NULL;
11316bc75619SDan Williams 		struct nfit_test *t = instances[i];
11326bc75619SDan Williams 
11336bc75619SDan Williams 		if (!t)
11346bc75619SDan Williams 			continue;
11356bc75619SDan Williams 		spin_lock(&nfit_test_lock);
11366bc75619SDan Williams 		list_for_each_entry(n, &t->resources, list) {
1137bd4cd745SDan Williams 			if (addr >= n->res.start && (addr < n->res.start
1138bd4cd745SDan Williams 						+ resource_size(&n->res))) {
11396bc75619SDan Williams 				nfit_res = n;
11406bc75619SDan Williams 				break;
11416bc75619SDan Williams 			} else if (addr >= (unsigned long) n->buf
11426bc75619SDan Williams 					&& (addr < (unsigned long) n->buf
1143bd4cd745SDan Williams 						+ resource_size(&n->res))) {
11446bc75619SDan Williams 				nfit_res = n;
11456bc75619SDan Williams 				break;
11466bc75619SDan Williams 			}
11476bc75619SDan Williams 		}
11486bc75619SDan Williams 		spin_unlock(&nfit_test_lock);
11496bc75619SDan Williams 		if (nfit_res)
11506bc75619SDan Williams 			return nfit_res;
11516bc75619SDan Williams 	}
11526bc75619SDan Williams 
11536bc75619SDan Williams 	return NULL;
11546bc75619SDan Williams }
11556bc75619SDan Williams 
1156f471f1a7SDan Williams static int ars_state_init(struct device *dev, struct ars_state *ars_state)
1157f471f1a7SDan Williams {
11589fb1a190SDave Jiang 	/* for testing, only store up to n records that fit within 4k */
1159f471f1a7SDan Williams 	ars_state->ars_status = devm_kzalloc(dev,
11609fb1a190SDave Jiang 			sizeof(struct nd_cmd_ars_status) + SZ_4K, GFP_KERNEL);
1161f471f1a7SDan Williams 	if (!ars_state->ars_status)
1162f471f1a7SDan Williams 		return -ENOMEM;
1163f471f1a7SDan Williams 	spin_lock_init(&ars_state->lock);
1164f471f1a7SDan Williams 	return 0;
1165f471f1a7SDan Williams }
1166f471f1a7SDan Williams 
1167231bf117SDan Williams static void put_dimms(void *data)
1168231bf117SDan Williams {
1169231bf117SDan Williams 	struct device **dimm_dev = data;
1170231bf117SDan Williams 	int i;
1171231bf117SDan Williams 
1172231bf117SDan Williams 	for (i = 0; i < NUM_DCR; i++)
1173231bf117SDan Williams 		if (dimm_dev[i])
1174231bf117SDan Williams 			device_unregister(dimm_dev[i]);
1175231bf117SDan Williams }
1176231bf117SDan Williams 
1177231bf117SDan Williams static struct class *nfit_test_dimm;
1178231bf117SDan Williams 
117973606afdSDan Williams static int dimm_name_to_id(struct device *dev)
118073606afdSDan Williams {
118173606afdSDan Williams 	int dimm;
118273606afdSDan Williams 
118373606afdSDan Williams 	if (sscanf(dev_name(dev), "test_dimm%d", &dimm) != 1
118473606afdSDan Williams 			|| dimm >= NUM_DCR || dimm < 0)
118573606afdSDan Williams 		return -ENXIO;
118673606afdSDan Williams 	return dimm;
118773606afdSDan Williams }
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 
119873606afdSDan Williams 	return sprintf(buf, "%#x", 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 
1232*55c72ab6SDan Williams static ssize_t fail_cmd_code_show(struct device *dev, struct device_attribute *attr,
1233*55c72ab6SDan Williams 		char *buf)
1234*55c72ab6SDan Williams {
1235*55c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
1236*55c72ab6SDan Williams 
1237*55c72ab6SDan Williams 	if (dimm < 0)
1238*55c72ab6SDan Williams 		return dimm;
1239*55c72ab6SDan Williams 
1240*55c72ab6SDan Williams 	return sprintf(buf, "%d\n", dimm_fail_cmd_code[dimm]);
1241*55c72ab6SDan Williams }
1242*55c72ab6SDan Williams 
1243*55c72ab6SDan Williams static ssize_t fail_cmd_code_store(struct device *dev, struct device_attribute *attr,
1244*55c72ab6SDan Williams 		const char *buf, size_t size)
1245*55c72ab6SDan Williams {
1246*55c72ab6SDan Williams 	int dimm = dimm_name_to_id(dev);
1247*55c72ab6SDan Williams 	unsigned long val;
1248*55c72ab6SDan Williams 	ssize_t rc;
1249*55c72ab6SDan Williams 
1250*55c72ab6SDan Williams 	if (dimm < 0)
1251*55c72ab6SDan Williams 		return dimm;
1252*55c72ab6SDan Williams 
1253*55c72ab6SDan Williams 	rc = kstrtol(buf, 0, &val);
1254*55c72ab6SDan Williams 	if (rc)
1255*55c72ab6SDan Williams 		return rc;
1256*55c72ab6SDan Williams 
1257*55c72ab6SDan Williams 	dimm_fail_cmd_code[dimm] = val;
1258*55c72ab6SDan Williams 	return size;
1259*55c72ab6SDan Williams }
1260*55c72ab6SDan Williams static DEVICE_ATTR_RW(fail_cmd_code);
1261*55c72ab6SDan Williams 
1262*55c72ab6SDan Williams 
126373606afdSDan Williams static struct attribute *nfit_test_dimm_attributes[] = {
126473606afdSDan Williams 	&dev_attr_fail_cmd.attr,
1265*55c72ab6SDan Williams 	&dev_attr_fail_cmd_code.attr,
126673606afdSDan Williams 	&dev_attr_handle.attr,
126773606afdSDan Williams 	NULL,
126873606afdSDan Williams };
126973606afdSDan Williams 
127073606afdSDan Williams static struct attribute_group nfit_test_dimm_attribute_group = {
127173606afdSDan Williams 	.attrs = nfit_test_dimm_attributes,
127273606afdSDan Williams };
127373606afdSDan Williams 
127473606afdSDan Williams static const struct attribute_group *nfit_test_dimm_attribute_groups[] = {
127573606afdSDan Williams 	&nfit_test_dimm_attribute_group,
127673606afdSDan Williams 	NULL,
127773606afdSDan Williams };
127873606afdSDan Williams 
1279ed07c433SDan Williams static void smart_init(struct nfit_test *t)
1280ed07c433SDan Williams {
1281ed07c433SDan Williams 	int i;
1282ed07c433SDan Williams 	const struct nd_intel_smart_threshold smart_t_data = {
1283ed07c433SDan Williams 		.alarm_control = ND_INTEL_SMART_SPARE_TRIP
1284ed07c433SDan Williams 			| ND_INTEL_SMART_TEMP_TRIP,
1285ed07c433SDan Williams 		.media_temperature = 40 * 16,
1286ed07c433SDan Williams 		.ctrl_temperature = 30 * 16,
1287ed07c433SDan Williams 		.spares = 5,
1288ed07c433SDan Williams 	};
1289ed07c433SDan Williams 	const struct nd_intel_smart smart_data = {
1290ed07c433SDan Williams 		.flags = ND_INTEL_SMART_HEALTH_VALID
1291ed07c433SDan Williams 			| ND_INTEL_SMART_SPARES_VALID
1292ed07c433SDan Williams 			| ND_INTEL_SMART_ALARM_VALID
1293ed07c433SDan Williams 			| ND_INTEL_SMART_USED_VALID
1294ed07c433SDan Williams 			| ND_INTEL_SMART_SHUTDOWN_VALID
1295ed07c433SDan Williams 			| ND_INTEL_SMART_MTEMP_VALID,
1296ed07c433SDan Williams 		.health = ND_INTEL_SMART_NON_CRITICAL_HEALTH,
1297ed07c433SDan Williams 		.media_temperature = 23 * 16,
1298f6adcca0SVishal Verma 		.ctrl_temperature = 25 * 16,
1299ed07c433SDan Williams 		.pmic_temperature = 40 * 16,
1300ed07c433SDan Williams 		.spares = 75,
1301ed07c433SDan Williams 		.alarm_flags = ND_INTEL_SMART_SPARE_TRIP
1302ed07c433SDan Williams 			| ND_INTEL_SMART_TEMP_TRIP,
1303ed07c433SDan Williams 		.ait_status = 1,
1304ed07c433SDan Williams 		.life_used = 5,
1305ed07c433SDan Williams 		.shutdown_state = 0,
1306ed07c433SDan Williams 		.vendor_size = 0,
1307ed07c433SDan Williams 		.shutdown_count = 100,
1308ed07c433SDan Williams 	};
1309ed07c433SDan Williams 
1310ed07c433SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1311ed07c433SDan Williams 		memcpy(&t->smart[i], &smart_data, sizeof(smart_data));
1312ed07c433SDan Williams 		memcpy(&t->smart_threshold[i], &smart_t_data,
1313ed07c433SDan Williams 				sizeof(smart_t_data));
1314ed07c433SDan Williams 	}
1315ed07c433SDan Williams }
1316ed07c433SDan Williams 
13176bc75619SDan Williams static int nfit_test0_alloc(struct nfit_test *t)
13186bc75619SDan Williams {
13196b577c9dSLinda Knippers 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * NUM_SPA
13206bc75619SDan Williams 			+ sizeof(struct acpi_nfit_memory_map) * NUM_MEM
13216bc75619SDan Williams 			+ sizeof(struct acpi_nfit_control_region) * NUM_DCR
13223b87356fSDan Williams 			+ offsetof(struct acpi_nfit_control_region,
13233b87356fSDan Williams 					window_size) * NUM_DCR
13249d27a87eSDan Williams 			+ sizeof(struct acpi_nfit_data_region) * NUM_BDW
132585d3fa02SDan Williams 			+ (sizeof(struct acpi_nfit_flush_address)
1326f81e1d35SDave Jiang 					+ sizeof(u64) * NUM_HINTS) * NUM_DCR
1327f81e1d35SDave Jiang 			+ sizeof(struct acpi_nfit_capabilities);
13286bc75619SDan Williams 	int i;
13296bc75619SDan Williams 
13306bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
13316bc75619SDan Williams 	if (!t->nfit_buf)
13326bc75619SDan Williams 		return -ENOMEM;
13336bc75619SDan Williams 	t->nfit_size = nfit_size;
13346bc75619SDan Williams 
1335ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[0]);
13366bc75619SDan Williams 	if (!t->spa_set[0])
13376bc75619SDan Williams 		return -ENOMEM;
13386bc75619SDan Williams 
1339ee8520feSDan Williams 	t->spa_set[1] = test_alloc(t, SPA1_SIZE, &t->spa_set_dma[1]);
13406bc75619SDan Williams 	if (!t->spa_set[1])
13416bc75619SDan Williams 		return -ENOMEM;
13426bc75619SDan Williams 
1343ee8520feSDan Williams 	t->spa_set[2] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[2]);
134420985164SVishal Verma 	if (!t->spa_set[2])
134520985164SVishal Verma 		return -ENOMEM;
134620985164SVishal Verma 
1347dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
13486bc75619SDan Williams 		t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
13496bc75619SDan Williams 		if (!t->dimm[i])
13506bc75619SDan Williams 			return -ENOMEM;
13516bc75619SDan Williams 
13526bc75619SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
13536bc75619SDan Williams 		if (!t->label[i])
13546bc75619SDan Williams 			return -ENOMEM;
13556bc75619SDan Williams 		sprintf(t->label[i], "label%d", i);
13569d27a87eSDan Williams 
13579d15ce9cSDan Williams 		t->flush[i] = test_alloc(t, max(PAGE_SIZE,
13589d15ce9cSDan Williams 					sizeof(u64) * NUM_HINTS),
135985d3fa02SDan Williams 				&t->flush_dma[i]);
13609d27a87eSDan Williams 		if (!t->flush[i])
13619d27a87eSDan Williams 			return -ENOMEM;
13626bc75619SDan Williams 	}
13636bc75619SDan Williams 
1364dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
13656bc75619SDan Williams 		t->dcr[i] = test_alloc(t, LABEL_SIZE, &t->dcr_dma[i]);
13666bc75619SDan Williams 		if (!t->dcr[i])
13676bc75619SDan Williams 			return -ENOMEM;
13686bc75619SDan Williams 	}
13696bc75619SDan Williams 
1370c14a868aSDan Williams 	t->_fit = test_alloc(t, sizeof(union acpi_object **), &t->_fit_dma);
1371c14a868aSDan Williams 	if (!t->_fit)
1372c14a868aSDan Williams 		return -ENOMEM;
1373c14a868aSDan Williams 
1374231bf117SDan Williams 	if (devm_add_action_or_reset(&t->pdev.dev, put_dimms, t->dimm_dev))
1375231bf117SDan Williams 		return -ENOMEM;
1376231bf117SDan Williams 	for (i = 0; i < NUM_DCR; i++) {
137773606afdSDan Williams 		t->dimm_dev[i] = device_create_with_groups(nfit_test_dimm,
137873606afdSDan Williams 				&t->pdev.dev, 0, NULL,
137973606afdSDan Williams 				nfit_test_dimm_attribute_groups,
138073606afdSDan Williams 				"test_dimm%d", i);
1381231bf117SDan Williams 		if (!t->dimm_dev[i])
1382231bf117SDan Williams 			return -ENOMEM;
1383231bf117SDan Williams 	}
1384231bf117SDan Williams 
1385ed07c433SDan Williams 	smart_init(t);
1386f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
13876bc75619SDan Williams }
13886bc75619SDan Williams 
13896bc75619SDan Williams static int nfit_test1_alloc(struct nfit_test *t)
13906bc75619SDan Williams {
13917bfe97c7SDan Williams 	size_t nfit_size = sizeof(struct acpi_nfit_system_address) * 2
1392ac40b675SDan Williams 		+ sizeof(struct acpi_nfit_memory_map) * 2
1393ac40b675SDan Williams 		+ offsetof(struct acpi_nfit_control_region, window_size) * 2;
1394dafb1048SDan Williams 	int i;
13956bc75619SDan Williams 
13966bc75619SDan Williams 	t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
13976bc75619SDan Williams 	if (!t->nfit_buf)
13986bc75619SDan Williams 		return -ENOMEM;
13996bc75619SDan Williams 	t->nfit_size = nfit_size;
14006bc75619SDan Williams 
1401ee8520feSDan Williams 	t->spa_set[0] = test_alloc(t, SPA2_SIZE, &t->spa_set_dma[0]);
14026bc75619SDan Williams 	if (!t->spa_set[0])
14036bc75619SDan Williams 		return -ENOMEM;
14046bc75619SDan Williams 
1405dafb1048SDan Williams 	for (i = 0; i < t->num_dcr; i++) {
1406dafb1048SDan Williams 		t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
1407dafb1048SDan Williams 		if (!t->label[i])
1408dafb1048SDan Williams 			return -ENOMEM;
1409dafb1048SDan Williams 		sprintf(t->label[i], "label%d", i);
1410dafb1048SDan Williams 	}
1411dafb1048SDan Williams 
14127bfe97c7SDan Williams 	t->spa_set[1] = test_alloc(t, SPA_VCD_SIZE, &t->spa_set_dma[1]);
14137bfe97c7SDan Williams 	if (!t->spa_set[1])
14147bfe97c7SDan Williams 		return -ENOMEM;
14157bfe97c7SDan Williams 
1416ed07c433SDan Williams 	smart_init(t);
1417f471f1a7SDan Williams 	return ars_state_init(&t->pdev.dev, &t->ars_state);
14186bc75619SDan Williams }
14196bc75619SDan Williams 
14205dc68e55SDan Williams static void dcr_common_init(struct acpi_nfit_control_region *dcr)
14215dc68e55SDan Williams {
14225dc68e55SDan Williams 	dcr->vendor_id = 0xabcd;
14235dc68e55SDan Williams 	dcr->device_id = 0;
14245dc68e55SDan Williams 	dcr->revision_id = 1;
14255dc68e55SDan Williams 	dcr->valid_fields = 1;
14265dc68e55SDan Williams 	dcr->manufacturing_location = 0xa;
14275dc68e55SDan Williams 	dcr->manufacturing_date = cpu_to_be16(2016);
14285dc68e55SDan Williams }
14295dc68e55SDan Williams 
14306bc75619SDan Williams static void nfit_test0_setup(struct nfit_test *t)
14316bc75619SDan Williams {
143285d3fa02SDan Williams 	const int flush_hint_size = sizeof(struct acpi_nfit_flush_address)
143385d3fa02SDan Williams 		+ (sizeof(u64) * NUM_HINTS);
14346bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
14356bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
14366bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
14376bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
14386bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
14396bc75619SDan Williams 	struct acpi_nfit_data_region *bdw;
14409d27a87eSDan Williams 	struct acpi_nfit_flush_address *flush;
1441f81e1d35SDave Jiang 	struct acpi_nfit_capabilities *pcap;
1442d7d8464dSRoss Zwisler 	unsigned int offset = 0, i;
14436bc75619SDan Williams 
14446bc75619SDan Williams 	/*
14456bc75619SDan Williams 	 * spa0 (interleave first half of dimm0 and dimm1, note storage
14466bc75619SDan Williams 	 * does not actually alias the related block-data-window
14476bc75619SDan Williams 	 * regions)
14486bc75619SDan Williams 	 */
14496b577c9dSLinda Knippers 	spa = nfit_buf;
14506bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14516bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14526bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
14536bc75619SDan Williams 	spa->range_index = 0+1;
14546bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
14556bc75619SDan Williams 	spa->length = SPA0_SIZE;
1456d7d8464dSRoss Zwisler 	offset += spa->header.length;
14576bc75619SDan Williams 
14586bc75619SDan Williams 	/*
14596bc75619SDan Williams 	 * spa1 (interleave last half of the 4 DIMMS, note storage
14606bc75619SDan Williams 	 * does not actually alias the related block-data-window
14616bc75619SDan Williams 	 * regions)
14626bc75619SDan Williams 	 */
1463d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
14646bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14656bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14666bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
14676bc75619SDan Williams 	spa->range_index = 1+1;
14686bc75619SDan Williams 	spa->address = t->spa_set_dma[1];
14696bc75619SDan Williams 	spa->length = SPA1_SIZE;
1470d7d8464dSRoss Zwisler 	offset += spa->header.length;
14716bc75619SDan Williams 
14726bc75619SDan Williams 	/* spa2 (dcr0) dimm0 */
1473d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
14746bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14756bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14766bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
14776bc75619SDan Williams 	spa->range_index = 2+1;
14786bc75619SDan Williams 	spa->address = t->dcr_dma[0];
14796bc75619SDan Williams 	spa->length = DCR_SIZE;
1480d7d8464dSRoss Zwisler 	offset += spa->header.length;
14816bc75619SDan Williams 
14826bc75619SDan Williams 	/* spa3 (dcr1) dimm1 */
1483d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
14846bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14856bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14866bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
14876bc75619SDan Williams 	spa->range_index = 3+1;
14886bc75619SDan Williams 	spa->address = t->dcr_dma[1];
14896bc75619SDan Williams 	spa->length = DCR_SIZE;
1490d7d8464dSRoss Zwisler 	offset += spa->header.length;
14916bc75619SDan Williams 
14926bc75619SDan Williams 	/* spa4 (dcr2) dimm2 */
1493d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
14946bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
14956bc75619SDan Williams 	spa->header.length = sizeof(*spa);
14966bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
14976bc75619SDan Williams 	spa->range_index = 4+1;
14986bc75619SDan Williams 	spa->address = t->dcr_dma[2];
14996bc75619SDan Williams 	spa->length = DCR_SIZE;
1500d7d8464dSRoss Zwisler 	offset += spa->header.length;
15016bc75619SDan Williams 
15026bc75619SDan Williams 	/* spa5 (dcr3) dimm3 */
1503d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15046bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15056bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15066bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
15076bc75619SDan Williams 	spa->range_index = 5+1;
15086bc75619SDan Williams 	spa->address = t->dcr_dma[3];
15096bc75619SDan Williams 	spa->length = DCR_SIZE;
1510d7d8464dSRoss Zwisler 	offset += spa->header.length;
15116bc75619SDan Williams 
15126bc75619SDan Williams 	/* spa6 (bdw for dcr0) dimm0 */
1513d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15146bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15156bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15166bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
15176bc75619SDan Williams 	spa->range_index = 6+1;
15186bc75619SDan Williams 	spa->address = t->dimm_dma[0];
15196bc75619SDan Williams 	spa->length = DIMM_SIZE;
1520d7d8464dSRoss Zwisler 	offset += spa->header.length;
15216bc75619SDan Williams 
15226bc75619SDan Williams 	/* spa7 (bdw for dcr1) dimm1 */
1523d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15246bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15256bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15266bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
15276bc75619SDan Williams 	spa->range_index = 7+1;
15286bc75619SDan Williams 	spa->address = t->dimm_dma[1];
15296bc75619SDan Williams 	spa->length = DIMM_SIZE;
1530d7d8464dSRoss Zwisler 	offset += spa->header.length;
15316bc75619SDan Williams 
15326bc75619SDan Williams 	/* spa8 (bdw for dcr2) dimm2 */
1533d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15346bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15356bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15366bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
15376bc75619SDan Williams 	spa->range_index = 8+1;
15386bc75619SDan Williams 	spa->address = t->dimm_dma[2];
15396bc75619SDan Williams 	spa->length = DIMM_SIZE;
1540d7d8464dSRoss Zwisler 	offset += spa->header.length;
15416bc75619SDan Williams 
15426bc75619SDan Williams 	/* spa9 (bdw for dcr3) dimm3 */
1543d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
15446bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
15456bc75619SDan Williams 	spa->header.length = sizeof(*spa);
15466bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
15476bc75619SDan Williams 	spa->range_index = 9+1;
15486bc75619SDan Williams 	spa->address = t->dimm_dma[3];
15496bc75619SDan Williams 	spa->length = DIMM_SIZE;
1550d7d8464dSRoss Zwisler 	offset += spa->header.length;
15516bc75619SDan Williams 
15526bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
15536bc75619SDan Williams 	memdev = nfit_buf + offset;
15546bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
15556bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
15566bc75619SDan Williams 	memdev->device_handle = handle[0];
15576bc75619SDan Williams 	memdev->physical_id = 0;
15586bc75619SDan Williams 	memdev->region_id = 0;
15596bc75619SDan Williams 	memdev->range_index = 0+1;
15603b87356fSDan Williams 	memdev->region_index = 4+1;
15616bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1562df06a2d5SDan Williams 	memdev->region_offset = 1;
15636bc75619SDan Williams 	memdev->address = 0;
15646bc75619SDan Williams 	memdev->interleave_index = 0;
15656bc75619SDan Williams 	memdev->interleave_ways = 2;
1566d7d8464dSRoss Zwisler 	offset += memdev->header.length;
15676bc75619SDan Williams 
15686bc75619SDan Williams 	/* mem-region1 (spa0, dimm1) */
1569d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
15706bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
15716bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
15726bc75619SDan Williams 	memdev->device_handle = handle[1];
15736bc75619SDan Williams 	memdev->physical_id = 1;
15746bc75619SDan Williams 	memdev->region_id = 0;
15756bc75619SDan Williams 	memdev->range_index = 0+1;
15763b87356fSDan Williams 	memdev->region_index = 5+1;
15776bc75619SDan Williams 	memdev->region_size = SPA0_SIZE/2;
1578df06a2d5SDan Williams 	memdev->region_offset = (1 << 8);
15796bc75619SDan Williams 	memdev->address = 0;
15806bc75619SDan Williams 	memdev->interleave_index = 0;
15816bc75619SDan Williams 	memdev->interleave_ways = 2;
1582ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1583d7d8464dSRoss Zwisler 	offset += memdev->header.length;
15846bc75619SDan Williams 
15856bc75619SDan Williams 	/* mem-region2 (spa1, dimm0) */
1586d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
15876bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
15886bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
15896bc75619SDan Williams 	memdev->device_handle = handle[0];
15906bc75619SDan Williams 	memdev->physical_id = 0;
15916bc75619SDan Williams 	memdev->region_id = 1;
15926bc75619SDan Williams 	memdev->range_index = 1+1;
15933b87356fSDan Williams 	memdev->region_index = 4+1;
15946bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1595df06a2d5SDan Williams 	memdev->region_offset = (1 << 16);
15966bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
15976bc75619SDan Williams 	memdev->interleave_index = 0;
15986bc75619SDan Williams 	memdev->interleave_ways = 4;
1599ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1600d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16016bc75619SDan Williams 
16026bc75619SDan Williams 	/* mem-region3 (spa1, dimm1) */
1603d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16046bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16056bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16066bc75619SDan Williams 	memdev->device_handle = handle[1];
16076bc75619SDan Williams 	memdev->physical_id = 1;
16086bc75619SDan Williams 	memdev->region_id = 1;
16096bc75619SDan Williams 	memdev->range_index = 1+1;
16103b87356fSDan Williams 	memdev->region_index = 5+1;
16116bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1612df06a2d5SDan Williams 	memdev->region_offset = (1 << 24);
16136bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
16146bc75619SDan Williams 	memdev->interleave_index = 0;
16156bc75619SDan Williams 	memdev->interleave_ways = 4;
1616d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16176bc75619SDan Williams 
16186bc75619SDan Williams 	/* mem-region4 (spa1, dimm2) */
1619d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16206bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16216bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16226bc75619SDan Williams 	memdev->device_handle = handle[2];
16236bc75619SDan Williams 	memdev->physical_id = 2;
16246bc75619SDan Williams 	memdev->region_id = 0;
16256bc75619SDan Williams 	memdev->range_index = 1+1;
16263b87356fSDan Williams 	memdev->region_index = 6+1;
16276bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1628df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 32);
16296bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
16306bc75619SDan Williams 	memdev->interleave_index = 0;
16316bc75619SDan Williams 	memdev->interleave_ways = 4;
1632ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1633d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16346bc75619SDan Williams 
16356bc75619SDan Williams 	/* mem-region5 (spa1, dimm3) */
1636d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16376bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16386bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16396bc75619SDan Williams 	memdev->device_handle = handle[3];
16406bc75619SDan Williams 	memdev->physical_id = 3;
16416bc75619SDan Williams 	memdev->region_id = 0;
16426bc75619SDan Williams 	memdev->range_index = 1+1;
16433b87356fSDan Williams 	memdev->region_index = 7+1;
16446bc75619SDan Williams 	memdev->region_size = SPA1_SIZE/4;
1645df06a2d5SDan Williams 	memdev->region_offset = (1ULL << 40);
16466bc75619SDan Williams 	memdev->address = SPA0_SIZE/2;
16476bc75619SDan Williams 	memdev->interleave_index = 0;
16486bc75619SDan Williams 	memdev->interleave_ways = 4;
1649d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16506bc75619SDan Williams 
16516bc75619SDan Williams 	/* mem-region6 (spa/dcr0, dimm0) */
1652d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16536bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16546bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16556bc75619SDan Williams 	memdev->device_handle = handle[0];
16566bc75619SDan Williams 	memdev->physical_id = 0;
16576bc75619SDan Williams 	memdev->region_id = 0;
16586bc75619SDan Williams 	memdev->range_index = 2+1;
16596bc75619SDan Williams 	memdev->region_index = 0+1;
16606bc75619SDan Williams 	memdev->region_size = 0;
16616bc75619SDan Williams 	memdev->region_offset = 0;
16626bc75619SDan Williams 	memdev->address = 0;
16636bc75619SDan Williams 	memdev->interleave_index = 0;
16646bc75619SDan Williams 	memdev->interleave_ways = 1;
1665d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16666bc75619SDan Williams 
16676bc75619SDan Williams 	/* mem-region7 (spa/dcr1, dimm1) */
1668d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16696bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16706bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16716bc75619SDan Williams 	memdev->device_handle = handle[1];
16726bc75619SDan Williams 	memdev->physical_id = 1;
16736bc75619SDan Williams 	memdev->region_id = 0;
16746bc75619SDan Williams 	memdev->range_index = 3+1;
16756bc75619SDan Williams 	memdev->region_index = 1+1;
16766bc75619SDan Williams 	memdev->region_size = 0;
16776bc75619SDan Williams 	memdev->region_offset = 0;
16786bc75619SDan Williams 	memdev->address = 0;
16796bc75619SDan Williams 	memdev->interleave_index = 0;
16806bc75619SDan Williams 	memdev->interleave_ways = 1;
1681d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16826bc75619SDan Williams 
16836bc75619SDan Williams 	/* mem-region8 (spa/dcr2, dimm2) */
1684d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
16856bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
16866bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
16876bc75619SDan Williams 	memdev->device_handle = handle[2];
16886bc75619SDan Williams 	memdev->physical_id = 2;
16896bc75619SDan Williams 	memdev->region_id = 0;
16906bc75619SDan Williams 	memdev->range_index = 4+1;
16916bc75619SDan Williams 	memdev->region_index = 2+1;
16926bc75619SDan Williams 	memdev->region_size = 0;
16936bc75619SDan Williams 	memdev->region_offset = 0;
16946bc75619SDan Williams 	memdev->address = 0;
16956bc75619SDan Williams 	memdev->interleave_index = 0;
16966bc75619SDan Williams 	memdev->interleave_ways = 1;
1697d7d8464dSRoss Zwisler 	offset += memdev->header.length;
16986bc75619SDan Williams 
16996bc75619SDan Williams 	/* mem-region9 (spa/dcr3, dimm3) */
1700d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17016bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17026bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17036bc75619SDan Williams 	memdev->device_handle = handle[3];
17046bc75619SDan Williams 	memdev->physical_id = 3;
17056bc75619SDan Williams 	memdev->region_id = 0;
17066bc75619SDan Williams 	memdev->range_index = 5+1;
17076bc75619SDan Williams 	memdev->region_index = 3+1;
17086bc75619SDan Williams 	memdev->region_size = 0;
17096bc75619SDan Williams 	memdev->region_offset = 0;
17106bc75619SDan Williams 	memdev->address = 0;
17116bc75619SDan Williams 	memdev->interleave_index = 0;
17126bc75619SDan Williams 	memdev->interleave_ways = 1;
1713d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17146bc75619SDan Williams 
17156bc75619SDan Williams 	/* mem-region10 (spa/bdw0, dimm0) */
1716d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17176bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17186bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17196bc75619SDan Williams 	memdev->device_handle = handle[0];
17206bc75619SDan Williams 	memdev->physical_id = 0;
17216bc75619SDan Williams 	memdev->region_id = 0;
17226bc75619SDan Williams 	memdev->range_index = 6+1;
17236bc75619SDan Williams 	memdev->region_index = 0+1;
17246bc75619SDan Williams 	memdev->region_size = 0;
17256bc75619SDan Williams 	memdev->region_offset = 0;
17266bc75619SDan Williams 	memdev->address = 0;
17276bc75619SDan Williams 	memdev->interleave_index = 0;
17286bc75619SDan Williams 	memdev->interleave_ways = 1;
1729d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17306bc75619SDan Williams 
17316bc75619SDan Williams 	/* mem-region11 (spa/bdw1, dimm1) */
1732d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17336bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17346bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17356bc75619SDan Williams 	memdev->device_handle = handle[1];
17366bc75619SDan Williams 	memdev->physical_id = 1;
17376bc75619SDan Williams 	memdev->region_id = 0;
17386bc75619SDan Williams 	memdev->range_index = 7+1;
17396bc75619SDan Williams 	memdev->region_index = 1+1;
17406bc75619SDan Williams 	memdev->region_size = 0;
17416bc75619SDan Williams 	memdev->region_offset = 0;
17426bc75619SDan Williams 	memdev->address = 0;
17436bc75619SDan Williams 	memdev->interleave_index = 0;
17446bc75619SDan Williams 	memdev->interleave_ways = 1;
1745d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17466bc75619SDan Williams 
17476bc75619SDan Williams 	/* mem-region12 (spa/bdw2, dimm2) */
1748d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17496bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17506bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17516bc75619SDan Williams 	memdev->device_handle = handle[2];
17526bc75619SDan Williams 	memdev->physical_id = 2;
17536bc75619SDan Williams 	memdev->region_id = 0;
17546bc75619SDan Williams 	memdev->range_index = 8+1;
17556bc75619SDan Williams 	memdev->region_index = 2+1;
17566bc75619SDan Williams 	memdev->region_size = 0;
17576bc75619SDan Williams 	memdev->region_offset = 0;
17586bc75619SDan Williams 	memdev->address = 0;
17596bc75619SDan Williams 	memdev->interleave_index = 0;
17606bc75619SDan Williams 	memdev->interleave_ways = 1;
1761d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17626bc75619SDan Williams 
17636bc75619SDan Williams 	/* mem-region13 (spa/dcr3, dimm3) */
1764d7d8464dSRoss Zwisler 	memdev = nfit_buf + offset;
17656bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
17666bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
17676bc75619SDan Williams 	memdev->device_handle = handle[3];
17686bc75619SDan Williams 	memdev->physical_id = 3;
17696bc75619SDan Williams 	memdev->region_id = 0;
17706bc75619SDan Williams 	memdev->range_index = 9+1;
17716bc75619SDan Williams 	memdev->region_index = 3+1;
17726bc75619SDan Williams 	memdev->region_size = 0;
17736bc75619SDan Williams 	memdev->region_offset = 0;
17746bc75619SDan Williams 	memdev->address = 0;
17756bc75619SDan Williams 	memdev->interleave_index = 0;
17766bc75619SDan Williams 	memdev->interleave_ways = 1;
1777ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
1778d7d8464dSRoss Zwisler 	offset += memdev->header.length;
17796bc75619SDan Williams 
17803b87356fSDan Williams 	/* dcr-descriptor0: blk */
17816bc75619SDan Williams 	dcr = nfit_buf + offset;
17826bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1783d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
17846bc75619SDan Williams 	dcr->region_index = 0+1;
17855dc68e55SDan Williams 	dcr_common_init(dcr);
17866bc75619SDan Williams 	dcr->serial_number = ~handle[0];
1787be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
17886bc75619SDan Williams 	dcr->windows = 1;
17896bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
17906bc75619SDan Williams 	dcr->command_offset = 0;
17916bc75619SDan Williams 	dcr->command_size = 8;
17926bc75619SDan Williams 	dcr->status_offset = 8;
17936bc75619SDan Williams 	dcr->status_size = 4;
1794d7d8464dSRoss Zwisler 	offset += dcr->header.length;
17956bc75619SDan Williams 
17963b87356fSDan Williams 	/* dcr-descriptor1: blk */
1797d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
17986bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1799d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
18006bc75619SDan Williams 	dcr->region_index = 1+1;
18015dc68e55SDan Williams 	dcr_common_init(dcr);
18026bc75619SDan Williams 	dcr->serial_number = ~handle[1];
1803be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
18046bc75619SDan Williams 	dcr->windows = 1;
18056bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
18066bc75619SDan Williams 	dcr->command_offset = 0;
18076bc75619SDan Williams 	dcr->command_size = 8;
18086bc75619SDan Williams 	dcr->status_offset = 8;
18096bc75619SDan Williams 	dcr->status_size = 4;
1810d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18116bc75619SDan Williams 
18123b87356fSDan Williams 	/* dcr-descriptor2: blk */
1813d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
18146bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1815d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
18166bc75619SDan Williams 	dcr->region_index = 2+1;
18175dc68e55SDan Williams 	dcr_common_init(dcr);
18186bc75619SDan Williams 	dcr->serial_number = ~handle[2];
1819be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
18206bc75619SDan Williams 	dcr->windows = 1;
18216bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
18226bc75619SDan Williams 	dcr->command_offset = 0;
18236bc75619SDan Williams 	dcr->command_size = 8;
18246bc75619SDan Williams 	dcr->status_offset = 8;
18256bc75619SDan Williams 	dcr->status_size = 4;
1826d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18276bc75619SDan Williams 
18283b87356fSDan Williams 	/* dcr-descriptor3: blk */
1829d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
18306bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1831d7d8464dSRoss Zwisler 	dcr->header.length = sizeof(*dcr);
18326bc75619SDan Williams 	dcr->region_index = 3+1;
18335dc68e55SDan Williams 	dcr_common_init(dcr);
18346bc75619SDan Williams 	dcr->serial_number = ~handle[3];
1835be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BLK;
18366bc75619SDan Williams 	dcr->windows = 1;
18376bc75619SDan Williams 	dcr->window_size = DCR_SIZE;
18386bc75619SDan Williams 	dcr->command_offset = 0;
18396bc75619SDan Williams 	dcr->command_size = 8;
18406bc75619SDan Williams 	dcr->status_offset = 8;
18416bc75619SDan Williams 	dcr->status_size = 4;
1842d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18436bc75619SDan Williams 
18443b87356fSDan Williams 	/* dcr-descriptor0: pmem */
18453b87356fSDan Williams 	dcr = nfit_buf + offset;
18463b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
18473b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
18483b87356fSDan Williams 			window_size);
18493b87356fSDan Williams 	dcr->region_index = 4+1;
18505dc68e55SDan Williams 	dcr_common_init(dcr);
18513b87356fSDan Williams 	dcr->serial_number = ~handle[0];
18523b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
18533b87356fSDan Williams 	dcr->windows = 0;
1854d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18553b87356fSDan Williams 
18563b87356fSDan Williams 	/* dcr-descriptor1: pmem */
1857d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
18583b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
18593b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
18603b87356fSDan Williams 			window_size);
18613b87356fSDan Williams 	dcr->region_index = 5+1;
18625dc68e55SDan Williams 	dcr_common_init(dcr);
18633b87356fSDan Williams 	dcr->serial_number = ~handle[1];
18643b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
18653b87356fSDan Williams 	dcr->windows = 0;
1866d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18673b87356fSDan Williams 
18683b87356fSDan Williams 	/* dcr-descriptor2: pmem */
1869d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
18703b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
18713b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
18723b87356fSDan Williams 			window_size);
18733b87356fSDan Williams 	dcr->region_index = 6+1;
18745dc68e55SDan Williams 	dcr_common_init(dcr);
18753b87356fSDan Williams 	dcr->serial_number = ~handle[2];
18763b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
18773b87356fSDan Williams 	dcr->windows = 0;
1878d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18793b87356fSDan Williams 
18803b87356fSDan Williams 	/* dcr-descriptor3: pmem */
1881d7d8464dSRoss Zwisler 	dcr = nfit_buf + offset;
18823b87356fSDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
18833b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
18843b87356fSDan Williams 			window_size);
18853b87356fSDan Williams 	dcr->region_index = 7+1;
18865dc68e55SDan Williams 	dcr_common_init(dcr);
18873b87356fSDan Williams 	dcr->serial_number = ~handle[3];
18883b87356fSDan Williams 	dcr->code = NFIT_FIC_BYTEN;
18893b87356fSDan Williams 	dcr->windows = 0;
1890d7d8464dSRoss Zwisler 	offset += dcr->header.length;
18913b87356fSDan Williams 
18926bc75619SDan Williams 	/* bdw0 (spa/dcr0, dimm0) */
18936bc75619SDan Williams 	bdw = nfit_buf + offset;
18946bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1895d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
18966bc75619SDan Williams 	bdw->region_index = 0+1;
18976bc75619SDan Williams 	bdw->windows = 1;
18986bc75619SDan Williams 	bdw->offset = 0;
18996bc75619SDan Williams 	bdw->size = BDW_SIZE;
19006bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
19016bc75619SDan Williams 	bdw->start_address = 0;
1902d7d8464dSRoss Zwisler 	offset += bdw->header.length;
19036bc75619SDan Williams 
19046bc75619SDan Williams 	/* bdw1 (spa/dcr1, dimm1) */
1905d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
19066bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1907d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
19086bc75619SDan Williams 	bdw->region_index = 1+1;
19096bc75619SDan Williams 	bdw->windows = 1;
19106bc75619SDan Williams 	bdw->offset = 0;
19116bc75619SDan Williams 	bdw->size = BDW_SIZE;
19126bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
19136bc75619SDan Williams 	bdw->start_address = 0;
1914d7d8464dSRoss Zwisler 	offset += bdw->header.length;
19156bc75619SDan Williams 
19166bc75619SDan Williams 	/* bdw2 (spa/dcr2, dimm2) */
1917d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
19186bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1919d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
19206bc75619SDan Williams 	bdw->region_index = 2+1;
19216bc75619SDan Williams 	bdw->windows = 1;
19226bc75619SDan Williams 	bdw->offset = 0;
19236bc75619SDan Williams 	bdw->size = BDW_SIZE;
19246bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
19256bc75619SDan Williams 	bdw->start_address = 0;
1926d7d8464dSRoss Zwisler 	offset += bdw->header.length;
19276bc75619SDan Williams 
19286bc75619SDan Williams 	/* bdw3 (spa/dcr3, dimm3) */
1929d7d8464dSRoss Zwisler 	bdw = nfit_buf + offset;
19306bc75619SDan Williams 	bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1931d7d8464dSRoss Zwisler 	bdw->header.length = sizeof(*bdw);
19326bc75619SDan Williams 	bdw->region_index = 3+1;
19336bc75619SDan Williams 	bdw->windows = 1;
19346bc75619SDan Williams 	bdw->offset = 0;
19356bc75619SDan Williams 	bdw->size = BDW_SIZE;
19366bc75619SDan Williams 	bdw->capacity = DIMM_SIZE;
19376bc75619SDan Williams 	bdw->start_address = 0;
1938d7d8464dSRoss Zwisler 	offset += bdw->header.length;
19396bc75619SDan Williams 
19409d27a87eSDan Williams 	/* flush0 (dimm0) */
19419d27a87eSDan Williams 	flush = nfit_buf + offset;
19429d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
194385d3fa02SDan Williams 	flush->header.length = flush_hint_size;
19449d27a87eSDan Williams 	flush->device_handle = handle[0];
194585d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
194685d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
194785d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[0] + i * sizeof(u64);
1948d7d8464dSRoss Zwisler 	offset += flush->header.length;
19499d27a87eSDan Williams 
19509d27a87eSDan Williams 	/* flush1 (dimm1) */
1951d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
19529d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
195385d3fa02SDan Williams 	flush->header.length = flush_hint_size;
19549d27a87eSDan Williams 	flush->device_handle = handle[1];
195585d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
195685d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
195785d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[1] + i * sizeof(u64);
1958d7d8464dSRoss Zwisler 	offset += flush->header.length;
19599d27a87eSDan Williams 
19609d27a87eSDan Williams 	/* flush2 (dimm2) */
1961d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
19629d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
196385d3fa02SDan Williams 	flush->header.length = flush_hint_size;
19649d27a87eSDan Williams 	flush->device_handle = handle[2];
196585d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
196685d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
196785d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[2] + i * sizeof(u64);
1968d7d8464dSRoss Zwisler 	offset += flush->header.length;
19699d27a87eSDan Williams 
19709d27a87eSDan Williams 	/* flush3 (dimm3) */
1971d7d8464dSRoss Zwisler 	flush = nfit_buf + offset;
19729d27a87eSDan Williams 	flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
197385d3fa02SDan Williams 	flush->header.length = flush_hint_size;
19749d27a87eSDan Williams 	flush->device_handle = handle[3];
197585d3fa02SDan Williams 	flush->hint_count = NUM_HINTS;
197685d3fa02SDan Williams 	for (i = 0; i < NUM_HINTS; i++)
197785d3fa02SDan Williams 		flush->hint_address[i] = t->flush_dma[3] + i * sizeof(u64);
1978d7d8464dSRoss Zwisler 	offset += flush->header.length;
19799d27a87eSDan Williams 
1980f81e1d35SDave Jiang 	/* platform capabilities */
1981d7d8464dSRoss Zwisler 	pcap = nfit_buf + offset;
1982f81e1d35SDave Jiang 	pcap->header.type = ACPI_NFIT_TYPE_CAPABILITIES;
1983f81e1d35SDave Jiang 	pcap->header.length = sizeof(*pcap);
1984f81e1d35SDave Jiang 	pcap->highest_capability = 1;
1985f81e1d35SDave Jiang 	pcap->capabilities = ACPI_NFIT_CAPABILITY_CACHE_FLUSH |
1986f81e1d35SDave Jiang 		ACPI_NFIT_CAPABILITY_MEM_FLUSH;
1987d7d8464dSRoss Zwisler 	offset += pcap->header.length;
1988f81e1d35SDave Jiang 
198920985164SVishal Verma 	if (t->setup_hotplug) {
19903b87356fSDan Williams 		/* dcr-descriptor4: blk */
199120985164SVishal Verma 		dcr = nfit_buf + offset;
199220985164SVishal Verma 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1993d7d8464dSRoss Zwisler 		dcr->header.length = sizeof(*dcr);
19943b87356fSDan Williams 		dcr->region_index = 8+1;
19955dc68e55SDan Williams 		dcr_common_init(dcr);
199620985164SVishal Verma 		dcr->serial_number = ~handle[4];
1997be26f9aeSDan Williams 		dcr->code = NFIT_FIC_BLK;
199820985164SVishal Verma 		dcr->windows = 1;
199920985164SVishal Verma 		dcr->window_size = DCR_SIZE;
200020985164SVishal Verma 		dcr->command_offset = 0;
200120985164SVishal Verma 		dcr->command_size = 8;
200220985164SVishal Verma 		dcr->status_offset = 8;
200320985164SVishal Verma 		dcr->status_size = 4;
2004d7d8464dSRoss Zwisler 		offset += dcr->header.length;
200520985164SVishal Verma 
20063b87356fSDan Williams 		/* dcr-descriptor4: pmem */
20073b87356fSDan Williams 		dcr = nfit_buf + offset;
20083b87356fSDan Williams 		dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
20093b87356fSDan Williams 		dcr->header.length = offsetof(struct acpi_nfit_control_region,
20103b87356fSDan Williams 				window_size);
20113b87356fSDan Williams 		dcr->region_index = 9+1;
20125dc68e55SDan Williams 		dcr_common_init(dcr);
20133b87356fSDan Williams 		dcr->serial_number = ~handle[4];
20143b87356fSDan Williams 		dcr->code = NFIT_FIC_BYTEN;
20153b87356fSDan Williams 		dcr->windows = 0;
2016d7d8464dSRoss Zwisler 		offset += dcr->header.length;
20173b87356fSDan Williams 
201820985164SVishal Verma 		/* bdw4 (spa/dcr4, dimm4) */
201920985164SVishal Verma 		bdw = nfit_buf + offset;
202020985164SVishal Verma 		bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
2021d7d8464dSRoss Zwisler 		bdw->header.length = sizeof(*bdw);
20223b87356fSDan Williams 		bdw->region_index = 8+1;
202320985164SVishal Verma 		bdw->windows = 1;
202420985164SVishal Verma 		bdw->offset = 0;
202520985164SVishal Verma 		bdw->size = BDW_SIZE;
202620985164SVishal Verma 		bdw->capacity = DIMM_SIZE;
202720985164SVishal Verma 		bdw->start_address = 0;
2028d7d8464dSRoss Zwisler 		offset += bdw->header.length;
202920985164SVishal Verma 
203020985164SVishal Verma 		/* spa10 (dcr4) dimm4 */
203120985164SVishal Verma 		spa = nfit_buf + offset;
203220985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
203320985164SVishal Verma 		spa->header.length = sizeof(*spa);
203420985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
203520985164SVishal Verma 		spa->range_index = 10+1;
203620985164SVishal Verma 		spa->address = t->dcr_dma[4];
203720985164SVishal Verma 		spa->length = DCR_SIZE;
2038d7d8464dSRoss Zwisler 		offset += spa->header.length;
203920985164SVishal Verma 
204020985164SVishal Verma 		/*
204120985164SVishal Verma 		 * spa11 (single-dimm interleave for hotplug, note storage
204220985164SVishal Verma 		 * does not actually alias the related block-data-window
204320985164SVishal Verma 		 * regions)
204420985164SVishal Verma 		 */
2045d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
204620985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
204720985164SVishal Verma 		spa->header.length = sizeof(*spa);
204820985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
204920985164SVishal Verma 		spa->range_index = 11+1;
205020985164SVishal Verma 		spa->address = t->spa_set_dma[2];
205120985164SVishal Verma 		spa->length = SPA0_SIZE;
2052d7d8464dSRoss Zwisler 		offset += spa->header.length;
205320985164SVishal Verma 
205420985164SVishal Verma 		/* spa12 (bdw for dcr4) dimm4 */
2055d7d8464dSRoss Zwisler 		spa = nfit_buf + offset;
205620985164SVishal Verma 		spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
205720985164SVishal Verma 		spa->header.length = sizeof(*spa);
205820985164SVishal Verma 		memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
205920985164SVishal Verma 		spa->range_index = 12+1;
206020985164SVishal Verma 		spa->address = t->dimm_dma[4];
206120985164SVishal Verma 		spa->length = DIMM_SIZE;
2062d7d8464dSRoss Zwisler 		offset += spa->header.length;
206320985164SVishal Verma 
206420985164SVishal Verma 		/* mem-region14 (spa/dcr4, dimm4) */
206520985164SVishal Verma 		memdev = nfit_buf + offset;
206620985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
206720985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
206820985164SVishal Verma 		memdev->device_handle = handle[4];
206920985164SVishal Verma 		memdev->physical_id = 4;
207020985164SVishal Verma 		memdev->region_id = 0;
207120985164SVishal Verma 		memdev->range_index = 10+1;
20723b87356fSDan Williams 		memdev->region_index = 8+1;
207320985164SVishal Verma 		memdev->region_size = 0;
207420985164SVishal Verma 		memdev->region_offset = 0;
207520985164SVishal Verma 		memdev->address = 0;
207620985164SVishal Verma 		memdev->interleave_index = 0;
207720985164SVishal Verma 		memdev->interleave_ways = 1;
2078d7d8464dSRoss Zwisler 		offset += memdev->header.length;
207920985164SVishal Verma 
2080d7d8464dSRoss Zwisler 		/* mem-region15 (spa11, dimm4) */
2081d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
208220985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
208320985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
208420985164SVishal Verma 		memdev->device_handle = handle[4];
208520985164SVishal Verma 		memdev->physical_id = 4;
208620985164SVishal Verma 		memdev->region_id = 0;
208720985164SVishal Verma 		memdev->range_index = 11+1;
20883b87356fSDan Williams 		memdev->region_index = 9+1;
208920985164SVishal Verma 		memdev->region_size = SPA0_SIZE;
2090df06a2d5SDan Williams 		memdev->region_offset = (1ULL << 48);
209120985164SVishal Verma 		memdev->address = 0;
209220985164SVishal Verma 		memdev->interleave_index = 0;
209320985164SVishal Verma 		memdev->interleave_ways = 1;
2094ac40b675SDan Williams 		memdev->flags = ACPI_NFIT_MEM_HEALTH_ENABLED;
2095d7d8464dSRoss Zwisler 		offset += memdev->header.length;
209620985164SVishal Verma 
20973b87356fSDan Williams 		/* mem-region16 (spa/bdw4, dimm4) */
2098d7d8464dSRoss Zwisler 		memdev = nfit_buf + offset;
209920985164SVishal Verma 		memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
210020985164SVishal Verma 		memdev->header.length = sizeof(*memdev);
210120985164SVishal Verma 		memdev->device_handle = handle[4];
210220985164SVishal Verma 		memdev->physical_id = 4;
210320985164SVishal Verma 		memdev->region_id = 0;
210420985164SVishal Verma 		memdev->range_index = 12+1;
21053b87356fSDan Williams 		memdev->region_index = 8+1;
210620985164SVishal Verma 		memdev->region_size = 0;
210720985164SVishal Verma 		memdev->region_offset = 0;
210820985164SVishal Verma 		memdev->address = 0;
210920985164SVishal Verma 		memdev->interleave_index = 0;
211020985164SVishal Verma 		memdev->interleave_ways = 1;
2111d7d8464dSRoss Zwisler 		offset += memdev->header.length;
211220985164SVishal Verma 
211320985164SVishal Verma 		/* flush3 (dimm4) */
211420985164SVishal Verma 		flush = nfit_buf + offset;
211520985164SVishal Verma 		flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
211685d3fa02SDan Williams 		flush->header.length = flush_hint_size;
211720985164SVishal Verma 		flush->device_handle = handle[4];
211885d3fa02SDan Williams 		flush->hint_count = NUM_HINTS;
211985d3fa02SDan Williams 		for (i = 0; i < NUM_HINTS; i++)
212085d3fa02SDan Williams 			flush->hint_address[i] = t->flush_dma[4]
212185d3fa02SDan Williams 				+ i * sizeof(u64);
2122d7d8464dSRoss Zwisler 		offset += flush->header.length;
21239741a559SRoss Zwisler 
21249741a559SRoss Zwisler 		/* sanity check to make sure we've filled the buffer */
21259741a559SRoss Zwisler 		WARN_ON(offset != t->nfit_size);
212620985164SVishal Verma 	}
212720985164SVishal Verma 
21281526f9e2SRoss Zwisler 	t->nfit_filled = offset;
21291526f9e2SRoss Zwisler 
21309fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
21319fb1a190SDave Jiang 			SPA0_SIZE);
2132f471f1a7SDan Williams 
21336bc75619SDan Williams 	acpi_desc = &t->acpi_desc;
2134e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
2135e3654ecaSDan Williams 	set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2136e3654ecaSDan Williams 	set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
2137ed07c433SDan Williams 	set_bit(ND_INTEL_SMART, &acpi_desc->dimm_cmd_force_en);
2138ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
2139ed07c433SDan Williams 	set_bit(ND_INTEL_SMART_SET_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
21404cf260fcSVishal Verma 	set_bit(ND_INTEL_SMART_INJECT, &acpi_desc->dimm_cmd_force_en);
2141e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2142e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2143e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2144e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
214510246dc8SYasunori Goto 	set_bit(ND_CMD_CALL, &acpi_desc->bus_cmd_force_en);
214610246dc8SYasunori Goto 	set_bit(NFIT_CMD_TRANSLATE_SPA, &acpi_desc->bus_nfit_cmd_force_en);
21479fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_SET, &acpi_desc->bus_nfit_cmd_force_en);
21489fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_CLEAR, &acpi_desc->bus_nfit_cmd_force_en);
21499fb1a190SDave Jiang 	set_bit(NFIT_CMD_ARS_INJECT_GET, &acpi_desc->bus_nfit_cmd_force_en);
2150bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_GET_INFO, &acpi_desc->dimm_cmd_force_en);
2151bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_START_UPDATE, &acpi_desc->dimm_cmd_force_en);
2152bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_SEND_DATA, &acpi_desc->dimm_cmd_force_en);
2153bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_UPDATE, &acpi_desc->dimm_cmd_force_en);
2154bfbaa952SDave Jiang 	set_bit(ND_INTEL_FW_FINISH_QUERY, &acpi_desc->dimm_cmd_force_en);
2155674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
21566bc75619SDan Williams }
21576bc75619SDan Williams 
21586bc75619SDan Williams static void nfit_test1_setup(struct nfit_test *t)
21596bc75619SDan Williams {
21606b577c9dSLinda Knippers 	size_t offset;
21616bc75619SDan Williams 	void *nfit_buf = t->nfit_buf;
21626bc75619SDan Williams 	struct acpi_nfit_memory_map *memdev;
21636bc75619SDan Williams 	struct acpi_nfit_control_region *dcr;
21646bc75619SDan Williams 	struct acpi_nfit_system_address *spa;
2165d26f73f0SDan Williams 	struct acpi_nfit_desc *acpi_desc;
21666bc75619SDan Williams 
21676b577c9dSLinda Knippers 	offset = 0;
21686bc75619SDan Williams 	/* spa0 (flat range with no bdw aliasing) */
21696bc75619SDan Williams 	spa = nfit_buf + offset;
21706bc75619SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
21716bc75619SDan Williams 	spa->header.length = sizeof(*spa);
21726bc75619SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
21736bc75619SDan Williams 	spa->range_index = 0+1;
21746bc75619SDan Williams 	spa->address = t->spa_set_dma[0];
21756bc75619SDan Williams 	spa->length = SPA2_SIZE;
2176d7d8464dSRoss Zwisler 	offset += spa->header.length;
21776bc75619SDan Williams 
21787bfe97c7SDan Williams 	/* virtual cd region */
2179d7d8464dSRoss Zwisler 	spa = nfit_buf + offset;
21807bfe97c7SDan Williams 	spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
21817bfe97c7SDan Williams 	spa->header.length = sizeof(*spa);
21827bfe97c7SDan Williams 	memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_VCD), 16);
21837bfe97c7SDan Williams 	spa->range_index = 0;
21847bfe97c7SDan Williams 	spa->address = t->spa_set_dma[1];
21857bfe97c7SDan Williams 	spa->length = SPA_VCD_SIZE;
2186d7d8464dSRoss Zwisler 	offset += spa->header.length;
21877bfe97c7SDan Williams 
21886bc75619SDan Williams 	/* mem-region0 (spa0, dimm0) */
21896bc75619SDan Williams 	memdev = nfit_buf + offset;
21906bc75619SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
21916bc75619SDan Williams 	memdev->header.length = sizeof(*memdev);
2192dafb1048SDan Williams 	memdev->device_handle = handle[5];
21936bc75619SDan Williams 	memdev->physical_id = 0;
21946bc75619SDan Williams 	memdev->region_id = 0;
21956bc75619SDan Williams 	memdev->range_index = 0+1;
21966bc75619SDan Williams 	memdev->region_index = 0+1;
21976bc75619SDan Williams 	memdev->region_size = SPA2_SIZE;
21986bc75619SDan Williams 	memdev->region_offset = 0;
21996bc75619SDan Williams 	memdev->address = 0;
22006bc75619SDan Williams 	memdev->interleave_index = 0;
22016bc75619SDan Williams 	memdev->interleave_ways = 1;
220258138820SDan Williams 	memdev->flags = ACPI_NFIT_MEM_SAVE_FAILED | ACPI_NFIT_MEM_RESTORE_FAILED
220358138820SDan Williams 		| ACPI_NFIT_MEM_FLUSH_FAILED | ACPI_NFIT_MEM_HEALTH_OBSERVED
2204f4295796SDan Williams 		| ACPI_NFIT_MEM_NOT_ARMED;
2205d7d8464dSRoss Zwisler 	offset += memdev->header.length;
22066bc75619SDan Williams 
22076bc75619SDan Williams 	/* dcr-descriptor0 */
22086bc75619SDan Williams 	dcr = nfit_buf + offset;
22096bc75619SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
22103b87356fSDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
22113b87356fSDan Williams 			window_size);
22126bc75619SDan Williams 	dcr->region_index = 0+1;
22135dc68e55SDan Williams 	dcr_common_init(dcr);
2214dafb1048SDan Williams 	dcr->serial_number = ~handle[5];
2215be26f9aeSDan Williams 	dcr->code = NFIT_FIC_BYTE;
22166bc75619SDan Williams 	dcr->windows = 0;
2217ac40b675SDan Williams 	offset += dcr->header.length;
2218d7d8464dSRoss Zwisler 
2219ac40b675SDan Williams 	memdev = nfit_buf + offset;
2220ac40b675SDan Williams 	memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
2221ac40b675SDan Williams 	memdev->header.length = sizeof(*memdev);
2222ac40b675SDan Williams 	memdev->device_handle = handle[6];
2223ac40b675SDan Williams 	memdev->physical_id = 0;
2224ac40b675SDan Williams 	memdev->region_id = 0;
2225ac40b675SDan Williams 	memdev->range_index = 0;
2226ac40b675SDan Williams 	memdev->region_index = 0+2;
2227ac40b675SDan Williams 	memdev->region_size = SPA2_SIZE;
2228ac40b675SDan Williams 	memdev->region_offset = 0;
2229ac40b675SDan Williams 	memdev->address = 0;
2230ac40b675SDan Williams 	memdev->interleave_index = 0;
2231ac40b675SDan Williams 	memdev->interleave_ways = 1;
2232ac40b675SDan Williams 	memdev->flags = ACPI_NFIT_MEM_MAP_FAILED;
2233d7d8464dSRoss Zwisler 	offset += memdev->header.length;
2234ac40b675SDan Williams 
2235ac40b675SDan Williams 	/* dcr-descriptor1 */
2236ac40b675SDan Williams 	dcr = nfit_buf + offset;
2237ac40b675SDan Williams 	dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
2238ac40b675SDan Williams 	dcr->header.length = offsetof(struct acpi_nfit_control_region,
2239ac40b675SDan Williams 			window_size);
2240ac40b675SDan Williams 	dcr->region_index = 0+2;
2241ac40b675SDan Williams 	dcr_common_init(dcr);
2242ac40b675SDan Williams 	dcr->serial_number = ~handle[6];
2243ac40b675SDan Williams 	dcr->code = NFIT_FIC_BYTE;
2244ac40b675SDan Williams 	dcr->windows = 0;
2245d7d8464dSRoss Zwisler 	offset += dcr->header.length;
2246ac40b675SDan Williams 
22479741a559SRoss Zwisler 	/* sanity check to make sure we've filled the buffer */
22489741a559SRoss Zwisler 	WARN_ON(offset != t->nfit_size);
22499741a559SRoss Zwisler 
22501526f9e2SRoss Zwisler 	t->nfit_filled = offset;
22511526f9e2SRoss Zwisler 
22529fb1a190SDave Jiang 	post_ars_status(&t->ars_state, &t->badrange, t->spa_set_dma[0],
22539fb1a190SDave Jiang 			SPA2_SIZE);
2254f471f1a7SDan Williams 
2255d26f73f0SDan Williams 	acpi_desc = &t->acpi_desc;
2256e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
2257e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
2258e3654ecaSDan Williams 	set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
2259e3654ecaSDan Williams 	set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
2260674d8bdeSDave Jiang 	set_bit(ND_INTEL_ENABLE_LSS_STATUS, &acpi_desc->dimm_cmd_force_en);
22616bc75619SDan Williams }
22626bc75619SDan Williams 
22636bc75619SDan Williams static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
22646bc75619SDan Williams 		void *iobuf, u64 len, int rw)
22656bc75619SDan Williams {
22666bc75619SDan Williams 	struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
22676bc75619SDan Williams 	struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
22686bc75619SDan Williams 	struct nd_region *nd_region = &ndbr->nd_region;
22696bc75619SDan Williams 	unsigned int lane;
22706bc75619SDan Williams 
22716bc75619SDan Williams 	lane = nd_region_acquire_lane(nd_region);
22726bc75619SDan Williams 	if (rw)
227367a3e8feSRoss Zwisler 		memcpy(mmio->addr.base + dpa, iobuf, len);
227467a3e8feSRoss Zwisler 	else {
227567a3e8feSRoss Zwisler 		memcpy(iobuf, mmio->addr.base + dpa, len);
227667a3e8feSRoss Zwisler 
22775deb67f7SRobin Murphy 		/* give us some some coverage of the arch_invalidate_pmem() API */
22785deb67f7SRobin Murphy 		arch_invalidate_pmem(mmio->addr.base + dpa, len);
227967a3e8feSRoss Zwisler 	}
22806bc75619SDan Williams 	nd_region_release_lane(nd_region, lane);
22816bc75619SDan Williams 
22826bc75619SDan Williams 	return 0;
22836bc75619SDan Williams }
22846bc75619SDan Williams 
2285a7de92daSDan Williams static unsigned long nfit_ctl_handle;
2286a7de92daSDan Williams 
2287a7de92daSDan Williams union acpi_object *result;
2288a7de92daSDan Williams 
2289a7de92daSDan Williams static union acpi_object *nfit_test_evaluate_dsm(acpi_handle handle,
229094116f81SAndy Shevchenko 		const guid_t *guid, u64 rev, u64 func, union acpi_object *argv4)
2291a7de92daSDan Williams {
2292a7de92daSDan Williams 	if (handle != &nfit_ctl_handle)
2293a7de92daSDan Williams 		return ERR_PTR(-ENXIO);
2294a7de92daSDan Williams 
2295a7de92daSDan Williams 	return result;
2296a7de92daSDan Williams }
2297a7de92daSDan Williams 
2298a7de92daSDan Williams static int setup_result(void *buf, size_t size)
2299a7de92daSDan Williams {
2300a7de92daSDan Williams 	result = kmalloc(sizeof(union acpi_object) + size, GFP_KERNEL);
2301a7de92daSDan Williams 	if (!result)
2302a7de92daSDan Williams 		return -ENOMEM;
2303a7de92daSDan Williams 	result->package.type = ACPI_TYPE_BUFFER,
2304a7de92daSDan Williams 	result->buffer.pointer = (void *) (result + 1);
2305a7de92daSDan Williams 	result->buffer.length = size;
2306a7de92daSDan Williams 	memcpy(result->buffer.pointer, buf, size);
2307a7de92daSDan Williams 	memset(buf, 0, size);
2308a7de92daSDan Williams 	return 0;
2309a7de92daSDan Williams }
2310a7de92daSDan Williams 
2311a7de92daSDan Williams static int nfit_ctl_test(struct device *dev)
2312a7de92daSDan Williams {
2313a7de92daSDan Williams 	int rc, cmd_rc;
2314a7de92daSDan Williams 	struct nvdimm *nvdimm;
2315a7de92daSDan Williams 	struct acpi_device *adev;
2316a7de92daSDan Williams 	struct nfit_mem *nfit_mem;
2317a7de92daSDan Williams 	struct nd_ars_record *record;
2318a7de92daSDan Williams 	struct acpi_nfit_desc *acpi_desc;
2319a7de92daSDan Williams 	const u64 test_val = 0x0123456789abcdefULL;
2320a7de92daSDan Williams 	unsigned long mask, cmd_size, offset;
2321a7de92daSDan Williams 	union {
2322a7de92daSDan Williams 		struct nd_cmd_get_config_size cfg_size;
2323fb2a1748SDan Williams 		struct nd_cmd_clear_error clear_err;
2324a7de92daSDan Williams 		struct nd_cmd_ars_status ars_stat;
2325a7de92daSDan Williams 		struct nd_cmd_ars_cap ars_cap;
2326a7de92daSDan Williams 		char buf[sizeof(struct nd_cmd_ars_status)
2327a7de92daSDan Williams 			+ sizeof(struct nd_ars_record)];
2328a7de92daSDan Williams 	} cmds;
2329a7de92daSDan Williams 
2330a7de92daSDan Williams 	adev = devm_kzalloc(dev, sizeof(*adev), GFP_KERNEL);
2331a7de92daSDan Williams 	if (!adev)
2332a7de92daSDan Williams 		return -ENOMEM;
2333a7de92daSDan Williams 	*adev = (struct acpi_device) {
2334a7de92daSDan Williams 		.handle = &nfit_ctl_handle,
2335a7de92daSDan Williams 		.dev = {
2336a7de92daSDan Williams 			.init_name = "test-adev",
2337a7de92daSDan Williams 		},
2338a7de92daSDan Williams 	};
2339a7de92daSDan Williams 
2340a7de92daSDan Williams 	acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2341a7de92daSDan Williams 	if (!acpi_desc)
2342a7de92daSDan Williams 		return -ENOMEM;
2343a7de92daSDan Williams 	*acpi_desc = (struct acpi_nfit_desc) {
2344a7de92daSDan Williams 		.nd_desc = {
2345a7de92daSDan Williams 			.cmd_mask = 1UL << ND_CMD_ARS_CAP
2346a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_START
2347a7de92daSDan Williams 				| 1UL << ND_CMD_ARS_STATUS
234810246dc8SYasunori Goto 				| 1UL << ND_CMD_CLEAR_ERROR
234910246dc8SYasunori Goto 				| 1UL << ND_CMD_CALL,
2350a7de92daSDan Williams 			.module = THIS_MODULE,
2351a7de92daSDan Williams 			.provider_name = "ACPI.NFIT",
2352a7de92daSDan Williams 			.ndctl = acpi_nfit_ctl,
23539fb1a190SDave Jiang 			.bus_dsm_mask = 1UL << NFIT_CMD_TRANSLATE_SPA
23549fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_SET
23559fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_CLEAR
23569fb1a190SDave Jiang 				| 1UL << NFIT_CMD_ARS_INJECT_GET,
2357a7de92daSDan Williams 		},
2358a7de92daSDan Williams 		.dev = &adev->dev,
2359a7de92daSDan Williams 	};
2360a7de92daSDan Williams 
2361a7de92daSDan Williams 	nfit_mem = devm_kzalloc(dev, sizeof(*nfit_mem), GFP_KERNEL);
2362a7de92daSDan Williams 	if (!nfit_mem)
2363a7de92daSDan Williams 		return -ENOMEM;
2364a7de92daSDan Williams 
2365a7de92daSDan Williams 	mask = 1UL << ND_CMD_SMART | 1UL << ND_CMD_SMART_THRESHOLD
2366a7de92daSDan Williams 		| 1UL << ND_CMD_DIMM_FLAGS | 1UL << ND_CMD_GET_CONFIG_SIZE
2367a7de92daSDan Williams 		| 1UL << ND_CMD_GET_CONFIG_DATA | 1UL << ND_CMD_SET_CONFIG_DATA
2368a7de92daSDan Williams 		| 1UL << ND_CMD_VENDOR;
2369a7de92daSDan Williams 	*nfit_mem = (struct nfit_mem) {
2370a7de92daSDan Williams 		.adev = adev,
2371a7de92daSDan Williams 		.family = NVDIMM_FAMILY_INTEL,
2372a7de92daSDan Williams 		.dsm_mask = mask,
2373a7de92daSDan Williams 	};
2374a7de92daSDan Williams 
2375a7de92daSDan Williams 	nvdimm = devm_kzalloc(dev, sizeof(*nvdimm), GFP_KERNEL);
2376a7de92daSDan Williams 	if (!nvdimm)
2377a7de92daSDan Williams 		return -ENOMEM;
2378a7de92daSDan Williams 	*nvdimm = (struct nvdimm) {
2379a7de92daSDan Williams 		.provider_data = nfit_mem,
2380a7de92daSDan Williams 		.cmd_mask = mask,
2381a7de92daSDan Williams 		.dev = {
2382a7de92daSDan Williams 			.init_name = "test-dimm",
2383a7de92daSDan Williams 		},
2384a7de92daSDan Williams 	};
2385a7de92daSDan Williams 
2386a7de92daSDan Williams 
2387a7de92daSDan Williams 	/* basic checkout of a typical 'get config size' command */
2388a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2389a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2390a7de92daSDan Williams 		.status = 0,
2391a7de92daSDan Williams 		.config_size = SZ_128K,
2392a7de92daSDan Williams 		.max_xfer = SZ_4K,
2393a7de92daSDan Williams 	};
2394a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2395a7de92daSDan Williams 	if (rc)
2396a7de92daSDan Williams 		return rc;
2397a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2398a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2399a7de92daSDan Williams 
2400a7de92daSDan Williams 	if (rc < 0 || cmd_rc || cmds.cfg_size.status != 0
2401a7de92daSDan Williams 			|| cmds.cfg_size.config_size != SZ_128K
2402a7de92daSDan Williams 			|| cmds.cfg_size.max_xfer != SZ_4K) {
2403a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2404a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2405a7de92daSDan Williams 		return -EIO;
2406a7de92daSDan Williams 	}
2407a7de92daSDan Williams 
2408a7de92daSDan Williams 
2409a7de92daSDan Williams 	/* test ars_status with zero output */
2410a7de92daSDan Williams 	cmd_size = offsetof(struct nd_cmd_ars_status, address);
2411a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2412a7de92daSDan Williams 		.out_length = 0,
2413a7de92daSDan Williams 	};
2414a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2415a7de92daSDan Williams 	if (rc)
2416a7de92daSDan Williams 		return rc;
2417a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2418a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2419a7de92daSDan Williams 
2420a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2421a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2422a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2423a7de92daSDan Williams 		return -EIO;
2424a7de92daSDan Williams 	}
2425a7de92daSDan Williams 
2426a7de92daSDan Williams 
2427a7de92daSDan Williams 	/* test ars_cap with benign extended status */
2428a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_cap);
2429a7de92daSDan Williams 	cmds.ars_cap = (struct nd_cmd_ars_cap) {
2430a7de92daSDan Williams 		.status = ND_ARS_PERSISTENT << 16,
2431a7de92daSDan Williams 	};
2432a7de92daSDan Williams 	offset = offsetof(struct nd_cmd_ars_cap, status);
2433a7de92daSDan Williams 	rc = setup_result(cmds.buf + offset, cmd_size - offset);
2434a7de92daSDan Williams 	if (rc)
2435a7de92daSDan Williams 		return rc;
2436a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_CAP,
2437a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2438a7de92daSDan Williams 
2439a7de92daSDan Williams 	if (rc < 0 || cmd_rc) {
2440a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2441a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2442a7de92daSDan Williams 		return -EIO;
2443a7de92daSDan Williams 	}
2444a7de92daSDan Williams 
2445a7de92daSDan Williams 
2446a7de92daSDan Williams 	/* test ars_status with 'status' trimmed from 'out_length' */
2447a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2448a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2449a7de92daSDan Williams 		.out_length = cmd_size - 4,
2450a7de92daSDan Williams 	};
2451a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2452a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2453a7de92daSDan Williams 		.length = test_val,
2454a7de92daSDan Williams 	};
2455a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2456a7de92daSDan Williams 	if (rc)
2457a7de92daSDan Williams 		return rc;
2458a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2459a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2460a7de92daSDan Williams 
2461a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2462a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2463a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2464a7de92daSDan Williams 		return -EIO;
2465a7de92daSDan Williams 	}
2466a7de92daSDan Williams 
2467a7de92daSDan Williams 
2468a7de92daSDan Williams 	/* test ars_status with 'Output (Size)' including 'status' */
2469a7de92daSDan Williams 	cmd_size = sizeof(cmds.ars_stat) + sizeof(struct nd_ars_record);
2470a7de92daSDan Williams 	cmds.ars_stat = (struct nd_cmd_ars_status) {
2471a7de92daSDan Williams 		.out_length = cmd_size,
2472a7de92daSDan Williams 	};
2473a7de92daSDan Williams 	record = &cmds.ars_stat.records[0];
2474a7de92daSDan Williams 	*record = (struct nd_ars_record) {
2475a7de92daSDan Williams 		.length = test_val,
2476a7de92daSDan Williams 	};
2477a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2478a7de92daSDan Williams 	if (rc)
2479a7de92daSDan Williams 		return rc;
2480a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_ARS_STATUS,
2481a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2482a7de92daSDan Williams 
2483a7de92daSDan Williams 	if (rc < 0 || cmd_rc || record->length != test_val) {
2484a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2485a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2486a7de92daSDan Williams 		return -EIO;
2487a7de92daSDan Williams 	}
2488a7de92daSDan Williams 
2489a7de92daSDan Williams 
2490a7de92daSDan Williams 	/* test extended status for get_config_size results in failure */
2491a7de92daSDan Williams 	cmd_size = sizeof(cmds.cfg_size);
2492a7de92daSDan Williams 	cmds.cfg_size = (struct nd_cmd_get_config_size) {
2493a7de92daSDan Williams 		.status = 1 << 16,
2494a7de92daSDan Williams 	};
2495a7de92daSDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2496a7de92daSDan Williams 	if (rc)
2497a7de92daSDan Williams 		return rc;
2498a7de92daSDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, nvdimm, ND_CMD_GET_CONFIG_SIZE,
2499a7de92daSDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2500a7de92daSDan Williams 
2501a7de92daSDan Williams 	if (rc < 0 || cmd_rc >= 0) {
2502a7de92daSDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2503a7de92daSDan Williams 				__func__, __LINE__, rc, cmd_rc);
2504a7de92daSDan Williams 		return -EIO;
2505a7de92daSDan Williams 	}
2506a7de92daSDan Williams 
2507fb2a1748SDan Williams 	/* test clear error */
2508fb2a1748SDan Williams 	cmd_size = sizeof(cmds.clear_err);
2509fb2a1748SDan Williams 	cmds.clear_err = (struct nd_cmd_clear_error) {
2510fb2a1748SDan Williams 		.length = 512,
2511fb2a1748SDan Williams 		.cleared = 512,
2512fb2a1748SDan Williams 	};
2513fb2a1748SDan Williams 	rc = setup_result(cmds.buf, cmd_size);
2514fb2a1748SDan Williams 	if (rc)
2515fb2a1748SDan Williams 		return rc;
2516fb2a1748SDan Williams 	rc = acpi_nfit_ctl(&acpi_desc->nd_desc, NULL, ND_CMD_CLEAR_ERROR,
2517fb2a1748SDan Williams 			cmds.buf, cmd_size, &cmd_rc);
2518fb2a1748SDan Williams 	if (rc < 0 || cmd_rc) {
2519fb2a1748SDan Williams 		dev_dbg(dev, "%s: failed at: %d rc: %d cmd_rc: %d\n",
2520fb2a1748SDan Williams 				__func__, __LINE__, rc, cmd_rc);
2521fb2a1748SDan Williams 		return -EIO;
2522fb2a1748SDan Williams 	}
2523fb2a1748SDan Williams 
2524a7de92daSDan Williams 	return 0;
2525a7de92daSDan Williams }
2526a7de92daSDan Williams 
25276bc75619SDan Williams static int nfit_test_probe(struct platform_device *pdev)
25286bc75619SDan Williams {
25296bc75619SDan Williams 	struct nvdimm_bus_descriptor *nd_desc;
25306bc75619SDan Williams 	struct acpi_nfit_desc *acpi_desc;
25316bc75619SDan Williams 	struct device *dev = &pdev->dev;
25326bc75619SDan Williams 	struct nfit_test *nfit_test;
2533231bf117SDan Williams 	struct nfit_mem *nfit_mem;
2534c14a868aSDan Williams 	union acpi_object *obj;
25356bc75619SDan Williams 	int rc;
25366bc75619SDan Williams 
2537a7de92daSDan Williams 	if (strcmp(dev_name(&pdev->dev), "nfit_test.0") == 0) {
2538a7de92daSDan Williams 		rc = nfit_ctl_test(&pdev->dev);
2539a7de92daSDan Williams 		if (rc)
2540a7de92daSDan Williams 			return rc;
2541a7de92daSDan Williams 	}
2542a7de92daSDan Williams 
25436bc75619SDan Williams 	nfit_test = to_nfit_test(&pdev->dev);
25446bc75619SDan Williams 
25456bc75619SDan Williams 	/* common alloc */
25466bc75619SDan Williams 	if (nfit_test->num_dcr) {
25476bc75619SDan Williams 		int num = nfit_test->num_dcr;
25486bc75619SDan Williams 
25496bc75619SDan Williams 		nfit_test->dimm = devm_kcalloc(dev, num, sizeof(void *),
25506bc75619SDan Williams 				GFP_KERNEL);
25516bc75619SDan Williams 		nfit_test->dimm_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
25526bc75619SDan Williams 				GFP_KERNEL);
25539d27a87eSDan Williams 		nfit_test->flush = devm_kcalloc(dev, num, sizeof(void *),
25549d27a87eSDan Williams 				GFP_KERNEL);
25559d27a87eSDan Williams 		nfit_test->flush_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
25569d27a87eSDan Williams 				GFP_KERNEL);
25576bc75619SDan Williams 		nfit_test->label = devm_kcalloc(dev, num, sizeof(void *),
25586bc75619SDan Williams 				GFP_KERNEL);
25596bc75619SDan Williams 		nfit_test->label_dma = devm_kcalloc(dev, num,
25606bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
25616bc75619SDan Williams 		nfit_test->dcr = devm_kcalloc(dev, num,
25626bc75619SDan Williams 				sizeof(struct nfit_test_dcr *), GFP_KERNEL);
25636bc75619SDan Williams 		nfit_test->dcr_dma = devm_kcalloc(dev, num,
25646bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
2565ed07c433SDan Williams 		nfit_test->smart = devm_kcalloc(dev, num,
2566ed07c433SDan Williams 				sizeof(struct nd_intel_smart), GFP_KERNEL);
2567ed07c433SDan Williams 		nfit_test->smart_threshold = devm_kcalloc(dev, num,
2568ed07c433SDan Williams 				sizeof(struct nd_intel_smart_threshold),
2569ed07c433SDan Williams 				GFP_KERNEL);
2570bfbaa952SDave Jiang 		nfit_test->fw = devm_kcalloc(dev, num,
2571bfbaa952SDave Jiang 				sizeof(struct nfit_test_fw), GFP_KERNEL);
25726bc75619SDan Williams 		if (nfit_test->dimm && nfit_test->dimm_dma && nfit_test->label
25736bc75619SDan Williams 				&& nfit_test->label_dma && nfit_test->dcr
25749d27a87eSDan Williams 				&& nfit_test->dcr_dma && nfit_test->flush
2575bfbaa952SDave Jiang 				&& nfit_test->flush_dma
2576bfbaa952SDave Jiang 				&& nfit_test->fw)
25776bc75619SDan Williams 			/* pass */;
25786bc75619SDan Williams 		else
25796bc75619SDan Williams 			return -ENOMEM;
25806bc75619SDan Williams 	}
25816bc75619SDan Williams 
25826bc75619SDan Williams 	if (nfit_test->num_pm) {
25836bc75619SDan Williams 		int num = nfit_test->num_pm;
25846bc75619SDan Williams 
25856bc75619SDan Williams 		nfit_test->spa_set = devm_kcalloc(dev, num, sizeof(void *),
25866bc75619SDan Williams 				GFP_KERNEL);
25876bc75619SDan Williams 		nfit_test->spa_set_dma = devm_kcalloc(dev, num,
25886bc75619SDan Williams 				sizeof(dma_addr_t), GFP_KERNEL);
25896bc75619SDan Williams 		if (nfit_test->spa_set && nfit_test->spa_set_dma)
25906bc75619SDan Williams 			/* pass */;
25916bc75619SDan Williams 		else
25926bc75619SDan Williams 			return -ENOMEM;
25936bc75619SDan Williams 	}
25946bc75619SDan Williams 
25956bc75619SDan Williams 	/* per-nfit specific alloc */
25966bc75619SDan Williams 	if (nfit_test->alloc(nfit_test))
25976bc75619SDan Williams 		return -ENOMEM;
25986bc75619SDan Williams 
25996bc75619SDan Williams 	nfit_test->setup(nfit_test);
26006bc75619SDan Williams 	acpi_desc = &nfit_test->acpi_desc;
2601a61fe6f7SDan Williams 	acpi_nfit_desc_init(acpi_desc, &pdev->dev);
26026bc75619SDan Williams 	acpi_desc->blk_do_io = nfit_test_blk_do_io;
26036bc75619SDan Williams 	nd_desc = &acpi_desc->nd_desc;
2604a61fe6f7SDan Williams 	nd_desc->provider_name = NULL;
2605bc9775d8SDan Williams 	nd_desc->module = THIS_MODULE;
2606a61fe6f7SDan Williams 	nd_desc->ndctl = nfit_test_ctl;
26076bc75619SDan Williams 
2608e7a11b44SDan Williams 	rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_buf,
26091526f9e2SRoss Zwisler 			nfit_test->nfit_filled);
261058cd71b4SDan Williams 	if (rc)
261120985164SVishal Verma 		return rc;
261220985164SVishal Verma 
2613fbabd829SDan Williams 	rc = devm_add_action_or_reset(&pdev->dev, acpi_nfit_shutdown, acpi_desc);
2614fbabd829SDan Williams 	if (rc)
2615fbabd829SDan Williams 		return rc;
2616fbabd829SDan Williams 
261720985164SVishal Verma 	if (nfit_test->setup != nfit_test0_setup)
261820985164SVishal Verma 		return 0;
261920985164SVishal Verma 
262020985164SVishal Verma 	nfit_test->setup_hotplug = 1;
262120985164SVishal Verma 	nfit_test->setup(nfit_test);
262220985164SVishal Verma 
2623c14a868aSDan Williams 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
2624c14a868aSDan Williams 	if (!obj)
2625c14a868aSDan Williams 		return -ENOMEM;
2626c14a868aSDan Williams 	obj->type = ACPI_TYPE_BUFFER;
2627c14a868aSDan Williams 	obj->buffer.length = nfit_test->nfit_size;
2628c14a868aSDan Williams 	obj->buffer.pointer = nfit_test->nfit_buf;
2629c14a868aSDan Williams 	*(nfit_test->_fit) = obj;
2630c14a868aSDan Williams 	__acpi_nfit_notify(&pdev->dev, nfit_test, 0x80);
2631231bf117SDan Williams 
2632231bf117SDan Williams 	/* associate dimm devices with nfit_mem data for notification testing */
2633231bf117SDan Williams 	mutex_lock(&acpi_desc->init_mutex);
2634231bf117SDan Williams 	list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
2635231bf117SDan Williams 		u32 nfit_handle = __to_nfit_memdev(nfit_mem)->device_handle;
2636231bf117SDan Williams 		int i;
2637231bf117SDan Williams 
2638231bf117SDan Williams 		for (i = 0; i < NUM_DCR; i++)
2639231bf117SDan Williams 			if (nfit_handle == handle[i])
2640231bf117SDan Williams 				dev_set_drvdata(nfit_test->dimm_dev[i],
2641231bf117SDan Williams 						nfit_mem);
2642231bf117SDan Williams 	}
2643231bf117SDan Williams 	mutex_unlock(&acpi_desc->init_mutex);
26446bc75619SDan Williams 
26456bc75619SDan Williams 	return 0;
26466bc75619SDan Williams }
26476bc75619SDan Williams 
26486bc75619SDan Williams static int nfit_test_remove(struct platform_device *pdev)
26496bc75619SDan Williams {
26506bc75619SDan Williams 	return 0;
26516bc75619SDan Williams }
26526bc75619SDan Williams 
26536bc75619SDan Williams static void nfit_test_release(struct device *dev)
26546bc75619SDan Williams {
26556bc75619SDan Williams 	struct nfit_test *nfit_test = to_nfit_test(dev);
26566bc75619SDan Williams 
26576bc75619SDan Williams 	kfree(nfit_test);
26586bc75619SDan Williams }
26596bc75619SDan Williams 
26606bc75619SDan Williams static const struct platform_device_id nfit_test_id[] = {
26616bc75619SDan Williams 	{ KBUILD_MODNAME },
26626bc75619SDan Williams 	{ },
26636bc75619SDan Williams };
26646bc75619SDan Williams 
26656bc75619SDan Williams static struct platform_driver nfit_test_driver = {
26666bc75619SDan Williams 	.probe = nfit_test_probe,
26676bc75619SDan Williams 	.remove = nfit_test_remove,
26686bc75619SDan Williams 	.driver = {
26696bc75619SDan Williams 		.name = KBUILD_MODNAME,
26706bc75619SDan Williams 	},
26716bc75619SDan Williams 	.id_table = nfit_test_id,
26726bc75619SDan Williams };
26736bc75619SDan Williams 
26746bc75619SDan Williams static __init int nfit_test_init(void)
26756bc75619SDan Williams {
26766bc75619SDan Williams 	int rc, i;
26776bc75619SDan Williams 
26780fb5c8dfSDan Williams 	pmem_test();
26790fb5c8dfSDan Williams 	libnvdimm_test();
26800fb5c8dfSDan Williams 	acpi_nfit_test();
26810fb5c8dfSDan Williams 	device_dax_test();
26820fb5c8dfSDan Williams 
2683a7de92daSDan Williams 	nfit_test_setup(nfit_test_lookup, nfit_test_evaluate_dsm);
2684231bf117SDan Williams 
26859fb1a190SDave Jiang 	nfit_wq = create_singlethread_workqueue("nfit");
26869fb1a190SDave Jiang 	if (!nfit_wq)
26879fb1a190SDave Jiang 		return -ENOMEM;
26889fb1a190SDave Jiang 
2689a7de92daSDan Williams 	nfit_test_dimm = class_create(THIS_MODULE, "nfit_test_dimm");
2690a7de92daSDan Williams 	if (IS_ERR(nfit_test_dimm)) {
2691a7de92daSDan Williams 		rc = PTR_ERR(nfit_test_dimm);
2692a7de92daSDan Williams 		goto err_register;
2693a7de92daSDan Williams 	}
26946bc75619SDan Williams 
26956bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++) {
26966bc75619SDan Williams 		struct nfit_test *nfit_test;
26976bc75619SDan Williams 		struct platform_device *pdev;
26986bc75619SDan Williams 
26996bc75619SDan Williams 		nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
27006bc75619SDan Williams 		if (!nfit_test) {
27016bc75619SDan Williams 			rc = -ENOMEM;
27026bc75619SDan Williams 			goto err_register;
27036bc75619SDan Williams 		}
27046bc75619SDan Williams 		INIT_LIST_HEAD(&nfit_test->resources);
27059fb1a190SDave Jiang 		badrange_init(&nfit_test->badrange);
27066bc75619SDan Williams 		switch (i) {
27076bc75619SDan Williams 		case 0:
27086bc75619SDan Williams 			nfit_test->num_pm = NUM_PM;
2709dafb1048SDan Williams 			nfit_test->dcr_idx = 0;
27106bc75619SDan Williams 			nfit_test->num_dcr = NUM_DCR;
27116bc75619SDan Williams 			nfit_test->alloc = nfit_test0_alloc;
27126bc75619SDan Williams 			nfit_test->setup = nfit_test0_setup;
27136bc75619SDan Williams 			break;
27146bc75619SDan Williams 		case 1:
2715a117699cSYasunori Goto 			nfit_test->num_pm = 2;
2716dafb1048SDan Williams 			nfit_test->dcr_idx = NUM_DCR;
2717ac40b675SDan Williams 			nfit_test->num_dcr = 2;
27186bc75619SDan Williams 			nfit_test->alloc = nfit_test1_alloc;
27196bc75619SDan Williams 			nfit_test->setup = nfit_test1_setup;
27206bc75619SDan Williams 			break;
27216bc75619SDan Williams 		default:
27226bc75619SDan Williams 			rc = -EINVAL;
27236bc75619SDan Williams 			goto err_register;
27246bc75619SDan Williams 		}
27256bc75619SDan Williams 		pdev = &nfit_test->pdev;
27266bc75619SDan Williams 		pdev->name = KBUILD_MODNAME;
27276bc75619SDan Williams 		pdev->id = i;
27286bc75619SDan Williams 		pdev->dev.release = nfit_test_release;
27296bc75619SDan Williams 		rc = platform_device_register(pdev);
27306bc75619SDan Williams 		if (rc) {
27316bc75619SDan Williams 			put_device(&pdev->dev);
27326bc75619SDan Williams 			goto err_register;
27336bc75619SDan Williams 		}
27348b06b884SDan Williams 		get_device(&pdev->dev);
27356bc75619SDan Williams 
27366bc75619SDan Williams 		rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
27376bc75619SDan Williams 		if (rc)
27386bc75619SDan Williams 			goto err_register;
27396bc75619SDan Williams 
27406bc75619SDan Williams 		instances[i] = nfit_test;
27419fb1a190SDave Jiang 		INIT_WORK(&nfit_test->work, uc_error_notify);
27426bc75619SDan Williams 	}
27436bc75619SDan Williams 
27446bc75619SDan Williams 	rc = platform_driver_register(&nfit_test_driver);
27456bc75619SDan Williams 	if (rc)
27466bc75619SDan Williams 		goto err_register;
27476bc75619SDan Williams 	return 0;
27486bc75619SDan Williams 
27496bc75619SDan Williams  err_register:
27509fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
27516bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
27526bc75619SDan Williams 		if (instances[i])
27536bc75619SDan Williams 			platform_device_unregister(&instances[i]->pdev);
27546bc75619SDan Williams 	nfit_test_teardown();
27558b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
27568b06b884SDan Williams 		if (instances[i])
27578b06b884SDan Williams 			put_device(&instances[i]->pdev.dev);
27588b06b884SDan Williams 
27596bc75619SDan Williams 	return rc;
27606bc75619SDan Williams }
27616bc75619SDan Williams 
27626bc75619SDan Williams static __exit void nfit_test_exit(void)
27636bc75619SDan Williams {
27646bc75619SDan Williams 	int i;
27656bc75619SDan Williams 
27669fb1a190SDave Jiang 	flush_workqueue(nfit_wq);
27679fb1a190SDave Jiang 	destroy_workqueue(nfit_wq);
27686bc75619SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
27696bc75619SDan Williams 		platform_device_unregister(&instances[i]->pdev);
27708b06b884SDan Williams 	platform_driver_unregister(&nfit_test_driver);
27716bc75619SDan Williams 	nfit_test_teardown();
27728b06b884SDan Williams 
27738b06b884SDan Williams 	for (i = 0; i < NUM_NFITS; i++)
27748b06b884SDan Williams 		put_device(&instances[i]->pdev.dev);
2775231bf117SDan Williams 	class_destroy(nfit_test_dimm);
27766bc75619SDan Williams }
27776bc75619SDan Williams 
27786bc75619SDan Williams module_init(nfit_test_init);
27796bc75619SDan Williams module_exit(nfit_test_exit);
27806bc75619SDan Williams MODULE_LICENSE("GPL v2");
27816bc75619SDan Williams MODULE_AUTHOR("Intel Corporation");
2782