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