Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/agpgart
[sfrench/cifs-2.6.git] / 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_0017");
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_init_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         return ret;
321 }
322
323 static int ehca_create_aqp1(struct ehca_shca *shca, u32 port)
324 {
325         struct ehca_sport *sport = &shca->sport[port - 1];
326         struct ib_cq *ibcq;
327         struct ib_qp *ibqp;
328         struct ib_qp_init_attr qp_init_attr;
329         int ret;
330
331         if (sport->ibcq_aqp1) {
332                 ehca_err(&shca->ib_device, "AQP1 CQ is already created.");
333                 return -EPERM;
334         }
335
336         ibcq = ib_create_cq(&shca->ib_device, NULL, NULL, (void*)(-1), 10);
337         if (IS_ERR(ibcq)) {
338                 ehca_err(&shca->ib_device, "Cannot create AQP1 CQ.");
339                 return PTR_ERR(ibcq);
340         }
341         sport->ibcq_aqp1 = ibcq;
342
343         if (sport->ibqp_aqp1) {
344                 ehca_err(&shca->ib_device, "AQP1 QP is already created.");
345                 ret = -EPERM;
346                 goto create_aqp1;
347         }
348
349         memset(&qp_init_attr, 0, sizeof(struct ib_qp_init_attr));
350         qp_init_attr.send_cq          = ibcq;
351         qp_init_attr.recv_cq          = ibcq;
352         qp_init_attr.sq_sig_type      = IB_SIGNAL_ALL_WR;
353         qp_init_attr.cap.max_send_wr  = 100;
354         qp_init_attr.cap.max_recv_wr  = 100;
355         qp_init_attr.cap.max_send_sge = 2;
356         qp_init_attr.cap.max_recv_sge = 1;
357         qp_init_attr.qp_type          = IB_QPT_GSI;
358         qp_init_attr.port_num         = port;
359         qp_init_attr.qp_context       = NULL;
360         qp_init_attr.event_handler    = NULL;
361         qp_init_attr.srq              = NULL;
362
363         ibqp = ib_create_qp(&shca->pd->ib_pd, &qp_init_attr);
364         if (IS_ERR(ibqp)) {
365                 ehca_err(&shca->ib_device, "Cannot create AQP1 QP.");
366                 ret = PTR_ERR(ibqp);
367                 goto create_aqp1;
368         }
369         sport->ibqp_aqp1 = ibqp;
370
371         return 0;
372
373 create_aqp1:
374         ib_destroy_cq(sport->ibcq_aqp1);
375         return ret;
376 }
377
378 static int ehca_destroy_aqp1(struct ehca_sport *sport)
379 {
380         int ret;
381
382         ret = ib_destroy_qp(sport->ibqp_aqp1);
383         if (ret) {
384                 ehca_gen_err("Cannot destroy AQP1 QP. ret=%x", ret);
385                 return ret;
386         }
387
388         ret = ib_destroy_cq(sport->ibcq_aqp1);
389         if (ret)
390                 ehca_gen_err("Cannot destroy AQP1 CQ. ret=%x", ret);
391
392         return ret;
393 }
394
395 static ssize_t ehca_show_debug_level(struct device_driver *ddp, char *buf)
396 {
397         return  snprintf(buf, PAGE_SIZE, "%d\n",
398                          ehca_debug_level);
399 }
400
401 static ssize_t ehca_store_debug_level(struct device_driver *ddp,
402                                       const char *buf, size_t count)
403 {
404         int value = (*buf) - '0';
405         if (value >= 0 && value <= 9)
406                 ehca_debug_level = value;
407         return 1;
408 }
409
410 DRIVER_ATTR(debug_level, S_IRUSR | S_IWUSR,
411             ehca_show_debug_level, ehca_store_debug_level);
412
413 void ehca_create_driver_sysfs(struct ibmebus_driver *drv)
414 {
415         driver_create_file(&drv->driver, &driver_attr_debug_level);
416 }
417
418 void ehca_remove_driver_sysfs(struct ibmebus_driver *drv)
419 {
420         driver_remove_file(&drv->driver, &driver_attr_debug_level);
421 }
422
423 #define EHCA_RESOURCE_ATTR(name)                                           \
424 static ssize_t  ehca_show_##name(struct device *dev,                       \
425                                  struct device_attribute *attr,            \
426                                  char *buf)                                \
427 {                                                                          \
428         struct ehca_shca *shca;                                            \
429         struct hipz_query_hca *rblock;                                     \
430         int data;                                                          \
431                                                                            \
432         shca = dev->driver_data;                                           \
433                                                                            \
434         rblock = kzalloc(H_CB_ALIGNMENT, GFP_KERNEL);                      \
435         if (!rblock) {                                                     \
436                 dev_err(dev, "Can't allocate rblock memory.");             \
437                 return 0;                                                  \
438         }                                                                  \
439                                                                            \
440         if (hipz_h_query_hca(shca->ipz_hca_handle, rblock) != H_SUCCESS) { \
441                 dev_err(dev, "Can't query device properties");             \
442                 kfree(rblock);                                             \
443                 return 0;                                                  \
444         }                                                                  \
445                                                                            \
446         data = rblock->name;                                               \
447         kfree(rblock);                                                     \
448                                                                            \
449         if ((strcmp(#name, "num_ports") == 0) && (ehca_nr_ports == 1))     \
450                 return snprintf(buf, 256, "1\n");                          \
451         else                                                               \
452                 return snprintf(buf, 256, "%d\n", data);                   \
453                                                                            \
454 }                                                                          \
455 static DEVICE_ATTR(name, S_IRUGO, ehca_show_##name, NULL);
456
457 EHCA_RESOURCE_ATTR(num_ports);
458 EHCA_RESOURCE_ATTR(hw_ver);
459 EHCA_RESOURCE_ATTR(max_eq);
460 EHCA_RESOURCE_ATTR(cur_eq);
461 EHCA_RESOURCE_ATTR(max_cq);
462 EHCA_RESOURCE_ATTR(cur_cq);
463 EHCA_RESOURCE_ATTR(max_qp);
464 EHCA_RESOURCE_ATTR(cur_qp);
465 EHCA_RESOURCE_ATTR(max_mr);
466 EHCA_RESOURCE_ATTR(cur_mr);
467 EHCA_RESOURCE_ATTR(max_mw);
468 EHCA_RESOURCE_ATTR(cur_mw);
469 EHCA_RESOURCE_ATTR(max_pd);
470 EHCA_RESOURCE_ATTR(max_ah);
471
472 static ssize_t ehca_show_adapter_handle(struct device *dev,
473                                         struct device_attribute *attr,
474                                         char *buf)
475 {
476         struct ehca_shca *shca = dev->driver_data;
477
478         return sprintf(buf, "%lx\n", shca->ipz_hca_handle.handle);
479
480 }
481 static DEVICE_ATTR(adapter_handle, S_IRUGO, ehca_show_adapter_handle, NULL);
482
483
484 void ehca_create_device_sysfs(struct ibmebus_dev *dev)
485 {
486         device_create_file(&dev->ofdev.dev, &dev_attr_adapter_handle);
487         device_create_file(&dev->ofdev.dev, &dev_attr_num_ports);
488         device_create_file(&dev->ofdev.dev, &dev_attr_hw_ver);
489         device_create_file(&dev->ofdev.dev, &dev_attr_max_eq);
490         device_create_file(&dev->ofdev.dev, &dev_attr_cur_eq);
491         device_create_file(&dev->ofdev.dev, &dev_attr_max_cq);
492         device_create_file(&dev->ofdev.dev, &dev_attr_cur_cq);
493         device_create_file(&dev->ofdev.dev, &dev_attr_max_qp);
494         device_create_file(&dev->ofdev.dev, &dev_attr_cur_qp);
495         device_create_file(&dev->ofdev.dev, &dev_attr_max_mr);
496         device_create_file(&dev->ofdev.dev, &dev_attr_cur_mr);
497         device_create_file(&dev->ofdev.dev, &dev_attr_max_mw);
498         device_create_file(&dev->ofdev.dev, &dev_attr_cur_mw);
499         device_create_file(&dev->ofdev.dev, &dev_attr_max_pd);
500         device_create_file(&dev->ofdev.dev, &dev_attr_max_ah);
501 }
502
503 void ehca_remove_device_sysfs(struct ibmebus_dev *dev)
504 {
505         device_remove_file(&dev->ofdev.dev, &dev_attr_adapter_handle);
506         device_remove_file(&dev->ofdev.dev, &dev_attr_num_ports);
507         device_remove_file(&dev->ofdev.dev, &dev_attr_hw_ver);
508         device_remove_file(&dev->ofdev.dev, &dev_attr_max_eq);
509         device_remove_file(&dev->ofdev.dev, &dev_attr_cur_eq);
510         device_remove_file(&dev->ofdev.dev, &dev_attr_max_cq);
511         device_remove_file(&dev->ofdev.dev, &dev_attr_cur_cq);
512         device_remove_file(&dev->ofdev.dev, &dev_attr_max_qp);
513         device_remove_file(&dev->ofdev.dev, &dev_attr_cur_qp);
514         device_remove_file(&dev->ofdev.dev, &dev_attr_max_mr);
515         device_remove_file(&dev->ofdev.dev, &dev_attr_cur_mr);
516         device_remove_file(&dev->ofdev.dev, &dev_attr_max_mw);
517         device_remove_file(&dev->ofdev.dev, &dev_attr_cur_mw);
518         device_remove_file(&dev->ofdev.dev, &dev_attr_max_pd);
519         device_remove_file(&dev->ofdev.dev, &dev_attr_max_ah);
520 }
521
522 static int __devinit ehca_probe(struct ibmebus_dev *dev,
523                                 const struct of_device_id *id)
524 {
525         struct ehca_shca *shca;
526         u64 *handle;
527         struct ib_pd *ibpd;
528         int ret;
529
530         handle = (u64 *)get_property(dev->ofdev.node, "ibm,hca-handle", NULL);
531         if (!handle) {
532                 ehca_gen_err("Cannot get eHCA handle for adapter: %s.",
533                              dev->ofdev.node->full_name);
534                 return -ENODEV;
535         }
536
537         if (!(*handle)) {
538                 ehca_gen_err("Wrong eHCA handle for adapter: %s.",
539                              dev->ofdev.node->full_name);
540                 return -ENODEV;
541         }
542
543         shca = (struct ehca_shca *)ib_alloc_device(sizeof(*shca));
544         if (!shca) {
545                 ehca_gen_err("Cannot allocate shca memory.");
546                 return -ENOMEM;
547         }
548
549         shca->ibmebus_dev = dev;
550         shca->ipz_hca_handle.handle = *handle;
551         dev->ofdev.dev.driver_data = shca;
552
553         ret = ehca_sense_attributes(shca);
554         if (ret < 0) {
555                 ehca_gen_err("Cannot sense eHCA attributes.");
556                 goto probe1;
557         }
558
559         ret = ehca_init_device(shca);
560         if (ret) {
561                 ehca_gen_err("Cannot init ehca  device struct");
562                 goto probe1;
563         }
564
565         /* create event queues */
566         ret = ehca_create_eq(shca, &shca->eq, EHCA_EQ, 2048);
567         if (ret) {
568                 ehca_err(&shca->ib_device, "Cannot create EQ.");
569                 goto probe1;
570         }
571
572         ret = ehca_create_eq(shca, &shca->neq, EHCA_NEQ, 513);
573         if (ret) {
574                 ehca_err(&shca->ib_device, "Cannot create NEQ.");
575                 goto probe3;
576         }
577
578         /* create internal protection domain */
579         ibpd = ehca_alloc_pd(&shca->ib_device, (void*)(-1), NULL);
580         if (IS_ERR(ibpd)) {
581                 ehca_err(&shca->ib_device, "Cannot create internal PD.");
582                 ret = PTR_ERR(ibpd);
583                 goto probe4;
584         }
585
586         shca->pd = container_of(ibpd, struct ehca_pd, ib_pd);
587         shca->pd->ib_pd.device = &shca->ib_device;
588
589         /* create internal max MR */
590         ret = ehca_reg_internal_maxmr(shca, shca->pd, &shca->maxmr);
591
592         if (ret) {
593                 ehca_err(&shca->ib_device, "Cannot create internal MR ret=%x",
594                          ret);
595                 goto probe5;
596         }
597
598         ret = ib_register_device(&shca->ib_device);
599         if (ret) {
600                 ehca_err(&shca->ib_device,
601                          "ib_register_device() failed ret=%x", ret);
602                 goto probe6;
603         }
604
605         /* create AQP1 for port 1 */
606         if (ehca_open_aqp1 == 1) {
607                 shca->sport[0].port_state = IB_PORT_DOWN;
608                 ret = ehca_create_aqp1(shca, 1);
609                 if (ret) {
610                         ehca_err(&shca->ib_device,
611                                  "Cannot create AQP1 for port 1.");
612                         goto probe7;
613                 }
614         }
615
616         /* create AQP1 for port 2 */
617         if ((ehca_open_aqp1 == 1) && (shca->num_ports == 2)) {
618                 shca->sport[1].port_state = IB_PORT_DOWN;
619                 ret = ehca_create_aqp1(shca, 2);
620                 if (ret) {
621                         ehca_err(&shca->ib_device,
622                                  "Cannot create AQP1 for port 2.");
623                         goto probe8;
624                 }
625         }
626
627         ehca_create_device_sysfs(dev);
628
629         spin_lock(&shca_list_lock);
630         list_add(&shca->shca_list, &shca_list);
631         spin_unlock(&shca_list_lock);
632
633         return 0;
634
635 probe8:
636         ret = ehca_destroy_aqp1(&shca->sport[0]);
637         if (ret)
638                 ehca_err(&shca->ib_device,
639                          "Cannot destroy AQP1 for port 1. ret=%x", ret);
640
641 probe7:
642         ib_unregister_device(&shca->ib_device);
643
644 probe6:
645         ret = ehca_dereg_internal_maxmr(shca);
646         if (ret)
647                 ehca_err(&shca->ib_device,
648                          "Cannot destroy internal MR. ret=%x", ret);
649
650 probe5:
651         ret = ehca_dealloc_pd(&shca->pd->ib_pd);
652         if (ret)
653                 ehca_err(&shca->ib_device,
654                          "Cannot destroy internal PD. ret=%x", ret);
655
656 probe4:
657         ret = ehca_destroy_eq(shca, &shca->neq);
658         if (ret)
659                 ehca_err(&shca->ib_device,
660                          "Cannot destroy NEQ. ret=%x", ret);
661
662 probe3:
663         ret = ehca_destroy_eq(shca, &shca->eq);
664         if (ret)
665                 ehca_err(&shca->ib_device,
666                          "Cannot destroy EQ. ret=%x", ret);
667
668 probe1:
669         ib_dealloc_device(&shca->ib_device);
670
671         return -EINVAL;
672 }
673
674 static int __devexit ehca_remove(struct ibmebus_dev *dev)
675 {
676         struct ehca_shca *shca = dev->ofdev.dev.driver_data;
677         int ret;
678
679         ehca_remove_device_sysfs(dev);
680
681         if (ehca_open_aqp1 == 1) {
682                 int i;
683                 for (i = 0; i < shca->num_ports; i++) {
684                         ret = ehca_destroy_aqp1(&shca->sport[i]);
685                         if (ret)
686                                 ehca_err(&shca->ib_device,
687                                          "Cannot destroy AQP1 for port %x "
688                                          "ret=%x", ret, i);
689                 }
690         }
691
692         ib_unregister_device(&shca->ib_device);
693
694         ret = ehca_dereg_internal_maxmr(shca);
695         if (ret)
696                 ehca_err(&shca->ib_device,
697                          "Cannot destroy internal MR. ret=%x", ret);
698
699         ret = ehca_dealloc_pd(&shca->pd->ib_pd);
700         if (ret)
701                 ehca_err(&shca->ib_device,
702                          "Cannot destroy internal PD. ret=%x", ret);
703
704         ret = ehca_destroy_eq(shca, &shca->eq);
705         if (ret)
706                 ehca_err(&shca->ib_device, "Cannot destroy EQ. ret=%x", ret);
707
708         ret = ehca_destroy_eq(shca, &shca->neq);
709         if (ret)
710                 ehca_err(&shca->ib_device, "Canot destroy NEQ. ret=%x", ret);
711
712         ib_dealloc_device(&shca->ib_device);
713
714         spin_lock(&shca_list_lock);
715         list_del(&shca->shca_list);
716         spin_unlock(&shca_list_lock);
717
718         return ret;
719 }
720
721 static struct of_device_id ehca_device_table[] =
722 {
723         {
724                 .name       = "lhca",
725                 .compatible = "IBM,lhca",
726         },
727         {},
728 };
729
730 static struct ibmebus_driver ehca_driver = {
731         .name     = "ehca",
732         .id_table = ehca_device_table,
733         .probe    = ehca_probe,
734         .remove   = ehca_remove,
735 };
736
737 void ehca_poll_eqs(unsigned long data)
738 {
739         struct ehca_shca *shca;
740
741         spin_lock(&shca_list_lock);
742         list_for_each_entry(shca, &shca_list, shca_list) {
743                 if (shca->eq.is_initialized)
744                         ehca_tasklet_eq((unsigned long)(void*)shca);
745         }
746         mod_timer(&poll_eqs_timer, jiffies + HZ);
747         spin_unlock(&shca_list_lock);
748 }
749
750 int __init ehca_module_init(void)
751 {
752         int ret;
753
754         printk(KERN_INFO "eHCA Infiniband Device Driver "
755                          "(Rel.: SVNEHCA_0017)\n");
756         idr_init(&ehca_qp_idr);
757         idr_init(&ehca_cq_idr);
758         spin_lock_init(&ehca_qp_idr_lock);
759         spin_lock_init(&ehca_cq_idr_lock);
760
761         INIT_LIST_HEAD(&shca_list);
762         spin_lock_init(&shca_list_lock);
763
764         if ((ret = ehca_create_comp_pool())) {
765                 ehca_gen_err("Cannot create comp pool.");
766                 return ret;
767         }
768
769         if ((ret = ehca_create_slab_caches())) {
770                 ehca_gen_err("Cannot create SLAB caches");
771                 ret = -ENOMEM;
772                 goto module_init1;
773         }
774
775         if ((ret = ibmebus_register_driver(&ehca_driver))) {
776                 ehca_gen_err("Cannot register eHCA device driver");
777                 ret = -EINVAL;
778                 goto module_init2;
779         }
780
781         ehca_create_driver_sysfs(&ehca_driver);
782
783         if (ehca_poll_all_eqs != 1) {
784                 ehca_gen_err("WARNING!!!");
785                 ehca_gen_err("It is possible to lose interrupts.");
786         } else {
787                 init_timer(&poll_eqs_timer);
788                 poll_eqs_timer.function = ehca_poll_eqs;
789                 poll_eqs_timer.expires = jiffies + HZ;
790                 add_timer(&poll_eqs_timer);
791         }
792
793         return 0;
794
795 module_init2:
796         ehca_destroy_slab_caches();
797
798 module_init1:
799         ehca_destroy_comp_pool();
800         return ret;
801 };
802
803 void __exit ehca_module_exit(void)
804 {
805         if (ehca_poll_all_eqs == 1)
806                 del_timer_sync(&poll_eqs_timer);
807
808         ehca_remove_driver_sysfs(&ehca_driver);
809         ibmebus_unregister_driver(&ehca_driver);
810
811         ehca_destroy_slab_caches();
812
813         ehca_destroy_comp_pool();
814
815         idr_destroy(&ehca_cq_idr);
816         idr_destroy(&ehca_qp_idr);
817 };
818
819 module_init(ehca_module_init);
820 module_exit(ehca_module_exit);