2 * Microsemi Switchtec(tm) PCIe Management Driver
3 * Copyright (c) 2017, Microsemi Corporation
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 #include <linux/switchtec.h>
17 #include <linux/switchtec_ioctl.h>
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
22 #include <linux/uaccess.h>
23 #include <linux/poll.h>
24 #include <linux/wait.h>
26 MODULE_DESCRIPTION("Microsemi Switchtec(tm) PCIe Management Driver");
27 MODULE_VERSION("0.1");
28 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("Microsemi Corporation");
31 static int max_devices = 16;
32 module_param(max_devices, int, 0644);
33 MODULE_PARM_DESC(max_devices, "max number of switchtec device instances");
35 static dev_t switchtec_devt;
36 static struct class *switchtec_class;
37 static DEFINE_IDA(switchtec_minor_ida);
46 struct switchtec_user {
47 struct switchtec_dev *stdev;
49 enum mrpc_state state;
51 struct completion comp;
53 struct list_head list;
60 unsigned char data[SWITCHTEC_MRPC_PAYLOAD_SIZE];
64 static struct switchtec_user *stuser_create(struct switchtec_dev *stdev)
66 struct switchtec_user *stuser;
68 stuser = kzalloc(sizeof(*stuser), GFP_KERNEL);
70 return ERR_PTR(-ENOMEM);
72 get_device(&stdev->dev);
73 stuser->stdev = stdev;
74 kref_init(&stuser->kref);
75 INIT_LIST_HEAD(&stuser->list);
76 init_completion(&stuser->comp);
77 stuser->event_cnt = atomic_read(&stdev->event_cnt);
79 dev_dbg(&stdev->dev, "%s: %p\n", __func__, stuser);
84 static void stuser_free(struct kref *kref)
86 struct switchtec_user *stuser;
88 stuser = container_of(kref, struct switchtec_user, kref);
90 dev_dbg(&stuser->stdev->dev, "%s: %p\n", __func__, stuser);
92 put_device(&stuser->stdev->dev);
96 static void stuser_put(struct switchtec_user *stuser)
98 kref_put(&stuser->kref, stuser_free);
101 static void stuser_set_state(struct switchtec_user *stuser,
102 enum mrpc_state state)
104 /* requires the mrpc_mutex to already be held when called */
106 const char * const state_names[] = {
107 [MRPC_IDLE] = "IDLE",
108 [MRPC_QUEUED] = "QUEUED",
109 [MRPC_RUNNING] = "RUNNING",
110 [MRPC_DONE] = "DONE",
113 stuser->state = state;
115 dev_dbg(&stuser->stdev->dev, "stuser state %p -> %s",
116 stuser, state_names[state]);
119 static void mrpc_complete_cmd(struct switchtec_dev *stdev);
121 static void mrpc_cmd_submit(struct switchtec_dev *stdev)
123 /* requires the mrpc_mutex to already be held when called */
125 struct switchtec_user *stuser;
127 if (stdev->mrpc_busy)
130 if (list_empty(&stdev->mrpc_queue))
133 stuser = list_entry(stdev->mrpc_queue.next, struct switchtec_user,
136 stuser_set_state(stuser, MRPC_RUNNING);
137 stdev->mrpc_busy = 1;
138 memcpy_toio(&stdev->mmio_mrpc->input_data,
139 stuser->data, stuser->data_len);
140 iowrite32(stuser->cmd, &stdev->mmio_mrpc->cmd);
142 stuser->status = ioread32(&stdev->mmio_mrpc->status);
143 if (stuser->status != SWITCHTEC_MRPC_STATUS_INPROGRESS)
144 mrpc_complete_cmd(stdev);
146 schedule_delayed_work(&stdev->mrpc_timeout,
147 msecs_to_jiffies(500));
150 static int mrpc_queue_cmd(struct switchtec_user *stuser)
152 /* requires the mrpc_mutex to already be held when called */
154 struct switchtec_dev *stdev = stuser->stdev;
156 kref_get(&stuser->kref);
157 stuser->read_len = sizeof(stuser->data);
158 stuser_set_state(stuser, MRPC_QUEUED);
159 init_completion(&stuser->comp);
160 list_add_tail(&stuser->list, &stdev->mrpc_queue);
162 mrpc_cmd_submit(stdev);
167 static void mrpc_complete_cmd(struct switchtec_dev *stdev)
169 /* requires the mrpc_mutex to already be held when called */
170 struct switchtec_user *stuser;
172 if (list_empty(&stdev->mrpc_queue))
175 stuser = list_entry(stdev->mrpc_queue.next, struct switchtec_user,
178 stuser->status = ioread32(&stdev->mmio_mrpc->status);
179 if (stuser->status == SWITCHTEC_MRPC_STATUS_INPROGRESS)
182 stuser_set_state(stuser, MRPC_DONE);
183 stuser->return_code = 0;
185 if (stuser->status != SWITCHTEC_MRPC_STATUS_DONE)
188 stuser->return_code = ioread32(&stdev->mmio_mrpc->ret_value);
189 if (stuser->return_code != 0)
192 memcpy_fromio(stuser->data, &stdev->mmio_mrpc->output_data,
196 complete_all(&stuser->comp);
197 list_del_init(&stuser->list);
199 stdev->mrpc_busy = 0;
201 mrpc_cmd_submit(stdev);
204 static void mrpc_event_work(struct work_struct *work)
206 struct switchtec_dev *stdev;
208 stdev = container_of(work, struct switchtec_dev, mrpc_work);
210 dev_dbg(&stdev->dev, "%s\n", __func__);
212 mutex_lock(&stdev->mrpc_mutex);
213 cancel_delayed_work(&stdev->mrpc_timeout);
214 mrpc_complete_cmd(stdev);
215 mutex_unlock(&stdev->mrpc_mutex);
218 static void mrpc_timeout_work(struct work_struct *work)
220 struct switchtec_dev *stdev;
223 stdev = container_of(work, struct switchtec_dev, mrpc_timeout.work);
225 dev_dbg(&stdev->dev, "%s\n", __func__);
227 mutex_lock(&stdev->mrpc_mutex);
229 status = ioread32(&stdev->mmio_mrpc->status);
230 if (status == SWITCHTEC_MRPC_STATUS_INPROGRESS) {
231 schedule_delayed_work(&stdev->mrpc_timeout,
232 msecs_to_jiffies(500));
236 mrpc_complete_cmd(stdev);
239 mutex_unlock(&stdev->mrpc_mutex);
242 static ssize_t device_version_show(struct device *dev,
243 struct device_attribute *attr, char *buf)
245 struct switchtec_dev *stdev = to_stdev(dev);
248 ver = ioread32(&stdev->mmio_sys_info->device_version);
250 return sprintf(buf, "%x\n", ver);
252 static DEVICE_ATTR_RO(device_version);
254 static ssize_t fw_version_show(struct device *dev,
255 struct device_attribute *attr, char *buf)
257 struct switchtec_dev *stdev = to_stdev(dev);
260 ver = ioread32(&stdev->mmio_sys_info->firmware_version);
262 return sprintf(buf, "%08x\n", ver);
264 static DEVICE_ATTR_RO(fw_version);
266 static ssize_t io_string_show(char *buf, void __iomem *attr, size_t len)
270 memcpy_fromio(buf, attr, len);
274 for (i = len - 1; i > 0; i--) {
284 #define DEVICE_ATTR_SYS_INFO_STR(field) \
285 static ssize_t field ## _show(struct device *dev, \
286 struct device_attribute *attr, char *buf) \
288 struct switchtec_dev *stdev = to_stdev(dev); \
289 return io_string_show(buf, &stdev->mmio_sys_info->field, \
290 sizeof(stdev->mmio_sys_info->field)); \
293 static DEVICE_ATTR_RO(field)
295 DEVICE_ATTR_SYS_INFO_STR(vendor_id);
296 DEVICE_ATTR_SYS_INFO_STR(product_id);
297 DEVICE_ATTR_SYS_INFO_STR(product_revision);
298 DEVICE_ATTR_SYS_INFO_STR(component_vendor);
300 static ssize_t component_id_show(struct device *dev,
301 struct device_attribute *attr, char *buf)
303 struct switchtec_dev *stdev = to_stdev(dev);
304 int id = ioread16(&stdev->mmio_sys_info->component_id);
306 return sprintf(buf, "PM%04X\n", id);
308 static DEVICE_ATTR_RO(component_id);
310 static ssize_t component_revision_show(struct device *dev,
311 struct device_attribute *attr, char *buf)
313 struct switchtec_dev *stdev = to_stdev(dev);
314 int rev = ioread8(&stdev->mmio_sys_info->component_revision);
316 return sprintf(buf, "%d\n", rev);
318 static DEVICE_ATTR_RO(component_revision);
320 static ssize_t partition_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);
327 static DEVICE_ATTR_RO(partition);
329 static ssize_t partition_count_show(struct device *dev,
330 struct device_attribute *attr, char *buf)
332 struct switchtec_dev *stdev = to_stdev(dev);
334 return sprintf(buf, "%d\n", stdev->partition_count);
336 static DEVICE_ATTR_RO(partition_count);
338 static struct attribute *switchtec_device_attrs[] = {
339 &dev_attr_device_version.attr,
340 &dev_attr_fw_version.attr,
341 &dev_attr_vendor_id.attr,
342 &dev_attr_product_id.attr,
343 &dev_attr_product_revision.attr,
344 &dev_attr_component_vendor.attr,
345 &dev_attr_component_id.attr,
346 &dev_attr_component_revision.attr,
347 &dev_attr_partition.attr,
348 &dev_attr_partition_count.attr,
352 ATTRIBUTE_GROUPS(switchtec_device);
354 static int switchtec_dev_open(struct inode *inode, struct file *filp)
356 struct switchtec_dev *stdev;
357 struct switchtec_user *stuser;
359 stdev = container_of(inode->i_cdev, struct switchtec_dev, cdev);
361 stuser = stuser_create(stdev);
363 return PTR_ERR(stuser);
365 filp->private_data = stuser;
366 nonseekable_open(inode, filp);
368 dev_dbg(&stdev->dev, "%s: %p\n", __func__, stuser);
373 static int switchtec_dev_release(struct inode *inode, struct file *filp)
375 struct switchtec_user *stuser = filp->private_data;
382 static int lock_mutex_and_test_alive(struct switchtec_dev *stdev)
384 if (mutex_lock_interruptible(&stdev->mrpc_mutex))
388 mutex_unlock(&stdev->mrpc_mutex);
395 static ssize_t switchtec_dev_write(struct file *filp, const char __user *data,
396 size_t size, loff_t *off)
398 struct switchtec_user *stuser = filp->private_data;
399 struct switchtec_dev *stdev = stuser->stdev;
402 if (size < sizeof(stuser->cmd) ||
403 size > sizeof(stuser->cmd) + sizeof(stuser->data))
406 stuser->data_len = size - sizeof(stuser->cmd);
408 rc = lock_mutex_and_test_alive(stdev);
412 if (stuser->state != MRPC_IDLE) {
417 rc = copy_from_user(&stuser->cmd, data, sizeof(stuser->cmd));
423 data += sizeof(stuser->cmd);
424 rc = copy_from_user(&stuser->data, data, size - sizeof(stuser->cmd));
430 rc = mrpc_queue_cmd(stuser);
433 mutex_unlock(&stdev->mrpc_mutex);
441 static ssize_t switchtec_dev_read(struct file *filp, char __user *data,
442 size_t size, loff_t *off)
444 struct switchtec_user *stuser = filp->private_data;
445 struct switchtec_dev *stdev = stuser->stdev;
448 if (size < sizeof(stuser->cmd) ||
449 size > sizeof(stuser->cmd) + sizeof(stuser->data))
452 rc = lock_mutex_and_test_alive(stdev);
456 if (stuser->state == MRPC_IDLE) {
457 mutex_unlock(&stdev->mrpc_mutex);
461 stuser->read_len = size - sizeof(stuser->return_code);
463 mutex_unlock(&stdev->mrpc_mutex);
465 if (filp->f_flags & O_NONBLOCK) {
466 if (!try_wait_for_completion(&stuser->comp))
469 rc = wait_for_completion_interruptible(&stuser->comp);
474 rc = lock_mutex_and_test_alive(stdev);
478 if (stuser->state != MRPC_DONE) {
479 mutex_unlock(&stdev->mrpc_mutex);
483 rc = copy_to_user(data, &stuser->return_code,
484 sizeof(stuser->return_code));
490 data += sizeof(stuser->return_code);
491 rc = copy_to_user(data, &stuser->data,
492 size - sizeof(stuser->return_code));
498 stuser_set_state(stuser, MRPC_IDLE);
501 mutex_unlock(&stdev->mrpc_mutex);
503 if (stuser->status == SWITCHTEC_MRPC_STATUS_DONE)
505 else if (stuser->status == SWITCHTEC_MRPC_STATUS_INTERRUPTED)
511 static unsigned int switchtec_dev_poll(struct file *filp, poll_table *wait)
513 struct switchtec_user *stuser = filp->private_data;
514 struct switchtec_dev *stdev = stuser->stdev;
517 poll_wait(filp, &stuser->comp.wait, wait);
518 poll_wait(filp, &stdev->event_wq, wait);
520 if (lock_mutex_and_test_alive(stdev))
521 return POLLIN | POLLRDHUP | POLLOUT | POLLERR | POLLHUP;
523 mutex_unlock(&stdev->mrpc_mutex);
525 if (try_wait_for_completion(&stuser->comp))
526 ret |= POLLIN | POLLRDNORM;
528 if (stuser->event_cnt != atomic_read(&stdev->event_cnt))
529 ret |= POLLPRI | POLLRDBAND;
534 static int ioctl_flash_info(struct switchtec_dev *stdev,
535 struct switchtec_ioctl_flash_info __user *uinfo)
537 struct switchtec_ioctl_flash_info info = {0};
538 struct flash_info_regs __iomem *fi = stdev->mmio_flash_info;
540 info.flash_length = ioread32(&fi->flash_length);
541 info.num_partitions = SWITCHTEC_IOCTL_NUM_PARTITIONS;
543 if (copy_to_user(uinfo, &info, sizeof(info)))
549 static void set_fw_info_part(struct switchtec_ioctl_flash_part_info *info,
550 struct partition_info __iomem *pi)
552 info->address = ioread32(&pi->address);
553 info->length = ioread32(&pi->length);
556 static int ioctl_flash_part_info(struct switchtec_dev *stdev,
557 struct switchtec_ioctl_flash_part_info __user *uinfo)
559 struct switchtec_ioctl_flash_part_info info = {0};
560 struct flash_info_regs __iomem *fi = stdev->mmio_flash_info;
561 struct sys_info_regs __iomem *si = stdev->mmio_sys_info;
562 u32 active_addr = -1;
564 if (copy_from_user(&info, uinfo, sizeof(info)))
567 switch (info.flash_partition) {
568 case SWITCHTEC_IOCTL_PART_CFG0:
569 active_addr = ioread32(&fi->active_cfg);
570 set_fw_info_part(&info, &fi->cfg0);
571 if (ioread16(&si->cfg_running) == SWITCHTEC_CFG0_RUNNING)
572 info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
574 case SWITCHTEC_IOCTL_PART_CFG1:
575 active_addr = ioread32(&fi->active_cfg);
576 set_fw_info_part(&info, &fi->cfg1);
577 if (ioread16(&si->cfg_running) == SWITCHTEC_CFG1_RUNNING)
578 info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
580 case SWITCHTEC_IOCTL_PART_IMG0:
581 active_addr = ioread32(&fi->active_img);
582 set_fw_info_part(&info, &fi->img0);
583 if (ioread16(&si->img_running) == SWITCHTEC_IMG0_RUNNING)
584 info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
586 case SWITCHTEC_IOCTL_PART_IMG1:
587 active_addr = ioread32(&fi->active_img);
588 set_fw_info_part(&info, &fi->img1);
589 if (ioread16(&si->img_running) == SWITCHTEC_IMG1_RUNNING)
590 info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
592 case SWITCHTEC_IOCTL_PART_NVLOG:
593 set_fw_info_part(&info, &fi->nvlog);
595 case SWITCHTEC_IOCTL_PART_VENDOR0:
596 set_fw_info_part(&info, &fi->vendor[0]);
598 case SWITCHTEC_IOCTL_PART_VENDOR1:
599 set_fw_info_part(&info, &fi->vendor[1]);
601 case SWITCHTEC_IOCTL_PART_VENDOR2:
602 set_fw_info_part(&info, &fi->vendor[2]);
604 case SWITCHTEC_IOCTL_PART_VENDOR3:
605 set_fw_info_part(&info, &fi->vendor[3]);
607 case SWITCHTEC_IOCTL_PART_VENDOR4:
608 set_fw_info_part(&info, &fi->vendor[4]);
610 case SWITCHTEC_IOCTL_PART_VENDOR5:
611 set_fw_info_part(&info, &fi->vendor[5]);
613 case SWITCHTEC_IOCTL_PART_VENDOR6:
614 set_fw_info_part(&info, &fi->vendor[6]);
616 case SWITCHTEC_IOCTL_PART_VENDOR7:
617 set_fw_info_part(&info, &fi->vendor[7]);
623 if (info.address == active_addr)
624 info.active |= SWITCHTEC_IOCTL_PART_ACTIVE;
626 if (copy_to_user(uinfo, &info, sizeof(info)))
632 static int ioctl_event_summary(struct switchtec_dev *stdev,
633 struct switchtec_user *stuser,
634 struct switchtec_ioctl_event_summary __user *usum)
636 struct switchtec_ioctl_event_summary s = {0};
640 s.global = ioread32(&stdev->mmio_sw_event->global_summary);
641 s.part_bitmap = ioread32(&stdev->mmio_sw_event->part_event_bitmap);
642 s.local_part = ioread32(&stdev->mmio_part_cfg->part_event_summary);
644 for (i = 0; i < stdev->partition_count; i++) {
645 reg = ioread32(&stdev->mmio_part_cfg_all[i].part_event_summary);
649 for (i = 0; i < SWITCHTEC_MAX_PFF_CSR; i++) {
650 reg = ioread16(&stdev->mmio_pff_csr[i].vendor_id);
651 if (reg != MICROSEMI_VENDOR_ID)
654 reg = ioread32(&stdev->mmio_pff_csr[i].pff_event_summary);
658 if (copy_to_user(usum, &s, sizeof(s)))
661 stuser->event_cnt = atomic_read(&stdev->event_cnt);
666 static u32 __iomem *global_ev_reg(struct switchtec_dev *stdev,
667 size_t offset, int index)
669 return (void __iomem *)stdev->mmio_sw_event + offset;
672 static u32 __iomem *part_ev_reg(struct switchtec_dev *stdev,
673 size_t offset, int index)
675 return (void __iomem *)&stdev->mmio_part_cfg_all[index] + offset;
678 static u32 __iomem *pff_ev_reg(struct switchtec_dev *stdev,
679 size_t offset, int index)
681 return (void __iomem *)&stdev->mmio_pff_csr[index] + offset;
684 #define EV_GLB(i, r)[i] = {offsetof(struct sw_event_regs, r), global_ev_reg}
685 #define EV_PAR(i, r)[i] = {offsetof(struct part_cfg_regs, r), part_ev_reg}
686 #define EV_PFF(i, r)[i] = {offsetof(struct pff_csr_regs, r), pff_ev_reg}
688 const struct event_reg {
690 u32 __iomem *(*map_reg)(struct switchtec_dev *stdev,
691 size_t offset, int index);
693 EV_GLB(SWITCHTEC_IOCTL_EVENT_STACK_ERROR, stack_error_event_hdr),
694 EV_GLB(SWITCHTEC_IOCTL_EVENT_PPU_ERROR, ppu_error_event_hdr),
695 EV_GLB(SWITCHTEC_IOCTL_EVENT_ISP_ERROR, isp_error_event_hdr),
696 EV_GLB(SWITCHTEC_IOCTL_EVENT_SYS_RESET, sys_reset_event_hdr),
697 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_EXC, fw_exception_hdr),
698 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_NMI, fw_nmi_hdr),
699 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_NON_FATAL, fw_non_fatal_hdr),
700 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_FATAL, fw_fatal_hdr),
701 EV_GLB(SWITCHTEC_IOCTL_EVENT_TWI_MRPC_COMP, twi_mrpc_comp_hdr),
702 EV_GLB(SWITCHTEC_IOCTL_EVENT_TWI_MRPC_COMP_ASYNC,
703 twi_mrpc_comp_async_hdr),
704 EV_GLB(SWITCHTEC_IOCTL_EVENT_CLI_MRPC_COMP, cli_mrpc_comp_hdr),
705 EV_GLB(SWITCHTEC_IOCTL_EVENT_CLI_MRPC_COMP_ASYNC,
706 cli_mrpc_comp_async_hdr),
707 EV_GLB(SWITCHTEC_IOCTL_EVENT_GPIO_INT, gpio_interrupt_hdr),
708 EV_PAR(SWITCHTEC_IOCTL_EVENT_PART_RESET, part_reset_hdr),
709 EV_PAR(SWITCHTEC_IOCTL_EVENT_MRPC_COMP, mrpc_comp_hdr),
710 EV_PAR(SWITCHTEC_IOCTL_EVENT_MRPC_COMP_ASYNC, mrpc_comp_async_hdr),
711 EV_PAR(SWITCHTEC_IOCTL_EVENT_DYN_PART_BIND_COMP, dyn_binding_hdr),
712 EV_PFF(SWITCHTEC_IOCTL_EVENT_AER_IN_P2P, aer_in_p2p_hdr),
713 EV_PFF(SWITCHTEC_IOCTL_EVENT_AER_IN_VEP, aer_in_vep_hdr),
714 EV_PFF(SWITCHTEC_IOCTL_EVENT_DPC, dpc_hdr),
715 EV_PFF(SWITCHTEC_IOCTL_EVENT_CTS, cts_hdr),
716 EV_PFF(SWITCHTEC_IOCTL_EVENT_HOTPLUG, hotplug_hdr),
717 EV_PFF(SWITCHTEC_IOCTL_EVENT_IER, ier_hdr),
718 EV_PFF(SWITCHTEC_IOCTL_EVENT_THRESH, threshold_hdr),
719 EV_PFF(SWITCHTEC_IOCTL_EVENT_POWER_MGMT, power_mgmt_hdr),
720 EV_PFF(SWITCHTEC_IOCTL_EVENT_TLP_THROTTLING, tlp_throttling_hdr),
721 EV_PFF(SWITCHTEC_IOCTL_EVENT_FORCE_SPEED, force_speed_hdr),
722 EV_PFF(SWITCHTEC_IOCTL_EVENT_CREDIT_TIMEOUT, credit_timeout_hdr),
723 EV_PFF(SWITCHTEC_IOCTL_EVENT_LINK_STATE, link_state_hdr),
726 static u32 __iomem *event_hdr_addr(struct switchtec_dev *stdev,
727 int event_id, int index)
731 if (event_id < 0 || event_id >= SWITCHTEC_IOCTL_MAX_EVENTS)
732 return ERR_PTR(-EINVAL);
734 off = event_regs[event_id].offset;
736 if (event_regs[event_id].map_reg == part_ev_reg) {
737 if (index == SWITCHTEC_IOCTL_EVENT_LOCAL_PART_IDX)
738 index = stdev->partition;
739 else if (index < 0 || index >= stdev->partition_count)
740 return ERR_PTR(-EINVAL);
741 } else if (event_regs[event_id].map_reg == pff_ev_reg) {
742 if (index < 0 || index >= stdev->pff_csr_count)
743 return ERR_PTR(-EINVAL);
746 return event_regs[event_id].map_reg(stdev, off, index);
749 static int event_ctl(struct switchtec_dev *stdev,
750 struct switchtec_ioctl_event_ctl *ctl)
756 reg = event_hdr_addr(stdev, ctl->event_id, ctl->index);
761 for (i = 0; i < ARRAY_SIZE(ctl->data); i++)
762 ctl->data[i] = ioread32(®[i + 1]);
764 ctl->occurred = hdr & SWITCHTEC_EVENT_OCCURRED;
765 ctl->count = (hdr >> 5) & 0xFF;
767 if (!(ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_CLEAR))
768 hdr &= ~SWITCHTEC_EVENT_CLEAR;
769 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL)
770 hdr |= SWITCHTEC_EVENT_EN_IRQ;
771 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_POLL)
772 hdr &= ~SWITCHTEC_EVENT_EN_IRQ;
773 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG)
774 hdr |= SWITCHTEC_EVENT_EN_LOG;
775 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_LOG)
776 hdr &= ~SWITCHTEC_EVENT_EN_LOG;
777 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI)
778 hdr |= SWITCHTEC_EVENT_EN_CLI;
779 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_CLI)
780 hdr &= ~SWITCHTEC_EVENT_EN_CLI;
781 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL)
782 hdr |= SWITCHTEC_EVENT_FATAL;
783 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_FATAL)
784 hdr &= ~SWITCHTEC_EVENT_FATAL;
790 if (hdr & SWITCHTEC_EVENT_EN_IRQ)
791 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL;
792 if (hdr & SWITCHTEC_EVENT_EN_LOG)
793 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG;
794 if (hdr & SWITCHTEC_EVENT_EN_CLI)
795 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI;
796 if (hdr & SWITCHTEC_EVENT_FATAL)
797 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL;
802 static int ioctl_event_ctl(struct switchtec_dev *stdev,
803 struct switchtec_ioctl_event_ctl __user *uctl)
807 struct switchtec_ioctl_event_ctl ctl;
809 if (copy_from_user(&ctl, uctl, sizeof(ctl)))
812 if (ctl.event_id >= SWITCHTEC_IOCTL_MAX_EVENTS)
815 if (ctl.flags & SWITCHTEC_IOCTL_EVENT_FLAG_UNUSED)
818 if (ctl.index == SWITCHTEC_IOCTL_EVENT_IDX_ALL) {
819 if (event_regs[ctl.event_id].map_reg == global_ev_reg)
821 else if (event_regs[ctl.event_id].map_reg == part_ev_reg)
822 nr_idxs = stdev->partition_count;
823 else if (event_regs[ctl.event_id].map_reg == pff_ev_reg)
824 nr_idxs = stdev->pff_csr_count;
828 for (ctl.index = 0; ctl.index < nr_idxs; ctl.index++) {
829 ret = event_ctl(stdev, &ctl);
834 ret = event_ctl(stdev, &ctl);
839 if (copy_to_user(uctl, &ctl, sizeof(ctl)))
845 static int ioctl_pff_to_port(struct switchtec_dev *stdev,
846 struct switchtec_ioctl_pff_port *up)
850 struct part_cfg_regs *pcfg;
851 struct switchtec_ioctl_pff_port p;
853 if (copy_from_user(&p, up, sizeof(p)))
857 for (part = 0; part < stdev->partition_count; part++) {
858 pcfg = &stdev->mmio_part_cfg_all[part];
861 reg = ioread32(&pcfg->usp_pff_inst_id);
867 reg = ioread32(&pcfg->vep_pff_inst_id);
869 p.port = SWITCHTEC_IOCTL_PFF_VEP;
873 for (i = 0; i < ARRAY_SIZE(pcfg->dsp_pff_inst_id); i++) {
874 reg = ioread32(&pcfg->dsp_pff_inst_id[i]);
886 if (copy_to_user(up, &p, sizeof(p)))
892 static int ioctl_port_to_pff(struct switchtec_dev *stdev,
893 struct switchtec_ioctl_pff_port *up)
895 struct switchtec_ioctl_pff_port p;
896 struct part_cfg_regs *pcfg;
898 if (copy_from_user(&p, up, sizeof(p)))
901 if (p.partition == SWITCHTEC_IOCTL_EVENT_LOCAL_PART_IDX)
902 pcfg = stdev->mmio_part_cfg;
903 else if (p.partition < stdev->partition_count)
904 pcfg = &stdev->mmio_part_cfg_all[p.partition];
910 p.pff = ioread32(&pcfg->usp_pff_inst_id);
912 case SWITCHTEC_IOCTL_PFF_VEP:
913 p.pff = ioread32(&pcfg->vep_pff_inst_id);
916 if (p.port > ARRAY_SIZE(pcfg->dsp_pff_inst_id))
918 p.pff = ioread32(&pcfg->dsp_pff_inst_id[p.port - 1]);
922 if (copy_to_user(up, &p, sizeof(p)))
928 static long switchtec_dev_ioctl(struct file *filp, unsigned int cmd,
931 struct switchtec_user *stuser = filp->private_data;
932 struct switchtec_dev *stdev = stuser->stdev;
934 void __user *argp = (void __user *)arg;
936 rc = lock_mutex_and_test_alive(stdev);
941 case SWITCHTEC_IOCTL_FLASH_INFO:
942 rc = ioctl_flash_info(stdev, argp);
944 case SWITCHTEC_IOCTL_FLASH_PART_INFO:
945 rc = ioctl_flash_part_info(stdev, argp);
947 case SWITCHTEC_IOCTL_EVENT_SUMMARY:
948 rc = ioctl_event_summary(stdev, stuser, argp);
950 case SWITCHTEC_IOCTL_EVENT_CTL:
951 rc = ioctl_event_ctl(stdev, argp);
953 case SWITCHTEC_IOCTL_PFF_TO_PORT:
954 rc = ioctl_pff_to_port(stdev, argp);
956 case SWITCHTEC_IOCTL_PORT_TO_PFF:
957 rc = ioctl_port_to_pff(stdev, argp);
964 mutex_unlock(&stdev->mrpc_mutex);
968 static const struct file_operations switchtec_fops = {
969 .owner = THIS_MODULE,
970 .open = switchtec_dev_open,
971 .release = switchtec_dev_release,
972 .write = switchtec_dev_write,
973 .read = switchtec_dev_read,
974 .poll = switchtec_dev_poll,
975 .unlocked_ioctl = switchtec_dev_ioctl,
976 .compat_ioctl = switchtec_dev_ioctl,
979 static void stdev_release(struct device *dev)
981 struct switchtec_dev *stdev = to_stdev(dev);
986 static void stdev_kill(struct switchtec_dev *stdev)
988 struct switchtec_user *stuser, *tmpuser;
990 pci_clear_master(stdev->pdev);
992 cancel_delayed_work_sync(&stdev->mrpc_timeout);
994 /* Mark the hardware as unavailable and complete all completions */
995 mutex_lock(&stdev->mrpc_mutex);
996 stdev->alive = false;
998 /* Wake up and kill any users waiting on an MRPC request */
999 list_for_each_entry_safe(stuser, tmpuser, &stdev->mrpc_queue, list) {
1000 complete_all(&stuser->comp);
1001 list_del_init(&stuser->list);
1005 mutex_unlock(&stdev->mrpc_mutex);
1007 /* Wake up any users waiting on event_wq */
1008 wake_up_interruptible(&stdev->event_wq);
1011 static struct switchtec_dev *stdev_create(struct pci_dev *pdev)
1013 struct switchtec_dev *stdev;
1019 stdev = kzalloc_node(sizeof(*stdev), GFP_KERNEL,
1020 dev_to_node(&pdev->dev));
1022 return ERR_PTR(-ENOMEM);
1024 stdev->alive = true;
1026 INIT_LIST_HEAD(&stdev->mrpc_queue);
1027 mutex_init(&stdev->mrpc_mutex);
1028 stdev->mrpc_busy = 0;
1029 INIT_WORK(&stdev->mrpc_work, mrpc_event_work);
1030 INIT_DELAYED_WORK(&stdev->mrpc_timeout, mrpc_timeout_work);
1031 init_waitqueue_head(&stdev->event_wq);
1032 atomic_set(&stdev->event_cnt, 0);
1035 device_initialize(dev);
1036 dev->class = switchtec_class;
1037 dev->parent = &pdev->dev;
1038 dev->groups = switchtec_device_groups;
1039 dev->release = stdev_release;
1041 minor = ida_simple_get(&switchtec_minor_ida, 0, 0,
1048 dev->devt = MKDEV(MAJOR(switchtec_devt), minor);
1049 dev_set_name(dev, "switchtec%d", minor);
1051 cdev = &stdev->cdev;
1052 cdev_init(cdev, &switchtec_fops);
1053 cdev->owner = THIS_MODULE;
1058 put_device(&stdev->dev);
1062 static int mask_event(struct switchtec_dev *stdev, int eid, int idx)
1064 size_t off = event_regs[eid].offset;
1065 u32 __iomem *hdr_reg;
1068 hdr_reg = event_regs[eid].map_reg(stdev, off, idx);
1069 hdr = ioread32(hdr_reg);
1071 if (!(hdr & SWITCHTEC_EVENT_OCCURRED && hdr & SWITCHTEC_EVENT_EN_IRQ))
1074 dev_dbg(&stdev->dev, "%s: %d %d %x\n", __func__, eid, idx, hdr);
1075 hdr &= ~(SWITCHTEC_EVENT_EN_IRQ | SWITCHTEC_EVENT_OCCURRED);
1076 iowrite32(hdr, hdr_reg);
1081 static int mask_all_events(struct switchtec_dev *stdev, int eid)
1086 if (event_regs[eid].map_reg == part_ev_reg) {
1087 for (idx = 0; idx < stdev->partition_count; idx++)
1088 count += mask_event(stdev, eid, idx);
1089 } else if (event_regs[eid].map_reg == pff_ev_reg) {
1090 for (idx = 0; idx < stdev->pff_csr_count; idx++) {
1091 if (!stdev->pff_local[idx])
1093 count += mask_event(stdev, eid, idx);
1096 count += mask_event(stdev, eid, 0);
1102 static irqreturn_t switchtec_event_isr(int irq, void *dev)
1104 struct switchtec_dev *stdev = dev;
1106 irqreturn_t ret = IRQ_NONE;
1107 int eid, event_count = 0;
1109 reg = ioread32(&stdev->mmio_part_cfg->mrpc_comp_hdr);
1110 if (reg & SWITCHTEC_EVENT_OCCURRED) {
1111 dev_dbg(&stdev->dev, "%s: mrpc comp\n", __func__);
1113 schedule_work(&stdev->mrpc_work);
1114 iowrite32(reg, &stdev->mmio_part_cfg->mrpc_comp_hdr);
1117 for (eid = 0; eid < SWITCHTEC_IOCTL_MAX_EVENTS; eid++)
1118 event_count += mask_all_events(stdev, eid);
1121 atomic_inc(&stdev->event_cnt);
1122 wake_up_interruptible(&stdev->event_wq);
1123 dev_dbg(&stdev->dev, "%s: %d events\n", __func__,
1131 static int switchtec_init_isr(struct switchtec_dev *stdev)
1136 nvecs = pci_alloc_irq_vectors(stdev->pdev, 1, 4,
1137 PCI_IRQ_MSIX | PCI_IRQ_MSI);
1141 event_irq = ioread32(&stdev->mmio_part_cfg->vep_vector_number);
1142 if (event_irq < 0 || event_irq >= nvecs)
1145 event_irq = pci_irq_vector(stdev->pdev, event_irq);
1149 return devm_request_irq(&stdev->pdev->dev, event_irq,
1150 switchtec_event_isr, 0,
1151 KBUILD_MODNAME, stdev);
1154 static void init_pff(struct switchtec_dev *stdev)
1158 struct part_cfg_regs *pcfg = stdev->mmio_part_cfg;
1160 for (i = 0; i < SWITCHTEC_MAX_PFF_CSR; i++) {
1161 reg = ioread16(&stdev->mmio_pff_csr[i].vendor_id);
1162 if (reg != MICROSEMI_VENDOR_ID)
1166 stdev->pff_csr_count = i;
1168 reg = ioread32(&pcfg->usp_pff_inst_id);
1169 if (reg < SWITCHTEC_MAX_PFF_CSR)
1170 stdev->pff_local[reg] = 1;
1172 reg = ioread32(&pcfg->vep_pff_inst_id);
1173 if (reg < SWITCHTEC_MAX_PFF_CSR)
1174 stdev->pff_local[reg] = 1;
1176 for (i = 0; i < ARRAY_SIZE(pcfg->dsp_pff_inst_id); i++) {
1177 reg = ioread32(&pcfg->dsp_pff_inst_id[i]);
1178 if (reg < SWITCHTEC_MAX_PFF_CSR)
1179 stdev->pff_local[reg] = 1;
1183 static int switchtec_init_pci(struct switchtec_dev *stdev,
1184 struct pci_dev *pdev)
1188 rc = pcim_enable_device(pdev);
1192 rc = pcim_iomap_regions(pdev, 0x1, KBUILD_MODNAME);
1196 pci_set_master(pdev);
1198 stdev->mmio = pcim_iomap_table(pdev)[0];
1199 stdev->mmio_mrpc = stdev->mmio + SWITCHTEC_GAS_MRPC_OFFSET;
1200 stdev->mmio_sw_event = stdev->mmio + SWITCHTEC_GAS_SW_EVENT_OFFSET;
1201 stdev->mmio_sys_info = stdev->mmio + SWITCHTEC_GAS_SYS_INFO_OFFSET;
1202 stdev->mmio_flash_info = stdev->mmio + SWITCHTEC_GAS_FLASH_INFO_OFFSET;
1203 stdev->mmio_ntb = stdev->mmio + SWITCHTEC_GAS_NTB_OFFSET;
1204 stdev->partition = ioread8(&stdev->mmio_sys_info->partition_id);
1205 stdev->partition_count = ioread8(&stdev->mmio_ntb->partition_count);
1206 stdev->mmio_part_cfg_all = stdev->mmio + SWITCHTEC_GAS_PART_CFG_OFFSET;
1207 stdev->mmio_part_cfg = &stdev->mmio_part_cfg_all[stdev->partition];
1208 stdev->mmio_pff_csr = stdev->mmio + SWITCHTEC_GAS_PFF_CSR_OFFSET;
1210 if (stdev->partition_count < 1)
1211 stdev->partition_count = 1;
1215 pci_set_drvdata(pdev, stdev);
1220 static int switchtec_pci_probe(struct pci_dev *pdev,
1221 const struct pci_device_id *id)
1223 struct switchtec_dev *stdev;
1226 stdev = stdev_create(pdev);
1228 return PTR_ERR(stdev);
1230 rc = switchtec_init_pci(stdev, pdev);
1234 rc = switchtec_init_isr(stdev);
1236 dev_err(&stdev->dev, "failed to init isr.\n");
1240 iowrite32(SWITCHTEC_EVENT_CLEAR |
1241 SWITCHTEC_EVENT_EN_IRQ,
1242 &stdev->mmio_part_cfg->mrpc_comp_hdr);
1244 rc = cdev_device_add(&stdev->cdev, &stdev->dev);
1248 dev_info(&stdev->dev, "Management device registered.\n");
1255 ida_simple_remove(&switchtec_minor_ida, MINOR(stdev->dev.devt));
1256 put_device(&stdev->dev);
1260 static void switchtec_pci_remove(struct pci_dev *pdev)
1262 struct switchtec_dev *stdev = pci_get_drvdata(pdev);
1264 pci_set_drvdata(pdev, NULL);
1266 cdev_device_del(&stdev->cdev, &stdev->dev);
1267 ida_simple_remove(&switchtec_minor_ida, MINOR(stdev->dev.devt));
1268 dev_info(&stdev->dev, "unregistered.\n");
1271 put_device(&stdev->dev);
1274 #define SWITCHTEC_PCI_DEVICE(device_id) \
1276 .vendor = MICROSEMI_VENDOR_ID, \
1277 .device = device_id, \
1278 .subvendor = PCI_ANY_ID, \
1279 .subdevice = PCI_ANY_ID, \
1280 .class = MICROSEMI_MGMT_CLASSCODE, \
1281 .class_mask = 0xFFFFFFFF, \
1284 .vendor = MICROSEMI_VENDOR_ID, \
1285 .device = device_id, \
1286 .subvendor = PCI_ANY_ID, \
1287 .subdevice = PCI_ANY_ID, \
1288 .class = MICROSEMI_NTB_CLASSCODE, \
1289 .class_mask = 0xFFFFFFFF, \
1292 static const struct pci_device_id switchtec_pci_tbl[] = {
1293 SWITCHTEC_PCI_DEVICE(0x8531), //PFX 24xG3
1294 SWITCHTEC_PCI_DEVICE(0x8532), //PFX 32xG3
1295 SWITCHTEC_PCI_DEVICE(0x8533), //PFX 48xG3
1296 SWITCHTEC_PCI_DEVICE(0x8534), //PFX 64xG3
1297 SWITCHTEC_PCI_DEVICE(0x8535), //PFX 80xG3
1298 SWITCHTEC_PCI_DEVICE(0x8536), //PFX 96xG3
1299 SWITCHTEC_PCI_DEVICE(0x8543), //PSX 48xG3
1300 SWITCHTEC_PCI_DEVICE(0x8544), //PSX 64xG3
1301 SWITCHTEC_PCI_DEVICE(0x8545), //PSX 80xG3
1302 SWITCHTEC_PCI_DEVICE(0x8546), //PSX 96xG3
1303 SWITCHTEC_PCI_DEVICE(0x8551), //PAX 24XG3
1304 SWITCHTEC_PCI_DEVICE(0x8552), //PAX 32XG3
1305 SWITCHTEC_PCI_DEVICE(0x8553), //PAX 48XG3
1306 SWITCHTEC_PCI_DEVICE(0x8554), //PAX 64XG3
1307 SWITCHTEC_PCI_DEVICE(0x8555), //PAX 80XG3
1308 SWITCHTEC_PCI_DEVICE(0x8556), //PAX 96XG3
1309 SWITCHTEC_PCI_DEVICE(0x8561), //PFXL 24XG3
1310 SWITCHTEC_PCI_DEVICE(0x8562), //PFXL 32XG3
1311 SWITCHTEC_PCI_DEVICE(0x8563), //PFXL 48XG3
1312 SWITCHTEC_PCI_DEVICE(0x8564), //PFXL 64XG3
1313 SWITCHTEC_PCI_DEVICE(0x8565), //PFXL 80XG3
1314 SWITCHTEC_PCI_DEVICE(0x8566), //PFXL 96XG3
1315 SWITCHTEC_PCI_DEVICE(0x8571), //PFXI 24XG3
1316 SWITCHTEC_PCI_DEVICE(0x8572), //PFXI 32XG3
1317 SWITCHTEC_PCI_DEVICE(0x8573), //PFXI 48XG3
1318 SWITCHTEC_PCI_DEVICE(0x8574), //PFXI 64XG3
1319 SWITCHTEC_PCI_DEVICE(0x8575), //PFXI 80XG3
1320 SWITCHTEC_PCI_DEVICE(0x8576), //PFXI 96XG3
1323 MODULE_DEVICE_TABLE(pci, switchtec_pci_tbl);
1325 static struct pci_driver switchtec_pci_driver = {
1326 .name = KBUILD_MODNAME,
1327 .id_table = switchtec_pci_tbl,
1328 .probe = switchtec_pci_probe,
1329 .remove = switchtec_pci_remove,
1332 static int __init switchtec_init(void)
1336 rc = alloc_chrdev_region(&switchtec_devt, 0, max_devices,
1341 switchtec_class = class_create(THIS_MODULE, "switchtec");
1342 if (IS_ERR(switchtec_class)) {
1343 rc = PTR_ERR(switchtec_class);
1344 goto err_create_class;
1347 rc = pci_register_driver(&switchtec_pci_driver);
1349 goto err_pci_register;
1351 pr_info(KBUILD_MODNAME ": loaded.\n");
1356 class_destroy(switchtec_class);
1359 unregister_chrdev_region(switchtec_devt, max_devices);
1363 module_init(switchtec_init);
1365 static void __exit switchtec_exit(void)
1367 pci_unregister_driver(&switchtec_pci_driver);
1368 class_destroy(switchtec_class);
1369 unregister_chrdev_region(switchtec_devt, max_devices);
1370 ida_destroy(&switchtec_minor_ida);
1372 pr_info(KBUILD_MODNAME ": unloaded.\n");
1374 module_exit(switchtec_exit);