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