blob: b917cc130e486fae7659be573bdf0287cd7f0890 [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 * 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
Hoang-Nam Nguyen7e28db52006-11-07 00:56:39 +010043#ifdef CONFIG_PPC_64K_PAGES
44#include <linux/slab.h>
45#endif
Heiko J Schickfab97222006-09-22 15:22:22 -070046#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");
Hoang-Nam Nguyen31726792007-02-28 18:01:02 +010055MODULE_VERSION("SVNEHCA_0022");
Heiko J Schickfab97222006-09-22 15:22:22 -070056
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;
Hoang-Nam Nguyen4fd30062007-02-15 17:08:33 +010065int ehca_scaling_code = 1;
Heiko J Schickfab97222006-09-22 15:22:22 -070066
67module_param_named(open_aqp1, ehca_open_aqp1, int, 0);
68module_param_named(debug_level, ehca_debug_level, int, 0);
69module_param_named(hw_level, ehca_hw_level, int, 0);
70module_param_named(nr_ports, ehca_nr_ports, int, 0);
71module_param_named(use_hp_mr, ehca_use_hp_mr, int, 0);
72module_param_named(port_act_time, ehca_port_act_time, int, 0);
73module_param_named(poll_all_eqs, ehca_poll_all_eqs, int, 0);
74module_param_named(static_rate, ehca_static_rate, int, 0);
Hoang-Nam Nguyen4fd30062007-02-15 17:08:33 +010075module_param_named(scaling_code, ehca_scaling_code, int, 0);
Heiko J Schickfab97222006-09-22 15:22:22 -070076
77MODULE_PARM_DESC(open_aqp1,
78 "AQP1 on startup (0: no (default), 1: yes)");
79MODULE_PARM_DESC(debug_level,
80 "debug level"
81 " (0: no debug traces (default), 1: with debug traces)");
82MODULE_PARM_DESC(hw_level,
83 "hardware level"
84 " (0: autosensing (default), 1: v. 0.20, 2: v. 0.21)");
85MODULE_PARM_DESC(nr_ports,
86 "number of connected ports (default: 2)");
87MODULE_PARM_DESC(use_hp_mr,
88 "high performance MRs (0: no (default), 1: yes)");
89MODULE_PARM_DESC(port_act_time,
90 "time to wait for port activation (default: 30 sec)");
91MODULE_PARM_DESC(poll_all_eqs,
92 "polls all event queues periodically"
93 " (0: no, 1: yes (default))");
94MODULE_PARM_DESC(static_rate,
95 "set permanent static rate (default: disabled)");
Hoang-Nam Nguyen4fd30062007-02-15 17:08:33 +010096MODULE_PARM_DESC(scaling_code,
97 "set scaling code (0: disabled, 1: enabled/default)");
Heiko J Schickfab97222006-09-22 15:22:22 -070098
99spinlock_t ehca_qp_idr_lock;
100spinlock_t ehca_cq_idr_lock;
Stefan Roscher5d882782007-05-09 13:47:56 +0200101spinlock_t hcall_lock;
Heiko J Schickfab97222006-09-22 15:22:22 -0700102DEFINE_IDR(ehca_qp_idr);
103DEFINE_IDR(ehca_cq_idr);
104
Hoang-Nam Nguyen7e28db52006-11-07 00:56:39 +0100105
Heiko J Schickfab97222006-09-22 15:22:22 -0700106static struct list_head shca_list; /* list of all registered ehcas */
107static spinlock_t shca_list_lock;
108
109static struct timer_list poll_eqs_timer;
110
Hoang-Nam Nguyen7e28db52006-11-07 00:56:39 +0100111#ifdef CONFIG_PPC_64K_PAGES
112static struct kmem_cache *ctblk_cache = NULL;
113
Hoang-Nam Nguyenf2d91362007-01-09 18:04:14 +0100114void *ehca_alloc_fw_ctrlblock(gfp_t flags)
Hoang-Nam Nguyen7e28db52006-11-07 00:56:39 +0100115{
Hoang-Nam Nguyenf2d91362007-01-09 18:04:14 +0100116 void *ret = kmem_cache_zalloc(ctblk_cache, flags);
Hoang-Nam Nguyen7e28db52006-11-07 00:56:39 +0100117 if (!ret)
118 ehca_gen_err("Out of memory for ctblk");
119 return ret;
120}
121
122void ehca_free_fw_ctrlblock(void *ptr)
123{
124 if (ptr)
125 kmem_cache_free(ctblk_cache, ptr);
126
127}
128#endif
129
Heiko J Schickfab97222006-09-22 15:22:22 -0700130static int ehca_create_slab_caches(void)
131{
132 int ret;
133
134 ret = ehca_init_pd_cache();
135 if (ret) {
136 ehca_gen_err("Cannot create PD SLAB cache.");
137 return ret;
138 }
139
140 ret = ehca_init_cq_cache();
141 if (ret) {
142 ehca_gen_err("Cannot create CQ SLAB cache.");
143 goto create_slab_caches2;
144 }
145
146 ret = ehca_init_qp_cache();
147 if (ret) {
148 ehca_gen_err("Cannot create QP SLAB cache.");
149 goto create_slab_caches3;
150 }
151
152 ret = ehca_init_av_cache();
153 if (ret) {
154 ehca_gen_err("Cannot create AV SLAB cache.");
155 goto create_slab_caches4;
156 }
157
158 ret = ehca_init_mrmw_cache();
159 if (ret) {
160 ehca_gen_err("Cannot create MR&MW SLAB cache.");
161 goto create_slab_caches5;
162 }
163
Hoang-Nam Nguyen7e28db52006-11-07 00:56:39 +0100164#ifdef CONFIG_PPC_64K_PAGES
165 ctblk_cache = kmem_cache_create("ehca_cache_ctblk",
166 EHCA_PAGESIZE, H_CB_ALIGNMENT,
167 SLAB_HWCACHE_ALIGN,
168 NULL, NULL);
169 if (!ctblk_cache) {
170 ehca_gen_err("Cannot create ctblk SLAB cache.");
171 ehca_cleanup_mrmw_cache();
172 goto create_slab_caches5;
173 }
174#endif
Heiko J Schickfab97222006-09-22 15:22:22 -0700175 return 0;
176
177create_slab_caches5:
178 ehca_cleanup_av_cache();
179
180create_slab_caches4:
181 ehca_cleanup_qp_cache();
182
183create_slab_caches3:
184 ehca_cleanup_cq_cache();
185
186create_slab_caches2:
187 ehca_cleanup_pd_cache();
188
189 return ret;
190}
191
192static void ehca_destroy_slab_caches(void)
193{
194 ehca_cleanup_mrmw_cache();
195 ehca_cleanup_av_cache();
196 ehca_cleanup_qp_cache();
197 ehca_cleanup_cq_cache();
198 ehca_cleanup_pd_cache();
Hoang-Nam Nguyen7e28db52006-11-07 00:56:39 +0100199#ifdef CONFIG_PPC_64K_PAGES
200 if (ctblk_cache)
201 kmem_cache_destroy(ctblk_cache);
202#endif
Heiko J Schickfab97222006-09-22 15:22:22 -0700203}
204
205#define EHCA_HCAAVER EHCA_BMASK_IBM(32,39)
206#define EHCA_REVID EHCA_BMASK_IBM(40,63)
207
208int ehca_sense_attributes(struct ehca_shca *shca)
209{
210 int ret = 0;
211 u64 h_ret;
212 struct hipz_query_hca *rblock;
213
Hoang-Nam Nguyenf2d91362007-01-09 18:04:14 +0100214 rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
Heiko J Schickfab97222006-09-22 15:22:22 -0700215 if (!rblock) {
216 ehca_gen_err("Cannot allocate rblock memory.");
217 return -ENOMEM;
218 }
219
220 h_ret = hipz_h_query_hca(shca->ipz_hca_handle, rblock);
221 if (h_ret != H_SUCCESS) {
222 ehca_gen_err("Cannot query device properties. h_ret=%lx",
223 h_ret);
224 ret = -EPERM;
225 goto num_ports1;
226 }
227
228 if (ehca_nr_ports == 1)
229 shca->num_ports = 1;
230 else
231 shca->num_ports = (u8)rblock->num_ports;
232
233 ehca_gen_dbg(" ... found %x ports", rblock->num_ports);
234
235 if (ehca_hw_level == 0) {
236 u32 hcaaver;
237 u32 revid;
238
239 hcaaver = EHCA_BMASK_GET(EHCA_HCAAVER, rblock->hw_ver);
240 revid = EHCA_BMASK_GET(EHCA_REVID, rblock->hw_ver);
241
242 ehca_gen_dbg(" ... hardware version=%x:%x", hcaaver, revid);
243
244 if ((hcaaver == 1) && (revid == 0))
245 shca->hw_level = 0;
246 else if ((hcaaver == 1) && (revid == 1))
247 shca->hw_level = 1;
248 else if ((hcaaver == 1) && (revid == 2))
249 shca->hw_level = 2;
250 }
251 ehca_gen_dbg(" ... hardware level=%x", shca->hw_level);
252
253 shca->sport[0].rate = IB_RATE_30_GBPS;
254 shca->sport[1].rate = IB_RATE_30_GBPS;
255
256num_ports1:
Hoang-Nam Nguyen7e28db52006-11-07 00:56:39 +0100257 ehca_free_fw_ctrlblock(rblock);
Heiko J Schickfab97222006-09-22 15:22:22 -0700258 return ret;
259}
260
261static int init_node_guid(struct ehca_shca *shca)
262{
263 int ret = 0;
264 struct hipz_query_hca *rblock;
265
Hoang-Nam Nguyenf2d91362007-01-09 18:04:14 +0100266 rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
Heiko J Schickfab97222006-09-22 15:22:22 -0700267 if (!rblock) {
268 ehca_err(&shca->ib_device, "Can't allocate rblock memory.");
269 return -ENOMEM;
270 }
271
272 if (hipz_h_query_hca(shca->ipz_hca_handle, rblock) != H_SUCCESS) {
273 ehca_err(&shca->ib_device, "Can't query device properties");
274 ret = -EINVAL;
275 goto init_node_guid1;
276 }
277
278 memcpy(&shca->ib_device.node_guid, &rblock->node_guid, sizeof(u64));
279
280init_node_guid1:
Hoang-Nam Nguyen7e28db52006-11-07 00:56:39 +0100281 ehca_free_fw_ctrlblock(rblock);
Heiko J Schickfab97222006-09-22 15:22:22 -0700282 return ret;
283}
284
Hoang-Nam Nguyen0f248d92006-10-02 14:52:17 -0700285int ehca_init_device(struct ehca_shca *shca)
Heiko J Schickfab97222006-09-22 15:22:22 -0700286{
287 int ret;
288
289 ret = init_node_guid(shca);
290 if (ret)
291 return ret;
292
293 strlcpy(shca->ib_device.name, "ehca%d", IB_DEVICE_NAME_MAX);
294 shca->ib_device.owner = THIS_MODULE;
295
Hoang-Nam Nguyen4c34bdf2007-01-24 00:13:35 +0100296 shca->ib_device.uverbs_abi_ver = 6;
Heiko J Schickfab97222006-09-22 15:22:22 -0700297 shca->ib_device.uverbs_cmd_mask =
298 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
299 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
300 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
301 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
302 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
303 (1ull << IB_USER_VERBS_CMD_REG_MR) |
304 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
305 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
306 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
307 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
308 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
309 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
310 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
311 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
312 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
313 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST);
314
Tom Tucker07ebafb2006-08-03 16:02:42 -0500315 shca->ib_device.node_type = RDMA_NODE_IB_CA;
Heiko J Schickfab97222006-09-22 15:22:22 -0700316 shca->ib_device.phys_port_cnt = shca->num_ports;
Michael S. Tsirkinf4fd0b22007-05-03 13:48:47 +0300317 shca->ib_device.num_comp_vectors = 1;
Heiko J Schickfab97222006-09-22 15:22:22 -0700318 shca->ib_device.dma_device = &shca->ibmebus_dev->ofdev.dev;
319 shca->ib_device.query_device = ehca_query_device;
320 shca->ib_device.query_port = ehca_query_port;
321 shca->ib_device.query_gid = ehca_query_gid;
322 shca->ib_device.query_pkey = ehca_query_pkey;
323 /* shca->in_device.modify_device = ehca_modify_device */
324 shca->ib_device.modify_port = ehca_modify_port;
325 shca->ib_device.alloc_ucontext = ehca_alloc_ucontext;
326 shca->ib_device.dealloc_ucontext = ehca_dealloc_ucontext;
327 shca->ib_device.alloc_pd = ehca_alloc_pd;
328 shca->ib_device.dealloc_pd = ehca_dealloc_pd;
329 shca->ib_device.create_ah = ehca_create_ah;
330 /* shca->ib_device.modify_ah = ehca_modify_ah; */
331 shca->ib_device.query_ah = ehca_query_ah;
332 shca->ib_device.destroy_ah = ehca_destroy_ah;
333 shca->ib_device.create_qp = ehca_create_qp;
334 shca->ib_device.modify_qp = ehca_modify_qp;
335 shca->ib_device.query_qp = ehca_query_qp;
336 shca->ib_device.destroy_qp = ehca_destroy_qp;
337 shca->ib_device.post_send = ehca_post_send;
338 shca->ib_device.post_recv = ehca_post_recv;
339 shca->ib_device.create_cq = ehca_create_cq;
340 shca->ib_device.destroy_cq = ehca_destroy_cq;
341 shca->ib_device.resize_cq = ehca_resize_cq;
342 shca->ib_device.poll_cq = ehca_poll_cq;
343 /* shca->ib_device.peek_cq = ehca_peek_cq; */
344 shca->ib_device.req_notify_cq = ehca_req_notify_cq;
345 /* shca->ib_device.req_ncomp_notif = ehca_req_ncomp_notif; */
346 shca->ib_device.get_dma_mr = ehca_get_dma_mr;
347 shca->ib_device.reg_phys_mr = ehca_reg_phys_mr;
348 shca->ib_device.reg_user_mr = ehca_reg_user_mr;
349 shca->ib_device.query_mr = ehca_query_mr;
350 shca->ib_device.dereg_mr = ehca_dereg_mr;
351 shca->ib_device.rereg_phys_mr = ehca_rereg_phys_mr;
352 shca->ib_device.alloc_mw = ehca_alloc_mw;
353 shca->ib_device.bind_mw = ehca_bind_mw;
354 shca->ib_device.dealloc_mw = ehca_dealloc_mw;
355 shca->ib_device.alloc_fmr = ehca_alloc_fmr;
356 shca->ib_device.map_phys_fmr = ehca_map_phys_fmr;
357 shca->ib_device.unmap_fmr = ehca_unmap_fmr;
358 shca->ib_device.dealloc_fmr = ehca_dealloc_fmr;
359 shca->ib_device.attach_mcast = ehca_attach_mcast;
360 shca->ib_device.detach_mcast = ehca_detach_mcast;
361 /* shca->ib_device.process_mad = ehca_process_mad; */
362 shca->ib_device.mmap = ehca_mmap;
363
Heiko J Schickfab97222006-09-22 15:22:22 -0700364 return ret;
365}
366
367static int ehca_create_aqp1(struct ehca_shca *shca, u32 port)
368{
369 struct ehca_sport *sport = &shca->sport[port - 1];
370 struct ib_cq *ibcq;
371 struct ib_qp *ibqp;
372 struct ib_qp_init_attr qp_init_attr;
373 int ret;
374
375 if (sport->ibcq_aqp1) {
376 ehca_err(&shca->ib_device, "AQP1 CQ is already created.");
377 return -EPERM;
378 }
379
Michael S. Tsirkinf4fd0b22007-05-03 13:48:47 +0300380 ibcq = ib_create_cq(&shca->ib_device, NULL, NULL, (void*)(-1), 10, 0);
Heiko J Schickfab97222006-09-22 15:22:22 -0700381 if (IS_ERR(ibcq)) {
382 ehca_err(&shca->ib_device, "Cannot create AQP1 CQ.");
383 return PTR_ERR(ibcq);
384 }
385 sport->ibcq_aqp1 = ibcq;
386
387 if (sport->ibqp_aqp1) {
388 ehca_err(&shca->ib_device, "AQP1 QP is already created.");
389 ret = -EPERM;
390 goto create_aqp1;
391 }
392
393 memset(&qp_init_attr, 0, sizeof(struct ib_qp_init_attr));
394 qp_init_attr.send_cq = ibcq;
395 qp_init_attr.recv_cq = ibcq;
396 qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
397 qp_init_attr.cap.max_send_wr = 100;
398 qp_init_attr.cap.max_recv_wr = 100;
399 qp_init_attr.cap.max_send_sge = 2;
400 qp_init_attr.cap.max_recv_sge = 1;
401 qp_init_attr.qp_type = IB_QPT_GSI;
402 qp_init_attr.port_num = port;
403 qp_init_attr.qp_context = NULL;
404 qp_init_attr.event_handler = NULL;
405 qp_init_attr.srq = NULL;
406
407 ibqp = ib_create_qp(&shca->pd->ib_pd, &qp_init_attr);
408 if (IS_ERR(ibqp)) {
409 ehca_err(&shca->ib_device, "Cannot create AQP1 QP.");
410 ret = PTR_ERR(ibqp);
411 goto create_aqp1;
412 }
413 sport->ibqp_aqp1 = ibqp;
414
415 return 0;
416
417create_aqp1:
418 ib_destroy_cq(sport->ibcq_aqp1);
419 return ret;
420}
421
422static int ehca_destroy_aqp1(struct ehca_sport *sport)
423{
424 int ret;
425
426 ret = ib_destroy_qp(sport->ibqp_aqp1);
427 if (ret) {
428 ehca_gen_err("Cannot destroy AQP1 QP. ret=%x", ret);
429 return ret;
430 }
431
432 ret = ib_destroy_cq(sport->ibcq_aqp1);
433 if (ret)
434 ehca_gen_err("Cannot destroy AQP1 CQ. ret=%x", ret);
435
436 return ret;
437}
438
439static ssize_t ehca_show_debug_level(struct device_driver *ddp, char *buf)
440{
Hoang-Nam Nguyen78d8d5f2007-02-15 17:06:33 +0100441 return snprintf(buf, PAGE_SIZE, "%d\n",
442 ehca_debug_level);
Heiko J Schickfab97222006-09-22 15:22:22 -0700443}
444
445static ssize_t ehca_store_debug_level(struct device_driver *ddp,
446 const char *buf, size_t count)
447{
448 int value = (*buf) - '0';
449 if (value >= 0 && value <= 9)
450 ehca_debug_level = value;
451 return 1;
452}
453
454DRIVER_ATTR(debug_level, S_IRUSR | S_IWUSR,
455 ehca_show_debug_level, ehca_store_debug_level);
456
457void ehca_create_driver_sysfs(struct ibmebus_driver *drv)
458{
459 driver_create_file(&drv->driver, &driver_attr_debug_level);
460}
461
462void ehca_remove_driver_sysfs(struct ibmebus_driver *drv)
463{
464 driver_remove_file(&drv->driver, &driver_attr_debug_level);
465}
466
467#define EHCA_RESOURCE_ATTR(name) \
468static ssize_t ehca_show_##name(struct device *dev, \
469 struct device_attribute *attr, \
470 char *buf) \
471{ \
472 struct ehca_shca *shca; \
473 struct hipz_query_hca *rblock; \
474 int data; \
475 \
476 shca = dev->driver_data; \
477 \
Hoang-Nam Nguyenf2d91362007-01-09 18:04:14 +0100478 rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL); \
Heiko J Schickfab97222006-09-22 15:22:22 -0700479 if (!rblock) { \
480 dev_err(dev, "Can't allocate rblock memory."); \
481 return 0; \
482 } \
483 \
484 if (hipz_h_query_hca(shca->ipz_hca_handle, rblock) != H_SUCCESS) { \
485 dev_err(dev, "Can't query device properties"); \
Hoang-Nam Nguyen7e28db52006-11-07 00:56:39 +0100486 ehca_free_fw_ctrlblock(rblock); \
Heiko J Schickfab97222006-09-22 15:22:22 -0700487 return 0; \
488 } \
489 \
490 data = rblock->name; \
Hoang-Nam Nguyen7e28db52006-11-07 00:56:39 +0100491 ehca_free_fw_ctrlblock(rblock); \
Heiko J Schickfab97222006-09-22 15:22:22 -0700492 \
493 if ((strcmp(#name, "num_ports") == 0) && (ehca_nr_ports == 1)) \
494 return snprintf(buf, 256, "1\n"); \
495 else \
496 return snprintf(buf, 256, "%d\n", data); \
497 \
498} \
499static DEVICE_ATTR(name, S_IRUGO, ehca_show_##name, NULL);
500
501EHCA_RESOURCE_ATTR(num_ports);
502EHCA_RESOURCE_ATTR(hw_ver);
503EHCA_RESOURCE_ATTR(max_eq);
504EHCA_RESOURCE_ATTR(cur_eq);
505EHCA_RESOURCE_ATTR(max_cq);
506EHCA_RESOURCE_ATTR(cur_cq);
507EHCA_RESOURCE_ATTR(max_qp);
508EHCA_RESOURCE_ATTR(cur_qp);
509EHCA_RESOURCE_ATTR(max_mr);
510EHCA_RESOURCE_ATTR(cur_mr);
511EHCA_RESOURCE_ATTR(max_mw);
512EHCA_RESOURCE_ATTR(cur_mw);
513EHCA_RESOURCE_ATTR(max_pd);
514EHCA_RESOURCE_ATTR(max_ah);
515
516static ssize_t ehca_show_adapter_handle(struct device *dev,
517 struct device_attribute *attr,
518 char *buf)
519{
520 struct ehca_shca *shca = dev->driver_data;
521
522 return sprintf(buf, "%lx\n", shca->ipz_hca_handle.handle);
523
524}
525static DEVICE_ATTR(adapter_handle, S_IRUGO, ehca_show_adapter_handle, NULL);
526
527
528void ehca_create_device_sysfs(struct ibmebus_dev *dev)
529{
530 device_create_file(&dev->ofdev.dev, &dev_attr_adapter_handle);
531 device_create_file(&dev->ofdev.dev, &dev_attr_num_ports);
532 device_create_file(&dev->ofdev.dev, &dev_attr_hw_ver);
533 device_create_file(&dev->ofdev.dev, &dev_attr_max_eq);
534 device_create_file(&dev->ofdev.dev, &dev_attr_cur_eq);
535 device_create_file(&dev->ofdev.dev, &dev_attr_max_cq);
536 device_create_file(&dev->ofdev.dev, &dev_attr_cur_cq);
537 device_create_file(&dev->ofdev.dev, &dev_attr_max_qp);
538 device_create_file(&dev->ofdev.dev, &dev_attr_cur_qp);
539 device_create_file(&dev->ofdev.dev, &dev_attr_max_mr);
540 device_create_file(&dev->ofdev.dev, &dev_attr_cur_mr);
541 device_create_file(&dev->ofdev.dev, &dev_attr_max_mw);
542 device_create_file(&dev->ofdev.dev, &dev_attr_cur_mw);
543 device_create_file(&dev->ofdev.dev, &dev_attr_max_pd);
544 device_create_file(&dev->ofdev.dev, &dev_attr_max_ah);
545}
546
547void ehca_remove_device_sysfs(struct ibmebus_dev *dev)
548{
549 device_remove_file(&dev->ofdev.dev, &dev_attr_adapter_handle);
550 device_remove_file(&dev->ofdev.dev, &dev_attr_num_ports);
551 device_remove_file(&dev->ofdev.dev, &dev_attr_hw_ver);
552 device_remove_file(&dev->ofdev.dev, &dev_attr_max_eq);
553 device_remove_file(&dev->ofdev.dev, &dev_attr_cur_eq);
554 device_remove_file(&dev->ofdev.dev, &dev_attr_max_cq);
555 device_remove_file(&dev->ofdev.dev, &dev_attr_cur_cq);
556 device_remove_file(&dev->ofdev.dev, &dev_attr_max_qp);
557 device_remove_file(&dev->ofdev.dev, &dev_attr_cur_qp);
558 device_remove_file(&dev->ofdev.dev, &dev_attr_max_mr);
559 device_remove_file(&dev->ofdev.dev, &dev_attr_cur_mr);
560 device_remove_file(&dev->ofdev.dev, &dev_attr_max_mw);
561 device_remove_file(&dev->ofdev.dev, &dev_attr_cur_mw);
562 device_remove_file(&dev->ofdev.dev, &dev_attr_max_pd);
563 device_remove_file(&dev->ofdev.dev, &dev_attr_max_ah);
564}
565
566static int __devinit ehca_probe(struct ibmebus_dev *dev,
567 const struct of_device_id *id)
568{
569 struct ehca_shca *shca;
Stephen Rothwella7edd0e2007-04-03 10:52:17 +1000570 const u64 *handle;
Heiko J Schickfab97222006-09-22 15:22:22 -0700571 struct ib_pd *ibpd;
572 int ret;
573
Stephen Rothwell40cd3a42007-05-01 13:54:02 +1000574 handle = of_get_property(dev->ofdev.node, "ibm,hca-handle", NULL);
Heiko J Schickfab97222006-09-22 15:22:22 -0700575 if (!handle) {
576 ehca_gen_err("Cannot get eHCA handle for adapter: %s.",
577 dev->ofdev.node->full_name);
578 return -ENODEV;
579 }
580
581 if (!(*handle)) {
582 ehca_gen_err("Wrong eHCA handle for adapter: %s.",
583 dev->ofdev.node->full_name);
584 return -ENODEV;
585 }
586
587 shca = (struct ehca_shca *)ib_alloc_device(sizeof(*shca));
588 if (!shca) {
589 ehca_gen_err("Cannot allocate shca memory.");
590 return -ENOMEM;
591 }
Joachim Fenkesc4ed7902007-04-24 17:44:31 +0200592 mutex_init(&shca->modify_mutex);
Heiko J Schickfab97222006-09-22 15:22:22 -0700593
594 shca->ibmebus_dev = dev;
595 shca->ipz_hca_handle.handle = *handle;
596 dev->ofdev.dev.driver_data = shca;
597
598 ret = ehca_sense_attributes(shca);
599 if (ret < 0) {
600 ehca_gen_err("Cannot sense eHCA attributes.");
601 goto probe1;
602 }
603
Hoang-Nam Nguyen0f248d92006-10-02 14:52:17 -0700604 ret = ehca_init_device(shca);
Heiko J Schickfab97222006-09-22 15:22:22 -0700605 if (ret) {
Hoang-Nam Nguyen0f248d92006-10-02 14:52:17 -0700606 ehca_gen_err("Cannot init ehca device struct");
Heiko J Schickfab97222006-09-22 15:22:22 -0700607 goto probe1;
608 }
609
610 /* create event queues */
611 ret = ehca_create_eq(shca, &shca->eq, EHCA_EQ, 2048);
612 if (ret) {
613 ehca_err(&shca->ib_device, "Cannot create EQ.");
Hoang-Nam Nguyen0f248d92006-10-02 14:52:17 -0700614 goto probe1;
Heiko J Schickfab97222006-09-22 15:22:22 -0700615 }
616
617 ret = ehca_create_eq(shca, &shca->neq, EHCA_NEQ, 513);
618 if (ret) {
619 ehca_err(&shca->ib_device, "Cannot create NEQ.");
620 goto probe3;
621 }
622
623 /* create internal protection domain */
624 ibpd = ehca_alloc_pd(&shca->ib_device, (void*)(-1), NULL);
625 if (IS_ERR(ibpd)) {
626 ehca_err(&shca->ib_device, "Cannot create internal PD.");
627 ret = PTR_ERR(ibpd);
628 goto probe4;
629 }
630
631 shca->pd = container_of(ibpd, struct ehca_pd, ib_pd);
632 shca->pd->ib_pd.device = &shca->ib_device;
633
634 /* create internal max MR */
635 ret = ehca_reg_internal_maxmr(shca, shca->pd, &shca->maxmr);
636
637 if (ret) {
638 ehca_err(&shca->ib_device, "Cannot create internal MR ret=%x",
639 ret);
640 goto probe5;
641 }
642
Hoang-Nam Nguyen0f248d92006-10-02 14:52:17 -0700643 ret = ib_register_device(&shca->ib_device);
644 if (ret) {
645 ehca_err(&shca->ib_device,
646 "ib_register_device() failed ret=%x", ret);
647 goto probe6;
648 }
649
Heiko J Schickfab97222006-09-22 15:22:22 -0700650 /* create AQP1 for port 1 */
651 if (ehca_open_aqp1 == 1) {
652 shca->sport[0].port_state = IB_PORT_DOWN;
653 ret = ehca_create_aqp1(shca, 1);
654 if (ret) {
655 ehca_err(&shca->ib_device,
656 "Cannot create AQP1 for port 1.");
Hoang-Nam Nguyen0f248d92006-10-02 14:52:17 -0700657 goto probe7;
Heiko J Schickfab97222006-09-22 15:22:22 -0700658 }
659 }
660
661 /* create AQP1 for port 2 */
662 if ((ehca_open_aqp1 == 1) && (shca->num_ports == 2)) {
663 shca->sport[1].port_state = IB_PORT_DOWN;
664 ret = ehca_create_aqp1(shca, 2);
665 if (ret) {
666 ehca_err(&shca->ib_device,
667 "Cannot create AQP1 for port 2.");
Hoang-Nam Nguyen0f248d92006-10-02 14:52:17 -0700668 goto probe8;
Heiko J Schickfab97222006-09-22 15:22:22 -0700669 }
670 }
671
672 ehca_create_device_sysfs(dev);
673
674 spin_lock(&shca_list_lock);
675 list_add(&shca->shca_list, &shca_list);
676 spin_unlock(&shca_list_lock);
677
678 return 0;
679
Hoang-Nam Nguyen0f248d92006-10-02 14:52:17 -0700680probe8:
Heiko J Schickfab97222006-09-22 15:22:22 -0700681 ret = ehca_destroy_aqp1(&shca->sport[0]);
682 if (ret)
683 ehca_err(&shca->ib_device,
684 "Cannot destroy AQP1 for port 1. ret=%x", ret);
685
Hoang-Nam Nguyen0f248d92006-10-02 14:52:17 -0700686probe7:
687 ib_unregister_device(&shca->ib_device);
688
Heiko J Schickfab97222006-09-22 15:22:22 -0700689probe6:
690 ret = ehca_dereg_internal_maxmr(shca);
691 if (ret)
692 ehca_err(&shca->ib_device,
693 "Cannot destroy internal MR. ret=%x", ret);
694
695probe5:
696 ret = ehca_dealloc_pd(&shca->pd->ib_pd);
697 if (ret)
698 ehca_err(&shca->ib_device,
699 "Cannot destroy internal PD. ret=%x", ret);
700
701probe4:
702 ret = ehca_destroy_eq(shca, &shca->neq);
703 if (ret)
704 ehca_err(&shca->ib_device,
705 "Cannot destroy NEQ. ret=%x", ret);
706
707probe3:
708 ret = ehca_destroy_eq(shca, &shca->eq);
709 if (ret)
710 ehca_err(&shca->ib_device,
711 "Cannot destroy EQ. ret=%x", ret);
712
Heiko J Schickfab97222006-09-22 15:22:22 -0700713probe1:
714 ib_dealloc_device(&shca->ib_device);
715
716 return -EINVAL;
717}
718
719static int __devexit ehca_remove(struct ibmebus_dev *dev)
720{
721 struct ehca_shca *shca = dev->ofdev.dev.driver_data;
722 int ret;
723
724 ehca_remove_device_sysfs(dev);
725
726 if (ehca_open_aqp1 == 1) {
727 int i;
728 for (i = 0; i < shca->num_ports; i++) {
729 ret = ehca_destroy_aqp1(&shca->sport[i]);
730 if (ret)
731 ehca_err(&shca->ib_device,
732 "Cannot destroy AQP1 for port %x "
733 "ret=%x", ret, i);
734 }
735 }
736
737 ib_unregister_device(&shca->ib_device);
738
739 ret = ehca_dereg_internal_maxmr(shca);
740 if (ret)
741 ehca_err(&shca->ib_device,
742 "Cannot destroy internal MR. ret=%x", ret);
743
744 ret = ehca_dealloc_pd(&shca->pd->ib_pd);
745 if (ret)
746 ehca_err(&shca->ib_device,
747 "Cannot destroy internal PD. ret=%x", ret);
748
749 ret = ehca_destroy_eq(shca, &shca->eq);
750 if (ret)
751 ehca_err(&shca->ib_device, "Cannot destroy EQ. ret=%x", ret);
752
753 ret = ehca_destroy_eq(shca, &shca->neq);
754 if (ret)
755 ehca_err(&shca->ib_device, "Canot destroy NEQ. ret=%x", ret);
756
757 ib_dealloc_device(&shca->ib_device);
758
759 spin_lock(&shca_list_lock);
760 list_del(&shca->shca_list);
761 spin_unlock(&shca_list_lock);
762
763 return ret;
764}
765
766static struct of_device_id ehca_device_table[] =
767{
768 {
769 .name = "lhca",
770 .compatible = "IBM,lhca",
771 },
772 {},
773};
774
775static struct ibmebus_driver ehca_driver = {
776 .name = "ehca",
777 .id_table = ehca_device_table,
778 .probe = ehca_probe,
779 .remove = ehca_remove,
780};
781
782void ehca_poll_eqs(unsigned long data)
783{
784 struct ehca_shca *shca;
785
786 spin_lock(&shca_list_lock);
787 list_for_each_entry(shca, &shca_list, shca_list) {
Hoang-Nam Nguyen78d8d5f2007-02-15 17:06:33 +0100788 if (shca->eq.is_initialized) {
789 /* call deadman proc only if eq ptr does not change */
790 struct ehca_eq *eq = &shca->eq;
791 int max = 3;
792 volatile u64 q_ofs, q_ofs2;
793 u64 flags;
794 spin_lock_irqsave(&eq->spinlock, flags);
795 q_ofs = eq->ipz_queue.current_q_offset;
796 spin_unlock_irqrestore(&eq->spinlock, flags);
797 do {
798 spin_lock_irqsave(&eq->spinlock, flags);
799 q_ofs2 = eq->ipz_queue.current_q_offset;
800 spin_unlock_irqrestore(&eq->spinlock, flags);
801 max--;
802 } while (q_ofs == q_ofs2 && max > 0);
803 if (q_ofs == q_ofs2)
804 ehca_process_eq(shca, 0);
805 }
Heiko J Schickfab97222006-09-22 15:22:22 -0700806 }
807 mod_timer(&poll_eqs_timer, jiffies + HZ);
808 spin_unlock(&shca_list_lock);
809}
810
811int __init ehca_module_init(void)
812{
813 int ret;
814
815 printk(KERN_INFO "eHCA Infiniband Device Driver "
Hoang-Nam Nguyen31726792007-02-28 18:01:02 +0100816 "(Rel.: SVNEHCA_0022)\n");
Heiko J Schickfab97222006-09-22 15:22:22 -0700817 idr_init(&ehca_qp_idr);
818 idr_init(&ehca_cq_idr);
819 spin_lock_init(&ehca_qp_idr_lock);
820 spin_lock_init(&ehca_cq_idr_lock);
Stefan Roscher5d882782007-05-09 13:47:56 +0200821 spin_lock_init(&hcall_lock);
Heiko J Schickfab97222006-09-22 15:22:22 -0700822
823 INIT_LIST_HEAD(&shca_list);
824 spin_lock_init(&shca_list_lock);
825
826 if ((ret = ehca_create_comp_pool())) {
827 ehca_gen_err("Cannot create comp pool.");
828 return ret;
829 }
830
831 if ((ret = ehca_create_slab_caches())) {
832 ehca_gen_err("Cannot create SLAB caches");
833 ret = -ENOMEM;
834 goto module_init1;
835 }
836
837 if ((ret = ibmebus_register_driver(&ehca_driver))) {
838 ehca_gen_err("Cannot register eHCA device driver");
839 ret = -EINVAL;
840 goto module_init2;
841 }
842
843 ehca_create_driver_sysfs(&ehca_driver);
844
845 if (ehca_poll_all_eqs != 1) {
846 ehca_gen_err("WARNING!!!");
847 ehca_gen_err("It is possible to lose interrupts.");
848 } else {
849 init_timer(&poll_eqs_timer);
850 poll_eqs_timer.function = ehca_poll_eqs;
851 poll_eqs_timer.expires = jiffies + HZ;
852 add_timer(&poll_eqs_timer);
853 }
854
855 return 0;
856
857module_init2:
858 ehca_destroy_slab_caches();
859
860module_init1:
861 ehca_destroy_comp_pool();
862 return ret;
863};
864
865void __exit ehca_module_exit(void)
866{
867 if (ehca_poll_all_eqs == 1)
868 del_timer_sync(&poll_eqs_timer);
869
870 ehca_remove_driver_sysfs(&ehca_driver);
871 ibmebus_unregister_driver(&ehca_driver);
872
873 ehca_destroy_slab_caches();
874
875 ehca_destroy_comp_pool();
876
877 idr_destroy(&ehca_cq_idr);
878 idr_destroy(&ehca_qp_idr);
879};
880
881module_init(ehca_module_init);
882module_exit(ehca_module_exit);