cxgb_main.c revision 1.8.8.1 1 /**************************************************************************
2
3 Copyright (c) 2007, Chelsio Inc.
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8
9 1. Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
11
12 2. Neither the name of the Chelsio Corporation nor the names of its
13 contributors may be used to endorse or promote products derived from
14 this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27
28 ***************************************************************************/
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: cxgb_main.c,v 1.8.8.1 2021/03/22 02:01:01 thorpej Exp $");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/conf.h>
37 #include <sys/bus.h>
38 #include <sys/ioccom.h>
39 #include <sys/mbuf.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <sys/sysctl.h>
43 #include <sys/queue.h>
44
45 #include <net/bpf.h>
46 #include <net/if.h>
47 #include <net/if_arp.h>
48 #include <net/if_dl.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
51
52 #include <netinet/in_systm.h>
53 #include <netinet/in.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip.h>
56 #include <netinet/tcp.h>
57 #include <netinet/udp.h>
58 #include <netinet/if_inarp.h>
59
60 #include <dev/pci/pcireg.h>
61 #include <dev/pci/pcivar.h>
62
63 #ifdef CONFIG_DEFINED
64 #include <cxgb_include.h>
65 #else
66 #include <dev/pci/cxgb/cxgb_include.h>
67 #endif
68
69 #ifdef PRIV_SUPPORTED
70 #include <sys/priv.h>
71 #endif
72
73 #include <altq/altq_conf.h>
74
75 static int cxgb_setup_msix(adapter_t *, int);
76 static void cxgb_teardown_msix(adapter_t *);
77 static int cxgb_init(struct ifnet *);
78 static void cxgb_init_locked(struct port_info *);
79 static void cxgb_stop_locked(struct port_info *);
80 static void cxgb_set_rxmode(struct port_info *);
81 static int cxgb_ioctl(struct ifnet *, unsigned long, void *);
82 static void cxgb_start(struct ifnet *);
83 static void cxgb_stop(struct ifnet *, int);
84 static void cxgb_start_proc(struct work *, void *);
85 static int cxgb_media_change(struct ifnet *);
86 static void cxgb_media_status(struct ifnet *, struct ifmediareq *);
87 static int setup_sge_qsets(adapter_t *);
88 static int cxgb_async_intr(void *);
89 static void cxgb_ext_intr_handler(struct work *, void *);
90 static void cxgb_tick_handler(struct work *, void *);
91 static void cxgb_down_locked(struct adapter *sc);
92 static void cxgb_tick(void *);
93 static void setup_rss(adapter_t *sc);
94
95 /* Attachment glue for the PCI controller end of the device. Each port of
96 * the device is attached separately, as defined later.
97 */
98 static int cxgb_controller_match(device_t dev, cfdata_t match, void *context);
99 static void cxgb_controller_attach(device_t parent, device_t dev, void *context);
100 static int cxgb_controller_detach(device_t dev, int flags);
101 static void cxgb_free(struct adapter *);
102 static __inline void reg_block_dump(struct adapter *ap, uint8_t *buf, unsigned int start,
103 unsigned int end);
104 static void touch_bars(device_t dev);
105
106 #ifdef notyet
107 static int offload_close(struct toedev *tdev);
108 #endif
109
110
111 CFATTACH_DECL_NEW(cxgbc, sizeof(struct adapter), cxgb_controller_match, cxgb_controller_attach, cxgb_controller_detach, NULL);
112
113 /*
114 * Attachment glue for the ports. Attachment is done directly to the
115 * controller device.
116 */
117 static int cxgb_port_match(device_t dev, cfdata_t match, void *context);
118 static void cxgb_port_attach(device_t dev, device_t self, void *context);
119 static int cxgb_port_detach(device_t dev, int flags);
120
121 CFATTACH_DECL_NEW(cxgb, sizeof(struct port_device), cxgb_port_match, cxgb_port_attach, cxgb_port_detach, NULL);
122
123 #define SGE_MSIX_COUNT (SGE_QSETS + 1)
124
125 extern int collapse_mbufs;
126 #ifdef MSI_SUPPORTED
127 /*
128 * The driver uses the best interrupt scheme available on a platform in the
129 * order MSI-X, MSI, legacy pin interrupts. This parameter determines which
130 * of these schemes the driver may consider as follows:
131 *
132 * msi = 2: choose from among all three options
133 * msi = 1 : only consider MSI and pin interrupts
134 * msi = 0: force pin interrupts
135 */
136 static int msi_allowed = 2;
137 #endif
138
139 /*
140 * The driver uses an auto-queue algorithm by default.
141 * To disable it and force a single queue-set per port, use singleq = 1.
142 */
143 static int singleq = 1;
144
145 enum {
146 MAX_TXQ_ENTRIES = 16384,
147 MAX_CTRL_TXQ_ENTRIES = 1024,
148 MAX_RSPQ_ENTRIES = 16384,
149 MAX_RX_BUFFERS = 16384,
150 MAX_RX_JUMBO_BUFFERS = 16384,
151 MIN_TXQ_ENTRIES = 4,
152 MIN_CTRL_TXQ_ENTRIES = 4,
153 MIN_RSPQ_ENTRIES = 32,
154 MIN_FL_ENTRIES = 32,
155 MIN_FL_JUMBO_ENTRIES = 32
156 };
157
158 struct filter_info {
159 u32 sip;
160 u32 sip_mask;
161 u32 dip;
162 u16 sport;
163 u16 dport;
164 u32 vlan:12;
165 u32 vlan_prio:3;
166 u32 mac_hit:1;
167 u32 mac_idx:4;
168 u32 mac_vld:1;
169 u32 pkt_type:2;
170 u32 report_filter_id:1;
171 u32 pass:1;
172 u32 rss:1;
173 u32 qset:3;
174 u32 locked:1;
175 u32 valid:1;
176 };
177
178 enum { FILTER_NO_VLAN_PRI = 7 };
179
180 #define PORT_MASK ((1 << MAX_NPORTS) - 1)
181
182 /* Table for probing the cards. The desc field isn't actually used */
183 struct cxgb_ident {
184 uint16_t vendor;
185 uint16_t device;
186 int index;
187 const char *desc;
188 } cxgb_identifiers[] = {
189 {PCI_VENDOR_ID_CHELSIO, 0x0020, 0, "PE9000"},
190 {PCI_VENDOR_ID_CHELSIO, 0x0021, 1, "T302E"},
191 {PCI_VENDOR_ID_CHELSIO, 0x0022, 2, "T310E"},
192 {PCI_VENDOR_ID_CHELSIO, 0x0023, 3, "T320X"},
193 {PCI_VENDOR_ID_CHELSIO, 0x0024, 1, "T302X"},
194 {PCI_VENDOR_ID_CHELSIO, 0x0025, 3, "T320E"},
195 {PCI_VENDOR_ID_CHELSIO, 0x0026, 2, "T310X"},
196 {PCI_VENDOR_ID_CHELSIO, 0x0030, 2, "T3B10"},
197 {PCI_VENDOR_ID_CHELSIO, 0x0031, 3, "T3B20"},
198 {PCI_VENDOR_ID_CHELSIO, 0x0032, 1, "T3B02"},
199 {PCI_VENDOR_ID_CHELSIO, 0x0033, 4, "T3B04"},
200 {0, 0, 0, NULL}
201 };
202
203
204 static inline char
205 t3rev2char(struct adapter *adapter)
206 {
207 char rev = 'z';
208
209 switch(adapter->params.rev) {
210 case T3_REV_A:
211 rev = 'a';
212 break;
213 case T3_REV_B:
214 case T3_REV_B2:
215 rev = 'b';
216 break;
217 case T3_REV_C:
218 rev = 'c';
219 break;
220 }
221 return rev;
222 }
223
224 static struct cxgb_ident *cxgb_get_ident(struct pci_attach_args *pa)
225 {
226 struct cxgb_ident *id;
227 int vendorid, deviceid;
228
229 vendorid = PCI_VENDOR(pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ID_REG));
230 deviceid = PCI_PRODUCT(pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ID_REG));
231
232 for (id = cxgb_identifiers; id->desc != NULL; id++) {
233 if ((id->vendor == vendorid) &&
234 (id->device == deviceid)) {
235 return (id);
236 }
237 }
238 return (NULL);
239 }
240
241 static const struct adapter_info *cxgb_get_adapter_info(struct pci_attach_args *pa)
242 {
243 struct cxgb_ident *id;
244 const struct adapter_info *ai;
245
246 id = cxgb_get_ident(pa);
247 if (id == NULL)
248 return (NULL);
249
250 ai = t3_get_adapter_info(id->index);
251 return (ai);
252 }
253
254 static int cxgb_controller_match(device_t dev, cfdata_t match, void *context)
255 {
256 struct pci_attach_args *pa = context;
257 const struct adapter_info *ai;
258
259 ai = cxgb_get_adapter_info(pa);
260 if (ai == NULL)
261 return (0);
262
263 return (100); // we ARE the best driver for this card!!
264 }
265
266 #define FW_FNAME "t3fw%d%d%d"
267 #define TPEEPROM_NAME "t3%ctpe%d%d%d"
268 #define TPSRAM_NAME "t3%cps%d%d%d"
269
270 int cxgb_cfprint(void *aux, const char *info);
271 int cxgb_cfprint(void *aux, const char *info)
272 {
273 if (info)
274 {
275 printf("cxgb_cfprint(%p, \"%s\")\n", aux, info);
276 INT3;
277 }
278
279 return (QUIET);
280 }
281
282 void cxgb_make_task(void *context)
283 {
284 struct cxgb_task *w = (struct cxgb_task *)context;
285
286 // we can only use workqueue_create() once the system is up and running
287 workqueue_create(&w->wq, w->name, w->func, w->context, PRIBIO, IPL_NET, 0);
288 // printf("======>> create workqueue for %s %p\n", w->name, w->wq);
289 }
290
291 static void
292 cxgb_controller_attach(device_t parent, device_t dev, void *context)
293 {
294 device_t child;
295 const struct adapter_info *ai;
296 struct adapter *sc;
297 struct pci_attach_args *pa = context;
298 struct cxgb_attach_args cxgb_args;
299 int locs[2];
300 int i, error = 0;
301 uint32_t vers;
302 int port_qsets = 1;
303 int reg;
304 #ifdef MSI_SUPPORTED
305 int msi_needed;
306 #endif
307
308 sc = device_private(dev);
309 sc->dev = dev;
310 memcpy(&sc->pa, pa, sizeof(struct pci_attach_args));
311 sc->msi_count = 0;
312 ai = cxgb_get_adapter_info(pa);
313
314 /*
315 * XXX not really related but a recent addition
316 */
317 #ifdef MSI_SUPPORTED
318 /* find the PCIe link width and set max read request to 4KB*/
319 if (pci_find_extcap(dev, PCIY_EXPRESS, ®) == 0) {
320 uint16_t lnk, pectl;
321 lnk = pci_read_config(dev, reg + 0x12, 2);
322 sc->link_width = (lnk >> 4) & 0x3f;
323
324 pectl = pci_read_config(dev, reg + 0x8, 2);
325 pectl = (pectl & ~0x7000) | (5 << 12);
326 pci_write_config(dev, reg + 0x8, pectl, 2);
327 }
328
329 if (sc->link_width != 0 && sc->link_width <= 4 &&
330 (ai->nports0 + ai->nports1) <= 2) {
331 device_printf(sc->dev,
332 "PCIe x%d Link, expect reduced performance\n",
333 sc->link_width);
334 }
335 #endif
336
337 touch_bars(dev);
338
339 pci_enable_busmaster(dev);
340
341 /*
342 * Allocate the registers and make them available to the driver.
343 * The registers that we care about for NIC mode are in BAR 0
344 */
345 sc->regs_rid = PCI_MAPREG_START;
346 t3_os_pci_read_config_4(sc, PCI_MAPREG_START, ®);
347
348 // call bus_space_map
349 sc->bar0 = reg&0xFFFFF000;
350 bus_space_map(sc->pa.pa_memt, sc->bar0, 4096, 0, &sc->bar0_handle);
351
352 MTX_INIT(&sc->sge.reg_lock, sc->reglockbuf, NULL, MTX_DEF);
353 MTX_INIT(&sc->mdio_lock, sc->mdiolockbuf, NULL, MTX_DEF);
354 MTX_INIT(&sc->elmer_lock, sc->elmerlockbuf, NULL, MTX_DEF);
355
356 sc->bt = sc->pa.pa_memt;
357 sc->bh = sc->bar0_handle;
358 sc->mmio_len = 4096;
359
360 if (t3_prep_adapter(sc, ai, 1) < 0) {
361 printf("prep adapter failed\n");
362 error = ENODEV;
363 goto out;
364 }
365 /* Allocate the BAR for doing MSI-X. If it succeeds, try to allocate
366 * enough messages for the queue sets. If that fails, try falling
367 * back to MSI. If that fails, then try falling back to the legacy
368 * interrupt pin model.
369 */
370 #ifdef MSI_SUPPORTED
371
372 sc->msix_regs_rid = 0x20;
373 if ((msi_allowed >= 2) &&
374 (sc->msix_regs_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
375 &sc->msix_regs_rid, RF_ACTIVE)) != NULL) {
376
377 msi_needed = sc->msi_count = SGE_MSIX_COUNT;
378
379 if (((error = pci_alloc_msix(dev, &sc->msi_count)) != 0) ||
380 (sc->msi_count != msi_needed)) {
381 device_printf(dev, "msix allocation failed - msi_count = %d"
382 " msi_needed=%d will try msi err=%d\n", sc->msi_count,
383 msi_needed, error);
384 sc->msi_count = 0;
385 pci_release_msi(dev);
386 bus_release_resource(dev, SYS_RES_MEMORY,
387 sc->msix_regs_rid, sc->msix_regs_res);
388 sc->msix_regs_res = NULL;
389 } else {
390 sc->flags |= USING_MSIX;
391 sc->cxgb_intr = t3_intr_msix;
392 }
393 }
394
395 if ((msi_allowed >= 1) && (sc->msi_count == 0)) {
396 sc->msi_count = 1;
397 if (pci_alloc_msi(dev, &sc->msi_count)) {
398 device_printf(dev, "alloc msi failed - will try INTx\n");
399 sc->msi_count = 0;
400 pci_release_msi(dev);
401 } else {
402 sc->flags |= USING_MSI;
403 sc->irq_rid = 1;
404 sc->cxgb_intr = t3_intr_msi;
405 }
406 }
407 #endif
408 if (sc->msi_count == 0) {
409 device_printf(dev, "using line interrupts\n");
410 sc->irq_rid = 0;
411 sc->cxgb_intr = t3b_intr;
412 }
413
414 sc->ext_intr_task.name = "cxgb_ext_intr_handler";
415 sc->ext_intr_task.func = cxgb_ext_intr_handler;
416 sc->ext_intr_task.context = sc;
417 kthread_create(PRI_NONE, 0, NULL, cxgb_make_task, &sc->ext_intr_task, NULL, "cxgb_make_task");
418
419 sc->tick_task.name = "cxgb_tick_handler";
420 sc->tick_task.func = cxgb_tick_handler;
421 sc->tick_task.context = sc;
422 kthread_create(PRI_NONE, 0, NULL, cxgb_make_task, &sc->tick_task, NULL, "cxgb_make_task");
423
424 /* Create a periodic callout for checking adapter status */
425 callout_init(&sc->cxgb_tick_ch, 0);
426
427 if (t3_check_fw_version(sc) != 0) {
428 /*
429 * Warn user that a firmware update will be attempted in init.
430 */
431 device_printf(dev, "firmware needs to be updated to version %d.%d.%d\n",
432 FW_VERSION_MAJOR, FW_VERSION_MINOR, FW_VERSION_MICRO);
433 sc->flags &= ~FW_UPTODATE;
434 } else {
435 sc->flags |= FW_UPTODATE;
436 }
437
438 if (t3_check_tpsram_version(sc) != 0) {
439 /*
440 * Warn user that a firmware update will be attempted in init.
441 */
442 device_printf(dev, "SRAM needs to be updated to version %c-%d.%d.%d\n",
443 t3rev2char(sc), TP_VERSION_MAJOR, TP_VERSION_MINOR, TP_VERSION_MICRO);
444 sc->flags &= ~TPS_UPTODATE;
445 } else {
446 sc->flags |= TPS_UPTODATE;
447 }
448
449 if ((sc->flags & USING_MSIX) && !singleq)
450 port_qsets = (SGE_QSETS/(sc)->params.nports);
451
452 /*
453 * Create a child device for each MAC. The ethernet attachment
454 * will be done in these children.
455 */
456 for (i = 0; i < (sc)->params.nports; i++) {
457 struct port_info *pi;
458
459 pi = &sc->port[i];
460 pi->adapter = sc;
461 pi->nqsets = port_qsets;
462 pi->first_qset = i*port_qsets;
463 pi->port_id = i;
464 pi->tx_chan = i >= ai->nports0;
465 pi->txpkt_intf = pi->tx_chan ? 2 * (i - ai->nports0) + 1 : 2 * i;
466 sc->rxpkt_map[pi->txpkt_intf] = i;
467 cxgb_args.port = i;
468 locs[0] = 1;
469 locs[1] = i;
470 printf("\n"); // for cleaner formatting in dmesg
471 child = config_found(dev, &cxgb_args, cxgb_cfprint,
472 CFARG_SUBMATCH, config_stdsubmatch,
473 CFARG_IATTR, "cxgbc",
474 CFARG_LOCATORS, locs,
475 CFARG_EOL);
476 printf("\n"); // for cleaner formatting in dmesg
477 sc->portdev[i] = child;
478 }
479
480 /*
481 * XXX need to poll for link status
482 */
483 sc->params.stats_update_period = 1;
484
485 /* initialize sge private state */
486 t3_sge_init_adapter(sc);
487
488 t3_led_ready(sc);
489
490 error = t3_get_fw_version(sc, &vers);
491 if (error)
492 goto out;
493
494 snprintf(&sc->fw_version[0], sizeof(sc->fw_version), "%d.%d.%d",
495 G_FW_VERSION_MAJOR(vers), G_FW_VERSION_MINOR(vers),
496 G_FW_VERSION_MICRO(vers));
497 out:
498 if (error)
499 {
500 cxgb_free(sc);
501 }
502 }
503
504 static int
505 cxgb_controller_detach(device_t dev, int flags)
506 {
507 struct adapter *sc;
508
509 sc = device_private(dev);
510
511 cxgb_free(sc);
512
513 return (0);
514 }
515
516 static void
517 cxgb_free(struct adapter *sc)
518 {
519 int i;
520
521 ADAPTER_LOCK(sc);
522 /*
523 * drops the lock
524 */
525 cxgb_down_locked(sc);
526
527 #ifdef MSI_SUPPORTED
528 if (sc->flags & (USING_MSI | USING_MSIX)) {
529 device_printf(sc->dev, "releasing msi message(s)\n");
530 pci_release_msi(sc->dev);
531 } else {
532 device_printf(sc->dev, "no msi message to release\n");
533 }
534 if (sc->msix_regs_res != NULL) {
535 bus_release_resource(sc->dev, SYS_RES_MEMORY, sc->msix_regs_rid,
536 sc->msix_regs_res);
537 }
538 #endif
539
540 t3_sge_deinit_sw(sc);
541 /*
542 * Wait for last callout
543 */
544
545 tsleep(&sc, 0, "cxgb unload", 3*hz);
546
547 for (i = 0; i < (sc)->params.nports; ++i) {
548 if (sc->portdev[i] != NULL)
549 {
550 INT3;
551 }
552 }
553
554 #ifdef notyet
555 if (is_offload(sc)) {
556 cxgb_adapter_unofld(sc);
557 if (isset(&sc->open_device_map, OFFLOAD_DEVMAP_BIT))
558 offload_close(&sc->tdev);
559 }
560 #endif
561
562 t3_free_sge_resources(sc);
563 free(sc->filters, M_DEVBUF);
564 t3_sge_free(sc);
565
566 MTX_DESTROY(&sc->mdio_lock);
567 MTX_DESTROY(&sc->sge.reg_lock);
568 MTX_DESTROY(&sc->elmer_lock);
569 ADAPTER_LOCK_DEINIT(sc);
570
571 return;
572 }
573
574 /**
575 * setup_sge_qsets - configure SGE Tx/Rx/response queues
576 * @sc: the controller softc
577 *
578 * Determines how many sets of SGE queues to use and initializes them.
579 * We support multiple queue sets per port if we have MSI-X, otherwise
580 * just one queue set per port.
581 */
582 static int
583 setup_sge_qsets(adapter_t *sc)
584 {
585 int i, j, err, irq_idx = 0, qset_idx = 0;
586 u_int ntxq = SGE_TXQ_PER_SET;
587
588 if ((err = t3_sge_alloc(sc)) != 0) {
589 device_printf(sc->dev, "t3_sge_alloc returned %d\n", err);
590 return (err);
591 }
592
593 if (sc->params.rev > 0 && !(sc->flags & USING_MSI))
594 irq_idx = -1;
595
596 for (i = 0; i < (sc)->params.nports; i++) {
597 struct port_info *pi = &sc->port[i];
598
599 for (j = 0; j < pi->nqsets; j++, qset_idx++) {
600 err = t3_sge_alloc_qset(sc, qset_idx, (sc)->params.nports,
601 (sc->flags & USING_MSIX) ? qset_idx + 1 : irq_idx,
602 &sc->params.sge.qset[qset_idx], ntxq, pi);
603 if (err) {
604 t3_free_sge_resources(sc);
605 device_printf(sc->dev, "t3_sge_alloc_qset failed with %d\n",
606 err);
607 return (err);
608 }
609 }
610 }
611
612 return (0);
613 }
614
615 static void
616 cxgb_teardown_msix(adapter_t *sc)
617 {
618 int i, nqsets;
619
620 for (nqsets = i = 0; i < (sc)->params.nports; i++)
621 nqsets += sc->port[i].nqsets;
622
623 for (i = 0; i < nqsets; i++) {
624 if (sc->msix_intr_tag[i] != NULL) {
625 sc->msix_intr_tag[i] = NULL;
626 }
627 if (sc->msix_irq_res[i] != NULL) {
628 sc->msix_irq_res[i] = NULL;
629 }
630 }
631 }
632
633 static int
634 cxgb_setup_msix(adapter_t *sc, int msix_count)
635 {
636 int i, j, k, nqsets, rid;
637
638 /* The first message indicates link changes and error conditions */
639 sc->irq_rid = 1;
640 /* Allocate PCI interrupt resources. */
641 if (pci_intr_map(&sc->pa, &sc->intr_handle))
642 {
643 printf("cxgb_setup_msix(%d): pci_intr_map() failed\n", __LINE__);
644 return (EINVAL);
645 }
646 sc->intr_cookie = pci_intr_establish_xname(sc->pa.pa_pc, sc->intr_handle,
647 IPL_NET, cxgb_async_intr, sc, device_xname(sc->dev));
648 if (sc->intr_cookie == NULL)
649 {
650 printf("cxgb_setup_msix(%d): pci_intr_establish() failed\n", __LINE__);
651 return (EINVAL);
652 }
653 for (i = k = 0; i < (sc)->params.nports; i++) {
654 nqsets = sc->port[i].nqsets;
655 for (j = 0; j < nqsets; j++, k++) {
656 rid = k + 2;
657 if (cxgb_debug)
658 printf("rid=%d ", rid);
659 INT3;
660 }
661 }
662
663
664 return (0);
665 }
666
667 static int cxgb_port_match(device_t dev, cfdata_t match, void *context)
668 {
669 return (100);
670 }
671
672 #define IFCAP_HWCSUM (IFCAP_CSUM_IPv4_Rx | IFCAP_CSUM_IPv4_Tx)
673 #define IFCAP_RXCSUM IFCAP_CSUM_IPv4_Rx
674 #define IFCAP_TXCSUM IFCAP_CSUM_IPv4_Tx
675
676 #ifdef TSO_SUPPORTED
677 #define CXGB_CAP (IFCAP_HWCSUM | IFCAP_TSO)
678 /* Don't enable TSO6 yet */
679 #define CXGB_CAP_ENABLE (IFCAP_HWCSUM | IFCAP_TSO4)
680 #else
681 #define CXGB_CAP (IFCAP_HWCSUM)
682 /* Don't enable TSO6 yet */
683 #define CXGB_CAP_ENABLE (IFCAP_HWCSUM)
684 #define IFCAP_TSO4 0x0
685 #define IFCAP_TSO6 0x0
686 #define CSUM_TSO 0x0
687 #endif
688
689 static void
690 cxgb_port_attach(device_t parent, device_t self, void *context)
691 {
692 struct port_info *p;
693 struct port_device *pd;
694 int *port_number = (int *)context;
695 char buf[32];
696 struct ifnet *ifp;
697 int media_flags;
698 pd = device_private(self);
699 pd->dev = self;
700 pd->parent = device_private(parent);
701 pd->port_number = *port_number;
702 p = &pd->parent->port[*port_number];
703 p->pd = pd;
704
705 PORT_LOCK_INIT(p, p->lockbuf);
706
707 /* Allocate an ifnet object and set it up */
708 ifp = p->ifp = (void *)malloc(sizeof (struct ifnet), M_IFADDR, M_WAITOK);
709 if (ifp == NULL) {
710 device_printf(self, "Cannot allocate ifnet\n");
711 return;
712 }
713 memset(ifp, 0, sizeof(struct ifnet));
714
715 /*
716 * Note that there is currently no watchdog timer.
717 */
718 snprintf(buf, sizeof(buf), "cxgb%d", p->port);
719 strcpy(ifp->if_xname, buf);
720 ifp->if_init = cxgb_init;
721 ifp->if_softc = p;
722 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
723 ifp->if_ioctl = cxgb_ioctl;
724 ifp->if_start = cxgb_start;
725 ifp->if_stop = cxgb_stop;
726 ifp->if_timer = 0; /* Disable ifnet watchdog */
727 ifp->if_watchdog = NULL;
728
729 ifp->if_snd.ifq_maxlen = TX_ETH_Q_SIZE;
730 IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_maxlen);
731
732 IFQ_SET_READY(&ifp->if_snd);
733
734 ifp->if_capabilities = ifp->if_capenable = 0;
735 ifp->if_baudrate = 10000000000; // 10 Gbps
736 /*
737 * disable TSO on 4-port - it isn't supported by the firmware yet
738 */
739 if (p->adapter->params.nports > 2) {
740 ifp->if_capabilities &= ~(IFCAP_TSO4 | IFCAP_TSO6);
741 ifp->if_capenable &= ~(IFCAP_TSO4 | IFCAP_TSO6);
742 }
743
744 if_attach(ifp);
745 ether_ifattach(ifp, p->hw_addr);
746 /*
747 * Only default to jumbo frames on 10GigE
748 */
749 if (p->adapter->params.nports <= 2)
750 ifp->if_mtu = 9000;
751 ifmedia_init(&p->media, IFM_IMASK, cxgb_media_change,
752 cxgb_media_status);
753
754 if (!strcmp(p->port_type->desc, "10GBASE-CX4")) {
755 media_flags = IFM_ETHER | IFM_10G_CX4 | IFM_FDX;
756 } else if (!strcmp(p->port_type->desc, "10GBASE-SR")) {
757 media_flags = IFM_ETHER | IFM_10G_SR | IFM_FDX;
758 } else if (!strcmp(p->port_type->desc, "10GBASE-XR")) {
759 media_flags = IFM_ETHER | IFM_10G_LR | IFM_FDX;
760 } else if (!strcmp(p->port_type->desc, "10/100/1000BASE-T")) {
761 ifmedia_add(&p->media, IFM_ETHER | IFM_10_T, 0, NULL);
762 ifmedia_add(&p->media, IFM_ETHER | IFM_10_T | IFM_FDX,
763 0, NULL);
764 ifmedia_add(&p->media, IFM_ETHER | IFM_100_TX,
765 0, NULL);
766 ifmedia_add(&p->media, IFM_ETHER | IFM_100_TX | IFM_FDX,
767 0, NULL);
768 ifmedia_add(&p->media, IFM_ETHER | IFM_1000_T | IFM_FDX,
769 0, NULL);
770 media_flags = 0;
771 } else {
772 printf("unsupported media type %s\n", p->port_type->desc);
773 return;
774 }
775 if (media_flags) {
776 ifmedia_add(&p->media, media_flags, 0, NULL);
777 ifmedia_set(&p->media, media_flags);
778 } else {
779 ifmedia_add(&p->media, IFM_ETHER | IFM_AUTO, 0, NULL);
780 ifmedia_set(&p->media, IFM_ETHER | IFM_AUTO);
781 }
782
783 snprintf(p->taskqbuf, TASKQ_NAME_LEN, "cxgb_port_taskq%d", p->port_id);
784 p->start_task.name = "cxgb_start_proc";
785 p->start_task.func = cxgb_start_proc;
786 p->start_task.context = ifp;
787 kthread_create(PRI_NONE, 0, NULL, cxgb_make_task, &p->start_task, NULL, "cxgb_make_task");
788
789 t3_sge_init_port(p);
790 }
791
792 static int
793 cxgb_port_detach(device_t self, int flags)
794 {
795 struct port_info *p;
796
797 p = device_private(self);
798
799 PORT_LOCK(p);
800 if (p->ifp->if_drv_flags & IFF_DRV_RUNNING)
801 cxgb_stop_locked(p);
802 PORT_UNLOCK(p);
803
804 if (p->start_task.wq != NULL) {
805 workqueue_destroy(p->start_task.wq);
806 p->start_task.wq = NULL;
807 }
808
809 ether_ifdetach(p->ifp);
810 /*
811 * the lock may be acquired in ifdetach
812 */
813 PORT_LOCK_DEINIT(p);
814 if_detach(p->ifp);
815
816 ifmedia_fini(&p->media);
817
818 return (0);
819 }
820
821 void
822 t3_fatal_err(struct adapter *sc)
823 {
824 u_int fw_status[4];
825
826 if (sc->flags & FULL_INIT_DONE) {
827 t3_sge_stop(sc);
828 t3_write_reg(sc, A_XGM_TX_CTRL, 0);
829 t3_write_reg(sc, A_XGM_RX_CTRL, 0);
830 t3_write_reg(sc, XGM_REG(A_XGM_TX_CTRL, 1), 0);
831 t3_write_reg(sc, XGM_REG(A_XGM_RX_CTRL, 1), 0);
832 t3_intr_disable(sc);
833 }
834 device_printf(sc->dev,"encountered fatal error, operation suspended\n");
835 if (!t3_cim_ctl_blk_read(sc, 0xa0, 4, fw_status))
836 device_printf(sc->dev, "FW_ status: 0x%x, 0x%x, 0x%x, 0x%x\n",
837 fw_status[0], fw_status[1], fw_status[2], fw_status[3]);
838 }
839
840 int
841 t3_os_find_pci_capability(adapter_t *sc, int cap)
842 {
843 device_t dev;
844 uint32_t status;
845 uint32_t bhlc;
846 uint32_t temp;
847 uint8_t ptr;
848 dev = sc->dev;
849 status = pci_conf_read(sc->pa.pa_pc, sc->pa.pa_tag, PCI_COMMAND_STATUS_REG);
850 if (!(status&PCI_STATUS_CAPLIST_SUPPORT))
851 return (0);
852 bhlc = pci_conf_read(sc->pa.pa_pc, sc->pa.pa_tag, PCI_BHLC_REG);
853 switch (PCI_HDRTYPE(bhlc))
854 {
855 case 0:
856 case 1:
857 ptr = PCI_CAPLISTPTR_REG;
858 break;
859 case 2:
860 ptr = PCI_CARDBUS_CAPLISTPTR_REG;
861 break;
862 default:
863 return (0);
864 }
865 temp = pci_conf_read(sc->pa.pa_pc, sc->pa.pa_tag, ptr);
866 ptr = PCI_CAPLIST_PTR(temp);
867 while (ptr != 0) {
868 temp = pci_conf_read(sc->pa.pa_pc, sc->pa.pa_tag, ptr);
869 if (PCI_CAPLIST_CAP(temp) == cap)
870 return (ptr);
871 ptr = PCI_CAPLIST_NEXT(temp);
872 }
873
874 return (0);
875 }
876
877 int
878 t3_os_pci_save_state(struct adapter *sc)
879 {
880 INT3;
881 return (0);
882 }
883
884 int
885 t3_os_pci_restore_state(struct adapter *sc)
886 {
887 INT3;
888 return (0);
889 }
890
891 /**
892 * t3_os_link_changed - handle link status changes
893 * @adapter: the adapter associated with the link change
894 * @port_id: the port index whose limk status has changed
895 * @link_stat: the new status of the link
896 * @speed: the new speed setting
897 * @duplex: the new duplex setting
898 * @fc: the new flow-control setting
899 *
900 * This is the OS-dependent handler for link status changes. The OS
901 * neutral handler takes care of most of the processing for these events,
902 * then calls this handler for any OS-specific processing.
903 */
904 void
905 t3_os_link_changed(adapter_t *adapter, int port_id, int link_status, int speed,
906 int duplex, int fc)
907 {
908 struct port_info *pi = &adapter->port[port_id];
909 struct cmac *mac = &adapter->port[port_id].mac;
910
911 if ((pi->ifp->if_flags & IFF_UP) == 0)
912 return;
913
914 if (link_status) {
915 t3_mac_enable(mac, MAC_DIRECTION_RX);
916 if_link_state_change(pi->ifp, LINK_STATE_UP);
917 } else {
918 if_link_state_change(pi->ifp, LINK_STATE_DOWN);
919 pi->phy.ops->power_down(&pi->phy, 1);
920 t3_mac_disable(mac, MAC_DIRECTION_RX);
921 t3_link_start(&pi->phy, mac, &pi->link_config);
922 }
923 }
924
925 /*
926 * Interrupt-context handler for external (PHY) interrupts.
927 */
928 void
929 t3_os_ext_intr_handler(adapter_t *sc)
930 {
931 if (cxgb_debug)
932 printf("t3_os_ext_intr_handler\n");
933 /*
934 * Schedule a task to handle external interrupts as they may be slow
935 * and we use a mutex to protect MDIO registers. We disable PHY
936 * interrupts in the meantime and let the task reenable them when
937 * it's done.
938 */
939 ADAPTER_LOCK(sc);
940 if (sc->slow_intr_mask) {
941 sc->slow_intr_mask &= ~F_T3DBG;
942 t3_write_reg(sc, A_PL_INT_ENABLE0, sc->slow_intr_mask);
943 workqueue_enqueue(sc->ext_intr_task.wq, &sc->ext_intr_task.w, NULL);
944 }
945 ADAPTER_UNLOCK(sc);
946 }
947
948 void
949 t3_os_set_hw_addr(adapter_t *adapter, int port_idx, u8 hw_addr[])
950 {
951
952 /*
953 * The ifnet might not be allocated before this gets called,
954 * as this is called early on in attach by t3_prep_adapter
955 * save the address off in the port structure
956 */
957 if (cxgb_debug)
958 printf("set_hw_addr on idx %d addr %02x:%02x:%02x:%02x:%02x:%02x\n",
959 port_idx, hw_addr[0], hw_addr[1], hw_addr[2], hw_addr[3], hw_addr[4], hw_addr[5]);
960 memcpy(adapter->port[port_idx].hw_addr, hw_addr, ETHER_ADDR_LEN);
961 }
962
963 /**
964 * link_start - enable a port
965 * @p: the port to enable
966 *
967 * Performs the MAC and PHY actions needed to enable a port.
968 */
969 static void
970 cxgb_link_start(struct port_info *p)
971 {
972 struct ifnet *ifp;
973 struct t3_rx_mode rm;
974 struct cmac *mac = &p->mac;
975
976 ifp = p->ifp;
977
978 t3_init_rx_mode(&rm, p);
979 if (!mac->multiport)
980 t3_mac_reset(mac);
981 t3_mac_set_mtu(mac, ifp->if_mtu + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
982 t3_mac_set_address(mac, 0, p->hw_addr);
983 t3_mac_set_rx_mode(mac, &rm);
984 t3_link_start(&p->phy, mac, &p->link_config);
985 t3_mac_enable(mac, MAC_DIRECTION_RX | MAC_DIRECTION_TX);
986 }
987
988 /**
989 * setup_rss - configure Receive Side Steering (per-queue connection demux)
990 * @adap: the adapter
991 *
992 * Sets up RSS to distribute packets to multiple receive queues. We
993 * configure the RSS CPU lookup table to distribute to the number of HW
994 * receive queues, and the response queue lookup table to narrow that
995 * down to the response queues actually configured for each port.
996 * We always configure the RSS mapping for two ports since the mapping
997 * table has plenty of entries.
998 */
999 static void
1000 setup_rss(adapter_t *adap)
1001 {
1002 int i;
1003 u_int nq[2];
1004 uint8_t cpus[SGE_QSETS + 1];
1005 uint16_t rspq_map[RSS_TABLE_SIZE];
1006
1007 for (i = 0; i < SGE_QSETS; ++i)
1008 cpus[i] = i;
1009 cpus[SGE_QSETS] = 0xff;
1010
1011 nq[0] = nq[1] = 0;
1012 for_each_port(adap, i) {
1013 const struct port_info *pi = adap2pinfo(adap, i);
1014
1015 nq[pi->tx_chan] += pi->nqsets;
1016 }
1017 nq[0] = uimax(nq[0], 1U);
1018 nq[1] = uimax(nq[1], 1U);
1019 for (i = 0; i < RSS_TABLE_SIZE / 2; ++i) {
1020 rspq_map[i] = i % nq[0];
1021 rspq_map[i + RSS_TABLE_SIZE / 2] = (i % nq[1]) + nq[0];
1022 }
1023 /* Calculate the reverse RSS map table */
1024 for (i = 0; i < RSS_TABLE_SIZE; ++i)
1025 if (adap->rrss_map[rspq_map[i]] == 0xff)
1026 adap->rrss_map[rspq_map[i]] = i;
1027
1028 t3_config_rss(adap, F_RQFEEDBACKENABLE | F_TNLLKPEN | F_TNLMAPEN |
1029 F_TNLPRTEN | F_TNL2TUPEN | F_TNL4TUPEN | F_OFDMAPEN |
1030 V_RRCPLCPUSIZE(6), cpus, rspq_map);
1031
1032 }
1033
1034 /*
1035 * Sends an mbuf to an offload queue driver
1036 * after dealing with any active network taps.
1037 */
1038 static inline int
1039 offload_tx(struct toedev *tdev, struct mbuf *m)
1040 {
1041 int ret;
1042
1043 critical_enter();
1044 ret = t3_offload_tx(tdev, m);
1045 critical_exit();
1046 return (ret);
1047 }
1048
1049 static void
1050 send_pktsched_cmd(struct adapter *adap, int sched, int qidx, int lo,
1051 int hi, int port)
1052 {
1053 struct mbuf *m;
1054 struct mngt_pktsched_wr *req;
1055
1056 m = m_gethdr(M_DONTWAIT, MT_DATA);
1057 if (m) {
1058 req = mtod(m, struct mngt_pktsched_wr *);
1059 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_MNGT));
1060 req->mngt_opcode = FW_MNGTOPCODE_PKTSCHED_SET;
1061 req->sched = sched;
1062 req->idx = qidx;
1063 req->min = lo;
1064 req->max = hi;
1065 req->binding = port;
1066 m->m_len = m->m_pkthdr.len = sizeof(*req);
1067 t3_mgmt_tx(adap, m);
1068 }
1069 }
1070
1071 static void
1072 bind_qsets(adapter_t *sc)
1073 {
1074 int i, j;
1075
1076 for (i = 0; i < (sc)->params.nports; ++i) {
1077 const struct port_info *pi = adap2pinfo(sc, i);
1078
1079 for (j = 0; j < pi->nqsets; ++j) {
1080 send_pktsched_cmd(sc, 1, pi->first_qset + j, -1,
1081 -1, pi->tx_chan);
1082
1083 }
1084 }
1085 }
1086
1087 /**
1088 * cxgb_up - enable the adapter
1089 * @adap: adapter being enabled
1090 *
1091 * Called when the first port is enabled, this function performs the
1092 * actions necessary to make an adapter operational, such as completing
1093 * the initialization of HW modules, and enabling interrupts.
1094 *
1095 */
1096 static int
1097 cxgb_up(struct adapter *sc)
1098 {
1099 int err = 0;
1100
1101 if ((sc->flags & FULL_INIT_DONE) == 0) {
1102
1103 if ((sc->flags & FW_UPTODATE) == 0)
1104 printf("SHOULD UPGRADE FIRMWARE!\n");
1105 if ((sc->flags & TPS_UPTODATE) == 0)
1106 printf("SHOULD UPDATE TPSRAM\n");
1107 err = t3_init_hw(sc, 0);
1108 if (err)
1109 goto out;
1110
1111 t3_write_reg(sc, A_ULPRX_TDDP_PSZ, V_HPZ0(PAGE_SHIFT - 12));
1112
1113 err = setup_sge_qsets(sc);
1114 if (err)
1115 goto out;
1116
1117 setup_rss(sc);
1118 sc->flags |= FULL_INIT_DONE;
1119 }
1120
1121 t3_intr_clear(sc);
1122
1123 /* If it's MSI or INTx, allocate a single interrupt for everything */
1124 if ((sc->flags & USING_MSIX) == 0) {
1125 if (pci_intr_map(&sc->pa, &sc->intr_handle))
1126 {
1127 device_printf(sc->dev, "Cannot allocate interrupt\n");
1128 err = EINVAL;
1129 goto out;
1130 }
1131 device_printf(sc->dev, "allocated intr_handle=%d\n", sc->intr_handle);
1132 sc->intr_cookie = pci_intr_establish_xname(sc->pa.pa_pc,
1133 sc->intr_handle, IPL_NET,
1134 sc->cxgb_intr, sc, device_xname(sc->dev));
1135 if (sc->intr_cookie == NULL)
1136 {
1137 device_printf(sc->dev, "Cannot establish interrupt\n");
1138 err = EINVAL;
1139 goto irq_err;
1140 }
1141 } else {
1142 printf("Using MSIX?!?!?!\n");
1143 INT3;
1144 cxgb_setup_msix(sc, sc->msi_count);
1145 }
1146
1147 t3_sge_start(sc);
1148 t3_intr_enable(sc);
1149
1150 if (!(sc->flags & QUEUES_BOUND)) {
1151 bind_qsets(sc);
1152 sc->flags |= QUEUES_BOUND;
1153 }
1154 out:
1155 return (err);
1156 irq_err:
1157 CH_ERR(sc, "request_irq failed, err %d\n", err);
1158 goto out;
1159 }
1160
1161
1162 /*
1163 * Release resources when all the ports and offloading have been stopped.
1164 */
1165 static void
1166 cxgb_down_locked(struct adapter *sc)
1167 {
1168 t3_sge_stop(sc);
1169 t3_intr_disable(sc);
1170
1171 INT3; // XXXXXXXXXXXXXXXXXX
1172
1173 if (sc->flags & USING_MSIX)
1174 cxgb_teardown_msix(sc);
1175 ADAPTER_UNLOCK(sc);
1176
1177 callout_drain(&sc->cxgb_tick_ch);
1178 callout_drain(&sc->sge_timer_ch);
1179
1180 #ifdef notyet
1181
1182 if (sc->port[i].tq != NULL)
1183 #endif
1184
1185 }
1186
1187 static int
1188 cxgb_init(struct ifnet *ifp)
1189 {
1190 struct port_info *p = ifp->if_softc;
1191
1192 PORT_LOCK(p);
1193 cxgb_init_locked(p);
1194 PORT_UNLOCK(p);
1195
1196 return (0); // ????????????
1197 }
1198
1199 static void
1200 cxgb_init_locked(struct port_info *p)
1201 {
1202 struct ifnet *ifp;
1203 adapter_t *sc = p->adapter;
1204 int err;
1205
1206 PORT_LOCK_ASSERT_OWNED(p);
1207 ifp = p->ifp;
1208
1209 ADAPTER_LOCK(p->adapter);
1210 if ((sc->open_device_map == 0) && (err = cxgb_up(sc))) {
1211 ADAPTER_UNLOCK(p->adapter);
1212 cxgb_stop_locked(p);
1213 return;
1214 }
1215 if (p->adapter->open_device_map == 0) {
1216 t3_intr_clear(sc);
1217 t3_sge_init_adapter(sc);
1218 }
1219 setbit(&p->adapter->open_device_map, p->port_id);
1220 ADAPTER_UNLOCK(p->adapter);
1221
1222 cxgb_link_start(p);
1223 t3_link_changed(sc, p->port_id);
1224 ifp->if_baudrate = p->link_config.speed * 1000000;
1225
1226 device_printf(sc->dev, "enabling interrupts on port=%d\n", p->port_id);
1227 t3_port_intr_enable(sc, p->port_id);
1228
1229 callout_reset(&sc->cxgb_tick_ch, sc->params.stats_update_period * hz,
1230 cxgb_tick, sc);
1231
1232 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1233 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1234 }
1235
1236 static void
1237 cxgb_set_rxmode(struct port_info *p)
1238 {
1239 struct t3_rx_mode rm;
1240 struct cmac *mac = &p->mac;
1241
1242 PORT_LOCK_ASSERT_OWNED(p);
1243
1244 t3_init_rx_mode(&rm, p);
1245 t3_mac_set_rx_mode(mac, &rm);
1246 }
1247
1248 static void
1249 cxgb_stop_locked(struct port_info *p)
1250 {
1251 struct ifnet *ifp;
1252
1253 PORT_LOCK_ASSERT_OWNED(p);
1254 ADAPTER_LOCK_ASSERT_NOTOWNED(p->adapter);
1255
1256 ifp = p->ifp;
1257
1258 t3_port_intr_disable(p->adapter, p->port_id);
1259 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1260 p->phy.ops->power_down(&p->phy, 1);
1261 t3_mac_disable(&p->mac, MAC_DIRECTION_TX | MAC_DIRECTION_RX);
1262
1263 ADAPTER_LOCK(p->adapter);
1264 clrbit(&p->adapter->open_device_map, p->port_id);
1265
1266
1267 if (p->adapter->open_device_map == 0) {
1268 cxgb_down_locked(p->adapter);
1269 } else
1270 ADAPTER_UNLOCK(p->adapter);
1271
1272 }
1273
1274 static int
1275 cxgb_set_mtu(struct port_info *p, int mtu)
1276 {
1277 struct ifnet *ifp = p->ifp;
1278 struct ifreq ifr;
1279 int error = 0;
1280
1281 ifr.ifr_mtu = mtu;
1282
1283 if ((mtu < ETHERMIN) || (mtu > ETHER_MAX_LEN_JUMBO))
1284 error = EINVAL;
1285 else if ((error = ifioctl_common(ifp, SIOCSIFMTU, &ifr)) == ENETRESET) {
1286 error = 0;
1287 PORT_LOCK(p);
1288 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1289 callout_stop(&p->adapter->cxgb_tick_ch);
1290 cxgb_stop_locked(p);
1291 cxgb_init_locked(p);
1292 }
1293 PORT_UNLOCK(p);
1294 }
1295 return (error);
1296 }
1297
1298 static int
1299 cxgb_ioctl(struct ifnet *ifp, unsigned long command, void *data)
1300 {
1301 struct port_info *p = ifp->if_softc;
1302 struct ifaddr *ifa = (struct ifaddr *)data;
1303 struct ifreq *ifr = (struct ifreq *)data;
1304 int flags, error = 0;
1305
1306 /*
1307 * XXX need to check that we aren't in the middle of an unload
1308 */
1309 printf("cxgb_ioctl(%d): command=%08lx\n", __LINE__, command);
1310 switch (command) {
1311 case SIOCSIFMTU:
1312 error = cxgb_set_mtu(p, ifr->ifr_mtu);
1313 printf("SIOCSIFMTU: error=%d\n", error);
1314 break;
1315 case SIOCINITIFADDR:
1316 printf("SIOCINITIFADDR:\n");
1317 PORT_LOCK(p);
1318 if (ifa->ifa_addr->sa_family == AF_INET) {
1319 ifp->if_flags |= IFF_UP;
1320 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1321 cxgb_init_locked(p);
1322 arp_ifinit(ifp, ifa);
1323 } else
1324 error = ether_ioctl(ifp, command, data);
1325 PORT_UNLOCK(p);
1326 break;
1327 case SIOCSIFFLAGS:
1328 printf("SIOCSIFFLAGS:\n");
1329 #if 0
1330 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1331 break;
1332 #endif
1333 callout_drain(&p->adapter->cxgb_tick_ch);
1334 PORT_LOCK(p);
1335 if (ifp->if_flags & IFF_UP) {
1336 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1337 flags = p->if_flags;
1338 if (((ifp->if_flags ^ flags) & IFF_PROMISC) ||
1339 ((ifp->if_flags ^ flags) & IFF_ALLMULTI))
1340 cxgb_set_rxmode(p);
1341 } else
1342 cxgb_init_locked(p);
1343 p->if_flags = ifp->if_flags;
1344 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1345 cxgb_stop_locked(p);
1346
1347 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1348 adapter_t *sc = p->adapter;
1349 callout_reset(&sc->cxgb_tick_ch,
1350 sc->params.stats_update_period * hz,
1351 cxgb_tick, sc);
1352 }
1353 PORT_UNLOCK(p);
1354 break;
1355 case SIOCSIFMEDIA:
1356 printf("SIOCSIFMEDIA:\n");
1357 case SIOCGIFMEDIA:
1358 error = ifmedia_ioctl(ifp, ifr, &p->media, command);
1359 printf("SIOCGIFMEDIA: error=%d\n", error);
1360 break;
1361 default:
1362 printf("Dir = %x Len = %x Group = '%c' Num = %x\n",
1363 (unsigned int)(command&0xe0000000)>>28, (unsigned int)(command&0x1fff0000)>>16,
1364 (unsigned int)(command&0xff00)>>8, (unsigned int)command&0xff);
1365 if ((error = ether_ioctl(ifp, command, data)) != ENETRESET)
1366 break;
1367 error = 0;
1368 break;
1369 }
1370 return (error);
1371 }
1372
1373 static int
1374 cxgb_start_tx(struct ifnet *ifp, uint32_t txmax)
1375 {
1376 struct sge_qset *qs;
1377 struct sge_txq *txq;
1378 struct port_info *p = ifp->if_softc;
1379 struct mbuf *m = NULL;
1380 int err, in_use_init, free_it;
1381
1382 if (!p->link_config.link_ok)
1383 {
1384 return (ENXIO);
1385 }
1386
1387 if (IFQ_IS_EMPTY(&ifp->if_snd))
1388 {
1389 return (ENOBUFS);
1390 }
1391
1392 qs = &p->adapter->sge.qs[p->first_qset];
1393 txq = &qs->txq[TXQ_ETH];
1394 err = 0;
1395
1396 if (txq->flags & TXQ_TRANSMITTING)
1397 {
1398 return (EINPROGRESS);
1399 }
1400
1401 mtx_lock(&txq->lock);
1402 txq->flags |= TXQ_TRANSMITTING;
1403 in_use_init = txq->in_use;
1404 while ((txq->in_use - in_use_init < txmax) &&
1405 (txq->size > txq->in_use + TX_MAX_DESC)) {
1406 free_it = 0;
1407 IFQ_DEQUEUE(&ifp->if_snd, m);
1408 if (m == NULL)
1409 break;
1410 /*
1411 * Convert chain to M_IOVEC
1412 */
1413 KASSERT((m->m_flags & M_IOVEC) == 0);
1414 #ifdef notyet
1415 m0 = m;
1416 if (collapse_mbufs && m->m_pkthdr.len > MCLBYTES &&
1417 m_collapse(m, TX_MAX_SEGS, &m0) == EFBIG) {
1418 if ((m0 = m_defrag(m, M_NOWAIT)) != NULL) {
1419 m = m0;
1420 m_collapse(m, TX_MAX_SEGS, &m0);
1421 } else
1422 break;
1423 }
1424 m = m0;
1425 #endif
1426 if ((err = t3_encap(p, &m, &free_it)) != 0)
1427 {
1428 printf("t3_encap() returned %d\n", err);
1429 break;
1430 }
1431 // bpf_mtap(ifp, m, BPF_D_OUT);
1432 if (free_it)
1433 {
1434 m_freem(m);
1435 }
1436 }
1437 txq->flags &= ~TXQ_TRANSMITTING;
1438 mtx_unlock(&txq->lock);
1439
1440 if (__predict_false(err)) {
1441 if (err == ENOMEM) {
1442 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1443 // XXXXXXXXXX lock/unlock??
1444 IF_PREPEND(&ifp->if_snd, m);
1445 }
1446 }
1447 if (err == 0 && m == NULL)
1448 err = ENOBUFS;
1449 else if ((err == 0) && (txq->size <= txq->in_use + TX_MAX_DESC) &&
1450 (ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0) {
1451 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1452 err = ENOSPC;
1453 }
1454 return (err);
1455 }
1456
1457 static void
1458 cxgb_start_proc(struct work *wk, void *arg)
1459 {
1460 struct ifnet *ifp = arg;
1461 struct port_info *pi = ifp->if_softc;
1462 struct sge_qset *qs;
1463 struct sge_txq *txq;
1464 int error;
1465
1466 qs = &pi->adapter->sge.qs[pi->first_qset];
1467 txq = &qs->txq[TXQ_ETH];
1468
1469 do {
1470 if (desc_reclaimable(txq) > TX_CLEAN_MAX_DESC >> 2)
1471 workqueue_enqueue(pi->timer_reclaim_task.wq, &pi->timer_reclaim_task.w, NULL);
1472
1473 error = cxgb_start_tx(ifp, TX_START_MAX_DESC);
1474 } while (error == 0);
1475 }
1476
1477 static void
1478 cxgb_start(struct ifnet *ifp)
1479 {
1480 struct port_info *pi = ifp->if_softc;
1481 struct sge_qset *qs;
1482 struct sge_txq *txq;
1483 int err;
1484
1485 qs = &pi->adapter->sge.qs[pi->first_qset];
1486 txq = &qs->txq[TXQ_ETH];
1487
1488 if (desc_reclaimable(txq) > TX_CLEAN_MAX_DESC >> 2)
1489 workqueue_enqueue(pi->timer_reclaim_task.wq, &pi->timer_reclaim_task.w, NULL);
1490
1491 err = cxgb_start_tx(ifp, TX_START_MAX_DESC);
1492
1493 if (err == 0)
1494 workqueue_enqueue(pi->start_task.wq, &pi->start_task.w, NULL);
1495 }
1496
1497 static void
1498 cxgb_stop(struct ifnet *ifp, int reason)
1499 {
1500 struct port_info *pi = ifp->if_softc;
1501
1502 printf("cxgb_stop(): pi=%p, reason=%d\n", pi, reason);
1503 INT3;
1504 }
1505
1506 static int
1507 cxgb_media_change(struct ifnet *ifp)
1508 {
1509 printf("media change not supported: ifp=%p\n", ifp);
1510 return (ENXIO);
1511 }
1512
1513 static void
1514 cxgb_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1515 {
1516 struct port_info *p;
1517
1518 p = ifp->if_softc;
1519
1520 ifmr->ifm_status = IFM_AVALID;
1521 ifmr->ifm_active = IFM_ETHER;
1522
1523 if (!p->link_config.link_ok)
1524 return;
1525
1526 ifmr->ifm_status |= IFM_ACTIVE;
1527
1528 switch (p->link_config.speed) {
1529 case 10:
1530 ifmr->ifm_active |= IFM_10_T;
1531 break;
1532 case 100:
1533 ifmr->ifm_active |= IFM_100_TX;
1534 break;
1535 case 1000:
1536 ifmr->ifm_active |= IFM_1000_T;
1537 break;
1538 }
1539
1540 if (p->link_config.duplex)
1541 ifmr->ifm_active |= IFM_FDX;
1542 else
1543 ifmr->ifm_active |= IFM_HDX;
1544 }
1545
1546 static int
1547 cxgb_async_intr(void *data)
1548 {
1549 adapter_t *sc = data;
1550
1551 if (cxgb_debug)
1552 device_printf(sc->dev, "cxgb_async_intr\n");
1553 /*
1554 * May need to sleep - defer to taskqueue
1555 */
1556 workqueue_enqueue(sc->slow_intr_task.wq, &sc->slow_intr_task.w, NULL);
1557
1558 return (1);
1559 }
1560
1561 static void
1562 cxgb_ext_intr_handler(struct work *wk, void *arg)
1563 {
1564 adapter_t *sc = (adapter_t *)arg;
1565
1566 if (cxgb_debug)
1567 printf("cxgb_ext_intr_handler\n");
1568
1569 t3_phy_intr_handler(sc);
1570
1571 /* Now reenable external interrupts */
1572 ADAPTER_LOCK(sc);
1573 if (sc->slow_intr_mask) {
1574 sc->slow_intr_mask |= F_T3DBG;
1575 t3_write_reg(sc, A_PL_INT_CAUSE0, F_T3DBG);
1576 t3_write_reg(sc, A_PL_INT_ENABLE0, sc->slow_intr_mask);
1577 }
1578 ADAPTER_UNLOCK(sc);
1579 }
1580
1581 static void
1582 check_link_status(adapter_t *sc)
1583 {
1584 int i;
1585
1586 for (i = 0; i < (sc)->params.nports; ++i) {
1587 struct port_info *p = &sc->port[i];
1588
1589 if (!(p->port_type->caps & SUPPORTED_IRQ))
1590 t3_link_changed(sc, i);
1591 p->ifp->if_baudrate = p->link_config.speed * 1000000;
1592 }
1593 }
1594
1595 static void
1596 check_t3b2_mac(struct adapter *adapter)
1597 {
1598 int i;
1599
1600 for_each_port(adapter, i) {
1601 struct port_info *p = &adapter->port[i];
1602 struct ifnet *ifp = p->ifp;
1603 int status;
1604
1605 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1606 continue;
1607
1608 status = 0;
1609 PORT_LOCK(p);
1610 if ((ifp->if_drv_flags & IFF_DRV_RUNNING))
1611 status = t3b2_mac_watchdog_task(&p->mac);
1612 if (status == 1)
1613 p->mac.stats.num_toggled++;
1614 else if (status == 2) {
1615 struct cmac *mac = &p->mac;
1616
1617 t3_mac_set_mtu(mac, ifp->if_mtu + ETHER_HDR_LEN
1618 + ETHER_VLAN_ENCAP_LEN);
1619 t3_mac_set_address(mac, 0, p->hw_addr);
1620 cxgb_set_rxmode(p);
1621 t3_link_start(&p->phy, mac, &p->link_config);
1622 t3_mac_enable(mac, MAC_DIRECTION_RX | MAC_DIRECTION_TX);
1623 t3_port_intr_enable(adapter, p->port_id);
1624 p->mac.stats.num_resets++;
1625 }
1626 PORT_UNLOCK(p);
1627 }
1628 }
1629
1630 static void
1631 cxgb_tick(void *arg)
1632 {
1633 adapter_t *sc = (adapter_t *)arg;
1634
1635 workqueue_enqueue(sc->tick_task.wq, &sc->tick_task.w, NULL);
1636
1637 if (sc->open_device_map != 0)
1638 callout_reset(&sc->cxgb_tick_ch, sc->params.stats_update_period * hz,
1639 cxgb_tick, sc);
1640 }
1641
1642 static void
1643 cxgb_tick_handler(struct work *wk, void *arg)
1644 {
1645 adapter_t *sc = (adapter_t *)arg;
1646 const struct adapter_params *p = &sc->params;
1647
1648 ADAPTER_LOCK(sc);
1649 if (p->linkpoll_period)
1650 check_link_status(sc);
1651
1652 /*
1653 * adapter lock can currently only be acquire after the
1654 * port lock
1655 */
1656 ADAPTER_UNLOCK(sc);
1657
1658 if (p->rev == T3_REV_B2 && p->nports < 4)
1659 check_t3b2_mac(sc);
1660 }
1661
1662 static void
1663 touch_bars(device_t dev)
1664 {
1665 /*
1666 * Don't enable yet
1667 */
1668 #if !defined(__LP64__) && 0
1669 u32 v;
1670
1671 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_1, &v);
1672 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_1, v);
1673 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_3, &v);
1674 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_3, v);
1675 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_5, &v);
1676 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_5, v);
1677 #endif
1678 }
1679
1680 static __inline void
1681 reg_block_dump(struct adapter *ap, uint8_t *buf, unsigned int start,
1682 unsigned int end)
1683 {
1684 uint32_t *p = (uint32_t *)buf + start;
1685
1686 for ( ; start <= end; start += sizeof(uint32_t))
1687 *p++ = t3_read_reg(ap, start);
1688 }
1689
1690