IB/qib: Update minor version number
[deliverable/linux.git] / drivers / infiniband / hw / qib / qib_init.c
CommitLineData
f931551b 1/*
e2eed58b 2 * Copyright (c) 2012, 2013 Intel Corporation. All rights reserved.
551ace12 3 * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
f931551b
RC
4 * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. 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
35#include <linux/pci.h>
36#include <linux/netdevice.h>
37#include <linux/vmalloc.h>
38#include <linux/delay.h>
39#include <linux/idr.h>
e4dd23d7 40#include <linux/module.h>
7fac3301 41#include <linux/printk.h>
8469ba39
MM
42#ifdef CONFIG_INFINIBAND_QIB_DCA
43#include <linux/dca.h>
44#endif
f931551b
RC
45
46#include "qib.h"
47#include "qib_common.h"
36a8f01c 48#include "qib_mad.h"
f931551b 49
7fac3301
MM
50#undef pr_fmt
51#define pr_fmt(fmt) QIB_DRV_NAME ": " fmt
52
f931551b
RC
53/*
54 * min buffers we want to have per context, after driver
55 */
56#define QIB_MIN_USER_CTXT_BUFCNT 7
57
58#define QLOGIC_IB_R_SOFTWARE_MASK 0xFF
59#define QLOGIC_IB_R_SOFTWARE_SHIFT 24
60#define QLOGIC_IB_R_EMULATOR_MASK (1ULL<<62)
61
62/*
63 * Number of ctxts we are configured to use (to allow for more pio
64 * buffers per ctxt, etc.) Zero means use chip value.
65 */
66ushort qib_cfgctxts;
67module_param_named(cfgctxts, qib_cfgctxts, ushort, S_IRUGO);
68MODULE_PARM_DESC(cfgctxts, "Set max number of contexts to use");
69
70/*
71 * If set, do not write to any regs if avoidable, hack to allow
72 * check for deranged default register values.
73 */
74ushort qib_mini_init;
75module_param_named(mini_init, qib_mini_init, ushort, S_IRUGO);
76MODULE_PARM_DESC(mini_init, "If set, do minimal diag init");
77
78unsigned qib_n_krcv_queues;
79module_param_named(krcvqs, qib_n_krcv_queues, uint, S_IRUGO);
80MODULE_PARM_DESC(krcvqs, "number of kernel receive queues per IB port");
81
36a8f01c
MM
82unsigned qib_cc_table_size;
83module_param_named(cc_table_size, qib_cc_table_size, uint, S_IRUGO);
84MODULE_PARM_DESC(cc_table_size, "Congestion control table entries 0 (CCA disabled - default), min = 128, max = 1984");
f931551b
RC
85/*
86 * qib_wc_pat parameter:
87 * 0 is WC via MTRR
88 * 1 is WC via PAT
89 * If PAT initialization fails, code reverts back to MTRR
90 */
91unsigned qib_wc_pat = 1; /* default (1) is to use PAT, not MTRR */
92module_param_named(wc_pat, qib_wc_pat, uint, S_IRUGO);
93MODULE_PARM_DESC(wc_pat, "enable write-combining via PAT mechanism");
94
f931551b
RC
95struct workqueue_struct *qib_cq_wq;
96
97static void verify_interrupt(unsigned long);
98
99static struct idr qib_unit_table;
100u32 qib_cpulist_count;
101unsigned long *qib_cpulist;
102
103/* set number of contexts we'll actually use */
104void qib_set_ctxtcnt(struct qib_devdata *dd)
105{
5dbbcb97 106 if (!qib_cfgctxts) {
0502f94c 107 dd->cfgctxts = dd->first_user_ctxt + num_online_cpus();
5dbbcb97
MM
108 if (dd->cfgctxts > dd->ctxtcnt)
109 dd->cfgctxts = dd->ctxtcnt;
110 } else if (qib_cfgctxts < dd->num_pports)
f931551b
RC
111 dd->cfgctxts = dd->ctxtcnt;
112 else if (qib_cfgctxts <= dd->ctxtcnt)
113 dd->cfgctxts = qib_cfgctxts;
114 else
115 dd->cfgctxts = dd->ctxtcnt;
6ceaadee
MH
116 dd->freectxts = (dd->first_user_ctxt > dd->cfgctxts) ? 0 :
117 dd->cfgctxts - dd->first_user_ctxt;
f931551b
RC
118}
119
120/*
121 * Common code for creating the receive context array.
122 */
123int qib_create_ctxts(struct qib_devdata *dd)
124{
125 unsigned i;
126 int ret;
127
128 /*
129 * Allocate full ctxtcnt array, rather than just cfgctxts, because
130 * cleanup iterates across all possible ctxts.
131 */
132 dd->rcd = kzalloc(sizeof(*dd->rcd) * dd->ctxtcnt, GFP_KERNEL);
133 if (!dd->rcd) {
7fac3301
MM
134 qib_dev_err(dd,
135 "Unable to allocate ctxtdata array, failing\n");
f931551b
RC
136 ret = -ENOMEM;
137 goto done;
138 }
139
140 /* create (one or more) kctxt */
141 for (i = 0; i < dd->first_user_ctxt; ++i) {
142 struct qib_pportdata *ppd;
143 struct qib_ctxtdata *rcd;
144
145 if (dd->skip_kctxt_mask & (1 << i))
146 continue;
147
148 ppd = dd->pport + (i % dd->num_pports);
149 rcd = qib_create_ctxtdata(ppd, i);
150 if (!rcd) {
7fac3301
MM
151 qib_dev_err(dd,
152 "Unable to allocate ctxtdata for Kernel ctxt, failing\n");
f931551b
RC
153 ret = -ENOMEM;
154 goto done;
155 }
156 rcd->pkeys[0] = QIB_DEFAULT_P_KEY;
157 rcd->seq_cnt = 1;
158 }
159 ret = 0;
160done:
161 return ret;
162}
163
164/*
165 * Common code for user and kernel context setup.
166 */
167struct qib_ctxtdata *qib_create_ctxtdata(struct qib_pportdata *ppd, u32 ctxt)
168{
169 struct qib_devdata *dd = ppd->dd;
170 struct qib_ctxtdata *rcd;
171
172 rcd = kzalloc(sizeof(*rcd), GFP_KERNEL);
173 if (rcd) {
174 INIT_LIST_HEAD(&rcd->qp_wait_list);
175 rcd->ppd = ppd;
176 rcd->dd = dd;
177 rcd->cnt = 1;
178 rcd->ctxt = ctxt;
179 dd->rcd[ctxt] = rcd;
180
181 dd->f_init_ctxt(rcd);
182
183 /*
184 * To avoid wasting a lot of memory, we allocate 32KB chunks
185 * of physically contiguous memory, advance through it until
186 * used up and then allocate more. Of course, we need
187 * memory to store those extra pointers, now. 32KB seems to
188 * be the most that is "safe" under memory pressure
189 * (creating large files and then copying them over
190 * NFS while doing lots of MPI jobs). The OOM killer can
191 * get invoked, even though we say we can sleep and this can
192 * cause significant system problems....
193 */
194 rcd->rcvegrbuf_size = 0x8000;
195 rcd->rcvegrbufs_perchunk =
196 rcd->rcvegrbuf_size / dd->rcvegrbufsize;
197 rcd->rcvegrbuf_chunks = (rcd->rcvegrcnt +
198 rcd->rcvegrbufs_perchunk - 1) /
199 rcd->rcvegrbufs_perchunk;
9e1c0e43
MM
200 BUG_ON(!is_power_of_2(rcd->rcvegrbufs_perchunk));
201 rcd->rcvegrbufs_perchunk_shift =
202 ilog2(rcd->rcvegrbufs_perchunk);
f931551b
RC
203 }
204 return rcd;
205}
206
207/*
208 * Common code for initializing the physical port structure.
209 */
210void qib_init_pportdata(struct qib_pportdata *ppd, struct qib_devdata *dd,
211 u8 hw_pidx, u8 port)
212{
36a8f01c 213 int size;
f931551b
RC
214 ppd->dd = dd;
215 ppd->hw_pidx = hw_pidx;
216 ppd->port = port; /* IB port number, not index */
217
218 spin_lock_init(&ppd->sdma_lock);
219 spin_lock_init(&ppd->lflags_lock);
220 init_waitqueue_head(&ppd->state_wait);
221
222 init_timer(&ppd->symerr_clear_timer);
223 ppd->symerr_clear_timer.function = qib_clear_symerror_on_linkup;
224 ppd->symerr_clear_timer.data = (unsigned long)ppd;
551ace12
MM
225
226 ppd->qib_wq = NULL;
36a8f01c
MM
227
228 spin_lock_init(&ppd->cc_shadow_lock);
229
230 if (qib_cc_table_size < IB_CCT_MIN_ENTRIES)
231 goto bail;
232
233 ppd->cc_supported_table_entries = min(max_t(int, qib_cc_table_size,
234 IB_CCT_MIN_ENTRIES), IB_CCT_ENTRIES*IB_CC_TABLE_CAP_DEFAULT);
235
236 ppd->cc_max_table_entries =
237 ppd->cc_supported_table_entries/IB_CCT_ENTRIES;
238
239 size = IB_CC_TABLE_CAP_DEFAULT * sizeof(struct ib_cc_table_entry)
240 * IB_CCT_ENTRIES;
241 ppd->ccti_entries = kzalloc(size, GFP_KERNEL);
242 if (!ppd->ccti_entries) {
243 qib_dev_err(dd,
244 "failed to allocate congestion control table for port %d!\n",
245 port);
246 goto bail;
247 }
248
249 size = IB_CC_CCS_ENTRIES * sizeof(struct ib_cc_congestion_entry);
250 ppd->congestion_entries = kzalloc(size, GFP_KERNEL);
251 if (!ppd->congestion_entries) {
252 qib_dev_err(dd,
253 "failed to allocate congestion setting list for port %d!\n",
254 port);
255 goto bail_1;
256 }
257
258 size = sizeof(struct cc_table_shadow);
259 ppd->ccti_entries_shadow = kzalloc(size, GFP_KERNEL);
260 if (!ppd->ccti_entries_shadow) {
261 qib_dev_err(dd,
262 "failed to allocate shadow ccti list for port %d!\n",
263 port);
264 goto bail_2;
265 }
266
267 size = sizeof(struct ib_cc_congestion_setting_attr);
268 ppd->congestion_entries_shadow = kzalloc(size, GFP_KERNEL);
269 if (!ppd->congestion_entries_shadow) {
270 qib_dev_err(dd,
271 "failed to allocate shadow congestion setting list for port %d!\n",
272 port);
273 goto bail_3;
274 }
275
276 return;
277
278bail_3:
279 kfree(ppd->ccti_entries_shadow);
280 ppd->ccti_entries_shadow = NULL;
281bail_2:
282 kfree(ppd->congestion_entries);
283 ppd->congestion_entries = NULL;
284bail_1:
285 kfree(ppd->ccti_entries);
286 ppd->ccti_entries = NULL;
287bail:
288 /* User is intentionally disabling the congestion control agent */
289 if (!qib_cc_table_size)
290 return;
291
292 if (qib_cc_table_size < IB_CCT_MIN_ENTRIES) {
293 qib_cc_table_size = 0;
294 qib_dev_err(dd,
295 "Congestion Control table size %d less than minimum %d for port %d\n",
296 qib_cc_table_size, IB_CCT_MIN_ENTRIES, port);
297 }
298
299 qib_dev_err(dd, "Congestion Control Agent disabled for port %d\n",
300 port);
301 return;
f931551b
RC
302}
303
304static int init_pioavailregs(struct qib_devdata *dd)
305{
306 int ret, pidx;
307 u64 *status_page;
308
309 dd->pioavailregs_dma = dma_alloc_coherent(
310 &dd->pcidev->dev, PAGE_SIZE, &dd->pioavailregs_phys,
311 GFP_KERNEL);
312 if (!dd->pioavailregs_dma) {
7fac3301
MM
313 qib_dev_err(dd,
314 "failed to allocate PIOavail reg area in memory\n");
f931551b
RC
315 ret = -ENOMEM;
316 goto done;
317 }
318
319 /*
320 * We really want L2 cache aligned, but for current CPUs of
321 * interest, they are the same.
322 */
323 status_page = (u64 *)
324 ((char *) dd->pioavailregs_dma +
325 ((2 * L1_CACHE_BYTES +
326 dd->pioavregs * sizeof(u64)) & ~L1_CACHE_BYTES));
327 /* device status comes first, for backwards compatibility */
328 dd->devstatusp = status_page;
329 *status_page++ = 0;
330 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
331 dd->pport[pidx].statusp = status_page;
332 *status_page++ = 0;
333 }
334
335 /*
336 * Setup buffer to hold freeze and other messages, accessible to
337 * apps, following statusp. This is per-unit, not per port.
338 */
339 dd->freezemsg = (char *) status_page;
340 *dd->freezemsg = 0;
341 /* length of msg buffer is "whatever is left" */
342 ret = (char *) status_page - (char *) dd->pioavailregs_dma;
343 dd->freezelen = PAGE_SIZE - ret;
344
345 ret = 0;
346
347done:
348 return ret;
349}
350
351/**
352 * init_shadow_tids - allocate the shadow TID array
353 * @dd: the qlogic_ib device
354 *
355 * allocate the shadow TID array, so we can qib_munlock previous
356 * entries. It may make more sense to move the pageshadow to the
357 * ctxt data structure, so we only allocate memory for ctxts actually
358 * in use, since we at 8k per ctxt, now.
359 * We don't want failures here to prevent use of the driver/chip,
360 * so no return value.
361 */
362static void init_shadow_tids(struct qib_devdata *dd)
363{
364 struct page **pages;
365 dma_addr_t *addrs;
366
948579cd 367 pages = vzalloc(dd->cfgctxts * dd->rcvtidcnt * sizeof(struct page *));
f931551b 368 if (!pages) {
7fac3301
MM
369 qib_dev_err(dd,
370 "failed to allocate shadow page * array, no expected sends!\n");
f931551b
RC
371 goto bail;
372 }
373
948579cd 374 addrs = vzalloc(dd->cfgctxts * dd->rcvtidcnt * sizeof(dma_addr_t));
f931551b 375 if (!addrs) {
7fac3301
MM
376 qib_dev_err(dd,
377 "failed to allocate shadow dma handle array, no expected sends!\n");
f931551b
RC
378 goto bail_free;
379 }
380
f931551b
RC
381 dd->pageshadow = pages;
382 dd->physshadow = addrs;
383 return;
384
385bail_free:
386 vfree(pages);
387bail:
388 dd->pageshadow = NULL;
389}
390
391/*
392 * Do initialization for device that is only needed on
393 * first detect, not on resets.
394 */
395static int loadtime_init(struct qib_devdata *dd)
396{
397 int ret = 0;
398
399 if (((dd->revision >> QLOGIC_IB_R_SOFTWARE_SHIFT) &
400 QLOGIC_IB_R_SOFTWARE_MASK) != QIB_CHIP_SWVERSION) {
7fac3301
MM
401 qib_dev_err(dd,
402 "Driver only handles version %d, chip swversion is %d (%llx), failng\n",
403 QIB_CHIP_SWVERSION,
404 (int)(dd->revision >>
f931551b 405 QLOGIC_IB_R_SOFTWARE_SHIFT) &
7fac3301
MM
406 QLOGIC_IB_R_SOFTWARE_MASK,
407 (unsigned long long) dd->revision);
f931551b
RC
408 ret = -ENOSYS;
409 goto done;
410 }
411
412 if (dd->revision & QLOGIC_IB_R_EMULATOR_MASK)
413 qib_devinfo(dd->pcidev, "%s", dd->boardversion);
414
415 spin_lock_init(&dd->pioavail_lock);
416 spin_lock_init(&dd->sendctrl_lock);
417 spin_lock_init(&dd->uctxt_lock);
418 spin_lock_init(&dd->qib_diag_trans_lock);
419 spin_lock_init(&dd->eep_st_lock);
420 mutex_init(&dd->eep_lock);
421
422 if (qib_mini_init)
423 goto done;
424
425 ret = init_pioavailregs(dd);
426 init_shadow_tids(dd);
427
428 qib_get_eeprom_info(dd);
429
430 /* setup time (don't start yet) to verify we got interrupt */
431 init_timer(&dd->intrchk_timer);
432 dd->intrchk_timer.function = verify_interrupt;
433 dd->intrchk_timer.data = (unsigned long) dd;
434
435done:
436 return ret;
437}
438
439/**
440 * init_after_reset - re-initialize after a reset
441 * @dd: the qlogic_ib device
442 *
443 * sanity check at least some of the values after reset, and
25985edc 444 * ensure no receive or transmit (explicitly, in case reset
f931551b
RC
445 * failed
446 */
447static int init_after_reset(struct qib_devdata *dd)
448{
449 int i;
450
451 /*
452 * Ensure chip does no sends or receives, tail updates, or
453 * pioavail updates while we re-initialize. This is mostly
454 * for the driver data structures, not chip registers.
455 */
456 for (i = 0; i < dd->num_pports; ++i) {
457 /*
458 * ctxt == -1 means "all contexts". Only really safe for
459 * _dis_abling things, as here.
460 */
461 dd->f_rcvctrl(dd->pport + i, QIB_RCVCTRL_CTXT_DIS |
462 QIB_RCVCTRL_INTRAVAIL_DIS |
463 QIB_RCVCTRL_TAILUPD_DIS, -1);
464 /* Redundant across ports for some, but no big deal. */
465 dd->f_sendctrl(dd->pport + i, QIB_SENDCTRL_SEND_DIS |
466 QIB_SENDCTRL_AVAIL_DIS);
467 }
468
469 return 0;
470}
471
472static void enable_chip(struct qib_devdata *dd)
473{
474 u64 rcvmask;
475 int i;
476
477 /*
478 * Enable PIO send, and update of PIOavail regs to memory.
479 */
480 for (i = 0; i < dd->num_pports; ++i)
481 dd->f_sendctrl(dd->pport + i, QIB_SENDCTRL_SEND_ENB |
482 QIB_SENDCTRL_AVAIL_ENB);
483 /*
484 * Enable kernel ctxts' receive and receive interrupt.
485 * Other ctxts done as user opens and inits them.
486 */
487 rcvmask = QIB_RCVCTRL_CTXT_ENB | QIB_RCVCTRL_INTRAVAIL_ENB;
488 rcvmask |= (dd->flags & QIB_NODMA_RTAIL) ?
489 QIB_RCVCTRL_TAILUPD_DIS : QIB_RCVCTRL_TAILUPD_ENB;
490 for (i = 0; dd->rcd && i < dd->first_user_ctxt; ++i) {
491 struct qib_ctxtdata *rcd = dd->rcd[i];
492
493 if (rcd)
494 dd->f_rcvctrl(rcd->ppd, rcvmask, i);
495 }
496}
497
498static void verify_interrupt(unsigned long opaque)
499{
500 struct qib_devdata *dd = (struct qib_devdata *) opaque;
501
502 if (!dd)
503 return; /* being torn down */
504
505 /*
506 * If we don't have a lid or any interrupts, let the user know and
507 * don't bother checking again.
508 */
509 if (dd->int_counter == 0) {
510 if (!dd->f_intr_fallback(dd))
7fac3301
MM
511 dev_err(&dd->pcidev->dev,
512 "No interrupts detected, not usable.\n");
f931551b
RC
513 else /* re-arm the timer to see if fallback works */
514 mod_timer(&dd->intrchk_timer, jiffies + HZ/2);
515 }
516}
517
518static void init_piobuf_state(struct qib_devdata *dd)
519{
520 int i, pidx;
521 u32 uctxts;
522
523 /*
524 * Ensure all buffers are free, and fifos empty. Buffers
525 * are common, so only do once for port 0.
526 *
527 * After enable and qib_chg_pioavailkernel so we can safely
528 * enable pioavail updates and PIOENABLE. After this, packets
529 * are ready and able to go out.
530 */
531 dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_ALL);
532 for (pidx = 0; pidx < dd->num_pports; ++pidx)
533 dd->f_sendctrl(dd->pport + pidx, QIB_SENDCTRL_FLUSH);
534
535 /*
536 * If not all sendbufs are used, add the one to each of the lower
537 * numbered contexts. pbufsctxt and lastctxt_piobuf are
538 * calculated in chip-specific code because it may cause some
539 * chip-specific adjustments to be made.
540 */
541 uctxts = dd->cfgctxts - dd->first_user_ctxt;
542 dd->ctxts_extrabuf = dd->pbufsctxt ?
543 dd->lastctxt_piobuf - (dd->pbufsctxt * uctxts) : 0;
544
545 /*
546 * Set up the shadow copies of the piobufavail registers,
547 * which we compare against the chip registers for now, and
548 * the in memory DMA'ed copies of the registers.
549 * By now pioavail updates to memory should have occurred, so
550 * copy them into our working/shadow registers; this is in
551 * case something went wrong with abort, but mostly to get the
552 * initial values of the generation bit correct.
553 */
554 for (i = 0; i < dd->pioavregs; i++) {
555 __le64 tmp;
556
557 tmp = dd->pioavailregs_dma[i];
558 /*
559 * Don't need to worry about pioavailkernel here
560 * because we will call qib_chg_pioavailkernel() later
561 * in initialization, to busy out buffers as needed.
562 */
563 dd->pioavailshadow[i] = le64_to_cpu(tmp);
564 }
565 while (i < ARRAY_SIZE(dd->pioavailshadow))
566 dd->pioavailshadow[i++] = 0; /* for debugging sanity */
567
568 /* after pioavailshadow is setup */
569 qib_chg_pioavailkernel(dd, 0, dd->piobcnt2k + dd->piobcnt4k,
570 TXCHK_CHG_TYPE_KERN, NULL);
571 dd->f_initvl15_bufs(dd);
572}
573
551ace12
MM
574/**
575 * qib_create_workqueues - create per port workqueues
576 * @dd: the qlogic_ib device
577 */
578static int qib_create_workqueues(struct qib_devdata *dd)
579{
580 int pidx;
581 struct qib_pportdata *ppd;
582
583 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
584 ppd = dd->pport + pidx;
585 if (!ppd->qib_wq) {
586 char wq_name[8]; /* 3 + 2 + 1 + 1 + 1 */
587 snprintf(wq_name, sizeof(wq_name), "qib%d_%d",
588 dd->unit, pidx);
589 ppd->qib_wq =
590 create_singlethread_workqueue(wq_name);
591 if (!ppd->qib_wq)
592 goto wq_error;
593 }
594 }
595 return 0;
596wq_error:
7fac3301
MM
597 pr_err("create_singlethread_workqueue failed for port %d\n",
598 pidx + 1);
551ace12
MM
599 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
600 ppd = dd->pport + pidx;
601 if (ppd->qib_wq) {
602 destroy_workqueue(ppd->qib_wq);
603 ppd->qib_wq = NULL;
604 }
605 }
606 return -ENOMEM;
607}
608
f931551b
RC
609/**
610 * qib_init - do the actual initialization sequence on the chip
611 * @dd: the qlogic_ib device
612 * @reinit: reinitializing, so don't allocate new memory
613 *
614 * Do the actual initialization sequence on the chip. This is done
615 * both from the init routine called from the PCI infrastructure, and
616 * when we reset the chip, or detect that it was reset internally,
617 * or it's administratively re-enabled.
618 *
619 * Memory allocation here and in called routines is only done in
620 * the first case (reinit == 0). We have to be careful, because even
621 * without memory allocation, we need to re-write all the chip registers
622 * TIDs, etc. after the reset or enable has completed.
623 */
624int qib_init(struct qib_devdata *dd, int reinit)
625{
626 int ret = 0, pidx, lastfail = 0;
627 u32 portok = 0;
628 unsigned i;
629 struct qib_ctxtdata *rcd;
630 struct qib_pportdata *ppd;
631 unsigned long flags;
632
633 /* Set linkstate to unknown, so we can watch for a transition. */
634 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
635 ppd = dd->pport + pidx;
636 spin_lock_irqsave(&ppd->lflags_lock, flags);
637 ppd->lflags &= ~(QIBL_LINKACTIVE | QIBL_LINKARMED |
638 QIBL_LINKDOWN | QIBL_LINKINIT |
639 QIBL_LINKV);
640 spin_unlock_irqrestore(&ppd->lflags_lock, flags);
641 }
642
643 if (reinit)
644 ret = init_after_reset(dd);
645 else
646 ret = loadtime_init(dd);
647 if (ret)
648 goto done;
649
650 /* Bypass most chip-init, to get to device creation */
651 if (qib_mini_init)
652 return 0;
653
654 ret = dd->f_late_initreg(dd);
655 if (ret)
656 goto done;
657
658 /* dd->rcd can be NULL if early init failed */
659 for (i = 0; dd->rcd && i < dd->first_user_ctxt; ++i) {
660 /*
661 * Set up the (kernel) rcvhdr queue and egr TIDs. If doing
662 * re-init, the simplest way to handle this is to free
663 * existing, and re-allocate.
664 * Need to re-create rest of ctxt 0 ctxtdata as well.
665 */
666 rcd = dd->rcd[i];
667 if (!rcd)
668 continue;
669
670 lastfail = qib_create_rcvhdrq(dd, rcd);
671 if (!lastfail)
672 lastfail = qib_setup_eagerbufs(rcd);
673 if (lastfail) {
7fac3301
MM
674 qib_dev_err(dd,
675 "failed to allocate kernel ctxt's rcvhdrq and/or egr bufs\n");
f931551b
RC
676 continue;
677 }
678 }
679
680 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
681 int mtu;
682 if (lastfail)
683 ret = lastfail;
684 ppd = dd->pport + pidx;
685 mtu = ib_mtu_enum_to_int(qib_ibmtu);
686 if (mtu == -1) {
687 mtu = QIB_DEFAULT_MTU;
688 qib_ibmtu = 0; /* don't leave invalid value */
689 }
690 /* set max we can ever have for this driver load */
691 ppd->init_ibmaxlen = min(mtu > 2048 ?
692 dd->piosize4k : dd->piosize2k,
693 dd->rcvegrbufsize +
694 (dd->rcvhdrentsize << 2));
695 /*
696 * Have to initialize ibmaxlen, but this will normally
697 * change immediately in qib_set_mtu().
698 */
699 ppd->ibmaxlen = ppd->init_ibmaxlen;
700 qib_set_mtu(ppd, mtu);
701
702 spin_lock_irqsave(&ppd->lflags_lock, flags);
703 ppd->lflags |= QIBL_IB_LINK_DISABLED;
704 spin_unlock_irqrestore(&ppd->lflags_lock, flags);
705
706 lastfail = dd->f_bringup_serdes(ppd);
707 if (lastfail) {
708 qib_devinfo(dd->pcidev,
709 "Failed to bringup IB port %u\n", ppd->port);
710 lastfail = -ENETDOWN;
711 continue;
712 }
713
f931551b
RC
714 portok++;
715 }
716
717 if (!portok) {
718 /* none of the ports initialized */
719 if (!ret && lastfail)
720 ret = lastfail;
721 else if (!ret)
722 ret = -ENETDOWN;
723 /* but continue on, so we can debug cause */
724 }
725
726 enable_chip(dd);
727
728 init_piobuf_state(dd);
729
730done:
731 if (!ret) {
732 /* chip is OK for user apps; mark it as initialized */
733 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
734 ppd = dd->pport + pidx;
735 /*
736 * Set status even if port serdes is not initialized
737 * so that diags will work.
738 */
739 *ppd->statusp |= QIB_STATUS_CHIP_PRESENT |
740 QIB_STATUS_INITTED;
741 if (!ppd->link_speed_enabled)
742 continue;
743 if (dd->flags & QIB_HAS_SEND_DMA)
744 ret = qib_setup_sdma(ppd);
745 init_timer(&ppd->hol_timer);
746 ppd->hol_timer.function = qib_hol_event;
747 ppd->hol_timer.data = (unsigned long)ppd;
748 ppd->hol_state = QIB_HOL_UP;
749 }
750
751 /* now we can enable all interrupts from the chip */
752 dd->f_set_intr_state(dd, 1);
753
754 /*
755 * Setup to verify we get an interrupt, and fallback
756 * to an alternate if necessary and possible.
757 */
758 mod_timer(&dd->intrchk_timer, jiffies + HZ/2);
759 /* start stats retrieval timer */
760 mod_timer(&dd->stats_timer, jiffies + HZ * ACTIVITY_TIMER);
761 }
762
763 /* if ret is non-zero, we probably should do some cleanup here... */
764 return ret;
765}
766
767/*
768 * These next two routines are placeholders in case we don't have per-arch
769 * code for controlling write combining. If explicit control of write
770 * combining is not available, performance will probably be awful.
771 */
772
773int __attribute__((weak)) qib_enable_wc(struct qib_devdata *dd)
774{
775 return -EOPNOTSUPP;
776}
777
778void __attribute__((weak)) qib_disable_wc(struct qib_devdata *dd)
779{
780}
781
782static inline struct qib_devdata *__qib_lookup(int unit)
783{
784 return idr_find(&qib_unit_table, unit);
785}
786
787struct qib_devdata *qib_lookup(int unit)
788{
789 struct qib_devdata *dd;
790 unsigned long flags;
791
792 spin_lock_irqsave(&qib_devs_lock, flags);
793 dd = __qib_lookup(unit);
794 spin_unlock_irqrestore(&qib_devs_lock, flags);
795
796 return dd;
797}
798
799/*
800 * Stop the timers during unit shutdown, or after an error late
801 * in initialization.
802 */
803static void qib_stop_timers(struct qib_devdata *dd)
804{
805 struct qib_pportdata *ppd;
806 int pidx;
807
808 if (dd->stats_timer.data) {
809 del_timer_sync(&dd->stats_timer);
810 dd->stats_timer.data = 0;
811 }
812 if (dd->intrchk_timer.data) {
813 del_timer_sync(&dd->intrchk_timer);
814 dd->intrchk_timer.data = 0;
815 }
816 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
817 ppd = dd->pport + pidx;
818 if (ppd->hol_timer.data)
819 del_timer_sync(&ppd->hol_timer);
820 if (ppd->led_override_timer.data) {
821 del_timer_sync(&ppd->led_override_timer);
822 atomic_set(&ppd->led_override_timer_active, 0);
823 }
824 if (ppd->symerr_clear_timer.data)
825 del_timer_sync(&ppd->symerr_clear_timer);
826 }
827}
828
829/**
830 * qib_shutdown_device - shut down a device
831 * @dd: the qlogic_ib device
832 *
833 * This is called to make the device quiet when we are about to
834 * unload the driver, and also when the device is administratively
835 * disabled. It does not free any data structures.
836 * Everything it does has to be setup again by qib_init(dd, 1)
837 */
838static void qib_shutdown_device(struct qib_devdata *dd)
839{
840 struct qib_pportdata *ppd;
841 unsigned pidx;
842
843 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
844 ppd = dd->pport + pidx;
845
846 spin_lock_irq(&ppd->lflags_lock);
847 ppd->lflags &= ~(QIBL_LINKDOWN | QIBL_LINKINIT |
848 QIBL_LINKARMED | QIBL_LINKACTIVE |
849 QIBL_LINKV);
850 spin_unlock_irq(&ppd->lflags_lock);
851 *ppd->statusp &= ~(QIB_STATUS_IB_CONF | QIB_STATUS_IB_READY);
852 }
853 dd->flags &= ~QIB_INITTED;
854
855 /* mask interrupts, but not errors */
856 dd->f_set_intr_state(dd, 0);
857
858 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
859 ppd = dd->pport + pidx;
860 dd->f_rcvctrl(ppd, QIB_RCVCTRL_TAILUPD_DIS |
861 QIB_RCVCTRL_CTXT_DIS |
862 QIB_RCVCTRL_INTRAVAIL_DIS |
863 QIB_RCVCTRL_PKEY_ENB, -1);
864 /*
865 * Gracefully stop all sends allowing any in progress to
866 * trickle out first.
867 */
868 dd->f_sendctrl(ppd, QIB_SENDCTRL_CLEAR);
869 }
870
871 /*
872 * Enough for anything that's going to trickle out to have actually
873 * done so.
874 */
875 udelay(20);
876
877 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
878 ppd = dd->pport + pidx;
879 dd->f_setextled(ppd, 0); /* make sure LEDs are off */
880
881 if (dd->flags & QIB_HAS_SEND_DMA)
882 qib_teardown_sdma(ppd);
883
884 dd->f_sendctrl(ppd, QIB_SENDCTRL_AVAIL_DIS |
885 QIB_SENDCTRL_SEND_DIS);
886 /*
887 * Clear SerdesEnable.
888 * We can't count on interrupts since we are stopping.
889 */
890 dd->f_quiet_serdes(ppd);
551ace12
MM
891
892 if (ppd->qib_wq) {
893 destroy_workqueue(ppd->qib_wq);
894 ppd->qib_wq = NULL;
895 }
f931551b
RC
896 }
897
898 qib_update_eeprom_log(dd);
899}
900
901/**
902 * qib_free_ctxtdata - free a context's allocated data
903 * @dd: the qlogic_ib device
904 * @rcd: the ctxtdata structure
905 *
906 * free up any allocated data for a context
907 * This should not touch anything that would affect a simultaneous
908 * re-allocation of context data, because it is called after qib_mutex
909 * is released (and can be called from reinit as well).
910 * It should never change any chip state, or global driver state.
911 */
912void qib_free_ctxtdata(struct qib_devdata *dd, struct qib_ctxtdata *rcd)
913{
914 if (!rcd)
915 return;
916
917 if (rcd->rcvhdrq) {
918 dma_free_coherent(&dd->pcidev->dev, rcd->rcvhdrq_size,
919 rcd->rcvhdrq, rcd->rcvhdrq_phys);
920 rcd->rcvhdrq = NULL;
921 if (rcd->rcvhdrtail_kvaddr) {
922 dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
923 rcd->rcvhdrtail_kvaddr,
924 rcd->rcvhdrqtailaddr_phys);
925 rcd->rcvhdrtail_kvaddr = NULL;
926 }
927 }
928 if (rcd->rcvegrbuf) {
929 unsigned e;
930
931 for (e = 0; e < rcd->rcvegrbuf_chunks; e++) {
932 void *base = rcd->rcvegrbuf[e];
933 size_t size = rcd->rcvegrbuf_size;
934
935 dma_free_coherent(&dd->pcidev->dev, size,
936 base, rcd->rcvegrbuf_phys[e]);
937 }
938 kfree(rcd->rcvegrbuf);
939 rcd->rcvegrbuf = NULL;
940 kfree(rcd->rcvegrbuf_phys);
941 rcd->rcvegrbuf_phys = NULL;
942 rcd->rcvegrbuf_chunks = 0;
943 }
944
945 kfree(rcd->tid_pg_list);
946 vfree(rcd->user_event_mask);
947 vfree(rcd->subctxt_uregbase);
948 vfree(rcd->subctxt_rcvegrbuf);
949 vfree(rcd->subctxt_rcvhdr_base);
950 kfree(rcd);
951}
952
953/*
954 * Perform a PIO buffer bandwidth write test, to verify proper system
955 * configuration. Even when all the setup calls work, occasionally
956 * BIOS or other issues can prevent write combining from working, or
957 * can cause other bandwidth problems to the chip.
958 *
959 * This test simply writes the same buffer over and over again, and
960 * measures close to the peak bandwidth to the chip (not testing
961 * data bandwidth to the wire). On chips that use an address-based
962 * trigger to send packets to the wire, this is easy. On chips that
963 * use a count to trigger, we want to make sure that the packet doesn't
964 * go out on the wire, or trigger flow control checks.
965 */
966static void qib_verify_pioperf(struct qib_devdata *dd)
967{
968 u32 pbnum, cnt, lcnt;
969 u32 __iomem *piobuf;
970 u32 *addr;
971 u64 msecs, emsecs;
972
973 piobuf = dd->f_getsendbuf(dd->pport, 0ULL, &pbnum);
974 if (!piobuf) {
975 qib_devinfo(dd->pcidev,
976 "No PIObufs for checking perf, skipping\n");
977 return;
978 }
979
980 /*
981 * Enough to give us a reasonable test, less than piobuf size, and
982 * likely multiple of store buffer length.
983 */
984 cnt = 1024;
985
986 addr = vmalloc(cnt);
987 if (!addr) {
988 qib_devinfo(dd->pcidev,
989 "Couldn't get memory for checking PIO perf,"
990 " skipping\n");
991 goto done;
992 }
993
994 preempt_disable(); /* we want reasonably accurate elapsed time */
995 msecs = 1 + jiffies_to_msecs(jiffies);
996 for (lcnt = 0; lcnt < 10000U; lcnt++) {
997 /* wait until we cross msec boundary */
998 if (jiffies_to_msecs(jiffies) >= msecs)
999 break;
1000 udelay(1);
1001 }
1002
1003 dd->f_set_armlaunch(dd, 0);
1004
1005 /*
1006 * length 0, no dwords actually sent
1007 */
1008 writeq(0, piobuf);
1009 qib_flush_wc();
1010
1011 /*
1012 * This is only roughly accurate, since even with preempt we
1013 * still take interrupts that could take a while. Running for
1014 * >= 5 msec seems to get us "close enough" to accurate values.
1015 */
1016 msecs = jiffies_to_msecs(jiffies);
1017 for (emsecs = lcnt = 0; emsecs <= 5UL; lcnt++) {
1018 qib_pio_copy(piobuf + 64, addr, cnt >> 2);
1019 emsecs = jiffies_to_msecs(jiffies) - msecs;
1020 }
1021
1022 /* 1 GiB/sec, slightly over IB SDR line rate */
1023 if (lcnt < (emsecs * 1024U))
1024 qib_dev_err(dd,
7fac3301 1025 "Performance problem: bandwidth to PIO buffers is only %u MiB/sec\n",
f931551b
RC
1026 lcnt / (u32) emsecs);
1027
1028 preempt_enable();
1029
1030 vfree(addr);
1031
1032done:
1033 /* disarm piobuf, so it's available again */
1034 dd->f_sendctrl(dd->pport, QIB_SENDCTRL_DISARM_BUF(pbnum));
1035 qib_sendbuf_done(dd, pbnum);
1036 dd->f_set_armlaunch(dd, 1);
1037}
1038
1039
1040void qib_free_devdata(struct qib_devdata *dd)
1041{
1042 unsigned long flags;
1043
1044 spin_lock_irqsave(&qib_devs_lock, flags);
1045 idr_remove(&qib_unit_table, dd->unit);
1046 list_del(&dd->list);
1047 spin_unlock_irqrestore(&qib_devs_lock, flags);
1048
1049 ib_dealloc_device(&dd->verbs_dev.ibdev);
1050}
1051
1052/*
1053 * Allocate our primary per-unit data structure. Must be done via verbs
1054 * allocator, because the verbs cleanup process both does cleanup and
1055 * free of the data structure.
1056 * "extra" is for chip-specific data.
1057 *
1058 * Use the idr mechanism to get a unit number for this unit.
1059 */
1060struct qib_devdata *qib_alloc_devdata(struct pci_dev *pdev, size_t extra)
1061{
1062 unsigned long flags;
1063 struct qib_devdata *dd;
1064 int ret;
1065
f931551b
RC
1066 dd = (struct qib_devdata *) ib_alloc_device(sizeof(*dd) + extra);
1067 if (!dd) {
1068 dd = ERR_PTR(-ENOMEM);
1069 goto bail;
1070 }
1071
80f22b44 1072 idr_preload(GFP_KERNEL);
f931551b 1073 spin_lock_irqsave(&qib_devs_lock, flags);
80f22b44
TH
1074
1075 ret = idr_alloc(&qib_unit_table, dd, 0, 0, GFP_NOWAIT);
1076 if (ret >= 0) {
1077 dd->unit = ret;
f931551b 1078 list_add(&dd->list, &qib_dev_list);
80f22b44
TH
1079 }
1080
f931551b 1081 spin_unlock_irqrestore(&qib_devs_lock, flags);
80f22b44 1082 idr_preload_end();
f931551b
RC
1083
1084 if (ret < 0) {
1085 qib_early_err(&pdev->dev,
1086 "Could not allocate unit ID: error %d\n", -ret);
1087 ib_dealloc_device(&dd->verbs_dev.ibdev);
1088 dd = ERR_PTR(ret);
1089 goto bail;
1090 }
1091
1092 if (!qib_cpulist_count) {
1093 u32 count = num_online_cpus();
1094 qib_cpulist = kzalloc(BITS_TO_LONGS(count) *
1095 sizeof(long), GFP_KERNEL);
1096 if (qib_cpulist)
1097 qib_cpulist_count = count;
1098 else
7fac3301
MM
1099 qib_early_err(&pdev->dev,
1100 "Could not alloc cpulist info, cpu affinity might be wrong\n");
f931551b
RC
1101 }
1102
1103bail:
1104 return dd;
1105}
1106
1107/*
1108 * Called from freeze mode handlers, and from PCI error
1109 * reporting code. Should be paranoid about state of
1110 * system and data structures.
1111 */
1112void qib_disable_after_error(struct qib_devdata *dd)
1113{
1114 if (dd->flags & QIB_INITTED) {
1115 u32 pidx;
1116
1117 dd->flags &= ~QIB_INITTED;
1118 if (dd->pport)
1119 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1120 struct qib_pportdata *ppd;
1121
1122 ppd = dd->pport + pidx;
1123 if (dd->flags & QIB_PRESENT) {
1124 qib_set_linkstate(ppd,
1125 QIB_IB_LINKDOWN_DISABLE);
1126 dd->f_setextled(ppd, 0);
1127 }
1128 *ppd->statusp &= ~QIB_STATUS_IB_READY;
1129 }
1130 }
1131
1132 /*
1133 * Mark as having had an error for driver, and also
1134 * for /sys and status word mapped to user programs.
1135 * This marks unit as not usable, until reset.
1136 */
1137 if (dd->devstatusp)
1138 *dd->devstatusp |= QIB_STATUS_HWERROR;
1139}
1140
1e6d9abe
GKH
1141static void qib_remove_one(struct pci_dev *);
1142static int qib_init_one(struct pci_dev *, const struct pci_device_id *);
f931551b 1143
e2eed58b 1144#define DRIVER_LOAD_MSG "Intel " QIB_DRV_NAME " loaded: "
f931551b
RC
1145#define PFX QIB_DRV_NAME ": "
1146
865b64be 1147static DEFINE_PCI_DEVICE_TABLE(qib_pci_tbl) = {
f931551b
RC
1148 { PCI_DEVICE(PCI_VENDOR_ID_PATHSCALE, PCI_DEVICE_ID_QLOGIC_IB_6120) },
1149 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_IB_7220) },
1150 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_IB_7322) },
1151 { 0, }
1152};
1153
1154MODULE_DEVICE_TABLE(pci, qib_pci_tbl);
1155
1156struct pci_driver qib_driver = {
1157 .name = QIB_DRV_NAME,
1158 .probe = qib_init_one,
1e6d9abe 1159 .remove = qib_remove_one,
f931551b
RC
1160 .id_table = qib_pci_tbl,
1161 .err_handler = &qib_pci_err_handler,
1162};
1163
8469ba39
MM
1164#ifdef CONFIG_INFINIBAND_QIB_DCA
1165
1166static int qib_notify_dca(struct notifier_block *, unsigned long, void *);
1167static struct notifier_block dca_notifier = {
1168 .notifier_call = qib_notify_dca,
1169 .next = NULL,
1170 .priority = 0
1171};
1172
1173static int qib_notify_dca_device(struct device *device, void *data)
1174{
1175 struct qib_devdata *dd = dev_get_drvdata(device);
1176 unsigned long event = *(unsigned long *)data;
1177
1178 return dd->f_notify_dca(dd, event);
1179}
1180
1181static int qib_notify_dca(struct notifier_block *nb, unsigned long event,
1182 void *p)
1183{
1184 int rval;
1185
1186 rval = driver_for_each_device(&qib_driver.driver, NULL,
1187 &event, qib_notify_dca_device);
1188 return rval ? NOTIFY_BAD : NOTIFY_DONE;
1189}
1190
1191#endif
1192
f931551b
RC
1193/*
1194 * Do all the generic driver unit- and chip-independent memory
1195 * allocation and initialization.
1196 */
1197static int __init qlogic_ib_init(void)
1198{
1199 int ret;
1200
1201 ret = qib_dev_init();
1202 if (ret)
1203 goto bail;
1204
950aff53 1205 qib_cq_wq = create_singlethread_workqueue("qib_cq");
f931551b
RC
1206 if (!qib_cq_wq) {
1207 ret = -ENOMEM;
f0626710 1208 goto bail_dev;
f931551b
RC
1209 }
1210
1211 /*
1212 * These must be called before the driver is registered with
1213 * the PCI subsystem.
1214 */
1215 idr_init(&qib_unit_table);
f931551b 1216
8469ba39
MM
1217#ifdef CONFIG_INFINIBAND_QIB_DCA
1218 dca_register_notify(&dca_notifier);
1219#endif
f931551b
RC
1220 ret = pci_register_driver(&qib_driver);
1221 if (ret < 0) {
7fac3301 1222 pr_err("Unable to register driver: error %d\n", -ret);
f931551b
RC
1223 goto bail_unit;
1224 }
1225
1226 /* not fatal if it doesn't work */
1227 if (qib_init_qibfs())
7fac3301 1228 pr_err("Unable to register ipathfs\n");
f931551b
RC
1229 goto bail; /* all OK */
1230
1231bail_unit:
8469ba39
MM
1232#ifdef CONFIG_INFINIBAND_QIB_DCA
1233 dca_unregister_notify(&dca_notifier);
1234#endif
f931551b 1235 idr_destroy(&qib_unit_table);
f931551b 1236 destroy_workqueue(qib_cq_wq);
f931551b
RC
1237bail_dev:
1238 qib_dev_cleanup();
1239bail:
1240 return ret;
1241}
1242
1243module_init(qlogic_ib_init);
1244
1245/*
1246 * Do the non-unit driver cleanup, memory free, etc. at unload.
1247 */
1248static void __exit qlogic_ib_cleanup(void)
1249{
1250 int ret;
1251
1252 ret = qib_exit_qibfs();
1253 if (ret)
7fac3301
MM
1254 pr_err(
1255 "Unable to cleanup counter filesystem: error %d\n",
1256 -ret);
f931551b 1257
8469ba39
MM
1258#ifdef CONFIG_INFINIBAND_QIB_DCA
1259 dca_unregister_notify(&dca_notifier);
1260#endif
f931551b
RC
1261 pci_unregister_driver(&qib_driver);
1262
f931551b
RC
1263 destroy_workqueue(qib_cq_wq);
1264
1265 qib_cpulist_count = 0;
1266 kfree(qib_cpulist);
1267
1268 idr_destroy(&qib_unit_table);
1269 qib_dev_cleanup();
1270}
1271
1272module_exit(qlogic_ib_cleanup);
1273
1274/* this can only be called after a successful initialization */
1275static void cleanup_device_data(struct qib_devdata *dd)
1276{
1277 int ctxt;
1278 int pidx;
1279 struct qib_ctxtdata **tmp;
1280 unsigned long flags;
1281
1282 /* users can't do anything more with chip */
36a8f01c 1283 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
f931551b
RC
1284 if (dd->pport[pidx].statusp)
1285 *dd->pport[pidx].statusp &= ~QIB_STATUS_CHIP_PRESENT;
1286
36a8f01c
MM
1287 spin_lock(&dd->pport[pidx].cc_shadow_lock);
1288
1289 kfree(dd->pport[pidx].congestion_entries);
1290 dd->pport[pidx].congestion_entries = NULL;
1291 kfree(dd->pport[pidx].ccti_entries);
1292 dd->pport[pidx].ccti_entries = NULL;
1293 kfree(dd->pport[pidx].ccti_entries_shadow);
1294 dd->pport[pidx].ccti_entries_shadow = NULL;
1295 kfree(dd->pport[pidx].congestion_entries_shadow);
1296 dd->pport[pidx].congestion_entries_shadow = NULL;
1297
1298 spin_unlock(&dd->pport[pidx].cc_shadow_lock);
1299 }
1300
f931551b
RC
1301 if (!qib_wc_pat)
1302 qib_disable_wc(dd);
1303
1304 if (dd->pioavailregs_dma) {
1305 dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
1306 (void *) dd->pioavailregs_dma,
1307 dd->pioavailregs_phys);
1308 dd->pioavailregs_dma = NULL;
1309 }
1310
1311 if (dd->pageshadow) {
1312 struct page **tmpp = dd->pageshadow;
1313 dma_addr_t *tmpd = dd->physshadow;
1314 int i, cnt = 0;
1315
1316 for (ctxt = 0; ctxt < dd->cfgctxts; ctxt++) {
1317 int ctxt_tidbase = ctxt * dd->rcvtidcnt;
1318 int maxtid = ctxt_tidbase + dd->rcvtidcnt;
1319
1320 for (i = ctxt_tidbase; i < maxtid; i++) {
1321 if (!tmpp[i])
1322 continue;
1323 pci_unmap_page(dd->pcidev, tmpd[i],
1324 PAGE_SIZE, PCI_DMA_FROMDEVICE);
1325 qib_release_user_pages(&tmpp[i], 1);
1326 tmpp[i] = NULL;
1327 cnt++;
1328 }
1329 }
1330
1331 tmpp = dd->pageshadow;
1332 dd->pageshadow = NULL;
1333 vfree(tmpp);
1334 }
1335
1336 /*
1337 * Free any resources still in use (usually just kernel contexts)
1338 * at unload; we do for ctxtcnt, because that's what we allocate.
1339 * We acquire lock to be really paranoid that rcd isn't being
1340 * accessed from some interrupt-related code (that should not happen,
1341 * but best to be sure).
1342 */
1343 spin_lock_irqsave(&dd->uctxt_lock, flags);
1344 tmp = dd->rcd;
1345 dd->rcd = NULL;
1346 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
1347 for (ctxt = 0; tmp && ctxt < dd->ctxtcnt; ctxt++) {
1348 struct qib_ctxtdata *rcd = tmp[ctxt];
1349
1350 tmp[ctxt] = NULL; /* debugging paranoia */
1351 qib_free_ctxtdata(dd, rcd);
1352 }
1353 kfree(tmp);
1354 kfree(dd->boardname);
1355}
1356
1357/*
1358 * Clean up on unit shutdown, or error during unit load after
1359 * successful initialization.
1360 */
1361static void qib_postinit_cleanup(struct qib_devdata *dd)
1362{
1363 /*
1364 * Clean up chip-specific stuff.
1365 * We check for NULL here, because it's outside
1366 * the kregbase check, and we need to call it
1367 * after the free_irq. Thus it's possible that
1368 * the function pointers were never initialized.
1369 */
1370 if (dd->f_cleanup)
1371 dd->f_cleanup(dd);
1372
1373 qib_pcie_ddcleanup(dd);
1374
1375 cleanup_device_data(dd);
1376
1377 qib_free_devdata(dd);
1378}
1379
1e6d9abe 1380static int qib_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
f931551b
RC
1381{
1382 int ret, j, pidx, initfail;
1383 struct qib_devdata *dd = NULL;
1384
1385 ret = qib_pcie_init(pdev, ent);
1386 if (ret)
1387 goto bail;
1388
1389 /*
1390 * Do device-specific initialiation, function table setup, dd
1391 * allocation, etc.
1392 */
1393 switch (ent->device) {
1394 case PCI_DEVICE_ID_QLOGIC_IB_6120:
7e3a1f4a 1395#ifdef CONFIG_PCI_MSI
f931551b 1396 dd = qib_init_iba6120_funcs(pdev, ent);
7e3a1f4a 1397#else
7fac3301 1398 qib_early_err(&pdev->dev,
e2eed58b 1399 "Intel PCIE device 0x%x cannot work if CONFIG_PCI_MSI is not enabled\n",
7fac3301 1400 ent->device);
9e43e010 1401 dd = ERR_PTR(-ENODEV);
7e3a1f4a 1402#endif
f931551b
RC
1403 break;
1404
1405 case PCI_DEVICE_ID_QLOGIC_IB_7220:
1406 dd = qib_init_iba7220_funcs(pdev, ent);
1407 break;
1408
1409 case PCI_DEVICE_ID_QLOGIC_IB_7322:
1410 dd = qib_init_iba7322_funcs(pdev, ent);
1411 break;
1412
1413 default:
7fac3301 1414 qib_early_err(&pdev->dev,
e2eed58b 1415 "Failing on unknown Intel deviceid 0x%x\n",
7fac3301 1416 ent->device);
f931551b
RC
1417 ret = -ENODEV;
1418 }
1419
1420 if (IS_ERR(dd))
1421 ret = PTR_ERR(dd);
1422 if (ret)
1423 goto bail; /* error already printed */
1424
551ace12
MM
1425 ret = qib_create_workqueues(dd);
1426 if (ret)
1427 goto bail;
1428
f931551b
RC
1429 /* do the generic initialization */
1430 initfail = qib_init(dd, 0);
1431
1432 ret = qib_register_ib_device(dd);
1433
1434 /*
1435 * Now ready for use. this should be cleared whenever we
1436 * detect a reset, or initiate one. If earlier failure,
1437 * we still create devices, so diags, etc. can be used
1438 * to determine cause of problem.
1439 */
1440 if (!qib_mini_init && !initfail && !ret)
1441 dd->flags |= QIB_INITTED;
1442
1443 j = qib_device_create(dd);
1444 if (j)
1445 qib_dev_err(dd, "Failed to create /dev devices: %d\n", -j);
1446 j = qibfs_add(dd);
1447 if (j)
1448 qib_dev_err(dd, "Failed filesystem setup for counters: %d\n",
1449 -j);
1450
1451 if (qib_mini_init || initfail || ret) {
1452 qib_stop_timers(dd);
f0626710 1453 flush_workqueue(ib_wq);
f931551b
RC
1454 for (pidx = 0; pidx < dd->num_pports; ++pidx)
1455 dd->f_quiet_serdes(dd->pport + pidx);
756a33b8
RC
1456 if (qib_mini_init)
1457 goto bail;
1458 if (!j) {
1459 (void) qibfs_remove(dd);
1460 qib_device_remove(dd);
1461 }
1462 if (!ret)
1463 qib_unregister_ib_device(dd);
1464 qib_postinit_cleanup(dd);
f931551b
RC
1465 if (initfail)
1466 ret = initfail;
1467 goto bail;
1468 }
1469
1470 if (!qib_wc_pat) {
1471 ret = qib_enable_wc(dd);
1472 if (ret) {
7fac3301
MM
1473 qib_dev_err(dd,
1474 "Write combining not enabled (err %d): performance may be poor\n",
1475 -ret);
f931551b
RC
1476 ret = 0;
1477 }
1478 }
1479
1480 qib_verify_pioperf(dd);
1481bail:
1482 return ret;
1483}
1484
1e6d9abe 1485static void qib_remove_one(struct pci_dev *pdev)
f931551b
RC
1486{
1487 struct qib_devdata *dd = pci_get_drvdata(pdev);
1488 int ret;
1489
1490 /* unregister from IB core */
1491 qib_unregister_ib_device(dd);
1492
1493 /*
1494 * Disable the IB link, disable interrupts on the device,
1495 * clear dma engines, etc.
1496 */
1497 if (!qib_mini_init)
1498 qib_shutdown_device(dd);
1499
1500 qib_stop_timers(dd);
1501
f0626710
TH
1502 /* wait until all of our (qsfp) queue_work() calls complete */
1503 flush_workqueue(ib_wq);
f931551b
RC
1504
1505 ret = qibfs_remove(dd);
1506 if (ret)
1507 qib_dev_err(dd, "Failed counters filesystem cleanup: %d\n",
1508 -ret);
1509
1510 qib_device_remove(dd);
1511
1512 qib_postinit_cleanup(dd);
1513}
1514
1515/**
1516 * qib_create_rcvhdrq - create a receive header queue
1517 * @dd: the qlogic_ib device
1518 * @rcd: the context data
1519 *
1520 * This must be contiguous memory (from an i/o perspective), and must be
1521 * DMA'able (which means for some systems, it will go through an IOMMU,
1522 * or be forced into a low address range).
1523 */
1524int qib_create_rcvhdrq(struct qib_devdata *dd, struct qib_ctxtdata *rcd)
1525{
1526 unsigned amt;
1527
1528 if (!rcd->rcvhdrq) {
1529 dma_addr_t phys_hdrqtail;
1530 gfp_t gfp_flags;
1531
1532 amt = ALIGN(dd->rcvhdrcnt * dd->rcvhdrentsize *
1533 sizeof(u32), PAGE_SIZE);
1534 gfp_flags = (rcd->ctxt >= dd->first_user_ctxt) ?
1535 GFP_USER : GFP_KERNEL;
1536 rcd->rcvhdrq = dma_alloc_coherent(
1537 &dd->pcidev->dev, amt, &rcd->rcvhdrq_phys,
1538 gfp_flags | __GFP_COMP);
1539
1540 if (!rcd->rcvhdrq) {
7fac3301
MM
1541 qib_dev_err(dd,
1542 "attempt to allocate %d bytes for ctxt %u rcvhdrq failed\n",
1543 amt, rcd->ctxt);
f931551b
RC
1544 goto bail;
1545 }
1546
1547 if (rcd->ctxt >= dd->first_user_ctxt) {
1548 rcd->user_event_mask = vmalloc_user(PAGE_SIZE);
1549 if (!rcd->user_event_mask)
1550 goto bail_free_hdrq;
1551 }
1552
1553 if (!(dd->flags & QIB_NODMA_RTAIL)) {
1554 rcd->rcvhdrtail_kvaddr = dma_alloc_coherent(
1555 &dd->pcidev->dev, PAGE_SIZE, &phys_hdrqtail,
1556 gfp_flags);
1557 if (!rcd->rcvhdrtail_kvaddr)
1558 goto bail_free;
1559 rcd->rcvhdrqtailaddr_phys = phys_hdrqtail;
1560 }
1561
1562 rcd->rcvhdrq_size = amt;
1563 }
1564
1565 /* clear for security and sanity on each use */
1566 memset(rcd->rcvhdrq, 0, rcd->rcvhdrq_size);
1567 if (rcd->rcvhdrtail_kvaddr)
1568 memset(rcd->rcvhdrtail_kvaddr, 0, PAGE_SIZE);
1569 return 0;
1570
1571bail_free:
7fac3301
MM
1572 qib_dev_err(dd,
1573 "attempt to allocate 1 page for ctxt %u rcvhdrqtailaddr failed\n",
1574 rcd->ctxt);
f931551b
RC
1575 vfree(rcd->user_event_mask);
1576 rcd->user_event_mask = NULL;
1577bail_free_hdrq:
1578 dma_free_coherent(&dd->pcidev->dev, amt, rcd->rcvhdrq,
1579 rcd->rcvhdrq_phys);
1580 rcd->rcvhdrq = NULL;
1581bail:
1582 return -ENOMEM;
1583}
1584
1585/**
1586 * allocate eager buffers, both kernel and user contexts.
1587 * @rcd: the context we are setting up.
1588 *
1589 * Allocate the eager TID buffers and program them into hip.
1590 * They are no longer completely contiguous, we do multiple allocation
1591 * calls. Otherwise we get the OOM code involved, by asking for too
1592 * much per call, with disastrous results on some kernels.
1593 */
1594int qib_setup_eagerbufs(struct qib_ctxtdata *rcd)
1595{
1596 struct qib_devdata *dd = rcd->dd;
1597 unsigned e, egrcnt, egrperchunk, chunk, egrsize, egroff;
1598 size_t size;
1599 gfp_t gfp_flags;
1600
1601 /*
1602 * GFP_USER, but without GFP_FS, so buffer cache can be
1603 * coalesced (we hope); otherwise, even at order 4,
1604 * heavy filesystem activity makes these fail, and we can
1605 * use compound pages.
1606 */
1607 gfp_flags = __GFP_WAIT | __GFP_IO | __GFP_COMP;
1608
1609 egrcnt = rcd->rcvegrcnt;
1610 egroff = rcd->rcvegr_tid_base;
1611 egrsize = dd->rcvegrbufsize;
1612
1613 chunk = rcd->rcvegrbuf_chunks;
1614 egrperchunk = rcd->rcvegrbufs_perchunk;
1615 size = rcd->rcvegrbuf_size;
1616 if (!rcd->rcvegrbuf) {
1617 rcd->rcvegrbuf =
1618 kzalloc(chunk * sizeof(rcd->rcvegrbuf[0]),
1619 GFP_KERNEL);
1620 if (!rcd->rcvegrbuf)
1621 goto bail;
1622 }
1623 if (!rcd->rcvegrbuf_phys) {
1624 rcd->rcvegrbuf_phys =
1625 kmalloc(chunk * sizeof(rcd->rcvegrbuf_phys[0]),
1626 GFP_KERNEL);
1627 if (!rcd->rcvegrbuf_phys)
1628 goto bail_rcvegrbuf;
1629 }
1630 for (e = 0; e < rcd->rcvegrbuf_chunks; e++) {
1631 if (rcd->rcvegrbuf[e])
1632 continue;
1633 rcd->rcvegrbuf[e] =
1634 dma_alloc_coherent(&dd->pcidev->dev, size,
1635 &rcd->rcvegrbuf_phys[e],
1636 gfp_flags);
1637 if (!rcd->rcvegrbuf[e])
1638 goto bail_rcvegrbuf_phys;
1639 }
1640
1641 rcd->rcvegr_phys = rcd->rcvegrbuf_phys[0];
1642
1643 for (e = chunk = 0; chunk < rcd->rcvegrbuf_chunks; chunk++) {
1644 dma_addr_t pa = rcd->rcvegrbuf_phys[chunk];
1645 unsigned i;
1646
5df4223a
RC
1647 /* clear for security and sanity on each use */
1648 memset(rcd->rcvegrbuf[chunk], 0, size);
1649
f931551b
RC
1650 for (i = 0; e < egrcnt && i < egrperchunk; e++, i++) {
1651 dd->f_put_tid(dd, e + egroff +
1652 (u64 __iomem *)
1653 ((char __iomem *)
1654 dd->kregbase +
1655 dd->rcvegrbase),
1656 RCVHQ_RCV_TYPE_EAGER, pa);
1657 pa += egrsize;
1658 }
1659 cond_resched(); /* don't hog the cpu */
1660 }
1661
1662 return 0;
1663
1664bail_rcvegrbuf_phys:
1665 for (e = 0; e < rcd->rcvegrbuf_chunks && rcd->rcvegrbuf[e]; e++)
1666 dma_free_coherent(&dd->pcidev->dev, size,
1667 rcd->rcvegrbuf[e], rcd->rcvegrbuf_phys[e]);
1668 kfree(rcd->rcvegrbuf_phys);
1669 rcd->rcvegrbuf_phys = NULL;
1670bail_rcvegrbuf:
1671 kfree(rcd->rcvegrbuf);
1672 rcd->rcvegrbuf = NULL;
1673bail:
1674 return -ENOMEM;
1675}
1676
fce24a9d
DO
1677/*
1678 * Note: Changes to this routine should be mirrored
1679 * for the diagnostics routine qib_remap_ioaddr32().
1680 * There is also related code for VL15 buffers in qib_init_7322_variables().
1681 * The teardown code that unmaps is in qib_pcie_ddcleanup()
1682 */
f931551b
RC
1683int init_chip_wc_pat(struct qib_devdata *dd, u32 vl15buflen)
1684{
1685 u64 __iomem *qib_kregbase = NULL;
1686 void __iomem *qib_piobase = NULL;
1687 u64 __iomem *qib_userbase = NULL;
1688 u64 qib_kreglen;
1689 u64 qib_pio2koffset = dd->piobufbase & 0xffffffff;
1690 u64 qib_pio4koffset = dd->piobufbase >> 32;
1691 u64 qib_pio2klen = dd->piobcnt2k * dd->palign;
1692 u64 qib_pio4klen = dd->piobcnt4k * dd->align4k;
1693 u64 qib_physaddr = dd->physaddr;
1694 u64 qib_piolen;
1695 u64 qib_userlen = 0;
1696
1697 /*
1698 * Free the old mapping because the kernel will try to reuse the
1699 * old mapping and not create a new mapping with the
1700 * write combining attribute.
1701 */
1702 iounmap(dd->kregbase);
1703 dd->kregbase = NULL;
1704
1705 /*
1706 * Assumes chip address space looks like:
1707 * - kregs + sregs + cregs + uregs (in any order)
1708 * - piobufs (2K and 4K bufs in either order)
1709 * or:
1710 * - kregs + sregs + cregs (in any order)
1711 * - piobufs (2K and 4K bufs in either order)
1712 * - uregs
1713 */
1714 if (dd->piobcnt4k == 0) {
1715 qib_kreglen = qib_pio2koffset;
1716 qib_piolen = qib_pio2klen;
1717 } else if (qib_pio2koffset < qib_pio4koffset) {
1718 qib_kreglen = qib_pio2koffset;
1719 qib_piolen = qib_pio4koffset + qib_pio4klen - qib_kreglen;
1720 } else {
1721 qib_kreglen = qib_pio4koffset;
1722 qib_piolen = qib_pio2koffset + qib_pio2klen - qib_kreglen;
1723 }
1724 qib_piolen += vl15buflen;
1725 /* Map just the configured ports (not all hw ports) */
1726 if (dd->uregbase > qib_kreglen)
1727 qib_userlen = dd->ureg_align * dd->cfgctxts;
1728
1729 /* Sanity checks passed, now create the new mappings */
1730 qib_kregbase = ioremap_nocache(qib_physaddr, qib_kreglen);
1731 if (!qib_kregbase)
1732 goto bail;
1733
1734 qib_piobase = ioremap_wc(qib_physaddr + qib_kreglen, qib_piolen);
1735 if (!qib_piobase)
1736 goto bail_kregbase;
1737
1738 if (qib_userlen) {
1739 qib_userbase = ioremap_nocache(qib_physaddr + dd->uregbase,
1740 qib_userlen);
1741 if (!qib_userbase)
1742 goto bail_piobase;
1743 }
1744
1745 dd->kregbase = qib_kregbase;
1746 dd->kregend = (u64 __iomem *)
1747 ((char __iomem *) qib_kregbase + qib_kreglen);
1748 dd->piobase = qib_piobase;
1749 dd->pio2kbase = (void __iomem *)
1750 (((char __iomem *) dd->piobase) +
1751 qib_pio2koffset - qib_kreglen);
1752 if (dd->piobcnt4k)
1753 dd->pio4kbase = (void __iomem *)
1754 (((char __iomem *) dd->piobase) +
1755 qib_pio4koffset - qib_kreglen);
1756 if (qib_userlen)
1757 /* ureg will now be accessed relative to dd->userbase */
1758 dd->userbase = qib_userbase;
1759 return 0;
1760
1761bail_piobase:
1762 iounmap(qib_piobase);
1763bail_kregbase:
1764 iounmap(qib_kregbase);
1765bail:
1766 return -ENOMEM;
1767}
This page took 0.258562 seconds and 5 git commands to generate.