xref: /linux/drivers/pci/switch/switchtec.c (revision afc9a42b7464f76e1388cad87d8543c69f6f74ed)
1080b47deSLogan Gunthorpe /*
2080b47deSLogan Gunthorpe  * Microsemi Switchtec(tm) PCIe Management Driver
3080b47deSLogan Gunthorpe  * Copyright (c) 2017, Microsemi Corporation
4080b47deSLogan Gunthorpe  *
5080b47deSLogan Gunthorpe  * This program is free software; you can redistribute it and/or modify it
6080b47deSLogan Gunthorpe  * under the terms and conditions of the GNU General Public License,
7080b47deSLogan Gunthorpe  * version 2, as published by the Free Software Foundation.
8080b47deSLogan Gunthorpe  *
9080b47deSLogan Gunthorpe  * This program is distributed in the hope it will be useful, but WITHOUT
10080b47deSLogan Gunthorpe  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11080b47deSLogan Gunthorpe  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12080b47deSLogan Gunthorpe  * more details.
13080b47deSLogan Gunthorpe  *
14080b47deSLogan Gunthorpe  */
15080b47deSLogan Gunthorpe 
165a1c269fSLogan Gunthorpe #include <linux/switchtec.h>
1752eabba5SLogan Gunthorpe #include <linux/switchtec_ioctl.h>
1852eabba5SLogan Gunthorpe 
19080b47deSLogan Gunthorpe #include <linux/interrupt.h>
20080b47deSLogan Gunthorpe #include <linux/module.h>
21080b47deSLogan Gunthorpe #include <linux/fs.h>
22080b47deSLogan Gunthorpe #include <linux/uaccess.h>
23080b47deSLogan Gunthorpe #include <linux/poll.h>
24080b47deSLogan Gunthorpe #include <linux/wait.h>
25080b47deSLogan Gunthorpe 
26080b47deSLogan Gunthorpe MODULE_DESCRIPTION("Microsemi Switchtec(tm) PCIe Management Driver");
27080b47deSLogan Gunthorpe MODULE_VERSION("0.1");
28080b47deSLogan Gunthorpe MODULE_LICENSE("GPL");
29080b47deSLogan Gunthorpe MODULE_AUTHOR("Microsemi Corporation");
30080b47deSLogan Gunthorpe 
31080b47deSLogan Gunthorpe static int max_devices = 16;
32080b47deSLogan Gunthorpe module_param(max_devices, int, 0644);
33080b47deSLogan Gunthorpe MODULE_PARM_DESC(max_devices, "max number of switchtec device instances");
34080b47deSLogan Gunthorpe 
35080b47deSLogan Gunthorpe static dev_t switchtec_devt;
36080b47deSLogan Gunthorpe static DEFINE_IDA(switchtec_minor_ida);
37080b47deSLogan Gunthorpe 
38302e994dSLogan Gunthorpe struct class *switchtec_class;
39302e994dSLogan Gunthorpe EXPORT_SYMBOL_GPL(switchtec_class);
40080b47deSLogan Gunthorpe 
41080b47deSLogan Gunthorpe enum mrpc_state {
42080b47deSLogan Gunthorpe 	MRPC_IDLE = 0,
43080b47deSLogan Gunthorpe 	MRPC_QUEUED,
44080b47deSLogan Gunthorpe 	MRPC_RUNNING,
45080b47deSLogan Gunthorpe 	MRPC_DONE,
46080b47deSLogan Gunthorpe };
47080b47deSLogan Gunthorpe 
48080b47deSLogan Gunthorpe struct switchtec_user {
49080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev;
50080b47deSLogan Gunthorpe 
51080b47deSLogan Gunthorpe 	enum mrpc_state state;
52080b47deSLogan Gunthorpe 
53080b47deSLogan Gunthorpe 	struct completion comp;
54080b47deSLogan Gunthorpe 	struct kref kref;
55080b47deSLogan Gunthorpe 	struct list_head list;
56080b47deSLogan Gunthorpe 
57080b47deSLogan Gunthorpe 	u32 cmd;
58080b47deSLogan Gunthorpe 	u32 status;
59080b47deSLogan Gunthorpe 	u32 return_code;
60080b47deSLogan Gunthorpe 	size_t data_len;
61080b47deSLogan Gunthorpe 	size_t read_len;
62080b47deSLogan Gunthorpe 	unsigned char data[SWITCHTEC_MRPC_PAYLOAD_SIZE];
63080b47deSLogan Gunthorpe 	int event_cnt;
64080b47deSLogan Gunthorpe };
65080b47deSLogan Gunthorpe 
66080b47deSLogan Gunthorpe static struct switchtec_user *stuser_create(struct switchtec_dev *stdev)
67080b47deSLogan Gunthorpe {
68080b47deSLogan Gunthorpe 	struct switchtec_user *stuser;
69080b47deSLogan Gunthorpe 
70080b47deSLogan Gunthorpe 	stuser = kzalloc(sizeof(*stuser), GFP_KERNEL);
71080b47deSLogan Gunthorpe 	if (!stuser)
72080b47deSLogan Gunthorpe 		return ERR_PTR(-ENOMEM);
73080b47deSLogan Gunthorpe 
74080b47deSLogan Gunthorpe 	get_device(&stdev->dev);
75080b47deSLogan Gunthorpe 	stuser->stdev = stdev;
76080b47deSLogan Gunthorpe 	kref_init(&stuser->kref);
77080b47deSLogan Gunthorpe 	INIT_LIST_HEAD(&stuser->list);
78080b47deSLogan Gunthorpe 	init_completion(&stuser->comp);
79080b47deSLogan Gunthorpe 	stuser->event_cnt = atomic_read(&stdev->event_cnt);
80080b47deSLogan Gunthorpe 
81080b47deSLogan Gunthorpe 	dev_dbg(&stdev->dev, "%s: %p\n", __func__, stuser);
82080b47deSLogan Gunthorpe 
83080b47deSLogan Gunthorpe 	return stuser;
84080b47deSLogan Gunthorpe }
85080b47deSLogan Gunthorpe 
86080b47deSLogan Gunthorpe static void stuser_free(struct kref *kref)
87080b47deSLogan Gunthorpe {
88080b47deSLogan Gunthorpe 	struct switchtec_user *stuser;
89080b47deSLogan Gunthorpe 
90080b47deSLogan Gunthorpe 	stuser = container_of(kref, struct switchtec_user, kref);
91080b47deSLogan Gunthorpe 
92080b47deSLogan Gunthorpe 	dev_dbg(&stuser->stdev->dev, "%s: %p\n", __func__, stuser);
93080b47deSLogan Gunthorpe 
94080b47deSLogan Gunthorpe 	put_device(&stuser->stdev->dev);
95080b47deSLogan Gunthorpe 	kfree(stuser);
96080b47deSLogan Gunthorpe }
97080b47deSLogan Gunthorpe 
98080b47deSLogan Gunthorpe static void stuser_put(struct switchtec_user *stuser)
99080b47deSLogan Gunthorpe {
100080b47deSLogan Gunthorpe 	kref_put(&stuser->kref, stuser_free);
101080b47deSLogan Gunthorpe }
102080b47deSLogan Gunthorpe 
103080b47deSLogan Gunthorpe static void stuser_set_state(struct switchtec_user *stuser,
104080b47deSLogan Gunthorpe 			     enum mrpc_state state)
105080b47deSLogan Gunthorpe {
106080b47deSLogan Gunthorpe 	/* requires the mrpc_mutex to already be held when called */
107080b47deSLogan Gunthorpe 
108080b47deSLogan Gunthorpe 	const char * const state_names[] = {
109080b47deSLogan Gunthorpe 		[MRPC_IDLE] = "IDLE",
110080b47deSLogan Gunthorpe 		[MRPC_QUEUED] = "QUEUED",
111080b47deSLogan Gunthorpe 		[MRPC_RUNNING] = "RUNNING",
112080b47deSLogan Gunthorpe 		[MRPC_DONE] = "DONE",
113080b47deSLogan Gunthorpe 	};
114080b47deSLogan Gunthorpe 
115080b47deSLogan Gunthorpe 	stuser->state = state;
116080b47deSLogan Gunthorpe 
117080b47deSLogan Gunthorpe 	dev_dbg(&stuser->stdev->dev, "stuser state %p -> %s",
118080b47deSLogan Gunthorpe 		stuser, state_names[state]);
119080b47deSLogan Gunthorpe }
120080b47deSLogan Gunthorpe 
121080b47deSLogan Gunthorpe static void mrpc_complete_cmd(struct switchtec_dev *stdev);
122080b47deSLogan Gunthorpe 
123080b47deSLogan Gunthorpe static void mrpc_cmd_submit(struct switchtec_dev *stdev)
124080b47deSLogan Gunthorpe {
125080b47deSLogan Gunthorpe 	/* requires the mrpc_mutex to already be held when called */
126080b47deSLogan Gunthorpe 
127080b47deSLogan Gunthorpe 	struct switchtec_user *stuser;
128080b47deSLogan Gunthorpe 
129080b47deSLogan Gunthorpe 	if (stdev->mrpc_busy)
130080b47deSLogan Gunthorpe 		return;
131080b47deSLogan Gunthorpe 
132080b47deSLogan Gunthorpe 	if (list_empty(&stdev->mrpc_queue))
133080b47deSLogan Gunthorpe 		return;
134080b47deSLogan Gunthorpe 
135080b47deSLogan Gunthorpe 	stuser = list_entry(stdev->mrpc_queue.next, struct switchtec_user,
136080b47deSLogan Gunthorpe 			    list);
137080b47deSLogan Gunthorpe 
138080b47deSLogan Gunthorpe 	stuser_set_state(stuser, MRPC_RUNNING);
139080b47deSLogan Gunthorpe 	stdev->mrpc_busy = 1;
140080b47deSLogan Gunthorpe 	memcpy_toio(&stdev->mmio_mrpc->input_data,
141080b47deSLogan Gunthorpe 		    stuser->data, stuser->data_len);
142080b47deSLogan Gunthorpe 	iowrite32(stuser->cmd, &stdev->mmio_mrpc->cmd);
143080b47deSLogan Gunthorpe 
144080b47deSLogan Gunthorpe 	stuser->status = ioread32(&stdev->mmio_mrpc->status);
145080b47deSLogan Gunthorpe 	if (stuser->status != SWITCHTEC_MRPC_STATUS_INPROGRESS)
146080b47deSLogan Gunthorpe 		mrpc_complete_cmd(stdev);
147080b47deSLogan Gunthorpe 
148080b47deSLogan Gunthorpe 	schedule_delayed_work(&stdev->mrpc_timeout,
149080b47deSLogan Gunthorpe 			      msecs_to_jiffies(500));
150080b47deSLogan Gunthorpe }
151080b47deSLogan Gunthorpe 
152080b47deSLogan Gunthorpe static int mrpc_queue_cmd(struct switchtec_user *stuser)
153080b47deSLogan Gunthorpe {
154080b47deSLogan Gunthorpe 	/* requires the mrpc_mutex to already be held when called */
155080b47deSLogan Gunthorpe 
156080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev = stuser->stdev;
157080b47deSLogan Gunthorpe 
158080b47deSLogan Gunthorpe 	kref_get(&stuser->kref);
159080b47deSLogan Gunthorpe 	stuser->read_len = sizeof(stuser->data);
160080b47deSLogan Gunthorpe 	stuser_set_state(stuser, MRPC_QUEUED);
161080b47deSLogan Gunthorpe 	init_completion(&stuser->comp);
162080b47deSLogan Gunthorpe 	list_add_tail(&stuser->list, &stdev->mrpc_queue);
163080b47deSLogan Gunthorpe 
164080b47deSLogan Gunthorpe 	mrpc_cmd_submit(stdev);
165080b47deSLogan Gunthorpe 
166080b47deSLogan Gunthorpe 	return 0;
167080b47deSLogan Gunthorpe }
168080b47deSLogan Gunthorpe 
169080b47deSLogan Gunthorpe static void mrpc_complete_cmd(struct switchtec_dev *stdev)
170080b47deSLogan Gunthorpe {
171080b47deSLogan Gunthorpe 	/* requires the mrpc_mutex to already be held when called */
172080b47deSLogan Gunthorpe 	struct switchtec_user *stuser;
173080b47deSLogan Gunthorpe 
174080b47deSLogan Gunthorpe 	if (list_empty(&stdev->mrpc_queue))
175080b47deSLogan Gunthorpe 		return;
176080b47deSLogan Gunthorpe 
177080b47deSLogan Gunthorpe 	stuser = list_entry(stdev->mrpc_queue.next, struct switchtec_user,
178080b47deSLogan Gunthorpe 			    list);
179080b47deSLogan Gunthorpe 
180080b47deSLogan Gunthorpe 	stuser->status = ioread32(&stdev->mmio_mrpc->status);
181080b47deSLogan Gunthorpe 	if (stuser->status == SWITCHTEC_MRPC_STATUS_INPROGRESS)
182080b47deSLogan Gunthorpe 		return;
183080b47deSLogan Gunthorpe 
184080b47deSLogan Gunthorpe 	stuser_set_state(stuser, MRPC_DONE);
185080b47deSLogan Gunthorpe 	stuser->return_code = 0;
186080b47deSLogan Gunthorpe 
187080b47deSLogan Gunthorpe 	if (stuser->status != SWITCHTEC_MRPC_STATUS_DONE)
188080b47deSLogan Gunthorpe 		goto out;
189080b47deSLogan Gunthorpe 
190080b47deSLogan Gunthorpe 	stuser->return_code = ioread32(&stdev->mmio_mrpc->ret_value);
191080b47deSLogan Gunthorpe 	if (stuser->return_code != 0)
192080b47deSLogan Gunthorpe 		goto out;
193080b47deSLogan Gunthorpe 
194080b47deSLogan Gunthorpe 	memcpy_fromio(stuser->data, &stdev->mmio_mrpc->output_data,
195080b47deSLogan Gunthorpe 		      stuser->read_len);
196080b47deSLogan Gunthorpe 
197080b47deSLogan Gunthorpe out:
198080b47deSLogan Gunthorpe 	complete_all(&stuser->comp);
199080b47deSLogan Gunthorpe 	list_del_init(&stuser->list);
200080b47deSLogan Gunthorpe 	stuser_put(stuser);
201080b47deSLogan Gunthorpe 	stdev->mrpc_busy = 0;
202080b47deSLogan Gunthorpe 
203080b47deSLogan Gunthorpe 	mrpc_cmd_submit(stdev);
204080b47deSLogan Gunthorpe }
205080b47deSLogan Gunthorpe 
206080b47deSLogan Gunthorpe static void mrpc_event_work(struct work_struct *work)
207080b47deSLogan Gunthorpe {
208080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev;
209080b47deSLogan Gunthorpe 
210080b47deSLogan Gunthorpe 	stdev = container_of(work, struct switchtec_dev, mrpc_work);
211080b47deSLogan Gunthorpe 
212080b47deSLogan Gunthorpe 	dev_dbg(&stdev->dev, "%s\n", __func__);
213080b47deSLogan Gunthorpe 
214080b47deSLogan Gunthorpe 	mutex_lock(&stdev->mrpc_mutex);
215080b47deSLogan Gunthorpe 	cancel_delayed_work(&stdev->mrpc_timeout);
216080b47deSLogan Gunthorpe 	mrpc_complete_cmd(stdev);
217080b47deSLogan Gunthorpe 	mutex_unlock(&stdev->mrpc_mutex);
218080b47deSLogan Gunthorpe }
219080b47deSLogan Gunthorpe 
220080b47deSLogan Gunthorpe static void mrpc_timeout_work(struct work_struct *work)
221080b47deSLogan Gunthorpe {
222080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev;
223080b47deSLogan Gunthorpe 	u32 status;
224080b47deSLogan Gunthorpe 
225080b47deSLogan Gunthorpe 	stdev = container_of(work, struct switchtec_dev, mrpc_timeout.work);
226080b47deSLogan Gunthorpe 
227080b47deSLogan Gunthorpe 	dev_dbg(&stdev->dev, "%s\n", __func__);
228080b47deSLogan Gunthorpe 
229080b47deSLogan Gunthorpe 	mutex_lock(&stdev->mrpc_mutex);
230080b47deSLogan Gunthorpe 
231080b47deSLogan Gunthorpe 	status = ioread32(&stdev->mmio_mrpc->status);
232080b47deSLogan Gunthorpe 	if (status == SWITCHTEC_MRPC_STATUS_INPROGRESS) {
233080b47deSLogan Gunthorpe 		schedule_delayed_work(&stdev->mrpc_timeout,
234080b47deSLogan Gunthorpe 				      msecs_to_jiffies(500));
235080b47deSLogan Gunthorpe 		goto out;
236080b47deSLogan Gunthorpe 	}
237080b47deSLogan Gunthorpe 
238080b47deSLogan Gunthorpe 	mrpc_complete_cmd(stdev);
239080b47deSLogan Gunthorpe 
240080b47deSLogan Gunthorpe out:
241080b47deSLogan Gunthorpe 	mutex_unlock(&stdev->mrpc_mutex);
242080b47deSLogan Gunthorpe }
243080b47deSLogan Gunthorpe 
2445d8e1881SLogan Gunthorpe static ssize_t device_version_show(struct device *dev,
2455d8e1881SLogan Gunthorpe 	struct device_attribute *attr, char *buf)
2465d8e1881SLogan Gunthorpe {
2475d8e1881SLogan Gunthorpe 	struct switchtec_dev *stdev = to_stdev(dev);
2485d8e1881SLogan Gunthorpe 	u32 ver;
2495d8e1881SLogan Gunthorpe 
2505d8e1881SLogan Gunthorpe 	ver = ioread32(&stdev->mmio_sys_info->device_version);
2515d8e1881SLogan Gunthorpe 
2525d8e1881SLogan Gunthorpe 	return sprintf(buf, "%x\n", ver);
2535d8e1881SLogan Gunthorpe }
2545d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(device_version);
2555d8e1881SLogan Gunthorpe 
2565d8e1881SLogan Gunthorpe static ssize_t fw_version_show(struct device *dev,
2575d8e1881SLogan Gunthorpe 	struct device_attribute *attr, char *buf)
2585d8e1881SLogan Gunthorpe {
2595d8e1881SLogan Gunthorpe 	struct switchtec_dev *stdev = to_stdev(dev);
2605d8e1881SLogan Gunthorpe 	u32 ver;
2615d8e1881SLogan Gunthorpe 
2625d8e1881SLogan Gunthorpe 	ver = ioread32(&stdev->mmio_sys_info->firmware_version);
2635d8e1881SLogan Gunthorpe 
2645d8e1881SLogan Gunthorpe 	return sprintf(buf, "%08x\n", ver);
2655d8e1881SLogan Gunthorpe }
2665d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(fw_version);
2675d8e1881SLogan Gunthorpe 
2685d8e1881SLogan Gunthorpe static ssize_t io_string_show(char *buf, void __iomem *attr, size_t len)
2695d8e1881SLogan Gunthorpe {
2705d8e1881SLogan Gunthorpe 	int i;
2715d8e1881SLogan Gunthorpe 
2725d8e1881SLogan Gunthorpe 	memcpy_fromio(buf, attr, len);
2735d8e1881SLogan Gunthorpe 	buf[len] = '\n';
2745d8e1881SLogan Gunthorpe 	buf[len + 1] = 0;
2755d8e1881SLogan Gunthorpe 
2765d8e1881SLogan Gunthorpe 	for (i = len - 1; i > 0; i--) {
2775d8e1881SLogan Gunthorpe 		if (buf[i] != ' ')
2785d8e1881SLogan Gunthorpe 			break;
2795d8e1881SLogan Gunthorpe 		buf[i] = '\n';
2805d8e1881SLogan Gunthorpe 		buf[i + 1] = 0;
2815d8e1881SLogan Gunthorpe 	}
2825d8e1881SLogan Gunthorpe 
2835d8e1881SLogan Gunthorpe 	return strlen(buf);
2845d8e1881SLogan Gunthorpe }
2855d8e1881SLogan Gunthorpe 
2865d8e1881SLogan Gunthorpe #define DEVICE_ATTR_SYS_INFO_STR(field) \
2875d8e1881SLogan Gunthorpe static ssize_t field ## _show(struct device *dev, \
2885d8e1881SLogan Gunthorpe 	struct device_attribute *attr, char *buf) \
2895d8e1881SLogan Gunthorpe { \
2905d8e1881SLogan Gunthorpe 	struct switchtec_dev *stdev = to_stdev(dev); \
2915d8e1881SLogan Gunthorpe 	return io_string_show(buf, &stdev->mmio_sys_info->field, \
2925d8e1881SLogan Gunthorpe 			    sizeof(stdev->mmio_sys_info->field)); \
2935d8e1881SLogan Gunthorpe } \
2945d8e1881SLogan Gunthorpe \
2955d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(field)
2965d8e1881SLogan Gunthorpe 
2975d8e1881SLogan Gunthorpe DEVICE_ATTR_SYS_INFO_STR(vendor_id);
2985d8e1881SLogan Gunthorpe DEVICE_ATTR_SYS_INFO_STR(product_id);
2995d8e1881SLogan Gunthorpe DEVICE_ATTR_SYS_INFO_STR(product_revision);
3005d8e1881SLogan Gunthorpe DEVICE_ATTR_SYS_INFO_STR(component_vendor);
3015d8e1881SLogan Gunthorpe 
3025d8e1881SLogan Gunthorpe static ssize_t component_id_show(struct device *dev,
3035d8e1881SLogan Gunthorpe 	struct device_attribute *attr, char *buf)
3045d8e1881SLogan Gunthorpe {
3055d8e1881SLogan Gunthorpe 	struct switchtec_dev *stdev = to_stdev(dev);
3065d8e1881SLogan Gunthorpe 	int id = ioread16(&stdev->mmio_sys_info->component_id);
3075d8e1881SLogan Gunthorpe 
3085d8e1881SLogan Gunthorpe 	return sprintf(buf, "PM%04X\n", id);
3095d8e1881SLogan Gunthorpe }
3105d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(component_id);
3115d8e1881SLogan Gunthorpe 
3125d8e1881SLogan Gunthorpe static ssize_t component_revision_show(struct device *dev,
3135d8e1881SLogan Gunthorpe 	struct device_attribute *attr, char *buf)
3145d8e1881SLogan Gunthorpe {
3155d8e1881SLogan Gunthorpe 	struct switchtec_dev *stdev = to_stdev(dev);
3165d8e1881SLogan Gunthorpe 	int rev = ioread8(&stdev->mmio_sys_info->component_revision);
3175d8e1881SLogan Gunthorpe 
3185d8e1881SLogan Gunthorpe 	return sprintf(buf, "%d\n", rev);
3195d8e1881SLogan Gunthorpe }
3205d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(component_revision);
3215d8e1881SLogan Gunthorpe 
3225d8e1881SLogan Gunthorpe static ssize_t partition_show(struct device *dev,
3235d8e1881SLogan Gunthorpe 	struct device_attribute *attr, char *buf)
3245d8e1881SLogan Gunthorpe {
3255d8e1881SLogan Gunthorpe 	struct switchtec_dev *stdev = to_stdev(dev);
3265d8e1881SLogan Gunthorpe 
3275d8e1881SLogan Gunthorpe 	return sprintf(buf, "%d\n", stdev->partition);
3285d8e1881SLogan Gunthorpe }
3295d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(partition);
3305d8e1881SLogan Gunthorpe 
3315d8e1881SLogan Gunthorpe static ssize_t partition_count_show(struct device *dev,
3325d8e1881SLogan Gunthorpe 	struct device_attribute *attr, char *buf)
3335d8e1881SLogan Gunthorpe {
3345d8e1881SLogan Gunthorpe 	struct switchtec_dev *stdev = to_stdev(dev);
3355d8e1881SLogan Gunthorpe 
3365d8e1881SLogan Gunthorpe 	return sprintf(buf, "%d\n", stdev->partition_count);
3375d8e1881SLogan Gunthorpe }
3385d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(partition_count);
3395d8e1881SLogan Gunthorpe 
3405d8e1881SLogan Gunthorpe static struct attribute *switchtec_device_attrs[] = {
3415d8e1881SLogan Gunthorpe 	&dev_attr_device_version.attr,
3425d8e1881SLogan Gunthorpe 	&dev_attr_fw_version.attr,
3435d8e1881SLogan Gunthorpe 	&dev_attr_vendor_id.attr,
3445d8e1881SLogan Gunthorpe 	&dev_attr_product_id.attr,
3455d8e1881SLogan Gunthorpe 	&dev_attr_product_revision.attr,
3465d8e1881SLogan Gunthorpe 	&dev_attr_component_vendor.attr,
3475d8e1881SLogan Gunthorpe 	&dev_attr_component_id.attr,
3485d8e1881SLogan Gunthorpe 	&dev_attr_component_revision.attr,
3495d8e1881SLogan Gunthorpe 	&dev_attr_partition.attr,
3505d8e1881SLogan Gunthorpe 	&dev_attr_partition_count.attr,
3515d8e1881SLogan Gunthorpe 	NULL,
3525d8e1881SLogan Gunthorpe };
3535d8e1881SLogan Gunthorpe 
3545d8e1881SLogan Gunthorpe ATTRIBUTE_GROUPS(switchtec_device);
3555d8e1881SLogan Gunthorpe 
356080b47deSLogan Gunthorpe static int switchtec_dev_open(struct inode *inode, struct file *filp)
357080b47deSLogan Gunthorpe {
358080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev;
359080b47deSLogan Gunthorpe 	struct switchtec_user *stuser;
360080b47deSLogan Gunthorpe 
361080b47deSLogan Gunthorpe 	stdev = container_of(inode->i_cdev, struct switchtec_dev, cdev);
362080b47deSLogan Gunthorpe 
363080b47deSLogan Gunthorpe 	stuser = stuser_create(stdev);
364080b47deSLogan Gunthorpe 	if (IS_ERR(stuser))
365080b47deSLogan Gunthorpe 		return PTR_ERR(stuser);
366080b47deSLogan Gunthorpe 
367080b47deSLogan Gunthorpe 	filp->private_data = stuser;
368080b47deSLogan Gunthorpe 	nonseekable_open(inode, filp);
369080b47deSLogan Gunthorpe 
370080b47deSLogan Gunthorpe 	dev_dbg(&stdev->dev, "%s: %p\n", __func__, stuser);
371080b47deSLogan Gunthorpe 
372080b47deSLogan Gunthorpe 	return 0;
373080b47deSLogan Gunthorpe }
374080b47deSLogan Gunthorpe 
375080b47deSLogan Gunthorpe static int switchtec_dev_release(struct inode *inode, struct file *filp)
376080b47deSLogan Gunthorpe {
377080b47deSLogan Gunthorpe 	struct switchtec_user *stuser = filp->private_data;
378080b47deSLogan Gunthorpe 
379080b47deSLogan Gunthorpe 	stuser_put(stuser);
380080b47deSLogan Gunthorpe 
381080b47deSLogan Gunthorpe 	return 0;
382080b47deSLogan Gunthorpe }
383080b47deSLogan Gunthorpe 
384080b47deSLogan Gunthorpe static int lock_mutex_and_test_alive(struct switchtec_dev *stdev)
385080b47deSLogan Gunthorpe {
386080b47deSLogan Gunthorpe 	if (mutex_lock_interruptible(&stdev->mrpc_mutex))
387080b47deSLogan Gunthorpe 		return -EINTR;
388080b47deSLogan Gunthorpe 
389080b47deSLogan Gunthorpe 	if (!stdev->alive) {
390080b47deSLogan Gunthorpe 		mutex_unlock(&stdev->mrpc_mutex);
391080b47deSLogan Gunthorpe 		return -ENODEV;
392080b47deSLogan Gunthorpe 	}
393080b47deSLogan Gunthorpe 
394080b47deSLogan Gunthorpe 	return 0;
395080b47deSLogan Gunthorpe }
396080b47deSLogan Gunthorpe 
397080b47deSLogan Gunthorpe static ssize_t switchtec_dev_write(struct file *filp, const char __user *data,
398080b47deSLogan Gunthorpe 				   size_t size, loff_t *off)
399080b47deSLogan Gunthorpe {
400080b47deSLogan Gunthorpe 	struct switchtec_user *stuser = filp->private_data;
401080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev = stuser->stdev;
402080b47deSLogan Gunthorpe 	int rc;
403080b47deSLogan Gunthorpe 
404080b47deSLogan Gunthorpe 	if (size < sizeof(stuser->cmd) ||
405080b47deSLogan Gunthorpe 	    size > sizeof(stuser->cmd) + sizeof(stuser->data))
406080b47deSLogan Gunthorpe 		return -EINVAL;
407080b47deSLogan Gunthorpe 
408080b47deSLogan Gunthorpe 	stuser->data_len = size - sizeof(stuser->cmd);
409080b47deSLogan Gunthorpe 
410080b47deSLogan Gunthorpe 	rc = lock_mutex_and_test_alive(stdev);
411080b47deSLogan Gunthorpe 	if (rc)
412080b47deSLogan Gunthorpe 		return rc;
413080b47deSLogan Gunthorpe 
414080b47deSLogan Gunthorpe 	if (stuser->state != MRPC_IDLE) {
415080b47deSLogan Gunthorpe 		rc = -EBADE;
416080b47deSLogan Gunthorpe 		goto out;
417080b47deSLogan Gunthorpe 	}
418080b47deSLogan Gunthorpe 
419080b47deSLogan Gunthorpe 	rc = copy_from_user(&stuser->cmd, data, sizeof(stuser->cmd));
420080b47deSLogan Gunthorpe 	if (rc) {
421080b47deSLogan Gunthorpe 		rc = -EFAULT;
422080b47deSLogan Gunthorpe 		goto out;
423080b47deSLogan Gunthorpe 	}
424080b47deSLogan Gunthorpe 
425080b47deSLogan Gunthorpe 	data += sizeof(stuser->cmd);
426080b47deSLogan Gunthorpe 	rc = copy_from_user(&stuser->data, data, size - sizeof(stuser->cmd));
427080b47deSLogan Gunthorpe 	if (rc) {
428080b47deSLogan Gunthorpe 		rc = -EFAULT;
429080b47deSLogan Gunthorpe 		goto out;
430080b47deSLogan Gunthorpe 	}
431080b47deSLogan Gunthorpe 
432080b47deSLogan Gunthorpe 	rc = mrpc_queue_cmd(stuser);
433080b47deSLogan Gunthorpe 
434080b47deSLogan Gunthorpe out:
435080b47deSLogan Gunthorpe 	mutex_unlock(&stdev->mrpc_mutex);
436080b47deSLogan Gunthorpe 
437080b47deSLogan Gunthorpe 	if (rc)
438080b47deSLogan Gunthorpe 		return rc;
439080b47deSLogan Gunthorpe 
440080b47deSLogan Gunthorpe 	return size;
441080b47deSLogan Gunthorpe }
442080b47deSLogan Gunthorpe 
443080b47deSLogan Gunthorpe static ssize_t switchtec_dev_read(struct file *filp, char __user *data,
444080b47deSLogan Gunthorpe 				  size_t size, loff_t *off)
445080b47deSLogan Gunthorpe {
446080b47deSLogan Gunthorpe 	struct switchtec_user *stuser = filp->private_data;
447080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev = stuser->stdev;
448080b47deSLogan Gunthorpe 	int rc;
449080b47deSLogan Gunthorpe 
450080b47deSLogan Gunthorpe 	if (size < sizeof(stuser->cmd) ||
451080b47deSLogan Gunthorpe 	    size > sizeof(stuser->cmd) + sizeof(stuser->data))
452080b47deSLogan Gunthorpe 		return -EINVAL;
453080b47deSLogan Gunthorpe 
454080b47deSLogan Gunthorpe 	rc = lock_mutex_and_test_alive(stdev);
455080b47deSLogan Gunthorpe 	if (rc)
456080b47deSLogan Gunthorpe 		return rc;
457080b47deSLogan Gunthorpe 
458080b47deSLogan Gunthorpe 	if (stuser->state == MRPC_IDLE) {
459080b47deSLogan Gunthorpe 		mutex_unlock(&stdev->mrpc_mutex);
460080b47deSLogan Gunthorpe 		return -EBADE;
461080b47deSLogan Gunthorpe 	}
462080b47deSLogan Gunthorpe 
463080b47deSLogan Gunthorpe 	stuser->read_len = size - sizeof(stuser->return_code);
464080b47deSLogan Gunthorpe 
465080b47deSLogan Gunthorpe 	mutex_unlock(&stdev->mrpc_mutex);
466080b47deSLogan Gunthorpe 
467080b47deSLogan Gunthorpe 	if (filp->f_flags & O_NONBLOCK) {
468080b47deSLogan Gunthorpe 		if (!try_wait_for_completion(&stuser->comp))
469080b47deSLogan Gunthorpe 			return -EAGAIN;
470080b47deSLogan Gunthorpe 	} else {
471080b47deSLogan Gunthorpe 		rc = wait_for_completion_interruptible(&stuser->comp);
472080b47deSLogan Gunthorpe 		if (rc < 0)
473080b47deSLogan Gunthorpe 			return rc;
474080b47deSLogan Gunthorpe 	}
475080b47deSLogan Gunthorpe 
476080b47deSLogan Gunthorpe 	rc = lock_mutex_and_test_alive(stdev);
477080b47deSLogan Gunthorpe 	if (rc)
478080b47deSLogan Gunthorpe 		return rc;
479080b47deSLogan Gunthorpe 
480080b47deSLogan Gunthorpe 	if (stuser->state != MRPC_DONE) {
481080b47deSLogan Gunthorpe 		mutex_unlock(&stdev->mrpc_mutex);
482080b47deSLogan Gunthorpe 		return -EBADE;
483080b47deSLogan Gunthorpe 	}
484080b47deSLogan Gunthorpe 
485080b47deSLogan Gunthorpe 	rc = copy_to_user(data, &stuser->return_code,
486080b47deSLogan Gunthorpe 			  sizeof(stuser->return_code));
487080b47deSLogan Gunthorpe 	if (rc) {
488080b47deSLogan Gunthorpe 		rc = -EFAULT;
489080b47deSLogan Gunthorpe 		goto out;
490080b47deSLogan Gunthorpe 	}
491080b47deSLogan Gunthorpe 
492080b47deSLogan Gunthorpe 	data += sizeof(stuser->return_code);
493080b47deSLogan Gunthorpe 	rc = copy_to_user(data, &stuser->data,
494080b47deSLogan Gunthorpe 			  size - sizeof(stuser->return_code));
495080b47deSLogan Gunthorpe 	if (rc) {
496080b47deSLogan Gunthorpe 		rc = -EFAULT;
497080b47deSLogan Gunthorpe 		goto out;
498080b47deSLogan Gunthorpe 	}
499080b47deSLogan Gunthorpe 
500080b47deSLogan Gunthorpe 	stuser_set_state(stuser, MRPC_IDLE);
501080b47deSLogan Gunthorpe 
502080b47deSLogan Gunthorpe out:
503080b47deSLogan Gunthorpe 	mutex_unlock(&stdev->mrpc_mutex);
504080b47deSLogan Gunthorpe 
505080b47deSLogan Gunthorpe 	if (stuser->status == SWITCHTEC_MRPC_STATUS_DONE)
506080b47deSLogan Gunthorpe 		return size;
507080b47deSLogan Gunthorpe 	else if (stuser->status == SWITCHTEC_MRPC_STATUS_INTERRUPTED)
508080b47deSLogan Gunthorpe 		return -ENXIO;
509080b47deSLogan Gunthorpe 	else
510080b47deSLogan Gunthorpe 		return -EBADMSG;
511080b47deSLogan Gunthorpe }
512080b47deSLogan Gunthorpe 
513*afc9a42bSAl Viro static __poll_t switchtec_dev_poll(struct file *filp, poll_table *wait)
514080b47deSLogan Gunthorpe {
515080b47deSLogan Gunthorpe 	struct switchtec_user *stuser = filp->private_data;
516080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev = stuser->stdev;
517*afc9a42bSAl Viro 	__poll_t ret = 0;
518080b47deSLogan Gunthorpe 
519080b47deSLogan Gunthorpe 	poll_wait(filp, &stuser->comp.wait, wait);
520080b47deSLogan Gunthorpe 	poll_wait(filp, &stdev->event_wq, wait);
521080b47deSLogan Gunthorpe 
522080b47deSLogan Gunthorpe 	if (lock_mutex_and_test_alive(stdev))
523080b47deSLogan Gunthorpe 		return POLLIN | POLLRDHUP | POLLOUT | POLLERR | POLLHUP;
524080b47deSLogan Gunthorpe 
525080b47deSLogan Gunthorpe 	mutex_unlock(&stdev->mrpc_mutex);
526080b47deSLogan Gunthorpe 
527080b47deSLogan Gunthorpe 	if (try_wait_for_completion(&stuser->comp))
528080b47deSLogan Gunthorpe 		ret |= POLLIN | POLLRDNORM;
529080b47deSLogan Gunthorpe 
530080b47deSLogan Gunthorpe 	if (stuser->event_cnt != atomic_read(&stdev->event_cnt))
531080b47deSLogan Gunthorpe 		ret |= POLLPRI | POLLRDBAND;
532080b47deSLogan Gunthorpe 
533080b47deSLogan Gunthorpe 	return ret;
534080b47deSLogan Gunthorpe }
535080b47deSLogan Gunthorpe 
53652eabba5SLogan Gunthorpe static int ioctl_flash_info(struct switchtec_dev *stdev,
53752eabba5SLogan Gunthorpe 			    struct switchtec_ioctl_flash_info __user *uinfo)
53852eabba5SLogan Gunthorpe {
53952eabba5SLogan Gunthorpe 	struct switchtec_ioctl_flash_info info = {0};
54052eabba5SLogan Gunthorpe 	struct flash_info_regs __iomem *fi = stdev->mmio_flash_info;
54152eabba5SLogan Gunthorpe 
54252eabba5SLogan Gunthorpe 	info.flash_length = ioread32(&fi->flash_length);
54352eabba5SLogan Gunthorpe 	info.num_partitions = SWITCHTEC_IOCTL_NUM_PARTITIONS;
54452eabba5SLogan Gunthorpe 
54552eabba5SLogan Gunthorpe 	if (copy_to_user(uinfo, &info, sizeof(info)))
54652eabba5SLogan Gunthorpe 		return -EFAULT;
54752eabba5SLogan Gunthorpe 
54852eabba5SLogan Gunthorpe 	return 0;
54952eabba5SLogan Gunthorpe }
55052eabba5SLogan Gunthorpe 
55152eabba5SLogan Gunthorpe static void set_fw_info_part(struct switchtec_ioctl_flash_part_info *info,
55252eabba5SLogan Gunthorpe 			     struct partition_info __iomem *pi)
55352eabba5SLogan Gunthorpe {
55452eabba5SLogan Gunthorpe 	info->address = ioread32(&pi->address);
55552eabba5SLogan Gunthorpe 	info->length = ioread32(&pi->length);
55652eabba5SLogan Gunthorpe }
55752eabba5SLogan Gunthorpe 
55852eabba5SLogan Gunthorpe static int ioctl_flash_part_info(struct switchtec_dev *stdev,
55952eabba5SLogan Gunthorpe 	struct switchtec_ioctl_flash_part_info __user *uinfo)
56052eabba5SLogan Gunthorpe {
56152eabba5SLogan Gunthorpe 	struct switchtec_ioctl_flash_part_info info = {0};
56252eabba5SLogan Gunthorpe 	struct flash_info_regs __iomem *fi = stdev->mmio_flash_info;
563079e3bc5SLogan Gunthorpe 	struct sys_info_regs __iomem *si = stdev->mmio_sys_info;
56452eabba5SLogan Gunthorpe 	u32 active_addr = -1;
56552eabba5SLogan Gunthorpe 
56652eabba5SLogan Gunthorpe 	if (copy_from_user(&info, uinfo, sizeof(info)))
56752eabba5SLogan Gunthorpe 		return -EFAULT;
56852eabba5SLogan Gunthorpe 
56952eabba5SLogan Gunthorpe 	switch (info.flash_partition) {
57052eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_CFG0:
57152eabba5SLogan Gunthorpe 		active_addr = ioread32(&fi->active_cfg);
57252eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->cfg0);
573079e3bc5SLogan Gunthorpe 		if (ioread16(&si->cfg_running) == SWITCHTEC_CFG0_RUNNING)
574079e3bc5SLogan Gunthorpe 			info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
57552eabba5SLogan Gunthorpe 		break;
57652eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_CFG1:
57752eabba5SLogan Gunthorpe 		active_addr = ioread32(&fi->active_cfg);
57852eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->cfg1);
579079e3bc5SLogan Gunthorpe 		if (ioread16(&si->cfg_running) == SWITCHTEC_CFG1_RUNNING)
580079e3bc5SLogan Gunthorpe 			info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
58152eabba5SLogan Gunthorpe 		break;
58252eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_IMG0:
58352eabba5SLogan Gunthorpe 		active_addr = ioread32(&fi->active_img);
58452eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->img0);
585079e3bc5SLogan Gunthorpe 		if (ioread16(&si->img_running) == SWITCHTEC_IMG0_RUNNING)
586079e3bc5SLogan Gunthorpe 			info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
58752eabba5SLogan Gunthorpe 		break;
58852eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_IMG1:
58952eabba5SLogan Gunthorpe 		active_addr = ioread32(&fi->active_img);
59052eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->img1);
591079e3bc5SLogan Gunthorpe 		if (ioread16(&si->img_running) == SWITCHTEC_IMG1_RUNNING)
592079e3bc5SLogan Gunthorpe 			info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
59352eabba5SLogan Gunthorpe 		break;
59452eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_NVLOG:
59552eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->nvlog);
59652eabba5SLogan Gunthorpe 		break;
59752eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_VENDOR0:
59852eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->vendor[0]);
59952eabba5SLogan Gunthorpe 		break;
60052eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_VENDOR1:
60152eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->vendor[1]);
60252eabba5SLogan Gunthorpe 		break;
60352eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_VENDOR2:
60452eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->vendor[2]);
60552eabba5SLogan Gunthorpe 		break;
60652eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_VENDOR3:
60752eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->vendor[3]);
60852eabba5SLogan Gunthorpe 		break;
60952eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_VENDOR4:
61052eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->vendor[4]);
61152eabba5SLogan Gunthorpe 		break;
61252eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_VENDOR5:
61352eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->vendor[5]);
61452eabba5SLogan Gunthorpe 		break;
61552eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_VENDOR6:
61652eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->vendor[6]);
61752eabba5SLogan Gunthorpe 		break;
61852eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PART_VENDOR7:
61952eabba5SLogan Gunthorpe 		set_fw_info_part(&info, &fi->vendor[7]);
62052eabba5SLogan Gunthorpe 		break;
62152eabba5SLogan Gunthorpe 	default:
62252eabba5SLogan Gunthorpe 		return -EINVAL;
62352eabba5SLogan Gunthorpe 	}
62452eabba5SLogan Gunthorpe 
62552eabba5SLogan Gunthorpe 	if (info.address == active_addr)
626079e3bc5SLogan Gunthorpe 		info.active |= SWITCHTEC_IOCTL_PART_ACTIVE;
62752eabba5SLogan Gunthorpe 
62852eabba5SLogan Gunthorpe 	if (copy_to_user(uinfo, &info, sizeof(info)))
62952eabba5SLogan Gunthorpe 		return -EFAULT;
63052eabba5SLogan Gunthorpe 
63152eabba5SLogan Gunthorpe 	return 0;
63252eabba5SLogan Gunthorpe }
63352eabba5SLogan Gunthorpe 
63452eabba5SLogan Gunthorpe static int ioctl_event_summary(struct switchtec_dev *stdev,
63552eabba5SLogan Gunthorpe 	struct switchtec_user *stuser,
63652eabba5SLogan Gunthorpe 	struct switchtec_ioctl_event_summary __user *usum)
63752eabba5SLogan Gunthorpe {
63852eabba5SLogan Gunthorpe 	struct switchtec_ioctl_event_summary s = {0};
63952eabba5SLogan Gunthorpe 	int i;
64052eabba5SLogan Gunthorpe 	u32 reg;
64152eabba5SLogan Gunthorpe 
64252eabba5SLogan Gunthorpe 	s.global = ioread32(&stdev->mmio_sw_event->global_summary);
64352eabba5SLogan Gunthorpe 	s.part_bitmap = ioread32(&stdev->mmio_sw_event->part_event_bitmap);
64452eabba5SLogan Gunthorpe 	s.local_part = ioread32(&stdev->mmio_part_cfg->part_event_summary);
64552eabba5SLogan Gunthorpe 
64652eabba5SLogan Gunthorpe 	for (i = 0; i < stdev->partition_count; i++) {
64752eabba5SLogan Gunthorpe 		reg = ioread32(&stdev->mmio_part_cfg_all[i].part_event_summary);
64852eabba5SLogan Gunthorpe 		s.part[i] = reg;
64952eabba5SLogan Gunthorpe 	}
65052eabba5SLogan Gunthorpe 
65152eabba5SLogan Gunthorpe 	for (i = 0; i < SWITCHTEC_MAX_PFF_CSR; i++) {
65252eabba5SLogan Gunthorpe 		reg = ioread16(&stdev->mmio_pff_csr[i].vendor_id);
65352eabba5SLogan Gunthorpe 		if (reg != MICROSEMI_VENDOR_ID)
65452eabba5SLogan Gunthorpe 			break;
65552eabba5SLogan Gunthorpe 
65652eabba5SLogan Gunthorpe 		reg = ioread32(&stdev->mmio_pff_csr[i].pff_event_summary);
65752eabba5SLogan Gunthorpe 		s.pff[i] = reg;
65852eabba5SLogan Gunthorpe 	}
65952eabba5SLogan Gunthorpe 
66052eabba5SLogan Gunthorpe 	if (copy_to_user(usum, &s, sizeof(s)))
66152eabba5SLogan Gunthorpe 		return -EFAULT;
66252eabba5SLogan Gunthorpe 
66352eabba5SLogan Gunthorpe 	stuser->event_cnt = atomic_read(&stdev->event_cnt);
66452eabba5SLogan Gunthorpe 
66552eabba5SLogan Gunthorpe 	return 0;
66652eabba5SLogan Gunthorpe }
66752eabba5SLogan Gunthorpe 
66852eabba5SLogan Gunthorpe static u32 __iomem *global_ev_reg(struct switchtec_dev *stdev,
66952eabba5SLogan Gunthorpe 				  size_t offset, int index)
67052eabba5SLogan Gunthorpe {
67152eabba5SLogan Gunthorpe 	return (void __iomem *)stdev->mmio_sw_event + offset;
67252eabba5SLogan Gunthorpe }
67352eabba5SLogan Gunthorpe 
67452eabba5SLogan Gunthorpe static u32 __iomem *part_ev_reg(struct switchtec_dev *stdev,
67552eabba5SLogan Gunthorpe 				size_t offset, int index)
67652eabba5SLogan Gunthorpe {
67752eabba5SLogan Gunthorpe 	return (void __iomem *)&stdev->mmio_part_cfg_all[index] + offset;
67852eabba5SLogan Gunthorpe }
67952eabba5SLogan Gunthorpe 
68052eabba5SLogan Gunthorpe static u32 __iomem *pff_ev_reg(struct switchtec_dev *stdev,
68152eabba5SLogan Gunthorpe 			       size_t offset, int index)
68252eabba5SLogan Gunthorpe {
68352eabba5SLogan Gunthorpe 	return (void __iomem *)&stdev->mmio_pff_csr[index] + offset;
68452eabba5SLogan Gunthorpe }
68552eabba5SLogan Gunthorpe 
68652eabba5SLogan Gunthorpe #define EV_GLB(i, r)[i] = {offsetof(struct sw_event_regs, r), global_ev_reg}
68752eabba5SLogan Gunthorpe #define EV_PAR(i, r)[i] = {offsetof(struct part_cfg_regs, r), part_ev_reg}
68852eabba5SLogan Gunthorpe #define EV_PFF(i, r)[i] = {offsetof(struct pff_csr_regs, r), pff_ev_reg}
68952eabba5SLogan Gunthorpe 
690f05f7355SColin Ian King static const struct event_reg {
69152eabba5SLogan Gunthorpe 	size_t offset;
69252eabba5SLogan Gunthorpe 	u32 __iomem *(*map_reg)(struct switchtec_dev *stdev,
69352eabba5SLogan Gunthorpe 				size_t offset, int index);
69452eabba5SLogan Gunthorpe } event_regs[] = {
69552eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_STACK_ERROR, stack_error_event_hdr),
69652eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_PPU_ERROR, ppu_error_event_hdr),
69752eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_ISP_ERROR, isp_error_event_hdr),
69852eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_SYS_RESET, sys_reset_event_hdr),
69952eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_EXC, fw_exception_hdr),
70052eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_NMI, fw_nmi_hdr),
70152eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_NON_FATAL, fw_non_fatal_hdr),
70252eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_FATAL, fw_fatal_hdr),
70352eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_TWI_MRPC_COMP, twi_mrpc_comp_hdr),
70452eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_TWI_MRPC_COMP_ASYNC,
70552eabba5SLogan Gunthorpe 	       twi_mrpc_comp_async_hdr),
70652eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_CLI_MRPC_COMP, cli_mrpc_comp_hdr),
70752eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_CLI_MRPC_COMP_ASYNC,
70852eabba5SLogan Gunthorpe 	       cli_mrpc_comp_async_hdr),
70952eabba5SLogan Gunthorpe 	EV_GLB(SWITCHTEC_IOCTL_EVENT_GPIO_INT, gpio_interrupt_hdr),
71052eabba5SLogan Gunthorpe 	EV_PAR(SWITCHTEC_IOCTL_EVENT_PART_RESET, part_reset_hdr),
71152eabba5SLogan Gunthorpe 	EV_PAR(SWITCHTEC_IOCTL_EVENT_MRPC_COMP, mrpc_comp_hdr),
71252eabba5SLogan Gunthorpe 	EV_PAR(SWITCHTEC_IOCTL_EVENT_MRPC_COMP_ASYNC, mrpc_comp_async_hdr),
71352eabba5SLogan Gunthorpe 	EV_PAR(SWITCHTEC_IOCTL_EVENT_DYN_PART_BIND_COMP, dyn_binding_hdr),
71452eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_AER_IN_P2P, aer_in_p2p_hdr),
71552eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_AER_IN_VEP, aer_in_vep_hdr),
71652eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_DPC, dpc_hdr),
71752eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_CTS, cts_hdr),
71852eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_HOTPLUG, hotplug_hdr),
71952eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_IER, ier_hdr),
72052eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_THRESH, threshold_hdr),
72152eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_POWER_MGMT, power_mgmt_hdr),
72252eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_TLP_THROTTLING, tlp_throttling_hdr),
72352eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_FORCE_SPEED, force_speed_hdr),
72452eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_CREDIT_TIMEOUT, credit_timeout_hdr),
72552eabba5SLogan Gunthorpe 	EV_PFF(SWITCHTEC_IOCTL_EVENT_LINK_STATE, link_state_hdr),
72652eabba5SLogan Gunthorpe };
72752eabba5SLogan Gunthorpe 
72852eabba5SLogan Gunthorpe static u32 __iomem *event_hdr_addr(struct switchtec_dev *stdev,
72952eabba5SLogan Gunthorpe 				   int event_id, int index)
73052eabba5SLogan Gunthorpe {
73152eabba5SLogan Gunthorpe 	size_t off;
73252eabba5SLogan Gunthorpe 
73352eabba5SLogan Gunthorpe 	if (event_id < 0 || event_id >= SWITCHTEC_IOCTL_MAX_EVENTS)
73452eabba5SLogan Gunthorpe 		return ERR_PTR(-EINVAL);
73552eabba5SLogan Gunthorpe 
73652eabba5SLogan Gunthorpe 	off = event_regs[event_id].offset;
73752eabba5SLogan Gunthorpe 
73852eabba5SLogan Gunthorpe 	if (event_regs[event_id].map_reg == part_ev_reg) {
73952eabba5SLogan Gunthorpe 		if (index == SWITCHTEC_IOCTL_EVENT_LOCAL_PART_IDX)
74052eabba5SLogan Gunthorpe 			index = stdev->partition;
74152eabba5SLogan Gunthorpe 		else if (index < 0 || index >= stdev->partition_count)
74252eabba5SLogan Gunthorpe 			return ERR_PTR(-EINVAL);
74352eabba5SLogan Gunthorpe 	} else if (event_regs[event_id].map_reg == pff_ev_reg) {
74452eabba5SLogan Gunthorpe 		if (index < 0 || index >= stdev->pff_csr_count)
74552eabba5SLogan Gunthorpe 			return ERR_PTR(-EINVAL);
74652eabba5SLogan Gunthorpe 	}
74752eabba5SLogan Gunthorpe 
74852eabba5SLogan Gunthorpe 	return event_regs[event_id].map_reg(stdev, off, index);
74952eabba5SLogan Gunthorpe }
75052eabba5SLogan Gunthorpe 
75152eabba5SLogan Gunthorpe static int event_ctl(struct switchtec_dev *stdev,
75252eabba5SLogan Gunthorpe 		     struct switchtec_ioctl_event_ctl *ctl)
75352eabba5SLogan Gunthorpe {
75452eabba5SLogan Gunthorpe 	int i;
75552eabba5SLogan Gunthorpe 	u32 __iomem *reg;
75652eabba5SLogan Gunthorpe 	u32 hdr;
75752eabba5SLogan Gunthorpe 
75852eabba5SLogan Gunthorpe 	reg = event_hdr_addr(stdev, ctl->event_id, ctl->index);
75952eabba5SLogan Gunthorpe 	if (IS_ERR(reg))
76052eabba5SLogan Gunthorpe 		return PTR_ERR(reg);
76152eabba5SLogan Gunthorpe 
76252eabba5SLogan Gunthorpe 	hdr = ioread32(reg);
76352eabba5SLogan Gunthorpe 	for (i = 0; i < ARRAY_SIZE(ctl->data); i++)
76452eabba5SLogan Gunthorpe 		ctl->data[i] = ioread32(&reg[i + 1]);
76552eabba5SLogan Gunthorpe 
76652eabba5SLogan Gunthorpe 	ctl->occurred = hdr & SWITCHTEC_EVENT_OCCURRED;
76752eabba5SLogan Gunthorpe 	ctl->count = (hdr >> 5) & 0xFF;
76852eabba5SLogan Gunthorpe 
76952eabba5SLogan Gunthorpe 	if (!(ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_CLEAR))
77052eabba5SLogan Gunthorpe 		hdr &= ~SWITCHTEC_EVENT_CLEAR;
77152eabba5SLogan Gunthorpe 	if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL)
77252eabba5SLogan Gunthorpe 		hdr |= SWITCHTEC_EVENT_EN_IRQ;
77352eabba5SLogan Gunthorpe 	if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_POLL)
77452eabba5SLogan Gunthorpe 		hdr &= ~SWITCHTEC_EVENT_EN_IRQ;
77552eabba5SLogan Gunthorpe 	if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG)
77652eabba5SLogan Gunthorpe 		hdr |= SWITCHTEC_EVENT_EN_LOG;
77752eabba5SLogan Gunthorpe 	if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_LOG)
77852eabba5SLogan Gunthorpe 		hdr &= ~SWITCHTEC_EVENT_EN_LOG;
77952eabba5SLogan Gunthorpe 	if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI)
78052eabba5SLogan Gunthorpe 		hdr |= SWITCHTEC_EVENT_EN_CLI;
78152eabba5SLogan Gunthorpe 	if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_CLI)
78252eabba5SLogan Gunthorpe 		hdr &= ~SWITCHTEC_EVENT_EN_CLI;
78352eabba5SLogan Gunthorpe 	if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL)
78452eabba5SLogan Gunthorpe 		hdr |= SWITCHTEC_EVENT_FATAL;
78552eabba5SLogan Gunthorpe 	if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_FATAL)
78652eabba5SLogan Gunthorpe 		hdr &= ~SWITCHTEC_EVENT_FATAL;
78752eabba5SLogan Gunthorpe 
78852eabba5SLogan Gunthorpe 	if (ctl->flags)
78952eabba5SLogan Gunthorpe 		iowrite32(hdr, reg);
79052eabba5SLogan Gunthorpe 
79152eabba5SLogan Gunthorpe 	ctl->flags = 0;
79252eabba5SLogan Gunthorpe 	if (hdr & SWITCHTEC_EVENT_EN_IRQ)
79352eabba5SLogan Gunthorpe 		ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL;
79452eabba5SLogan Gunthorpe 	if (hdr & SWITCHTEC_EVENT_EN_LOG)
79552eabba5SLogan Gunthorpe 		ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG;
79652eabba5SLogan Gunthorpe 	if (hdr & SWITCHTEC_EVENT_EN_CLI)
79752eabba5SLogan Gunthorpe 		ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI;
79852eabba5SLogan Gunthorpe 	if (hdr & SWITCHTEC_EVENT_FATAL)
79952eabba5SLogan Gunthorpe 		ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL;
80052eabba5SLogan Gunthorpe 
80152eabba5SLogan Gunthorpe 	return 0;
80252eabba5SLogan Gunthorpe }
80352eabba5SLogan Gunthorpe 
80452eabba5SLogan Gunthorpe static int ioctl_event_ctl(struct switchtec_dev *stdev,
80552eabba5SLogan Gunthorpe 	struct switchtec_ioctl_event_ctl __user *uctl)
80652eabba5SLogan Gunthorpe {
80752eabba5SLogan Gunthorpe 	int ret;
80852eabba5SLogan Gunthorpe 	int nr_idxs;
80952eabba5SLogan Gunthorpe 	struct switchtec_ioctl_event_ctl ctl;
81052eabba5SLogan Gunthorpe 
81152eabba5SLogan Gunthorpe 	if (copy_from_user(&ctl, uctl, sizeof(ctl)))
81252eabba5SLogan Gunthorpe 		return -EFAULT;
81352eabba5SLogan Gunthorpe 
81452eabba5SLogan Gunthorpe 	if (ctl.event_id >= SWITCHTEC_IOCTL_MAX_EVENTS)
81552eabba5SLogan Gunthorpe 		return -EINVAL;
81652eabba5SLogan Gunthorpe 
81752eabba5SLogan Gunthorpe 	if (ctl.flags & SWITCHTEC_IOCTL_EVENT_FLAG_UNUSED)
81852eabba5SLogan Gunthorpe 		return -EINVAL;
81952eabba5SLogan Gunthorpe 
82052eabba5SLogan Gunthorpe 	if (ctl.index == SWITCHTEC_IOCTL_EVENT_IDX_ALL) {
82152eabba5SLogan Gunthorpe 		if (event_regs[ctl.event_id].map_reg == global_ev_reg)
82252eabba5SLogan Gunthorpe 			nr_idxs = 1;
82352eabba5SLogan Gunthorpe 		else if (event_regs[ctl.event_id].map_reg == part_ev_reg)
82452eabba5SLogan Gunthorpe 			nr_idxs = stdev->partition_count;
82552eabba5SLogan Gunthorpe 		else if (event_regs[ctl.event_id].map_reg == pff_ev_reg)
82652eabba5SLogan Gunthorpe 			nr_idxs = stdev->pff_csr_count;
82752eabba5SLogan Gunthorpe 		else
82852eabba5SLogan Gunthorpe 			return -EINVAL;
82952eabba5SLogan Gunthorpe 
83052eabba5SLogan Gunthorpe 		for (ctl.index = 0; ctl.index < nr_idxs; ctl.index++) {
83152eabba5SLogan Gunthorpe 			ret = event_ctl(stdev, &ctl);
83252eabba5SLogan Gunthorpe 			if (ret < 0)
83352eabba5SLogan Gunthorpe 				return ret;
83452eabba5SLogan Gunthorpe 		}
83552eabba5SLogan Gunthorpe 	} else {
83652eabba5SLogan Gunthorpe 		ret = event_ctl(stdev, &ctl);
83752eabba5SLogan Gunthorpe 		if (ret < 0)
83852eabba5SLogan Gunthorpe 			return ret;
83952eabba5SLogan Gunthorpe 	}
84052eabba5SLogan Gunthorpe 
84152eabba5SLogan Gunthorpe 	if (copy_to_user(uctl, &ctl, sizeof(ctl)))
84252eabba5SLogan Gunthorpe 		return -EFAULT;
84352eabba5SLogan Gunthorpe 
84452eabba5SLogan Gunthorpe 	return 0;
84552eabba5SLogan Gunthorpe }
84652eabba5SLogan Gunthorpe 
84752eabba5SLogan Gunthorpe static int ioctl_pff_to_port(struct switchtec_dev *stdev,
84852eabba5SLogan Gunthorpe 			     struct switchtec_ioctl_pff_port *up)
84952eabba5SLogan Gunthorpe {
85052eabba5SLogan Gunthorpe 	int i, part;
85152eabba5SLogan Gunthorpe 	u32 reg;
85252eabba5SLogan Gunthorpe 	struct part_cfg_regs *pcfg;
85352eabba5SLogan Gunthorpe 	struct switchtec_ioctl_pff_port p;
85452eabba5SLogan Gunthorpe 
85552eabba5SLogan Gunthorpe 	if (copy_from_user(&p, up, sizeof(p)))
85652eabba5SLogan Gunthorpe 		return -EFAULT;
85752eabba5SLogan Gunthorpe 
85852eabba5SLogan Gunthorpe 	p.port = -1;
85952eabba5SLogan Gunthorpe 	for (part = 0; part < stdev->partition_count; part++) {
86052eabba5SLogan Gunthorpe 		pcfg = &stdev->mmio_part_cfg_all[part];
86152eabba5SLogan Gunthorpe 		p.partition = part;
86252eabba5SLogan Gunthorpe 
86352eabba5SLogan Gunthorpe 		reg = ioread32(&pcfg->usp_pff_inst_id);
86452eabba5SLogan Gunthorpe 		if (reg == p.pff) {
86552eabba5SLogan Gunthorpe 			p.port = 0;
86652eabba5SLogan Gunthorpe 			break;
86752eabba5SLogan Gunthorpe 		}
86852eabba5SLogan Gunthorpe 
86952eabba5SLogan Gunthorpe 		reg = ioread32(&pcfg->vep_pff_inst_id);
87052eabba5SLogan Gunthorpe 		if (reg == p.pff) {
87152eabba5SLogan Gunthorpe 			p.port = SWITCHTEC_IOCTL_PFF_VEP;
87252eabba5SLogan Gunthorpe 			break;
87352eabba5SLogan Gunthorpe 		}
87452eabba5SLogan Gunthorpe 
87552eabba5SLogan Gunthorpe 		for (i = 0; i < ARRAY_SIZE(pcfg->dsp_pff_inst_id); i++) {
87652eabba5SLogan Gunthorpe 			reg = ioread32(&pcfg->dsp_pff_inst_id[i]);
87752eabba5SLogan Gunthorpe 			if (reg != p.pff)
87852eabba5SLogan Gunthorpe 				continue;
87952eabba5SLogan Gunthorpe 
88052eabba5SLogan Gunthorpe 			p.port = i + 1;
88152eabba5SLogan Gunthorpe 			break;
88252eabba5SLogan Gunthorpe 		}
88352eabba5SLogan Gunthorpe 
88452eabba5SLogan Gunthorpe 		if (p.port != -1)
88552eabba5SLogan Gunthorpe 			break;
88652eabba5SLogan Gunthorpe 	}
88752eabba5SLogan Gunthorpe 
88852eabba5SLogan Gunthorpe 	if (copy_to_user(up, &p, sizeof(p)))
88952eabba5SLogan Gunthorpe 		return -EFAULT;
89052eabba5SLogan Gunthorpe 
89152eabba5SLogan Gunthorpe 	return 0;
89252eabba5SLogan Gunthorpe }
89352eabba5SLogan Gunthorpe 
89452eabba5SLogan Gunthorpe static int ioctl_port_to_pff(struct switchtec_dev *stdev,
89552eabba5SLogan Gunthorpe 			     struct switchtec_ioctl_pff_port *up)
89652eabba5SLogan Gunthorpe {
89752eabba5SLogan Gunthorpe 	struct switchtec_ioctl_pff_port p;
89852eabba5SLogan Gunthorpe 	struct part_cfg_regs *pcfg;
89952eabba5SLogan Gunthorpe 
90052eabba5SLogan Gunthorpe 	if (copy_from_user(&p, up, sizeof(p)))
90152eabba5SLogan Gunthorpe 		return -EFAULT;
90252eabba5SLogan Gunthorpe 
90352eabba5SLogan Gunthorpe 	if (p.partition == SWITCHTEC_IOCTL_EVENT_LOCAL_PART_IDX)
90452eabba5SLogan Gunthorpe 		pcfg = stdev->mmio_part_cfg;
90552eabba5SLogan Gunthorpe 	else if (p.partition < stdev->partition_count)
90652eabba5SLogan Gunthorpe 		pcfg = &stdev->mmio_part_cfg_all[p.partition];
90752eabba5SLogan Gunthorpe 	else
90852eabba5SLogan Gunthorpe 		return -EINVAL;
90952eabba5SLogan Gunthorpe 
91052eabba5SLogan Gunthorpe 	switch (p.port) {
91152eabba5SLogan Gunthorpe 	case 0:
91252eabba5SLogan Gunthorpe 		p.pff = ioread32(&pcfg->usp_pff_inst_id);
91352eabba5SLogan Gunthorpe 		break;
91452eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PFF_VEP:
91552eabba5SLogan Gunthorpe 		p.pff = ioread32(&pcfg->vep_pff_inst_id);
91652eabba5SLogan Gunthorpe 		break;
91752eabba5SLogan Gunthorpe 	default:
91852eabba5SLogan Gunthorpe 		if (p.port > ARRAY_SIZE(pcfg->dsp_pff_inst_id))
91952eabba5SLogan Gunthorpe 			return -EINVAL;
92052eabba5SLogan Gunthorpe 		p.pff = ioread32(&pcfg->dsp_pff_inst_id[p.port - 1]);
92152eabba5SLogan Gunthorpe 		break;
92252eabba5SLogan Gunthorpe 	}
92352eabba5SLogan Gunthorpe 
92452eabba5SLogan Gunthorpe 	if (copy_to_user(up, &p, sizeof(p)))
92552eabba5SLogan Gunthorpe 		return -EFAULT;
92652eabba5SLogan Gunthorpe 
92752eabba5SLogan Gunthorpe 	return 0;
92852eabba5SLogan Gunthorpe }
92952eabba5SLogan Gunthorpe 
93052eabba5SLogan Gunthorpe static long switchtec_dev_ioctl(struct file *filp, unsigned int cmd,
93152eabba5SLogan Gunthorpe 				unsigned long arg)
93252eabba5SLogan Gunthorpe {
93352eabba5SLogan Gunthorpe 	struct switchtec_user *stuser = filp->private_data;
93452eabba5SLogan Gunthorpe 	struct switchtec_dev *stdev = stuser->stdev;
93552eabba5SLogan Gunthorpe 	int rc;
93652eabba5SLogan Gunthorpe 	void __user *argp = (void __user *)arg;
93752eabba5SLogan Gunthorpe 
93852eabba5SLogan Gunthorpe 	rc = lock_mutex_and_test_alive(stdev);
93952eabba5SLogan Gunthorpe 	if (rc)
94052eabba5SLogan Gunthorpe 		return rc;
94152eabba5SLogan Gunthorpe 
94252eabba5SLogan Gunthorpe 	switch (cmd) {
94352eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_FLASH_INFO:
94452eabba5SLogan Gunthorpe 		rc = ioctl_flash_info(stdev, argp);
94552eabba5SLogan Gunthorpe 		break;
94652eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_FLASH_PART_INFO:
94752eabba5SLogan Gunthorpe 		rc = ioctl_flash_part_info(stdev, argp);
94852eabba5SLogan Gunthorpe 		break;
94952eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_EVENT_SUMMARY:
95052eabba5SLogan Gunthorpe 		rc = ioctl_event_summary(stdev, stuser, argp);
95152eabba5SLogan Gunthorpe 		break;
95252eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_EVENT_CTL:
95352eabba5SLogan Gunthorpe 		rc = ioctl_event_ctl(stdev, argp);
95452eabba5SLogan Gunthorpe 		break;
95552eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PFF_TO_PORT:
95652eabba5SLogan Gunthorpe 		rc = ioctl_pff_to_port(stdev, argp);
95752eabba5SLogan Gunthorpe 		break;
95852eabba5SLogan Gunthorpe 	case SWITCHTEC_IOCTL_PORT_TO_PFF:
95952eabba5SLogan Gunthorpe 		rc = ioctl_port_to_pff(stdev, argp);
96052eabba5SLogan Gunthorpe 		break;
96152eabba5SLogan Gunthorpe 	default:
96252eabba5SLogan Gunthorpe 		rc = -ENOTTY;
96352eabba5SLogan Gunthorpe 		break;
96452eabba5SLogan Gunthorpe 	}
96552eabba5SLogan Gunthorpe 
96652eabba5SLogan Gunthorpe 	mutex_unlock(&stdev->mrpc_mutex);
96752eabba5SLogan Gunthorpe 	return rc;
96852eabba5SLogan Gunthorpe }
96952eabba5SLogan Gunthorpe 
970080b47deSLogan Gunthorpe static const struct file_operations switchtec_fops = {
971080b47deSLogan Gunthorpe 	.owner = THIS_MODULE,
972080b47deSLogan Gunthorpe 	.open = switchtec_dev_open,
973080b47deSLogan Gunthorpe 	.release = switchtec_dev_release,
974080b47deSLogan Gunthorpe 	.write = switchtec_dev_write,
975080b47deSLogan Gunthorpe 	.read = switchtec_dev_read,
976080b47deSLogan Gunthorpe 	.poll = switchtec_dev_poll,
97752eabba5SLogan Gunthorpe 	.unlocked_ioctl = switchtec_dev_ioctl,
97852eabba5SLogan Gunthorpe 	.compat_ioctl = switchtec_dev_ioctl,
979080b47deSLogan Gunthorpe };
980080b47deSLogan Gunthorpe 
98148c302dcSLogan Gunthorpe static void link_event_work(struct work_struct *work)
98248c302dcSLogan Gunthorpe {
98348c302dcSLogan Gunthorpe 	struct switchtec_dev *stdev;
98448c302dcSLogan Gunthorpe 
98548c302dcSLogan Gunthorpe 	stdev = container_of(work, struct switchtec_dev, link_event_work);
98648c302dcSLogan Gunthorpe 
98748c302dcSLogan Gunthorpe 	if (stdev->link_notifier)
98848c302dcSLogan Gunthorpe 		stdev->link_notifier(stdev);
98948c302dcSLogan Gunthorpe }
99048c302dcSLogan Gunthorpe 
99148c302dcSLogan Gunthorpe static void check_link_state_events(struct switchtec_dev *stdev)
99248c302dcSLogan Gunthorpe {
99348c302dcSLogan Gunthorpe 	int idx;
99448c302dcSLogan Gunthorpe 	u32 reg;
99548c302dcSLogan Gunthorpe 	int count;
99648c302dcSLogan Gunthorpe 	int occurred = 0;
99748c302dcSLogan Gunthorpe 
99848c302dcSLogan Gunthorpe 	for (idx = 0; idx < stdev->pff_csr_count; idx++) {
99948c302dcSLogan Gunthorpe 		reg = ioread32(&stdev->mmio_pff_csr[idx].link_state_hdr);
100048c302dcSLogan Gunthorpe 		dev_dbg(&stdev->dev, "link_state: %d->%08x\n", idx, reg);
100148c302dcSLogan Gunthorpe 		count = (reg >> 5) & 0xFF;
100248c302dcSLogan Gunthorpe 
100348c302dcSLogan Gunthorpe 		if (count != stdev->link_event_count[idx]) {
100448c302dcSLogan Gunthorpe 			occurred = 1;
100548c302dcSLogan Gunthorpe 			stdev->link_event_count[idx] = count;
100648c302dcSLogan Gunthorpe 		}
100748c302dcSLogan Gunthorpe 	}
100848c302dcSLogan Gunthorpe 
100948c302dcSLogan Gunthorpe 	if (occurred)
101048c302dcSLogan Gunthorpe 		schedule_work(&stdev->link_event_work);
101148c302dcSLogan Gunthorpe }
101248c302dcSLogan Gunthorpe 
101348c302dcSLogan Gunthorpe static void enable_link_state_events(struct switchtec_dev *stdev)
101448c302dcSLogan Gunthorpe {
101548c302dcSLogan Gunthorpe 	int idx;
101648c302dcSLogan Gunthorpe 
101748c302dcSLogan Gunthorpe 	for (idx = 0; idx < stdev->pff_csr_count; idx++) {
101848c302dcSLogan Gunthorpe 		iowrite32(SWITCHTEC_EVENT_CLEAR |
101948c302dcSLogan Gunthorpe 			  SWITCHTEC_EVENT_EN_IRQ,
102048c302dcSLogan Gunthorpe 			  &stdev->mmio_pff_csr[idx].link_state_hdr);
102148c302dcSLogan Gunthorpe 	}
102248c302dcSLogan Gunthorpe }
102348c302dcSLogan Gunthorpe 
1024080b47deSLogan Gunthorpe static void stdev_release(struct device *dev)
1025080b47deSLogan Gunthorpe {
1026080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev = to_stdev(dev);
1027080b47deSLogan Gunthorpe 
1028080b47deSLogan Gunthorpe 	kfree(stdev);
1029080b47deSLogan Gunthorpe }
1030080b47deSLogan Gunthorpe 
1031080b47deSLogan Gunthorpe static void stdev_kill(struct switchtec_dev *stdev)
1032080b47deSLogan Gunthorpe {
1033080b47deSLogan Gunthorpe 	struct switchtec_user *stuser, *tmpuser;
1034080b47deSLogan Gunthorpe 
1035080b47deSLogan Gunthorpe 	pci_clear_master(stdev->pdev);
1036080b47deSLogan Gunthorpe 
1037080b47deSLogan Gunthorpe 	cancel_delayed_work_sync(&stdev->mrpc_timeout);
1038080b47deSLogan Gunthorpe 
1039080b47deSLogan Gunthorpe 	/* Mark the hardware as unavailable and complete all completions */
1040080b47deSLogan Gunthorpe 	mutex_lock(&stdev->mrpc_mutex);
1041080b47deSLogan Gunthorpe 	stdev->alive = false;
1042080b47deSLogan Gunthorpe 
1043080b47deSLogan Gunthorpe 	/* Wake up and kill any users waiting on an MRPC request */
1044080b47deSLogan Gunthorpe 	list_for_each_entry_safe(stuser, tmpuser, &stdev->mrpc_queue, list) {
1045080b47deSLogan Gunthorpe 		complete_all(&stuser->comp);
1046080b47deSLogan Gunthorpe 		list_del_init(&stuser->list);
1047080b47deSLogan Gunthorpe 		stuser_put(stuser);
1048080b47deSLogan Gunthorpe 	}
1049080b47deSLogan Gunthorpe 
1050080b47deSLogan Gunthorpe 	mutex_unlock(&stdev->mrpc_mutex);
1051080b47deSLogan Gunthorpe 
1052080b47deSLogan Gunthorpe 	/* Wake up any users waiting on event_wq */
1053080b47deSLogan Gunthorpe 	wake_up_interruptible(&stdev->event_wq);
1054080b47deSLogan Gunthorpe }
1055080b47deSLogan Gunthorpe 
1056080b47deSLogan Gunthorpe static struct switchtec_dev *stdev_create(struct pci_dev *pdev)
1057080b47deSLogan Gunthorpe {
1058080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev;
1059080b47deSLogan Gunthorpe 	int minor;
1060080b47deSLogan Gunthorpe 	struct device *dev;
1061080b47deSLogan Gunthorpe 	struct cdev *cdev;
1062080b47deSLogan Gunthorpe 	int rc;
1063080b47deSLogan Gunthorpe 
1064080b47deSLogan Gunthorpe 	stdev = kzalloc_node(sizeof(*stdev), GFP_KERNEL,
1065080b47deSLogan Gunthorpe 			     dev_to_node(&pdev->dev));
1066080b47deSLogan Gunthorpe 	if (!stdev)
1067080b47deSLogan Gunthorpe 		return ERR_PTR(-ENOMEM);
1068080b47deSLogan Gunthorpe 
1069080b47deSLogan Gunthorpe 	stdev->alive = true;
1070080b47deSLogan Gunthorpe 	stdev->pdev = pdev;
1071080b47deSLogan Gunthorpe 	INIT_LIST_HEAD(&stdev->mrpc_queue);
1072080b47deSLogan Gunthorpe 	mutex_init(&stdev->mrpc_mutex);
1073080b47deSLogan Gunthorpe 	stdev->mrpc_busy = 0;
1074080b47deSLogan Gunthorpe 	INIT_WORK(&stdev->mrpc_work, mrpc_event_work);
1075080b47deSLogan Gunthorpe 	INIT_DELAYED_WORK(&stdev->mrpc_timeout, mrpc_timeout_work);
107648c302dcSLogan Gunthorpe 	INIT_WORK(&stdev->link_event_work, link_event_work);
1077080b47deSLogan Gunthorpe 	init_waitqueue_head(&stdev->event_wq);
1078080b47deSLogan Gunthorpe 	atomic_set(&stdev->event_cnt, 0);
1079080b47deSLogan Gunthorpe 
1080080b47deSLogan Gunthorpe 	dev = &stdev->dev;
1081080b47deSLogan Gunthorpe 	device_initialize(dev);
1082080b47deSLogan Gunthorpe 	dev->class = switchtec_class;
1083080b47deSLogan Gunthorpe 	dev->parent = &pdev->dev;
10845d8e1881SLogan Gunthorpe 	dev->groups = switchtec_device_groups;
1085080b47deSLogan Gunthorpe 	dev->release = stdev_release;
1086080b47deSLogan Gunthorpe 
1087080b47deSLogan Gunthorpe 	minor = ida_simple_get(&switchtec_minor_ida, 0, 0,
1088080b47deSLogan Gunthorpe 			       GFP_KERNEL);
1089080b47deSLogan Gunthorpe 	if (minor < 0) {
1090080b47deSLogan Gunthorpe 		rc = minor;
1091080b47deSLogan Gunthorpe 		goto err_put;
1092080b47deSLogan Gunthorpe 	}
1093080b47deSLogan Gunthorpe 
1094080b47deSLogan Gunthorpe 	dev->devt = MKDEV(MAJOR(switchtec_devt), minor);
1095080b47deSLogan Gunthorpe 	dev_set_name(dev, "switchtec%d", minor);
1096080b47deSLogan Gunthorpe 
1097080b47deSLogan Gunthorpe 	cdev = &stdev->cdev;
1098080b47deSLogan Gunthorpe 	cdev_init(cdev, &switchtec_fops);
1099080b47deSLogan Gunthorpe 	cdev->owner = THIS_MODULE;
1100080b47deSLogan Gunthorpe 
1101080b47deSLogan Gunthorpe 	return stdev;
1102080b47deSLogan Gunthorpe 
1103080b47deSLogan Gunthorpe err_put:
1104080b47deSLogan Gunthorpe 	put_device(&stdev->dev);
1105080b47deSLogan Gunthorpe 	return ERR_PTR(rc);
1106080b47deSLogan Gunthorpe }
1107080b47deSLogan Gunthorpe 
110852eabba5SLogan Gunthorpe static int mask_event(struct switchtec_dev *stdev, int eid, int idx)
110952eabba5SLogan Gunthorpe {
111052eabba5SLogan Gunthorpe 	size_t off = event_regs[eid].offset;
111152eabba5SLogan Gunthorpe 	u32 __iomem *hdr_reg;
111252eabba5SLogan Gunthorpe 	u32 hdr;
111352eabba5SLogan Gunthorpe 
111452eabba5SLogan Gunthorpe 	hdr_reg = event_regs[eid].map_reg(stdev, off, idx);
111552eabba5SLogan Gunthorpe 	hdr = ioread32(hdr_reg);
111652eabba5SLogan Gunthorpe 
111752eabba5SLogan Gunthorpe 	if (!(hdr & SWITCHTEC_EVENT_OCCURRED && hdr & SWITCHTEC_EVENT_EN_IRQ))
111852eabba5SLogan Gunthorpe 		return 0;
111952eabba5SLogan Gunthorpe 
112048c302dcSLogan Gunthorpe 	if (eid == SWITCHTEC_IOCTL_EVENT_LINK_STATE)
112148c302dcSLogan Gunthorpe 		return 0;
112248c302dcSLogan Gunthorpe 
112352eabba5SLogan Gunthorpe 	dev_dbg(&stdev->dev, "%s: %d %d %x\n", __func__, eid, idx, hdr);
112452eabba5SLogan Gunthorpe 	hdr &= ~(SWITCHTEC_EVENT_EN_IRQ | SWITCHTEC_EVENT_OCCURRED);
112552eabba5SLogan Gunthorpe 	iowrite32(hdr, hdr_reg);
112652eabba5SLogan Gunthorpe 
112752eabba5SLogan Gunthorpe 	return 1;
112852eabba5SLogan Gunthorpe }
112952eabba5SLogan Gunthorpe 
113052eabba5SLogan Gunthorpe static int mask_all_events(struct switchtec_dev *stdev, int eid)
113152eabba5SLogan Gunthorpe {
113252eabba5SLogan Gunthorpe 	int idx;
113352eabba5SLogan Gunthorpe 	int count = 0;
113452eabba5SLogan Gunthorpe 
113552eabba5SLogan Gunthorpe 	if (event_regs[eid].map_reg == part_ev_reg) {
113652eabba5SLogan Gunthorpe 		for (idx = 0; idx < stdev->partition_count; idx++)
113752eabba5SLogan Gunthorpe 			count += mask_event(stdev, eid, idx);
113852eabba5SLogan Gunthorpe 	} else if (event_regs[eid].map_reg == pff_ev_reg) {
113952eabba5SLogan Gunthorpe 		for (idx = 0; idx < stdev->pff_csr_count; idx++) {
114052eabba5SLogan Gunthorpe 			if (!stdev->pff_local[idx])
114152eabba5SLogan Gunthorpe 				continue;
114248c302dcSLogan Gunthorpe 
114352eabba5SLogan Gunthorpe 			count += mask_event(stdev, eid, idx);
114452eabba5SLogan Gunthorpe 		}
114552eabba5SLogan Gunthorpe 	} else {
114652eabba5SLogan Gunthorpe 		count += mask_event(stdev, eid, 0);
114752eabba5SLogan Gunthorpe 	}
114852eabba5SLogan Gunthorpe 
114952eabba5SLogan Gunthorpe 	return count;
115052eabba5SLogan Gunthorpe }
115152eabba5SLogan Gunthorpe 
1152080b47deSLogan Gunthorpe static irqreturn_t switchtec_event_isr(int irq, void *dev)
1153080b47deSLogan Gunthorpe {
1154080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev = dev;
1155080b47deSLogan Gunthorpe 	u32 reg;
1156080b47deSLogan Gunthorpe 	irqreturn_t ret = IRQ_NONE;
115752eabba5SLogan Gunthorpe 	int eid, event_count = 0;
1158080b47deSLogan Gunthorpe 
1159080b47deSLogan Gunthorpe 	reg = ioread32(&stdev->mmio_part_cfg->mrpc_comp_hdr);
1160080b47deSLogan Gunthorpe 	if (reg & SWITCHTEC_EVENT_OCCURRED) {
1161080b47deSLogan Gunthorpe 		dev_dbg(&stdev->dev, "%s: mrpc comp\n", __func__);
1162080b47deSLogan Gunthorpe 		ret = IRQ_HANDLED;
1163080b47deSLogan Gunthorpe 		schedule_work(&stdev->mrpc_work);
1164080b47deSLogan Gunthorpe 		iowrite32(reg, &stdev->mmio_part_cfg->mrpc_comp_hdr);
1165080b47deSLogan Gunthorpe 	}
1166080b47deSLogan Gunthorpe 
116748c302dcSLogan Gunthorpe 	check_link_state_events(stdev);
116848c302dcSLogan Gunthorpe 
116952eabba5SLogan Gunthorpe 	for (eid = 0; eid < SWITCHTEC_IOCTL_MAX_EVENTS; eid++)
117052eabba5SLogan Gunthorpe 		event_count += mask_all_events(stdev, eid);
117152eabba5SLogan Gunthorpe 
117252eabba5SLogan Gunthorpe 	if (event_count) {
117352eabba5SLogan Gunthorpe 		atomic_inc(&stdev->event_cnt);
117452eabba5SLogan Gunthorpe 		wake_up_interruptible(&stdev->event_wq);
117552eabba5SLogan Gunthorpe 		dev_dbg(&stdev->dev, "%s: %d events\n", __func__,
117652eabba5SLogan Gunthorpe 			event_count);
117752eabba5SLogan Gunthorpe 		return IRQ_HANDLED;
117852eabba5SLogan Gunthorpe 	}
117952eabba5SLogan Gunthorpe 
1180080b47deSLogan Gunthorpe 	return ret;
1181080b47deSLogan Gunthorpe }
1182080b47deSLogan Gunthorpe 
1183080b47deSLogan Gunthorpe static int switchtec_init_isr(struct switchtec_dev *stdev)
1184080b47deSLogan Gunthorpe {
1185080b47deSLogan Gunthorpe 	int nvecs;
1186080b47deSLogan Gunthorpe 	int event_irq;
1187080b47deSLogan Gunthorpe 
1188080b47deSLogan Gunthorpe 	nvecs = pci_alloc_irq_vectors(stdev->pdev, 1, 4,
1189080b47deSLogan Gunthorpe 				      PCI_IRQ_MSIX | PCI_IRQ_MSI);
1190080b47deSLogan Gunthorpe 	if (nvecs < 0)
1191080b47deSLogan Gunthorpe 		return nvecs;
1192080b47deSLogan Gunthorpe 
1193080b47deSLogan Gunthorpe 	event_irq = ioread32(&stdev->mmio_part_cfg->vep_vector_number);
1194080b47deSLogan Gunthorpe 	if (event_irq < 0 || event_irq >= nvecs)
1195080b47deSLogan Gunthorpe 		return -EFAULT;
1196080b47deSLogan Gunthorpe 
1197080b47deSLogan Gunthorpe 	event_irq = pci_irq_vector(stdev->pdev, event_irq);
1198080b47deSLogan Gunthorpe 	if (event_irq < 0)
1199080b47deSLogan Gunthorpe 		return event_irq;
1200080b47deSLogan Gunthorpe 
1201080b47deSLogan Gunthorpe 	return devm_request_irq(&stdev->pdev->dev, event_irq,
1202080b47deSLogan Gunthorpe 				switchtec_event_isr, 0,
1203080b47deSLogan Gunthorpe 				KBUILD_MODNAME, stdev);
1204080b47deSLogan Gunthorpe }
1205080b47deSLogan Gunthorpe 
1206080b47deSLogan Gunthorpe static void init_pff(struct switchtec_dev *stdev)
1207080b47deSLogan Gunthorpe {
1208080b47deSLogan Gunthorpe 	int i;
1209080b47deSLogan Gunthorpe 	u32 reg;
1210080b47deSLogan Gunthorpe 	struct part_cfg_regs *pcfg = stdev->mmio_part_cfg;
1211080b47deSLogan Gunthorpe 
1212080b47deSLogan Gunthorpe 	for (i = 0; i < SWITCHTEC_MAX_PFF_CSR; i++) {
1213080b47deSLogan Gunthorpe 		reg = ioread16(&stdev->mmio_pff_csr[i].vendor_id);
1214080b47deSLogan Gunthorpe 		if (reg != MICROSEMI_VENDOR_ID)
1215080b47deSLogan Gunthorpe 			break;
1216080b47deSLogan Gunthorpe 	}
1217080b47deSLogan Gunthorpe 
1218080b47deSLogan Gunthorpe 	stdev->pff_csr_count = i;
1219080b47deSLogan Gunthorpe 
1220080b47deSLogan Gunthorpe 	reg = ioread32(&pcfg->usp_pff_inst_id);
1221080b47deSLogan Gunthorpe 	if (reg < SWITCHTEC_MAX_PFF_CSR)
1222080b47deSLogan Gunthorpe 		stdev->pff_local[reg] = 1;
1223080b47deSLogan Gunthorpe 
1224080b47deSLogan Gunthorpe 	reg = ioread32(&pcfg->vep_pff_inst_id);
1225080b47deSLogan Gunthorpe 	if (reg < SWITCHTEC_MAX_PFF_CSR)
1226080b47deSLogan Gunthorpe 		stdev->pff_local[reg] = 1;
1227080b47deSLogan Gunthorpe 
1228080b47deSLogan Gunthorpe 	for (i = 0; i < ARRAY_SIZE(pcfg->dsp_pff_inst_id); i++) {
1229080b47deSLogan Gunthorpe 		reg = ioread32(&pcfg->dsp_pff_inst_id[i]);
1230080b47deSLogan Gunthorpe 		if (reg < SWITCHTEC_MAX_PFF_CSR)
1231080b47deSLogan Gunthorpe 			stdev->pff_local[reg] = 1;
1232080b47deSLogan Gunthorpe 	}
1233080b47deSLogan Gunthorpe }
1234080b47deSLogan Gunthorpe 
1235080b47deSLogan Gunthorpe static int switchtec_init_pci(struct switchtec_dev *stdev,
1236080b47deSLogan Gunthorpe 			      struct pci_dev *pdev)
1237080b47deSLogan Gunthorpe {
1238080b47deSLogan Gunthorpe 	int rc;
1239080b47deSLogan Gunthorpe 
1240080b47deSLogan Gunthorpe 	rc = pcim_enable_device(pdev);
1241080b47deSLogan Gunthorpe 	if (rc)
1242080b47deSLogan Gunthorpe 		return rc;
1243080b47deSLogan Gunthorpe 
1244080b47deSLogan Gunthorpe 	rc = pcim_iomap_regions(pdev, 0x1, KBUILD_MODNAME);
1245080b47deSLogan Gunthorpe 	if (rc)
1246080b47deSLogan Gunthorpe 		return rc;
1247080b47deSLogan Gunthorpe 
1248080b47deSLogan Gunthorpe 	pci_set_master(pdev);
1249080b47deSLogan Gunthorpe 
1250080b47deSLogan Gunthorpe 	stdev->mmio = pcim_iomap_table(pdev)[0];
1251080b47deSLogan Gunthorpe 	stdev->mmio_mrpc = stdev->mmio + SWITCHTEC_GAS_MRPC_OFFSET;
1252080b47deSLogan Gunthorpe 	stdev->mmio_sw_event = stdev->mmio + SWITCHTEC_GAS_SW_EVENT_OFFSET;
1253080b47deSLogan Gunthorpe 	stdev->mmio_sys_info = stdev->mmio + SWITCHTEC_GAS_SYS_INFO_OFFSET;
1254080b47deSLogan Gunthorpe 	stdev->mmio_flash_info = stdev->mmio + SWITCHTEC_GAS_FLASH_INFO_OFFSET;
1255080b47deSLogan Gunthorpe 	stdev->mmio_ntb = stdev->mmio + SWITCHTEC_GAS_NTB_OFFSET;
12569871e9bbSLogan Gunthorpe 	stdev->partition = ioread8(&stdev->mmio_sys_info->partition_id);
1257080b47deSLogan Gunthorpe 	stdev->partition_count = ioread8(&stdev->mmio_ntb->partition_count);
1258080b47deSLogan Gunthorpe 	stdev->mmio_part_cfg_all = stdev->mmio + SWITCHTEC_GAS_PART_CFG_OFFSET;
1259080b47deSLogan Gunthorpe 	stdev->mmio_part_cfg = &stdev->mmio_part_cfg_all[stdev->partition];
1260080b47deSLogan Gunthorpe 	stdev->mmio_pff_csr = stdev->mmio + SWITCHTEC_GAS_PFF_CSR_OFFSET;
1261080b47deSLogan Gunthorpe 
12629871e9bbSLogan Gunthorpe 	if (stdev->partition_count < 1)
12639871e9bbSLogan Gunthorpe 		stdev->partition_count = 1;
12649871e9bbSLogan Gunthorpe 
1265080b47deSLogan Gunthorpe 	init_pff(stdev);
1266080b47deSLogan Gunthorpe 
1267080b47deSLogan Gunthorpe 	pci_set_drvdata(pdev, stdev);
1268080b47deSLogan Gunthorpe 
1269080b47deSLogan Gunthorpe 	return 0;
1270080b47deSLogan Gunthorpe }
1271080b47deSLogan Gunthorpe 
1272080b47deSLogan Gunthorpe static int switchtec_pci_probe(struct pci_dev *pdev,
1273080b47deSLogan Gunthorpe 			       const struct pci_device_id *id)
1274080b47deSLogan Gunthorpe {
1275080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev;
1276080b47deSLogan Gunthorpe 	int rc;
1277080b47deSLogan Gunthorpe 
127833dea5aaSLogan Gunthorpe 	if (pdev->class == MICROSEMI_NTB_CLASSCODE)
127933dea5aaSLogan Gunthorpe 		request_module_nowait("ntb_hw_switchtec");
128033dea5aaSLogan Gunthorpe 
1281080b47deSLogan Gunthorpe 	stdev = stdev_create(pdev);
1282080b47deSLogan Gunthorpe 	if (IS_ERR(stdev))
1283080b47deSLogan Gunthorpe 		return PTR_ERR(stdev);
1284080b47deSLogan Gunthorpe 
1285080b47deSLogan Gunthorpe 	rc = switchtec_init_pci(stdev, pdev);
1286080b47deSLogan Gunthorpe 	if (rc)
1287080b47deSLogan Gunthorpe 		goto err_put;
1288080b47deSLogan Gunthorpe 
1289080b47deSLogan Gunthorpe 	rc = switchtec_init_isr(stdev);
1290080b47deSLogan Gunthorpe 	if (rc) {
1291080b47deSLogan Gunthorpe 		dev_err(&stdev->dev, "failed to init isr.\n");
1292080b47deSLogan Gunthorpe 		goto err_put;
1293080b47deSLogan Gunthorpe 	}
1294080b47deSLogan Gunthorpe 
1295080b47deSLogan Gunthorpe 	iowrite32(SWITCHTEC_EVENT_CLEAR |
1296080b47deSLogan Gunthorpe 		  SWITCHTEC_EVENT_EN_IRQ,
1297080b47deSLogan Gunthorpe 		  &stdev->mmio_part_cfg->mrpc_comp_hdr);
129848c302dcSLogan Gunthorpe 	enable_link_state_events(stdev);
1299080b47deSLogan Gunthorpe 
1300e40cf640SLogan Gunthorpe 	rc = cdev_device_add(&stdev->cdev, &stdev->dev);
1301080b47deSLogan Gunthorpe 	if (rc)
1302080b47deSLogan Gunthorpe 		goto err_devadd;
1303080b47deSLogan Gunthorpe 
1304080b47deSLogan Gunthorpe 	dev_info(&stdev->dev, "Management device registered.\n");
1305080b47deSLogan Gunthorpe 
1306080b47deSLogan Gunthorpe 	return 0;
1307080b47deSLogan Gunthorpe 
1308080b47deSLogan Gunthorpe err_devadd:
1309080b47deSLogan Gunthorpe 	stdev_kill(stdev);
1310080b47deSLogan Gunthorpe err_put:
1311080b47deSLogan Gunthorpe 	ida_simple_remove(&switchtec_minor_ida, MINOR(stdev->dev.devt));
1312080b47deSLogan Gunthorpe 	put_device(&stdev->dev);
1313080b47deSLogan Gunthorpe 	return rc;
1314080b47deSLogan Gunthorpe }
1315080b47deSLogan Gunthorpe 
1316080b47deSLogan Gunthorpe static void switchtec_pci_remove(struct pci_dev *pdev)
1317080b47deSLogan Gunthorpe {
1318080b47deSLogan Gunthorpe 	struct switchtec_dev *stdev = pci_get_drvdata(pdev);
1319080b47deSLogan Gunthorpe 
1320080b47deSLogan Gunthorpe 	pci_set_drvdata(pdev, NULL);
1321080b47deSLogan Gunthorpe 
1322e40cf640SLogan Gunthorpe 	cdev_device_del(&stdev->cdev, &stdev->dev);
1323080b47deSLogan Gunthorpe 	ida_simple_remove(&switchtec_minor_ida, MINOR(stdev->dev.devt));
1324080b47deSLogan Gunthorpe 	dev_info(&stdev->dev, "unregistered.\n");
1325080b47deSLogan Gunthorpe 
1326080b47deSLogan Gunthorpe 	stdev_kill(stdev);
1327080b47deSLogan Gunthorpe 	put_device(&stdev->dev);
1328080b47deSLogan Gunthorpe }
1329080b47deSLogan Gunthorpe 
1330080b47deSLogan Gunthorpe #define SWITCHTEC_PCI_DEVICE(device_id) \
1331080b47deSLogan Gunthorpe 	{ \
1332080b47deSLogan Gunthorpe 		.vendor     = MICROSEMI_VENDOR_ID, \
1333080b47deSLogan Gunthorpe 		.device     = device_id, \
1334080b47deSLogan Gunthorpe 		.subvendor  = PCI_ANY_ID, \
1335080b47deSLogan Gunthorpe 		.subdevice  = PCI_ANY_ID, \
1336080b47deSLogan Gunthorpe 		.class      = MICROSEMI_MGMT_CLASSCODE, \
1337080b47deSLogan Gunthorpe 		.class_mask = 0xFFFFFFFF, \
1338080b47deSLogan Gunthorpe 	}, \
1339080b47deSLogan Gunthorpe 	{ \
1340080b47deSLogan Gunthorpe 		.vendor     = MICROSEMI_VENDOR_ID, \
1341080b47deSLogan Gunthorpe 		.device     = device_id, \
1342080b47deSLogan Gunthorpe 		.subvendor  = PCI_ANY_ID, \
1343080b47deSLogan Gunthorpe 		.subdevice  = PCI_ANY_ID, \
1344080b47deSLogan Gunthorpe 		.class      = MICROSEMI_NTB_CLASSCODE, \
1345080b47deSLogan Gunthorpe 		.class_mask = 0xFFFFFFFF, \
1346080b47deSLogan Gunthorpe 	}
1347080b47deSLogan Gunthorpe 
1348080b47deSLogan Gunthorpe static const struct pci_device_id switchtec_pci_tbl[] = {
1349080b47deSLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8531),  //PFX 24xG3
1350080b47deSLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8532),  //PFX 32xG3
1351080b47deSLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8533),  //PFX 48xG3
1352080b47deSLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8534),  //PFX 64xG3
1353080b47deSLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8535),  //PFX 80xG3
1354080b47deSLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8536),  //PFX 96xG3
1355080b47deSLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8543),  //PSX 48xG3
1356080b47deSLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8544),  //PSX 64xG3
1357080b47deSLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8545),  //PSX 80xG3
1358080b47deSLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8546),  //PSX 96xG3
1359393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8551),  //PAX 24XG3
1360393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8552),  //PAX 32XG3
1361393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8553),  //PAX 48XG3
1362393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8554),  //PAX 64XG3
1363393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8555),  //PAX 80XG3
1364393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8556),  //PAX 96XG3
1365393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8561),  //PFXL 24XG3
1366393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8562),  //PFXL 32XG3
1367393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8563),  //PFXL 48XG3
1368393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8564),  //PFXL 64XG3
1369393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8565),  //PFXL 80XG3
1370393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8566),  //PFXL 96XG3
1371393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8571),  //PFXI 24XG3
1372393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8572),  //PFXI 32XG3
1373393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8573),  //PFXI 48XG3
1374393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8574),  //PFXI 64XG3
1375393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8575),  //PFXI 80XG3
1376393958d0SLogan Gunthorpe 	SWITCHTEC_PCI_DEVICE(0x8576),  //PFXI 96XG3
1377080b47deSLogan Gunthorpe 	{0}
1378080b47deSLogan Gunthorpe };
1379080b47deSLogan Gunthorpe MODULE_DEVICE_TABLE(pci, switchtec_pci_tbl);
1380080b47deSLogan Gunthorpe 
1381080b47deSLogan Gunthorpe static struct pci_driver switchtec_pci_driver = {
1382080b47deSLogan Gunthorpe 	.name		= KBUILD_MODNAME,
1383080b47deSLogan Gunthorpe 	.id_table	= switchtec_pci_tbl,
1384080b47deSLogan Gunthorpe 	.probe		= switchtec_pci_probe,
1385080b47deSLogan Gunthorpe 	.remove		= switchtec_pci_remove,
1386080b47deSLogan Gunthorpe };
1387080b47deSLogan Gunthorpe 
1388080b47deSLogan Gunthorpe static int __init switchtec_init(void)
1389080b47deSLogan Gunthorpe {
1390080b47deSLogan Gunthorpe 	int rc;
1391080b47deSLogan Gunthorpe 
1392080b47deSLogan Gunthorpe 	rc = alloc_chrdev_region(&switchtec_devt, 0, max_devices,
1393080b47deSLogan Gunthorpe 				 "switchtec");
1394080b47deSLogan Gunthorpe 	if (rc)
1395080b47deSLogan Gunthorpe 		return rc;
1396080b47deSLogan Gunthorpe 
1397080b47deSLogan Gunthorpe 	switchtec_class = class_create(THIS_MODULE, "switchtec");
1398080b47deSLogan Gunthorpe 	if (IS_ERR(switchtec_class)) {
1399080b47deSLogan Gunthorpe 		rc = PTR_ERR(switchtec_class);
1400080b47deSLogan Gunthorpe 		goto err_create_class;
1401080b47deSLogan Gunthorpe 	}
1402080b47deSLogan Gunthorpe 
1403080b47deSLogan Gunthorpe 	rc = pci_register_driver(&switchtec_pci_driver);
1404080b47deSLogan Gunthorpe 	if (rc)
1405080b47deSLogan Gunthorpe 		goto err_pci_register;
1406080b47deSLogan Gunthorpe 
1407080b47deSLogan Gunthorpe 	pr_info(KBUILD_MODNAME ": loaded.\n");
1408080b47deSLogan Gunthorpe 
1409080b47deSLogan Gunthorpe 	return 0;
1410080b47deSLogan Gunthorpe 
1411080b47deSLogan Gunthorpe err_pci_register:
1412080b47deSLogan Gunthorpe 	class_destroy(switchtec_class);
1413080b47deSLogan Gunthorpe 
1414080b47deSLogan Gunthorpe err_create_class:
1415080b47deSLogan Gunthorpe 	unregister_chrdev_region(switchtec_devt, max_devices);
1416080b47deSLogan Gunthorpe 
1417080b47deSLogan Gunthorpe 	return rc;
1418080b47deSLogan Gunthorpe }
1419080b47deSLogan Gunthorpe module_init(switchtec_init);
1420080b47deSLogan Gunthorpe 
1421080b47deSLogan Gunthorpe static void __exit switchtec_exit(void)
1422080b47deSLogan Gunthorpe {
1423080b47deSLogan Gunthorpe 	pci_unregister_driver(&switchtec_pci_driver);
1424080b47deSLogan Gunthorpe 	class_destroy(switchtec_class);
1425080b47deSLogan Gunthorpe 	unregister_chrdev_region(switchtec_devt, max_devices);
1426080b47deSLogan Gunthorpe 	ida_destroy(&switchtec_minor_ida);
1427080b47deSLogan Gunthorpe 
1428080b47deSLogan Gunthorpe 	pr_info(KBUILD_MODNAME ": unloaded.\n");
1429080b47deSLogan Gunthorpe }
1430080b47deSLogan Gunthorpe module_exit(switchtec_exit);
1431