IB/ehca: Make internal_create/destroy_qp() static
[linux-2.6-block.git] / drivers / infiniband / hw / ehca / ehca_main.c
CommitLineData
fab97220
HS
1/*
2 * IBM eServer eHCA Infiniband device driver for Linux on POWER
3 *
4 * module start stop, hca detection
5 *
6 * Authors: Heiko J Schick <schickhj@de.ibm.com>
7 * Hoang-Nam Nguyen <hnguyen@de.ibm.com>
8 * Joachim Fenkes <fenkes@de.ibm.com>
9 *
10 * Copyright (c) 2005 IBM Corporation
11 *
12 * All rights reserved.
13 *
14 * This source code is distributed under a dual license of GPL v2.0 and OpenIB
15 * BSD.
16 *
17 * OpenIB BSD License
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions are met:
21 *
22 * Redistributions of source code must retain the above copyright notice, this
23 * list of conditions and the following disclaimer.
24 *
25 * Redistributions in binary form must reproduce the above copyright notice,
26 * this list of conditions and the following disclaimer in the documentation
27 * and/or other materials
28 * provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
34 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
37 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
38 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
41 */
42
7e28db5d
HNN
43#ifdef CONFIG_PPC_64K_PAGES
44#include <linux/slab.h>
45#endif
fab97220
HS
46#include "ehca_classes.h"
47#include "ehca_iverbs.h"
48#include "ehca_mrmw.h"
49#include "ehca_tools.h"
50#include "hcp_if.h"
51
52MODULE_LICENSE("Dual BSD/GPL");
53MODULE_AUTHOR("Christoph Raisch <raisch@de.ibm.com>");
54MODULE_DESCRIPTION("IBM eServer HCA InfiniBand Device Driver");
4e430dcb 55MODULE_VERSION("SVNEHCA_0023");
fab97220
HS
56
57int ehca_open_aqp1 = 0;
58int ehca_debug_level = 0;
59int ehca_hw_level = 0;
60int ehca_nr_ports = 2;
61int ehca_use_hp_mr = 0;
62int ehca_port_act_time = 30;
63int ehca_poll_all_eqs = 1;
64int ehca_static_rate = -1;
4e430dcb 65int ehca_scaling_code = 0;
5bb7d929 66int ehca_mr_largepage = 0;
fab97220
HS
67
68module_param_named(open_aqp1, ehca_open_aqp1, int, 0);
69module_param_named(debug_level, ehca_debug_level, int, 0);
70module_param_named(hw_level, ehca_hw_level, int, 0);
71module_param_named(nr_ports, ehca_nr_ports, int, 0);
72module_param_named(use_hp_mr, ehca_use_hp_mr, int, 0);
73module_param_named(port_act_time, ehca_port_act_time, int, 0);
74module_param_named(poll_all_eqs, ehca_poll_all_eqs, int, 0);
75module_param_named(static_rate, ehca_static_rate, int, 0);
5bb7d929
HNN
76module_param_named(scaling_code, ehca_scaling_code, int, 0);
77module_param_named(mr_largepage, ehca_mr_largepage, int, 0);
fab97220
HS
78
79MODULE_PARM_DESC(open_aqp1,
80 "AQP1 on startup (0: no (default), 1: yes)");
81MODULE_PARM_DESC(debug_level,
82 "debug level"
83 " (0: no debug traces (default), 1: with debug traces)");
84MODULE_PARM_DESC(hw_level,
85 "hardware level"
86 " (0: autosensing (default), 1: v. 0.20, 2: v. 0.21)");
87MODULE_PARM_DESC(nr_ports,
88 "number of connected ports (default: 2)");
89MODULE_PARM_DESC(use_hp_mr,
90 "high performance MRs (0: no (default), 1: yes)");
91MODULE_PARM_DESC(port_act_time,
92 "time to wait for port activation (default: 30 sec)");
93MODULE_PARM_DESC(poll_all_eqs,
94 "polls all event queues periodically"
95 " (0: no, 1: yes (default))");
96MODULE_PARM_DESC(static_rate,
97 "set permanent static rate (default: disabled)");
4fd30060 98MODULE_PARM_DESC(scaling_code,
b8a3ba55 99 "set scaling code (0: disabled/default, 1: enabled)");
5bb7d929
HNN
100MODULE_PARM_DESC(mr_largepage,
101 "use large page for MR (0: use PAGE_SIZE (default), "
102 "1: use large page depending on MR size");
fab97220 103
26ed687f
JF
104DEFINE_RWLOCK(ehca_qp_idr_lock);
105DEFINE_RWLOCK(ehca_cq_idr_lock);
fab97220
HS
106DEFINE_IDR(ehca_qp_idr);
107DEFINE_IDR(ehca_cq_idr);
108
9844b71b
JF
109static LIST_HEAD(shca_list); /* list of all registered ehcas */
110static DEFINE_SPINLOCK(shca_list_lock);
fab97220
HS
111
112static struct timer_list poll_eqs_timer;
113
7e28db5d 114#ifdef CONFIG_PPC_64K_PAGES
2b94397a 115static struct kmem_cache *ctblk_cache;
7e28db5d 116
f2d91361 117void *ehca_alloc_fw_ctrlblock(gfp_t flags)
7e28db5d 118{
f2d91361 119 void *ret = kmem_cache_zalloc(ctblk_cache, flags);
7e28db5d
HNN
120 if (!ret)
121 ehca_gen_err("Out of memory for ctblk");
122 return ret;
123}
124
125void ehca_free_fw_ctrlblock(void *ptr)
126{
127 if (ptr)
128 kmem_cache_free(ctblk_cache, ptr);
129
130}
131#endif
132
51d2bfbd
HNN
133int ehca2ib_return_code(u64 ehca_rc)
134{
135 switch (ehca_rc) {
136 case H_SUCCESS:
137 return 0;
138 case H_RESOURCE: /* Resource in use */
139 case H_BUSY:
140 return -EBUSY;
141 case H_NOT_ENOUGH_RESOURCES: /* insufficient resources */
142 case H_CONSTRAINED: /* resource constraint */
143 case H_NO_MEM:
144 return -ENOMEM;
145 default:
146 return -EINVAL;
147 }
148}
149
fab97220
HS
150static int ehca_create_slab_caches(void)
151{
152 int ret;
153
154 ret = ehca_init_pd_cache();
155 if (ret) {
156 ehca_gen_err("Cannot create PD SLAB cache.");
157 return ret;
158 }
159
160 ret = ehca_init_cq_cache();
161 if (ret) {
162 ehca_gen_err("Cannot create CQ SLAB cache.");
163 goto create_slab_caches2;
164 }
165
166 ret = ehca_init_qp_cache();
167 if (ret) {
168 ehca_gen_err("Cannot create QP SLAB cache.");
169 goto create_slab_caches3;
170 }
171
172 ret = ehca_init_av_cache();
173 if (ret) {
174 ehca_gen_err("Cannot create AV SLAB cache.");
175 goto create_slab_caches4;
176 }
177
178 ret = ehca_init_mrmw_cache();
179 if (ret) {
180 ehca_gen_err("Cannot create MR&MW SLAB cache.");
181 goto create_slab_caches5;
182 }
183
7e28db5d
HNN
184#ifdef CONFIG_PPC_64K_PAGES
185 ctblk_cache = kmem_cache_create("ehca_cache_ctblk",
186 EHCA_PAGESIZE, H_CB_ALIGNMENT,
187 SLAB_HWCACHE_ALIGN,
20c2df83 188 NULL);
7e28db5d
HNN
189 if (!ctblk_cache) {
190 ehca_gen_err("Cannot create ctblk SLAB cache.");
191 ehca_cleanup_mrmw_cache();
192 goto create_slab_caches5;
193 }
194#endif
fab97220
HS
195 return 0;
196
197create_slab_caches5:
198 ehca_cleanup_av_cache();
199
200create_slab_caches4:
201 ehca_cleanup_qp_cache();
202
203create_slab_caches3:
204 ehca_cleanup_cq_cache();
205
206create_slab_caches2:
207 ehca_cleanup_pd_cache();
208
209 return ret;
210}
211
212static void ehca_destroy_slab_caches(void)
213{
214 ehca_cleanup_mrmw_cache();
215 ehca_cleanup_av_cache();
216 ehca_cleanup_qp_cache();
217 ehca_cleanup_cq_cache();
218 ehca_cleanup_pd_cache();
7e28db5d
HNN
219#ifdef CONFIG_PPC_64K_PAGES
220 if (ctblk_cache)
221 kmem_cache_destroy(ctblk_cache);
222#endif
fab97220
HS
223}
224
2b94397a
HNN
225#define EHCA_HCAAVER EHCA_BMASK_IBM(32, 39)
226#define EHCA_REVID EHCA_BMASK_IBM(40, 63)
fab97220 227
91f13aa3
JF
228static struct cap_descr {
229 u64 mask;
230 char *descr;
231} hca_cap_descr[] = {
232 { HCA_CAP_AH_PORT_NR_CHECK, "HCA_CAP_AH_PORT_NR_CHECK" },
233 { HCA_CAP_ATOMIC, "HCA_CAP_ATOMIC" },
234 { HCA_CAP_AUTO_PATH_MIG, "HCA_CAP_AUTO_PATH_MIG" },
235 { HCA_CAP_BAD_P_KEY_CTR, "HCA_CAP_BAD_P_KEY_CTR" },
236 { HCA_CAP_SQD_RTS_PORT_CHANGE, "HCA_CAP_SQD_RTS_PORT_CHANGE" },
237 { HCA_CAP_CUR_QP_STATE_MOD, "HCA_CAP_CUR_QP_STATE_MOD" },
238 { HCA_CAP_INIT_TYPE, "HCA_CAP_INIT_TYPE" },
239 { HCA_CAP_PORT_ACTIVE_EVENT, "HCA_CAP_PORT_ACTIVE_EVENT" },
240 { HCA_CAP_Q_KEY_VIOL_CTR, "HCA_CAP_Q_KEY_VIOL_CTR" },
241 { HCA_CAP_WQE_RESIZE, "HCA_CAP_WQE_RESIZE" },
242 { HCA_CAP_RAW_PACKET_MCAST, "HCA_CAP_RAW_PACKET_MCAST" },
243 { HCA_CAP_SHUTDOWN_PORT, "HCA_CAP_SHUTDOWN_PORT" },
244 { HCA_CAP_RC_LL_QP, "HCA_CAP_RC_LL_QP" },
245 { HCA_CAP_SRQ, "HCA_CAP_SRQ" },
246 { HCA_CAP_UD_LL_QP, "HCA_CAP_UD_LL_QP" },
247 { HCA_CAP_RESIZE_MR, "HCA_CAP_RESIZE_MR" },
248 { HCA_CAP_MINI_QP, "HCA_CAP_MINI_QP" },
249};
250
fab97220
HS
251int ehca_sense_attributes(struct ehca_shca *shca)
252{
91f13aa3 253 int i, ret = 0;
fab97220
HS
254 u64 h_ret;
255 struct hipz_query_hca *rblock;
91f13aa3 256 struct hipz_query_port *port;
fab97220 257
f2d91361 258 rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
fab97220
HS
259 if (!rblock) {
260 ehca_gen_err("Cannot allocate rblock memory.");
261 return -ENOMEM;
262 }
263
264 h_ret = hipz_h_query_hca(shca->ipz_hca_handle, rblock);
265 if (h_ret != H_SUCCESS) {
266 ehca_gen_err("Cannot query device properties. h_ret=%lx",
267 h_ret);
268 ret = -EPERM;
91f13aa3 269 goto sense_attributes1;
fab97220
HS
270 }
271
272 if (ehca_nr_ports == 1)
273 shca->num_ports = 1;
274 else
275 shca->num_ports = (u8)rblock->num_ports;
276
277 ehca_gen_dbg(" ... found %x ports", rblock->num_ports);
278
279 if (ehca_hw_level == 0) {
280 u32 hcaaver;
281 u32 revid;
282
283 hcaaver = EHCA_BMASK_GET(EHCA_HCAAVER, rblock->hw_ver);
284 revid = EHCA_BMASK_GET(EHCA_REVID, rblock->hw_ver);
285
286 ehca_gen_dbg(" ... hardware version=%x:%x", hcaaver, revid);
287
fbb9318b
JF
288 if (hcaaver == 1) {
289 if (revid <= 3)
290 shca->hw_level = 0x10 | (revid + 1);
291 else
292 shca->hw_level = 0x14;
293 } else if (hcaaver == 2) {
294 if (revid == 0)
295 shca->hw_level = 0x21;
296 else if (revid == 0x10)
297 shca->hw_level = 0x22;
298 else if (revid == 0x20 || revid == 0x21)
299 shca->hw_level = 0x23;
300 }
301
302 if (!shca->hw_level) {
91f13aa3
JF
303 ehca_gen_warn("unknown hardware version"
304 " - assuming default level");
305 shca->hw_level = 0x22;
306 }
fbb9318b
JF
307 } else
308 shca->hw_level = ehca_hw_level;
fab97220
HS
309 ehca_gen_dbg(" ... hardware level=%x", shca->hw_level);
310
311 shca->sport[0].rate = IB_RATE_30_GBPS;
312 shca->sport[1].rate = IB_RATE_30_GBPS;
313
91f13aa3
JF
314 shca->hca_cap = rblock->hca_cap_indicators;
315 ehca_gen_dbg(" ... HCA capabilities:");
316 for (i = 0; i < ARRAY_SIZE(hca_cap_descr); i++)
317 if (EHCA_BMASK_GET(hca_cap_descr[i].mask, shca->hca_cap))
318 ehca_gen_dbg(" %s", hca_cap_descr[i].descr);
319
5bb7d929
HNN
320 shca->hca_cap_mr_pgsize = rblock->memory_page_size_supported;
321
2b94397a 322 port = (struct hipz_query_port *)rblock;
91f13aa3
JF
323 h_ret = hipz_h_query_port(shca->ipz_hca_handle, 1, port);
324 if (h_ret != H_SUCCESS) {
325 ehca_gen_err("Cannot query port properties. h_ret=%lx",
326 h_ret);
327 ret = -EPERM;
328 goto sense_attributes1;
329 }
330
331 shca->max_mtu = port->max_mtu;
332
333sense_attributes1:
7e28db5d 334 ehca_free_fw_ctrlblock(rblock);
fab97220
HS
335 return ret;
336}
337
338static int init_node_guid(struct ehca_shca *shca)
339{
340 int ret = 0;
341 struct hipz_query_hca *rblock;
342
f2d91361 343 rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
fab97220
HS
344 if (!rblock) {
345 ehca_err(&shca->ib_device, "Can't allocate rblock memory.");
346 return -ENOMEM;
347 }
348
349 if (hipz_h_query_hca(shca->ipz_hca_handle, rblock) != H_SUCCESS) {
350 ehca_err(&shca->ib_device, "Can't query device properties");
351 ret = -EINVAL;
352 goto init_node_guid1;
353 }
354
355 memcpy(&shca->ib_device.node_guid, &rblock->node_guid, sizeof(u64));
356
357init_node_guid1:
7e28db5d 358 ehca_free_fw_ctrlblock(rblock);
fab97220
HS
359 return ret;
360}
361
0f248d9c 362int ehca_init_device(struct ehca_shca *shca)
fab97220
HS
363{
364 int ret;
365
366 ret = init_node_guid(shca);
367 if (ret)
368 return ret;
369
370 strlcpy(shca->ib_device.name, "ehca%d", IB_DEVICE_NAME_MAX);
371 shca->ib_device.owner = THIS_MODULE;
372
a6a12947 373 shca->ib_device.uverbs_abi_ver = 7;
fab97220
HS
374 shca->ib_device.uverbs_cmd_mask =
375 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
376 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
377 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
378 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
379 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
380 (1ull << IB_USER_VERBS_CMD_REG_MR) |
381 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
382 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
383 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
384 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
385 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
386 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
387 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
388 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
389 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
390 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST);
391
07ebafba 392 shca->ib_device.node_type = RDMA_NODE_IB_CA;
fab97220 393 shca->ib_device.phys_port_cnt = shca->num_ports;
f4fd0b22 394 shca->ib_device.num_comp_vectors = 1;
fab97220
HS
395 shca->ib_device.dma_device = &shca->ibmebus_dev->ofdev.dev;
396 shca->ib_device.query_device = ehca_query_device;
397 shca->ib_device.query_port = ehca_query_port;
398 shca->ib_device.query_gid = ehca_query_gid;
399 shca->ib_device.query_pkey = ehca_query_pkey;
400 /* shca->in_device.modify_device = ehca_modify_device */
401 shca->ib_device.modify_port = ehca_modify_port;
402 shca->ib_device.alloc_ucontext = ehca_alloc_ucontext;
403 shca->ib_device.dealloc_ucontext = ehca_dealloc_ucontext;
404 shca->ib_device.alloc_pd = ehca_alloc_pd;
405 shca->ib_device.dealloc_pd = ehca_dealloc_pd;
406 shca->ib_device.create_ah = ehca_create_ah;
407 /* shca->ib_device.modify_ah = ehca_modify_ah; */
408 shca->ib_device.query_ah = ehca_query_ah;
409 shca->ib_device.destroy_ah = ehca_destroy_ah;
410 shca->ib_device.create_qp = ehca_create_qp;
411 shca->ib_device.modify_qp = ehca_modify_qp;
412 shca->ib_device.query_qp = ehca_query_qp;
413 shca->ib_device.destroy_qp = ehca_destroy_qp;
414 shca->ib_device.post_send = ehca_post_send;
415 shca->ib_device.post_recv = ehca_post_recv;
416 shca->ib_device.create_cq = ehca_create_cq;
417 shca->ib_device.destroy_cq = ehca_destroy_cq;
418 shca->ib_device.resize_cq = ehca_resize_cq;
419 shca->ib_device.poll_cq = ehca_poll_cq;
420 /* shca->ib_device.peek_cq = ehca_peek_cq; */
421 shca->ib_device.req_notify_cq = ehca_req_notify_cq;
422 /* shca->ib_device.req_ncomp_notif = ehca_req_ncomp_notif; */
423 shca->ib_device.get_dma_mr = ehca_get_dma_mr;
424 shca->ib_device.reg_phys_mr = ehca_reg_phys_mr;
425 shca->ib_device.reg_user_mr = ehca_reg_user_mr;
426 shca->ib_device.query_mr = ehca_query_mr;
427 shca->ib_device.dereg_mr = ehca_dereg_mr;
428 shca->ib_device.rereg_phys_mr = ehca_rereg_phys_mr;
429 shca->ib_device.alloc_mw = ehca_alloc_mw;
430 shca->ib_device.bind_mw = ehca_bind_mw;
431 shca->ib_device.dealloc_mw = ehca_dealloc_mw;
432 shca->ib_device.alloc_fmr = ehca_alloc_fmr;
433 shca->ib_device.map_phys_fmr = ehca_map_phys_fmr;
434 shca->ib_device.unmap_fmr = ehca_unmap_fmr;
435 shca->ib_device.dealloc_fmr = ehca_dealloc_fmr;
436 shca->ib_device.attach_mcast = ehca_attach_mcast;
437 shca->ib_device.detach_mcast = ehca_detach_mcast;
438 /* shca->ib_device.process_mad = ehca_process_mad; */
439 shca->ib_device.mmap = ehca_mmap;
440
a6a12947
JF
441 if (EHCA_BMASK_GET(HCA_CAP_SRQ, shca->hca_cap)) {
442 shca->ib_device.uverbs_cmd_mask |=
443 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
444 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
445 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
446 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ);
447
448 shca->ib_device.create_srq = ehca_create_srq;
449 shca->ib_device.modify_srq = ehca_modify_srq;
450 shca->ib_device.query_srq = ehca_query_srq;
451 shca->ib_device.destroy_srq = ehca_destroy_srq;
452 shca->ib_device.post_srq_recv = ehca_post_srq_recv;
453 }
454
fab97220
HS
455 return ret;
456}
457
458static int ehca_create_aqp1(struct ehca_shca *shca, u32 port)
459{
460 struct ehca_sport *sport = &shca->sport[port - 1];
461 struct ib_cq *ibcq;
462 struct ib_qp *ibqp;
463 struct ib_qp_init_attr qp_init_attr;
464 int ret;
465
466 if (sport->ibcq_aqp1) {
467 ehca_err(&shca->ib_device, "AQP1 CQ is already created.");
468 return -EPERM;
469 }
470
2b94397a 471 ibcq = ib_create_cq(&shca->ib_device, NULL, NULL, (void *)(-1), 10, 0);
fab97220
HS
472 if (IS_ERR(ibcq)) {
473 ehca_err(&shca->ib_device, "Cannot create AQP1 CQ.");
474 return PTR_ERR(ibcq);
475 }
476 sport->ibcq_aqp1 = ibcq;
477
478 if (sport->ibqp_aqp1) {
479 ehca_err(&shca->ib_device, "AQP1 QP is already created.");
480 ret = -EPERM;
481 goto create_aqp1;
482 }
483
484 memset(&qp_init_attr, 0, sizeof(struct ib_qp_init_attr));
485 qp_init_attr.send_cq = ibcq;
486 qp_init_attr.recv_cq = ibcq;
487 qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
488 qp_init_attr.cap.max_send_wr = 100;
489 qp_init_attr.cap.max_recv_wr = 100;
490 qp_init_attr.cap.max_send_sge = 2;
491 qp_init_attr.cap.max_recv_sge = 1;
492 qp_init_attr.qp_type = IB_QPT_GSI;
493 qp_init_attr.port_num = port;
494 qp_init_attr.qp_context = NULL;
495 qp_init_attr.event_handler = NULL;
496 qp_init_attr.srq = NULL;
497
498 ibqp = ib_create_qp(&shca->pd->ib_pd, &qp_init_attr);
499 if (IS_ERR(ibqp)) {
500 ehca_err(&shca->ib_device, "Cannot create AQP1 QP.");
501 ret = PTR_ERR(ibqp);
502 goto create_aqp1;
503 }
504 sport->ibqp_aqp1 = ibqp;
505
506 return 0;
507
508create_aqp1:
509 ib_destroy_cq(sport->ibcq_aqp1);
510 return ret;
511}
512
513static int ehca_destroy_aqp1(struct ehca_sport *sport)
514{
515 int ret;
516
517 ret = ib_destroy_qp(sport->ibqp_aqp1);
518 if (ret) {
519 ehca_gen_err("Cannot destroy AQP1 QP. ret=%x", ret);
520 return ret;
521 }
522
523 ret = ib_destroy_cq(sport->ibcq_aqp1);
524 if (ret)
525 ehca_gen_err("Cannot destroy AQP1 CQ. ret=%x", ret);
526
527 return ret;
528}
529
530static ssize_t ehca_show_debug_level(struct device_driver *ddp, char *buf)
531{
78d8d5f9
HNN
532 return snprintf(buf, PAGE_SIZE, "%d\n",
533 ehca_debug_level);
fab97220
HS
534}
535
536static ssize_t ehca_store_debug_level(struct device_driver *ddp,
537 const char *buf, size_t count)
538{
539 int value = (*buf) - '0';
540 if (value >= 0 && value <= 9)
541 ehca_debug_level = value;
542 return 1;
543}
544
545DRIVER_ATTR(debug_level, S_IRUSR | S_IWUSR,
546 ehca_show_debug_level, ehca_store_debug_level);
547
bba9b601
JF
548static struct attribute *ehca_drv_attrs[] = {
549 &driver_attr_debug_level.attr,
550 NULL
551};
fab97220 552
bba9b601
JF
553static struct attribute_group ehca_drv_attr_grp = {
554 .attrs = ehca_drv_attrs
555};
fab97220
HS
556
557#define EHCA_RESOURCE_ATTR(name) \
558static ssize_t ehca_show_##name(struct device *dev, \
559 struct device_attribute *attr, \
560 char *buf) \
561{ \
562 struct ehca_shca *shca; \
563 struct hipz_query_hca *rblock; \
564 int data; \
565 \
566 shca = dev->driver_data; \
567 \
f2d91361 568 rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL); \
fab97220
HS
569 if (!rblock) { \
570 dev_err(dev, "Can't allocate rblock memory."); \
571 return 0; \
572 } \
573 \
574 if (hipz_h_query_hca(shca->ipz_hca_handle, rblock) != H_SUCCESS) { \
575 dev_err(dev, "Can't query device properties"); \
7e28db5d 576 ehca_free_fw_ctrlblock(rblock); \
fab97220
HS
577 return 0; \
578 } \
579 \
580 data = rblock->name; \
7e28db5d 581 ehca_free_fw_ctrlblock(rblock); \
fab97220
HS
582 \
583 if ((strcmp(#name, "num_ports") == 0) && (ehca_nr_ports == 1)) \
584 return snprintf(buf, 256, "1\n"); \
585 else \
586 return snprintf(buf, 256, "%d\n", data); \
587 \
588} \
589static DEVICE_ATTR(name, S_IRUGO, ehca_show_##name, NULL);
590
591EHCA_RESOURCE_ATTR(num_ports);
592EHCA_RESOURCE_ATTR(hw_ver);
593EHCA_RESOURCE_ATTR(max_eq);
594EHCA_RESOURCE_ATTR(cur_eq);
595EHCA_RESOURCE_ATTR(max_cq);
596EHCA_RESOURCE_ATTR(cur_cq);
597EHCA_RESOURCE_ATTR(max_qp);
598EHCA_RESOURCE_ATTR(cur_qp);
599EHCA_RESOURCE_ATTR(max_mr);
600EHCA_RESOURCE_ATTR(cur_mr);
601EHCA_RESOURCE_ATTR(max_mw);
602EHCA_RESOURCE_ATTR(cur_mw);
603EHCA_RESOURCE_ATTR(max_pd);
604EHCA_RESOURCE_ATTR(max_ah);
605
606static ssize_t ehca_show_adapter_handle(struct device *dev,
607 struct device_attribute *attr,
608 char *buf)
609{
610 struct ehca_shca *shca = dev->driver_data;
611
612 return sprintf(buf, "%lx\n", shca->ipz_hca_handle.handle);
613
614}
615static DEVICE_ATTR(adapter_handle, S_IRUGO, ehca_show_adapter_handle, NULL);
616
5bb7d929
HNN
617static ssize_t ehca_show_mr_largepage(struct device *dev,
618 struct device_attribute *attr,
619 char *buf)
620{
621 return sprintf(buf, "%d\n", ehca_mr_largepage);
622}
623static DEVICE_ATTR(mr_largepage, S_IRUGO, ehca_show_mr_largepage, NULL);
624
bba9b601
JF
625static struct attribute *ehca_dev_attrs[] = {
626 &dev_attr_adapter_handle.attr,
627 &dev_attr_num_ports.attr,
628 &dev_attr_hw_ver.attr,
629 &dev_attr_max_eq.attr,
630 &dev_attr_cur_eq.attr,
631 &dev_attr_max_cq.attr,
632 &dev_attr_cur_cq.attr,
633 &dev_attr_max_qp.attr,
634 &dev_attr_cur_qp.attr,
635 &dev_attr_max_mr.attr,
636 &dev_attr_cur_mr.attr,
637 &dev_attr_max_mw.attr,
638 &dev_attr_cur_mw.attr,
639 &dev_attr_max_pd.attr,
640 &dev_attr_max_ah.attr,
5bb7d929 641 &dev_attr_mr_largepage.attr,
bba9b601
JF
642 NULL
643};
fab97220 644
bba9b601
JF
645static struct attribute_group ehca_dev_attr_grp = {
646 .attrs = ehca_dev_attrs
647};
fab97220
HS
648
649static int __devinit ehca_probe(struct ibmebus_dev *dev,
650 const struct of_device_id *id)
651{
652 struct ehca_shca *shca;
a7edd0e6 653 const u64 *handle;
fab97220
HS
654 struct ib_pd *ibpd;
655 int ret;
656
40cd3a45 657 handle = of_get_property(dev->ofdev.node, "ibm,hca-handle", NULL);
fab97220
HS
658 if (!handle) {
659 ehca_gen_err("Cannot get eHCA handle for adapter: %s.",
660 dev->ofdev.node->full_name);
661 return -ENODEV;
662 }
663
664 if (!(*handle)) {
665 ehca_gen_err("Wrong eHCA handle for adapter: %s.",
666 dev->ofdev.node->full_name);
667 return -ENODEV;
668 }
669
670 shca = (struct ehca_shca *)ib_alloc_device(sizeof(*shca));
671 if (!shca) {
672 ehca_gen_err("Cannot allocate shca memory.");
673 return -ENOMEM;
674 }
c4ed790d 675 mutex_init(&shca->modify_mutex);
fab97220
HS
676
677 shca->ibmebus_dev = dev;
678 shca->ipz_hca_handle.handle = *handle;
679 dev->ofdev.dev.driver_data = shca;
680
681 ret = ehca_sense_attributes(shca);
682 if (ret < 0) {
683 ehca_gen_err("Cannot sense eHCA attributes.");
684 goto probe1;
685 }
686
0f248d9c 687 ret = ehca_init_device(shca);
fab97220 688 if (ret) {
0f248d9c 689 ehca_gen_err("Cannot init ehca device struct");
fab97220
HS
690 goto probe1;
691 }
692
693 /* create event queues */
694 ret = ehca_create_eq(shca, &shca->eq, EHCA_EQ, 2048);
695 if (ret) {
696 ehca_err(&shca->ib_device, "Cannot create EQ.");
0f248d9c 697 goto probe1;
fab97220
HS
698 }
699
700 ret = ehca_create_eq(shca, &shca->neq, EHCA_NEQ, 513);
701 if (ret) {
702 ehca_err(&shca->ib_device, "Cannot create NEQ.");
703 goto probe3;
704 }
705
706 /* create internal protection domain */
2b94397a 707 ibpd = ehca_alloc_pd(&shca->ib_device, (void *)(-1), NULL);
fab97220
HS
708 if (IS_ERR(ibpd)) {
709 ehca_err(&shca->ib_device, "Cannot create internal PD.");
710 ret = PTR_ERR(ibpd);
711 goto probe4;
712 }
713
714 shca->pd = container_of(ibpd, struct ehca_pd, ib_pd);
715 shca->pd->ib_pd.device = &shca->ib_device;
716
717 /* create internal max MR */
718 ret = ehca_reg_internal_maxmr(shca, shca->pd, &shca->maxmr);
719
720 if (ret) {
721 ehca_err(&shca->ib_device, "Cannot create internal MR ret=%x",
722 ret);
723 goto probe5;
724 }
725
0f248d9c
HNN
726 ret = ib_register_device(&shca->ib_device);
727 if (ret) {
728 ehca_err(&shca->ib_device,
729 "ib_register_device() failed ret=%x", ret);
730 goto probe6;
731 }
732
fab97220
HS
733 /* create AQP1 for port 1 */
734 if (ehca_open_aqp1 == 1) {
735 shca->sport[0].port_state = IB_PORT_DOWN;
736 ret = ehca_create_aqp1(shca, 1);
737 if (ret) {
738 ehca_err(&shca->ib_device,
739 "Cannot create AQP1 for port 1.");
0f248d9c 740 goto probe7;
fab97220
HS
741 }
742 }
743
744 /* create AQP1 for port 2 */
745 if ((ehca_open_aqp1 == 1) && (shca->num_ports == 2)) {
746 shca->sport[1].port_state = IB_PORT_DOWN;
747 ret = ehca_create_aqp1(shca, 2);
748 if (ret) {
749 ehca_err(&shca->ib_device,
750 "Cannot create AQP1 for port 2.");
0f248d9c 751 goto probe8;
fab97220
HS
752 }
753 }
754
bba9b601
JF
755 ret = sysfs_create_group(&dev->ofdev.dev.kobj, &ehca_dev_attr_grp);
756 if (ret) /* only complain; we can live without attributes */
757 ehca_err(&shca->ib_device,
758 "Cannot create device attributes ret=%d", ret);
fab97220
HS
759
760 spin_lock(&shca_list_lock);
761 list_add(&shca->shca_list, &shca_list);
762 spin_unlock(&shca_list_lock);
763
764 return 0;
765
0f248d9c 766probe8:
fab97220
HS
767 ret = ehca_destroy_aqp1(&shca->sport[0]);
768 if (ret)
769 ehca_err(&shca->ib_device,
770 "Cannot destroy AQP1 for port 1. ret=%x", ret);
771
0f248d9c
HNN
772probe7:
773 ib_unregister_device(&shca->ib_device);
774
fab97220
HS
775probe6:
776 ret = ehca_dereg_internal_maxmr(shca);
777 if (ret)
778 ehca_err(&shca->ib_device,
779 "Cannot destroy internal MR. ret=%x", ret);
780
781probe5:
782 ret = ehca_dealloc_pd(&shca->pd->ib_pd);
783 if (ret)
784 ehca_err(&shca->ib_device,
785 "Cannot destroy internal PD. ret=%x", ret);
786
787probe4:
788 ret = ehca_destroy_eq(shca, &shca->neq);
789 if (ret)
790 ehca_err(&shca->ib_device,
791 "Cannot destroy NEQ. ret=%x", ret);
792
793probe3:
794 ret = ehca_destroy_eq(shca, &shca->eq);
795 if (ret)
796 ehca_err(&shca->ib_device,
797 "Cannot destroy EQ. ret=%x", ret);
798
fab97220
HS
799probe1:
800 ib_dealloc_device(&shca->ib_device);
801
802 return -EINVAL;
803}
804
805static int __devexit ehca_remove(struct ibmebus_dev *dev)
806{
807 struct ehca_shca *shca = dev->ofdev.dev.driver_data;
808 int ret;
809
bba9b601 810 sysfs_remove_group(&dev->ofdev.dev.kobj, &ehca_dev_attr_grp);
fab97220
HS
811
812 if (ehca_open_aqp1 == 1) {
813 int i;
814 for (i = 0; i < shca->num_ports; i++) {
815 ret = ehca_destroy_aqp1(&shca->sport[i]);
816 if (ret)
817 ehca_err(&shca->ib_device,
818 "Cannot destroy AQP1 for port %x "
819 "ret=%x", ret, i);
820 }
821 }
822
823 ib_unregister_device(&shca->ib_device);
824
825 ret = ehca_dereg_internal_maxmr(shca);
826 if (ret)
827 ehca_err(&shca->ib_device,
828 "Cannot destroy internal MR. ret=%x", ret);
829
830 ret = ehca_dealloc_pd(&shca->pd->ib_pd);
831 if (ret)
832 ehca_err(&shca->ib_device,
833 "Cannot destroy internal PD. ret=%x", ret);
834
835 ret = ehca_destroy_eq(shca, &shca->eq);
836 if (ret)
837 ehca_err(&shca->ib_device, "Cannot destroy EQ. ret=%x", ret);
838
839 ret = ehca_destroy_eq(shca, &shca->neq);
840 if (ret)
841 ehca_err(&shca->ib_device, "Canot destroy NEQ. ret=%x", ret);
842
843 ib_dealloc_device(&shca->ib_device);
844
845 spin_lock(&shca_list_lock);
846 list_del(&shca->shca_list);
847 spin_unlock(&shca_list_lock);
848
849 return ret;
850}
851
852static struct of_device_id ehca_device_table[] =
853{
854 {
855 .name = "lhca",
856 .compatible = "IBM,lhca",
857 },
858 {},
859};
860
861static struct ibmebus_driver ehca_driver = {
862 .name = "ehca",
863 .id_table = ehca_device_table,
864 .probe = ehca_probe,
865 .remove = ehca_remove,
866};
867
868void ehca_poll_eqs(unsigned long data)
869{
870 struct ehca_shca *shca;
871
872 spin_lock(&shca_list_lock);
873 list_for_each_entry(shca, &shca_list, shca_list) {
78d8d5f9
HNN
874 if (shca->eq.is_initialized) {
875 /* call deadman proc only if eq ptr does not change */
876 struct ehca_eq *eq = &shca->eq;
877 int max = 3;
878 volatile u64 q_ofs, q_ofs2;
879 u64 flags;
880 spin_lock_irqsave(&eq->spinlock, flags);
881 q_ofs = eq->ipz_queue.current_q_offset;
882 spin_unlock_irqrestore(&eq->spinlock, flags);
883 do {
884 spin_lock_irqsave(&eq->spinlock, flags);
885 q_ofs2 = eq->ipz_queue.current_q_offset;
886 spin_unlock_irqrestore(&eq->spinlock, flags);
887 max--;
888 } while (q_ofs == q_ofs2 && max > 0);
889 if (q_ofs == q_ofs2)
890 ehca_process_eq(shca, 0);
891 }
fab97220
HS
892 }
893 mod_timer(&poll_eqs_timer, jiffies + HZ);
894 spin_unlock(&shca_list_lock);
895}
896
897int __init ehca_module_init(void)
898{
899 int ret;
900
901 printk(KERN_INFO "eHCA Infiniband Device Driver "
4e430dcb 902 "(Rel.: SVNEHCA_0023)\n");
fab97220 903
2b94397a
HNN
904 ret = ehca_create_comp_pool();
905 if (ret) {
fab97220
HS
906 ehca_gen_err("Cannot create comp pool.");
907 return ret;
908 }
909
2b94397a
HNN
910 ret = ehca_create_slab_caches();
911 if (ret) {
fab97220
HS
912 ehca_gen_err("Cannot create SLAB caches");
913 ret = -ENOMEM;
914 goto module_init1;
915 }
916
2b94397a
HNN
917 ret = ibmebus_register_driver(&ehca_driver);
918 if (ret) {
fab97220
HS
919 ehca_gen_err("Cannot register eHCA device driver");
920 ret = -EINVAL;
921 goto module_init2;
922 }
923
bba9b601
JF
924 ret = sysfs_create_group(&ehca_driver.driver.kobj, &ehca_drv_attr_grp);
925 if (ret) /* only complain; we can live without attributes */
926 ehca_gen_err("Cannot create driver attributes ret=%d", ret);
fab97220
HS
927
928 if (ehca_poll_all_eqs != 1) {
929 ehca_gen_err("WARNING!!!");
930 ehca_gen_err("It is possible to lose interrupts.");
931 } else {
932 init_timer(&poll_eqs_timer);
933 poll_eqs_timer.function = ehca_poll_eqs;
934 poll_eqs_timer.expires = jiffies + HZ;
935 add_timer(&poll_eqs_timer);
936 }
937
938 return 0;
939
940module_init2:
941 ehca_destroy_slab_caches();
942
943module_init1:
944 ehca_destroy_comp_pool();
945 return ret;
946};
947
948void __exit ehca_module_exit(void)
949{
950 if (ehca_poll_all_eqs == 1)
951 del_timer_sync(&poll_eqs_timer);
952
bba9b601 953 sysfs_remove_group(&ehca_driver.driver.kobj, &ehca_drv_attr_grp);
fab97220
HS
954 ibmebus_unregister_driver(&ehca_driver);
955
956 ehca_destroy_slab_caches();
957
958 ehca_destroy_comp_pool();
959
960 idr_destroy(&ehca_cq_idr);
961 idr_destroy(&ehca_qp_idr);
962};
963
964module_init(ehca_module_init);
965module_exit(ehca_module_exit);