RDMA: iWARP Core Changes.
[linux-2.6] / drivers / infiniband / hw / ehca / ehca_main.c
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
43 #include "ehca_classes.h"
44 #include "ehca_iverbs.h"
45 #include "ehca_mrmw.h"
46 #include "ehca_tools.h"
47 #include "hcp_if.h"
48
49 MODULE_LICENSE("Dual BSD/GPL");
50 MODULE_AUTHOR("Christoph Raisch <raisch@de.ibm.com>");
51 MODULE_DESCRIPTION("IBM eServer HCA InfiniBand Device Driver");
52 MODULE_VERSION("SVNEHCA_0016");
53
54 int ehca_open_aqp1     = 0;
55 int ehca_debug_level   = 0;
56 int ehca_hw_level      = 0;
57 int ehca_nr_ports      = 2;
58 int ehca_use_hp_mr     = 0;
59 int ehca_port_act_time = 30;
60 int ehca_poll_all_eqs  = 1;
61 int ehca_static_rate   = -1;
62
63 module_param_named(open_aqp1,     ehca_open_aqp1,     int, 0);
64 module_param_named(debug_level,   ehca_debug_level,   int, 0);
65 module_param_named(hw_level,      ehca_hw_level,      int, 0);
66 module_param_named(nr_ports,      ehca_nr_ports,      int, 0);
67 module_param_named(use_hp_mr,     ehca_use_hp_mr,     int, 0);
68 module_param_named(port_act_time, ehca_port_act_time, int, 0);
69 module_param_named(poll_all_eqs,  ehca_poll_all_eqs,  int, 0);
70 module_param_named(static_rate,   ehca_static_rate,   int, 0);
71
72 MODULE_PARM_DESC(open_aqp1,
73                  "AQP1 on startup (0: no (default), 1: yes)");
74 MODULE_PARM_DESC(debug_level,
75                  "debug level"
76                  " (0: no debug traces (default), 1: with debug traces)");
77 MODULE_PARM_DESC(hw_level,
78                  "hardware level"
79                  " (0: autosensing (default), 1: v. 0.20, 2: v. 0.21)");
80 MODULE_PARM_DESC(nr_ports,
81                  "number of connected ports (default: 2)");
82 MODULE_PARM_DESC(use_hp_mr,
83                  "high performance MRs (0: no (default), 1: yes)");
84 MODULE_PARM_DESC(port_act_time,
85                  "time to wait for port activation (default: 30 sec)");
86 MODULE_PARM_DESC(poll_all_eqs,
87                  "polls all event queues periodically"
88                  " (0: no, 1: yes (default))");
89 MODULE_PARM_DESC(static_rate,
90                  "set permanent static rate (default: disabled)");
91
92 spinlock_t ehca_qp_idr_lock;
93 spinlock_t ehca_cq_idr_lock;
94 DEFINE_IDR(ehca_qp_idr);
95 DEFINE_IDR(ehca_cq_idr);
96
97 static struct list_head shca_list; /* list of all registered ehcas */
98 static spinlock_t shca_list_lock;
99
100 static struct timer_list poll_eqs_timer;
101
102 static int ehca_create_slab_caches(void)
103 {
104         int ret;
105
106         ret = ehca_init_pd_cache();
107         if (ret) {
108                 ehca_gen_err("Cannot create PD SLAB cache.");
109                 return ret;
110         }
111
112         ret = ehca_init_cq_cache();
113         if (ret) {
114                 ehca_gen_err("Cannot create CQ SLAB cache.");
115                 goto create_slab_caches2;
116         }
117
118         ret = ehca_init_qp_cache();
119         if (ret) {
120                 ehca_gen_err("Cannot create QP SLAB cache.");
121                 goto create_slab_caches3;
122         }
123
124         ret = ehca_init_av_cache();
125         if (ret) {
126                 ehca_gen_err("Cannot create AV SLAB cache.");
127                 goto create_slab_caches4;
128         }
129
130         ret = ehca_init_mrmw_cache();
131         if (ret) {
132                 ehca_gen_err("Cannot create MR&MW SLAB cache.");
133                 goto create_slab_caches5;
134         }
135
136         return 0;
137
138 create_slab_caches5:
139         ehca_cleanup_av_cache();
140
141 create_slab_caches4:
142         ehca_cleanup_qp_cache();
143
144 create_slab_caches3:
145         ehca_cleanup_cq_cache();
146
147 create_slab_caches2:
148         ehca_cleanup_pd_cache();
149
150         return ret;
151 }
152
153 static void ehca_destroy_slab_caches(void)
154 {
155         ehca_cleanup_mrmw_cache();
156         ehca_cleanup_av_cache();
157         ehca_cleanup_qp_cache();
158         ehca_cleanup_cq_cache();
159         ehca_cleanup_pd_cache();
160 }
161
162 #define EHCA_HCAAVER  EHCA_BMASK_IBM(32,39)
163 #define EHCA_REVID    EHCA_BMASK_IBM(40,63)
164
165 int ehca_sense_attributes(struct ehca_shca *shca)
166 {
167         int ret = 0;
168         u64 h_ret;
169         struct hipz_query_hca *rblock;
170
171         rblock = kzalloc(H_CB_ALIGNMENT, GFP_KERNEL);
172         if (!rblock) {
173                 ehca_gen_err("Cannot allocate rblock memory.");
174                 return -ENOMEM;
175         }
176
177         h_ret = hipz_h_query_hca(shca->ipz_hca_handle, rblock);
178         if (h_ret != H_SUCCESS) {
179                 ehca_gen_err("Cannot query device properties. h_ret=%lx",
180                              h_ret);
181                 ret = -EPERM;
182                 goto num_ports1;
183         }
184
185         if (ehca_nr_ports == 1)
186                 shca->num_ports = 1;
187         else
188                 shca->num_ports = (u8)rblock->num_ports;
189
190         ehca_gen_dbg(" ... found %x ports", rblock->num_ports);
191
192         if (ehca_hw_level == 0) {
193                 u32 hcaaver;
194                 u32 revid;
195
196                 hcaaver = EHCA_BMASK_GET(EHCA_HCAAVER, rblock->hw_ver);
197                 revid   = EHCA_BMASK_GET(EHCA_REVID, rblock->hw_ver);
198
199                 ehca_gen_dbg(" ... hardware version=%x:%x", hcaaver, revid);
200
201                 if ((hcaaver == 1) && (revid == 0))
202                         shca->hw_level = 0;
203                 else if ((hcaaver == 1) && (revid == 1))
204                         shca->hw_level = 1;
205                 else if ((hcaaver == 1) && (revid == 2))
206                         shca->hw_level = 2;
207         }
208         ehca_gen_dbg(" ... hardware level=%x", shca->hw_level);
209
210         shca->sport[0].rate = IB_RATE_30_GBPS;
211         shca->sport[1].rate = IB_RATE_30_GBPS;
212
213 num_ports1:
214         kfree(rblock);
215         return ret;
216 }
217
218 static int init_node_guid(struct ehca_shca *shca)
219 {
220         int ret = 0;
221         struct hipz_query_hca *rblock;
222
223         rblock = kzalloc(H_CB_ALIGNMENT, GFP_KERNEL);
224         if (!rblock) {
225                 ehca_err(&shca->ib_device, "Can't allocate rblock memory.");
226                 return -ENOMEM;
227         }
228
229         if (hipz_h_query_hca(shca->ipz_hca_handle, rblock) != H_SUCCESS) {
230                 ehca_err(&shca->ib_device, "Can't query device properties");
231                 ret = -EINVAL;
232                 goto init_node_guid1;
233         }
234
235         memcpy(&shca->ib_device.node_guid, &rblock->node_guid, sizeof(u64));
236
237 init_node_guid1:
238         kfree(rblock);
239         return ret;
240 }
241
242 int ehca_register_device(struct ehca_shca *shca)
243 {
244         int ret;
245
246         ret = init_node_guid(shca);
247         if (ret)
248                 return ret;
249
250         strlcpy(shca->ib_device.name, "ehca%d", IB_DEVICE_NAME_MAX);
251         shca->ib_device.owner               = THIS_MODULE;
252
253         shca->ib_device.uverbs_abi_ver      = 5;
254         shca->ib_device.uverbs_cmd_mask     =
255                 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT)         |
256                 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)        |
257                 (1ull << IB_USER_VERBS_CMD_QUERY_PORT)          |
258                 (1ull << IB_USER_VERBS_CMD_ALLOC_PD)            |
259                 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD)          |
260                 (1ull << IB_USER_VERBS_CMD_REG_MR)              |
261                 (1ull << IB_USER_VERBS_CMD_DEREG_MR)            |
262                 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
263                 (1ull << IB_USER_VERBS_CMD_CREATE_CQ)           |
264                 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ)          |
265                 (1ull << IB_USER_VERBS_CMD_CREATE_QP)           |
266                 (1ull << IB_USER_VERBS_CMD_MODIFY_QP)           |
267                 (1ull << IB_USER_VERBS_CMD_QUERY_QP)            |
268                 (1ull << IB_USER_VERBS_CMD_DESTROY_QP)          |
269                 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)        |
270                 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST);
271
272         shca->ib_device.node_type           = RDMA_NODE_IB_CA;
273         shca->ib_device.phys_port_cnt       = shca->num_ports;
274         shca->ib_device.dma_device          = &shca->ibmebus_dev->ofdev.dev;
275         shca->ib_device.query_device        = ehca_query_device;
276         shca->ib_device.query_port          = ehca_query_port;
277         shca->ib_device.query_gid           = ehca_query_gid;
278         shca->ib_device.query_pkey          = ehca_query_pkey;
279         /* shca->in_device.modify_device    = ehca_modify_device    */
280         shca->ib_device.modify_port         = ehca_modify_port;
281         shca->ib_device.alloc_ucontext      = ehca_alloc_ucontext;
282         shca->ib_device.dealloc_ucontext    = ehca_dealloc_ucontext;
283         shca->ib_device.alloc_pd            = ehca_alloc_pd;
284         shca->ib_device.dealloc_pd          = ehca_dealloc_pd;
285         shca->ib_device.create_ah           = ehca_create_ah;
286         /* shca->ib_device.modify_ah        = ehca_modify_ah;       */
287         shca->ib_device.query_ah            = ehca_query_ah;
288         shca->ib_device.destroy_ah          = ehca_destroy_ah;
289         shca->ib_device.create_qp           = ehca_create_qp;
290         shca->ib_device.modify_qp           = ehca_modify_qp;
291         shca->ib_device.query_qp            = ehca_query_qp;
292         shca->ib_device.destroy_qp          = ehca_destroy_qp;
293         shca->ib_device.post_send           = ehca_post_send;
294         shca->ib_device.post_recv           = ehca_post_recv;
295         shca->ib_device.create_cq           = ehca_create_cq;
296         shca->ib_device.destroy_cq          = ehca_destroy_cq;
297         shca->ib_device.resize_cq           = ehca_resize_cq;
298         shca->ib_device.poll_cq             = ehca_poll_cq;
299         /* shca->ib_device.peek_cq          = ehca_peek_cq;         */
300         shca->ib_device.req_notify_cq       = ehca_req_notify_cq;
301         /* shca->ib_device.req_ncomp_notif  = ehca_req_ncomp_notif; */
302         shca->ib_device.get_dma_mr          = ehca_get_dma_mr;
303         shca->ib_device.reg_phys_mr         = ehca_reg_phys_mr;
304         shca->ib_device.reg_user_mr         = ehca_reg_user_mr;
305         shca->ib_device.query_mr            = ehca_query_mr;
306         shca->ib_device.dereg_mr            = ehca_dereg_mr;
307         shca->ib_device.rereg_phys_mr       = ehca_rereg_phys_mr;
308         shca->ib_device.alloc_mw            = ehca_alloc_mw;
309         shca->ib_device.bind_mw             = ehca_bind_mw;
310         shca->ib_device.dealloc_mw          = ehca_dealloc_mw;
311         shca->ib_device.alloc_fmr           = ehca_alloc_fmr;
312         shca->ib_device.map_phys_fmr        = ehca_map_phys_fmr;
313         shca->ib_device.unmap_fmr           = ehca_unmap_fmr;
314         shca->ib_device.dealloc_fmr         = ehca_dealloc_fmr;
315         shca->ib_device.attach_mcast        = ehca_attach_mcast;
316         shca->ib_device.detach_mcast        = ehca_detach_mcast;
317         /* shca->ib_device.process_mad      = ehca_process_mad;     */
318         shca->ib_device.mmap                = ehca_mmap;
319
320         ret = ib_register_device(&shca->ib_device);
321         if (ret)
322                 ehca_err(&shca->ib_device,
323                          "ib_register_device() failed ret=%x", ret);
324
325         return ret;
326 }
327
328 static int ehca_create_aqp1(struct ehca_shca *shca, u32 port)
329 {
330         struct ehca_sport *sport = &shca->sport[port - 1];
331         struct ib_cq *ibcq;
332         struct ib_qp *ibqp;
333         struct ib_qp_init_attr qp_init_attr;
334         int ret;
335
336         if (sport->ibcq_aqp1) {
337                 ehca_err(&shca->ib_device, "AQP1 CQ is already created.");
338                 return -EPERM;
339         }
340
341         ibcq = ib_create_cq(&shca->ib_device, NULL, NULL, (void*)(-1), 10);
342         if (IS_ERR(ibcq)) {
343                 ehca_err(&shca->ib_device, "Cannot create AQP1 CQ.");
344                 return PTR_ERR(ibcq);
345         }
346         sport->ibcq_aqp1 = ibcq;
347
348         if (sport->ibqp_aqp1) {
349                 ehca_err(&shca->ib_device, "AQP1 QP is already created.");
350                 ret = -EPERM;
351                 goto create_aqp1;
352         }
353
354         memset(&qp_init_attr, 0, sizeof(struct ib_qp_init_attr));
355         qp_init_attr.send_cq          = ibcq;
356         qp_init_attr.recv_cq          = ibcq;
357         qp_init_attr.sq_sig_type      = IB_SIGNAL_ALL_WR;
358         qp_init_attr.cap.max_send_wr  = 100;
359         qp_init_attr.cap.max_recv_wr  = 100;
360         qp_init_attr.cap.max_send_sge = 2;
361         qp_init_attr.cap.max_recv_sge = 1;
362         qp_init_attr.qp_type          = IB_QPT_GSI;
363         qp_init_attr.port_num         = port;
364         qp_init_attr.qp_context       = NULL;
365         qp_init_attr.event_handler    = NULL;
366         qp_init_attr.srq              = NULL;
367
368         ibqp = ib_create_qp(&shca->pd->ib_pd, &qp_init_attr);
369         if (IS_ERR(ibqp)) {
370                 ehca_err(&shca->ib_device, "Cannot create AQP1 QP.");
371                 ret = PTR_ERR(ibqp);
372                 goto create_aqp1;
373         }
374         sport->ibqp_aqp1 = ibqp;
375
376         return 0;
377
378 create_aqp1:
379         ib_destroy_cq(sport->ibcq_aqp1);
380         return ret;
381 }
382
383 static int ehca_destroy_aqp1(struct ehca_sport *sport)
384 {
385         int ret;
386
387         ret = ib_destroy_qp(sport->ibqp_aqp1);
388         if (ret) {
389                 ehca_gen_err("Cannot destroy AQP1 QP. ret=%x", ret);
390                 return ret;
391         }
392
393         ret = ib_destroy_cq(sport->ibcq_aqp1);
394         if (ret)
395                 ehca_gen_err("Cannot destroy AQP1 CQ. ret=%x", ret);
396
397         return ret;
398 }
399
400 static ssize_t ehca_show_debug_level(struct device_driver *ddp, char *buf)
401 {
402         return  snprintf(buf, PAGE_SIZE, "%d\n",
403                          ehca_debug_level);
404 }
405
406 static ssize_t ehca_store_debug_level(struct device_driver *ddp,
407                                       const char *buf, size_t count)
408 {
409         int value = (*buf) - '0';
410         if (value >= 0 && value <= 9)
411                 ehca_debug_level = value;
412         return 1;
413 }
414
415 DRIVER_ATTR(debug_level, S_IRUSR | S_IWUSR,
416             ehca_show_debug_level, ehca_store_debug_level);
417
418 void ehca_create_driver_sysfs(struct ibmebus_driver *drv)
419 {
420         driver_create_file(&drv->driver, &driver_attr_debug_level);
421 }
422
423 void ehca_remove_driver_sysfs(struct ibmebus_driver *drv)
424 {
425         driver_remove_file(&drv->driver, &driver_attr_debug_level);
426 }
427
428 #define EHCA_RESOURCE_ATTR(name)                                           \
429 static ssize_t  ehca_show_##name(struct device *dev,                       \
430                                  struct device_attribute *attr,            \
431                                  char *buf)                                \
432 {                                                                          \
433         struct ehca_shca *shca;                                            \
434         struct hipz_query_hca *rblock;                                     \
435         int data;                                                          \
436                                                                            \
437         shca = dev->driver_data;                                           \
438                                                                            \
439         rblock = kzalloc(H_CB_ALIGNMENT, GFP_KERNEL);                      \
440         if (!rblock) {                                                     \
441                 dev_err(dev, "Can't allocate rblock memory.");             \
442                 return 0;                                                  \
443         }                                                                  \
444                                                                            \
445         if (hipz_h_query_hca(shca->ipz_hca_handle, rblock) != H_SUCCESS) { \
446                 dev_err(dev, "Can't query device properties");             \
447                 kfree(rblock);                                             \
448                 return 0;                                                  \
449         }                                                                  \
450                                                                            \
451         data = rblock->name;                                               \
452         kfree(rblock);                                                     \
453                                                                            \
454         if ((strcmp(#name, "num_ports") == 0) && (ehca_nr_ports == 1))     \
455                 return snprintf(buf, 256, "1\n");                          \
456         else                                                               \
457                 return snprintf(buf, 256, "%d\n", data);                   \
458                                                                            \
459 }                                                                          \
460 static DEVICE_ATTR(name, S_IRUGO, ehca_show_##name, NULL);
461
462 EHCA_RESOURCE_ATTR(num_ports);
463 EHCA_RESOURCE_ATTR(hw_ver);
464 EHCA_RESOURCE_ATTR(max_eq);
465 EHCA_RESOURCE_ATTR(cur_eq);
466 EHCA_RESOURCE_ATTR(max_cq);
467 EHCA_RESOURCE_ATTR(cur_cq);
468 EHCA_RESOURCE_ATTR(max_qp);
469 EHCA_RESOURCE_ATTR(cur_qp);
470 EHCA_RESOURCE_ATTR(max_mr);
471 EHCA_RESOURCE_ATTR(cur_mr);
472 EHCA_RESOURCE_ATTR(max_mw);
473 EHCA_RESOURCE_ATTR(cur_mw);
474 EHCA_RESOURCE_ATTR(max_pd);
475 EHCA_RESOURCE_ATTR(max_ah);
476
477 static ssize_t ehca_show_adapter_handle(struct device *dev,
478                                         struct device_attribute *attr,
479                                         char *buf)
480 {
481         struct ehca_shca *shca = dev->driver_data;
482
483         return sprintf(buf, "%lx\n", shca->ipz_hca_handle.handle);
484
485 }
486 static DEVICE_ATTR(adapter_handle, S_IRUGO, ehca_show_adapter_handle, NULL);
487
488
489 void ehca_create_device_sysfs(struct ibmebus_dev *dev)
490 {
491         device_create_file(&dev->ofdev.dev, &dev_attr_adapter_handle);
492         device_create_file(&dev->ofdev.dev, &dev_attr_num_ports);
493         device_create_file(&dev->ofdev.dev, &dev_attr_hw_ver);
494         device_create_file(&dev->ofdev.dev, &dev_attr_max_eq);
495         device_create_file(&dev->ofdev.dev, &dev_attr_cur_eq);
496         device_create_file(&dev->ofdev.dev, &dev_attr_max_cq);
497         device_create_file(&dev->ofdev.dev, &dev_attr_cur_cq);
498         device_create_file(&dev->ofdev.dev, &dev_attr_max_qp);
499         device_create_file(&dev->ofdev.dev, &dev_attr_cur_qp);
500         device_create_file(&dev->ofdev.dev, &dev_attr_max_mr);
501         device_create_file(&dev->ofdev.dev, &dev_attr_cur_mr);
502         device_create_file(&dev->ofdev.dev, &dev_attr_max_mw);
503         device_create_file(&dev->ofdev.dev, &dev_attr_cur_mw);
504         device_create_file(&dev->ofdev.dev, &dev_attr_max_pd);
505         device_create_file(&dev->ofdev.dev, &dev_attr_max_ah);
506 }
507
508 void ehca_remove_device_sysfs(struct ibmebus_dev *dev)
509 {
510         device_remove_file(&dev->ofdev.dev, &dev_attr_adapter_handle);
511         device_remove_file(&dev->ofdev.dev, &dev_attr_num_ports);
512         device_remove_file(&dev->ofdev.dev, &dev_attr_hw_ver);
513         device_remove_file(&dev->ofdev.dev, &dev_attr_max_eq);
514         device_remove_file(&dev->ofdev.dev, &dev_attr_cur_eq);
515         device_remove_file(&dev->ofdev.dev, &dev_attr_max_cq);
516         device_remove_file(&dev->ofdev.dev, &dev_attr_cur_cq);
517         device_remove_file(&dev->ofdev.dev, &dev_attr_max_qp);
518         device_remove_file(&dev->ofdev.dev, &dev_attr_cur_qp);
519         device_remove_file(&dev->ofdev.dev, &dev_attr_max_mr);
520         device_remove_file(&dev->ofdev.dev, &dev_attr_cur_mr);
521         device_remove_file(&dev->ofdev.dev, &dev_attr_max_mw);
522         device_remove_file(&dev->ofdev.dev, &dev_attr_cur_mw);
523         device_remove_file(&dev->ofdev.dev, &dev_attr_max_pd);
524         device_remove_file(&dev->ofdev.dev, &dev_attr_max_ah);
525 }
526
527 static int __devinit ehca_probe(struct ibmebus_dev *dev,
528                                 const struct of_device_id *id)
529 {
530         struct ehca_shca *shca;
531         u64 *handle;
532         struct ib_pd *ibpd;
533         int ret;
534
535         handle = (u64 *)get_property(dev->ofdev.node, "ibm,hca-handle", NULL);
536         if (!handle) {
537                 ehca_gen_err("Cannot get eHCA handle for adapter: %s.",
538                              dev->ofdev.node->full_name);
539                 return -ENODEV;
540         }
541
542         if (!(*handle)) {
543                 ehca_gen_err("Wrong eHCA handle for adapter: %s.",
544                              dev->ofdev.node->full_name);
545                 return -ENODEV;
546         }
547
548         shca = (struct ehca_shca *)ib_alloc_device(sizeof(*shca));
549         if (!shca) {
550                 ehca_gen_err("Cannot allocate shca memory.");
551                 return -ENOMEM;
552         }
553
554         shca->ibmebus_dev = dev;
555         shca->ipz_hca_handle.handle = *handle;
556         dev->ofdev.dev.driver_data = shca;
557
558         ret = ehca_sense_attributes(shca);
559         if (ret < 0) {
560                 ehca_gen_err("Cannot sense eHCA attributes.");
561                 goto probe1;
562         }
563
564         ret = ehca_register_device(shca);
565         if (ret) {
566                 ehca_gen_err("Cannot register Infiniband device");
567                 goto probe1;
568         }
569
570         /* create event queues */
571         ret = ehca_create_eq(shca, &shca->eq, EHCA_EQ, 2048);
572         if (ret) {
573                 ehca_err(&shca->ib_device, "Cannot create EQ.");
574                 goto probe2;
575         }
576
577         ret = ehca_create_eq(shca, &shca->neq, EHCA_NEQ, 513);
578         if (ret) {
579                 ehca_err(&shca->ib_device, "Cannot create NEQ.");
580                 goto probe3;
581         }
582
583         /* create internal protection domain */
584         ibpd = ehca_alloc_pd(&shca->ib_device, (void*)(-1), NULL);
585         if (IS_ERR(ibpd)) {
586                 ehca_err(&shca->ib_device, "Cannot create internal PD.");
587                 ret = PTR_ERR(ibpd);
588                 goto probe4;
589         }
590
591         shca->pd = container_of(ibpd, struct ehca_pd, ib_pd);
592         shca->pd->ib_pd.device = &shca->ib_device;
593
594         /* create internal max MR */
595         ret = ehca_reg_internal_maxmr(shca, shca->pd, &shca->maxmr);
596
597         if (ret) {
598                 ehca_err(&shca->ib_device, "Cannot create internal MR ret=%x",
599                          ret);
600                 goto probe5;
601         }
602
603         /* create AQP1 for port 1 */
604         if (ehca_open_aqp1 == 1) {
605                 shca->sport[0].port_state = IB_PORT_DOWN;
606                 ret = ehca_create_aqp1(shca, 1);
607                 if (ret) {
608                         ehca_err(&shca->ib_device,
609                                  "Cannot create AQP1 for port 1.");
610                         goto probe6;
611                 }
612         }
613
614         /* create AQP1 for port 2 */
615         if ((ehca_open_aqp1 == 1) && (shca->num_ports == 2)) {
616                 shca->sport[1].port_state = IB_PORT_DOWN;
617                 ret = ehca_create_aqp1(shca, 2);
618                 if (ret) {
619                         ehca_err(&shca->ib_device,
620                                  "Cannot create AQP1 for port 2.");
621                         goto probe7;
622                 }
623         }
624
625         ehca_create_device_sysfs(dev);
626
627         spin_lock(&shca_list_lock);
628         list_add(&shca->shca_list, &shca_list);
629         spin_unlock(&shca_list_lock);
630
631         return 0;
632
633 probe7:
634         ret = ehca_destroy_aqp1(&shca->sport[0]);
635         if (ret)
636                 ehca_err(&shca->ib_device,
637                          "Cannot destroy AQP1 for port 1. ret=%x", ret);
638
639 probe6:
640         ret = ehca_dereg_internal_maxmr(shca);
641         if (ret)
642                 ehca_err(&shca->ib_device,
643                          "Cannot destroy internal MR. ret=%x", ret);
644
645 probe5:
646         ret = ehca_dealloc_pd(&shca->pd->ib_pd);
647         if (ret)
648                 ehca_err(&shca->ib_device,
649                          "Cannot destroy internal PD. ret=%x", ret);
650
651 probe4:
652         ret = ehca_destroy_eq(shca, &shca->neq);
653         if (ret)
654                 ehca_err(&shca->ib_device,
655                          "Cannot destroy NEQ. ret=%x", ret);
656
657 probe3:
658         ret = ehca_destroy_eq(shca, &shca->eq);
659         if (ret)
660                 ehca_err(&shca->ib_device,
661                          "Cannot destroy EQ. ret=%x", ret);
662
663 probe2:
664         ib_unregister_device(&shca->ib_device);
665
666 probe1:
667         ib_dealloc_device(&shca->ib_device);
668
669         return -EINVAL;
670 }
671
672 static int __devexit ehca_remove(struct ibmebus_dev *dev)
673 {
674         struct ehca_shca *shca = dev->ofdev.dev.driver_data;
675         int ret;
676
677         ehca_remove_device_sysfs(dev);
678
679         if (ehca_open_aqp1 == 1) {
680                 int i;
681                 for (i = 0; i < shca->num_ports; i++) {
682                         ret = ehca_destroy_aqp1(&shca->sport[i]);
683                         if (ret)
684                                 ehca_err(&shca->ib_device,
685                                          "Cannot destroy AQP1 for port %x "
686                                          "ret=%x", ret, i);
687                 }
688         }
689
690         ib_unregister_device(&shca->ib_device);
691
692         ret = ehca_dereg_internal_maxmr(shca);
693         if (ret)
694                 ehca_err(&shca->ib_device,
695                          "Cannot destroy internal MR. ret=%x", ret);
696
697         ret = ehca_dealloc_pd(&shca->pd->ib_pd);
698         if (ret)
699                 ehca_err(&shca->ib_device,
700                          "Cannot destroy internal PD. ret=%x", ret);
701
702         ret = ehca_destroy_eq(shca, &shca->eq);
703         if (ret)
704                 ehca_err(&shca->ib_device, "Cannot destroy EQ. ret=%x", ret);
705
706         ret = ehca_destroy_eq(shca, &shca->neq);
707         if (ret)
708                 ehca_err(&shca->ib_device, "Canot destroy NEQ. ret=%x", ret);
709
710         ib_dealloc_device(&shca->ib_device);
711
712         spin_lock(&shca_list_lock);
713         list_del(&shca->shca_list);
714         spin_unlock(&shca_list_lock);
715
716         return ret;
717 }
718
719 static struct of_device_id ehca_device_table[] =
720 {
721         {
722                 .name       = "lhca",
723                 .compatible = "IBM,lhca",
724         },
725         {},
726 };
727
728 static struct ibmebus_driver ehca_driver = {
729         .name     = "ehca",
730         .id_table = ehca_device_table,
731         .probe    = ehca_probe,
732         .remove   = ehca_remove,
733 };
734
735 void ehca_poll_eqs(unsigned long data)
736 {
737         struct ehca_shca *shca;
738
739         spin_lock(&shca_list_lock);
740         list_for_each_entry(shca, &shca_list, shca_list) {
741                 if (shca->eq.is_initialized)
742                         ehca_tasklet_eq((unsigned long)(void*)shca);
743         }
744         mod_timer(&poll_eqs_timer, jiffies + HZ);
745         spin_unlock(&shca_list_lock);
746 }
747
748 int __init ehca_module_init(void)
749 {
750         int ret;
751
752         printk(KERN_INFO "eHCA Infiniband Device Driver "
753                          "(Rel.: SVNEHCA_0016)\n");
754         idr_init(&ehca_qp_idr);
755         idr_init(&ehca_cq_idr);
756         spin_lock_init(&ehca_qp_idr_lock);
757         spin_lock_init(&ehca_cq_idr_lock);
758
759         INIT_LIST_HEAD(&shca_list);
760         spin_lock_init(&shca_list_lock);
761
762         if ((ret = ehca_create_comp_pool())) {
763                 ehca_gen_err("Cannot create comp pool.");
764                 return ret;
765         }
766
767         if ((ret = ehca_create_slab_caches())) {
768                 ehca_gen_err("Cannot create SLAB caches");
769                 ret = -ENOMEM;
770                 goto module_init1;
771         }
772
773         if ((ret = ibmebus_register_driver(&ehca_driver))) {
774                 ehca_gen_err("Cannot register eHCA device driver");
775                 ret = -EINVAL;
776                 goto module_init2;
777         }
778
779         ehca_create_driver_sysfs(&ehca_driver);
780
781         if (ehca_poll_all_eqs != 1) {
782                 ehca_gen_err("WARNING!!!");
783                 ehca_gen_err("It is possible to lose interrupts.");
784         } else {
785                 init_timer(&poll_eqs_timer);
786                 poll_eqs_timer.function = ehca_poll_eqs;
787                 poll_eqs_timer.expires = jiffies + HZ;
788                 add_timer(&poll_eqs_timer);
789         }
790
791         return 0;
792
793 module_init2:
794         ehca_destroy_slab_caches();
795
796 module_init1:
797         ehca_destroy_comp_pool();
798         return ret;
799 };
800
801 void __exit ehca_module_exit(void)
802 {
803         if (ehca_poll_all_eqs == 1)
804                 del_timer_sync(&poll_eqs_timer);
805
806         ehca_remove_driver_sysfs(&ehca_driver);
807         ibmebus_unregister_driver(&ehca_driver);
808
809         ehca_destroy_slab_caches();
810
811         ehca_destroy_comp_pool();
812
813         idr_destroy(&ehca_cq_idr);
814         idr_destroy(&ehca_qp_idr);
815 };
816
817 module_init(ehca_module_init);
818 module_exit(ehca_module_exit);