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 int nirqs = 32;
34 module_param(nirqs, int, 0644);
35 MODULE_PARM_DESC(nirqs, "number of interrupts to allocate (more may be useful for NTB applications)");
37 static dev_t switchtec_devt;
38 static DEFINE_IDA(switchtec_minor_ida);
40 struct class *switchtec_class;
41 EXPORT_SYMBOL_GPL(switchtec_class);
50 struct switchtec_user {
51 struct switchtec_dev *stdev;
53 enum mrpc_state state;
55 struct completion comp;
57 struct list_head list;
64 unsigned char data[SWITCHTEC_MRPC_PAYLOAD_SIZE];
68 static struct switchtec_user *stuser_create(struct switchtec_dev *stdev)
70 struct switchtec_user *stuser;
72 stuser = kzalloc(sizeof(*stuser), GFP_KERNEL);
74 return ERR_PTR(-ENOMEM);
76 get_device(&stdev->dev);
77 stuser->stdev = stdev;
78 kref_init(&stuser->kref);
79 INIT_LIST_HEAD(&stuser->list);
80 init_completion(&stuser->comp);
81 stuser->event_cnt = atomic_read(&stdev->event_cnt);
83 dev_dbg(&stdev->dev, "%s: %p\n", __func__, stuser);
88 static void stuser_free(struct kref *kref)
90 struct switchtec_user *stuser;
92 stuser = container_of(kref, struct switchtec_user, kref);
94 dev_dbg(&stuser->stdev->dev, "%s: %p\n", __func__, stuser);
96 put_device(&stuser->stdev->dev);
100 static void stuser_put(struct switchtec_user *stuser)
102 kref_put(&stuser->kref, stuser_free);
105 static void stuser_set_state(struct switchtec_user *stuser,
106 enum mrpc_state state)
108 /* requires the mrpc_mutex to already be held when called */
110 const char * const state_names[] = {
111 [MRPC_IDLE] = "IDLE",
112 [MRPC_QUEUED] = "QUEUED",
113 [MRPC_RUNNING] = "RUNNING",
114 [MRPC_DONE] = "DONE",
117 stuser->state = state;
119 dev_dbg(&stuser->stdev->dev, "stuser state %p -> %s",
120 stuser, state_names[state]);
123 static void mrpc_complete_cmd(struct switchtec_dev *stdev);
125 static void flush_wc_buf(struct switchtec_dev *stdev)
127 struct ntb_dbmsg_regs __iomem *mmio_dbmsg;
130 * odb (outbound doorbell) register is processed by low latency
131 * hardware and w/o side effect
133 mmio_dbmsg = (void __iomem *)stdev->mmio_ntb +
134 SWITCHTEC_NTB_REG_DBMSG_OFFSET;
135 ioread32(&mmio_dbmsg->odb);
138 static void mrpc_cmd_submit(struct switchtec_dev *stdev)
140 /* requires the mrpc_mutex to already be held when called */
142 struct switchtec_user *stuser;
144 if (stdev->mrpc_busy)
147 if (list_empty(&stdev->mrpc_queue))
150 stuser = list_entry(stdev->mrpc_queue.next, struct switchtec_user,
153 if (stdev->dma_mrpc) {
154 stdev->dma_mrpc->status = SWITCHTEC_MRPC_STATUS_INPROGRESS;
155 memset(stdev->dma_mrpc->data, 0xFF, SWITCHTEC_MRPC_PAYLOAD_SIZE);
158 stuser_set_state(stuser, MRPC_RUNNING);
159 stdev->mrpc_busy = 1;
160 memcpy_toio(&stdev->mmio_mrpc->input_data,
161 stuser->data, stuser->data_len);
163 iowrite32(stuser->cmd, &stdev->mmio_mrpc->cmd);
165 schedule_delayed_work(&stdev->mrpc_timeout,
166 msecs_to_jiffies(500));
169 static int mrpc_queue_cmd(struct switchtec_user *stuser)
171 /* requires the mrpc_mutex to already be held when called */
173 struct switchtec_dev *stdev = stuser->stdev;
175 kref_get(&stuser->kref);
176 stuser->read_len = sizeof(stuser->data);
177 stuser_set_state(stuser, MRPC_QUEUED);
178 init_completion(&stuser->comp);
179 list_add_tail(&stuser->list, &stdev->mrpc_queue);
181 mrpc_cmd_submit(stdev);
186 static void mrpc_complete_cmd(struct switchtec_dev *stdev)
188 /* requires the mrpc_mutex to already be held when called */
189 struct switchtec_user *stuser;
191 if (list_empty(&stdev->mrpc_queue))
194 stuser = list_entry(stdev->mrpc_queue.next, struct switchtec_user,
198 stuser->status = stdev->dma_mrpc->status;
200 stuser->status = ioread32(&stdev->mmio_mrpc->status);
202 if (stuser->status == SWITCHTEC_MRPC_STATUS_INPROGRESS)
205 stuser_set_state(stuser, MRPC_DONE);
206 stuser->return_code = 0;
208 if (stuser->status != SWITCHTEC_MRPC_STATUS_DONE)
212 stuser->return_code = stdev->dma_mrpc->rtn_code;
214 stuser->return_code = ioread32(&stdev->mmio_mrpc->ret_value);
215 if (stuser->return_code != 0)
219 memcpy(stuser->data, &stdev->dma_mrpc->data,
222 memcpy_fromio(stuser->data, &stdev->mmio_mrpc->output_data,
225 complete_all(&stuser->comp);
226 list_del_init(&stuser->list);
228 stdev->mrpc_busy = 0;
230 mrpc_cmd_submit(stdev);
233 static void mrpc_event_work(struct work_struct *work)
235 struct switchtec_dev *stdev;
237 stdev = container_of(work, struct switchtec_dev, mrpc_work);
239 dev_dbg(&stdev->dev, "%s\n", __func__);
241 mutex_lock(&stdev->mrpc_mutex);
242 cancel_delayed_work(&stdev->mrpc_timeout);
243 mrpc_complete_cmd(stdev);
244 mutex_unlock(&stdev->mrpc_mutex);
247 static void mrpc_timeout_work(struct work_struct *work)
249 struct switchtec_dev *stdev;
252 stdev = container_of(work, struct switchtec_dev, mrpc_timeout.work);
254 dev_dbg(&stdev->dev, "%s\n", __func__);
256 mutex_lock(&stdev->mrpc_mutex);
259 status = stdev->dma_mrpc->status;
261 status = ioread32(&stdev->mmio_mrpc->status);
262 if (status == SWITCHTEC_MRPC_STATUS_INPROGRESS) {
263 schedule_delayed_work(&stdev->mrpc_timeout,
264 msecs_to_jiffies(500));
268 mrpc_complete_cmd(stdev);
270 mutex_unlock(&stdev->mrpc_mutex);
273 static ssize_t device_version_show(struct device *dev,
274 struct device_attribute *attr, char *buf)
276 struct switchtec_dev *stdev = to_stdev(dev);
279 ver = ioread32(&stdev->mmio_sys_info->device_version);
281 return sprintf(buf, "%x\n", ver);
283 static DEVICE_ATTR_RO(device_version);
285 static ssize_t fw_version_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
288 struct switchtec_dev *stdev = to_stdev(dev);
291 ver = ioread32(&stdev->mmio_sys_info->firmware_version);
293 return sprintf(buf, "%08x\n", ver);
295 static DEVICE_ATTR_RO(fw_version);
297 static ssize_t io_string_show(char *buf, void __iomem *attr, size_t len)
301 memcpy_fromio(buf, attr, len);
305 for (i = len - 1; i > 0; i--) {
315 #define DEVICE_ATTR_SYS_INFO_STR(field) \
316 static ssize_t field ## _show(struct device *dev, \
317 struct device_attribute *attr, char *buf) \
319 struct switchtec_dev *stdev = to_stdev(dev); \
320 struct sys_info_regs __iomem *si = stdev->mmio_sys_info; \
321 if (stdev->gen == SWITCHTEC_GEN3) \
322 return io_string_show(buf, &si->gen3.field, \
323 sizeof(si->gen3.field)); \
328 static DEVICE_ATTR_RO(field)
330 DEVICE_ATTR_SYS_INFO_STR(vendor_id);
331 DEVICE_ATTR_SYS_INFO_STR(product_id);
332 DEVICE_ATTR_SYS_INFO_STR(product_revision);
334 static ssize_t component_vendor_show(struct device *dev,
335 struct device_attribute *attr, char *buf)
337 struct switchtec_dev *stdev = to_stdev(dev);
338 struct sys_info_regs __iomem *si = stdev->mmio_sys_info;
340 /* component_vendor field not supported after gen3 */
341 if (stdev->gen != SWITCHTEC_GEN3)
342 return sprintf(buf, "none\n");
344 return io_string_show(buf, &si->gen3.component_vendor,
345 sizeof(si->gen3.component_vendor));
347 static DEVICE_ATTR_RO(component_vendor);
349 static ssize_t component_id_show(struct device *dev,
350 struct device_attribute *attr, char *buf)
352 struct switchtec_dev *stdev = to_stdev(dev);
353 int id = ioread16(&stdev->mmio_sys_info->gen3.component_id);
355 /* component_id field not supported after gen3 */
356 if (stdev->gen != SWITCHTEC_GEN3)
357 return sprintf(buf, "none\n");
359 return sprintf(buf, "PM%04X\n", id);
361 static DEVICE_ATTR_RO(component_id);
363 static ssize_t component_revision_show(struct device *dev,
364 struct device_attribute *attr, char *buf)
366 struct switchtec_dev *stdev = to_stdev(dev);
367 int rev = ioread8(&stdev->mmio_sys_info->gen3.component_revision);
369 /* component_revision field not supported after gen3 */
370 if (stdev->gen != SWITCHTEC_GEN3)
371 return sprintf(buf, "255\n");
373 return sprintf(buf, "%d\n", rev);
375 static DEVICE_ATTR_RO(component_revision);
377 static ssize_t partition_show(struct device *dev,
378 struct device_attribute *attr, char *buf)
380 struct switchtec_dev *stdev = to_stdev(dev);
382 return sprintf(buf, "%d\n", stdev->partition);
384 static DEVICE_ATTR_RO(partition);
386 static ssize_t partition_count_show(struct device *dev,
387 struct device_attribute *attr, char *buf)
389 struct switchtec_dev *stdev = to_stdev(dev);
391 return sprintf(buf, "%d\n", stdev->partition_count);
393 static DEVICE_ATTR_RO(partition_count);
395 static struct attribute *switchtec_device_attrs[] = {
396 &dev_attr_device_version.attr,
397 &dev_attr_fw_version.attr,
398 &dev_attr_vendor_id.attr,
399 &dev_attr_product_id.attr,
400 &dev_attr_product_revision.attr,
401 &dev_attr_component_vendor.attr,
402 &dev_attr_component_id.attr,
403 &dev_attr_component_revision.attr,
404 &dev_attr_partition.attr,
405 &dev_attr_partition_count.attr,
409 ATTRIBUTE_GROUPS(switchtec_device);
411 static int switchtec_dev_open(struct inode *inode, struct file *filp)
413 struct switchtec_dev *stdev;
414 struct switchtec_user *stuser;
416 stdev = container_of(inode->i_cdev, struct switchtec_dev, cdev);
418 stuser = stuser_create(stdev);
420 return PTR_ERR(stuser);
422 filp->private_data = stuser;
423 stream_open(inode, filp);
425 dev_dbg(&stdev->dev, "%s: %p\n", __func__, stuser);
430 static int switchtec_dev_release(struct inode *inode, struct file *filp)
432 struct switchtec_user *stuser = filp->private_data;
439 static int lock_mutex_and_test_alive(struct switchtec_dev *stdev)
441 if (mutex_lock_interruptible(&stdev->mrpc_mutex))
445 mutex_unlock(&stdev->mrpc_mutex);
452 static ssize_t switchtec_dev_write(struct file *filp, const char __user *data,
453 size_t size, loff_t *off)
455 struct switchtec_user *stuser = filp->private_data;
456 struct switchtec_dev *stdev = stuser->stdev;
459 if (size < sizeof(stuser->cmd) ||
460 size > sizeof(stuser->cmd) + sizeof(stuser->data))
463 stuser->data_len = size - sizeof(stuser->cmd);
465 rc = lock_mutex_and_test_alive(stdev);
469 if (stuser->state != MRPC_IDLE) {
474 rc = copy_from_user(&stuser->cmd, data, sizeof(stuser->cmd));
480 data += sizeof(stuser->cmd);
481 rc = copy_from_user(&stuser->data, data, size - sizeof(stuser->cmd));
487 rc = mrpc_queue_cmd(stuser);
490 mutex_unlock(&stdev->mrpc_mutex);
498 static ssize_t switchtec_dev_read(struct file *filp, char __user *data,
499 size_t size, loff_t *off)
501 struct switchtec_user *stuser = filp->private_data;
502 struct switchtec_dev *stdev = stuser->stdev;
505 if (size < sizeof(stuser->cmd) ||
506 size > sizeof(stuser->cmd) + sizeof(stuser->data))
509 rc = lock_mutex_and_test_alive(stdev);
513 if (stuser->state == MRPC_IDLE) {
514 mutex_unlock(&stdev->mrpc_mutex);
518 stuser->read_len = size - sizeof(stuser->return_code);
520 mutex_unlock(&stdev->mrpc_mutex);
522 if (filp->f_flags & O_NONBLOCK) {
523 if (!try_wait_for_completion(&stuser->comp))
526 rc = wait_for_completion_interruptible(&stuser->comp);
531 rc = lock_mutex_and_test_alive(stdev);
535 if (stuser->state != MRPC_DONE) {
536 mutex_unlock(&stdev->mrpc_mutex);
540 rc = copy_to_user(data, &stuser->return_code,
541 sizeof(stuser->return_code));
547 data += sizeof(stuser->return_code);
548 rc = copy_to_user(data, &stuser->data,
549 size - sizeof(stuser->return_code));
555 stuser_set_state(stuser, MRPC_IDLE);
558 mutex_unlock(&stdev->mrpc_mutex);
560 if (stuser->status == SWITCHTEC_MRPC_STATUS_DONE)
562 else if (stuser->status == SWITCHTEC_MRPC_STATUS_INTERRUPTED)
568 static __poll_t switchtec_dev_poll(struct file *filp, poll_table *wait)
570 struct switchtec_user *stuser = filp->private_data;
571 struct switchtec_dev *stdev = stuser->stdev;
574 poll_wait(filp, &stuser->comp.wait, wait);
575 poll_wait(filp, &stdev->event_wq, wait);
577 if (lock_mutex_and_test_alive(stdev))
578 return EPOLLIN | EPOLLRDHUP | EPOLLOUT | EPOLLERR | EPOLLHUP;
580 mutex_unlock(&stdev->mrpc_mutex);
582 if (try_wait_for_completion(&stuser->comp))
583 ret |= EPOLLIN | EPOLLRDNORM;
585 if (stuser->event_cnt != atomic_read(&stdev->event_cnt))
586 ret |= EPOLLPRI | EPOLLRDBAND;
591 static int ioctl_flash_info(struct switchtec_dev *stdev,
592 struct switchtec_ioctl_flash_info __user *uinfo)
594 struct switchtec_ioctl_flash_info info = {0};
595 struct flash_info_regs __iomem *fi = stdev->mmio_flash_info;
597 if (stdev->gen == SWITCHTEC_GEN3) {
598 info.flash_length = ioread32(&fi->gen3.flash_length);
599 info.num_partitions = SWITCHTEC_NUM_PARTITIONS_GEN3;
604 if (copy_to_user(uinfo, &info, sizeof(info)))
610 static void set_fw_info_part(struct switchtec_ioctl_flash_part_info *info,
611 struct partition_info __iomem *pi)
613 info->address = ioread32(&pi->address);
614 info->length = ioread32(&pi->length);
617 static int flash_part_info_gen3(struct switchtec_dev *stdev,
618 struct switchtec_ioctl_flash_part_info *info)
620 struct flash_info_regs_gen3 __iomem *fi =
621 &stdev->mmio_flash_info->gen3;
622 struct sys_info_regs_gen3 __iomem *si = &stdev->mmio_sys_info->gen3;
623 u32 active_addr = -1;
625 switch (info->flash_partition) {
626 case SWITCHTEC_IOCTL_PART_CFG0:
627 active_addr = ioread32(&fi->active_cfg);
628 set_fw_info_part(info, &fi->cfg0);
629 if (ioread16(&si->cfg_running) == SWITCHTEC_GEN3_CFG0_RUNNING)
630 info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
632 case SWITCHTEC_IOCTL_PART_CFG1:
633 active_addr = ioread32(&fi->active_cfg);
634 set_fw_info_part(info, &fi->cfg1);
635 if (ioread16(&si->cfg_running) == SWITCHTEC_GEN3_CFG1_RUNNING)
636 info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
638 case SWITCHTEC_IOCTL_PART_IMG0:
639 active_addr = ioread32(&fi->active_img);
640 set_fw_info_part(info, &fi->img0);
641 if (ioread16(&si->img_running) == SWITCHTEC_GEN3_IMG0_RUNNING)
642 info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
644 case SWITCHTEC_IOCTL_PART_IMG1:
645 active_addr = ioread32(&fi->active_img);
646 set_fw_info_part(info, &fi->img1);
647 if (ioread16(&si->img_running) == SWITCHTEC_GEN3_IMG1_RUNNING)
648 info->active |= SWITCHTEC_IOCTL_PART_RUNNING;
650 case SWITCHTEC_IOCTL_PART_NVLOG:
651 set_fw_info_part(info, &fi->nvlog);
653 case SWITCHTEC_IOCTL_PART_VENDOR0:
654 set_fw_info_part(info, &fi->vendor[0]);
656 case SWITCHTEC_IOCTL_PART_VENDOR1:
657 set_fw_info_part(info, &fi->vendor[1]);
659 case SWITCHTEC_IOCTL_PART_VENDOR2:
660 set_fw_info_part(info, &fi->vendor[2]);
662 case SWITCHTEC_IOCTL_PART_VENDOR3:
663 set_fw_info_part(info, &fi->vendor[3]);
665 case SWITCHTEC_IOCTL_PART_VENDOR4:
666 set_fw_info_part(info, &fi->vendor[4]);
668 case SWITCHTEC_IOCTL_PART_VENDOR5:
669 set_fw_info_part(info, &fi->vendor[5]);
671 case SWITCHTEC_IOCTL_PART_VENDOR6:
672 set_fw_info_part(info, &fi->vendor[6]);
674 case SWITCHTEC_IOCTL_PART_VENDOR7:
675 set_fw_info_part(info, &fi->vendor[7]);
681 if (info->address == active_addr)
682 info->active |= SWITCHTEC_IOCTL_PART_ACTIVE;
687 static int ioctl_flash_part_info(struct switchtec_dev *stdev,
688 struct switchtec_ioctl_flash_part_info __user *uinfo)
691 struct switchtec_ioctl_flash_part_info info = {0};
693 if (copy_from_user(&info, uinfo, sizeof(info)))
696 if (stdev->gen == SWITCHTEC_GEN3) {
697 ret = flash_part_info_gen3(stdev, &info);
704 if (copy_to_user(uinfo, &info, sizeof(info)))
710 static int ioctl_event_summary(struct switchtec_dev *stdev,
711 struct switchtec_user *stuser,
712 struct switchtec_ioctl_event_summary __user *usum,
715 struct switchtec_ioctl_event_summary *s;
720 s = kzalloc(sizeof(*s), GFP_KERNEL);
724 s->global = ioread32(&stdev->mmio_sw_event->global_summary);
725 s->part_bitmap = ioread64(&stdev->mmio_sw_event->part_event_bitmap);
726 s->local_part = ioread32(&stdev->mmio_part_cfg->part_event_summary);
728 for (i = 0; i < stdev->partition_count; i++) {
729 reg = ioread32(&stdev->mmio_part_cfg_all[i].part_event_summary);
733 for (i = 0; i < stdev->pff_csr_count; i++) {
734 reg = ioread32(&stdev->mmio_pff_csr[i].pff_event_summary);
738 if (copy_to_user(usum, s, size)) {
743 stuser->event_cnt = atomic_read(&stdev->event_cnt);
750 static u32 __iomem *global_ev_reg(struct switchtec_dev *stdev,
751 size_t offset, int index)
753 return (void __iomem *)stdev->mmio_sw_event + offset;
756 static u32 __iomem *part_ev_reg(struct switchtec_dev *stdev,
757 size_t offset, int index)
759 return (void __iomem *)&stdev->mmio_part_cfg_all[index] + offset;
762 static u32 __iomem *pff_ev_reg(struct switchtec_dev *stdev,
763 size_t offset, int index)
765 return (void __iomem *)&stdev->mmio_pff_csr[index] + offset;
768 #define EV_GLB(i, r)[i] = {offsetof(struct sw_event_regs, r), global_ev_reg}
769 #define EV_PAR(i, r)[i] = {offsetof(struct part_cfg_regs, r), part_ev_reg}
770 #define EV_PFF(i, r)[i] = {offsetof(struct pff_csr_regs, r), pff_ev_reg}
772 static const struct event_reg {
774 u32 __iomem *(*map_reg)(struct switchtec_dev *stdev,
775 size_t offset, int index);
777 EV_GLB(SWITCHTEC_IOCTL_EVENT_STACK_ERROR, stack_error_event_hdr),
778 EV_GLB(SWITCHTEC_IOCTL_EVENT_PPU_ERROR, ppu_error_event_hdr),
779 EV_GLB(SWITCHTEC_IOCTL_EVENT_ISP_ERROR, isp_error_event_hdr),
780 EV_GLB(SWITCHTEC_IOCTL_EVENT_SYS_RESET, sys_reset_event_hdr),
781 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_EXC, fw_exception_hdr),
782 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_NMI, fw_nmi_hdr),
783 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_NON_FATAL, fw_non_fatal_hdr),
784 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_FATAL, fw_fatal_hdr),
785 EV_GLB(SWITCHTEC_IOCTL_EVENT_TWI_MRPC_COMP, twi_mrpc_comp_hdr),
786 EV_GLB(SWITCHTEC_IOCTL_EVENT_TWI_MRPC_COMP_ASYNC,
787 twi_mrpc_comp_async_hdr),
788 EV_GLB(SWITCHTEC_IOCTL_EVENT_CLI_MRPC_COMP, cli_mrpc_comp_hdr),
789 EV_GLB(SWITCHTEC_IOCTL_EVENT_CLI_MRPC_COMP_ASYNC,
790 cli_mrpc_comp_async_hdr),
791 EV_GLB(SWITCHTEC_IOCTL_EVENT_GPIO_INT, gpio_interrupt_hdr),
792 EV_GLB(SWITCHTEC_IOCTL_EVENT_GFMS, gfms_event_hdr),
793 EV_PAR(SWITCHTEC_IOCTL_EVENT_PART_RESET, part_reset_hdr),
794 EV_PAR(SWITCHTEC_IOCTL_EVENT_MRPC_COMP, mrpc_comp_hdr),
795 EV_PAR(SWITCHTEC_IOCTL_EVENT_MRPC_COMP_ASYNC, mrpc_comp_async_hdr),
796 EV_PAR(SWITCHTEC_IOCTL_EVENT_DYN_PART_BIND_COMP, dyn_binding_hdr),
797 EV_PAR(SWITCHTEC_IOCTL_EVENT_INTERCOMM_REQ_NOTIFY,
798 intercomm_notify_hdr),
799 EV_PFF(SWITCHTEC_IOCTL_EVENT_AER_IN_P2P, aer_in_p2p_hdr),
800 EV_PFF(SWITCHTEC_IOCTL_EVENT_AER_IN_VEP, aer_in_vep_hdr),
801 EV_PFF(SWITCHTEC_IOCTL_EVENT_DPC, dpc_hdr),
802 EV_PFF(SWITCHTEC_IOCTL_EVENT_CTS, cts_hdr),
803 EV_PFF(SWITCHTEC_IOCTL_EVENT_UEC, uec_hdr),
804 EV_PFF(SWITCHTEC_IOCTL_EVENT_HOTPLUG, hotplug_hdr),
805 EV_PFF(SWITCHTEC_IOCTL_EVENT_IER, ier_hdr),
806 EV_PFF(SWITCHTEC_IOCTL_EVENT_THRESH, threshold_hdr),
807 EV_PFF(SWITCHTEC_IOCTL_EVENT_POWER_MGMT, power_mgmt_hdr),
808 EV_PFF(SWITCHTEC_IOCTL_EVENT_TLP_THROTTLING, tlp_throttling_hdr),
809 EV_PFF(SWITCHTEC_IOCTL_EVENT_FORCE_SPEED, force_speed_hdr),
810 EV_PFF(SWITCHTEC_IOCTL_EVENT_CREDIT_TIMEOUT, credit_timeout_hdr),
811 EV_PFF(SWITCHTEC_IOCTL_EVENT_LINK_STATE, link_state_hdr),
814 static u32 __iomem *event_hdr_addr(struct switchtec_dev *stdev,
815 int event_id, int index)
819 if (event_id < 0 || event_id >= SWITCHTEC_IOCTL_MAX_EVENTS)
820 return ERR_PTR(-EINVAL);
822 off = event_regs[event_id].offset;
824 if (event_regs[event_id].map_reg == part_ev_reg) {
825 if (index == SWITCHTEC_IOCTL_EVENT_LOCAL_PART_IDX)
826 index = stdev->partition;
827 else if (index < 0 || index >= stdev->partition_count)
828 return ERR_PTR(-EINVAL);
829 } else if (event_regs[event_id].map_reg == pff_ev_reg) {
830 if (index < 0 || index >= stdev->pff_csr_count)
831 return ERR_PTR(-EINVAL);
834 return event_regs[event_id].map_reg(stdev, off, index);
837 static int event_ctl(struct switchtec_dev *stdev,
838 struct switchtec_ioctl_event_ctl *ctl)
844 reg = event_hdr_addr(stdev, ctl->event_id, ctl->index);
849 for (i = 0; i < ARRAY_SIZE(ctl->data); i++)
850 ctl->data[i] = ioread32(®[i + 1]);
852 ctl->occurred = hdr & SWITCHTEC_EVENT_OCCURRED;
853 ctl->count = (hdr >> 5) & 0xFF;
855 if (!(ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_CLEAR))
856 hdr &= ~SWITCHTEC_EVENT_CLEAR;
857 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL)
858 hdr |= SWITCHTEC_EVENT_EN_IRQ;
859 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_POLL)
860 hdr &= ~SWITCHTEC_EVENT_EN_IRQ;
861 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG)
862 hdr |= SWITCHTEC_EVENT_EN_LOG;
863 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_LOG)
864 hdr &= ~SWITCHTEC_EVENT_EN_LOG;
865 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI)
866 hdr |= SWITCHTEC_EVENT_EN_CLI;
867 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_CLI)
868 hdr &= ~SWITCHTEC_EVENT_EN_CLI;
869 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL)
870 hdr |= SWITCHTEC_EVENT_FATAL;
871 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_FATAL)
872 hdr &= ~SWITCHTEC_EVENT_FATAL;
878 if (hdr & SWITCHTEC_EVENT_EN_IRQ)
879 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL;
880 if (hdr & SWITCHTEC_EVENT_EN_LOG)
881 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG;
882 if (hdr & SWITCHTEC_EVENT_EN_CLI)
883 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI;
884 if (hdr & SWITCHTEC_EVENT_FATAL)
885 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL;
890 static int ioctl_event_ctl(struct switchtec_dev *stdev,
891 struct switchtec_ioctl_event_ctl __user *uctl)
895 unsigned int event_flags;
896 struct switchtec_ioctl_event_ctl ctl;
898 if (copy_from_user(&ctl, uctl, sizeof(ctl)))
901 if (ctl.event_id >= SWITCHTEC_IOCTL_MAX_EVENTS)
904 if (ctl.flags & SWITCHTEC_IOCTL_EVENT_FLAG_UNUSED)
907 if (ctl.index == SWITCHTEC_IOCTL_EVENT_IDX_ALL) {
908 if (event_regs[ctl.event_id].map_reg == global_ev_reg)
910 else if (event_regs[ctl.event_id].map_reg == part_ev_reg)
911 nr_idxs = stdev->partition_count;
912 else if (event_regs[ctl.event_id].map_reg == pff_ev_reg)
913 nr_idxs = stdev->pff_csr_count;
917 event_flags = ctl.flags;
918 for (ctl.index = 0; ctl.index < nr_idxs; ctl.index++) {
919 ctl.flags = event_flags;
920 ret = event_ctl(stdev, &ctl);
925 ret = event_ctl(stdev, &ctl);
930 if (copy_to_user(uctl, &ctl, sizeof(ctl)))
936 static int ioctl_pff_to_port(struct switchtec_dev *stdev,
937 struct switchtec_ioctl_pff_port *up)
941 struct part_cfg_regs *pcfg;
942 struct switchtec_ioctl_pff_port p;
944 if (copy_from_user(&p, up, sizeof(p)))
948 for (part = 0; part < stdev->partition_count; part++) {
949 pcfg = &stdev->mmio_part_cfg_all[part];
952 reg = ioread32(&pcfg->usp_pff_inst_id);
958 reg = ioread32(&pcfg->vep_pff_inst_id);
960 p.port = SWITCHTEC_IOCTL_PFF_VEP;
964 for (i = 0; i < ARRAY_SIZE(pcfg->dsp_pff_inst_id); i++) {
965 reg = ioread32(&pcfg->dsp_pff_inst_id[i]);
977 if (copy_to_user(up, &p, sizeof(p)))
983 static int ioctl_port_to_pff(struct switchtec_dev *stdev,
984 struct switchtec_ioctl_pff_port *up)
986 struct switchtec_ioctl_pff_port p;
987 struct part_cfg_regs *pcfg;
989 if (copy_from_user(&p, up, sizeof(p)))
992 if (p.partition == SWITCHTEC_IOCTL_EVENT_LOCAL_PART_IDX)
993 pcfg = stdev->mmio_part_cfg;
994 else if (p.partition < stdev->partition_count)
995 pcfg = &stdev->mmio_part_cfg_all[p.partition];
1001 p.pff = ioread32(&pcfg->usp_pff_inst_id);
1003 case SWITCHTEC_IOCTL_PFF_VEP:
1004 p.pff = ioread32(&pcfg->vep_pff_inst_id);
1007 if (p.port > ARRAY_SIZE(pcfg->dsp_pff_inst_id))
1009 p.port = array_index_nospec(p.port,
1010 ARRAY_SIZE(pcfg->dsp_pff_inst_id) + 1);
1011 p.pff = ioread32(&pcfg->dsp_pff_inst_id[p.port - 1]);
1015 if (copy_to_user(up, &p, sizeof(p)))
1021 static long switchtec_dev_ioctl(struct file *filp, unsigned int cmd,
1024 struct switchtec_user *stuser = filp->private_data;
1025 struct switchtec_dev *stdev = stuser->stdev;
1027 void __user *argp = (void __user *)arg;
1029 rc = lock_mutex_and_test_alive(stdev);
1034 case SWITCHTEC_IOCTL_FLASH_INFO:
1035 rc = ioctl_flash_info(stdev, argp);
1037 case SWITCHTEC_IOCTL_FLASH_PART_INFO:
1038 rc = ioctl_flash_part_info(stdev, argp);
1040 case SWITCHTEC_IOCTL_EVENT_SUMMARY_LEGACY:
1041 rc = ioctl_event_summary(stdev, stuser, argp,
1042 sizeof(struct switchtec_ioctl_event_summary_legacy));
1044 case SWITCHTEC_IOCTL_EVENT_CTL:
1045 rc = ioctl_event_ctl(stdev, argp);
1047 case SWITCHTEC_IOCTL_PFF_TO_PORT:
1048 rc = ioctl_pff_to_port(stdev, argp);
1050 case SWITCHTEC_IOCTL_PORT_TO_PFF:
1051 rc = ioctl_port_to_pff(stdev, argp);
1053 case SWITCHTEC_IOCTL_EVENT_SUMMARY:
1054 rc = ioctl_event_summary(stdev, stuser, argp,
1055 sizeof(struct switchtec_ioctl_event_summary));
1062 mutex_unlock(&stdev->mrpc_mutex);
1066 static const struct file_operations switchtec_fops = {
1067 .owner = THIS_MODULE,
1068 .open = switchtec_dev_open,
1069 .release = switchtec_dev_release,
1070 .write = switchtec_dev_write,
1071 .read = switchtec_dev_read,
1072 .poll = switchtec_dev_poll,
1073 .unlocked_ioctl = switchtec_dev_ioctl,
1074 .compat_ioctl = compat_ptr_ioctl,
1077 static void link_event_work(struct work_struct *work)
1079 struct switchtec_dev *stdev;
1081 stdev = container_of(work, struct switchtec_dev, link_event_work);
1083 if (stdev->link_notifier)
1084 stdev->link_notifier(stdev);
1087 static void check_link_state_events(struct switchtec_dev *stdev)
1094 for (idx = 0; idx < stdev->pff_csr_count; idx++) {
1095 reg = ioread32(&stdev->mmio_pff_csr[idx].link_state_hdr);
1096 dev_dbg(&stdev->dev, "link_state: %d->%08x\n", idx, reg);
1097 count = (reg >> 5) & 0xFF;
1099 if (count != stdev->link_event_count[idx]) {
1101 stdev->link_event_count[idx] = count;
1106 schedule_work(&stdev->link_event_work);
1109 static void enable_link_state_events(struct switchtec_dev *stdev)
1113 for (idx = 0; idx < stdev->pff_csr_count; idx++) {
1114 iowrite32(SWITCHTEC_EVENT_CLEAR |
1115 SWITCHTEC_EVENT_EN_IRQ,
1116 &stdev->mmio_pff_csr[idx].link_state_hdr);
1120 static void enable_dma_mrpc(struct switchtec_dev *stdev)
1122 writeq(stdev->dma_mrpc_dma_addr, &stdev->mmio_mrpc->dma_addr);
1123 flush_wc_buf(stdev);
1124 iowrite32(SWITCHTEC_DMA_MRPC_EN, &stdev->mmio_mrpc->dma_en);
1127 static void stdev_release(struct device *dev)
1129 struct switchtec_dev *stdev = to_stdev(dev);
1131 if (stdev->dma_mrpc) {
1132 iowrite32(0, &stdev->mmio_mrpc->dma_en);
1133 flush_wc_buf(stdev);
1134 writeq(0, &stdev->mmio_mrpc->dma_addr);
1135 dma_free_coherent(&stdev->pdev->dev, sizeof(*stdev->dma_mrpc),
1136 stdev->dma_mrpc, stdev->dma_mrpc_dma_addr);
1141 static void stdev_kill(struct switchtec_dev *stdev)
1143 struct switchtec_user *stuser, *tmpuser;
1145 pci_clear_master(stdev->pdev);
1147 cancel_delayed_work_sync(&stdev->mrpc_timeout);
1149 /* Mark the hardware as unavailable and complete all completions */
1150 mutex_lock(&stdev->mrpc_mutex);
1151 stdev->alive = false;
1153 /* Wake up and kill any users waiting on an MRPC request */
1154 list_for_each_entry_safe(stuser, tmpuser, &stdev->mrpc_queue, list) {
1155 complete_all(&stuser->comp);
1156 list_del_init(&stuser->list);
1160 mutex_unlock(&stdev->mrpc_mutex);
1162 /* Wake up any users waiting on event_wq */
1163 wake_up_interruptible(&stdev->event_wq);
1166 static struct switchtec_dev *stdev_create(struct pci_dev *pdev)
1168 struct switchtec_dev *stdev;
1174 stdev = kzalloc_node(sizeof(*stdev), GFP_KERNEL,
1175 dev_to_node(&pdev->dev));
1177 return ERR_PTR(-ENOMEM);
1179 stdev->alive = true;
1181 INIT_LIST_HEAD(&stdev->mrpc_queue);
1182 mutex_init(&stdev->mrpc_mutex);
1183 stdev->mrpc_busy = 0;
1184 INIT_WORK(&stdev->mrpc_work, mrpc_event_work);
1185 INIT_DELAYED_WORK(&stdev->mrpc_timeout, mrpc_timeout_work);
1186 INIT_WORK(&stdev->link_event_work, link_event_work);
1187 init_waitqueue_head(&stdev->event_wq);
1188 atomic_set(&stdev->event_cnt, 0);
1191 device_initialize(dev);
1192 dev->class = switchtec_class;
1193 dev->parent = &pdev->dev;
1194 dev->groups = switchtec_device_groups;
1195 dev->release = stdev_release;
1197 minor = ida_simple_get(&switchtec_minor_ida, 0, 0,
1204 dev->devt = MKDEV(MAJOR(switchtec_devt), minor);
1205 dev_set_name(dev, "switchtec%d", minor);
1207 cdev = &stdev->cdev;
1208 cdev_init(cdev, &switchtec_fops);
1209 cdev->owner = THIS_MODULE;
1214 put_device(&stdev->dev);
1218 static int mask_event(struct switchtec_dev *stdev, int eid, int idx)
1220 size_t off = event_regs[eid].offset;
1221 u32 __iomem *hdr_reg;
1224 hdr_reg = event_regs[eid].map_reg(stdev, off, idx);
1225 hdr = ioread32(hdr_reg);
1227 if (!(hdr & SWITCHTEC_EVENT_OCCURRED && hdr & SWITCHTEC_EVENT_EN_IRQ))
1230 dev_dbg(&stdev->dev, "%s: %d %d %x\n", __func__, eid, idx, hdr);
1231 hdr &= ~(SWITCHTEC_EVENT_EN_IRQ | SWITCHTEC_EVENT_OCCURRED);
1232 iowrite32(hdr, hdr_reg);
1237 static int mask_all_events(struct switchtec_dev *stdev, int eid)
1242 if (event_regs[eid].map_reg == part_ev_reg) {
1243 for (idx = 0; idx < stdev->partition_count; idx++)
1244 count += mask_event(stdev, eid, idx);
1245 } else if (event_regs[eid].map_reg == pff_ev_reg) {
1246 for (idx = 0; idx < stdev->pff_csr_count; idx++) {
1247 if (!stdev->pff_local[idx])
1250 count += mask_event(stdev, eid, idx);
1253 count += mask_event(stdev, eid, 0);
1259 static irqreturn_t switchtec_event_isr(int irq, void *dev)
1261 struct switchtec_dev *stdev = dev;
1263 irqreturn_t ret = IRQ_NONE;
1264 int eid, event_count = 0;
1266 reg = ioread32(&stdev->mmio_part_cfg->mrpc_comp_hdr);
1267 if (reg & SWITCHTEC_EVENT_OCCURRED) {
1268 dev_dbg(&stdev->dev, "%s: mrpc comp\n", __func__);
1270 schedule_work(&stdev->mrpc_work);
1271 iowrite32(reg, &stdev->mmio_part_cfg->mrpc_comp_hdr);
1274 check_link_state_events(stdev);
1276 for (eid = 0; eid < SWITCHTEC_IOCTL_MAX_EVENTS; eid++) {
1277 if (eid == SWITCHTEC_IOCTL_EVENT_LINK_STATE ||
1278 eid == SWITCHTEC_IOCTL_EVENT_MRPC_COMP)
1281 event_count += mask_all_events(stdev, eid);
1285 atomic_inc(&stdev->event_cnt);
1286 wake_up_interruptible(&stdev->event_wq);
1287 dev_dbg(&stdev->dev, "%s: %d events\n", __func__,
1296 static irqreturn_t switchtec_dma_mrpc_isr(int irq, void *dev)
1298 struct switchtec_dev *stdev = dev;
1299 irqreturn_t ret = IRQ_NONE;
1301 iowrite32(SWITCHTEC_EVENT_CLEAR |
1302 SWITCHTEC_EVENT_EN_IRQ,
1303 &stdev->mmio_part_cfg->mrpc_comp_hdr);
1304 schedule_work(&stdev->mrpc_work);
1310 static int switchtec_init_isr(struct switchtec_dev *stdev)
1320 nvecs = pci_alloc_irq_vectors(stdev->pdev, 1, nirqs,
1321 PCI_IRQ_MSIX | PCI_IRQ_MSI |
1326 event_irq = ioread16(&stdev->mmio_part_cfg->vep_vector_number);
1327 if (event_irq < 0 || event_irq >= nvecs)
1330 event_irq = pci_irq_vector(stdev->pdev, event_irq);
1334 rc = devm_request_irq(&stdev->pdev->dev, event_irq,
1335 switchtec_event_isr, 0,
1336 KBUILD_MODNAME, stdev);
1341 if (!stdev->dma_mrpc)
1344 dma_mrpc_irq = ioread32(&stdev->mmio_mrpc->dma_vector);
1345 if (dma_mrpc_irq < 0 || dma_mrpc_irq >= nvecs)
1348 dma_mrpc_irq = pci_irq_vector(stdev->pdev, dma_mrpc_irq);
1349 if (dma_mrpc_irq < 0)
1350 return dma_mrpc_irq;
1352 rc = devm_request_irq(&stdev->pdev->dev, dma_mrpc_irq,
1353 switchtec_dma_mrpc_isr, 0,
1354 KBUILD_MODNAME, stdev);
1359 static void init_pff(struct switchtec_dev *stdev)
1363 struct part_cfg_regs *pcfg = stdev->mmio_part_cfg;
1365 for (i = 0; i < SWITCHTEC_MAX_PFF_CSR; i++) {
1366 reg = ioread16(&stdev->mmio_pff_csr[i].vendor_id);
1367 if (reg != PCI_VENDOR_ID_MICROSEMI)
1371 stdev->pff_csr_count = i;
1373 reg = ioread32(&pcfg->usp_pff_inst_id);
1374 if (reg < stdev->pff_csr_count)
1375 stdev->pff_local[reg] = 1;
1377 reg = ioread32(&pcfg->vep_pff_inst_id);
1378 if (reg < stdev->pff_csr_count)
1379 stdev->pff_local[reg] = 1;
1381 for (i = 0; i < ARRAY_SIZE(pcfg->dsp_pff_inst_id); i++) {
1382 reg = ioread32(&pcfg->dsp_pff_inst_id[i]);
1383 if (reg < stdev->pff_csr_count)
1384 stdev->pff_local[reg] = 1;
1388 static int switchtec_init_pci(struct switchtec_dev *stdev,
1389 struct pci_dev *pdev)
1393 unsigned long res_start, res_len;
1394 u32 __iomem *part_id;
1396 rc = pcim_enable_device(pdev);
1400 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1404 pci_set_master(pdev);
1406 res_start = pci_resource_start(pdev, 0);
1407 res_len = pci_resource_len(pdev, 0);
1409 if (!devm_request_mem_region(&pdev->dev, res_start,
1410 res_len, KBUILD_MODNAME))
1413 stdev->mmio_mrpc = devm_ioremap_wc(&pdev->dev, res_start,
1414 SWITCHTEC_GAS_TOP_CFG_OFFSET);
1415 if (!stdev->mmio_mrpc)
1418 map = devm_ioremap(&pdev->dev,
1419 res_start + SWITCHTEC_GAS_TOP_CFG_OFFSET,
1420 res_len - SWITCHTEC_GAS_TOP_CFG_OFFSET);
1424 stdev->mmio = map - SWITCHTEC_GAS_TOP_CFG_OFFSET;
1425 stdev->mmio_sw_event = stdev->mmio + SWITCHTEC_GAS_SW_EVENT_OFFSET;
1426 stdev->mmio_sys_info = stdev->mmio + SWITCHTEC_GAS_SYS_INFO_OFFSET;
1427 stdev->mmio_flash_info = stdev->mmio + SWITCHTEC_GAS_FLASH_INFO_OFFSET;
1428 stdev->mmio_ntb = stdev->mmio + SWITCHTEC_GAS_NTB_OFFSET;
1430 if (stdev->gen == SWITCHTEC_GEN3)
1431 part_id = &stdev->mmio_sys_info->gen3.partition_id;
1435 stdev->partition = ioread8(part_id);
1436 stdev->partition_count = ioread8(&stdev->mmio_ntb->partition_count);
1437 stdev->mmio_part_cfg_all = stdev->mmio + SWITCHTEC_GAS_PART_CFG_OFFSET;
1438 stdev->mmio_part_cfg = &stdev->mmio_part_cfg_all[stdev->partition];
1439 stdev->mmio_pff_csr = stdev->mmio + SWITCHTEC_GAS_PFF_CSR_OFFSET;
1441 if (stdev->partition_count < 1)
1442 stdev->partition_count = 1;
1446 pci_set_drvdata(pdev, stdev);
1451 if (ioread32(&stdev->mmio_mrpc->dma_ver) == 0)
1454 stdev->dma_mrpc = dma_alloc_coherent(&stdev->pdev->dev,
1455 sizeof(*stdev->dma_mrpc),
1456 &stdev->dma_mrpc_dma_addr,
1458 if (stdev->dma_mrpc == NULL)
1464 static int switchtec_pci_probe(struct pci_dev *pdev,
1465 const struct pci_device_id *id)
1467 struct switchtec_dev *stdev;
1470 if (pdev->class == (PCI_CLASS_BRIDGE_OTHER << 8))
1471 request_module_nowait("ntb_hw_switchtec");
1473 stdev = stdev_create(pdev);
1475 return PTR_ERR(stdev);
1477 stdev->gen = id->driver_data;
1479 rc = switchtec_init_pci(stdev, pdev);
1483 rc = switchtec_init_isr(stdev);
1485 dev_err(&stdev->dev, "failed to init isr.\n");
1489 iowrite32(SWITCHTEC_EVENT_CLEAR |
1490 SWITCHTEC_EVENT_EN_IRQ,
1491 &stdev->mmio_part_cfg->mrpc_comp_hdr);
1492 enable_link_state_events(stdev);
1494 if (stdev->dma_mrpc)
1495 enable_dma_mrpc(stdev);
1497 rc = cdev_device_add(&stdev->cdev, &stdev->dev);
1501 dev_info(&stdev->dev, "Management device registered.\n");
1508 ida_simple_remove(&switchtec_minor_ida, MINOR(stdev->dev.devt));
1509 put_device(&stdev->dev);
1513 static void switchtec_pci_remove(struct pci_dev *pdev)
1515 struct switchtec_dev *stdev = pci_get_drvdata(pdev);
1517 pci_set_drvdata(pdev, NULL);
1519 cdev_device_del(&stdev->cdev, &stdev->dev);
1520 ida_simple_remove(&switchtec_minor_ida, MINOR(stdev->dev.devt));
1521 dev_info(&stdev->dev, "unregistered.\n");
1523 put_device(&stdev->dev);
1526 #define SWITCHTEC_PCI_DEVICE(device_id, gen) \
1528 .vendor = PCI_VENDOR_ID_MICROSEMI, \
1529 .device = device_id, \
1530 .subvendor = PCI_ANY_ID, \
1531 .subdevice = PCI_ANY_ID, \
1532 .class = (PCI_CLASS_MEMORY_OTHER << 8), \
1533 .class_mask = 0xFFFFFFFF, \
1534 .driver_data = gen, \
1537 .vendor = PCI_VENDOR_ID_MICROSEMI, \
1538 .device = device_id, \
1539 .subvendor = PCI_ANY_ID, \
1540 .subdevice = PCI_ANY_ID, \
1541 .class = (PCI_CLASS_BRIDGE_OTHER << 8), \
1542 .class_mask = 0xFFFFFFFF, \
1543 .driver_data = gen, \
1546 static const struct pci_device_id switchtec_pci_tbl[] = {
1547 SWITCHTEC_PCI_DEVICE(0x8531, SWITCHTEC_GEN3), //PFX 24xG3
1548 SWITCHTEC_PCI_DEVICE(0x8532, SWITCHTEC_GEN3), //PFX 32xG3
1549 SWITCHTEC_PCI_DEVICE(0x8533, SWITCHTEC_GEN3), //PFX 48xG3
1550 SWITCHTEC_PCI_DEVICE(0x8534, SWITCHTEC_GEN3), //PFX 64xG3
1551 SWITCHTEC_PCI_DEVICE(0x8535, SWITCHTEC_GEN3), //PFX 80xG3
1552 SWITCHTEC_PCI_DEVICE(0x8536, SWITCHTEC_GEN3), //PFX 96xG3
1553 SWITCHTEC_PCI_DEVICE(0x8541, SWITCHTEC_GEN3), //PSX 24xG3
1554 SWITCHTEC_PCI_DEVICE(0x8542, SWITCHTEC_GEN3), //PSX 32xG3
1555 SWITCHTEC_PCI_DEVICE(0x8543, SWITCHTEC_GEN3), //PSX 48xG3
1556 SWITCHTEC_PCI_DEVICE(0x8544, SWITCHTEC_GEN3), //PSX 64xG3
1557 SWITCHTEC_PCI_DEVICE(0x8545, SWITCHTEC_GEN3), //PSX 80xG3
1558 SWITCHTEC_PCI_DEVICE(0x8546, SWITCHTEC_GEN3), //PSX 96xG3
1559 SWITCHTEC_PCI_DEVICE(0x8551, SWITCHTEC_GEN3), //PAX 24XG3
1560 SWITCHTEC_PCI_DEVICE(0x8552, SWITCHTEC_GEN3), //PAX 32XG3
1561 SWITCHTEC_PCI_DEVICE(0x8553, SWITCHTEC_GEN3), //PAX 48XG3
1562 SWITCHTEC_PCI_DEVICE(0x8554, SWITCHTEC_GEN3), //PAX 64XG3
1563 SWITCHTEC_PCI_DEVICE(0x8555, SWITCHTEC_GEN3), //PAX 80XG3
1564 SWITCHTEC_PCI_DEVICE(0x8556, SWITCHTEC_GEN3), //PAX 96XG3
1565 SWITCHTEC_PCI_DEVICE(0x8561, SWITCHTEC_GEN3), //PFXL 24XG3
1566 SWITCHTEC_PCI_DEVICE(0x8562, SWITCHTEC_GEN3), //PFXL 32XG3
1567 SWITCHTEC_PCI_DEVICE(0x8563, SWITCHTEC_GEN3), //PFXL 48XG3
1568 SWITCHTEC_PCI_DEVICE(0x8564, SWITCHTEC_GEN3), //PFXL 64XG3
1569 SWITCHTEC_PCI_DEVICE(0x8565, SWITCHTEC_GEN3), //PFXL 80XG3
1570 SWITCHTEC_PCI_DEVICE(0x8566, SWITCHTEC_GEN3), //PFXL 96XG3
1571 SWITCHTEC_PCI_DEVICE(0x8571, SWITCHTEC_GEN3), //PFXI 24XG3
1572 SWITCHTEC_PCI_DEVICE(0x8572, SWITCHTEC_GEN3), //PFXI 32XG3
1573 SWITCHTEC_PCI_DEVICE(0x8573, SWITCHTEC_GEN3), //PFXI 48XG3
1574 SWITCHTEC_PCI_DEVICE(0x8574, SWITCHTEC_GEN3), //PFXI 64XG3
1575 SWITCHTEC_PCI_DEVICE(0x8575, SWITCHTEC_GEN3), //PFXI 80XG3
1576 SWITCHTEC_PCI_DEVICE(0x8576, SWITCHTEC_GEN3), //PFXI 96XG3
1579 MODULE_DEVICE_TABLE(pci, switchtec_pci_tbl);
1581 static struct pci_driver switchtec_pci_driver = {
1582 .name = KBUILD_MODNAME,
1583 .id_table = switchtec_pci_tbl,
1584 .probe = switchtec_pci_probe,
1585 .remove = switchtec_pci_remove,
1588 static int __init switchtec_init(void)
1592 rc = alloc_chrdev_region(&switchtec_devt, 0, max_devices,
1597 switchtec_class = class_create(THIS_MODULE, "switchtec");
1598 if (IS_ERR(switchtec_class)) {
1599 rc = PTR_ERR(switchtec_class);
1600 goto err_create_class;
1603 rc = pci_register_driver(&switchtec_pci_driver);
1605 goto err_pci_register;
1607 pr_info(KBUILD_MODNAME ": loaded.\n");
1612 class_destroy(switchtec_class);
1615 unregister_chrdev_region(switchtec_devt, max_devices);
1619 module_init(switchtec_init);
1621 static void __exit switchtec_exit(void)
1623 pci_unregister_driver(&switchtec_pci_driver);
1624 class_destroy(switchtec_class);
1625 unregister_chrdev_region(switchtec_devt, max_devices);
1626 ida_destroy(&switchtec_minor_ida);
1628 pr_info(KBUILD_MODNAME ": unloaded.\n");
1630 module_exit(switchtec_exit);