USB: ohci-pnx4008: Remove unnecessary cast of return value of kzalloc
[pandora-kernel.git] / drivers / net / mlx4 / srq.c
1 /*
2  * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <linux/init.h>
34
35 #include <linux/mlx4/cmd.h>
36
37 #include "mlx4.h"
38 #include "icm.h"
39
40 struct mlx4_srq_context {
41         __be32                  state_logsize_srqn;
42         u8                      logstride;
43         u8                      reserved1[3];
44         u8                      pg_offset;
45         u8                      reserved2[3];
46         u32                     reserved3;
47         u8                      log_page_size;
48         u8                      reserved4[2];
49         u8                      mtt_base_addr_h;
50         __be32                  mtt_base_addr_l;
51         __be32                  pd;
52         __be16                  limit_watermark;
53         __be16                  wqe_cnt;
54         u16                     reserved5;
55         __be16                  wqe_counter;
56         u32                     reserved6;
57         __be64                  db_rec_addr;
58 };
59
60 void mlx4_srq_event(struct mlx4_dev *dev, u32 srqn, int event_type)
61 {
62         struct mlx4_srq_table *srq_table = &mlx4_priv(dev)->srq_table;
63         struct mlx4_srq *srq;
64
65         spin_lock(&srq_table->lock);
66
67         srq = radix_tree_lookup(&srq_table->tree, srqn & (dev->caps.num_srqs - 1));
68         if (srq)
69                 atomic_inc(&srq->refcount);
70
71         spin_unlock(&srq_table->lock);
72
73         if (!srq) {
74                 mlx4_warn(dev, "Async event for bogus SRQ %08x\n", srqn);
75                 return;
76         }
77
78         srq->event(srq, event_type);
79
80         if (atomic_dec_and_test(&srq->refcount))
81                 complete(&srq->free);
82 }
83
84 static int mlx4_SW2HW_SRQ(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
85                           int srq_num)
86 {
87         return mlx4_cmd(dev, mailbox->dma, srq_num, 0, MLX4_CMD_SW2HW_SRQ,
88                         MLX4_CMD_TIME_CLASS_A);
89 }
90
91 static int mlx4_HW2SW_SRQ(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
92                           int srq_num)
93 {
94         return mlx4_cmd_box(dev, 0, mailbox ? mailbox->dma : 0, srq_num,
95                             mailbox ? 0 : 1, MLX4_CMD_HW2SW_SRQ,
96                             MLX4_CMD_TIME_CLASS_A);
97 }
98
99 static int mlx4_ARM_SRQ(struct mlx4_dev *dev, int srq_num, int limit_watermark)
100 {
101         return mlx4_cmd(dev, limit_watermark, srq_num, 0, MLX4_CMD_ARM_SRQ,
102                         MLX4_CMD_TIME_CLASS_B);
103 }
104
105 int mlx4_srq_alloc(struct mlx4_dev *dev, u32 pdn, struct mlx4_mtt *mtt,
106                    u64 db_rec, struct mlx4_srq *srq)
107 {
108         struct mlx4_srq_table *srq_table = &mlx4_priv(dev)->srq_table;
109         struct mlx4_cmd_mailbox *mailbox;
110         struct mlx4_srq_context *srq_context;
111         u64 mtt_addr;
112         int err;
113
114         srq->srqn = mlx4_bitmap_alloc(&srq_table->bitmap);
115         if (srq->srqn == -1)
116                 return -ENOMEM;
117
118         err = mlx4_table_get(dev, &srq_table->table, srq->srqn);
119         if (err)
120                 goto err_out;
121
122         err = mlx4_table_get(dev, &srq_table->cmpt_table, srq->srqn);
123         if (err)
124                 goto err_put;
125
126         spin_lock_irq(&srq_table->lock);
127         err = radix_tree_insert(&srq_table->tree, srq->srqn, srq);
128         spin_unlock_irq(&srq_table->lock);
129         if (err)
130                 goto err_cmpt_put;
131
132         mailbox = mlx4_alloc_cmd_mailbox(dev);
133         if (IS_ERR(mailbox)) {
134                 err = PTR_ERR(mailbox);
135                 goto err_radix;
136         }
137
138         srq_context = mailbox->buf;
139         memset(srq_context, 0, sizeof *srq_context);
140
141         srq_context->state_logsize_srqn = cpu_to_be32((ilog2(srq->max) << 24) |
142                                                       srq->srqn);
143         srq_context->logstride          = srq->wqe_shift - 4;
144         srq_context->log_page_size      = mtt->page_shift - MLX4_ICM_PAGE_SHIFT;
145
146         mtt_addr = mlx4_mtt_addr(dev, mtt);
147         srq_context->mtt_base_addr_h    = mtt_addr >> 32;
148         srq_context->mtt_base_addr_l    = cpu_to_be32(mtt_addr & 0xffffffff);
149         srq_context->pd                 = cpu_to_be32(pdn);
150         srq_context->db_rec_addr        = cpu_to_be64(db_rec);
151
152         err = mlx4_SW2HW_SRQ(dev, mailbox, srq->srqn);
153         mlx4_free_cmd_mailbox(dev, mailbox);
154         if (err)
155                 goto err_radix;
156
157         atomic_set(&srq->refcount, 1);
158         init_completion(&srq->free);
159
160         return 0;
161
162 err_radix:
163         spin_lock_irq(&srq_table->lock);
164         radix_tree_delete(&srq_table->tree, srq->srqn);
165         spin_unlock_irq(&srq_table->lock);
166
167 err_cmpt_put:
168         mlx4_table_put(dev, &srq_table->cmpt_table, srq->srqn);
169
170 err_put:
171         mlx4_table_put(dev, &srq_table->table, srq->srqn);
172
173 err_out:
174         mlx4_bitmap_free(&srq_table->bitmap, srq->srqn);
175
176         return err;
177 }
178 EXPORT_SYMBOL_GPL(mlx4_srq_alloc);
179
180 void mlx4_srq_free(struct mlx4_dev *dev, struct mlx4_srq *srq)
181 {
182         struct mlx4_srq_table *srq_table = &mlx4_priv(dev)->srq_table;
183         int err;
184
185         err = mlx4_HW2SW_SRQ(dev, NULL, srq->srqn);
186         if (err)
187                 mlx4_warn(dev, "HW2SW_SRQ failed (%d) for SRQN %06x\n", err, srq->srqn);
188
189         spin_lock_irq(&srq_table->lock);
190         radix_tree_delete(&srq_table->tree, srq->srqn);
191         spin_unlock_irq(&srq_table->lock);
192
193         if (atomic_dec_and_test(&srq->refcount))
194                 complete(&srq->free);
195         wait_for_completion(&srq->free);
196
197         mlx4_table_put(dev, &srq_table->table, srq->srqn);
198         mlx4_bitmap_free(&srq_table->bitmap, srq->srqn);
199 }
200 EXPORT_SYMBOL_GPL(mlx4_srq_free);
201
202 int mlx4_srq_arm(struct mlx4_dev *dev, struct mlx4_srq *srq, int limit_watermark)
203 {
204         return mlx4_ARM_SRQ(dev, srq->srqn, limit_watermark);
205 }
206 EXPORT_SYMBOL_GPL(mlx4_srq_arm);
207
208 int __devinit mlx4_init_srq_table(struct mlx4_dev *dev)
209 {
210         struct mlx4_srq_table *srq_table = &mlx4_priv(dev)->srq_table;
211         int err;
212
213         spin_lock_init(&srq_table->lock);
214         INIT_RADIX_TREE(&srq_table->tree, GFP_ATOMIC);
215
216         err = mlx4_bitmap_init(&srq_table->bitmap, dev->caps.num_srqs,
217                                dev->caps.num_srqs - 1, dev->caps.reserved_srqs);
218         if (err)
219                 return err;
220
221         return 0;
222 }
223
224 void mlx4_cleanup_srq_table(struct mlx4_dev *dev)
225 {
226         mlx4_bitmap_cleanup(&mlx4_priv(dev)->srq_table.bitmap);
227 }