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