GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / staging / fsl-mc / bus / dprc-driver.c
1 /*
2  * Freescale data path resource container (DPRC) driver
3  *
4  * Copyright (C) 2014 Freescale Semiconductor, Inc.
5  * Author: German Rivera <German.Rivera@freescale.com>
6  *
7  * This file is licensed under the terms of the GNU General Public
8  * License version 2. This program is licensed "as is" without any
9  * warranty of any kind, whether express or implied.
10  */
11
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/interrupt.h>
15 #include <linux/msi.h>
16 #include "../include/mc-bus.h"
17 #include "../include/mc-sys.h"
18
19 #include "dprc-cmd.h"
20 #include "fsl-mc-private.h"
21
22 #define FSL_MC_DPRC_DRIVER_NAME    "fsl_mc_dprc"
23
24 #define FSL_MC_DEVICE_MATCH(_mc_dev, _obj_desc) \
25         (strcmp((_mc_dev)->obj_desc.type, (_obj_desc)->type) == 0 && \
26          (_mc_dev)->obj_desc.id == (_obj_desc)->id)
27
28 struct dprc_child_objs {
29         int child_count;
30         struct dprc_obj_desc *child_array;
31 };
32
33 static int __fsl_mc_device_remove_if_not_in_mc(struct device *dev, void *data)
34 {
35         int i;
36         struct dprc_child_objs *objs;
37         struct fsl_mc_device *mc_dev;
38
39         WARN_ON(!dev);
40         WARN_ON(!data);
41         mc_dev = to_fsl_mc_device(dev);
42         objs = data;
43
44         for (i = 0; i < objs->child_count; i++) {
45                 struct dprc_obj_desc *obj_desc = &objs->child_array[i];
46
47                 if (strlen(obj_desc->type) != 0 &&
48                     FSL_MC_DEVICE_MATCH(mc_dev, obj_desc))
49                         break;
50         }
51
52         if (i == objs->child_count)
53                 fsl_mc_device_remove(mc_dev);
54
55         return 0;
56 }
57
58 static int __fsl_mc_device_remove(struct device *dev, void *data)
59 {
60         WARN_ON(!dev);
61         WARN_ON(data);
62         fsl_mc_device_remove(to_fsl_mc_device(dev));
63         return 0;
64 }
65
66 /**
67  * dprc_remove_devices - Removes devices for objects removed from a DPRC
68  *
69  * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
70  * @obj_desc_array: array of object descriptors for child objects currently
71  * present in the DPRC in the MC.
72  * @num_child_objects_in_mc: number of entries in obj_desc_array
73  *
74  * Synchronizes the state of the Linux bus driver with the actual state of
75  * the MC by removing devices that represent MC objects that have
76  * been dynamically removed in the physical DPRC.
77  */
78 static void dprc_remove_devices(struct fsl_mc_device *mc_bus_dev,
79                                 struct dprc_obj_desc *obj_desc_array,
80                                 int num_child_objects_in_mc)
81 {
82         if (num_child_objects_in_mc != 0) {
83                 /*
84                  * Remove child objects that are in the DPRC in Linux,
85                  * but not in the MC:
86                  */
87                 struct dprc_child_objs objs;
88
89                 objs.child_count = num_child_objects_in_mc;
90                 objs.child_array = obj_desc_array;
91                 device_for_each_child(&mc_bus_dev->dev, &objs,
92                                       __fsl_mc_device_remove_if_not_in_mc);
93         } else {
94                 /*
95                  * There are no child objects for this DPRC in the MC.
96                  * So, remove all the child devices from Linux:
97                  */
98                 device_for_each_child(&mc_bus_dev->dev, NULL,
99                                       __fsl_mc_device_remove);
100         }
101 }
102
103 static int __fsl_mc_device_match(struct device *dev, void *data)
104 {
105         struct dprc_obj_desc *obj_desc = data;
106         struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
107
108         return FSL_MC_DEVICE_MATCH(mc_dev, obj_desc);
109 }
110
111 static struct fsl_mc_device *fsl_mc_device_lookup(struct dprc_obj_desc
112                                                                 *obj_desc,
113                                                   struct fsl_mc_device
114                                                                 *mc_bus_dev)
115 {
116         struct device *dev;
117
118         dev = device_find_child(&mc_bus_dev->dev, obj_desc,
119                                 __fsl_mc_device_match);
120
121         return dev ? to_fsl_mc_device(dev) : NULL;
122 }
123
124 /**
125  * check_plugged_state_change - Check change in an MC object's plugged state
126  *
127  * @mc_dev: pointer to the fsl-mc device for a given MC object
128  * @obj_desc: pointer to the MC object's descriptor in the MC
129  *
130  * If the plugged state has changed from unplugged to plugged, the fsl-mc
131  * device is bound to the corresponding device driver.
132  * If the plugged state has changed from plugged to unplugged, the fsl-mc
133  * device is unbound from the corresponding device driver.
134  */
135 static void check_plugged_state_change(struct fsl_mc_device *mc_dev,
136                                        struct dprc_obj_desc *obj_desc)
137 {
138         int error;
139         u32 plugged_flag_at_mc =
140                         obj_desc->state & DPRC_OBJ_STATE_PLUGGED;
141
142         if (plugged_flag_at_mc !=
143             (mc_dev->obj_desc.state & DPRC_OBJ_STATE_PLUGGED)) {
144                 if (plugged_flag_at_mc) {
145                         mc_dev->obj_desc.state |= DPRC_OBJ_STATE_PLUGGED;
146                         error = device_attach(&mc_dev->dev);
147                         if (error < 0) {
148                                 dev_err(&mc_dev->dev,
149                                         "device_attach() failed: %d\n",
150                                         error);
151                         }
152                 } else {
153                         mc_dev->obj_desc.state &= ~DPRC_OBJ_STATE_PLUGGED;
154                         device_release_driver(&mc_dev->dev);
155                 }
156         }
157 }
158
159 /**
160  * dprc_add_new_devices - Adds devices to the logical bus for a DPRC
161  *
162  * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
163  * @obj_desc_array: array of device descriptors for child devices currently
164  * present in the physical DPRC.
165  * @num_child_objects_in_mc: number of entries in obj_desc_array
166  *
167  * Synchronizes the state of the Linux bus driver with the actual
168  * state of the MC by adding objects that have been newly discovered
169  * in the physical DPRC.
170  */
171 static void dprc_add_new_devices(struct fsl_mc_device *mc_bus_dev,
172                                  struct dprc_obj_desc *obj_desc_array,
173                                  int num_child_objects_in_mc)
174 {
175         int error;
176         int i;
177
178         for (i = 0; i < num_child_objects_in_mc; i++) {
179                 struct fsl_mc_device *child_dev;
180                 struct dprc_obj_desc *obj_desc = &obj_desc_array[i];
181
182                 if (strlen(obj_desc->type) == 0)
183                         continue;
184
185                 /*
186                  * Check if device is already known to Linux:
187                  */
188                 child_dev = fsl_mc_device_lookup(obj_desc, mc_bus_dev);
189                 if (child_dev) {
190                         check_plugged_state_change(child_dev, obj_desc);
191                         continue;
192                 }
193
194                 error = fsl_mc_device_add(obj_desc, NULL, &mc_bus_dev->dev,
195                                           &child_dev);
196                 if (error < 0)
197                         continue;
198         }
199 }
200
201 /**
202  * dprc_scan_objects - Discover objects in a DPRC
203  *
204  * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
205  * @total_irq_count: total number of IRQs needed by objects in the DPRC.
206  *
207  * Detects objects added and removed from a DPRC and synchronizes the
208  * state of the Linux bus driver, MC by adding and removing
209  * devices accordingly.
210  * Two types of devices can be found in a DPRC: allocatable objects (e.g.,
211  * dpbp, dpmcp) and non-allocatable devices (e.g., dprc, dpni).
212  * All allocatable devices needed to be probed before all non-allocatable
213  * devices, to ensure that device drivers for non-allocatable
214  * devices can allocate any type of allocatable devices.
215  * That is, we need to ensure that the corresponding resource pools are
216  * populated before they can get allocation requests from probe callbacks
217  * of the device drivers for the non-allocatable devices.
218  */
219 int dprc_scan_objects(struct fsl_mc_device *mc_bus_dev,
220                       unsigned int *total_irq_count)
221 {
222         int num_child_objects;
223         int dprc_get_obj_failures;
224         int error;
225         unsigned int irq_count = mc_bus_dev->obj_desc.irq_count;
226         struct dprc_obj_desc *child_obj_desc_array = NULL;
227
228         error = dprc_get_obj_count(mc_bus_dev->mc_io,
229                                    0,
230                                    mc_bus_dev->mc_handle,
231                                    &num_child_objects);
232         if (error < 0) {
233                 dev_err(&mc_bus_dev->dev, "dprc_get_obj_count() failed: %d\n",
234                         error);
235                 return error;
236         }
237
238         if (num_child_objects != 0) {
239                 int i;
240
241                 child_obj_desc_array =
242                     devm_kmalloc_array(&mc_bus_dev->dev, num_child_objects,
243                                        sizeof(*child_obj_desc_array),
244                                        GFP_KERNEL);
245                 if (!child_obj_desc_array)
246                         return -ENOMEM;
247
248                 /*
249                  * Discover objects currently present in the physical DPRC:
250                  */
251                 dprc_get_obj_failures = 0;
252                 for (i = 0; i < num_child_objects; i++) {
253                         struct dprc_obj_desc *obj_desc =
254                             &child_obj_desc_array[i];
255
256                         error = dprc_get_obj(mc_bus_dev->mc_io,
257                                              0,
258                                              mc_bus_dev->mc_handle,
259                                              i, obj_desc);
260                         if (error < 0) {
261                                 dev_err(&mc_bus_dev->dev,
262                                         "dprc_get_obj(i=%d) failed: %d\n",
263                                         i, error);
264                                 /*
265                                  * Mark the obj entry as "invalid", by using the
266                                  * empty string as obj type:
267                                  */
268                                 obj_desc->type[0] = '\0';
269                                 obj_desc->id = error;
270                                 dprc_get_obj_failures++;
271                                 continue;
272                         }
273
274                         /*
275                          * add a quirk for all versions of dpsec < 4.0...none
276                          * are coherent regardless of what the MC reports.
277                          */
278                         if ((strcmp(obj_desc->type, "dpseci") == 0) &&
279                             (obj_desc->ver_major < 4))
280                                 obj_desc->flags |=
281                                         DPRC_OBJ_FLAG_NO_MEM_SHAREABILITY;
282
283                         irq_count += obj_desc->irq_count;
284                         dev_dbg(&mc_bus_dev->dev,
285                                 "Discovered object: type %s, id %d\n",
286                                 obj_desc->type, obj_desc->id);
287                 }
288
289                 if (dprc_get_obj_failures != 0) {
290                         dev_err(&mc_bus_dev->dev,
291                                 "%d out of %d devices could not be retrieved\n",
292                                 dprc_get_obj_failures, num_child_objects);
293                 }
294         }
295
296         *total_irq_count = irq_count;
297         dprc_remove_devices(mc_bus_dev, child_obj_desc_array,
298                             num_child_objects);
299
300         dprc_add_new_devices(mc_bus_dev, child_obj_desc_array,
301                              num_child_objects);
302
303         if (child_obj_desc_array)
304                 devm_kfree(&mc_bus_dev->dev, child_obj_desc_array);
305
306         return 0;
307 }
308 EXPORT_SYMBOL_GPL(dprc_scan_objects);
309
310 /**
311  * dprc_scan_container - Scans a physical DPRC and synchronizes Linux bus state
312  *
313  * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
314  *
315  * Scans the physical DPRC and synchronizes the state of the Linux
316  * bus driver with the actual state of the MC by adding and removing
317  * devices as appropriate.
318  */
319 int dprc_scan_container(struct fsl_mc_device *mc_bus_dev)
320 {
321         int error;
322         unsigned int irq_count;
323         struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
324
325         fsl_mc_init_all_resource_pools(mc_bus_dev);
326
327         /*
328          * Discover objects in the DPRC:
329          */
330         mutex_lock(&mc_bus->scan_mutex);
331         error = dprc_scan_objects(mc_bus_dev, &irq_count);
332         mutex_unlock(&mc_bus->scan_mutex);
333         if (error < 0)
334                 goto error;
335
336         if (dev_get_msi_domain(&mc_bus_dev->dev) && !mc_bus->irq_resources) {
337                 if (irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS) {
338                         dev_warn(&mc_bus_dev->dev,
339                                  "IRQs needed (%u) exceed IRQs preallocated (%u)\n",
340                                  irq_count, FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
341                 }
342
343                 error = fsl_mc_populate_irq_pool(
344                                 mc_bus,
345                                 FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
346                 if (error < 0)
347                         goto error;
348         }
349
350         return 0;
351 error:
352         fsl_mc_cleanup_all_resource_pools(mc_bus_dev);
353         return error;
354 }
355 EXPORT_SYMBOL_GPL(dprc_scan_container);
356
357 /**
358  * dprc_irq0_handler - Regular ISR for DPRC interrupt 0
359  *
360  * @irq: IRQ number of the interrupt being handled
361  * @arg: Pointer to device structure
362  */
363 static irqreturn_t dprc_irq0_handler(int irq_num, void *arg)
364 {
365         return IRQ_WAKE_THREAD;
366 }
367
368 /**
369  * dprc_irq0_handler_thread - Handler thread function for DPRC interrupt 0
370  *
371  * @irq: IRQ number of the interrupt being handled
372  * @arg: Pointer to device structure
373  */
374 static irqreturn_t dprc_irq0_handler_thread(int irq_num, void *arg)
375 {
376         int error;
377         u32 status;
378         struct device *dev = arg;
379         struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
380         struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
381         struct fsl_mc_io *mc_io = mc_dev->mc_io;
382         struct msi_desc *msi_desc = mc_dev->irqs[0]->msi_desc;
383
384         dev_dbg(dev, "DPRC IRQ %d triggered on CPU %u\n",
385                 irq_num, smp_processor_id());
386
387         if (WARN_ON(!(mc_dev->flags & FSL_MC_IS_DPRC)))
388                 return IRQ_HANDLED;
389
390         mutex_lock(&mc_bus->scan_mutex);
391         if (WARN_ON(!msi_desc || msi_desc->irq != (u32)irq_num))
392                 goto out;
393
394         status = 0;
395         error = dprc_get_irq_status(mc_io, 0, mc_dev->mc_handle, 0,
396                                     &status);
397         if (error < 0) {
398                 dev_err(dev,
399                         "dprc_get_irq_status() failed: %d\n", error);
400                 goto out;
401         }
402
403         error = dprc_clear_irq_status(mc_io, 0, mc_dev->mc_handle, 0,
404                                       status);
405         if (error < 0) {
406                 dev_err(dev,
407                         "dprc_clear_irq_status() failed: %d\n", error);
408                 goto out;
409         }
410
411         if (status & (DPRC_IRQ_EVENT_OBJ_ADDED |
412                       DPRC_IRQ_EVENT_OBJ_REMOVED |
413                       DPRC_IRQ_EVENT_CONTAINER_DESTROYED |
414                       DPRC_IRQ_EVENT_OBJ_DESTROYED |
415                       DPRC_IRQ_EVENT_OBJ_CREATED)) {
416                 unsigned int irq_count;
417
418                 error = dprc_scan_objects(mc_dev, &irq_count);
419                 if (error < 0) {
420                         /*
421                          * If the error is -ENXIO, we ignore it, as it indicates
422                          * that the object scan was aborted, as we detected that
423                          * an object was removed from the DPRC in the MC, while
424                          * we were scanning the DPRC.
425                          */
426                         if (error != -ENXIO) {
427                                 dev_err(dev, "dprc_scan_objects() failed: %d\n",
428                                         error);
429                         }
430
431                         goto out;
432                 }
433
434                 if (irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS) {
435                         dev_warn(dev,
436                                  "IRQs needed (%u) exceed IRQs preallocated (%u)\n",
437                                  irq_count, FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
438                 }
439         }
440
441 out:
442         mutex_unlock(&mc_bus->scan_mutex);
443         return IRQ_HANDLED;
444 }
445
446 /*
447  * Disable and clear interrupt for a given DPRC object
448  */
449 static int disable_dprc_irq(struct fsl_mc_device *mc_dev)
450 {
451         int error;
452         struct fsl_mc_io *mc_io = mc_dev->mc_io;
453
454         WARN_ON(mc_dev->obj_desc.irq_count != 1);
455
456         /*
457          * Disable generation of interrupt, while we configure it:
458          */
459         error = dprc_set_irq_enable(mc_io, 0, mc_dev->mc_handle, 0, 0);
460         if (error < 0) {
461                 dev_err(&mc_dev->dev,
462                         "Disabling DPRC IRQ failed: dprc_set_irq_enable() failed: %d\n",
463                         error);
464                 return error;
465         }
466
467         /*
468          * Disable all interrupt causes for the interrupt:
469          */
470         error = dprc_set_irq_mask(mc_io, 0, mc_dev->mc_handle, 0, 0x0);
471         if (error < 0) {
472                 dev_err(&mc_dev->dev,
473                         "Disabling DPRC IRQ failed: dprc_set_irq_mask() failed: %d\n",
474                         error);
475                 return error;
476         }
477
478         /*
479          * Clear any leftover interrupts:
480          */
481         error = dprc_clear_irq_status(mc_io, 0, mc_dev->mc_handle, 0, ~0x0U);
482         if (error < 0) {
483                 dev_err(&mc_dev->dev,
484                         "Disabling DPRC IRQ failed: dprc_clear_irq_status() failed: %d\n",
485                         error);
486                 return error;
487         }
488
489         return 0;
490 }
491
492 static int register_dprc_irq_handler(struct fsl_mc_device *mc_dev)
493 {
494         int error;
495         struct fsl_mc_device_irq *irq = mc_dev->irqs[0];
496
497         WARN_ON(mc_dev->obj_desc.irq_count != 1);
498
499         /*
500          * NOTE: devm_request_threaded_irq() invokes the device-specific
501          * function that programs the MSI physically in the device
502          */
503         error = devm_request_threaded_irq(&mc_dev->dev,
504                                           irq->msi_desc->irq,
505                                           dprc_irq0_handler,
506                                           dprc_irq0_handler_thread,
507                                           IRQF_NO_SUSPEND | IRQF_ONESHOT,
508                                           "FSL MC DPRC irq0",
509                                           &mc_dev->dev);
510         if (error < 0) {
511                 dev_err(&mc_dev->dev,
512                         "devm_request_threaded_irq() failed: %d\n",
513                         error);
514                 return error;
515         }
516
517         return 0;
518 }
519
520 static int enable_dprc_irq(struct fsl_mc_device *mc_dev)
521 {
522         int error;
523
524         /*
525          * Enable all interrupt causes for the interrupt:
526          */
527         error = dprc_set_irq_mask(mc_dev->mc_io, 0, mc_dev->mc_handle, 0,
528                                   ~0x0u);
529         if (error < 0) {
530                 dev_err(&mc_dev->dev,
531                         "Enabling DPRC IRQ failed: dprc_set_irq_mask() failed: %d\n",
532                         error);
533
534                 return error;
535         }
536
537         /*
538          * Enable generation of the interrupt:
539          */
540         error = dprc_set_irq_enable(mc_dev->mc_io, 0, mc_dev->mc_handle, 0, 1);
541         if (error < 0) {
542                 dev_err(&mc_dev->dev,
543                         "Enabling DPRC IRQ failed: dprc_set_irq_enable() failed: %d\n",
544                         error);
545
546                 return error;
547         }
548
549         return 0;
550 }
551
552 /*
553  * Setup interrupt for a given DPRC device
554  */
555 static int dprc_setup_irq(struct fsl_mc_device *mc_dev)
556 {
557         int error;
558
559         error = fsl_mc_allocate_irqs(mc_dev);
560         if (error < 0)
561                 return error;
562
563         error = disable_dprc_irq(mc_dev);
564         if (error < 0)
565                 goto error_free_irqs;
566
567         error = register_dprc_irq_handler(mc_dev);
568         if (error < 0)
569                 goto error_free_irqs;
570
571         error = enable_dprc_irq(mc_dev);
572         if (error < 0)
573                 goto error_free_irqs;
574
575         return 0;
576
577 error_free_irqs:
578         fsl_mc_free_irqs(mc_dev);
579         return error;
580 }
581
582 /**
583  * dprc_probe - callback invoked when a DPRC is being bound to this driver
584  *
585  * @mc_dev: Pointer to fsl-mc device representing a DPRC
586  *
587  * It opens the physical DPRC in the MC.
588  * It scans the DPRC to discover the MC objects contained in it.
589  * It creates the interrupt pool for the MC bus associated with the DPRC.
590  * It configures the interrupts for the DPRC device itself.
591  */
592 static int dprc_probe(struct fsl_mc_device *mc_dev)
593 {
594         int error;
595         size_t region_size;
596         struct device *parent_dev = mc_dev->dev.parent;
597         struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
598         bool mc_io_created = false;
599         bool msi_domain_set = false;
600
601         if (WARN_ON(strcmp(mc_dev->obj_desc.type, "dprc") != 0))
602                 return -EINVAL;
603
604         if (WARN_ON(dev_get_msi_domain(&mc_dev->dev)))
605                 return -EINVAL;
606
607         if (!mc_dev->mc_io) {
608                 /*
609                  * This is a child DPRC:
610                  */
611                 if (WARN_ON(!dev_is_fsl_mc(parent_dev)))
612                         return -EINVAL;
613
614                 if (WARN_ON(mc_dev->obj_desc.region_count == 0))
615                         return -EINVAL;
616
617                 region_size = mc_dev->regions[0].end -
618                               mc_dev->regions[0].start + 1;
619
620                 error = fsl_create_mc_io(&mc_dev->dev,
621                                          mc_dev->regions[0].start,
622                                          region_size,
623                                          NULL,
624                                          FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
625                                          &mc_dev->mc_io);
626                 if (error < 0)
627                         return error;
628
629                 mc_io_created = true;
630
631                 /*
632                  * Inherit parent MSI domain:
633                  */
634                 dev_set_msi_domain(&mc_dev->dev,
635                                    dev_get_msi_domain(parent_dev));
636                 msi_domain_set = true;
637         } else {
638                 /*
639                  * This is a root DPRC
640                  */
641                 struct irq_domain *mc_msi_domain;
642
643                 if (WARN_ON(dev_is_fsl_mc(parent_dev)))
644                         return -EINVAL;
645
646                 error = fsl_mc_find_msi_domain(parent_dev,
647                                                &mc_msi_domain);
648                 if (error < 0) {
649                         dev_warn(&mc_dev->dev,
650                                  "WARNING: MC bus without interrupt support\n");
651                 } else {
652                         dev_set_msi_domain(&mc_dev->dev, mc_msi_domain);
653                         msi_domain_set = true;
654                 }
655         }
656
657         error = dprc_open(mc_dev->mc_io, 0, mc_dev->obj_desc.id,
658                           &mc_dev->mc_handle);
659         if (error < 0) {
660                 dev_err(&mc_dev->dev, "dprc_open() failed: %d\n", error);
661                 goto error_cleanup_msi_domain;
662         }
663
664         error = dprc_get_attributes(mc_dev->mc_io, 0, mc_dev->mc_handle,
665                                     &mc_bus->dprc_attr);
666         if (error < 0) {
667                 dev_err(&mc_dev->dev, "dprc_get_attributes() failed: %d\n",
668                         error);
669                 goto error_cleanup_open;
670         }
671
672         if (mc_bus->dprc_attr.version.major < DPRC_MIN_VER_MAJOR ||
673            (mc_bus->dprc_attr.version.major == DPRC_MIN_VER_MAJOR &&
674             mc_bus->dprc_attr.version.minor < DPRC_MIN_VER_MINOR)) {
675                 dev_err(&mc_dev->dev,
676                         "ERROR: DPRC version %d.%d not supported\n",
677                         mc_bus->dprc_attr.version.major,
678                         mc_bus->dprc_attr.version.minor);
679                 error = -ENOTSUPP;
680                 goto error_cleanup_open;
681         }
682
683         mutex_init(&mc_bus->scan_mutex);
684
685         /*
686          * Discover MC objects in DPRC object:
687          */
688         error = dprc_scan_container(mc_dev);
689         if (error < 0)
690                 goto error_cleanup_open;
691
692         /*
693          * Configure interrupt for the DPRC object associated with this MC bus:
694          */
695         error = dprc_setup_irq(mc_dev);
696         if (error < 0)
697                 goto error_cleanup_open;
698
699         dev_info(&mc_dev->dev, "DPRC device bound to driver");
700         return 0;
701
702 error_cleanup_open:
703         (void)dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
704
705 error_cleanup_msi_domain:
706         if (msi_domain_set)
707                 dev_set_msi_domain(&mc_dev->dev, NULL);
708
709         if (mc_io_created) {
710                 fsl_destroy_mc_io(mc_dev->mc_io);
711                 mc_dev->mc_io = NULL;
712         }
713
714         return error;
715 }
716
717 /*
718  * Tear down interrupt for a given DPRC object
719  */
720 static void dprc_teardown_irq(struct fsl_mc_device *mc_dev)
721 {
722         struct fsl_mc_device_irq *irq = mc_dev->irqs[0];
723
724         (void)disable_dprc_irq(mc_dev);
725
726         devm_free_irq(&mc_dev->dev, irq->msi_desc->irq, &mc_dev->dev);
727
728         fsl_mc_free_irqs(mc_dev);
729 }
730
731 /**
732  * dprc_remove - callback invoked when a DPRC is being unbound from this driver
733  *
734  * @mc_dev: Pointer to fsl-mc device representing the DPRC
735  *
736  * It removes the DPRC's child objects from Linux (not from the MC) and
737  * closes the DPRC device in the MC.
738  * It tears down the interrupts that were configured for the DPRC device.
739  * It destroys the interrupt pool associated with this MC bus.
740  */
741 static int dprc_remove(struct fsl_mc_device *mc_dev)
742 {
743         int error;
744         struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
745
746         if (WARN_ON(strcmp(mc_dev->obj_desc.type, "dprc") != 0))
747                 return -EINVAL;
748         if (WARN_ON(!mc_dev->mc_io))
749                 return -EINVAL;
750
751         if (WARN_ON(!mc_bus->irq_resources))
752                 return -EINVAL;
753
754         if (dev_get_msi_domain(&mc_dev->dev))
755                 dprc_teardown_irq(mc_dev);
756
757         device_for_each_child(&mc_dev->dev, NULL, __fsl_mc_device_remove);
758
759         if (dev_get_msi_domain(&mc_dev->dev)) {
760                 fsl_mc_cleanup_irq_pool(mc_bus);
761                 dev_set_msi_domain(&mc_dev->dev, NULL);
762         }
763
764         fsl_mc_cleanup_all_resource_pools(mc_dev);
765
766         error = dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
767         if (error < 0)
768                 dev_err(&mc_dev->dev, "dprc_close() failed: %d\n", error);
769
770         if (!fsl_mc_is_root_dprc(&mc_dev->dev)) {
771                 fsl_destroy_mc_io(mc_dev->mc_io);
772                 mc_dev->mc_io = NULL;
773         }
774
775         dev_info(&mc_dev->dev, "DPRC device unbound from driver");
776         return 0;
777 }
778
779 static const struct fsl_mc_device_id match_id_table[] = {
780         {
781          .vendor = FSL_MC_VENDOR_FREESCALE,
782          .obj_type = "dprc"},
783         {.vendor = 0x0},
784 };
785
786 static struct fsl_mc_driver dprc_driver = {
787         .driver = {
788                    .name = FSL_MC_DPRC_DRIVER_NAME,
789                    .owner = THIS_MODULE,
790                    .pm = NULL,
791                    },
792         .match_id_table = match_id_table,
793         .probe = dprc_probe,
794         .remove = dprc_remove,
795 };
796
797 int __init dprc_driver_init(void)
798 {
799         return fsl_mc_driver_register(&dprc_driver);
800 }
801
802 void dprc_driver_exit(void)
803 {
804         fsl_mc_driver_unregister(&dprc_driver);
805 }