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