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>
16f7eb7b8aSWesley Sheng #include <linux/io-64-nonatomic-lo-hi.h>
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
28b8af8549SKrzysztof Wilczynski static bool use_dma_mrpc = true;
29f7eb7b8aSWesley Sheng module_param(use_dma_mrpc, bool, 0644);
30f7eb7b8aSWesley Sheng MODULE_PARM_DESC(use_dma_mrpc,
31f7eb7b8aSWesley Sheng "Enable the use of the DMA MRPC feature");
32f7eb7b8aSWesley Sheng
33fcdf8e95SLogan Gunthorpe static int nirqs = 32;
34fcdf8e95SLogan Gunthorpe module_param(nirqs, int, 0644);
35fcdf8e95SLogan Gunthorpe MODULE_PARM_DESC(nirqs, "number of interrupts to allocate (more may be useful for NTB applications)");
36fcdf8e95SLogan Gunthorpe
37080b47deSLogan Gunthorpe static dev_t switchtec_devt;
38080b47deSLogan Gunthorpe static DEFINE_IDA(switchtec_minor_ida);
39080b47deSLogan Gunthorpe
40*8a74e4eaSGreg Kroah-Hartman const struct class switchtec_class = {
41*8a74e4eaSGreg Kroah-Hartman .name = "switchtec",
42*8a74e4eaSGreg Kroah-Hartman };
43302e994dSLogan Gunthorpe EXPORT_SYMBOL_GPL(switchtec_class);
44080b47deSLogan Gunthorpe
45080b47deSLogan Gunthorpe enum mrpc_state {
46080b47deSLogan Gunthorpe MRPC_IDLE = 0,
47080b47deSLogan Gunthorpe MRPC_QUEUED,
48080b47deSLogan Gunthorpe MRPC_RUNNING,
49080b47deSLogan Gunthorpe MRPC_DONE,
501a323bd0SKelvin Cao MRPC_IO_ERROR,
51080b47deSLogan Gunthorpe };
52080b47deSLogan Gunthorpe
53080b47deSLogan Gunthorpe struct switchtec_user {
54080b47deSLogan Gunthorpe struct switchtec_dev *stdev;
55080b47deSLogan Gunthorpe
56080b47deSLogan Gunthorpe enum mrpc_state state;
57080b47deSLogan Gunthorpe
58deaa0a8aSSebastian Andrzej Siewior wait_queue_head_t cmd_comp;
59080b47deSLogan Gunthorpe struct kref kref;
60080b47deSLogan Gunthorpe struct list_head list;
61080b47deSLogan Gunthorpe
62deaa0a8aSSebastian Andrzej Siewior bool cmd_done;
63080b47deSLogan Gunthorpe u32 cmd;
64080b47deSLogan Gunthorpe u32 status;
65080b47deSLogan Gunthorpe u32 return_code;
66080b47deSLogan Gunthorpe size_t data_len;
67080b47deSLogan Gunthorpe size_t read_len;
68080b47deSLogan Gunthorpe unsigned char data[SWITCHTEC_MRPC_PAYLOAD_SIZE];
69080b47deSLogan Gunthorpe int event_cnt;
70080b47deSLogan Gunthorpe };
71080b47deSLogan Gunthorpe
721a323bd0SKelvin Cao /*
731a323bd0SKelvin Cao * The MMIO reads to the device_id register should always return the device ID
741a323bd0SKelvin Cao * of the device, otherwise the firmware is probably stuck or unreachable
751a323bd0SKelvin Cao * due to a firmware reset which clears PCI state including the BARs and Memory
761a323bd0SKelvin Cao * Space Enable bits.
771a323bd0SKelvin Cao */
is_firmware_running(struct switchtec_dev * stdev)781a323bd0SKelvin Cao static int is_firmware_running(struct switchtec_dev *stdev)
791a323bd0SKelvin Cao {
801a323bd0SKelvin Cao u32 device = ioread32(&stdev->mmio_sys_info->device_id);
811a323bd0SKelvin Cao
821a323bd0SKelvin Cao return stdev->pdev->device == device;
831a323bd0SKelvin Cao }
841a323bd0SKelvin Cao
stuser_create(struct switchtec_dev * stdev)85080b47deSLogan Gunthorpe static struct switchtec_user *stuser_create(struct switchtec_dev *stdev)
86080b47deSLogan Gunthorpe {
87080b47deSLogan Gunthorpe struct switchtec_user *stuser;
88080b47deSLogan Gunthorpe
89080b47deSLogan Gunthorpe stuser = kzalloc(sizeof(*stuser), GFP_KERNEL);
90080b47deSLogan Gunthorpe if (!stuser)
91080b47deSLogan Gunthorpe return ERR_PTR(-ENOMEM);
92080b47deSLogan Gunthorpe
93080b47deSLogan Gunthorpe get_device(&stdev->dev);
94080b47deSLogan Gunthorpe stuser->stdev = stdev;
95080b47deSLogan Gunthorpe kref_init(&stuser->kref);
96080b47deSLogan Gunthorpe INIT_LIST_HEAD(&stuser->list);
97deaa0a8aSSebastian Andrzej Siewior init_waitqueue_head(&stuser->cmd_comp);
98080b47deSLogan Gunthorpe stuser->event_cnt = atomic_read(&stdev->event_cnt);
99080b47deSLogan Gunthorpe
100080b47deSLogan Gunthorpe dev_dbg(&stdev->dev, "%s: %p\n", __func__, stuser);
101080b47deSLogan Gunthorpe
102080b47deSLogan Gunthorpe return stuser;
103080b47deSLogan Gunthorpe }
104080b47deSLogan Gunthorpe
stuser_free(struct kref * kref)105080b47deSLogan Gunthorpe static void stuser_free(struct kref *kref)
106080b47deSLogan Gunthorpe {
107080b47deSLogan Gunthorpe struct switchtec_user *stuser;
108080b47deSLogan Gunthorpe
109080b47deSLogan Gunthorpe stuser = container_of(kref, struct switchtec_user, kref);
110080b47deSLogan Gunthorpe
111080b47deSLogan Gunthorpe dev_dbg(&stuser->stdev->dev, "%s: %p\n", __func__, stuser);
112080b47deSLogan Gunthorpe
113080b47deSLogan Gunthorpe put_device(&stuser->stdev->dev);
114080b47deSLogan Gunthorpe kfree(stuser);
115080b47deSLogan Gunthorpe }
116080b47deSLogan Gunthorpe
stuser_put(struct switchtec_user * stuser)117080b47deSLogan Gunthorpe static void stuser_put(struct switchtec_user *stuser)
118080b47deSLogan Gunthorpe {
119080b47deSLogan Gunthorpe kref_put(&stuser->kref, stuser_free);
120080b47deSLogan Gunthorpe }
121080b47deSLogan Gunthorpe
stuser_set_state(struct switchtec_user * stuser,enum mrpc_state state)122080b47deSLogan Gunthorpe static void stuser_set_state(struct switchtec_user *stuser,
123080b47deSLogan Gunthorpe enum mrpc_state state)
124080b47deSLogan Gunthorpe {
125080b47deSLogan Gunthorpe /* requires the mrpc_mutex to already be held when called */
126080b47deSLogan Gunthorpe
127b76521f6SKelvin Cao static const char * const state_names[] = {
128080b47deSLogan Gunthorpe [MRPC_IDLE] = "IDLE",
129080b47deSLogan Gunthorpe [MRPC_QUEUED] = "QUEUED",
130080b47deSLogan Gunthorpe [MRPC_RUNNING] = "RUNNING",
131080b47deSLogan Gunthorpe [MRPC_DONE] = "DONE",
1321a323bd0SKelvin Cao [MRPC_IO_ERROR] = "IO_ERROR",
133080b47deSLogan Gunthorpe };
134080b47deSLogan Gunthorpe
135080b47deSLogan Gunthorpe stuser->state = state;
136080b47deSLogan Gunthorpe
137080b47deSLogan Gunthorpe dev_dbg(&stuser->stdev->dev, "stuser state %p -> %s",
138080b47deSLogan Gunthorpe stuser, state_names[state]);
139080b47deSLogan Gunthorpe }
140080b47deSLogan Gunthorpe
141080b47deSLogan Gunthorpe static void mrpc_complete_cmd(struct switchtec_dev *stdev);
142080b47deSLogan Gunthorpe
flush_wc_buf(struct switchtec_dev * stdev)14352d8db8eSKelvin Cao static void flush_wc_buf(struct switchtec_dev *stdev)
14452d8db8eSKelvin Cao {
14552d8db8eSKelvin Cao struct ntb_dbmsg_regs __iomem *mmio_dbmsg;
14652d8db8eSKelvin Cao
14752d8db8eSKelvin Cao /*
14852d8db8eSKelvin Cao * odb (outbound doorbell) register is processed by low latency
14952d8db8eSKelvin Cao * hardware and w/o side effect
15052d8db8eSKelvin Cao */
15152d8db8eSKelvin Cao mmio_dbmsg = (void __iomem *)stdev->mmio_ntb +
15252d8db8eSKelvin Cao SWITCHTEC_NTB_REG_DBMSG_OFFSET;
15352d8db8eSKelvin Cao ioread32(&mmio_dbmsg->odb);
15452d8db8eSKelvin Cao }
15552d8db8eSKelvin Cao
mrpc_cmd_submit(struct switchtec_dev * stdev)156080b47deSLogan Gunthorpe static void mrpc_cmd_submit(struct switchtec_dev *stdev)
157080b47deSLogan Gunthorpe {
158080b47deSLogan Gunthorpe /* requires the mrpc_mutex to already be held when called */
159080b47deSLogan Gunthorpe
160080b47deSLogan Gunthorpe struct switchtec_user *stuser;
161080b47deSLogan Gunthorpe
162080b47deSLogan Gunthorpe if (stdev->mrpc_busy)
163080b47deSLogan Gunthorpe return;
164080b47deSLogan Gunthorpe
165080b47deSLogan Gunthorpe if (list_empty(&stdev->mrpc_queue))
166080b47deSLogan Gunthorpe return;
167080b47deSLogan Gunthorpe
168080b47deSLogan Gunthorpe stuser = list_entry(stdev->mrpc_queue.next, struct switchtec_user,
169080b47deSLogan Gunthorpe list);
170080b47deSLogan Gunthorpe
171f7eb7b8aSWesley Sheng if (stdev->dma_mrpc) {
172f7eb7b8aSWesley Sheng stdev->dma_mrpc->status = SWITCHTEC_MRPC_STATUS_INPROGRESS;
173f7eb7b8aSWesley Sheng memset(stdev->dma_mrpc->data, 0xFF, SWITCHTEC_MRPC_PAYLOAD_SIZE);
174f7eb7b8aSWesley Sheng }
175f7eb7b8aSWesley Sheng
176080b47deSLogan Gunthorpe stuser_set_state(stuser, MRPC_RUNNING);
177080b47deSLogan Gunthorpe stdev->mrpc_busy = 1;
178080b47deSLogan Gunthorpe memcpy_toio(&stdev->mmio_mrpc->input_data,
179080b47deSLogan Gunthorpe stuser->data, stuser->data_len);
18052d8db8eSKelvin Cao flush_wc_buf(stdev);
181080b47deSLogan Gunthorpe iowrite32(stuser->cmd, &stdev->mmio_mrpc->cmd);
182080b47deSLogan Gunthorpe
183080b47deSLogan Gunthorpe schedule_delayed_work(&stdev->mrpc_timeout,
184080b47deSLogan Gunthorpe msecs_to_jiffies(500));
185080b47deSLogan Gunthorpe }
186080b47deSLogan Gunthorpe
mrpc_queue_cmd(struct switchtec_user * stuser)187080b47deSLogan Gunthorpe static int mrpc_queue_cmd(struct switchtec_user *stuser)
188080b47deSLogan Gunthorpe {
189080b47deSLogan Gunthorpe /* requires the mrpc_mutex to already be held when called */
190080b47deSLogan Gunthorpe
191080b47deSLogan Gunthorpe struct switchtec_dev *stdev = stuser->stdev;
192080b47deSLogan Gunthorpe
193080b47deSLogan Gunthorpe kref_get(&stuser->kref);
194080b47deSLogan Gunthorpe stuser->read_len = sizeof(stuser->data);
195080b47deSLogan Gunthorpe stuser_set_state(stuser, MRPC_QUEUED);
196deaa0a8aSSebastian Andrzej Siewior stuser->cmd_done = false;
197080b47deSLogan Gunthorpe list_add_tail(&stuser->list, &stdev->mrpc_queue);
198080b47deSLogan Gunthorpe
199080b47deSLogan Gunthorpe mrpc_cmd_submit(stdev);
200080b47deSLogan Gunthorpe
201080b47deSLogan Gunthorpe return 0;
202080b47deSLogan Gunthorpe }
203080b47deSLogan Gunthorpe
mrpc_cleanup_cmd(struct switchtec_dev * stdev)2041a323bd0SKelvin Cao static void mrpc_cleanup_cmd(struct switchtec_dev *stdev)
2051a323bd0SKelvin Cao {
2061a323bd0SKelvin Cao /* requires the mrpc_mutex to already be held when called */
2071a323bd0SKelvin Cao
2081a323bd0SKelvin Cao struct switchtec_user *stuser = list_entry(stdev->mrpc_queue.next,
2091a323bd0SKelvin Cao struct switchtec_user, list);
2101a323bd0SKelvin Cao
2111a323bd0SKelvin Cao stuser->cmd_done = true;
2121a323bd0SKelvin Cao wake_up_interruptible(&stuser->cmd_comp);
2131a323bd0SKelvin Cao list_del_init(&stuser->list);
2141a323bd0SKelvin Cao stuser_put(stuser);
2151a323bd0SKelvin Cao stdev->mrpc_busy = 0;
2161a323bd0SKelvin Cao
2171a323bd0SKelvin Cao mrpc_cmd_submit(stdev);
2181a323bd0SKelvin Cao }
2191a323bd0SKelvin Cao
mrpc_complete_cmd(struct switchtec_dev * stdev)220080b47deSLogan Gunthorpe static void mrpc_complete_cmd(struct switchtec_dev *stdev)
221080b47deSLogan Gunthorpe {
222080b47deSLogan Gunthorpe /* requires the mrpc_mutex to already be held when called */
2231a323bd0SKelvin Cao
224080b47deSLogan Gunthorpe struct switchtec_user *stuser;
225080b47deSLogan Gunthorpe
226080b47deSLogan Gunthorpe if (list_empty(&stdev->mrpc_queue))
227080b47deSLogan Gunthorpe return;
228080b47deSLogan Gunthorpe
229080b47deSLogan Gunthorpe stuser = list_entry(stdev->mrpc_queue.next, struct switchtec_user,
230080b47deSLogan Gunthorpe list);
231080b47deSLogan Gunthorpe
232f7eb7b8aSWesley Sheng if (stdev->dma_mrpc)
233f7eb7b8aSWesley Sheng stuser->status = stdev->dma_mrpc->status;
234f7eb7b8aSWesley Sheng else
235080b47deSLogan Gunthorpe stuser->status = ioread32(&stdev->mmio_mrpc->status);
236f7eb7b8aSWesley Sheng
237080b47deSLogan Gunthorpe if (stuser->status == SWITCHTEC_MRPC_STATUS_INPROGRESS)
238080b47deSLogan Gunthorpe return;
239080b47deSLogan Gunthorpe
240080b47deSLogan Gunthorpe stuser_set_state(stuser, MRPC_DONE);
241080b47deSLogan Gunthorpe stuser->return_code = 0;
242080b47deSLogan Gunthorpe
243551ec658SKelvin Cao if (stuser->status != SWITCHTEC_MRPC_STATUS_DONE &&
244551ec658SKelvin Cao stuser->status != SWITCHTEC_MRPC_STATUS_ERROR)
245080b47deSLogan Gunthorpe goto out;
246080b47deSLogan Gunthorpe
247f7eb7b8aSWesley Sheng if (stdev->dma_mrpc)
248f7eb7b8aSWesley Sheng stuser->return_code = stdev->dma_mrpc->rtn_code;
249f7eb7b8aSWesley Sheng else
250080b47deSLogan Gunthorpe stuser->return_code = ioread32(&stdev->mmio_mrpc->ret_value);
251080b47deSLogan Gunthorpe if (stuser->return_code != 0)
252080b47deSLogan Gunthorpe goto out;
253080b47deSLogan Gunthorpe
254f7eb7b8aSWesley Sheng if (stdev->dma_mrpc)
255f7eb7b8aSWesley Sheng memcpy(stuser->data, &stdev->dma_mrpc->data,
256f7eb7b8aSWesley Sheng stuser->read_len);
257f7eb7b8aSWesley Sheng else
258080b47deSLogan Gunthorpe memcpy_fromio(stuser->data, &stdev->mmio_mrpc->output_data,
259080b47deSLogan Gunthorpe stuser->read_len);
260080b47deSLogan Gunthorpe out:
2611a323bd0SKelvin Cao mrpc_cleanup_cmd(stdev);
262080b47deSLogan Gunthorpe }
263080b47deSLogan Gunthorpe
mrpc_event_work(struct work_struct * work)264080b47deSLogan Gunthorpe static void mrpc_event_work(struct work_struct *work)
265080b47deSLogan Gunthorpe {
266080b47deSLogan Gunthorpe struct switchtec_dev *stdev;
267080b47deSLogan Gunthorpe
268080b47deSLogan Gunthorpe stdev = container_of(work, struct switchtec_dev, mrpc_work);
269080b47deSLogan Gunthorpe
270080b47deSLogan Gunthorpe dev_dbg(&stdev->dev, "%s\n", __func__);
271080b47deSLogan Gunthorpe
272080b47deSLogan Gunthorpe mutex_lock(&stdev->mrpc_mutex);
273080b47deSLogan Gunthorpe cancel_delayed_work(&stdev->mrpc_timeout);
274080b47deSLogan Gunthorpe mrpc_complete_cmd(stdev);
275080b47deSLogan Gunthorpe mutex_unlock(&stdev->mrpc_mutex);
276080b47deSLogan Gunthorpe }
277080b47deSLogan Gunthorpe
mrpc_error_complete_cmd(struct switchtec_dev * stdev)2781a323bd0SKelvin Cao static void mrpc_error_complete_cmd(struct switchtec_dev *stdev)
2791a323bd0SKelvin Cao {
2801a323bd0SKelvin Cao /* requires the mrpc_mutex to already be held when called */
2811a323bd0SKelvin Cao
2821a323bd0SKelvin Cao struct switchtec_user *stuser;
2831a323bd0SKelvin Cao
2841a323bd0SKelvin Cao if (list_empty(&stdev->mrpc_queue))
2851a323bd0SKelvin Cao return;
2861a323bd0SKelvin Cao
2871a323bd0SKelvin Cao stuser = list_entry(stdev->mrpc_queue.next,
2881a323bd0SKelvin Cao struct switchtec_user, list);
2891a323bd0SKelvin Cao
2901a323bd0SKelvin Cao stuser_set_state(stuser, MRPC_IO_ERROR);
2911a323bd0SKelvin Cao
2921a323bd0SKelvin Cao mrpc_cleanup_cmd(stdev);
2931a323bd0SKelvin Cao }
2941a323bd0SKelvin Cao
mrpc_timeout_work(struct work_struct * work)295080b47deSLogan Gunthorpe static void mrpc_timeout_work(struct work_struct *work)
296080b47deSLogan Gunthorpe {
297080b47deSLogan Gunthorpe struct switchtec_dev *stdev;
298080b47deSLogan Gunthorpe u32 status;
299080b47deSLogan Gunthorpe
300080b47deSLogan Gunthorpe stdev = container_of(work, struct switchtec_dev, mrpc_timeout.work);
301080b47deSLogan Gunthorpe
302080b47deSLogan Gunthorpe dev_dbg(&stdev->dev, "%s\n", __func__);
303080b47deSLogan Gunthorpe
304080b47deSLogan Gunthorpe mutex_lock(&stdev->mrpc_mutex);
305080b47deSLogan Gunthorpe
3061a323bd0SKelvin Cao if (!is_firmware_running(stdev)) {
3071a323bd0SKelvin Cao mrpc_error_complete_cmd(stdev);
3081a323bd0SKelvin Cao goto out;
3091a323bd0SKelvin Cao }
3101a323bd0SKelvin Cao
311f7eb7b8aSWesley Sheng if (stdev->dma_mrpc)
312f7eb7b8aSWesley Sheng status = stdev->dma_mrpc->status;
313f7eb7b8aSWesley Sheng else
314080b47deSLogan Gunthorpe status = ioread32(&stdev->mmio_mrpc->status);
315080b47deSLogan Gunthorpe if (status == SWITCHTEC_MRPC_STATUS_INPROGRESS) {
316080b47deSLogan Gunthorpe schedule_delayed_work(&stdev->mrpc_timeout,
317080b47deSLogan Gunthorpe msecs_to_jiffies(500));
318080b47deSLogan Gunthorpe goto out;
319080b47deSLogan Gunthorpe }
320080b47deSLogan Gunthorpe
321080b47deSLogan Gunthorpe mrpc_complete_cmd(stdev);
322080b47deSLogan Gunthorpe out:
323080b47deSLogan Gunthorpe mutex_unlock(&stdev->mrpc_mutex);
324080b47deSLogan Gunthorpe }
325080b47deSLogan Gunthorpe
device_version_show(struct device * dev,struct device_attribute * attr,char * buf)3265d8e1881SLogan Gunthorpe static ssize_t device_version_show(struct device *dev,
3275d8e1881SLogan Gunthorpe struct device_attribute *attr, char *buf)
3285d8e1881SLogan Gunthorpe {
3295d8e1881SLogan Gunthorpe struct switchtec_dev *stdev = to_stdev(dev);
3305d8e1881SLogan Gunthorpe u32 ver;
3315d8e1881SLogan Gunthorpe
3325d8e1881SLogan Gunthorpe ver = ioread32(&stdev->mmio_sys_info->device_version);
3335d8e1881SLogan Gunthorpe
334f8cf6e51SKrzysztof Wilczyński return sysfs_emit(buf, "%x\n", ver);
3355d8e1881SLogan Gunthorpe }
3365d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(device_version);
3375d8e1881SLogan Gunthorpe
fw_version_show(struct device * dev,struct device_attribute * attr,char * buf)3385d8e1881SLogan Gunthorpe static ssize_t fw_version_show(struct device *dev,
3395d8e1881SLogan Gunthorpe struct device_attribute *attr, char *buf)
3405d8e1881SLogan Gunthorpe {
3415d8e1881SLogan Gunthorpe struct switchtec_dev *stdev = to_stdev(dev);
3425d8e1881SLogan Gunthorpe u32 ver;
3435d8e1881SLogan Gunthorpe
3445d8e1881SLogan Gunthorpe ver = ioread32(&stdev->mmio_sys_info->firmware_version);
3455d8e1881SLogan Gunthorpe
346f8cf6e51SKrzysztof Wilczyński return sysfs_emit(buf, "%08x\n", ver);
3475d8e1881SLogan Gunthorpe }
3485d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(fw_version);
3495d8e1881SLogan Gunthorpe
io_string_show(char * buf,void __iomem * attr,size_t len)3505d8e1881SLogan Gunthorpe static ssize_t io_string_show(char *buf, void __iomem *attr, size_t len)
3515d8e1881SLogan Gunthorpe {
3525d8e1881SLogan Gunthorpe int i;
3535d8e1881SLogan Gunthorpe
3545d8e1881SLogan Gunthorpe memcpy_fromio(buf, attr, len);
3555d8e1881SLogan Gunthorpe buf[len] = '\n';
3565d8e1881SLogan Gunthorpe buf[len + 1] = 0;
3575d8e1881SLogan Gunthorpe
3585d8e1881SLogan Gunthorpe for (i = len - 1; i > 0; i--) {
3595d8e1881SLogan Gunthorpe if (buf[i] != ' ')
3605d8e1881SLogan Gunthorpe break;
3615d8e1881SLogan Gunthorpe buf[i] = '\n';
3625d8e1881SLogan Gunthorpe buf[i + 1] = 0;
3635d8e1881SLogan Gunthorpe }
3645d8e1881SLogan Gunthorpe
3655d8e1881SLogan Gunthorpe return strlen(buf);
3665d8e1881SLogan Gunthorpe }
3675d8e1881SLogan Gunthorpe
3685d8e1881SLogan Gunthorpe #define DEVICE_ATTR_SYS_INFO_STR(field) \
3695d8e1881SLogan Gunthorpe static ssize_t field ## _show(struct device *dev, \
3705d8e1881SLogan Gunthorpe struct device_attribute *attr, char *buf) \
3715d8e1881SLogan Gunthorpe { \
3725d8e1881SLogan Gunthorpe struct switchtec_dev *stdev = to_stdev(dev); \
373993d208dSLogan Gunthorpe struct sys_info_regs __iomem *si = stdev->mmio_sys_info; \
374993d208dSLogan Gunthorpe if (stdev->gen == SWITCHTEC_GEN3) \
375993d208dSLogan Gunthorpe return io_string_show(buf, &si->gen3.field, \
376993d208dSLogan Gunthorpe sizeof(si->gen3.field)); \
3770fb53e64SKelvin Cao else if (stdev->gen >= SWITCHTEC_GEN4) \
378a3321ca3SLogan Gunthorpe return io_string_show(buf, &si->gen4.field, \
379a3321ca3SLogan Gunthorpe sizeof(si->gen4.field)); \
380993d208dSLogan Gunthorpe else \
38167116444SKelvin Cao return -EOPNOTSUPP; \
3825d8e1881SLogan Gunthorpe } \
3835d8e1881SLogan Gunthorpe \
3845d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(field)
3855d8e1881SLogan Gunthorpe
3865d8e1881SLogan Gunthorpe DEVICE_ATTR_SYS_INFO_STR(vendor_id);
3875d8e1881SLogan Gunthorpe DEVICE_ATTR_SYS_INFO_STR(product_id);
3885d8e1881SLogan Gunthorpe DEVICE_ATTR_SYS_INFO_STR(product_revision);
389b13313a0SLogan Gunthorpe
component_vendor_show(struct device * dev,struct device_attribute * attr,char * buf)390b13313a0SLogan Gunthorpe static ssize_t component_vendor_show(struct device *dev,
391b13313a0SLogan Gunthorpe struct device_attribute *attr, char *buf)
392b13313a0SLogan Gunthorpe {
393b13313a0SLogan Gunthorpe struct switchtec_dev *stdev = to_stdev(dev);
394b13313a0SLogan Gunthorpe struct sys_info_regs __iomem *si = stdev->mmio_sys_info;
395b13313a0SLogan Gunthorpe
396b13313a0SLogan Gunthorpe /* component_vendor field not supported after gen3 */
397b13313a0SLogan Gunthorpe if (stdev->gen != SWITCHTEC_GEN3)
398f8cf6e51SKrzysztof Wilczyński return sysfs_emit(buf, "none\n");
399b13313a0SLogan Gunthorpe
400993d208dSLogan Gunthorpe return io_string_show(buf, &si->gen3.component_vendor,
401993d208dSLogan Gunthorpe sizeof(si->gen3.component_vendor));
402b13313a0SLogan Gunthorpe }
403b13313a0SLogan Gunthorpe static DEVICE_ATTR_RO(component_vendor);
4045d8e1881SLogan Gunthorpe
component_id_show(struct device * dev,struct device_attribute * attr,char * buf)4055d8e1881SLogan Gunthorpe static ssize_t component_id_show(struct device *dev,
4065d8e1881SLogan Gunthorpe struct device_attribute *attr, char *buf)
4075d8e1881SLogan Gunthorpe {
4085d8e1881SLogan Gunthorpe struct switchtec_dev *stdev = to_stdev(dev);
409993d208dSLogan Gunthorpe int id = ioread16(&stdev->mmio_sys_info->gen3.component_id);
4105d8e1881SLogan Gunthorpe
411b13313a0SLogan Gunthorpe /* component_id field not supported after gen3 */
412b13313a0SLogan Gunthorpe if (stdev->gen != SWITCHTEC_GEN3)
413f8cf6e51SKrzysztof Wilczyński return sysfs_emit(buf, "none\n");
414b13313a0SLogan Gunthorpe
415f8cf6e51SKrzysztof Wilczyński return sysfs_emit(buf, "PM%04X\n", id);
4165d8e1881SLogan Gunthorpe }
4175d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(component_id);
4185d8e1881SLogan Gunthorpe
component_revision_show(struct device * dev,struct device_attribute * attr,char * buf)4195d8e1881SLogan Gunthorpe static ssize_t component_revision_show(struct device *dev,
4205d8e1881SLogan Gunthorpe struct device_attribute *attr, char *buf)
4215d8e1881SLogan Gunthorpe {
4225d8e1881SLogan Gunthorpe struct switchtec_dev *stdev = to_stdev(dev);
423993d208dSLogan Gunthorpe int rev = ioread8(&stdev->mmio_sys_info->gen3.component_revision);
4245d8e1881SLogan Gunthorpe
425b13313a0SLogan Gunthorpe /* component_revision field not supported after gen3 */
426b13313a0SLogan Gunthorpe if (stdev->gen != SWITCHTEC_GEN3)
427f8cf6e51SKrzysztof Wilczyński return sysfs_emit(buf, "255\n");
428b13313a0SLogan Gunthorpe
429f8cf6e51SKrzysztof Wilczyński return sysfs_emit(buf, "%d\n", rev);
4305d8e1881SLogan Gunthorpe }
4315d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(component_revision);
4325d8e1881SLogan Gunthorpe
partition_show(struct device * dev,struct device_attribute * attr,char * buf)4335d8e1881SLogan Gunthorpe static ssize_t partition_show(struct device *dev,
4345d8e1881SLogan Gunthorpe struct device_attribute *attr, char *buf)
4355d8e1881SLogan Gunthorpe {
4365d8e1881SLogan Gunthorpe struct switchtec_dev *stdev = to_stdev(dev);
4375d8e1881SLogan Gunthorpe
438f8cf6e51SKrzysztof Wilczyński return sysfs_emit(buf, "%d\n", stdev->partition);
4395d8e1881SLogan Gunthorpe }
4405d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(partition);
4415d8e1881SLogan Gunthorpe
partition_count_show(struct device * dev,struct device_attribute * attr,char * buf)4425d8e1881SLogan Gunthorpe static ssize_t partition_count_show(struct device *dev,
4435d8e1881SLogan Gunthorpe struct device_attribute *attr, char *buf)
4445d8e1881SLogan Gunthorpe {
4455d8e1881SLogan Gunthorpe struct switchtec_dev *stdev = to_stdev(dev);
4465d8e1881SLogan Gunthorpe
447f8cf6e51SKrzysztof Wilczyński return sysfs_emit(buf, "%d\n", stdev->partition_count);
4485d8e1881SLogan Gunthorpe }
4495d8e1881SLogan Gunthorpe static DEVICE_ATTR_RO(partition_count);
4505d8e1881SLogan Gunthorpe
4515d8e1881SLogan Gunthorpe static struct attribute *switchtec_device_attrs[] = {
4525d8e1881SLogan Gunthorpe &dev_attr_device_version.attr,
4535d8e1881SLogan Gunthorpe &dev_attr_fw_version.attr,
4545d8e1881SLogan Gunthorpe &dev_attr_vendor_id.attr,
4555d8e1881SLogan Gunthorpe &dev_attr_product_id.attr,
4565d8e1881SLogan Gunthorpe &dev_attr_product_revision.attr,
4575d8e1881SLogan Gunthorpe &dev_attr_component_vendor.attr,
4585d8e1881SLogan Gunthorpe &dev_attr_component_id.attr,
4595d8e1881SLogan Gunthorpe &dev_attr_component_revision.attr,
4605d8e1881SLogan Gunthorpe &dev_attr_partition.attr,
4615d8e1881SLogan Gunthorpe &dev_attr_partition_count.attr,
4625d8e1881SLogan Gunthorpe NULL,
4635d8e1881SLogan Gunthorpe };
4645d8e1881SLogan Gunthorpe
4655d8e1881SLogan Gunthorpe ATTRIBUTE_GROUPS(switchtec_device);
4665d8e1881SLogan Gunthorpe
switchtec_dev_open(struct inode * inode,struct file * filp)467080b47deSLogan Gunthorpe static int switchtec_dev_open(struct inode *inode, struct file *filp)
468080b47deSLogan Gunthorpe {
469080b47deSLogan Gunthorpe struct switchtec_dev *stdev;
470080b47deSLogan Gunthorpe struct switchtec_user *stuser;
471080b47deSLogan Gunthorpe
472080b47deSLogan Gunthorpe stdev = container_of(inode->i_cdev, struct switchtec_dev, cdev);
473080b47deSLogan Gunthorpe
474080b47deSLogan Gunthorpe stuser = stuser_create(stdev);
475080b47deSLogan Gunthorpe if (IS_ERR(stuser))
476080b47deSLogan Gunthorpe return PTR_ERR(stuser);
477080b47deSLogan Gunthorpe
478080b47deSLogan Gunthorpe filp->private_data = stuser;
479c5bf68feSKirill Smelkov stream_open(inode, filp);
480080b47deSLogan Gunthorpe
481080b47deSLogan Gunthorpe dev_dbg(&stdev->dev, "%s: %p\n", __func__, stuser);
482080b47deSLogan Gunthorpe
483080b47deSLogan Gunthorpe return 0;
484080b47deSLogan Gunthorpe }
485080b47deSLogan Gunthorpe
switchtec_dev_release(struct inode * inode,struct file * filp)486080b47deSLogan Gunthorpe static int switchtec_dev_release(struct inode *inode, struct file *filp)
487080b47deSLogan Gunthorpe {
488080b47deSLogan Gunthorpe struct switchtec_user *stuser = filp->private_data;
489080b47deSLogan Gunthorpe
490080b47deSLogan Gunthorpe stuser_put(stuser);
491080b47deSLogan Gunthorpe
492080b47deSLogan Gunthorpe return 0;
493080b47deSLogan Gunthorpe }
494080b47deSLogan Gunthorpe
lock_mutex_and_test_alive(struct switchtec_dev * stdev)495080b47deSLogan Gunthorpe static int lock_mutex_and_test_alive(struct switchtec_dev *stdev)
496080b47deSLogan Gunthorpe {
497080b47deSLogan Gunthorpe if (mutex_lock_interruptible(&stdev->mrpc_mutex))
498080b47deSLogan Gunthorpe return -EINTR;
499080b47deSLogan Gunthorpe
500080b47deSLogan Gunthorpe if (!stdev->alive) {
501080b47deSLogan Gunthorpe mutex_unlock(&stdev->mrpc_mutex);
502080b47deSLogan Gunthorpe return -ENODEV;
503080b47deSLogan Gunthorpe }
504080b47deSLogan Gunthorpe
505080b47deSLogan Gunthorpe return 0;
506080b47deSLogan Gunthorpe }
507080b47deSLogan Gunthorpe
switchtec_dev_write(struct file * filp,const char __user * data,size_t size,loff_t * off)508080b47deSLogan Gunthorpe static ssize_t switchtec_dev_write(struct file *filp, const char __user *data,
509080b47deSLogan Gunthorpe size_t size, loff_t *off)
510080b47deSLogan Gunthorpe {
511080b47deSLogan Gunthorpe struct switchtec_user *stuser = filp->private_data;
512080b47deSLogan Gunthorpe struct switchtec_dev *stdev = stuser->stdev;
513080b47deSLogan Gunthorpe int rc;
514080b47deSLogan Gunthorpe
515080b47deSLogan Gunthorpe if (size < sizeof(stuser->cmd) ||
516080b47deSLogan Gunthorpe size > sizeof(stuser->cmd) + sizeof(stuser->data))
517080b47deSLogan Gunthorpe return -EINVAL;
518080b47deSLogan Gunthorpe
519080b47deSLogan Gunthorpe stuser->data_len = size - sizeof(stuser->cmd);
520080b47deSLogan Gunthorpe
521080b47deSLogan Gunthorpe rc = lock_mutex_and_test_alive(stdev);
522080b47deSLogan Gunthorpe if (rc)
523080b47deSLogan Gunthorpe return rc;
524080b47deSLogan Gunthorpe
525080b47deSLogan Gunthorpe if (stuser->state != MRPC_IDLE) {
526080b47deSLogan Gunthorpe rc = -EBADE;
527080b47deSLogan Gunthorpe goto out;
528080b47deSLogan Gunthorpe }
529080b47deSLogan Gunthorpe
530080b47deSLogan Gunthorpe rc = copy_from_user(&stuser->cmd, data, sizeof(stuser->cmd));
531080b47deSLogan Gunthorpe if (rc) {
532080b47deSLogan Gunthorpe rc = -EFAULT;
533080b47deSLogan Gunthorpe goto out;
534080b47deSLogan Gunthorpe }
535ce7c8860SKelvin Cao if (((MRPC_CMD_ID(stuser->cmd) == MRPC_GAS_WRITE) ||
536ce7c8860SKelvin Cao (MRPC_CMD_ID(stuser->cmd) == MRPC_GAS_READ)) &&
537ce7c8860SKelvin Cao !capable(CAP_SYS_ADMIN)) {
538ce7c8860SKelvin Cao rc = -EPERM;
539ce7c8860SKelvin Cao goto out;
540ce7c8860SKelvin Cao }
541080b47deSLogan Gunthorpe
542080b47deSLogan Gunthorpe data += sizeof(stuser->cmd);
543080b47deSLogan Gunthorpe rc = copy_from_user(&stuser->data, data, size - sizeof(stuser->cmd));
544080b47deSLogan Gunthorpe if (rc) {
545080b47deSLogan Gunthorpe rc = -EFAULT;
546080b47deSLogan Gunthorpe goto out;
547080b47deSLogan Gunthorpe }
548080b47deSLogan Gunthorpe
549080b47deSLogan Gunthorpe rc = mrpc_queue_cmd(stuser);
550080b47deSLogan Gunthorpe
551080b47deSLogan Gunthorpe out:
552080b47deSLogan Gunthorpe mutex_unlock(&stdev->mrpc_mutex);
553080b47deSLogan Gunthorpe
554080b47deSLogan Gunthorpe if (rc)
555080b47deSLogan Gunthorpe return rc;
556080b47deSLogan Gunthorpe
557080b47deSLogan Gunthorpe return size;
558080b47deSLogan Gunthorpe }
559080b47deSLogan Gunthorpe
switchtec_dev_read(struct file * filp,char __user * data,size_t size,loff_t * off)560080b47deSLogan Gunthorpe static ssize_t switchtec_dev_read(struct file *filp, char __user *data,
561080b47deSLogan Gunthorpe size_t size, loff_t *off)
562080b47deSLogan Gunthorpe {
563080b47deSLogan Gunthorpe struct switchtec_user *stuser = filp->private_data;
564080b47deSLogan Gunthorpe struct switchtec_dev *stdev = stuser->stdev;
565080b47deSLogan Gunthorpe int rc;
566080b47deSLogan Gunthorpe
567080b47deSLogan Gunthorpe if (size < sizeof(stuser->cmd) ||
568080b47deSLogan Gunthorpe size > sizeof(stuser->cmd) + sizeof(stuser->data))
569080b47deSLogan Gunthorpe return -EINVAL;
570080b47deSLogan Gunthorpe
571080b47deSLogan Gunthorpe rc = lock_mutex_and_test_alive(stdev);
572080b47deSLogan Gunthorpe if (rc)
573080b47deSLogan Gunthorpe return rc;
574080b47deSLogan Gunthorpe
575080b47deSLogan Gunthorpe if (stuser->state == MRPC_IDLE) {
576080b47deSLogan Gunthorpe mutex_unlock(&stdev->mrpc_mutex);
577080b47deSLogan Gunthorpe return -EBADE;
578080b47deSLogan Gunthorpe }
579080b47deSLogan Gunthorpe
580080b47deSLogan Gunthorpe stuser->read_len = size - sizeof(stuser->return_code);
581080b47deSLogan Gunthorpe
582080b47deSLogan Gunthorpe mutex_unlock(&stdev->mrpc_mutex);
583080b47deSLogan Gunthorpe
584080b47deSLogan Gunthorpe if (filp->f_flags & O_NONBLOCK) {
585deaa0a8aSSebastian Andrzej Siewior if (!stuser->cmd_done)
586080b47deSLogan Gunthorpe return -EAGAIN;
587080b47deSLogan Gunthorpe } else {
588deaa0a8aSSebastian Andrzej Siewior rc = wait_event_interruptible(stuser->cmd_comp,
589deaa0a8aSSebastian Andrzej Siewior stuser->cmd_done);
590080b47deSLogan Gunthorpe if (rc < 0)
591080b47deSLogan Gunthorpe return rc;
592080b47deSLogan Gunthorpe }
593080b47deSLogan Gunthorpe
594080b47deSLogan Gunthorpe rc = lock_mutex_and_test_alive(stdev);
595080b47deSLogan Gunthorpe if (rc)
596080b47deSLogan Gunthorpe return rc;
597080b47deSLogan Gunthorpe
5981a323bd0SKelvin Cao if (stuser->state == MRPC_IO_ERROR) {
5991a323bd0SKelvin Cao mutex_unlock(&stdev->mrpc_mutex);
6001a323bd0SKelvin Cao return -EIO;
6011a323bd0SKelvin Cao }
6021a323bd0SKelvin Cao
603080b47deSLogan Gunthorpe if (stuser->state != MRPC_DONE) {
604080b47deSLogan Gunthorpe mutex_unlock(&stdev->mrpc_mutex);
605080b47deSLogan Gunthorpe return -EBADE;
606080b47deSLogan Gunthorpe }
607080b47deSLogan Gunthorpe
608080b47deSLogan Gunthorpe rc = copy_to_user(data, &stuser->return_code,
609080b47deSLogan Gunthorpe sizeof(stuser->return_code));
610080b47deSLogan Gunthorpe if (rc) {
611ddc10938SBjorn Helgaas mutex_unlock(&stdev->mrpc_mutex);
612ddc10938SBjorn Helgaas return -EFAULT;
613080b47deSLogan Gunthorpe }
614080b47deSLogan Gunthorpe
615080b47deSLogan Gunthorpe data += sizeof(stuser->return_code);
616080b47deSLogan Gunthorpe rc = copy_to_user(data, &stuser->data,
617080b47deSLogan Gunthorpe size - sizeof(stuser->return_code));
618080b47deSLogan Gunthorpe if (rc) {
619ddc10938SBjorn Helgaas mutex_unlock(&stdev->mrpc_mutex);
620ddc10938SBjorn Helgaas return -EFAULT;
621080b47deSLogan Gunthorpe }
622080b47deSLogan Gunthorpe
623080b47deSLogan Gunthorpe stuser_set_state(stuser, MRPC_IDLE);
624080b47deSLogan Gunthorpe
625080b47deSLogan Gunthorpe mutex_unlock(&stdev->mrpc_mutex);
626080b47deSLogan Gunthorpe
627551ec658SKelvin Cao if (stuser->status == SWITCHTEC_MRPC_STATUS_DONE ||
628551ec658SKelvin Cao stuser->status == SWITCHTEC_MRPC_STATUS_ERROR)
629080b47deSLogan Gunthorpe return size;
630080b47deSLogan Gunthorpe else if (stuser->status == SWITCHTEC_MRPC_STATUS_INTERRUPTED)
631080b47deSLogan Gunthorpe return -ENXIO;
632080b47deSLogan Gunthorpe else
633080b47deSLogan Gunthorpe return -EBADMSG;
634080b47deSLogan Gunthorpe }
635080b47deSLogan Gunthorpe
switchtec_dev_poll(struct file * filp,poll_table * wait)636afc9a42bSAl Viro static __poll_t switchtec_dev_poll(struct file *filp, poll_table *wait)
637080b47deSLogan Gunthorpe {
638080b47deSLogan Gunthorpe struct switchtec_user *stuser = filp->private_data;
639080b47deSLogan Gunthorpe struct switchtec_dev *stdev = stuser->stdev;
640afc9a42bSAl Viro __poll_t ret = 0;
641080b47deSLogan Gunthorpe
642deaa0a8aSSebastian Andrzej Siewior poll_wait(filp, &stuser->cmd_comp, wait);
643080b47deSLogan Gunthorpe poll_wait(filp, &stdev->event_wq, wait);
644080b47deSLogan Gunthorpe
645080b47deSLogan Gunthorpe if (lock_mutex_and_test_alive(stdev))
646a9a08845SLinus Torvalds return EPOLLIN | EPOLLRDHUP | EPOLLOUT | EPOLLERR | EPOLLHUP;
647080b47deSLogan Gunthorpe
648080b47deSLogan Gunthorpe mutex_unlock(&stdev->mrpc_mutex);
649080b47deSLogan Gunthorpe
650deaa0a8aSSebastian Andrzej Siewior if (stuser->cmd_done)
651a9a08845SLinus Torvalds ret |= EPOLLIN | EPOLLRDNORM;
652080b47deSLogan Gunthorpe
653080b47deSLogan Gunthorpe if (stuser->event_cnt != atomic_read(&stdev->event_cnt))
654a9a08845SLinus Torvalds ret |= EPOLLPRI | EPOLLRDBAND;
655080b47deSLogan Gunthorpe
656080b47deSLogan Gunthorpe return ret;
657080b47deSLogan Gunthorpe }
658080b47deSLogan Gunthorpe
ioctl_flash_info(struct switchtec_dev * stdev,struct switchtec_ioctl_flash_info __user * uinfo)65952eabba5SLogan Gunthorpe static int ioctl_flash_info(struct switchtec_dev *stdev,
66052eabba5SLogan Gunthorpe struct switchtec_ioctl_flash_info __user *uinfo)
66152eabba5SLogan Gunthorpe {
66252eabba5SLogan Gunthorpe struct switchtec_ioctl_flash_info info = {0};
66352eabba5SLogan Gunthorpe struct flash_info_regs __iomem *fi = stdev->mmio_flash_info;
66452eabba5SLogan Gunthorpe
665993d208dSLogan Gunthorpe if (stdev->gen == SWITCHTEC_GEN3) {
666993d208dSLogan Gunthorpe info.flash_length = ioread32(&fi->gen3.flash_length);
667fcccd282SLogan Gunthorpe info.num_partitions = SWITCHTEC_NUM_PARTITIONS_GEN3;
6680fb53e64SKelvin Cao } else if (stdev->gen >= SWITCHTEC_GEN4) {
6694efa1d2eSKelvin Cao info.flash_length = ioread32(&fi->gen4.flash_length);
6704efa1d2eSKelvin Cao info.num_partitions = SWITCHTEC_NUM_PARTITIONS_GEN4;
671993d208dSLogan Gunthorpe } else {
67267116444SKelvin Cao return -EOPNOTSUPP;
673993d208dSLogan Gunthorpe }
67452eabba5SLogan Gunthorpe
67552eabba5SLogan Gunthorpe if (copy_to_user(uinfo, &info, sizeof(info)))
67652eabba5SLogan Gunthorpe return -EFAULT;
67752eabba5SLogan Gunthorpe
67852eabba5SLogan Gunthorpe return 0;
67952eabba5SLogan Gunthorpe }
68052eabba5SLogan Gunthorpe
set_fw_info_part(struct switchtec_ioctl_flash_part_info * info,struct partition_info __iomem * pi)68152eabba5SLogan Gunthorpe static void set_fw_info_part(struct switchtec_ioctl_flash_part_info *info,
68252eabba5SLogan Gunthorpe struct partition_info __iomem *pi)
68352eabba5SLogan Gunthorpe {
68452eabba5SLogan Gunthorpe info->address = ioread32(&pi->address);
68552eabba5SLogan Gunthorpe info->length = ioread32(&pi->length);
68652eabba5SLogan Gunthorpe }
68752eabba5SLogan Gunthorpe
flash_part_info_gen3(struct switchtec_dev * stdev,struct switchtec_ioctl_flash_part_info * info)6886a3d1b54SLogan Gunthorpe static int flash_part_info_gen3(struct switchtec_dev *stdev,
6896a3d1b54SLogan Gunthorpe struct switchtec_ioctl_flash_part_info *info)
69052eabba5SLogan Gunthorpe {
691993d208dSLogan Gunthorpe struct flash_info_regs_gen3 __iomem *fi =
692993d208dSLogan Gunthorpe &stdev->mmio_flash_info->gen3;
693993d208dSLogan Gunthorpe struct sys_info_regs_gen3 __iomem *si = &stdev->mmio_sys_info->gen3;
69452eabba5SLogan Gunthorpe u32 active_addr = -1;
69552eabba5SLogan Gunthorpe
6966a3d1b54SLogan Gunthorpe switch (info->flash_partition) {
69752eabba5SLogan Gunthorpe case SWITCHTEC_IOCTL_PART_CFG0:
69852eabba5SLogan Gunthorpe active_addr = ioread32(&fi->active_cfg);
6996a3d1b54SLogan Gunthorpe set_fw_info_part(info, &fi->cfg0);
700fcccd282SLogan Gunthorpe if (ioread16(&si->cfg_running) == SWITCHTEC_GEN3_CFG0_RUNNING)
7016a3d1b54SLogan Gunthorpe info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
70252eabba5SLogan Gunthorpe break;
70352eabba5SLogan Gunthorpe case SWITCHTEC_IOCTL_PART_CFG1:
70452eabba5SLogan Gunthorpe active_addr = ioread32(&fi->active_cfg);
7056a3d1b54SLogan Gunthorpe set_fw_info_part(info, &fi->cfg1);
706fcccd282SLogan Gunthorpe if (ioread16(&si->cfg_running) == SWITCHTEC_GEN3_CFG1_RUNNING)
7076a3d1b54SLogan Gunthorpe info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
70852eabba5SLogan Gunthorpe break;
70952eabba5SLogan Gunthorpe case SWITCHTEC_IOCTL_PART_IMG0:
71052eabba5SLogan Gunthorpe active_addr = ioread32(&fi->active_img);
7116a3d1b54SLogan Gunthorpe set_fw_info_part(info, &fi->img0);
712fcccd282SLogan Gunthorpe if (ioread16(&si->img_running) == SWITCHTEC_GEN3_IMG0_RUNNING)
7136a3d1b54SLogan Gunthorpe info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
71452eabba5SLogan Gunthorpe break;
71552eabba5SLogan Gunthorpe case SWITCHTEC_IOCTL_PART_IMG1:
71652eabba5SLogan Gunthorpe active_addr = ioread32(&fi->active_img);
7176a3d1b54SLogan Gunthorpe set_fw_info_part(info, &fi->img1);
718fcccd282SLogan Gunthorpe if (ioread16(&si->img_running) == SWITCHTEC_GEN3_IMG1_RUNNING)
7196a3d1b54SLogan Gunthorpe info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
72052eabba5SLogan Gunthorpe break;
72152eabba5SLogan Gunthorpe case SWITCHTEC_IOCTL_PART_NVLOG:
7226a3d1b54SLogan Gunthorpe set_fw_info_part(info, &fi->nvlog);
72352eabba5SLogan Gunthorpe break;
72452eabba5SLogan Gunthorpe case SWITCHTEC_IOCTL_PART_VENDOR0:
7256a3d1b54SLogan Gunthorpe set_fw_info_part(info, &fi->vendor[0]);
72652eabba5SLogan Gunthorpe break;
72752eabba5SLogan Gunthorpe case SWITCHTEC_IOCTL_PART_VENDOR1:
7286a3d1b54SLogan Gunthorpe set_fw_info_part(info, &fi->vendor[1]);
72952eabba5SLogan Gunthorpe break;
73052eabba5SLogan Gunthorpe case SWITCHTEC_IOCTL_PART_VENDOR2:
7316a3d1b54SLogan Gunthorpe set_fw_info_part(info, &fi->vendor[2]);
73252eabba5SLogan Gunthorpe break;
73352eabba5SLogan Gunthorpe case SWITCHTEC_IOCTL_PART_VENDOR3:
7346a3d1b54SLogan Gunthorpe set_fw_info_part(info, &fi->vendor[3]);
73552eabba5SLogan Gunthorpe break;
73652eabba5SLogan Gunthorpe case SWITCHTEC_IOCTL_PART_VENDOR4:
7376a3d1b54SLogan Gunthorpe set_fw_info_part(info, &fi->vendor[4]);
73852eabba5SLogan Gunthorpe break;
73952eabba5SLogan Gunthorpe case SWITCHTEC_IOCTL_PART_VENDOR5:
7406a3d1b54SLogan Gunthorpe set_fw_info_part(info, &fi->vendor[5]);
74152eabba5SLogan Gunthorpe break;
74252eabba5SLogan Gunthorpe case SWITCHTEC_IOCTL_PART_VENDOR6:
7436a3d1b54SLogan Gunthorpe set_fw_info_part(info, &fi->vendor[6]);
74452eabba5SLogan Gunthorpe break;
74552eabba5SLogan Gunthorpe case SWITCHTEC_IOCTL_PART_VENDOR7:
7466a3d1b54SLogan Gunthorpe set_fw_info_part(info, &fi->vendor[7]);
74752eabba5SLogan Gunthorpe break;
74852eabba5SLogan Gunthorpe default:
74952eabba5SLogan Gunthorpe return -EINVAL;
75052eabba5SLogan Gunthorpe }
75152eabba5SLogan Gunthorpe
7526a3d1b54SLogan Gunthorpe if (info->address == active_addr)
7536a3d1b54SLogan Gunthorpe info->active |= SWITCHTEC_IOCTL_PART_ACTIVE;
7546a3d1b54SLogan Gunthorpe
7556a3d1b54SLogan Gunthorpe return 0;
7566a3d1b54SLogan Gunthorpe }
7576a3d1b54SLogan Gunthorpe
flash_part_info_gen4(struct switchtec_dev * stdev,struct switchtec_ioctl_flash_part_info * info)7584efa1d2eSKelvin Cao static int flash_part_info_gen4(struct switchtec_dev *stdev,
7594efa1d2eSKelvin Cao struct switchtec_ioctl_flash_part_info *info)
7604efa1d2eSKelvin Cao {
7614efa1d2eSKelvin Cao struct flash_info_regs_gen4 __iomem *fi = &stdev->mmio_flash_info->gen4;
7624efa1d2eSKelvin Cao struct sys_info_regs_gen4 __iomem *si = &stdev->mmio_sys_info->gen4;
7634efa1d2eSKelvin Cao struct active_partition_info_gen4 __iomem *af = &fi->active_flag;
7644efa1d2eSKelvin Cao
7654efa1d2eSKelvin Cao switch (info->flash_partition) {
7664efa1d2eSKelvin Cao case SWITCHTEC_IOCTL_PART_MAP_0:
7674efa1d2eSKelvin Cao set_fw_info_part(info, &fi->map0);
7684efa1d2eSKelvin Cao break;
7694efa1d2eSKelvin Cao case SWITCHTEC_IOCTL_PART_MAP_1:
7704efa1d2eSKelvin Cao set_fw_info_part(info, &fi->map1);
7714efa1d2eSKelvin Cao break;
7724efa1d2eSKelvin Cao case SWITCHTEC_IOCTL_PART_KEY_0:
7734efa1d2eSKelvin Cao set_fw_info_part(info, &fi->key0);
7744efa1d2eSKelvin Cao if (ioread8(&af->key) == SWITCHTEC_GEN4_KEY0_ACTIVE)
7754efa1d2eSKelvin Cao info->active |= SWITCHTEC_IOCTL_PART_ACTIVE;
7764efa1d2eSKelvin Cao if (ioread16(&si->key_running) == SWITCHTEC_GEN4_KEY0_RUNNING)
7774efa1d2eSKelvin Cao info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
7784efa1d2eSKelvin Cao break;
7794efa1d2eSKelvin Cao case SWITCHTEC_IOCTL_PART_KEY_1:
7804efa1d2eSKelvin Cao set_fw_info_part(info, &fi->key1);
7814efa1d2eSKelvin Cao if (ioread8(&af->key) == SWITCHTEC_GEN4_KEY1_ACTIVE)
7824efa1d2eSKelvin Cao info->active |= SWITCHTEC_IOCTL_PART_ACTIVE;
7834efa1d2eSKelvin Cao if (ioread16(&si->key_running) == SWITCHTEC_GEN4_KEY1_RUNNING)
7844efa1d2eSKelvin Cao info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
7854efa1d2eSKelvin Cao break;
7864efa1d2eSKelvin Cao case SWITCHTEC_IOCTL_PART_BL2_0:
7874efa1d2eSKelvin Cao set_fw_info_part(info, &fi->bl2_0);
7884efa1d2eSKelvin Cao if (ioread8(&af->bl2) == SWITCHTEC_GEN4_BL2_0_ACTIVE)
7894efa1d2eSKelvin Cao info->active |= SWITCHTEC_IOCTL_PART_ACTIVE;
7904efa1d2eSKelvin Cao if (ioread16(&si->bl2_running) == SWITCHTEC_GEN4_BL2_0_RUNNING)
7914efa1d2eSKelvin Cao info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
7924efa1d2eSKelvin Cao break;
7934efa1d2eSKelvin Cao case SWITCHTEC_IOCTL_PART_BL2_1:
7944efa1d2eSKelvin Cao set_fw_info_part(info, &fi->bl2_1);
7954efa1d2eSKelvin Cao if (ioread8(&af->bl2) == SWITCHTEC_GEN4_BL2_1_ACTIVE)
7964efa1d2eSKelvin Cao info->active |= SWITCHTEC_IOCTL_PART_ACTIVE;
7974efa1d2eSKelvin Cao if (ioread16(&si->bl2_running) == SWITCHTEC_GEN4_BL2_1_RUNNING)
7984efa1d2eSKelvin Cao info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
7994efa1d2eSKelvin Cao break;
8004efa1d2eSKelvin Cao case SWITCHTEC_IOCTL_PART_CFG0:
8014efa1d2eSKelvin Cao set_fw_info_part(info, &fi->cfg0);
8024efa1d2eSKelvin Cao if (ioread8(&af->cfg) == SWITCHTEC_GEN4_CFG0_ACTIVE)
8034efa1d2eSKelvin Cao info->active |= SWITCHTEC_IOCTL_PART_ACTIVE;
8044efa1d2eSKelvin Cao if (ioread16(&si->cfg_running) == SWITCHTEC_GEN4_CFG0_RUNNING)
8054efa1d2eSKelvin Cao info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
8064efa1d2eSKelvin Cao break;
8074efa1d2eSKelvin Cao case SWITCHTEC_IOCTL_PART_CFG1:
8084efa1d2eSKelvin Cao set_fw_info_part(info, &fi->cfg1);
8094efa1d2eSKelvin Cao if (ioread8(&af->cfg) == SWITCHTEC_GEN4_CFG1_ACTIVE)
8104efa1d2eSKelvin Cao info->active |= SWITCHTEC_IOCTL_PART_ACTIVE;
8114efa1d2eSKelvin Cao if (ioread16(&si->cfg_running) == SWITCHTEC_GEN4_CFG1_RUNNING)
8124efa1d2eSKelvin Cao info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
8134efa1d2eSKelvin Cao break;
8144efa1d2eSKelvin Cao case SWITCHTEC_IOCTL_PART_IMG0:
8154efa1d2eSKelvin Cao set_fw_info_part(info, &fi->img0);
8164efa1d2eSKelvin Cao if (ioread8(&af->img) == SWITCHTEC_GEN4_IMG0_ACTIVE)
8174efa1d2eSKelvin Cao info->active |= SWITCHTEC_IOCTL_PART_ACTIVE;
8184efa1d2eSKelvin Cao if (ioread16(&si->img_running) == SWITCHTEC_GEN4_IMG0_RUNNING)
8194efa1d2eSKelvin Cao info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
8204efa1d2eSKelvin Cao break;
8214efa1d2eSKelvin Cao case SWITCHTEC_IOCTL_PART_IMG1:
8224efa1d2eSKelvin Cao set_fw_info_part(info, &fi->img1);
8234efa1d2eSKelvin Cao if (ioread8(&af->img) == SWITCHTEC_GEN4_IMG1_ACTIVE)
8244efa1d2eSKelvin Cao info->active |= SWITCHTEC_IOCTL_PART_ACTIVE;
8254efa1d2eSKelvin Cao if (ioread16(&si->img_running) == SWITCHTEC_GEN4_IMG1_RUNNING)
8264efa1d2eSKelvin Cao info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
8274efa1d2eSKelvin Cao break;
8284efa1d2eSKelvin Cao case SWITCHTEC_IOCTL_PART_NVLOG:
8294efa1d2eSKelvin Cao set_fw_info_part(info, &fi->nvlog);
8304efa1d2eSKelvin Cao break;
8314efa1d2eSKelvin Cao case SWITCHTEC_IOCTL_PART_VENDOR0:
8324efa1d2eSKelvin Cao set_fw_info_part(info, &fi->vendor[0]);
8334efa1d2eSKelvin Cao break;
8344efa1d2eSKelvin Cao case SWITCHTEC_IOCTL_PART_VENDOR1:
8354efa1d2eSKelvin Cao set_fw_info_part(info, &fi->vendor[1]);
8364efa1d2eSKelvin Cao break;
8374efa1d2eSKelvin Cao case SWITCHTEC_IOCTL_PART_VENDOR2:
8384efa1d2eSKelvin Cao set_fw_info_part(info, &fi->vendor[2]);
8394efa1d2eSKelvin Cao break;
8404efa1d2eSKelvin Cao case SWITCHTEC_IOCTL_PART_VENDOR3:
8414efa1d2eSKelvin Cao set_fw_info_part(info, &fi->vendor[3]);
8424efa1d2eSKelvin Cao break;
8434efa1d2eSKelvin Cao case SWITCHTEC_IOCTL_PART_VENDOR4:
8444efa1d2eSKelvin Cao set_fw_info_part(info, &fi->vendor[4]);
8454efa1d2eSKelvin Cao break;
8464efa1d2eSKelvin Cao case SWITCHTEC_IOCTL_PART_VENDOR5:
8474efa1d2eSKelvin Cao set_fw_info_part(info, &fi->vendor[5]);
8484efa1d2eSKelvin Cao break;
8494efa1d2eSKelvin Cao case SWITCHTEC_IOCTL_PART_VENDOR6:
8504efa1d2eSKelvin Cao set_fw_info_part(info, &fi->vendor[6]);
8514efa1d2eSKelvin Cao break;
8524efa1d2eSKelvin Cao case SWITCHTEC_IOCTL_PART_VENDOR7:
8534efa1d2eSKelvin Cao set_fw_info_part(info, &fi->vendor[7]);
8544efa1d2eSKelvin Cao break;
8554efa1d2eSKelvin Cao default:
8564efa1d2eSKelvin Cao return -EINVAL;
8574efa1d2eSKelvin Cao }
8584efa1d2eSKelvin Cao
8594efa1d2eSKelvin Cao return 0;
8604efa1d2eSKelvin Cao }
8614efa1d2eSKelvin Cao
ioctl_flash_part_info(struct switchtec_dev * stdev,struct switchtec_ioctl_flash_part_info __user * uinfo)8626a3d1b54SLogan Gunthorpe static int ioctl_flash_part_info(struct switchtec_dev *stdev,
8636a3d1b54SLogan Gunthorpe struct switchtec_ioctl_flash_part_info __user *uinfo)
8646a3d1b54SLogan Gunthorpe {
8656a3d1b54SLogan Gunthorpe int ret;
8666a3d1b54SLogan Gunthorpe struct switchtec_ioctl_flash_part_info info = {0};
8676a3d1b54SLogan Gunthorpe
8686a3d1b54SLogan Gunthorpe if (copy_from_user(&info, uinfo, sizeof(info)))
8696a3d1b54SLogan Gunthorpe return -EFAULT;
8706a3d1b54SLogan Gunthorpe
8716a3d1b54SLogan Gunthorpe if (stdev->gen == SWITCHTEC_GEN3) {
8726a3d1b54SLogan Gunthorpe ret = flash_part_info_gen3(stdev, &info);
8736a3d1b54SLogan Gunthorpe if (ret)
8746a3d1b54SLogan Gunthorpe return ret;
8750fb53e64SKelvin Cao } else if (stdev->gen >= SWITCHTEC_GEN4) {
8764efa1d2eSKelvin Cao ret = flash_part_info_gen4(stdev, &info);
8774efa1d2eSKelvin Cao if (ret)
8784efa1d2eSKelvin Cao return ret;
8796a3d1b54SLogan Gunthorpe } else {
88067116444SKelvin Cao return -EOPNOTSUPP;
8816a3d1b54SLogan Gunthorpe }
88252eabba5SLogan Gunthorpe
88352eabba5SLogan Gunthorpe if (copy_to_user(uinfo, &info, sizeof(info)))
88452eabba5SLogan Gunthorpe return -EFAULT;
88552eabba5SLogan Gunthorpe
88652eabba5SLogan Gunthorpe return 0;
88752eabba5SLogan Gunthorpe }
88852eabba5SLogan Gunthorpe
ioctl_event_summary(struct switchtec_dev * stdev,struct switchtec_user * stuser,struct switchtec_ioctl_event_summary __user * usum,size_t size)88952eabba5SLogan Gunthorpe static int ioctl_event_summary(struct switchtec_dev *stdev,
89052eabba5SLogan Gunthorpe struct switchtec_user *stuser,
891ba8a3982SWesley Sheng struct switchtec_ioctl_event_summary __user *usum,
892ba8a3982SWesley Sheng size_t size)
89352eabba5SLogan Gunthorpe {
894ba8a3982SWesley Sheng struct switchtec_ioctl_event_summary *s;
89552eabba5SLogan Gunthorpe int i;
89652eabba5SLogan Gunthorpe u32 reg;
897ba8a3982SWesley Sheng int ret = 0;
89852eabba5SLogan Gunthorpe
899ba8a3982SWesley Sheng s = kzalloc(sizeof(*s), GFP_KERNEL);
900ba8a3982SWesley Sheng if (!s)
901ba8a3982SWesley Sheng return -ENOMEM;
902ba8a3982SWesley Sheng
903ba8a3982SWesley Sheng s->global = ioread32(&stdev->mmio_sw_event->global_summary);
9046acdf7e1SLogan Gunthorpe s->part_bitmap = ioread64(&stdev->mmio_sw_event->part_event_bitmap);
905ba8a3982SWesley Sheng s->local_part = ioread32(&stdev->mmio_part_cfg->part_event_summary);
90652eabba5SLogan Gunthorpe
90752eabba5SLogan Gunthorpe for (i = 0; i < stdev->partition_count; i++) {
90852eabba5SLogan Gunthorpe reg = ioread32(&stdev->mmio_part_cfg_all[i].part_event_summary);
909ba8a3982SWesley Sheng s->part[i] = reg;
91052eabba5SLogan Gunthorpe }
91152eabba5SLogan Gunthorpe
9127501a02aSWesley Sheng for (i = 0; i < stdev->pff_csr_count; i++) {
91352eabba5SLogan Gunthorpe reg = ioread32(&stdev->mmio_pff_csr[i].pff_event_summary);
914ba8a3982SWesley Sheng s->pff[i] = reg;
91552eabba5SLogan Gunthorpe }
91652eabba5SLogan Gunthorpe
917ba8a3982SWesley Sheng if (copy_to_user(usum, s, size)) {
918ba8a3982SWesley Sheng ret = -EFAULT;
919ba8a3982SWesley Sheng goto error_case;
920ba8a3982SWesley Sheng }
92152eabba5SLogan Gunthorpe
92252eabba5SLogan Gunthorpe stuser->event_cnt = atomic_read(&stdev->event_cnt);
92352eabba5SLogan Gunthorpe
924ba8a3982SWesley Sheng error_case:
925ba8a3982SWesley Sheng kfree(s);
926ba8a3982SWesley Sheng return ret;
92752eabba5SLogan Gunthorpe }
92852eabba5SLogan Gunthorpe
global_ev_reg(struct switchtec_dev * stdev,size_t offset,int index)92952eabba5SLogan Gunthorpe static u32 __iomem *global_ev_reg(struct switchtec_dev *stdev,
93052eabba5SLogan Gunthorpe size_t offset, int index)
93152eabba5SLogan Gunthorpe {
93252eabba5SLogan Gunthorpe return (void __iomem *)stdev->mmio_sw_event + offset;
93352eabba5SLogan Gunthorpe }
93452eabba5SLogan Gunthorpe
part_ev_reg(struct switchtec_dev * stdev,size_t offset,int index)93552eabba5SLogan Gunthorpe static u32 __iomem *part_ev_reg(struct switchtec_dev *stdev,
93652eabba5SLogan Gunthorpe size_t offset, int index)
93752eabba5SLogan Gunthorpe {
93852eabba5SLogan Gunthorpe return (void __iomem *)&stdev->mmio_part_cfg_all[index] + offset;
93952eabba5SLogan Gunthorpe }
94052eabba5SLogan Gunthorpe
pff_ev_reg(struct switchtec_dev * stdev,size_t offset,int index)94152eabba5SLogan Gunthorpe static u32 __iomem *pff_ev_reg(struct switchtec_dev *stdev,
94252eabba5SLogan Gunthorpe size_t offset, int index)
94352eabba5SLogan Gunthorpe {
94452eabba5SLogan Gunthorpe return (void __iomem *)&stdev->mmio_pff_csr[index] + offset;
94552eabba5SLogan Gunthorpe }
94652eabba5SLogan Gunthorpe
94752eabba5SLogan Gunthorpe #define EV_GLB(i, r)[i] = {offsetof(struct sw_event_regs, r), global_ev_reg}
94852eabba5SLogan Gunthorpe #define EV_PAR(i, r)[i] = {offsetof(struct part_cfg_regs, r), part_ev_reg}
94952eabba5SLogan Gunthorpe #define EV_PFF(i, r)[i] = {offsetof(struct pff_csr_regs, r), pff_ev_reg}
95052eabba5SLogan Gunthorpe
951f05f7355SColin Ian King static const struct event_reg {
95252eabba5SLogan Gunthorpe size_t offset;
95352eabba5SLogan Gunthorpe u32 __iomem *(*map_reg)(struct switchtec_dev *stdev,
95452eabba5SLogan Gunthorpe size_t offset, int index);
95552eabba5SLogan Gunthorpe } event_regs[] = {
95652eabba5SLogan Gunthorpe EV_GLB(SWITCHTEC_IOCTL_EVENT_STACK_ERROR, stack_error_event_hdr),
95752eabba5SLogan Gunthorpe EV_GLB(SWITCHTEC_IOCTL_EVENT_PPU_ERROR, ppu_error_event_hdr),
95852eabba5SLogan Gunthorpe EV_GLB(SWITCHTEC_IOCTL_EVENT_ISP_ERROR, isp_error_event_hdr),
95952eabba5SLogan Gunthorpe EV_GLB(SWITCHTEC_IOCTL_EVENT_SYS_RESET, sys_reset_event_hdr),
96052eabba5SLogan Gunthorpe EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_EXC, fw_exception_hdr),
96152eabba5SLogan Gunthorpe EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_NMI, fw_nmi_hdr),
96252eabba5SLogan Gunthorpe EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_NON_FATAL, fw_non_fatal_hdr),
96352eabba5SLogan Gunthorpe EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_FATAL, fw_fatal_hdr),
96452eabba5SLogan Gunthorpe EV_GLB(SWITCHTEC_IOCTL_EVENT_TWI_MRPC_COMP, twi_mrpc_comp_hdr),
96552eabba5SLogan Gunthorpe EV_GLB(SWITCHTEC_IOCTL_EVENT_TWI_MRPC_COMP_ASYNC,
96652eabba5SLogan Gunthorpe twi_mrpc_comp_async_hdr),
96752eabba5SLogan Gunthorpe EV_GLB(SWITCHTEC_IOCTL_EVENT_CLI_MRPC_COMP, cli_mrpc_comp_hdr),
96852eabba5SLogan Gunthorpe EV_GLB(SWITCHTEC_IOCTL_EVENT_CLI_MRPC_COMP_ASYNC,
96952eabba5SLogan Gunthorpe cli_mrpc_comp_async_hdr),
97052eabba5SLogan Gunthorpe EV_GLB(SWITCHTEC_IOCTL_EVENT_GPIO_INT, gpio_interrupt_hdr),
971f0edce7aSLogan Gunthorpe EV_GLB(SWITCHTEC_IOCTL_EVENT_GFMS, gfms_event_hdr),
97252eabba5SLogan Gunthorpe EV_PAR(SWITCHTEC_IOCTL_EVENT_PART_RESET, part_reset_hdr),
97352eabba5SLogan Gunthorpe EV_PAR(SWITCHTEC_IOCTL_EVENT_MRPC_COMP, mrpc_comp_hdr),
97452eabba5SLogan Gunthorpe EV_PAR(SWITCHTEC_IOCTL_EVENT_MRPC_COMP_ASYNC, mrpc_comp_async_hdr),
97552eabba5SLogan Gunthorpe EV_PAR(SWITCHTEC_IOCTL_EVENT_DYN_PART_BIND_COMP, dyn_binding_hdr),
976a6b0ef9aSLogan Gunthorpe EV_PAR(SWITCHTEC_IOCTL_EVENT_INTERCOMM_REQ_NOTIFY,
977a6b0ef9aSLogan Gunthorpe intercomm_notify_hdr),
97852eabba5SLogan Gunthorpe EV_PFF(SWITCHTEC_IOCTL_EVENT_AER_IN_P2P, aer_in_p2p_hdr),
97952eabba5SLogan Gunthorpe EV_PFF(SWITCHTEC_IOCTL_EVENT_AER_IN_VEP, aer_in_vep_hdr),
98052eabba5SLogan Gunthorpe EV_PFF(SWITCHTEC_IOCTL_EVENT_DPC, dpc_hdr),
98152eabba5SLogan Gunthorpe EV_PFF(SWITCHTEC_IOCTL_EVENT_CTS, cts_hdr),
982a6b0ef9aSLogan Gunthorpe EV_PFF(SWITCHTEC_IOCTL_EVENT_UEC, uec_hdr),
98352eabba5SLogan Gunthorpe EV_PFF(SWITCHTEC_IOCTL_EVENT_HOTPLUG, hotplug_hdr),
98452eabba5SLogan Gunthorpe EV_PFF(SWITCHTEC_IOCTL_EVENT_IER, ier_hdr),
98552eabba5SLogan Gunthorpe EV_PFF(SWITCHTEC_IOCTL_EVENT_THRESH, threshold_hdr),
98652eabba5SLogan Gunthorpe EV_PFF(SWITCHTEC_IOCTL_EVENT_POWER_MGMT, power_mgmt_hdr),
98752eabba5SLogan Gunthorpe EV_PFF(SWITCHTEC_IOCTL_EVENT_TLP_THROTTLING, tlp_throttling_hdr),
98852eabba5SLogan Gunthorpe EV_PFF(SWITCHTEC_IOCTL_EVENT_FORCE_SPEED, force_speed_hdr),
98952eabba5SLogan Gunthorpe EV_PFF(SWITCHTEC_IOCTL_EVENT_CREDIT_TIMEOUT, credit_timeout_hdr),
99052eabba5SLogan Gunthorpe EV_PFF(SWITCHTEC_IOCTL_EVENT_LINK_STATE, link_state_hdr),
99152eabba5SLogan Gunthorpe };
99252eabba5SLogan Gunthorpe
event_hdr_addr(struct switchtec_dev * stdev,int event_id,int index)99352eabba5SLogan Gunthorpe static u32 __iomem *event_hdr_addr(struct switchtec_dev *stdev,
99452eabba5SLogan Gunthorpe int event_id, int index)
99552eabba5SLogan Gunthorpe {
99652eabba5SLogan Gunthorpe size_t off;
99752eabba5SLogan Gunthorpe
99852eabba5SLogan Gunthorpe if (event_id < 0 || event_id >= SWITCHTEC_IOCTL_MAX_EVENTS)
9995f11723bSLogan Gunthorpe return (u32 __iomem *)ERR_PTR(-EINVAL);
100052eabba5SLogan Gunthorpe
100152eabba5SLogan Gunthorpe off = event_regs[event_id].offset;
100252eabba5SLogan Gunthorpe
100352eabba5SLogan Gunthorpe if (event_regs[event_id].map_reg == part_ev_reg) {
100452eabba5SLogan Gunthorpe if (index == SWITCHTEC_IOCTL_EVENT_LOCAL_PART_IDX)
100552eabba5SLogan Gunthorpe index = stdev->partition;
100652eabba5SLogan Gunthorpe else if (index < 0 || index >= stdev->partition_count)
10075f11723bSLogan Gunthorpe return (u32 __iomem *)ERR_PTR(-EINVAL);
100852eabba5SLogan Gunthorpe } else if (event_regs[event_id].map_reg == pff_ev_reg) {
100952eabba5SLogan Gunthorpe if (index < 0 || index >= stdev->pff_csr_count)
10105f11723bSLogan Gunthorpe return (u32 __iomem *)ERR_PTR(-EINVAL);
101152eabba5SLogan Gunthorpe }
101252eabba5SLogan Gunthorpe
101352eabba5SLogan Gunthorpe return event_regs[event_id].map_reg(stdev, off, index);
101452eabba5SLogan Gunthorpe }
101552eabba5SLogan Gunthorpe
event_ctl(struct switchtec_dev * stdev,struct switchtec_ioctl_event_ctl * ctl)101652eabba5SLogan Gunthorpe static int event_ctl(struct switchtec_dev *stdev,
101752eabba5SLogan Gunthorpe struct switchtec_ioctl_event_ctl *ctl)
101852eabba5SLogan Gunthorpe {
101952eabba5SLogan Gunthorpe int i;
102052eabba5SLogan Gunthorpe u32 __iomem *reg;
102152eabba5SLogan Gunthorpe u32 hdr;
102252eabba5SLogan Gunthorpe
102352eabba5SLogan Gunthorpe reg = event_hdr_addr(stdev, ctl->event_id, ctl->index);
102452eabba5SLogan Gunthorpe if (IS_ERR(reg))
102552eabba5SLogan Gunthorpe return PTR_ERR(reg);
102652eabba5SLogan Gunthorpe
102752eabba5SLogan Gunthorpe hdr = ioread32(reg);
10289f37ab04SLogan Gunthorpe if (hdr & SWITCHTEC_EVENT_NOT_SUPP)
10299f37ab04SLogan Gunthorpe return -EOPNOTSUPP;
10309f37ab04SLogan Gunthorpe
103152eabba5SLogan Gunthorpe for (i = 0; i < ARRAY_SIZE(ctl->data); i++)
103252eabba5SLogan Gunthorpe ctl->data[i] = ioread32(®[i + 1]);
103352eabba5SLogan Gunthorpe
103452eabba5SLogan Gunthorpe ctl->occurred = hdr & SWITCHTEC_EVENT_OCCURRED;
103552eabba5SLogan Gunthorpe ctl->count = (hdr >> 5) & 0xFF;
103652eabba5SLogan Gunthorpe
103752eabba5SLogan Gunthorpe if (!(ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_CLEAR))
103852eabba5SLogan Gunthorpe hdr &= ~SWITCHTEC_EVENT_CLEAR;
103952eabba5SLogan Gunthorpe if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL)
104052eabba5SLogan Gunthorpe hdr |= SWITCHTEC_EVENT_EN_IRQ;
104152eabba5SLogan Gunthorpe if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_POLL)
104252eabba5SLogan Gunthorpe hdr &= ~SWITCHTEC_EVENT_EN_IRQ;
104352eabba5SLogan Gunthorpe if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG)
104452eabba5SLogan Gunthorpe hdr |= SWITCHTEC_EVENT_EN_LOG;
104552eabba5SLogan Gunthorpe if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_LOG)
104652eabba5SLogan Gunthorpe hdr &= ~SWITCHTEC_EVENT_EN_LOG;
104752eabba5SLogan Gunthorpe if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI)
104852eabba5SLogan Gunthorpe hdr |= SWITCHTEC_EVENT_EN_CLI;
104952eabba5SLogan Gunthorpe if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_CLI)
105052eabba5SLogan Gunthorpe hdr &= ~SWITCHTEC_EVENT_EN_CLI;
105152eabba5SLogan Gunthorpe if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL)
105252eabba5SLogan Gunthorpe hdr |= SWITCHTEC_EVENT_FATAL;
105352eabba5SLogan Gunthorpe if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_FATAL)
105452eabba5SLogan Gunthorpe hdr &= ~SWITCHTEC_EVENT_FATAL;
105552eabba5SLogan Gunthorpe
105652eabba5SLogan Gunthorpe if (ctl->flags)
105752eabba5SLogan Gunthorpe iowrite32(hdr, reg);
105852eabba5SLogan Gunthorpe
105952eabba5SLogan Gunthorpe ctl->flags = 0;
106052eabba5SLogan Gunthorpe if (hdr & SWITCHTEC_EVENT_EN_IRQ)
106152eabba5SLogan Gunthorpe ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL;
106252eabba5SLogan Gunthorpe if (hdr & SWITCHTEC_EVENT_EN_LOG)
106352eabba5SLogan Gunthorpe ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG;
106452eabba5SLogan Gunthorpe if (hdr & SWITCHTEC_EVENT_EN_CLI)
106552eabba5SLogan Gunthorpe ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI;
106652eabba5SLogan Gunthorpe if (hdr & SWITCHTEC_EVENT_FATAL)
106752eabba5SLogan Gunthorpe ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL;
106852eabba5SLogan Gunthorpe
106952eabba5SLogan Gunthorpe return 0;
107052eabba5SLogan Gunthorpe }
107152eabba5SLogan Gunthorpe
ioctl_event_ctl(struct switchtec_dev * stdev,struct switchtec_ioctl_event_ctl __user * uctl)107252eabba5SLogan Gunthorpe static int ioctl_event_ctl(struct switchtec_dev *stdev,
107352eabba5SLogan Gunthorpe struct switchtec_ioctl_event_ctl __user *uctl)
107452eabba5SLogan Gunthorpe {
107552eabba5SLogan Gunthorpe int ret;
107652eabba5SLogan Gunthorpe int nr_idxs;
1077e4a7dca5SJoey Zhang unsigned int event_flags;
107852eabba5SLogan Gunthorpe struct switchtec_ioctl_event_ctl ctl;
107952eabba5SLogan Gunthorpe
108052eabba5SLogan Gunthorpe if (copy_from_user(&ctl, uctl, sizeof(ctl)))
108152eabba5SLogan Gunthorpe return -EFAULT;
108252eabba5SLogan Gunthorpe
108352eabba5SLogan Gunthorpe if (ctl.event_id >= SWITCHTEC_IOCTL_MAX_EVENTS)
108452eabba5SLogan Gunthorpe return -EINVAL;
108552eabba5SLogan Gunthorpe
108652eabba5SLogan Gunthorpe if (ctl.flags & SWITCHTEC_IOCTL_EVENT_FLAG_UNUSED)
108752eabba5SLogan Gunthorpe return -EINVAL;
108852eabba5SLogan Gunthorpe
108952eabba5SLogan Gunthorpe if (ctl.index == SWITCHTEC_IOCTL_EVENT_IDX_ALL) {
109052eabba5SLogan Gunthorpe if (event_regs[ctl.event_id].map_reg == global_ev_reg)
109152eabba5SLogan Gunthorpe nr_idxs = 1;
109252eabba5SLogan Gunthorpe else if (event_regs[ctl.event_id].map_reg == part_ev_reg)
109352eabba5SLogan Gunthorpe nr_idxs = stdev->partition_count;
109452eabba5SLogan Gunthorpe else if (event_regs[ctl.event_id].map_reg == pff_ev_reg)
109552eabba5SLogan Gunthorpe nr_idxs = stdev->pff_csr_count;
109652eabba5SLogan Gunthorpe else
109752eabba5SLogan Gunthorpe return -EINVAL;
109852eabba5SLogan Gunthorpe
1099e4a7dca5SJoey Zhang event_flags = ctl.flags;
110052eabba5SLogan Gunthorpe for (ctl.index = 0; ctl.index < nr_idxs; ctl.index++) {
1101e4a7dca5SJoey Zhang ctl.flags = event_flags;
110252eabba5SLogan Gunthorpe ret = event_ctl(stdev, &ctl);
11039f37ab04SLogan Gunthorpe if (ret < 0 && ret != -EOPNOTSUPP)
110452eabba5SLogan Gunthorpe return ret;
110552eabba5SLogan Gunthorpe }
110652eabba5SLogan Gunthorpe } else {
110752eabba5SLogan Gunthorpe ret = event_ctl(stdev, &ctl);
110852eabba5SLogan Gunthorpe if (ret < 0)
110952eabba5SLogan Gunthorpe return ret;
111052eabba5SLogan Gunthorpe }
111152eabba5SLogan Gunthorpe
111252eabba5SLogan Gunthorpe if (copy_to_user(uctl, &ctl, sizeof(ctl)))
111352eabba5SLogan Gunthorpe return -EFAULT;
111452eabba5SLogan Gunthorpe
111552eabba5SLogan Gunthorpe return 0;
111652eabba5SLogan Gunthorpe }
111752eabba5SLogan Gunthorpe
ioctl_pff_to_port(struct switchtec_dev * stdev,struct switchtec_ioctl_pff_port __user * up)111852eabba5SLogan Gunthorpe static int ioctl_pff_to_port(struct switchtec_dev *stdev,
11195f11723bSLogan Gunthorpe struct switchtec_ioctl_pff_port __user *up)
112052eabba5SLogan Gunthorpe {
112152eabba5SLogan Gunthorpe int i, part;
112252eabba5SLogan Gunthorpe u32 reg;
11235f11723bSLogan Gunthorpe struct part_cfg_regs __iomem *pcfg;
112452eabba5SLogan Gunthorpe struct switchtec_ioctl_pff_port p;
112552eabba5SLogan Gunthorpe
112652eabba5SLogan Gunthorpe if (copy_from_user(&p, up, sizeof(p)))
112752eabba5SLogan Gunthorpe return -EFAULT;
112852eabba5SLogan Gunthorpe
112952eabba5SLogan Gunthorpe p.port = -1;
113052eabba5SLogan Gunthorpe for (part = 0; part < stdev->partition_count; part++) {
113152eabba5SLogan Gunthorpe pcfg = &stdev->mmio_part_cfg_all[part];
113252eabba5SLogan Gunthorpe p.partition = part;
113352eabba5SLogan Gunthorpe
113452eabba5SLogan Gunthorpe reg = ioread32(&pcfg->usp_pff_inst_id);
113552eabba5SLogan Gunthorpe if (reg == p.pff) {
113652eabba5SLogan Gunthorpe p.port = 0;
113752eabba5SLogan Gunthorpe break;
113852eabba5SLogan Gunthorpe }
113952eabba5SLogan Gunthorpe
11401420ac21SKelvin Cao reg = ioread32(&pcfg->vep_pff_inst_id) & 0xFF;
114152eabba5SLogan Gunthorpe if (reg == p.pff) {
114252eabba5SLogan Gunthorpe p.port = SWITCHTEC_IOCTL_PFF_VEP;
114352eabba5SLogan Gunthorpe break;
114452eabba5SLogan Gunthorpe }
114552eabba5SLogan Gunthorpe
114652eabba5SLogan Gunthorpe for (i = 0; i < ARRAY_SIZE(pcfg->dsp_pff_inst_id); i++) {
114752eabba5SLogan Gunthorpe reg = ioread32(&pcfg->dsp_pff_inst_id[i]);
114852eabba5SLogan Gunthorpe if (reg != p.pff)
114952eabba5SLogan Gunthorpe continue;
115052eabba5SLogan Gunthorpe
115152eabba5SLogan Gunthorpe p.port = i + 1;
115252eabba5SLogan Gunthorpe break;
115352eabba5SLogan Gunthorpe }
115452eabba5SLogan Gunthorpe
115552eabba5SLogan Gunthorpe if (p.port != -1)
115652eabba5SLogan Gunthorpe break;
115752eabba5SLogan Gunthorpe }
115852eabba5SLogan Gunthorpe
115952eabba5SLogan Gunthorpe if (copy_to_user(up, &p, sizeof(p)))
116052eabba5SLogan Gunthorpe return -EFAULT;
116152eabba5SLogan Gunthorpe
116252eabba5SLogan Gunthorpe return 0;
116352eabba5SLogan Gunthorpe }
116452eabba5SLogan Gunthorpe
ioctl_port_to_pff(struct switchtec_dev * stdev,struct switchtec_ioctl_pff_port __user * up)116552eabba5SLogan Gunthorpe static int ioctl_port_to_pff(struct switchtec_dev *stdev,
11665f11723bSLogan Gunthorpe struct switchtec_ioctl_pff_port __user *up)
116752eabba5SLogan Gunthorpe {
116852eabba5SLogan Gunthorpe struct switchtec_ioctl_pff_port p;
11695f11723bSLogan Gunthorpe struct part_cfg_regs __iomem *pcfg;
117052eabba5SLogan Gunthorpe
117152eabba5SLogan Gunthorpe if (copy_from_user(&p, up, sizeof(p)))
117252eabba5SLogan Gunthorpe return -EFAULT;
117352eabba5SLogan Gunthorpe
117452eabba5SLogan Gunthorpe if (p.partition == SWITCHTEC_IOCTL_EVENT_LOCAL_PART_IDX)
117552eabba5SLogan Gunthorpe pcfg = stdev->mmio_part_cfg;
117652eabba5SLogan Gunthorpe else if (p.partition < stdev->partition_count)
117752eabba5SLogan Gunthorpe pcfg = &stdev->mmio_part_cfg_all[p.partition];
117852eabba5SLogan Gunthorpe else
117952eabba5SLogan Gunthorpe return -EINVAL;
118052eabba5SLogan Gunthorpe
118152eabba5SLogan Gunthorpe switch (p.port) {
118252eabba5SLogan Gunthorpe case 0:
118352eabba5SLogan Gunthorpe p.pff = ioread32(&pcfg->usp_pff_inst_id);
118452eabba5SLogan Gunthorpe break;
118552eabba5SLogan Gunthorpe case SWITCHTEC_IOCTL_PFF_VEP:
11861420ac21SKelvin Cao p.pff = ioread32(&pcfg->vep_pff_inst_id) & 0xFF;
118752eabba5SLogan Gunthorpe break;
118852eabba5SLogan Gunthorpe default:
118952eabba5SLogan Gunthorpe if (p.port > ARRAY_SIZE(pcfg->dsp_pff_inst_id))
119052eabba5SLogan Gunthorpe return -EINVAL;
119146feb6b4SGustavo A. R. Silva p.port = array_index_nospec(p.port,
119246feb6b4SGustavo A. R. Silva ARRAY_SIZE(pcfg->dsp_pff_inst_id) + 1);
119352eabba5SLogan Gunthorpe p.pff = ioread32(&pcfg->dsp_pff_inst_id[p.port - 1]);
119452eabba5SLogan Gunthorpe break;
119552eabba5SLogan Gunthorpe }
119652eabba5SLogan Gunthorpe
119752eabba5SLogan Gunthorpe if (copy_to_user(up, &p, sizeof(p)))
119852eabba5SLogan Gunthorpe return -EFAULT;
119952eabba5SLogan Gunthorpe
120052eabba5SLogan Gunthorpe return 0;
120152eabba5SLogan Gunthorpe }
120252eabba5SLogan Gunthorpe
switchtec_dev_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)120352eabba5SLogan Gunthorpe static long switchtec_dev_ioctl(struct file *filp, unsigned int cmd,
120452eabba5SLogan Gunthorpe unsigned long arg)
120552eabba5SLogan Gunthorpe {
120652eabba5SLogan Gunthorpe struct switchtec_user *stuser = filp->private_data;
120752eabba5SLogan Gunthorpe struct switchtec_dev *stdev = stuser->stdev;
120852eabba5SLogan Gunthorpe int rc;
120952eabba5SLogan Gunthorpe void __user *argp = (void __user *)arg;
121052eabba5SLogan Gunthorpe
121152eabba5SLogan Gunthorpe rc = lock_mutex_and_test_alive(stdev);
121252eabba5SLogan Gunthorpe if (rc)
121352eabba5SLogan Gunthorpe return rc;
121452eabba5SLogan Gunthorpe
121552eabba5SLogan Gunthorpe switch (cmd) {
121652eabba5SLogan Gunthorpe case SWITCHTEC_IOCTL_FLASH_INFO:
121752eabba5SLogan Gunthorpe rc = ioctl_flash_info(stdev, argp);
121852eabba5SLogan Gunthorpe break;
121952eabba5SLogan Gunthorpe case SWITCHTEC_IOCTL_FLASH_PART_INFO:
122052eabba5SLogan Gunthorpe rc = ioctl_flash_part_info(stdev, argp);
122152eabba5SLogan Gunthorpe break;
1222ba8a3982SWesley Sheng case SWITCHTEC_IOCTL_EVENT_SUMMARY_LEGACY:
1223ba8a3982SWesley Sheng rc = ioctl_event_summary(stdev, stuser, argp,
1224ba8a3982SWesley Sheng sizeof(struct switchtec_ioctl_event_summary_legacy));
122552eabba5SLogan Gunthorpe break;
122652eabba5SLogan Gunthorpe case SWITCHTEC_IOCTL_EVENT_CTL:
122752eabba5SLogan Gunthorpe rc = ioctl_event_ctl(stdev, argp);
122852eabba5SLogan Gunthorpe break;
122952eabba5SLogan Gunthorpe case SWITCHTEC_IOCTL_PFF_TO_PORT:
123052eabba5SLogan Gunthorpe rc = ioctl_pff_to_port(stdev, argp);
123152eabba5SLogan Gunthorpe break;
123252eabba5SLogan Gunthorpe case SWITCHTEC_IOCTL_PORT_TO_PFF:
123352eabba5SLogan Gunthorpe rc = ioctl_port_to_pff(stdev, argp);
123452eabba5SLogan Gunthorpe break;
1235ba8a3982SWesley Sheng case SWITCHTEC_IOCTL_EVENT_SUMMARY:
1236ba8a3982SWesley Sheng rc = ioctl_event_summary(stdev, stuser, argp,
1237ba8a3982SWesley Sheng sizeof(struct switchtec_ioctl_event_summary));
1238ba8a3982SWesley Sheng break;
123952eabba5SLogan Gunthorpe default:
124052eabba5SLogan Gunthorpe rc = -ENOTTY;
124152eabba5SLogan Gunthorpe break;
124252eabba5SLogan Gunthorpe }
124352eabba5SLogan Gunthorpe
124452eabba5SLogan Gunthorpe mutex_unlock(&stdev->mrpc_mutex);
124552eabba5SLogan Gunthorpe return rc;
124652eabba5SLogan Gunthorpe }
124752eabba5SLogan Gunthorpe
1248080b47deSLogan Gunthorpe static const struct file_operations switchtec_fops = {
1249080b47deSLogan Gunthorpe .owner = THIS_MODULE,
1250080b47deSLogan Gunthorpe .open = switchtec_dev_open,
1251080b47deSLogan Gunthorpe .release = switchtec_dev_release,
1252080b47deSLogan Gunthorpe .write = switchtec_dev_write,
1253080b47deSLogan Gunthorpe .read = switchtec_dev_read,
1254080b47deSLogan Gunthorpe .poll = switchtec_dev_poll,
125552eabba5SLogan Gunthorpe .unlocked_ioctl = switchtec_dev_ioctl,
12561832f2d8SArnd Bergmann .compat_ioctl = compat_ptr_ioctl,
1257080b47deSLogan Gunthorpe };
1258080b47deSLogan Gunthorpe
link_event_work(struct work_struct * work)125948c302dcSLogan Gunthorpe static void link_event_work(struct work_struct *work)
126048c302dcSLogan Gunthorpe {
126148c302dcSLogan Gunthorpe struct switchtec_dev *stdev;
126248c302dcSLogan Gunthorpe
126348c302dcSLogan Gunthorpe stdev = container_of(work, struct switchtec_dev, link_event_work);
126448c302dcSLogan Gunthorpe
126548c302dcSLogan Gunthorpe if (stdev->link_notifier)
126648c302dcSLogan Gunthorpe stdev->link_notifier(stdev);
126748c302dcSLogan Gunthorpe }
126848c302dcSLogan Gunthorpe
check_link_state_events(struct switchtec_dev * stdev)126948c302dcSLogan Gunthorpe static void check_link_state_events(struct switchtec_dev *stdev)
127048c302dcSLogan Gunthorpe {
127148c302dcSLogan Gunthorpe int idx;
127248c302dcSLogan Gunthorpe u32 reg;
127348c302dcSLogan Gunthorpe int count;
127448c302dcSLogan Gunthorpe int occurred = 0;
127548c302dcSLogan Gunthorpe
127648c302dcSLogan Gunthorpe for (idx = 0; idx < stdev->pff_csr_count; idx++) {
127748c302dcSLogan Gunthorpe reg = ioread32(&stdev->mmio_pff_csr[idx].link_state_hdr);
127848c302dcSLogan Gunthorpe dev_dbg(&stdev->dev, "link_state: %d->%08x\n", idx, reg);
127948c302dcSLogan Gunthorpe count = (reg >> 5) & 0xFF;
128048c302dcSLogan Gunthorpe
128148c302dcSLogan Gunthorpe if (count != stdev->link_event_count[idx]) {
128248c302dcSLogan Gunthorpe occurred = 1;
128348c302dcSLogan Gunthorpe stdev->link_event_count[idx] = count;
128448c302dcSLogan Gunthorpe }
128548c302dcSLogan Gunthorpe }
128648c302dcSLogan Gunthorpe
128748c302dcSLogan Gunthorpe if (occurred)
128848c302dcSLogan Gunthorpe schedule_work(&stdev->link_event_work);
128948c302dcSLogan Gunthorpe }
129048c302dcSLogan Gunthorpe
enable_link_state_events(struct switchtec_dev * stdev)129148c302dcSLogan Gunthorpe static void enable_link_state_events(struct switchtec_dev *stdev)
129248c302dcSLogan Gunthorpe {
129348c302dcSLogan Gunthorpe int idx;
129448c302dcSLogan Gunthorpe
129548c302dcSLogan Gunthorpe for (idx = 0; idx < stdev->pff_csr_count; idx++) {
129648c302dcSLogan Gunthorpe iowrite32(SWITCHTEC_EVENT_CLEAR |
129748c302dcSLogan Gunthorpe SWITCHTEC_EVENT_EN_IRQ,
129848c302dcSLogan Gunthorpe &stdev->mmio_pff_csr[idx].link_state_hdr);
129948c302dcSLogan Gunthorpe }
130048c302dcSLogan Gunthorpe }
130148c302dcSLogan Gunthorpe
enable_dma_mrpc(struct switchtec_dev * stdev)1302f7eb7b8aSWesley Sheng static void enable_dma_mrpc(struct switchtec_dev *stdev)
1303f7eb7b8aSWesley Sheng {
1304f7eb7b8aSWesley Sheng writeq(stdev->dma_mrpc_dma_addr, &stdev->mmio_mrpc->dma_addr);
1305f7eb7b8aSWesley Sheng flush_wc_buf(stdev);
1306f7eb7b8aSWesley Sheng iowrite32(SWITCHTEC_DMA_MRPC_EN, &stdev->mmio_mrpc->dma_en);
1307f7eb7b8aSWesley Sheng }
1308f7eb7b8aSWesley Sheng
stdev_release(struct device * dev)1309080b47deSLogan Gunthorpe static void stdev_release(struct device *dev)
1310080b47deSLogan Gunthorpe {
1311080b47deSLogan Gunthorpe struct switchtec_dev *stdev = to_stdev(dev);
1312080b47deSLogan Gunthorpe
1313080b47deSLogan Gunthorpe kfree(stdev);
1314080b47deSLogan Gunthorpe }
1315080b47deSLogan Gunthorpe
stdev_kill(struct switchtec_dev * stdev)1316080b47deSLogan Gunthorpe static void stdev_kill(struct switchtec_dev *stdev)
1317080b47deSLogan Gunthorpe {
1318080b47deSLogan Gunthorpe struct switchtec_user *stuser, *tmpuser;
1319080b47deSLogan Gunthorpe
1320080b47deSLogan Gunthorpe pci_clear_master(stdev->pdev);
1321080b47deSLogan Gunthorpe
1322080b47deSLogan Gunthorpe cancel_delayed_work_sync(&stdev->mrpc_timeout);
1323080b47deSLogan Gunthorpe
1324080b47deSLogan Gunthorpe /* Mark the hardware as unavailable and complete all completions */
1325080b47deSLogan Gunthorpe mutex_lock(&stdev->mrpc_mutex);
1326080b47deSLogan Gunthorpe stdev->alive = false;
1327080b47deSLogan Gunthorpe
1328080b47deSLogan Gunthorpe /* Wake up and kill any users waiting on an MRPC request */
1329080b47deSLogan Gunthorpe list_for_each_entry_safe(stuser, tmpuser, &stdev->mrpc_queue, list) {
1330deaa0a8aSSebastian Andrzej Siewior stuser->cmd_done = true;
1331deaa0a8aSSebastian Andrzej Siewior wake_up_interruptible(&stuser->cmd_comp);
1332080b47deSLogan Gunthorpe list_del_init(&stuser->list);
1333080b47deSLogan Gunthorpe stuser_put(stuser);
1334080b47deSLogan Gunthorpe }
1335080b47deSLogan Gunthorpe
1336080b47deSLogan Gunthorpe mutex_unlock(&stdev->mrpc_mutex);
1337080b47deSLogan Gunthorpe
1338080b47deSLogan Gunthorpe /* Wake up any users waiting on event_wq */
1339080b47deSLogan Gunthorpe wake_up_interruptible(&stdev->event_wq);
1340080b47deSLogan Gunthorpe }
1341080b47deSLogan Gunthorpe
stdev_create(struct pci_dev * pdev)1342080b47deSLogan Gunthorpe static struct switchtec_dev *stdev_create(struct pci_dev *pdev)
1343080b47deSLogan Gunthorpe {
1344080b47deSLogan Gunthorpe struct switchtec_dev *stdev;
1345080b47deSLogan Gunthorpe int minor;
1346080b47deSLogan Gunthorpe struct device *dev;
1347080b47deSLogan Gunthorpe struct cdev *cdev;
1348080b47deSLogan Gunthorpe int rc;
1349080b47deSLogan Gunthorpe
1350080b47deSLogan Gunthorpe stdev = kzalloc_node(sizeof(*stdev), GFP_KERNEL,
1351080b47deSLogan Gunthorpe dev_to_node(&pdev->dev));
1352080b47deSLogan Gunthorpe if (!stdev)
1353080b47deSLogan Gunthorpe return ERR_PTR(-ENOMEM);
1354080b47deSLogan Gunthorpe
1355080b47deSLogan Gunthorpe stdev->alive = true;
1356df254611SDaniel Stodden stdev->pdev = pci_dev_get(pdev);
1357080b47deSLogan Gunthorpe INIT_LIST_HEAD(&stdev->mrpc_queue);
1358080b47deSLogan Gunthorpe mutex_init(&stdev->mrpc_mutex);
1359080b47deSLogan Gunthorpe stdev->mrpc_busy = 0;
1360080b47deSLogan Gunthorpe INIT_WORK(&stdev->mrpc_work, mrpc_event_work);
1361080b47deSLogan Gunthorpe INIT_DELAYED_WORK(&stdev->mrpc_timeout, mrpc_timeout_work);
136248c302dcSLogan Gunthorpe INIT_WORK(&stdev->link_event_work, link_event_work);
1363080b47deSLogan Gunthorpe init_waitqueue_head(&stdev->event_wq);
1364080b47deSLogan Gunthorpe atomic_set(&stdev->event_cnt, 0);
1365080b47deSLogan Gunthorpe
1366080b47deSLogan Gunthorpe dev = &stdev->dev;
1367080b47deSLogan Gunthorpe device_initialize(dev);
1368*8a74e4eaSGreg Kroah-Hartman dev->class = &switchtec_class;
1369080b47deSLogan Gunthorpe dev->parent = &pdev->dev;
13705d8e1881SLogan Gunthorpe dev->groups = switchtec_device_groups;
1371080b47deSLogan Gunthorpe dev->release = stdev_release;
1372080b47deSLogan Gunthorpe
1373aa195350SKe Liu minor = ida_alloc(&switchtec_minor_ida, GFP_KERNEL);
1374080b47deSLogan Gunthorpe if (minor < 0) {
1375080b47deSLogan Gunthorpe rc = minor;
1376080b47deSLogan Gunthorpe goto err_put;
1377080b47deSLogan Gunthorpe }
1378080b47deSLogan Gunthorpe
1379080b47deSLogan Gunthorpe dev->devt = MKDEV(MAJOR(switchtec_devt), minor);
1380080b47deSLogan Gunthorpe dev_set_name(dev, "switchtec%d", minor);
1381080b47deSLogan Gunthorpe
1382080b47deSLogan Gunthorpe cdev = &stdev->cdev;
1383080b47deSLogan Gunthorpe cdev_init(cdev, &switchtec_fops);
1384080b47deSLogan Gunthorpe cdev->owner = THIS_MODULE;
1385080b47deSLogan Gunthorpe
1386080b47deSLogan Gunthorpe return stdev;
1387080b47deSLogan Gunthorpe
1388080b47deSLogan Gunthorpe err_put:
1389df254611SDaniel Stodden pci_dev_put(stdev->pdev);
1390080b47deSLogan Gunthorpe put_device(&stdev->dev);
1391080b47deSLogan Gunthorpe return ERR_PTR(rc);
1392080b47deSLogan Gunthorpe }
1393080b47deSLogan Gunthorpe
mask_event(struct switchtec_dev * stdev,int eid,int idx)139452eabba5SLogan Gunthorpe static int mask_event(struct switchtec_dev *stdev, int eid, int idx)
139552eabba5SLogan Gunthorpe {
139652eabba5SLogan Gunthorpe size_t off = event_regs[eid].offset;
139752eabba5SLogan Gunthorpe u32 __iomem *hdr_reg;
139852eabba5SLogan Gunthorpe u32 hdr;
139952eabba5SLogan Gunthorpe
140052eabba5SLogan Gunthorpe hdr_reg = event_regs[eid].map_reg(stdev, off, idx);
140152eabba5SLogan Gunthorpe hdr = ioread32(hdr_reg);
140252eabba5SLogan Gunthorpe
14039f37ab04SLogan Gunthorpe if (hdr & SWITCHTEC_EVENT_NOT_SUPP)
14049f37ab04SLogan Gunthorpe return 0;
14059f37ab04SLogan Gunthorpe
140652eabba5SLogan Gunthorpe if (!(hdr & SWITCHTEC_EVENT_OCCURRED && hdr & SWITCHTEC_EVENT_EN_IRQ))
140752eabba5SLogan Gunthorpe return 0;
140852eabba5SLogan Gunthorpe
140952eabba5SLogan Gunthorpe dev_dbg(&stdev->dev, "%s: %d %d %x\n", __func__, eid, idx, hdr);
141052eabba5SLogan Gunthorpe hdr &= ~(SWITCHTEC_EVENT_EN_IRQ | SWITCHTEC_EVENT_OCCURRED);
141152eabba5SLogan Gunthorpe iowrite32(hdr, hdr_reg);
141252eabba5SLogan Gunthorpe
141352eabba5SLogan Gunthorpe return 1;
141452eabba5SLogan Gunthorpe }
141552eabba5SLogan Gunthorpe
mask_all_events(struct switchtec_dev * stdev,int eid)141652eabba5SLogan Gunthorpe static int mask_all_events(struct switchtec_dev *stdev, int eid)
141752eabba5SLogan Gunthorpe {
141852eabba5SLogan Gunthorpe int idx;
141952eabba5SLogan Gunthorpe int count = 0;
142052eabba5SLogan Gunthorpe
142152eabba5SLogan Gunthorpe if (event_regs[eid].map_reg == part_ev_reg) {
142252eabba5SLogan Gunthorpe for (idx = 0; idx < stdev->partition_count; idx++)
142352eabba5SLogan Gunthorpe count += mask_event(stdev, eid, idx);
142452eabba5SLogan Gunthorpe } else if (event_regs[eid].map_reg == pff_ev_reg) {
142552eabba5SLogan Gunthorpe for (idx = 0; idx < stdev->pff_csr_count; idx++) {
142652eabba5SLogan Gunthorpe if (!stdev->pff_local[idx])
142752eabba5SLogan Gunthorpe continue;
142848c302dcSLogan Gunthorpe
142952eabba5SLogan Gunthorpe count += mask_event(stdev, eid, idx);
143052eabba5SLogan Gunthorpe }
143152eabba5SLogan Gunthorpe } else {
143252eabba5SLogan Gunthorpe count += mask_event(stdev, eid, 0);
143352eabba5SLogan Gunthorpe }
143452eabba5SLogan Gunthorpe
143552eabba5SLogan Gunthorpe return count;
143652eabba5SLogan Gunthorpe }
143752eabba5SLogan Gunthorpe
switchtec_event_isr(int irq,void * dev)1438080b47deSLogan Gunthorpe static irqreturn_t switchtec_event_isr(int irq, void *dev)
1439080b47deSLogan Gunthorpe {
1440080b47deSLogan Gunthorpe struct switchtec_dev *stdev = dev;
1441080b47deSLogan Gunthorpe u32 reg;
1442080b47deSLogan Gunthorpe irqreturn_t ret = IRQ_NONE;
144352eabba5SLogan Gunthorpe int eid, event_count = 0;
1444080b47deSLogan Gunthorpe
1445080b47deSLogan Gunthorpe reg = ioread32(&stdev->mmio_part_cfg->mrpc_comp_hdr);
1446080b47deSLogan Gunthorpe if (reg & SWITCHTEC_EVENT_OCCURRED) {
1447080b47deSLogan Gunthorpe dev_dbg(&stdev->dev, "%s: mrpc comp\n", __func__);
1448080b47deSLogan Gunthorpe ret = IRQ_HANDLED;
1449080b47deSLogan Gunthorpe schedule_work(&stdev->mrpc_work);
1450080b47deSLogan Gunthorpe iowrite32(reg, &stdev->mmio_part_cfg->mrpc_comp_hdr);
1451080b47deSLogan Gunthorpe }
1452080b47deSLogan Gunthorpe
145348c302dcSLogan Gunthorpe check_link_state_events(stdev);
145448c302dcSLogan Gunthorpe
14552085747dSWesley Sheng for (eid = 0; eid < SWITCHTEC_IOCTL_MAX_EVENTS; eid++) {
14562085747dSWesley Sheng if (eid == SWITCHTEC_IOCTL_EVENT_LINK_STATE ||
14572085747dSWesley Sheng eid == SWITCHTEC_IOCTL_EVENT_MRPC_COMP)
14582085747dSWesley Sheng continue;
14592085747dSWesley Sheng
146052eabba5SLogan Gunthorpe event_count += mask_all_events(stdev, eid);
14612085747dSWesley Sheng }
146252eabba5SLogan Gunthorpe
146352eabba5SLogan Gunthorpe if (event_count) {
146452eabba5SLogan Gunthorpe atomic_inc(&stdev->event_cnt);
146552eabba5SLogan Gunthorpe wake_up_interruptible(&stdev->event_wq);
146652eabba5SLogan Gunthorpe dev_dbg(&stdev->dev, "%s: %d events\n", __func__,
146752eabba5SLogan Gunthorpe event_count);
146852eabba5SLogan Gunthorpe return IRQ_HANDLED;
146952eabba5SLogan Gunthorpe }
147052eabba5SLogan Gunthorpe
1471080b47deSLogan Gunthorpe return ret;
1472080b47deSLogan Gunthorpe }
1473080b47deSLogan Gunthorpe
1474f7eb7b8aSWesley Sheng
switchtec_dma_mrpc_isr(int irq,void * dev)1475f7eb7b8aSWesley Sheng static irqreturn_t switchtec_dma_mrpc_isr(int irq, void *dev)
1476f7eb7b8aSWesley Sheng {
1477f7eb7b8aSWesley Sheng struct switchtec_dev *stdev = dev;
1478f7eb7b8aSWesley Sheng
1479f7eb7b8aSWesley Sheng iowrite32(SWITCHTEC_EVENT_CLEAR |
1480f7eb7b8aSWesley Sheng SWITCHTEC_EVENT_EN_IRQ,
1481f7eb7b8aSWesley Sheng &stdev->mmio_part_cfg->mrpc_comp_hdr);
1482f7eb7b8aSWesley Sheng schedule_work(&stdev->mrpc_work);
1483f7eb7b8aSWesley Sheng
14844e353ff4SBjorn Helgaas return IRQ_HANDLED;
1485f7eb7b8aSWesley Sheng }
1486f7eb7b8aSWesley Sheng
switchtec_init_isr(struct switchtec_dev * stdev)1487080b47deSLogan Gunthorpe static int switchtec_init_isr(struct switchtec_dev *stdev)
1488080b47deSLogan Gunthorpe {
1489080b47deSLogan Gunthorpe int nvecs;
1490080b47deSLogan Gunthorpe int event_irq;
1491f7eb7b8aSWesley Sheng int dma_mrpc_irq;
1492f7eb7b8aSWesley Sheng int rc;
1493080b47deSLogan Gunthorpe
1494fcdf8e95SLogan Gunthorpe if (nirqs < 4)
1495fcdf8e95SLogan Gunthorpe nirqs = 4;
1496fcdf8e95SLogan Gunthorpe
1497fcdf8e95SLogan Gunthorpe nvecs = pci_alloc_irq_vectors(stdev->pdev, 1, nirqs,
1498fcdf8e95SLogan Gunthorpe PCI_IRQ_MSIX | PCI_IRQ_MSI |
1499fcdf8e95SLogan Gunthorpe PCI_IRQ_VIRTUAL);
1500080b47deSLogan Gunthorpe if (nvecs < 0)
1501080b47deSLogan Gunthorpe return nvecs;
1502080b47deSLogan Gunthorpe
15039375646bSLogan Gunthorpe event_irq = ioread16(&stdev->mmio_part_cfg->vep_vector_number);
1504080b47deSLogan Gunthorpe if (event_irq < 0 || event_irq >= nvecs)
1505080b47deSLogan Gunthorpe return -EFAULT;
1506080b47deSLogan Gunthorpe
1507080b47deSLogan Gunthorpe event_irq = pci_irq_vector(stdev->pdev, event_irq);
1508080b47deSLogan Gunthorpe if (event_irq < 0)
1509080b47deSLogan Gunthorpe return event_irq;
1510080b47deSLogan Gunthorpe
1511f7eb7b8aSWesley Sheng rc = devm_request_irq(&stdev->pdev->dev, event_irq,
1512080b47deSLogan Gunthorpe switchtec_event_isr, 0,
1513080b47deSLogan Gunthorpe KBUILD_MODNAME, stdev);
1514f7eb7b8aSWesley Sheng
1515f7eb7b8aSWesley Sheng if (rc)
1516f7eb7b8aSWesley Sheng return rc;
1517f7eb7b8aSWesley Sheng
1518f7eb7b8aSWesley Sheng if (!stdev->dma_mrpc)
1519f7eb7b8aSWesley Sheng return rc;
1520f7eb7b8aSWesley Sheng
1521f7eb7b8aSWesley Sheng dma_mrpc_irq = ioread32(&stdev->mmio_mrpc->dma_vector);
1522f7eb7b8aSWesley Sheng if (dma_mrpc_irq < 0 || dma_mrpc_irq >= nvecs)
1523f7eb7b8aSWesley Sheng return -EFAULT;
1524f7eb7b8aSWesley Sheng
1525f7eb7b8aSWesley Sheng dma_mrpc_irq = pci_irq_vector(stdev->pdev, dma_mrpc_irq);
1526f7eb7b8aSWesley Sheng if (dma_mrpc_irq < 0)
1527f7eb7b8aSWesley Sheng return dma_mrpc_irq;
1528f7eb7b8aSWesley Sheng
1529f7eb7b8aSWesley Sheng rc = devm_request_irq(&stdev->pdev->dev, dma_mrpc_irq,
1530f7eb7b8aSWesley Sheng switchtec_dma_mrpc_isr, 0,
1531f7eb7b8aSWesley Sheng KBUILD_MODNAME, stdev);
1532f7eb7b8aSWesley Sheng
1533f7eb7b8aSWesley Sheng return rc;
1534080b47deSLogan Gunthorpe }
1535080b47deSLogan Gunthorpe
init_pff(struct switchtec_dev * stdev)1536080b47deSLogan Gunthorpe static void init_pff(struct switchtec_dev *stdev)
1537080b47deSLogan Gunthorpe {
1538080b47deSLogan Gunthorpe int i;
1539080b47deSLogan Gunthorpe u32 reg;
154042dae893SLogan Gunthorpe struct part_cfg_regs __iomem *pcfg = stdev->mmio_part_cfg;
1541080b47deSLogan Gunthorpe
1542080b47deSLogan Gunthorpe for (i = 0; i < SWITCHTEC_MAX_PFF_CSR; i++) {
1543080b47deSLogan Gunthorpe reg = ioread16(&stdev->mmio_pff_csr[i].vendor_id);
1544cfdfc14eSDoug Meyer if (reg != PCI_VENDOR_ID_MICROSEMI)
1545080b47deSLogan Gunthorpe break;
1546080b47deSLogan Gunthorpe }
1547080b47deSLogan Gunthorpe
1548080b47deSLogan Gunthorpe stdev->pff_csr_count = i;
1549080b47deSLogan Gunthorpe
1550080b47deSLogan Gunthorpe reg = ioread32(&pcfg->usp_pff_inst_id);
15517501a02aSWesley Sheng if (reg < stdev->pff_csr_count)
1552080b47deSLogan Gunthorpe stdev->pff_local[reg] = 1;
1553080b47deSLogan Gunthorpe
15541420ac21SKelvin Cao reg = ioread32(&pcfg->vep_pff_inst_id) & 0xFF;
15557501a02aSWesley Sheng if (reg < stdev->pff_csr_count)
1556080b47deSLogan Gunthorpe stdev->pff_local[reg] = 1;
1557080b47deSLogan Gunthorpe
1558080b47deSLogan Gunthorpe for (i = 0; i < ARRAY_SIZE(pcfg->dsp_pff_inst_id); i++) {
1559080b47deSLogan Gunthorpe reg = ioread32(&pcfg->dsp_pff_inst_id[i]);
15607501a02aSWesley Sheng if (reg < stdev->pff_csr_count)
1561080b47deSLogan Gunthorpe stdev->pff_local[reg] = 1;
1562080b47deSLogan Gunthorpe }
1563080b47deSLogan Gunthorpe }
1564080b47deSLogan Gunthorpe
switchtec_init_pci(struct switchtec_dev * stdev,struct pci_dev * pdev)1565080b47deSLogan Gunthorpe static int switchtec_init_pci(struct switchtec_dev *stdev,
1566080b47deSLogan Gunthorpe struct pci_dev *pdev)
1567080b47deSLogan Gunthorpe {
1568080b47deSLogan Gunthorpe int rc;
156952d8db8eSKelvin Cao void __iomem *map;
157052d8db8eSKelvin Cao unsigned long res_start, res_len;
1571993d208dSLogan Gunthorpe u32 __iomem *part_id;
1572080b47deSLogan Gunthorpe
1573080b47deSLogan Gunthorpe rc = pcim_enable_device(pdev);
1574080b47deSLogan Gunthorpe if (rc)
1575080b47deSLogan Gunthorpe return rc;
1576080b47deSLogan Gunthorpe
1577aa82130aSWesley Sheng rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1578aff614c6SBoris Glimcher if (rc)
1579aff614c6SBoris Glimcher return rc;
1580aff614c6SBoris Glimcher
1581080b47deSLogan Gunthorpe pci_set_master(pdev);
1582080b47deSLogan Gunthorpe
158352d8db8eSKelvin Cao res_start = pci_resource_start(pdev, 0);
158452d8db8eSKelvin Cao res_len = pci_resource_len(pdev, 0);
158552d8db8eSKelvin Cao
158652d8db8eSKelvin Cao if (!devm_request_mem_region(&pdev->dev, res_start,
158752d8db8eSKelvin Cao res_len, KBUILD_MODNAME))
158852d8db8eSKelvin Cao return -EBUSY;
158952d8db8eSKelvin Cao
159052d8db8eSKelvin Cao stdev->mmio_mrpc = devm_ioremap_wc(&pdev->dev, res_start,
159152d8db8eSKelvin Cao SWITCHTEC_GAS_TOP_CFG_OFFSET);
159252d8db8eSKelvin Cao if (!stdev->mmio_mrpc)
159352d8db8eSKelvin Cao return -ENOMEM;
159452d8db8eSKelvin Cao
159552d8db8eSKelvin Cao map = devm_ioremap(&pdev->dev,
159652d8db8eSKelvin Cao res_start + SWITCHTEC_GAS_TOP_CFG_OFFSET,
159752d8db8eSKelvin Cao res_len - SWITCHTEC_GAS_TOP_CFG_OFFSET);
159852d8db8eSKelvin Cao if (!map)
159952d8db8eSKelvin Cao return -ENOMEM;
160052d8db8eSKelvin Cao
160152d8db8eSKelvin Cao stdev->mmio = map - SWITCHTEC_GAS_TOP_CFG_OFFSET;
1602080b47deSLogan Gunthorpe stdev->mmio_sw_event = stdev->mmio + SWITCHTEC_GAS_SW_EVENT_OFFSET;
1603080b47deSLogan Gunthorpe stdev->mmio_sys_info = stdev->mmio + SWITCHTEC_GAS_SYS_INFO_OFFSET;
1604080b47deSLogan Gunthorpe stdev->mmio_flash_info = stdev->mmio + SWITCHTEC_GAS_FLASH_INFO_OFFSET;
1605080b47deSLogan Gunthorpe stdev->mmio_ntb = stdev->mmio + SWITCHTEC_GAS_NTB_OFFSET;
1606993d208dSLogan Gunthorpe
1607993d208dSLogan Gunthorpe if (stdev->gen == SWITCHTEC_GEN3)
1608993d208dSLogan Gunthorpe part_id = &stdev->mmio_sys_info->gen3.partition_id;
16090fb53e64SKelvin Cao else if (stdev->gen >= SWITCHTEC_GEN4)
1610a3321ca3SLogan Gunthorpe part_id = &stdev->mmio_sys_info->gen4.partition_id;
1611993d208dSLogan Gunthorpe else
161267116444SKelvin Cao return -EOPNOTSUPP;
1613993d208dSLogan Gunthorpe
1614993d208dSLogan Gunthorpe stdev->partition = ioread8(part_id);
1615080b47deSLogan Gunthorpe stdev->partition_count = ioread8(&stdev->mmio_ntb->partition_count);
1616080b47deSLogan Gunthorpe stdev->mmio_part_cfg_all = stdev->mmio + SWITCHTEC_GAS_PART_CFG_OFFSET;
1617080b47deSLogan Gunthorpe stdev->mmio_part_cfg = &stdev->mmio_part_cfg_all[stdev->partition];
1618080b47deSLogan Gunthorpe stdev->mmio_pff_csr = stdev->mmio + SWITCHTEC_GAS_PFF_CSR_OFFSET;
1619080b47deSLogan Gunthorpe
16209871e9bbSLogan Gunthorpe if (stdev->partition_count < 1)
16219871e9bbSLogan Gunthorpe stdev->partition_count = 1;
16229871e9bbSLogan Gunthorpe
1623080b47deSLogan Gunthorpe init_pff(stdev);
1624080b47deSLogan Gunthorpe
1625080b47deSLogan Gunthorpe pci_set_drvdata(pdev, stdev);
1626080b47deSLogan Gunthorpe
1627f7eb7b8aSWesley Sheng if (!use_dma_mrpc)
1628f7eb7b8aSWesley Sheng return 0;
1629f7eb7b8aSWesley Sheng
1630f7eb7b8aSWesley Sheng if (ioread32(&stdev->mmio_mrpc->dma_ver) == 0)
1631f7eb7b8aSWesley Sheng return 0;
1632f7eb7b8aSWesley Sheng
1633750afb08SLuis Chamberlain stdev->dma_mrpc = dma_alloc_coherent(&stdev->pdev->dev,
1634f7eb7b8aSWesley Sheng sizeof(*stdev->dma_mrpc),
1635f7eb7b8aSWesley Sheng &stdev->dma_mrpc_dma_addr,
1636f7eb7b8aSWesley Sheng GFP_KERNEL);
1637f7eb7b8aSWesley Sheng if (stdev->dma_mrpc == NULL)
1638f7eb7b8aSWesley Sheng return -ENOMEM;
1639f7eb7b8aSWesley Sheng
1640080b47deSLogan Gunthorpe return 0;
1641080b47deSLogan Gunthorpe }
1642080b47deSLogan Gunthorpe
switchtec_exit_pci(struct switchtec_dev * stdev)1643df254611SDaniel Stodden static void switchtec_exit_pci(struct switchtec_dev *stdev)
1644df254611SDaniel Stodden {
1645df254611SDaniel Stodden if (stdev->dma_mrpc) {
1646df254611SDaniel Stodden iowrite32(0, &stdev->mmio_mrpc->dma_en);
1647df254611SDaniel Stodden flush_wc_buf(stdev);
1648df254611SDaniel Stodden writeq(0, &stdev->mmio_mrpc->dma_addr);
1649df254611SDaniel Stodden dma_free_coherent(&stdev->pdev->dev, sizeof(*stdev->dma_mrpc),
1650df254611SDaniel Stodden stdev->dma_mrpc, stdev->dma_mrpc_dma_addr);
1651df254611SDaniel Stodden stdev->dma_mrpc = NULL;
1652df254611SDaniel Stodden }
1653df254611SDaniel Stodden }
1654df254611SDaniel Stodden
switchtec_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)1655080b47deSLogan Gunthorpe static int switchtec_pci_probe(struct pci_dev *pdev,
1656080b47deSLogan Gunthorpe const struct pci_device_id *id)
1657080b47deSLogan Gunthorpe {
1658080b47deSLogan Gunthorpe struct switchtec_dev *stdev;
1659080b47deSLogan Gunthorpe int rc;
1660080b47deSLogan Gunthorpe
1661cfdfc14eSDoug Meyer if (pdev->class == (PCI_CLASS_BRIDGE_OTHER << 8))
166233dea5aaSLogan Gunthorpe request_module_nowait("ntb_hw_switchtec");
166333dea5aaSLogan Gunthorpe
1664080b47deSLogan Gunthorpe stdev = stdev_create(pdev);
1665080b47deSLogan Gunthorpe if (IS_ERR(stdev))
1666080b47deSLogan Gunthorpe return PTR_ERR(stdev);
1667080b47deSLogan Gunthorpe
1668b13313a0SLogan Gunthorpe stdev->gen = id->driver_data;
1669b13313a0SLogan Gunthorpe
1670080b47deSLogan Gunthorpe rc = switchtec_init_pci(stdev, pdev);
1671080b47deSLogan Gunthorpe if (rc)
1672080b47deSLogan Gunthorpe goto err_put;
1673080b47deSLogan Gunthorpe
1674080b47deSLogan Gunthorpe rc = switchtec_init_isr(stdev);
1675080b47deSLogan Gunthorpe if (rc) {
1676080b47deSLogan Gunthorpe dev_err(&stdev->dev, "failed to init isr.\n");
1677dec529b0SChristophe JAILLET goto err_exit_pci;
1678080b47deSLogan Gunthorpe }
1679080b47deSLogan Gunthorpe
1680080b47deSLogan Gunthorpe iowrite32(SWITCHTEC_EVENT_CLEAR |
1681080b47deSLogan Gunthorpe SWITCHTEC_EVENT_EN_IRQ,
1682080b47deSLogan Gunthorpe &stdev->mmio_part_cfg->mrpc_comp_hdr);
168348c302dcSLogan Gunthorpe enable_link_state_events(stdev);
1684080b47deSLogan Gunthorpe
1685f7eb7b8aSWesley Sheng if (stdev->dma_mrpc)
1686f7eb7b8aSWesley Sheng enable_dma_mrpc(stdev);
1687f7eb7b8aSWesley Sheng
1688e40cf640SLogan Gunthorpe rc = cdev_device_add(&stdev->cdev, &stdev->dev);
1689080b47deSLogan Gunthorpe if (rc)
1690080b47deSLogan Gunthorpe goto err_devadd;
1691080b47deSLogan Gunthorpe
1692080b47deSLogan Gunthorpe dev_info(&stdev->dev, "Management device registered.\n");
1693080b47deSLogan Gunthorpe
1694080b47deSLogan Gunthorpe return 0;
1695080b47deSLogan Gunthorpe
1696080b47deSLogan Gunthorpe err_devadd:
1697080b47deSLogan Gunthorpe stdev_kill(stdev);
1698dec529b0SChristophe JAILLET err_exit_pci:
1699dec529b0SChristophe JAILLET switchtec_exit_pci(stdev);
1700080b47deSLogan Gunthorpe err_put:
1701aa195350SKe Liu ida_free(&switchtec_minor_ida, MINOR(stdev->dev.devt));
1702080b47deSLogan Gunthorpe put_device(&stdev->dev);
1703080b47deSLogan Gunthorpe return rc;
1704080b47deSLogan Gunthorpe }
1705080b47deSLogan Gunthorpe
switchtec_pci_remove(struct pci_dev * pdev)1706080b47deSLogan Gunthorpe static void switchtec_pci_remove(struct pci_dev *pdev)
1707080b47deSLogan Gunthorpe {
1708080b47deSLogan Gunthorpe struct switchtec_dev *stdev = pci_get_drvdata(pdev);
1709080b47deSLogan Gunthorpe
1710080b47deSLogan Gunthorpe pci_set_drvdata(pdev, NULL);
1711080b47deSLogan Gunthorpe
1712e40cf640SLogan Gunthorpe cdev_device_del(&stdev->cdev, &stdev->dev);
1713aa195350SKe Liu ida_free(&switchtec_minor_ida, MINOR(stdev->dev.devt));
1714080b47deSLogan Gunthorpe dev_info(&stdev->dev, "unregistered.\n");
1715080b47deSLogan Gunthorpe stdev_kill(stdev);
1716df254611SDaniel Stodden switchtec_exit_pci(stdev);
1717df254611SDaniel Stodden pci_dev_put(stdev->pdev);
1718df254611SDaniel Stodden stdev->pdev = NULL;
1719080b47deSLogan Gunthorpe put_device(&stdev->dev);
1720080b47deSLogan Gunthorpe }
1721080b47deSLogan Gunthorpe
1722b13313a0SLogan Gunthorpe #define SWITCHTEC_PCI_DEVICE(device_id, gen) \
1723080b47deSLogan Gunthorpe { \
1724cfdfc14eSDoug Meyer .vendor = PCI_VENDOR_ID_MICROSEMI, \
1725080b47deSLogan Gunthorpe .device = device_id, \
1726080b47deSLogan Gunthorpe .subvendor = PCI_ANY_ID, \
1727080b47deSLogan Gunthorpe .subdevice = PCI_ANY_ID, \
1728cfdfc14eSDoug Meyer .class = (PCI_CLASS_MEMORY_OTHER << 8), \
1729080b47deSLogan Gunthorpe .class_mask = 0xFFFFFFFF, \
1730b13313a0SLogan Gunthorpe .driver_data = gen, \
1731080b47deSLogan Gunthorpe }, \
1732080b47deSLogan Gunthorpe { \
1733cfdfc14eSDoug Meyer .vendor = PCI_VENDOR_ID_MICROSEMI, \
1734080b47deSLogan Gunthorpe .device = device_id, \
1735080b47deSLogan Gunthorpe .subvendor = PCI_ANY_ID, \
1736080b47deSLogan Gunthorpe .subdevice = PCI_ANY_ID, \
1737cfdfc14eSDoug Meyer .class = (PCI_CLASS_BRIDGE_OTHER << 8), \
1738080b47deSLogan Gunthorpe .class_mask = 0xFFFFFFFF, \
1739b13313a0SLogan Gunthorpe .driver_data = gen, \
1740080b47deSLogan Gunthorpe }
1741080b47deSLogan Gunthorpe
1742080b47deSLogan Gunthorpe static const struct pci_device_id switchtec_pci_tbl[] = {
1743846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8531, SWITCHTEC_GEN3), /* PFX 24xG3 */
1744846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8532, SWITCHTEC_GEN3), /* PFX 32xG3 */
1745846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8533, SWITCHTEC_GEN3), /* PFX 48xG3 */
1746846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8534, SWITCHTEC_GEN3), /* PFX 64xG3 */
1747846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8535, SWITCHTEC_GEN3), /* PFX 80xG3 */
1748846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8536, SWITCHTEC_GEN3), /* PFX 96xG3 */
1749846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8541, SWITCHTEC_GEN3), /* PSX 24xG3 */
1750846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8542, SWITCHTEC_GEN3), /* PSX 32xG3 */
1751846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8543, SWITCHTEC_GEN3), /* PSX 48xG3 */
1752846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8544, SWITCHTEC_GEN3), /* PSX 64xG3 */
1753846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8545, SWITCHTEC_GEN3), /* PSX 80xG3 */
1754846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8546, SWITCHTEC_GEN3), /* PSX 96xG3 */
1755846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8551, SWITCHTEC_GEN3), /* PAX 24XG3 */
1756846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8552, SWITCHTEC_GEN3), /* PAX 32XG3 */
1757846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8553, SWITCHTEC_GEN3), /* PAX 48XG3 */
1758846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8554, SWITCHTEC_GEN3), /* PAX 64XG3 */
1759846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8555, SWITCHTEC_GEN3), /* PAX 80XG3 */
1760846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8556, SWITCHTEC_GEN3), /* PAX 96XG3 */
1761846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8561, SWITCHTEC_GEN3), /* PFXL 24XG3 */
1762846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8562, SWITCHTEC_GEN3), /* PFXL 32XG3 */
1763846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8563, SWITCHTEC_GEN3), /* PFXL 48XG3 */
1764846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8564, SWITCHTEC_GEN3), /* PFXL 64XG3 */
1765846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8565, SWITCHTEC_GEN3), /* PFXL 80XG3 */
1766846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8566, SWITCHTEC_GEN3), /* PFXL 96XG3 */
1767846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8571, SWITCHTEC_GEN3), /* PFXI 24XG3 */
1768846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8572, SWITCHTEC_GEN3), /* PFXI 32XG3 */
1769846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8573, SWITCHTEC_GEN3), /* PFXI 48XG3 */
1770846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8574, SWITCHTEC_GEN3), /* PFXI 64XG3 */
1771846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8575, SWITCHTEC_GEN3), /* PFXI 80XG3 */
1772846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x8576, SWITCHTEC_GEN3), /* PFXI 96XG3 */
1773846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4000, SWITCHTEC_GEN4), /* PFX 100XG4 */
1774846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4084, SWITCHTEC_GEN4), /* PFX 84XG4 */
1775846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4068, SWITCHTEC_GEN4), /* PFX 68XG4 */
1776846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4052, SWITCHTEC_GEN4), /* PFX 52XG4 */
1777846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4036, SWITCHTEC_GEN4), /* PFX 36XG4 */
1778846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4028, SWITCHTEC_GEN4), /* PFX 28XG4 */
1779846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4100, SWITCHTEC_GEN4), /* PSX 100XG4 */
1780846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4184, SWITCHTEC_GEN4), /* PSX 84XG4 */
1781846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4168, SWITCHTEC_GEN4), /* PSX 68XG4 */
1782846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4152, SWITCHTEC_GEN4), /* PSX 52XG4 */
1783846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4136, SWITCHTEC_GEN4), /* PSX 36XG4 */
1784846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4128, SWITCHTEC_GEN4), /* PSX 28XG4 */
1785846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4200, SWITCHTEC_GEN4), /* PAX 100XG4 */
1786846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4284, SWITCHTEC_GEN4), /* PAX 84XG4 */
1787846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4268, SWITCHTEC_GEN4), /* PAX 68XG4 */
1788846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4252, SWITCHTEC_GEN4), /* PAX 52XG4 */
1789846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4236, SWITCHTEC_GEN4), /* PAX 36XG4 */
1790846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4228, SWITCHTEC_GEN4), /* PAX 28XG4 */
1791846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4352, SWITCHTEC_GEN4), /* PFXA 52XG4 */
1792846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4336, SWITCHTEC_GEN4), /* PFXA 36XG4 */
1793846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4328, SWITCHTEC_GEN4), /* PFXA 28XG4 */
1794846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4452, SWITCHTEC_GEN4), /* PSXA 52XG4 */
1795846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4436, SWITCHTEC_GEN4), /* PSXA 36XG4 */
1796846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4428, SWITCHTEC_GEN4), /* PSXA 28XG4 */
1797846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4552, SWITCHTEC_GEN4), /* PAXA 52XG4 */
1798846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4536, SWITCHTEC_GEN4), /* PAXA 36XG4 */
1799846691f5SKelvin Cao SWITCHTEC_PCI_DEVICE(0x4528, SWITCHTEC_GEN4), /* PAXA 28XG4 */
18000fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5000, SWITCHTEC_GEN5), /* PFX 100XG5 */
18010fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5084, SWITCHTEC_GEN5), /* PFX 84XG5 */
18020fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5068, SWITCHTEC_GEN5), /* PFX 68XG5 */
18030fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5052, SWITCHTEC_GEN5), /* PFX 52XG5 */
18040fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5036, SWITCHTEC_GEN5), /* PFX 36XG5 */
18050fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5028, SWITCHTEC_GEN5), /* PFX 28XG5 */
18060fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5100, SWITCHTEC_GEN5), /* PSX 100XG5 */
18070fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5184, SWITCHTEC_GEN5), /* PSX 84XG5 */
18080fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5168, SWITCHTEC_GEN5), /* PSX 68XG5 */
18090fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5152, SWITCHTEC_GEN5), /* PSX 52XG5 */
18100fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5136, SWITCHTEC_GEN5), /* PSX 36XG5 */
18110fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5128, SWITCHTEC_GEN5), /* PSX 28XG5 */
18120fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5200, SWITCHTEC_GEN5), /* PAX 100XG5 */
18130fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5284, SWITCHTEC_GEN5), /* PAX 84XG5 */
18140fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5268, SWITCHTEC_GEN5), /* PAX 68XG5 */
18150fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5252, SWITCHTEC_GEN5), /* PAX 52XG5 */
18160fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5236, SWITCHTEC_GEN5), /* PAX 36XG5 */
18170fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5228, SWITCHTEC_GEN5), /* PAX 28XG5 */
18180fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5300, SWITCHTEC_GEN5), /* PFXA 100XG5 */
18190fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5384, SWITCHTEC_GEN5), /* PFXA 84XG5 */
18200fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5368, SWITCHTEC_GEN5), /* PFXA 68XG5 */
18210fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5352, SWITCHTEC_GEN5), /* PFXA 52XG5 */
18220fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5336, SWITCHTEC_GEN5), /* PFXA 36XG5 */
18230fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5328, SWITCHTEC_GEN5), /* PFXA 28XG5 */
18240fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5400, SWITCHTEC_GEN5), /* PSXA 100XG5 */
18250fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5484, SWITCHTEC_GEN5), /* PSXA 84XG5 */
18260fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5468, SWITCHTEC_GEN5), /* PSXA 68XG5 */
18270fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5452, SWITCHTEC_GEN5), /* PSXA 52XG5 */
18280fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5436, SWITCHTEC_GEN5), /* PSXA 36XG5 */
18290fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5428, SWITCHTEC_GEN5), /* PSXA 28XG5 */
18300fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5500, SWITCHTEC_GEN5), /* PAXA 100XG5 */
18310fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5584, SWITCHTEC_GEN5), /* PAXA 84XG5 */
18320fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5568, SWITCHTEC_GEN5), /* PAXA 68XG5 */
18330fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5552, SWITCHTEC_GEN5), /* PAXA 52XG5 */
18340fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5536, SWITCHTEC_GEN5), /* PAXA 36XG5 */
18350fb53e64SKelvin Cao SWITCHTEC_PCI_DEVICE(0x5528, SWITCHTEC_GEN5), /* PAXA 28XG5 */
1836080b47deSLogan Gunthorpe {0}
1837080b47deSLogan Gunthorpe };
1838080b47deSLogan Gunthorpe MODULE_DEVICE_TABLE(pci, switchtec_pci_tbl);
1839080b47deSLogan Gunthorpe
1840080b47deSLogan Gunthorpe static struct pci_driver switchtec_pci_driver = {
1841080b47deSLogan Gunthorpe .name = KBUILD_MODNAME,
1842080b47deSLogan Gunthorpe .id_table = switchtec_pci_tbl,
1843080b47deSLogan Gunthorpe .probe = switchtec_pci_probe,
1844080b47deSLogan Gunthorpe .remove = switchtec_pci_remove,
1845080b47deSLogan Gunthorpe };
1846080b47deSLogan Gunthorpe
switchtec_init(void)1847080b47deSLogan Gunthorpe static int __init switchtec_init(void)
1848080b47deSLogan Gunthorpe {
1849080b47deSLogan Gunthorpe int rc;
1850080b47deSLogan Gunthorpe
1851080b47deSLogan Gunthorpe rc = alloc_chrdev_region(&switchtec_devt, 0, max_devices,
1852080b47deSLogan Gunthorpe "switchtec");
1853080b47deSLogan Gunthorpe if (rc)
1854080b47deSLogan Gunthorpe return rc;
1855080b47deSLogan Gunthorpe
1856*8a74e4eaSGreg Kroah-Hartman rc = class_register(&switchtec_class);
1857*8a74e4eaSGreg Kroah-Hartman if (rc)
1858080b47deSLogan Gunthorpe goto err_create_class;
1859080b47deSLogan Gunthorpe
1860080b47deSLogan Gunthorpe rc = pci_register_driver(&switchtec_pci_driver);
1861080b47deSLogan Gunthorpe if (rc)
1862080b47deSLogan Gunthorpe goto err_pci_register;
1863080b47deSLogan Gunthorpe
1864080b47deSLogan Gunthorpe pr_info(KBUILD_MODNAME ": loaded.\n");
1865080b47deSLogan Gunthorpe
1866080b47deSLogan Gunthorpe return 0;
1867080b47deSLogan Gunthorpe
1868080b47deSLogan Gunthorpe err_pci_register:
1869*8a74e4eaSGreg Kroah-Hartman class_unregister(&switchtec_class);
1870080b47deSLogan Gunthorpe
1871080b47deSLogan Gunthorpe err_create_class:
1872080b47deSLogan Gunthorpe unregister_chrdev_region(switchtec_devt, max_devices);
1873080b47deSLogan Gunthorpe
1874080b47deSLogan Gunthorpe return rc;
1875080b47deSLogan Gunthorpe }
1876080b47deSLogan Gunthorpe module_init(switchtec_init);
1877080b47deSLogan Gunthorpe
switchtec_exit(void)1878080b47deSLogan Gunthorpe static void __exit switchtec_exit(void)
1879080b47deSLogan Gunthorpe {
1880080b47deSLogan Gunthorpe pci_unregister_driver(&switchtec_pci_driver);
1881*8a74e4eaSGreg Kroah-Hartman class_unregister(&switchtec_class);
1882080b47deSLogan Gunthorpe unregister_chrdev_region(switchtec_devt, max_devices);
1883080b47deSLogan Gunthorpe ida_destroy(&switchtec_minor_ida);
1884080b47deSLogan Gunthorpe
1885080b47deSLogan Gunthorpe pr_info(KBUILD_MODNAME ": unloaded.\n");
1886080b47deSLogan Gunthorpe }
1887080b47deSLogan Gunthorpe module_exit(switchtec_exit);
1888