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 DEFINE_IDA(switchtec_minor_ida);
38 struct class *switchtec_class;
39 EXPORT_SYMBOL_GPL(switchtec_class);
48 struct switchtec_user {
49 struct switchtec_dev *stdev;
51 enum mrpc_state state;
53 struct completion comp;
55 struct list_head list;
62 unsigned char data[SWITCHTEC_MRPC_PAYLOAD_SIZE];
66 static struct switchtec_user *stuser_create(struct switchtec_dev *stdev)
68 struct switchtec_user *stuser;
70 stuser = kzalloc(sizeof(*stuser), GFP_KERNEL);
72 return ERR_PTR(-ENOMEM);
74 get_device(&stdev->dev);
75 stuser->stdev = stdev;
76 kref_init(&stuser->kref);
77 INIT_LIST_HEAD(&stuser->list);
78 init_completion(&stuser->comp);
79 stuser->event_cnt = atomic_read(&stdev->event_cnt);
81 dev_dbg(&stdev->dev, "%s: %p\n", __func__, stuser);
86 static void stuser_free(struct kref *kref)
88 struct switchtec_user *stuser;
90 stuser = container_of(kref, struct switchtec_user, kref);
92 dev_dbg(&stuser->stdev->dev, "%s: %p\n", __func__, stuser);
94 put_device(&stuser->stdev->dev);
98 static void stuser_put(struct switchtec_user *stuser)
100 kref_put(&stuser->kref, stuser_free);
103 static void stuser_set_state(struct switchtec_user *stuser,
104 enum mrpc_state state)
106 /* requires the mrpc_mutex to already be held when called */
108 const char * const state_names[] = {
109 [MRPC_IDLE] = "IDLE",
110 [MRPC_QUEUED] = "QUEUED",
111 [MRPC_RUNNING] = "RUNNING",
112 [MRPC_DONE] = "DONE",
115 stuser->state = state;
117 dev_dbg(&stuser->stdev->dev, "stuser state %p -> %s",
118 stuser, state_names[state]);
121 static void mrpc_complete_cmd(struct switchtec_dev *stdev);
123 static void mrpc_cmd_submit(struct switchtec_dev *stdev)
125 /* requires the mrpc_mutex to already be held when called */
127 struct switchtec_user *stuser;
129 if (stdev->mrpc_busy)
132 if (list_empty(&stdev->mrpc_queue))
135 stuser = list_entry(stdev->mrpc_queue.next, struct switchtec_user,
138 stuser_set_state(stuser, MRPC_RUNNING);
139 stdev->mrpc_busy = 1;
140 memcpy_toio(&stdev->mmio_mrpc->input_data,
141 stuser->data, stuser->data_len);
142 iowrite32(stuser->cmd, &stdev->mmio_mrpc->cmd);
144 stuser->status = ioread32(&stdev->mmio_mrpc->status);
145 if (stuser->status != SWITCHTEC_MRPC_STATUS_INPROGRESS)
146 mrpc_complete_cmd(stdev);
148 schedule_delayed_work(&stdev->mrpc_timeout,
149 msecs_to_jiffies(500));
152 static int mrpc_queue_cmd(struct switchtec_user *stuser)
154 /* requires the mrpc_mutex to already be held when called */
156 struct switchtec_dev *stdev = stuser->stdev;
158 kref_get(&stuser->kref);
159 stuser->read_len = sizeof(stuser->data);
160 stuser_set_state(stuser, MRPC_QUEUED);
161 init_completion(&stuser->comp);
162 list_add_tail(&stuser->list, &stdev->mrpc_queue);
164 mrpc_cmd_submit(stdev);
169 static void mrpc_complete_cmd(struct switchtec_dev *stdev)
171 /* requires the mrpc_mutex to already be held when called */
172 struct switchtec_user *stuser;
174 if (list_empty(&stdev->mrpc_queue))
177 stuser = list_entry(stdev->mrpc_queue.next, struct switchtec_user,
180 stuser->status = ioread32(&stdev->mmio_mrpc->status);
181 if (stuser->status == SWITCHTEC_MRPC_STATUS_INPROGRESS)
184 stuser_set_state(stuser, MRPC_DONE);
185 stuser->return_code = 0;
187 if (stuser->status != SWITCHTEC_MRPC_STATUS_DONE)
190 stuser->return_code = ioread32(&stdev->mmio_mrpc->ret_value);
191 if (stuser->return_code != 0)
194 memcpy_fromio(stuser->data, &stdev->mmio_mrpc->output_data,
198 complete_all(&stuser->comp);
199 list_del_init(&stuser->list);
201 stdev->mrpc_busy = 0;
203 mrpc_cmd_submit(stdev);
206 static void mrpc_event_work(struct work_struct *work)
208 struct switchtec_dev *stdev;
210 stdev = container_of(work, struct switchtec_dev, mrpc_work);
212 dev_dbg(&stdev->dev, "%s\n", __func__);
214 mutex_lock(&stdev->mrpc_mutex);
215 cancel_delayed_work(&stdev->mrpc_timeout);
216 mrpc_complete_cmd(stdev);
217 mutex_unlock(&stdev->mrpc_mutex);
220 static void mrpc_timeout_work(struct work_struct *work)
222 struct switchtec_dev *stdev;
225 stdev = container_of(work, struct switchtec_dev, mrpc_timeout.work);
227 dev_dbg(&stdev->dev, "%s\n", __func__);
229 mutex_lock(&stdev->mrpc_mutex);
231 status = ioread32(&stdev->mmio_mrpc->status);
232 if (status == SWITCHTEC_MRPC_STATUS_INPROGRESS) {
233 schedule_delayed_work(&stdev->mrpc_timeout,
234 msecs_to_jiffies(500));
238 mrpc_complete_cmd(stdev);
241 mutex_unlock(&stdev->mrpc_mutex);
244 static ssize_t device_version_show(struct device *dev,
245 struct device_attribute *attr, char *buf)
247 struct switchtec_dev *stdev = to_stdev(dev);
250 ver = ioread32(&stdev->mmio_sys_info->device_version);
252 return sprintf(buf, "%x\n", ver);
254 static DEVICE_ATTR_RO(device_version);
256 static ssize_t fw_version_show(struct device *dev,
257 struct device_attribute *attr, char *buf)
259 struct switchtec_dev *stdev = to_stdev(dev);
262 ver = ioread32(&stdev->mmio_sys_info->firmware_version);
264 return sprintf(buf, "%08x\n", ver);
266 static DEVICE_ATTR_RO(fw_version);
268 static ssize_t io_string_show(char *buf, void __iomem *attr, size_t len)
272 memcpy_fromio(buf, attr, len);
276 for (i = len - 1; i > 0; i--) {
286 #define DEVICE_ATTR_SYS_INFO_STR(field) \
287 static ssize_t field ## _show(struct device *dev, \
288 struct device_attribute *attr, char *buf) \
290 struct switchtec_dev *stdev = to_stdev(dev); \
291 return io_string_show(buf, &stdev->mmio_sys_info->field, \
292 sizeof(stdev->mmio_sys_info->field)); \
295 static DEVICE_ATTR_RO(field)
297 DEVICE_ATTR_SYS_INFO_STR(vendor_id);
298 DEVICE_ATTR_SYS_INFO_STR(product_id);
299 DEVICE_ATTR_SYS_INFO_STR(product_revision);
300 DEVICE_ATTR_SYS_INFO_STR(component_vendor);
302 static ssize_t component_id_show(struct device *dev,
303 struct device_attribute *attr, char *buf)
305 struct switchtec_dev *stdev = to_stdev(dev);
306 int id = ioread16(&stdev->mmio_sys_info->component_id);
308 return sprintf(buf, "PM%04X\n", id);
310 static DEVICE_ATTR_RO(component_id);
312 static ssize_t component_revision_show(struct device *dev,
313 struct device_attribute *attr, char *buf)
315 struct switchtec_dev *stdev = to_stdev(dev);
316 int rev = ioread8(&stdev->mmio_sys_info->component_revision);
318 return sprintf(buf, "%d\n", rev);
320 static DEVICE_ATTR_RO(component_revision);
322 static ssize_t partition_show(struct device *dev,
323 struct device_attribute *attr, char *buf)
325 struct switchtec_dev *stdev = to_stdev(dev);
327 return sprintf(buf, "%d\n", stdev->partition);
329 static DEVICE_ATTR_RO(partition);
331 static ssize_t partition_count_show(struct device *dev,
332 struct device_attribute *attr, char *buf)
334 struct switchtec_dev *stdev = to_stdev(dev);
336 return sprintf(buf, "%d\n", stdev->partition_count);
338 static DEVICE_ATTR_RO(partition_count);
340 static struct attribute *switchtec_device_attrs[] = {
341 &dev_attr_device_version.attr,
342 &dev_attr_fw_version.attr,
343 &dev_attr_vendor_id.attr,
344 &dev_attr_product_id.attr,
345 &dev_attr_product_revision.attr,
346 &dev_attr_component_vendor.attr,
347 &dev_attr_component_id.attr,
348 &dev_attr_component_revision.attr,
349 &dev_attr_partition.attr,
350 &dev_attr_partition_count.attr,
354 ATTRIBUTE_GROUPS(switchtec_device);
356 static int switchtec_dev_open(struct inode *inode, struct file *filp)
358 struct switchtec_dev *stdev;
359 struct switchtec_user *stuser;
361 stdev = container_of(inode->i_cdev, struct switchtec_dev, cdev);
363 stuser = stuser_create(stdev);
365 return PTR_ERR(stuser);
367 filp->private_data = stuser;
368 nonseekable_open(inode, filp);
370 dev_dbg(&stdev->dev, "%s: %p\n", __func__, stuser);
375 static int switchtec_dev_release(struct inode *inode, struct file *filp)
377 struct switchtec_user *stuser = filp->private_data;
384 static int lock_mutex_and_test_alive(struct switchtec_dev *stdev)
386 if (mutex_lock_interruptible(&stdev->mrpc_mutex))
390 mutex_unlock(&stdev->mrpc_mutex);
397 static ssize_t switchtec_dev_write(struct file *filp, const char __user *data,
398 size_t size, loff_t *off)
400 struct switchtec_user *stuser = filp->private_data;
401 struct switchtec_dev *stdev = stuser->stdev;
404 if (size < sizeof(stuser->cmd) ||
405 size > sizeof(stuser->cmd) + sizeof(stuser->data))
408 stuser->data_len = size - sizeof(stuser->cmd);
410 rc = lock_mutex_and_test_alive(stdev);
414 if (stuser->state != MRPC_IDLE) {
419 rc = copy_from_user(&stuser->cmd, data, sizeof(stuser->cmd));
425 data += sizeof(stuser->cmd);
426 rc = copy_from_user(&stuser->data, data, size - sizeof(stuser->cmd));
432 rc = mrpc_queue_cmd(stuser);
435 mutex_unlock(&stdev->mrpc_mutex);
443 static ssize_t switchtec_dev_read(struct file *filp, char __user *data,
444 size_t size, loff_t *off)
446 struct switchtec_user *stuser = filp->private_data;
447 struct switchtec_dev *stdev = stuser->stdev;
450 if (size < sizeof(stuser->cmd) ||
451 size > sizeof(stuser->cmd) + sizeof(stuser->data))
454 rc = lock_mutex_and_test_alive(stdev);
458 if (stuser->state == MRPC_IDLE) {
459 mutex_unlock(&stdev->mrpc_mutex);
463 stuser->read_len = size - sizeof(stuser->return_code);
465 mutex_unlock(&stdev->mrpc_mutex);
467 if (filp->f_flags & O_NONBLOCK) {
468 if (!try_wait_for_completion(&stuser->comp))
471 rc = wait_for_completion_interruptible(&stuser->comp);
476 rc = lock_mutex_and_test_alive(stdev);
480 if (stuser->state != MRPC_DONE) {
481 mutex_unlock(&stdev->mrpc_mutex);
485 rc = copy_to_user(data, &stuser->return_code,
486 sizeof(stuser->return_code));
492 data += sizeof(stuser->return_code);
493 rc = copy_to_user(data, &stuser->data,
494 size - sizeof(stuser->return_code));
500 stuser_set_state(stuser, MRPC_IDLE);
503 mutex_unlock(&stdev->mrpc_mutex);
505 if (stuser->status == SWITCHTEC_MRPC_STATUS_DONE)
507 else if (stuser->status == SWITCHTEC_MRPC_STATUS_INTERRUPTED)
513 static unsigned int switchtec_dev_poll(struct file *filp, poll_table *wait)
515 struct switchtec_user *stuser = filp->private_data;
516 struct switchtec_dev *stdev = stuser->stdev;
519 poll_wait(filp, &stuser->comp.wait, wait);
520 poll_wait(filp, &stdev->event_wq, wait);
522 if (lock_mutex_and_test_alive(stdev))
523 return POLLIN | POLLRDHUP | POLLOUT | POLLERR | POLLHUP;
525 mutex_unlock(&stdev->mrpc_mutex);
527 if (try_wait_for_completion(&stuser->comp))
528 ret |= POLLIN | POLLRDNORM;
530 if (stuser->event_cnt != atomic_read(&stdev->event_cnt))
531 ret |= POLLPRI | POLLRDBAND;
536 static int ioctl_flash_info(struct switchtec_dev *stdev,
537 struct switchtec_ioctl_flash_info __user *uinfo)
539 struct switchtec_ioctl_flash_info info = {0};
540 struct flash_info_regs __iomem *fi = stdev->mmio_flash_info;
542 info.flash_length = ioread32(&fi->flash_length);
543 info.num_partitions = SWITCHTEC_IOCTL_NUM_PARTITIONS;
545 if (copy_to_user(uinfo, &info, sizeof(info)))
551 static void set_fw_info_part(struct switchtec_ioctl_flash_part_info *info,
552 struct partition_info __iomem *pi)
554 info->address = ioread32(&pi->address);
555 info->length = ioread32(&pi->length);
558 static int ioctl_flash_part_info(struct switchtec_dev *stdev,
559 struct switchtec_ioctl_flash_part_info __user *uinfo)
561 struct switchtec_ioctl_flash_part_info info = {0};
562 struct flash_info_regs __iomem *fi = stdev->mmio_flash_info;
563 struct sys_info_regs __iomem *si = stdev->mmio_sys_info;
564 u32 active_addr = -1;
566 if (copy_from_user(&info, uinfo, sizeof(info)))
569 switch (info.flash_partition) {
570 case SWITCHTEC_IOCTL_PART_CFG0:
571 active_addr = ioread32(&fi->active_cfg);
572 set_fw_info_part(&info, &fi->cfg0);
573 if (ioread16(&si->cfg_running) == SWITCHTEC_CFG0_RUNNING)
574 info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
576 case SWITCHTEC_IOCTL_PART_CFG1:
577 active_addr = ioread32(&fi->active_cfg);
578 set_fw_info_part(&info, &fi->cfg1);
579 if (ioread16(&si->cfg_running) == SWITCHTEC_CFG1_RUNNING)
580 info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
582 case SWITCHTEC_IOCTL_PART_IMG0:
583 active_addr = ioread32(&fi->active_img);
584 set_fw_info_part(&info, &fi->img0);
585 if (ioread16(&si->img_running) == SWITCHTEC_IMG0_RUNNING)
586 info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
588 case SWITCHTEC_IOCTL_PART_IMG1:
589 active_addr = ioread32(&fi->active_img);
590 set_fw_info_part(&info, &fi->img1);
591 if (ioread16(&si->img_running) == SWITCHTEC_IMG1_RUNNING)
592 info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
594 case SWITCHTEC_IOCTL_PART_NVLOG:
595 set_fw_info_part(&info, &fi->nvlog);
597 case SWITCHTEC_IOCTL_PART_VENDOR0:
598 set_fw_info_part(&info, &fi->vendor[0]);
600 case SWITCHTEC_IOCTL_PART_VENDOR1:
601 set_fw_info_part(&info, &fi->vendor[1]);
603 case SWITCHTEC_IOCTL_PART_VENDOR2:
604 set_fw_info_part(&info, &fi->vendor[2]);
606 case SWITCHTEC_IOCTL_PART_VENDOR3:
607 set_fw_info_part(&info, &fi->vendor[3]);
609 case SWITCHTEC_IOCTL_PART_VENDOR4:
610 set_fw_info_part(&info, &fi->vendor[4]);
612 case SWITCHTEC_IOCTL_PART_VENDOR5:
613 set_fw_info_part(&info, &fi->vendor[5]);
615 case SWITCHTEC_IOCTL_PART_VENDOR6:
616 set_fw_info_part(&info, &fi->vendor[6]);
618 case SWITCHTEC_IOCTL_PART_VENDOR7:
619 set_fw_info_part(&info, &fi->vendor[7]);
625 if (info.address == active_addr)
626 info.active |= SWITCHTEC_IOCTL_PART_ACTIVE;
628 if (copy_to_user(uinfo, &info, sizeof(info)))
634 static int ioctl_event_summary(struct switchtec_dev *stdev,
635 struct switchtec_user *stuser,
636 struct switchtec_ioctl_event_summary __user *usum)
638 struct switchtec_ioctl_event_summary s = {0};
642 s.global = ioread32(&stdev->mmio_sw_event->global_summary);
643 s.part_bitmap = ioread32(&stdev->mmio_sw_event->part_event_bitmap);
644 s.local_part = ioread32(&stdev->mmio_part_cfg->part_event_summary);
646 for (i = 0; i < stdev->partition_count; i++) {
647 reg = ioread32(&stdev->mmio_part_cfg_all[i].part_event_summary);
651 for (i = 0; i < SWITCHTEC_MAX_PFF_CSR; i++) {
652 reg = ioread16(&stdev->mmio_pff_csr[i].vendor_id);
653 if (reg != MICROSEMI_VENDOR_ID)
656 reg = ioread32(&stdev->mmio_pff_csr[i].pff_event_summary);
660 if (copy_to_user(usum, &s, sizeof(s)))
663 stuser->event_cnt = atomic_read(&stdev->event_cnt);
668 static u32 __iomem *global_ev_reg(struct switchtec_dev *stdev,
669 size_t offset, int index)
671 return (void __iomem *)stdev->mmio_sw_event + offset;
674 static u32 __iomem *part_ev_reg(struct switchtec_dev *stdev,
675 size_t offset, int index)
677 return (void __iomem *)&stdev->mmio_part_cfg_all[index] + offset;
680 static u32 __iomem *pff_ev_reg(struct switchtec_dev *stdev,
681 size_t offset, int index)
683 return (void __iomem *)&stdev->mmio_pff_csr[index] + offset;
686 #define EV_GLB(i, r)[i] = {offsetof(struct sw_event_regs, r), global_ev_reg}
687 #define EV_PAR(i, r)[i] = {offsetof(struct part_cfg_regs, r), part_ev_reg}
688 #define EV_PFF(i, r)[i] = {offsetof(struct pff_csr_regs, r), pff_ev_reg}
690 static const struct event_reg {
692 u32 __iomem *(*map_reg)(struct switchtec_dev *stdev,
693 size_t offset, int index);
695 EV_GLB(SWITCHTEC_IOCTL_EVENT_STACK_ERROR, stack_error_event_hdr),
696 EV_GLB(SWITCHTEC_IOCTL_EVENT_PPU_ERROR, ppu_error_event_hdr),
697 EV_GLB(SWITCHTEC_IOCTL_EVENT_ISP_ERROR, isp_error_event_hdr),
698 EV_GLB(SWITCHTEC_IOCTL_EVENT_SYS_RESET, sys_reset_event_hdr),
699 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_EXC, fw_exception_hdr),
700 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_NMI, fw_nmi_hdr),
701 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_NON_FATAL, fw_non_fatal_hdr),
702 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_FATAL, fw_fatal_hdr),
703 EV_GLB(SWITCHTEC_IOCTL_EVENT_TWI_MRPC_COMP, twi_mrpc_comp_hdr),
704 EV_GLB(SWITCHTEC_IOCTL_EVENT_TWI_MRPC_COMP_ASYNC,
705 twi_mrpc_comp_async_hdr),
706 EV_GLB(SWITCHTEC_IOCTL_EVENT_CLI_MRPC_COMP, cli_mrpc_comp_hdr),
707 EV_GLB(SWITCHTEC_IOCTL_EVENT_CLI_MRPC_COMP_ASYNC,
708 cli_mrpc_comp_async_hdr),
709 EV_GLB(SWITCHTEC_IOCTL_EVENT_GPIO_INT, gpio_interrupt_hdr),
710 EV_PAR(SWITCHTEC_IOCTL_EVENT_PART_RESET, part_reset_hdr),
711 EV_PAR(SWITCHTEC_IOCTL_EVENT_MRPC_COMP, mrpc_comp_hdr),
712 EV_PAR(SWITCHTEC_IOCTL_EVENT_MRPC_COMP_ASYNC, mrpc_comp_async_hdr),
713 EV_PAR(SWITCHTEC_IOCTL_EVENT_DYN_PART_BIND_COMP, dyn_binding_hdr),
714 EV_PFF(SWITCHTEC_IOCTL_EVENT_AER_IN_P2P, aer_in_p2p_hdr),
715 EV_PFF(SWITCHTEC_IOCTL_EVENT_AER_IN_VEP, aer_in_vep_hdr),
716 EV_PFF(SWITCHTEC_IOCTL_EVENT_DPC, dpc_hdr),
717 EV_PFF(SWITCHTEC_IOCTL_EVENT_CTS, cts_hdr),
718 EV_PFF(SWITCHTEC_IOCTL_EVENT_HOTPLUG, hotplug_hdr),
719 EV_PFF(SWITCHTEC_IOCTL_EVENT_IER, ier_hdr),
720 EV_PFF(SWITCHTEC_IOCTL_EVENT_THRESH, threshold_hdr),
721 EV_PFF(SWITCHTEC_IOCTL_EVENT_POWER_MGMT, power_mgmt_hdr),
722 EV_PFF(SWITCHTEC_IOCTL_EVENT_TLP_THROTTLING, tlp_throttling_hdr),
723 EV_PFF(SWITCHTEC_IOCTL_EVENT_FORCE_SPEED, force_speed_hdr),
724 EV_PFF(SWITCHTEC_IOCTL_EVENT_CREDIT_TIMEOUT, credit_timeout_hdr),
725 EV_PFF(SWITCHTEC_IOCTL_EVENT_LINK_STATE, link_state_hdr),
728 static u32 __iomem *event_hdr_addr(struct switchtec_dev *stdev,
729 int event_id, int index)
733 if (event_id < 0 || event_id >= SWITCHTEC_IOCTL_MAX_EVENTS)
734 return ERR_PTR(-EINVAL);
736 off = event_regs[event_id].offset;
738 if (event_regs[event_id].map_reg == part_ev_reg) {
739 if (index == SWITCHTEC_IOCTL_EVENT_LOCAL_PART_IDX)
740 index = stdev->partition;
741 else if (index < 0 || index >= stdev->partition_count)
742 return ERR_PTR(-EINVAL);
743 } else if (event_regs[event_id].map_reg == pff_ev_reg) {
744 if (index < 0 || index >= stdev->pff_csr_count)
745 return ERR_PTR(-EINVAL);
748 return event_regs[event_id].map_reg(stdev, off, index);
751 static int event_ctl(struct switchtec_dev *stdev,
752 struct switchtec_ioctl_event_ctl *ctl)
758 reg = event_hdr_addr(stdev, ctl->event_id, ctl->index);
763 for (i = 0; i < ARRAY_SIZE(ctl->data); i++)
764 ctl->data[i] = ioread32(®[i + 1]);
766 ctl->occurred = hdr & SWITCHTEC_EVENT_OCCURRED;
767 ctl->count = (hdr >> 5) & 0xFF;
769 if (!(ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_CLEAR))
770 hdr &= ~SWITCHTEC_EVENT_CLEAR;
771 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL)
772 hdr |= SWITCHTEC_EVENT_EN_IRQ;
773 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_POLL)
774 hdr &= ~SWITCHTEC_EVENT_EN_IRQ;
775 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG)
776 hdr |= SWITCHTEC_EVENT_EN_LOG;
777 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_LOG)
778 hdr &= ~SWITCHTEC_EVENT_EN_LOG;
779 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI)
780 hdr |= SWITCHTEC_EVENT_EN_CLI;
781 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_CLI)
782 hdr &= ~SWITCHTEC_EVENT_EN_CLI;
783 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL)
784 hdr |= SWITCHTEC_EVENT_FATAL;
785 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_FATAL)
786 hdr &= ~SWITCHTEC_EVENT_FATAL;
792 if (hdr & SWITCHTEC_EVENT_EN_IRQ)
793 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL;
794 if (hdr & SWITCHTEC_EVENT_EN_LOG)
795 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG;
796 if (hdr & SWITCHTEC_EVENT_EN_CLI)
797 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI;
798 if (hdr & SWITCHTEC_EVENT_FATAL)
799 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL;
804 static int ioctl_event_ctl(struct switchtec_dev *stdev,
805 struct switchtec_ioctl_event_ctl __user *uctl)
809 struct switchtec_ioctl_event_ctl ctl;
811 if (copy_from_user(&ctl, uctl, sizeof(ctl)))
814 if (ctl.event_id >= SWITCHTEC_IOCTL_MAX_EVENTS)
817 if (ctl.flags & SWITCHTEC_IOCTL_EVENT_FLAG_UNUSED)
820 if (ctl.index == SWITCHTEC_IOCTL_EVENT_IDX_ALL) {
821 if (event_regs[ctl.event_id].map_reg == global_ev_reg)
823 else if (event_regs[ctl.event_id].map_reg == part_ev_reg)
824 nr_idxs = stdev->partition_count;
825 else if (event_regs[ctl.event_id].map_reg == pff_ev_reg)
826 nr_idxs = stdev->pff_csr_count;
830 for (ctl.index = 0; ctl.index < nr_idxs; ctl.index++) {
831 ret = event_ctl(stdev, &ctl);
836 ret = event_ctl(stdev, &ctl);
841 if (copy_to_user(uctl, &ctl, sizeof(ctl)))
847 static int ioctl_pff_to_port(struct switchtec_dev *stdev,
848 struct switchtec_ioctl_pff_port *up)
852 struct part_cfg_regs *pcfg;
853 struct switchtec_ioctl_pff_port p;
855 if (copy_from_user(&p, up, sizeof(p)))
859 for (part = 0; part < stdev->partition_count; part++) {
860 pcfg = &stdev->mmio_part_cfg_all[part];
863 reg = ioread32(&pcfg->usp_pff_inst_id);
869 reg = ioread32(&pcfg->vep_pff_inst_id);
871 p.port = SWITCHTEC_IOCTL_PFF_VEP;
875 for (i = 0; i < ARRAY_SIZE(pcfg->dsp_pff_inst_id); i++) {
876 reg = ioread32(&pcfg->dsp_pff_inst_id[i]);
888 if (copy_to_user(up, &p, sizeof(p)))
894 static int ioctl_port_to_pff(struct switchtec_dev *stdev,
895 struct switchtec_ioctl_pff_port *up)
897 struct switchtec_ioctl_pff_port p;
898 struct part_cfg_regs *pcfg;
900 if (copy_from_user(&p, up, sizeof(p)))
903 if (p.partition == SWITCHTEC_IOCTL_EVENT_LOCAL_PART_IDX)
904 pcfg = stdev->mmio_part_cfg;
905 else if (p.partition < stdev->partition_count)
906 pcfg = &stdev->mmio_part_cfg_all[p.partition];
912 p.pff = ioread32(&pcfg->usp_pff_inst_id);
914 case SWITCHTEC_IOCTL_PFF_VEP:
915 p.pff = ioread32(&pcfg->vep_pff_inst_id);
918 if (p.port > ARRAY_SIZE(pcfg->dsp_pff_inst_id))
920 p.pff = ioread32(&pcfg->dsp_pff_inst_id[p.port - 1]);
924 if (copy_to_user(up, &p, sizeof(p)))
930 static long switchtec_dev_ioctl(struct file *filp, unsigned int cmd,
933 struct switchtec_user *stuser = filp->private_data;
934 struct switchtec_dev *stdev = stuser->stdev;
936 void __user *argp = (void __user *)arg;
938 rc = lock_mutex_and_test_alive(stdev);
943 case SWITCHTEC_IOCTL_FLASH_INFO:
944 rc = ioctl_flash_info(stdev, argp);
946 case SWITCHTEC_IOCTL_FLASH_PART_INFO:
947 rc = ioctl_flash_part_info(stdev, argp);
949 case SWITCHTEC_IOCTL_EVENT_SUMMARY:
950 rc = ioctl_event_summary(stdev, stuser, argp);
952 case SWITCHTEC_IOCTL_EVENT_CTL:
953 rc = ioctl_event_ctl(stdev, argp);
955 case SWITCHTEC_IOCTL_PFF_TO_PORT:
956 rc = ioctl_pff_to_port(stdev, argp);
958 case SWITCHTEC_IOCTL_PORT_TO_PFF:
959 rc = ioctl_port_to_pff(stdev, argp);
966 mutex_unlock(&stdev->mrpc_mutex);
970 static const struct file_operations switchtec_fops = {
971 .owner = THIS_MODULE,
972 .open = switchtec_dev_open,
973 .release = switchtec_dev_release,
974 .write = switchtec_dev_write,
975 .read = switchtec_dev_read,
976 .poll = switchtec_dev_poll,
977 .unlocked_ioctl = switchtec_dev_ioctl,
978 .compat_ioctl = switchtec_dev_ioctl,
981 static void link_event_work(struct work_struct *work)
983 struct switchtec_dev *stdev;
985 stdev = container_of(work, struct switchtec_dev, link_event_work);
987 if (stdev->link_notifier)
988 stdev->link_notifier(stdev);
991 static void check_link_state_events(struct switchtec_dev *stdev)
998 for (idx = 0; idx < stdev->pff_csr_count; idx++) {
999 reg = ioread32(&stdev->mmio_pff_csr[idx].link_state_hdr);
1000 dev_dbg(&stdev->dev, "link_state: %d->%08x\n", idx, reg);
1001 count = (reg >> 5) & 0xFF;
1003 if (count != stdev->link_event_count[idx]) {
1005 stdev->link_event_count[idx] = count;
1010 schedule_work(&stdev->link_event_work);
1013 static void enable_link_state_events(struct switchtec_dev *stdev)
1017 for (idx = 0; idx < stdev->pff_csr_count; idx++) {
1018 iowrite32(SWITCHTEC_EVENT_CLEAR |
1019 SWITCHTEC_EVENT_EN_IRQ,
1020 &stdev->mmio_pff_csr[idx].link_state_hdr);
1024 static void stdev_release(struct device *dev)
1026 struct switchtec_dev *stdev = to_stdev(dev);
1031 static void stdev_kill(struct switchtec_dev *stdev)
1033 struct switchtec_user *stuser, *tmpuser;
1035 pci_clear_master(stdev->pdev);
1037 cancel_delayed_work_sync(&stdev->mrpc_timeout);
1039 /* Mark the hardware as unavailable and complete all completions */
1040 mutex_lock(&stdev->mrpc_mutex);
1041 stdev->alive = false;
1043 /* Wake up and kill any users waiting on an MRPC request */
1044 list_for_each_entry_safe(stuser, tmpuser, &stdev->mrpc_queue, list) {
1045 complete_all(&stuser->comp);
1046 list_del_init(&stuser->list);
1050 mutex_unlock(&stdev->mrpc_mutex);
1052 /* Wake up any users waiting on event_wq */
1053 wake_up_interruptible(&stdev->event_wq);
1056 static struct switchtec_dev *stdev_create(struct pci_dev *pdev)
1058 struct switchtec_dev *stdev;
1064 stdev = kzalloc_node(sizeof(*stdev), GFP_KERNEL,
1065 dev_to_node(&pdev->dev));
1067 return ERR_PTR(-ENOMEM);
1069 stdev->alive = true;
1071 INIT_LIST_HEAD(&stdev->mrpc_queue);
1072 mutex_init(&stdev->mrpc_mutex);
1073 stdev->mrpc_busy = 0;
1074 INIT_WORK(&stdev->mrpc_work, mrpc_event_work);
1075 INIT_DELAYED_WORK(&stdev->mrpc_timeout, mrpc_timeout_work);
1076 INIT_WORK(&stdev->link_event_work, link_event_work);
1077 init_waitqueue_head(&stdev->event_wq);
1078 atomic_set(&stdev->event_cnt, 0);
1081 device_initialize(dev);
1082 dev->class = switchtec_class;
1083 dev->parent = &pdev->dev;
1084 dev->groups = switchtec_device_groups;
1085 dev->release = stdev_release;
1087 minor = ida_simple_get(&switchtec_minor_ida, 0, 0,
1094 dev->devt = MKDEV(MAJOR(switchtec_devt), minor);
1095 dev_set_name(dev, "switchtec%d", minor);
1097 cdev = &stdev->cdev;
1098 cdev_init(cdev, &switchtec_fops);
1099 cdev->owner = THIS_MODULE;
1104 put_device(&stdev->dev);
1108 static int mask_event(struct switchtec_dev *stdev, int eid, int idx)
1110 size_t off = event_regs[eid].offset;
1111 u32 __iomem *hdr_reg;
1114 hdr_reg = event_regs[eid].map_reg(stdev, off, idx);
1115 hdr = ioread32(hdr_reg);
1117 if (!(hdr & SWITCHTEC_EVENT_OCCURRED && hdr & SWITCHTEC_EVENT_EN_IRQ))
1120 if (eid == SWITCHTEC_IOCTL_EVENT_LINK_STATE)
1123 dev_dbg(&stdev->dev, "%s: %d %d %x\n", __func__, eid, idx, hdr);
1124 hdr &= ~(SWITCHTEC_EVENT_EN_IRQ | SWITCHTEC_EVENT_OCCURRED);
1125 iowrite32(hdr, hdr_reg);
1130 static int mask_all_events(struct switchtec_dev *stdev, int eid)
1135 if (event_regs[eid].map_reg == part_ev_reg) {
1136 for (idx = 0; idx < stdev->partition_count; idx++)
1137 count += mask_event(stdev, eid, idx);
1138 } else if (event_regs[eid].map_reg == pff_ev_reg) {
1139 for (idx = 0; idx < stdev->pff_csr_count; idx++) {
1140 if (!stdev->pff_local[idx])
1143 count += mask_event(stdev, eid, idx);
1146 count += mask_event(stdev, eid, 0);
1152 static irqreturn_t switchtec_event_isr(int irq, void *dev)
1154 struct switchtec_dev *stdev = dev;
1156 irqreturn_t ret = IRQ_NONE;
1157 int eid, event_count = 0;
1159 reg = ioread32(&stdev->mmio_part_cfg->mrpc_comp_hdr);
1160 if (reg & SWITCHTEC_EVENT_OCCURRED) {
1161 dev_dbg(&stdev->dev, "%s: mrpc comp\n", __func__);
1163 schedule_work(&stdev->mrpc_work);
1164 iowrite32(reg, &stdev->mmio_part_cfg->mrpc_comp_hdr);
1167 check_link_state_events(stdev);
1169 for (eid = 0; eid < SWITCHTEC_IOCTL_MAX_EVENTS; eid++)
1170 event_count += mask_all_events(stdev, eid);
1173 atomic_inc(&stdev->event_cnt);
1174 wake_up_interruptible(&stdev->event_wq);
1175 dev_dbg(&stdev->dev, "%s: %d events\n", __func__,
1183 static int switchtec_init_isr(struct switchtec_dev *stdev)
1188 nvecs = pci_alloc_irq_vectors(stdev->pdev, 1, 4,
1189 PCI_IRQ_MSIX | PCI_IRQ_MSI);
1193 event_irq = ioread32(&stdev->mmio_part_cfg->vep_vector_number);
1194 if (event_irq < 0 || event_irq >= nvecs)
1197 event_irq = pci_irq_vector(stdev->pdev, event_irq);
1201 return devm_request_irq(&stdev->pdev->dev, event_irq,
1202 switchtec_event_isr, 0,
1203 KBUILD_MODNAME, stdev);
1206 static void init_pff(struct switchtec_dev *stdev)
1210 struct part_cfg_regs *pcfg = stdev->mmio_part_cfg;
1212 for (i = 0; i < SWITCHTEC_MAX_PFF_CSR; i++) {
1213 reg = ioread16(&stdev->mmio_pff_csr[i].vendor_id);
1214 if (reg != MICROSEMI_VENDOR_ID)
1218 stdev->pff_csr_count = i;
1220 reg = ioread32(&pcfg->usp_pff_inst_id);
1221 if (reg < SWITCHTEC_MAX_PFF_CSR)
1222 stdev->pff_local[reg] = 1;
1224 reg = ioread32(&pcfg->vep_pff_inst_id);
1225 if (reg < SWITCHTEC_MAX_PFF_CSR)
1226 stdev->pff_local[reg] = 1;
1228 for (i = 0; i < ARRAY_SIZE(pcfg->dsp_pff_inst_id); i++) {
1229 reg = ioread32(&pcfg->dsp_pff_inst_id[i]);
1230 if (reg < SWITCHTEC_MAX_PFF_CSR)
1231 stdev->pff_local[reg] = 1;
1235 static int switchtec_init_pci(struct switchtec_dev *stdev,
1236 struct pci_dev *pdev)
1240 rc = pcim_enable_device(pdev);
1244 rc = pcim_iomap_regions(pdev, 0x1, KBUILD_MODNAME);
1248 pci_set_master(pdev);
1250 stdev->mmio = pcim_iomap_table(pdev)[0];
1251 stdev->mmio_mrpc = stdev->mmio + SWITCHTEC_GAS_MRPC_OFFSET;
1252 stdev->mmio_sw_event = stdev->mmio + SWITCHTEC_GAS_SW_EVENT_OFFSET;
1253 stdev->mmio_sys_info = stdev->mmio + SWITCHTEC_GAS_SYS_INFO_OFFSET;
1254 stdev->mmio_flash_info = stdev->mmio + SWITCHTEC_GAS_FLASH_INFO_OFFSET;
1255 stdev->mmio_ntb = stdev->mmio + SWITCHTEC_GAS_NTB_OFFSET;
1256 stdev->partition = ioread8(&stdev->mmio_sys_info->partition_id);
1257 stdev->partition_count = ioread8(&stdev->mmio_ntb->partition_count);
1258 stdev->mmio_part_cfg_all = stdev->mmio + SWITCHTEC_GAS_PART_CFG_OFFSET;
1259 stdev->mmio_part_cfg = &stdev->mmio_part_cfg_all[stdev->partition];
1260 stdev->mmio_pff_csr = stdev->mmio + SWITCHTEC_GAS_PFF_CSR_OFFSET;
1262 if (stdev->partition_count < 1)
1263 stdev->partition_count = 1;
1267 pci_set_drvdata(pdev, stdev);
1272 static int switchtec_pci_probe(struct pci_dev *pdev,
1273 const struct pci_device_id *id)
1275 struct switchtec_dev *stdev;
1278 if (pdev->class == MICROSEMI_NTB_CLASSCODE)
1279 request_module_nowait("ntb_hw_switchtec");
1281 stdev = stdev_create(pdev);
1283 return PTR_ERR(stdev);
1285 rc = switchtec_init_pci(stdev, pdev);
1289 rc = switchtec_init_isr(stdev);
1291 dev_err(&stdev->dev, "failed to init isr.\n");
1295 iowrite32(SWITCHTEC_EVENT_CLEAR |
1296 SWITCHTEC_EVENT_EN_IRQ,
1297 &stdev->mmio_part_cfg->mrpc_comp_hdr);
1298 enable_link_state_events(stdev);
1300 rc = cdev_device_add(&stdev->cdev, &stdev->dev);
1304 dev_info(&stdev->dev, "Management device registered.\n");
1311 ida_simple_remove(&switchtec_minor_ida, MINOR(stdev->dev.devt));
1312 put_device(&stdev->dev);
1316 static void switchtec_pci_remove(struct pci_dev *pdev)
1318 struct switchtec_dev *stdev = pci_get_drvdata(pdev);
1320 pci_set_drvdata(pdev, NULL);
1322 cdev_device_del(&stdev->cdev, &stdev->dev);
1323 ida_simple_remove(&switchtec_minor_ida, MINOR(stdev->dev.devt));
1324 dev_info(&stdev->dev, "unregistered.\n");
1327 put_device(&stdev->dev);
1330 #define SWITCHTEC_PCI_DEVICE(device_id) \
1332 .vendor = MICROSEMI_VENDOR_ID, \
1333 .device = device_id, \
1334 .subvendor = PCI_ANY_ID, \
1335 .subdevice = PCI_ANY_ID, \
1336 .class = MICROSEMI_MGMT_CLASSCODE, \
1337 .class_mask = 0xFFFFFFFF, \
1340 .vendor = MICROSEMI_VENDOR_ID, \
1341 .device = device_id, \
1342 .subvendor = PCI_ANY_ID, \
1343 .subdevice = PCI_ANY_ID, \
1344 .class = MICROSEMI_NTB_CLASSCODE, \
1345 .class_mask = 0xFFFFFFFF, \
1348 static const struct pci_device_id switchtec_pci_tbl[] = {
1349 SWITCHTEC_PCI_DEVICE(0x8531), //PFX 24xG3
1350 SWITCHTEC_PCI_DEVICE(0x8532), //PFX 32xG3
1351 SWITCHTEC_PCI_DEVICE(0x8533), //PFX 48xG3
1352 SWITCHTEC_PCI_DEVICE(0x8534), //PFX 64xG3
1353 SWITCHTEC_PCI_DEVICE(0x8535), //PFX 80xG3
1354 SWITCHTEC_PCI_DEVICE(0x8536), //PFX 96xG3
1355 SWITCHTEC_PCI_DEVICE(0x8543), //PSX 48xG3
1356 SWITCHTEC_PCI_DEVICE(0x8544), //PSX 64xG3
1357 SWITCHTEC_PCI_DEVICE(0x8545), //PSX 80xG3
1358 SWITCHTEC_PCI_DEVICE(0x8546), //PSX 96xG3
1359 SWITCHTEC_PCI_DEVICE(0x8551), //PAX 24XG3
1360 SWITCHTEC_PCI_DEVICE(0x8552), //PAX 32XG3
1361 SWITCHTEC_PCI_DEVICE(0x8553), //PAX 48XG3
1362 SWITCHTEC_PCI_DEVICE(0x8554), //PAX 64XG3
1363 SWITCHTEC_PCI_DEVICE(0x8555), //PAX 80XG3
1364 SWITCHTEC_PCI_DEVICE(0x8556), //PAX 96XG3
1365 SWITCHTEC_PCI_DEVICE(0x8561), //PFXL 24XG3
1366 SWITCHTEC_PCI_DEVICE(0x8562), //PFXL 32XG3
1367 SWITCHTEC_PCI_DEVICE(0x8563), //PFXL 48XG3
1368 SWITCHTEC_PCI_DEVICE(0x8564), //PFXL 64XG3
1369 SWITCHTEC_PCI_DEVICE(0x8565), //PFXL 80XG3
1370 SWITCHTEC_PCI_DEVICE(0x8566), //PFXL 96XG3
1371 SWITCHTEC_PCI_DEVICE(0x8571), //PFXI 24XG3
1372 SWITCHTEC_PCI_DEVICE(0x8572), //PFXI 32XG3
1373 SWITCHTEC_PCI_DEVICE(0x8573), //PFXI 48XG3
1374 SWITCHTEC_PCI_DEVICE(0x8574), //PFXI 64XG3
1375 SWITCHTEC_PCI_DEVICE(0x8575), //PFXI 80XG3
1376 SWITCHTEC_PCI_DEVICE(0x8576), //PFXI 96XG3
1379 MODULE_DEVICE_TABLE(pci, switchtec_pci_tbl);
1381 static struct pci_driver switchtec_pci_driver = {
1382 .name = KBUILD_MODNAME,
1383 .id_table = switchtec_pci_tbl,
1384 .probe = switchtec_pci_probe,
1385 .remove = switchtec_pci_remove,
1388 static int __init switchtec_init(void)
1392 rc = alloc_chrdev_region(&switchtec_devt, 0, max_devices,
1397 switchtec_class = class_create(THIS_MODULE, "switchtec");
1398 if (IS_ERR(switchtec_class)) {
1399 rc = PTR_ERR(switchtec_class);
1400 goto err_create_class;
1403 rc = pci_register_driver(&switchtec_pci_driver);
1405 goto err_pci_register;
1407 pr_info(KBUILD_MODNAME ": loaded.\n");
1412 class_destroy(switchtec_class);
1415 unregister_chrdev_region(switchtec_devt, max_devices);
1419 module_init(switchtec_init);
1421 static void __exit switchtec_exit(void)
1423 pci_unregister_driver(&switchtec_pci_driver);
1424 class_destroy(switchtec_class);
1425 unregister_chrdev_region(switchtec_devt, max_devices);
1426 ida_destroy(&switchtec_minor_ida);
1428 pr_info(KBUILD_MODNAME ": unloaded.\n");
1430 module_exit(switchtec_exit);