IB/ipath: Print better error messages if kernel is misconfigured
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / infiniband / hw / ipath / ipath_driver.c
CommitLineData
7bb206e3 1/*
759d5768 2 * Copyright (c) 2006 QLogic, Inc. All rights reserved.
7bb206e3
BS
3 * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34#include <linux/spinlock.h>
35#include <linux/idr.h>
36#include <linux/pci.h>
37#include <linux/delay.h>
38#include <linux/netdevice.h>
39#include <linux/vmalloc.h>
40
41#include "ipath_kernel.h"
b1c1b6a3 42#include "ipath_verbs.h"
27b678dd 43#include "ipath_common.h"
7bb206e3
BS
44
45static void ipath_update_pio_bufs(struct ipath_devdata *);
46
47const char *ipath_get_unit_name(int unit)
48{
49 static char iname[16];
50 snprintf(iname, sizeof iname, "infinipath%u", unit);
51 return iname;
52}
53
759d5768 54#define DRIVER_LOAD_MSG "QLogic " IPATH_DRV_NAME " loaded: "
7bb206e3
BS
55#define PFX IPATH_DRV_NAME ": "
56
57/*
58 * The size has to be longer than this string, so we can append
59 * board/chip information to it in the init code.
60 */
b55f4f06 61const char ib_ipath_version[] = IPATH_IDSTR "\n";
7bb206e3
BS
62
63static struct idr unit_table;
64DEFINE_SPINLOCK(ipath_devs_lock);
65LIST_HEAD(ipath_dev_list);
66
0fd41363 67wait_queue_head_t ipath_state_wait;
7bb206e3
BS
68
69unsigned ipath_debug = __IPATH_INFO;
70
71module_param_named(debug, ipath_debug, uint, S_IWUSR | S_IRUGO);
72MODULE_PARM_DESC(debug, "mask for debug prints");
73EXPORT_SYMBOL_GPL(ipath_debug);
74
75MODULE_LICENSE("GPL");
759d5768
BS
76MODULE_AUTHOR("QLogic <support@pathscale.com>");
77MODULE_DESCRIPTION("QLogic InfiniPath driver");
7bb206e3
BS
78
79const char *ipath_ibcstatus_str[] = {
80 "Disabled",
81 "LinkUp",
82 "PollActive",
83 "PollQuiet",
84 "SleepDelay",
85 "SleepQuiet",
86 "LState6", /* unused */
87 "LState7", /* unused */
88 "CfgDebounce",
89 "CfgRcvfCfg",
90 "CfgWaitRmt",
91 "CfgIdle",
92 "RecovRetrain",
93 "LState0xD", /* unused */
94 "RecovWaitRmt",
95 "RecovIdle",
96};
97
7bb206e3
BS
98static void __devexit ipath_remove_one(struct pci_dev *);
99static int __devinit ipath_init_one(struct pci_dev *,
100 const struct pci_device_id *);
101
102/* Only needed for registration, nothing else needs this info */
103#define PCI_VENDOR_ID_PATHSCALE 0x1fc1
104#define PCI_DEVICE_ID_INFINIPATH_HT 0xd
105#define PCI_DEVICE_ID_INFINIPATH_PE800 0x10
106
107static const struct pci_device_id ipath_pci_tbl[] = {
6f4bb3d8
RD
108 { PCI_DEVICE(PCI_VENDOR_ID_PATHSCALE, PCI_DEVICE_ID_INFINIPATH_HT) },
109 { PCI_DEVICE(PCI_VENDOR_ID_PATHSCALE, PCI_DEVICE_ID_INFINIPATH_PE800) },
110 { 0, }
7bb206e3
BS
111};
112
113MODULE_DEVICE_TABLE(pci, ipath_pci_tbl);
114
115static struct pci_driver ipath_driver = {
116 .name = IPATH_DRV_NAME,
117 .probe = ipath_init_one,
118 .remove = __devexit_p(ipath_remove_one),
119 .id_table = ipath_pci_tbl,
120};
121
7bb206e3
BS
122
123static inline void read_bars(struct ipath_devdata *dd, struct pci_dev *dev,
124 u32 *bar0, u32 *bar1)
125{
126 int ret;
127
128 ret = pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, bar0);
129 if (ret)
130 ipath_dev_err(dd, "failed to read bar0 before enable: "
131 "error %d\n", -ret);
132
133 ret = pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, bar1);
134 if (ret)
135 ipath_dev_err(dd, "failed to read bar1 before enable: "
136 "error %d\n", -ret);
137
138 ipath_dbg("Read bar0 %x bar1 %x\n", *bar0, *bar1);
139}
140
141static void ipath_free_devdata(struct pci_dev *pdev,
142 struct ipath_devdata *dd)
143{
144 unsigned long flags;
145
146 pci_set_drvdata(pdev, NULL);
147
148 if (dd->ipath_unit != -1) {
149 spin_lock_irqsave(&ipath_devs_lock, flags);
150 idr_remove(&unit_table, dd->ipath_unit);
151 list_del(&dd->ipath_list);
152 spin_unlock_irqrestore(&ipath_devs_lock, flags);
153 }
06993ca6 154 vfree(dd);
7bb206e3
BS
155}
156
157static struct ipath_devdata *ipath_alloc_devdata(struct pci_dev *pdev)
158{
159 unsigned long flags;
160 struct ipath_devdata *dd;
7bb206e3
BS
161 int ret;
162
163 if (!idr_pre_get(&unit_table, GFP_KERNEL)) {
164 dd = ERR_PTR(-ENOMEM);
165 goto bail;
166 }
167
06993ca6 168 dd = vmalloc(sizeof(*dd));
7bb206e3
BS
169 if (!dd) {
170 dd = ERR_PTR(-ENOMEM);
171 goto bail;
172 }
06993ca6 173 memset(dd, 0, sizeof(*dd));
7bb206e3
BS
174 dd->ipath_unit = -1;
175
176 spin_lock_irqsave(&ipath_devs_lock, flags);
177
178 ret = idr_get_new(&unit_table, dd, &dd->ipath_unit);
179 if (ret < 0) {
180 printk(KERN_ERR IPATH_DRV_NAME
181 ": Could not allocate unit ID: error %d\n", -ret);
182 ipath_free_devdata(pdev, dd);
183 dd = ERR_PTR(ret);
184 goto bail_unlock;
185 }
186
187 dd->pcidev = pdev;
188 pci_set_drvdata(pdev, dd);
189
190 list_add(&dd->ipath_list, &ipath_dev_list);
191
192bail_unlock:
193 spin_unlock_irqrestore(&ipath_devs_lock, flags);
194
195bail:
196 return dd;
197}
198
199static inline struct ipath_devdata *__ipath_lookup(int unit)
200{
201 return idr_find(&unit_table, unit);
202}
203
204struct ipath_devdata *ipath_lookup(int unit)
205{
206 struct ipath_devdata *dd;
207 unsigned long flags;
208
209 spin_lock_irqsave(&ipath_devs_lock, flags);
210 dd = __ipath_lookup(unit);
211 spin_unlock_irqrestore(&ipath_devs_lock, flags);
212
213 return dd;
214}
215
216int ipath_count_units(int *npresentp, int *nupp, u32 *maxportsp)
217{
218 int nunits, npresent, nup;
219 struct ipath_devdata *dd;
220 unsigned long flags;
221 u32 maxports;
222
223 nunits = npresent = nup = maxports = 0;
224
225 spin_lock_irqsave(&ipath_devs_lock, flags);
226
227 list_for_each_entry(dd, &ipath_dev_list, ipath_list) {
228 nunits++;
229 if ((dd->ipath_flags & IPATH_PRESENT) && dd->ipath_kregbase)
230 npresent++;
231 if (dd->ipath_lid &&
232 !(dd->ipath_flags & (IPATH_DISABLED | IPATH_LINKDOWN
233 | IPATH_LINKUNK)))
234 nup++;
235 if (dd->ipath_cfgports > maxports)
236 maxports = dd->ipath_cfgports;
237 }
238
239 spin_unlock_irqrestore(&ipath_devs_lock, flags);
240
241 if (npresentp)
242 *npresentp = npresent;
243 if (nupp)
244 *nupp = nup;
245 if (maxportsp)
246 *maxportsp = maxports;
247
248 return nunits;
249}
250
7bb206e3
BS
251/*
252 * These next two routines are placeholders in case we don't have per-arch
253 * code for controlling write combining. If explicit control of write
254 * combining is not available, performance will probably be awful.
255 */
256
257int __attribute__((weak)) ipath_enable_wc(struct ipath_devdata *dd)
258{
259 return -EOPNOTSUPP;
260}
261
262void __attribute__((weak)) ipath_disable_wc(struct ipath_devdata *dd)
263{
264}
265
266static int __devinit ipath_init_one(struct pci_dev *pdev,
267 const struct pci_device_id *ent)
268{
269 int ret, len, j;
270 struct ipath_devdata *dd;
271 unsigned long long addr;
272 u32 bar0 = 0, bar1 = 0;
273 u8 rev;
274
7bb206e3
BS
275 dd = ipath_alloc_devdata(pdev);
276 if (IS_ERR(dd)) {
277 ret = PTR_ERR(dd);
278 printk(KERN_ERR IPATH_DRV_NAME
279 ": Could not allocate devdata: error %d\n", -ret);
f37bda92 280 goto bail;
7bb206e3
BS
281 }
282
283 ipath_cdbg(VERBOSE, "initializing unit #%u\n", dd->ipath_unit);
284
285 read_bars(dd, pdev, &bar0, &bar1);
286
287 ret = pci_enable_device(pdev);
288 if (ret) {
289 /* This can happen iff:
290 *
291 * We did a chip reset, and then failed to reprogram the
292 * BAR, or the chip reset due to an internal error. We then
293 * unloaded the driver and reloaded it.
294 *
295 * Both reset cases set the BAR back to initial state. For
296 * the latter case, the AER sticky error bit at offset 0x718
297 * should be set, but the Linux kernel doesn't yet know
298 * about that, it appears. If the original BAR was retained
299 * in the kernel data structures, this may be OK.
300 */
301 ipath_dev_err(dd, "enable unit %d failed: error %d\n",
302 dd->ipath_unit, -ret);
303 goto bail_devdata;
304 }
305 addr = pci_resource_start(pdev, 0);
306 len = pci_resource_len(pdev, 0);
51f65ebc 307 ipath_cdbg(VERBOSE, "regbase (0) %llx len %d pdev->irq %d, vend %x/%x "
7bb206e3
BS
308 "driver_data %lx\n", addr, len, pdev->irq, ent->vendor,
309 ent->device, ent->driver_data);
310
311 read_bars(dd, pdev, &bar0, &bar1);
312
313 if (!bar1 && !(bar0 & ~0xf)) {
314 if (addr) {
315 dev_info(&pdev->dev, "BAR is 0 (probable RESET), "
316 "rewriting as %llx\n", addr);
317 ret = pci_write_config_dword(
318 pdev, PCI_BASE_ADDRESS_0, addr);
319 if (ret) {
320 ipath_dev_err(dd, "rewrite of BAR0 "
321 "failed: err %d\n", -ret);
322 goto bail_disable;
323 }
324 ret = pci_write_config_dword(
325 pdev, PCI_BASE_ADDRESS_1, addr >> 32);
326 if (ret) {
327 ipath_dev_err(dd, "rewrite of BAR1 "
328 "failed: err %d\n", -ret);
329 goto bail_disable;
330 }
331 } else {
332 ipath_dev_err(dd, "BAR is 0 (probable RESET), "
333 "not usable until reboot\n");
334 ret = -ENODEV;
335 goto bail_disable;
336 }
337 }
338
339 ret = pci_request_regions(pdev, IPATH_DRV_NAME);
340 if (ret) {
341 dev_info(&pdev->dev, "pci_request_regions unit %u fails: "
342 "err %d\n", dd->ipath_unit, -ret);
343 goto bail_disable;
344 }
345
346 ret = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
347 if (ret) {
68dd43a1
BS
348 /*
349 * if the 64 bit setup fails, try 32 bit. Some systems
350 * do not setup 64 bit maps on systems with 2GB or less
351 * memory installed.
352 */
353 ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
354 if (ret) {
b1d8865a
BS
355 dev_info(&pdev->dev,
356 "Unable to set DMA mask for unit %u: %d\n",
357 dd->ipath_unit, ret);
68dd43a1
BS
358 goto bail_regions;
359 }
b1d8865a 360 else {
68dd43a1 361 ipath_dbg("No 64bit DMA mask, used 32 bit mask\n");
b1d8865a
BS
362 ret = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
363 if (ret)
364 dev_info(&pdev->dev,
365 "Unable to set DMA consistent mask "
366 "for unit %u: %d\n",
367 dd->ipath_unit, ret);
368
369 }
370 }
371 else {
372 ret = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
373 if (ret)
374 dev_info(&pdev->dev,
375 "Unable to set DMA consistent mask "
376 "for unit %u: %d\n",
377 dd->ipath_unit, ret);
7bb206e3
BS
378 }
379
380 pci_set_master(pdev);
381
382 /*
383 * Save BARs to rewrite after device reset. Save all 64 bits of
384 * BAR, just in case.
385 */
386 dd->ipath_pcibar0 = addr;
387 dd->ipath_pcibar1 = addr >> 32;
388 dd->ipath_deviceid = ent->device; /* save for later use */
389 dd->ipath_vendorid = ent->vendor;
390
391 /* setup the chip-specific functions, as early as possible. */
392 switch (ent->device) {
393 case PCI_DEVICE_ID_INFINIPATH_HT:
820054b7 394#ifdef CONFIG_HT_IRQ
525d0ca1 395 ipath_init_iba6110_funcs(dd);
7bb206e3 396 break;
820054b7
BS
397#else
398 ipath_dev_err(dd, "QLogic HT device 0x%x cannot work if "
399 "CONFIG_HT_IRQ is not enabled\n", ent->device);
400 return -ENODEV;
e757bef2 401#endif
7bb206e3 402 case PCI_DEVICE_ID_INFINIPATH_PE800:
820054b7 403#ifdef CONFIG_PCI_MSI
525d0ca1 404 ipath_init_iba6120_funcs(dd);
7bb206e3 405 break;
820054b7
BS
406#else
407 ipath_dev_err(dd, "QLogic PCIE device 0x%x cannot work if "
408 "CONFIG_PCI_MSI is not enabled\n", ent->device);
409 return -ENODEV;
e757bef2 410#endif
7bb206e3 411 default:
759d5768 412 ipath_dev_err(dd, "Found unknown QLogic deviceid 0x%x, "
7bb206e3
BS
413 "failing\n", ent->device);
414 return -ENODEV;
415 }
416
417 for (j = 0; j < 6; j++) {
418 if (!pdev->resource[j].start)
419 continue;
e29419ff
GKH
420 ipath_cdbg(VERBOSE, "BAR %d start %llx, end %llx, len %llx\n",
421 j, (unsigned long long)pdev->resource[j].start,
422 (unsigned long long)pdev->resource[j].end,
423 (unsigned long long)pci_resource_len(pdev, j));
7bb206e3
BS
424 }
425
426 if (!addr) {
427 ipath_dev_err(dd, "No valid address in BAR 0!\n");
428 ret = -ENODEV;
429 goto bail_regions;
430 }
431
432 dd->ipath_deviceid = ent->device; /* save for later use */
433 dd->ipath_vendorid = ent->vendor;
434
435 ret = pci_read_config_byte(pdev, PCI_REVISION_ID, &rev);
436 if (ret) {
437 ipath_dev_err(dd, "Failed to read PCI revision ID unit "
438 "%u: err %d\n", dd->ipath_unit, -ret);
439 goto bail_regions; /* shouldn't ever happen */
440 }
441 dd->ipath_pcirev = rev;
442
eb9dc6f4
BS
443#if defined(__powerpc__)
444 /* There isn't a generic way to specify writethrough mappings */
445 dd->ipath_kregbase = __ioremap(addr, len,
446 (_PAGE_NO_CACHE|_PAGE_WRITETHRU));
447#else
7bb206e3 448 dd->ipath_kregbase = ioremap_nocache(addr, len);
eb9dc6f4 449#endif
7bb206e3
BS
450
451 if (!dd->ipath_kregbase) {
452 ipath_dbg("Unable to map io addr %llx to kvirt, failing\n",
453 addr);
454 ret = -ENOMEM;
455 goto bail_iounmap;
456 }
457 dd->ipath_kregend = (u64 __iomem *)
458 ((void __iomem *)dd->ipath_kregbase + len);
459 dd->ipath_physaddr = addr; /* used for io_remap, etc. */
460 /* for user mmap */
b35f004d
BS
461 ipath_cdbg(VERBOSE, "mapped io addr %llx to kregbase %p\n",
462 addr, dd->ipath_kregbase);
7bb206e3
BS
463
464 /*
465 * clear ipath_flags here instead of in ipath_init_chip as it is set
466 * by ipath_setup_htconfig.
467 */
468 dd->ipath_flags = 0;
fba75200
BS
469 dd->ipath_lli_counter = 0;
470 dd->ipath_lli_errors = 0;
7bb206e3
BS
471
472 if (dd->ipath_f_bus(dd, pdev))
473 ipath_dev_err(dd, "Failed to setup config space; "
474 "continuing anyway\n");
475
476 /*
dace1453 477 * set up our interrupt handler; IRQF_SHARED probably not needed,
7bb206e3
BS
478 * since MSI interrupts shouldn't be shared but won't hurt for now.
479 * check 0 irq after we return from chip-specific bus setup, since
480 * that can affect this due to setup
481 */
51f65ebc 482 if (!dd->ipath_irq)
7bb206e3
BS
483 ipath_dev_err(dd, "irq is 0, BIOS error? Interrupts won't "
484 "work\n");
485 else {
51f65ebc 486 ret = request_irq(dd->ipath_irq, ipath_intr, IRQF_SHARED,
7bb206e3
BS
487 IPATH_DRV_NAME, dd);
488 if (ret) {
489 ipath_dev_err(dd, "Couldn't setup irq handler, "
51f65ebc 490 "irq=%d: %d\n", dd->ipath_irq, ret);
7bb206e3
BS
491 goto bail_iounmap;
492 }
493 }
494
495 ret = ipath_init_chip(dd, 0); /* do the chip-specific init */
496 if (ret)
7b196e2f 497 goto bail_irqsetup;
7bb206e3
BS
498
499 ret = ipath_enable_wc(dd);
500
501 if (ret) {
502 ipath_dev_err(dd, "Write combining not enabled "
503 "(err %d): performance may be poor\n",
504 -ret);
505 ret = 0;
506 }
507
508 ipath_device_create_group(&pdev->dev, dd);
509 ipathfs_add_device(dd);
510 ipath_user_add(dd);
a2acb2ff 511 ipath_diag_add(dd);
b1c1b6a3 512 ipath_register_ib_device(dd);
7bb206e3
BS
513
514 goto bail;
515
7b196e2f
AJ
516bail_irqsetup:
517 if (pdev->irq) free_irq(pdev->irq, dd);
518
7bb206e3
BS
519bail_iounmap:
520 iounmap((volatile void __iomem *) dd->ipath_kregbase);
521
522bail_regions:
523 pci_release_regions(pdev);
524
525bail_disable:
526 pci_disable_device(pdev);
527
528bail_devdata:
529 ipath_free_devdata(pdev, dd);
530
7bb206e3
BS
531bail:
532 return ret;
533}
534
7227aac4 535static void __devexit cleanup_device(struct ipath_devdata *dd)
7bb206e3 536{
7227aac4 537 int port;
7bb206e3 538
7227aac4 539 ipath_shutdown_device(dd);
7bb206e3 540
7227aac4
BS
541 if (*dd->ipath_statusp & IPATH_STATUS_CHIP_PRESENT) {
542 /* can't do anything more with chip; needs re-init */
543 *dd->ipath_statusp &= ~IPATH_STATUS_CHIP_PRESENT;
544 if (dd->ipath_kregbase) {
545 /*
546 * if we haven't already cleaned up before these are
547 * to ensure any register reads/writes "fail" until
548 * re-init
549 */
550 dd->ipath_kregbase = NULL;
551 dd->ipath_uregbase = 0;
552 dd->ipath_sregbase = 0;
553 dd->ipath_cregbase = 0;
554 dd->ipath_kregsize = 0;
555 }
556 ipath_disable_wc(dd);
557 }
c78f6415 558
7227aac4
BS
559 if (dd->ipath_pioavailregs_dma) {
560 dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
561 (void *) dd->ipath_pioavailregs_dma,
562 dd->ipath_pioavailregs_phys);
563 dd->ipath_pioavailregs_dma = NULL;
564 }
565 if (dd->ipath_dummy_hdrq) {
566 dma_free_coherent(&dd->pcidev->dev,
567 dd->ipath_pd[0]->port_rcvhdrq_size,
568 dd->ipath_dummy_hdrq, dd->ipath_dummy_hdrq_phys);
569 dd->ipath_dummy_hdrq = NULL;
570 }
571
572 if (dd->ipath_pageshadow) {
573 struct page **tmpp = dd->ipath_pageshadow;
574 dma_addr_t *tmpd = dd->ipath_physshadow;
575 int i, cnt = 0;
576
577 ipath_cdbg(VERBOSE, "Unlocking any expTID pages still "
578 "locked\n");
579 for (port = 0; port < dd->ipath_cfgports; port++) {
580 int port_tidbase = port * dd->ipath_rcvtidcnt;
581 int maxtid = port_tidbase + dd->ipath_rcvtidcnt;
582 for (i = port_tidbase; i < maxtid; i++) {
583 if (!tmpp[i])
584 continue;
585 pci_unmap_page(dd->pcidev, tmpd[i],
586 PAGE_SIZE, PCI_DMA_FROMDEVICE);
587 ipath_release_user_pages(&tmpp[i], 1);
588 tmpp[i] = NULL;
589 cnt++;
590 }
591 }
592 if (cnt) {
593 ipath_stats.sps_pageunlocks += cnt;
594 ipath_cdbg(VERBOSE, "There were still %u expTID "
595 "entries locked\n", cnt);
596 }
597 if (ipath_stats.sps_pagelocks ||
598 ipath_stats.sps_pageunlocks)
599 ipath_cdbg(VERBOSE, "%llu pages locked, %llu "
600 "unlocked via ipath_m{un}lock\n",
601 (unsigned long long)
602 ipath_stats.sps_pagelocks,
603 (unsigned long long)
604 ipath_stats.sps_pageunlocks);
605
606 ipath_cdbg(VERBOSE, "Free shadow page tid array at %p\n",
607 dd->ipath_pageshadow);
608 vfree(dd->ipath_pageshadow);
609 dd->ipath_pageshadow = NULL;
c78f6415
BS
610 }
611
7227aac4
BS
612 /*
613 * free any resources still in use (usually just kernel ports)
614 * at unload; we do for portcnt, not cfgports, because cfgports
615 * could have changed while we were loaded.
616 */
617 for (port = 0; port < dd->ipath_portcnt; port++) {
618 struct ipath_portdata *pd = dd->ipath_pd[port];
619 dd->ipath_pd[port] = NULL;
620 ipath_free_pddata(dd, pd);
621 }
622 kfree(dd->ipath_pd);
623 /*
624 * debuggability, in case some cleanup path tries to use it
625 * after this
626 */
627 dd->ipath_pd = NULL;
628}
629
630static void __devexit ipath_remove_one(struct pci_dev *pdev)
631{
632 struct ipath_devdata *dd = pci_get_drvdata(pdev);
633
634 ipath_cdbg(VERBOSE, "removing, pdev=%p, dd=%p\n", pdev, dd);
635
636 if (dd->verbs_dev)
637 ipath_unregister_ib_device(dd->verbs_dev);
638
a2acb2ff
BS
639 ipath_diag_remove(dd);
640 ipath_user_remove(dd);
7bb206e3
BS
641 ipathfs_remove_device(dd);
642 ipath_device_remove_group(&pdev->dev, dd);
7227aac4 643
7bb206e3
BS
644 ipath_cdbg(VERBOSE, "Releasing pci memory regions, dd %p, "
645 "unit %u\n", dd, (u32) dd->ipath_unit);
7227aac4
BS
646
647 cleanup_device(dd);
648
649 /*
650 * turn off rcv, send, and interrupts for all ports, all drivers
651 * should also hard reset the chip here?
652 * free up port 0 (kernel) rcvhdr, egr bufs, and eventually tid bufs
653 * for all versions of the driver, if they were allocated
654 */
51f65ebc
BS
655 if (dd->ipath_irq) {
656 ipath_cdbg(VERBOSE, "unit %u free irq %d\n",
657 dd->ipath_unit, dd->ipath_irq);
658 dd->ipath_f_free_irq(dd);
7227aac4
BS
659 } else
660 ipath_dbg("irq is 0, not doing free_irq "
661 "for unit %u\n", dd->ipath_unit);
662 /*
663 * we check for NULL here, because it's outside
664 * the kregbase check, and we need to call it
665 * after the free_irq. Thus it's possible that
666 * the function pointers were never initialized.
667 */
668 if (dd->ipath_f_cleanup)
669 /* clean up chip-specific stuff */
670 dd->ipath_f_cleanup(dd);
671
672 ipath_cdbg(VERBOSE, "Unmapping kregbase %p\n", dd->ipath_kregbase);
673 iounmap((volatile void __iomem *) dd->ipath_kregbase);
7bb206e3
BS
674 pci_release_regions(pdev);
675 ipath_cdbg(VERBOSE, "calling pci_disable_device\n");
676 pci_disable_device(pdev);
677
678 ipath_free_devdata(pdev, dd);
7bb206e3
BS
679}
680
681/* general driver use */
682DEFINE_MUTEX(ipath_mutex);
683
684static DEFINE_SPINLOCK(ipath_pioavail_lock);
685
686/**
687 * ipath_disarm_piobufs - cancel a range of PIO buffers
688 * @dd: the infinipath device
689 * @first: the first PIO buffer to cancel
690 * @cnt: the number of PIO buffers to cancel
691 *
692 * cancel a range of PIO buffers, used when they might be armed, but
693 * not triggered. Used at init to ensure buffer state, and also user
694 * process close, in case it died while writing to a PIO buffer
695 * Also after errors.
696 */
697void ipath_disarm_piobufs(struct ipath_devdata *dd, unsigned first,
698 unsigned cnt)
699{
700 unsigned i, last = first + cnt;
701 u64 sendctrl, sendorig;
702
703 ipath_cdbg(PKT, "disarm %u PIObufs first=%u\n", cnt, first);
704 sendorig = dd->ipath_sendctrl | INFINIPATH_S_DISARM;
705 for (i = first; i < last; i++) {
706 sendctrl = sendorig |
707 (i << INFINIPATH_S_DISARMPIOBUF_SHIFT);
708 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
709 sendctrl);
710 }
711
712 /*
713 * Write it again with current value, in case ipath_sendctrl changed
714 * while we were looping; no critical bits that would require
715 * locking.
716 *
717 * Write a 0, and then the original value, reading scratch in
718 * between. This seems to avoid a chip timing race that causes
719 * pioavail updates to memory to stop.
720 */
721 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
722 0);
723 sendorig = ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
724 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
725 dd->ipath_sendctrl);
726}
727
728/**
729 * ipath_wait_linkstate - wait for an IB link state change to occur
730 * @dd: the infinipath device
731 * @state: the state to wait for
732 * @msecs: the number of milliseconds to wait
733 *
734 * wait up to msecs milliseconds for IB link state change to occur for
735 * now, take the easy polling route. Currently used only by
34b2aafe 736 * ipath_set_linkstate. Returns 0 if state reached, otherwise
7bb206e3
BS
737 * -ETIMEDOUT state can have multiple states set, for any of several
738 * transitions.
739 */
34b2aafe
BS
740static int ipath_wait_linkstate(struct ipath_devdata *dd, u32 state,
741 int msecs)
7bb206e3 742{
0fd41363
BS
743 dd->ipath_state_wanted = state;
744 wait_event_interruptible_timeout(ipath_state_wait,
7bb206e3
BS
745 (dd->ipath_flags & state),
746 msecs_to_jiffies(msecs));
0fd41363 747 dd->ipath_state_wanted = 0;
7bb206e3
BS
748
749 if (!(dd->ipath_flags & state)) {
750 u64 val;
0fd41363
BS
751 ipath_cdbg(VERBOSE, "Didn't reach linkstate %s within %u"
752 " ms\n",
7bb206e3
BS
753 /* test INIT ahead of DOWN, both can be set */
754 (state & IPATH_LINKINIT) ? "INIT" :
755 ((state & IPATH_LINKDOWN) ? "DOWN" :
756 ((state & IPATH_LINKARMED) ? "ARM" : "ACTIVE")),
757 msecs);
758 val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_ibcstatus);
759 ipath_cdbg(VERBOSE, "ibcc=%llx ibcstatus=%llx (%s)\n",
760 (unsigned long long) ipath_read_kreg64(
761 dd, dd->ipath_kregs->kr_ibcctrl),
762 (unsigned long long) val,
763 ipath_ibcstatus_str[val & 0xf]);
764 }
765 return (dd->ipath_flags & state) ? 0 : -ETIMEDOUT;
766}
767
8ec1077b
BS
768/*
769 * Decode the error status into strings, deciding whether to always
770 * print * it or not depending on "normal packet errors" vs everything
771 * else. Return 1 if "real" errors, otherwise 0 if only packet
772 * errors, so caller can decide what to print with the string.
773 */
774int ipath_decode_err(char *buf, size_t blen, ipath_err_t err)
7bb206e3 775{
8ec1077b 776 int iserr = 1;
7bb206e3 777 *buf = '\0';
8ec1077b
BS
778 if (err & INFINIPATH_E_PKTERRS) {
779 if (!(err & ~INFINIPATH_E_PKTERRS))
780 iserr = 0; // if only packet errors.
781 if (ipath_debug & __IPATH_ERRPKTDBG) {
782 if (err & INFINIPATH_E_REBP)
783 strlcat(buf, "EBP ", blen);
784 if (err & INFINIPATH_E_RVCRC)
785 strlcat(buf, "VCRC ", blen);
786 if (err & INFINIPATH_E_RICRC) {
787 strlcat(buf, "CRC ", blen);
788 // clear for check below, so only once
789 err &= INFINIPATH_E_RICRC;
790 }
791 if (err & INFINIPATH_E_RSHORTPKTLEN)
792 strlcat(buf, "rshortpktlen ", blen);
793 if (err & INFINIPATH_E_SDROPPEDDATAPKT)
794 strlcat(buf, "sdroppeddatapkt ", blen);
795 if (err & INFINIPATH_E_SPKTLEN)
796 strlcat(buf, "spktlen ", blen);
797 }
798 if ((err & INFINIPATH_E_RICRC) &&
799 !(err&(INFINIPATH_E_RVCRC|INFINIPATH_E_REBP)))
800 strlcat(buf, "CRC ", blen);
801 if (!iserr)
802 goto done;
803 }
7bb206e3
BS
804 if (err & INFINIPATH_E_RHDRLEN)
805 strlcat(buf, "rhdrlen ", blen);
806 if (err & INFINIPATH_E_RBADTID)
807 strlcat(buf, "rbadtid ", blen);
808 if (err & INFINIPATH_E_RBADVERSION)
809 strlcat(buf, "rbadversion ", blen);
810 if (err & INFINIPATH_E_RHDR)
811 strlcat(buf, "rhdr ", blen);
812 if (err & INFINIPATH_E_RLONGPKTLEN)
813 strlcat(buf, "rlongpktlen ", blen);
7bb206e3
BS
814 if (err & INFINIPATH_E_RMAXPKTLEN)
815 strlcat(buf, "rmaxpktlen ", blen);
816 if (err & INFINIPATH_E_RMINPKTLEN)
817 strlcat(buf, "rminpktlen ", blen);
8ec1077b
BS
818 if (err & INFINIPATH_E_SMINPKTLEN)
819 strlcat(buf, "sminpktlen ", blen);
7bb206e3
BS
820 if (err & INFINIPATH_E_RFORMATERR)
821 strlcat(buf, "rformaterr ", blen);
822 if (err & INFINIPATH_E_RUNSUPVL)
823 strlcat(buf, "runsupvl ", blen);
824 if (err & INFINIPATH_E_RUNEXPCHAR)
825 strlcat(buf, "runexpchar ", blen);
826 if (err & INFINIPATH_E_RIBFLOW)
827 strlcat(buf, "ribflow ", blen);
7bb206e3
BS
828 if (err & INFINIPATH_E_SUNDERRUN)
829 strlcat(buf, "sunderrun ", blen);
830 if (err & INFINIPATH_E_SPIOARMLAUNCH)
831 strlcat(buf, "spioarmlaunch ", blen);
832 if (err & INFINIPATH_E_SUNEXPERRPKTNUM)
833 strlcat(buf, "sunexperrpktnum ", blen);
7bb206e3
BS
834 if (err & INFINIPATH_E_SDROPPEDSMPPKT)
835 strlcat(buf, "sdroppedsmppkt ", blen);
836 if (err & INFINIPATH_E_SMAXPKTLEN)
837 strlcat(buf, "smaxpktlen ", blen);
7bb206e3
BS
838 if (err & INFINIPATH_E_SUNSUPVL)
839 strlcat(buf, "sunsupVL ", blen);
7bb206e3
BS
840 if (err & INFINIPATH_E_INVALIDADDR)
841 strlcat(buf, "invalidaddr ", blen);
7bb206e3
BS
842 if (err & INFINIPATH_E_RRCVEGRFULL)
843 strlcat(buf, "rcvegrfull ", blen);
844 if (err & INFINIPATH_E_RRCVHDRFULL)
845 strlcat(buf, "rcvhdrfull ", blen);
846 if (err & INFINIPATH_E_IBSTATUSCHANGED)
847 strlcat(buf, "ibcstatuschg ", blen);
848 if (err & INFINIPATH_E_RIBLOSTLINK)
849 strlcat(buf, "riblostlink ", blen);
850 if (err & INFINIPATH_E_HARDWARE)
851 strlcat(buf, "hardware ", blen);
852 if (err & INFINIPATH_E_RESET)
853 strlcat(buf, "reset ", blen);
8ec1077b
BS
854done:
855 return iserr;
7bb206e3
BS
856}
857
858/**
859 * get_rhf_errstring - decode RHF errors
860 * @err: the err number
861 * @msg: the output buffer
862 * @len: the length of the output buffer
863 *
864 * only used one place now, may want more later
865 */
866static void get_rhf_errstring(u32 err, char *msg, size_t len)
867{
868 /* if no errors, and so don't need to check what's first */
869 *msg = '\0';
870
871 if (err & INFINIPATH_RHF_H_ICRCERR)
872 strlcat(msg, "icrcerr ", len);
873 if (err & INFINIPATH_RHF_H_VCRCERR)
874 strlcat(msg, "vcrcerr ", len);
875 if (err & INFINIPATH_RHF_H_PARITYERR)
876 strlcat(msg, "parityerr ", len);
877 if (err & INFINIPATH_RHF_H_LENERR)
878 strlcat(msg, "lenerr ", len);
879 if (err & INFINIPATH_RHF_H_MTUERR)
880 strlcat(msg, "mtuerr ", len);
881 if (err & INFINIPATH_RHF_H_IHDRERR)
882 /* infinipath hdr checksum error */
883 strlcat(msg, "ipathhdrerr ", len);
884 if (err & INFINIPATH_RHF_H_TIDERR)
885 strlcat(msg, "tiderr ", len);
886 if (err & INFINIPATH_RHF_H_MKERR)
887 /* bad port, offset, etc. */
888 strlcat(msg, "invalid ipathhdr ", len);
889 if (err & INFINIPATH_RHF_H_IBERR)
890 strlcat(msg, "iberr ", len);
891 if (err & INFINIPATH_RHF_L_SWA)
892 strlcat(msg, "swA ", len);
893 if (err & INFINIPATH_RHF_L_SWB)
894 strlcat(msg, "swB ", len);
895}
896
897/**
898 * ipath_get_egrbuf - get an eager buffer
899 * @dd: the infinipath device
900 * @bufnum: the eager buffer to get
901 * @err: unused
902 *
903 * must only be called if ipath_pd[port] is known to be allocated
904 */
905static inline void *ipath_get_egrbuf(struct ipath_devdata *dd, u32 bufnum,
906 int err)
907{
1fd3b40f
BS
908 return dd->ipath_port0_skbinfo ?
909 (void *) dd->ipath_port0_skbinfo[bufnum].skb->data : NULL;
7bb206e3
BS
910}
911
912/**
913 * ipath_alloc_skb - allocate an skb and buffer with possible constraints
914 * @dd: the infinipath device
915 * @gfp_mask: the sk_buff SFP mask
916 */
917struct sk_buff *ipath_alloc_skb(struct ipath_devdata *dd,
918 gfp_t gfp_mask)
919{
920 struct sk_buff *skb;
921 u32 len;
922
923 /*
924 * Only fully supported way to handle this is to allocate lots
925 * extra, align as needed, and then do skb_reserve(). That wastes
926 * a lot of memory... I'll have to hack this into infinipath_copy
927 * also.
928 */
929
930 /*
1fd3b40f
BS
931 * We need 2 extra bytes for ipath_ether data sent in the
932 * key header. In order to keep everything dword aligned,
933 * we'll reserve 4 bytes.
7bb206e3 934 */
1fd3b40f
BS
935 len = dd->ipath_ibmaxlen + 4;
936
7bb206e3 937 if (dd->ipath_flags & IPATH_4BYTE_TID) {
1fd3b40f 938 /* We need a 2KB multiple alignment, and there is no way
7bb206e3
BS
939 * to do it except to allocate extra and then skb_reserve
940 * enough to bring it up to the right alignment.
941 */
1fd3b40f 942 len += 2047;
7bb206e3 943 }
1fd3b40f 944
7bb206e3
BS
945 skb = __dev_alloc_skb(len, gfp_mask);
946 if (!skb) {
947 ipath_dev_err(dd, "Failed to allocate skbuff, length %u\n",
948 len);
949 goto bail;
950 }
1fd3b40f
BS
951
952 skb_reserve(skb, 4);
953
7bb206e3 954 if (dd->ipath_flags & IPATH_4BYTE_TID) {
1fd3b40f 955 u32 una = (unsigned long)skb->data & 2047;
7bb206e3 956 if (una)
1fd3b40f
BS
957 skb_reserve(skb, 2048 - una);
958 }
7bb206e3
BS
959
960bail:
961 return skb;
962}
963
3d37b9e2
RC
964static void ipath_rcv_hdrerr(struct ipath_devdata *dd,
965 u32 eflags,
966 u32 l,
967 u32 etail,
968 u64 *rc)
969{
970 char emsg[128];
971 struct ipath_message_header *hdr;
972
973 get_rhf_errstring(eflags, emsg, sizeof emsg);
974 hdr = (struct ipath_message_header *)&rc[1];
975 ipath_cdbg(PKT, "RHFerrs %x hdrqtail=%x typ=%u "
976 "tlen=%x opcode=%x egridx=%x: %s\n",
977 eflags, l,
978 ipath_hdrget_rcv_type((__le32 *) rc),
979 ipath_hdrget_length_in_bytes((__le32 *) rc),
980 be32_to_cpu(hdr->bth[0]) >> 24,
981 etail, emsg);
982
983 /* Count local link integrity errors. */
984 if (eflags & (INFINIPATH_RHF_H_ICRCERR | INFINIPATH_RHF_H_VCRCERR)) {
985 u8 n = (dd->ipath_ibcctrl >>
986 INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT) &
987 INFINIPATH_IBCC_PHYERRTHRESHOLD_MASK;
988
989 if (++dd->ipath_lli_counter > n) {
990 dd->ipath_lli_counter = 0;
991 dd->ipath_lli_errors++;
992 }
993 }
994}
995
7bb206e3
BS
996/*
997 * ipath_kreceive - receive a packet
998 * @dd: the infinipath device
999 *
1000 * called from interrupt handler for errors or receive interrupt
1001 */
1002void ipath_kreceive(struct ipath_devdata *dd)
1003{
1004 u64 *rc;
1005 void *ebuf;
1006 const u32 rsize = dd->ipath_rcvhdrentsize; /* words */
1007 const u32 maxcnt = dd->ipath_rcvhdrcnt * rsize; /* words */
1008 u32 etail = -1, l, hdrqtail;
27b678dd 1009 struct ipath_message_header *hdr;
57abad25 1010 u32 eflags, i, etype, tlen, pkttot = 0, updegr=0, reloop=0;
7bb206e3 1011 static u64 totcalls; /* stats, may eventually remove */
7bb206e3
BS
1012
1013 if (!dd->ipath_hdrqtailptr) {
1014 ipath_dev_err(dd,
1015 "hdrqtailptr not set, can't do receives\n");
1016 goto bail;
1017 }
1018
1019 /* There is already a thread processing this queue. */
1020 if (test_and_set_bit(0, &dd->ipath_rcv_pending))
1021 goto bail;
1022
f5f99929 1023 l = dd->ipath_port0head;
57abad25
BS
1024 hdrqtail = (u32) le64_to_cpu(*dd->ipath_hdrqtailptr);
1025 if (l == hdrqtail)
7bb206e3
BS
1026 goto done;
1027
57abad25 1028reloop:
f5f99929 1029 for (i = 0; l != hdrqtail; i++) {
7bb206e3
BS
1030 u32 qp;
1031 u8 *bthbytes;
1032
1033 rc = (u64 *) (dd->ipath_pd[0]->port_rcvhdrq + (l << 2));
27b678dd 1034 hdr = (struct ipath_message_header *)&rc[1];
7bb206e3
BS
1035 /*
1036 * could make a network order version of IPATH_KD_QP, and
1037 * do the obvious shift before masking to speed this up.
1038 */
1039 qp = ntohl(hdr->bth[1]) & 0xffffff;
1040 bthbytes = (u8 *) hdr->bth;
1041
27b678dd
BS
1042 eflags = ipath_hdrget_err_flags((__le32 *) rc);
1043 etype = ipath_hdrget_rcv_type((__le32 *) rc);
7bb206e3 1044 /* total length */
27b678dd 1045 tlen = ipath_hdrget_length_in_bytes((__le32 *) rc);
7bb206e3
BS
1046 ebuf = NULL;
1047 if (etype != RCVHQ_RCV_TYPE_EXPECTED) {
1048 /*
1049 * it turns out that the chips uses an eager buffer
1050 * for all non-expected packets, whether it "needs"
1051 * one or not. So always get the index, but don't
1052 * set ebuf (so we try to copy data) unless the
1053 * length requires it.
1054 */
27b678dd 1055 etail = ipath_hdrget_index((__le32 *) rc);
7bb206e3
BS
1056 if (tlen > sizeof(*hdr) ||
1057 etype == RCVHQ_RCV_TYPE_NON_KD)
1058 ebuf = ipath_get_egrbuf(dd, etail, 0);
1059 }
1060
1061 /*
1062 * both tiderr and ipathhdrerr are set for all plain IB
1063 * packets; only ipathhdrerr should be set.
1064 */
1065
1066 if (etype != RCVHQ_RCV_TYPE_NON_KD && etype !=
27b678dd 1067 RCVHQ_RCV_TYPE_ERROR && ipath_hdrget_ipath_ver(
7bb206e3
BS
1068 hdr->iph.ver_port_tid_offset) !=
1069 IPS_PROTO_VERSION) {
1070 ipath_cdbg(PKT, "Bad InfiniPath protocol version "
1071 "%x\n", etype);
1072 }
1073
3d37b9e2
RC
1074 if (unlikely(eflags))
1075 ipath_rcv_hdrerr(dd, eflags, l, etail, rc);
1076 else if (etype == RCVHQ_RCV_TYPE_NON_KD) {
34b2aafe
BS
1077 ipath_ib_rcv(dd->verbs_dev, rc + 1, ebuf, tlen);
1078 if (dd->ipath_lli_counter)
1079 dd->ipath_lli_counter--;
1080 ipath_cdbg(PKT, "typ %x, opcode %x (eager, "
1081 "qp=%x), len %x; ignored\n",
1082 etype, bthbytes[0], qp, tlen);
7bb206e3 1083 }
34b2aafe
BS
1084 else if (etype == RCVHQ_RCV_TYPE_EAGER)
1085 ipath_cdbg(PKT, "typ %x, opcode %x (eager, "
1086 "qp=%x), len %x; ignored\n",
1087 etype, bthbytes[0], qp, tlen);
7bb206e3
BS
1088 else if (etype == RCVHQ_RCV_TYPE_EXPECTED)
1089 ipath_dbg("Bug: Expected TID, opcode %x; ignored\n",
1090 be32_to_cpu(hdr->bth[0]) & 0xff);
3d37b9e2 1091 else {
7bb206e3
BS
1092 /*
1093 * error packet, type of error unknown.
1094 * Probably type 3, but we don't know, so don't
1095 * even try to print the opcode, etc.
1096 */
1097 ipath_dbg("Error Pkt, but no eflags! egrbuf %x, "
1098 "len %x\nhdrq@%lx;hdrq+%x rhf: %llx; "
1099 "hdr %llx %llx %llx %llx %llx\n",
1100 etail, tlen, (unsigned long) rc, l,
1101 (unsigned long long) rc[0],
1102 (unsigned long long) rc[1],
1103 (unsigned long long) rc[2],
1104 (unsigned long long) rc[3],
1105 (unsigned long long) rc[4],
1106 (unsigned long long) rc[5]);
1107 }
1108 l += rsize;
1109 if (l >= maxcnt)
1110 l = 0;
f5f99929
BS
1111 if (etype != RCVHQ_RCV_TYPE_EXPECTED)
1112 updegr = 1;
7bb206e3 1113 /*
f5f99929
BS
1114 * update head regs on last packet, and every 16 packets.
1115 * Reduce bus traffic, while still trying to prevent
1116 * rcvhdrq overflows, for when the queue is nearly full
7bb206e3 1117 */
f5f99929
BS
1118 if (l == hdrqtail || (i && !(i&0xf))) {
1119 u64 lval;
525d0ca1
BS
1120 if (l == hdrqtail)
1121 /* request IBA6120 interrupt only on last */
f5f99929
BS
1122 lval = dd->ipath_rhdrhead_intr_off | l;
1123 else
1124 lval = l;
1125 (void)ipath_write_ureg(dd, ur_rcvhdrhead, lval, 0);
1126 if (updegr) {
1127 (void)ipath_write_ureg(dd, ur_rcvegrindexhead,
1128 etail, 0);
1129 updegr = 0;
1130 }
1131 }
7bb206e3
BS
1132 }
1133
57abad25 1134 if (!dd->ipath_rhdrhead_intr_off && !reloop) {
525d0ca1 1135 /* IBA6110 workaround; we can have a race clearing chip
57abad25
BS
1136 * interrupt with another interrupt about to be delivered,
1137 * and can clear it before it is delivered on the GPIO
1138 * workaround. By doing the extra check here for the
1139 * in-memory tail register updating while we were doing
1140 * earlier packets, we "almost" guarantee we have covered
1141 * that case.
1142 */
1143 u32 hqtail = (u32)le64_to_cpu(*dd->ipath_hdrqtailptr);
1144 if (hqtail != hdrqtail) {
1145 hdrqtail = hqtail;
1146 reloop = 1; /* loop 1 extra time at most */
1147 goto reloop;
1148 }
1149 }
1150
7bb206e3
BS
1151 pkttot += i;
1152
1153 dd->ipath_port0head = l;
1154
7bb206e3
BS
1155 if (pkttot > ipath_stats.sps_maxpkts_call)
1156 ipath_stats.sps_maxpkts_call = pkttot;
1157 ipath_stats.sps_port0pkts += pkttot;
1158 ipath_stats.sps_avgpkts_call =
1159 ipath_stats.sps_port0pkts / ++totcalls;
1160
1161done:
1162 clear_bit(0, &dd->ipath_rcv_pending);
1163 smp_mb__after_clear_bit();
1164
1165bail:;
1166}
1167
1168/**
1169 * ipath_update_pio_bufs - update shadow copy of the PIO availability map
1170 * @dd: the infinipath device
1171 *
1172 * called whenever our local copy indicates we have run out of send buffers
1173 * NOTE: This can be called from interrupt context by some code
1174 * and from non-interrupt context by ipath_getpiobuf().
1175 */
1176
1177static void ipath_update_pio_bufs(struct ipath_devdata *dd)
1178{
1179 unsigned long flags;
1180 int i;
1181 const unsigned piobregs = (unsigned)dd->ipath_pioavregs;
1182
1183 /* If the generation (check) bits have changed, then we update the
1184 * busy bit for the corresponding PIO buffer. This algorithm will
1185 * modify positions to the value they already have in some cases
1186 * (i.e., no change), but it's faster than changing only the bits
1187 * that have changed.
1188 *
1189 * We would like to do this atomicly, to avoid spinlocks in the
1190 * critical send path, but that's not really possible, given the
1191 * type of changes, and that this routine could be called on
1192 * multiple cpu's simultaneously, so we lock in this routine only,
1193 * to avoid conflicting updates; all we change is the shadow, and
1194 * it's a single 64 bit memory location, so by definition the update
1195 * is atomic in terms of what other cpu's can see in testing the
1196 * bits. The spin_lock overhead isn't too bad, since it only
1197 * happens when all buffers are in use, so only cpu overhead, not
1198 * latency or bandwidth is affected.
1199 */
1200#define _IPATH_ALL_CHECKBITS 0x5555555555555555ULL
1201 if (!dd->ipath_pioavailregs_dma) {
1202 ipath_dbg("Update shadow pioavail, but regs_dma NULL!\n");
1203 return;
1204 }
1205 if (ipath_debug & __IPATH_VERBDBG) {
1206 /* only if packet debug and verbose */
1207 volatile __le64 *dma = dd->ipath_pioavailregs_dma;
1208 unsigned long *shadow = dd->ipath_pioavailshadow;
1209
1210 ipath_cdbg(PKT, "Refill avail, dma0=%llx shad0=%lx, "
1211 "d1=%llx s1=%lx, d2=%llx s2=%lx, d3=%llx "
1212 "s3=%lx\n",
1213 (unsigned long long) le64_to_cpu(dma[0]),
1214 shadow[0],
1215 (unsigned long long) le64_to_cpu(dma[1]),
1216 shadow[1],
1217 (unsigned long long) le64_to_cpu(dma[2]),
1218 shadow[2],
1219 (unsigned long long) le64_to_cpu(dma[3]),
1220 shadow[3]);
1221 if (piobregs > 4)
1222 ipath_cdbg(
1223 PKT, "2nd group, dma4=%llx shad4=%lx, "
1224 "d5=%llx s5=%lx, d6=%llx s6=%lx, "
1225 "d7=%llx s7=%lx\n",
1226 (unsigned long long) le64_to_cpu(dma[4]),
1227 shadow[4],
1228 (unsigned long long) le64_to_cpu(dma[5]),
1229 shadow[5],
1230 (unsigned long long) le64_to_cpu(dma[6]),
1231 shadow[6],
1232 (unsigned long long) le64_to_cpu(dma[7]),
1233 shadow[7]);
1234 }
1235 spin_lock_irqsave(&ipath_pioavail_lock, flags);
1236 for (i = 0; i < piobregs; i++) {
1237 u64 pchbusy, pchg, piov, pnew;
1238 /*
1239 * Chip Errata: bug 6641; even and odd qwords>3 are swapped
1240 */
1241 if (i > 3) {
1242 if (i & 1)
1243 piov = le64_to_cpu(
1244 dd->ipath_pioavailregs_dma[i - 1]);
1245 else
1246 piov = le64_to_cpu(
1247 dd->ipath_pioavailregs_dma[i + 1]);
1248 } else
1249 piov = le64_to_cpu(dd->ipath_pioavailregs_dma[i]);
1250 pchg = _IPATH_ALL_CHECKBITS &
1251 ~(dd->ipath_pioavailshadow[i] ^ piov);
1252 pchbusy = pchg << INFINIPATH_SENDPIOAVAIL_BUSY_SHIFT;
1253 if (pchg && (pchbusy & dd->ipath_pioavailshadow[i])) {
1254 pnew = dd->ipath_pioavailshadow[i] & ~pchbusy;
1255 pnew |= piov & pchbusy;
1256 dd->ipath_pioavailshadow[i] = pnew;
1257 }
1258 }
1259 spin_unlock_irqrestore(&ipath_pioavail_lock, flags);
1260}
1261
1262/**
1263 * ipath_setrcvhdrsize - set the receive header size
1264 * @dd: the infinipath device
1265 * @rhdrsize: the receive header size
1266 *
1267 * called from user init code, and also layered driver init
1268 */
1269int ipath_setrcvhdrsize(struct ipath_devdata *dd, unsigned rhdrsize)
1270{
1271 int ret = 0;
1272
1273 if (dd->ipath_flags & IPATH_RCVHDRSZ_SET) {
1274 if (dd->ipath_rcvhdrsize != rhdrsize) {
1275 dev_info(&dd->pcidev->dev,
1276 "Error: can't set protocol header "
1277 "size %u, already %u\n",
1278 rhdrsize, dd->ipath_rcvhdrsize);
1279 ret = -EAGAIN;
1280 } else
1281 ipath_cdbg(VERBOSE, "Reuse same protocol header "
1282 "size %u\n", dd->ipath_rcvhdrsize);
1283 } else if (rhdrsize > (dd->ipath_rcvhdrentsize -
1284 (sizeof(u64) / sizeof(u32)))) {
1285 ipath_dbg("Error: can't set protocol header size %u "
1286 "(> max %u)\n", rhdrsize,
1287 dd->ipath_rcvhdrentsize -
1288 (u32) (sizeof(u64) / sizeof(u32)));
1289 ret = -EOVERFLOW;
1290 } else {
1291 dd->ipath_flags |= IPATH_RCVHDRSZ_SET;
1292 dd->ipath_rcvhdrsize = rhdrsize;
1293 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvhdrsize,
1294 dd->ipath_rcvhdrsize);
1295 ipath_cdbg(VERBOSE, "Set protocol header size to %u\n",
1296 dd->ipath_rcvhdrsize);
1297 }
1298 return ret;
1299}
1300
1301/**
1302 * ipath_getpiobuf - find an available pio buffer
1303 * @dd: the infinipath device
1304 * @pbufnum: the buffer number is placed here
1305 *
1306 * do appropriate marking as busy, etc.
1307 * returns buffer number if one found (>=0), negative number is error.
0fd41363 1308 * Used by ipath_layer_send
7bb206e3
BS
1309 */
1310u32 __iomem *ipath_getpiobuf(struct ipath_devdata *dd, u32 * pbufnum)
1311{
1312 int i, j, starti, updated = 0;
1313 unsigned piobcnt, iter;
1314 unsigned long flags;
1315 unsigned long *shadow = dd->ipath_pioavailshadow;
1316 u32 __iomem *buf;
1317
1318 piobcnt = (unsigned)(dd->ipath_piobcnt2k
1319 + dd->ipath_piobcnt4k);
1320 starti = dd->ipath_lastport_piobuf;
1321 iter = piobcnt - starti;
1322 if (dd->ipath_upd_pio_shadow) {
1323 /*
1324 * Minor optimization. If we had no buffers on last call,
1325 * start out by doing the update; continue and do scan even
1326 * if no buffers were updated, to be paranoid
1327 */
1328 ipath_update_pio_bufs(dd);
1329 /* we scanned here, don't do it at end of scan */
1330 updated = 1;
1331 i = starti;
1332 } else
1333 i = dd->ipath_lastpioindex;
1334
1335rescan:
1336 /*
1337 * while test_and_set_bit() is atomic, we do that and then the
1338 * change_bit(), and the pair is not. See if this is the cause
1339 * of the remaining armlaunch errors.
1340 */
1341 spin_lock_irqsave(&ipath_pioavail_lock, flags);
1342 for (j = 0; j < iter; j++, i++) {
1343 if (i >= piobcnt)
1344 i = starti;
1345 /*
1346 * To avoid bus lock overhead, we first find a candidate
1347 * buffer, then do the test and set, and continue if that
1348 * fails.
1349 */
1350 if (test_bit((2 * i) + 1, shadow) ||
1351 test_and_set_bit((2 * i) + 1, shadow))
1352 continue;
1353 /* flip generation bit */
1354 change_bit(2 * i, shadow);
1355 break;
1356 }
1357 spin_unlock_irqrestore(&ipath_pioavail_lock, flags);
1358
1359 if (j == iter) {
1360 volatile __le64 *dma = dd->ipath_pioavailregs_dma;
1361
1362 /*
1363 * first time through; shadow exhausted, but may be real
1364 * buffers available, so go see; if any updated, rescan
1365 * (once)
1366 */
1367 if (!updated) {
1368 ipath_update_pio_bufs(dd);
1369 updated = 1;
1370 i = starti;
1371 goto rescan;
1372 }
1373 dd->ipath_upd_pio_shadow = 1;
1374 /*
1375 * not atomic, but if we lose one once in a while, that's OK
1376 */
1377 ipath_stats.sps_nopiobufs++;
1378 if (!(++dd->ipath_consec_nopiobuf % 100000)) {
1379 ipath_dbg(
1380 "%u pio sends with no bufavail; dmacopy: "
1381 "%llx %llx %llx %llx; shadow: "
1382 "%lx %lx %lx %lx\n",
1383 dd->ipath_consec_nopiobuf,
1384 (unsigned long long) le64_to_cpu(dma[0]),
1385 (unsigned long long) le64_to_cpu(dma[1]),
1386 (unsigned long long) le64_to_cpu(dma[2]),
1387 (unsigned long long) le64_to_cpu(dma[3]),
1388 shadow[0], shadow[1], shadow[2],
1389 shadow[3]);
1390 /*
1391 * 4 buffers per byte, 4 registers above, cover rest
1392 * below
1393 */
1394 if ((dd->ipath_piobcnt2k + dd->ipath_piobcnt4k) >
1395 (sizeof(shadow[0]) * 4 * 4))
1396 ipath_dbg("2nd group: dmacopy: %llx %llx "
1397 "%llx %llx; shadow: %lx %lx "
1398 "%lx %lx\n",
1399 (unsigned long long)
1400 le64_to_cpu(dma[4]),
1401 (unsigned long long)
1402 le64_to_cpu(dma[5]),
1403 (unsigned long long)
1404 le64_to_cpu(dma[6]),
1405 (unsigned long long)
1406 le64_to_cpu(dma[7]),
1407 shadow[4], shadow[5],
1408 shadow[6], shadow[7]);
1409 }
1410 buf = NULL;
1411 goto bail;
1412 }
1413
7bb206e3
BS
1414 /*
1415 * set next starting place. Since it's just an optimization,
1416 * it doesn't matter who wins on this, so no locking
1417 */
1418 dd->ipath_lastpioindex = i + 1;
1419 if (dd->ipath_upd_pio_shadow)
1420 dd->ipath_upd_pio_shadow = 0;
1421 if (dd->ipath_consec_nopiobuf)
1422 dd->ipath_consec_nopiobuf = 0;
1423 if (i < dd->ipath_piobcnt2k)
1424 buf = (u32 __iomem *) (dd->ipath_pio2kbase +
1425 i * dd->ipath_palign);
1426 else
1427 buf = (u32 __iomem *)
1428 (dd->ipath_pio4kbase +
1429 (i - dd->ipath_piobcnt2k) * dd->ipath_4kalign);
1430 ipath_cdbg(VERBOSE, "Return piobuf%u %uk @ %p\n",
1431 i, (i < dd->ipath_piobcnt2k) ? 2 : 4, buf);
1432 if (pbufnum)
1433 *pbufnum = i;
1434
1435bail:
1436 return buf;
1437}
1438
1439/**
1440 * ipath_create_rcvhdrq - create a receive header queue
1441 * @dd: the infinipath device
1442 * @pd: the port data
1443 *
f37bda92
BS
1444 * this must be contiguous memory (from an i/o perspective), and must be
1445 * DMA'able (which means for some systems, it will go through an IOMMU,
1446 * or be forced into a low address range).
7bb206e3
BS
1447 */
1448int ipath_create_rcvhdrq(struct ipath_devdata *dd,
1449 struct ipath_portdata *pd)
1450{
f37bda92 1451 int ret = 0;
7bb206e3 1452
7bb206e3 1453 if (!pd->port_rcvhdrq) {
f37bda92 1454 dma_addr_t phys_hdrqtail;
7bb206e3 1455 gfp_t gfp_flags = GFP_USER | __GFP_COMP;
f37bda92
BS
1456 int amt = ALIGN(dd->ipath_rcvhdrcnt * dd->ipath_rcvhdrentsize *
1457 sizeof(u32), PAGE_SIZE);
7bb206e3
BS
1458
1459 pd->port_rcvhdrq = dma_alloc_coherent(
1460 &dd->pcidev->dev, amt, &pd->port_rcvhdrq_phys,
1461 gfp_flags);
1462
1463 if (!pd->port_rcvhdrq) {
1464 ipath_dev_err(dd, "attempt to allocate %d bytes "
1465 "for port %u rcvhdrq failed\n",
1466 amt, pd->port_port);
1467 ret = -ENOMEM;
1468 goto bail;
1469 }
f37bda92
BS
1470 pd->port_rcvhdrtail_kvaddr = dma_alloc_coherent(
1471 &dd->pcidev->dev, PAGE_SIZE, &phys_hdrqtail, GFP_KERNEL);
1472 if (!pd->port_rcvhdrtail_kvaddr) {
1473 ipath_dev_err(dd, "attempt to allocate 1 page "
1474 "for port %u rcvhdrqtailaddr failed\n",
1475 pd->port_port);
1476 ret = -ENOMEM;
221e3198
BS
1477 dma_free_coherent(&dd->pcidev->dev, amt,
1478 pd->port_rcvhdrq, pd->port_rcvhdrq_phys);
1479 pd->port_rcvhdrq = NULL;
f37bda92
BS
1480 goto bail;
1481 }
1482 pd->port_rcvhdrqtailaddr_phys = phys_hdrqtail;
7bb206e3
BS
1483
1484 pd->port_rcvhdrq_size = amt;
1485
1486 ipath_cdbg(VERBOSE, "%d pages at %p (phys %lx) size=%lu "
1487 "for port %u rcvhdr Q\n",
1488 amt >> PAGE_SHIFT, pd->port_rcvhdrq,
1489 (unsigned long) pd->port_rcvhdrq_phys,
1490 (unsigned long) pd->port_rcvhdrq_size,
1491 pd->port_port);
f37bda92
BS
1492
1493 ipath_cdbg(VERBOSE, "port %d hdrtailaddr, %llx physical\n",
1494 pd->port_port,
1495 (unsigned long long) phys_hdrqtail);
7bb206e3 1496 }
f37bda92
BS
1497 else
1498 ipath_cdbg(VERBOSE, "reuse port %d rcvhdrq @%p %llx phys; "
1499 "hdrtailaddr@%p %llx physical\n",
1500 pd->port_port, pd->port_rcvhdrq,
1fd3b40f
BS
1501 (unsigned long long) pd->port_rcvhdrq_phys,
1502 pd->port_rcvhdrtail_kvaddr, (unsigned long long)
1503 pd->port_rcvhdrqtailaddr_phys);
f37bda92
BS
1504
1505 /* clear for security and sanity on each use */
1506 memset(pd->port_rcvhdrq, 0, pd->port_rcvhdrq_size);
076fafcd 1507 memset(pd->port_rcvhdrtail_kvaddr, 0, PAGE_SIZE);
7bb206e3
BS
1508
1509 /*
1510 * tell chip each time we init it, even if we are re-using previous
f37bda92 1511 * memory (we zero the register at process close)
7bb206e3 1512 */
f37bda92
BS
1513 ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdrtailaddr,
1514 pd->port_port, pd->port_rcvhdrqtailaddr_phys);
7bb206e3
BS
1515 ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdraddr,
1516 pd->port_port, pd->port_rcvhdrq_phys);
1517
1518 ret = 0;
1519bail:
1520 return ret;
1521}
1522
1523int ipath_waitfor_complete(struct ipath_devdata *dd, ipath_kreg reg_id,
1524 u64 bits_to_wait_for, u64 * valp)
1525{
1526 unsigned long timeout;
1527 u64 lastval, val;
1528 int ret;
1529
1530 lastval = ipath_read_kreg64(dd, reg_id);
1531 /* wait a ridiculously long time */
1532 timeout = jiffies + msecs_to_jiffies(5);
1533 do {
1534 val = ipath_read_kreg64(dd, reg_id);
1535 /* set so they have something, even on failures. */
1536 *valp = val;
1537 if ((val & bits_to_wait_for) == bits_to_wait_for) {
1538 ret = 0;
1539 break;
1540 }
1541 if (val != lastval)
1542 ipath_cdbg(VERBOSE, "Changed from %llx to %llx, "
1543 "waiting for %llx bits\n",
1544 (unsigned long long) lastval,
1545 (unsigned long long) val,
1546 (unsigned long long) bits_to_wait_for);
1547 cond_resched();
1548 if (time_after(jiffies, timeout)) {
1549 ipath_dbg("Didn't get bits %llx in register 0x%x, "
1550 "got %llx\n",
1551 (unsigned long long) bits_to_wait_for,
1552 reg_id, (unsigned long long) *valp);
1553 ret = -ENODEV;
1554 break;
1555 }
1556 } while (1);
1557
1558 return ret;
1559}
1560
1561/**
1562 * ipath_waitfor_mdio_cmdready - wait for last command to complete
1563 * @dd: the infinipath device
1564 *
1565 * Like ipath_waitfor_complete(), but we wait for the CMDVALID bit to go
1566 * away indicating the last command has completed. It doesn't return data
1567 */
1568int ipath_waitfor_mdio_cmdready(struct ipath_devdata *dd)
1569{
1570 unsigned long timeout;
1571 u64 val;
1572 int ret;
1573
1574 /* wait a ridiculously long time */
1575 timeout = jiffies + msecs_to_jiffies(5);
1576 do {
1577 val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_mdio);
1578 if (!(val & IPATH_MDIO_CMDVALID)) {
1579 ret = 0;
1580 break;
1581 }
1582 cond_resched();
1583 if (time_after(jiffies, timeout)) {
1584 ipath_dbg("CMDVALID stuck in mdio reg? (%llx)\n",
1585 (unsigned long long) val);
1586 ret = -ENODEV;
1587 break;
1588 }
1589 } while (1);
1590
1591 return ret;
1592}
1593
34b2aafe 1594static void ipath_set_ib_lstate(struct ipath_devdata *dd, int which)
7bb206e3
BS
1595{
1596 static const char *what[4] = {
1597 [0] = "DOWN",
1598 [INFINIPATH_IBCC_LINKCMD_INIT] = "INIT",
1599 [INFINIPATH_IBCC_LINKCMD_ARMED] = "ARMED",
1600 [INFINIPATH_IBCC_LINKCMD_ACTIVE] = "ACTIVE"
1601 };
f37bda92
BS
1602 int linkcmd = (which >> INFINIPATH_IBCC_LINKCMD_SHIFT) &
1603 INFINIPATH_IBCC_LINKCMD_MASK;
1604
0fd41363 1605 ipath_cdbg(VERBOSE, "Trying to move unit %u to %s, current ltstate "
7bb206e3 1606 "is %s\n", dd->ipath_unit,
f37bda92 1607 what[linkcmd],
7bb206e3
BS
1608 ipath_ibcstatus_str[
1609 (ipath_read_kreg64
1610 (dd, dd->ipath_kregs->kr_ibcstatus) >>
1611 INFINIPATH_IBCS_LINKTRAININGSTATE_SHIFT) &
1612 INFINIPATH_IBCS_LINKTRAININGSTATE_MASK]);
f37bda92 1613 /* flush all queued sends when going to DOWN or INIT, to be sure that
0fd41363 1614 * they don't block MAD packets */
f37bda92
BS
1615 if (!linkcmd || linkcmd == INFINIPATH_IBCC_LINKCMD_INIT) {
1616 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
1617 INFINIPATH_S_ABORT);
1618 ipath_disarm_piobufs(dd, dd->ipath_lastport_piobuf,
1619 (unsigned)(dd->ipath_piobcnt2k +
1620 dd->ipath_piobcnt4k) -
1621 dd->ipath_lastport_piobuf);
1622 }
7bb206e3
BS
1623
1624 ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
1625 dd->ipath_ibcctrl | which);
1626}
1627
34b2aafe
BS
1628int ipath_set_linkstate(struct ipath_devdata *dd, u8 newstate)
1629{
1630 u32 lstate;
1631 int ret;
1632
1633 switch (newstate) {
1634 case IPATH_IB_LINKDOWN:
1635 ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKINITCMD_POLL <<
1636 INFINIPATH_IBCC_LINKINITCMD_SHIFT);
1637 /* don't wait */
1638 ret = 0;
1639 goto bail;
1640
1641 case IPATH_IB_LINKDOWN_SLEEP:
1642 ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKINITCMD_SLEEP <<
1643 INFINIPATH_IBCC_LINKINITCMD_SHIFT);
1644 /* don't wait */
1645 ret = 0;
1646 goto bail;
1647
1648 case IPATH_IB_LINKDOWN_DISABLE:
1649 ipath_set_ib_lstate(dd,
1650 INFINIPATH_IBCC_LINKINITCMD_DISABLE <<
1651 INFINIPATH_IBCC_LINKINITCMD_SHIFT);
1652 /* don't wait */
1653 ret = 0;
1654 goto bail;
1655
1656 case IPATH_IB_LINKINIT:
1657 if (dd->ipath_flags & IPATH_LINKINIT) {
1658 ret = 0;
1659 goto bail;
1660 }
1661 ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_INIT <<
1662 INFINIPATH_IBCC_LINKCMD_SHIFT);
1663 lstate = IPATH_LINKINIT;
1664 break;
1665
1666 case IPATH_IB_LINKARM:
1667 if (dd->ipath_flags & IPATH_LINKARMED) {
1668 ret = 0;
1669 goto bail;
1670 }
1671 if (!(dd->ipath_flags &
1672 (IPATH_LINKINIT | IPATH_LINKACTIVE))) {
1673 ret = -EINVAL;
1674 goto bail;
1675 }
1676 ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_ARMED <<
1677 INFINIPATH_IBCC_LINKCMD_SHIFT);
1678 /*
1679 * Since the port can transition to ACTIVE by receiving
1680 * a non VL 15 packet, wait for either state.
1681 */
1682 lstate = IPATH_LINKARMED | IPATH_LINKACTIVE;
1683 break;
1684
1685 case IPATH_IB_LINKACTIVE:
1686 if (dd->ipath_flags & IPATH_LINKACTIVE) {
1687 ret = 0;
1688 goto bail;
1689 }
1690 if (!(dd->ipath_flags & IPATH_LINKARMED)) {
1691 ret = -EINVAL;
1692 goto bail;
1693 }
1694 ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKCMD_ACTIVE <<
1695 INFINIPATH_IBCC_LINKCMD_SHIFT);
1696 lstate = IPATH_LINKACTIVE;
1697 break;
1698
946db67f
BS
1699 case IPATH_IB_LINK_LOOPBACK:
1700 dev_info(&dd->pcidev->dev, "Enabling IB local loopback\n");
1701 dd->ipath_ibcctrl |= INFINIPATH_IBCC_LOOPBACK;
1702 ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
1703 dd->ipath_ibcctrl);
1704 ret = 0;
1705 goto bail; // no state change to wait for
1706
1707 case IPATH_IB_LINK_EXTERNAL:
1708 dev_info(&dd->pcidev->dev, "Disabling IB local loopback (normal)\n");
1709 dd->ipath_ibcctrl &= ~INFINIPATH_IBCC_LOOPBACK;
1710 ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
1711 dd->ipath_ibcctrl);
1712 ret = 0;
1713 goto bail; // no state change to wait for
1714
34b2aafe
BS
1715 default:
1716 ipath_dbg("Invalid linkstate 0x%x requested\n", newstate);
1717 ret = -EINVAL;
1718 goto bail;
1719 }
1720 ret = ipath_wait_linkstate(dd, lstate, 2000);
1721
1722bail:
1723 return ret;
1724}
1725
1726/**
1727 * ipath_set_mtu - set the MTU
1728 * @dd: the infinipath device
1729 * @arg: the new MTU
1730 *
1731 * we can handle "any" incoming size, the issue here is whether we
1732 * need to restrict our outgoing size. For now, we don't do any
1733 * sanity checking on this, and we don't deal with what happens to
1734 * programs that are already running when the size changes.
1735 * NOTE: changing the MTU will usually cause the IBC to go back to
1736 * link initialize (IPATH_IBSTATE_INIT) state...
1737 */
1738int ipath_set_mtu(struct ipath_devdata *dd, u16 arg)
1739{
1740 u32 piosize;
1741 int changed = 0;
1742 int ret;
1743
1744 /*
1745 * mtu is IB data payload max. It's the largest power of 2 less
1746 * than piosize (or even larger, since it only really controls the
1747 * largest we can receive; we can send the max of the mtu and
1748 * piosize). We check that it's one of the valid IB sizes.
1749 */
1750 if (arg != 256 && arg != 512 && arg != 1024 && arg != 2048 &&
1751 arg != 4096) {
1752 ipath_dbg("Trying to set invalid mtu %u, failing\n", arg);
1753 ret = -EINVAL;
1754 goto bail;
1755 }
1756 if (dd->ipath_ibmtu == arg) {
1757 ret = 0; /* same as current */
1758 goto bail;
1759 }
1760
1761 piosize = dd->ipath_ibmaxlen;
1762 dd->ipath_ibmtu = arg;
1763
1764 if (arg >= (piosize - IPATH_PIO_MAXIBHDR)) {
1765 /* Only if it's not the initial value (or reset to it) */
1766 if (piosize != dd->ipath_init_ibmaxlen) {
1767 dd->ipath_ibmaxlen = piosize;
1768 changed = 1;
1769 }
1770 } else if ((arg + IPATH_PIO_MAXIBHDR) != dd->ipath_ibmaxlen) {
1771 piosize = arg + IPATH_PIO_MAXIBHDR;
1772 ipath_cdbg(VERBOSE, "ibmaxlen was 0x%x, setting to 0x%x "
1773 "(mtu 0x%x)\n", dd->ipath_ibmaxlen, piosize,
1774 arg);
1775 dd->ipath_ibmaxlen = piosize;
1776 changed = 1;
1777 }
1778
1779 if (changed) {
1780 /*
1781 * set the IBC maxpktlength to the size of our pio
1782 * buffers in words
1783 */
1784 u64 ibc = dd->ipath_ibcctrl;
1785 ibc &= ~(INFINIPATH_IBCC_MAXPKTLEN_MASK <<
1786 INFINIPATH_IBCC_MAXPKTLEN_SHIFT);
1787
1788 piosize = piosize - 2 * sizeof(u32); /* ignore pbc */
1789 dd->ipath_ibmaxlen = piosize;
1790 piosize /= sizeof(u32); /* in words */
1791 /*
1792 * for ICRC, which we only send in diag test pkt mode, and
1793 * we don't need to worry about that for mtu
1794 */
1795 piosize += 1;
1796
1797 ibc |= piosize << INFINIPATH_IBCC_MAXPKTLEN_SHIFT;
1798 dd->ipath_ibcctrl = ibc;
1799 ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
1800 dd->ipath_ibcctrl);
1801 dd->ipath_f_tidtemplate(dd);
1802 }
1803
1804 ret = 0;
1805
1806bail:
1807 return ret;
1808}
1809
1810int ipath_set_lid(struct ipath_devdata *dd, u32 arg, u8 lmc)
1811{
1812 dd->ipath_lid = arg;
1813 dd->ipath_lmc = lmc;
1814
1815 return 0;
1816}
1817
7bb206e3
BS
1818
1819/**
1820 * ipath_write_kreg_port - write a device's per-port 64-bit kernel register
1821 * @dd: the infinipath device
1822 * @regno: the register number to write
1823 * @port: the port containing the register
1824 * @value: the value to write
1825 *
1826 * Registers that vary with the chip implementation constants (port)
1827 * use this routine.
1828 */
1829void ipath_write_kreg_port(const struct ipath_devdata *dd, ipath_kreg regno,
1830 unsigned port, u64 value)
1831{
1832 u16 where;
1833
1834 if (port < dd->ipath_portcnt &&
1835 (regno == dd->ipath_kregs->kr_rcvhdraddr ||
1836 regno == dd->ipath_kregs->kr_rcvhdrtailaddr))
1837 where = regno + port;
1838 else
1839 where = -1;
1840
1841 ipath_write_kreg(dd, where, value);
1842}
1843
1844/**
1845 * ipath_shutdown_device - shut down a device
1846 * @dd: the infinipath device
1847 *
1848 * This is called to make the device quiet when we are about to
1849 * unload the driver, and also when the device is administratively
1850 * disabled. It does not free any data structures.
1851 * Everything it does has to be setup again by ipath_init_chip(dd,1)
1852 */
1853void ipath_shutdown_device(struct ipath_devdata *dd)
1854{
7bb206e3
BS
1855 ipath_dbg("Shutting down the device\n");
1856
1857 dd->ipath_flags |= IPATH_LINKUNK;
1858 dd->ipath_flags &= ~(IPATH_INITTED | IPATH_LINKDOWN |
1859 IPATH_LINKINIT | IPATH_LINKARMED |
1860 IPATH_LINKACTIVE);
1861 *dd->ipath_statusp &= ~(IPATH_STATUS_IB_CONF |
1862 IPATH_STATUS_IB_READY);
1863
1864 /* mask interrupts, but not errors */
1865 ipath_write_kreg(dd, dd->ipath_kregs->kr_intmask, 0ULL);
1866
1867 dd->ipath_rcvctrl = 0;
1868 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1869 dd->ipath_rcvctrl);
1870
1871 /*
1872 * gracefully stop all sends allowing any in progress to trickle out
1873 * first.
1874 */
1875 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, 0ULL);
1876 /* flush it */
44f8e3f3 1877 ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
7bb206e3
BS
1878 /*
1879 * enough for anything that's going to trickle out to have actually
1880 * done so.
1881 */
1882 udelay(5);
1883
1884 /*
1885 * abort any armed or launched PIO buffers that didn't go. (self
1886 * clearing). Will cause any packet currently being transmitted to
1887 * go out with an EBP, and may also cause a short packet error on
1888 * the receiver.
1889 */
1890 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
1891 INFINIPATH_S_ABORT);
1892
1893 ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKINITCMD_DISABLE <<
1894 INFINIPATH_IBCC_LINKINITCMD_SHIFT);
1895
7bb206e3
BS
1896 /* disable IBC */
1897 dd->ipath_control &= ~INFINIPATH_C_LINKENABLE;
1898 ipath_write_kreg(dd, dd->ipath_kregs->kr_control,
a40f55fc 1899 dd->ipath_control | INFINIPATH_C_FREEZEMODE);
7bb206e3
BS
1900
1901 /*
1902 * clear SerdesEnable and turn the leds off; do this here because
1903 * we are unloading, so don't count on interrupts to move along
1904 * Turn the LEDs off explictly for the same reason.
1905 */
1906 dd->ipath_f_quiet_serdes(dd);
1907 dd->ipath_f_setextled(dd, 0, 0);
1908
1909 if (dd->ipath_stats_timer_active) {
1910 del_timer_sync(&dd->ipath_stats_timer);
1911 dd->ipath_stats_timer_active = 0;
1912 }
1913
1914 /*
1915 * clear all interrupts and errors, so that the next time the driver
1916 * is loaded or device is enabled, we know that whatever is set
1917 * happened while we were unloaded
1918 */
1919 ipath_write_kreg(dd, dd->ipath_kregs->kr_hwerrclear,
1920 ~0ULL & ~INFINIPATH_HWE_MEMBISTFAILED);
1921 ipath_write_kreg(dd, dd->ipath_kregs->kr_errorclear, -1LL);
1922 ipath_write_kreg(dd, dd->ipath_kregs->kr_intclear, -1LL);
1923}
1924
1925/**
1926 * ipath_free_pddata - free a port's allocated data
1927 * @dd: the infinipath device
f37bda92 1928 * @pd: the portdata structure
7bb206e3 1929 *
f37bda92
BS
1930 * free up any allocated data for a port
1931 * This should not touch anything that would affect a simultaneous
1932 * re-allocation of port data, because it is called after ipath_mutex
1933 * is released (and can be called from reinit as well).
1934 * It should never change any chip state, or global driver state.
1935 * (The only exception to global state is freeing the port0 port0_skbs.)
7bb206e3 1936 */
f37bda92 1937void ipath_free_pddata(struct ipath_devdata *dd, struct ipath_portdata *pd)
7bb206e3 1938{
7bb206e3
BS
1939 if (!pd)
1940 return;
f37bda92
BS
1941
1942 if (pd->port_rcvhdrq) {
7bb206e3
BS
1943 ipath_cdbg(VERBOSE, "free closed port %d rcvhdrq @ %p "
1944 "(size=%lu)\n", pd->port_port, pd->port_rcvhdrq,
1945 (unsigned long) pd->port_rcvhdrq_size);
1946 dma_free_coherent(&dd->pcidev->dev, pd->port_rcvhdrq_size,
1947 pd->port_rcvhdrq, pd->port_rcvhdrq_phys);
1948 pd->port_rcvhdrq = NULL;
f37bda92
BS
1949 if (pd->port_rcvhdrtail_kvaddr) {
1950 dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
076fafcd 1951 pd->port_rcvhdrtail_kvaddr,
f37bda92
BS
1952 pd->port_rcvhdrqtailaddr_phys);
1953 pd->port_rcvhdrtail_kvaddr = NULL;
1954 }
7bb206e3 1955 }
f37bda92
BS
1956 if (pd->port_port && pd->port_rcvegrbuf) {
1957 unsigned e;
1958
1959 for (e = 0; e < pd->port_rcvegrbuf_chunks; e++) {
1960 void *base = pd->port_rcvegrbuf[e];
1961 size_t size = pd->port_rcvegrbuf_size;
1962
1963 ipath_cdbg(VERBOSE, "egrbuf free(%p, %lu), "
1964 "chunk %u/%u\n", base,
1965 (unsigned long) size,
1966 e, pd->port_rcvegrbuf_chunks);
1967 dma_free_coherent(&dd->pcidev->dev, size,
1968 base, pd->port_rcvegrbuf_phys[e]);
7bb206e3 1969 }
9929b0fb 1970 kfree(pd->port_rcvegrbuf);
f37bda92 1971 pd->port_rcvegrbuf = NULL;
9929b0fb 1972 kfree(pd->port_rcvegrbuf_phys);
f37bda92 1973 pd->port_rcvegrbuf_phys = NULL;
7bb206e3 1974 pd->port_rcvegrbuf_chunks = 0;
1fd3b40f 1975 } else if (pd->port_port == 0 && dd->ipath_port0_skbinfo) {
7bb206e3 1976 unsigned e;
1fd3b40f 1977 struct ipath_skbinfo *skbinfo = dd->ipath_port0_skbinfo;
7bb206e3 1978
1fd3b40f
BS
1979 dd->ipath_port0_skbinfo = NULL;
1980 ipath_cdbg(VERBOSE, "free closed port %d "
1981 "ipath_port0_skbinfo @ %p\n", pd->port_port,
1982 skbinfo);
7bb206e3 1983 for (e = 0; e < dd->ipath_rcvegrcnt; e++)
1fd3b40f
BS
1984 if (skbinfo[e].skb) {
1985 pci_unmap_single(dd->pcidev, skbinfo[e].phys,
1986 dd->ipath_ibmaxlen,
1987 PCI_DMA_FROMDEVICE);
1988 dev_kfree_skb(skbinfo[e].skb);
1989 }
1990 vfree(skbinfo);
7bb206e3 1991 }
f37bda92 1992 kfree(pd->port_tid_pg_list);
9929b0fb
BS
1993 vfree(pd->subport_uregbase);
1994 vfree(pd->subport_rcvegrbuf);
1995 vfree(pd->subport_rcvhdr_base);
f37bda92 1996 kfree(pd);
7bb206e3
BS
1997}
1998
ac2ae4c9 1999static int __init infinipath_init(void)
7bb206e3
BS
2000{
2001 int ret;
2002
39c0d0b9
BS
2003 if (ipath_debug & __IPATH_DBG)
2004 printk(KERN_INFO DRIVER_LOAD_MSG "%s", ib_ipath_version);
7bb206e3
BS
2005
2006 /*
2007 * These must be called before the driver is registered with
2008 * the PCI subsystem.
2009 */
2010 idr_init(&unit_table);
2011 if (!idr_pre_get(&unit_table, GFP_KERNEL)) {
2012 ret = -ENOMEM;
2013 goto bail;
2014 }
2015
2016 ret = pci_register_driver(&ipath_driver);
2017 if (ret < 0) {
2018 printk(KERN_ERR IPATH_DRV_NAME
2019 ": Unable to register driver: error %d\n", -ret);
2020 goto bail_unit;
2021 }
2022
2023 ret = ipath_driver_create_group(&ipath_driver.driver);
2024 if (ret < 0) {
2025 printk(KERN_ERR IPATH_DRV_NAME ": Unable to create driver "
2026 "sysfs entries: error %d\n", -ret);
2027 goto bail_pci;
2028 }
2029
2030 ret = ipath_init_ipathfs();
2031 if (ret < 0) {
2032 printk(KERN_ERR IPATH_DRV_NAME ": Unable to create "
2033 "ipathfs: error %d\n", -ret);
2034 goto bail_group;
2035 }
2036
2037 goto bail;
2038
2039bail_group:
2040 ipath_driver_remove_group(&ipath_driver.driver);
2041
2042bail_pci:
2043 pci_unregister_driver(&ipath_driver);
2044
2045bail_unit:
2046 idr_destroy(&unit_table);
2047
2048bail:
2049 return ret;
2050}
2051
7bb206e3
BS
2052static void __exit infinipath_cleanup(void)
2053{
7bb206e3
BS
2054 ipath_exit_ipathfs();
2055
2056 ipath_driver_remove_group(&ipath_driver.driver);
2057
7bb206e3
BS
2058 ipath_cdbg(VERBOSE, "Unregistering pci driver\n");
2059 pci_unregister_driver(&ipath_driver);
2060
2061 idr_destroy(&unit_table);
2062}
2063
2064/**
2065 * ipath_reset_device - reset the chip if possible
2066 * @unit: the device to reset
2067 *
2068 * Whether or not reset is successful, we attempt to re-initialize the chip
2069 * (that is, much like a driver unload/reload). We clear the INITTED flag
2070 * so that the various entry points will fail until we reinitialize. For
2071 * now, we only allow this if no user ports are open that use chip resources
2072 */
2073int ipath_reset_device(int unit)
2074{
2075 int ret, i;
2076 struct ipath_devdata *dd = ipath_lookup(unit);
2077
2078 if (!dd) {
2079 ret = -ENODEV;
2080 goto bail;
2081 }
2082
2083 dev_info(&dd->pcidev->dev, "Reset on unit %u requested\n", unit);
2084
2085 if (!dd->ipath_kregbase || !(dd->ipath_flags & IPATH_PRESENT)) {
2086 dev_info(&dd->pcidev->dev, "Invalid unit number %u or "
2087 "not initialized or not present\n", unit);
2088 ret = -ENXIO;
2089 goto bail;
2090 }
2091
2092 if (dd->ipath_pd)
23e86a45 2093 for (i = 1; i < dd->ipath_cfgports; i++) {
7bb206e3
BS
2094 if (dd->ipath_pd[i] && dd->ipath_pd[i]->port_cnt) {
2095 ipath_dbg("unit %u port %d is in use "
2096 "(PID %u cmd %s), can't reset\n",
2097 unit, i,
2098 dd->ipath_pd[i]->port_pid,
2099 dd->ipath_pd[i]->port_comm);
2100 ret = -EBUSY;
2101 goto bail;
2102 }
2103 }
2104
2105 dd->ipath_flags &= ~IPATH_INITTED;
2106 ret = dd->ipath_f_reset(dd);
2107 if (ret != 1)
2108 ipath_dbg("reset was not successful\n");
2109 ipath_dbg("Trying to reinitialize unit %u after reset attempt\n",
2110 unit);
2111 ret = ipath_init_chip(dd, 1);
2112 if (ret)
2113 ipath_dev_err(dd, "Reinitialize unit %u after "
2114 "reset failed with %d\n", unit, ret);
2115 else
2116 dev_info(&dd->pcidev->dev, "Reinitialized unit %u after "
2117 "resetting\n", unit);
2118
2119bail:
2120 return ret;
2121}
2122
30fc5c31
BS
2123int ipath_set_rx_pol_inv(struct ipath_devdata *dd, u8 new_pol_inv)
2124{
2125 u64 val;
2126 if ( new_pol_inv > INFINIPATH_XGXS_RX_POL_MASK ) {
2127 return -1;
2128 }
2129 if ( dd->ipath_rx_pol_inv != new_pol_inv ) {
2130 dd->ipath_rx_pol_inv = new_pol_inv;
2131 val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_xgxsconfig);
2132 val &= ~(INFINIPATH_XGXS_RX_POL_MASK <<
3cd96564
RD
2133 INFINIPATH_XGXS_RX_POL_SHIFT);
2134 val |= ((u64)dd->ipath_rx_pol_inv) <<
2135 INFINIPATH_XGXS_RX_POL_SHIFT;
30fc5c31
BS
2136 ipath_write_kreg(dd, dd->ipath_kregs->kr_xgxsconfig, val);
2137 }
2138 return 0;
2139}
7bb206e3
BS
2140module_init(infinipath_init);
2141module_exit(infinipath_cleanup);