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 bool use_dma_mrpc = 1;
29 module_param(use_dma_mrpc, bool, 0644);
30 MODULE_PARM_DESC(use_dma_mrpc,
31 "Enable the use of the DMA MRPC feature");
33 static dev_t switchtec_devt;
34 static DEFINE_IDA(switchtec_minor_ida);
36 struct class *switchtec_class;
37 EXPORT_SYMBOL_GPL(switchtec_class);
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 flush_wc_buf(struct switchtec_dev *stdev)
123 struct ntb_dbmsg_regs __iomem *mmio_dbmsg;
126 * odb (outbound doorbell) register is processed by low latency
127 * hardware and w/o side effect
129 mmio_dbmsg = (void __iomem *)stdev->mmio_ntb +
130 SWITCHTEC_NTB_REG_DBMSG_OFFSET;
131 ioread32(&mmio_dbmsg->odb);
134 static void mrpc_cmd_submit(struct switchtec_dev *stdev)
136 /* requires the mrpc_mutex to already be held when called */
138 struct switchtec_user *stuser;
140 if (stdev->mrpc_busy)
143 if (list_empty(&stdev->mrpc_queue))
146 stuser = list_entry(stdev->mrpc_queue.next, struct switchtec_user,
149 if (stdev->dma_mrpc) {
150 stdev->dma_mrpc->status = SWITCHTEC_MRPC_STATUS_INPROGRESS;
151 memset(stdev->dma_mrpc->data, 0xFF, SWITCHTEC_MRPC_PAYLOAD_SIZE);
154 stuser_set_state(stuser, MRPC_RUNNING);
155 stdev->mrpc_busy = 1;
156 memcpy_toio(&stdev->mmio_mrpc->input_data,
157 stuser->data, stuser->data_len);
159 iowrite32(stuser->cmd, &stdev->mmio_mrpc->cmd);
161 schedule_delayed_work(&stdev->mrpc_timeout,
162 msecs_to_jiffies(500));
165 static int mrpc_queue_cmd(struct switchtec_user *stuser)
167 /* requires the mrpc_mutex to already be held when called */
169 struct switchtec_dev *stdev = stuser->stdev;
171 kref_get(&stuser->kref);
172 stuser->read_len = sizeof(stuser->data);
173 stuser_set_state(stuser, MRPC_QUEUED);
174 init_completion(&stuser->comp);
175 list_add_tail(&stuser->list, &stdev->mrpc_queue);
177 mrpc_cmd_submit(stdev);
182 static void mrpc_complete_cmd(struct switchtec_dev *stdev)
184 /* requires the mrpc_mutex to already be held when called */
185 struct switchtec_user *stuser;
187 if (list_empty(&stdev->mrpc_queue))
190 stuser = list_entry(stdev->mrpc_queue.next, struct switchtec_user,
194 stuser->status = stdev->dma_mrpc->status;
196 stuser->status = ioread32(&stdev->mmio_mrpc->status);
198 if (stuser->status == SWITCHTEC_MRPC_STATUS_INPROGRESS)
201 stuser_set_state(stuser, MRPC_DONE);
202 stuser->return_code = 0;
204 if (stuser->status != SWITCHTEC_MRPC_STATUS_DONE)
208 stuser->return_code = stdev->dma_mrpc->rtn_code;
210 stuser->return_code = ioread32(&stdev->mmio_mrpc->ret_value);
211 if (stuser->return_code != 0)
215 memcpy(stuser->data, &stdev->dma_mrpc->data,
218 memcpy_fromio(stuser->data, &stdev->mmio_mrpc->output_data,
221 complete_all(&stuser->comp);
222 list_del_init(&stuser->list);
224 stdev->mrpc_busy = 0;
226 mrpc_cmd_submit(stdev);
229 static void mrpc_event_work(struct work_struct *work)
231 struct switchtec_dev *stdev;
233 stdev = container_of(work, struct switchtec_dev, mrpc_work);
235 dev_dbg(&stdev->dev, "%s\n", __func__);
237 mutex_lock(&stdev->mrpc_mutex);
238 cancel_delayed_work(&stdev->mrpc_timeout);
239 mrpc_complete_cmd(stdev);
240 mutex_unlock(&stdev->mrpc_mutex);
243 static void mrpc_timeout_work(struct work_struct *work)
245 struct switchtec_dev *stdev;
248 stdev = container_of(work, struct switchtec_dev, mrpc_timeout.work);
250 dev_dbg(&stdev->dev, "%s\n", __func__);
252 mutex_lock(&stdev->mrpc_mutex);
255 status = stdev->dma_mrpc->status;
257 status = ioread32(&stdev->mmio_mrpc->status);
258 if (status == SWITCHTEC_MRPC_STATUS_INPROGRESS) {
259 schedule_delayed_work(&stdev->mrpc_timeout,
260 msecs_to_jiffies(500));
264 mrpc_complete_cmd(stdev);
266 mutex_unlock(&stdev->mrpc_mutex);
269 static ssize_t device_version_show(struct device *dev,
270 struct device_attribute *attr, char *buf)
272 struct switchtec_dev *stdev = to_stdev(dev);
275 ver = ioread32(&stdev->mmio_sys_info->device_version);
277 return sprintf(buf, "%x\n", ver);
279 static DEVICE_ATTR_RO(device_version);
281 static ssize_t fw_version_show(struct device *dev,
282 struct device_attribute *attr, char *buf)
284 struct switchtec_dev *stdev = to_stdev(dev);
287 ver = ioread32(&stdev->mmio_sys_info->firmware_version);
289 return sprintf(buf, "%08x\n", ver);
291 static DEVICE_ATTR_RO(fw_version);
293 static ssize_t io_string_show(char *buf, void __iomem *attr, size_t len)
297 memcpy_fromio(buf, attr, len);
301 for (i = len - 1; i > 0; i--) {
311 #define DEVICE_ATTR_SYS_INFO_STR(field) \
312 static ssize_t field ## _show(struct device *dev, \
313 struct device_attribute *attr, char *buf) \
315 struct switchtec_dev *stdev = to_stdev(dev); \
316 return io_string_show(buf, &stdev->mmio_sys_info->field, \
317 sizeof(stdev->mmio_sys_info->field)); \
320 static DEVICE_ATTR_RO(field)
322 DEVICE_ATTR_SYS_INFO_STR(vendor_id);
323 DEVICE_ATTR_SYS_INFO_STR(product_id);
324 DEVICE_ATTR_SYS_INFO_STR(product_revision);
325 DEVICE_ATTR_SYS_INFO_STR(component_vendor);
327 static ssize_t component_id_show(struct device *dev,
328 struct device_attribute *attr, char *buf)
330 struct switchtec_dev *stdev = to_stdev(dev);
331 int id = ioread16(&stdev->mmio_sys_info->component_id);
333 return sprintf(buf, "PM%04X\n", id);
335 static DEVICE_ATTR_RO(component_id);
337 static ssize_t component_revision_show(struct device *dev,
338 struct device_attribute *attr, char *buf)
340 struct switchtec_dev *stdev = to_stdev(dev);
341 int rev = ioread8(&stdev->mmio_sys_info->component_revision);
343 return sprintf(buf, "%d\n", rev);
345 static DEVICE_ATTR_RO(component_revision);
347 static ssize_t partition_show(struct device *dev,
348 struct device_attribute *attr, char *buf)
350 struct switchtec_dev *stdev = to_stdev(dev);
352 return sprintf(buf, "%d\n", stdev->partition);
354 static DEVICE_ATTR_RO(partition);
356 static ssize_t partition_count_show(struct device *dev,
357 struct device_attribute *attr, char *buf)
359 struct switchtec_dev *stdev = to_stdev(dev);
361 return sprintf(buf, "%d\n", stdev->partition_count);
363 static DEVICE_ATTR_RO(partition_count);
365 static struct attribute *switchtec_device_attrs[] = {
366 &dev_attr_device_version.attr,
367 &dev_attr_fw_version.attr,
368 &dev_attr_vendor_id.attr,
369 &dev_attr_product_id.attr,
370 &dev_attr_product_revision.attr,
371 &dev_attr_component_vendor.attr,
372 &dev_attr_component_id.attr,
373 &dev_attr_component_revision.attr,
374 &dev_attr_partition.attr,
375 &dev_attr_partition_count.attr,
379 ATTRIBUTE_GROUPS(switchtec_device);
381 static int switchtec_dev_open(struct inode *inode, struct file *filp)
383 struct switchtec_dev *stdev;
384 struct switchtec_user *stuser;
386 stdev = container_of(inode->i_cdev, struct switchtec_dev, cdev);
388 stuser = stuser_create(stdev);
390 return PTR_ERR(stuser);
392 filp->private_data = stuser;
393 nonseekable_open(inode, filp);
395 dev_dbg(&stdev->dev, "%s: %p\n", __func__, stuser);
400 static int switchtec_dev_release(struct inode *inode, struct file *filp)
402 struct switchtec_user *stuser = filp->private_data;
409 static int lock_mutex_and_test_alive(struct switchtec_dev *stdev)
411 if (mutex_lock_interruptible(&stdev->mrpc_mutex))
415 mutex_unlock(&stdev->mrpc_mutex);
422 static ssize_t switchtec_dev_write(struct file *filp, const char __user *data,
423 size_t size, loff_t *off)
425 struct switchtec_user *stuser = filp->private_data;
426 struct switchtec_dev *stdev = stuser->stdev;
429 if (size < sizeof(stuser->cmd) ||
430 size > sizeof(stuser->cmd) + sizeof(stuser->data))
433 stuser->data_len = size - sizeof(stuser->cmd);
435 rc = lock_mutex_and_test_alive(stdev);
439 if (stuser->state != MRPC_IDLE) {
444 rc = copy_from_user(&stuser->cmd, data, sizeof(stuser->cmd));
450 data += sizeof(stuser->cmd);
451 rc = copy_from_user(&stuser->data, data, size - sizeof(stuser->cmd));
457 rc = mrpc_queue_cmd(stuser);
460 mutex_unlock(&stdev->mrpc_mutex);
468 static ssize_t switchtec_dev_read(struct file *filp, char __user *data,
469 size_t size, loff_t *off)
471 struct switchtec_user *stuser = filp->private_data;
472 struct switchtec_dev *stdev = stuser->stdev;
475 if (size < sizeof(stuser->cmd) ||
476 size > sizeof(stuser->cmd) + sizeof(stuser->data))
479 rc = lock_mutex_and_test_alive(stdev);
483 if (stuser->state == MRPC_IDLE) {
484 mutex_unlock(&stdev->mrpc_mutex);
488 stuser->read_len = size - sizeof(stuser->return_code);
490 mutex_unlock(&stdev->mrpc_mutex);
492 if (filp->f_flags & O_NONBLOCK) {
493 if (!try_wait_for_completion(&stuser->comp))
496 rc = wait_for_completion_interruptible(&stuser->comp);
501 rc = lock_mutex_and_test_alive(stdev);
505 if (stuser->state != MRPC_DONE) {
506 mutex_unlock(&stdev->mrpc_mutex);
510 rc = copy_to_user(data, &stuser->return_code,
511 sizeof(stuser->return_code));
517 data += sizeof(stuser->return_code);
518 rc = copy_to_user(data, &stuser->data,
519 size - sizeof(stuser->return_code));
525 stuser_set_state(stuser, MRPC_IDLE);
528 mutex_unlock(&stdev->mrpc_mutex);
530 if (stuser->status == SWITCHTEC_MRPC_STATUS_DONE)
532 else if (stuser->status == SWITCHTEC_MRPC_STATUS_INTERRUPTED)
538 static __poll_t switchtec_dev_poll(struct file *filp, poll_table *wait)
540 struct switchtec_user *stuser = filp->private_data;
541 struct switchtec_dev *stdev = stuser->stdev;
544 poll_wait(filp, &stuser->comp.wait, wait);
545 poll_wait(filp, &stdev->event_wq, wait);
547 if (lock_mutex_and_test_alive(stdev))
548 return EPOLLIN | EPOLLRDHUP | EPOLLOUT | EPOLLERR | EPOLLHUP;
550 mutex_unlock(&stdev->mrpc_mutex);
552 if (try_wait_for_completion(&stuser->comp))
553 ret |= EPOLLIN | EPOLLRDNORM;
555 if (stuser->event_cnt != atomic_read(&stdev->event_cnt))
556 ret |= EPOLLPRI | EPOLLRDBAND;
561 static int ioctl_flash_info(struct switchtec_dev *stdev,
562 struct switchtec_ioctl_flash_info __user *uinfo)
564 struct switchtec_ioctl_flash_info info = {0};
565 struct flash_info_regs __iomem *fi = stdev->mmio_flash_info;
567 info.flash_length = ioread32(&fi->flash_length);
568 info.num_partitions = SWITCHTEC_IOCTL_NUM_PARTITIONS;
570 if (copy_to_user(uinfo, &info, sizeof(info)))
576 static void set_fw_info_part(struct switchtec_ioctl_flash_part_info *info,
577 struct partition_info __iomem *pi)
579 info->address = ioread32(&pi->address);
580 info->length = ioread32(&pi->length);
583 static int ioctl_flash_part_info(struct switchtec_dev *stdev,
584 struct switchtec_ioctl_flash_part_info __user *uinfo)
586 struct switchtec_ioctl_flash_part_info info = {0};
587 struct flash_info_regs __iomem *fi = stdev->mmio_flash_info;
588 struct sys_info_regs __iomem *si = stdev->mmio_sys_info;
589 u32 active_addr = -1;
591 if (copy_from_user(&info, uinfo, sizeof(info)))
594 switch (info.flash_partition) {
595 case SWITCHTEC_IOCTL_PART_CFG0:
596 active_addr = ioread32(&fi->active_cfg);
597 set_fw_info_part(&info, &fi->cfg0);
598 if (ioread16(&si->cfg_running) == SWITCHTEC_CFG0_RUNNING)
599 info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
601 case SWITCHTEC_IOCTL_PART_CFG1:
602 active_addr = ioread32(&fi->active_cfg);
603 set_fw_info_part(&info, &fi->cfg1);
604 if (ioread16(&si->cfg_running) == SWITCHTEC_CFG1_RUNNING)
605 info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
607 case SWITCHTEC_IOCTL_PART_IMG0:
608 active_addr = ioread32(&fi->active_img);
609 set_fw_info_part(&info, &fi->img0);
610 if (ioread16(&si->img_running) == SWITCHTEC_IMG0_RUNNING)
611 info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
613 case SWITCHTEC_IOCTL_PART_IMG1:
614 active_addr = ioread32(&fi->active_img);
615 set_fw_info_part(&info, &fi->img1);
616 if (ioread16(&si->img_running) == SWITCHTEC_IMG1_RUNNING)
617 info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
619 case SWITCHTEC_IOCTL_PART_NVLOG:
620 set_fw_info_part(&info, &fi->nvlog);
622 case SWITCHTEC_IOCTL_PART_VENDOR0:
623 set_fw_info_part(&info, &fi->vendor[0]);
625 case SWITCHTEC_IOCTL_PART_VENDOR1:
626 set_fw_info_part(&info, &fi->vendor[1]);
628 case SWITCHTEC_IOCTL_PART_VENDOR2:
629 set_fw_info_part(&info, &fi->vendor[2]);
631 case SWITCHTEC_IOCTL_PART_VENDOR3:
632 set_fw_info_part(&info, &fi->vendor[3]);
634 case SWITCHTEC_IOCTL_PART_VENDOR4:
635 set_fw_info_part(&info, &fi->vendor[4]);
637 case SWITCHTEC_IOCTL_PART_VENDOR5:
638 set_fw_info_part(&info, &fi->vendor[5]);
640 case SWITCHTEC_IOCTL_PART_VENDOR6:
641 set_fw_info_part(&info, &fi->vendor[6]);
643 case SWITCHTEC_IOCTL_PART_VENDOR7:
644 set_fw_info_part(&info, &fi->vendor[7]);
650 if (info.address == active_addr)
651 info.active |= SWITCHTEC_IOCTL_PART_ACTIVE;
653 if (copy_to_user(uinfo, &info, sizeof(info)))
659 static int ioctl_event_summary(struct switchtec_dev *stdev,
660 struct switchtec_user *stuser,
661 struct switchtec_ioctl_event_summary __user *usum)
663 struct switchtec_ioctl_event_summary s = {0};
667 s.global = ioread32(&stdev->mmio_sw_event->global_summary);
668 s.part_bitmap = ioread32(&stdev->mmio_sw_event->part_event_bitmap);
669 s.local_part = ioread32(&stdev->mmio_part_cfg->part_event_summary);
671 for (i = 0; i < stdev->partition_count; i++) {
672 reg = ioread32(&stdev->mmio_part_cfg_all[i].part_event_summary);
676 for (i = 0; i < SWITCHTEC_MAX_PFF_CSR; i++) {
677 reg = ioread16(&stdev->mmio_pff_csr[i].vendor_id);
678 if (reg != PCI_VENDOR_ID_MICROSEMI)
681 reg = ioread32(&stdev->mmio_pff_csr[i].pff_event_summary);
685 if (copy_to_user(usum, &s, sizeof(s)))
688 stuser->event_cnt = atomic_read(&stdev->event_cnt);
693 static u32 __iomem *global_ev_reg(struct switchtec_dev *stdev,
694 size_t offset, int index)
696 return (void __iomem *)stdev->mmio_sw_event + offset;
699 static u32 __iomem *part_ev_reg(struct switchtec_dev *stdev,
700 size_t offset, int index)
702 return (void __iomem *)&stdev->mmio_part_cfg_all[index] + offset;
705 static u32 __iomem *pff_ev_reg(struct switchtec_dev *stdev,
706 size_t offset, int index)
708 return (void __iomem *)&stdev->mmio_pff_csr[index] + offset;
711 #define EV_GLB(i, r)[i] = {offsetof(struct sw_event_regs, r), global_ev_reg}
712 #define EV_PAR(i, r)[i] = {offsetof(struct part_cfg_regs, r), part_ev_reg}
713 #define EV_PFF(i, r)[i] = {offsetof(struct pff_csr_regs, r), pff_ev_reg}
715 static const struct event_reg {
717 u32 __iomem *(*map_reg)(struct switchtec_dev *stdev,
718 size_t offset, int index);
720 EV_GLB(SWITCHTEC_IOCTL_EVENT_STACK_ERROR, stack_error_event_hdr),
721 EV_GLB(SWITCHTEC_IOCTL_EVENT_PPU_ERROR, ppu_error_event_hdr),
722 EV_GLB(SWITCHTEC_IOCTL_EVENT_ISP_ERROR, isp_error_event_hdr),
723 EV_GLB(SWITCHTEC_IOCTL_EVENT_SYS_RESET, sys_reset_event_hdr),
724 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_EXC, fw_exception_hdr),
725 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_NMI, fw_nmi_hdr),
726 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_NON_FATAL, fw_non_fatal_hdr),
727 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_FATAL, fw_fatal_hdr),
728 EV_GLB(SWITCHTEC_IOCTL_EVENT_TWI_MRPC_COMP, twi_mrpc_comp_hdr),
729 EV_GLB(SWITCHTEC_IOCTL_EVENT_TWI_MRPC_COMP_ASYNC,
730 twi_mrpc_comp_async_hdr),
731 EV_GLB(SWITCHTEC_IOCTL_EVENT_CLI_MRPC_COMP, cli_mrpc_comp_hdr),
732 EV_GLB(SWITCHTEC_IOCTL_EVENT_CLI_MRPC_COMP_ASYNC,
733 cli_mrpc_comp_async_hdr),
734 EV_GLB(SWITCHTEC_IOCTL_EVENT_GPIO_INT, gpio_interrupt_hdr),
735 EV_GLB(SWITCHTEC_IOCTL_EVENT_GFMS, gfms_event_hdr),
736 EV_PAR(SWITCHTEC_IOCTL_EVENT_PART_RESET, part_reset_hdr),
737 EV_PAR(SWITCHTEC_IOCTL_EVENT_MRPC_COMP, mrpc_comp_hdr),
738 EV_PAR(SWITCHTEC_IOCTL_EVENT_MRPC_COMP_ASYNC, mrpc_comp_async_hdr),
739 EV_PAR(SWITCHTEC_IOCTL_EVENT_DYN_PART_BIND_COMP, dyn_binding_hdr),
740 EV_PFF(SWITCHTEC_IOCTL_EVENT_AER_IN_P2P, aer_in_p2p_hdr),
741 EV_PFF(SWITCHTEC_IOCTL_EVENT_AER_IN_VEP, aer_in_vep_hdr),
742 EV_PFF(SWITCHTEC_IOCTL_EVENT_DPC, dpc_hdr),
743 EV_PFF(SWITCHTEC_IOCTL_EVENT_CTS, cts_hdr),
744 EV_PFF(SWITCHTEC_IOCTL_EVENT_HOTPLUG, hotplug_hdr),
745 EV_PFF(SWITCHTEC_IOCTL_EVENT_IER, ier_hdr),
746 EV_PFF(SWITCHTEC_IOCTL_EVENT_THRESH, threshold_hdr),
747 EV_PFF(SWITCHTEC_IOCTL_EVENT_POWER_MGMT, power_mgmt_hdr),
748 EV_PFF(SWITCHTEC_IOCTL_EVENT_TLP_THROTTLING, tlp_throttling_hdr),
749 EV_PFF(SWITCHTEC_IOCTL_EVENT_FORCE_SPEED, force_speed_hdr),
750 EV_PFF(SWITCHTEC_IOCTL_EVENT_CREDIT_TIMEOUT, credit_timeout_hdr),
751 EV_PFF(SWITCHTEC_IOCTL_EVENT_LINK_STATE, link_state_hdr),
754 static u32 __iomem *event_hdr_addr(struct switchtec_dev *stdev,
755 int event_id, int index)
759 if (event_id < 0 || event_id >= SWITCHTEC_IOCTL_MAX_EVENTS)
760 return ERR_PTR(-EINVAL);
762 off = event_regs[event_id].offset;
764 if (event_regs[event_id].map_reg == part_ev_reg) {
765 if (index == SWITCHTEC_IOCTL_EVENT_LOCAL_PART_IDX)
766 index = stdev->partition;
767 else if (index < 0 || index >= stdev->partition_count)
768 return ERR_PTR(-EINVAL);
769 } else if (event_regs[event_id].map_reg == pff_ev_reg) {
770 if (index < 0 || index >= stdev->pff_csr_count)
771 return ERR_PTR(-EINVAL);
774 return event_regs[event_id].map_reg(stdev, off, index);
777 static int event_ctl(struct switchtec_dev *stdev,
778 struct switchtec_ioctl_event_ctl *ctl)
784 reg = event_hdr_addr(stdev, ctl->event_id, ctl->index);
789 for (i = 0; i < ARRAY_SIZE(ctl->data); i++)
790 ctl->data[i] = ioread32(®[i + 1]);
792 ctl->occurred = hdr & SWITCHTEC_EVENT_OCCURRED;
793 ctl->count = (hdr >> 5) & 0xFF;
795 if (!(ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_CLEAR))
796 hdr &= ~SWITCHTEC_EVENT_CLEAR;
797 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL)
798 hdr |= SWITCHTEC_EVENT_EN_IRQ;
799 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_POLL)
800 hdr &= ~SWITCHTEC_EVENT_EN_IRQ;
801 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG)
802 hdr |= SWITCHTEC_EVENT_EN_LOG;
803 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_LOG)
804 hdr &= ~SWITCHTEC_EVENT_EN_LOG;
805 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI)
806 hdr |= SWITCHTEC_EVENT_EN_CLI;
807 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_CLI)
808 hdr &= ~SWITCHTEC_EVENT_EN_CLI;
809 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL)
810 hdr |= SWITCHTEC_EVENT_FATAL;
811 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_FATAL)
812 hdr &= ~SWITCHTEC_EVENT_FATAL;
818 if (hdr & SWITCHTEC_EVENT_EN_IRQ)
819 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL;
820 if (hdr & SWITCHTEC_EVENT_EN_LOG)
821 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG;
822 if (hdr & SWITCHTEC_EVENT_EN_CLI)
823 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI;
824 if (hdr & SWITCHTEC_EVENT_FATAL)
825 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL;
830 static int ioctl_event_ctl(struct switchtec_dev *stdev,
831 struct switchtec_ioctl_event_ctl __user *uctl)
835 unsigned int event_flags;
836 struct switchtec_ioctl_event_ctl ctl;
838 if (copy_from_user(&ctl, uctl, sizeof(ctl)))
841 if (ctl.event_id >= SWITCHTEC_IOCTL_MAX_EVENTS)
844 if (ctl.flags & SWITCHTEC_IOCTL_EVENT_FLAG_UNUSED)
847 if (ctl.index == SWITCHTEC_IOCTL_EVENT_IDX_ALL) {
848 if (event_regs[ctl.event_id].map_reg == global_ev_reg)
850 else if (event_regs[ctl.event_id].map_reg == part_ev_reg)
851 nr_idxs = stdev->partition_count;
852 else if (event_regs[ctl.event_id].map_reg == pff_ev_reg)
853 nr_idxs = stdev->pff_csr_count;
857 event_flags = ctl.flags;
858 for (ctl.index = 0; ctl.index < nr_idxs; ctl.index++) {
859 ctl.flags = event_flags;
860 ret = event_ctl(stdev, &ctl);
865 ret = event_ctl(stdev, &ctl);
870 if (copy_to_user(uctl, &ctl, sizeof(ctl)))
876 static int ioctl_pff_to_port(struct switchtec_dev *stdev,
877 struct switchtec_ioctl_pff_port *up)
881 struct part_cfg_regs *pcfg;
882 struct switchtec_ioctl_pff_port p;
884 if (copy_from_user(&p, up, sizeof(p)))
888 for (part = 0; part < stdev->partition_count; part++) {
889 pcfg = &stdev->mmio_part_cfg_all[part];
892 reg = ioread32(&pcfg->usp_pff_inst_id);
898 reg = ioread32(&pcfg->vep_pff_inst_id);
900 p.port = SWITCHTEC_IOCTL_PFF_VEP;
904 for (i = 0; i < ARRAY_SIZE(pcfg->dsp_pff_inst_id); i++) {
905 reg = ioread32(&pcfg->dsp_pff_inst_id[i]);
917 if (copy_to_user(up, &p, sizeof(p)))
923 static int ioctl_port_to_pff(struct switchtec_dev *stdev,
924 struct switchtec_ioctl_pff_port *up)
926 struct switchtec_ioctl_pff_port p;
927 struct part_cfg_regs *pcfg;
929 if (copy_from_user(&p, up, sizeof(p)))
932 if (p.partition == SWITCHTEC_IOCTL_EVENT_LOCAL_PART_IDX)
933 pcfg = stdev->mmio_part_cfg;
934 else if (p.partition < stdev->partition_count)
935 pcfg = &stdev->mmio_part_cfg_all[p.partition];
941 p.pff = ioread32(&pcfg->usp_pff_inst_id);
943 case SWITCHTEC_IOCTL_PFF_VEP:
944 p.pff = ioread32(&pcfg->vep_pff_inst_id);
947 if (p.port > ARRAY_SIZE(pcfg->dsp_pff_inst_id))
949 p.port = array_index_nospec(p.port,
950 ARRAY_SIZE(pcfg->dsp_pff_inst_id) + 1);
951 p.pff = ioread32(&pcfg->dsp_pff_inst_id[p.port - 1]);
955 if (copy_to_user(up, &p, sizeof(p)))
961 static long switchtec_dev_ioctl(struct file *filp, unsigned int cmd,
964 struct switchtec_user *stuser = filp->private_data;
965 struct switchtec_dev *stdev = stuser->stdev;
967 void __user *argp = (void __user *)arg;
969 rc = lock_mutex_and_test_alive(stdev);
974 case SWITCHTEC_IOCTL_FLASH_INFO:
975 rc = ioctl_flash_info(stdev, argp);
977 case SWITCHTEC_IOCTL_FLASH_PART_INFO:
978 rc = ioctl_flash_part_info(stdev, argp);
980 case SWITCHTEC_IOCTL_EVENT_SUMMARY:
981 rc = ioctl_event_summary(stdev, stuser, argp);
983 case SWITCHTEC_IOCTL_EVENT_CTL:
984 rc = ioctl_event_ctl(stdev, argp);
986 case SWITCHTEC_IOCTL_PFF_TO_PORT:
987 rc = ioctl_pff_to_port(stdev, argp);
989 case SWITCHTEC_IOCTL_PORT_TO_PFF:
990 rc = ioctl_port_to_pff(stdev, argp);
997 mutex_unlock(&stdev->mrpc_mutex);
1001 static const struct file_operations switchtec_fops = {
1002 .owner = THIS_MODULE,
1003 .open = switchtec_dev_open,
1004 .release = switchtec_dev_release,
1005 .write = switchtec_dev_write,
1006 .read = switchtec_dev_read,
1007 .poll = switchtec_dev_poll,
1008 .unlocked_ioctl = switchtec_dev_ioctl,
1009 .compat_ioctl = switchtec_dev_ioctl,
1012 static void link_event_work(struct work_struct *work)
1014 struct switchtec_dev *stdev;
1016 stdev = container_of(work, struct switchtec_dev, link_event_work);
1018 if (stdev->link_notifier)
1019 stdev->link_notifier(stdev);
1022 static void check_link_state_events(struct switchtec_dev *stdev)
1029 for (idx = 0; idx < stdev->pff_csr_count; idx++) {
1030 reg = ioread32(&stdev->mmio_pff_csr[idx].link_state_hdr);
1031 dev_dbg(&stdev->dev, "link_state: %d->%08x\n", idx, reg);
1032 count = (reg >> 5) & 0xFF;
1034 if (count != stdev->link_event_count[idx]) {
1036 stdev->link_event_count[idx] = count;
1041 schedule_work(&stdev->link_event_work);
1044 static void enable_link_state_events(struct switchtec_dev *stdev)
1048 for (idx = 0; idx < stdev->pff_csr_count; idx++) {
1049 iowrite32(SWITCHTEC_EVENT_CLEAR |
1050 SWITCHTEC_EVENT_EN_IRQ,
1051 &stdev->mmio_pff_csr[idx].link_state_hdr);
1055 static void enable_dma_mrpc(struct switchtec_dev *stdev)
1057 writeq(stdev->dma_mrpc_dma_addr, &stdev->mmio_mrpc->dma_addr);
1058 flush_wc_buf(stdev);
1059 iowrite32(SWITCHTEC_DMA_MRPC_EN, &stdev->mmio_mrpc->dma_en);
1062 static void stdev_release(struct device *dev)
1064 struct switchtec_dev *stdev = to_stdev(dev);
1066 if (stdev->dma_mrpc) {
1067 iowrite32(0, &stdev->mmio_mrpc->dma_en);
1068 flush_wc_buf(stdev);
1069 writeq(0, &stdev->mmio_mrpc->dma_addr);
1070 dma_free_coherent(&stdev->pdev->dev, sizeof(*stdev->dma_mrpc),
1071 stdev->dma_mrpc, stdev->dma_mrpc_dma_addr);
1076 static void stdev_kill(struct switchtec_dev *stdev)
1078 struct switchtec_user *stuser, *tmpuser;
1080 pci_clear_master(stdev->pdev);
1082 cancel_delayed_work_sync(&stdev->mrpc_timeout);
1084 /* Mark the hardware as unavailable and complete all completions */
1085 mutex_lock(&stdev->mrpc_mutex);
1086 stdev->alive = false;
1088 /* Wake up and kill any users waiting on an MRPC request */
1089 list_for_each_entry_safe(stuser, tmpuser, &stdev->mrpc_queue, list) {
1090 complete_all(&stuser->comp);
1091 list_del_init(&stuser->list);
1095 mutex_unlock(&stdev->mrpc_mutex);
1097 /* Wake up any users waiting on event_wq */
1098 wake_up_interruptible(&stdev->event_wq);
1101 static struct switchtec_dev *stdev_create(struct pci_dev *pdev)
1103 struct switchtec_dev *stdev;
1109 stdev = kzalloc_node(sizeof(*stdev), GFP_KERNEL,
1110 dev_to_node(&pdev->dev));
1112 return ERR_PTR(-ENOMEM);
1114 stdev->alive = true;
1116 INIT_LIST_HEAD(&stdev->mrpc_queue);
1117 mutex_init(&stdev->mrpc_mutex);
1118 stdev->mrpc_busy = 0;
1119 INIT_WORK(&stdev->mrpc_work, mrpc_event_work);
1120 INIT_DELAYED_WORK(&stdev->mrpc_timeout, mrpc_timeout_work);
1121 INIT_WORK(&stdev->link_event_work, link_event_work);
1122 init_waitqueue_head(&stdev->event_wq);
1123 atomic_set(&stdev->event_cnt, 0);
1126 device_initialize(dev);
1127 dev->class = switchtec_class;
1128 dev->parent = &pdev->dev;
1129 dev->groups = switchtec_device_groups;
1130 dev->release = stdev_release;
1132 minor = ida_simple_get(&switchtec_minor_ida, 0, 0,
1139 dev->devt = MKDEV(MAJOR(switchtec_devt), minor);
1140 dev_set_name(dev, "switchtec%d", minor);
1142 cdev = &stdev->cdev;
1143 cdev_init(cdev, &switchtec_fops);
1144 cdev->owner = THIS_MODULE;
1149 put_device(&stdev->dev);
1153 static int mask_event(struct switchtec_dev *stdev, int eid, int idx)
1155 size_t off = event_regs[eid].offset;
1156 u32 __iomem *hdr_reg;
1159 hdr_reg = event_regs[eid].map_reg(stdev, off, idx);
1160 hdr = ioread32(hdr_reg);
1162 if (!(hdr & SWITCHTEC_EVENT_OCCURRED && hdr & SWITCHTEC_EVENT_EN_IRQ))
1165 if (eid == SWITCHTEC_IOCTL_EVENT_LINK_STATE)
1168 dev_dbg(&stdev->dev, "%s: %d %d %x\n", __func__, eid, idx, hdr);
1169 hdr &= ~(SWITCHTEC_EVENT_EN_IRQ | SWITCHTEC_EVENT_OCCURRED);
1170 iowrite32(hdr, hdr_reg);
1175 static int mask_all_events(struct switchtec_dev *stdev, int eid)
1180 if (event_regs[eid].map_reg == part_ev_reg) {
1181 for (idx = 0; idx < stdev->partition_count; idx++)
1182 count += mask_event(stdev, eid, idx);
1183 } else if (event_regs[eid].map_reg == pff_ev_reg) {
1184 for (idx = 0; idx < stdev->pff_csr_count; idx++) {
1185 if (!stdev->pff_local[idx])
1188 count += mask_event(stdev, eid, idx);
1191 count += mask_event(stdev, eid, 0);
1197 static irqreturn_t switchtec_event_isr(int irq, void *dev)
1199 struct switchtec_dev *stdev = dev;
1201 irqreturn_t ret = IRQ_NONE;
1202 int eid, event_count = 0;
1204 reg = ioread32(&stdev->mmio_part_cfg->mrpc_comp_hdr);
1205 if (reg & SWITCHTEC_EVENT_OCCURRED) {
1206 dev_dbg(&stdev->dev, "%s: mrpc comp\n", __func__);
1208 schedule_work(&stdev->mrpc_work);
1209 iowrite32(reg, &stdev->mmio_part_cfg->mrpc_comp_hdr);
1212 check_link_state_events(stdev);
1214 for (eid = 0; eid < SWITCHTEC_IOCTL_MAX_EVENTS; eid++)
1215 event_count += mask_all_events(stdev, eid);
1218 atomic_inc(&stdev->event_cnt);
1219 wake_up_interruptible(&stdev->event_wq);
1220 dev_dbg(&stdev->dev, "%s: %d events\n", __func__,
1229 static irqreturn_t switchtec_dma_mrpc_isr(int irq, void *dev)
1231 struct switchtec_dev *stdev = dev;
1232 irqreturn_t ret = IRQ_NONE;
1234 iowrite32(SWITCHTEC_EVENT_CLEAR |
1235 SWITCHTEC_EVENT_EN_IRQ,
1236 &stdev->mmio_part_cfg->mrpc_comp_hdr);
1237 schedule_work(&stdev->mrpc_work);
1243 static int switchtec_init_isr(struct switchtec_dev *stdev)
1250 nvecs = pci_alloc_irq_vectors(stdev->pdev, 1, 4,
1251 PCI_IRQ_MSIX | PCI_IRQ_MSI);
1255 event_irq = ioread32(&stdev->mmio_part_cfg->vep_vector_number);
1256 if (event_irq < 0 || event_irq >= nvecs)
1259 event_irq = pci_irq_vector(stdev->pdev, event_irq);
1263 rc = devm_request_irq(&stdev->pdev->dev, event_irq,
1264 switchtec_event_isr, 0,
1265 KBUILD_MODNAME, stdev);
1270 if (!stdev->dma_mrpc)
1273 dma_mrpc_irq = ioread32(&stdev->mmio_mrpc->dma_vector);
1274 if (dma_mrpc_irq < 0 || dma_mrpc_irq >= nvecs)
1277 dma_mrpc_irq = pci_irq_vector(stdev->pdev, dma_mrpc_irq);
1278 if (dma_mrpc_irq < 0)
1279 return dma_mrpc_irq;
1281 rc = devm_request_irq(&stdev->pdev->dev, dma_mrpc_irq,
1282 switchtec_dma_mrpc_isr, 0,
1283 KBUILD_MODNAME, stdev);
1288 static void init_pff(struct switchtec_dev *stdev)
1292 struct part_cfg_regs *pcfg = stdev->mmio_part_cfg;
1294 for (i = 0; i < SWITCHTEC_MAX_PFF_CSR; i++) {
1295 reg = ioread16(&stdev->mmio_pff_csr[i].vendor_id);
1296 if (reg != PCI_VENDOR_ID_MICROSEMI)
1300 stdev->pff_csr_count = i;
1302 reg = ioread32(&pcfg->usp_pff_inst_id);
1303 if (reg < SWITCHTEC_MAX_PFF_CSR)
1304 stdev->pff_local[reg] = 1;
1306 reg = ioread32(&pcfg->vep_pff_inst_id);
1307 if (reg < SWITCHTEC_MAX_PFF_CSR)
1308 stdev->pff_local[reg] = 1;
1310 for (i = 0; i < ARRAY_SIZE(pcfg->dsp_pff_inst_id); i++) {
1311 reg = ioread32(&pcfg->dsp_pff_inst_id[i]);
1312 if (reg < SWITCHTEC_MAX_PFF_CSR)
1313 stdev->pff_local[reg] = 1;
1317 static int switchtec_init_pci(struct switchtec_dev *stdev,
1318 struct pci_dev *pdev)
1322 unsigned long res_start, res_len;
1324 rc = pcim_enable_device(pdev);
1328 rc = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
1332 pci_set_master(pdev);
1334 res_start = pci_resource_start(pdev, 0);
1335 res_len = pci_resource_len(pdev, 0);
1337 if (!devm_request_mem_region(&pdev->dev, res_start,
1338 res_len, KBUILD_MODNAME))
1341 stdev->mmio_mrpc = devm_ioremap_wc(&pdev->dev, res_start,
1342 SWITCHTEC_GAS_TOP_CFG_OFFSET);
1343 if (!stdev->mmio_mrpc)
1346 map = devm_ioremap(&pdev->dev,
1347 res_start + SWITCHTEC_GAS_TOP_CFG_OFFSET,
1348 res_len - SWITCHTEC_GAS_TOP_CFG_OFFSET);
1352 stdev->mmio = map - SWITCHTEC_GAS_TOP_CFG_OFFSET;
1353 stdev->mmio_sw_event = stdev->mmio + SWITCHTEC_GAS_SW_EVENT_OFFSET;
1354 stdev->mmio_sys_info = stdev->mmio + SWITCHTEC_GAS_SYS_INFO_OFFSET;
1355 stdev->mmio_flash_info = stdev->mmio + SWITCHTEC_GAS_FLASH_INFO_OFFSET;
1356 stdev->mmio_ntb = stdev->mmio + SWITCHTEC_GAS_NTB_OFFSET;
1357 stdev->partition = ioread8(&stdev->mmio_sys_info->partition_id);
1358 stdev->partition_count = ioread8(&stdev->mmio_ntb->partition_count);
1359 stdev->mmio_part_cfg_all = stdev->mmio + SWITCHTEC_GAS_PART_CFG_OFFSET;
1360 stdev->mmio_part_cfg = &stdev->mmio_part_cfg_all[stdev->partition];
1361 stdev->mmio_pff_csr = stdev->mmio + SWITCHTEC_GAS_PFF_CSR_OFFSET;
1363 if (stdev->partition_count < 1)
1364 stdev->partition_count = 1;
1368 pci_set_drvdata(pdev, stdev);
1373 if (ioread32(&stdev->mmio_mrpc->dma_ver) == 0)
1376 stdev->dma_mrpc = dma_alloc_coherent(&stdev->pdev->dev,
1377 sizeof(*stdev->dma_mrpc),
1378 &stdev->dma_mrpc_dma_addr,
1380 if (stdev->dma_mrpc == NULL)
1386 static int switchtec_pci_probe(struct pci_dev *pdev,
1387 const struct pci_device_id *id)
1389 struct switchtec_dev *stdev;
1392 if (pdev->class == (PCI_CLASS_BRIDGE_OTHER << 8))
1393 request_module_nowait("ntb_hw_switchtec");
1395 stdev = stdev_create(pdev);
1397 return PTR_ERR(stdev);
1399 rc = switchtec_init_pci(stdev, pdev);
1403 rc = switchtec_init_isr(stdev);
1405 dev_err(&stdev->dev, "failed to init isr.\n");
1409 iowrite32(SWITCHTEC_EVENT_CLEAR |
1410 SWITCHTEC_EVENT_EN_IRQ,
1411 &stdev->mmio_part_cfg->mrpc_comp_hdr);
1412 enable_link_state_events(stdev);
1414 if (stdev->dma_mrpc)
1415 enable_dma_mrpc(stdev);
1417 rc = cdev_device_add(&stdev->cdev, &stdev->dev);
1421 dev_info(&stdev->dev, "Management device registered.\n");
1428 ida_simple_remove(&switchtec_minor_ida, MINOR(stdev->dev.devt));
1429 put_device(&stdev->dev);
1433 static void switchtec_pci_remove(struct pci_dev *pdev)
1435 struct switchtec_dev *stdev = pci_get_drvdata(pdev);
1437 pci_set_drvdata(pdev, NULL);
1439 cdev_device_del(&stdev->cdev, &stdev->dev);
1440 ida_simple_remove(&switchtec_minor_ida, MINOR(stdev->dev.devt));
1441 dev_info(&stdev->dev, "unregistered.\n");
1443 put_device(&stdev->dev);
1446 #define SWITCHTEC_PCI_DEVICE(device_id) \
1448 .vendor = PCI_VENDOR_ID_MICROSEMI, \
1449 .device = device_id, \
1450 .subvendor = PCI_ANY_ID, \
1451 .subdevice = PCI_ANY_ID, \
1452 .class = (PCI_CLASS_MEMORY_OTHER << 8), \
1453 .class_mask = 0xFFFFFFFF, \
1456 .vendor = PCI_VENDOR_ID_MICROSEMI, \
1457 .device = device_id, \
1458 .subvendor = PCI_ANY_ID, \
1459 .subdevice = PCI_ANY_ID, \
1460 .class = (PCI_CLASS_BRIDGE_OTHER << 8), \
1461 .class_mask = 0xFFFFFFFF, \
1464 static const struct pci_device_id switchtec_pci_tbl[] = {
1465 SWITCHTEC_PCI_DEVICE(0x8531), //PFX 24xG3
1466 SWITCHTEC_PCI_DEVICE(0x8532), //PFX 32xG3
1467 SWITCHTEC_PCI_DEVICE(0x8533), //PFX 48xG3
1468 SWITCHTEC_PCI_DEVICE(0x8534), //PFX 64xG3
1469 SWITCHTEC_PCI_DEVICE(0x8535), //PFX 80xG3
1470 SWITCHTEC_PCI_DEVICE(0x8536), //PFX 96xG3
1471 SWITCHTEC_PCI_DEVICE(0x8541), //PSX 24xG3
1472 SWITCHTEC_PCI_DEVICE(0x8542), //PSX 32xG3
1473 SWITCHTEC_PCI_DEVICE(0x8543), //PSX 48xG3
1474 SWITCHTEC_PCI_DEVICE(0x8544), //PSX 64xG3
1475 SWITCHTEC_PCI_DEVICE(0x8545), //PSX 80xG3
1476 SWITCHTEC_PCI_DEVICE(0x8546), //PSX 96xG3
1477 SWITCHTEC_PCI_DEVICE(0x8551), //PAX 24XG3
1478 SWITCHTEC_PCI_DEVICE(0x8552), //PAX 32XG3
1479 SWITCHTEC_PCI_DEVICE(0x8553), //PAX 48XG3
1480 SWITCHTEC_PCI_DEVICE(0x8554), //PAX 64XG3
1481 SWITCHTEC_PCI_DEVICE(0x8555), //PAX 80XG3
1482 SWITCHTEC_PCI_DEVICE(0x8556), //PAX 96XG3
1483 SWITCHTEC_PCI_DEVICE(0x8561), //PFXL 24XG3
1484 SWITCHTEC_PCI_DEVICE(0x8562), //PFXL 32XG3
1485 SWITCHTEC_PCI_DEVICE(0x8563), //PFXL 48XG3
1486 SWITCHTEC_PCI_DEVICE(0x8564), //PFXL 64XG3
1487 SWITCHTEC_PCI_DEVICE(0x8565), //PFXL 80XG3
1488 SWITCHTEC_PCI_DEVICE(0x8566), //PFXL 96XG3
1489 SWITCHTEC_PCI_DEVICE(0x8571), //PFXI 24XG3
1490 SWITCHTEC_PCI_DEVICE(0x8572), //PFXI 32XG3
1491 SWITCHTEC_PCI_DEVICE(0x8573), //PFXI 48XG3
1492 SWITCHTEC_PCI_DEVICE(0x8574), //PFXI 64XG3
1493 SWITCHTEC_PCI_DEVICE(0x8575), //PFXI 80XG3
1494 SWITCHTEC_PCI_DEVICE(0x8576), //PFXI 96XG3
1497 MODULE_DEVICE_TABLE(pci, switchtec_pci_tbl);
1499 static struct pci_driver switchtec_pci_driver = {
1500 .name = KBUILD_MODNAME,
1501 .id_table = switchtec_pci_tbl,
1502 .probe = switchtec_pci_probe,
1503 .remove = switchtec_pci_remove,
1506 static int __init switchtec_init(void)
1510 rc = alloc_chrdev_region(&switchtec_devt, 0, max_devices,
1515 switchtec_class = class_create(THIS_MODULE, "switchtec");
1516 if (IS_ERR(switchtec_class)) {
1517 rc = PTR_ERR(switchtec_class);
1518 goto err_create_class;
1521 rc = pci_register_driver(&switchtec_pci_driver);
1523 goto err_pci_register;
1525 pr_info(KBUILD_MODNAME ": loaded.\n");
1530 class_destroy(switchtec_class);
1533 unregister_chrdev_region(switchtec_devt, max_devices);
1537 module_init(switchtec_init);
1539 static void __exit switchtec_exit(void)
1541 pci_unregister_driver(&switchtec_pci_driver);
1542 class_destroy(switchtec_class);
1543 unregister_chrdev_region(switchtec_devt, max_devices);
1544 ida_destroy(&switchtec_minor_ida);
1546 pr_info(KBUILD_MODNAME ": unloaded.\n");
1548 module_exit(switchtec_exit);