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