1 // SPDX-License-Identifier: GPL-2.0
3 * Microsemi Switchtec(tm) PCIe Management Driver
4 * Copyright (c) 2017, Microsemi Corporation
7 #include <linux/switchtec.h>
8 #include <linux/switchtec_ioctl.h>
10 #include <linux/interrupt.h>
11 #include <linux/module.h>
13 #include <linux/uaccess.h>
14 #include <linux/poll.h>
15 #include <linux/wait.h>
16 #include <linux/io-64-nonatomic-lo-hi.h>
17 #include <linux/nospec.h>
19 MODULE_DESCRIPTION("Microsemi Switchtec(tm) PCIe Management Driver");
20 MODULE_VERSION("0.1");
21 MODULE_LICENSE("GPL");
22 MODULE_AUTHOR("Microsemi Corporation");
24 static int max_devices = 16;
25 module_param(max_devices, int, 0644);
26 MODULE_PARM_DESC(max_devices, "max number of switchtec device instances");
28 static dev_t switchtec_devt;
29 static DEFINE_IDA(switchtec_minor_ida);
31 struct class *switchtec_class;
32 EXPORT_SYMBOL_GPL(switchtec_class);
41 struct switchtec_user {
42 struct switchtec_dev *stdev;
44 enum mrpc_state state;
46 struct completion comp;
48 struct list_head list;
55 unsigned char data[SWITCHTEC_MRPC_PAYLOAD_SIZE];
59 static struct switchtec_user *stuser_create(struct switchtec_dev *stdev)
61 struct switchtec_user *stuser;
63 stuser = kzalloc(sizeof(*stuser), GFP_KERNEL);
65 return ERR_PTR(-ENOMEM);
67 get_device(&stdev->dev);
68 stuser->stdev = stdev;
69 kref_init(&stuser->kref);
70 INIT_LIST_HEAD(&stuser->list);
71 init_completion(&stuser->comp);
72 stuser->event_cnt = atomic_read(&stdev->event_cnt);
74 dev_dbg(&stdev->dev, "%s: %p\n", __func__, stuser);
79 static void stuser_free(struct kref *kref)
81 struct switchtec_user *stuser;
83 stuser = container_of(kref, struct switchtec_user, kref);
85 dev_dbg(&stuser->stdev->dev, "%s: %p\n", __func__, stuser);
87 put_device(&stuser->stdev->dev);
91 static void stuser_put(struct switchtec_user *stuser)
93 kref_put(&stuser->kref, stuser_free);
96 static void stuser_set_state(struct switchtec_user *stuser,
97 enum mrpc_state state)
99 /* requires the mrpc_mutex to already be held when called */
101 const char * const state_names[] = {
102 [MRPC_IDLE] = "IDLE",
103 [MRPC_QUEUED] = "QUEUED",
104 [MRPC_RUNNING] = "RUNNING",
105 [MRPC_DONE] = "DONE",
108 stuser->state = state;
110 dev_dbg(&stuser->stdev->dev, "stuser state %p -> %s",
111 stuser, state_names[state]);
114 static void mrpc_complete_cmd(struct switchtec_dev *stdev);
116 static void mrpc_cmd_submit(struct switchtec_dev *stdev)
118 /* requires the mrpc_mutex to already be held when called */
120 struct switchtec_user *stuser;
122 if (stdev->mrpc_busy)
125 if (list_empty(&stdev->mrpc_queue))
128 stuser = list_entry(stdev->mrpc_queue.next, struct switchtec_user,
131 stuser_set_state(stuser, MRPC_RUNNING);
132 stdev->mrpc_busy = 1;
133 memcpy_toio(&stdev->mmio_mrpc->input_data,
134 stuser->data, stuser->data_len);
135 iowrite32(stuser->cmd, &stdev->mmio_mrpc->cmd);
137 schedule_delayed_work(&stdev->mrpc_timeout,
138 msecs_to_jiffies(500));
141 static int mrpc_queue_cmd(struct switchtec_user *stuser)
143 /* requires the mrpc_mutex to already be held when called */
145 struct switchtec_dev *stdev = stuser->stdev;
147 kref_get(&stuser->kref);
148 stuser->read_len = sizeof(stuser->data);
149 stuser_set_state(stuser, MRPC_QUEUED);
150 reinit_completion(&stuser->comp);
151 list_add_tail(&stuser->list, &stdev->mrpc_queue);
153 mrpc_cmd_submit(stdev);
158 static void mrpc_complete_cmd(struct switchtec_dev *stdev)
160 /* requires the mrpc_mutex to already be held when called */
161 struct switchtec_user *stuser;
163 if (list_empty(&stdev->mrpc_queue))
166 stuser = list_entry(stdev->mrpc_queue.next, struct switchtec_user,
169 stuser->status = ioread32(&stdev->mmio_mrpc->status);
170 if (stuser->status == SWITCHTEC_MRPC_STATUS_INPROGRESS)
173 stuser_set_state(stuser, MRPC_DONE);
174 stuser->return_code = 0;
176 if (stuser->status != SWITCHTEC_MRPC_STATUS_DONE)
179 stuser->return_code = ioread32(&stdev->mmio_mrpc->ret_value);
180 if (stuser->return_code != 0)
183 memcpy_fromio(stuser->data, &stdev->mmio_mrpc->output_data,
187 complete_all(&stuser->comp);
188 list_del_init(&stuser->list);
190 stdev->mrpc_busy = 0;
192 mrpc_cmd_submit(stdev);
195 static void mrpc_event_work(struct work_struct *work)
197 struct switchtec_dev *stdev;
199 stdev = container_of(work, struct switchtec_dev, mrpc_work);
201 dev_dbg(&stdev->dev, "%s\n", __func__);
203 mutex_lock(&stdev->mrpc_mutex);
204 cancel_delayed_work(&stdev->mrpc_timeout);
205 mrpc_complete_cmd(stdev);
206 mutex_unlock(&stdev->mrpc_mutex);
209 static void mrpc_timeout_work(struct work_struct *work)
211 struct switchtec_dev *stdev;
214 stdev = container_of(work, struct switchtec_dev, mrpc_timeout.work);
216 dev_dbg(&stdev->dev, "%s\n", __func__);
218 mutex_lock(&stdev->mrpc_mutex);
220 status = ioread32(&stdev->mmio_mrpc->status);
221 if (status == SWITCHTEC_MRPC_STATUS_INPROGRESS) {
222 schedule_delayed_work(&stdev->mrpc_timeout,
223 msecs_to_jiffies(500));
227 mrpc_complete_cmd(stdev);
230 mutex_unlock(&stdev->mrpc_mutex);
233 static ssize_t device_version_show(struct device *dev,
234 struct device_attribute *attr, char *buf)
236 struct switchtec_dev *stdev = to_stdev(dev);
239 ver = ioread32(&stdev->mmio_sys_info->device_version);
241 return sprintf(buf, "%x\n", ver);
243 static DEVICE_ATTR_RO(device_version);
245 static ssize_t fw_version_show(struct device *dev,
246 struct device_attribute *attr, char *buf)
248 struct switchtec_dev *stdev = to_stdev(dev);
251 ver = ioread32(&stdev->mmio_sys_info->firmware_version);
253 return sprintf(buf, "%08x\n", ver);
255 static DEVICE_ATTR_RO(fw_version);
257 static ssize_t io_string_show(char *buf, void __iomem *attr, size_t len)
261 memcpy_fromio(buf, attr, len);
265 for (i = len - 1; i > 0; i--) {
275 #define DEVICE_ATTR_SYS_INFO_STR(field) \
276 static ssize_t field ## _show(struct device *dev, \
277 struct device_attribute *attr, char *buf) \
279 struct switchtec_dev *stdev = to_stdev(dev); \
280 return io_string_show(buf, &stdev->mmio_sys_info->field, \
281 sizeof(stdev->mmio_sys_info->field)); \
284 static DEVICE_ATTR_RO(field)
286 DEVICE_ATTR_SYS_INFO_STR(vendor_id);
287 DEVICE_ATTR_SYS_INFO_STR(product_id);
288 DEVICE_ATTR_SYS_INFO_STR(product_revision);
289 DEVICE_ATTR_SYS_INFO_STR(component_vendor);
291 static ssize_t component_id_show(struct device *dev,
292 struct device_attribute *attr, char *buf)
294 struct switchtec_dev *stdev = to_stdev(dev);
295 int id = ioread16(&stdev->mmio_sys_info->component_id);
297 return sprintf(buf, "PM%04X\n", id);
299 static DEVICE_ATTR_RO(component_id);
301 static ssize_t component_revision_show(struct device *dev,
302 struct device_attribute *attr, char *buf)
304 struct switchtec_dev *stdev = to_stdev(dev);
305 int rev = ioread8(&stdev->mmio_sys_info->component_revision);
307 return sprintf(buf, "%d\n", rev);
309 static DEVICE_ATTR_RO(component_revision);
311 static ssize_t partition_show(struct device *dev,
312 struct device_attribute *attr, char *buf)
314 struct switchtec_dev *stdev = to_stdev(dev);
316 return sprintf(buf, "%d\n", stdev->partition);
318 static DEVICE_ATTR_RO(partition);
320 static ssize_t partition_count_show(struct device *dev,
321 struct device_attribute *attr, char *buf)
323 struct switchtec_dev *stdev = to_stdev(dev);
325 return sprintf(buf, "%d\n", stdev->partition_count);
327 static DEVICE_ATTR_RO(partition_count);
329 static struct attribute *switchtec_device_attrs[] = {
330 &dev_attr_device_version.attr,
331 &dev_attr_fw_version.attr,
332 &dev_attr_vendor_id.attr,
333 &dev_attr_product_id.attr,
334 &dev_attr_product_revision.attr,
335 &dev_attr_component_vendor.attr,
336 &dev_attr_component_id.attr,
337 &dev_attr_component_revision.attr,
338 &dev_attr_partition.attr,
339 &dev_attr_partition_count.attr,
343 ATTRIBUTE_GROUPS(switchtec_device);
345 static int switchtec_dev_open(struct inode *inode, struct file *filp)
347 struct switchtec_dev *stdev;
348 struct switchtec_user *stuser;
350 stdev = container_of(inode->i_cdev, struct switchtec_dev, cdev);
352 stuser = stuser_create(stdev);
354 return PTR_ERR(stuser);
356 filp->private_data = stuser;
357 nonseekable_open(inode, filp);
359 dev_dbg(&stdev->dev, "%s: %p\n", __func__, stuser);
364 static int switchtec_dev_release(struct inode *inode, struct file *filp)
366 struct switchtec_user *stuser = filp->private_data;
373 static int lock_mutex_and_test_alive(struct switchtec_dev *stdev)
375 if (mutex_lock_interruptible(&stdev->mrpc_mutex))
379 mutex_unlock(&stdev->mrpc_mutex);
386 static ssize_t switchtec_dev_write(struct file *filp, const char __user *data,
387 size_t size, loff_t *off)
389 struct switchtec_user *stuser = filp->private_data;
390 struct switchtec_dev *stdev = stuser->stdev;
393 if (size < sizeof(stuser->cmd) ||
394 size > sizeof(stuser->cmd) + sizeof(stuser->data))
397 stuser->data_len = size - sizeof(stuser->cmd);
399 rc = lock_mutex_and_test_alive(stdev);
403 if (stuser->state != MRPC_IDLE) {
408 rc = copy_from_user(&stuser->cmd, data, sizeof(stuser->cmd));
414 data += sizeof(stuser->cmd);
415 rc = copy_from_user(&stuser->data, data, size - sizeof(stuser->cmd));
421 rc = mrpc_queue_cmd(stuser);
424 mutex_unlock(&stdev->mrpc_mutex);
432 static ssize_t switchtec_dev_read(struct file *filp, char __user *data,
433 size_t size, loff_t *off)
435 struct switchtec_user *stuser = filp->private_data;
436 struct switchtec_dev *stdev = stuser->stdev;
439 if (size < sizeof(stuser->cmd) ||
440 size > sizeof(stuser->cmd) + sizeof(stuser->data))
443 rc = lock_mutex_and_test_alive(stdev);
447 if (stuser->state == MRPC_IDLE) {
448 mutex_unlock(&stdev->mrpc_mutex);
452 stuser->read_len = size - sizeof(stuser->return_code);
454 mutex_unlock(&stdev->mrpc_mutex);
456 if (filp->f_flags & O_NONBLOCK) {
457 if (!try_wait_for_completion(&stuser->comp))
460 rc = wait_for_completion_interruptible(&stuser->comp);
465 rc = lock_mutex_and_test_alive(stdev);
469 if (stuser->state != MRPC_DONE) {
470 mutex_unlock(&stdev->mrpc_mutex);
474 rc = copy_to_user(data, &stuser->return_code,
475 sizeof(stuser->return_code));
481 data += sizeof(stuser->return_code);
482 rc = copy_to_user(data, &stuser->data,
483 size - sizeof(stuser->return_code));
489 stuser_set_state(stuser, MRPC_IDLE);
492 mutex_unlock(&stdev->mrpc_mutex);
494 if (stuser->status == SWITCHTEC_MRPC_STATUS_DONE)
496 else if (stuser->status == SWITCHTEC_MRPC_STATUS_INTERRUPTED)
502 static __poll_t switchtec_dev_poll(struct file *filp, poll_table *wait)
504 struct switchtec_user *stuser = filp->private_data;
505 struct switchtec_dev *stdev = stuser->stdev;
508 poll_wait(filp, &stuser->comp.wait, wait);
509 poll_wait(filp, &stdev->event_wq, wait);
511 if (lock_mutex_and_test_alive(stdev))
512 return EPOLLIN | EPOLLRDHUP | EPOLLOUT | EPOLLERR | EPOLLHUP;
514 mutex_unlock(&stdev->mrpc_mutex);
516 if (try_wait_for_completion(&stuser->comp))
517 ret |= EPOLLIN | EPOLLRDNORM;
519 if (stuser->event_cnt != atomic_read(&stdev->event_cnt))
520 ret |= EPOLLPRI | EPOLLRDBAND;
525 static int ioctl_flash_info(struct switchtec_dev *stdev,
526 struct switchtec_ioctl_flash_info __user *uinfo)
528 struct switchtec_ioctl_flash_info info = {0};
529 struct flash_info_regs __iomem *fi = stdev->mmio_flash_info;
531 info.flash_length = ioread32(&fi->flash_length);
532 info.num_partitions = SWITCHTEC_IOCTL_NUM_PARTITIONS;
534 if (copy_to_user(uinfo, &info, sizeof(info)))
540 static void set_fw_info_part(struct switchtec_ioctl_flash_part_info *info,
541 struct partition_info __iomem *pi)
543 info->address = ioread32(&pi->address);
544 info->length = ioread32(&pi->length);
547 static int ioctl_flash_part_info(struct switchtec_dev *stdev,
548 struct switchtec_ioctl_flash_part_info __user *uinfo)
550 struct switchtec_ioctl_flash_part_info info = {0};
551 struct flash_info_regs __iomem *fi = stdev->mmio_flash_info;
552 struct sys_info_regs __iomem *si = stdev->mmio_sys_info;
553 u32 active_addr = -1;
555 if (copy_from_user(&info, uinfo, sizeof(info)))
558 switch (info.flash_partition) {
559 case SWITCHTEC_IOCTL_PART_CFG0:
560 active_addr = ioread32(&fi->active_cfg);
561 set_fw_info_part(&info, &fi->cfg0);
562 if (ioread16(&si->cfg_running) == SWITCHTEC_CFG0_RUNNING)
563 info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
565 case SWITCHTEC_IOCTL_PART_CFG1:
566 active_addr = ioread32(&fi->active_cfg);
567 set_fw_info_part(&info, &fi->cfg1);
568 if (ioread16(&si->cfg_running) == SWITCHTEC_CFG1_RUNNING)
569 info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
571 case SWITCHTEC_IOCTL_PART_IMG0:
572 active_addr = ioread32(&fi->active_img);
573 set_fw_info_part(&info, &fi->img0);
574 if (ioread16(&si->img_running) == SWITCHTEC_IMG0_RUNNING)
575 info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
577 case SWITCHTEC_IOCTL_PART_IMG1:
578 active_addr = ioread32(&fi->active_img);
579 set_fw_info_part(&info, &fi->img1);
580 if (ioread16(&si->img_running) == SWITCHTEC_IMG1_RUNNING)
581 info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
583 case SWITCHTEC_IOCTL_PART_NVLOG:
584 set_fw_info_part(&info, &fi->nvlog);
586 case SWITCHTEC_IOCTL_PART_VENDOR0:
587 set_fw_info_part(&info, &fi->vendor[0]);
589 case SWITCHTEC_IOCTL_PART_VENDOR1:
590 set_fw_info_part(&info, &fi->vendor[1]);
592 case SWITCHTEC_IOCTL_PART_VENDOR2:
593 set_fw_info_part(&info, &fi->vendor[2]);
595 case SWITCHTEC_IOCTL_PART_VENDOR3:
596 set_fw_info_part(&info, &fi->vendor[3]);
598 case SWITCHTEC_IOCTL_PART_VENDOR4:
599 set_fw_info_part(&info, &fi->vendor[4]);
601 case SWITCHTEC_IOCTL_PART_VENDOR5:
602 set_fw_info_part(&info, &fi->vendor[5]);
604 case SWITCHTEC_IOCTL_PART_VENDOR6:
605 set_fw_info_part(&info, &fi->vendor[6]);
607 case SWITCHTEC_IOCTL_PART_VENDOR7:
608 set_fw_info_part(&info, &fi->vendor[7]);
614 if (info.address == active_addr)
615 info.active |= SWITCHTEC_IOCTL_PART_ACTIVE;
617 if (copy_to_user(uinfo, &info, sizeof(info)))
623 static int ioctl_event_summary(struct switchtec_dev *stdev,
624 struct switchtec_user *stuser,
625 struct switchtec_ioctl_event_summary __user *usum)
627 struct switchtec_ioctl_event_summary s = {0};
631 s.global = ioread32(&stdev->mmio_sw_event->global_summary);
632 s.part_bitmap = readq(&stdev->mmio_sw_event->part_event_bitmap);
633 s.local_part = ioread32(&stdev->mmio_part_cfg->part_event_summary);
635 for (i = 0; i < stdev->partition_count; i++) {
636 reg = ioread32(&stdev->mmio_part_cfg_all[i].part_event_summary);
640 for (i = 0; i < SWITCHTEC_MAX_PFF_CSR; i++) {
641 reg = ioread16(&stdev->mmio_pff_csr[i].vendor_id);
642 if (reg != PCI_VENDOR_ID_MICROSEMI)
645 reg = ioread32(&stdev->mmio_pff_csr[i].pff_event_summary);
649 if (copy_to_user(usum, &s, sizeof(s)))
652 stuser->event_cnt = atomic_read(&stdev->event_cnt);
657 static u32 __iomem *global_ev_reg(struct switchtec_dev *stdev,
658 size_t offset, int index)
660 return (void __iomem *)stdev->mmio_sw_event + offset;
663 static u32 __iomem *part_ev_reg(struct switchtec_dev *stdev,
664 size_t offset, int index)
666 return (void __iomem *)&stdev->mmio_part_cfg_all[index] + offset;
669 static u32 __iomem *pff_ev_reg(struct switchtec_dev *stdev,
670 size_t offset, int index)
672 return (void __iomem *)&stdev->mmio_pff_csr[index] + offset;
675 #define EV_GLB(i, r)[i] = {offsetof(struct sw_event_regs, r), global_ev_reg}
676 #define EV_PAR(i, r)[i] = {offsetof(struct part_cfg_regs, r), part_ev_reg}
677 #define EV_PFF(i, r)[i] = {offsetof(struct pff_csr_regs, r), pff_ev_reg}
679 static const struct event_reg {
681 u32 __iomem *(*map_reg)(struct switchtec_dev *stdev,
682 size_t offset, int index);
684 EV_GLB(SWITCHTEC_IOCTL_EVENT_STACK_ERROR, stack_error_event_hdr),
685 EV_GLB(SWITCHTEC_IOCTL_EVENT_PPU_ERROR, ppu_error_event_hdr),
686 EV_GLB(SWITCHTEC_IOCTL_EVENT_ISP_ERROR, isp_error_event_hdr),
687 EV_GLB(SWITCHTEC_IOCTL_EVENT_SYS_RESET, sys_reset_event_hdr),
688 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_EXC, fw_exception_hdr),
689 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_NMI, fw_nmi_hdr),
690 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_NON_FATAL, fw_non_fatal_hdr),
691 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_FATAL, fw_fatal_hdr),
692 EV_GLB(SWITCHTEC_IOCTL_EVENT_TWI_MRPC_COMP, twi_mrpc_comp_hdr),
693 EV_GLB(SWITCHTEC_IOCTL_EVENT_TWI_MRPC_COMP_ASYNC,
694 twi_mrpc_comp_async_hdr),
695 EV_GLB(SWITCHTEC_IOCTL_EVENT_CLI_MRPC_COMP, cli_mrpc_comp_hdr),
696 EV_GLB(SWITCHTEC_IOCTL_EVENT_CLI_MRPC_COMP_ASYNC,
697 cli_mrpc_comp_async_hdr),
698 EV_GLB(SWITCHTEC_IOCTL_EVENT_GPIO_INT, gpio_interrupt_hdr),
699 EV_GLB(SWITCHTEC_IOCTL_EVENT_GFMS, gfms_event_hdr),
700 EV_PAR(SWITCHTEC_IOCTL_EVENT_PART_RESET, part_reset_hdr),
701 EV_PAR(SWITCHTEC_IOCTL_EVENT_MRPC_COMP, mrpc_comp_hdr),
702 EV_PAR(SWITCHTEC_IOCTL_EVENT_MRPC_COMP_ASYNC, mrpc_comp_async_hdr),
703 EV_PAR(SWITCHTEC_IOCTL_EVENT_DYN_PART_BIND_COMP, dyn_binding_hdr),
704 EV_PFF(SWITCHTEC_IOCTL_EVENT_AER_IN_P2P, aer_in_p2p_hdr),
705 EV_PFF(SWITCHTEC_IOCTL_EVENT_AER_IN_VEP, aer_in_vep_hdr),
706 EV_PFF(SWITCHTEC_IOCTL_EVENT_DPC, dpc_hdr),
707 EV_PFF(SWITCHTEC_IOCTL_EVENT_CTS, cts_hdr),
708 EV_PFF(SWITCHTEC_IOCTL_EVENT_HOTPLUG, hotplug_hdr),
709 EV_PFF(SWITCHTEC_IOCTL_EVENT_IER, ier_hdr),
710 EV_PFF(SWITCHTEC_IOCTL_EVENT_THRESH, threshold_hdr),
711 EV_PFF(SWITCHTEC_IOCTL_EVENT_POWER_MGMT, power_mgmt_hdr),
712 EV_PFF(SWITCHTEC_IOCTL_EVENT_TLP_THROTTLING, tlp_throttling_hdr),
713 EV_PFF(SWITCHTEC_IOCTL_EVENT_FORCE_SPEED, force_speed_hdr),
714 EV_PFF(SWITCHTEC_IOCTL_EVENT_CREDIT_TIMEOUT, credit_timeout_hdr),
715 EV_PFF(SWITCHTEC_IOCTL_EVENT_LINK_STATE, link_state_hdr),
718 static u32 __iomem *event_hdr_addr(struct switchtec_dev *stdev,
719 int event_id, int index)
723 if (event_id < 0 || event_id >= SWITCHTEC_IOCTL_MAX_EVENTS)
724 return ERR_PTR(-EINVAL);
726 off = event_regs[event_id].offset;
728 if (event_regs[event_id].map_reg == part_ev_reg) {
729 if (index == SWITCHTEC_IOCTL_EVENT_LOCAL_PART_IDX)
730 index = stdev->partition;
731 else if (index < 0 || index >= stdev->partition_count)
732 return ERR_PTR(-EINVAL);
733 } else if (event_regs[event_id].map_reg == pff_ev_reg) {
734 if (index < 0 || index >= stdev->pff_csr_count)
735 return ERR_PTR(-EINVAL);
738 return event_regs[event_id].map_reg(stdev, off, index);
741 static int event_ctl(struct switchtec_dev *stdev,
742 struct switchtec_ioctl_event_ctl *ctl)
748 reg = event_hdr_addr(stdev, ctl->event_id, ctl->index);
753 for (i = 0; i < ARRAY_SIZE(ctl->data); i++)
754 ctl->data[i] = ioread32(®[i + 1]);
756 ctl->occurred = hdr & SWITCHTEC_EVENT_OCCURRED;
757 ctl->count = (hdr >> 5) & 0xFF;
759 if (!(ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_CLEAR))
760 hdr &= ~SWITCHTEC_EVENT_CLEAR;
761 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL)
762 hdr |= SWITCHTEC_EVENT_EN_IRQ;
763 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_POLL)
764 hdr &= ~SWITCHTEC_EVENT_EN_IRQ;
765 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG)
766 hdr |= SWITCHTEC_EVENT_EN_LOG;
767 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_LOG)
768 hdr &= ~SWITCHTEC_EVENT_EN_LOG;
769 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI)
770 hdr |= SWITCHTEC_EVENT_EN_CLI;
771 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_CLI)
772 hdr &= ~SWITCHTEC_EVENT_EN_CLI;
773 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL)
774 hdr |= SWITCHTEC_EVENT_FATAL;
775 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_FATAL)
776 hdr &= ~SWITCHTEC_EVENT_FATAL;
782 if (hdr & SWITCHTEC_EVENT_EN_IRQ)
783 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL;
784 if (hdr & SWITCHTEC_EVENT_EN_LOG)
785 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG;
786 if (hdr & SWITCHTEC_EVENT_EN_CLI)
787 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI;
788 if (hdr & SWITCHTEC_EVENT_FATAL)
789 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL;
794 static int ioctl_event_ctl(struct switchtec_dev *stdev,
795 struct switchtec_ioctl_event_ctl __user *uctl)
799 unsigned int event_flags;
800 struct switchtec_ioctl_event_ctl ctl;
802 if (copy_from_user(&ctl, uctl, sizeof(ctl)))
805 if (ctl.event_id >= SWITCHTEC_IOCTL_MAX_EVENTS)
808 if (ctl.flags & SWITCHTEC_IOCTL_EVENT_FLAG_UNUSED)
811 if (ctl.index == SWITCHTEC_IOCTL_EVENT_IDX_ALL) {
812 if (event_regs[ctl.event_id].map_reg == global_ev_reg)
814 else if (event_regs[ctl.event_id].map_reg == part_ev_reg)
815 nr_idxs = stdev->partition_count;
816 else if (event_regs[ctl.event_id].map_reg == pff_ev_reg)
817 nr_idxs = stdev->pff_csr_count;
821 event_flags = ctl.flags;
822 for (ctl.index = 0; ctl.index < nr_idxs; ctl.index++) {
823 ctl.flags = event_flags;
824 ret = event_ctl(stdev, &ctl);
829 ret = event_ctl(stdev, &ctl);
834 if (copy_to_user(uctl, &ctl, sizeof(ctl)))
840 static int ioctl_pff_to_port(struct switchtec_dev *stdev,
841 struct switchtec_ioctl_pff_port *up)
845 struct part_cfg_regs *pcfg;
846 struct switchtec_ioctl_pff_port p;
848 if (copy_from_user(&p, up, sizeof(p)))
852 for (part = 0; part < stdev->partition_count; part++) {
853 pcfg = &stdev->mmio_part_cfg_all[part];
856 reg = ioread32(&pcfg->usp_pff_inst_id);
862 reg = ioread32(&pcfg->vep_pff_inst_id);
864 p.port = SWITCHTEC_IOCTL_PFF_VEP;
868 for (i = 0; i < ARRAY_SIZE(pcfg->dsp_pff_inst_id); i++) {
869 reg = ioread32(&pcfg->dsp_pff_inst_id[i]);
881 if (copy_to_user(up, &p, sizeof(p)))
887 static int ioctl_port_to_pff(struct switchtec_dev *stdev,
888 struct switchtec_ioctl_pff_port *up)
890 struct switchtec_ioctl_pff_port p;
891 struct part_cfg_regs *pcfg;
893 if (copy_from_user(&p, up, sizeof(p)))
896 if (p.partition == SWITCHTEC_IOCTL_EVENT_LOCAL_PART_IDX)
897 pcfg = stdev->mmio_part_cfg;
898 else if (p.partition < stdev->partition_count)
899 pcfg = &stdev->mmio_part_cfg_all[p.partition];
905 p.pff = ioread32(&pcfg->usp_pff_inst_id);
907 case SWITCHTEC_IOCTL_PFF_VEP:
908 p.pff = ioread32(&pcfg->vep_pff_inst_id);
911 if (p.port > ARRAY_SIZE(pcfg->dsp_pff_inst_id))
913 p.port = array_index_nospec(p.port,
914 ARRAY_SIZE(pcfg->dsp_pff_inst_id) + 1);
915 p.pff = ioread32(&pcfg->dsp_pff_inst_id[p.port - 1]);
919 if (copy_to_user(up, &p, sizeof(p)))
925 static long switchtec_dev_ioctl(struct file *filp, unsigned int cmd,
928 struct switchtec_user *stuser = filp->private_data;
929 struct switchtec_dev *stdev = stuser->stdev;
931 void __user *argp = (void __user *)arg;
933 rc = lock_mutex_and_test_alive(stdev);
938 case SWITCHTEC_IOCTL_FLASH_INFO:
939 rc = ioctl_flash_info(stdev, argp);
941 case SWITCHTEC_IOCTL_FLASH_PART_INFO:
942 rc = ioctl_flash_part_info(stdev, argp);
944 case SWITCHTEC_IOCTL_EVENT_SUMMARY:
945 rc = ioctl_event_summary(stdev, stuser, argp);
947 case SWITCHTEC_IOCTL_EVENT_CTL:
948 rc = ioctl_event_ctl(stdev, argp);
950 case SWITCHTEC_IOCTL_PFF_TO_PORT:
951 rc = ioctl_pff_to_port(stdev, argp);
953 case SWITCHTEC_IOCTL_PORT_TO_PFF:
954 rc = ioctl_port_to_pff(stdev, argp);
961 mutex_unlock(&stdev->mrpc_mutex);
965 static const struct file_operations switchtec_fops = {
966 .owner = THIS_MODULE,
967 .open = switchtec_dev_open,
968 .release = switchtec_dev_release,
969 .write = switchtec_dev_write,
970 .read = switchtec_dev_read,
971 .poll = switchtec_dev_poll,
972 .unlocked_ioctl = switchtec_dev_ioctl,
973 .compat_ioctl = switchtec_dev_ioctl,
976 static void link_event_work(struct work_struct *work)
978 struct switchtec_dev *stdev;
980 stdev = container_of(work, struct switchtec_dev, link_event_work);
982 if (stdev->link_notifier)
983 stdev->link_notifier(stdev);
986 static void check_link_state_events(struct switchtec_dev *stdev)
993 for (idx = 0; idx < stdev->pff_csr_count; idx++) {
994 reg = ioread32(&stdev->mmio_pff_csr[idx].link_state_hdr);
995 dev_dbg(&stdev->dev, "link_state: %d->%08x\n", idx, reg);
996 count = (reg >> 5) & 0xFF;
998 if (count != stdev->link_event_count[idx]) {
1000 stdev->link_event_count[idx] = count;
1005 schedule_work(&stdev->link_event_work);
1008 static void enable_link_state_events(struct switchtec_dev *stdev)
1012 for (idx = 0; idx < stdev->pff_csr_count; idx++) {
1013 iowrite32(SWITCHTEC_EVENT_CLEAR |
1014 SWITCHTEC_EVENT_EN_IRQ,
1015 &stdev->mmio_pff_csr[idx].link_state_hdr);
1019 static void stdev_release(struct device *dev)
1021 struct switchtec_dev *stdev = to_stdev(dev);
1026 static void stdev_kill(struct switchtec_dev *stdev)
1028 struct switchtec_user *stuser, *tmpuser;
1030 pci_clear_master(stdev->pdev);
1032 cancel_delayed_work_sync(&stdev->mrpc_timeout);
1034 /* Mark the hardware as unavailable and complete all completions */
1035 mutex_lock(&stdev->mrpc_mutex);
1036 stdev->alive = false;
1038 /* Wake up and kill any users waiting on an MRPC request */
1039 list_for_each_entry_safe(stuser, tmpuser, &stdev->mrpc_queue, list) {
1040 complete_all(&stuser->comp);
1041 list_del_init(&stuser->list);
1045 mutex_unlock(&stdev->mrpc_mutex);
1047 /* Wake up any users waiting on event_wq */
1048 wake_up_interruptible(&stdev->event_wq);
1051 static struct switchtec_dev *stdev_create(struct pci_dev *pdev)
1053 struct switchtec_dev *stdev;
1059 stdev = kzalloc_node(sizeof(*stdev), GFP_KERNEL,
1060 dev_to_node(&pdev->dev));
1062 return ERR_PTR(-ENOMEM);
1064 stdev->alive = true;
1066 INIT_LIST_HEAD(&stdev->mrpc_queue);
1067 mutex_init(&stdev->mrpc_mutex);
1068 stdev->mrpc_busy = 0;
1069 INIT_WORK(&stdev->mrpc_work, mrpc_event_work);
1070 INIT_DELAYED_WORK(&stdev->mrpc_timeout, mrpc_timeout_work);
1071 INIT_WORK(&stdev->link_event_work, link_event_work);
1072 init_waitqueue_head(&stdev->event_wq);
1073 atomic_set(&stdev->event_cnt, 0);
1076 device_initialize(dev);
1077 dev->class = switchtec_class;
1078 dev->parent = &pdev->dev;
1079 dev->groups = switchtec_device_groups;
1080 dev->release = stdev_release;
1082 minor = ida_simple_get(&switchtec_minor_ida, 0, 0,
1089 dev->devt = MKDEV(MAJOR(switchtec_devt), minor);
1090 dev_set_name(dev, "switchtec%d", minor);
1092 cdev = &stdev->cdev;
1093 cdev_init(cdev, &switchtec_fops);
1094 cdev->owner = THIS_MODULE;
1099 put_device(&stdev->dev);
1103 static int mask_event(struct switchtec_dev *stdev, int eid, int idx)
1105 size_t off = event_regs[eid].offset;
1106 u32 __iomem *hdr_reg;
1109 hdr_reg = event_regs[eid].map_reg(stdev, off, idx);
1110 hdr = ioread32(hdr_reg);
1112 if (!(hdr & SWITCHTEC_EVENT_OCCURRED && hdr & SWITCHTEC_EVENT_EN_IRQ))
1115 if (eid == SWITCHTEC_IOCTL_EVENT_LINK_STATE ||
1116 eid == SWITCHTEC_IOCTL_EVENT_MRPC_COMP)
1119 dev_dbg(&stdev->dev, "%s: %d %d %x\n", __func__, eid, idx, hdr);
1120 hdr &= ~(SWITCHTEC_EVENT_EN_IRQ | SWITCHTEC_EVENT_OCCURRED);
1121 iowrite32(hdr, hdr_reg);
1126 static int mask_all_events(struct switchtec_dev *stdev, int eid)
1131 if (event_regs[eid].map_reg == part_ev_reg) {
1132 for (idx = 0; idx < stdev->partition_count; idx++)
1133 count += mask_event(stdev, eid, idx);
1134 } else if (event_regs[eid].map_reg == pff_ev_reg) {
1135 for (idx = 0; idx < stdev->pff_csr_count; idx++) {
1136 if (!stdev->pff_local[idx])
1139 count += mask_event(stdev, eid, idx);
1142 count += mask_event(stdev, eid, 0);
1148 static irqreturn_t switchtec_event_isr(int irq, void *dev)
1150 struct switchtec_dev *stdev = dev;
1152 irqreturn_t ret = IRQ_NONE;
1153 int eid, event_count = 0;
1155 reg = ioread32(&stdev->mmio_part_cfg->mrpc_comp_hdr);
1156 if (reg & SWITCHTEC_EVENT_OCCURRED) {
1157 dev_dbg(&stdev->dev, "%s: mrpc comp\n", __func__);
1159 schedule_work(&stdev->mrpc_work);
1160 iowrite32(reg, &stdev->mmio_part_cfg->mrpc_comp_hdr);
1163 check_link_state_events(stdev);
1165 for (eid = 0; eid < SWITCHTEC_IOCTL_MAX_EVENTS; eid++)
1166 event_count += mask_all_events(stdev, eid);
1169 atomic_inc(&stdev->event_cnt);
1170 wake_up_interruptible(&stdev->event_wq);
1171 dev_dbg(&stdev->dev, "%s: %d events\n", __func__,
1179 static int switchtec_init_isr(struct switchtec_dev *stdev)
1184 nvecs = pci_alloc_irq_vectors(stdev->pdev, 1, 4,
1185 PCI_IRQ_MSIX | PCI_IRQ_MSI);
1189 event_irq = ioread16(&stdev->mmio_part_cfg->vep_vector_number);
1190 if (event_irq < 0 || event_irq >= nvecs)
1193 event_irq = pci_irq_vector(stdev->pdev, event_irq);
1197 return devm_request_irq(&stdev->pdev->dev, event_irq,
1198 switchtec_event_isr, 0,
1199 KBUILD_MODNAME, stdev);
1202 static void init_pff(struct switchtec_dev *stdev)
1206 struct part_cfg_regs *pcfg = stdev->mmio_part_cfg;
1208 for (i = 0; i < SWITCHTEC_MAX_PFF_CSR; i++) {
1209 reg = ioread16(&stdev->mmio_pff_csr[i].vendor_id);
1210 if (reg != PCI_VENDOR_ID_MICROSEMI)
1214 stdev->pff_csr_count = i;
1216 reg = ioread32(&pcfg->usp_pff_inst_id);
1217 if (reg < SWITCHTEC_MAX_PFF_CSR)
1218 stdev->pff_local[reg] = 1;
1220 reg = ioread32(&pcfg->vep_pff_inst_id);
1221 if (reg < SWITCHTEC_MAX_PFF_CSR)
1222 stdev->pff_local[reg] = 1;
1224 for (i = 0; i < ARRAY_SIZE(pcfg->dsp_pff_inst_id); i++) {
1225 reg = ioread32(&pcfg->dsp_pff_inst_id[i]);
1226 if (reg < SWITCHTEC_MAX_PFF_CSR)
1227 stdev->pff_local[reg] = 1;
1231 static int switchtec_init_pci(struct switchtec_dev *stdev,
1232 struct pci_dev *pdev)
1236 rc = pcim_enable_device(pdev);
1240 rc = pcim_iomap_regions(pdev, 0x1, KBUILD_MODNAME);
1244 pci_set_master(pdev);
1246 stdev->mmio = pcim_iomap_table(pdev)[0];
1247 stdev->mmio_mrpc = stdev->mmio + SWITCHTEC_GAS_MRPC_OFFSET;
1248 stdev->mmio_sw_event = stdev->mmio + SWITCHTEC_GAS_SW_EVENT_OFFSET;
1249 stdev->mmio_sys_info = stdev->mmio + SWITCHTEC_GAS_SYS_INFO_OFFSET;
1250 stdev->mmio_flash_info = stdev->mmio + SWITCHTEC_GAS_FLASH_INFO_OFFSET;
1251 stdev->mmio_ntb = stdev->mmio + SWITCHTEC_GAS_NTB_OFFSET;
1252 stdev->partition = ioread8(&stdev->mmio_sys_info->partition_id);
1253 stdev->partition_count = ioread8(&stdev->mmio_ntb->partition_count);
1254 stdev->mmio_part_cfg_all = stdev->mmio + SWITCHTEC_GAS_PART_CFG_OFFSET;
1255 stdev->mmio_part_cfg = &stdev->mmio_part_cfg_all[stdev->partition];
1256 stdev->mmio_pff_csr = stdev->mmio + SWITCHTEC_GAS_PFF_CSR_OFFSET;
1258 if (stdev->partition_count < 1)
1259 stdev->partition_count = 1;
1263 pci_set_drvdata(pdev, stdev);
1268 static int switchtec_pci_probe(struct pci_dev *pdev,
1269 const struct pci_device_id *id)
1271 struct switchtec_dev *stdev;
1274 if (pdev->class == (PCI_CLASS_BRIDGE_OTHER << 8))
1275 request_module_nowait("ntb_hw_switchtec");
1277 stdev = stdev_create(pdev);
1279 return PTR_ERR(stdev);
1281 rc = switchtec_init_pci(stdev, pdev);
1285 rc = switchtec_init_isr(stdev);
1287 dev_err(&stdev->dev, "failed to init isr.\n");
1291 iowrite32(SWITCHTEC_EVENT_CLEAR |
1292 SWITCHTEC_EVENT_EN_IRQ,
1293 &stdev->mmio_part_cfg->mrpc_comp_hdr);
1294 enable_link_state_events(stdev);
1296 rc = cdev_device_add(&stdev->cdev, &stdev->dev);
1300 dev_info(&stdev->dev, "Management device registered.\n");
1307 ida_simple_remove(&switchtec_minor_ida, MINOR(stdev->dev.devt));
1308 put_device(&stdev->dev);
1312 static void switchtec_pci_remove(struct pci_dev *pdev)
1314 struct switchtec_dev *stdev = pci_get_drvdata(pdev);
1316 pci_set_drvdata(pdev, NULL);
1318 cdev_device_del(&stdev->cdev, &stdev->dev);
1319 ida_simple_remove(&switchtec_minor_ida, MINOR(stdev->dev.devt));
1320 dev_info(&stdev->dev, "unregistered.\n");
1323 put_device(&stdev->dev);
1326 #define SWITCHTEC_PCI_DEVICE(device_id) \
1328 .vendor = PCI_VENDOR_ID_MICROSEMI, \
1329 .device = device_id, \
1330 .subvendor = PCI_ANY_ID, \
1331 .subdevice = PCI_ANY_ID, \
1332 .class = (PCI_CLASS_MEMORY_OTHER << 8), \
1333 .class_mask = 0xFFFFFFFF, \
1336 .vendor = PCI_VENDOR_ID_MICROSEMI, \
1337 .device = device_id, \
1338 .subvendor = PCI_ANY_ID, \
1339 .subdevice = PCI_ANY_ID, \
1340 .class = (PCI_CLASS_BRIDGE_OTHER << 8), \
1341 .class_mask = 0xFFFFFFFF, \
1344 static const struct pci_device_id switchtec_pci_tbl[] = {
1345 SWITCHTEC_PCI_DEVICE(0x8531), //PFX 24xG3
1346 SWITCHTEC_PCI_DEVICE(0x8532), //PFX 32xG3
1347 SWITCHTEC_PCI_DEVICE(0x8533), //PFX 48xG3
1348 SWITCHTEC_PCI_DEVICE(0x8534), //PFX 64xG3
1349 SWITCHTEC_PCI_DEVICE(0x8535), //PFX 80xG3
1350 SWITCHTEC_PCI_DEVICE(0x8536), //PFX 96xG3
1351 SWITCHTEC_PCI_DEVICE(0x8541), //PSX 24xG3
1352 SWITCHTEC_PCI_DEVICE(0x8542), //PSX 32xG3
1353 SWITCHTEC_PCI_DEVICE(0x8543), //PSX 48xG3
1354 SWITCHTEC_PCI_DEVICE(0x8544), //PSX 64xG3
1355 SWITCHTEC_PCI_DEVICE(0x8545), //PSX 80xG3
1356 SWITCHTEC_PCI_DEVICE(0x8546), //PSX 96xG3
1357 SWITCHTEC_PCI_DEVICE(0x8551), //PAX 24XG3
1358 SWITCHTEC_PCI_DEVICE(0x8552), //PAX 32XG3
1359 SWITCHTEC_PCI_DEVICE(0x8553), //PAX 48XG3
1360 SWITCHTEC_PCI_DEVICE(0x8554), //PAX 64XG3
1361 SWITCHTEC_PCI_DEVICE(0x8555), //PAX 80XG3
1362 SWITCHTEC_PCI_DEVICE(0x8556), //PAX 96XG3
1363 SWITCHTEC_PCI_DEVICE(0x8561), //PFXL 24XG3
1364 SWITCHTEC_PCI_DEVICE(0x8562), //PFXL 32XG3
1365 SWITCHTEC_PCI_DEVICE(0x8563), //PFXL 48XG3
1366 SWITCHTEC_PCI_DEVICE(0x8564), //PFXL 64XG3
1367 SWITCHTEC_PCI_DEVICE(0x8565), //PFXL 80XG3
1368 SWITCHTEC_PCI_DEVICE(0x8566), //PFXL 96XG3
1369 SWITCHTEC_PCI_DEVICE(0x8571), //PFXI 24XG3
1370 SWITCHTEC_PCI_DEVICE(0x8572), //PFXI 32XG3
1371 SWITCHTEC_PCI_DEVICE(0x8573), //PFXI 48XG3
1372 SWITCHTEC_PCI_DEVICE(0x8574), //PFXI 64XG3
1373 SWITCHTEC_PCI_DEVICE(0x8575), //PFXI 80XG3
1374 SWITCHTEC_PCI_DEVICE(0x8576), //PFXI 96XG3
1377 MODULE_DEVICE_TABLE(pci, switchtec_pci_tbl);
1379 static struct pci_driver switchtec_pci_driver = {
1380 .name = KBUILD_MODNAME,
1381 .id_table = switchtec_pci_tbl,
1382 .probe = switchtec_pci_probe,
1383 .remove = switchtec_pci_remove,
1386 static int __init switchtec_init(void)
1390 rc = alloc_chrdev_region(&switchtec_devt, 0, max_devices,
1395 switchtec_class = class_create(THIS_MODULE, "switchtec");
1396 if (IS_ERR(switchtec_class)) {
1397 rc = PTR_ERR(switchtec_class);
1398 goto err_create_class;
1401 rc = pci_register_driver(&switchtec_pci_driver);
1403 goto err_pci_register;
1405 pr_info(KBUILD_MODNAME ": loaded.\n");
1410 class_destroy(switchtec_class);
1413 unregister_chrdev_region(switchtec_devt, max_devices);
1417 module_init(switchtec_init);
1419 static void __exit switchtec_exit(void)
1421 pci_unregister_driver(&switchtec_pci_driver);
1422 class_destroy(switchtec_class);
1423 unregister_chrdev_region(switchtec_devt, max_devices);
1424 ida_destroy(&switchtec_minor_ida);
1426 pr_info(KBUILD_MODNAME ": unloaded.\n");
1428 module_exit(switchtec_exit);