42c822f0ab3c050eb813be7b0f5a19b31ce1234f
[mirror/winof/.git] / hw / mthca / kernel / mthca_main.c
1 /*
2  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4  * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  *
34  * $Id: mthca_main.c 3056 2005-08-11 04:27:10Z roland $
35  */
36
37 #include "mthca_dev.h"
38 #if defined(EVENT_TRACING)
39 #ifdef offsetof
40 #undef offsetof
41 #endif
42 #include "mthca_main.tmh"
43 #endif
44 #include "mthca_config_reg.h"
45 #include "mthca_cmd.h"
46 #include "mthca_profile.h"
47 #include "mthca_memfree.h"
48
49 static const char mthca_version[] =
50         DRV_NAME ": HCA Driver v"
51         DRV_VERSION " (" DRV_RELDATE ")";
52
53 static struct mthca_profile default_profile = {
54         1 << 16,                // num_qp
55         4,                                      // rdb_per_qp
56         0,                              // num_srq
57         1 << 16,                // num_cq
58         1 << 13,                // num_mcg
59         1 << 17,                // num_mpt
60         1 << 20,                // num_mtt
61         1 << 15,                // num_udav (Tavor only)
62         0,                                      // num_uar
63         1 << 18,                // uarc_size (Arbel only)
64         1 << 18,                // fmr_reserved_mtts (Tavor only)
65 };
66
67 /* Types of supported HCA */
68 enum __hca_type {
69         TAVOR,                  /* MT23108                        */
70         ARBEL_COMPAT,           /* MT25208 in Tavor compat mode   */
71         ARBEL_NATIVE,           /* MT25218 with extended features */
72         SINAI,                  /* MT25204 */
73         LIVEFISH                /* a burning device */
74 };
75
76 #define MTHCA_FW_VER(major, minor, subminor) \
77         (((u64) (major) << 32) | ((u64) (minor) << 16) | (u64) (subminor))
78
79 static struct {
80         u64 max_unsupported_fw;
81         u64 min_supported_fw;
82         int is_memfree;
83         int is_pcie;
84 } mthca_hca_table[] = {
85         { MTHCA_FW_VER(3, 2, 0), MTHCA_FW_VER(3, 3, 3), 0, 0 }, /* TAVOR */
86         { MTHCA_FW_VER(4, 6, 0), MTHCA_FW_VER(4, 7, 0), 0, 1 }, /* ARBEL_COMPAT */
87         { MTHCA_FW_VER(5, 0, 0), MTHCA_FW_VER(5, 1, 300), 1, 1 },       /* ARBEL_NATIVE */
88         { MTHCA_FW_VER(1, 0, 0), MTHCA_FW_VER(1, 0, 1), 1, 1 }, /* SINAI */
89         { MTHCA_FW_VER(0, 0, 0), MTHCA_FW_VER(0, 0, 0), 0, 0 }          /* LIVEFISH */
90 };
91
92
93 #define HCA(v, d, t) \
94         { PCI_VENDOR_ID_##v,    PCI_DEVICE_ID_MELLANOX_##d, t }
95
96 static struct pci_device_id {
97         unsigned                vendor;
98         unsigned                device;
99         enum __hca_type driver_data;
100 } mthca_pci_table[] = {
101         HCA(MELLANOX, TAVOR,        TAVOR),
102         HCA(MELLANOX, ARBEL_COMPAT, ARBEL_COMPAT),
103         HCA(MELLANOX, ARBEL,        ARBEL_NATIVE),
104         HCA(MELLANOX, SINAI_OLD,    SINAI),
105         HCA(MELLANOX, SINAI,        SINAI),
106         HCA(TOPSPIN,  TAVOR,        TAVOR),
107         HCA(TOPSPIN,  ARBEL_COMPAT, TAVOR),
108         HCA(TOPSPIN,  ARBEL,        ARBEL_NATIVE),
109         HCA(TOPSPIN,  SINAI_OLD,    SINAI),
110         HCA(TOPSPIN,  SINAI,        SINAI),
111         // live fishes
112         HCA(MELLANOX, TAVOR_BD, LIVEFISH),
113         HCA(MELLANOX, ARBEL_BD,         LIVEFISH),
114         HCA(MELLANOX, SINAI_OLD_BD,     LIVEFISH),
115         HCA(MELLANOX, SINAI_BD,         LIVEFISH),
116         HCA(TOPSPIN, TAVOR_BD,          LIVEFISH),
117         HCA(TOPSPIN, ARBEL_BD,          LIVEFISH),
118         HCA(TOPSPIN, SINAI_OLD_BD,      LIVEFISH),
119         HCA(TOPSPIN, SINAI_BD,          LIVEFISH),
120 };
121 #define MTHCA_PCI_TABLE_SIZE (sizeof(mthca_pci_table)/sizeof(struct pci_device_id))
122
123 // wrapper to driver's hca_tune_pci
124 static NTSTATUS mthca_tune_pci(struct mthca_dev *mdev)
125 {
126         PDEVICE_OBJECT pdo = mdev->ext->cl_ext.p_self_do;
127         return hca_tune_pci(pdo, &mdev->uplink_info);
128 }
129
130 int mthca_get_dev_info(struct mthca_dev *mdev, __be64 *node_guid, u32 *hw_id)
131 {
132         struct ib_device_attr props;
133         struct ib_device *ib_dev = &mdev->ib_dev;
134         int err = (ib_dev->query_device )(ib_dev, &props );
135
136         if (err) {
137                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("can't get guid - mthca_query_port() failed (%08X)\n", err ));
138                 return err;
139         }
140
141         //TODO: do we need to convert GUID to LE by cl_ntoh64(x) ?
142         *node_guid = ib_dev->node_guid;
143         *hw_id = props.hw_ver;
144         return 0;
145 }
146
147 static struct pci_device_id * mthca_find_pci_dev(unsigned ven_id, unsigned dev_id)
148 {
149         struct pci_device_id *p_id = mthca_pci_table;
150         int i;
151
152         // find p_id (appropriate line in mthca_pci_table)
153         for (i = 0; i < MTHCA_PCI_TABLE_SIZE; ++i, ++p_id) {
154                 if (p_id->device == dev_id && p_id->vendor ==  ven_id)
155                         return p_id;
156         }
157         return NULL;
158 }
159
160
161 static int  mthca_dev_lim(struct mthca_dev *mdev, struct mthca_dev_lim *dev_lim)
162 {
163         int err;
164         u8 status;
165
166         err = mthca_QUERY_DEV_LIM(mdev, dev_lim, &status);
167         if (err) {
168                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("QUERY_DEV_LIM command failed, aborting.\n"));
169                 return err;
170         }
171         if (status) {
172                 HCA_PRINT_EV(TRACE_LEVEL_ERROR ,HCA_DBG_LOW ,("QUERY_DEV_LIM returned status 0x%02x, "
173                           "aborting.\n", status));
174                 return -EINVAL;
175         }
176         if (dev_lim->min_page_sz > PAGE_SIZE) {
177                 HCA_PRINT_EV(TRACE_LEVEL_ERROR ,HCA_DBG_LOW ,("HCA minimum page size of %d bigger than "
178                           "kernel PAGE_SIZE of %ld, aborting.\n",
179                           dev_lim->min_page_sz, PAGE_SIZE));
180                 return -ENODEV;
181         }
182         if (dev_lim->num_ports > MTHCA_MAX_PORTS) {
183                 HCA_PRINT_EV(TRACE_LEVEL_ERROR ,HCA_DBG_LOW ,("HCA has %d ports, but we only support %d, "
184                           "aborting.\n",
185                           dev_lim->num_ports, MTHCA_MAX_PORTS));
186                 return -ENODEV;
187         }
188
189         if (dev_lim->uar_size > (int)pci_resource_len(mdev, HCA_BAR_TYPE_UAR)) {
190                 HCA_PRINT_EV(TRACE_LEVEL_ERROR ,HCA_DBG_LOW , ("HCA reported UAR size of 0x%x bigger than "
191                           "Bar%d size of 0x%lx, aborting.\n",
192                           dev_lim->uar_size, HCA_BAR_TYPE_UAR, 
193                           (unsigned long)pci_resource_len(mdev, HCA_BAR_TYPE_UAR)));
194                 return -ENODEV;
195         }
196         
197
198         mdev->limits.num_ports          = dev_lim->num_ports;
199         mdev->limits.vl_cap             = dev_lim->max_vl;
200         mdev->limits.mtu_cap            = dev_lim->max_mtu;
201         mdev->limits.gid_table_len      = dev_lim->max_gids;
202         mdev->limits.pkey_table_len     = dev_lim->max_pkeys;
203         mdev->limits.local_ca_ack_delay = dev_lim->local_ca_ack_delay;
204         mdev->limits.max_sg             = dev_lim->max_sg;
205         mdev->limits.max_wqes           = dev_lim->max_qp_sz;
206         mdev->limits.max_qp_init_rdma   = dev_lim->max_requester_per_qp;
207         mdev->limits.reserved_qps       = dev_lim->reserved_qps;
208         mdev->limits.max_srq_wqes       = dev_lim->max_srq_sz;
209         mdev->limits.reserved_srqs      = dev_lim->reserved_srqs;
210         mdev->limits.reserved_eecs      = dev_lim->reserved_eecs;
211         mdev->limits.max_desc_sz      = dev_lim->max_desc_sz;
212         /*
213          * Subtract 1 from the limit because we need to allocate a
214          * spare CQE so the HCA HW can tell the difference between an
215          * empty CQ and a full CQ.
216          */
217         mdev->limits.max_cqes           = dev_lim->max_cq_sz - 1;
218         mdev->limits.reserved_cqs       = dev_lim->reserved_cqs;
219         mdev->limits.reserved_eqs       = dev_lim->reserved_eqs;
220         mdev->limits.reserved_mtts      = dev_lim->reserved_mtts;
221         mdev->limits.reserved_mrws      = dev_lim->reserved_mrws;
222         mdev->limits.reserved_uars      = dev_lim->reserved_uars;
223         mdev->limits.reserved_pds       = dev_lim->reserved_pds;
224         mdev->limits.port_width_cap     = (u8)dev_lim->max_port_width;
225         mdev->limits.page_size_cap      = !(u32)(dev_lim->min_page_sz - 1);
226         mdev->limits.flags                              = dev_lim->flags;
227
228         /* IB_DEVICE_RESIZE_MAX_WR not supported by driver.
229            May be doable since hardware supports it for SRQ.
230
231            IB_DEVICE_N_NOTIFY_CQ is supported by hardware but not by driver.
232
233            IB_DEVICE_SRQ_RESIZE is supported by hardware but SRQ is not
234            supported by driver. */
235         mdev->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
236                 IB_DEVICE_PORT_ACTIVE_EVENT |
237                 IB_DEVICE_SYS_IMAGE_GUID |
238                 IB_DEVICE_RC_RNR_NAK_GEN;
239
240         if (dev_lim->flags & DEV_LIM_FLAG_BAD_PKEY_CNTR)
241                 mdev->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
242
243         if (dev_lim->flags & DEV_LIM_FLAG_BAD_QKEY_CNTR)
244                 mdev->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
245
246         if (dev_lim->flags & DEV_LIM_FLAG_RAW_MULTI)
247                 mdev->device_cap_flags |= IB_DEVICE_RAW_MULTI;
248
249         if (dev_lim->flags & DEV_LIM_FLAG_AUTO_PATH_MIG)
250                 mdev->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
251
252         if (dev_lim->flags & DEV_LIM_FLAG_UD_AV_PORT_ENFORCE)
253                 mdev->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
254
255         if (dev_lim->flags & DEV_LIM_FLAG_SRQ)
256                 mdev->mthca_flags |= MTHCA_FLAG_SRQ;
257
258         return 0;
259 }
260
261 static int  mthca_init_tavor(struct mthca_dev *mdev)
262 {
263         u8 status;
264         int err;
265         struct mthca_dev_lim        dev_lim;
266         struct mthca_profile        profile;
267         struct mthca_init_hca_param init_hca;
268
269         err = mthca_SYS_EN(mdev, &status);
270         if (err) {
271                 HCA_PRINT_EV(TRACE_LEVEL_ERROR ,HCA_DBG_LOW ,("SYS_EN command failed, aborting.\n"));
272                 return err;
273         }
274         if (status) {
275                 HCA_PRINT_EV(TRACE_LEVEL_ERROR ,HCA_DBG_LOW ,("SYS_EN returned status 0x%02x, "
276                           "aborting.\n", status));
277                 return -EINVAL;
278         }
279
280         err = mthca_QUERY_FW(mdev, &status);
281         if (err) {
282                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("QUERY_FW command failed, aborting.\n"));
283                 goto err_disable;
284         }
285         if (status) {
286                 HCA_PRINT_EV(TRACE_LEVEL_ERROR ,HCA_DBG_LOW ,("QUERY_FW returned status 0x%02x, "
287                           "aborting.\n", status));
288                 err = -EINVAL;
289                 goto err_disable;
290         }
291         err = mthca_QUERY_DDR(mdev, &status);
292         if (err) {
293                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("QUERY_DDR command failed, aborting.\n"));
294                 goto err_disable;
295         }
296         if (status) {
297                 HCA_PRINT_EV(TRACE_LEVEL_ERROR ,HCA_DBG_LOW ,( "QUERY_DDR returned status 0x%02x, "
298                           "aborting.\n", status));
299                 err = -EINVAL;
300                 goto err_disable;
301         }
302
303         err = mthca_dev_lim(mdev, &dev_lim);
304         if (err) {
305                 HCA_PRINT_EV(TRACE_LEVEL_ERROR ,HCA_DBG_LOW ,( "QUERY_DEV_LIM command failed, aborting.\n"));
306                 goto err_disable;
307         }
308
309         profile = default_profile;
310         profile.num_uar   = dev_lim.uar_size / PAGE_SIZE;
311         profile.uarc_size = 0;
312         if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
313                 profile.num_srq = dev_lim.max_srqs;
314
315         err = (int)mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
316         if (err < 0)
317                 goto err_disable;
318
319         err = (int)mthca_INIT_HCA(mdev, &init_hca, &status);
320         if (err) {
321                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("INIT_HCA command failed, aborting.\n"));
322                 goto err_disable;
323         }
324         if (status) {
325                 HCA_PRINT_EV(TRACE_LEVEL_ERROR ,HCA_DBG_LOW ,("INIT_HCA returned status 0x%02x, "
326                           "aborting.\n", status));
327                 err = -EINVAL;
328                 goto err_disable;
329         }
330
331         return 0;
332
333 err_disable:
334         mthca_SYS_DIS(mdev, &status);
335
336         return err;
337 }
338
339 static int  mthca_load_fw(struct mthca_dev *mdev)
340 {
341         u8 status;
342         int err;
343
344         /* FIXME: use HCA-attached memory for FW if present */
345
346         mdev->fw.arbel.fw_icm =
347                 mthca_alloc_icm(mdev, mdev->fw.arbel.fw_pages,
348                                 GFP_HIGHUSER | __GFP_NOWARN);
349         if (!mdev->fw.arbel.fw_icm) {
350                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("Couldn't allocate FW area, aborting.\n"));
351                 return -ENOMEM;
352         }
353
354         err = mthca_MAP_FA(mdev, mdev->fw.arbel.fw_icm, &status);
355         if (err) {
356                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("MAP_FA command failed, aborting.\n"));
357                 goto err_free;
358         }
359         if (status) {
360                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("MAP_FA returned status 0x%02x, aborting.\n", status));
361                 err = -EINVAL;
362                 goto err_free;
363         }
364         err = mthca_RUN_FW(mdev, &status);
365         if (err) {
366                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("RUN_FW command failed, aborting.\n"));
367                 goto err_unmap_fa;
368         }
369         if (status) {
370                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("RUN_FW returned status 0x%02x, aborting.\n", status));
371                 err = -EINVAL;
372                 goto err_unmap_fa;
373         }
374
375         return 0;
376
377 err_unmap_fa:
378         mthca_UNMAP_FA(mdev, &status);
379
380 err_free:
381         mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
382         return err;
383 }
384
385 static int  mthca_init_icm(struct mthca_dev *mdev,
386                                     struct mthca_dev_lim *dev_lim,
387                                     struct mthca_init_hca_param *init_hca,
388                                     u64 icm_size)
389 {
390         u64 aux_pages;
391         u8 status;
392         int err;
393
394         err = mthca_SET_ICM_SIZE(mdev, icm_size, &aux_pages, &status);
395         if (err) {
396                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("SET_ICM_SIZE command failed, aborting.\n"));
397                 return err;
398         }
399         if (status) {
400                 HCA_PRINT_EV(TRACE_LEVEL_ERROR ,HCA_DBG_LOW ,("SET_ICM_SIZE returned status 0x%02x, "
401                           "aborting.\n", status));
402                 return -EINVAL;
403         }
404
405         HCA_PRINT(TRACE_LEVEL_INFORMATION ,HCA_DBG_LOW , ("%I64d KB of HCA context requires %I64d KB aux memory.\n",
406                   (u64) icm_size >> 10,
407                   (u64) aux_pages << 2));
408
409         mdev->fw.arbel.aux_icm = mthca_alloc_icm(mdev, (int)aux_pages,
410                                                  GFP_HIGHUSER | __GFP_NOWARN);
411         if (!mdev->fw.arbel.aux_icm) {
412                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("Couldn't allocate aux memory, aborting.\n"));
413                 return -ENOMEM;
414         }
415
416         err = mthca_MAP_ICM_AUX(mdev, mdev->fw.arbel.aux_icm, &status);
417         if (err) {
418                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("MAP_ICM_AUX command failed, aborting.\n"));
419                 goto err_free_aux;
420         }
421         if (status) {
422                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("MAP_ICM_AUX returned status 0x%02x, aborting.\n", status));
423                 err = -EINVAL;
424                 goto err_free_aux;
425         }
426
427         err = mthca_map_eq_icm(mdev, init_hca->eqc_base);
428         if (err) {
429                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("Failed to map EQ context memory, aborting.\n"));
430                 goto err_unmap_aux;
431         }
432
433         mdev->mr_table.mtt_table = mthca_alloc_icm_table(mdev, init_hca->mtt_base,
434                                                          MTHCA_MTT_SEG_SIZE,
435                                                          mdev->limits.num_mtt_segs,
436                                                          mdev->limits.reserved_mtts, 1);
437         if (!mdev->mr_table.mtt_table) {
438                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("Failed to map MTT context memory, aborting.\n"));
439                 err = -ENOMEM;
440                 goto err_unmap_eq;
441         }
442
443         mdev->mr_table.mpt_table = mthca_alloc_icm_table(mdev, init_hca->mpt_base,
444                                                          dev_lim->mpt_entry_sz,
445                                                          mdev->limits.num_mpts,
446                                                          mdev->limits.reserved_mrws, 1);
447         if (!mdev->mr_table.mpt_table) {
448                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("Failed to map MPT context memory, aborting.\n"));
449                 err = -ENOMEM;
450                 goto err_unmap_mtt;
451         }
452
453         mdev->qp_table.qp_table = mthca_alloc_icm_table(mdev, init_hca->qpc_base,
454                                                         dev_lim->qpc_entry_sz,
455                                                         mdev->limits.num_qps,
456                                                         mdev->limits.reserved_qps, 0);
457         if (!mdev->qp_table.qp_table) {
458                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("Failed to map QP context memory, aborting.\n"));
459                 err = -ENOMEM;
460                 goto err_unmap_mpt;
461         }
462
463         mdev->qp_table.eqp_table = mthca_alloc_icm_table(mdev, init_hca->eqpc_base,
464                                                          dev_lim->eqpc_entry_sz,
465                                                          mdev->limits.num_qps,
466                                                          mdev->limits.reserved_qps, 0);
467         if (!mdev->qp_table.eqp_table) {
468                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("Failed to map EQP context memory, aborting.\n"));
469                 err = -ENOMEM;
470                 goto err_unmap_qp;
471         }
472
473         mdev->qp_table.rdb_table = mthca_alloc_icm_table(mdev, init_hca->rdb_base,
474                                                          MTHCA_RDB_ENTRY_SIZE,
475                                                          mdev->limits.num_qps <<
476                                                          mdev->qp_table.rdb_shift,
477                                                          0, 0);
478         if (!mdev->qp_table.rdb_table) {
479                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("Failed to map RDB context memory, aborting\n"));
480                 err = -ENOMEM;
481                 goto err_unmap_eqp;
482         }
483
484        mdev->cq_table.table = mthca_alloc_icm_table(mdev, init_hca->cqc_base,
485                                                     dev_lim->cqc_entry_sz,
486                                                     mdev->limits.num_cqs,
487                                                     mdev->limits.reserved_cqs, 0);
488         if (!mdev->cq_table.table) {
489                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("Failed to map CQ context memory, aborting.\n"));
490                 err = -ENOMEM;
491                 goto err_unmap_rdb;
492         }
493
494         if (mdev->mthca_flags & MTHCA_FLAG_SRQ) {
495                 mdev->srq_table.table =
496                         mthca_alloc_icm_table(mdev, init_hca->srqc_base,
497                                               dev_lim->srq_entry_sz,
498                                               mdev->limits.num_srqs,
499                                               mdev->limits.reserved_srqs, 0);
500                 if (!mdev->srq_table.table) {
501                         HCA_PRINT_EV(TRACE_LEVEL_ERROR ,HCA_DBG_LOW ,("Failed to map SRQ context memory, "
502                                   "aborting.\n"));
503                         err = -ENOMEM;
504                         goto err_unmap_cq;
505                 }
506         }
507
508         /*
509          * It's not strictly required, but for simplicity just map the
510          * whole multicast group table now.  The table isn't very big
511          * and it's a lot easier than trying to track ref counts.
512          */
513         mdev->mcg_table.table = mthca_alloc_icm_table(mdev, init_hca->mc_base,
514                                                       MTHCA_MGM_ENTRY_SIZE,
515                                                       mdev->limits.num_mgms +
516                                                       mdev->limits.num_amgms,
517                                                       mdev->limits.num_mgms +
518                                                       mdev->limits.num_amgms,
519                                                       0);
520         if (!mdev->mcg_table.table) {
521                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("Failed to map MCG context memory, aborting.\n"));
522                 err = -ENOMEM;
523                 goto err_unmap_srq;
524         }
525
526         return 0;
527
528 err_unmap_srq:
529         if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
530                 mthca_free_icm_table(mdev, mdev->srq_table.table);
531
532 err_unmap_cq:
533         mthca_free_icm_table(mdev, mdev->cq_table.table);
534
535 err_unmap_rdb:
536         mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
537
538 err_unmap_eqp:
539         mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
540
541 err_unmap_qp:
542         mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
543
544 err_unmap_mpt:
545         mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
546
547 err_unmap_mtt:
548         mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
549
550 err_unmap_eq:
551         mthca_unmap_eq_icm(mdev);
552
553 err_unmap_aux:
554         mthca_UNMAP_ICM_AUX(mdev, &status);
555
556 err_free_aux:
557         mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
558
559         return err;
560 }
561
562 static int  mthca_init_arbel(struct mthca_dev *mdev)
563 {
564         struct mthca_dev_lim        dev_lim;
565         struct mthca_profile        profile;
566         struct mthca_init_hca_param init_hca;
567         u64 icm_size;
568         u8 status;
569         int err;
570
571         err = mthca_QUERY_FW(mdev, &status);
572         if (err) {
573                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("QUERY_FW command failed, aborting.\n"));
574                 return err;
575         }
576         if (status) {
577                 HCA_PRINT_EV(TRACE_LEVEL_ERROR ,HCA_DBG_LOW ,("QUERY_FW returned status 0x%02x, "
578                           "aborting.\n", status));
579                 return -EINVAL;
580         }
581
582         err = mthca_ENABLE_LAM(mdev, &status);
583         if (err) {
584                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("ENABLE_LAM command failed, aborting.\n"));
585                 return err;
586         }
587         if (status == MTHCA_CMD_STAT_LAM_NOT_PRE) {
588                 HCA_PRINT(TRACE_LEVEL_INFORMATION   ,HCA_DBG_LOW   ,("No HCA-attached memory (running in MemFree mode)\n"));
589                 mdev->mthca_flags |= MTHCA_FLAG_NO_LAM;
590         } else if (status) {
591                 HCA_PRINT_EV(TRACE_LEVEL_ERROR ,HCA_DBG_LOW ,("ENABLE_LAM returned status 0x%02x, "
592                           "aborting.\n", status));
593                 return -EINVAL;
594         }
595
596         err = mthca_load_fw(mdev);
597         if (err) {
598                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("Failed to start FW, aborting.\n"));
599                 goto err_disable;
600         }
601
602         err = mthca_dev_lim(mdev, &dev_lim);
603         if (err) {
604                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("QUERY_DEV_LIM command failed, aborting.\n"));
605                 goto err_stop_fw;
606         }
607
608         profile = default_profile;
609         profile.num_uar  = dev_lim.uar_size / PAGE_SIZE;
610         profile.num_udav = 0;
611         if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
612                 profile.num_srq = dev_lim.max_srqs;
613
614         icm_size = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
615         if ((int) icm_size < 0) {
616                 err = (int)icm_size;
617                 goto err_stop_fw;
618         }
619
620         err = mthca_init_icm(mdev, &dev_lim, &init_hca, icm_size);
621         if (err)
622                 goto err_stop_fw;
623
624         err = mthca_INIT_HCA(mdev, &init_hca, &status);
625         if (err) {
626                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("INIT_HCA command failed, aborting.\n"));
627                 goto err_free_icm;
628         }
629         if (status) {
630                 HCA_PRINT_EV(TRACE_LEVEL_ERROR ,HCA_DBG_LOW ,("INIT_HCA returned status 0x%02x, "
631                           "aborting.\n", status));
632                 err = -EINVAL;
633                 goto err_free_icm;
634         }
635
636         return 0;
637
638 err_free_icm:
639         if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
640                 mthca_free_icm_table(mdev, mdev->srq_table.table);
641         mthca_free_icm_table(mdev, mdev->cq_table.table);
642         mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
643         mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
644         mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
645         mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
646         mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
647         mthca_unmap_eq_icm(mdev);
648
649         mthca_UNMAP_ICM_AUX(mdev, &status);
650         mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
651
652 err_stop_fw:
653         mthca_UNMAP_FA(mdev, &status);
654         mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
655
656 err_disable:
657         if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
658                 mthca_DISABLE_LAM(mdev, &status);
659
660         return err;
661 }
662
663 static void mthca_close_hca(struct mthca_dev *mdev)
664 {
665         u8 status;
666
667         mthca_CLOSE_HCA(mdev, 0, &status);
668
669         if (mthca_is_memfree(mdev)) {
670                 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
671                         mthca_free_icm_table(mdev, mdev->srq_table.table);
672                 mthca_free_icm_table(mdev, mdev->cq_table.table);
673                 mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
674                 mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
675                 mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
676                 mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
677                 mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
678                 mthca_free_icm_table(mdev, mdev->mcg_table.table);
679                 mthca_unmap_eq_icm(mdev);
680
681                 mthca_UNMAP_ICM_AUX(mdev, &status);
682                 mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
683
684                 mthca_UNMAP_FA(mdev, &status);
685                 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
686
687                 if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
688                         mthca_DISABLE_LAM(mdev, &status);
689         } else
690                 mthca_SYS_DIS(mdev, &status);
691 }
692
693 static int  mthca_init_hca(struct mthca_dev *mdev)
694 {
695         u8 status;
696         int err;
697         struct mthca_adapter adapter;
698
699         if (mthca_is_memfree(mdev))
700                 err = mthca_init_arbel(mdev);
701         else
702                 err = mthca_init_tavor(mdev);
703
704         if (err)
705                 return err;
706
707         err = mthca_QUERY_ADAPTER(mdev, &adapter, &status);
708         if (err) {
709                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("QUERY_ADAPTER command failed, aborting.\n"));
710                 goto err_close;
711         }
712         if (status) {
713                 HCA_PRINT_EV(TRACE_LEVEL_ERROR ,HCA_DBG_LOW ,("QUERY_ADAPTER returned status 0x%02x, "
714                           "aborting.\n", status));
715                 err = -EINVAL;
716                 goto err_close;
717         }
718
719         mdev->eq_table.inta_pin = adapter.inta_pin;
720         mdev->rev_id            = adapter.revision_id;
721         memcpy(mdev->board_id, adapter.board_id, sizeof mdev->board_id);
722
723         return 0;
724
725 err_close:
726         mthca_close_hca(mdev);
727         return err;
728 }
729
730 static int  mthca_setup_hca(struct mthca_dev *mdev)
731 {
732         int err;
733         u8 status;
734
735         MTHCA_INIT_DOORBELL_LOCK(&mdev->doorbell_lock);
736
737         err = mthca_init_uar_table(mdev);
738         if (err) {
739                 HCA_PRINT_EV(TRACE_LEVEL_ERROR,HCA_DBG_LOW,("Failed to initialize "
740                           "user access region table, aborting.\n"));
741                 return err;
742         }
743
744         err = mthca_uar_alloc(mdev, &mdev->driver_uar);
745         if (err) {
746                 HCA_PRINT_EV(TRACE_LEVEL_ERROR,HCA_DBG_LOW,("Failed to allocate driver access region, "
747                           "aborting.\n"));
748                 goto err_uar_table_free;
749         }
750
751         mdev->kar = ioremap((io_addr_t)mdev->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE,&mdev->kar_size);
752         if (!mdev->kar) {
753                 HCA_PRINT_EV(TRACE_LEVEL_ERROR,HCA_DBG_LOW,("Couldn't map kernel access region, "
754                           "aborting.\n"));
755                 err = -ENOMEM;
756                 goto err_uar_free;
757         }
758
759         err = mthca_init_pd_table(mdev);
760         if (err) {
761                 HCA_PRINT_EV(TRACE_LEVEL_ERROR,HCA_DBG_LOW,("Failed to initialize "
762                           "protection domain table, aborting.\n"));
763                 goto err_kar_unmap;
764         }
765
766         err = mthca_init_mr_table(mdev);
767         if (err) {
768                 HCA_PRINT_EV(TRACE_LEVEL_ERROR,HCA_DBG_LOW,("Failed to initialize "
769                           "memory region table, aborting.\n"));
770                 goto err_pd_table_free;
771         }
772
773         err = mthca_pd_alloc(mdev, 1, &mdev->driver_pd);
774         if (err) {
775                 HCA_PRINT_EV(TRACE_LEVEL_ERROR,HCA_DBG_LOW,("Failed to create driver PD, "
776                           "aborting.\n"));
777                 goto err_mr_table_free;
778         }
779
780         err = mthca_init_eq_table(mdev);
781         if (err) {
782                 HCA_PRINT_EV(TRACE_LEVEL_ERROR,HCA_DBG_LOW, ("Failed to initialize "
783                           "event queue table, aborting.\n"));
784                 goto err_pd_free;
785         }
786
787         err = mthca_cmd_use_events(mdev);
788         if (err) {
789                 HCA_PRINT_EV(TRACE_LEVEL_ERROR,HCA_DBG_LOW,("Failed to switch to event-driven "
790                           "firmware commands, aborting.\n"));
791                 goto err_eq_table_free;
792         }
793
794         err = mthca_NOP(mdev, &status);
795         if (err || status) {
796                 HCA_PRINT_EV(TRACE_LEVEL_ERROR  ,HCA_DBG_LOW  ,("NOP command failed to generate interrupt, aborting.\n"));
797                 if (mdev->mthca_flags & (MTHCA_FLAG_MSI | MTHCA_FLAG_MSI_X)){
798                         HCA_PRINT_EV(TRACE_LEVEL_ERROR  ,HCA_DBG_LOW  ,("Try again with MSI/MSI-X disabled.\n"));
799                 }else{
800                         HCA_PRINT_EV(TRACE_LEVEL_ERROR  ,HCA_DBG_LOW  ,("BIOS or ACPI interrupt routing problem?\n"));
801                 }
802
803                 goto err_cmd_poll;
804         }
805
806         HCA_PRINT(TRACE_LEVEL_VERBOSE  ,HCA_DBG_LOW  ,("NOP command IRQ test passed\n"));
807
808         err = mthca_init_cq_table(mdev);
809         if (err) {
810                 HCA_PRINT_EV(TRACE_LEVEL_ERROR,HCA_DBG_LOW,("Failed to initialize "
811                           "completion queue table, aborting.\n"));
812                 goto err_cmd_poll;
813         }
814
815         err = mthca_init_srq_table(mdev);
816         if (err) {
817                 HCA_PRINT_EV(TRACE_LEVEL_ERROR,HCA_DBG_LOW,("Failed to initialize "
818                           "shared receive queue table, aborting.\n"));
819                 goto err_cq_table_free;
820         }
821
822         err = mthca_init_qp_table(mdev);
823         if (err) {
824                 HCA_PRINT_EV(TRACE_LEVEL_ERROR,HCA_DBG_LOW, ("Failed to initialize "
825                           "queue pair table, aborting.\n"));
826                 goto err_srq_table_free;
827         }
828
829         err = mthca_init_av_table(mdev);
830         if (err) {
831                 HCA_PRINT_EV(TRACE_LEVEL_ERROR,HCA_DBG_LOW,("Failed to initialize "
832                           "address vector table, aborting.\n"));
833                 goto err_qp_table_free;
834         }
835
836         err = mthca_init_mcg_table(mdev);
837         if (err) {
838                 HCA_PRINT_EV(TRACE_LEVEL_ERROR,HCA_DBG_LOW,("Failed to initialize "
839                           "multicast group table, aborting.\n"));
840                 goto err_av_table_free;
841         }
842
843         return 0;
844
845 err_av_table_free:
846         mthca_cleanup_av_table(mdev);
847
848 err_qp_table_free:
849         mthca_cleanup_qp_table(mdev);
850
851 err_srq_table_free:
852         mthca_cleanup_srq_table(mdev);
853
854 err_cq_table_free:
855         mthca_cleanup_cq_table(mdev);
856
857 err_cmd_poll:
858         mthca_cmd_use_polling(mdev);
859
860 err_eq_table_free:
861         mthca_cleanup_eq_table(mdev);
862
863 err_pd_free:
864         mthca_pd_free(mdev, &mdev->driver_pd);
865
866 err_mr_table_free:
867         mthca_cleanup_mr_table(mdev);
868
869 err_pd_table_free:
870         mthca_cleanup_pd_table(mdev);
871
872 err_kar_unmap:
873         iounmap(mdev->kar, mdev->kar_size);
874
875 err_uar_free:
876         mthca_uar_free(mdev, &mdev->driver_uar);
877
878 err_uar_table_free:
879         mthca_cleanup_uar_table(mdev);
880         return err;
881 }
882
883
884 static int      mthca_check_fw(struct mthca_dev *mdev, struct pci_device_id *p_id)
885 {
886         int err = 0;
887         
888         if (mdev->fw_ver <= mthca_hca_table[p_id->driver_data].max_unsupported_fw) {
889                 HCA_PRINT_EV(TRACE_LEVEL_ERROR ,HCA_DBG_LOW ,("HCA FW version %d.%d.%d is not supported. Use %d.%d.%d or higher.\n",
890                            (int) (mdev->fw_ver >> 32), (int) (mdev->fw_ver >> 16) & 0xffff,
891                            (int) (mdev->fw_ver & 0xffff),
892                            (int) (mthca_hca_table[p_id->driver_data].min_supported_fw >> 32),
893                            (int) (mthca_hca_table[p_id->driver_data].min_supported_fw >> 16) & 0xffff,
894                            (int) (mthca_hca_table[p_id->driver_data].min_supported_fw & 0xffff)));
895                 err = -EINVAL;
896         }
897         else 
898         if (mdev->fw_ver < mthca_hca_table[p_id->driver_data].min_supported_fw) {
899                 HCA_PRINT_EV(TRACE_LEVEL_WARNING ,HCA_DBG_LOW ,
900                         ("The HCA FW version is %d.%d.%d, which is not the latest one. \n"
901                         "If you meet any issues with the HCA please first try to upgrade the FW to version %d.%d.%d or higher.\n",
902                                  (int) (mdev->fw_ver >> 32), (int) (mdev->fw_ver >> 16) & 0xffff,
903                                  (int) (mdev->fw_ver & 0xffff),
904                                  (int) (mthca_hca_table[p_id->driver_data].min_supported_fw >> 32),
905                                  (int) (mthca_hca_table[p_id->driver_data].min_supported_fw >> 16) & 0xffff,
906                                  (int) (mthca_hca_table[p_id->driver_data].min_supported_fw & 0xffff)));
907         }
908         else {
909                 HCA_PRINT(TRACE_LEVEL_INFORMATION ,HCA_DBG_LOW ,("Current HCA FW version is %d.%d.%d. \n",
910                                  (int) (mdev->fw_ver >> 32), (int) (mdev->fw_ver >> 16) & 0xffff,
911                                  (int) (mdev->fw_ver & 0xffff)));
912         }
913
914         return err;
915 }
916
917 NTSTATUS mthca_init_one(hca_dev_ext_t *ext)
918 {
919         static int mthca_version_printed = 0;
920         int err;
921         NTSTATUS status;
922         struct mthca_dev *mdev;
923         struct pci_device_id *p_id;
924
925         /* print version */
926         if (!mthca_version_printed) {
927                 HCA_PRINT(TRACE_LEVEL_INFORMATION ,HCA_DBG_LOW ,("%s\n", mthca_version));
928                 ++mthca_version_printed;
929         }
930
931         /* find the type of device */
932         p_id = mthca_find_pci_dev(
933                 (unsigned)ext->hcaConfig.VendorID,
934                 (unsigned)ext->hcaConfig.DeviceID);
935         if (p_id == NULL) {
936                 status = STATUS_NO_SUCH_DEVICE;
937                 goto end;
938         }
939
940         /* allocate mdev structure */
941         mdev = kmalloc(sizeof *mdev, GFP_KERNEL);
942         if (!mdev) {
943                 // can't use HCA_PRINT_EV here !
944                 HCA_PRINT(TRACE_LEVEL_ERROR ,HCA_DBG_LOW ,("Device struct alloc failed, "
945                         "aborting.\n"));
946                 status = STATUS_INSUFFICIENT_RESOURCES;
947                 goto end;
948         }
949          
950         /* set some fields */
951         RtlZeroMemory(mdev, sizeof *mdev);
952         mdev->ext = ext;                /* pointer to DEVICE OBJECT extension */
953         mdev->hca_type = p_id->driver_data;
954         mdev->ib_dev.mdev = mdev;
955         if (p_id->driver_data == LIVEFISH)
956                 mdev->mthca_flags |= MTHCA_FLAG_LIVEFISH;
957         if (mthca_is_livefish(mdev))
958                 goto done;
959         if (ext->hca_hidden)
960                 mdev->mthca_flags |= MTHCA_FLAG_DDR_HIDDEN;
961         if (mthca_hca_table[p_id->driver_data].is_memfree)
962                 mdev->mthca_flags |= MTHCA_FLAG_MEMFREE;
963         if (mthca_hca_table[p_id->driver_data].is_pcie)
964                 mdev->mthca_flags |= MTHCA_FLAG_PCIE;
965         
966 //TODO: after we have a FW, capable of reset, 
967 // write a routine, that only presses the button
968
969         /*
970          * Now reset the HCA before we touch the PCI capabilities or
971          * attempt a firmware command, since a boot ROM may have left
972          * the HCA in an undefined state.
973          */
974         status = hca_reset( mdev->ext->cl_ext.p_self_do, p_id->driver_data == TAVOR );
975         if ( !NT_SUCCESS( status ) ) {
976                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("Failed to reset HCA, aborting.\n"));
977                 goto err_free_dev;
978         }
979
980         if (mthca_cmd_init(mdev)) {
981                 HCA_PRINT_EV(TRACE_LEVEL_ERROR   ,HCA_DBG_LOW   ,("Failed to init command interface, aborting.\n"));
982                 status = STATUS_DEVICE_DATA_ERROR;
983                 goto err_free_dev;
984         }
985
986         status = mthca_tune_pci(mdev);
987         if ( !NT_SUCCESS( status ) ) {
988                 goto err_cmd;
989         }
990
991         err = mthca_init_hca(mdev); 
992         if (err) {
993                 status = STATUS_UNSUCCESSFUL;
994                 goto err_cmd;
995         }
996
997         err = mthca_check_fw(mdev, p_id);
998         if (err) {
999                 status = STATUS_UNSUCCESSFUL;
1000                 goto err_close;
1001         }
1002
1003         err = mthca_setup_hca(mdev);
1004         if (err) {
1005                 status = STATUS_UNSUCCESSFUL;
1006                 goto err_close;
1007         }
1008
1009         err = mthca_register_device(mdev);
1010         if (err) {
1011                 status = STATUS_UNSUCCESSFUL;
1012                 goto err_cleanup;
1013         }
1014
1015         done:
1016         ext->hca.mdev = mdev;
1017         mdev->state = MTHCA_DEV_INITIALIZED;
1018         return 0;
1019
1020 err_cleanup:
1021         mthca_cleanup_mcg_table(mdev);
1022         mthca_cleanup_av_table(mdev);
1023         mthca_cleanup_qp_table(mdev);
1024         mthca_cleanup_srq_table(mdev);
1025         mthca_cleanup_cq_table(mdev);
1026         mthca_cmd_use_polling(mdev);
1027         mthca_cleanup_eq_table(mdev);
1028
1029         mthca_pd_free(mdev, &mdev->driver_pd);
1030
1031         mthca_cleanup_mr_table(mdev);
1032         mthca_cleanup_pd_table(mdev);
1033         mthca_cleanup_uar_table(mdev);
1034
1035 err_close:
1036         mthca_close_hca(mdev);
1037
1038 err_cmd:
1039         mthca_cmd_cleanup(mdev);
1040
1041 err_free_dev:
1042         kfree(mdev);
1043         
1044 end:
1045         return status;
1046 }
1047
1048 void mthca_remove_one(hca_dev_ext_t *ext)
1049 {
1050         struct mthca_dev *mdev = ext->hca.mdev;
1051         u8 status;
1052         int p;
1053
1054         ext->hca.mdev = NULL;
1055         if (mdev) {
1056                 mdev->state = MTHCA_DEV_UNINITIALIZED;
1057                 if (mthca_is_livefish(mdev))
1058                         goto done;
1059                 mthca_unregister_device(mdev);
1060
1061                 for (p = 1; p <= mdev->limits.num_ports; ++p)
1062                         mthca_CLOSE_IB(mdev, p, &status);
1063
1064                 mthca_cleanup_mcg_table(mdev);
1065                 mthca_cleanup_av_table(mdev);
1066                 mthca_cleanup_qp_table(mdev);
1067                 mthca_cleanup_srq_table(mdev);
1068                 mthca_cleanup_cq_table(mdev);
1069                 mthca_cmd_use_polling(mdev);
1070                 mthca_cleanup_eq_table(mdev);
1071                 mthca_pd_free(mdev, &mdev->driver_pd);
1072                 mthca_cleanup_mr_table(mdev);
1073                 mthca_cleanup_pd_table(mdev);
1074                 iounmap(mdev->kar, mdev->kar_size);
1075                 mthca_uar_free(mdev, &mdev->driver_uar);
1076                 mthca_cleanup_uar_table(mdev);
1077                 mthca_close_hca(mdev);
1078                 mthca_cmd_cleanup(mdev);
1079 done:
1080                 kfree(mdev);
1081         }
1082 }
1083
1084
1085