xref: /linux/drivers/pci/switch/switchtec.c (revision 52d8db8e0cd7c28316514568fe5df0cfd4fa2075)
18cfab3cfSBjorn Helgaas // SPDX-License-Identifier: GPL-2.0
2080b47deSLogan Gunthorpe /*
3080b47deSLogan Gunthorpe  * Microsemi Switchtec(tm) PCIe Management Driver
4080b47deSLogan Gunthorpe  * Copyright (c) 2017, Microsemi Corporation
5080b47deSLogan Gunthorpe  */
6080b47deSLogan Gunthorpe 
75a1c269fSLogan Gunthorpe #include <linux/switchtec.h>
852eabba5SLogan Gunthorpe #include <linux/switchtec_ioctl.h>
952eabba5SLogan Gunthorpe 
10080b47deSLogan Gunthorpe #include <linux/interrupt.h>
11080b47deSLogan Gunthorpe #include <linux/module.h>
12080b47deSLogan Gunthorpe #include <linux/fs.h>
13080b47deSLogan Gunthorpe #include <linux/uaccess.h>
14080b47deSLogan Gunthorpe #include <linux/poll.h>
15080b47deSLogan Gunthorpe #include <linux/wait.h>
16080b47deSLogan Gunthorpe 
1746feb6b4SGustavo A. R. Silva #include <linux/nospec.h>
1846feb6b4SGustavo A. R. Silva 
19080b47deSLogan Gunthorpe MODULE_DESCRIPTION("Microsemi Switchtec(tm) PCIe Management Driver");
20080b47deSLogan Gunthorpe MODULE_VERSION("0.1");
21080b47deSLogan Gunthorpe MODULE_LICENSE("GPL");
22080b47deSLogan Gunthorpe MODULE_AUTHOR("Microsemi Corporation");
23080b47deSLogan Gunthorpe 
24080b47deSLogan Gunthorpe static int max_devices = 16;
25080b47deSLogan Gunthorpe module_param(max_devices, int, 0644);
26080b47deSLogan Gunthorpe MODULE_PARM_DESC(max_devices, "max number of switchtec device instances");
27080b47deSLogan Gunthorpe 
28080b47deSLogan Gunthorpe static dev_t switchtec_devt;
29080b47deSLogan Gunthorpe static DEFINE_IDA(switchtec_minor_ida);
30080b47deSLogan Gunthorpe 
31302e994dSLogan Gunthorpe struct class *switchtec_class;
32302e994dSLogan Gunthorpe EXPORT_SYMBOL_GPL(switchtec_class);
33080b47deSLogan Gunthorpe 
34080b47deSLogan Gunthorpe enum mrpc_state {
35080b47deSLogan Gunthorpe 	MRPC_IDLE = 0,
36080b47deSLogan Gunthorpe 	MRPC_QUEUED,
37080b47deSLogan Gunthorpe 	MRPC_RUNNING,
38080b47deSLogan Gunthorpe 	MRPC_DONE,
39080b47deSLogan Gunthorpe };
40080b47deSLogan Gunthorpe 
41080b47deSLogan Gunthorpe struct switchtec_user {
42080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev;
43080b47deSLogan Gunthorpe 
44080b47deSLogan Gunthorpe 	enum mrpc_state state;
45080b47deSLogan Gunthorpe 
46080b47deSLogan Gunthorpe 	struct completion comp;
47080b47deSLogan Gunthorpe 	struct kref kref;
48080b47deSLogan Gunthorpe 	struct list_head list;
49080b47deSLogan Gunthorpe 
50080b47deSLogan Gunthorpe 	u32 cmd;
51080b47deSLogan Gunthorpe 	u32 status;
52080b47deSLogan Gunthorpe 	u32 return_code;
53080b47deSLogan Gunthorpe 	size_t data_len;
54080b47deSLogan Gunthorpe 	size_t read_len;
55080b47deSLogan Gunthorpe 	unsigned char data[SWITCHTEC_MRPC_PAYLOAD_SIZE];
56080b47deSLogan Gunthorpe 	int event_cnt;
57080b47deSLogan Gunthorpe };
58080b47deSLogan Gunthorpe 
59080b47deSLogan Gunthorpe static struct switchtec_user *stuser_create(struct switchtec_dev *stdev)
60080b47deSLogan Gunthorpe {
61080b47deSLogan Gunthorpe 	struct switchtec_user *stuser;
62080b47deSLogan Gunthorpe 
63080b47deSLogan Gunthorpe 	stuser = kzalloc(sizeof(*stuser), GFP_KERNEL);
64080b47deSLogan Gunthorpe 	if (!stuser)
65080b47deSLogan Gunthorpe 		return ERR_PTR(-ENOMEM);
66080b47deSLogan Gunthorpe 
67080b47deSLogan Gunthorpe 	get_device(&stdev->dev);
68080b47deSLogan Gunthorpe 	stuser->stdev = stdev;
69080b47deSLogan Gunthorpe 	kref_init(&stuser->kref);
70080b47deSLogan Gunthorpe 	INIT_LIST_HEAD(&stuser->list);
71080b47deSLogan Gunthorpe 	init_completion(&stuser->comp);
72080b47deSLogan Gunthorpe 	stuser->event_cnt = atomic_read(&stdev->event_cnt);
73080b47deSLogan Gunthorpe 
74080b47deSLogan Gunthorpe 	dev_dbg(&stdev->dev, "%s: %p\n", __func__, stuser);
75080b47deSLogan Gunthorpe 
76080b47deSLogan Gunthorpe 	return stuser;
77080b47deSLogan Gunthorpe }
78080b47deSLogan Gunthorpe 
79080b47deSLogan Gunthorpe static void stuser_free(struct kref *kref)
80080b47deSLogan Gunthorpe {
81080b47deSLogan Gunthorpe 	struct switchtec_user *stuser;
82080b47deSLogan Gunthorpe 
83080b47deSLogan Gunthorpe 	stuser = container_of(kref, struct switchtec_user, kref);
84080b47deSLogan Gunthorpe 
85080b47deSLogan Gunthorpe 	dev_dbg(&stuser->stdev->dev, "%s: %p\n", __func__, stuser);
86080b47deSLogan Gunthorpe 
87080b47deSLogan Gunthorpe 	put_device(&stuser->stdev->dev);
88080b47deSLogan Gunthorpe 	kfree(stuser);
89080b47deSLogan Gunthorpe }
90080b47deSLogan Gunthorpe 
91080b47deSLogan Gunthorpe static void stuser_put(struct switchtec_user *stuser)
92080b47deSLogan Gunthorpe {
93080b47deSLogan Gunthorpe 	kref_put(&stuser->kref, stuser_free);
94080b47deSLogan Gunthorpe }
95080b47deSLogan Gunthorpe 
96080b47deSLogan Gunthorpe static void stuser_set_state(struct switchtec_user *stuser,
97080b47deSLogan Gunthorpe 			     enum mrpc_state state)
98080b47deSLogan Gunthorpe {
99080b47deSLogan Gunthorpe 	/* requires the mrpc_mutex to already be held when called */
100080b47deSLogan Gunthorpe 
101080b47deSLogan Gunthorpe 	const char * const state_names[] = {
102080b47deSLogan Gunthorpe 		[MRPC_IDLE] = "IDLE",
103080b47deSLogan Gunthorpe 		[MRPC_QUEUED] = "QUEUED",
104080b47deSLogan Gunthorpe 		[MRPC_RUNNING] = "RUNNING",
105080b47deSLogan Gunthorpe 		[MRPC_DONE] = "DONE",
106080b47deSLogan Gunthorpe 	};
107080b47deSLogan Gunthorpe 
108080b47deSLogan Gunthorpe 	stuser->state = state;
109080b47deSLogan Gunthorpe 
110080b47deSLogan Gunthorpe 	dev_dbg(&stuser->stdev->dev, "stuser state %p -> %s",
111080b47deSLogan Gunthorpe 		stuser, state_names[state]);
112080b47deSLogan Gunthorpe }
113080b47deSLogan Gunthorpe 
114080b47deSLogan Gunthorpe static void mrpc_complete_cmd(struct switchtec_dev *stdev);
115080b47deSLogan Gunthorpe 
116*52d8db8eSKelvin Cao static void flush_wc_buf(struct switchtec_dev *stdev)
117*52d8db8eSKelvin Cao {
118*52d8db8eSKelvin Cao 	struct ntb_dbmsg_regs __iomem *mmio_dbmsg;
119*52d8db8eSKelvin Cao 
120*52d8db8eSKelvin Cao 	/*
121*52d8db8eSKelvin Cao 	 * odb (outbound doorbell) register is processed by low latency
122*52d8db8eSKelvin Cao 	 * hardware and w/o side effect
123*52d8db8eSKelvin Cao 	 */
124*52d8db8eSKelvin Cao 	mmio_dbmsg = (void __iomem *)stdev->mmio_ntb +
125*52d8db8eSKelvin Cao 		SWITCHTEC_NTB_REG_DBMSG_OFFSET;
126*52d8db8eSKelvin Cao 	ioread32(&mmio_dbmsg->odb);
127*52d8db8eSKelvin Cao }
128*52d8db8eSKelvin Cao 
129080b47deSLogan Gunthorpe static void mrpc_cmd_submit(struct switchtec_dev *stdev)
130080b47deSLogan Gunthorpe {
131080b47deSLogan Gunthorpe 	/* requires the mrpc_mutex to already be held when called */
132080b47deSLogan Gunthorpe 
133080b47deSLogan Gunthorpe 	struct switchtec_user *stuser;
134080b47deSLogan Gunthorpe 
135080b47deSLogan Gunthorpe 	if (stdev->mrpc_busy)
136080b47deSLogan Gunthorpe 		return;
137080b47deSLogan Gunthorpe 
138080b47deSLogan Gunthorpe 	if (list_empty(&stdev->mrpc_queue))
139080b47deSLogan Gunthorpe 		return;
140080b47deSLogan Gunthorpe 
141080b47deSLogan Gunthorpe 	stuser = list_entry(stdev->mrpc_queue.next, struct switchtec_user,
142080b47deSLogan Gunthorpe 			    list);
143080b47deSLogan Gunthorpe 
144080b47deSLogan Gunthorpe 	stuser_set_state(stuser, MRPC_RUNNING);
145080b47deSLogan Gunthorpe 	stdev->mrpc_busy = 1;
146080b47deSLogan Gunthorpe 	memcpy_toio(&stdev->mmio_mrpc->input_data,
147080b47deSLogan Gunthorpe 		    stuser->data, stuser->data_len);
148*52d8db8eSKelvin Cao 	flush_wc_buf(stdev);
149080b47deSLogan Gunthorpe 	iowrite32(stuser->cmd, &stdev->mmio_mrpc->cmd);
150080b47deSLogan Gunthorpe 
151080b47deSLogan Gunthorpe 	schedule_delayed_work(&stdev->mrpc_timeout,
152080b47deSLogan Gunthorpe 			      msecs_to_jiffies(500));
153080b47deSLogan Gunthorpe }
154080b47deSLogan Gunthorpe 
155080b47deSLogan Gunthorpe static int mrpc_queue_cmd(struct switchtec_user *stuser)
156080b47deSLogan Gunthorpe {
157080b47deSLogan Gunthorpe 	/* requires the mrpc_mutex to already be held when called */
158080b47deSLogan Gunthorpe 
159080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev = stuser->stdev;
160080b47deSLogan Gunthorpe 
161080b47deSLogan Gunthorpe 	kref_get(&stuser->kref);
162080b47deSLogan Gunthorpe 	stuser->read_len = sizeof(stuser->data);
163080b47deSLogan Gunthorpe 	stuser_set_state(stuser, MRPC_QUEUED);
164080b47deSLogan Gunthorpe 	init_completion(&stuser->comp);
165080b47deSLogan Gunthorpe 	list_add_tail(&stuser->list, &stdev->mrpc_queue);
166080b47deSLogan Gunthorpe 
167080b47deSLogan Gunthorpe 	mrpc_cmd_submit(stdev);
168080b47deSLogan Gunthorpe 
169080b47deSLogan Gunthorpe 	return 0;
170080b47deSLogan Gunthorpe }
171080b47deSLogan Gunthorpe 
172080b47deSLogan Gunthorpe static void mrpc_complete_cmd(struct switchtec_dev *stdev)
173080b47deSLogan Gunthorpe {
174080b47deSLogan Gunthorpe 	/* requires the mrpc_mutex to already be held when called */
175080b47deSLogan Gunthorpe 	struct switchtec_user *stuser;
176080b47deSLogan Gunthorpe 
177080b47deSLogan Gunthorpe 	if (list_empty(&stdev->mrpc_queue))
178080b47deSLogan Gunthorpe 		return;
179080b47deSLogan Gunthorpe 
180080b47deSLogan Gunthorpe 	stuser = list_entry(stdev->mrpc_queue.next, struct switchtec_user,
181080b47deSLogan Gunthorpe 			    list);
182080b47deSLogan Gunthorpe 
183080b47deSLogan Gunthorpe 	stuser->status = ioread32(&stdev->mmio_mrpc->status);
184080b47deSLogan Gunthorpe 	if (stuser->status == SWITCHTEC_MRPC_STATUS_INPROGRESS)
185080b47deSLogan Gunthorpe 		return;
186080b47deSLogan Gunthorpe 
187080b47deSLogan Gunthorpe 	stuser_set_state(stuser, MRPC_DONE);
188080b47deSLogan Gunthorpe 	stuser->return_code = 0;
189080b47deSLogan Gunthorpe 
190080b47deSLogan Gunthorpe 	if (stuser->status != SWITCHTEC_MRPC_STATUS_DONE)
191080b47deSLogan Gunthorpe 		goto out;
192080b47deSLogan Gunthorpe 
193080b47deSLogan Gunthorpe 	stuser->return_code = ioread32(&stdev->mmio_mrpc->ret_value);
194080b47deSLogan Gunthorpe 	if (stuser->return_code != 0)
195080b47deSLogan Gunthorpe 		goto out;
196080b47deSLogan Gunthorpe 
197080b47deSLogan Gunthorpe 	memcpy_fromio(stuser->data, &stdev->mmio_mrpc->output_data,
198080b47deSLogan Gunthorpe 		      stuser->read_len);
199080b47deSLogan Gunthorpe 
200080b47deSLogan Gunthorpe out:
201080b47deSLogan Gunthorpe 	complete_all(&stuser->comp);
202080b47deSLogan Gunthorpe 	list_del_init(&stuser->list);
203080b47deSLogan Gunthorpe 	stuser_put(stuser);
204080b47deSLogan Gunthorpe 	stdev->mrpc_busy = 0;
205080b47deSLogan Gunthorpe 
206080b47deSLogan Gunthorpe 	mrpc_cmd_submit(stdev);
207080b47deSLogan Gunthorpe }
208080b47deSLogan Gunthorpe 
209080b47deSLogan Gunthorpe static void mrpc_event_work(struct work_struct *work)
210080b47deSLogan Gunthorpe {
211080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev;
212080b47deSLogan Gunthorpe 
213080b47deSLogan Gunthorpe 	stdev = container_of(work, struct switchtec_dev, mrpc_work);
214080b47deSLogan Gunthorpe 
215080b47deSLogan Gunthorpe 	dev_dbg(&stdev->dev, "%s\n", __func__);
216080b47deSLogan Gunthorpe 
217080b47deSLogan Gunthorpe 	mutex_lock(&stdev->mrpc_mutex);
218080b47deSLogan Gunthorpe 	cancel_delayed_work(&stdev->mrpc_timeout);
219080b47deSLogan Gunthorpe 	mrpc_complete_cmd(stdev);
220080b47deSLogan Gunthorpe 	mutex_unlock(&stdev->mrpc_mutex);
221080b47deSLogan Gunthorpe }
222080b47deSLogan Gunthorpe 
223080b47deSLogan Gunthorpe static void mrpc_timeout_work(struct work_struct *work)
224080b47deSLogan Gunthorpe {
225080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev;
226080b47deSLogan Gunthorpe 	u32 status;
227080b47deSLogan Gunthorpe 
228080b47deSLogan Gunthorpe 	stdev = container_of(work, struct switchtec_dev, mrpc_timeout.work);
229080b47deSLogan Gunthorpe 
230080b47deSLogan Gunthorpe 	dev_dbg(&stdev->dev, "%s\n", __func__);
231080b47deSLogan Gunthorpe 
232080b47deSLogan Gunthorpe 	mutex_lock(&stdev->mrpc_mutex);
233080b47deSLogan Gunthorpe 
234080b47deSLogan Gunthorpe 	status = ioread32(&stdev->mmio_mrpc->status);
235080b47deSLogan Gunthorpe 	if (status == SWITCHTEC_MRPC_STATUS_INPROGRESS) {
236080b47deSLogan Gunthorpe 		schedule_delayed_work(&stdev->mrpc_timeout,
237080b47deSLogan Gunthorpe 				      msecs_to_jiffies(500));
238080b47deSLogan Gunthorpe 		goto out;
239080b47deSLogan Gunthorpe 	}
240080b47deSLogan Gunthorpe 
241080b47deSLogan Gunthorpe 	mrpc_complete_cmd(stdev);
242080b47deSLogan Gunthorpe 
243080b47deSLogan Gunthorpe out:
244080b47deSLogan Gunthorpe 	mutex_unlock(&stdev->mrpc_mutex);
245080b47deSLogan Gunthorpe }
246080b47deSLogan Gunthorpe 
2475d8e1881SLogan Gunthorpe static ssize_t device_version_show(struct device *dev,
2485d8e1881SLogan Gunthorpe 	struct device_attribute *attr, char *buf)
2495d8e1881SLogan Gunthorpe {
2505d8e1881SLogan Gunthorpe 	struct switchtec_dev *stdev = to_stdev(dev);
2515d8e1881SLogan Gunthorpe 	u32 ver;
2525d8e1881SLogan Gunthorpe 
2535d8e1881SLogan Gunthorpe 	ver = ioread32(&stdev->mmio_sys_info->device_version);
2545d8e1881SLogan Gunthorpe 
2555d8e1881SLogan Gunthorpe 	return sprintf(buf, "%x\n", ver);
2565d8e1881SLogan Gunthorpe }
2575d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(device_version);
2585d8e1881SLogan Gunthorpe 
2595d8e1881SLogan Gunthorpe static ssize_t fw_version_show(struct device *dev,
2605d8e1881SLogan Gunthorpe 	struct device_attribute *attr, char *buf)
2615d8e1881SLogan Gunthorpe {
2625d8e1881SLogan Gunthorpe 	struct switchtec_dev *stdev = to_stdev(dev);
2635d8e1881SLogan Gunthorpe 	u32 ver;
2645d8e1881SLogan Gunthorpe 
2655d8e1881SLogan Gunthorpe 	ver = ioread32(&stdev->mmio_sys_info->firmware_version);
2665d8e1881SLogan Gunthorpe 
2675d8e1881SLogan Gunthorpe 	return sprintf(buf, "%08x\n", ver);
2685d8e1881SLogan Gunthorpe }
2695d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(fw_version);
2705d8e1881SLogan Gunthorpe 
2715d8e1881SLogan Gunthorpe static ssize_t io_string_show(char *buf, void __iomem *attr, size_t len)
2725d8e1881SLogan Gunthorpe {
2735d8e1881SLogan Gunthorpe 	int i;
2745d8e1881SLogan Gunthorpe 
2755d8e1881SLogan Gunthorpe 	memcpy_fromio(buf, attr, len);
2765d8e1881SLogan Gunthorpe 	buf[len] = '\n';
2775d8e1881SLogan Gunthorpe 	buf[len + 1] = 0;
2785d8e1881SLogan Gunthorpe 
2795d8e1881SLogan Gunthorpe 	for (i = len - 1; i > 0; i--) {
2805d8e1881SLogan Gunthorpe 		if (buf[i] != ' ')
2815d8e1881SLogan Gunthorpe 			break;
2825d8e1881SLogan Gunthorpe 		buf[i] = '\n';
2835d8e1881SLogan Gunthorpe 		buf[i + 1] = 0;
2845d8e1881SLogan Gunthorpe 	}
2855d8e1881SLogan Gunthorpe 
2865d8e1881SLogan Gunthorpe 	return strlen(buf);
2875d8e1881SLogan Gunthorpe }
2885d8e1881SLogan Gunthorpe 
2895d8e1881SLogan Gunthorpe #define DEVICE_ATTR_SYS_INFO_STR(field) \
2905d8e1881SLogan Gunthorpe static ssize_t field ## _show(struct device *dev, \
2915d8e1881SLogan Gunthorpe 	struct device_attribute *attr, char *buf) \
2925d8e1881SLogan Gunthorpe { \
2935d8e1881SLogan Gunthorpe 	struct switchtec_dev *stdev = to_stdev(dev); \
2945d8e1881SLogan Gunthorpe 	return io_string_show(buf, &stdev->mmio_sys_info->field, \
2955d8e1881SLogan Gunthorpe 			    sizeof(stdev->mmio_sys_info->field)); \
2965d8e1881SLogan Gunthorpe } \
2975d8e1881SLogan Gunthorpe \
2985d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(field)
2995d8e1881SLogan Gunthorpe 
3005d8e1881SLogan Gunthorpe DEVICE_ATTR_SYS_INFO_STR(vendor_id);
3015d8e1881SLogan Gunthorpe DEVICE_ATTR_SYS_INFO_STR(product_id);
3025d8e1881SLogan Gunthorpe DEVICE_ATTR_SYS_INFO_STR(product_revision);
3035d8e1881SLogan Gunthorpe DEVICE_ATTR_SYS_INFO_STR(component_vendor);
3045d8e1881SLogan Gunthorpe 
3055d8e1881SLogan Gunthorpe static ssize_t component_id_show(struct device *dev,
3065d8e1881SLogan Gunthorpe 	struct device_attribute *attr, char *buf)
3075d8e1881SLogan Gunthorpe {
3085d8e1881SLogan Gunthorpe 	struct switchtec_dev *stdev = to_stdev(dev);
3095d8e1881SLogan Gunthorpe 	int id = ioread16(&stdev->mmio_sys_info->component_id);
3105d8e1881SLogan Gunthorpe 
3115d8e1881SLogan Gunthorpe 	return sprintf(buf, "PM%04X\n", id);
3125d8e1881SLogan Gunthorpe }
3135d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(component_id);
3145d8e1881SLogan Gunthorpe 
3155d8e1881SLogan Gunthorpe static ssize_t component_revision_show(struct device *dev,
3165d8e1881SLogan Gunthorpe 	struct device_attribute *attr, char *buf)
3175d8e1881SLogan Gunthorpe {
3185d8e1881SLogan Gunthorpe 	struct switchtec_dev *stdev = to_stdev(dev);
3195d8e1881SLogan Gunthorpe 	int rev = ioread8(&stdev->mmio_sys_info->component_revision);
3205d8e1881SLogan Gunthorpe 
3215d8e1881SLogan Gunthorpe 	return sprintf(buf, "%d\n", rev);
3225d8e1881SLogan Gunthorpe }
3235d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(component_revision);
3245d8e1881SLogan Gunthorpe 
3255d8e1881SLogan Gunthorpe static ssize_t partition_show(struct device *dev,
3265d8e1881SLogan Gunthorpe 	struct device_attribute *attr, char *buf)
3275d8e1881SLogan Gunthorpe {
3285d8e1881SLogan Gunthorpe 	struct switchtec_dev *stdev = to_stdev(dev);
3295d8e1881SLogan Gunthorpe 
3305d8e1881SLogan Gunthorpe 	return sprintf(buf, "%d\n", stdev->partition);
3315d8e1881SLogan Gunthorpe }
3325d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(partition);
3335d8e1881SLogan Gunthorpe 
3345d8e1881SLogan Gunthorpe static ssize_t partition_count_show(struct device *dev,
3355d8e1881SLogan Gunthorpe 	struct device_attribute *attr, char *buf)
3365d8e1881SLogan Gunthorpe {
3375d8e1881SLogan Gunthorpe 	struct switchtec_dev *stdev = to_stdev(dev);
3385d8e1881SLogan Gunthorpe 
3395d8e1881SLogan Gunthorpe 	return sprintf(buf, "%d\n", stdev->partition_count);
3405d8e1881SLogan Gunthorpe }
3415d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(partition_count);
3425d8e1881SLogan Gunthorpe 
3435d8e1881SLogan Gunthorpe static struct attribute *switchtec_device_attrs[] = {
3445d8e1881SLogan Gunthorpe 	&dev_attr_device_version.attr,
3455d8e1881SLogan Gunthorpe 	&dev_attr_fw_version.attr,
3465d8e1881SLogan Gunthorpe 	&dev_attr_vendor_id.attr,
3475d8e1881SLogan Gunthorpe 	&dev_attr_product_id.attr,
3485d8e1881SLogan Gunthorpe 	&dev_attr_product_revision.attr,
3495d8e1881SLogan Gunthorpe 	&dev_attr_component_vendor.attr,
3505d8e1881SLogan Gunthorpe 	&dev_attr_component_id.attr,
3515d8e1881SLogan Gunthorpe 	&dev_attr_component_revision.attr,
3525d8e1881SLogan Gunthorpe 	&dev_attr_partition.attr,
3535d8e1881SLogan Gunthorpe 	&dev_attr_partition_count.attr,
3545d8e1881SLogan Gunthorpe 	NULL,
3555d8e1881SLogan Gunthorpe };
3565d8e1881SLogan Gunthorpe 
3575d8e1881SLogan Gunthorpe ATTRIBUTE_GROUPS(switchtec_device);
3585d8e1881SLogan Gunthorpe 
359080b47deSLogan Gunthorpe static int switchtec_dev_open(struct inode *inode, struct file *filp)
360080b47deSLogan Gunthorpe {
361080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev;
362080b47deSLogan Gunthorpe 	struct switchtec_user *stuser;
363080b47deSLogan Gunthorpe 
364080b47deSLogan Gunthorpe 	stdev = container_of(inode->i_cdev, struct switchtec_dev, cdev);
365080b47deSLogan Gunthorpe 
366080b47deSLogan Gunthorpe 	stuser = stuser_create(stdev);
367080b47deSLogan Gunthorpe 	if (IS_ERR(stuser))
368080b47deSLogan Gunthorpe 		return PTR_ERR(stuser);
369080b47deSLogan Gunthorpe 
370080b47deSLogan Gunthorpe 	filp->private_data = stuser;
371080b47deSLogan Gunthorpe 	nonseekable_open(inode, filp);
372080b47deSLogan Gunthorpe 
373080b47deSLogan Gunthorpe 	dev_dbg(&stdev->dev, "%s: %p\n", __func__, stuser);
374080b47deSLogan Gunthorpe 
375080b47deSLogan Gunthorpe 	return 0;
376080b47deSLogan Gunthorpe }
377080b47deSLogan Gunthorpe 
378080b47deSLogan Gunthorpe static int switchtec_dev_release(struct inode *inode, struct file *filp)
379080b47deSLogan Gunthorpe {
380080b47deSLogan Gunthorpe 	struct switchtec_user *stuser = filp->private_data;
381080b47deSLogan Gunthorpe 
382080b47deSLogan Gunthorpe 	stuser_put(stuser);
383080b47deSLogan Gunthorpe 
384080b47deSLogan Gunthorpe 	return 0;
385080b47deSLogan Gunthorpe }
386080b47deSLogan Gunthorpe 
387080b47deSLogan Gunthorpe static int lock_mutex_and_test_alive(struct switchtec_dev *stdev)
388080b47deSLogan Gunthorpe {
389080b47deSLogan Gunthorpe 	if (mutex_lock_interruptible(&stdev->mrpc_mutex))
390080b47deSLogan Gunthorpe 		return -EINTR;
391080b47deSLogan Gunthorpe 
392080b47deSLogan Gunthorpe 	if (!stdev->alive) {
393080b47deSLogan Gunthorpe 		mutex_unlock(&stdev->mrpc_mutex);
394080b47deSLogan Gunthorpe 		return -ENODEV;
395080b47deSLogan Gunthorpe 	}
396080b47deSLogan Gunthorpe 
397080b47deSLogan Gunthorpe 	return 0;
398080b47deSLogan Gunthorpe }
399080b47deSLogan Gunthorpe 
400080b47deSLogan Gunthorpe static ssize_t switchtec_dev_write(struct file *filp, const char __user *data,
401080b47deSLogan Gunthorpe 				   size_t size, loff_t *off)
402080b47deSLogan Gunthorpe {
403080b47deSLogan Gunthorpe 	struct switchtec_user *stuser = filp->private_data;
404080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev = stuser->stdev;
405080b47deSLogan Gunthorpe 	int rc;
406080b47deSLogan Gunthorpe 
407080b47deSLogan Gunthorpe 	if (size < sizeof(stuser->cmd) ||
408080b47deSLogan Gunthorpe 	    size > sizeof(stuser->cmd) + sizeof(stuser->data))
409080b47deSLogan Gunthorpe 		return -EINVAL;
410080b47deSLogan Gunthorpe 
411080b47deSLogan Gunthorpe 	stuser->data_len = size - sizeof(stuser->cmd);
412080b47deSLogan Gunthorpe 
413080b47deSLogan Gunthorpe 	rc = lock_mutex_and_test_alive(stdev);
414080b47deSLogan Gunthorpe 	if (rc)
415080b47deSLogan Gunthorpe 		return rc;
416080b47deSLogan Gunthorpe 
417080b47deSLogan Gunthorpe 	if (stuser->state != MRPC_IDLE) {
418080b47deSLogan Gunthorpe 		rc = -EBADE;
419080b47deSLogan Gunthorpe 		goto out;
420080b47deSLogan Gunthorpe 	}
421080b47deSLogan Gunthorpe 
422080b47deSLogan Gunthorpe 	rc = copy_from_user(&stuser->cmd, data, sizeof(stuser->cmd));
423080b47deSLogan Gunthorpe 	if (rc) {
424080b47deSLogan Gunthorpe 		rc = -EFAULT;
425080b47deSLogan Gunthorpe 		goto out;
426080b47deSLogan Gunthorpe 	}
427080b47deSLogan Gunthorpe 
428080b47deSLogan Gunthorpe 	data += sizeof(stuser->cmd);
429080b47deSLogan Gunthorpe 	rc = copy_from_user(&stuser->data, data, size - sizeof(stuser->cmd));
430080b47deSLogan Gunthorpe 	if (rc) {
431080b47deSLogan Gunthorpe 		rc = -EFAULT;
432080b47deSLogan Gunthorpe 		goto out;
433080b47deSLogan Gunthorpe 	}
434080b47deSLogan Gunthorpe 
435080b47deSLogan Gunthorpe 	rc = mrpc_queue_cmd(stuser);
436080b47deSLogan Gunthorpe 
437080b47deSLogan Gunthorpe out:
438080b47deSLogan Gunthorpe 	mutex_unlock(&stdev->mrpc_mutex);
439080b47deSLogan Gunthorpe 
440080b47deSLogan Gunthorpe 	if (rc)
441080b47deSLogan Gunthorpe 		return rc;
442080b47deSLogan Gunthorpe 
443080b47deSLogan Gunthorpe 	return size;
444080b47deSLogan Gunthorpe }
445080b47deSLogan Gunthorpe 
446080b47deSLogan Gunthorpe static ssize_t switchtec_dev_read(struct file *filp, char __user *data,
447080b47deSLogan Gunthorpe 				  size_t size, loff_t *off)
448080b47deSLogan Gunthorpe {
449080b47deSLogan Gunthorpe 	struct switchtec_user *stuser = filp->private_data;
450080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev = stuser->stdev;
451080b47deSLogan Gunthorpe 	int rc;
452080b47deSLogan Gunthorpe 
453080b47deSLogan Gunthorpe 	if (size < sizeof(stuser->cmd) ||
454080b47deSLogan Gunthorpe 	    size > sizeof(stuser->cmd) + sizeof(stuser->data))
455080b47deSLogan Gunthorpe 		return -EINVAL;
456080b47deSLogan Gunthorpe 
457080b47deSLogan Gunthorpe 	rc = lock_mutex_and_test_alive(stdev);
458080b47deSLogan Gunthorpe 	if (rc)
459080b47deSLogan Gunthorpe 		return rc;
460080b47deSLogan Gunthorpe 
461080b47deSLogan Gunthorpe 	if (stuser->state == MRPC_IDLE) {
462080b47deSLogan Gunthorpe 		mutex_unlock(&stdev->mrpc_mutex);
463080b47deSLogan Gunthorpe 		return -EBADE;
464080b47deSLogan Gunthorpe 	}
465080b47deSLogan Gunthorpe 
466080b47deSLogan Gunthorpe 	stuser->read_len = size - sizeof(stuser->return_code);
467080b47deSLogan Gunthorpe 
468080b47deSLogan Gunthorpe 	mutex_unlock(&stdev->mrpc_mutex);
469080b47deSLogan Gunthorpe 
470080b47deSLogan Gunthorpe 	if (filp->f_flags & O_NONBLOCK) {
471080b47deSLogan Gunthorpe 		if (!try_wait_for_completion(&stuser->comp))
472080b47deSLogan Gunthorpe 			return -EAGAIN;
473080b47deSLogan Gunthorpe 	} else {
474080b47deSLogan Gunthorpe 		rc = wait_for_completion_interruptible(&stuser->comp);
475080b47deSLogan Gunthorpe 		if (rc < 0)
476080b47deSLogan Gunthorpe 			return rc;
477080b47deSLogan Gunthorpe 	}
478080b47deSLogan Gunthorpe 
479080b47deSLogan Gunthorpe 	rc = lock_mutex_and_test_alive(stdev);
480080b47deSLogan Gunthorpe 	if (rc)
481080b47deSLogan Gunthorpe 		return rc;
482080b47deSLogan Gunthorpe 
483080b47deSLogan Gunthorpe 	if (stuser->state != MRPC_DONE) {
484080b47deSLogan Gunthorpe 		mutex_unlock(&stdev->mrpc_mutex);
485080b47deSLogan Gunthorpe 		return -EBADE;
486080b47deSLogan Gunthorpe 	}
487080b47deSLogan Gunthorpe 
488080b47deSLogan Gunthorpe 	rc = copy_to_user(data, &stuser->return_code,
489080b47deSLogan Gunthorpe 			  sizeof(stuser->return_code));
490080b47deSLogan Gunthorpe 	if (rc) {
491080b47deSLogan Gunthorpe 		rc = -EFAULT;
492080b47deSLogan Gunthorpe 		goto out;
493080b47deSLogan Gunthorpe 	}
494080b47deSLogan Gunthorpe 
495080b47deSLogan Gunthorpe 	data += sizeof(stuser->return_code);
496080b47deSLogan Gunthorpe 	rc = copy_to_user(data, &stuser->data,
497080b47deSLogan Gunthorpe 			  size - sizeof(stuser->return_code));
498080b47deSLogan Gunthorpe 	if (rc) {
499080b47deSLogan Gunthorpe 		rc = -EFAULT;
500080b47deSLogan Gunthorpe 		goto out;
501080b47deSLogan Gunthorpe 	}
502080b47deSLogan Gunthorpe 
503080b47deSLogan Gunthorpe 	stuser_set_state(stuser, MRPC_IDLE);
504080b47deSLogan Gunthorpe 
505080b47deSLogan Gunthorpe out:
506080b47deSLogan Gunthorpe 	mutex_unlock(&stdev->mrpc_mutex);
507080b47deSLogan Gunthorpe 
508080b47deSLogan Gunthorpe 	if (stuser->status == SWITCHTEC_MRPC_STATUS_DONE)
509080b47deSLogan Gunthorpe 		return size;
510080b47deSLogan Gunthorpe 	else if (stuser->status == SWITCHTEC_MRPC_STATUS_INTERRUPTED)
511080b47deSLogan Gunthorpe 		return -ENXIO;
512080b47deSLogan Gunthorpe 	else
513080b47deSLogan Gunthorpe 		return -EBADMSG;
514080b47deSLogan Gunthorpe }
515080b47deSLogan Gunthorpe 
516afc9a42bSAl Viro static __poll_t switchtec_dev_poll(struct file *filp, poll_table *wait)
517080b47deSLogan Gunthorpe {
518080b47deSLogan Gunthorpe 	struct switchtec_user *stuser = filp->private_data;
519080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev = stuser->stdev;
520afc9a42bSAl Viro 	__poll_t ret = 0;
521080b47deSLogan Gunthorpe 
522080b47deSLogan Gunthorpe 	poll_wait(filp, &stuser->comp.wait, wait);
523080b47deSLogan Gunthorpe 	poll_wait(filp, &stdev->event_wq, wait);
524080b47deSLogan Gunthorpe 
525080b47deSLogan Gunthorpe 	if (lock_mutex_and_test_alive(stdev))
526a9a08845SLinus Torvalds 		return EPOLLIN | EPOLLRDHUP | EPOLLOUT | EPOLLERR | EPOLLHUP;
527080b47deSLogan Gunthorpe 
528080b47deSLogan Gunthorpe 	mutex_unlock(&stdev->mrpc_mutex);
529080b47deSLogan Gunthorpe 
530080b47deSLogan Gunthorpe 	if (try_wait_for_completion(&stuser->comp))
531a9a08845SLinus Torvalds 		ret |= EPOLLIN | EPOLLRDNORM;
532080b47deSLogan Gunthorpe 
533080b47deSLogan Gunthorpe 	if (stuser->event_cnt != atomic_read(&stdev->event_cnt))
534a9a08845SLinus Torvalds 		ret |= EPOLLPRI | EPOLLRDBAND;
535080b47deSLogan Gunthorpe 
536080b47deSLogan Gunthorpe 	return ret;
537080b47deSLogan Gunthorpe }
538080b47deSLogan Gunthorpe 
53952eabba5SLogan Gunthorpe static int ioctl_flash_info(struct switchtec_dev *stdev,
54052eabba5SLogan Gunthorpe 			    struct switchtec_ioctl_flash_info __user *uinfo)
54152eabba5SLogan Gunthorpe {
54252eabba5SLogan Gunthorpe 	struct switchtec_ioctl_flash_info info = {0};
54352eabba5SLogan Gunthorpe 	struct flash_info_regs __iomem *fi = stdev->mmio_flash_info;
54452eabba5SLogan Gunthorpe 
54552eabba5SLogan Gunthorpe 	info.flash_length = ioread32(&fi->flash_length);
54652eabba5SLogan Gunthorpe 	info.num_partitions = SWITCHTEC_IOCTL_NUM_PARTITIONS;
54752eabba5SLogan Gunthorpe 
54852eabba5SLogan Gunthorpe 	if (copy_to_user(uinfo, &info, sizeof(info)))
54952eabba5SLogan Gunthorpe 		return -EFAULT;
55052eabba5SLogan Gunthorpe 
55152eabba5SLogan Gunthorpe 	return 0;
55252eabba5SLogan Gunthorpe }
55352eabba5SLogan Gunthorpe 
55452eabba5SLogan Gunthorpe static void set_fw_info_part(struct switchtec_ioctl_flash_part_info *info,
55552eabba5SLogan Gunthorpe 			     struct partition_info __iomem *pi)
55652eabba5SLogan Gunthorpe {
55752eabba5SLogan Gunthorpe 	info->address = ioread32(&pi->address);
55852eabba5SLogan Gunthorpe 	info->length = ioread32(&pi->length);
55952eabba5SLogan Gunthorpe }
56052eabba5SLogan Gunthorpe 
56152eabba5SLogan Gunthorpe static int ioctl_flash_part_info(struct switchtec_dev *stdev,
56252eabba5SLogan Gunthorpe 	struct switchtec_ioctl_flash_part_info __user *uinfo)
56352eabba5SLogan Gunthorpe {
56452eabba5SLogan Gunthorpe 	struct switchtec_ioctl_flash_part_info info = {0};
56552eabba5SLogan Gunthorpe 	struct flash_info_regs __iomem *fi = stdev->mmio_flash_info;
566079e3bc5SLogan Gunthorpe 	struct sys_info_regs __iomem *si = stdev->mmio_sys_info;
56752eabba5SLogan Gunthorpe 	u32 active_addr = -1;
56852eabba5SLogan Gunthorpe 
56952eabba5SLogan Gunthorpe 	if (copy_from_user(&info, uinfo, sizeof(info)))
57052eabba5SLogan Gunthorpe 		return -EFAULT;
57152eabba5SLogan Gunthorpe 
57252eabba5SLogan Gunthorpe 	switch (info.flash_partition) {
57352eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_CFG0:
57452eabba5SLogan Gunthorpe 		active_addr = ioread32(&fi->active_cfg);
57552eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->cfg0);
576079e3bc5SLogan Gunthorpe 		if (ioread16(&si->cfg_running) == SWITCHTEC_CFG0_RUNNING)
577079e3bc5SLogan Gunthorpe 			info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
57852eabba5SLogan Gunthorpe 		break;
57952eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_CFG1:
58052eabba5SLogan Gunthorpe 		active_addr = ioread32(&fi->active_cfg);
58152eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->cfg1);
582079e3bc5SLogan Gunthorpe 		if (ioread16(&si->cfg_running) == SWITCHTEC_CFG1_RUNNING)
583079e3bc5SLogan Gunthorpe 			info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
58452eabba5SLogan Gunthorpe 		break;
58552eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_IMG0:
58652eabba5SLogan Gunthorpe 		active_addr = ioread32(&fi->active_img);
58752eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->img0);
588079e3bc5SLogan Gunthorpe 		if (ioread16(&si->img_running) == SWITCHTEC_IMG0_RUNNING)
589079e3bc5SLogan Gunthorpe 			info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
59052eabba5SLogan Gunthorpe 		break;
59152eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_IMG1:
59252eabba5SLogan Gunthorpe 		active_addr = ioread32(&fi->active_img);
59352eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->img1);
594079e3bc5SLogan Gunthorpe 		if (ioread16(&si->img_running) == SWITCHTEC_IMG1_RUNNING)
595079e3bc5SLogan Gunthorpe 			info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
59652eabba5SLogan Gunthorpe 		break;
59752eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_NVLOG:
59852eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->nvlog);
59952eabba5SLogan Gunthorpe 		break;
60052eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_VENDOR0:
60152eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->vendor[0]);
60252eabba5SLogan Gunthorpe 		break;
60352eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_VENDOR1:
60452eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->vendor[1]);
60552eabba5SLogan Gunthorpe 		break;
60652eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_VENDOR2:
60752eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->vendor[2]);
60852eabba5SLogan Gunthorpe 		break;
60952eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_VENDOR3:
61052eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->vendor[3]);
61152eabba5SLogan Gunthorpe 		break;
61252eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_VENDOR4:
61352eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->vendor[4]);
61452eabba5SLogan Gunthorpe 		break;
61552eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_VENDOR5:
61652eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->vendor[5]);
61752eabba5SLogan Gunthorpe 		break;
61852eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_VENDOR6:
61952eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->vendor[6]);
62052eabba5SLogan Gunthorpe 		break;
62152eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_VENDOR7:
62252eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->vendor[7]);
62352eabba5SLogan Gunthorpe 		break;
62452eabba5SLogan Gunthorpe 	default:
62552eabba5SLogan Gunthorpe 		return -EINVAL;
62652eabba5SLogan Gunthorpe 	}
62752eabba5SLogan Gunthorpe 
62852eabba5SLogan Gunthorpe 	if (info.address == active_addr)
629079e3bc5SLogan Gunthorpe 		info.active |= SWITCHTEC_IOCTL_PART_ACTIVE;
63052eabba5SLogan Gunthorpe 
63152eabba5SLogan Gunthorpe 	if (copy_to_user(uinfo, &info, sizeof(info)))
63252eabba5SLogan Gunthorpe 		return -EFAULT;
63352eabba5SLogan Gunthorpe 
63452eabba5SLogan Gunthorpe 	return 0;
63552eabba5SLogan Gunthorpe }
63652eabba5SLogan Gunthorpe 
63752eabba5SLogan Gunthorpe static int ioctl_event_summary(struct switchtec_dev *stdev,
63852eabba5SLogan Gunthorpe 	struct switchtec_user *stuser,
63952eabba5SLogan Gunthorpe 	struct switchtec_ioctl_event_summary __user *usum)
64052eabba5SLogan Gunthorpe {
64152eabba5SLogan Gunthorpe 	struct switchtec_ioctl_event_summary s = {0};
64252eabba5SLogan Gunthorpe 	int i;
64352eabba5SLogan Gunthorpe 	u32 reg;
64452eabba5SLogan Gunthorpe 
64552eabba5SLogan Gunthorpe 	s.global = ioread32(&stdev->mmio_sw_event->global_summary);
64652eabba5SLogan Gunthorpe 	s.part_bitmap = ioread32(&stdev->mmio_sw_event->part_event_bitmap);
64752eabba5SLogan Gunthorpe 	s.local_part = ioread32(&stdev->mmio_part_cfg->part_event_summary);
64852eabba5SLogan Gunthorpe 
64952eabba5SLogan Gunthorpe 	for (i = 0; i < stdev->partition_count; i++) {
65052eabba5SLogan Gunthorpe 		reg = ioread32(&stdev->mmio_part_cfg_all[i].part_event_summary);
65152eabba5SLogan Gunthorpe 		s.part[i] = reg;
65252eabba5SLogan Gunthorpe 	}
65352eabba5SLogan Gunthorpe 
65452eabba5SLogan Gunthorpe 	for (i = 0; i < SWITCHTEC_MAX_PFF_CSR; i++) {
65552eabba5SLogan Gunthorpe 		reg = ioread16(&stdev->mmio_pff_csr[i].vendor_id);
656cfdfc14eSDoug Meyer 		if (reg != PCI_VENDOR_ID_MICROSEMI)
65752eabba5SLogan Gunthorpe 			break;
65852eabba5SLogan Gunthorpe 
65952eabba5SLogan Gunthorpe 		reg = ioread32(&stdev->mmio_pff_csr[i].pff_event_summary);
66052eabba5SLogan Gunthorpe 		s.pff[i] = reg;
66152eabba5SLogan Gunthorpe 	}
66252eabba5SLogan Gunthorpe 
66352eabba5SLogan Gunthorpe 	if (copy_to_user(usum, &s, sizeof(s)))
66452eabba5SLogan Gunthorpe 		return -EFAULT;
66552eabba5SLogan Gunthorpe 
66652eabba5SLogan Gunthorpe 	stuser->event_cnt = atomic_read(&stdev->event_cnt);
66752eabba5SLogan Gunthorpe 
66852eabba5SLogan Gunthorpe 	return 0;
66952eabba5SLogan Gunthorpe }
67052eabba5SLogan Gunthorpe 
67152eabba5SLogan Gunthorpe static u32 __iomem *global_ev_reg(struct switchtec_dev *stdev,
67252eabba5SLogan Gunthorpe 				  size_t offset, int index)
67352eabba5SLogan Gunthorpe {
67452eabba5SLogan Gunthorpe 	return (void __iomem *)stdev->mmio_sw_event + offset;
67552eabba5SLogan Gunthorpe }
67652eabba5SLogan Gunthorpe 
67752eabba5SLogan Gunthorpe static u32 __iomem *part_ev_reg(struct switchtec_dev *stdev,
67852eabba5SLogan Gunthorpe 				size_t offset, int index)
67952eabba5SLogan Gunthorpe {
68052eabba5SLogan Gunthorpe 	return (void __iomem *)&stdev->mmio_part_cfg_all[index] + offset;
68152eabba5SLogan Gunthorpe }
68252eabba5SLogan Gunthorpe 
68352eabba5SLogan Gunthorpe static u32 __iomem *pff_ev_reg(struct switchtec_dev *stdev,
68452eabba5SLogan Gunthorpe 			       size_t offset, int index)
68552eabba5SLogan Gunthorpe {
68652eabba5SLogan Gunthorpe 	return (void __iomem *)&stdev->mmio_pff_csr[index] + offset;
68752eabba5SLogan Gunthorpe }
68852eabba5SLogan Gunthorpe 
68952eabba5SLogan Gunthorpe #define EV_GLB(i, r)[i] = {offsetof(struct sw_event_regs, r), global_ev_reg}
69052eabba5SLogan Gunthorpe #define EV_PAR(i, r)[i] = {offsetof(struct part_cfg_regs, r), part_ev_reg}
69152eabba5SLogan Gunthorpe #define EV_PFF(i, r)[i] = {offsetof(struct pff_csr_regs, r), pff_ev_reg}
69252eabba5SLogan Gunthorpe 
693f05f7355SColin Ian King static const struct event_reg {
69452eabba5SLogan Gunthorpe 	size_t offset;
69552eabba5SLogan Gunthorpe 	u32 __iomem *(*map_reg)(struct switchtec_dev *stdev,
69652eabba5SLogan Gunthorpe 				size_t offset, int index);
69752eabba5SLogan Gunthorpe } event_regs[] = {
69852eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_STACK_ERROR, stack_error_event_hdr),
69952eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_PPU_ERROR, ppu_error_event_hdr),
70052eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_ISP_ERROR, isp_error_event_hdr),
70152eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_SYS_RESET, sys_reset_event_hdr),
70252eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_EXC, fw_exception_hdr),
70352eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_NMI, fw_nmi_hdr),
70452eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_NON_FATAL, fw_non_fatal_hdr),
70552eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_FATAL, fw_fatal_hdr),
70652eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_TWI_MRPC_COMP, twi_mrpc_comp_hdr),
70752eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_TWI_MRPC_COMP_ASYNC,
70852eabba5SLogan Gunthorpe 	       twi_mrpc_comp_async_hdr),
70952eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_CLI_MRPC_COMP, cli_mrpc_comp_hdr),
71052eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_CLI_MRPC_COMP_ASYNC,
71152eabba5SLogan Gunthorpe 	       cli_mrpc_comp_async_hdr),
71252eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_GPIO_INT, gpio_interrupt_hdr),
713f0edce7aSLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_GFMS, gfms_event_hdr),
71452eabba5SLogan Gunthorpe 	EV_PAR(SWITCHTEC_IOCTL_EVENT_PART_RESET, part_reset_hdr),
71552eabba5SLogan Gunthorpe 	EV_PAR(SWITCHTEC_IOCTL_EVENT_MRPC_COMP, mrpc_comp_hdr),
71652eabba5SLogan Gunthorpe 	EV_PAR(SWITCHTEC_IOCTL_EVENT_MRPC_COMP_ASYNC, mrpc_comp_async_hdr),
71752eabba5SLogan Gunthorpe 	EV_PAR(SWITCHTEC_IOCTL_EVENT_DYN_PART_BIND_COMP, dyn_binding_hdr),
71852eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_AER_IN_P2P, aer_in_p2p_hdr),
71952eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_AER_IN_VEP, aer_in_vep_hdr),
72052eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_DPC, dpc_hdr),
72152eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_CTS, cts_hdr),
72252eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_HOTPLUG, hotplug_hdr),
72352eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_IER, ier_hdr),
72452eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_THRESH, threshold_hdr),
72552eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_POWER_MGMT, power_mgmt_hdr),
72652eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_TLP_THROTTLING, tlp_throttling_hdr),
72752eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_FORCE_SPEED, force_speed_hdr),
72852eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_CREDIT_TIMEOUT, credit_timeout_hdr),
72952eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_LINK_STATE, link_state_hdr),
73052eabba5SLogan Gunthorpe };
73152eabba5SLogan Gunthorpe 
73252eabba5SLogan Gunthorpe static u32 __iomem *event_hdr_addr(struct switchtec_dev *stdev,
73352eabba5SLogan Gunthorpe 				   int event_id, int index)
73452eabba5SLogan Gunthorpe {
73552eabba5SLogan Gunthorpe 	size_t off;
73652eabba5SLogan Gunthorpe 
73752eabba5SLogan Gunthorpe 	if (event_id < 0 || event_id >= SWITCHTEC_IOCTL_MAX_EVENTS)
73852eabba5SLogan Gunthorpe 		return ERR_PTR(-EINVAL);
73952eabba5SLogan Gunthorpe 
74052eabba5SLogan Gunthorpe 	off = event_regs[event_id].offset;
74152eabba5SLogan Gunthorpe 
74252eabba5SLogan Gunthorpe 	if (event_regs[event_id].map_reg == part_ev_reg) {
74352eabba5SLogan Gunthorpe 		if (index == SWITCHTEC_IOCTL_EVENT_LOCAL_PART_IDX)
74452eabba5SLogan Gunthorpe 			index = stdev->partition;
74552eabba5SLogan Gunthorpe 		else if (index < 0 || index >= stdev->partition_count)
74652eabba5SLogan Gunthorpe 			return ERR_PTR(-EINVAL);
74752eabba5SLogan Gunthorpe 	} else if (event_regs[event_id].map_reg == pff_ev_reg) {
74852eabba5SLogan Gunthorpe 		if (index < 0 || index >= stdev->pff_csr_count)
74952eabba5SLogan Gunthorpe 			return ERR_PTR(-EINVAL);
75052eabba5SLogan Gunthorpe 	}
75152eabba5SLogan Gunthorpe 
75252eabba5SLogan Gunthorpe 	return event_regs[event_id].map_reg(stdev, off, index);
75352eabba5SLogan Gunthorpe }
75452eabba5SLogan Gunthorpe 
75552eabba5SLogan Gunthorpe static int event_ctl(struct switchtec_dev *stdev,
75652eabba5SLogan Gunthorpe 		     struct switchtec_ioctl_event_ctl *ctl)
75752eabba5SLogan Gunthorpe {
75852eabba5SLogan Gunthorpe 	int i;
75952eabba5SLogan Gunthorpe 	u32 __iomem *reg;
76052eabba5SLogan Gunthorpe 	u32 hdr;
76152eabba5SLogan Gunthorpe 
76252eabba5SLogan Gunthorpe 	reg = event_hdr_addr(stdev, ctl->event_id, ctl->index);
76352eabba5SLogan Gunthorpe 	if (IS_ERR(reg))
76452eabba5SLogan Gunthorpe 		return PTR_ERR(reg);
76552eabba5SLogan Gunthorpe 
76652eabba5SLogan Gunthorpe 	hdr = ioread32(reg);
76752eabba5SLogan Gunthorpe 	for (i = 0; i < ARRAY_SIZE(ctl->data); i++)
76852eabba5SLogan Gunthorpe 		ctl->data[i] = ioread32(&reg[i + 1]);
76952eabba5SLogan Gunthorpe 
77052eabba5SLogan Gunthorpe 	ctl->occurred = hdr & SWITCHTEC_EVENT_OCCURRED;
77152eabba5SLogan Gunthorpe 	ctl->count = (hdr >> 5) & 0xFF;
77252eabba5SLogan Gunthorpe 
77352eabba5SLogan Gunthorpe 	if (!(ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_CLEAR))
77452eabba5SLogan Gunthorpe 		hdr &= ~SWITCHTEC_EVENT_CLEAR;
77552eabba5SLogan Gunthorpe 	if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL)
77652eabba5SLogan Gunthorpe 		hdr |= SWITCHTEC_EVENT_EN_IRQ;
77752eabba5SLogan Gunthorpe 	if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_POLL)
77852eabba5SLogan Gunthorpe 		hdr &= ~SWITCHTEC_EVENT_EN_IRQ;
77952eabba5SLogan Gunthorpe 	if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG)
78052eabba5SLogan Gunthorpe 		hdr |= SWITCHTEC_EVENT_EN_LOG;
78152eabba5SLogan Gunthorpe 	if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_LOG)
78252eabba5SLogan Gunthorpe 		hdr &= ~SWITCHTEC_EVENT_EN_LOG;
78352eabba5SLogan Gunthorpe 	if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI)
78452eabba5SLogan Gunthorpe 		hdr |= SWITCHTEC_EVENT_EN_CLI;
78552eabba5SLogan Gunthorpe 	if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_CLI)
78652eabba5SLogan Gunthorpe 		hdr &= ~SWITCHTEC_EVENT_EN_CLI;
78752eabba5SLogan Gunthorpe 	if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL)
78852eabba5SLogan Gunthorpe 		hdr |= SWITCHTEC_EVENT_FATAL;
78952eabba5SLogan Gunthorpe 	if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_FATAL)
79052eabba5SLogan Gunthorpe 		hdr &= ~SWITCHTEC_EVENT_FATAL;
79152eabba5SLogan Gunthorpe 
79252eabba5SLogan Gunthorpe 	if (ctl->flags)
79352eabba5SLogan Gunthorpe 		iowrite32(hdr, reg);
79452eabba5SLogan Gunthorpe 
79552eabba5SLogan Gunthorpe 	ctl->flags = 0;
79652eabba5SLogan Gunthorpe 	if (hdr & SWITCHTEC_EVENT_EN_IRQ)
79752eabba5SLogan Gunthorpe 		ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL;
79852eabba5SLogan Gunthorpe 	if (hdr & SWITCHTEC_EVENT_EN_LOG)
79952eabba5SLogan Gunthorpe 		ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG;
80052eabba5SLogan Gunthorpe 	if (hdr & SWITCHTEC_EVENT_EN_CLI)
80152eabba5SLogan Gunthorpe 		ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI;
80252eabba5SLogan Gunthorpe 	if (hdr & SWITCHTEC_EVENT_FATAL)
80352eabba5SLogan Gunthorpe 		ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL;
80452eabba5SLogan Gunthorpe 
80552eabba5SLogan Gunthorpe 	return 0;
80652eabba5SLogan Gunthorpe }
80752eabba5SLogan Gunthorpe 
80852eabba5SLogan Gunthorpe static int ioctl_event_ctl(struct switchtec_dev *stdev,
80952eabba5SLogan Gunthorpe 	struct switchtec_ioctl_event_ctl __user *uctl)
81052eabba5SLogan Gunthorpe {
81152eabba5SLogan Gunthorpe 	int ret;
81252eabba5SLogan Gunthorpe 	int nr_idxs;
813e4a7dca5SJoey Zhang 	unsigned int event_flags;
81452eabba5SLogan Gunthorpe 	struct switchtec_ioctl_event_ctl ctl;
81552eabba5SLogan Gunthorpe 
81652eabba5SLogan Gunthorpe 	if (copy_from_user(&ctl, uctl, sizeof(ctl)))
81752eabba5SLogan Gunthorpe 		return -EFAULT;
81852eabba5SLogan Gunthorpe 
81952eabba5SLogan Gunthorpe 	if (ctl.event_id >= SWITCHTEC_IOCTL_MAX_EVENTS)
82052eabba5SLogan Gunthorpe 		return -EINVAL;
82152eabba5SLogan Gunthorpe 
82252eabba5SLogan Gunthorpe 	if (ctl.flags & SWITCHTEC_IOCTL_EVENT_FLAG_UNUSED)
82352eabba5SLogan Gunthorpe 		return -EINVAL;
82452eabba5SLogan Gunthorpe 
82552eabba5SLogan Gunthorpe 	if (ctl.index == SWITCHTEC_IOCTL_EVENT_IDX_ALL) {
82652eabba5SLogan Gunthorpe 		if (event_regs[ctl.event_id].map_reg == global_ev_reg)
82752eabba5SLogan Gunthorpe 			nr_idxs = 1;
82852eabba5SLogan Gunthorpe 		else if (event_regs[ctl.event_id].map_reg == part_ev_reg)
82952eabba5SLogan Gunthorpe 			nr_idxs = stdev->partition_count;
83052eabba5SLogan Gunthorpe 		else if (event_regs[ctl.event_id].map_reg == pff_ev_reg)
83152eabba5SLogan Gunthorpe 			nr_idxs = stdev->pff_csr_count;
83252eabba5SLogan Gunthorpe 		else
83352eabba5SLogan Gunthorpe 			return -EINVAL;
83452eabba5SLogan Gunthorpe 
835e4a7dca5SJoey Zhang 		event_flags = ctl.flags;
83652eabba5SLogan Gunthorpe 		for (ctl.index = 0; ctl.index < nr_idxs; ctl.index++) {
837e4a7dca5SJoey Zhang 			ctl.flags = event_flags;
83852eabba5SLogan Gunthorpe 			ret = event_ctl(stdev, &ctl);
83952eabba5SLogan Gunthorpe 			if (ret < 0)
84052eabba5SLogan Gunthorpe 				return ret;
84152eabba5SLogan Gunthorpe 		}
84252eabba5SLogan Gunthorpe 	} else {
84352eabba5SLogan Gunthorpe 		ret = event_ctl(stdev, &ctl);
84452eabba5SLogan Gunthorpe 		if (ret < 0)
84552eabba5SLogan Gunthorpe 			return ret;
84652eabba5SLogan Gunthorpe 	}
84752eabba5SLogan Gunthorpe 
84852eabba5SLogan Gunthorpe 	if (copy_to_user(uctl, &ctl, sizeof(ctl)))
84952eabba5SLogan Gunthorpe 		return -EFAULT;
85052eabba5SLogan Gunthorpe 
85152eabba5SLogan Gunthorpe 	return 0;
85252eabba5SLogan Gunthorpe }
85352eabba5SLogan Gunthorpe 
85452eabba5SLogan Gunthorpe static int ioctl_pff_to_port(struct switchtec_dev *stdev,
85552eabba5SLogan Gunthorpe 			     struct switchtec_ioctl_pff_port *up)
85652eabba5SLogan Gunthorpe {
85752eabba5SLogan Gunthorpe 	int i, part;
85852eabba5SLogan Gunthorpe 	u32 reg;
85952eabba5SLogan Gunthorpe 	struct part_cfg_regs *pcfg;
86052eabba5SLogan Gunthorpe 	struct switchtec_ioctl_pff_port p;
86152eabba5SLogan Gunthorpe 
86252eabba5SLogan Gunthorpe 	if (copy_from_user(&p, up, sizeof(p)))
86352eabba5SLogan Gunthorpe 		return -EFAULT;
86452eabba5SLogan Gunthorpe 
86552eabba5SLogan Gunthorpe 	p.port = -1;
86652eabba5SLogan Gunthorpe 	for (part = 0; part < stdev->partition_count; part++) {
86752eabba5SLogan Gunthorpe 		pcfg = &stdev->mmio_part_cfg_all[part];
86852eabba5SLogan Gunthorpe 		p.partition = part;
86952eabba5SLogan Gunthorpe 
87052eabba5SLogan Gunthorpe 		reg = ioread32(&pcfg->usp_pff_inst_id);
87152eabba5SLogan Gunthorpe 		if (reg == p.pff) {
87252eabba5SLogan Gunthorpe 			p.port = 0;
87352eabba5SLogan Gunthorpe 			break;
87452eabba5SLogan Gunthorpe 		}
87552eabba5SLogan Gunthorpe 
87652eabba5SLogan Gunthorpe 		reg = ioread32(&pcfg->vep_pff_inst_id);
87752eabba5SLogan Gunthorpe 		if (reg == p.pff) {
87852eabba5SLogan Gunthorpe 			p.port = SWITCHTEC_IOCTL_PFF_VEP;
87952eabba5SLogan Gunthorpe 			break;
88052eabba5SLogan Gunthorpe 		}
88152eabba5SLogan Gunthorpe 
88252eabba5SLogan Gunthorpe 		for (i = 0; i < ARRAY_SIZE(pcfg->dsp_pff_inst_id); i++) {
88352eabba5SLogan Gunthorpe 			reg = ioread32(&pcfg->dsp_pff_inst_id[i]);
88452eabba5SLogan Gunthorpe 			if (reg != p.pff)
88552eabba5SLogan Gunthorpe 				continue;
88652eabba5SLogan Gunthorpe 
88752eabba5SLogan Gunthorpe 			p.port = i + 1;
88852eabba5SLogan Gunthorpe 			break;
88952eabba5SLogan Gunthorpe 		}
89052eabba5SLogan Gunthorpe 
89152eabba5SLogan Gunthorpe 		if (p.port != -1)
89252eabba5SLogan Gunthorpe 			break;
89352eabba5SLogan Gunthorpe 	}
89452eabba5SLogan Gunthorpe 
89552eabba5SLogan Gunthorpe 	if (copy_to_user(up, &p, sizeof(p)))
89652eabba5SLogan Gunthorpe 		return -EFAULT;
89752eabba5SLogan Gunthorpe 
89852eabba5SLogan Gunthorpe 	return 0;
89952eabba5SLogan Gunthorpe }
90052eabba5SLogan Gunthorpe 
90152eabba5SLogan Gunthorpe static int ioctl_port_to_pff(struct switchtec_dev *stdev,
90252eabba5SLogan Gunthorpe 			     struct switchtec_ioctl_pff_port *up)
90352eabba5SLogan Gunthorpe {
90452eabba5SLogan Gunthorpe 	struct switchtec_ioctl_pff_port p;
90552eabba5SLogan Gunthorpe 	struct part_cfg_regs *pcfg;
90652eabba5SLogan Gunthorpe 
90752eabba5SLogan Gunthorpe 	if (copy_from_user(&p, up, sizeof(p)))
90852eabba5SLogan Gunthorpe 		return -EFAULT;
90952eabba5SLogan Gunthorpe 
91052eabba5SLogan Gunthorpe 	if (p.partition == SWITCHTEC_IOCTL_EVENT_LOCAL_PART_IDX)
91152eabba5SLogan Gunthorpe 		pcfg = stdev->mmio_part_cfg;
91252eabba5SLogan Gunthorpe 	else if (p.partition < stdev->partition_count)
91352eabba5SLogan Gunthorpe 		pcfg = &stdev->mmio_part_cfg_all[p.partition];
91452eabba5SLogan Gunthorpe 	else
91552eabba5SLogan Gunthorpe 		return -EINVAL;
91652eabba5SLogan Gunthorpe 
91752eabba5SLogan Gunthorpe 	switch (p.port) {
91852eabba5SLogan Gunthorpe 	case 0:
91952eabba5SLogan Gunthorpe 		p.pff = ioread32(&pcfg->usp_pff_inst_id);
92052eabba5SLogan Gunthorpe 		break;
92152eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PFF_VEP:
92252eabba5SLogan Gunthorpe 		p.pff = ioread32(&pcfg->vep_pff_inst_id);
92352eabba5SLogan Gunthorpe 		break;
92452eabba5SLogan Gunthorpe 	default:
92552eabba5SLogan Gunthorpe 		if (p.port > ARRAY_SIZE(pcfg->dsp_pff_inst_id))
92652eabba5SLogan Gunthorpe 			return -EINVAL;
92746feb6b4SGustavo A. R. Silva 		p.port = array_index_nospec(p.port,
92846feb6b4SGustavo A. R. Silva 					ARRAY_SIZE(pcfg->dsp_pff_inst_id) + 1);
92952eabba5SLogan Gunthorpe 		p.pff = ioread32(&pcfg->dsp_pff_inst_id[p.port - 1]);
93052eabba5SLogan Gunthorpe 		break;
93152eabba5SLogan Gunthorpe 	}
93252eabba5SLogan Gunthorpe 
93352eabba5SLogan Gunthorpe 	if (copy_to_user(up, &p, sizeof(p)))
93452eabba5SLogan Gunthorpe 		return -EFAULT;
93552eabba5SLogan Gunthorpe 
93652eabba5SLogan Gunthorpe 	return 0;
93752eabba5SLogan Gunthorpe }
93852eabba5SLogan Gunthorpe 
93952eabba5SLogan Gunthorpe static long switchtec_dev_ioctl(struct file *filp, unsigned int cmd,
94052eabba5SLogan Gunthorpe 				unsigned long arg)
94152eabba5SLogan Gunthorpe {
94252eabba5SLogan Gunthorpe 	struct switchtec_user *stuser = filp->private_data;
94352eabba5SLogan Gunthorpe 	struct switchtec_dev *stdev = stuser->stdev;
94452eabba5SLogan Gunthorpe 	int rc;
94552eabba5SLogan Gunthorpe 	void __user *argp = (void __user *)arg;
94652eabba5SLogan Gunthorpe 
94752eabba5SLogan Gunthorpe 	rc = lock_mutex_and_test_alive(stdev);
94852eabba5SLogan Gunthorpe 	if (rc)
94952eabba5SLogan Gunthorpe 		return rc;
95052eabba5SLogan Gunthorpe 
95152eabba5SLogan Gunthorpe 	switch (cmd) {
95252eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_FLASH_INFO:
95352eabba5SLogan Gunthorpe 		rc = ioctl_flash_info(stdev, argp);
95452eabba5SLogan Gunthorpe 		break;
95552eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_FLASH_PART_INFO:
95652eabba5SLogan Gunthorpe 		rc = ioctl_flash_part_info(stdev, argp);
95752eabba5SLogan Gunthorpe 		break;
95852eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_EVENT_SUMMARY:
95952eabba5SLogan Gunthorpe 		rc = ioctl_event_summary(stdev, stuser, argp);
96052eabba5SLogan Gunthorpe 		break;
96152eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_EVENT_CTL:
96252eabba5SLogan Gunthorpe 		rc = ioctl_event_ctl(stdev, argp);
96352eabba5SLogan Gunthorpe 		break;
96452eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PFF_TO_PORT:
96552eabba5SLogan Gunthorpe 		rc = ioctl_pff_to_port(stdev, argp);
96652eabba5SLogan Gunthorpe 		break;
96752eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PORT_TO_PFF:
96852eabba5SLogan Gunthorpe 		rc = ioctl_port_to_pff(stdev, argp);
96952eabba5SLogan Gunthorpe 		break;
97052eabba5SLogan Gunthorpe 	default:
97152eabba5SLogan Gunthorpe 		rc = -ENOTTY;
97252eabba5SLogan Gunthorpe 		break;
97352eabba5SLogan Gunthorpe 	}
97452eabba5SLogan Gunthorpe 
97552eabba5SLogan Gunthorpe 	mutex_unlock(&stdev->mrpc_mutex);
97652eabba5SLogan Gunthorpe 	return rc;
97752eabba5SLogan Gunthorpe }
97852eabba5SLogan Gunthorpe 
979080b47deSLogan Gunthorpe static const struct file_operations switchtec_fops = {
980080b47deSLogan Gunthorpe 	.owner = THIS_MODULE,
981080b47deSLogan Gunthorpe 	.open = switchtec_dev_open,
982080b47deSLogan Gunthorpe 	.release = switchtec_dev_release,
983080b47deSLogan Gunthorpe 	.write = switchtec_dev_write,
984080b47deSLogan Gunthorpe 	.read = switchtec_dev_read,
985080b47deSLogan Gunthorpe 	.poll = switchtec_dev_poll,
98652eabba5SLogan Gunthorpe 	.unlocked_ioctl = switchtec_dev_ioctl,
98752eabba5SLogan Gunthorpe 	.compat_ioctl = switchtec_dev_ioctl,
988080b47deSLogan Gunthorpe };
989080b47deSLogan Gunthorpe 
99048c302dcSLogan Gunthorpe static void link_event_work(struct work_struct *work)
99148c302dcSLogan Gunthorpe {
99248c302dcSLogan Gunthorpe 	struct switchtec_dev *stdev;
99348c302dcSLogan Gunthorpe 
99448c302dcSLogan Gunthorpe 	stdev = container_of(work, struct switchtec_dev, link_event_work);
99548c302dcSLogan Gunthorpe 
99648c302dcSLogan Gunthorpe 	if (stdev->link_notifier)
99748c302dcSLogan Gunthorpe 		stdev->link_notifier(stdev);
99848c302dcSLogan Gunthorpe }
99948c302dcSLogan Gunthorpe 
100048c302dcSLogan Gunthorpe static void check_link_state_events(struct switchtec_dev *stdev)
100148c302dcSLogan Gunthorpe {
100248c302dcSLogan Gunthorpe 	int idx;
100348c302dcSLogan Gunthorpe 	u32 reg;
100448c302dcSLogan Gunthorpe 	int count;
100548c302dcSLogan Gunthorpe 	int occurred = 0;
100648c302dcSLogan Gunthorpe 
100748c302dcSLogan Gunthorpe 	for (idx = 0; idx < stdev->pff_csr_count; idx++) {
100848c302dcSLogan Gunthorpe 		reg = ioread32(&stdev->mmio_pff_csr[idx].link_state_hdr);
100948c302dcSLogan Gunthorpe 		dev_dbg(&stdev->dev, "link_state: %d->%08x\n", idx, reg);
101048c302dcSLogan Gunthorpe 		count = (reg >> 5) & 0xFF;
101148c302dcSLogan Gunthorpe 
101248c302dcSLogan Gunthorpe 		if (count != stdev->link_event_count[idx]) {
101348c302dcSLogan Gunthorpe 			occurred = 1;
101448c302dcSLogan Gunthorpe 			stdev->link_event_count[idx] = count;
101548c302dcSLogan Gunthorpe 		}
101648c302dcSLogan Gunthorpe 	}
101748c302dcSLogan Gunthorpe 
101848c302dcSLogan Gunthorpe 	if (occurred)
101948c302dcSLogan Gunthorpe 		schedule_work(&stdev->link_event_work);
102048c302dcSLogan Gunthorpe }
102148c302dcSLogan Gunthorpe 
102248c302dcSLogan Gunthorpe static void enable_link_state_events(struct switchtec_dev *stdev)
102348c302dcSLogan Gunthorpe {
102448c302dcSLogan Gunthorpe 	int idx;
102548c302dcSLogan Gunthorpe 
102648c302dcSLogan Gunthorpe 	for (idx = 0; idx < stdev->pff_csr_count; idx++) {
102748c302dcSLogan Gunthorpe 		iowrite32(SWITCHTEC_EVENT_CLEAR |
102848c302dcSLogan Gunthorpe 			  SWITCHTEC_EVENT_EN_IRQ,
102948c302dcSLogan Gunthorpe 			  &stdev->mmio_pff_csr[idx].link_state_hdr);
103048c302dcSLogan Gunthorpe 	}
103148c302dcSLogan Gunthorpe }
103248c302dcSLogan Gunthorpe 
1033080b47deSLogan Gunthorpe static void stdev_release(struct device *dev)
1034080b47deSLogan Gunthorpe {
1035080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev = to_stdev(dev);
1036080b47deSLogan Gunthorpe 
1037080b47deSLogan Gunthorpe 	kfree(stdev);
1038080b47deSLogan Gunthorpe }
1039080b47deSLogan Gunthorpe 
1040080b47deSLogan Gunthorpe static void stdev_kill(struct switchtec_dev *stdev)
1041080b47deSLogan Gunthorpe {
1042080b47deSLogan Gunthorpe 	struct switchtec_user *stuser, *tmpuser;
1043080b47deSLogan Gunthorpe 
1044080b47deSLogan Gunthorpe 	pci_clear_master(stdev->pdev);
1045080b47deSLogan Gunthorpe 
1046080b47deSLogan Gunthorpe 	cancel_delayed_work_sync(&stdev->mrpc_timeout);
1047080b47deSLogan Gunthorpe 
1048080b47deSLogan Gunthorpe 	/* Mark the hardware as unavailable and complete all completions */
1049080b47deSLogan Gunthorpe 	mutex_lock(&stdev->mrpc_mutex);
1050080b47deSLogan Gunthorpe 	stdev->alive = false;
1051080b47deSLogan Gunthorpe 
1052080b47deSLogan Gunthorpe 	/* Wake up and kill any users waiting on an MRPC request */
1053080b47deSLogan Gunthorpe 	list_for_each_entry_safe(stuser, tmpuser, &stdev->mrpc_queue, list) {
1054080b47deSLogan Gunthorpe 		complete_all(&stuser->comp);
1055080b47deSLogan Gunthorpe 		list_del_init(&stuser->list);
1056080b47deSLogan Gunthorpe 		stuser_put(stuser);
1057080b47deSLogan Gunthorpe 	}
1058080b47deSLogan Gunthorpe 
1059080b47deSLogan Gunthorpe 	mutex_unlock(&stdev->mrpc_mutex);
1060080b47deSLogan Gunthorpe 
1061080b47deSLogan Gunthorpe 	/* Wake up any users waiting on event_wq */
1062080b47deSLogan Gunthorpe 	wake_up_interruptible(&stdev->event_wq);
1063080b47deSLogan Gunthorpe }
1064080b47deSLogan Gunthorpe 
1065080b47deSLogan Gunthorpe static struct switchtec_dev *stdev_create(struct pci_dev *pdev)
1066080b47deSLogan Gunthorpe {
1067080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev;
1068080b47deSLogan Gunthorpe 	int minor;
1069080b47deSLogan Gunthorpe 	struct device *dev;
1070080b47deSLogan Gunthorpe 	struct cdev *cdev;
1071080b47deSLogan Gunthorpe 	int rc;
1072080b47deSLogan Gunthorpe 
1073080b47deSLogan Gunthorpe 	stdev = kzalloc_node(sizeof(*stdev), GFP_KERNEL,
1074080b47deSLogan Gunthorpe 			     dev_to_node(&pdev->dev));
1075080b47deSLogan Gunthorpe 	if (!stdev)
1076080b47deSLogan Gunthorpe 		return ERR_PTR(-ENOMEM);
1077080b47deSLogan Gunthorpe 
1078080b47deSLogan Gunthorpe 	stdev->alive = true;
1079080b47deSLogan Gunthorpe 	stdev->pdev = pdev;
1080080b47deSLogan Gunthorpe 	INIT_LIST_HEAD(&stdev->mrpc_queue);
1081080b47deSLogan Gunthorpe 	mutex_init(&stdev->mrpc_mutex);
1082080b47deSLogan Gunthorpe 	stdev->mrpc_busy = 0;
1083080b47deSLogan Gunthorpe 	INIT_WORK(&stdev->mrpc_work, mrpc_event_work);
1084080b47deSLogan Gunthorpe 	INIT_DELAYED_WORK(&stdev->mrpc_timeout, mrpc_timeout_work);
108548c302dcSLogan Gunthorpe 	INIT_WORK(&stdev->link_event_work, link_event_work);
1086080b47deSLogan Gunthorpe 	init_waitqueue_head(&stdev->event_wq);
1087080b47deSLogan Gunthorpe 	atomic_set(&stdev->event_cnt, 0);
1088080b47deSLogan Gunthorpe 
1089080b47deSLogan Gunthorpe 	dev = &stdev->dev;
1090080b47deSLogan Gunthorpe 	device_initialize(dev);
1091080b47deSLogan Gunthorpe 	dev->class = switchtec_class;
1092080b47deSLogan Gunthorpe 	dev->parent = &pdev->dev;
10935d8e1881SLogan Gunthorpe 	dev->groups = switchtec_device_groups;
1094080b47deSLogan Gunthorpe 	dev->release = stdev_release;
1095080b47deSLogan Gunthorpe 
1096080b47deSLogan Gunthorpe 	minor = ida_simple_get(&switchtec_minor_ida, 0, 0,
1097080b47deSLogan Gunthorpe 			       GFP_KERNEL);
1098080b47deSLogan Gunthorpe 	if (minor < 0) {
1099080b47deSLogan Gunthorpe 		rc = minor;
1100080b47deSLogan Gunthorpe 		goto err_put;
1101080b47deSLogan Gunthorpe 	}
1102080b47deSLogan Gunthorpe 
1103080b47deSLogan Gunthorpe 	dev->devt = MKDEV(MAJOR(switchtec_devt), minor);
1104080b47deSLogan Gunthorpe 	dev_set_name(dev, "switchtec%d", minor);
1105080b47deSLogan Gunthorpe 
1106080b47deSLogan Gunthorpe 	cdev = &stdev->cdev;
1107080b47deSLogan Gunthorpe 	cdev_init(cdev, &switchtec_fops);
1108080b47deSLogan Gunthorpe 	cdev->owner = THIS_MODULE;
1109080b47deSLogan Gunthorpe 
1110080b47deSLogan Gunthorpe 	return stdev;
1111080b47deSLogan Gunthorpe 
1112080b47deSLogan Gunthorpe err_put:
1113080b47deSLogan Gunthorpe 	put_device(&stdev->dev);
1114080b47deSLogan Gunthorpe 	return ERR_PTR(rc);
1115080b47deSLogan Gunthorpe }
1116080b47deSLogan Gunthorpe 
111752eabba5SLogan Gunthorpe static int mask_event(struct switchtec_dev *stdev, int eid, int idx)
111852eabba5SLogan Gunthorpe {
111952eabba5SLogan Gunthorpe 	size_t off = event_regs[eid].offset;
112052eabba5SLogan Gunthorpe 	u32 __iomem *hdr_reg;
112152eabba5SLogan Gunthorpe 	u32 hdr;
112252eabba5SLogan Gunthorpe 
112352eabba5SLogan Gunthorpe 	hdr_reg = event_regs[eid].map_reg(stdev, off, idx);
112452eabba5SLogan Gunthorpe 	hdr = ioread32(hdr_reg);
112552eabba5SLogan Gunthorpe 
112652eabba5SLogan Gunthorpe 	if (!(hdr & SWITCHTEC_EVENT_OCCURRED && hdr & SWITCHTEC_EVENT_EN_IRQ))
112752eabba5SLogan Gunthorpe 		return 0;
112852eabba5SLogan Gunthorpe 
112948c302dcSLogan Gunthorpe 	if (eid == SWITCHTEC_IOCTL_EVENT_LINK_STATE)
113048c302dcSLogan Gunthorpe 		return 0;
113148c302dcSLogan Gunthorpe 
113252eabba5SLogan Gunthorpe 	dev_dbg(&stdev->dev, "%s: %d %d %x\n", __func__, eid, idx, hdr);
113352eabba5SLogan Gunthorpe 	hdr &= ~(SWITCHTEC_EVENT_EN_IRQ | SWITCHTEC_EVENT_OCCURRED);
113452eabba5SLogan Gunthorpe 	iowrite32(hdr, hdr_reg);
113552eabba5SLogan Gunthorpe 
113652eabba5SLogan Gunthorpe 	return 1;
113752eabba5SLogan Gunthorpe }
113852eabba5SLogan Gunthorpe 
113952eabba5SLogan Gunthorpe static int mask_all_events(struct switchtec_dev *stdev, int eid)
114052eabba5SLogan Gunthorpe {
114152eabba5SLogan Gunthorpe 	int idx;
114252eabba5SLogan Gunthorpe 	int count = 0;
114352eabba5SLogan Gunthorpe 
114452eabba5SLogan Gunthorpe 	if (event_regs[eid].map_reg == part_ev_reg) {
114552eabba5SLogan Gunthorpe 		for (idx = 0; idx < stdev->partition_count; idx++)
114652eabba5SLogan Gunthorpe 			count += mask_event(stdev, eid, idx);
114752eabba5SLogan Gunthorpe 	} else if (event_regs[eid].map_reg == pff_ev_reg) {
114852eabba5SLogan Gunthorpe 		for (idx = 0; idx < stdev->pff_csr_count; idx++) {
114952eabba5SLogan Gunthorpe 			if (!stdev->pff_local[idx])
115052eabba5SLogan Gunthorpe 				continue;
115148c302dcSLogan Gunthorpe 
115252eabba5SLogan Gunthorpe 			count += mask_event(stdev, eid, idx);
115352eabba5SLogan Gunthorpe 		}
115452eabba5SLogan Gunthorpe 	} else {
115552eabba5SLogan Gunthorpe 		count += mask_event(stdev, eid, 0);
115652eabba5SLogan Gunthorpe 	}
115752eabba5SLogan Gunthorpe 
115852eabba5SLogan Gunthorpe 	return count;
115952eabba5SLogan Gunthorpe }
116052eabba5SLogan Gunthorpe 
1161080b47deSLogan Gunthorpe static irqreturn_t switchtec_event_isr(int irq, void *dev)
1162080b47deSLogan Gunthorpe {
1163080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev = dev;
1164080b47deSLogan Gunthorpe 	u32 reg;
1165080b47deSLogan Gunthorpe 	irqreturn_t ret = IRQ_NONE;
116652eabba5SLogan Gunthorpe 	int eid, event_count = 0;
1167080b47deSLogan Gunthorpe 
1168080b47deSLogan Gunthorpe 	reg = ioread32(&stdev->mmio_part_cfg->mrpc_comp_hdr);
1169080b47deSLogan Gunthorpe 	if (reg & SWITCHTEC_EVENT_OCCURRED) {
1170080b47deSLogan Gunthorpe 		dev_dbg(&stdev->dev, "%s: mrpc comp\n", __func__);
1171080b47deSLogan Gunthorpe 		ret = IRQ_HANDLED;
1172080b47deSLogan Gunthorpe 		schedule_work(&stdev->mrpc_work);
1173080b47deSLogan Gunthorpe 		iowrite32(reg, &stdev->mmio_part_cfg->mrpc_comp_hdr);
1174080b47deSLogan Gunthorpe 	}
1175080b47deSLogan Gunthorpe 
117648c302dcSLogan Gunthorpe 	check_link_state_events(stdev);
117748c302dcSLogan Gunthorpe 
117852eabba5SLogan Gunthorpe 	for (eid = 0; eid < SWITCHTEC_IOCTL_MAX_EVENTS; eid++)
117952eabba5SLogan Gunthorpe 		event_count += mask_all_events(stdev, eid);
118052eabba5SLogan Gunthorpe 
118152eabba5SLogan Gunthorpe 	if (event_count) {
118252eabba5SLogan Gunthorpe 		atomic_inc(&stdev->event_cnt);
118352eabba5SLogan Gunthorpe 		wake_up_interruptible(&stdev->event_wq);
118452eabba5SLogan Gunthorpe 		dev_dbg(&stdev->dev, "%s: %d events\n", __func__,
118552eabba5SLogan Gunthorpe 			event_count);
118652eabba5SLogan Gunthorpe 		return IRQ_HANDLED;
118752eabba5SLogan Gunthorpe 	}
118852eabba5SLogan Gunthorpe 
1189080b47deSLogan Gunthorpe 	return ret;
1190080b47deSLogan Gunthorpe }
1191080b47deSLogan Gunthorpe 
1192080b47deSLogan Gunthorpe static int switchtec_init_isr(struct switchtec_dev *stdev)
1193080b47deSLogan Gunthorpe {
1194080b47deSLogan Gunthorpe 	int nvecs;
1195080b47deSLogan Gunthorpe 	int event_irq;
1196080b47deSLogan Gunthorpe 
1197080b47deSLogan Gunthorpe 	nvecs = pci_alloc_irq_vectors(stdev->pdev, 1, 4,
1198080b47deSLogan Gunthorpe 				      PCI_IRQ_MSIX | PCI_IRQ_MSI);
1199080b47deSLogan Gunthorpe 	if (nvecs < 0)
1200080b47deSLogan Gunthorpe 		return nvecs;
1201080b47deSLogan Gunthorpe 
1202080b47deSLogan Gunthorpe 	event_irq = ioread32(&stdev->mmio_part_cfg->vep_vector_number);
1203080b47deSLogan Gunthorpe 	if (event_irq < 0 || event_irq >= nvecs)
1204080b47deSLogan Gunthorpe 		return -EFAULT;
1205080b47deSLogan Gunthorpe 
1206080b47deSLogan Gunthorpe 	event_irq = pci_irq_vector(stdev->pdev, event_irq);
1207080b47deSLogan Gunthorpe 	if (event_irq < 0)
1208080b47deSLogan Gunthorpe 		return event_irq;
1209080b47deSLogan Gunthorpe 
1210080b47deSLogan Gunthorpe 	return devm_request_irq(&stdev->pdev->dev, event_irq,
1211080b47deSLogan Gunthorpe 				switchtec_event_isr, 0,
1212080b47deSLogan Gunthorpe 				KBUILD_MODNAME, stdev);
1213080b47deSLogan Gunthorpe }
1214080b47deSLogan Gunthorpe 
1215080b47deSLogan Gunthorpe static void init_pff(struct switchtec_dev *stdev)
1216080b47deSLogan Gunthorpe {
1217080b47deSLogan Gunthorpe 	int i;
1218080b47deSLogan Gunthorpe 	u32 reg;
1219080b47deSLogan Gunthorpe 	struct part_cfg_regs *pcfg = stdev->mmio_part_cfg;
1220080b47deSLogan Gunthorpe 
1221080b47deSLogan Gunthorpe 	for (i = 0; i < SWITCHTEC_MAX_PFF_CSR; i++) {
1222080b47deSLogan Gunthorpe 		reg = ioread16(&stdev->mmio_pff_csr[i].vendor_id);
1223cfdfc14eSDoug Meyer 		if (reg != PCI_VENDOR_ID_MICROSEMI)
1224080b47deSLogan Gunthorpe 			break;
1225080b47deSLogan Gunthorpe 	}
1226080b47deSLogan Gunthorpe 
1227080b47deSLogan Gunthorpe 	stdev->pff_csr_count = i;
1228080b47deSLogan Gunthorpe 
1229080b47deSLogan Gunthorpe 	reg = ioread32(&pcfg->usp_pff_inst_id);
1230080b47deSLogan Gunthorpe 	if (reg < SWITCHTEC_MAX_PFF_CSR)
1231080b47deSLogan Gunthorpe 		stdev->pff_local[reg] = 1;
1232080b47deSLogan Gunthorpe 
1233080b47deSLogan Gunthorpe 	reg = ioread32(&pcfg->vep_pff_inst_id);
1234080b47deSLogan Gunthorpe 	if (reg < SWITCHTEC_MAX_PFF_CSR)
1235080b47deSLogan Gunthorpe 		stdev->pff_local[reg] = 1;
1236080b47deSLogan Gunthorpe 
1237080b47deSLogan Gunthorpe 	for (i = 0; i < ARRAY_SIZE(pcfg->dsp_pff_inst_id); i++) {
1238080b47deSLogan Gunthorpe 		reg = ioread32(&pcfg->dsp_pff_inst_id[i]);
1239080b47deSLogan Gunthorpe 		if (reg < SWITCHTEC_MAX_PFF_CSR)
1240080b47deSLogan Gunthorpe 			stdev->pff_local[reg] = 1;
1241080b47deSLogan Gunthorpe 	}
1242080b47deSLogan Gunthorpe }
1243080b47deSLogan Gunthorpe 
1244080b47deSLogan Gunthorpe static int switchtec_init_pci(struct switchtec_dev *stdev,
1245080b47deSLogan Gunthorpe 			      struct pci_dev *pdev)
1246080b47deSLogan Gunthorpe {
1247080b47deSLogan Gunthorpe 	int rc;
1248*52d8db8eSKelvin Cao 	void __iomem *map;
1249*52d8db8eSKelvin Cao 	unsigned long res_start, res_len;
1250080b47deSLogan Gunthorpe 
1251080b47deSLogan Gunthorpe 	rc = pcim_enable_device(pdev);
1252080b47deSLogan Gunthorpe 	if (rc)
1253080b47deSLogan Gunthorpe 		return rc;
1254080b47deSLogan Gunthorpe 
1255aff614c6SBoris Glimcher 	rc = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
1256aff614c6SBoris Glimcher 	if (rc)
1257aff614c6SBoris Glimcher 		return rc;
1258aff614c6SBoris Glimcher 
1259080b47deSLogan Gunthorpe 	pci_set_master(pdev);
1260080b47deSLogan Gunthorpe 
1261*52d8db8eSKelvin Cao 	res_start = pci_resource_start(pdev, 0);
1262*52d8db8eSKelvin Cao 	res_len = pci_resource_len(pdev, 0);
1263*52d8db8eSKelvin Cao 
1264*52d8db8eSKelvin Cao 	if (!devm_request_mem_region(&pdev->dev, res_start,
1265*52d8db8eSKelvin Cao 				     res_len, KBUILD_MODNAME))
1266*52d8db8eSKelvin Cao 		return -EBUSY;
1267*52d8db8eSKelvin Cao 
1268*52d8db8eSKelvin Cao 	stdev->mmio_mrpc = devm_ioremap_wc(&pdev->dev, res_start,
1269*52d8db8eSKelvin Cao 					   SWITCHTEC_GAS_TOP_CFG_OFFSET);
1270*52d8db8eSKelvin Cao 	if (!stdev->mmio_mrpc)
1271*52d8db8eSKelvin Cao 		return -ENOMEM;
1272*52d8db8eSKelvin Cao 
1273*52d8db8eSKelvin Cao 	map = devm_ioremap(&pdev->dev,
1274*52d8db8eSKelvin Cao 			   res_start + SWITCHTEC_GAS_TOP_CFG_OFFSET,
1275*52d8db8eSKelvin Cao 			   res_len - SWITCHTEC_GAS_TOP_CFG_OFFSET);
1276*52d8db8eSKelvin Cao 	if (!map)
1277*52d8db8eSKelvin Cao 		return -ENOMEM;
1278*52d8db8eSKelvin Cao 
1279*52d8db8eSKelvin Cao 	stdev->mmio = map - SWITCHTEC_GAS_TOP_CFG_OFFSET;
1280080b47deSLogan Gunthorpe 	stdev->mmio_sw_event = stdev->mmio + SWITCHTEC_GAS_SW_EVENT_OFFSET;
1281080b47deSLogan Gunthorpe 	stdev->mmio_sys_info = stdev->mmio + SWITCHTEC_GAS_SYS_INFO_OFFSET;
1282080b47deSLogan Gunthorpe 	stdev->mmio_flash_info = stdev->mmio + SWITCHTEC_GAS_FLASH_INFO_OFFSET;
1283080b47deSLogan Gunthorpe 	stdev->mmio_ntb = stdev->mmio + SWITCHTEC_GAS_NTB_OFFSET;
12849871e9bbSLogan Gunthorpe 	stdev->partition = ioread8(&stdev->mmio_sys_info->partition_id);
1285080b47deSLogan Gunthorpe 	stdev->partition_count = ioread8(&stdev->mmio_ntb->partition_count);
1286080b47deSLogan Gunthorpe 	stdev->mmio_part_cfg_all = stdev->mmio + SWITCHTEC_GAS_PART_CFG_OFFSET;
1287080b47deSLogan Gunthorpe 	stdev->mmio_part_cfg = &stdev->mmio_part_cfg_all[stdev->partition];
1288080b47deSLogan Gunthorpe 	stdev->mmio_pff_csr = stdev->mmio + SWITCHTEC_GAS_PFF_CSR_OFFSET;
1289080b47deSLogan Gunthorpe 
12909871e9bbSLogan Gunthorpe 	if (stdev->partition_count < 1)
12919871e9bbSLogan Gunthorpe 		stdev->partition_count = 1;
12929871e9bbSLogan Gunthorpe 
1293080b47deSLogan Gunthorpe 	init_pff(stdev);
1294080b47deSLogan Gunthorpe 
1295080b47deSLogan Gunthorpe 	pci_set_drvdata(pdev, stdev);
1296080b47deSLogan Gunthorpe 
1297080b47deSLogan Gunthorpe 	return 0;
1298080b47deSLogan Gunthorpe }
1299080b47deSLogan Gunthorpe 
1300080b47deSLogan Gunthorpe static int switchtec_pci_probe(struct pci_dev *pdev,
1301080b47deSLogan Gunthorpe 			       const struct pci_device_id *id)
1302080b47deSLogan Gunthorpe {
1303080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev;
1304080b47deSLogan Gunthorpe 	int rc;
1305080b47deSLogan Gunthorpe 
1306cfdfc14eSDoug Meyer 	if (pdev->class == (PCI_CLASS_BRIDGE_OTHER << 8))
130733dea5aaSLogan Gunthorpe 		request_module_nowait("ntb_hw_switchtec");
130833dea5aaSLogan Gunthorpe 
1309080b47deSLogan Gunthorpe 	stdev = stdev_create(pdev);
1310080b47deSLogan Gunthorpe 	if (IS_ERR(stdev))
1311080b47deSLogan Gunthorpe 		return PTR_ERR(stdev);
1312080b47deSLogan Gunthorpe 
1313080b47deSLogan Gunthorpe 	rc = switchtec_init_pci(stdev, pdev);
1314080b47deSLogan Gunthorpe 	if (rc)
1315080b47deSLogan Gunthorpe 		goto err_put;
1316080b47deSLogan Gunthorpe 
1317080b47deSLogan Gunthorpe 	rc = switchtec_init_isr(stdev);
1318080b47deSLogan Gunthorpe 	if (rc) {
1319080b47deSLogan Gunthorpe 		dev_err(&stdev->dev, "failed to init isr.\n");
1320080b47deSLogan Gunthorpe 		goto err_put;
1321080b47deSLogan Gunthorpe 	}
1322080b47deSLogan Gunthorpe 
1323080b47deSLogan Gunthorpe 	iowrite32(SWITCHTEC_EVENT_CLEAR |
1324080b47deSLogan Gunthorpe 		  SWITCHTEC_EVENT_EN_IRQ,
1325080b47deSLogan Gunthorpe 		  &stdev->mmio_part_cfg->mrpc_comp_hdr);
132648c302dcSLogan Gunthorpe 	enable_link_state_events(stdev);
1327080b47deSLogan Gunthorpe 
1328e40cf640SLogan Gunthorpe 	rc = cdev_device_add(&stdev->cdev, &stdev->dev);
1329080b47deSLogan Gunthorpe 	if (rc)
1330080b47deSLogan Gunthorpe 		goto err_devadd;
1331080b47deSLogan Gunthorpe 
1332080b47deSLogan Gunthorpe 	dev_info(&stdev->dev, "Management device registered.\n");
1333080b47deSLogan Gunthorpe 
1334080b47deSLogan Gunthorpe 	return 0;
1335080b47deSLogan Gunthorpe 
1336080b47deSLogan Gunthorpe err_devadd:
1337080b47deSLogan Gunthorpe 	stdev_kill(stdev);
1338080b47deSLogan Gunthorpe err_put:
1339080b47deSLogan Gunthorpe 	ida_simple_remove(&switchtec_minor_ida, MINOR(stdev->dev.devt));
1340080b47deSLogan Gunthorpe 	put_device(&stdev->dev);
1341080b47deSLogan Gunthorpe 	return rc;
1342080b47deSLogan Gunthorpe }
1343080b47deSLogan Gunthorpe 
1344080b47deSLogan Gunthorpe static void switchtec_pci_remove(struct pci_dev *pdev)
1345080b47deSLogan Gunthorpe {
1346080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev = pci_get_drvdata(pdev);
1347080b47deSLogan Gunthorpe 
1348080b47deSLogan Gunthorpe 	pci_set_drvdata(pdev, NULL);
1349080b47deSLogan Gunthorpe 
1350e40cf640SLogan Gunthorpe 	cdev_device_del(&stdev->cdev, &stdev->dev);
1351080b47deSLogan Gunthorpe 	ida_simple_remove(&switchtec_minor_ida, MINOR(stdev->dev.devt));
1352080b47deSLogan Gunthorpe 	dev_info(&stdev->dev, "unregistered.\n");
1353080b47deSLogan Gunthorpe 
1354080b47deSLogan Gunthorpe 	stdev_kill(stdev);
1355080b47deSLogan Gunthorpe 	put_device(&stdev->dev);
1356080b47deSLogan Gunthorpe }
1357080b47deSLogan Gunthorpe 
1358080b47deSLogan Gunthorpe #define SWITCHTEC_PCI_DEVICE(device_id) \
1359080b47deSLogan Gunthorpe 	{ \
1360cfdfc14eSDoug Meyer 		.vendor     = PCI_VENDOR_ID_MICROSEMI, \
1361080b47deSLogan Gunthorpe 		.device     = device_id, \
1362080b47deSLogan Gunthorpe 		.subvendor  = PCI_ANY_ID, \
1363080b47deSLogan Gunthorpe 		.subdevice  = PCI_ANY_ID, \
1364cfdfc14eSDoug Meyer 		.class      = (PCI_CLASS_MEMORY_OTHER << 8), \
1365080b47deSLogan Gunthorpe 		.class_mask = 0xFFFFFFFF, \
1366080b47deSLogan Gunthorpe 	}, \
1367080b47deSLogan Gunthorpe 	{ \
1368cfdfc14eSDoug Meyer 		.vendor     = PCI_VENDOR_ID_MICROSEMI, \
1369080b47deSLogan Gunthorpe 		.device     = device_id, \
1370080b47deSLogan Gunthorpe 		.subvendor  = PCI_ANY_ID, \
1371080b47deSLogan Gunthorpe 		.subdevice  = PCI_ANY_ID, \
1372cfdfc14eSDoug Meyer 		.class      = (PCI_CLASS_BRIDGE_OTHER << 8), \
1373080b47deSLogan Gunthorpe 		.class_mask = 0xFFFFFFFF, \
1374080b47deSLogan Gunthorpe 	}
1375080b47deSLogan Gunthorpe 
1376080b47deSLogan Gunthorpe static const struct pci_device_id switchtec_pci_tbl[] = {
1377080b47deSLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8531),  //PFX 24xG3
1378080b47deSLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8532),  //PFX 32xG3
1379080b47deSLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8533),  //PFX 48xG3
1380080b47deSLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8534),  //PFX 64xG3
1381080b47deSLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8535),  //PFX 80xG3
1382080b47deSLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8536),  //PFX 96xG3
1383bb6b42b4SKelvin Cao 	SWITCHTEC_PCI_DEVICE(0x8541),  //PSX 24xG3
1384bb6b42b4SKelvin Cao 	SWITCHTEC_PCI_DEVICE(0x8542),  //PSX 32xG3
1385080b47deSLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8543),  //PSX 48xG3
1386080b47deSLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8544),  //PSX 64xG3
1387080b47deSLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8545),  //PSX 80xG3
1388080b47deSLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8546),  //PSX 96xG3
1389393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8551),  //PAX 24XG3
1390393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8552),  //PAX 32XG3
1391393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8553),  //PAX 48XG3
1392393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8554),  //PAX 64XG3
1393393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8555),  //PAX 80XG3
1394393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8556),  //PAX 96XG3
1395393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8561),  //PFXL 24XG3
1396393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8562),  //PFXL 32XG3
1397393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8563),  //PFXL 48XG3
1398393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8564),  //PFXL 64XG3
1399393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8565),  //PFXL 80XG3
1400393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8566),  //PFXL 96XG3
1401393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8571),  //PFXI 24XG3
1402393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8572),  //PFXI 32XG3
1403393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8573),  //PFXI 48XG3
1404393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8574),  //PFXI 64XG3
1405393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8575),  //PFXI 80XG3
1406393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8576),  //PFXI 96XG3
1407080b47deSLogan Gunthorpe 	{0}
1408080b47deSLogan Gunthorpe };
1409080b47deSLogan Gunthorpe MODULE_DEVICE_TABLE(pci, switchtec_pci_tbl);
1410080b47deSLogan Gunthorpe 
1411080b47deSLogan Gunthorpe static struct pci_driver switchtec_pci_driver = {
1412080b47deSLogan Gunthorpe 	.name		= KBUILD_MODNAME,
1413080b47deSLogan Gunthorpe 	.id_table	= switchtec_pci_tbl,
1414080b47deSLogan Gunthorpe 	.probe		= switchtec_pci_probe,
1415080b47deSLogan Gunthorpe 	.remove		= switchtec_pci_remove,
1416080b47deSLogan Gunthorpe };
1417080b47deSLogan Gunthorpe 
1418080b47deSLogan Gunthorpe static int __init switchtec_init(void)
1419080b47deSLogan Gunthorpe {
1420080b47deSLogan Gunthorpe 	int rc;
1421080b47deSLogan Gunthorpe 
1422080b47deSLogan Gunthorpe 	rc = alloc_chrdev_region(&switchtec_devt, 0, max_devices,
1423080b47deSLogan Gunthorpe 				 "switchtec");
1424080b47deSLogan Gunthorpe 	if (rc)
1425080b47deSLogan Gunthorpe 		return rc;
1426080b47deSLogan Gunthorpe 
1427080b47deSLogan Gunthorpe 	switchtec_class = class_create(THIS_MODULE, "switchtec");
1428080b47deSLogan Gunthorpe 	if (IS_ERR(switchtec_class)) {
1429080b47deSLogan Gunthorpe 		rc = PTR_ERR(switchtec_class);
1430080b47deSLogan Gunthorpe 		goto err_create_class;
1431080b47deSLogan Gunthorpe 	}
1432080b47deSLogan Gunthorpe 
1433080b47deSLogan Gunthorpe 	rc = pci_register_driver(&switchtec_pci_driver);
1434080b47deSLogan Gunthorpe 	if (rc)
1435080b47deSLogan Gunthorpe 		goto err_pci_register;
1436080b47deSLogan Gunthorpe 
1437080b47deSLogan Gunthorpe 	pr_info(KBUILD_MODNAME ": loaded.\n");
1438080b47deSLogan Gunthorpe 
1439080b47deSLogan Gunthorpe 	return 0;
1440080b47deSLogan Gunthorpe 
1441080b47deSLogan Gunthorpe err_pci_register:
1442080b47deSLogan Gunthorpe 	class_destroy(switchtec_class);
1443080b47deSLogan Gunthorpe 
1444080b47deSLogan Gunthorpe err_create_class:
1445080b47deSLogan Gunthorpe 	unregister_chrdev_region(switchtec_devt, max_devices);
1446080b47deSLogan Gunthorpe 
1447080b47deSLogan Gunthorpe 	return rc;
1448080b47deSLogan Gunthorpe }
1449080b47deSLogan Gunthorpe module_init(switchtec_init);
1450080b47deSLogan Gunthorpe 
1451080b47deSLogan Gunthorpe static void __exit switchtec_exit(void)
1452080b47deSLogan Gunthorpe {
1453080b47deSLogan Gunthorpe 	pci_unregister_driver(&switchtec_pci_driver);
1454080b47deSLogan Gunthorpe 	class_destroy(switchtec_class);
1455080b47deSLogan Gunthorpe 	unregister_chrdev_region(switchtec_devt, max_devices);
1456080b47deSLogan Gunthorpe 	ida_destroy(&switchtec_minor_ida);
1457080b47deSLogan Gunthorpe 
1458080b47deSLogan Gunthorpe 	pr_info(KBUILD_MODNAME ": unloaded.\n");
1459080b47deSLogan Gunthorpe }
1460080b47deSLogan Gunthorpe module_exit(switchtec_exit);
1461