staging: rtl8712: use octal permissions
[linux-2.6-block.git] / drivers / staging / fsl-mc / bus / dprc-driver.c
1 /*
2  * Freescale data path resource container (DPRC) driver
3  *
4  * Copyright (C) 2014-2016 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                         put_device(&child_dev->dev);
192                         continue;
193                 }
194
195                 error = fsl_mc_device_add(obj_desc, NULL, &mc_bus_dev->dev,
196                                           &child_dev);
197                 if (error < 0)
198                         continue;
199         }
200 }
201
202 /**
203  * dprc_scan_objects - Discover objects in a DPRC
204  *
205  * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
206  * @total_irq_count: total number of IRQs needed by objects in the DPRC.
207  *
208  * Detects objects added and removed from a DPRC and synchronizes the
209  * state of the Linux bus driver, MC by adding and removing
210  * devices accordingly.
211  * Two types of devices can be found in a DPRC: allocatable objects (e.g.,
212  * dpbp, dpmcp) and non-allocatable devices (e.g., dprc, dpni).
213  * All allocatable devices needed to be probed before all non-allocatable
214  * devices, to ensure that device drivers for non-allocatable
215  * devices can allocate any type of allocatable devices.
216  * That is, we need to ensure that the corresponding resource pools are
217  * populated before they can get allocation requests from probe callbacks
218  * of the device drivers for the non-allocatable devices.
219  */
220 int dprc_scan_objects(struct fsl_mc_device *mc_bus_dev,
221                       unsigned int *total_irq_count)
222 {
223         int num_child_objects;
224         int dprc_get_obj_failures;
225         int error;
226         unsigned int irq_count = mc_bus_dev->obj_desc.irq_count;
227         struct dprc_obj_desc *child_obj_desc_array = NULL;
228
229         error = dprc_get_obj_count(mc_bus_dev->mc_io,
230                                    0,
231                                    mc_bus_dev->mc_handle,
232                                    &num_child_objects);
233         if (error < 0) {
234                 dev_err(&mc_bus_dev->dev, "dprc_get_obj_count() failed: %d\n",
235                         error);
236                 return error;
237         }
238
239         if (num_child_objects != 0) {
240                 int i;
241
242                 child_obj_desc_array =
243                     devm_kmalloc_array(&mc_bus_dev->dev, num_child_objects,
244                                        sizeof(*child_obj_desc_array),
245                                        GFP_KERNEL);
246                 if (!child_obj_desc_array)
247                         return -ENOMEM;
248
249                 /*
250                  * Discover objects currently present in the physical DPRC:
251                  */
252                 dprc_get_obj_failures = 0;
253                 for (i = 0; i < num_child_objects; i++) {
254                         struct dprc_obj_desc *obj_desc =
255                             &child_obj_desc_array[i];
256
257                         error = dprc_get_obj(mc_bus_dev->mc_io,
258                                              0,
259                                              mc_bus_dev->mc_handle,
260                                              i, obj_desc);
261                         if (error < 0) {
262                                 dev_err(&mc_bus_dev->dev,
263                                         "dprc_get_obj(i=%d) failed: %d\n",
264                                         i, error);
265                                 /*
266                                  * Mark the obj entry as "invalid", by using the
267                                  * empty string as obj type:
268                                  */
269                                 obj_desc->type[0] = '\0';
270                                 obj_desc->id = error;
271                                 dprc_get_obj_failures++;
272                                 continue;
273                         }
274
275                         /*
276                          * add a quirk for all versions of dpsec < 4.0...none
277                          * are coherent regardless of what the MC reports.
278                          */
279                         if ((strcmp(obj_desc->type, "dpseci") == 0) &&
280                             (obj_desc->ver_major < 4))
281                                 obj_desc->flags |=
282                                         DPRC_OBJ_FLAG_NO_MEM_SHAREABILITY;
283
284                         irq_count += obj_desc->irq_count;
285                         dev_dbg(&mc_bus_dev->dev,
286                                 "Discovered object: type %s, id %d\n",
287                                 obj_desc->type, obj_desc->id);
288                 }
289
290                 if (dprc_get_obj_failures != 0) {
291                         dev_err(&mc_bus_dev->dev,
292                                 "%d out of %d devices could not be retrieved\n",
293                                 dprc_get_obj_failures, num_child_objects);
294                 }
295         }
296
297         *total_irq_count = irq_count;
298         dprc_remove_devices(mc_bus_dev, child_obj_desc_array,
299                             num_child_objects);
300
301         dprc_add_new_devices(mc_bus_dev, child_obj_desc_array,
302                              num_child_objects);
303
304         if (child_obj_desc_array)
305                 devm_kfree(&mc_bus_dev->dev, child_obj_desc_array);
306
307         return 0;
308 }
309 EXPORT_SYMBOL_GPL(dprc_scan_objects);
310
311 /**
312  * dprc_scan_container - Scans a physical DPRC and synchronizes Linux bus state
313  *
314  * @mc_bus_dev: pointer to the fsl-mc device that represents a DPRC object
315  *
316  * Scans the physical DPRC and synchronizes the state of the Linux
317  * bus driver with the actual state of the MC by adding and removing
318  * devices as appropriate.
319  */
320 int dprc_scan_container(struct fsl_mc_device *mc_bus_dev)
321 {
322         int error;
323         unsigned int irq_count;
324         struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_bus_dev);
325
326         fsl_mc_init_all_resource_pools(mc_bus_dev);
327
328         /*
329          * Discover objects in the DPRC:
330          */
331         mutex_lock(&mc_bus->scan_mutex);
332         error = dprc_scan_objects(mc_bus_dev, &irq_count);
333         mutex_unlock(&mc_bus->scan_mutex);
334         if (error < 0)
335                 goto error;
336
337         if (dev_get_msi_domain(&mc_bus_dev->dev) && !mc_bus->irq_resources) {
338                 if (irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS) {
339                         dev_warn(&mc_bus_dev->dev,
340                                  "IRQs needed (%u) exceed IRQs preallocated (%u)\n",
341                                  irq_count, FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
342                 }
343
344                 error = fsl_mc_populate_irq_pool(
345                                 mc_bus,
346                                 FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
347                 if (error < 0)
348                         goto error;
349         }
350
351         return 0;
352 error:
353         fsl_mc_cleanup_all_resource_pools(mc_bus_dev);
354         return error;
355 }
356 EXPORT_SYMBOL_GPL(dprc_scan_container);
357
358 /**
359  * dprc_irq0_handler - Regular ISR for DPRC interrupt 0
360  *
361  * @irq: IRQ number of the interrupt being handled
362  * @arg: Pointer to device structure
363  */
364 static irqreturn_t dprc_irq0_handler(int irq_num, void *arg)
365 {
366         return IRQ_WAKE_THREAD;
367 }
368
369 /**
370  * dprc_irq0_handler_thread - Handler thread function for DPRC interrupt 0
371  *
372  * @irq: IRQ number of the interrupt being handled
373  * @arg: Pointer to device structure
374  */
375 static irqreturn_t dprc_irq0_handler_thread(int irq_num, void *arg)
376 {
377         int error;
378         u32 status;
379         struct device *dev = arg;
380         struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
381         struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
382         struct fsl_mc_io *mc_io = mc_dev->mc_io;
383         struct msi_desc *msi_desc = mc_dev->irqs[0]->msi_desc;
384
385         dev_dbg(dev, "DPRC IRQ %d triggered on CPU %u\n",
386                 irq_num, smp_processor_id());
387
388         if (WARN_ON(!(mc_dev->flags & FSL_MC_IS_DPRC)))
389                 return IRQ_HANDLED;
390
391         mutex_lock(&mc_bus->scan_mutex);
392         if (WARN_ON(!msi_desc || msi_desc->irq != (u32)irq_num))
393                 goto out;
394
395         status = 0;
396         error = dprc_get_irq_status(mc_io, 0, mc_dev->mc_handle, 0,
397                                     &status);
398         if (error < 0) {
399                 dev_err(dev,
400                         "dprc_get_irq_status() failed: %d\n", error);
401                 goto out;
402         }
403
404         error = dprc_clear_irq_status(mc_io, 0, mc_dev->mc_handle, 0,
405                                       status);
406         if (error < 0) {
407                 dev_err(dev,
408                         "dprc_clear_irq_status() failed: %d\n", error);
409                 goto out;
410         }
411
412         if (status & (DPRC_IRQ_EVENT_OBJ_ADDED |
413                       DPRC_IRQ_EVENT_OBJ_REMOVED |
414                       DPRC_IRQ_EVENT_CONTAINER_DESTROYED |
415                       DPRC_IRQ_EVENT_OBJ_DESTROYED |
416                       DPRC_IRQ_EVENT_OBJ_CREATED)) {
417                 unsigned int irq_count;
418
419                 error = dprc_scan_objects(mc_dev, &irq_count);
420                 if (error < 0) {
421                         /*
422                          * If the error is -ENXIO, we ignore it, as it indicates
423                          * that the object scan was aborted, as we detected that
424                          * an object was removed from the DPRC in the MC, while
425                          * we were scanning the DPRC.
426                          */
427                         if (error != -ENXIO) {
428                                 dev_err(dev, "dprc_scan_objects() failed: %d\n",
429                                         error);
430                         }
431
432                         goto out;
433                 }
434
435                 if (irq_count > FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS) {
436                         dev_warn(dev,
437                                  "IRQs needed (%u) exceed IRQs preallocated (%u)\n",
438                                  irq_count, FSL_MC_IRQ_POOL_MAX_TOTAL_IRQS);
439                 }
440         }
441
442 out:
443         mutex_unlock(&mc_bus->scan_mutex);
444         return IRQ_HANDLED;
445 }
446
447 /*
448  * Disable and clear interrupt for a given DPRC object
449  */
450 static int disable_dprc_irq(struct fsl_mc_device *mc_dev)
451 {
452         int error;
453         struct fsl_mc_io *mc_io = mc_dev->mc_io;
454
455         WARN_ON(mc_dev->obj_desc.irq_count != 1);
456
457         /*
458          * Disable generation of interrupt, while we configure it:
459          */
460         error = dprc_set_irq_enable(mc_io, 0, mc_dev->mc_handle, 0, 0);
461         if (error < 0) {
462                 dev_err(&mc_dev->dev,
463                         "Disabling DPRC IRQ failed: dprc_set_irq_enable() failed: %d\n",
464                         error);
465                 return error;
466         }
467
468         /*
469          * Disable all interrupt causes for the interrupt:
470          */
471         error = dprc_set_irq_mask(mc_io, 0, mc_dev->mc_handle, 0, 0x0);
472         if (error < 0) {
473                 dev_err(&mc_dev->dev,
474                         "Disabling DPRC IRQ failed: dprc_set_irq_mask() failed: %d\n",
475                         error);
476                 return error;
477         }
478
479         /*
480          * Clear any leftover interrupts:
481          */
482         error = dprc_clear_irq_status(mc_io, 0, mc_dev->mc_handle, 0, ~0x0U);
483         if (error < 0) {
484                 dev_err(&mc_dev->dev,
485                         "Disabling DPRC IRQ failed: dprc_clear_irq_status() failed: %d\n",
486                         error);
487                 return error;
488         }
489
490         return 0;
491 }
492
493 static int register_dprc_irq_handler(struct fsl_mc_device *mc_dev)
494 {
495         int error;
496         struct fsl_mc_device_irq *irq = mc_dev->irqs[0];
497
498         WARN_ON(mc_dev->obj_desc.irq_count != 1);
499
500         /*
501          * NOTE: devm_request_threaded_irq() invokes the device-specific
502          * function that programs the MSI physically in the device
503          */
504         error = devm_request_threaded_irq(&mc_dev->dev,
505                                           irq->msi_desc->irq,
506                                           dprc_irq0_handler,
507                                           dprc_irq0_handler_thread,
508                                           IRQF_NO_SUSPEND | IRQF_ONESHOT,
509                                           dev_name(&mc_dev->dev),
510                                           &mc_dev->dev);
511         if (error < 0) {
512                 dev_err(&mc_dev->dev,
513                         "devm_request_threaded_irq() failed: %d\n",
514                         error);
515                 return error;
516         }
517
518         return 0;
519 }
520
521 static int enable_dprc_irq(struct fsl_mc_device *mc_dev)
522 {
523         int error;
524
525         /*
526          * Enable all interrupt causes for the interrupt:
527          */
528         error = dprc_set_irq_mask(mc_dev->mc_io, 0, mc_dev->mc_handle, 0,
529                                   ~0x0u);
530         if (error < 0) {
531                 dev_err(&mc_dev->dev,
532                         "Enabling DPRC IRQ failed: dprc_set_irq_mask() failed: %d\n",
533                         error);
534
535                 return error;
536         }
537
538         /*
539          * Enable generation of the interrupt:
540          */
541         error = dprc_set_irq_enable(mc_dev->mc_io, 0, mc_dev->mc_handle, 0, 1);
542         if (error < 0) {
543                 dev_err(&mc_dev->dev,
544                         "Enabling DPRC IRQ failed: dprc_set_irq_enable() failed: %d\n",
545                         error);
546
547                 return error;
548         }
549
550         return 0;
551 }
552
553 /*
554  * Setup interrupt for a given DPRC device
555  */
556 static int dprc_setup_irq(struct fsl_mc_device *mc_dev)
557 {
558         int error;
559
560         error = fsl_mc_allocate_irqs(mc_dev);
561         if (error < 0)
562                 return error;
563
564         error = disable_dprc_irq(mc_dev);
565         if (error < 0)
566                 goto error_free_irqs;
567
568         error = register_dprc_irq_handler(mc_dev);
569         if (error < 0)
570                 goto error_free_irqs;
571
572         error = enable_dprc_irq(mc_dev);
573         if (error < 0)
574                 goto error_free_irqs;
575
576         return 0;
577
578 error_free_irqs:
579         fsl_mc_free_irqs(mc_dev);
580         return error;
581 }
582
583 /**
584  * dprc_probe - callback invoked when a DPRC is being bound to this driver
585  *
586  * @mc_dev: Pointer to fsl-mc device representing a DPRC
587  *
588  * It opens the physical DPRC in the MC.
589  * It scans the DPRC to discover the MC objects contained in it.
590  * It creates the interrupt pool for the MC bus associated with the DPRC.
591  * It configures the interrupts for the DPRC device itself.
592  */
593 static int dprc_probe(struct fsl_mc_device *mc_dev)
594 {
595         int error;
596         size_t region_size;
597         struct device *parent_dev = mc_dev->dev.parent;
598         struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
599         bool mc_io_created = false;
600         bool msi_domain_set = false;
601         u16 major_ver, minor_ver;
602
603         if (WARN_ON(strcmp(mc_dev->obj_desc.type, "dprc") != 0))
604                 return -EINVAL;
605
606         if (WARN_ON(dev_get_msi_domain(&mc_dev->dev)))
607                 return -EINVAL;
608
609         if (!mc_dev->mc_io) {
610                 /*
611                  * This is a child DPRC:
612                  */
613                 if (WARN_ON(!dev_is_fsl_mc(parent_dev)))
614                         return -EINVAL;
615
616                 if (WARN_ON(mc_dev->obj_desc.region_count == 0))
617                         return -EINVAL;
618
619                 region_size = mc_dev->regions[0].end -
620                               mc_dev->regions[0].start + 1;
621
622                 error = fsl_create_mc_io(&mc_dev->dev,
623                                          mc_dev->regions[0].start,
624                                          region_size,
625                                          NULL,
626                                          FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
627                                          &mc_dev->mc_io);
628                 if (error < 0)
629                         return error;
630
631                 mc_io_created = true;
632
633                 /*
634                  * Inherit parent MSI domain:
635                  */
636                 dev_set_msi_domain(&mc_dev->dev,
637                                    dev_get_msi_domain(parent_dev));
638                 msi_domain_set = true;
639         } else {
640                 /*
641                  * This is a root DPRC
642                  */
643                 struct irq_domain *mc_msi_domain;
644
645                 if (WARN_ON(dev_is_fsl_mc(parent_dev)))
646                         return -EINVAL;
647
648                 error = fsl_mc_find_msi_domain(parent_dev,
649                                                &mc_msi_domain);
650                 if (error < 0) {
651                         dev_warn(&mc_dev->dev,
652                                  "WARNING: MC bus without interrupt support\n");
653                 } else {
654                         dev_set_msi_domain(&mc_dev->dev, mc_msi_domain);
655                         msi_domain_set = true;
656                 }
657         }
658
659         error = dprc_open(mc_dev->mc_io, 0, mc_dev->obj_desc.id,
660                           &mc_dev->mc_handle);
661         if (error < 0) {
662                 dev_err(&mc_dev->dev, "dprc_open() failed: %d\n", error);
663                 goto error_cleanup_msi_domain;
664         }
665
666         error = dprc_get_attributes(mc_dev->mc_io, 0, mc_dev->mc_handle,
667                                     &mc_bus->dprc_attr);
668         if (error < 0) {
669                 dev_err(&mc_dev->dev, "dprc_get_attributes() failed: %d\n",
670                         error);
671                 goto error_cleanup_open;
672         }
673
674         error = dprc_get_api_version(mc_dev->mc_io, 0,
675                                      &major_ver,
676                                      &minor_ver);
677         if (error < 0) {
678                 dev_err(&mc_dev->dev, "dprc_get_api_version() failed: %d\n",
679                         error);
680                 goto error_cleanup_open;
681         }
682
683         if (major_ver < DPRC_MIN_VER_MAJOR ||
684            (major_ver == DPRC_MIN_VER_MAJOR &&
685             minor_ver < DPRC_MIN_VER_MINOR)) {
686                 dev_err(&mc_dev->dev,
687                         "ERROR: DPRC version %d.%d not supported\n",
688                         major_ver, minor_ver);
689                 error = -ENOTSUPP;
690                 goto error_cleanup_open;
691         }
692
693         mutex_init(&mc_bus->scan_mutex);
694
695         /*
696          * Discover MC objects in DPRC object:
697          */
698         error = dprc_scan_container(mc_dev);
699         if (error < 0)
700                 goto error_cleanup_open;
701
702         /*
703          * Configure interrupt for the DPRC object associated with this MC bus:
704          */
705         error = dprc_setup_irq(mc_dev);
706         if (error < 0)
707                 goto error_cleanup_open;
708
709         dev_info(&mc_dev->dev, "DPRC device bound to driver");
710         return 0;
711
712 error_cleanup_open:
713         (void)dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
714
715 error_cleanup_msi_domain:
716         if (msi_domain_set)
717                 dev_set_msi_domain(&mc_dev->dev, NULL);
718
719         if (mc_io_created) {
720                 fsl_destroy_mc_io(mc_dev->mc_io);
721                 mc_dev->mc_io = NULL;
722         }
723
724         return error;
725 }
726
727 /*
728  * Tear down interrupt for a given DPRC object
729  */
730 static void dprc_teardown_irq(struct fsl_mc_device *mc_dev)
731 {
732         struct fsl_mc_device_irq *irq = mc_dev->irqs[0];
733
734         (void)disable_dprc_irq(mc_dev);
735
736         devm_free_irq(&mc_dev->dev, irq->msi_desc->irq, &mc_dev->dev);
737
738         fsl_mc_free_irqs(mc_dev);
739 }
740
741 /**
742  * dprc_remove - callback invoked when a DPRC is being unbound from this driver
743  *
744  * @mc_dev: Pointer to fsl-mc device representing the DPRC
745  *
746  * It removes the DPRC's child objects from Linux (not from the MC) and
747  * closes the DPRC device in the MC.
748  * It tears down the interrupts that were configured for the DPRC device.
749  * It destroys the interrupt pool associated with this MC bus.
750  */
751 static int dprc_remove(struct fsl_mc_device *mc_dev)
752 {
753         int error;
754         struct fsl_mc_bus *mc_bus = to_fsl_mc_bus(mc_dev);
755
756         if (WARN_ON(strcmp(mc_dev->obj_desc.type, "dprc") != 0))
757                 return -EINVAL;
758         if (WARN_ON(!mc_dev->mc_io))
759                 return -EINVAL;
760
761         if (WARN_ON(!mc_bus->irq_resources))
762                 return -EINVAL;
763
764         if (dev_get_msi_domain(&mc_dev->dev))
765                 dprc_teardown_irq(mc_dev);
766
767         device_for_each_child(&mc_dev->dev, NULL, __fsl_mc_device_remove);
768
769         if (dev_get_msi_domain(&mc_dev->dev)) {
770                 fsl_mc_cleanup_irq_pool(mc_bus);
771                 dev_set_msi_domain(&mc_dev->dev, NULL);
772         }
773
774         fsl_mc_cleanup_all_resource_pools(mc_dev);
775
776         error = dprc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
777         if (error < 0)
778                 dev_err(&mc_dev->dev, "dprc_close() failed: %d\n", error);
779
780         if (!fsl_mc_is_root_dprc(&mc_dev->dev)) {
781                 fsl_destroy_mc_io(mc_dev->mc_io);
782                 mc_dev->mc_io = NULL;
783         }
784
785         dev_info(&mc_dev->dev, "DPRC device unbound from driver");
786         return 0;
787 }
788
789 static const struct fsl_mc_device_id match_id_table[] = {
790         {
791          .vendor = FSL_MC_VENDOR_FREESCALE,
792          .obj_type = "dprc"},
793         {.vendor = 0x0},
794 };
795
796 static struct fsl_mc_driver dprc_driver = {
797         .driver = {
798                    .name = FSL_MC_DPRC_DRIVER_NAME,
799                    .owner = THIS_MODULE,
800                    .pm = NULL,
801                    },
802         .match_id_table = match_id_table,
803         .probe = dprc_probe,
804         .remove = dprc_remove,
805 };
806
807 int __init dprc_driver_init(void)
808 {
809         return fsl_mc_driver_register(&dprc_driver);
810 }
811
812 void dprc_driver_exit(void)
813 {
814         fsl_mc_driver_unregister(&dprc_driver);
815 }