982e3efd98d3ef6d1f6c3cbce6b894e494c0f870
[cascardo/linux.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
47 #include <linux/notifier.h>
48 #include <linux/memory.h>
49 #include "ehca_classes.h"
50 #include "ehca_iverbs.h"
51 #include "ehca_mrmw.h"
52 #include "ehca_tools.h"
53 #include "hcp_if.h"
54
55 #define HCAD_VERSION "0029"
56
57 MODULE_LICENSE("Dual BSD/GPL");
58 MODULE_AUTHOR("Christoph Raisch <raisch@de.ibm.com>");
59 MODULE_DESCRIPTION("IBM eServer HCA InfiniBand Device Driver");
60 MODULE_VERSION(HCAD_VERSION);
61
62 static bool ehca_open_aqp1    = 0;
63 static int ehca_hw_level      = 0;
64 static bool ehca_poll_all_eqs = 1;
65
66 int ehca_debug_level   = 0;
67 int ehca_nr_ports      = -1;
68 bool ehca_use_hp_mr    = 0;
69 int ehca_port_act_time = 30;
70 int ehca_static_rate   = -1;
71 bool ehca_scaling_code = 0;
72 int ehca_lock_hcalls   = -1;
73 int ehca_max_cq        = -1;
74 int ehca_max_qp        = -1;
75
76 module_param_named(open_aqp1,     ehca_open_aqp1,     bool, S_IRUGO);
77 module_param_named(debug_level,   ehca_debug_level,   int,  S_IRUGO);
78 module_param_named(hw_level,      ehca_hw_level,      int,  S_IRUGO);
79 module_param_named(nr_ports,      ehca_nr_ports,      int,  S_IRUGO);
80 module_param_named(use_hp_mr,     ehca_use_hp_mr,     bool, S_IRUGO);
81 module_param_named(port_act_time, ehca_port_act_time, int,  S_IRUGO);
82 module_param_named(poll_all_eqs,  ehca_poll_all_eqs,  bool, S_IRUGO);
83 module_param_named(static_rate,   ehca_static_rate,   int,  S_IRUGO);
84 module_param_named(scaling_code,  ehca_scaling_code,  bool, S_IRUGO);
85 module_param_named(lock_hcalls,   ehca_lock_hcalls,   bint, S_IRUGO);
86 module_param_named(number_of_cqs, ehca_max_cq,        int,  S_IRUGO);
87 module_param_named(number_of_qps, ehca_max_qp,        int,  S_IRUGO);
88
89 MODULE_PARM_DESC(open_aqp1,
90                  "Open AQP1 on startup (default: no)");
91 MODULE_PARM_DESC(debug_level,
92                  "Amount of debug output (0: none (default), 1: traces, "
93                  "2: some dumps, 3: lots)");
94 MODULE_PARM_DESC(hw_level,
95                  "Hardware level (0: autosensing (default), "
96                  "0x10..0x14: eHCA, 0x20..0x23: eHCA2)");
97 MODULE_PARM_DESC(nr_ports,
98                  "number of connected ports (-1: autodetect (default), "
99                  "1: port one only, 2: two ports)");
100 MODULE_PARM_DESC(use_hp_mr,
101                  "Use high performance MRs (default: no)");
102 MODULE_PARM_DESC(port_act_time,
103                  "Time to wait for port activation (default: 30 sec)");
104 MODULE_PARM_DESC(poll_all_eqs,
105                  "Poll all event queues periodically (default: yes)");
106 MODULE_PARM_DESC(static_rate,
107                  "Set permanent static rate (default: no static rate)");
108 MODULE_PARM_DESC(scaling_code,
109                  "Enable scaling code (default: no)");
110 MODULE_PARM_DESC(lock_hcalls,
111                  "Serialize all hCalls made by the driver "
112                  "(default: autodetect)");
113 MODULE_PARM_DESC(number_of_cqs,
114                 "Max number of CQs which can be allocated "
115                 "(default: autodetect)");
116 MODULE_PARM_DESC(number_of_qps,
117                 "Max number of QPs which can be allocated "
118                 "(default: autodetect)");
119
120 DEFINE_RWLOCK(ehca_qp_idr_lock);
121 DEFINE_RWLOCK(ehca_cq_idr_lock);
122 DEFINE_IDR(ehca_qp_idr);
123 DEFINE_IDR(ehca_cq_idr);
124
125 static LIST_HEAD(shca_list); /* list of all registered ehcas */
126 DEFINE_SPINLOCK(shca_list_lock);
127
128 static struct timer_list poll_eqs_timer;
129
130 #ifdef CONFIG_PPC_64K_PAGES
131 static struct kmem_cache *ctblk_cache;
132
133 void *ehca_alloc_fw_ctrlblock(gfp_t flags)
134 {
135         void *ret = kmem_cache_zalloc(ctblk_cache, flags);
136         if (!ret)
137                 ehca_gen_err("Out of memory for ctblk");
138         return ret;
139 }
140
141 void ehca_free_fw_ctrlblock(void *ptr)
142 {
143         if (ptr)
144                 kmem_cache_free(ctblk_cache, ptr);
145
146 }
147 #endif
148
149 int ehca2ib_return_code(u64 ehca_rc)
150 {
151         switch (ehca_rc) {
152         case H_SUCCESS:
153                 return 0;
154         case H_RESOURCE:             /* Resource in use */
155         case H_BUSY:
156                 return -EBUSY;
157         case H_NOT_ENOUGH_RESOURCES: /* insufficient resources */
158         case H_CONSTRAINED:          /* resource constraint */
159         case H_NO_MEM:
160                 return -ENOMEM;
161         default:
162                 return -EINVAL;
163         }
164 }
165
166 static int ehca_create_slab_caches(void)
167 {
168         int ret;
169
170         ret = ehca_init_pd_cache();
171         if (ret) {
172                 ehca_gen_err("Cannot create PD SLAB cache.");
173                 return ret;
174         }
175
176         ret = ehca_init_cq_cache();
177         if (ret) {
178                 ehca_gen_err("Cannot create CQ SLAB cache.");
179                 goto create_slab_caches2;
180         }
181
182         ret = ehca_init_qp_cache();
183         if (ret) {
184                 ehca_gen_err("Cannot create QP SLAB cache.");
185                 goto create_slab_caches3;
186         }
187
188         ret = ehca_init_av_cache();
189         if (ret) {
190                 ehca_gen_err("Cannot create AV SLAB cache.");
191                 goto create_slab_caches4;
192         }
193
194         ret = ehca_init_mrmw_cache();
195         if (ret) {
196                 ehca_gen_err("Cannot create MR&MW SLAB cache.");
197                 goto create_slab_caches5;
198         }
199
200         ret = ehca_init_small_qp_cache();
201         if (ret) {
202                 ehca_gen_err("Cannot create small queue SLAB cache.");
203                 goto create_slab_caches6;
204         }
205
206 #ifdef CONFIG_PPC_64K_PAGES
207         ctblk_cache = kmem_cache_create("ehca_cache_ctblk",
208                                         EHCA_PAGESIZE, H_CB_ALIGNMENT,
209                                         SLAB_HWCACHE_ALIGN,
210                                         NULL);
211         if (!ctblk_cache) {
212                 ehca_gen_err("Cannot create ctblk SLAB cache.");
213                 ehca_cleanup_small_qp_cache();
214                 goto create_slab_caches6;
215         }
216 #endif
217         return 0;
218
219 create_slab_caches6:
220         ehca_cleanup_mrmw_cache();
221
222 create_slab_caches5:
223         ehca_cleanup_av_cache();
224
225 create_slab_caches4:
226         ehca_cleanup_qp_cache();
227
228 create_slab_caches3:
229         ehca_cleanup_cq_cache();
230
231 create_slab_caches2:
232         ehca_cleanup_pd_cache();
233
234         return ret;
235 }
236
237 static void ehca_destroy_slab_caches(void)
238 {
239         ehca_cleanup_small_qp_cache();
240         ehca_cleanup_mrmw_cache();
241         ehca_cleanup_av_cache();
242         ehca_cleanup_qp_cache();
243         ehca_cleanup_cq_cache();
244         ehca_cleanup_pd_cache();
245 #ifdef CONFIG_PPC_64K_PAGES
246         if (ctblk_cache)
247                 kmem_cache_destroy(ctblk_cache);
248 #endif
249 }
250
251 #define EHCA_HCAAVER  EHCA_BMASK_IBM(32, 39)
252 #define EHCA_REVID    EHCA_BMASK_IBM(40, 63)
253
254 static struct cap_descr {
255         u64 mask;
256         char *descr;
257 } hca_cap_descr[] = {
258         { HCA_CAP_AH_PORT_NR_CHECK, "HCA_CAP_AH_PORT_NR_CHECK" },
259         { HCA_CAP_ATOMIC, "HCA_CAP_ATOMIC" },
260         { HCA_CAP_AUTO_PATH_MIG, "HCA_CAP_AUTO_PATH_MIG" },
261         { HCA_CAP_BAD_P_KEY_CTR, "HCA_CAP_BAD_P_KEY_CTR" },
262         { HCA_CAP_SQD_RTS_PORT_CHANGE, "HCA_CAP_SQD_RTS_PORT_CHANGE" },
263         { HCA_CAP_CUR_QP_STATE_MOD, "HCA_CAP_CUR_QP_STATE_MOD" },
264         { HCA_CAP_INIT_TYPE, "HCA_CAP_INIT_TYPE" },
265         { HCA_CAP_PORT_ACTIVE_EVENT, "HCA_CAP_PORT_ACTIVE_EVENT" },
266         { HCA_CAP_Q_KEY_VIOL_CTR, "HCA_CAP_Q_KEY_VIOL_CTR" },
267         { HCA_CAP_WQE_RESIZE, "HCA_CAP_WQE_RESIZE" },
268         { HCA_CAP_RAW_PACKET_MCAST, "HCA_CAP_RAW_PACKET_MCAST" },
269         { HCA_CAP_SHUTDOWN_PORT, "HCA_CAP_SHUTDOWN_PORT" },
270         { HCA_CAP_RC_LL_QP, "HCA_CAP_RC_LL_QP" },
271         { HCA_CAP_SRQ, "HCA_CAP_SRQ" },
272         { HCA_CAP_UD_LL_QP, "HCA_CAP_UD_LL_QP" },
273         { HCA_CAP_RESIZE_MR, "HCA_CAP_RESIZE_MR" },
274         { HCA_CAP_MINI_QP, "HCA_CAP_MINI_QP" },
275         { HCA_CAP_H_ALLOC_RES_SYNC, "HCA_CAP_H_ALLOC_RES_SYNC" },
276 };
277
278 static int ehca_sense_attributes(struct ehca_shca *shca)
279 {
280         int i, ret = 0;
281         u64 h_ret;
282         struct hipz_query_hca *rblock;
283         struct hipz_query_port *port;
284         const char *loc_code;
285
286         static const u32 pgsize_map[] = {
287                 HCA_CAP_MR_PGSIZE_4K,  0x1000,
288                 HCA_CAP_MR_PGSIZE_64K, 0x10000,
289                 HCA_CAP_MR_PGSIZE_1M,  0x100000,
290                 HCA_CAP_MR_PGSIZE_16M, 0x1000000,
291         };
292
293         ehca_gen_dbg("Probing adapter %s...",
294                      shca->ofdev->dev.of_node->full_name);
295         loc_code = of_get_property(shca->ofdev->dev.of_node, "ibm,loc-code",
296                                    NULL);
297         if (loc_code)
298                 ehca_gen_dbg(" ... location lode=%s", loc_code);
299
300         rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
301         if (!rblock) {
302                 ehca_gen_err("Cannot allocate rblock memory.");
303                 return -ENOMEM;
304         }
305
306         h_ret = hipz_h_query_hca(shca->ipz_hca_handle, rblock);
307         if (h_ret != H_SUCCESS) {
308                 ehca_gen_err("Cannot query device properties. h_ret=%lli",
309                              h_ret);
310                 ret = -EPERM;
311                 goto sense_attributes1;
312         }
313
314         if (ehca_nr_ports == 1)
315                 shca->num_ports = 1;
316         else
317                 shca->num_ports = (u8)rblock->num_ports;
318
319         ehca_gen_dbg(" ... found %x ports", rblock->num_ports);
320
321         if (ehca_hw_level == 0) {
322                 u32 hcaaver;
323                 u32 revid;
324
325                 hcaaver = EHCA_BMASK_GET(EHCA_HCAAVER, rblock->hw_ver);
326                 revid   = EHCA_BMASK_GET(EHCA_REVID, rblock->hw_ver);
327
328                 ehca_gen_dbg(" ... hardware version=%x:%x", hcaaver, revid);
329
330                 if (hcaaver == 1) {
331                         if (revid <= 3)
332                                 shca->hw_level = 0x10 | (revid + 1);
333                         else
334                                 shca->hw_level = 0x14;
335                 } else if (hcaaver == 2) {
336                         if (revid == 0)
337                                 shca->hw_level = 0x21;
338                         else if (revid == 0x10)
339                                 shca->hw_level = 0x22;
340                         else if (revid == 0x20 || revid == 0x21)
341                                 shca->hw_level = 0x23;
342                 }
343
344                 if (!shca->hw_level) {
345                         ehca_gen_warn("unknown hardware version"
346                                       " - assuming default level");
347                         shca->hw_level = 0x22;
348                 }
349         } else
350                 shca->hw_level = ehca_hw_level;
351         ehca_gen_dbg(" ... hardware level=%x", shca->hw_level);
352
353         shca->hca_cap = rblock->hca_cap_indicators;
354         ehca_gen_dbg(" ... HCA capabilities:");
355         for (i = 0; i < ARRAY_SIZE(hca_cap_descr); i++)
356                 if (EHCA_BMASK_GET(hca_cap_descr[i].mask, shca->hca_cap))
357                         ehca_gen_dbg("   %s", hca_cap_descr[i].descr);
358
359         /* Autodetect hCall locking -- the "H_ALLOC_RESOURCE synced" flag is
360          * a firmware property, so it's valid across all adapters
361          */
362         if (ehca_lock_hcalls == -1)
363                 ehca_lock_hcalls = !EHCA_BMASK_GET(HCA_CAP_H_ALLOC_RES_SYNC,
364                                         shca->hca_cap);
365
366         /* translate supported MR page sizes; always support 4K */
367         shca->hca_cap_mr_pgsize = EHCA_PAGESIZE;
368         for (i = 0; i < ARRAY_SIZE(pgsize_map); i += 2)
369                 if (rblock->memory_page_size_supported & pgsize_map[i])
370                         shca->hca_cap_mr_pgsize |= pgsize_map[i + 1];
371
372         /* Set maximum number of CQs and QPs to calculate EQ size */
373         if (shca->max_num_qps == -1)
374                 shca->max_num_qps = min_t(int, rblock->max_qp,
375                                           EHCA_MAX_NUM_QUEUES);
376         else if (shca->max_num_qps < 1 || shca->max_num_qps > rblock->max_qp) {
377                 ehca_gen_warn("The requested number of QPs is out of range "
378                               "(1 - %i) specified by HW. Value is set to %i",
379                               rblock->max_qp, rblock->max_qp);
380                 shca->max_num_qps = rblock->max_qp;
381         }
382
383         if (shca->max_num_cqs == -1)
384                 shca->max_num_cqs = min_t(int, rblock->max_cq,
385                                           EHCA_MAX_NUM_QUEUES);
386         else if (shca->max_num_cqs < 1 || shca->max_num_cqs > rblock->max_cq) {
387                 ehca_gen_warn("The requested number of CQs is out of range "
388                               "(1 - %i) specified by HW. Value is set to %i",
389                               rblock->max_cq, rblock->max_cq);
390         }
391
392         /* query max MTU from first port -- it's the same for all ports */
393         port = (struct hipz_query_port *)rblock;
394         h_ret = hipz_h_query_port(shca->ipz_hca_handle, 1, port);
395         if (h_ret != H_SUCCESS) {
396                 ehca_gen_err("Cannot query port properties. h_ret=%lli",
397                              h_ret);
398                 ret = -EPERM;
399                 goto sense_attributes1;
400         }
401
402         shca->max_mtu = port->max_mtu;
403
404 sense_attributes1:
405         ehca_free_fw_ctrlblock(rblock);
406         return ret;
407 }
408
409 static int init_node_guid(struct ehca_shca *shca)
410 {
411         int ret = 0;
412         struct hipz_query_hca *rblock;
413
414         rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
415         if (!rblock) {
416                 ehca_err(&shca->ib_device, "Can't allocate rblock memory.");
417                 return -ENOMEM;
418         }
419
420         if (hipz_h_query_hca(shca->ipz_hca_handle, rblock) != H_SUCCESS) {
421                 ehca_err(&shca->ib_device, "Can't query device properties");
422                 ret = -EINVAL;
423                 goto init_node_guid1;
424         }
425
426         memcpy(&shca->ib_device.node_guid, &rblock->node_guid, sizeof(u64));
427
428 init_node_guid1:
429         ehca_free_fw_ctrlblock(rblock);
430         return ret;
431 }
432
433 static int ehca_init_device(struct ehca_shca *shca)
434 {
435         int ret;
436
437         ret = init_node_guid(shca);
438         if (ret)
439                 return ret;
440
441         strlcpy(shca->ib_device.name, "ehca%d", IB_DEVICE_NAME_MAX);
442         shca->ib_device.owner               = THIS_MODULE;
443
444         shca->ib_device.uverbs_abi_ver      = 8;
445         shca->ib_device.uverbs_cmd_mask     =
446                 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT)         |
447                 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)        |
448                 (1ull << IB_USER_VERBS_CMD_QUERY_PORT)          |
449                 (1ull << IB_USER_VERBS_CMD_ALLOC_PD)            |
450                 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD)          |
451                 (1ull << IB_USER_VERBS_CMD_REG_MR)              |
452                 (1ull << IB_USER_VERBS_CMD_DEREG_MR)            |
453                 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
454                 (1ull << IB_USER_VERBS_CMD_CREATE_CQ)           |
455                 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ)          |
456                 (1ull << IB_USER_VERBS_CMD_CREATE_QP)           |
457                 (1ull << IB_USER_VERBS_CMD_MODIFY_QP)           |
458                 (1ull << IB_USER_VERBS_CMD_QUERY_QP)            |
459                 (1ull << IB_USER_VERBS_CMD_DESTROY_QP)          |
460                 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)        |
461                 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST);
462
463         shca->ib_device.node_type           = RDMA_NODE_IB_CA;
464         shca->ib_device.phys_port_cnt       = shca->num_ports;
465         shca->ib_device.num_comp_vectors    = 1;
466         shca->ib_device.dma_device          = &shca->ofdev->dev;
467         shca->ib_device.query_device        = ehca_query_device;
468         shca->ib_device.query_port          = ehca_query_port;
469         shca->ib_device.query_gid           = ehca_query_gid;
470         shca->ib_device.query_pkey          = ehca_query_pkey;
471         /* shca->in_device.modify_device    = ehca_modify_device    */
472         shca->ib_device.modify_port         = ehca_modify_port;
473         shca->ib_device.alloc_ucontext      = ehca_alloc_ucontext;
474         shca->ib_device.dealloc_ucontext    = ehca_dealloc_ucontext;
475         shca->ib_device.alloc_pd            = ehca_alloc_pd;
476         shca->ib_device.dealloc_pd          = ehca_dealloc_pd;
477         shca->ib_device.create_ah           = ehca_create_ah;
478         /* shca->ib_device.modify_ah        = ehca_modify_ah;       */
479         shca->ib_device.query_ah            = ehca_query_ah;
480         shca->ib_device.destroy_ah          = ehca_destroy_ah;
481         shca->ib_device.create_qp           = ehca_create_qp;
482         shca->ib_device.modify_qp           = ehca_modify_qp;
483         shca->ib_device.query_qp            = ehca_query_qp;
484         shca->ib_device.destroy_qp          = ehca_destroy_qp;
485         shca->ib_device.post_send           = ehca_post_send;
486         shca->ib_device.post_recv           = ehca_post_recv;
487         shca->ib_device.create_cq           = ehca_create_cq;
488         shca->ib_device.destroy_cq          = ehca_destroy_cq;
489         shca->ib_device.resize_cq           = ehca_resize_cq;
490         shca->ib_device.poll_cq             = ehca_poll_cq;
491         /* shca->ib_device.peek_cq          = ehca_peek_cq;         */
492         shca->ib_device.req_notify_cq       = ehca_req_notify_cq;
493         /* shca->ib_device.req_ncomp_notif  = ehca_req_ncomp_notif; */
494         shca->ib_device.get_dma_mr          = ehca_get_dma_mr;
495         shca->ib_device.reg_phys_mr         = ehca_reg_phys_mr;
496         shca->ib_device.reg_user_mr         = ehca_reg_user_mr;
497         shca->ib_device.query_mr            = ehca_query_mr;
498         shca->ib_device.dereg_mr            = ehca_dereg_mr;
499         shca->ib_device.rereg_phys_mr       = ehca_rereg_phys_mr;
500         shca->ib_device.alloc_mw            = ehca_alloc_mw;
501         shca->ib_device.bind_mw             = ehca_bind_mw;
502         shca->ib_device.dealloc_mw          = ehca_dealloc_mw;
503         shca->ib_device.alloc_fmr           = ehca_alloc_fmr;
504         shca->ib_device.map_phys_fmr        = ehca_map_phys_fmr;
505         shca->ib_device.unmap_fmr           = ehca_unmap_fmr;
506         shca->ib_device.dealloc_fmr         = ehca_dealloc_fmr;
507         shca->ib_device.attach_mcast        = ehca_attach_mcast;
508         shca->ib_device.detach_mcast        = ehca_detach_mcast;
509         shca->ib_device.process_mad         = ehca_process_mad;
510         shca->ib_device.mmap                = ehca_mmap;
511         shca->ib_device.dma_ops             = &ehca_dma_mapping_ops;
512
513         if (EHCA_BMASK_GET(HCA_CAP_SRQ, shca->hca_cap)) {
514                 shca->ib_device.uverbs_cmd_mask |=
515                         (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
516                         (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
517                         (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
518                         (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ);
519
520                 shca->ib_device.create_srq          = ehca_create_srq;
521                 shca->ib_device.modify_srq          = ehca_modify_srq;
522                 shca->ib_device.query_srq           = ehca_query_srq;
523                 shca->ib_device.destroy_srq         = ehca_destroy_srq;
524                 shca->ib_device.post_srq_recv       = ehca_post_srq_recv;
525         }
526
527         return ret;
528 }
529
530 static int ehca_create_aqp1(struct ehca_shca *shca, u32 port)
531 {
532         struct ehca_sport *sport = &shca->sport[port - 1];
533         struct ib_cq *ibcq;
534         struct ib_qp *ibqp;
535         struct ib_qp_init_attr qp_init_attr;
536         int ret;
537
538         if (sport->ibcq_aqp1) {
539                 ehca_err(&shca->ib_device, "AQP1 CQ is already created.");
540                 return -EPERM;
541         }
542
543         ibcq = ib_create_cq(&shca->ib_device, NULL, NULL, (void *)(-1), 10, 0);
544         if (IS_ERR(ibcq)) {
545                 ehca_err(&shca->ib_device, "Cannot create AQP1 CQ.");
546                 return PTR_ERR(ibcq);
547         }
548         sport->ibcq_aqp1 = ibcq;
549
550         if (sport->ibqp_sqp[IB_QPT_GSI]) {
551                 ehca_err(&shca->ib_device, "AQP1 QP is already created.");
552                 ret = -EPERM;
553                 goto create_aqp1;
554         }
555
556         memset(&qp_init_attr, 0, sizeof(struct ib_qp_init_attr));
557         qp_init_attr.send_cq          = ibcq;
558         qp_init_attr.recv_cq          = ibcq;
559         qp_init_attr.sq_sig_type      = IB_SIGNAL_ALL_WR;
560         qp_init_attr.cap.max_send_wr  = 100;
561         qp_init_attr.cap.max_recv_wr  = 100;
562         qp_init_attr.cap.max_send_sge = 2;
563         qp_init_attr.cap.max_recv_sge = 1;
564         qp_init_attr.qp_type          = IB_QPT_GSI;
565         qp_init_attr.port_num         = port;
566         qp_init_attr.qp_context       = NULL;
567         qp_init_attr.event_handler    = NULL;
568         qp_init_attr.srq              = NULL;
569
570         ibqp = ib_create_qp(&shca->pd->ib_pd, &qp_init_attr);
571         if (IS_ERR(ibqp)) {
572                 ehca_err(&shca->ib_device, "Cannot create AQP1 QP.");
573                 ret = PTR_ERR(ibqp);
574                 goto create_aqp1;
575         }
576         sport->ibqp_sqp[IB_QPT_GSI] = ibqp;
577
578         return 0;
579
580 create_aqp1:
581         ib_destroy_cq(sport->ibcq_aqp1);
582         return ret;
583 }
584
585 static int ehca_destroy_aqp1(struct ehca_sport *sport)
586 {
587         int ret;
588
589         ret = ib_destroy_qp(sport->ibqp_sqp[IB_QPT_GSI]);
590         if (ret) {
591                 ehca_gen_err("Cannot destroy AQP1 QP. ret=%i", ret);
592                 return ret;
593         }
594
595         ret = ib_destroy_cq(sport->ibcq_aqp1);
596         if (ret)
597                 ehca_gen_err("Cannot destroy AQP1 CQ. ret=%i", ret);
598
599         return ret;
600 }
601
602 static ssize_t ehca_show_debug_level(struct device_driver *ddp, char *buf)
603 {
604         return snprintf(buf, PAGE_SIZE, "%d\n", ehca_debug_level);
605 }
606
607 static ssize_t ehca_store_debug_level(struct device_driver *ddp,
608                                       const char *buf, size_t count)
609 {
610         int value = (*buf) - '0';
611         if (value >= 0 && value <= 9)
612                 ehca_debug_level = value;
613         return 1;
614 }
615
616 static DRIVER_ATTR(debug_level, S_IRUSR | S_IWUSR,
617                    ehca_show_debug_level, ehca_store_debug_level);
618
619 static struct attribute *ehca_drv_attrs[] = {
620         &driver_attr_debug_level.attr,
621         NULL
622 };
623
624 static struct attribute_group ehca_drv_attr_grp = {
625         .attrs = ehca_drv_attrs
626 };
627
628 static const struct attribute_group *ehca_drv_attr_groups[] = {
629         &ehca_drv_attr_grp,
630         NULL,
631 };
632
633 #define EHCA_RESOURCE_ATTR(name)                                           \
634 static ssize_t  ehca_show_##name(struct device *dev,                       \
635                                  struct device_attribute *attr,            \
636                                  char *buf)                                \
637 {                                                                          \
638         struct ehca_shca *shca;                                            \
639         struct hipz_query_hca *rblock;                                     \
640         int data;                                                          \
641                                                                            \
642         shca = dev_get_drvdata(dev);                                       \
643                                                                            \
644         rblock = ehca_alloc_fw_ctrlblock(GFP_KERNEL);                      \
645         if (!rblock) {                                                     \
646                 dev_err(dev, "Can't allocate rblock memory.\n");           \
647                 return 0;                                                  \
648         }                                                                  \
649                                                                            \
650         if (hipz_h_query_hca(shca->ipz_hca_handle, rblock) != H_SUCCESS) { \
651                 dev_err(dev, "Can't query device properties\n");           \
652                 ehca_free_fw_ctrlblock(rblock);                            \
653                 return 0;                                                  \
654         }                                                                  \
655                                                                            \
656         data = rblock->name;                                               \
657         ehca_free_fw_ctrlblock(rblock);                                    \
658                                                                            \
659         if ((strcmp(#name, "num_ports") == 0) && (ehca_nr_ports == 1))     \
660                 return snprintf(buf, 256, "1\n");                          \
661         else                                                               \
662                 return snprintf(buf, 256, "%d\n", data);                   \
663                                                                            \
664 }                                                                          \
665 static DEVICE_ATTR(name, S_IRUGO, ehca_show_##name, NULL);
666
667 EHCA_RESOURCE_ATTR(num_ports);
668 EHCA_RESOURCE_ATTR(hw_ver);
669 EHCA_RESOURCE_ATTR(max_eq);
670 EHCA_RESOURCE_ATTR(cur_eq);
671 EHCA_RESOURCE_ATTR(max_cq);
672 EHCA_RESOURCE_ATTR(cur_cq);
673 EHCA_RESOURCE_ATTR(max_qp);
674 EHCA_RESOURCE_ATTR(cur_qp);
675 EHCA_RESOURCE_ATTR(max_mr);
676 EHCA_RESOURCE_ATTR(cur_mr);
677 EHCA_RESOURCE_ATTR(max_mw);
678 EHCA_RESOURCE_ATTR(cur_mw);
679 EHCA_RESOURCE_ATTR(max_pd);
680 EHCA_RESOURCE_ATTR(max_ah);
681
682 static ssize_t ehca_show_adapter_handle(struct device *dev,
683                                         struct device_attribute *attr,
684                                         char *buf)
685 {
686         struct ehca_shca *shca = dev_get_drvdata(dev);
687
688         return sprintf(buf, "%llx\n", shca->ipz_hca_handle.handle);
689
690 }
691 static DEVICE_ATTR(adapter_handle, S_IRUGO, ehca_show_adapter_handle, NULL);
692
693 static struct attribute *ehca_dev_attrs[] = {
694         &dev_attr_adapter_handle.attr,
695         &dev_attr_num_ports.attr,
696         &dev_attr_hw_ver.attr,
697         &dev_attr_max_eq.attr,
698         &dev_attr_cur_eq.attr,
699         &dev_attr_max_cq.attr,
700         &dev_attr_cur_cq.attr,
701         &dev_attr_max_qp.attr,
702         &dev_attr_cur_qp.attr,
703         &dev_attr_max_mr.attr,
704         &dev_attr_cur_mr.attr,
705         &dev_attr_max_mw.attr,
706         &dev_attr_cur_mw.attr,
707         &dev_attr_max_pd.attr,
708         &dev_attr_max_ah.attr,
709         NULL
710 };
711
712 static struct attribute_group ehca_dev_attr_grp = {
713         .attrs = ehca_dev_attrs
714 };
715
716 static int ehca_probe(struct platform_device *dev)
717 {
718         struct ehca_shca *shca;
719         const u64 *handle;
720         struct ib_pd *ibpd;
721         int ret, i, eq_size;
722         unsigned long flags;
723
724         handle = of_get_property(dev->dev.of_node, "ibm,hca-handle", NULL);
725         if (!handle) {
726                 ehca_gen_err("Cannot get eHCA handle for adapter: %s.",
727                              dev->dev.of_node->full_name);
728                 return -ENODEV;
729         }
730
731         if (!(*handle)) {
732                 ehca_gen_err("Wrong eHCA handle for adapter: %s.",
733                              dev->dev.of_node->full_name);
734                 return -ENODEV;
735         }
736
737         shca = (struct ehca_shca *)ib_alloc_device(sizeof(*shca));
738         if (!shca) {
739                 ehca_gen_err("Cannot allocate shca memory.");
740                 return -ENOMEM;
741         }
742
743         mutex_init(&shca->modify_mutex);
744         atomic_set(&shca->num_cqs, 0);
745         atomic_set(&shca->num_qps, 0);
746         shca->max_num_qps = ehca_max_qp;
747         shca->max_num_cqs = ehca_max_cq;
748
749         for (i = 0; i < ARRAY_SIZE(shca->sport); i++)
750                 spin_lock_init(&shca->sport[i].mod_sqp_lock);
751
752         shca->ofdev = dev;
753         shca->ipz_hca_handle.handle = *handle;
754         dev_set_drvdata(&dev->dev, shca);
755
756         ret = ehca_sense_attributes(shca);
757         if (ret < 0) {
758                 ehca_gen_err("Cannot sense eHCA attributes.");
759                 goto probe1;
760         }
761
762         ret = ehca_init_device(shca);
763         if (ret) {
764                 ehca_gen_err("Cannot init ehca  device struct");
765                 goto probe1;
766         }
767
768         eq_size = 2 * shca->max_num_cqs + 4 * shca->max_num_qps;
769         /* create event queues */
770         ret = ehca_create_eq(shca, &shca->eq, EHCA_EQ, eq_size);
771         if (ret) {
772                 ehca_err(&shca->ib_device, "Cannot create EQ.");
773                 goto probe1;
774         }
775
776         ret = ehca_create_eq(shca, &shca->neq, EHCA_NEQ, 513);
777         if (ret) {
778                 ehca_err(&shca->ib_device, "Cannot create NEQ.");
779                 goto probe3;
780         }
781
782         /* create internal protection domain */
783         ibpd = ehca_alloc_pd(&shca->ib_device, (void *)(-1), NULL);
784         if (IS_ERR(ibpd)) {
785                 ehca_err(&shca->ib_device, "Cannot create internal PD.");
786                 ret = PTR_ERR(ibpd);
787                 goto probe4;
788         }
789
790         shca->pd = container_of(ibpd, struct ehca_pd, ib_pd);
791         shca->pd->ib_pd.device = &shca->ib_device;
792
793         /* create internal max MR */
794         ret = ehca_reg_internal_maxmr(shca, shca->pd, &shca->maxmr);
795
796         if (ret) {
797                 ehca_err(&shca->ib_device, "Cannot create internal MR ret=%i",
798                          ret);
799                 goto probe5;
800         }
801
802         ret = ib_register_device(&shca->ib_device, NULL);
803         if (ret) {
804                 ehca_err(&shca->ib_device,
805                          "ib_register_device() failed ret=%i", ret);
806                 goto probe6;
807         }
808
809         /* create AQP1 for port 1 */
810         if (ehca_open_aqp1 == 1) {
811                 shca->sport[0].port_state = IB_PORT_DOWN;
812                 ret = ehca_create_aqp1(shca, 1);
813                 if (ret) {
814                         ehca_err(&shca->ib_device,
815                                  "Cannot create AQP1 for port 1.");
816                         goto probe7;
817                 }
818         }
819
820         /* create AQP1 for port 2 */
821         if ((ehca_open_aqp1 == 1) && (shca->num_ports == 2)) {
822                 shca->sport[1].port_state = IB_PORT_DOWN;
823                 ret = ehca_create_aqp1(shca, 2);
824                 if (ret) {
825                         ehca_err(&shca->ib_device,
826                                  "Cannot create AQP1 for port 2.");
827                         goto probe8;
828                 }
829         }
830
831         ret = sysfs_create_group(&dev->dev.kobj, &ehca_dev_attr_grp);
832         if (ret) /* only complain; we can live without attributes */
833                 ehca_err(&shca->ib_device,
834                          "Cannot create device attributes  ret=%d", ret);
835
836         spin_lock_irqsave(&shca_list_lock, flags);
837         list_add(&shca->shca_list, &shca_list);
838         spin_unlock_irqrestore(&shca_list_lock, flags);
839
840         return 0;
841
842 probe8:
843         ret = ehca_destroy_aqp1(&shca->sport[0]);
844         if (ret)
845                 ehca_err(&shca->ib_device,
846                          "Cannot destroy AQP1 for port 1. ret=%i", ret);
847
848 probe7:
849         ib_unregister_device(&shca->ib_device);
850
851 probe6:
852         ret = ehca_dereg_internal_maxmr(shca);
853         if (ret)
854                 ehca_err(&shca->ib_device,
855                          "Cannot destroy internal MR. ret=%x", ret);
856
857 probe5:
858         ret = ehca_dealloc_pd(&shca->pd->ib_pd);
859         if (ret)
860                 ehca_err(&shca->ib_device,
861                          "Cannot destroy internal PD. ret=%x", ret);
862
863 probe4:
864         ret = ehca_destroy_eq(shca, &shca->neq);
865         if (ret)
866                 ehca_err(&shca->ib_device,
867                          "Cannot destroy NEQ. ret=%x", ret);
868
869 probe3:
870         ret = ehca_destroy_eq(shca, &shca->eq);
871         if (ret)
872                 ehca_err(&shca->ib_device,
873                          "Cannot destroy EQ. ret=%x", ret);
874
875 probe1:
876         ib_dealloc_device(&shca->ib_device);
877
878         return -EINVAL;
879 }
880
881 static int ehca_remove(struct platform_device *dev)
882 {
883         struct ehca_shca *shca = dev_get_drvdata(&dev->dev);
884         unsigned long flags;
885         int ret;
886
887         sysfs_remove_group(&dev->dev.kobj, &ehca_dev_attr_grp);
888
889         if (ehca_open_aqp1 == 1) {
890                 int i;
891                 for (i = 0; i < shca->num_ports; i++) {
892                         ret = ehca_destroy_aqp1(&shca->sport[i]);
893                         if (ret)
894                                 ehca_err(&shca->ib_device,
895                                          "Cannot destroy AQP1 for port %x "
896                                          "ret=%i", ret, i);
897                 }
898         }
899
900         ib_unregister_device(&shca->ib_device);
901
902         ret = ehca_dereg_internal_maxmr(shca);
903         if (ret)
904                 ehca_err(&shca->ib_device,
905                          "Cannot destroy internal MR. ret=%i", ret);
906
907         ret = ehca_dealloc_pd(&shca->pd->ib_pd);
908         if (ret)
909                 ehca_err(&shca->ib_device,
910                          "Cannot destroy internal PD. ret=%i", ret);
911
912         ret = ehca_destroy_eq(shca, &shca->eq);
913         if (ret)
914                 ehca_err(&shca->ib_device, "Cannot destroy EQ. ret=%i", ret);
915
916         ret = ehca_destroy_eq(shca, &shca->neq);
917         if (ret)
918                 ehca_err(&shca->ib_device, "Canot destroy NEQ. ret=%i", ret);
919
920         ib_dealloc_device(&shca->ib_device);
921
922         spin_lock_irqsave(&shca_list_lock, flags);
923         list_del(&shca->shca_list);
924         spin_unlock_irqrestore(&shca_list_lock, flags);
925
926         return ret;
927 }
928
929 static struct of_device_id ehca_device_table[] =
930 {
931         {
932                 .name       = "lhca",
933                 .compatible = "IBM,lhca",
934         },
935         {},
936 };
937 MODULE_DEVICE_TABLE(of, ehca_device_table);
938
939 static struct platform_driver ehca_driver = {
940         .probe       = ehca_probe,
941         .remove      = ehca_remove,
942         .driver = {
943                 .name = "ehca",
944                 .owner = THIS_MODULE,
945                 .groups = ehca_drv_attr_groups,
946                 .of_match_table = ehca_device_table,
947         },
948 };
949
950 void ehca_poll_eqs(unsigned long data)
951 {
952         struct ehca_shca *shca;
953
954         spin_lock(&shca_list_lock);
955         list_for_each_entry(shca, &shca_list, shca_list) {
956                 if (shca->eq.is_initialized) {
957                         /* call deadman proc only if eq ptr does not change */
958                         struct ehca_eq *eq = &shca->eq;
959                         int max = 3;
960                         volatile u64 q_ofs, q_ofs2;
961                         unsigned long flags;
962                         spin_lock_irqsave(&eq->spinlock, flags);
963                         q_ofs = eq->ipz_queue.current_q_offset;
964                         spin_unlock_irqrestore(&eq->spinlock, flags);
965                         do {
966                                 spin_lock_irqsave(&eq->spinlock, flags);
967                                 q_ofs2 = eq->ipz_queue.current_q_offset;
968                                 spin_unlock_irqrestore(&eq->spinlock, flags);
969                                 max--;
970                         } while (q_ofs == q_ofs2 && max > 0);
971                         if (q_ofs == q_ofs2)
972                                 ehca_process_eq(shca, 0);
973                 }
974         }
975         mod_timer(&poll_eqs_timer, round_jiffies(jiffies + HZ));
976         spin_unlock(&shca_list_lock);
977 }
978
979 static int ehca_mem_notifier(struct notifier_block *nb,
980                              unsigned long action, void *data)
981 {
982         static unsigned long ehca_dmem_warn_time;
983         unsigned long flags;
984
985         switch (action) {
986         case MEM_CANCEL_OFFLINE:
987         case MEM_CANCEL_ONLINE:
988         case MEM_ONLINE:
989         case MEM_OFFLINE:
990                 return NOTIFY_OK;
991         case MEM_GOING_ONLINE:
992         case MEM_GOING_OFFLINE:
993                 /* only ok if no hca is attached to the lpar */
994                 spin_lock_irqsave(&shca_list_lock, flags);
995                 if (list_empty(&shca_list)) {
996                         spin_unlock_irqrestore(&shca_list_lock, flags);
997                         return NOTIFY_OK;
998                 } else {
999                         spin_unlock_irqrestore(&shca_list_lock, flags);
1000                         if (printk_timed_ratelimit(&ehca_dmem_warn_time,
1001                                                    30 * 1000))
1002                                 ehca_gen_err("DMEM operations are not allowed"
1003                                              "in conjunction with eHCA");
1004                         return NOTIFY_BAD;
1005                 }
1006         }
1007         return NOTIFY_OK;
1008 }
1009
1010 static struct notifier_block ehca_mem_nb = {
1011         .notifier_call = ehca_mem_notifier,
1012 };
1013
1014 static int __init ehca_module_init(void)
1015 {
1016         int ret;
1017
1018         printk(KERN_INFO "eHCA Infiniband Device Driver "
1019                "(Version " HCAD_VERSION ")\n");
1020
1021         ret = ehca_create_comp_pool();
1022         if (ret) {
1023                 ehca_gen_err("Cannot create comp pool.");
1024                 return ret;
1025         }
1026
1027         ret = ehca_create_slab_caches();
1028         if (ret) {
1029                 ehca_gen_err("Cannot create SLAB caches");
1030                 ret = -ENOMEM;
1031                 goto module_init1;
1032         }
1033
1034         ret = ehca_create_busmap();
1035         if (ret) {
1036                 ehca_gen_err("Cannot create busmap.");
1037                 goto module_init2;
1038         }
1039
1040         ret = ibmebus_register_driver(&ehca_driver);
1041         if (ret) {
1042                 ehca_gen_err("Cannot register eHCA device driver");
1043                 ret = -EINVAL;
1044                 goto module_init3;
1045         }
1046
1047         ret = register_memory_notifier(&ehca_mem_nb);
1048         if (ret) {
1049                 ehca_gen_err("Failed registering memory add/remove notifier");
1050                 goto module_init4;
1051         }
1052
1053         if (ehca_poll_all_eqs != 1) {
1054                 ehca_gen_err("WARNING!!!");
1055                 ehca_gen_err("It is possible to lose interrupts.");
1056         } else {
1057                 init_timer(&poll_eqs_timer);
1058                 poll_eqs_timer.function = ehca_poll_eqs;
1059                 poll_eqs_timer.expires = jiffies + HZ;
1060                 add_timer(&poll_eqs_timer);
1061         }
1062
1063         return 0;
1064
1065 module_init4:
1066         ibmebus_unregister_driver(&ehca_driver);
1067
1068 module_init3:
1069         ehca_destroy_busmap();
1070
1071 module_init2:
1072         ehca_destroy_slab_caches();
1073
1074 module_init1:
1075         ehca_destroy_comp_pool();
1076         return ret;
1077 };
1078
1079 static void __exit ehca_module_exit(void)
1080 {
1081         if (ehca_poll_all_eqs == 1)
1082                 del_timer_sync(&poll_eqs_timer);
1083
1084         ibmebus_unregister_driver(&ehca_driver);
1085
1086         unregister_memory_notifier(&ehca_mem_nb);
1087
1088         ehca_destroy_busmap();
1089
1090         ehca_destroy_slab_caches();
1091
1092         ehca_destroy_comp_pool();
1093
1094         idr_destroy(&ehca_cq_idr);
1095         idr_destroy(&ehca_qp_idr);
1096 };
1097
1098 module_init(ehca_module_init);
1099 module_exit(ehca_module_exit);