blob: 5aaa4ce04ec3dfbdecb71286104cbba9d26ff881 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Microsemi Switchtec(tm) PCIe Management Driver
4 * Copyright (c) 2017, Microsemi Corporation
5 */
6
7#include <linux/switchtec.h>
8#include <linux/switchtec_ioctl.h>
9
10#include <linux/interrupt.h>
11#include <linux/module.h>
12#include <linux/fs.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>
18
19MODULE_DESCRIPTION("Microsemi Switchtec(tm) PCIe Management Driver");
20MODULE_VERSION("0.1");
21MODULE_LICENSE("GPL");
22MODULE_AUTHOR("Microsemi Corporation");
23
24static int max_devices = 16;
25module_param(max_devices, int, 0644);
26MODULE_PARM_DESC(max_devices, "max number of switchtec device instances");
27
28static dev_t switchtec_devt;
29static DEFINE_IDA(switchtec_minor_ida);
30
31struct class *switchtec_class;
32EXPORT_SYMBOL_GPL(switchtec_class);
33
34enum mrpc_state {
35 MRPC_IDLE = 0,
36 MRPC_QUEUED,
37 MRPC_RUNNING,
38 MRPC_DONE,
39};
40
41struct switchtec_user {
42 struct switchtec_dev *stdev;
43
44 enum mrpc_state state;
45
46 struct completion comp;
47 struct kref kref;
48 struct list_head list;
49
50 u32 cmd;
51 u32 status;
52 u32 return_code;
53 size_t data_len;
54 size_t read_len;
55 unsigned char data[SWITCHTEC_MRPC_PAYLOAD_SIZE];
56 int event_cnt;
57};
58
59static struct switchtec_user *stuser_create(struct switchtec_dev *stdev)
60{
61 struct switchtec_user *stuser;
62
63 stuser = kzalloc(sizeof(*stuser), GFP_KERNEL);
64 if (!stuser)
65 return ERR_PTR(-ENOMEM);
66
67 get_device(&stdev->dev);
68 stuser->stdev = stdev;
69 kref_init(&stuser->kref);
70 INIT_LIST_HEAD(&stuser->list);
71 init_completion(&stuser->comp);
72 stuser->event_cnt = atomic_read(&stdev->event_cnt);
73
74 dev_dbg(&stdev->dev, "%s: %p\n", __func__, stuser);
75
76 return stuser;
77}
78
79static void stuser_free(struct kref *kref)
80{
81 struct switchtec_user *stuser;
82
83 stuser = container_of(kref, struct switchtec_user, kref);
84
85 dev_dbg(&stuser->stdev->dev, "%s: %p\n", __func__, stuser);
86
87 put_device(&stuser->stdev->dev);
88 kfree(stuser);
89}
90
91static void stuser_put(struct switchtec_user *stuser)
92{
93 kref_put(&stuser->kref, stuser_free);
94}
95
96static void stuser_set_state(struct switchtec_user *stuser,
97 enum mrpc_state state)
98{
99 /* requires the mrpc_mutex to already be held when called */
100
101 const char * const state_names[] = {
102 [MRPC_IDLE] = "IDLE",
103 [MRPC_QUEUED] = "QUEUED",
104 [MRPC_RUNNING] = "RUNNING",
105 [MRPC_DONE] = "DONE",
106 };
107
108 stuser->state = state;
109
110 dev_dbg(&stuser->stdev->dev, "stuser state %p -> %s",
111 stuser, state_names[state]);
112}
113
114static void mrpc_complete_cmd(struct switchtec_dev *stdev);
115
116static void mrpc_cmd_submit(struct switchtec_dev *stdev)
117{
118 /* requires the mrpc_mutex to already be held when called */
119
120 struct switchtec_user *stuser;
121
122 if (stdev->mrpc_busy)
123 return;
124
125 if (list_empty(&stdev->mrpc_queue))
126 return;
127
128 stuser = list_entry(stdev->mrpc_queue.next, struct switchtec_user,
129 list);
130
131 stuser_set_state(stuser, MRPC_RUNNING);
132 stdev->mrpc_busy = 1;
133 memcpy_toio(&stdev->mmio_mrpc->input_data,
134 stuser->data, stuser->data_len);
135 iowrite32(stuser->cmd, &stdev->mmio_mrpc->cmd);
136
137 stuser->status = ioread32(&stdev->mmio_mrpc->status);
138 if (stuser->status != SWITCHTEC_MRPC_STATUS_INPROGRESS)
139 mrpc_complete_cmd(stdev);
140
141 schedule_delayed_work(&stdev->mrpc_timeout,
142 msecs_to_jiffies(500));
143}
144
145static int mrpc_queue_cmd(struct switchtec_user *stuser)
146{
147 /* requires the mrpc_mutex to already be held when called */
148
149 struct switchtec_dev *stdev = stuser->stdev;
150
151 kref_get(&stuser->kref);
152 stuser->read_len = sizeof(stuser->data);
153 stuser_set_state(stuser, MRPC_QUEUED);
154 init_completion(&stuser->comp);
155 list_add_tail(&stuser->list, &stdev->mrpc_queue);
156
157 mrpc_cmd_submit(stdev);
158
159 return 0;
160}
161
162static void mrpc_complete_cmd(struct switchtec_dev *stdev)
163{
164 /* requires the mrpc_mutex to already be held when called */
165 struct switchtec_user *stuser;
166
167 if (list_empty(&stdev->mrpc_queue))
168 return;
169
170 stuser = list_entry(stdev->mrpc_queue.next, struct switchtec_user,
171 list);
172
173 stuser->status = ioread32(&stdev->mmio_mrpc->status);
174 if (stuser->status == SWITCHTEC_MRPC_STATUS_INPROGRESS)
175 return;
176
177 stuser_set_state(stuser, MRPC_DONE);
178 stuser->return_code = 0;
179
180 if (stuser->status != SWITCHTEC_MRPC_STATUS_DONE)
181 goto out;
182
183 stuser->return_code = ioread32(&stdev->mmio_mrpc->ret_value);
184 if (stuser->return_code != 0)
185 goto out;
186
187 memcpy_fromio(stuser->data, &stdev->mmio_mrpc->output_data,
188 stuser->read_len);
189
190out:
191 complete_all(&stuser->comp);
192 list_del_init(&stuser->list);
193 stuser_put(stuser);
194 stdev->mrpc_busy = 0;
195
196 mrpc_cmd_submit(stdev);
197}
198
199static void mrpc_event_work(struct work_struct *work)
200{
201 struct switchtec_dev *stdev;
202
203 stdev = container_of(work, struct switchtec_dev, mrpc_work);
204
205 dev_dbg(&stdev->dev, "%s\n", __func__);
206
207 mutex_lock(&stdev->mrpc_mutex);
208 cancel_delayed_work(&stdev->mrpc_timeout);
209 mrpc_complete_cmd(stdev);
210 mutex_unlock(&stdev->mrpc_mutex);
211}
212
213static void mrpc_timeout_work(struct work_struct *work)
214{
215 struct switchtec_dev *stdev;
216 u32 status;
217
218 stdev = container_of(work, struct switchtec_dev, mrpc_timeout.work);
219
220 dev_dbg(&stdev->dev, "%s\n", __func__);
221
222 mutex_lock(&stdev->mrpc_mutex);
223
224 status = ioread32(&stdev->mmio_mrpc->status);
225 if (status == SWITCHTEC_MRPC_STATUS_INPROGRESS) {
226 schedule_delayed_work(&stdev->mrpc_timeout,
227 msecs_to_jiffies(500));
228 goto out;
229 }
230
231 mrpc_complete_cmd(stdev);
232
233out:
234 mutex_unlock(&stdev->mrpc_mutex);
235}
236
237static ssize_t device_version_show(struct device *dev,
238 struct device_attribute *attr, char *buf)
239{
240 struct switchtec_dev *stdev = to_stdev(dev);
241 u32 ver;
242
243 ver = ioread32(&stdev->mmio_sys_info->device_version);
244
245 return sprintf(buf, "%x\n", ver);
246}
247static DEVICE_ATTR_RO(device_version);
248
249static ssize_t fw_version_show(struct device *dev,
250 struct device_attribute *attr, char *buf)
251{
252 struct switchtec_dev *stdev = to_stdev(dev);
253 u32 ver;
254
255 ver = ioread32(&stdev->mmio_sys_info->firmware_version);
256
257 return sprintf(buf, "%08x\n", ver);
258}
259static DEVICE_ATTR_RO(fw_version);
260
261static ssize_t io_string_show(char *buf, void __iomem *attr, size_t len)
262{
263 int i;
264
265 memcpy_fromio(buf, attr, len);
266 buf[len] = '\n';
267 buf[len + 1] = 0;
268
269 for (i = len - 1; i > 0; i--) {
270 if (buf[i] != ' ')
271 break;
272 buf[i] = '\n';
273 buf[i + 1] = 0;
274 }
275
276 return strlen(buf);
277}
278
279#define DEVICE_ATTR_SYS_INFO_STR(field) \
280static ssize_t field ## _show(struct device *dev, \
281 struct device_attribute *attr, char *buf) \
282{ \
283 struct switchtec_dev *stdev = to_stdev(dev); \
284 return io_string_show(buf, &stdev->mmio_sys_info->field, \
285 sizeof(stdev->mmio_sys_info->field)); \
286} \
287\
288static DEVICE_ATTR_RO(field)
289
290DEVICE_ATTR_SYS_INFO_STR(vendor_id);
291DEVICE_ATTR_SYS_INFO_STR(product_id);
292DEVICE_ATTR_SYS_INFO_STR(product_revision);
293DEVICE_ATTR_SYS_INFO_STR(component_vendor);
294
295static ssize_t component_id_show(struct device *dev,
296 struct device_attribute *attr, char *buf)
297{
298 struct switchtec_dev *stdev = to_stdev(dev);
299 int id = ioread16(&stdev->mmio_sys_info->component_id);
300
301 return sprintf(buf, "PM%04X\n", id);
302}
303static DEVICE_ATTR_RO(component_id);
304
305static ssize_t component_revision_show(struct device *dev,
306 struct device_attribute *attr, char *buf)
307{
308 struct switchtec_dev *stdev = to_stdev(dev);
309 int rev = ioread8(&stdev->mmio_sys_info->component_revision);
310
311 return sprintf(buf, "%d\n", rev);
312}
313static DEVICE_ATTR_RO(component_revision);
314
315static ssize_t partition_show(struct device *dev,
316 struct device_attribute *attr, char *buf)
317{
318 struct switchtec_dev *stdev = to_stdev(dev);
319
320 return sprintf(buf, "%d\n", stdev->partition);
321}
322static DEVICE_ATTR_RO(partition);
323
324static ssize_t partition_count_show(struct device *dev,
325 struct device_attribute *attr, char *buf)
326{
327 struct switchtec_dev *stdev = to_stdev(dev);
328
329 return sprintf(buf, "%d\n", stdev->partition_count);
330}
331static DEVICE_ATTR_RO(partition_count);
332
333static struct attribute *switchtec_device_attrs[] = {
334 &dev_attr_device_version.attr,
335 &dev_attr_fw_version.attr,
336 &dev_attr_vendor_id.attr,
337 &dev_attr_product_id.attr,
338 &dev_attr_product_revision.attr,
339 &dev_attr_component_vendor.attr,
340 &dev_attr_component_id.attr,
341 &dev_attr_component_revision.attr,
342 &dev_attr_partition.attr,
343 &dev_attr_partition_count.attr,
344 NULL,
345};
346
347ATTRIBUTE_GROUPS(switchtec_device);
348
349static int switchtec_dev_open(struct inode *inode, struct file *filp)
350{
351 struct switchtec_dev *stdev;
352 struct switchtec_user *stuser;
353
354 stdev = container_of(inode->i_cdev, struct switchtec_dev, cdev);
355
356 stuser = stuser_create(stdev);
357 if (IS_ERR(stuser))
358 return PTR_ERR(stuser);
359
360 filp->private_data = stuser;
361 nonseekable_open(inode, filp);
362
363 dev_dbg(&stdev->dev, "%s: %p\n", __func__, stuser);
364
365 return 0;
366}
367
368static int switchtec_dev_release(struct inode *inode, struct file *filp)
369{
370 struct switchtec_user *stuser = filp->private_data;
371
372 stuser_put(stuser);
373
374 return 0;
375}
376
377static int lock_mutex_and_test_alive(struct switchtec_dev *stdev)
378{
379 if (mutex_lock_interruptible(&stdev->mrpc_mutex))
380 return -EINTR;
381
382 if (!stdev->alive) {
383 mutex_unlock(&stdev->mrpc_mutex);
384 return -ENODEV;
385 }
386
387 return 0;
388}
389
390static ssize_t switchtec_dev_write(struct file *filp, const char __user *data,
391 size_t size, loff_t *off)
392{
393 struct switchtec_user *stuser = filp->private_data;
394 struct switchtec_dev *stdev = stuser->stdev;
395 int rc;
396
397 if (size < sizeof(stuser->cmd) ||
398 size > sizeof(stuser->cmd) + sizeof(stuser->data))
399 return -EINVAL;
400
401 stuser->data_len = size - sizeof(stuser->cmd);
402
403 rc = lock_mutex_and_test_alive(stdev);
404 if (rc)
405 return rc;
406
407 if (stuser->state != MRPC_IDLE) {
408 rc = -EBADE;
409 goto out;
410 }
411
412 rc = copy_from_user(&stuser->cmd, data, sizeof(stuser->cmd));
413 if (rc) {
414 rc = -EFAULT;
415 goto out;
416 }
417
418 data += sizeof(stuser->cmd);
419 rc = copy_from_user(&stuser->data, data, size - sizeof(stuser->cmd));
420 if (rc) {
421 rc = -EFAULT;
422 goto out;
423 }
424
425 rc = mrpc_queue_cmd(stuser);
426
427out:
428 mutex_unlock(&stdev->mrpc_mutex);
429
430 if (rc)
431 return rc;
432
433 return size;
434}
435
436static ssize_t switchtec_dev_read(struct file *filp, char __user *data,
437 size_t size, loff_t *off)
438{
439 struct switchtec_user *stuser = filp->private_data;
440 struct switchtec_dev *stdev = stuser->stdev;
441 int rc;
442
443 if (size < sizeof(stuser->cmd) ||
444 size > sizeof(stuser->cmd) + sizeof(stuser->data))
445 return -EINVAL;
446
447 rc = lock_mutex_and_test_alive(stdev);
448 if (rc)
449 return rc;
450
451 if (stuser->state == MRPC_IDLE) {
452 mutex_unlock(&stdev->mrpc_mutex);
453 return -EBADE;
454 }
455
456 stuser->read_len = size - sizeof(stuser->return_code);
457
458 mutex_unlock(&stdev->mrpc_mutex);
459
460 if (filp->f_flags & O_NONBLOCK) {
461 if (!try_wait_for_completion(&stuser->comp))
462 return -EAGAIN;
463 } else {
464 rc = wait_for_completion_interruptible(&stuser->comp);
465 if (rc < 0)
466 return rc;
467 }
468
469 rc = lock_mutex_and_test_alive(stdev);
470 if (rc)
471 return rc;
472
473 if (stuser->state != MRPC_DONE) {
474 mutex_unlock(&stdev->mrpc_mutex);
475 return -EBADE;
476 }
477
478 rc = copy_to_user(data, &stuser->return_code,
479 sizeof(stuser->return_code));
480 if (rc) {
481 rc = -EFAULT;
482 goto out;
483 }
484
485 data += sizeof(stuser->return_code);
486 rc = copy_to_user(data, &stuser->data,
487 size - sizeof(stuser->return_code));
488 if (rc) {
489 rc = -EFAULT;
490 goto out;
491 }
492
493 stuser_set_state(stuser, MRPC_IDLE);
494
495out:
496 mutex_unlock(&stdev->mrpc_mutex);
497
498 if (stuser->status == SWITCHTEC_MRPC_STATUS_DONE)
499 return size;
500 else if (stuser->status == SWITCHTEC_MRPC_STATUS_INTERRUPTED)
501 return -ENXIO;
502 else
503 return -EBADMSG;
504}
505
506static __poll_t switchtec_dev_poll(struct file *filp, poll_table *wait)
507{
508 struct switchtec_user *stuser = filp->private_data;
509 struct switchtec_dev *stdev = stuser->stdev;
510 __poll_t ret = 0;
511
512 poll_wait(filp, &stuser->comp.wait, wait);
513 poll_wait(filp, &stdev->event_wq, wait);
514
515 if (lock_mutex_and_test_alive(stdev))
516 return EPOLLIN | EPOLLRDHUP | EPOLLOUT | EPOLLERR | EPOLLHUP;
517
518 mutex_unlock(&stdev->mrpc_mutex);
519
520 if (try_wait_for_completion(&stuser->comp))
521 ret |= EPOLLIN | EPOLLRDNORM;
522
523 if (stuser->event_cnt != atomic_read(&stdev->event_cnt))
524 ret |= EPOLLPRI | EPOLLRDBAND;
525
526 return ret;
527}
528
529static int ioctl_flash_info(struct switchtec_dev *stdev,
530 struct switchtec_ioctl_flash_info __user *uinfo)
531{
532 struct switchtec_ioctl_flash_info info = {0};
533 struct flash_info_regs __iomem *fi = stdev->mmio_flash_info;
534
535 info.flash_length = ioread32(&fi->flash_length);
536 info.num_partitions = SWITCHTEC_IOCTL_NUM_PARTITIONS;
537
538 if (copy_to_user(uinfo, &info, sizeof(info)))
539 return -EFAULT;
540
541 return 0;
542}
543
544static void set_fw_info_part(struct switchtec_ioctl_flash_part_info *info,
545 struct partition_info __iomem *pi)
546{
547 info->address = ioread32(&pi->address);
548 info->length = ioread32(&pi->length);
549}
550
551static int ioctl_flash_part_info(struct switchtec_dev *stdev,
552 struct switchtec_ioctl_flash_part_info __user *uinfo)
553{
554 struct switchtec_ioctl_flash_part_info info = {0};
555 struct flash_info_regs __iomem *fi = stdev->mmio_flash_info;
556 struct sys_info_regs __iomem *si = stdev->mmio_sys_info;
557 u32 active_addr = -1;
558
559 if (copy_from_user(&info, uinfo, sizeof(info)))
560 return -EFAULT;
561
562 switch (info.flash_partition) {
563 case SWITCHTEC_IOCTL_PART_CFG0:
564 active_addr = ioread32(&fi->active_cfg);
565 set_fw_info_part(&info, &fi->cfg0);
566 if (ioread16(&si->cfg_running) == SWITCHTEC_CFG0_RUNNING)
567 info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
568 break;
569 case SWITCHTEC_IOCTL_PART_CFG1:
570 active_addr = ioread32(&fi->active_cfg);
571 set_fw_info_part(&info, &fi->cfg1);
572 if (ioread16(&si->cfg_running) == SWITCHTEC_CFG1_RUNNING)
573 info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
574 break;
575 case SWITCHTEC_IOCTL_PART_IMG0:
576 active_addr = ioread32(&fi->active_img);
577 set_fw_info_part(&info, &fi->img0);
578 if (ioread16(&si->img_running) == SWITCHTEC_IMG0_RUNNING)
579 info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
580 break;
581 case SWITCHTEC_IOCTL_PART_IMG1:
582 active_addr = ioread32(&fi->active_img);
583 set_fw_info_part(&info, &fi->img1);
584 if (ioread16(&si->img_running) == SWITCHTEC_IMG1_RUNNING)
585 info.active |= SWITCHTEC_IOCTL_PART_RUNNING;
586 break;
587 case SWITCHTEC_IOCTL_PART_NVLOG:
588 set_fw_info_part(&info, &fi->nvlog);
589 break;
590 case SWITCHTEC_IOCTL_PART_VENDOR0:
591 set_fw_info_part(&info, &fi->vendor[0]);
592 break;
593 case SWITCHTEC_IOCTL_PART_VENDOR1:
594 set_fw_info_part(&info, &fi->vendor[1]);
595 break;
596 case SWITCHTEC_IOCTL_PART_VENDOR2:
597 set_fw_info_part(&info, &fi->vendor[2]);
598 break;
599 case SWITCHTEC_IOCTL_PART_VENDOR3:
600 set_fw_info_part(&info, &fi->vendor[3]);
601 break;
602 case SWITCHTEC_IOCTL_PART_VENDOR4:
603 set_fw_info_part(&info, &fi->vendor[4]);
604 break;
605 case SWITCHTEC_IOCTL_PART_VENDOR5:
606 set_fw_info_part(&info, &fi->vendor[5]);
607 break;
608 case SWITCHTEC_IOCTL_PART_VENDOR6:
609 set_fw_info_part(&info, &fi->vendor[6]);
610 break;
611 case SWITCHTEC_IOCTL_PART_VENDOR7:
612 set_fw_info_part(&info, &fi->vendor[7]);
613 break;
614 default:
615 return -EINVAL;
616 }
617
618 if (info.address == active_addr)
619 info.active |= SWITCHTEC_IOCTL_PART_ACTIVE;
620
621 if (copy_to_user(uinfo, &info, sizeof(info)))
622 return -EFAULT;
623
624 return 0;
625}
626
627static int ioctl_event_summary(struct switchtec_dev *stdev,
628 struct switchtec_user *stuser,
629 struct switchtec_ioctl_event_summary __user *usum)
630{
631 struct switchtec_ioctl_event_summary s = {0};
632 int i;
633 u32 reg;
634
635 s.global = ioread32(&stdev->mmio_sw_event->global_summary);
636 s.part_bitmap = readq(&stdev->mmio_sw_event->part_event_bitmap);
637 s.local_part = ioread32(&stdev->mmio_part_cfg->part_event_summary);
638
639 for (i = 0; i < stdev->partition_count; i++) {
640 reg = ioread32(&stdev->mmio_part_cfg_all[i].part_event_summary);
641 s.part[i] = reg;
642 }
643
644 for (i = 0; i < SWITCHTEC_MAX_PFF_CSR; i++) {
645 reg = ioread16(&stdev->mmio_pff_csr[i].vendor_id);
646 if (reg != PCI_VENDOR_ID_MICROSEMI)
647 break;
648
649 reg = ioread32(&stdev->mmio_pff_csr[i].pff_event_summary);
650 s.pff[i] = reg;
651 }
652
653 if (copy_to_user(usum, &s, sizeof(s)))
654 return -EFAULT;
655
656 stuser->event_cnt = atomic_read(&stdev->event_cnt);
657
658 return 0;
659}
660
661static u32 __iomem *global_ev_reg(struct switchtec_dev *stdev,
662 size_t offset, int index)
663{
664 return (void __iomem *)stdev->mmio_sw_event + offset;
665}
666
667static u32 __iomem *part_ev_reg(struct switchtec_dev *stdev,
668 size_t offset, int index)
669{
670 return (void __iomem *)&stdev->mmio_part_cfg_all[index] + offset;
671}
672
673static u32 __iomem *pff_ev_reg(struct switchtec_dev *stdev,
674 size_t offset, int index)
675{
676 return (void __iomem *)&stdev->mmio_pff_csr[index] + offset;
677}
678
679#define EV_GLB(i, r)[i] = {offsetof(struct sw_event_regs, r), global_ev_reg}
680#define EV_PAR(i, r)[i] = {offsetof(struct part_cfg_regs, r), part_ev_reg}
681#define EV_PFF(i, r)[i] = {offsetof(struct pff_csr_regs, r), pff_ev_reg}
682
683static const struct event_reg {
684 size_t offset;
685 u32 __iomem *(*map_reg)(struct switchtec_dev *stdev,
686 size_t offset, int index);
687} event_regs[] = {
688 EV_GLB(SWITCHTEC_IOCTL_EVENT_STACK_ERROR, stack_error_event_hdr),
689 EV_GLB(SWITCHTEC_IOCTL_EVENT_PPU_ERROR, ppu_error_event_hdr),
690 EV_GLB(SWITCHTEC_IOCTL_EVENT_ISP_ERROR, isp_error_event_hdr),
691 EV_GLB(SWITCHTEC_IOCTL_EVENT_SYS_RESET, sys_reset_event_hdr),
692 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_EXC, fw_exception_hdr),
693 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_NMI, fw_nmi_hdr),
694 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_NON_FATAL, fw_non_fatal_hdr),
695 EV_GLB(SWITCHTEC_IOCTL_EVENT_FW_FATAL, fw_fatal_hdr),
696 EV_GLB(SWITCHTEC_IOCTL_EVENT_TWI_MRPC_COMP, twi_mrpc_comp_hdr),
697 EV_GLB(SWITCHTEC_IOCTL_EVENT_TWI_MRPC_COMP_ASYNC,
698 twi_mrpc_comp_async_hdr),
699 EV_GLB(SWITCHTEC_IOCTL_EVENT_CLI_MRPC_COMP, cli_mrpc_comp_hdr),
700 EV_GLB(SWITCHTEC_IOCTL_EVENT_CLI_MRPC_COMP_ASYNC,
701 cli_mrpc_comp_async_hdr),
702 EV_GLB(SWITCHTEC_IOCTL_EVENT_GPIO_INT, gpio_interrupt_hdr),
703 EV_GLB(SWITCHTEC_IOCTL_EVENT_GFMS, gfms_event_hdr),
704 EV_PAR(SWITCHTEC_IOCTL_EVENT_PART_RESET, part_reset_hdr),
705 EV_PAR(SWITCHTEC_IOCTL_EVENT_MRPC_COMP, mrpc_comp_hdr),
706 EV_PAR(SWITCHTEC_IOCTL_EVENT_MRPC_COMP_ASYNC, mrpc_comp_async_hdr),
707 EV_PAR(SWITCHTEC_IOCTL_EVENT_DYN_PART_BIND_COMP, dyn_binding_hdr),
708 EV_PFF(SWITCHTEC_IOCTL_EVENT_AER_IN_P2P, aer_in_p2p_hdr),
709 EV_PFF(SWITCHTEC_IOCTL_EVENT_AER_IN_VEP, aer_in_vep_hdr),
710 EV_PFF(SWITCHTEC_IOCTL_EVENT_DPC, dpc_hdr),
711 EV_PFF(SWITCHTEC_IOCTL_EVENT_CTS, cts_hdr),
712 EV_PFF(SWITCHTEC_IOCTL_EVENT_HOTPLUG, hotplug_hdr),
713 EV_PFF(SWITCHTEC_IOCTL_EVENT_IER, ier_hdr),
714 EV_PFF(SWITCHTEC_IOCTL_EVENT_THRESH, threshold_hdr),
715 EV_PFF(SWITCHTEC_IOCTL_EVENT_POWER_MGMT, power_mgmt_hdr),
716 EV_PFF(SWITCHTEC_IOCTL_EVENT_TLP_THROTTLING, tlp_throttling_hdr),
717 EV_PFF(SWITCHTEC_IOCTL_EVENT_FORCE_SPEED, force_speed_hdr),
718 EV_PFF(SWITCHTEC_IOCTL_EVENT_CREDIT_TIMEOUT, credit_timeout_hdr),
719 EV_PFF(SWITCHTEC_IOCTL_EVENT_LINK_STATE, link_state_hdr),
720};
721
722static u32 __iomem *event_hdr_addr(struct switchtec_dev *stdev,
723 int event_id, int index)
724{
725 size_t off;
726
727 if (event_id < 0 || event_id >= SWITCHTEC_IOCTL_MAX_EVENTS)
728 return ERR_PTR(-EINVAL);
729
730 off = event_regs[event_id].offset;
731
732 if (event_regs[event_id].map_reg == part_ev_reg) {
733 if (index == SWITCHTEC_IOCTL_EVENT_LOCAL_PART_IDX)
734 index = stdev->partition;
735 else if (index < 0 || index >= stdev->partition_count)
736 return ERR_PTR(-EINVAL);
737 } else if (event_regs[event_id].map_reg == pff_ev_reg) {
738 if (index < 0 || index >= stdev->pff_csr_count)
739 return ERR_PTR(-EINVAL);
740 }
741
742 return event_regs[event_id].map_reg(stdev, off, index);
743}
744
745static int event_ctl(struct switchtec_dev *stdev,
746 struct switchtec_ioctl_event_ctl *ctl)
747{
748 int i;
749 u32 __iomem *reg;
750 u32 hdr;
751
752 reg = event_hdr_addr(stdev, ctl->event_id, ctl->index);
753 if (IS_ERR(reg))
754 return PTR_ERR(reg);
755
756 hdr = ioread32(reg);
757 for (i = 0; i < ARRAY_SIZE(ctl->data); i++)
758 ctl->data[i] = ioread32(&reg[i + 1]);
759
760 ctl->occurred = hdr & SWITCHTEC_EVENT_OCCURRED;
761 ctl->count = (hdr >> 5) & 0xFF;
762
763 if (!(ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_CLEAR))
764 hdr &= ~SWITCHTEC_EVENT_CLEAR;
765 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL)
766 hdr |= SWITCHTEC_EVENT_EN_IRQ;
767 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_POLL)
768 hdr &= ~SWITCHTEC_EVENT_EN_IRQ;
769 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG)
770 hdr |= SWITCHTEC_EVENT_EN_LOG;
771 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_LOG)
772 hdr &= ~SWITCHTEC_EVENT_EN_LOG;
773 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI)
774 hdr |= SWITCHTEC_EVENT_EN_CLI;
775 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_CLI)
776 hdr &= ~SWITCHTEC_EVENT_EN_CLI;
777 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL)
778 hdr |= SWITCHTEC_EVENT_FATAL;
779 if (ctl->flags & SWITCHTEC_IOCTL_EVENT_FLAG_DIS_FATAL)
780 hdr &= ~SWITCHTEC_EVENT_FATAL;
781
782 if (ctl->flags)
783 iowrite32(hdr, reg);
784
785 ctl->flags = 0;
786 if (hdr & SWITCHTEC_EVENT_EN_IRQ)
787 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_POLL;
788 if (hdr & SWITCHTEC_EVENT_EN_LOG)
789 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_LOG;
790 if (hdr & SWITCHTEC_EVENT_EN_CLI)
791 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_CLI;
792 if (hdr & SWITCHTEC_EVENT_FATAL)
793 ctl->flags |= SWITCHTEC_IOCTL_EVENT_FLAG_EN_FATAL;
794
795 return 0;
796}
797
798static int ioctl_event_ctl(struct switchtec_dev *stdev,
799 struct switchtec_ioctl_event_ctl __user *uctl)
800{
801 int ret;
802 int nr_idxs;
803 unsigned int event_flags;
804 struct switchtec_ioctl_event_ctl ctl;
805
806 if (copy_from_user(&ctl, uctl, sizeof(ctl)))
807 return -EFAULT;
808
809 if (ctl.event_id >= SWITCHTEC_IOCTL_MAX_EVENTS)
810 return -EINVAL;
811
812 if (ctl.flags & SWITCHTEC_IOCTL_EVENT_FLAG_UNUSED)
813 return -EINVAL;
814
815 if (ctl.index == SWITCHTEC_IOCTL_EVENT_IDX_ALL) {
816 if (event_regs[ctl.event_id].map_reg == global_ev_reg)
817 nr_idxs = 1;
818 else if (event_regs[ctl.event_id].map_reg == part_ev_reg)
819 nr_idxs = stdev->partition_count;
820 else if (event_regs[ctl.event_id].map_reg == pff_ev_reg)
821 nr_idxs = stdev->pff_csr_count;
822 else
823 return -EINVAL;
824
825 event_flags = ctl.flags;
826 for (ctl.index = 0; ctl.index < nr_idxs; ctl.index++) {
827 ctl.flags = event_flags;
828 ret = event_ctl(stdev, &ctl);
829 if (ret < 0)
830 return ret;
831 }
832 } else {
833 ret = event_ctl(stdev, &ctl);
834 if (ret < 0)
835 return ret;
836 }
837
838 if (copy_to_user(uctl, &ctl, sizeof(ctl)))
839 return -EFAULT;
840
841 return 0;
842}
843
844static int ioctl_pff_to_port(struct switchtec_dev *stdev,
845 struct switchtec_ioctl_pff_port *up)
846{
847 int i, part;
848 u32 reg;
849 struct part_cfg_regs *pcfg;
850 struct switchtec_ioctl_pff_port p;
851
852 if (copy_from_user(&p, up, sizeof(p)))
853 return -EFAULT;
854
855 p.port = -1;
856 for (part = 0; part < stdev->partition_count; part++) {
857 pcfg = &stdev->mmio_part_cfg_all[part];
858 p.partition = part;
859
860 reg = ioread32(&pcfg->usp_pff_inst_id);
861 if (reg == p.pff) {
862 p.port = 0;
863 break;
864 }
865
866 reg = ioread32(&pcfg->vep_pff_inst_id);
867 if (reg == p.pff) {
868 p.port = SWITCHTEC_IOCTL_PFF_VEP;
869 break;
870 }
871
872 for (i = 0; i < ARRAY_SIZE(pcfg->dsp_pff_inst_id); i++) {
873 reg = ioread32(&pcfg->dsp_pff_inst_id[i]);
874 if (reg != p.pff)
875 continue;
876
877 p.port = i + 1;
878 break;
879 }
880
881 if (p.port != -1)
882 break;
883 }
884
885 if (copy_to_user(up, &p, sizeof(p)))
886 return -EFAULT;
887
888 return 0;
889}
890
891static int ioctl_port_to_pff(struct switchtec_dev *stdev,
892 struct switchtec_ioctl_pff_port *up)
893{
894 struct switchtec_ioctl_pff_port p;
895 struct part_cfg_regs *pcfg;
896
897 if (copy_from_user(&p, up, sizeof(p)))
898 return -EFAULT;
899
900 if (p.partition == SWITCHTEC_IOCTL_EVENT_LOCAL_PART_IDX)
901 pcfg = stdev->mmio_part_cfg;
902 else if (p.partition < stdev->partition_count)
903 pcfg = &stdev->mmio_part_cfg_all[p.partition];
904 else
905 return -EINVAL;
906
907 switch (p.port) {
908 case 0:
909 p.pff = ioread32(&pcfg->usp_pff_inst_id);
910 break;
911 case SWITCHTEC_IOCTL_PFF_VEP:
912 p.pff = ioread32(&pcfg->vep_pff_inst_id);
913 break;
914 default:
915 if (p.port > ARRAY_SIZE(pcfg->dsp_pff_inst_id))
916 return -EINVAL;
917 p.port = array_index_nospec(p.port,
918 ARRAY_SIZE(pcfg->dsp_pff_inst_id) + 1);
919 p.pff = ioread32(&pcfg->dsp_pff_inst_id[p.port - 1]);
920 break;
921 }
922
923 if (copy_to_user(up, &p, sizeof(p)))
924 return -EFAULT;
925
926 return 0;
927}
928
929static long switchtec_dev_ioctl(struct file *filp, unsigned int cmd,
930 unsigned long arg)
931{
932 struct switchtec_user *stuser = filp->private_data;
933 struct switchtec_dev *stdev = stuser->stdev;
934 int rc;
935 void __user *argp = (void __user *)arg;
936
937 rc = lock_mutex_and_test_alive(stdev);
938 if (rc)
939 return rc;
940
941 switch (cmd) {
942 case SWITCHTEC_IOCTL_FLASH_INFO:
943 rc = ioctl_flash_info(stdev, argp);
944 break;
945 case SWITCHTEC_IOCTL_FLASH_PART_INFO:
946 rc = ioctl_flash_part_info(stdev, argp);
947 break;
948 case SWITCHTEC_IOCTL_EVENT_SUMMARY:
949 rc = ioctl_event_summary(stdev, stuser, argp);
950 break;
951 case SWITCHTEC_IOCTL_EVENT_CTL:
952 rc = ioctl_event_ctl(stdev, argp);
953 break;
954 case SWITCHTEC_IOCTL_PFF_TO_PORT:
955 rc = ioctl_pff_to_port(stdev, argp);
956 break;
957 case SWITCHTEC_IOCTL_PORT_TO_PFF:
958 rc = ioctl_port_to_pff(stdev, argp);
959 break;
960 default:
961 rc = -ENOTTY;
962 break;
963 }
964
965 mutex_unlock(&stdev->mrpc_mutex);
966 return rc;
967}
968
969static const struct file_operations switchtec_fops = {
970 .owner = THIS_MODULE,
971 .open = switchtec_dev_open,
972 .release = switchtec_dev_release,
973 .write = switchtec_dev_write,
974 .read = switchtec_dev_read,
975 .poll = switchtec_dev_poll,
976 .unlocked_ioctl = switchtec_dev_ioctl,
977 .compat_ioctl = switchtec_dev_ioctl,
978};
979
980static void link_event_work(struct work_struct *work)
981{
982 struct switchtec_dev *stdev;
983
984 stdev = container_of(work, struct switchtec_dev, link_event_work);
985
986 if (stdev->link_notifier)
987 stdev->link_notifier(stdev);
988}
989
990static void check_link_state_events(struct switchtec_dev *stdev)
991{
992 int idx;
993 u32 reg;
994 int count;
995 int occurred = 0;
996
997 for (idx = 0; idx < stdev->pff_csr_count; idx++) {
998 reg = ioread32(&stdev->mmio_pff_csr[idx].link_state_hdr);
999 dev_dbg(&stdev->dev, "link_state: %d->%08x\n", idx, reg);
1000 count = (reg >> 5) & 0xFF;
1001
1002 if (count != stdev->link_event_count[idx]) {
1003 occurred = 1;
1004 stdev->link_event_count[idx] = count;
1005 }
1006 }
1007
1008 if (occurred)
1009 schedule_work(&stdev->link_event_work);
1010}
1011
1012static void enable_link_state_events(struct switchtec_dev *stdev)
1013{
1014 int idx;
1015
1016 for (idx = 0; idx < stdev->pff_csr_count; idx++) {
1017 iowrite32(SWITCHTEC_EVENT_CLEAR |
1018 SWITCHTEC_EVENT_EN_IRQ,
1019 &stdev->mmio_pff_csr[idx].link_state_hdr);
1020 }
1021}
1022
1023static void stdev_release(struct device *dev)
1024{
1025 struct switchtec_dev *stdev = to_stdev(dev);
1026
1027 kfree(stdev);
1028}
1029
1030static void stdev_kill(struct switchtec_dev *stdev)
1031{
1032 struct switchtec_user *stuser, *tmpuser;
1033
1034 pci_clear_master(stdev->pdev);
1035
1036 cancel_delayed_work_sync(&stdev->mrpc_timeout);
1037
1038 /* Mark the hardware as unavailable and complete all completions */
1039 mutex_lock(&stdev->mrpc_mutex);
1040 stdev->alive = false;
1041
1042 /* Wake up and kill any users waiting on an MRPC request */
1043 list_for_each_entry_safe(stuser, tmpuser, &stdev->mrpc_queue, list) {
1044 complete_all(&stuser->comp);
1045 list_del_init(&stuser->list);
1046 stuser_put(stuser);
1047 }
1048
1049 mutex_unlock(&stdev->mrpc_mutex);
1050
1051 /* Wake up any users waiting on event_wq */
1052 wake_up_interruptible(&stdev->event_wq);
1053}
1054
1055static struct switchtec_dev *stdev_create(struct pci_dev *pdev)
1056{
1057 struct switchtec_dev *stdev;
1058 int minor;
1059 struct device *dev;
1060 struct cdev *cdev;
1061 int rc;
1062
1063 stdev = kzalloc_node(sizeof(*stdev), GFP_KERNEL,
1064 dev_to_node(&pdev->dev));
1065 if (!stdev)
1066 return ERR_PTR(-ENOMEM);
1067
1068 stdev->alive = true;
1069 stdev->pdev = pdev;
1070 INIT_LIST_HEAD(&stdev->mrpc_queue);
1071 mutex_init(&stdev->mrpc_mutex);
1072 stdev->mrpc_busy = 0;
1073 INIT_WORK(&stdev->mrpc_work, mrpc_event_work);
1074 INIT_DELAYED_WORK(&stdev->mrpc_timeout, mrpc_timeout_work);
1075 INIT_WORK(&stdev->link_event_work, link_event_work);
1076 init_waitqueue_head(&stdev->event_wq);
1077 atomic_set(&stdev->event_cnt, 0);
1078
1079 dev = &stdev->dev;
1080 device_initialize(dev);
1081 dev->class = switchtec_class;
1082 dev->parent = &pdev->dev;
1083 dev->groups = switchtec_device_groups;
1084 dev->release = stdev_release;
1085
1086 minor = ida_simple_get(&switchtec_minor_ida, 0, 0,
1087 GFP_KERNEL);
1088 if (minor < 0) {
1089 rc = minor;
1090 goto err_put;
1091 }
1092
1093 dev->devt = MKDEV(MAJOR(switchtec_devt), minor);
1094 dev_set_name(dev, "switchtec%d", minor);
1095
1096 cdev = &stdev->cdev;
1097 cdev_init(cdev, &switchtec_fops);
1098 cdev->owner = THIS_MODULE;
1099
1100 return stdev;
1101
1102err_put:
1103 put_device(&stdev->dev);
1104 return ERR_PTR(rc);
1105}
1106
1107static int mask_event(struct switchtec_dev *stdev, int eid, int idx)
1108{
1109 size_t off = event_regs[eid].offset;
1110 u32 __iomem *hdr_reg;
1111 u32 hdr;
1112
1113 hdr_reg = event_regs[eid].map_reg(stdev, off, idx);
1114 hdr = ioread32(hdr_reg);
1115
1116 if (!(hdr & SWITCHTEC_EVENT_OCCURRED && hdr & SWITCHTEC_EVENT_EN_IRQ))
1117 return 0;
1118
1119 if (eid == SWITCHTEC_IOCTL_EVENT_LINK_STATE ||
1120 eid == SWITCHTEC_IOCTL_EVENT_MRPC_COMP)
1121 return 0;
1122
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);
1126
1127 return 1;
1128}
1129
1130static int mask_all_events(struct switchtec_dev *stdev, int eid)
1131{
1132 int idx;
1133 int count = 0;
1134
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])
1141 continue;
1142
1143 count += mask_event(stdev, eid, idx);
1144 }
1145 } else {
1146 count += mask_event(stdev, eid, 0);
1147 }
1148
1149 return count;
1150}
1151
1152static irqreturn_t switchtec_event_isr(int irq, void *dev)
1153{
1154 struct switchtec_dev *stdev = dev;
1155 u32 reg;
1156 irqreturn_t ret = IRQ_NONE;
1157 int eid, event_count = 0;
1158
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__);
1162 ret = IRQ_HANDLED;
1163 schedule_work(&stdev->mrpc_work);
1164 iowrite32(reg, &stdev->mmio_part_cfg->mrpc_comp_hdr);
1165 }
1166
1167 check_link_state_events(stdev);
1168
1169 for (eid = 0; eid < SWITCHTEC_IOCTL_MAX_EVENTS; eid++)
1170 event_count += mask_all_events(stdev, eid);
1171
1172 if (event_count) {
1173 atomic_inc(&stdev->event_cnt);
1174 wake_up_interruptible(&stdev->event_wq);
1175 dev_dbg(&stdev->dev, "%s: %d events\n", __func__,
1176 event_count);
1177 return IRQ_HANDLED;
1178 }
1179
1180 return ret;
1181}
1182
1183static int switchtec_init_isr(struct switchtec_dev *stdev)
1184{
1185 int nvecs;
1186 int event_irq;
1187
1188 nvecs = pci_alloc_irq_vectors(stdev->pdev, 1, 4,
1189 PCI_IRQ_MSIX | PCI_IRQ_MSI);
1190 if (nvecs < 0)
1191 return nvecs;
1192
1193 event_irq = ioread32(&stdev->mmio_part_cfg->vep_vector_number);
1194 if (event_irq < 0 || event_irq >= nvecs)
1195 return -EFAULT;
1196
1197 event_irq = pci_irq_vector(stdev->pdev, event_irq);
1198 if (event_irq < 0)
1199 return event_irq;
1200
1201 return devm_request_irq(&stdev->pdev->dev, event_irq,
1202 switchtec_event_isr, 0,
1203 KBUILD_MODNAME, stdev);
1204}
1205
1206static void init_pff(struct switchtec_dev *stdev)
1207{
1208 int i;
1209 u32 reg;
1210 struct part_cfg_regs *pcfg = stdev->mmio_part_cfg;
1211
1212 for (i = 0; i < SWITCHTEC_MAX_PFF_CSR; i++) {
1213 reg = ioread16(&stdev->mmio_pff_csr[i].vendor_id);
1214 if (reg != PCI_VENDOR_ID_MICROSEMI)
1215 break;
1216 }
1217
1218 stdev->pff_csr_count = i;
1219
1220 reg = ioread32(&pcfg->usp_pff_inst_id);
1221 if (reg < SWITCHTEC_MAX_PFF_CSR)
1222 stdev->pff_local[reg] = 1;
1223
1224 reg = ioread32(&pcfg->vep_pff_inst_id);
1225 if (reg < SWITCHTEC_MAX_PFF_CSR)
1226 stdev->pff_local[reg] = 1;
1227
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;
1232 }
1233}
1234
1235static int switchtec_init_pci(struct switchtec_dev *stdev,
1236 struct pci_dev *pdev)
1237{
1238 int rc;
1239
1240 rc = pcim_enable_device(pdev);
1241 if (rc)
1242 return rc;
1243
1244 rc = pcim_iomap_regions(pdev, 0x1, KBUILD_MODNAME);
1245 if (rc)
1246 return rc;
1247
1248 pci_set_master(pdev);
1249
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;
1261
1262 if (stdev->partition_count < 1)
1263 stdev->partition_count = 1;
1264
1265 init_pff(stdev);
1266
1267 pci_set_drvdata(pdev, stdev);
1268
1269 return 0;
1270}
1271
1272static int switchtec_pci_probe(struct pci_dev *pdev,
1273 const struct pci_device_id *id)
1274{
1275 struct switchtec_dev *stdev;
1276 int rc;
1277
1278 if (pdev->class == (PCI_CLASS_BRIDGE_OTHER << 8))
1279 request_module_nowait("ntb_hw_switchtec");
1280
1281 stdev = stdev_create(pdev);
1282 if (IS_ERR(stdev))
1283 return PTR_ERR(stdev);
1284
1285 rc = switchtec_init_pci(stdev, pdev);
1286 if (rc)
1287 goto err_put;
1288
1289 rc = switchtec_init_isr(stdev);
1290 if (rc) {
1291 dev_err(&stdev->dev, "failed to init isr.\n");
1292 goto err_put;
1293 }
1294
1295 iowrite32(SWITCHTEC_EVENT_CLEAR |
1296 SWITCHTEC_EVENT_EN_IRQ,
1297 &stdev->mmio_part_cfg->mrpc_comp_hdr);
1298 enable_link_state_events(stdev);
1299
1300 rc = cdev_device_add(&stdev->cdev, &stdev->dev);
1301 if (rc)
1302 goto err_devadd;
1303
1304 dev_info(&stdev->dev, "Management device registered.\n");
1305
1306 return 0;
1307
1308err_devadd:
1309 stdev_kill(stdev);
1310err_put:
1311 ida_simple_remove(&switchtec_minor_ida, MINOR(stdev->dev.devt));
1312 put_device(&stdev->dev);
1313 return rc;
1314}
1315
1316static void switchtec_pci_remove(struct pci_dev *pdev)
1317{
1318 struct switchtec_dev *stdev = pci_get_drvdata(pdev);
1319
1320 pci_set_drvdata(pdev, NULL);
1321
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");
1325
1326 stdev_kill(stdev);
1327 put_device(&stdev->dev);
1328}
1329
1330#define SWITCHTEC_PCI_DEVICE(device_id) \
1331 { \
1332 .vendor = PCI_VENDOR_ID_MICROSEMI, \
1333 .device = device_id, \
1334 .subvendor = PCI_ANY_ID, \
1335 .subdevice = PCI_ANY_ID, \
1336 .class = (PCI_CLASS_MEMORY_OTHER << 8), \
1337 .class_mask = 0xFFFFFFFF, \
1338 }, \
1339 { \
1340 .vendor = PCI_VENDOR_ID_MICROSEMI, \
1341 .device = device_id, \
1342 .subvendor = PCI_ANY_ID, \
1343 .subdevice = PCI_ANY_ID, \
1344 .class = (PCI_CLASS_BRIDGE_OTHER << 8), \
1345 .class_mask = 0xFFFFFFFF, \
1346 }
1347
1348static 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(0x8541), //PSX 24xG3
1356 SWITCHTEC_PCI_DEVICE(0x8542), //PSX 32xG3
1357 SWITCHTEC_PCI_DEVICE(0x8543), //PSX 48xG3
1358 SWITCHTEC_PCI_DEVICE(0x8544), //PSX 64xG3
1359 SWITCHTEC_PCI_DEVICE(0x8545), //PSX 80xG3
1360 SWITCHTEC_PCI_DEVICE(0x8546), //PSX 96xG3
1361 SWITCHTEC_PCI_DEVICE(0x8551), //PAX 24XG3
1362 SWITCHTEC_PCI_DEVICE(0x8552), //PAX 32XG3
1363 SWITCHTEC_PCI_DEVICE(0x8553), //PAX 48XG3
1364 SWITCHTEC_PCI_DEVICE(0x8554), //PAX 64XG3
1365 SWITCHTEC_PCI_DEVICE(0x8555), //PAX 80XG3
1366 SWITCHTEC_PCI_DEVICE(0x8556), //PAX 96XG3
1367 SWITCHTEC_PCI_DEVICE(0x8561), //PFXL 24XG3
1368 SWITCHTEC_PCI_DEVICE(0x8562), //PFXL 32XG3
1369 SWITCHTEC_PCI_DEVICE(0x8563), //PFXL 48XG3
1370 SWITCHTEC_PCI_DEVICE(0x8564), //PFXL 64XG3
1371 SWITCHTEC_PCI_DEVICE(0x8565), //PFXL 80XG3
1372 SWITCHTEC_PCI_DEVICE(0x8566), //PFXL 96XG3
1373 SWITCHTEC_PCI_DEVICE(0x8571), //PFXI 24XG3
1374 SWITCHTEC_PCI_DEVICE(0x8572), //PFXI 32XG3
1375 SWITCHTEC_PCI_DEVICE(0x8573), //PFXI 48XG3
1376 SWITCHTEC_PCI_DEVICE(0x8574), //PFXI 64XG3
1377 SWITCHTEC_PCI_DEVICE(0x8575), //PFXI 80XG3
1378 SWITCHTEC_PCI_DEVICE(0x8576), //PFXI 96XG3
1379 {0}
1380};
1381MODULE_DEVICE_TABLE(pci, switchtec_pci_tbl);
1382
1383static struct pci_driver switchtec_pci_driver = {
1384 .name = KBUILD_MODNAME,
1385 .id_table = switchtec_pci_tbl,
1386 .probe = switchtec_pci_probe,
1387 .remove = switchtec_pci_remove,
1388};
1389
1390static int __init switchtec_init(void)
1391{
1392 int rc;
1393
1394 rc = alloc_chrdev_region(&switchtec_devt, 0, max_devices,
1395 "switchtec");
1396 if (rc)
1397 return rc;
1398
1399 switchtec_class = class_create(THIS_MODULE, "switchtec");
1400 if (IS_ERR(switchtec_class)) {
1401 rc = PTR_ERR(switchtec_class);
1402 goto err_create_class;
1403 }
1404
1405 rc = pci_register_driver(&switchtec_pci_driver);
1406 if (rc)
1407 goto err_pci_register;
1408
1409 pr_info(KBUILD_MODNAME ": loaded.\n");
1410
1411 return 0;
1412
1413err_pci_register:
1414 class_destroy(switchtec_class);
1415
1416err_create_class:
1417 unregister_chrdev_region(switchtec_devt, max_devices);
1418
1419 return rc;
1420}
1421module_init(switchtec_init);
1422
1423static void __exit switchtec_exit(void)
1424{
1425 pci_unregister_driver(&switchtec_pci_driver);
1426 class_destroy(switchtec_class);
1427 unregister_chrdev_region(switchtec_devt, max_devices);
1428 ida_destroy(&switchtec_minor_ida);
1429
1430 pr_info(KBUILD_MODNAME ": unloaded.\n");
1431}
1432module_exit(switchtec_exit);