[IB] mthca: Fix memory leak on device close
[safe/jmp/linux-2.6] / drivers / infiniband / hw / mthca / 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 1396 2004-12-28 04:10:27Z roland $
35  */
36
37 #include <linux/config.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/errno.h>
41 #include <linux/pci.h>
42 #include <linux/interrupt.h>
43
44 #include "mthca_dev.h"
45 #include "mthca_config_reg.h"
46 #include "mthca_cmd.h"
47 #include "mthca_profile.h"
48 #include "mthca_memfree.h"
49
50 MODULE_AUTHOR("Roland Dreier");
51 MODULE_DESCRIPTION("Mellanox InfiniBand HCA low-level driver");
52 MODULE_LICENSE("Dual BSD/GPL");
53 MODULE_VERSION(DRV_VERSION);
54
55 #ifdef CONFIG_PCI_MSI
56
57 static int msi_x = 0;
58 module_param(msi_x, int, 0444);
59 MODULE_PARM_DESC(msi_x, "attempt to use MSI-X if nonzero");
60
61 static int msi = 0;
62 module_param(msi, int, 0444);
63 MODULE_PARM_DESC(msi, "attempt to use MSI if nonzero");
64
65 #else /* CONFIG_PCI_MSI */
66
67 #define msi_x (0)
68 #define msi   (0)
69
70 #endif /* CONFIG_PCI_MSI */
71
72 static const char mthca_version[] __devinitdata =
73         DRV_NAME ": Mellanox InfiniBand HCA driver v"
74         DRV_VERSION " (" DRV_RELDATE ")\n";
75
76 static struct mthca_profile default_profile = {
77         .num_qp            = 1 << 16,
78         .rdb_per_qp        = 4,
79         .num_cq            = 1 << 16,
80         .num_mcg           = 1 << 13,
81         .num_mpt           = 1 << 17,
82         .num_mtt           = 1 << 20,
83         .num_udav          = 1 << 15,   /* Tavor only */
84         .fmr_reserved_mtts = 1 << 18,   /* Tavor only */
85         .uarc_size         = 1 << 18,   /* Arbel only */
86 };
87
88 static int __devinit mthca_tune_pci(struct mthca_dev *mdev)
89 {
90         int cap;
91         u16 val;
92
93         /* First try to max out Read Byte Count */
94         cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_PCIX);
95         if (cap) {
96                 if (pci_read_config_word(mdev->pdev, cap + PCI_X_CMD, &val)) {
97                         mthca_err(mdev, "Couldn't read PCI-X command register, "
98                                   "aborting.\n");
99                         return -ENODEV;
100                 }
101                 val = (val & ~PCI_X_CMD_MAX_READ) | (3 << 2);
102                 if (pci_write_config_word(mdev->pdev, cap + PCI_X_CMD, val)) {
103                         mthca_err(mdev, "Couldn't write PCI-X command register, "
104                                   "aborting.\n");
105                         return -ENODEV;
106                 }
107         } else if (!(mdev->mthca_flags & MTHCA_FLAG_PCIE))
108                 mthca_info(mdev, "No PCI-X capability, not setting RBC.\n");
109
110         cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_EXP);
111         if (cap) {
112                 if (pci_read_config_word(mdev->pdev, cap + PCI_EXP_DEVCTL, &val)) {
113                         mthca_err(mdev, "Couldn't read PCI Express device control "
114                                   "register, aborting.\n");
115                         return -ENODEV;
116                 }
117                 val = (val & ~PCI_EXP_DEVCTL_READRQ) | (5 << 12);
118                 if (pci_write_config_word(mdev->pdev, cap + PCI_EXP_DEVCTL, val)) {
119                         mthca_err(mdev, "Couldn't write PCI Express device control "
120                                   "register, aborting.\n");
121                         return -ENODEV;
122                 }
123         } else if (mdev->mthca_flags & MTHCA_FLAG_PCIE)
124                 mthca_info(mdev, "No PCI Express capability, "
125                            "not setting Max Read Request Size.\n");
126
127         return 0;
128 }
129
130 static int __devinit mthca_dev_lim(struct mthca_dev *mdev, struct mthca_dev_lim *dev_lim)
131 {
132         int err;
133         u8 status;
134
135         err = mthca_QUERY_DEV_LIM(mdev, dev_lim, &status);
136         if (err) {
137                 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
138                 return err;
139         }
140         if (status) {
141                 mthca_err(mdev, "QUERY_DEV_LIM returned status 0x%02x, "
142                           "aborting.\n", status);
143                 return -EINVAL;
144         }
145         if (dev_lim->min_page_sz > PAGE_SIZE) {
146                 mthca_err(mdev, "HCA minimum page size of %d bigger than "
147                           "kernel PAGE_SIZE of %ld, aborting.\n",
148                           dev_lim->min_page_sz, PAGE_SIZE);
149                 return -ENODEV;
150         }
151         if (dev_lim->num_ports > MTHCA_MAX_PORTS) {
152                 mthca_err(mdev, "HCA has %d ports, but we only support %d, "
153                           "aborting.\n",
154                           dev_lim->num_ports, MTHCA_MAX_PORTS);
155                 return -ENODEV;
156         }
157
158         mdev->limits.num_ports          = dev_lim->num_ports;
159         mdev->limits.vl_cap             = dev_lim->max_vl;
160         mdev->limits.mtu_cap            = dev_lim->max_mtu;
161         mdev->limits.gid_table_len      = dev_lim->max_gids;
162         mdev->limits.pkey_table_len     = dev_lim->max_pkeys;
163         mdev->limits.local_ca_ack_delay = dev_lim->local_ca_ack_delay;
164         mdev->limits.max_sg             = dev_lim->max_sg;
165         mdev->limits.reserved_qps       = dev_lim->reserved_qps;
166         mdev->limits.reserved_srqs      = dev_lim->reserved_srqs;
167         mdev->limits.reserved_eecs      = dev_lim->reserved_eecs;
168         mdev->limits.reserved_cqs       = dev_lim->reserved_cqs;
169         mdev->limits.reserved_eqs       = dev_lim->reserved_eqs;
170         mdev->limits.reserved_mtts      = dev_lim->reserved_mtts;
171         mdev->limits.reserved_mrws      = dev_lim->reserved_mrws;
172         mdev->limits.reserved_uars      = dev_lim->reserved_uars;
173         mdev->limits.reserved_pds       = dev_lim->reserved_pds;
174         mdev->limits.port_width_cap     = dev_lim->max_port_width;
175
176         /* IB_DEVICE_RESIZE_MAX_WR not supported by driver.
177            May be doable since hardware supports it for SRQ.
178
179            IB_DEVICE_N_NOTIFY_CQ is supported by hardware but not by driver.
180
181            IB_DEVICE_SRQ_RESIZE is supported by hardware but SRQ is not
182            supported by driver. */
183         mdev->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
184                 IB_DEVICE_PORT_ACTIVE_EVENT |
185                 IB_DEVICE_SYS_IMAGE_GUID |
186                 IB_DEVICE_RC_RNR_NAK_GEN;
187
188         if (dev_lim->flags & DEV_LIM_FLAG_BAD_PKEY_CNTR)
189                 mdev->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
190
191         if (dev_lim->flags & DEV_LIM_FLAG_BAD_QKEY_CNTR)
192                 mdev->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
193
194         if (dev_lim->flags & DEV_LIM_FLAG_RAW_MULTI)
195                 mdev->device_cap_flags |= IB_DEVICE_RAW_MULTI;
196
197         if (dev_lim->flags & DEV_LIM_FLAG_AUTO_PATH_MIG)
198                 mdev->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
199
200         if (dev_lim->flags & DEV_LIM_FLAG_UD_AV_PORT_ENFORCE)
201                 mdev->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
202
203         if (dev_lim->flags & DEV_LIM_FLAG_SRQ)
204                 mdev->mthca_flags |= MTHCA_FLAG_SRQ;
205
206         return 0;
207 }
208
209 static int __devinit mthca_init_tavor(struct mthca_dev *mdev)
210 {
211         u8 status;
212         int err;
213         struct mthca_dev_lim        dev_lim;
214         struct mthca_profile        profile;
215         struct mthca_init_hca_param init_hca;
216
217         err = mthca_SYS_EN(mdev, &status);
218         if (err) {
219                 mthca_err(mdev, "SYS_EN command failed, aborting.\n");
220                 return err;
221         }
222         if (status) {
223                 mthca_err(mdev, "SYS_EN returned status 0x%02x, "
224                           "aborting.\n", status);
225                 return -EINVAL;
226         }
227
228         err = mthca_QUERY_FW(mdev, &status);
229         if (err) {
230                 mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
231                 goto err_disable;
232         }
233         if (status) {
234                 mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
235                           "aborting.\n", status);
236                 err = -EINVAL;
237                 goto err_disable;
238         }
239         err = mthca_QUERY_DDR(mdev, &status);
240         if (err) {
241                 mthca_err(mdev, "QUERY_DDR command failed, aborting.\n");
242                 goto err_disable;
243         }
244         if (status) {
245                 mthca_err(mdev, "QUERY_DDR returned status 0x%02x, "
246                           "aborting.\n", status);
247                 err = -EINVAL;
248                 goto err_disable;
249         }
250
251         err = mthca_dev_lim(mdev, &dev_lim);
252
253         profile = default_profile;
254         profile.num_uar   = dev_lim.uar_size / PAGE_SIZE;
255         profile.uarc_size = 0;
256         if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
257                 profile.num_srq = dev_lim.max_srqs;
258
259         err = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
260         if (err < 0)
261                 goto err_disable;
262
263         err = mthca_INIT_HCA(mdev, &init_hca, &status);
264         if (err) {
265                 mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
266                 goto err_disable;
267         }
268         if (status) {
269                 mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
270                           "aborting.\n", status);
271                 err = -EINVAL;
272                 goto err_disable;
273         }
274
275         return 0;
276
277 err_disable:
278         mthca_SYS_DIS(mdev, &status);
279
280         return err;
281 }
282
283 static int __devinit mthca_load_fw(struct mthca_dev *mdev)
284 {
285         u8 status;
286         int err;
287
288         /* FIXME: use HCA-attached memory for FW if present */
289
290         mdev->fw.arbel.fw_icm =
291                 mthca_alloc_icm(mdev, mdev->fw.arbel.fw_pages,
292                                 GFP_HIGHUSER | __GFP_NOWARN);
293         if (!mdev->fw.arbel.fw_icm) {
294                 mthca_err(mdev, "Couldn't allocate FW area, aborting.\n");
295                 return -ENOMEM;
296         }
297
298         err = mthca_MAP_FA(mdev, mdev->fw.arbel.fw_icm, &status);
299         if (err) {
300                 mthca_err(mdev, "MAP_FA command failed, aborting.\n");
301                 goto err_free;
302         }
303         if (status) {
304                 mthca_err(mdev, "MAP_FA returned status 0x%02x, aborting.\n", status);
305                 err = -EINVAL;
306                 goto err_free;
307         }
308         err = mthca_RUN_FW(mdev, &status);
309         if (err) {
310                 mthca_err(mdev, "RUN_FW command failed, aborting.\n");
311                 goto err_unmap_fa;
312         }
313         if (status) {
314                 mthca_err(mdev, "RUN_FW returned status 0x%02x, aborting.\n", status);
315                 err = -EINVAL;
316                 goto err_unmap_fa;
317         }
318
319         return 0;
320
321 err_unmap_fa:
322         mthca_UNMAP_FA(mdev, &status);
323
324 err_free:
325         mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
326         return err;
327 }
328
329 static int __devinit mthca_init_icm(struct mthca_dev *mdev,
330                                     struct mthca_dev_lim *dev_lim,
331                                     struct mthca_init_hca_param *init_hca,
332                                     u64 icm_size)
333 {
334         u64 aux_pages;
335         u8 status;
336         int err;
337
338         err = mthca_SET_ICM_SIZE(mdev, icm_size, &aux_pages, &status);
339         if (err) {
340                 mthca_err(mdev, "SET_ICM_SIZE command failed, aborting.\n");
341                 return err;
342         }
343         if (status) {
344                 mthca_err(mdev, "SET_ICM_SIZE returned status 0x%02x, "
345                           "aborting.\n", status);
346                 return -EINVAL;
347         }
348
349         mthca_dbg(mdev, "%lld KB of HCA context requires %lld KB aux memory.\n",
350                   (unsigned long long) icm_size >> 10,
351                   (unsigned long long) aux_pages << 2);
352
353         mdev->fw.arbel.aux_icm = mthca_alloc_icm(mdev, aux_pages,
354                                                  GFP_HIGHUSER | __GFP_NOWARN);
355         if (!mdev->fw.arbel.aux_icm) {
356                 mthca_err(mdev, "Couldn't allocate aux memory, aborting.\n");
357                 return -ENOMEM;
358         }
359
360         err = mthca_MAP_ICM_AUX(mdev, mdev->fw.arbel.aux_icm, &status);
361         if (err) {
362                 mthca_err(mdev, "MAP_ICM_AUX command failed, aborting.\n");
363                 goto err_free_aux;
364         }
365         if (status) {
366                 mthca_err(mdev, "MAP_ICM_AUX returned status 0x%02x, aborting.\n", status);
367                 err = -EINVAL;
368                 goto err_free_aux;
369         }
370
371         err = mthca_map_eq_icm(mdev, init_hca->eqc_base);
372         if (err) {
373                 mthca_err(mdev, "Failed to map EQ context memory, aborting.\n");
374                 goto err_unmap_aux;
375         }
376
377         mdev->mr_table.mtt_table = mthca_alloc_icm_table(mdev, init_hca->mtt_base,
378                                                          MTHCA_MTT_SEG_SIZE,
379                                                          mdev->limits.num_mtt_segs,
380                                                          mdev->limits.reserved_mtts, 1);
381         if (!mdev->mr_table.mtt_table) {
382                 mthca_err(mdev, "Failed to map MTT context memory, aborting.\n");
383                 err = -ENOMEM;
384                 goto err_unmap_eq;
385         }
386
387         mdev->mr_table.mpt_table = mthca_alloc_icm_table(mdev, init_hca->mpt_base,
388                                                          dev_lim->mpt_entry_sz,
389                                                          mdev->limits.num_mpts,
390                                                          mdev->limits.reserved_mrws, 1);
391         if (!mdev->mr_table.mpt_table) {
392                 mthca_err(mdev, "Failed to map MPT context memory, aborting.\n");
393                 err = -ENOMEM;
394                 goto err_unmap_mtt;
395         }
396
397         mdev->qp_table.qp_table = mthca_alloc_icm_table(mdev, init_hca->qpc_base,
398                                                         dev_lim->qpc_entry_sz,
399                                                         mdev->limits.num_qps,
400                                                         mdev->limits.reserved_qps, 0);
401         if (!mdev->qp_table.qp_table) {
402                 mthca_err(mdev, "Failed to map QP context memory, aborting.\n");
403                 err = -ENOMEM;
404                 goto err_unmap_mpt;
405         }
406
407         mdev->qp_table.eqp_table = mthca_alloc_icm_table(mdev, init_hca->eqpc_base,
408                                                          dev_lim->eqpc_entry_sz,
409                                                          mdev->limits.num_qps,
410                                                          mdev->limits.reserved_qps, 0);
411         if (!mdev->qp_table.eqp_table) {
412                 mthca_err(mdev, "Failed to map EQP context memory, aborting.\n");
413                 err = -ENOMEM;
414                 goto err_unmap_qp;
415         }
416
417         mdev->qp_table.rdb_table = mthca_alloc_icm_table(mdev, init_hca->rdb_base,
418                                                          MTHCA_RDB_ENTRY_SIZE,
419                                                          mdev->limits.num_qps <<
420                                                          mdev->qp_table.rdb_shift,
421                                                          0, 0);
422         if (!mdev->qp_table.rdb_table) {
423                 mthca_err(mdev, "Failed to map RDB context memory, aborting\n");
424                 err = -ENOMEM;
425                 goto err_unmap_eqp;
426         }
427
428        mdev->cq_table.table = mthca_alloc_icm_table(mdev, init_hca->cqc_base,
429                                                     dev_lim->cqc_entry_sz,
430                                                     mdev->limits.num_cqs,
431                                                     mdev->limits.reserved_cqs, 0);
432         if (!mdev->cq_table.table) {
433                 mthca_err(mdev, "Failed to map CQ context memory, aborting.\n");
434                 err = -ENOMEM;
435                 goto err_unmap_rdb;
436         }
437
438         if (mdev->mthca_flags & MTHCA_FLAG_SRQ) {
439                 mdev->srq_table.table =
440                         mthca_alloc_icm_table(mdev, init_hca->srqc_base,
441                                               dev_lim->srq_entry_sz,
442                                               mdev->limits.num_srqs,
443                                               mdev->limits.reserved_srqs, 0);
444                 if (!mdev->srq_table.table) {
445                         mthca_err(mdev, "Failed to map SRQ context memory, "
446                                   "aborting.\n");
447                         err = -ENOMEM;
448                         goto err_unmap_cq;
449                 }
450         }
451
452         /*
453          * It's not strictly required, but for simplicity just map the
454          * whole multicast group table now.  The table isn't very big
455          * and it's a lot easier than trying to track ref counts.
456          */
457         mdev->mcg_table.table = mthca_alloc_icm_table(mdev, init_hca->mc_base,
458                                                       MTHCA_MGM_ENTRY_SIZE,
459                                                       mdev->limits.num_mgms +
460                                                       mdev->limits.num_amgms,
461                                                       mdev->limits.num_mgms +
462                                                       mdev->limits.num_amgms,
463                                                       0);
464         if (!mdev->mcg_table.table) {
465                 mthca_err(mdev, "Failed to map MCG context memory, aborting.\n");
466                 err = -ENOMEM;
467                 goto err_unmap_srq;
468         }
469
470         return 0;
471
472 err_unmap_srq:
473         if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
474                 mthca_free_icm_table(mdev, mdev->srq_table.table);
475
476 err_unmap_cq:
477         mthca_free_icm_table(mdev, mdev->cq_table.table);
478
479 err_unmap_rdb:
480         mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
481
482 err_unmap_eqp:
483         mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
484
485 err_unmap_qp:
486         mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
487
488 err_unmap_mpt:
489         mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
490
491 err_unmap_mtt:
492         mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
493
494 err_unmap_eq:
495         mthca_unmap_eq_icm(mdev);
496
497 err_unmap_aux:
498         mthca_UNMAP_ICM_AUX(mdev, &status);
499
500 err_free_aux:
501         mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
502
503         return err;
504 }
505
506 static void mthca_free_icms(struct mthca_dev *mdev)
507 {
508         u8 status;
509
510         mthca_free_icm_table(mdev, mdev->mcg_table.table);
511         if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
512                 mthca_free_icm_table(mdev, mdev->srq_table.table);
513         mthca_free_icm_table(mdev, mdev->cq_table.table);
514         mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
515         mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
516         mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
517         mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
518         mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
519         mthca_unmap_eq_icm(mdev);
520
521         mthca_UNMAP_ICM_AUX(mdev, &status);
522         mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
523 }
524
525 static int __devinit mthca_init_arbel(struct mthca_dev *mdev)
526 {
527         struct mthca_dev_lim        dev_lim;
528         struct mthca_profile        profile;
529         struct mthca_init_hca_param init_hca;
530         u64 icm_size;
531         u8 status;
532         int err;
533
534         err = mthca_QUERY_FW(mdev, &status);
535         if (err) {
536                 mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
537                 return err;
538         }
539         if (status) {
540                 mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
541                           "aborting.\n", status);
542                 return -EINVAL;
543         }
544
545         err = mthca_ENABLE_LAM(mdev, &status);
546         if (err) {
547                 mthca_err(mdev, "ENABLE_LAM command failed, aborting.\n");
548                 return err;
549         }
550         if (status == MTHCA_CMD_STAT_LAM_NOT_PRE) {
551                 mthca_dbg(mdev, "No HCA-attached memory (running in MemFree mode)\n");
552                 mdev->mthca_flags |= MTHCA_FLAG_NO_LAM;
553         } else if (status) {
554                 mthca_err(mdev, "ENABLE_LAM returned status 0x%02x, "
555                           "aborting.\n", status);
556                 return -EINVAL;
557         }
558
559         err = mthca_load_fw(mdev);
560         if (err) {
561                 mthca_err(mdev, "Failed to start FW, aborting.\n");
562                 goto err_disable;
563         }
564
565         err = mthca_dev_lim(mdev, &dev_lim);
566         if (err) {
567                 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
568                 goto err_stop_fw;
569         }
570
571         profile = default_profile;
572         profile.num_uar  = dev_lim.uar_size / PAGE_SIZE;
573         profile.num_udav = 0;
574         if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
575                 profile.num_srq = dev_lim.max_srqs;
576
577         icm_size = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
578         if ((int) icm_size < 0) {
579                 err = icm_size;
580                 goto err_stop_fw;
581         }
582
583         err = mthca_init_icm(mdev, &dev_lim, &init_hca, icm_size);
584         if (err)
585                 goto err_stop_fw;
586
587         err = mthca_INIT_HCA(mdev, &init_hca, &status);
588         if (err) {
589                 mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
590                 goto err_free_icm;
591         }
592         if (status) {
593                 mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
594                           "aborting.\n", status);
595                 err = -EINVAL;
596                 goto err_free_icm;
597         }
598
599         return 0;
600
601 err_free_icm:
602         mthca_free_icms(mdev);
603
604 err_stop_fw:
605         mthca_UNMAP_FA(mdev, &status);
606         mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
607
608 err_disable:
609         if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
610                 mthca_DISABLE_LAM(mdev, &status);
611
612         return err;
613 }
614
615 static void mthca_close_hca(struct mthca_dev *mdev)
616 {
617         u8 status;
618
619         mthca_CLOSE_HCA(mdev, 0, &status);
620
621         if (mthca_is_memfree(mdev)) {
622                 mthca_free_icms(mdev);
623
624                 mthca_UNMAP_FA(mdev, &status);
625                 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
626
627                 if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
628                         mthca_DISABLE_LAM(mdev, &status);
629         } else
630                 mthca_SYS_DIS(mdev, &status);
631 }
632
633 static int __devinit mthca_init_hca(struct mthca_dev *mdev)
634 {
635         u8 status;
636         int err;
637         struct mthca_adapter adapter;
638
639         if (mthca_is_memfree(mdev))
640                 err = mthca_init_arbel(mdev);
641         else
642                 err = mthca_init_tavor(mdev);
643
644         if (err)
645                 return err;
646
647         err = mthca_QUERY_ADAPTER(mdev, &adapter, &status);
648         if (err) {
649                 mthca_err(mdev, "QUERY_ADAPTER command failed, aborting.\n");
650                 goto err_close;
651         }
652         if (status) {
653                 mthca_err(mdev, "QUERY_ADAPTER returned status 0x%02x, "
654                           "aborting.\n", status);
655                 err = -EINVAL;
656                 goto err_close;
657         }
658
659         mdev->eq_table.inta_pin = adapter.inta_pin;
660         mdev->rev_id            = adapter.revision_id;
661         memcpy(mdev->board_id, adapter.board_id, sizeof mdev->board_id);
662
663         return 0;
664
665 err_close:
666         mthca_close_hca(mdev);
667         return err;
668 }
669
670 static int __devinit mthca_setup_hca(struct mthca_dev *dev)
671 {
672         int err;
673         u8 status;
674
675         MTHCA_INIT_DOORBELL_LOCK(&dev->doorbell_lock);
676
677         err = mthca_init_uar_table(dev);
678         if (err) {
679                 mthca_err(dev, "Failed to initialize "
680                           "user access region table, aborting.\n");
681                 return err;
682         }
683
684         err = mthca_uar_alloc(dev, &dev->driver_uar);
685         if (err) {
686                 mthca_err(dev, "Failed to allocate driver access region, "
687                           "aborting.\n");
688                 goto err_uar_table_free;
689         }
690
691         dev->kar = ioremap(dev->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
692         if (!dev->kar) {
693                 mthca_err(dev, "Couldn't map kernel access region, "
694                           "aborting.\n");
695                 err = -ENOMEM;
696                 goto err_uar_free;
697         }
698
699         err = mthca_init_pd_table(dev);
700         if (err) {
701                 mthca_err(dev, "Failed to initialize "
702                           "protection domain table, aborting.\n");
703                 goto err_kar_unmap;
704         }
705
706         err = mthca_init_mr_table(dev);
707         if (err) {
708                 mthca_err(dev, "Failed to initialize "
709                           "memory region table, aborting.\n");
710                 goto err_pd_table_free;
711         }
712
713         err = mthca_pd_alloc(dev, 1, &dev->driver_pd);
714         if (err) {
715                 mthca_err(dev, "Failed to create driver PD, "
716                           "aborting.\n");
717                 goto err_mr_table_free;
718         }
719
720         err = mthca_init_eq_table(dev);
721         if (err) {
722                 mthca_err(dev, "Failed to initialize "
723                           "event queue table, aborting.\n");
724                 goto err_pd_free;
725         }
726
727         err = mthca_cmd_use_events(dev);
728         if (err) {
729                 mthca_err(dev, "Failed to switch to event-driven "
730                           "firmware commands, aborting.\n");
731                 goto err_eq_table_free;
732         }
733
734         err = mthca_NOP(dev, &status);
735         if (err || status) {
736                 mthca_err(dev, "NOP command failed to generate interrupt (IRQ %d), aborting.\n",
737                           dev->mthca_flags & MTHCA_FLAG_MSI_X ?
738                           dev->eq_table.eq[MTHCA_EQ_CMD].msi_x_vector :
739                           dev->pdev->irq);
740                 if (dev->mthca_flags & (MTHCA_FLAG_MSI | MTHCA_FLAG_MSI_X))
741                         mthca_err(dev, "Try again with MSI/MSI-X disabled.\n");
742                 else
743                         mthca_err(dev, "BIOS or ACPI interrupt routing problem?\n");
744
745                 goto err_cmd_poll;
746         }
747
748         mthca_dbg(dev, "NOP command IRQ test passed\n");
749
750         err = mthca_init_cq_table(dev);
751         if (err) {
752                 mthca_err(dev, "Failed to initialize "
753                           "completion queue table, aborting.\n");
754                 goto err_cmd_poll;
755         }
756
757         err = mthca_init_srq_table(dev);
758         if (err) {
759                 mthca_err(dev, "Failed to initialize "
760                           "shared receive queue table, aborting.\n");
761                 goto err_cq_table_free;
762         }
763
764         err = mthca_init_qp_table(dev);
765         if (err) {
766                 mthca_err(dev, "Failed to initialize "
767                           "queue pair table, aborting.\n");
768                 goto err_srq_table_free;
769         }
770
771         err = mthca_init_av_table(dev);
772         if (err) {
773                 mthca_err(dev, "Failed to initialize "
774                           "address vector table, aborting.\n");
775                 goto err_qp_table_free;
776         }
777
778         err = mthca_init_mcg_table(dev);
779         if (err) {
780                 mthca_err(dev, "Failed to initialize "
781                           "multicast group table, aborting.\n");
782                 goto err_av_table_free;
783         }
784
785         return 0;
786
787 err_av_table_free:
788         mthca_cleanup_av_table(dev);
789
790 err_qp_table_free:
791         mthca_cleanup_qp_table(dev);
792
793 err_srq_table_free:
794         mthca_cleanup_srq_table(dev);
795
796 err_cq_table_free:
797         mthca_cleanup_cq_table(dev);
798
799 err_cmd_poll:
800         mthca_cmd_use_polling(dev);
801
802 err_eq_table_free:
803         mthca_cleanup_eq_table(dev);
804
805 err_pd_free:
806         mthca_pd_free(dev, &dev->driver_pd);
807
808 err_mr_table_free:
809         mthca_cleanup_mr_table(dev);
810
811 err_pd_table_free:
812         mthca_cleanup_pd_table(dev);
813
814 err_kar_unmap:
815         iounmap(dev->kar);
816
817 err_uar_free:
818         mthca_uar_free(dev, &dev->driver_uar);
819
820 err_uar_table_free:
821         mthca_cleanup_uar_table(dev);
822         return err;
823 }
824
825 static int __devinit mthca_request_regions(struct pci_dev *pdev,
826                                            int ddr_hidden)
827 {
828         int err;
829
830         /*
831          * We can't just use pci_request_regions() because the MSI-X
832          * table is right in the middle of the first BAR.  If we did
833          * pci_request_region and grab all of the first BAR, then
834          * setting up MSI-X would fail, since the PCI core wants to do
835          * request_mem_region on the MSI-X vector table.
836          *
837          * So just request what we need right now, and request any
838          * other regions we need when setting up EQs.
839          */
840         if (!request_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
841                                 MTHCA_HCR_SIZE, DRV_NAME))
842                 return -EBUSY;
843
844         err = pci_request_region(pdev, 2, DRV_NAME);
845         if (err)
846                 goto err_bar2_failed;
847
848         if (!ddr_hidden) {
849                 err = pci_request_region(pdev, 4, DRV_NAME);
850                 if (err)
851                         goto err_bar4_failed;
852         }
853
854         return 0;
855
856 err_bar4_failed:
857         pci_release_region(pdev, 2);
858
859 err_bar2_failed:
860         release_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
861                            MTHCA_HCR_SIZE);
862
863         return err;
864 }
865
866 static void mthca_release_regions(struct pci_dev *pdev,
867                                   int ddr_hidden)
868 {
869         if (!ddr_hidden)
870                 pci_release_region(pdev, 4);
871
872         pci_release_region(pdev, 2);
873
874         release_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
875                            MTHCA_HCR_SIZE);
876 }
877
878 static int __devinit mthca_enable_msi_x(struct mthca_dev *mdev)
879 {
880         struct msix_entry entries[3];
881         int err;
882
883         entries[0].entry = 0;
884         entries[1].entry = 1;
885         entries[2].entry = 2;
886
887         err = pci_enable_msix(mdev->pdev, entries, ARRAY_SIZE(entries));
888         if (err) {
889                 if (err > 0)
890                         mthca_info(mdev, "Only %d MSI-X vectors available, "
891                                    "not using MSI-X\n", err);
892                 return err;
893         }
894
895         mdev->eq_table.eq[MTHCA_EQ_COMP ].msi_x_vector = entries[0].vector;
896         mdev->eq_table.eq[MTHCA_EQ_ASYNC].msi_x_vector = entries[1].vector;
897         mdev->eq_table.eq[MTHCA_EQ_CMD  ].msi_x_vector = entries[2].vector;
898
899         return 0;
900 }
901
902 /* Types of supported HCA */
903 enum {
904         TAVOR,                  /* MT23108                        */
905         ARBEL_COMPAT,           /* MT25208 in Tavor compat mode   */
906         ARBEL_NATIVE,           /* MT25208 with extended features */
907         SINAI                   /* MT25204 */
908 };
909
910 #define MTHCA_FW_VER(major, minor, subminor) \
911         (((u64) (major) << 32) | ((u64) (minor) << 16) | (u64) (subminor))
912
913 static struct {
914         u64 latest_fw;
915         int is_memfree;
916         int is_pcie;
917 } mthca_hca_table[] = {
918         [TAVOR]        = { .latest_fw = MTHCA_FW_VER(3, 3, 3), .is_memfree = 0, .is_pcie = 0 },
919         [ARBEL_COMPAT] = { .latest_fw = MTHCA_FW_VER(4, 7, 0), .is_memfree = 0, .is_pcie = 1 },
920         [ARBEL_NATIVE] = { .latest_fw = MTHCA_FW_VER(5, 1, 0), .is_memfree = 1, .is_pcie = 1 },
921         [SINAI]        = { .latest_fw = MTHCA_FW_VER(1, 0, 1), .is_memfree = 1, .is_pcie = 1 }
922 };
923
924 static int __devinit mthca_init_one(struct pci_dev *pdev,
925                                     const struct pci_device_id *id)
926 {
927         static int mthca_version_printed = 0;
928         int ddr_hidden = 0;
929         int err;
930         struct mthca_dev *mdev;
931
932         if (!mthca_version_printed) {
933                 printk(KERN_INFO "%s", mthca_version);
934                 ++mthca_version_printed;
935         }
936
937         printk(KERN_INFO PFX "Initializing %s\n",
938                pci_name(pdev));
939
940         if (id->driver_data >= ARRAY_SIZE(mthca_hca_table)) {
941                 printk(KERN_ERR PFX "%s has invalid driver data %lx\n",
942                        pci_name(pdev), id->driver_data);
943                 return -ENODEV;
944         }
945
946         err = pci_enable_device(pdev);
947         if (err) {
948                 dev_err(&pdev->dev, "Cannot enable PCI device, "
949                         "aborting.\n");
950                 return err;
951         }
952
953         /*
954          * Check for BARs.  We expect 0: 1MB, 2: 8MB, 4: DDR (may not
955          * be present)
956          */
957         if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
958             pci_resource_len(pdev, 0) != 1 << 20) {
959                 dev_err(&pdev->dev, "Missing DCS, aborting.\n");
960                 err = -ENODEV;
961                 goto err_disable_pdev;
962         }
963         if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM) ||
964             pci_resource_len(pdev, 2) != 1 << 23) {
965                 dev_err(&pdev->dev, "Missing UAR, aborting.\n");
966                 err = -ENODEV;
967                 goto err_disable_pdev;
968         }
969         if (!(pci_resource_flags(pdev, 4) & IORESOURCE_MEM))
970                 ddr_hidden = 1;
971
972         err = mthca_request_regions(pdev, ddr_hidden);
973         if (err) {
974                 dev_err(&pdev->dev, "Cannot obtain PCI resources, "
975                         "aborting.\n");
976                 goto err_disable_pdev;
977         }
978
979         pci_set_master(pdev);
980
981         err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
982         if (err) {
983                 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask.\n");
984                 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
985                 if (err) {
986                         dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting.\n");
987                         goto err_free_res;
988                 }
989         }
990         err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
991         if (err) {
992                 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit "
993                          "consistent PCI DMA mask.\n");
994                 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
995                 if (err) {
996                         dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, "
997                                 "aborting.\n");
998                         goto err_free_res;
999                 }
1000         }
1001
1002         mdev = (struct mthca_dev *) ib_alloc_device(sizeof *mdev);
1003         if (!mdev) {
1004                 dev_err(&pdev->dev, "Device struct alloc failed, "
1005                         "aborting.\n");
1006                 err = -ENOMEM;
1007                 goto err_free_res;
1008         }
1009
1010         mdev->pdev = pdev;
1011
1012         if (ddr_hidden)
1013                 mdev->mthca_flags |= MTHCA_FLAG_DDR_HIDDEN;
1014         if (mthca_hca_table[id->driver_data].is_memfree)
1015                 mdev->mthca_flags |= MTHCA_FLAG_MEMFREE;
1016         if (mthca_hca_table[id->driver_data].is_pcie)
1017                 mdev->mthca_flags |= MTHCA_FLAG_PCIE;
1018
1019         /*
1020          * Now reset the HCA before we touch the PCI capabilities or
1021          * attempt a firmware command, since a boot ROM may have left
1022          * the HCA in an undefined state.
1023          */
1024         err = mthca_reset(mdev);
1025         if (err) {
1026                 mthca_err(mdev, "Failed to reset HCA, aborting.\n");
1027                 goto err_free_dev;
1028         }
1029
1030         if (msi_x && !mthca_enable_msi_x(mdev))
1031                 mdev->mthca_flags |= MTHCA_FLAG_MSI_X;
1032         if (msi && !(mdev->mthca_flags & MTHCA_FLAG_MSI_X) &&
1033             !pci_enable_msi(pdev))
1034                 mdev->mthca_flags |= MTHCA_FLAG_MSI;
1035
1036         if (mthca_cmd_init(mdev)) {
1037                 mthca_err(mdev, "Failed to init command interface, aborting.\n");
1038                 goto err_free_dev;
1039         }
1040
1041         err = mthca_tune_pci(mdev);
1042         if (err)
1043                 goto err_cmd;
1044
1045         err = mthca_init_hca(mdev);
1046         if (err)
1047                 goto err_cmd;
1048
1049         if (mdev->fw_ver < mthca_hca_table[id->driver_data].latest_fw) {
1050                 mthca_warn(mdev, "HCA FW version %x.%x.%x is old (%x.%x.%x is current).\n",
1051                            (int) (mdev->fw_ver >> 32), (int) (mdev->fw_ver >> 16) & 0xffff,
1052                            (int) (mdev->fw_ver & 0xffff),
1053                            (int) (mthca_hca_table[id->driver_data].latest_fw >> 32),
1054                            (int) (mthca_hca_table[id->driver_data].latest_fw >> 16) & 0xffff,
1055                            (int) (mthca_hca_table[id->driver_data].latest_fw & 0xffff));
1056                 mthca_warn(mdev, "If you have problems, try updating your HCA FW.\n");
1057         }
1058
1059         err = mthca_setup_hca(mdev);
1060         if (err)
1061                 goto err_close;
1062
1063         err = mthca_register_device(mdev);
1064         if (err)
1065                 goto err_cleanup;
1066
1067         err = mthca_create_agents(mdev);
1068         if (err)
1069                 goto err_unregister;
1070
1071         pci_set_drvdata(pdev, mdev);
1072
1073         return 0;
1074
1075 err_unregister:
1076         mthca_unregister_device(mdev);
1077
1078 err_cleanup:
1079         mthca_cleanup_mcg_table(mdev);
1080         mthca_cleanup_av_table(mdev);
1081         mthca_cleanup_qp_table(mdev);
1082         mthca_cleanup_srq_table(mdev);
1083         mthca_cleanup_cq_table(mdev);
1084         mthca_cmd_use_polling(mdev);
1085         mthca_cleanup_eq_table(mdev);
1086
1087         mthca_pd_free(mdev, &mdev->driver_pd);
1088
1089         mthca_cleanup_mr_table(mdev);
1090         mthca_cleanup_pd_table(mdev);
1091         mthca_cleanup_uar_table(mdev);
1092
1093 err_close:
1094         mthca_close_hca(mdev);
1095
1096 err_cmd:
1097         mthca_cmd_cleanup(mdev);
1098
1099 err_free_dev:
1100         if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1101                 pci_disable_msix(pdev);
1102         if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1103                 pci_disable_msi(pdev);
1104
1105         ib_dealloc_device(&mdev->ib_dev);
1106
1107 err_free_res:
1108         mthca_release_regions(pdev, ddr_hidden);
1109
1110 err_disable_pdev:
1111         pci_disable_device(pdev);
1112         pci_set_drvdata(pdev, NULL);
1113         return err;
1114 }
1115
1116 static void __devexit mthca_remove_one(struct pci_dev *pdev)
1117 {
1118         struct mthca_dev *mdev = pci_get_drvdata(pdev);
1119         u8 status;
1120         int p;
1121
1122         if (mdev) {
1123                 mthca_free_agents(mdev);
1124                 mthca_unregister_device(mdev);
1125
1126                 for (p = 1; p <= mdev->limits.num_ports; ++p)
1127                         mthca_CLOSE_IB(mdev, p, &status);
1128
1129                 mthca_cleanup_mcg_table(mdev);
1130                 mthca_cleanup_av_table(mdev);
1131                 mthca_cleanup_qp_table(mdev);
1132                 mthca_cleanup_srq_table(mdev);
1133                 mthca_cleanup_cq_table(mdev);
1134                 mthca_cmd_use_polling(mdev);
1135                 mthca_cleanup_eq_table(mdev);
1136
1137                 mthca_pd_free(mdev, &mdev->driver_pd);
1138
1139                 mthca_cleanup_mr_table(mdev);
1140                 mthca_cleanup_pd_table(mdev);
1141
1142                 iounmap(mdev->kar);
1143                 mthca_uar_free(mdev, &mdev->driver_uar);
1144                 mthca_cleanup_uar_table(mdev);
1145                 mthca_close_hca(mdev);
1146                 mthca_cmd_cleanup(mdev);
1147
1148                 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1149                         pci_disable_msix(pdev);
1150                 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1151                         pci_disable_msi(pdev);
1152
1153                 ib_dealloc_device(&mdev->ib_dev);
1154                 mthca_release_regions(pdev, mdev->mthca_flags &
1155                                       MTHCA_FLAG_DDR_HIDDEN);
1156                 pci_disable_device(pdev);
1157                 pci_set_drvdata(pdev, NULL);
1158         }
1159 }
1160
1161 static struct pci_device_id mthca_pci_table[] = {
1162         { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_TAVOR),
1163           .driver_data = TAVOR },
1164         { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_TAVOR),
1165           .driver_data = TAVOR },
1166         { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
1167           .driver_data = ARBEL_COMPAT },
1168         { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
1169           .driver_data = ARBEL_COMPAT },
1170         { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL),
1171           .driver_data = ARBEL_NATIVE },
1172         { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL),
1173           .driver_data = ARBEL_NATIVE },
1174         { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI),
1175           .driver_data = SINAI },
1176         { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI),
1177           .driver_data = SINAI },
1178         { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
1179           .driver_data = SINAI },
1180         { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
1181           .driver_data = SINAI },
1182         { 0, }
1183 };
1184
1185 MODULE_DEVICE_TABLE(pci, mthca_pci_table);
1186
1187 static struct pci_driver mthca_driver = {
1188         .name           = DRV_NAME,
1189         .id_table       = mthca_pci_table,
1190         .probe          = mthca_init_one,
1191         .remove         = __devexit_p(mthca_remove_one)
1192 };
1193
1194 static int __init mthca_init(void)
1195 {
1196         int ret;
1197
1198         ret = pci_register_driver(&mthca_driver);
1199         return ret < 0 ? ret : 0;
1200 }
1201
1202 static void __exit mthca_cleanup(void)
1203 {
1204         pci_unregister_driver(&mthca_driver);
1205 }
1206
1207 module_init(mthca_init);
1208 module_exit(mthca_cleanup);