blob: 2fe554855fa5bbdef4ee1ff0aa63982231393094 [file] [log] [blame]
Heiko J Schickfab97222006-09-22 15:22:22 -07001/*
2 * IBM eServer eHCA Infiniband device driver for Linux on POWER
3 *
4 * PD functions
5 *
6 * Authors: Christoph Raisch <raisch@de.ibm.com>
7 *
8 * Copyright (c) 2005 IBM Corporation
9 *
10 * All rights reserved.
11 *
12 * This source code is distributed under a dual license of GPL v2.0 and OpenIB
13 * BSD.
14 *
15 * OpenIB BSD License
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are met:
19 *
20 * Redistributions of source code must retain the above copyright notice, this
21 * list of conditions and the following disclaimer.
22 *
23 * Redistributions in binary form must reproduce the above copyright notice,
24 * this list of conditions and the following disclaimer in the documentation
25 * and/or other materials
26 * provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
35 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
36 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
Heiko J Schickfab97222006-09-22 15:22:22 -070041#include "ehca_tools.h"
42#include "ehca_iverbs.h"
43
44static struct kmem_cache *pd_cache;
45
46struct ib_pd *ehca_alloc_pd(struct ib_device *device,
47 struct ib_ucontext *context, struct ib_udata *udata)
48{
49 struct ehca_pd *pd;
Stefan Roschere2f81da2007-07-20 16:04:17 +020050 int i;
Heiko J Schickfab97222006-09-22 15:22:22 -070051
Robert P. J. Dayc3762222007-02-10 01:45:03 -080052 pd = kmem_cache_zalloc(pd_cache, GFP_KERNEL);
Heiko J Schickfab97222006-09-22 15:22:22 -070053 if (!pd) {
54 ehca_err(device, "device=%p context=%p out of memory",
55 device, context);
56 return ERR_PTR(-ENOMEM);
57 }
58
Stefan Roschere2f81da2007-07-20 16:04:17 +020059 for (i = 0; i < 2; i++) {
60 INIT_LIST_HEAD(&pd->free[i]);
61 INIT_LIST_HEAD(&pd->full[i]);
62 }
63 mutex_init(&pd->lock);
Heiko J Schickfab97222006-09-22 15:22:22 -070064
65 /*
66 * Kernel PD: when device = -1, 0
67 * User PD: when context != -1
68 */
69 if (!context) {
70 /*
71 * Kernel PDs after init reuses always
72 * the one created in ehca_shca_reopen()
73 */
74 struct ehca_shca *shca = container_of(device, struct ehca_shca,
75 ib_device);
76 pd->fw_pd.value = shca->pd->fw_pd.value;
77 } else
78 pd->fw_pd.value = (u64)pd;
79
80 return &pd->ib_pd;
81}
82
83int ehca_dealloc_pd(struct ib_pd *pd)
84{
Heiko J Schickfab97222006-09-22 15:22:22 -070085 struct ehca_pd *my_pd = container_of(pd, struct ehca_pd, ib_pd);
Stefan Roschere2f81da2007-07-20 16:04:17 +020086 int i, leftovers = 0;
Stefan Roschere2f81da2007-07-20 16:04:17 +020087 struct ipz_small_queue_page *page, *tmp;
Heiko J Schickfab97222006-09-22 15:22:22 -070088
Stefan Roschere2f81da2007-07-20 16:04:17 +020089 for (i = 0; i < 2; i++) {
90 list_splice(&my_pd->full[i], &my_pd->free[i]);
91 list_for_each_entry_safe(page, tmp, &my_pd->free[i], list) {
92 leftovers = 1;
93 free_page(page->page);
94 kmem_cache_free(small_qp_cache, page);
95 }
96 }
97
98 if (leftovers)
99 ehca_warn(pd->device,
100 "Some small queue pages were not freed");
101
102 kmem_cache_free(pd_cache, my_pd);
Heiko J Schickfab97222006-09-22 15:22:22 -0700103
104 return 0;
105}
106
107int ehca_init_pd_cache(void)
108{
109 pd_cache = kmem_cache_create("ehca_cache_pd",
110 sizeof(struct ehca_pd), 0,
111 SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +0900112 NULL);
Heiko J Schickfab97222006-09-22 15:22:22 -0700113 if (!pd_cache)
114 return -ENOMEM;
115 return 0;
116}
117
118void ehca_cleanup_pd_cache(void)
119{
120 if (pd_cache)
121 kmem_cache_destroy(pd_cache);
122}