1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2016-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "linux_nvswitch.h"
25 
26 #include <linux/version.h>
27 
28 #include "conftest.h"
29 #include "nvlink_errors.h"
30 #include "nvlink_linux.h"
31 #include "nvCpuUuid.h"
32 #include "nv-time.h"
33 #include "nvlink_caps.h"
34 #include "nvlink_proto.h"
35 
36 #include <linux/module.h>
37 #include <linux/interrupt.h>
38 #include <linux/cdev.h>
39 #include <linux/fs.h>
40 #include <linux/slab.h>
41 #include <linux/uaccess.h>
42 #include <linux/poll.h>
43 #include <linux/sched.h>
44 #include <linux/time.h>
45 #include <linux/string.h>
46 #include <linux/moduleparam.h>
47 #include <linux/ctype.h>
48 #include <linux/wait.h>
49 #include <linux/jiffies.h>
50 
51 #include "ioctl_nvswitch.h"
52 
53 static const struct
54 {
55     NvlStatus status;
56     int err;
57 } nvswitch_status_map[] = {
58     { NVL_ERR_GENERIC,                  -EIO        },
59     { NVL_NO_MEM,                       -ENOMEM     },
60     { NVL_BAD_ARGS,                     -EINVAL     },
61     { NVL_ERR_INVALID_STATE,            -EIO        },
62     { NVL_ERR_NOT_SUPPORTED,            -EOPNOTSUPP },
63     { NVL_NOT_FOUND,                    -EINVAL     },
64     { NVL_ERR_STATE_IN_USE,             -EBUSY      },
65     { NVL_ERR_NOT_IMPLEMENTED,          -ENOSYS     },
66     { NVL_ERR_INSUFFICIENT_PERMISSIONS, -EPERM      },
67     { NVL_ERR_OPERATING_SYSTEM,         -EIO        },
68     { NVL_MORE_PROCESSING_REQUIRED,     -EAGAIN     },
69     { NVL_SUCCESS,                       0          },
70 };
71 
72 int
nvswitch_map_status(NvlStatus status)73 nvswitch_map_status
74 (
75     NvlStatus status
76 )
77 {
78     int err = -EIO;
79     NvU32 i;
80     NvU32 limit = sizeof(nvswitch_status_map) / sizeof(nvswitch_status_map[0]);
81 
82     for (i = 0; i < limit; i++)
83     {
84         if (nvswitch_status_map[i].status == status ||
85             nvswitch_status_map[i].status == -status)
86         {
87             err = nvswitch_status_map[i].err;
88             break;
89         }
90     }
91 
92     return err;
93 }
94 
95 #if !defined(IRQF_SHARED)
96 #define IRQF_SHARED SA_SHIRQ
97 #endif
98 
99 #define NV_FILE_INODE(file) (file)->f_inode
100 
101 static int nvswitch_probe(struct pci_dev *, const struct pci_device_id *);
102 static void nvswitch_remove(struct pci_dev *);
103 
104 static struct pci_device_id nvswitch_pci_table[] =
105 {
106     {
107         .vendor      = PCI_VENDOR_ID_NVIDIA,
108         .device      = PCI_ANY_ID,
109         .subvendor   = PCI_ANY_ID,
110         .subdevice   = PCI_ANY_ID,
111         .class       = (PCI_CLASS_BRIDGE_OTHER << 8),
112         .class_mask  = ~0
113     },
114     {}
115 };
116 
117 static struct pci_driver nvswitch_pci_driver =
118 {
119     .name           = NVSWITCH_DRIVER_NAME,
120     .id_table       = nvswitch_pci_table,
121     .probe          = nvswitch_probe,
122     .remove         = nvswitch_remove,
123     .shutdown       = nvswitch_remove
124 };
125 
126 //
127 // nvidia_nvswitch_mknod uses minor number 255 to create nvidia-nvswitchctl
128 // node. Hence, if NVSWITCH_CTL_MINOR is changed, then NV_NVSWITCH_CTL_MINOR
129 // should be updated. See nvdia-modprobe-utils.h
130 //
131 #define NVSWITCH_CTL_MINOR 255
132 #define NVSWITCH_MINOR_COUNT (NVSWITCH_CTL_MINOR + 1)
133 
134 // 32 bit hex value - including 0x prefix. (10 chars)
135 #define NVSWITCH_REGKEY_VALUE_LEN 10
136 
137 static char *NvSwitchRegDwords;
138 module_param(NvSwitchRegDwords, charp, 0);
139 MODULE_PARM_DESC(NvSwitchRegDwords, "NvSwitch regkey");
140 
141 static char *NvSwitchBlacklist;
142 module_param(NvSwitchBlacklist, charp, 0);
143 MODULE_PARM_DESC(NvSwitchBlacklist, "NvSwitchBlacklist=uuid[,uuid...]");
144 
145 //
146 // Locking:
147 //   We handle nvswitch driver locking in the OS layer. The nvswitch lib
148 //   layer does not have its own locking. It relies on the OS layer for
149 //   atomicity.
150 //
151 //   All locking is done with sleep locks. We use threaded MSI interrupts to
152 //   facilitate this.
153 //
154 //   When handling a request from a user context we use the interruptible
155 //   version to enable a quick ^C return if there is lock contention.
156 //
157 //   nvswitch.driver_mutex is used to protect driver's global state, "struct
158 //   NVSWITCH". The driver_mutex is taken during .probe, .remove, .open,
159 //   .close, and nvswitch-ctl .ioctl operations.
160 //
161 //   nvswitch_dev.device_mutex is used to protect per-device state, "struct
162 //   NVSWITCH_DEV", once a device is opened. The device_mutex is taken during
163 //   .ioctl, .poll and other background tasks.
164 //
165 //   The kernel guarantees that .close won't happen while .ioctl and .poll
166 //   are going on and without successful .open one can't execute any file ops.
167 //   This behavior guarantees correctness of the locking model.
168 //
169 //   If .close is invoked and holding the lock which is also used by threaded
170 //   tasks such as interrupt, driver will deadlock while trying to stop such
171 //   tasks. For example, when threaded interrupts are enabled, free_irq() calls
172 //   kthread_stop() to flush pending interrupt tasks. The locking model
173 //   makes sure that such deadlock cases don't happen.
174 //
175 // Lock ordering:
176 //   nvswitch.driver_mutex
177 //   nvswitch_dev.device_mutex
178 //
179 // Note:
180 //   Due to bug 2856314, nvswitch_dev.device_mutex is taken when calling
181 //   nvswitch_post_init_device() in nvswitch_probe().
182 //
183 
184 // Per-chip driver state is defined in linux_nvswitch.h
185 
186 // Global driver state
187 typedef struct
188 {
189     NvBool initialized;
190     struct cdev cdev;
191     struct cdev cdev_ctl;
192     dev_t devno;
193     atomic_t count;
194     struct mutex driver_mutex;
195     struct list_head devices;
196 } NVSWITCH;
197 
198 static NVSWITCH nvswitch = {0};
199 
200 // NvSwitch event
201 typedef struct nvswitch_event_t
202 {
203     wait_queue_head_t wait_q_event;
204     NvBool            event_pending;
205 } nvswitch_event_t;
206 
207 typedef struct nvswitch_file_private
208 {
209     NVSWITCH_DEV     *nvswitch_dev;
210     nvswitch_event_t file_event;
211     struct
212     {
213         /* A duped file descriptor for fabric_mgmt capability */
214         int fabric_mgmt;
215     } capability_fds;
216 } nvswitch_file_private_t;
217 
218 #define NVSWITCH_SET_FILE_PRIVATE(filp, data) ((filp)->private_data = (data))
219 #define NVSWITCH_GET_FILE_PRIVATE(filp) ((nvswitch_file_private_t *)(filp)->private_data)
220 
221 static int nvswitch_device_open(struct inode *inode, struct file *file);
222 static int nvswitch_device_release(struct inode *inode, struct file *file);
223 static unsigned int nvswitch_device_poll(struct file *file, poll_table *wait);
224 static int nvswitch_device_ioctl(struct inode *inode,
225                                  struct file *file,
226                                  unsigned int cmd,
227                                  unsigned long arg);
228 static long nvswitch_device_unlocked_ioctl(struct file *file,
229                                            unsigned int cmd,
230                                            unsigned long arg);
231 
232 static int nvswitch_ctl_ioctl(struct inode *inode,
233                               struct file *file,
234                               unsigned int cmd,
235                               unsigned long arg);
236 static long nvswitch_ctl_unlocked_ioctl(struct file *file,
237                                         unsigned int cmd,
238                                         unsigned long arg);
239 
240 struct file_operations device_fops =
241 {
242     .owner = THIS_MODULE,
243     .unlocked_ioctl = nvswitch_device_unlocked_ioctl,
244     .open    = nvswitch_device_open,
245     .release = nvswitch_device_release,
246     .poll    = nvswitch_device_poll
247 };
248 
249 struct file_operations ctl_fops =
250 {
251     .owner = THIS_MODULE,
252     .unlocked_ioctl = nvswitch_ctl_unlocked_ioctl,
253 };
254 
255 static int nvswitch_initialize_device_interrupt(NVSWITCH_DEV *nvswitch_dev);
256 static void nvswitch_shutdown_device_interrupt(NVSWITCH_DEV *nvswitch_dev);
257 static void nvswitch_load_bar_info(NVSWITCH_DEV *nvswitch_dev);
258 static void nvswitch_task_dispatch(NVSWITCH_DEV *nvswitch_dev);
259 
260 static NvBool
nvswitch_is_device_blacklisted(NVSWITCH_DEV * nvswitch_dev)261 nvswitch_is_device_blacklisted
262 (
263     NVSWITCH_DEV *nvswitch_dev
264 )
265 {
266     NVSWITCH_DEVICE_FABRIC_STATE device_fabric_state = 0;
267     NvlStatus status;
268 
269     status = nvswitch_lib_read_fabric_state(nvswitch_dev->lib_device,
270                                             &device_fabric_state, NULL, NULL);
271 
272     if (status != NVL_SUCCESS)
273     {
274         printk(KERN_INFO "%s: Failed to read fabric state, %x\n", nvswitch_dev->name, status);
275         return NV_FALSE;
276     }
277 
278     return device_fabric_state == NVSWITCH_DEVICE_FABRIC_STATE_BLACKLISTED;
279 }
280 
281 static void
nvswitch_deinit_background_tasks(NVSWITCH_DEV * nvswitch_dev)282 nvswitch_deinit_background_tasks
283 (
284     NVSWITCH_DEV *nvswitch_dev
285 )
286 {
287     NV_ATOMIC_SET(nvswitch_dev->task_q_ready, 0);
288 
289     wake_up(&nvswitch_dev->wait_q_shutdown);
290 
291     nv_kthread_q_stop(&nvswitch_dev->task_q);
292 }
293 
294 static int
nvswitch_init_background_tasks(NVSWITCH_DEV * nvswitch_dev)295 nvswitch_init_background_tasks
296 (
297     NVSWITCH_DEV *nvswitch_dev
298 )
299 {
300     int rc;
301 
302     rc = nv_kthread_q_init(&nvswitch_dev->task_q, nvswitch_dev->sname);
303     if (rc)
304     {
305         printk(KERN_ERR "%s: Failed to create task queue\n", nvswitch_dev->name);
306         return rc;
307     }
308 
309     NV_ATOMIC_SET(nvswitch_dev->task_q_ready, 1);
310 
311     nv_kthread_q_item_init(&nvswitch_dev->task_item,
312                            (nv_q_func_t) &nvswitch_task_dispatch,
313                            nvswitch_dev);
314 
315     if (!nv_kthread_q_schedule_q_item(&nvswitch_dev->task_q,
316                                       &nvswitch_dev->task_item))
317     {
318         printk(KERN_ERR "%s: Failed to schedule an item\n",nvswitch_dev->name);
319         rc = -ENODEV;
320         goto init_background_task_failed;
321     }
322 
323     return 0;
324 
325 init_background_task_failed:
326     nvswitch_deinit_background_tasks(nvswitch_dev);
327 
328     return rc;
329 }
330 
331 static NVSWITCH_DEV*
nvswitch_find_device(int minor)332 nvswitch_find_device(int minor)
333 {
334     struct list_head *cur;
335     NVSWITCH_DEV *nvswitch_dev = NULL;
336 
337     list_for_each(cur, &nvswitch.devices)
338     {
339         nvswitch_dev = list_entry(cur, NVSWITCH_DEV, list_node);
340         if (nvswitch_dev->minor == minor)
341         {
342             return nvswitch_dev;
343         }
344     }
345 
346     return NULL;
347 }
348 
349 static int
nvswitch_find_minor(void)350 nvswitch_find_minor(void)
351 {
352     struct list_head *cur;
353     NVSWITCH_DEV *nvswitch_dev;
354     int minor;
355     int minor_in_use;
356 
357     for (minor = 0; minor < NVSWITCH_DEVICE_INSTANCE_MAX; minor++)
358     {
359         minor_in_use = 0;
360 
361         list_for_each(cur, &nvswitch.devices)
362         {
363             nvswitch_dev = list_entry(cur, NVSWITCH_DEV, list_node);
364             if (nvswitch_dev->minor == minor)
365             {
366                 minor_in_use = 1;
367                 break;
368             }
369         }
370 
371         if (!minor_in_use)
372         {
373             return minor;
374         }
375     }
376 
377     return NVSWITCH_DEVICE_INSTANCE_MAX;
378 }
379 
380 static int
nvswitch_init_i2c_adapters(NVSWITCH_DEV * nvswitch_dev)381 nvswitch_init_i2c_adapters
382 (
383     NVSWITCH_DEV *nvswitch_dev
384 )
385 {
386     NvlStatus retval;
387     NvU32 i, valid_ports_mask;
388     struct i2c_adapter *adapter;
389     nvswitch_i2c_adapter_entry *adapter_entry;
390 
391     if (!nvswitch_lib_is_i2c_supported(nvswitch_dev->lib_device))
392     {
393         return 0;
394     }
395 
396     retval = nvswitch_lib_get_valid_ports_mask(nvswitch_dev->lib_device,
397                                                &valid_ports_mask);
398     if (retval != NVL_SUCCESS)
399     {
400         printk(KERN_ERR "Failed to get valid I2C ports mask.\n");
401         return -ENODEV;
402     }
403 
404     FOR_EACH_INDEX_IN_MASK(32, i, valid_ports_mask)
405     {
406         adapter = nvswitch_i2c_add_adapter(nvswitch_dev, i);
407         if (adapter == NULL)
408         {
409             continue;
410         }
411 
412         adapter_entry = nvswitch_os_malloc(sizeof(*adapter_entry));
413         if (adapter_entry == NULL)
414         {
415             printk(KERN_ERR "Failed to create I2C adapter entry.\n");
416             nvswitch_i2c_del_adapter(adapter);
417             continue;
418         }
419 
420         adapter_entry->adapter = adapter;
421 
422         list_add_tail(&adapter_entry->entry, &nvswitch_dev->i2c_adapter_list);
423     }
424     FOR_EACH_INDEX_IN_MASK_END;
425 
426     return 0;
427 }
428 
429 static void
nvswitch_deinit_i2c_adapters(NVSWITCH_DEV * nvswitch_dev)430 nvswitch_deinit_i2c_adapters
431 (
432     NVSWITCH_DEV *nvswitch_dev
433 )
434 {
435     nvswitch_i2c_adapter_entry *curr;
436     nvswitch_i2c_adapter_entry *next;
437 
438     list_for_each_entry_safe(curr,
439                              next,
440                              &nvswitch_dev->i2c_adapter_list,
441                              entry)
442     {
443         nvswitch_i2c_del_adapter(curr->adapter);
444         list_del(&curr->entry);
445         nvswitch_os_free(curr);
446     }
447 }
448 
449 static int
nvswitch_init_device(NVSWITCH_DEV * nvswitch_dev)450 nvswitch_init_device
451 (
452     NVSWITCH_DEV *nvswitch_dev
453 )
454 {
455     struct pci_dev *pci_dev = nvswitch_dev->pci_dev;
456     NvlStatus retval;
457     int rc;
458 
459     INIT_LIST_HEAD(&nvswitch_dev->i2c_adapter_list);
460 
461     retval = nvswitch_lib_register_device(NV_PCI_DOMAIN_NUMBER(pci_dev),
462                                           NV_PCI_BUS_NUMBER(pci_dev),
463                                           NV_PCI_SLOT_NUMBER(pci_dev),
464                                           PCI_FUNC(pci_dev->devfn),
465                                           pci_dev->device,
466                                           pci_dev,
467                                           nvswitch_dev->minor,
468                                           &nvswitch_dev->lib_device);
469     if (NVL_SUCCESS != retval)
470     {
471         printk(KERN_ERR "%s: Failed to register device : %d\n",
472                nvswitch_dev->name,
473                retval);
474         return -ENODEV;
475     }
476 
477     nvswitch_load_bar_info(nvswitch_dev);
478 
479     retval = nvswitch_lib_initialize_device(nvswitch_dev->lib_device);
480     if (NVL_SUCCESS != retval)
481     {
482         printk(KERN_ERR "%s: Failed to initialize device : %d\n",
483                nvswitch_dev->name,
484                retval);
485         rc = -ENODEV;
486         goto init_device_failed;
487     }
488 
489     nvswitch_lib_get_uuid(nvswitch_dev->lib_device, &nvswitch_dev->uuid);
490 
491     if (nvswitch_lib_get_bios_version(nvswitch_dev->lib_device,
492                                       &nvswitch_dev->bios_ver) != NVL_SUCCESS)
493     {
494         nvswitch_dev->bios_ver = 0;
495     }
496 
497     if (nvswitch_lib_get_physid(nvswitch_dev->lib_device,
498                                 &nvswitch_dev->phys_id) != NVL_SUCCESS)
499     {
500         nvswitch_dev->phys_id = NVSWITCH_INVALID_PHYS_ID;
501     }
502 
503     rc = nvswitch_initialize_device_interrupt(nvswitch_dev);
504     if (rc)
505     {
506         printk(KERN_ERR "%s: Failed to initialize interrupt : %d\n",
507                nvswitch_dev->name,
508                rc);
509         goto init_intr_failed;
510     }
511 
512     if (nvswitch_is_device_blacklisted(nvswitch_dev))
513     {
514         printk(KERN_ERR "%s: Blacklisted nvswitch device\n", nvswitch_dev->name);
515         // Keep device registered for HAL access and Fabric State updates
516         return 0;
517     }
518 
519     nvswitch_lib_enable_interrupts(nvswitch_dev->lib_device);
520 
521     return 0;
522 
523 init_intr_failed:
524     nvswitch_lib_shutdown_device(nvswitch_dev->lib_device);
525 
526 init_device_failed:
527     nvswitch_lib_unregister_device(nvswitch_dev->lib_device);
528     nvswitch_dev->lib_device = NULL;
529 
530     return rc;
531 }
532 
533 static int
nvswitch_post_init_device(NVSWITCH_DEV * nvswitch_dev)534 nvswitch_post_init_device
535 (
536     NVSWITCH_DEV *nvswitch_dev
537 )
538 {
539     int rc;
540     NvlStatus retval;
541 
542     rc = nvswitch_init_i2c_adapters(nvswitch_dev);
543     if (rc < 0)
544     {
545        return rc;
546     }
547 
548     retval = nvswitch_lib_post_init_device(nvswitch_dev->lib_device);
549     if (retval != NVL_SUCCESS)
550     {
551         return -ENODEV;
552     }
553 
554     return 0;
555 }
556 
557 static void
nvswitch_post_init_blacklisted(NVSWITCH_DEV * nvswitch_dev)558 nvswitch_post_init_blacklisted
559 (
560     NVSWITCH_DEV *nvswitch_dev
561 )
562 {
563     nvswitch_lib_post_init_blacklist_device(nvswitch_dev->lib_device);
564 }
565 
566 static void
nvswitch_deinit_device(NVSWITCH_DEV * nvswitch_dev)567 nvswitch_deinit_device
568 (
569     NVSWITCH_DEV *nvswitch_dev
570 )
571 {
572     nvswitch_deinit_i2c_adapters(nvswitch_dev);
573 
574     nvswitch_lib_disable_interrupts(nvswitch_dev->lib_device);
575 
576     nvswitch_shutdown_device_interrupt(nvswitch_dev);
577 
578     nvswitch_lib_shutdown_device(nvswitch_dev->lib_device);
579 
580     nvswitch_lib_unregister_device(nvswitch_dev->lib_device);
581     nvswitch_dev->lib_device = NULL;
582 }
583 
584 static void
nvswitch_init_file_event(nvswitch_file_private_t * private)585 nvswitch_init_file_event
586 (
587     nvswitch_file_private_t *private
588 )
589 {
590     init_waitqueue_head(&private->file_event.wait_q_event);
591     private->file_event.event_pending = NV_FALSE;
592 }
593 
594 //
595 // Basic device open to support IOCTL interface
596 //
597 static int
nvswitch_device_open(struct inode * inode,struct file * file)598 nvswitch_device_open
599 (
600     struct inode *inode,
601     struct file *file
602 )
603 {
604     NVSWITCH_DEV *nvswitch_dev;
605     int rc = 0;
606     nvswitch_file_private_t *private = NULL;
607 
608     //
609     // Get the major/minor device
610     // We might want this for routing requests to multiple nvswitches
611     //
612     printk(KERN_INFO "nvidia-nvswitch%d: open (major=%d)\n",
613            MINOR(inode->i_rdev),
614            MAJOR(inode->i_rdev));
615 
616     rc = mutex_lock_interruptible(&nvswitch.driver_mutex);
617     if (rc)
618     {
619         return rc;
620     }
621 
622     nvswitch_dev = nvswitch_find_device(MINOR(inode->i_rdev));
623     if (!nvswitch_dev)
624     {
625         rc = -ENODEV;
626         goto done;
627     }
628 
629     if (nvswitch_is_device_blacklisted(nvswitch_dev))
630     {
631         rc = -ENODEV;
632         goto done;
633     }
634 
635     private = nvswitch_os_malloc(sizeof(*private));
636     if (private == NULL)
637     {
638         rc = -ENOMEM;
639         goto done;
640     }
641 
642     private->nvswitch_dev = nvswitch_dev;
643 
644     nvswitch_init_file_event(private);
645 
646     private->capability_fds.fabric_mgmt = -1;
647     NVSWITCH_SET_FILE_PRIVATE(file, private);
648 
649     NV_ATOMIC_INC(nvswitch_dev->ref_count);
650 
651 done:
652     mutex_unlock(&nvswitch.driver_mutex);
653 
654     return rc;
655 }
656 
657 //
658 // Basic device release to support IOCTL interface
659 //
660 static int
nvswitch_device_release(struct inode * inode,struct file * file)661 nvswitch_device_release
662 (
663     struct inode *inode,
664     struct file *file
665 )
666 {
667     nvswitch_file_private_t *private = NVSWITCH_GET_FILE_PRIVATE(file);
668     NVSWITCH_DEV *nvswitch_dev = private->nvswitch_dev;
669 
670     printk(KERN_INFO "nvidia-nvswitch%d: release (major=%d)\n",
671            MINOR(inode->i_rdev),
672            MAJOR(inode->i_rdev));
673 
674     mutex_lock(&nvswitch.driver_mutex);
675 
676     nvswitch_lib_remove_client_events(nvswitch_dev->lib_device, (void *)private);
677 
678     //
679     // If there are no outstanding references and the device is marked
680     // unusable, free it.
681     //
682     if (NV_ATOMIC_DEC_AND_TEST(nvswitch_dev->ref_count) &&
683         nvswitch_dev->unusable)
684     {
685         kfree(nvswitch_dev);
686     }
687 
688     if (private->capability_fds.fabric_mgmt > 0)
689     {
690         nvlink_cap_release(private->capability_fds.fabric_mgmt);
691         private->capability_fds.fabric_mgmt = -1;
692     }
693 
694     nvswitch_os_free(file->private_data);
695     NVSWITCH_SET_FILE_PRIVATE(file, NULL);
696 
697     mutex_unlock(&nvswitch.driver_mutex);
698 
699     return 0;
700 }
701 
702 static unsigned int
nvswitch_device_poll(struct file * file,poll_table * wait)703 nvswitch_device_poll
704 (
705     struct file *file,
706     poll_table *wait
707 )
708 {
709     nvswitch_file_private_t *private = NVSWITCH_GET_FILE_PRIVATE(file);
710     NVSWITCH_DEV *nvswitch_dev = private->nvswitch_dev;
711     int rc = 0;
712     NvlStatus status;
713     struct NVSWITCH_CLIENT_EVENT *client_event;
714 
715     rc = mutex_lock_interruptible(&nvswitch_dev->device_mutex);
716     if (rc)
717     {
718         return rc;
719     }
720 
721     if (nvswitch_dev->unusable)
722     {
723         printk(KERN_INFO "%s: a stale fd detected\n", nvswitch_dev->name);
724         rc = POLLHUP;
725         goto done;
726     }
727 
728     status = nvswitch_lib_get_client_event(nvswitch_dev->lib_device,
729                                            (void *) private, &client_event);
730     if (status != NVL_SUCCESS)
731     {
732         printk(KERN_INFO "%s: no events registered for fd\n", nvswitch_dev->name);
733         rc = POLLERR;
734         goto done;
735     }
736 
737     poll_wait(file, &private->file_event.wait_q_event, wait);
738 
739     if (private->file_event.event_pending)
740     {
741         rc = POLLPRI | POLLIN;
742         private->file_event.event_pending = NV_FALSE;
743     }
744 
745 done:
746     mutex_unlock(&nvswitch_dev->device_mutex);
747 
748     return rc;
749 }
750 
751 typedef struct {
752     void *kernel_params;                // Kernel copy of ioctl parameters
753     unsigned long kernel_params_size;   // Size of ioctl params according to user
754 } IOCTL_STATE;
755 
756 //
757 // Clean up any dynamically allocated memory for ioctl state
758 //
759 static void
nvswitch_ioctl_state_cleanup(IOCTL_STATE * state)760 nvswitch_ioctl_state_cleanup
761 (
762     IOCTL_STATE *state
763 )
764 {
765     kfree(state->kernel_params);
766     state->kernel_params = NULL;
767 }
768 
769 //
770 // Initialize buffer state for ioctl.
771 //
772 // This handles allocating memory and copying user data into kernel space.  The
773 // ioctl params structure only is supported. Nested data pointers are not handled.
774 //
775 // State is maintained in the IOCTL_STATE struct for use by the ioctl, _sync and
776 // _cleanup calls.
777 //
778 static int
nvswitch_ioctl_state_start(IOCTL_STATE * state,int cmd,unsigned long user_arg)779 nvswitch_ioctl_state_start(IOCTL_STATE *state, int cmd, unsigned long user_arg)
780 {
781     int rc;
782 
783     state->kernel_params = NULL;
784     state->kernel_params_size = _IOC_SIZE(cmd);
785 
786     if (0 == state->kernel_params_size)
787     {
788         return 0;
789     }
790 
791     state->kernel_params = kzalloc(state->kernel_params_size, GFP_KERNEL);
792     if (NULL == state->kernel_params)
793     {
794         rc = -ENOMEM;
795         goto nvswitch_ioctl_state_start_fail;
796     }
797 
798     // Copy params to kernel buffers.  Simple _IOR() ioctls can skip this step.
799     if (_IOC_DIR(cmd) & _IOC_WRITE)
800     {
801         rc = copy_from_user(state->kernel_params,
802                             (const void *)user_arg,
803                             state->kernel_params_size);
804         if (rc)
805         {
806             rc = -EFAULT;
807             goto nvswitch_ioctl_state_start_fail;
808         }
809     }
810 
811     return 0;
812 
813 nvswitch_ioctl_state_start_fail:
814     nvswitch_ioctl_state_cleanup(state);
815     return rc;
816 }
817 
818 //
819 // Synchronize any ioctl output in the kernel buffers to the user mode buffers.
820 //
821 static int
nvswitch_ioctl_state_sync(IOCTL_STATE * state,int cmd,unsigned long user_arg)822 nvswitch_ioctl_state_sync
823 (
824     IOCTL_STATE *state,
825     int cmd,
826     unsigned long user_arg
827 )
828 {
829     int rc;
830 
831     // Nothing to do if no buffer or write-only ioctl
832     if ((0 == state->kernel_params_size) || (0 == (_IOC_DIR(cmd) & _IOC_READ)))
833     {
834         return 0;
835     }
836 
837     // Copy params structure back to user mode
838     rc = copy_to_user((void *)user_arg,
839                       state->kernel_params,
840                       state->kernel_params_size);
841     if (rc)
842     {
843         rc = -EFAULT;
844     }
845 
846     return rc;
847 }
848 
849 static int
nvswitch_device_ioctl(struct inode * inode,struct file * file,unsigned int cmd,unsigned long arg)850 nvswitch_device_ioctl
851 (
852     struct inode *inode,
853     struct file *file,
854     unsigned int cmd,
855     unsigned long arg
856 )
857 {
858     nvswitch_file_private_t *private = NVSWITCH_GET_FILE_PRIVATE(file);
859     NVSWITCH_DEV *nvswitch_dev = private->nvswitch_dev;
860     IOCTL_STATE state = {0};
861     NvlStatus retval;
862     int rc = 0;
863 
864     if (_IOC_TYPE(cmd) != NVSWITCH_DEV_IO_TYPE)
865     {
866         return -EINVAL;
867     }
868 
869     rc = mutex_lock_interruptible(&nvswitch_dev->device_mutex);
870     if (rc)
871     {
872         return rc;
873     }
874 
875     if (nvswitch_dev->unusable)
876     {
877         printk(KERN_INFO "%s: a stale fd detected\n", nvswitch_dev->name);
878         rc = -ENODEV;
879         goto nvswitch_device_ioctl_exit;
880     }
881 
882     if (nvswitch_is_device_blacklisted(nvswitch_dev))
883     {
884         printk(KERN_INFO "%s: ioctl attempted on blacklisted device\n", nvswitch_dev->name);
885         rc = -ENODEV;
886         goto nvswitch_device_ioctl_exit;
887     }
888 
889     rc = nvswitch_ioctl_state_start(&state, cmd, arg);
890     if (rc)
891     {
892         goto nvswitch_device_ioctl_exit;
893     }
894 
895     retval = nvswitch_lib_ctrl(nvswitch_dev->lib_device,
896                                _IOC_NR(cmd),
897                                state.kernel_params,
898                                state.kernel_params_size,
899                                file->private_data);
900     rc = nvswitch_map_status(retval);
901     if (!rc)
902     {
903         rc = nvswitch_ioctl_state_sync(&state, cmd, arg);
904     }
905 
906     nvswitch_ioctl_state_cleanup(&state);
907 
908 nvswitch_device_ioctl_exit:
909     mutex_unlock(&nvswitch_dev->device_mutex);
910 
911     return rc;
912 }
913 
914 static long
nvswitch_device_unlocked_ioctl(struct file * file,unsigned int cmd,unsigned long arg)915 nvswitch_device_unlocked_ioctl
916 (
917     struct file *file,
918     unsigned int cmd,
919     unsigned long arg
920 )
921 {
922     return nvswitch_device_ioctl(NV_FILE_INODE(file), file, cmd, arg);
923 }
924 
925 static int
nvswitch_ctl_check_version(NVSWITCH_CHECK_VERSION_PARAMS * p)926 nvswitch_ctl_check_version(NVSWITCH_CHECK_VERSION_PARAMS *p)
927 {
928     NvlStatus retval;
929 
930     p->is_compatible = 0;
931     p->user.version[NVSWITCH_VERSION_STRING_LENGTH - 1] = '\0';
932 
933     retval = nvswitch_lib_check_api_version(p->user.version, p->kernel.version,
934                                             NVSWITCH_VERSION_STRING_LENGTH);
935     if (retval == NVL_SUCCESS)
936     {
937         p->is_compatible = 1;
938     }
939     else if (retval == -NVL_ERR_NOT_SUPPORTED)
940     {
941         printk(KERN_ERR "nvidia-nvswitch: Version mismatch, "
942                "kernel version %s user version %s\n",
943                p->kernel.version, p->user.version);
944     }
945     else
946     {
947         // An unexpected failure
948         return nvswitch_map_status(retval);
949     }
950 
951     return 0;
952 }
953 
954 static void
nvswitch_ctl_get_devices(NVSWITCH_GET_DEVICES_PARAMS * p)955 nvswitch_ctl_get_devices(NVSWITCH_GET_DEVICES_PARAMS *p)
956 {
957     int index = 0;
958     NVSWITCH_DEV *nvswitch_dev;
959     struct list_head *cur;
960 
961     BUILD_BUG_ON(NVSWITCH_DEVICE_INSTANCE_MAX != NVSWITCH_MAX_DEVICES);
962 
963     list_for_each(cur, &nvswitch.devices)
964     {
965         nvswitch_dev = list_entry(cur, NVSWITCH_DEV, list_node);
966         p->info[index].deviceInstance = nvswitch_dev->minor;
967         p->info[index].pciDomain = NV_PCI_DOMAIN_NUMBER(nvswitch_dev->pci_dev);
968         p->info[index].pciBus = NV_PCI_BUS_NUMBER(nvswitch_dev->pci_dev);
969         p->info[index].pciDevice = NV_PCI_SLOT_NUMBER(nvswitch_dev->pci_dev);
970         p->info[index].pciFunction = PCI_FUNC(nvswitch_dev->pci_dev->devfn);
971         index++;
972     }
973 
974     p->deviceCount = index;
975 }
976 
977 static void
nvswitch_ctl_get_devices_v2(NVSWITCH_GET_DEVICES_V2_PARAMS * p)978 nvswitch_ctl_get_devices_v2(NVSWITCH_GET_DEVICES_V2_PARAMS *p)
979 {
980     int index = 0;
981     NVSWITCH_DEV *nvswitch_dev;
982     struct list_head *cur;
983 
984     BUILD_BUG_ON(NVSWITCH_DEVICE_INSTANCE_MAX != NVSWITCH_MAX_DEVICES);
985 
986     list_for_each(cur, &nvswitch.devices)
987     {
988         nvswitch_dev = list_entry(cur, NVSWITCH_DEV, list_node);
989         p->info[index].deviceInstance = nvswitch_dev->minor;
990         memcpy(&p->info[index].uuid, &nvswitch_dev->uuid, sizeof(nvswitch_dev->uuid));
991         p->info[index].pciDomain = NV_PCI_DOMAIN_NUMBER(nvswitch_dev->pci_dev);
992         p->info[index].pciBus = NV_PCI_BUS_NUMBER(nvswitch_dev->pci_dev);
993         p->info[index].pciDevice = NV_PCI_SLOT_NUMBER(nvswitch_dev->pci_dev);
994         p->info[index].pciFunction = PCI_FUNC(nvswitch_dev->pci_dev->devfn);
995         p->info[index].physId = nvswitch_dev->phys_id;
996 
997         if (nvswitch_dev->lib_device != NULL)
998         {
999             mutex_lock(&nvswitch_dev->device_mutex);
1000             (void)nvswitch_lib_read_fabric_state(nvswitch_dev->lib_device,
1001                                                  &p->info[index].deviceState,
1002                                                  &p->info[index].deviceReason,
1003                                                  &p->info[index].driverState);
1004 
1005             p->info[index].bTnvlEnabled = nvswitch_lib_is_tnvl_enabled(nvswitch_dev->lib_device);
1006             mutex_unlock(&nvswitch_dev->device_mutex);
1007         }
1008         index++;
1009     }
1010 
1011     p->deviceCount = index;
1012 }
1013 
1014 #define NVSWITCH_CTL_CHECK_PARAMS(type, size) (sizeof(type) == size ? 0 : -EINVAL)
1015 
1016 static int
nvswitch_ctl_cmd_dispatch(unsigned int cmd,void * params,unsigned int param_size)1017 nvswitch_ctl_cmd_dispatch
1018 (
1019     unsigned int cmd,
1020     void *params,
1021     unsigned int param_size
1022 )
1023 {
1024     int rc;
1025 
1026     switch(cmd)
1027     {
1028         case CTRL_NVSWITCH_CHECK_VERSION:
1029             rc = NVSWITCH_CTL_CHECK_PARAMS(NVSWITCH_CHECK_VERSION_PARAMS,
1030                                            param_size);
1031             if (!rc)
1032             {
1033                 rc = nvswitch_ctl_check_version(params);
1034             }
1035             break;
1036         case CTRL_NVSWITCH_GET_DEVICES:
1037             rc = NVSWITCH_CTL_CHECK_PARAMS(NVSWITCH_GET_DEVICES_PARAMS,
1038                                            param_size);
1039             if (!rc)
1040             {
1041                 nvswitch_ctl_get_devices(params);
1042             }
1043             break;
1044         case CTRL_NVSWITCH_GET_DEVICES_V2:
1045             rc = NVSWITCH_CTL_CHECK_PARAMS(NVSWITCH_GET_DEVICES_V2_PARAMS,
1046                                            param_size);
1047             if (!rc)
1048             {
1049                 nvswitch_ctl_get_devices_v2(params);
1050             }
1051             break;
1052 
1053         default:
1054             rc = -EINVAL;
1055             break;
1056     }
1057 
1058     return rc;
1059 }
1060 
1061 static int
nvswitch_ctl_ioctl(struct inode * inode,struct file * file,unsigned int cmd,unsigned long arg)1062 nvswitch_ctl_ioctl
1063 (
1064     struct inode *inode,
1065     struct file *file,
1066     unsigned int cmd,
1067     unsigned long arg
1068 )
1069 {
1070     int rc = 0;
1071     IOCTL_STATE state = {0};
1072 
1073     if (_IOC_TYPE(cmd) != NVSWITCH_CTL_IO_TYPE)
1074     {
1075         return -EINVAL;
1076     }
1077 
1078     rc = mutex_lock_interruptible(&nvswitch.driver_mutex);
1079     if (rc)
1080     {
1081         return rc;
1082     }
1083 
1084     rc = nvswitch_ioctl_state_start(&state, cmd, arg);
1085     if (rc)
1086     {
1087         goto nvswitch_ctl_ioctl_exit;
1088     }
1089 
1090     rc = nvswitch_ctl_cmd_dispatch(_IOC_NR(cmd),
1091                                    state.kernel_params,
1092                                    state.kernel_params_size);
1093     if (!rc)
1094     {
1095         rc = nvswitch_ioctl_state_sync(&state, cmd, arg);
1096     }
1097 
1098     nvswitch_ioctl_state_cleanup(&state);
1099 
1100 nvswitch_ctl_ioctl_exit:
1101     mutex_unlock(&nvswitch.driver_mutex);
1102 
1103     return rc;
1104 }
1105 
1106 static long
nvswitch_ctl_unlocked_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1107 nvswitch_ctl_unlocked_ioctl
1108 (
1109     struct file *file,
1110     unsigned int cmd,
1111     unsigned long arg
1112 )
1113 {
1114     return nvswitch_ctl_ioctl(NV_FILE_INODE(file), file, cmd, arg);
1115 }
1116 
1117 static irqreturn_t
nvswitch_isr_pending(int irq,void * arg)1118 nvswitch_isr_pending
1119 (
1120     int   irq,
1121     void *arg
1122 )
1123 {
1124 
1125     NVSWITCH_DEV *nvswitch_dev = (NVSWITCH_DEV *)arg;
1126     NvlStatus retval;
1127 
1128     //
1129     // On silicon MSI must be enabled.  Since interrupts will not be shared
1130     // with MSI, we can simply signal the thread.
1131     //
1132     if (nvswitch_dev->irq_mechanism == NVSWITCH_IRQ_MSI)
1133     {
1134         return IRQ_WAKE_THREAD;
1135     }
1136 
1137     if (nvswitch_dev->irq_mechanism == NVSWITCH_IRQ_PIN)
1138     {
1139         //
1140         // We do not take mutex in the interrupt context. The interrupt
1141         // check is safe to driver state.
1142         //
1143         retval = nvswitch_lib_check_interrupts(nvswitch_dev->lib_device);
1144 
1145         // Wake interrupt thread if there is an interrupt pending
1146         if (-NVL_MORE_PROCESSING_REQUIRED == retval)
1147         {
1148             nvswitch_lib_disable_interrupts(nvswitch_dev->lib_device);
1149             return IRQ_WAKE_THREAD;
1150         }
1151 
1152         // PCI errors are handled else where.
1153         if (-NVL_PCI_ERROR == retval)
1154         {
1155             return IRQ_NONE;
1156         }
1157 
1158         if (NVL_SUCCESS != retval)
1159         {
1160             pr_err("nvidia-nvswitch: unrecoverable error in ISR\n");
1161             NVSWITCH_OS_ASSERT(0);
1162         }
1163         return IRQ_NONE;
1164     }
1165 
1166     pr_err("nvidia-nvswitch: unsupported IRQ mechanism in ISR\n");
1167     NVSWITCH_OS_ASSERT(0);
1168 
1169     return IRQ_NONE;
1170 }
1171 
1172 static irqreturn_t
nvswitch_isr_thread(int irq,void * arg)1173 nvswitch_isr_thread
1174 (
1175     int   irq,
1176     void *arg
1177 )
1178 {
1179     NVSWITCH_DEV *nvswitch_dev = (NVSWITCH_DEV *)arg;
1180     NvlStatus retval;
1181 
1182     mutex_lock(&nvswitch_dev->device_mutex);
1183 
1184     retval = nvswitch_lib_service_interrupts(nvswitch_dev->lib_device);
1185 
1186     wake_up(&nvswitch_dev->wait_q_errors);
1187 
1188     if (nvswitch_dev->irq_mechanism == NVSWITCH_IRQ_PIN)
1189     {
1190         nvswitch_lib_enable_interrupts(nvswitch_dev->lib_device);
1191     }
1192 
1193     mutex_unlock(&nvswitch_dev->device_mutex);
1194 
1195     if (WARN_ON(retval != NVL_SUCCESS))
1196     {
1197         printk(KERN_ERR "%s: Interrupts disabled to avoid a storm\n",
1198                nvswitch_dev->name);
1199     }
1200 
1201     return IRQ_HANDLED;
1202 }
1203 
1204 static void
nvswitch_task_dispatch(NVSWITCH_DEV * nvswitch_dev)1205 nvswitch_task_dispatch
1206 (
1207     NVSWITCH_DEV *nvswitch_dev
1208 )
1209 {
1210     NvU64 nsec;
1211     NvU64 timeout;
1212     NvS64 rc;
1213 
1214     if (NV_ATOMIC_READ(nvswitch_dev->task_q_ready) == 0)
1215     {
1216         return;
1217     }
1218 
1219     mutex_lock(&nvswitch_dev->device_mutex);
1220 
1221     nsec = nvswitch_lib_deferred_task_dispatcher(nvswitch_dev->lib_device);
1222 
1223     mutex_unlock(&nvswitch_dev->device_mutex);
1224 
1225     timeout = usecs_to_jiffies(nsec / NSEC_PER_USEC);
1226 
1227     rc = wait_event_interruptible_timeout(nvswitch_dev->wait_q_shutdown,
1228                               (NV_ATOMIC_READ(nvswitch_dev->task_q_ready) == 0),
1229                               timeout);
1230 
1231     //
1232     // These background tasks should rarely, if ever, get interrupted. We use
1233     // the "interruptible" variant of wait_event in order to avoid contributing
1234     // to the system load average (/proc/loadavg), and to avoid softlockup
1235     // warnings that can occur if a kernel thread lingers too long in an
1236     // uninterruptible state. If this does get interrupted, we'd like to debug
1237     // and find out why, so WARN in that case.
1238     //
1239     WARN_ON(rc < 0);
1240 
1241     //
1242     // Schedule a work item only if the above actually timed out or got
1243     // interrupted, without the condition becoming true.
1244     //
1245     if (rc <= 0)
1246     {
1247         if (!nv_kthread_q_schedule_q_item(&nvswitch_dev->task_q,
1248                                           &nvswitch_dev->task_item))
1249         {
1250             printk(KERN_ERR "%s: Failed to re-schedule background task\n",
1251                    nvswitch_dev->name);
1252         }
1253     }
1254 }
1255 
1256 static int
nvswitch_probe(struct pci_dev * pci_dev,const struct pci_device_id * id_table)1257 nvswitch_probe
1258 (
1259     struct pci_dev *pci_dev,
1260     const struct pci_device_id *id_table
1261 )
1262 {
1263     NVSWITCH_DEV *nvswitch_dev = NULL;
1264     int rc = 0;
1265     int minor;
1266 
1267     if (!nvswitch_lib_validate_device_id(pci_dev->device))
1268     {
1269         return -EINVAL;
1270     }
1271 
1272     printk(KERN_INFO "nvidia-nvswitch: Probing device %04x:%02x:%02x.%x, "
1273            "Vendor Id = 0x%x, Device Id = 0x%x, Class = 0x%x \n",
1274            NV_PCI_DOMAIN_NUMBER(pci_dev),
1275            NV_PCI_BUS_NUMBER(pci_dev),
1276            NV_PCI_SLOT_NUMBER(pci_dev),
1277            PCI_FUNC(pci_dev->devfn),
1278            pci_dev->vendor,
1279            pci_dev->device,
1280            pci_dev->class);
1281 
1282     mutex_lock(&nvswitch.driver_mutex);
1283 
1284     minor = nvswitch_find_minor();
1285     if (minor >= NVSWITCH_DEVICE_INSTANCE_MAX)
1286     {
1287         rc = -ERANGE;
1288         goto find_minor_failed;
1289     }
1290 
1291     nvswitch_dev = kzalloc(sizeof(*nvswitch_dev), GFP_KERNEL);
1292     if (NULL == nvswitch_dev)
1293     {
1294         rc = -ENOMEM;
1295         goto kzalloc_failed;
1296     }
1297 
1298     mutex_init(&nvswitch_dev->device_mutex);
1299     init_waitqueue_head(&nvswitch_dev->wait_q_errors);
1300     init_waitqueue_head(&nvswitch_dev->wait_q_shutdown);
1301 
1302     snprintf(nvswitch_dev->name, sizeof(nvswitch_dev->name),
1303         NVSWITCH_DRIVER_NAME "%d", minor);
1304 
1305     snprintf(nvswitch_dev->sname, sizeof(nvswitch_dev->sname),
1306         NVSWITCH_SHORT_NAME "%d", minor);
1307 
1308     rc = pci_enable_device(pci_dev);
1309     if (rc)
1310     {
1311         printk(KERN_ERR "%s: Failed to enable PCI device : %d\n",
1312                nvswitch_dev->name,
1313                rc);
1314         goto pci_enable_device_failed;
1315     }
1316 
1317     pci_set_master(pci_dev);
1318 
1319     rc = pci_request_regions(pci_dev, nvswitch_dev->name);
1320     if (rc)
1321     {
1322         printk(KERN_ERR "%s: Failed to request memory regions : %d\n",
1323                nvswitch_dev->name,
1324                rc);
1325         goto pci_request_regions_failed;
1326     }
1327 
1328     nvswitch_dev->bar0 = pci_iomap(pci_dev, 0, 0);
1329     if (!nvswitch_dev->bar0)
1330     {
1331         rc = -ENOMEM;
1332         printk(KERN_ERR "%s: Failed to map BAR0 region : %d\n",
1333                nvswitch_dev->name,
1334                rc);
1335         goto pci_iomap_failed;
1336     }
1337 
1338     nvswitch_dev->pci_dev = pci_dev;
1339     nvswitch_dev->minor = minor;
1340 
1341     rc = nvswitch_init_device(nvswitch_dev);
1342     if (rc)
1343     {
1344         printk(KERN_ERR "%s: Failed to initialize device : %d\n",
1345                nvswitch_dev->name,
1346                rc);
1347         goto init_device_failed;
1348     }
1349 
1350     if (nvswitch_is_device_blacklisted(nvswitch_dev))
1351     {
1352         nvswitch_post_init_blacklisted(nvswitch_dev);
1353         goto blacklisted;
1354     }
1355 
1356     //
1357     // device_mutex held here because post_init entries may call soeService_HAL()
1358     // with IRQs on. see bug 2856314 for more info
1359     //
1360     mutex_lock(&nvswitch_dev->device_mutex);
1361     rc = nvswitch_post_init_device(nvswitch_dev);
1362     mutex_unlock(&nvswitch_dev->device_mutex);
1363     if (rc)
1364     {
1365         printk(KERN_ERR "%s:Failed during device post init : %d\n",
1366                nvswitch_dev->name, rc);
1367         goto post_init_device_failed;
1368     }
1369 
1370 blacklisted:
1371     rc = nvswitch_init_background_tasks(nvswitch_dev);
1372     if (rc)
1373     {
1374         printk(KERN_ERR "%s: Failed to initialize background tasks : %d\n",
1375                nvswitch_dev->name,
1376                rc);
1377         goto init_background_task_failed;
1378     }
1379 
1380     pci_set_drvdata(pci_dev, nvswitch_dev);
1381 
1382     nvswitch_procfs_device_add(nvswitch_dev);
1383 
1384     list_add_tail(&nvswitch_dev->list_node, &nvswitch.devices);
1385 
1386     NV_ATOMIC_INC(nvswitch.count);
1387 
1388     mutex_unlock(&nvswitch.driver_mutex);
1389 
1390     return 0;
1391 
1392 init_background_task_failed:
1393 post_init_device_failed:
1394     nvswitch_deinit_device(nvswitch_dev);
1395 
1396 init_device_failed:
1397     pci_iounmap(pci_dev, nvswitch_dev->bar0);
1398 
1399 pci_iomap_failed:
1400     pci_release_regions(pci_dev);
1401 
1402 pci_request_regions_failed:
1403 #ifdef CONFIG_PCI
1404     pci_clear_master(pci_dev);
1405 #endif
1406     pci_disable_device(pci_dev);
1407 
1408 pci_enable_device_failed:
1409     kfree(nvswitch_dev);
1410 
1411 kzalloc_failed:
1412 find_minor_failed:
1413     mutex_unlock(&nvswitch.driver_mutex);
1414 
1415     return rc;
1416 }
1417 
1418 void
nvswitch_remove(struct pci_dev * pci_dev)1419 nvswitch_remove
1420 (
1421     struct pci_dev *pci_dev
1422 )
1423 {
1424     NVSWITCH_DEV *nvswitch_dev;
1425 
1426     mutex_lock(&nvswitch.driver_mutex);
1427 
1428     nvswitch_dev = pci_get_drvdata(pci_dev);
1429 
1430     if (nvswitch_dev == NULL)
1431     {
1432         goto done;
1433     }
1434 
1435     printk(KERN_INFO "%s: removing device %04x:%02x:%02x.%x\n",
1436            nvswitch_dev->name,
1437            NV_PCI_DOMAIN_NUMBER(pci_dev),
1438            NV_PCI_BUS_NUMBER(pci_dev),
1439            NV_PCI_SLOT_NUMBER(pci_dev),
1440            PCI_FUNC(pci_dev->devfn));
1441 
1442     //
1443     // Synchronize with device operations such as .ioctls/.poll, and then mark
1444     // the device unusable.
1445     //
1446     mutex_lock(&nvswitch_dev->device_mutex);
1447     nvswitch_dev->unusable = NV_TRUE;
1448     mutex_unlock(&nvswitch_dev->device_mutex);
1449 
1450     NV_ATOMIC_DEC(nvswitch.count);
1451 
1452     list_del(&nvswitch_dev->list_node);
1453 
1454     nvswitch_deinit_background_tasks(nvswitch_dev);
1455 
1456     nvswitch_deinit_device(nvswitch_dev);
1457 
1458     pci_set_drvdata(pci_dev, NULL);
1459 
1460     pci_iounmap(pci_dev, nvswitch_dev->bar0);
1461 
1462     pci_release_regions(pci_dev);
1463 
1464 #ifdef CONFIG_PCI
1465     pci_clear_master(pci_dev);
1466 #endif
1467 
1468     pci_disable_device(pci_dev);
1469 
1470     nvswitch_procfs_device_remove(nvswitch_dev);
1471 
1472     // Free nvswitch_dev only if it is not in use.
1473     if (NV_ATOMIC_READ(nvswitch_dev->ref_count) == 0)
1474     {
1475         kfree(nvswitch_dev);
1476     }
1477 
1478 done:
1479     mutex_unlock(&nvswitch.driver_mutex);
1480 
1481     return;
1482 }
1483 
1484 static void
nvswitch_load_bar_info(NVSWITCH_DEV * nvswitch_dev)1485 nvswitch_load_bar_info
1486 (
1487     NVSWITCH_DEV *nvswitch_dev
1488 )
1489 {
1490     struct pci_dev *pci_dev = nvswitch_dev->pci_dev;
1491     nvlink_pci_info *info;
1492     NvU32 bar = 0;
1493 
1494     nvswitch_lib_get_device_info(nvswitch_dev->lib_device, &info);
1495 
1496     info->bars[0].offset = NVRM_PCICFG_BAR_OFFSET(0);
1497     pci_read_config_dword(pci_dev, info->bars[0].offset, &bar);
1498 
1499     info->bars[0].busAddress = (bar & PCI_BASE_ADDRESS_MEM_MASK);
1500     if (NV_PCI_RESOURCE_FLAGS(pci_dev, 0) & PCI_BASE_ADDRESS_MEM_TYPE_64)
1501     {
1502         pci_read_config_dword(pci_dev, info->bars[0].offset + 4, &bar);
1503         info->bars[0].busAddress |= (((NvU64)bar) << 32);
1504     }
1505 
1506     info->bars[0].baseAddr = NV_PCI_RESOURCE_START(pci_dev, 0);
1507 
1508     info->bars[0].barSize = NV_PCI_RESOURCE_SIZE(pci_dev, 0);
1509 
1510     info->bars[0].pBar = nvswitch_dev->bar0;
1511 }
1512 
1513 static int
_nvswitch_initialize_msix_interrupt(NVSWITCH_DEV * nvswitch_dev)1514 _nvswitch_initialize_msix_interrupt
1515 (
1516     NVSWITCH_DEV *nvswitch_dev
1517 )
1518 {
1519     // Not supported (bug 3018806)
1520     return -EINVAL;
1521 }
1522 
1523 static int
_nvswitch_initialize_msi_interrupt(NVSWITCH_DEV * nvswitch_dev)1524 _nvswitch_initialize_msi_interrupt
1525 (
1526     NVSWITCH_DEV *nvswitch_dev
1527 )
1528 {
1529 #ifdef CONFIG_PCI_MSI
1530     struct pci_dev *pci_dev = nvswitch_dev->pci_dev;
1531     int rc;
1532 
1533     rc = pci_enable_msi(pci_dev);
1534     if (rc)
1535     {
1536         return rc;
1537     }
1538 
1539     return 0;
1540 #else
1541     return -EINVAL;
1542 #endif
1543 }
1544 
1545 static int
_nvswitch_get_irq_caps(NVSWITCH_DEV * nvswitch_dev,unsigned long * irq_caps)1546 _nvswitch_get_irq_caps(NVSWITCH_DEV *nvswitch_dev, unsigned long *irq_caps)
1547 {
1548     struct pci_dev *pci_dev;
1549 
1550     if (!nvswitch_dev || !irq_caps)
1551         return -EINVAL;
1552 
1553     pci_dev = nvswitch_dev->pci_dev;
1554 
1555     if (pci_find_capability(pci_dev, PCI_CAP_ID_MSIX))
1556         set_bit(NVSWITCH_IRQ_MSIX, irq_caps);
1557 
1558     if (pci_find_capability(pci_dev, PCI_CAP_ID_MSI))
1559         set_bit(NVSWITCH_IRQ_MSI, irq_caps);
1560 
1561     if (nvswitch_lib_use_pin_irq(nvswitch_dev->lib_device))
1562         set_bit(NVSWITCH_IRQ_PIN, irq_caps);
1563 
1564     return 0;
1565 }
1566 
1567 static int
nvswitch_initialize_device_interrupt(NVSWITCH_DEV * nvswitch_dev)1568 nvswitch_initialize_device_interrupt
1569 (
1570     NVSWITCH_DEV *nvswitch_dev
1571 )
1572 {
1573     struct pci_dev *pci_dev = nvswitch_dev->pci_dev;
1574     int flags = 0;
1575     unsigned long irq_caps = 0;
1576     int rc;
1577 
1578     if (_nvswitch_get_irq_caps(nvswitch_dev, &irq_caps))
1579     {
1580         pr_err("%s: failed to retrieve device interrupt capabilities\n",
1581                nvswitch_dev->name);
1582         return -EINVAL;
1583     }
1584 
1585     nvswitch_dev->irq_mechanism = NVSWITCH_IRQ_NONE;
1586 
1587     if (test_bit(NVSWITCH_IRQ_MSIX, &irq_caps))
1588     {
1589         rc = _nvswitch_initialize_msix_interrupt(nvswitch_dev);
1590         if (!rc)
1591         {
1592             nvswitch_dev->irq_mechanism = NVSWITCH_IRQ_MSIX;
1593             pr_info("%s: using MSI-X\n", nvswitch_dev->name);
1594         }
1595     }
1596 
1597     if (nvswitch_dev->irq_mechanism == NVSWITCH_IRQ_NONE
1598         && test_bit(NVSWITCH_IRQ_MSI, &irq_caps))
1599     {
1600         rc = _nvswitch_initialize_msi_interrupt(nvswitch_dev);
1601         if (!rc)
1602         {
1603             nvswitch_dev->irq_mechanism = NVSWITCH_IRQ_MSI;
1604             pr_info("%s: using MSI\n", nvswitch_dev->name);
1605         }
1606     }
1607 
1608     if (nvswitch_dev->irq_mechanism == NVSWITCH_IRQ_NONE
1609         && test_bit(NVSWITCH_IRQ_PIN, &irq_caps))
1610     {
1611         flags |= IRQF_SHARED;
1612         nvswitch_dev->irq_mechanism = NVSWITCH_IRQ_PIN;
1613         pr_info("%s: using PCI pin\n", nvswitch_dev->name);
1614     }
1615 
1616     if (nvswitch_dev->irq_mechanism == NVSWITCH_IRQ_NONE)
1617     {
1618         pr_err("%s: No supported interrupt mechanism was found. This device supports:\n",
1619                nvswitch_dev->name);
1620 
1621         if (test_bit(NVSWITCH_IRQ_MSIX, &irq_caps))
1622             pr_err("%s: MSI-X\n", nvswitch_dev->name);
1623         if (test_bit(NVSWITCH_IRQ_MSI, &irq_caps))
1624             pr_err("%s: MSI\n", nvswitch_dev->name);
1625         if (test_bit(NVSWITCH_IRQ_PIN, &irq_caps))
1626              pr_err("%s: PCI Pin\n", nvswitch_dev->name);
1627 
1628         return -EINVAL;
1629     }
1630 
1631     rc = request_threaded_irq(pci_dev->irq,
1632                               nvswitch_isr_pending,
1633                               nvswitch_isr_thread,
1634                               flags, nvswitch_dev->sname,
1635                               nvswitch_dev);
1636     if (rc)
1637     {
1638 #ifdef CONFIG_PCI_MSI
1639         if (nvswitch_dev->irq_mechanism == NVSWITCH_IRQ_MSI)
1640         {
1641             pci_disable_msi(pci_dev);
1642         }
1643 #endif
1644         printk(KERN_ERR "%s: failed to get IRQ\n",
1645                nvswitch_dev->name);
1646 
1647         return rc;
1648     }
1649 
1650     return 0;
1651 }
1652 
1653 void
nvswitch_shutdown_device_interrupt(NVSWITCH_DEV * nvswitch_dev)1654 nvswitch_shutdown_device_interrupt
1655 (
1656     NVSWITCH_DEV *nvswitch_dev
1657 )
1658 {
1659     struct pci_dev *pci_dev = nvswitch_dev->pci_dev;
1660 
1661     free_irq(pci_dev->irq, nvswitch_dev);
1662 #ifdef CONFIG_PCI_MSI
1663     if (nvswitch_dev->irq_mechanism == NVSWITCH_IRQ_MSI)
1664     {
1665         pci_disable_msi(pci_dev);
1666     }
1667 #endif
1668 }
1669 
1670 static void
nvswitch_ctl_exit(void)1671 nvswitch_ctl_exit
1672 (
1673     void
1674 )
1675 {
1676     cdev_del(&nvswitch.cdev_ctl);
1677 }
1678 
1679 static int
nvswitch_ctl_init(int major)1680 nvswitch_ctl_init
1681 (
1682     int major
1683 )
1684 {
1685     int rc = 0;
1686     dev_t nvswitch_ctl = MKDEV(major, NVSWITCH_CTL_MINOR);
1687 
1688     cdev_init(&nvswitch.cdev_ctl, &ctl_fops);
1689 
1690     nvswitch.cdev_ctl.owner = THIS_MODULE;
1691 
1692     rc = cdev_add(&nvswitch.cdev_ctl, nvswitch_ctl, 1);
1693     if (rc < 0)
1694     {
1695         printk(KERN_ERR "nvidia-nvswitch: Unable to create cdev ctl\n");
1696         return rc;
1697     }
1698 
1699     return 0;
1700 }
1701 
1702 //
1703 // Initialize nvswitch driver SW state.  This is currently called
1704 // from the RM as a backdoor interface, and not by the Linux device
1705 // manager
1706 //
1707 int
nvswitch_init(void)1708 nvswitch_init
1709 (
1710     void
1711 )
1712 {
1713     int rc;
1714 
1715     if (nvswitch.initialized)
1716     {
1717         printk(KERN_ERR "nvidia-nvswitch: Interface already initialized\n");
1718         return -EBUSY;
1719     }
1720 
1721     BUILD_BUG_ON(NVSWITCH_DEVICE_INSTANCE_MAX >= NVSWITCH_MINOR_COUNT);
1722 
1723     mutex_init(&nvswitch.driver_mutex);
1724 
1725     INIT_LIST_HEAD(&nvswitch.devices);
1726 
1727     rc = alloc_chrdev_region(&nvswitch.devno,
1728                              0,
1729                              NVSWITCH_MINOR_COUNT,
1730                              NVSWITCH_DRIVER_NAME);
1731     if (rc < 0)
1732     {
1733         printk(KERN_ERR "nvidia-nvswitch: Unable to create cdev region\n");
1734         goto alloc_chrdev_region_fail;
1735     }
1736 
1737     printk(KERN_ERR, "nvidia-nvswitch: Major: %d Minor: %d\n",
1738            MAJOR(nvswitch.devno),
1739            MINOR(nvswitch.devno));
1740 
1741     cdev_init(&nvswitch.cdev, &device_fops);
1742     nvswitch.cdev.owner = THIS_MODULE;
1743     rc = cdev_add(&nvswitch.cdev, nvswitch.devno, NVSWITCH_DEVICE_INSTANCE_MAX);
1744     if (rc < 0)
1745     {
1746         printk(KERN_ERR "nvidia-nvswitch: Unable to create cdev\n");
1747         goto cdev_add_fail;
1748     }
1749 
1750     rc = nvswitch_procfs_init();
1751     if (rc < 0)
1752     {
1753         goto nvswitch_procfs_init_fail;
1754     }
1755 
1756     rc = pci_register_driver(&nvswitch_pci_driver);
1757     if (rc < 0)
1758     {
1759         printk(KERN_ERR "nvidia-nvswitch: Failed to register driver : %d\n", rc);
1760         goto pci_register_driver_fail;
1761     }
1762 
1763     rc = nvswitch_ctl_init(MAJOR(nvswitch.devno));
1764     if (rc < 0)
1765     {
1766         goto nvswitch_ctl_init_fail;
1767     }
1768 
1769     nvswitch.initialized = NV_TRUE;
1770 
1771     return 0;
1772 
1773 nvswitch_ctl_init_fail:
1774     pci_unregister_driver(&nvswitch_pci_driver);
1775 
1776 pci_register_driver_fail:
1777 nvswitch_procfs_init_fail:
1778     cdev_del(&nvswitch.cdev);
1779 
1780 cdev_add_fail:
1781     unregister_chrdev_region(nvswitch.devno, NVSWITCH_MINOR_COUNT);
1782 
1783 alloc_chrdev_region_fail:
1784 
1785     return rc;
1786 }
1787 
1788 //
1789 // Clean up driver state on exit.  Currently called from RM backdoor call,
1790 // and not by the Linux device manager.
1791 //
1792 void
nvswitch_exit(void)1793 nvswitch_exit
1794 (
1795     void
1796 )
1797 {
1798     if (NV_FALSE == nvswitch.initialized)
1799     {
1800         return;
1801     }
1802 
1803     nvswitch_ctl_exit();
1804 
1805     pci_unregister_driver(&nvswitch_pci_driver);
1806 
1807     nvswitch_procfs_exit();
1808 
1809     cdev_del(&nvswitch.cdev);
1810 
1811     unregister_chrdev_region(nvswitch.devno, NVSWITCH_MINOR_COUNT);
1812 
1813     WARN_ON(!list_empty(&nvswitch.devices));
1814 
1815     nvswitch.initialized = NV_FALSE;
1816 }
1817 
1818 //
1819 // Get current time in seconds.nanoseconds
1820 // In this implementation, the time is monotonic time
1821 //
1822 NvU64
nvswitch_os_get_platform_time(void)1823 nvswitch_os_get_platform_time
1824 (
1825     void
1826 )
1827 {
1828     struct timespec64 ts;
1829 
1830     ktime_get_raw_ts64(&ts);
1831     return (NvU64) timespec64_to_ns(&ts);
1832 }
1833 
1834 //
1835 // Get current time in seconds.nanoseconds
1836 // In this implementation, the time is from epoch time
1837 // (midnight UTC of January 1, 1970).
1838 // This implementation cannot be used for polling loops
1839 // due to clock skew during system startup (bug 3302382,
1840 // 3297170, 3273847, 3277478, 200693329).
1841 // Instead, nvswitch_os_get_platform_time() is used
1842 // for polling loops
1843 //
1844 NvU64
nvswitch_os_get_platform_time_epoch(void)1845 nvswitch_os_get_platform_time_epoch
1846 (
1847     void
1848 )
1849 {
1850     struct timespec64 ts;
1851 
1852     ktime_get_real_ts64(&ts);
1853     return (NvU64) timespec64_to_ns(&ts);
1854 }
1855 
1856 void
nvswitch_os_print(const int log_level,const char * fmt,...)1857 nvswitch_os_print
1858 (
1859     const int  log_level,
1860     const char *fmt,
1861     ...
1862 )
1863 {
1864     va_list arglist;
1865     char   *kern_level;
1866     char    fmt_printk[NVSWITCH_LOG_BUFFER_SIZE];
1867 
1868     switch (log_level)
1869     {
1870         case NVSWITCH_DBG_LEVEL_MMIO:
1871         case NVSWITCH_DBG_LEVEL_NOISY:
1872             kern_level = KERN_DEBUG;
1873             break;
1874         case NVSWITCH_DBG_LEVEL_INFO:
1875         case NVSWITCH_DBG_LEVEL_SETUP:
1876             kern_level = KERN_INFO;
1877             break;
1878         case NVSWITCH_DBG_LEVEL_WARN:
1879             kern_level = KERN_WARNING;
1880             break;
1881         case NVSWITCH_DBG_LEVEL_ERROR:
1882             kern_level = KERN_ERR;
1883             break;
1884         default:
1885             kern_level = KERN_DEFAULT;
1886             break;
1887     }
1888 
1889     va_start(arglist, fmt);
1890     snprintf(fmt_printk, sizeof(fmt_printk), "%s%s", kern_level, fmt);
1891     vprintk(fmt_printk, arglist);
1892     va_end(arglist);
1893 }
1894 
1895 void
nvswitch_os_override_platform(void * os_handle,NvBool * rtlsim)1896 nvswitch_os_override_platform
1897 (
1898     void *os_handle,
1899     NvBool *rtlsim
1900 )
1901 {
1902     // Never run on RTL
1903     *rtlsim = NV_FALSE;
1904 }
1905 
1906 NvlStatus
nvswitch_os_read_registery_binary(void * os_handle,const char * name,NvU8 * data,NvU32 length)1907 nvswitch_os_read_registery_binary
1908 (
1909     void *os_handle,
1910     const char *name,
1911     NvU8 *data,
1912     NvU32 length
1913 )
1914 {
1915     return -NVL_ERR_NOT_SUPPORTED;
1916 }
1917 
1918 NvU32
nvswitch_os_get_device_count(void)1919 nvswitch_os_get_device_count
1920 (
1921     void
1922 )
1923 {
1924     return NV_ATOMIC_READ(nvswitch.count);
1925 }
1926 
1927 //
1928 // A helper to convert a string to an unsigned int.
1929 //
1930 // The string should be NULL terminated.
1931 // Only works with base16 values.
1932 //
1933 static int
nvswitch_os_strtouint(char * str,unsigned int * data)1934 nvswitch_os_strtouint
1935 (
1936     char *str,
1937     unsigned int *data
1938 )
1939 {
1940     char *p;
1941     unsigned long long val;
1942 
1943     if (!str || !data)
1944     {
1945         return -EINVAL;
1946     }
1947 
1948     *data = 0;
1949     val = 0;
1950     p = str;
1951 
1952     while (*p != '\0')
1953     {
1954         if ((tolower(*p) == 'x') && (*str == '0') && (p == str + 1))
1955         {
1956             p++;
1957         }
1958         else if (*p >='0' && *p <= '9')
1959         {
1960             val = val * 16 + (*p - '0');
1961             p++;
1962         }
1963         else if (tolower(*p) >= 'a' && tolower(*p) <= 'f')
1964         {
1965             val = val * 16 + (tolower(*p) - 'a' + 10);
1966             p++;
1967         }
1968         else
1969         {
1970             return -EINVAL;
1971         }
1972     }
1973 
1974     if (val > 0xFFFFFFFF)
1975     {
1976         return -EINVAL;
1977     }
1978 
1979     *data = (unsigned int)val;
1980 
1981     return 0;
1982 }
1983 
1984 NvlStatus
nvswitch_os_read_registry_dword(void * os_handle,const char * name,NvU32 * data)1985 nvswitch_os_read_registry_dword
1986 (
1987     void *os_handle,
1988     const char *name,
1989     NvU32 *data
1990 )
1991 {
1992     char *regkey, *regkey_val_start, *regkey_val_end;
1993     char regkey_val[NVSWITCH_REGKEY_VALUE_LEN + 1];
1994     NvU32 regkey_val_len = 0;
1995 
1996     *data = 0;
1997 
1998     if (!NvSwitchRegDwords)
1999     {
2000         return -NVL_ERR_GENERIC;
2001     }
2002 
2003     regkey = strstr(NvSwitchRegDwords, name);
2004     if (!regkey)
2005     {
2006         return -NVL_ERR_GENERIC;
2007     }
2008 
2009     regkey = strchr(regkey, '=');
2010     if (!regkey)
2011     {
2012         return -NVL_ERR_GENERIC;
2013     }
2014 
2015     regkey_val_start = regkey + 1;
2016 
2017     regkey_val_end = strchr(regkey, ';');
2018     if (!regkey_val_end)
2019     {
2020         regkey_val_end = strchr(regkey, '\0');
2021     }
2022 
2023     regkey_val_len = regkey_val_end - regkey_val_start;
2024     if (regkey_val_len > NVSWITCH_REGKEY_VALUE_LEN || regkey_val_len == 0)
2025     {
2026         return -NVL_ERR_GENERIC;
2027     }
2028 
2029     strncpy(regkey_val, regkey_val_start, regkey_val_len);
2030     regkey_val[regkey_val_len] = '\0';
2031 
2032     if (nvswitch_os_strtouint(regkey_val, data) != 0)
2033     {
2034         return -NVL_ERR_GENERIC;
2035     }
2036 
2037     return NVL_SUCCESS;
2038 }
2039 
2040 static NvBool
_nvswitch_is_space(const char ch)2041 _nvswitch_is_space(const char ch)
2042 {
2043     return ((ch == ' ') || ((ch >= '\t') && (ch <= '\r')));
2044 }
2045 
2046 static char *
_nvswitch_remove_spaces(const char * in)2047 _nvswitch_remove_spaces(const char *in)
2048 {
2049     unsigned int len = nvswitch_os_strlen(in) + 1;
2050     const char *in_ptr;
2051     char *out, *out_ptr;
2052 
2053     out = nvswitch_os_malloc(len);
2054     if (out == NULL)
2055         return NULL;
2056 
2057     in_ptr = in;
2058     out_ptr = out;
2059 
2060     while (*in_ptr != '\0')
2061     {
2062         if (!_nvswitch_is_space(*in_ptr))
2063             *out_ptr++ = *in_ptr;
2064         in_ptr++;
2065     }
2066     *out_ptr = '\0';
2067 
2068     return out;
2069 }
2070 
2071 /*
2072  * Compare given string UUID with the NvSwitchBlacklist registry parameter string and
2073  * return whether the UUID is in the NvSwitch blacklist
2074  */
2075 NvBool
nvswitch_os_is_uuid_in_blacklist(NvUuid * uuid)2076 nvswitch_os_is_uuid_in_blacklist
2077 (
2078     NvUuid *uuid
2079 )
2080 {
2081     char *list;
2082     char *ptr;
2083     char *token;
2084     NvU8 uuid_string[NVSWITCH_UUID_STRING_LENGTH];
2085 
2086     if (NvSwitchBlacklist == NULL)
2087         return NV_FALSE;
2088 
2089     if (nvswitch_uuid_to_string(uuid, uuid_string, NVSWITCH_UUID_STRING_LENGTH) == 0)
2090         return NV_FALSE;
2091 
2092     if ((list = _nvswitch_remove_spaces(NvSwitchBlacklist)) == NULL)
2093         return NV_FALSE;
2094 
2095     ptr = list;
2096 
2097     while ((token = strsep(&ptr, ",")) != NULL)
2098     {
2099         if (strcmp(token, uuid_string) == 0)
2100         {
2101             nvswitch_os_free(list);
2102             return NV_TRUE;
2103         }
2104     }
2105     nvswitch_os_free(list);
2106     return NV_FALSE;
2107 }
2108 
2109 
2110 NvlStatus
nvswitch_os_alloc_contig_memory(void * os_handle,void ** virt_addr,NvU32 size,NvBool force_dma32)2111 nvswitch_os_alloc_contig_memory
2112 (
2113     void *os_handle,
2114     void **virt_addr,
2115     NvU32 size,
2116     NvBool force_dma32
2117 )
2118 {
2119     NvU32 gfp_flags;
2120     unsigned long nv_gfp_addr = 0;
2121 
2122     if (!virt_addr)
2123         return -NVL_BAD_ARGS;
2124 
2125     gfp_flags = GFP_KERNEL | (force_dma32 ? GFP_DMA32 : 0);
2126     NV_GET_FREE_PAGES(nv_gfp_addr, get_order(size), gfp_flags);
2127 
2128     if(!nv_gfp_addr)
2129     {
2130         pr_err("nvidia-nvswitch: unable to allocate kernel memory\n");
2131         return -NVL_NO_MEM;
2132     }
2133 
2134     *virt_addr = (void *)nv_gfp_addr;
2135 
2136     return NVL_SUCCESS;
2137 }
2138 
2139 void
nvswitch_os_free_contig_memory(void * os_handle,void * virt_addr,NvU32 size)2140 nvswitch_os_free_contig_memory
2141 (
2142     void *os_handle,
2143     void *virt_addr,
2144     NvU32 size
2145 )
2146 {
2147     NV_FREE_PAGES((unsigned long)virt_addr, get_order(size));
2148 }
2149 
2150 static inline int
_nvswitch_to_pci_dma_direction(NvU32 direction)2151 _nvswitch_to_pci_dma_direction
2152 (
2153     NvU32 direction
2154 )
2155 {
2156     if (direction == NVSWITCH_DMA_DIR_TO_SYSMEM)
2157         return DMA_FROM_DEVICE;
2158     else if (direction == NVSWITCH_DMA_DIR_FROM_SYSMEM)
2159         return DMA_TO_DEVICE;
2160     else
2161         return DMA_BIDIRECTIONAL;
2162 }
2163 
2164 NvlStatus
nvswitch_os_map_dma_region(void * os_handle,void * cpu_addr,NvU64 * dma_handle,NvU32 size,NvU32 direction)2165 nvswitch_os_map_dma_region
2166 (
2167     void *os_handle,
2168     void *cpu_addr,
2169     NvU64 *dma_handle,
2170     NvU32 size,
2171     NvU32 direction
2172 )
2173 {
2174     int dma_dir;
2175     struct pci_dev *pdev = (struct pci_dev *)os_handle;
2176 
2177     if (!pdev || !cpu_addr || !dma_handle)
2178         return -NVL_BAD_ARGS;
2179 
2180     dma_dir = _nvswitch_to_pci_dma_direction(direction);
2181 
2182     *dma_handle = (NvU64)dma_map_single(&pdev->dev, cpu_addr, size, dma_dir);
2183 
2184     if (dma_mapping_error(&pdev->dev, *dma_handle))
2185     {
2186         pr_err("nvidia-nvswitch: unable to create PCI DMA mapping\n");
2187         return -NVL_ERR_GENERIC;
2188     }
2189 
2190     return NVL_SUCCESS;
2191 }
2192 
2193 NvlStatus
nvswitch_os_unmap_dma_region(void * os_handle,void * cpu_addr,NvU64 dma_handle,NvU32 size,NvU32 direction)2194 nvswitch_os_unmap_dma_region
2195 (
2196     void *os_handle,
2197     void *cpu_addr,
2198     NvU64 dma_handle,
2199     NvU32 size,
2200     NvU32 direction
2201 )
2202 {
2203     int dma_dir;
2204     struct pci_dev *pdev = (struct pci_dev *)os_handle;
2205 
2206     if (!pdev || !cpu_addr)
2207         return -NVL_BAD_ARGS;
2208 
2209     dma_dir = _nvswitch_to_pci_dma_direction(direction);
2210 
2211     dma_unmap_single(&pdev->dev, dma_handle, size, dma_dir);
2212 
2213     return NVL_SUCCESS;
2214 }
2215 
2216 NvlStatus
nvswitch_os_set_dma_mask(void * os_handle,NvU32 dma_addr_width)2217 nvswitch_os_set_dma_mask
2218 (
2219     void *os_handle,
2220     NvU32 dma_addr_width
2221 )
2222 {
2223     struct pci_dev *pdev = (struct pci_dev *)os_handle;
2224 
2225     if (!pdev)
2226         return -NVL_BAD_ARGS;
2227 
2228     if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(dma_addr_width)))
2229         return -NVL_ERR_GENERIC;
2230 
2231     return NVL_SUCCESS;
2232 }
2233 
2234 NvlStatus
nvswitch_os_sync_dma_region_for_cpu(void * os_handle,NvU64 dma_handle,NvU32 size,NvU32 direction)2235 nvswitch_os_sync_dma_region_for_cpu
2236 (
2237     void *os_handle,
2238     NvU64 dma_handle,
2239     NvU32 size,
2240     NvU32 direction
2241 )
2242 {
2243     int dma_dir;
2244     struct pci_dev *pdev = (struct pci_dev *)os_handle;
2245 
2246     if (!pdev)
2247         return -NVL_BAD_ARGS;
2248 
2249     dma_dir = _nvswitch_to_pci_dma_direction(direction);
2250 
2251     dma_sync_single_for_cpu(&pdev->dev, dma_handle, size, dma_dir);
2252 
2253     return NVL_SUCCESS;
2254 }
2255 
2256 NvlStatus
nvswitch_os_sync_dma_region_for_device(void * os_handle,NvU64 dma_handle,NvU32 size,NvU32 direction)2257 nvswitch_os_sync_dma_region_for_device
2258 (
2259     void *os_handle,
2260     NvU64 dma_handle,
2261     NvU32 size,
2262     NvU32 direction
2263 )
2264 {
2265     int dma_dir;
2266     struct pci_dev *pdev = (struct pci_dev *)os_handle;
2267 
2268     if (!pdev)
2269         return -NVL_BAD_ARGS;
2270 
2271     dma_dir = _nvswitch_to_pci_dma_direction(direction);
2272 
2273     dma_sync_single_for_device(&pdev->dev, dma_handle, size, dma_dir);
2274 
2275     return NVL_SUCCESS;
2276 }
2277 
2278 static inline void *
_nvswitch_os_malloc(NvLength size)2279 _nvswitch_os_malloc
2280 (
2281     NvLength size
2282 )
2283 {
2284     void *ptr = NULL;
2285 
2286     if (!NV_MAY_SLEEP())
2287     {
2288         if (size <= NVSWITCH_KMALLOC_LIMIT)
2289         {
2290             ptr = kmalloc(size, NV_GFP_ATOMIC);
2291         }
2292     }
2293     else
2294     {
2295         if (size <= NVSWITCH_KMALLOC_LIMIT)
2296         {
2297             ptr = kmalloc(size, NV_GFP_NO_OOM);
2298         }
2299 
2300         if (ptr == NULL)
2301         {
2302             ptr = vmalloc(size);
2303         }
2304     }
2305 
2306     return ptr;
2307 }
2308 
2309 void *
nvswitch_os_malloc_trace(NvLength size,const char * file,NvU32 line)2310 nvswitch_os_malloc_trace
2311 (
2312     NvLength size,
2313     const char *file,
2314     NvU32 line
2315 )
2316 {
2317 #if defined(NV_MEM_LOGGER)
2318     void *ptr = _nvswitch_os_malloc(size);
2319     if (ptr)
2320     {
2321         nv_memdbg_add(ptr, size, file, line);
2322     }
2323 
2324     return ptr;
2325 #else
2326     return _nvswitch_os_malloc(size);
2327 #endif
2328 }
2329 
2330 static inline void
_nvswitch_os_free(void * ptr)2331 _nvswitch_os_free
2332 (
2333     void *ptr
2334 )
2335 {
2336     if (!ptr)
2337         return;
2338 
2339     if (is_vmalloc_addr(ptr))
2340     {
2341         vfree(ptr);
2342     }
2343     else
2344     {
2345         kfree(ptr);
2346     }
2347 }
2348 
2349 void
nvswitch_os_free(void * ptr)2350 nvswitch_os_free
2351 (
2352     void *ptr
2353 )
2354 {
2355 #if defined (NV_MEM_LOGGER)
2356     if (ptr == NULL)
2357         return;
2358 
2359     nv_memdbg_remove(ptr, 0, NULL, 0);
2360 
2361     return _nvswitch_os_free(ptr);
2362 #else
2363     return _nvswitch_os_free(ptr);
2364 #endif
2365 }
2366 
2367 NvLength
nvswitch_os_strlen(const char * str)2368 nvswitch_os_strlen
2369 (
2370     const char *str
2371 )
2372 {
2373     return strlen(str);
2374 }
2375 
2376 char*
nvswitch_os_strncpy(char * dest,const char * src,NvLength length)2377 nvswitch_os_strncpy
2378 (
2379     char *dest,
2380     const char *src,
2381     NvLength length
2382 )
2383 {
2384     return strncpy(dest, src, length);
2385 }
2386 
2387 int
nvswitch_os_strncmp(const char * s1,const char * s2,NvLength length)2388 nvswitch_os_strncmp
2389 (
2390     const char *s1,
2391     const char *s2,
2392     NvLength length
2393 )
2394 {
2395     return strncmp(s1, s2, length);
2396 }
2397 
2398 char*
nvswitch_os_strncat(char * s1,const char * s2,NvLength length)2399 nvswitch_os_strncat
2400 (
2401     char *s1,
2402     const char *s2,
2403     NvLength length
2404 )
2405 {
2406     return strncat(s1, s2, length);
2407 }
2408 
2409 void *
nvswitch_os_memset(void * dest,int value,NvLength size)2410 nvswitch_os_memset
2411 (
2412     void *dest,
2413     int value,
2414     NvLength size
2415 )
2416 {
2417      return memset(dest, value, size);
2418 }
2419 
2420 void *
nvswitch_os_memcpy(void * dest,const void * src,NvLength size)2421 nvswitch_os_memcpy
2422 (
2423     void *dest,
2424     const void *src,
2425     NvLength size
2426 )
2427 {
2428     return memcpy(dest, src, size);
2429 }
2430 
2431 int
nvswitch_os_memcmp(const void * s1,const void * s2,NvLength size)2432 nvswitch_os_memcmp
2433 (
2434     const void *s1,
2435     const void *s2,
2436     NvLength size
2437 )
2438 {
2439     return memcmp(s1, s2, size);
2440 }
2441 
2442 NvU32
nvswitch_os_mem_read32(const volatile void * address)2443 nvswitch_os_mem_read32
2444 (
2445     const volatile void * address
2446 )
2447 {
2448     return (*(const volatile NvU32*)(address));
2449 }
2450 
2451 void
nvswitch_os_mem_write32(volatile void * address,NvU32 data)2452 nvswitch_os_mem_write32
2453 (
2454     volatile void *address,
2455     NvU32 data
2456 )
2457 {
2458     (*(volatile NvU32 *)(address)) = data;
2459 }
2460 
2461 NvU64
nvswitch_os_mem_read64(const volatile void * address)2462 nvswitch_os_mem_read64
2463 (
2464     const volatile void * address
2465 )
2466 {
2467     return (*(const volatile NvU64 *)(address));
2468 }
2469 
2470 void
nvswitch_os_mem_write64(volatile void * address,NvU64 data)2471 nvswitch_os_mem_write64
2472 (
2473     volatile void *address,
2474     NvU64 data
2475 )
2476 {
2477     (*(volatile NvU64 *)(address)) = data;
2478 }
2479 
2480 int
nvswitch_os_snprintf(char * dest,NvLength size,const char * fmt,...)2481 nvswitch_os_snprintf
2482 (
2483     char *dest,
2484     NvLength size,
2485     const char *fmt,
2486     ...
2487 )
2488 {
2489     va_list arglist;
2490     int chars_written;
2491 
2492     va_start(arglist, fmt);
2493     chars_written = vsnprintf(dest, size, fmt, arglist);
2494     va_end(arglist);
2495 
2496     return chars_written;
2497 }
2498 
2499 int
nvswitch_os_vsnprintf(char * buf,NvLength size,const char * fmt,va_list arglist)2500 nvswitch_os_vsnprintf
2501 (
2502     char *buf,
2503     NvLength size,
2504     const char *fmt,
2505     va_list arglist
2506 )
2507 {
2508     return vsnprintf(buf, size, fmt, arglist);
2509 }
2510 
2511 void
nvswitch_os_assert_log(const char * fmt,...)2512 nvswitch_os_assert_log
2513 (
2514     const char *fmt,
2515     ...
2516 )
2517 {
2518     if (printk_ratelimit())
2519     {
2520         va_list arglist;
2521         char fmt_printk[NVSWITCH_LOG_BUFFER_SIZE];
2522 
2523         va_start(arglist, fmt);
2524         vsnprintf(fmt_printk, sizeof(fmt_printk), fmt, arglist);
2525         va_end(arglist);
2526         nvswitch_os_print(NVSWITCH_DBG_LEVEL_ERROR, fmt_printk);
2527         WARN_ON(1);
2528      }
2529      dbg_breakpoint();
2530 }
2531 
2532 /*
2533  * Sleep for specified milliseconds. Yields the CPU to scheduler.
2534  */
2535 void
nvswitch_os_sleep(unsigned int ms)2536 nvswitch_os_sleep
2537 (
2538     unsigned int ms
2539 )
2540 {
2541     NV_STATUS status;
2542     status = nv_sleep_ms(ms);
2543 
2544     if (status != NV_OK)
2545     {
2546         if (printk_ratelimit())
2547         {
2548             nvswitch_os_print(NVSWITCH_DBG_LEVEL_ERROR, "NVSwitch: requested"
2549                               " sleep duration %d msec exceeded %d msec\n",
2550                               ms, NV_MAX_ISR_DELAY_MS);
2551             WARN_ON(1);
2552         }
2553     }
2554 }
2555 
2556 NvlStatus
nvswitch_os_acquire_fabric_mgmt_cap(void * osPrivate,NvU64 capDescriptor)2557 nvswitch_os_acquire_fabric_mgmt_cap
2558 (
2559     void *osPrivate,
2560     NvU64 capDescriptor
2561 )
2562 {
2563     int dup_fd = -1;
2564     nvswitch_file_private_t *private_data = (nvswitch_file_private_t *)osPrivate;
2565 
2566     if (private_data == NULL)
2567     {
2568         return -NVL_BAD_ARGS;
2569     }
2570 
2571     dup_fd = nvlink_cap_acquire((int)capDescriptor,
2572                                 NVLINK_CAP_FABRIC_MANAGEMENT);
2573     if (dup_fd < 0)
2574     {
2575         return -NVL_ERR_OPERATING_SYSTEM;
2576     }
2577 
2578     private_data->capability_fds.fabric_mgmt = dup_fd;
2579     return NVL_SUCCESS;
2580 }
2581 
2582 int
nvswitch_os_is_fabric_manager(void * osPrivate)2583 nvswitch_os_is_fabric_manager
2584 (
2585     void *osPrivate
2586 )
2587 {
2588     nvswitch_file_private_t *private_data = (nvswitch_file_private_t *)osPrivate;
2589 
2590     /* Make sure that fabric mgmt capbaility fd is valid */
2591     if ((private_data == NULL) ||
2592         (private_data->capability_fds.fabric_mgmt < 0))
2593     {
2594         return 0;
2595     }
2596 
2597     return 1;
2598 }
2599 
2600 int
nvswitch_os_is_admin(void)2601 nvswitch_os_is_admin
2602 (
2603     void
2604 )
2605 {
2606     return NV_IS_SUSER();
2607 }
2608 
2609 #define NV_KERNEL_RELEASE    ((LINUX_VERSION_CODE >> 16) & 0x0ff)
2610 #define NV_KERNEL_VERSION    ((LINUX_VERSION_CODE >> 8)  & 0x0ff)
2611 #define NV_KERNEL_SUBVERSION ((LINUX_VERSION_CODE)       & 0x0ff)
2612 
2613 NvlStatus
nvswitch_os_get_os_version(NvU32 * pMajorVer,NvU32 * pMinorVer,NvU32 * pBuildNum)2614 nvswitch_os_get_os_version
2615 (
2616     NvU32 *pMajorVer,
2617     NvU32 *pMinorVer,
2618     NvU32 *pBuildNum
2619 )
2620 {
2621     if (pMajorVer)
2622         *pMajorVer = NV_KERNEL_RELEASE;
2623     if (pMinorVer)
2624         *pMinorVer = NV_KERNEL_VERSION;
2625     if (pBuildNum)
2626         *pBuildNum = NV_KERNEL_SUBVERSION;
2627 
2628     return NVL_SUCCESS;
2629 }
2630 
2631 /*!
2632  * @brief: OS specific handling to add an event.
2633  */
2634 NvlStatus
nvswitch_os_add_client_event(void * osHandle,void * osPrivate,NvU32 eventId)2635 nvswitch_os_add_client_event
2636 (
2637     void            *osHandle,
2638     void            *osPrivate,
2639     NvU32           eventId
2640 )
2641 {
2642     return NVL_SUCCESS;
2643 }
2644 
2645 /*!
2646  * @brief: OS specific handling to remove all events corresponding to osPrivate.
2647  */
2648 NvlStatus
nvswitch_os_remove_client_event(void * osHandle,void * osPrivate)2649 nvswitch_os_remove_client_event
2650 (
2651     void            *osHandle,
2652     void            *osPrivate
2653 )
2654 {
2655     return NVL_SUCCESS;
2656 }
2657 
2658 /*!
2659  * @brief: OS specific handling to notify an event.
2660  */
2661 NvlStatus
nvswitch_os_notify_client_event(void * osHandle,void * osPrivate,NvU32 eventId)2662 nvswitch_os_notify_client_event
2663 (
2664     void *osHandle,
2665     void *osPrivate,
2666     NvU32 eventId
2667 )
2668 {
2669     nvswitch_file_private_t *private_data = (nvswitch_file_private_t *)osPrivate;
2670 
2671     if (private_data == NULL)
2672     {
2673         return -NVL_BAD_ARGS;
2674     }
2675 
2676     private_data->file_event.event_pending = NV_TRUE;
2677     wake_up_interruptible(&private_data->file_event.wait_q_event);
2678 
2679     return NVL_SUCCESS;
2680 }
2681 
2682 /*!
2683  * @brief: Gets OS specific support for the REGISTER_EVENTS ioctl
2684  */
2685 NvlStatus
nvswitch_os_get_supported_register_events_params(NvBool * many_events,NvBool * os_descriptor)2686 nvswitch_os_get_supported_register_events_params
2687 (
2688     NvBool *many_events,
2689     NvBool *os_descriptor
2690 )
2691 {
2692     *many_events   = NV_FALSE;
2693     *os_descriptor = NV_FALSE;
2694     return NVL_SUCCESS;
2695 }
2696 
2697 NvlStatus
nvswitch_os_get_pid(NvU32 * pPid)2698 nvswitch_os_get_pid
2699 (
2700     NvU32 *pPid
2701 )
2702 {
2703     if (pPid != NULL)
2704     {
2705         *pPid = task_pid_nr(current);
2706     }
2707 
2708     return NVL_SUCCESS;
2709 }
2710