blob: 9af7406d6a623cccf3437cdc70a7256ff9b44fb8 [file] [log] [blame]
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001/*
Bryan O'Sullivan759d5762006-07-01 04:35:49 -07002 * Copyright (c) 2006 QLogic, Inc. All rights reserved.
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08003 * 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"
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -080042#include "ipath_layer.h"
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -070043#include "ipath_verbs.h"
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -070044#include "ipath_common.h"
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -080045
46static void ipath_update_pio_bufs(struct ipath_devdata *);
47
48const char *ipath_get_unit_name(int unit)
49{
50 static char iname[16];
51 snprintf(iname, sizeof iname, "infinipath%u", unit);
52 return iname;
53}
54
Bryan O'Sullivan759d5762006-07-01 04:35:49 -070055#define DRIVER_LOAD_MSG "QLogic " IPATH_DRV_NAME " loaded: "
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -080056#define PFX IPATH_DRV_NAME ": "
57
58/*
59 * The size has to be longer than this string, so we can append
60 * board/chip information to it in the init code.
61 */
62const char ipath_core_version[] = IPATH_IDSTR "\n";
63
64static struct idr unit_table;
65DEFINE_SPINLOCK(ipath_devs_lock);
66LIST_HEAD(ipath_dev_list);
67
68wait_queue_head_t ipath_sma_state_wait;
69
70unsigned ipath_debug = __IPATH_INFO;
71
72module_param_named(debug, ipath_debug, uint, S_IWUSR | S_IRUGO);
73MODULE_PARM_DESC(debug, "mask for debug prints");
74EXPORT_SYMBOL_GPL(ipath_debug);
75
76MODULE_LICENSE("GPL");
Bryan O'Sullivan759d5762006-07-01 04:35:49 -070077MODULE_AUTHOR("QLogic <support@pathscale.com>");
78MODULE_DESCRIPTION("QLogic InfiniPath driver");
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -080079
80const char *ipath_ibcstatus_str[] = {
81 "Disabled",
82 "LinkUp",
83 "PollActive",
84 "PollQuiet",
85 "SleepDelay",
86 "SleepQuiet",
87 "LState6", /* unused */
88 "LState7", /* unused */
89 "CfgDebounce",
90 "CfgRcvfCfg",
91 "CfgWaitRmt",
92 "CfgIdle",
93 "RecovRetrain",
94 "LState0xD", /* unused */
95 "RecovWaitRmt",
96 "RecovIdle",
97};
98
99/*
100 * These variables are initialized in the chip-specific files
101 * but are defined here.
102 */
103u16 ipath_gpio_sda_num, ipath_gpio_scl_num;
104u64 ipath_gpio_sda, ipath_gpio_scl;
105u64 infinipath_i_bitsextant;
106ipath_err_t infinipath_e_bitsextant, infinipath_hwe_bitsextant;
107u32 infinipath_i_rcvavail_mask, infinipath_i_rcvurg_mask;
108
109static void __devexit ipath_remove_one(struct pci_dev *);
110static int __devinit ipath_init_one(struct pci_dev *,
111 const struct pci_device_id *);
112
113/* Only needed for registration, nothing else needs this info */
114#define PCI_VENDOR_ID_PATHSCALE 0x1fc1
115#define PCI_DEVICE_ID_INFINIPATH_HT 0xd
116#define PCI_DEVICE_ID_INFINIPATH_PE800 0x10
117
118static const struct pci_device_id ipath_pci_tbl[] = {
Roland Dreier6f4bb3d2006-05-12 14:57:52 -0700119 { PCI_DEVICE(PCI_VENDOR_ID_PATHSCALE, PCI_DEVICE_ID_INFINIPATH_HT) },
120 { PCI_DEVICE(PCI_VENDOR_ID_PATHSCALE, PCI_DEVICE_ID_INFINIPATH_PE800) },
121 { 0, }
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800122};
123
124MODULE_DEVICE_TABLE(pci, ipath_pci_tbl);
125
126static struct pci_driver ipath_driver = {
127 .name = IPATH_DRV_NAME,
128 .probe = ipath_init_one,
129 .remove = __devexit_p(ipath_remove_one),
130 .id_table = ipath_pci_tbl,
131};
132
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800133
134static inline void read_bars(struct ipath_devdata *dd, struct pci_dev *dev,
135 u32 *bar0, u32 *bar1)
136{
137 int ret;
138
139 ret = pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, bar0);
140 if (ret)
141 ipath_dev_err(dd, "failed to read bar0 before enable: "
142 "error %d\n", -ret);
143
144 ret = pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, bar1);
145 if (ret)
146 ipath_dev_err(dd, "failed to read bar1 before enable: "
147 "error %d\n", -ret);
148
149 ipath_dbg("Read bar0 %x bar1 %x\n", *bar0, *bar1);
150}
151
152static void ipath_free_devdata(struct pci_dev *pdev,
153 struct ipath_devdata *dd)
154{
155 unsigned long flags;
156
157 pci_set_drvdata(pdev, NULL);
158
159 if (dd->ipath_unit != -1) {
160 spin_lock_irqsave(&ipath_devs_lock, flags);
161 idr_remove(&unit_table, dd->ipath_unit);
162 list_del(&dd->ipath_list);
163 spin_unlock_irqrestore(&ipath_devs_lock, flags);
164 }
Bryan O'Sullivan06993ca2006-07-01 04:36:02 -0700165 vfree(dd);
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800166}
167
168static struct ipath_devdata *ipath_alloc_devdata(struct pci_dev *pdev)
169{
170 unsigned long flags;
171 struct ipath_devdata *dd;
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800172 int ret;
173
174 if (!idr_pre_get(&unit_table, GFP_KERNEL)) {
175 dd = ERR_PTR(-ENOMEM);
176 goto bail;
177 }
178
Bryan O'Sullivan06993ca2006-07-01 04:36:02 -0700179 dd = vmalloc(sizeof(*dd));
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800180 if (!dd) {
181 dd = ERR_PTR(-ENOMEM);
182 goto bail;
183 }
Bryan O'Sullivan06993ca2006-07-01 04:36:02 -0700184 memset(dd, 0, sizeof(*dd));
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800185 dd->ipath_unit = -1;
186
187 spin_lock_irqsave(&ipath_devs_lock, flags);
188
189 ret = idr_get_new(&unit_table, dd, &dd->ipath_unit);
190 if (ret < 0) {
191 printk(KERN_ERR IPATH_DRV_NAME
192 ": Could not allocate unit ID: error %d\n", -ret);
193 ipath_free_devdata(pdev, dd);
194 dd = ERR_PTR(ret);
195 goto bail_unlock;
196 }
197
198 dd->pcidev = pdev;
199 pci_set_drvdata(pdev, dd);
200
201 list_add(&dd->ipath_list, &ipath_dev_list);
202
203bail_unlock:
204 spin_unlock_irqrestore(&ipath_devs_lock, flags);
205
206bail:
207 return dd;
208}
209
210static inline struct ipath_devdata *__ipath_lookup(int unit)
211{
212 return idr_find(&unit_table, unit);
213}
214
215struct ipath_devdata *ipath_lookup(int unit)
216{
217 struct ipath_devdata *dd;
218 unsigned long flags;
219
220 spin_lock_irqsave(&ipath_devs_lock, flags);
221 dd = __ipath_lookup(unit);
222 spin_unlock_irqrestore(&ipath_devs_lock, flags);
223
224 return dd;
225}
226
227int ipath_count_units(int *npresentp, int *nupp, u32 *maxportsp)
228{
229 int nunits, npresent, nup;
230 struct ipath_devdata *dd;
231 unsigned long flags;
232 u32 maxports;
233
234 nunits = npresent = nup = maxports = 0;
235
236 spin_lock_irqsave(&ipath_devs_lock, flags);
237
238 list_for_each_entry(dd, &ipath_dev_list, ipath_list) {
239 nunits++;
240 if ((dd->ipath_flags & IPATH_PRESENT) && dd->ipath_kregbase)
241 npresent++;
242 if (dd->ipath_lid &&
243 !(dd->ipath_flags & (IPATH_DISABLED | IPATH_LINKDOWN
244 | IPATH_LINKUNK)))
245 nup++;
246 if (dd->ipath_cfgports > maxports)
247 maxports = dd->ipath_cfgports;
248 }
249
250 spin_unlock_irqrestore(&ipath_devs_lock, flags);
251
252 if (npresentp)
253 *npresentp = npresent;
254 if (nupp)
255 *nupp = nup;
256 if (maxportsp)
257 *maxportsp = maxports;
258
259 return nunits;
260}
261
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800262/*
263 * These next two routines are placeholders in case we don't have per-arch
264 * code for controlling write combining. If explicit control of write
265 * combining is not available, performance will probably be awful.
266 */
267
268int __attribute__((weak)) ipath_enable_wc(struct ipath_devdata *dd)
269{
270 return -EOPNOTSUPP;
271}
272
273void __attribute__((weak)) ipath_disable_wc(struct ipath_devdata *dd)
274{
275}
276
277static int __devinit ipath_init_one(struct pci_dev *pdev,
278 const struct pci_device_id *ent)
279{
280 int ret, len, j;
281 struct ipath_devdata *dd;
282 unsigned long long addr;
283 u32 bar0 = 0, bar1 = 0;
284 u8 rev;
285
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800286 dd = ipath_alloc_devdata(pdev);
287 if (IS_ERR(dd)) {
288 ret = PTR_ERR(dd);
289 printk(KERN_ERR IPATH_DRV_NAME
290 ": Could not allocate devdata: error %d\n", -ret);
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -0700291 goto bail;
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800292 }
293
294 ipath_cdbg(VERBOSE, "initializing unit #%u\n", dd->ipath_unit);
295
296 read_bars(dd, pdev, &bar0, &bar1);
297
298 ret = pci_enable_device(pdev);
299 if (ret) {
300 /* This can happen iff:
301 *
302 * We did a chip reset, and then failed to reprogram the
303 * BAR, or the chip reset due to an internal error. We then
304 * unloaded the driver and reloaded it.
305 *
306 * Both reset cases set the BAR back to initial state. For
307 * the latter case, the AER sticky error bit at offset 0x718
308 * should be set, but the Linux kernel doesn't yet know
309 * about that, it appears. If the original BAR was retained
310 * in the kernel data structures, this may be OK.
311 */
312 ipath_dev_err(dd, "enable unit %d failed: error %d\n",
313 dd->ipath_unit, -ret);
314 goto bail_devdata;
315 }
316 addr = pci_resource_start(pdev, 0);
317 len = pci_resource_len(pdev, 0);
318 ipath_cdbg(VERBOSE, "regbase (0) %llx len %d irq %x, vend %x/%x "
319 "driver_data %lx\n", addr, len, pdev->irq, ent->vendor,
320 ent->device, ent->driver_data);
321
322 read_bars(dd, pdev, &bar0, &bar1);
323
324 if (!bar1 && !(bar0 & ~0xf)) {
325 if (addr) {
326 dev_info(&pdev->dev, "BAR is 0 (probable RESET), "
327 "rewriting as %llx\n", addr);
328 ret = pci_write_config_dword(
329 pdev, PCI_BASE_ADDRESS_0, addr);
330 if (ret) {
331 ipath_dev_err(dd, "rewrite of BAR0 "
332 "failed: err %d\n", -ret);
333 goto bail_disable;
334 }
335 ret = pci_write_config_dword(
336 pdev, PCI_BASE_ADDRESS_1, addr >> 32);
337 if (ret) {
338 ipath_dev_err(dd, "rewrite of BAR1 "
339 "failed: err %d\n", -ret);
340 goto bail_disable;
341 }
342 } else {
343 ipath_dev_err(dd, "BAR is 0 (probable RESET), "
344 "not usable until reboot\n");
345 ret = -ENODEV;
346 goto bail_disable;
347 }
348 }
349
350 ret = pci_request_regions(pdev, IPATH_DRV_NAME);
351 if (ret) {
352 dev_info(&pdev->dev, "pci_request_regions unit %u fails: "
353 "err %d\n", dd->ipath_unit, -ret);
354 goto bail_disable;
355 }
356
357 ret = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
358 if (ret) {
Bryan O'Sullivan68dd43a2006-04-24 14:22:58 -0700359 /*
360 * if the 64 bit setup fails, try 32 bit. Some systems
361 * do not setup 64 bit maps on systems with 2GB or less
362 * memory installed.
363 */
364 ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
365 if (ret) {
Bryan O'Sullivanb1d88652006-07-01 04:35:59 -0700366 dev_info(&pdev->dev,
367 "Unable to set DMA mask for unit %u: %d\n",
368 dd->ipath_unit, ret);
Bryan O'Sullivan68dd43a2006-04-24 14:22:58 -0700369 goto bail_regions;
370 }
Bryan O'Sullivanb1d88652006-07-01 04:35:59 -0700371 else {
Bryan O'Sullivan68dd43a2006-04-24 14:22:58 -0700372 ipath_dbg("No 64bit DMA mask, used 32 bit mask\n");
Bryan O'Sullivanb1d88652006-07-01 04:35:59 -0700373 ret = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
374 if (ret)
375 dev_info(&pdev->dev,
376 "Unable to set DMA consistent mask "
377 "for unit %u: %d\n",
378 dd->ipath_unit, ret);
379
380 }
381 }
382 else {
383 ret = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
384 if (ret)
385 dev_info(&pdev->dev,
386 "Unable to set DMA consistent mask "
387 "for unit %u: %d\n",
388 dd->ipath_unit, ret);
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800389 }
390
391 pci_set_master(pdev);
392
393 /*
394 * Save BARs to rewrite after device reset. Save all 64 bits of
395 * BAR, just in case.
396 */
397 dd->ipath_pcibar0 = addr;
398 dd->ipath_pcibar1 = addr >> 32;
399 dd->ipath_deviceid = ent->device; /* save for later use */
400 dd->ipath_vendorid = ent->vendor;
401
402 /* setup the chip-specific functions, as early as possible. */
403 switch (ent->device) {
404 case PCI_DEVICE_ID_INFINIPATH_HT:
405 ipath_init_ht400_funcs(dd);
406 break;
407 case PCI_DEVICE_ID_INFINIPATH_PE800:
408 ipath_init_pe800_funcs(dd);
409 break;
410 default:
Bryan O'Sullivan759d5762006-07-01 04:35:49 -0700411 ipath_dev_err(dd, "Found unknown QLogic deviceid 0x%x, "
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800412 "failing\n", ent->device);
413 return -ENODEV;
414 }
415
416 for (j = 0; j < 6; j++) {
417 if (!pdev->resource[j].start)
418 continue;
Greg Kroah-Hartmane29419f2006-06-12 15:20:16 -0700419 ipath_cdbg(VERBOSE, "BAR %d start %llx, end %llx, len %llx\n",
420 j, (unsigned long long)pdev->resource[j].start,
421 (unsigned long long)pdev->resource[j].end,
422 (unsigned long long)pci_resource_len(pdev, j));
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800423 }
424
425 if (!addr) {
426 ipath_dev_err(dd, "No valid address in BAR 0!\n");
427 ret = -ENODEV;
428 goto bail_regions;
429 }
430
431 dd->ipath_deviceid = ent->device; /* save for later use */
432 dd->ipath_vendorid = ent->vendor;
433
434 ret = pci_read_config_byte(pdev, PCI_REVISION_ID, &rev);
435 if (ret) {
436 ipath_dev_err(dd, "Failed to read PCI revision ID unit "
437 "%u: err %d\n", dd->ipath_unit, -ret);
438 goto bail_regions; /* shouldn't ever happen */
439 }
440 dd->ipath_pcirev = rev;
441
Bryan O'Sullivaneb9dc6f2006-08-25 11:24:26 -0700442#if defined(__powerpc__)
443 /* There isn't a generic way to specify writethrough mappings */
444 dd->ipath_kregbase = __ioremap(addr, len,
445 (_PAGE_NO_CACHE|_PAGE_WRITETHRU));
446#else
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800447 dd->ipath_kregbase = ioremap_nocache(addr, len);
Bryan O'Sullivaneb9dc6f2006-08-25 11:24:26 -0700448#endif
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800449
450 if (!dd->ipath_kregbase) {
451 ipath_dbg("Unable to map io addr %llx to kvirt, failing\n",
452 addr);
453 ret = -ENOMEM;
454 goto bail_iounmap;
455 }
456 dd->ipath_kregend = (u64 __iomem *)
457 ((void __iomem *)dd->ipath_kregbase + len);
458 dd->ipath_physaddr = addr; /* used for io_remap, etc. */
459 /* for user mmap */
Bryan O'Sullivanb35f0042006-07-01 04:35:59 -0700460 ipath_cdbg(VERBOSE, "mapped io addr %llx to kregbase %p\n",
461 addr, dd->ipath_kregbase);
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800462
463 /*
464 * clear ipath_flags here instead of in ipath_init_chip as it is set
465 * by ipath_setup_htconfig.
466 */
467 dd->ipath_flags = 0;
Bryan O'Sullivanfba75202006-07-01 04:36:09 -0700468 dd->ipath_lli_counter = 0;
469 dd->ipath_lli_errors = 0;
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800470
471 if (dd->ipath_f_bus(dd, pdev))
472 ipath_dev_err(dd, "Failed to setup config space; "
473 "continuing anyway\n");
474
475 /*
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700476 * set up our interrupt handler; IRQF_SHARED probably not needed,
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800477 * since MSI interrupts shouldn't be shared but won't hurt for now.
478 * check 0 irq after we return from chip-specific bus setup, since
479 * that can affect this due to setup
480 */
481 if (!pdev->irq)
482 ipath_dev_err(dd, "irq is 0, BIOS error? Interrupts won't "
483 "work\n");
484 else {
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700485 ret = request_irq(pdev->irq, ipath_intr, IRQF_SHARED,
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800486 IPATH_DRV_NAME, dd);
487 if (ret) {
488 ipath_dev_err(dd, "Couldn't setup irq handler, "
489 "irq=%u: %d\n", pdev->irq, ret);
490 goto bail_iounmap;
491 }
492 }
493
494 ret = ipath_init_chip(dd, 0); /* do the chip-specific init */
495 if (ret)
496 goto bail_iounmap;
497
498 ret = ipath_enable_wc(dd);
499
500 if (ret) {
501 ipath_dev_err(dd, "Write combining not enabled "
502 "(err %d): performance may be poor\n",
503 -ret);
504 ret = 0;
505 }
506
507 ipath_device_create_group(&pdev->dev, dd);
508 ipathfs_add_device(dd);
509 ipath_user_add(dd);
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700510 ipath_diag_add(dd);
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800511 ipath_layer_add(dd);
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -0700512 ipath_register_ib_device(dd);
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800513
514 goto bail;
515
516bail_iounmap:
517 iounmap((volatile void __iomem *) dd->ipath_kregbase);
518
519bail_regions:
520 pci_release_regions(pdev);
521
522bail_disable:
523 pci_disable_device(pdev);
524
525bail_devdata:
526 ipath_free_devdata(pdev, dd);
527
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800528bail:
529 return ret;
530}
531
532static void __devexit ipath_remove_one(struct pci_dev *pdev)
533{
534 struct ipath_devdata *dd;
535
536 ipath_cdbg(VERBOSE, "removing, pdev=%p\n", pdev);
537 if (!pdev)
538 return;
539
540 dd = pci_get_drvdata(pdev);
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -0700541 ipath_unregister_ib_device(dd->verbs_dev);
Bryan O'Sullivana2acb2f2006-07-01 04:35:52 -0700542 ipath_layer_remove(dd);
543 ipath_diag_remove(dd);
544 ipath_user_remove(dd);
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800545 ipathfs_remove_device(dd);
546 ipath_device_remove_group(&pdev->dev, dd);
547 ipath_cdbg(VERBOSE, "Releasing pci memory regions, dd %p, "
548 "unit %u\n", dd, (u32) dd->ipath_unit);
549 if (dd->ipath_kregbase) {
550 ipath_cdbg(VERBOSE, "Unmapping kregbase %p\n",
551 dd->ipath_kregbase);
552 iounmap((volatile void __iomem *) dd->ipath_kregbase);
553 dd->ipath_kregbase = NULL;
554 }
555 pci_release_regions(pdev);
556 ipath_cdbg(VERBOSE, "calling pci_disable_device\n");
557 pci_disable_device(pdev);
558
559 ipath_free_devdata(pdev, dd);
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800560}
561
562/* general driver use */
563DEFINE_MUTEX(ipath_mutex);
564
565static DEFINE_SPINLOCK(ipath_pioavail_lock);
566
567/**
568 * ipath_disarm_piobufs - cancel a range of PIO buffers
569 * @dd: the infinipath device
570 * @first: the first PIO buffer to cancel
571 * @cnt: the number of PIO buffers to cancel
572 *
573 * cancel a range of PIO buffers, used when they might be armed, but
574 * not triggered. Used at init to ensure buffer state, and also user
575 * process close, in case it died while writing to a PIO buffer
576 * Also after errors.
577 */
578void ipath_disarm_piobufs(struct ipath_devdata *dd, unsigned first,
579 unsigned cnt)
580{
581 unsigned i, last = first + cnt;
582 u64 sendctrl, sendorig;
583
584 ipath_cdbg(PKT, "disarm %u PIObufs first=%u\n", cnt, first);
585 sendorig = dd->ipath_sendctrl | INFINIPATH_S_DISARM;
586 for (i = first; i < last; i++) {
587 sendctrl = sendorig |
588 (i << INFINIPATH_S_DISARMPIOBUF_SHIFT);
589 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
590 sendctrl);
591 }
592
593 /*
594 * Write it again with current value, in case ipath_sendctrl changed
595 * while we were looping; no critical bits that would require
596 * locking.
597 *
598 * Write a 0, and then the original value, reading scratch in
599 * between. This seems to avoid a chip timing race that causes
600 * pioavail updates to memory to stop.
601 */
602 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
603 0);
604 sendorig = ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
605 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
606 dd->ipath_sendctrl);
607}
608
609/**
610 * ipath_wait_linkstate - wait for an IB link state change to occur
611 * @dd: the infinipath device
612 * @state: the state to wait for
613 * @msecs: the number of milliseconds to wait
614 *
615 * wait up to msecs milliseconds for IB link state change to occur for
616 * now, take the easy polling route. Currently used only by
617 * ipath_layer_set_linkstate. Returns 0 if state reached, otherwise
618 * -ETIMEDOUT state can have multiple states set, for any of several
619 * transitions.
620 */
621int ipath_wait_linkstate(struct ipath_devdata *dd, u32 state, int msecs)
622{
623 dd->ipath_sma_state_wanted = state;
624 wait_event_interruptible_timeout(ipath_sma_state_wait,
625 (dd->ipath_flags & state),
626 msecs_to_jiffies(msecs));
627 dd->ipath_sma_state_wanted = 0;
628
629 if (!(dd->ipath_flags & state)) {
630 u64 val;
631 ipath_cdbg(SMA, "Didn't reach linkstate %s within %u ms\n",
632 /* test INIT ahead of DOWN, both can be set */
633 (state & IPATH_LINKINIT) ? "INIT" :
634 ((state & IPATH_LINKDOWN) ? "DOWN" :
635 ((state & IPATH_LINKARMED) ? "ARM" : "ACTIVE")),
636 msecs);
637 val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_ibcstatus);
638 ipath_cdbg(VERBOSE, "ibcc=%llx ibcstatus=%llx (%s)\n",
639 (unsigned long long) ipath_read_kreg64(
640 dd, dd->ipath_kregs->kr_ibcctrl),
641 (unsigned long long) val,
642 ipath_ibcstatus_str[val & 0xf]);
643 }
644 return (dd->ipath_flags & state) ? 0 : -ETIMEDOUT;
645}
646
647void ipath_decode_err(char *buf, size_t blen, ipath_err_t err)
648{
649 *buf = '\0';
650 if (err & INFINIPATH_E_RHDRLEN)
651 strlcat(buf, "rhdrlen ", blen);
652 if (err & INFINIPATH_E_RBADTID)
653 strlcat(buf, "rbadtid ", blen);
654 if (err & INFINIPATH_E_RBADVERSION)
655 strlcat(buf, "rbadversion ", blen);
656 if (err & INFINIPATH_E_RHDR)
657 strlcat(buf, "rhdr ", blen);
658 if (err & INFINIPATH_E_RLONGPKTLEN)
659 strlcat(buf, "rlongpktlen ", blen);
660 if (err & INFINIPATH_E_RSHORTPKTLEN)
661 strlcat(buf, "rshortpktlen ", blen);
662 if (err & INFINIPATH_E_RMAXPKTLEN)
663 strlcat(buf, "rmaxpktlen ", blen);
664 if (err & INFINIPATH_E_RMINPKTLEN)
665 strlcat(buf, "rminpktlen ", blen);
666 if (err & INFINIPATH_E_RFORMATERR)
667 strlcat(buf, "rformaterr ", blen);
668 if (err & INFINIPATH_E_RUNSUPVL)
669 strlcat(buf, "runsupvl ", blen);
670 if (err & INFINIPATH_E_RUNEXPCHAR)
671 strlcat(buf, "runexpchar ", blen);
672 if (err & INFINIPATH_E_RIBFLOW)
673 strlcat(buf, "ribflow ", blen);
674 if (err & INFINIPATH_E_REBP)
675 strlcat(buf, "EBP ", blen);
676 if (err & INFINIPATH_E_SUNDERRUN)
677 strlcat(buf, "sunderrun ", blen);
678 if (err & INFINIPATH_E_SPIOARMLAUNCH)
679 strlcat(buf, "spioarmlaunch ", blen);
680 if (err & INFINIPATH_E_SUNEXPERRPKTNUM)
681 strlcat(buf, "sunexperrpktnum ", blen);
682 if (err & INFINIPATH_E_SDROPPEDDATAPKT)
683 strlcat(buf, "sdroppeddatapkt ", blen);
684 if (err & INFINIPATH_E_SDROPPEDSMPPKT)
685 strlcat(buf, "sdroppedsmppkt ", blen);
686 if (err & INFINIPATH_E_SMAXPKTLEN)
687 strlcat(buf, "smaxpktlen ", blen);
688 if (err & INFINIPATH_E_SMINPKTLEN)
689 strlcat(buf, "sminpktlen ", blen);
690 if (err & INFINIPATH_E_SUNSUPVL)
691 strlcat(buf, "sunsupVL ", blen);
692 if (err & INFINIPATH_E_SPKTLEN)
693 strlcat(buf, "spktlen ", blen);
694 if (err & INFINIPATH_E_INVALIDADDR)
695 strlcat(buf, "invalidaddr ", blen);
696 if (err & INFINIPATH_E_RICRC)
697 strlcat(buf, "CRC ", blen);
698 if (err & INFINIPATH_E_RVCRC)
699 strlcat(buf, "VCRC ", blen);
700 if (err & INFINIPATH_E_RRCVEGRFULL)
701 strlcat(buf, "rcvegrfull ", blen);
702 if (err & INFINIPATH_E_RRCVHDRFULL)
703 strlcat(buf, "rcvhdrfull ", blen);
704 if (err & INFINIPATH_E_IBSTATUSCHANGED)
705 strlcat(buf, "ibcstatuschg ", blen);
706 if (err & INFINIPATH_E_RIBLOSTLINK)
707 strlcat(buf, "riblostlink ", blen);
708 if (err & INFINIPATH_E_HARDWARE)
709 strlcat(buf, "hardware ", blen);
710 if (err & INFINIPATH_E_RESET)
711 strlcat(buf, "reset ", blen);
712}
713
714/**
715 * get_rhf_errstring - decode RHF errors
716 * @err: the err number
717 * @msg: the output buffer
718 * @len: the length of the output buffer
719 *
720 * only used one place now, may want more later
721 */
722static void get_rhf_errstring(u32 err, char *msg, size_t len)
723{
724 /* if no errors, and so don't need to check what's first */
725 *msg = '\0';
726
727 if (err & INFINIPATH_RHF_H_ICRCERR)
728 strlcat(msg, "icrcerr ", len);
729 if (err & INFINIPATH_RHF_H_VCRCERR)
730 strlcat(msg, "vcrcerr ", len);
731 if (err & INFINIPATH_RHF_H_PARITYERR)
732 strlcat(msg, "parityerr ", len);
733 if (err & INFINIPATH_RHF_H_LENERR)
734 strlcat(msg, "lenerr ", len);
735 if (err & INFINIPATH_RHF_H_MTUERR)
736 strlcat(msg, "mtuerr ", len);
737 if (err & INFINIPATH_RHF_H_IHDRERR)
738 /* infinipath hdr checksum error */
739 strlcat(msg, "ipathhdrerr ", len);
740 if (err & INFINIPATH_RHF_H_TIDERR)
741 strlcat(msg, "tiderr ", len);
742 if (err & INFINIPATH_RHF_H_MKERR)
743 /* bad port, offset, etc. */
744 strlcat(msg, "invalid ipathhdr ", len);
745 if (err & INFINIPATH_RHF_H_IBERR)
746 strlcat(msg, "iberr ", len);
747 if (err & INFINIPATH_RHF_L_SWA)
748 strlcat(msg, "swA ", len);
749 if (err & INFINIPATH_RHF_L_SWB)
750 strlcat(msg, "swB ", len);
751}
752
753/**
754 * ipath_get_egrbuf - get an eager buffer
755 * @dd: the infinipath device
756 * @bufnum: the eager buffer to get
757 * @err: unused
758 *
759 * must only be called if ipath_pd[port] is known to be allocated
760 */
761static inline void *ipath_get_egrbuf(struct ipath_devdata *dd, u32 bufnum,
762 int err)
763{
764 return dd->ipath_port0_skbs ?
765 (void *)dd->ipath_port0_skbs[bufnum]->data : NULL;
766}
767
768/**
769 * ipath_alloc_skb - allocate an skb and buffer with possible constraints
770 * @dd: the infinipath device
771 * @gfp_mask: the sk_buff SFP mask
772 */
773struct sk_buff *ipath_alloc_skb(struct ipath_devdata *dd,
774 gfp_t gfp_mask)
775{
776 struct sk_buff *skb;
777 u32 len;
778
779 /*
780 * Only fully supported way to handle this is to allocate lots
781 * extra, align as needed, and then do skb_reserve(). That wastes
782 * a lot of memory... I'll have to hack this into infinipath_copy
783 * also.
784 */
785
786 /*
787 * We need 4 extra bytes for unaligned transfer copying
788 */
789 if (dd->ipath_flags & IPATH_4BYTE_TID) {
790 /* we need a 4KB multiple alignment, and there is no way
791 * to do it except to allocate extra and then skb_reserve
792 * enough to bring it up to the right alignment.
793 */
794 len = dd->ipath_ibmaxlen + 4 + (1 << 11) - 1;
795 }
796 else
797 len = dd->ipath_ibmaxlen + 4;
798 skb = __dev_alloc_skb(len, gfp_mask);
799 if (!skb) {
800 ipath_dev_err(dd, "Failed to allocate skbuff, length %u\n",
801 len);
802 goto bail;
803 }
804 if (dd->ipath_flags & IPATH_4BYTE_TID) {
805 u32 una = ((1 << 11) - 1) & (unsigned long)(skb->data + 4);
806 if (una)
807 skb_reserve(skb, 4 + (1 << 11) - una);
808 else
809 skb_reserve(skb, 4);
810 } else
811 skb_reserve(skb, 4);
812
813bail:
814 return skb;
815}
816
817/**
818 * ipath_rcv_layer - receive a packet for the layered (ethernet) driver
819 * @dd: the infinipath device
820 * @etail: the sk_buff number
821 * @tlen: the total packet length
822 * @hdr: the ethernet header
823 *
824 * Separate routine for better overall optimization
825 */
826static void ipath_rcv_layer(struct ipath_devdata *dd, u32 etail,
827 u32 tlen, struct ether_header *hdr)
828{
829 u32 elen;
830 u8 pad, *bthbytes;
831 struct sk_buff *skb, *nskb;
832
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700833 if (dd->ipath_port0_skbs &&
834 hdr->sub_opcode == IPATH_ITH4X_OPCODE_ENCAP) {
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800835 /*
836 * Allocate a new sk_buff to replace the one we give
837 * to the network stack.
838 */
839 nskb = ipath_alloc_skb(dd, GFP_ATOMIC);
840 if (!nskb) {
841 /* count OK packets that we drop */
842 ipath_stats.sps_krdrops++;
843 return;
844 }
845
846 bthbytes = (u8 *) hdr->bth;
847 pad = (bthbytes[1] >> 4) & 3;
848 /* +CRC32 */
849 elen = tlen - (sizeof(*hdr) + pad + sizeof(u32));
850
851 skb = dd->ipath_port0_skbs[etail];
852 dd->ipath_port0_skbs[etail] = nskb;
853 skb_put(skb, elen);
854
855 dd->ipath_f_put_tid(dd, etail + (u64 __iomem *)
856 ((char __iomem *) dd->ipath_kregbase
857 + dd->ipath_rcvegrbase), 0,
858 virt_to_phys(nskb->data));
859
860 __ipath_layer_rcv(dd, hdr, skb);
861
862 /* another ether packet received */
863 ipath_stats.sps_ether_rpkts++;
864 }
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700865 else if (hdr->sub_opcode == IPATH_ITH4X_OPCODE_LID_ARP)
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800866 __ipath_layer_rcv_lid(dd, hdr);
867}
868
Ralph Campbell3d37b9e2006-07-17 18:18:36 -0700869static void ipath_rcv_hdrerr(struct ipath_devdata *dd,
870 u32 eflags,
871 u32 l,
872 u32 etail,
873 u64 *rc)
874{
875 char emsg[128];
876 struct ipath_message_header *hdr;
877
878 get_rhf_errstring(eflags, emsg, sizeof emsg);
879 hdr = (struct ipath_message_header *)&rc[1];
880 ipath_cdbg(PKT, "RHFerrs %x hdrqtail=%x typ=%u "
881 "tlen=%x opcode=%x egridx=%x: %s\n",
882 eflags, l,
883 ipath_hdrget_rcv_type((__le32 *) rc),
884 ipath_hdrget_length_in_bytes((__le32 *) rc),
885 be32_to_cpu(hdr->bth[0]) >> 24,
886 etail, emsg);
887
888 /* Count local link integrity errors. */
889 if (eflags & (INFINIPATH_RHF_H_ICRCERR | INFINIPATH_RHF_H_VCRCERR)) {
890 u8 n = (dd->ipath_ibcctrl >>
891 INFINIPATH_IBCC_PHYERRTHRESHOLD_SHIFT) &
892 INFINIPATH_IBCC_PHYERRTHRESHOLD_MASK;
893
894 if (++dd->ipath_lli_counter > n) {
895 dd->ipath_lli_counter = 0;
896 dd->ipath_lli_errors++;
897 }
898 }
899}
900
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800901/*
902 * ipath_kreceive - receive a packet
903 * @dd: the infinipath device
904 *
905 * called from interrupt handler for errors or receive interrupt
906 */
907void ipath_kreceive(struct ipath_devdata *dd)
908{
909 u64 *rc;
910 void *ebuf;
911 const u32 rsize = dd->ipath_rcvhdrentsize; /* words */
912 const u32 maxcnt = dd->ipath_rcvhdrcnt * rsize; /* words */
913 u32 etail = -1, l, hdrqtail;
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700914 struct ipath_message_header *hdr;
Bryan O'Sullivan57abad22006-07-01 04:36:05 -0700915 u32 eflags, i, etype, tlen, pkttot = 0, updegr=0, reloop=0;
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800916 static u64 totcalls; /* stats, may eventually remove */
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800917
918 if (!dd->ipath_hdrqtailptr) {
919 ipath_dev_err(dd,
920 "hdrqtailptr not set, can't do receives\n");
921 goto bail;
922 }
923
924 /* There is already a thread processing this queue. */
925 if (test_and_set_bit(0, &dd->ipath_rcv_pending))
926 goto bail;
927
Bryan O'Sullivanf5f99922006-07-01 04:36:05 -0700928 l = dd->ipath_port0head;
Bryan O'Sullivan57abad22006-07-01 04:36:05 -0700929 hdrqtail = (u32) le64_to_cpu(*dd->ipath_hdrqtailptr);
930 if (l == hdrqtail)
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800931 goto done;
932
Bryan O'Sullivan57abad22006-07-01 04:36:05 -0700933reloop:
Bryan O'Sullivanf5f99922006-07-01 04:36:05 -0700934 for (i = 0; l != hdrqtail; i++) {
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800935 u32 qp;
936 u8 *bthbytes;
937
938 rc = (u64 *) (dd->ipath_pd[0]->port_rcvhdrq + (l << 2));
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700939 hdr = (struct ipath_message_header *)&rc[1];
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800940 /*
941 * could make a network order version of IPATH_KD_QP, and
942 * do the obvious shift before masking to speed this up.
943 */
944 qp = ntohl(hdr->bth[1]) & 0xffffff;
945 bthbytes = (u8 *) hdr->bth;
946
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700947 eflags = ipath_hdrget_err_flags((__le32 *) rc);
948 etype = ipath_hdrget_rcv_type((__le32 *) rc);
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800949 /* total length */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700950 tlen = ipath_hdrget_length_in_bytes((__le32 *) rc);
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800951 ebuf = NULL;
952 if (etype != RCVHQ_RCV_TYPE_EXPECTED) {
953 /*
954 * it turns out that the chips uses an eager buffer
955 * for all non-expected packets, whether it "needs"
956 * one or not. So always get the index, but don't
957 * set ebuf (so we try to copy data) unless the
958 * length requires it.
959 */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700960 etail = ipath_hdrget_index((__le32 *) rc);
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800961 if (tlen > sizeof(*hdr) ||
962 etype == RCVHQ_RCV_TYPE_NON_KD)
963 ebuf = ipath_get_egrbuf(dd, etail, 0);
964 }
965
966 /*
967 * both tiderr and ipathhdrerr are set for all plain IB
968 * packets; only ipathhdrerr should be set.
969 */
970
971 if (etype != RCVHQ_RCV_TYPE_NON_KD && etype !=
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700972 RCVHQ_RCV_TYPE_ERROR && ipath_hdrget_ipath_ver(
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800973 hdr->iph.ver_port_tid_offset) !=
974 IPS_PROTO_VERSION) {
975 ipath_cdbg(PKT, "Bad InfiniPath protocol version "
976 "%x\n", etype);
977 }
978
Ralph Campbell3d37b9e2006-07-17 18:18:36 -0700979 if (unlikely(eflags))
980 ipath_rcv_hdrerr(dd, eflags, l, etail, rc);
981 else if (etype == RCVHQ_RCV_TYPE_NON_KD) {
Bryan O'Sullivanb1c1b6a2006-08-25 11:24:31 -0700982 ipath_ib_rcv(dd->verbs_dev, rc + 1, ebuf,
983 tlen);
Bryan O'Sullivanfba75202006-07-01 04:36:09 -0700984 if (dd->ipath_lli_counter)
985 dd->ipath_lli_counter--;
986
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -0800987 } else if (etype == RCVHQ_RCV_TYPE_EAGER) {
988 if (qp == IPATH_KD_QP &&
989 bthbytes[0] == ipath_layer_rcv_opcode &&
990 ebuf)
991 ipath_rcv_layer(dd, etail, tlen,
992 (struct ether_header *)hdr);
993 else
994 ipath_cdbg(PKT, "typ %x, opcode %x (eager, "
995 "qp=%x), len %x; ignored\n",
996 etype, bthbytes[0], qp, tlen);
997 }
998 else if (etype == RCVHQ_RCV_TYPE_EXPECTED)
999 ipath_dbg("Bug: Expected TID, opcode %x; ignored\n",
1000 be32_to_cpu(hdr->bth[0]) & 0xff);
Ralph Campbell3d37b9e2006-07-17 18:18:36 -07001001 else {
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001002 /*
1003 * error packet, type of error unknown.
1004 * Probably type 3, but we don't know, so don't
1005 * even try to print the opcode, etc.
1006 */
1007 ipath_dbg("Error Pkt, but no eflags! egrbuf %x, "
1008 "len %x\nhdrq@%lx;hdrq+%x rhf: %llx; "
1009 "hdr %llx %llx %llx %llx %llx\n",
1010 etail, tlen, (unsigned long) rc, l,
1011 (unsigned long long) rc[0],
1012 (unsigned long long) rc[1],
1013 (unsigned long long) rc[2],
1014 (unsigned long long) rc[3],
1015 (unsigned long long) rc[4],
1016 (unsigned long long) rc[5]);
1017 }
1018 l += rsize;
1019 if (l >= maxcnt)
1020 l = 0;
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001021 if (etype != RCVHQ_RCV_TYPE_EXPECTED)
Bryan O'Sullivanf5f99922006-07-01 04:36:05 -07001022 updegr = 1;
1023 /*
1024 * update head regs on last packet, and every 16 packets.
1025 * Reduce bus traffic, while still trying to prevent
1026 * rcvhdrq overflows, for when the queue is nearly full
1027 */
1028 if (l == hdrqtail || (i && !(i&0xf))) {
1029 u64 lval;
Bryan O'Sullivan57abad22006-07-01 04:36:05 -07001030 if (l == hdrqtail) /* PE-800 interrupt only on last */
Bryan O'Sullivanf5f99922006-07-01 04:36:05 -07001031 lval = dd->ipath_rhdrhead_intr_off | l;
1032 else
1033 lval = l;
1034 (void)ipath_write_ureg(dd, ur_rcvhdrhead, lval, 0);
1035 if (updegr) {
1036 (void)ipath_write_ureg(dd, ur_rcvegrindexhead,
1037 etail, 0);
1038 updegr = 0;
1039 }
1040 }
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001041 }
1042
Bryan O'Sullivan57abad22006-07-01 04:36:05 -07001043 if (!dd->ipath_rhdrhead_intr_off && !reloop) {
1044 /* HT-400 workaround; we can have a race clearing chip
1045 * interrupt with another interrupt about to be delivered,
1046 * and can clear it before it is delivered on the GPIO
1047 * workaround. By doing the extra check here for the
1048 * in-memory tail register updating while we were doing
1049 * earlier packets, we "almost" guarantee we have covered
1050 * that case.
1051 */
1052 u32 hqtail = (u32)le64_to_cpu(*dd->ipath_hdrqtailptr);
1053 if (hqtail != hdrqtail) {
1054 hdrqtail = hqtail;
1055 reloop = 1; /* loop 1 extra time at most */
1056 goto reloop;
1057 }
1058 }
1059
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001060 pkttot += i;
1061
1062 dd->ipath_port0head = l;
1063
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001064 if (pkttot > ipath_stats.sps_maxpkts_call)
1065 ipath_stats.sps_maxpkts_call = pkttot;
1066 ipath_stats.sps_port0pkts += pkttot;
1067 ipath_stats.sps_avgpkts_call =
1068 ipath_stats.sps_port0pkts / ++totcalls;
1069
1070done:
1071 clear_bit(0, &dd->ipath_rcv_pending);
1072 smp_mb__after_clear_bit();
1073
1074bail:;
1075}
1076
1077/**
1078 * ipath_update_pio_bufs - update shadow copy of the PIO availability map
1079 * @dd: the infinipath device
1080 *
1081 * called whenever our local copy indicates we have run out of send buffers
1082 * NOTE: This can be called from interrupt context by some code
1083 * and from non-interrupt context by ipath_getpiobuf().
1084 */
1085
1086static void ipath_update_pio_bufs(struct ipath_devdata *dd)
1087{
1088 unsigned long flags;
1089 int i;
1090 const unsigned piobregs = (unsigned)dd->ipath_pioavregs;
1091
1092 /* If the generation (check) bits have changed, then we update the
1093 * busy bit for the corresponding PIO buffer. This algorithm will
1094 * modify positions to the value they already have in some cases
1095 * (i.e., no change), but it's faster than changing only the bits
1096 * that have changed.
1097 *
1098 * We would like to do this atomicly, to avoid spinlocks in the
1099 * critical send path, but that's not really possible, given the
1100 * type of changes, and that this routine could be called on
1101 * multiple cpu's simultaneously, so we lock in this routine only,
1102 * to avoid conflicting updates; all we change is the shadow, and
1103 * it's a single 64 bit memory location, so by definition the update
1104 * is atomic in terms of what other cpu's can see in testing the
1105 * bits. The spin_lock overhead isn't too bad, since it only
1106 * happens when all buffers are in use, so only cpu overhead, not
1107 * latency or bandwidth is affected.
1108 */
1109#define _IPATH_ALL_CHECKBITS 0x5555555555555555ULL
1110 if (!dd->ipath_pioavailregs_dma) {
1111 ipath_dbg("Update shadow pioavail, but regs_dma NULL!\n");
1112 return;
1113 }
1114 if (ipath_debug & __IPATH_VERBDBG) {
1115 /* only if packet debug and verbose */
1116 volatile __le64 *dma = dd->ipath_pioavailregs_dma;
1117 unsigned long *shadow = dd->ipath_pioavailshadow;
1118
1119 ipath_cdbg(PKT, "Refill avail, dma0=%llx shad0=%lx, "
1120 "d1=%llx s1=%lx, d2=%llx s2=%lx, d3=%llx "
1121 "s3=%lx\n",
1122 (unsigned long long) le64_to_cpu(dma[0]),
1123 shadow[0],
1124 (unsigned long long) le64_to_cpu(dma[1]),
1125 shadow[1],
1126 (unsigned long long) le64_to_cpu(dma[2]),
1127 shadow[2],
1128 (unsigned long long) le64_to_cpu(dma[3]),
1129 shadow[3]);
1130 if (piobregs > 4)
1131 ipath_cdbg(
1132 PKT, "2nd group, dma4=%llx shad4=%lx, "
1133 "d5=%llx s5=%lx, d6=%llx s6=%lx, "
1134 "d7=%llx s7=%lx\n",
1135 (unsigned long long) le64_to_cpu(dma[4]),
1136 shadow[4],
1137 (unsigned long long) le64_to_cpu(dma[5]),
1138 shadow[5],
1139 (unsigned long long) le64_to_cpu(dma[6]),
1140 shadow[6],
1141 (unsigned long long) le64_to_cpu(dma[7]),
1142 shadow[7]);
1143 }
1144 spin_lock_irqsave(&ipath_pioavail_lock, flags);
1145 for (i = 0; i < piobregs; i++) {
1146 u64 pchbusy, pchg, piov, pnew;
1147 /*
1148 * Chip Errata: bug 6641; even and odd qwords>3 are swapped
1149 */
1150 if (i > 3) {
1151 if (i & 1)
1152 piov = le64_to_cpu(
1153 dd->ipath_pioavailregs_dma[i - 1]);
1154 else
1155 piov = le64_to_cpu(
1156 dd->ipath_pioavailregs_dma[i + 1]);
1157 } else
1158 piov = le64_to_cpu(dd->ipath_pioavailregs_dma[i]);
1159 pchg = _IPATH_ALL_CHECKBITS &
1160 ~(dd->ipath_pioavailshadow[i] ^ piov);
1161 pchbusy = pchg << INFINIPATH_SENDPIOAVAIL_BUSY_SHIFT;
1162 if (pchg && (pchbusy & dd->ipath_pioavailshadow[i])) {
1163 pnew = dd->ipath_pioavailshadow[i] & ~pchbusy;
1164 pnew |= piov & pchbusy;
1165 dd->ipath_pioavailshadow[i] = pnew;
1166 }
1167 }
1168 spin_unlock_irqrestore(&ipath_pioavail_lock, flags);
1169}
1170
1171/**
1172 * ipath_setrcvhdrsize - set the receive header size
1173 * @dd: the infinipath device
1174 * @rhdrsize: the receive header size
1175 *
1176 * called from user init code, and also layered driver init
1177 */
1178int ipath_setrcvhdrsize(struct ipath_devdata *dd, unsigned rhdrsize)
1179{
1180 int ret = 0;
1181
1182 if (dd->ipath_flags & IPATH_RCVHDRSZ_SET) {
1183 if (dd->ipath_rcvhdrsize != rhdrsize) {
1184 dev_info(&dd->pcidev->dev,
1185 "Error: can't set protocol header "
1186 "size %u, already %u\n",
1187 rhdrsize, dd->ipath_rcvhdrsize);
1188 ret = -EAGAIN;
1189 } else
1190 ipath_cdbg(VERBOSE, "Reuse same protocol header "
1191 "size %u\n", dd->ipath_rcvhdrsize);
1192 } else if (rhdrsize > (dd->ipath_rcvhdrentsize -
1193 (sizeof(u64) / sizeof(u32)))) {
1194 ipath_dbg("Error: can't set protocol header size %u "
1195 "(> max %u)\n", rhdrsize,
1196 dd->ipath_rcvhdrentsize -
1197 (u32) (sizeof(u64) / sizeof(u32)));
1198 ret = -EOVERFLOW;
1199 } else {
1200 dd->ipath_flags |= IPATH_RCVHDRSZ_SET;
1201 dd->ipath_rcvhdrsize = rhdrsize;
1202 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvhdrsize,
1203 dd->ipath_rcvhdrsize);
1204 ipath_cdbg(VERBOSE, "Set protocol header size to %u\n",
1205 dd->ipath_rcvhdrsize);
1206 }
1207 return ret;
1208}
1209
1210/**
1211 * ipath_getpiobuf - find an available pio buffer
1212 * @dd: the infinipath device
1213 * @pbufnum: the buffer number is placed here
1214 *
1215 * do appropriate marking as busy, etc.
1216 * returns buffer number if one found (>=0), negative number is error.
1217 * Used by ipath_sma_send_pkt and ipath_layer_send
1218 */
1219u32 __iomem *ipath_getpiobuf(struct ipath_devdata *dd, u32 * pbufnum)
1220{
1221 int i, j, starti, updated = 0;
1222 unsigned piobcnt, iter;
1223 unsigned long flags;
1224 unsigned long *shadow = dd->ipath_pioavailshadow;
1225 u32 __iomem *buf;
1226
1227 piobcnt = (unsigned)(dd->ipath_piobcnt2k
1228 + dd->ipath_piobcnt4k);
1229 starti = dd->ipath_lastport_piobuf;
1230 iter = piobcnt - starti;
1231 if (dd->ipath_upd_pio_shadow) {
1232 /*
1233 * Minor optimization. If we had no buffers on last call,
1234 * start out by doing the update; continue and do scan even
1235 * if no buffers were updated, to be paranoid
1236 */
1237 ipath_update_pio_bufs(dd);
1238 /* we scanned here, don't do it at end of scan */
1239 updated = 1;
1240 i = starti;
1241 } else
1242 i = dd->ipath_lastpioindex;
1243
1244rescan:
1245 /*
1246 * while test_and_set_bit() is atomic, we do that and then the
1247 * change_bit(), and the pair is not. See if this is the cause
1248 * of the remaining armlaunch errors.
1249 */
1250 spin_lock_irqsave(&ipath_pioavail_lock, flags);
1251 for (j = 0; j < iter; j++, i++) {
1252 if (i >= piobcnt)
1253 i = starti;
1254 /*
1255 * To avoid bus lock overhead, we first find a candidate
1256 * buffer, then do the test and set, and continue if that
1257 * fails.
1258 */
1259 if (test_bit((2 * i) + 1, shadow) ||
1260 test_and_set_bit((2 * i) + 1, shadow))
1261 continue;
1262 /* flip generation bit */
1263 change_bit(2 * i, shadow);
1264 break;
1265 }
1266 spin_unlock_irqrestore(&ipath_pioavail_lock, flags);
1267
1268 if (j == iter) {
1269 volatile __le64 *dma = dd->ipath_pioavailregs_dma;
1270
1271 /*
1272 * first time through; shadow exhausted, but may be real
1273 * buffers available, so go see; if any updated, rescan
1274 * (once)
1275 */
1276 if (!updated) {
1277 ipath_update_pio_bufs(dd);
1278 updated = 1;
1279 i = starti;
1280 goto rescan;
1281 }
1282 dd->ipath_upd_pio_shadow = 1;
1283 /*
1284 * not atomic, but if we lose one once in a while, that's OK
1285 */
1286 ipath_stats.sps_nopiobufs++;
1287 if (!(++dd->ipath_consec_nopiobuf % 100000)) {
1288 ipath_dbg(
1289 "%u pio sends with no bufavail; dmacopy: "
1290 "%llx %llx %llx %llx; shadow: "
1291 "%lx %lx %lx %lx\n",
1292 dd->ipath_consec_nopiobuf,
1293 (unsigned long long) le64_to_cpu(dma[0]),
1294 (unsigned long long) le64_to_cpu(dma[1]),
1295 (unsigned long long) le64_to_cpu(dma[2]),
1296 (unsigned long long) le64_to_cpu(dma[3]),
1297 shadow[0], shadow[1], shadow[2],
1298 shadow[3]);
1299 /*
1300 * 4 buffers per byte, 4 registers above, cover rest
1301 * below
1302 */
1303 if ((dd->ipath_piobcnt2k + dd->ipath_piobcnt4k) >
1304 (sizeof(shadow[0]) * 4 * 4))
1305 ipath_dbg("2nd group: dmacopy: %llx %llx "
1306 "%llx %llx; shadow: %lx %lx "
1307 "%lx %lx\n",
1308 (unsigned long long)
1309 le64_to_cpu(dma[4]),
1310 (unsigned long long)
1311 le64_to_cpu(dma[5]),
1312 (unsigned long long)
1313 le64_to_cpu(dma[6]),
1314 (unsigned long long)
1315 le64_to_cpu(dma[7]),
1316 shadow[4], shadow[5],
1317 shadow[6], shadow[7]);
1318 }
1319 buf = NULL;
1320 goto bail;
1321 }
1322
1323 if (updated)
1324 /*
1325 * ran out of bufs, now some (at least this one we just
1326 * got) are now available, so tell the layered driver.
1327 */
1328 __ipath_layer_intr(dd, IPATH_LAYER_INT_SEND_CONTINUE);
1329
1330 /*
1331 * set next starting place. Since it's just an optimization,
1332 * it doesn't matter who wins on this, so no locking
1333 */
1334 dd->ipath_lastpioindex = i + 1;
1335 if (dd->ipath_upd_pio_shadow)
1336 dd->ipath_upd_pio_shadow = 0;
1337 if (dd->ipath_consec_nopiobuf)
1338 dd->ipath_consec_nopiobuf = 0;
1339 if (i < dd->ipath_piobcnt2k)
1340 buf = (u32 __iomem *) (dd->ipath_pio2kbase +
1341 i * dd->ipath_palign);
1342 else
1343 buf = (u32 __iomem *)
1344 (dd->ipath_pio4kbase +
1345 (i - dd->ipath_piobcnt2k) * dd->ipath_4kalign);
1346 ipath_cdbg(VERBOSE, "Return piobuf%u %uk @ %p\n",
1347 i, (i < dd->ipath_piobcnt2k) ? 2 : 4, buf);
1348 if (pbufnum)
1349 *pbufnum = i;
1350
1351bail:
1352 return buf;
1353}
1354
1355/**
1356 * ipath_create_rcvhdrq - create a receive header queue
1357 * @dd: the infinipath device
1358 * @pd: the port data
1359 *
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001360 * this must be contiguous memory (from an i/o perspective), and must be
1361 * DMA'able (which means for some systems, it will go through an IOMMU,
1362 * or be forced into a low address range).
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001363 */
1364int ipath_create_rcvhdrq(struct ipath_devdata *dd,
1365 struct ipath_portdata *pd)
1366{
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001367 int ret = 0;
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001368
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001369 if (!pd->port_rcvhdrq) {
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001370 dma_addr_t phys_hdrqtail;
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001371 gfp_t gfp_flags = GFP_USER | __GFP_COMP;
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001372 int amt = ALIGN(dd->ipath_rcvhdrcnt * dd->ipath_rcvhdrentsize *
1373 sizeof(u32), PAGE_SIZE);
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001374
1375 pd->port_rcvhdrq = dma_alloc_coherent(
1376 &dd->pcidev->dev, amt, &pd->port_rcvhdrq_phys,
1377 gfp_flags);
1378
1379 if (!pd->port_rcvhdrq) {
1380 ipath_dev_err(dd, "attempt to allocate %d bytes "
1381 "for port %u rcvhdrq failed\n",
1382 amt, pd->port_port);
1383 ret = -ENOMEM;
1384 goto bail;
1385 }
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001386 pd->port_rcvhdrtail_kvaddr = dma_alloc_coherent(
1387 &dd->pcidev->dev, PAGE_SIZE, &phys_hdrqtail, GFP_KERNEL);
1388 if (!pd->port_rcvhdrtail_kvaddr) {
1389 ipath_dev_err(dd, "attempt to allocate 1 page "
1390 "for port %u rcvhdrqtailaddr failed\n",
1391 pd->port_port);
1392 ret = -ENOMEM;
1393 goto bail;
1394 }
1395 pd->port_rcvhdrqtailaddr_phys = phys_hdrqtail;
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001396
1397 pd->port_rcvhdrq_size = amt;
1398
1399 ipath_cdbg(VERBOSE, "%d pages at %p (phys %lx) size=%lu "
1400 "for port %u rcvhdr Q\n",
1401 amt >> PAGE_SHIFT, pd->port_rcvhdrq,
1402 (unsigned long) pd->port_rcvhdrq_phys,
1403 (unsigned long) pd->port_rcvhdrq_size,
1404 pd->port_port);
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001405
1406 ipath_cdbg(VERBOSE, "port %d hdrtailaddr, %llx physical\n",
1407 pd->port_port,
1408 (unsigned long long) phys_hdrqtail);
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001409 }
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001410 else
1411 ipath_cdbg(VERBOSE, "reuse port %d rcvhdrq @%p %llx phys; "
1412 "hdrtailaddr@%p %llx physical\n",
1413 pd->port_port, pd->port_rcvhdrq,
1414 pd->port_rcvhdrq_phys, pd->port_rcvhdrtail_kvaddr,
1415 (unsigned long long)pd->port_rcvhdrqtailaddr_phys);
1416
1417 /* clear for security and sanity on each use */
1418 memset(pd->port_rcvhdrq, 0, pd->port_rcvhdrq_size);
1419 memset((void *)pd->port_rcvhdrtail_kvaddr, 0, PAGE_SIZE);
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001420
1421 /*
1422 * tell chip each time we init it, even if we are re-using previous
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001423 * memory (we zero the register at process close)
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001424 */
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001425 ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdrtailaddr,
1426 pd->port_port, pd->port_rcvhdrqtailaddr_phys);
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001427 ipath_write_kreg_port(dd, dd->ipath_kregs->kr_rcvhdraddr,
1428 pd->port_port, pd->port_rcvhdrq_phys);
1429
1430 ret = 0;
1431bail:
1432 return ret;
1433}
1434
1435int ipath_waitfor_complete(struct ipath_devdata *dd, ipath_kreg reg_id,
1436 u64 bits_to_wait_for, u64 * valp)
1437{
1438 unsigned long timeout;
1439 u64 lastval, val;
1440 int ret;
1441
1442 lastval = ipath_read_kreg64(dd, reg_id);
1443 /* wait a ridiculously long time */
1444 timeout = jiffies + msecs_to_jiffies(5);
1445 do {
1446 val = ipath_read_kreg64(dd, reg_id);
1447 /* set so they have something, even on failures. */
1448 *valp = val;
1449 if ((val & bits_to_wait_for) == bits_to_wait_for) {
1450 ret = 0;
1451 break;
1452 }
1453 if (val != lastval)
1454 ipath_cdbg(VERBOSE, "Changed from %llx to %llx, "
1455 "waiting for %llx bits\n",
1456 (unsigned long long) lastval,
1457 (unsigned long long) val,
1458 (unsigned long long) bits_to_wait_for);
1459 cond_resched();
1460 if (time_after(jiffies, timeout)) {
1461 ipath_dbg("Didn't get bits %llx in register 0x%x, "
1462 "got %llx\n",
1463 (unsigned long long) bits_to_wait_for,
1464 reg_id, (unsigned long long) *valp);
1465 ret = -ENODEV;
1466 break;
1467 }
1468 } while (1);
1469
1470 return ret;
1471}
1472
1473/**
1474 * ipath_waitfor_mdio_cmdready - wait for last command to complete
1475 * @dd: the infinipath device
1476 *
1477 * Like ipath_waitfor_complete(), but we wait for the CMDVALID bit to go
1478 * away indicating the last command has completed. It doesn't return data
1479 */
1480int ipath_waitfor_mdio_cmdready(struct ipath_devdata *dd)
1481{
1482 unsigned long timeout;
1483 u64 val;
1484 int ret;
1485
1486 /* wait a ridiculously long time */
1487 timeout = jiffies + msecs_to_jiffies(5);
1488 do {
1489 val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_mdio);
1490 if (!(val & IPATH_MDIO_CMDVALID)) {
1491 ret = 0;
1492 break;
1493 }
1494 cond_resched();
1495 if (time_after(jiffies, timeout)) {
1496 ipath_dbg("CMDVALID stuck in mdio reg? (%llx)\n",
1497 (unsigned long long) val);
1498 ret = -ENODEV;
1499 break;
1500 }
1501 } while (1);
1502
1503 return ret;
1504}
1505
1506void ipath_set_ib_lstate(struct ipath_devdata *dd, int which)
1507{
1508 static const char *what[4] = {
1509 [0] = "DOWN",
1510 [INFINIPATH_IBCC_LINKCMD_INIT] = "INIT",
1511 [INFINIPATH_IBCC_LINKCMD_ARMED] = "ARMED",
1512 [INFINIPATH_IBCC_LINKCMD_ACTIVE] = "ACTIVE"
1513 };
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001514 int linkcmd = (which >> INFINIPATH_IBCC_LINKCMD_SHIFT) &
1515 INFINIPATH_IBCC_LINKCMD_MASK;
1516
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001517 ipath_cdbg(SMA, "Trying to move unit %u to %s, current ltstate "
1518 "is %s\n", dd->ipath_unit,
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001519 what[linkcmd],
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001520 ipath_ibcstatus_str[
1521 (ipath_read_kreg64
1522 (dd, dd->ipath_kregs->kr_ibcstatus) >>
1523 INFINIPATH_IBCS_LINKTRAININGSTATE_SHIFT) &
1524 INFINIPATH_IBCS_LINKTRAININGSTATE_MASK]);
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001525 /* flush all queued sends when going to DOWN or INIT, to be sure that
1526 * they don't block SMA and other MAD packets */
1527 if (!linkcmd || linkcmd == INFINIPATH_IBCC_LINKCMD_INIT) {
1528 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
1529 INFINIPATH_S_ABORT);
1530 ipath_disarm_piobufs(dd, dd->ipath_lastport_piobuf,
1531 (unsigned)(dd->ipath_piobcnt2k +
1532 dd->ipath_piobcnt4k) -
1533 dd->ipath_lastport_piobuf);
1534 }
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001535
1536 ipath_write_kreg(dd, dd->ipath_kregs->kr_ibcctrl,
1537 dd->ipath_ibcctrl | which);
1538}
1539
1540/**
1541 * ipath_read_kreg64_port - read a device's per-port 64-bit kernel register
1542 * @dd: the infinipath device
1543 * @regno: the register number to read
1544 * @port: the port containing the register
1545 *
1546 * Registers that vary with the chip implementation constants (port)
1547 * use this routine.
1548 */
1549u64 ipath_read_kreg64_port(const struct ipath_devdata *dd, ipath_kreg regno,
1550 unsigned port)
1551{
1552 u16 where;
1553
1554 if (port < dd->ipath_portcnt &&
1555 (regno == dd->ipath_kregs->kr_rcvhdraddr ||
1556 regno == dd->ipath_kregs->kr_rcvhdrtailaddr))
1557 where = regno + port;
1558 else
1559 where = -1;
1560
1561 return ipath_read_kreg64(dd, where);
1562}
1563
1564/**
1565 * ipath_write_kreg_port - write a device's per-port 64-bit kernel register
1566 * @dd: the infinipath device
1567 * @regno: the register number to write
1568 * @port: the port containing the register
1569 * @value: the value to write
1570 *
1571 * Registers that vary with the chip implementation constants (port)
1572 * use this routine.
1573 */
1574void ipath_write_kreg_port(const struct ipath_devdata *dd, ipath_kreg regno,
1575 unsigned port, u64 value)
1576{
1577 u16 where;
1578
1579 if (port < dd->ipath_portcnt &&
1580 (regno == dd->ipath_kregs->kr_rcvhdraddr ||
1581 regno == dd->ipath_kregs->kr_rcvhdrtailaddr))
1582 where = regno + port;
1583 else
1584 where = -1;
1585
1586 ipath_write_kreg(dd, where, value);
1587}
1588
1589/**
1590 * ipath_shutdown_device - shut down a device
1591 * @dd: the infinipath device
1592 *
1593 * This is called to make the device quiet when we are about to
1594 * unload the driver, and also when the device is administratively
1595 * disabled. It does not free any data structures.
1596 * Everything it does has to be setup again by ipath_init_chip(dd,1)
1597 */
1598void ipath_shutdown_device(struct ipath_devdata *dd)
1599{
1600 u64 val;
1601
1602 ipath_dbg("Shutting down the device\n");
1603
1604 dd->ipath_flags |= IPATH_LINKUNK;
1605 dd->ipath_flags &= ~(IPATH_INITTED | IPATH_LINKDOWN |
1606 IPATH_LINKINIT | IPATH_LINKARMED |
1607 IPATH_LINKACTIVE);
1608 *dd->ipath_statusp &= ~(IPATH_STATUS_IB_CONF |
1609 IPATH_STATUS_IB_READY);
1610
1611 /* mask interrupts, but not errors */
1612 ipath_write_kreg(dd, dd->ipath_kregs->kr_intmask, 0ULL);
1613
1614 dd->ipath_rcvctrl = 0;
1615 ipath_write_kreg(dd, dd->ipath_kregs->kr_rcvctrl,
1616 dd->ipath_rcvctrl);
1617
1618 /*
1619 * gracefully stop all sends allowing any in progress to trickle out
1620 * first.
1621 */
1622 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl, 0ULL);
1623 /* flush it */
1624 val = ipath_read_kreg64(dd, dd->ipath_kregs->kr_scratch);
1625 /*
1626 * enough for anything that's going to trickle out to have actually
1627 * done so.
1628 */
1629 udelay(5);
1630
1631 /*
1632 * abort any armed or launched PIO buffers that didn't go. (self
1633 * clearing). Will cause any packet currently being transmitted to
1634 * go out with an EBP, and may also cause a short packet error on
1635 * the receiver.
1636 */
1637 ipath_write_kreg(dd, dd->ipath_kregs->kr_sendctrl,
1638 INFINIPATH_S_ABORT);
1639
1640 ipath_set_ib_lstate(dd, INFINIPATH_IBCC_LINKINITCMD_DISABLE <<
1641 INFINIPATH_IBCC_LINKINITCMD_SHIFT);
1642
1643 /*
1644 * we are shutting down, so tell the layered driver. We don't do
1645 * this on just a link state change, much like ethernet, a cable
1646 * unplug, etc. doesn't change driver state
1647 */
1648 ipath_layer_intr(dd, IPATH_LAYER_INT_IF_DOWN);
1649
1650 /* disable IBC */
1651 dd->ipath_control &= ~INFINIPATH_C_LINKENABLE;
1652 ipath_write_kreg(dd, dd->ipath_kregs->kr_control,
Bryan O'Sullivana40f55f2006-07-01 04:36:00 -07001653 dd->ipath_control | INFINIPATH_C_FREEZEMODE);
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001654
1655 /*
1656 * clear SerdesEnable and turn the leds off; do this here because
1657 * we are unloading, so don't count on interrupts to move along
1658 * Turn the LEDs off explictly for the same reason.
1659 */
1660 dd->ipath_f_quiet_serdes(dd);
1661 dd->ipath_f_setextled(dd, 0, 0);
1662
1663 if (dd->ipath_stats_timer_active) {
1664 del_timer_sync(&dd->ipath_stats_timer);
1665 dd->ipath_stats_timer_active = 0;
1666 }
1667
1668 /*
1669 * clear all interrupts and errors, so that the next time the driver
1670 * is loaded or device is enabled, we know that whatever is set
1671 * happened while we were unloaded
1672 */
1673 ipath_write_kreg(dd, dd->ipath_kregs->kr_hwerrclear,
1674 ~0ULL & ~INFINIPATH_HWE_MEMBISTFAILED);
1675 ipath_write_kreg(dd, dd->ipath_kregs->kr_errorclear, -1LL);
1676 ipath_write_kreg(dd, dd->ipath_kregs->kr_intclear, -1LL);
1677}
1678
1679/**
1680 * ipath_free_pddata - free a port's allocated data
1681 * @dd: the infinipath device
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001682 * @pd: the portdata structure
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001683 *
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001684 * free up any allocated data for a port
1685 * This should not touch anything that would affect a simultaneous
1686 * re-allocation of port data, because it is called after ipath_mutex
1687 * is released (and can be called from reinit as well).
1688 * It should never change any chip state, or global driver state.
1689 * (The only exception to global state is freeing the port0 port0_skbs.)
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001690 */
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001691void ipath_free_pddata(struct ipath_devdata *dd, struct ipath_portdata *pd)
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001692{
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001693 if (!pd)
1694 return;
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001695
1696 if (pd->port_rcvhdrq) {
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001697 ipath_cdbg(VERBOSE, "free closed port %d rcvhdrq @ %p "
1698 "(size=%lu)\n", pd->port_port, pd->port_rcvhdrq,
1699 (unsigned long) pd->port_rcvhdrq_size);
1700 dma_free_coherent(&dd->pcidev->dev, pd->port_rcvhdrq_size,
1701 pd->port_rcvhdrq, pd->port_rcvhdrq_phys);
1702 pd->port_rcvhdrq = NULL;
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001703 if (pd->port_rcvhdrtail_kvaddr) {
1704 dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
1705 (void *)pd->port_rcvhdrtail_kvaddr,
1706 pd->port_rcvhdrqtailaddr_phys);
1707 pd->port_rcvhdrtail_kvaddr = NULL;
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001708 }
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001709 }
1710 if (pd->port_port && pd->port_rcvegrbuf) {
1711 unsigned e;
1712
1713 for (e = 0; e < pd->port_rcvegrbuf_chunks; e++) {
1714 void *base = pd->port_rcvegrbuf[e];
1715 size_t size = pd->port_rcvegrbuf_size;
1716
1717 ipath_cdbg(VERBOSE, "egrbuf free(%p, %lu), "
1718 "chunk %u/%u\n", base,
1719 (unsigned long) size,
1720 e, pd->port_rcvegrbuf_chunks);
1721 dma_free_coherent(&dd->pcidev->dev, size,
1722 base, pd->port_rcvegrbuf_phys[e]);
1723 }
1724 vfree(pd->port_rcvegrbuf);
1725 pd->port_rcvegrbuf = NULL;
1726 vfree(pd->port_rcvegrbuf_phys);
1727 pd->port_rcvegrbuf_phys = NULL;
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001728 pd->port_rcvegrbuf_chunks = 0;
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001729 } else if (pd->port_port == 0 && dd->ipath_port0_skbs) {
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001730 unsigned e;
1731 struct sk_buff **skbs = dd->ipath_port0_skbs;
1732
1733 dd->ipath_port0_skbs = NULL;
1734 ipath_cdbg(VERBOSE, "free closed port %d ipath_port0_skbs "
1735 "@ %p\n", pd->port_port, skbs);
1736 for (e = 0; e < dd->ipath_rcvegrcnt; e++)
1737 if (skbs[e])
1738 dev_kfree_skb(skbs[e]);
1739 vfree(skbs);
1740 }
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001741 kfree(pd->port_tid_pg_list);
1742 kfree(pd);
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001743}
1744
Roland Dreierac2ae4c2006-04-19 11:40:12 -07001745static int __init infinipath_init(void)
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001746{
1747 int ret;
1748
1749 ipath_dbg(KERN_INFO DRIVER_LOAD_MSG "%s", ipath_core_version);
1750
1751 /*
1752 * These must be called before the driver is registered with
1753 * the PCI subsystem.
1754 */
1755 idr_init(&unit_table);
1756 if (!idr_pre_get(&unit_table, GFP_KERNEL)) {
1757 ret = -ENOMEM;
1758 goto bail;
1759 }
1760
1761 ret = pci_register_driver(&ipath_driver);
1762 if (ret < 0) {
1763 printk(KERN_ERR IPATH_DRV_NAME
1764 ": Unable to register driver: error %d\n", -ret);
1765 goto bail_unit;
1766 }
1767
1768 ret = ipath_driver_create_group(&ipath_driver.driver);
1769 if (ret < 0) {
1770 printk(KERN_ERR IPATH_DRV_NAME ": Unable to create driver "
1771 "sysfs entries: error %d\n", -ret);
1772 goto bail_pci;
1773 }
1774
1775 ret = ipath_init_ipathfs();
1776 if (ret < 0) {
1777 printk(KERN_ERR IPATH_DRV_NAME ": Unable to create "
1778 "ipathfs: error %d\n", -ret);
1779 goto bail_group;
1780 }
1781
1782 goto bail;
1783
1784bail_group:
1785 ipath_driver_remove_group(&ipath_driver.driver);
1786
1787bail_pci:
1788 pci_unregister_driver(&ipath_driver);
1789
1790bail_unit:
1791 idr_destroy(&unit_table);
1792
1793bail:
1794 return ret;
1795}
1796
1797static void cleanup_device(struct ipath_devdata *dd)
1798{
1799 int port;
1800
1801 ipath_shutdown_device(dd);
1802
1803 if (*dd->ipath_statusp & IPATH_STATUS_CHIP_PRESENT) {
1804 /* can't do anything more with chip; needs re-init */
1805 *dd->ipath_statusp &= ~IPATH_STATUS_CHIP_PRESENT;
1806 if (dd->ipath_kregbase) {
1807 /*
1808 * if we haven't already cleaned up before these are
1809 * to ensure any register reads/writes "fail" until
1810 * re-init
1811 */
1812 dd->ipath_kregbase = NULL;
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001813 dd->ipath_uregbase = 0;
1814 dd->ipath_sregbase = 0;
1815 dd->ipath_cregbase = 0;
1816 dd->ipath_kregsize = 0;
1817 }
1818 ipath_disable_wc(dd);
1819 }
1820
1821 if (dd->ipath_pioavailregs_dma) {
1822 dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
1823 (void *) dd->ipath_pioavailregs_dma,
1824 dd->ipath_pioavailregs_phys);
1825 dd->ipath_pioavailregs_dma = NULL;
1826 }
Bryan O'Sullivan35783ec2006-07-01 04:36:15 -07001827 if (dd->ipath_dummy_hdrq) {
1828 dma_free_coherent(&dd->pcidev->dev,
1829 dd->ipath_pd[0]->port_rcvhdrq_size,
1830 dd->ipath_dummy_hdrq, dd->ipath_dummy_hdrq_phys);
1831 dd->ipath_dummy_hdrq = NULL;
1832 }
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001833
1834 if (dd->ipath_pageshadow) {
1835 struct page **tmpp = dd->ipath_pageshadow;
1836 int i, cnt = 0;
1837
1838 ipath_cdbg(VERBOSE, "Unlocking any expTID pages still "
1839 "locked\n");
1840 for (port = 0; port < dd->ipath_cfgports; port++) {
1841 int port_tidbase = port * dd->ipath_rcvtidcnt;
1842 int maxtid = port_tidbase + dd->ipath_rcvtidcnt;
1843 for (i = port_tidbase; i < maxtid; i++) {
1844 if (!tmpp[i])
1845 continue;
1846 ipath_release_user_pages(&tmpp[i], 1);
1847 tmpp[i] = NULL;
1848 cnt++;
1849 }
1850 }
1851 if (cnt) {
1852 ipath_stats.sps_pageunlocks += cnt;
1853 ipath_cdbg(VERBOSE, "There were still %u expTID "
1854 "entries locked\n", cnt);
1855 }
1856 if (ipath_stats.sps_pagelocks ||
1857 ipath_stats.sps_pageunlocks)
1858 ipath_cdbg(VERBOSE, "%llu pages locked, %llu "
1859 "unlocked via ipath_m{un}lock\n",
1860 (unsigned long long)
1861 ipath_stats.sps_pagelocks,
1862 (unsigned long long)
1863 ipath_stats.sps_pageunlocks);
1864
1865 ipath_cdbg(VERBOSE, "Free shadow page tid array at %p\n",
1866 dd->ipath_pageshadow);
1867 vfree(dd->ipath_pageshadow);
1868 dd->ipath_pageshadow = NULL;
1869 }
1870
1871 /*
1872 * free any resources still in use (usually just kernel ports)
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001873 * at unload; we do for portcnt, not cfgports, because cfgports
1874 * could have changed while we were loaded.
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001875 */
Bryan O'Sullivanf37bda92006-07-01 04:36:03 -07001876 for (port = 0; port < dd->ipath_portcnt; port++) {
1877 struct ipath_portdata *pd = dd->ipath_pd[port];
1878 dd->ipath_pd[port] = NULL;
1879 ipath_free_pddata(dd, pd);
1880 }
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001881 kfree(dd->ipath_pd);
1882 /*
1883 * debuggability, in case some cleanup path tries to use it
1884 * after this
1885 */
1886 dd->ipath_pd = NULL;
1887}
1888
1889static void __exit infinipath_cleanup(void)
1890{
1891 struct ipath_devdata *dd, *tmp;
1892 unsigned long flags;
1893
1894 ipath_exit_ipathfs();
1895
1896 ipath_driver_remove_group(&ipath_driver.driver);
1897
1898 spin_lock_irqsave(&ipath_devs_lock, flags);
1899
1900 /*
1901 * turn off rcv, send, and interrupts for all ports, all drivers
1902 * should also hard reset the chip here?
1903 * free up port 0 (kernel) rcvhdr, egr bufs, and eventually tid bufs
1904 * for all versions of the driver, if they were allocated
1905 */
1906 list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
1907 spin_unlock_irqrestore(&ipath_devs_lock, flags);
1908
1909 if (dd->ipath_kregbase)
1910 cleanup_device(dd);
1911
1912 if (dd->pcidev) {
1913 if (dd->pcidev->irq) {
1914 ipath_cdbg(VERBOSE,
1915 "unit %u free_irq of irq %x\n",
1916 dd->ipath_unit, dd->pcidev->irq);
1917 free_irq(dd->pcidev->irq, dd);
1918 } else
1919 ipath_dbg("irq is 0, not doing free_irq "
1920 "for unit %u\n", dd->ipath_unit);
Bryan O'Sullivanb0ff7c22006-05-23 11:32:33 -07001921
1922 /*
1923 * we check for NULL here, because it's outside
1924 * the kregbase check, and we need to call it
1925 * after the free_irq. Thus it's possible that
1926 * the function pointers were never initialized.
1927 */
1928 if (dd->ipath_f_cleanup)
1929 /* clean up chip-specific stuff */
1930 dd->ipath_f_cleanup(dd);
1931
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001932 dd->pcidev = NULL;
1933 }
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001934 spin_lock_irqsave(&ipath_devs_lock, flags);
1935 }
1936
1937 spin_unlock_irqrestore(&ipath_devs_lock, flags);
1938
1939 ipath_cdbg(VERBOSE, "Unregistering pci driver\n");
1940 pci_unregister_driver(&ipath_driver);
1941
1942 idr_destroy(&unit_table);
1943}
1944
1945/**
1946 * ipath_reset_device - reset the chip if possible
1947 * @unit: the device to reset
1948 *
1949 * Whether or not reset is successful, we attempt to re-initialize the chip
1950 * (that is, much like a driver unload/reload). We clear the INITTED flag
1951 * so that the various entry points will fail until we reinitialize. For
1952 * now, we only allow this if no user ports are open that use chip resources
1953 */
1954int ipath_reset_device(int unit)
1955{
1956 int ret, i;
1957 struct ipath_devdata *dd = ipath_lookup(unit);
1958
1959 if (!dd) {
1960 ret = -ENODEV;
1961 goto bail;
1962 }
1963
1964 dev_info(&dd->pcidev->dev, "Reset on unit %u requested\n", unit);
1965
1966 if (!dd->ipath_kregbase || !(dd->ipath_flags & IPATH_PRESENT)) {
1967 dev_info(&dd->pcidev->dev, "Invalid unit number %u or "
1968 "not initialized or not present\n", unit);
1969 ret = -ENXIO;
1970 goto bail;
1971 }
1972
1973 if (dd->ipath_pd)
Bryan O'Sullivan23e86a42006-04-24 14:22:59 -07001974 for (i = 1; i < dd->ipath_cfgports; i++) {
Bryan O'Sullivan7bb206e2006-03-29 15:23:24 -08001975 if (dd->ipath_pd[i] && dd->ipath_pd[i]->port_cnt) {
1976 ipath_dbg("unit %u port %d is in use "
1977 "(PID %u cmd %s), can't reset\n",
1978 unit, i,
1979 dd->ipath_pd[i]->port_pid,
1980 dd->ipath_pd[i]->port_comm);
1981 ret = -EBUSY;
1982 goto bail;
1983 }
1984 }
1985
1986 dd->ipath_flags &= ~IPATH_INITTED;
1987 ret = dd->ipath_f_reset(dd);
1988 if (ret != 1)
1989 ipath_dbg("reset was not successful\n");
1990 ipath_dbg("Trying to reinitialize unit %u after reset attempt\n",
1991 unit);
1992 ret = ipath_init_chip(dd, 1);
1993 if (ret)
1994 ipath_dev_err(dd, "Reinitialize unit %u after "
1995 "reset failed with %d\n", unit, ret);
1996 else
1997 dev_info(&dd->pcidev->dev, "Reinitialized unit %u after "
1998 "resetting\n", unit);
1999
2000bail:
2001 return ret;
2002}
2003
2004module_init(infinipath_init);
2005module_exit(infinipath_cleanup);