1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2021 Intel Corporation. All rights reserved. */
3 #include <linux/libnvdimm.h>
4 #include <asm/unaligned.h>
5 #include <linux/device.h>
6 #include <linux/module.h>
7 #include <linux/ndctl.h>
8 #include <linux/async.h>
9 #include <linux/slab.h>
14 * Ordered workqueue for cxl nvdimm device arrival and departure
15 * to coordinate bus rescans when a bridge arrives and trigger remove
16 * operations when the bridge is removed.
18 static struct workqueue_struct *cxl_pmem_wq;
20 static __read_mostly DECLARE_BITMAP(exclusive_cmds, CXL_MEM_COMMAND_ID_MAX);
22 static void clear_exclusive(void *cxlds)
24 clear_exclusive_cxl_commands(cxlds, exclusive_cmds);
27 static void unregister_nvdimm(void *nvdimm)
29 nvdimm_delete(nvdimm);
32 static int cxl_nvdimm_probe(struct device *dev)
34 struct cxl_nvdimm *cxl_nvd = to_cxl_nvdimm(dev);
35 struct cxl_memdev *cxlmd = cxl_nvd->cxlmd;
36 unsigned long flags = 0, cmd_mask = 0;
37 struct cxl_dev_state *cxlds = cxlmd->cxlds;
38 struct cxl_nvdimm_bridge *cxl_nvb;
39 struct nvdimm *nvdimm;
42 cxl_nvb = cxl_find_nvdimm_bridge(cxl_nvd);
46 device_lock(&cxl_nvb->dev);
47 if (!cxl_nvb->nvdimm_bus) {
52 set_exclusive_cxl_commands(cxlds, exclusive_cmds);
53 rc = devm_add_action_or_reset(dev, clear_exclusive, cxlds);
57 set_bit(NDD_LABELING, &flags);
58 set_bit(ND_CMD_GET_CONFIG_SIZE, &cmd_mask);
59 set_bit(ND_CMD_GET_CONFIG_DATA, &cmd_mask);
60 set_bit(ND_CMD_SET_CONFIG_DATA, &cmd_mask);
61 nvdimm = nvdimm_create(cxl_nvb->nvdimm_bus, cxl_nvd, NULL, flags,
68 dev_set_drvdata(dev, nvdimm);
69 rc = devm_add_action_or_reset(dev, unregister_nvdimm, nvdimm);
71 device_unlock(&cxl_nvb->dev);
72 put_device(&cxl_nvb->dev);
77 static struct cxl_driver cxl_nvdimm_driver = {
79 .probe = cxl_nvdimm_probe,
80 .id = CXL_DEVICE_NVDIMM,
83 static int cxl_pmem_get_config_size(struct cxl_dev_state *cxlds,
84 struct nd_cmd_get_config_size *cmd,
87 if (sizeof(*cmd) > buf_len)
90 *cmd = (struct nd_cmd_get_config_size) {
91 .config_size = cxlds->lsa_size,
92 .max_xfer = cxlds->payload_size,
98 static int cxl_pmem_get_config_data(struct cxl_dev_state *cxlds,
99 struct nd_cmd_get_config_data_hdr *cmd,
100 unsigned int buf_len)
102 struct cxl_mbox_get_lsa get_lsa;
105 if (sizeof(*cmd) > buf_len)
107 if (struct_size(cmd, out_buf, cmd->in_length) > buf_len)
110 get_lsa = (struct cxl_mbox_get_lsa) {
111 .offset = cpu_to_le32(cmd->in_offset),
112 .length = cpu_to_le32(cmd->in_length),
115 rc = cxl_mbox_send_cmd(cxlds, CXL_MBOX_OP_GET_LSA, &get_lsa,
116 sizeof(get_lsa), cmd->out_buf, cmd->in_length);
122 static int cxl_pmem_set_config_data(struct cxl_dev_state *cxlds,
123 struct nd_cmd_set_config_hdr *cmd,
124 unsigned int buf_len)
126 struct cxl_mbox_set_lsa *set_lsa;
129 if (sizeof(*cmd) > buf_len)
132 /* 4-byte status follows the input data in the payload */
133 if (struct_size(cmd, in_buf, cmd->in_length) + 4 > buf_len)
137 kvzalloc(struct_size(set_lsa, data, cmd->in_length), GFP_KERNEL);
141 *set_lsa = (struct cxl_mbox_set_lsa) {
142 .offset = cpu_to_le32(cmd->in_offset),
144 memcpy(set_lsa->data, cmd->in_buf, cmd->in_length);
146 rc = cxl_mbox_send_cmd(cxlds, CXL_MBOX_OP_SET_LSA, set_lsa,
147 struct_size(set_lsa, data, cmd->in_length),
151 * Set "firmware" status (4-packed bytes at the end of the input
154 put_unaligned(0, (u32 *) &cmd->in_buf[cmd->in_length]);
160 static int cxl_pmem_nvdimm_ctl(struct nvdimm *nvdimm, unsigned int cmd,
161 void *buf, unsigned int buf_len)
163 struct cxl_nvdimm *cxl_nvd = nvdimm_provider_data(nvdimm);
164 unsigned long cmd_mask = nvdimm_cmd_mask(nvdimm);
165 struct cxl_memdev *cxlmd = cxl_nvd->cxlmd;
166 struct cxl_dev_state *cxlds = cxlmd->cxlds;
168 if (!test_bit(cmd, &cmd_mask))
172 case ND_CMD_GET_CONFIG_SIZE:
173 return cxl_pmem_get_config_size(cxlds, buf, buf_len);
174 case ND_CMD_GET_CONFIG_DATA:
175 return cxl_pmem_get_config_data(cxlds, buf, buf_len);
176 case ND_CMD_SET_CONFIG_DATA:
177 return cxl_pmem_set_config_data(cxlds, buf, buf_len);
183 static int cxl_pmem_ctl(struct nvdimm_bus_descriptor *nd_desc,
184 struct nvdimm *nvdimm, unsigned int cmd, void *buf,
185 unsigned int buf_len, int *cmd_rc)
188 * No firmware response to translate, let the transport error
189 * code take precedence.
195 return cxl_pmem_nvdimm_ctl(nvdimm, cmd, buf, buf_len);
198 static bool online_nvdimm_bus(struct cxl_nvdimm_bridge *cxl_nvb)
200 if (cxl_nvb->nvdimm_bus)
202 cxl_nvb->nvdimm_bus =
203 nvdimm_bus_register(&cxl_nvb->dev, &cxl_nvb->nd_desc);
204 return cxl_nvb->nvdimm_bus != NULL;
207 static int cxl_nvdimm_release_driver(struct device *dev, void *data)
209 if (!is_cxl_nvdimm(dev))
211 device_release_driver(dev);
215 static void offline_nvdimm_bus(struct nvdimm_bus *nvdimm_bus)
221 * Set the state of cxl_nvdimm devices to unbound / idle before
222 * nvdimm_bus_unregister() rips the nvdimm objects out from
225 bus_for_each_dev(&cxl_bus_type, NULL, NULL, cxl_nvdimm_release_driver);
226 nvdimm_bus_unregister(nvdimm_bus);
229 static void cxl_nvb_update_state(struct work_struct *work)
231 struct cxl_nvdimm_bridge *cxl_nvb =
232 container_of(work, typeof(*cxl_nvb), state_work);
233 struct nvdimm_bus *victim_bus = NULL;
234 bool release = false, rescan = false;
236 device_lock(&cxl_nvb->dev);
237 switch (cxl_nvb->state) {
239 if (!online_nvdimm_bus(cxl_nvb)) {
240 dev_err(&cxl_nvb->dev,
241 "failed to establish nvdimm bus\n");
246 case CXL_NVB_OFFLINE:
248 victim_bus = cxl_nvb->nvdimm_bus;
249 cxl_nvb->nvdimm_bus = NULL;
254 device_unlock(&cxl_nvb->dev);
257 device_release_driver(&cxl_nvb->dev);
259 int rc = bus_rescan_devices(&cxl_bus_type);
261 dev_dbg(&cxl_nvb->dev, "rescan: %d\n", rc);
263 offline_nvdimm_bus(victim_bus);
265 put_device(&cxl_nvb->dev);
268 static void cxl_nvdimm_bridge_state_work(struct cxl_nvdimm_bridge *cxl_nvb)
271 * Take a reference that the workqueue will drop if new work
274 get_device(&cxl_nvb->dev);
275 if (!queue_work(cxl_pmem_wq, &cxl_nvb->state_work))
276 put_device(&cxl_nvb->dev);
279 static void cxl_nvdimm_bridge_remove(struct device *dev)
281 struct cxl_nvdimm_bridge *cxl_nvb = to_cxl_nvdimm_bridge(dev);
283 if (cxl_nvb->state == CXL_NVB_ONLINE)
284 cxl_nvb->state = CXL_NVB_OFFLINE;
285 cxl_nvdimm_bridge_state_work(cxl_nvb);
288 static int cxl_nvdimm_bridge_probe(struct device *dev)
290 struct cxl_nvdimm_bridge *cxl_nvb = to_cxl_nvdimm_bridge(dev);
292 if (cxl_nvb->state == CXL_NVB_DEAD)
295 if (cxl_nvb->state == CXL_NVB_NEW) {
296 cxl_nvb->nd_desc = (struct nvdimm_bus_descriptor) {
297 .provider_name = "CXL",
298 .module = THIS_MODULE,
299 .ndctl = cxl_pmem_ctl,
302 INIT_WORK(&cxl_nvb->state_work, cxl_nvb_update_state);
305 cxl_nvb->state = CXL_NVB_ONLINE;
306 cxl_nvdimm_bridge_state_work(cxl_nvb);
311 static struct cxl_driver cxl_nvdimm_bridge_driver = {
312 .name = "cxl_nvdimm_bridge",
313 .probe = cxl_nvdimm_bridge_probe,
314 .remove = cxl_nvdimm_bridge_remove,
315 .id = CXL_DEVICE_NVDIMM_BRIDGE,
319 * Return all bridges to the CXL_NVB_NEW state to invalidate any
320 * ->state_work referring to the now destroyed cxl_pmem_wq.
322 static int cxl_nvdimm_bridge_reset(struct device *dev, void *data)
324 struct cxl_nvdimm_bridge *cxl_nvb;
326 if (!is_cxl_nvdimm_bridge(dev))
329 cxl_nvb = to_cxl_nvdimm_bridge(dev);
331 cxl_nvb->state = CXL_NVB_NEW;
337 static void destroy_cxl_pmem_wq(void)
339 destroy_workqueue(cxl_pmem_wq);
340 bus_for_each_dev(&cxl_bus_type, NULL, NULL, cxl_nvdimm_bridge_reset);
343 static __init int cxl_pmem_init(void)
347 set_bit(CXL_MEM_COMMAND_ID_SET_SHUTDOWN_STATE, exclusive_cmds);
348 set_bit(CXL_MEM_COMMAND_ID_SET_LSA, exclusive_cmds);
350 cxl_pmem_wq = alloc_ordered_workqueue("cxl_pmem", 0);
354 rc = cxl_driver_register(&cxl_nvdimm_bridge_driver);
358 rc = cxl_driver_register(&cxl_nvdimm_driver);
365 cxl_driver_unregister(&cxl_nvdimm_bridge_driver);
367 destroy_cxl_pmem_wq();
371 static __exit void cxl_pmem_exit(void)
373 cxl_driver_unregister(&cxl_nvdimm_driver);
374 cxl_driver_unregister(&cxl_nvdimm_bridge_driver);
375 destroy_cxl_pmem_wq();
378 MODULE_LICENSE("GPL v2");
379 module_init(cxl_pmem_init);
380 module_exit(cxl_pmem_exit);
381 MODULE_IMPORT_NS(CXL);
382 MODULE_ALIAS_CXL(CXL_DEVICE_NVDIMM_BRIDGE);
383 MODULE_ALIAS_CXL(CXL_DEVICE_NVDIMM);