1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
/*
* aQuantia Corporation Network Driver
* Copyright (C) 2014-2017 aQuantia Corporation. All rights reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* (1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* (2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* (3)The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/bitstring.h>
#include <sys/kernel.h>
#include <sys/sbuf.h>
#include <sys/socket.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/iflib.h>
#include "aq_common.h"
#include "aq_device.h"
#include "aq_ring.h"
#include "aq_dbg.h"
#include "aq_hw.h"
#include "aq_hw_llh.h"
#include "aq_fw.h"
int
aq_update_hw_stats(struct aq_dev *aq_dev)
{
struct aq_hw *hw = &aq_dev->hw;
struct aq_hw_stats stats;
memset(&stats, 0, sizeof(stats));
if (aq_hw_mpi_read_stats(hw, &stats) != 0)
return (0);
#define AQ_SDELTA(_N_) do { \
aq_dev->curr_stats._N_ += stats._N_ - aq_dev->last_stats._N_; \
} while (0)
if (aq_dev->linkup) {
AQ_SDELTA(uprc);
AQ_SDELTA(mprc);
AQ_SDELTA(bprc);
AQ_SDELTA(cprc);
AQ_SDELTA(erpt);
AQ_SDELTA(uptc);
AQ_SDELTA(mptc);
AQ_SDELTA(bptc);
AQ_SDELTA(erpr);
AQ_SDELTA(ubrc);
AQ_SDELTA(ubtc);
AQ_SDELTA(mbrc);
AQ_SDELTA(mbtc);
AQ_SDELTA(bbrc);
AQ_SDELTA(bbtc);
AQ_SDELTA(ptc);
AQ_SDELTA(prc);
AQ_SDELTA(dpc);
/*
* Per-cast octets present: derive the aggregate from them.
* Otherwise (B0) accumulate the firmware-reported aggregate.
*/
if (stats.ubrc | stats.mbrc | stats.bbrc)
aq_dev->curr_stats.brc = aq_dev->curr_stats.ubrc +
aq_dev->curr_stats.mbrc + aq_dev->curr_stats.bbrc;
else
AQ_SDELTA(brc);
if (stats.ubtc | stats.mbtc | stats.bbtc)
aq_dev->curr_stats.btc = aq_dev->curr_stats.ubtc +
aq_dev->curr_stats.mbtc + aq_dev->curr_stats.bbtc;
else
AQ_SDELTA(btc);
}
#undef AQ_SDELTA
memcpy(&aq_dev->last_stats, &stats, sizeof(stats));
return (0);
}
#define AQ_THERMAL_HYSTERESIS_MC 18000 /* recover this far below the limit */
#define AQ_THERMAL_RECOVER_MC 90000 /* fallback when the limit is unreadable */
#define AQ_THERMAL_SETTLE_POLLS 5 /* ~5 s for the PHY reset to settle */
#define AQ_THERMAL_RETRY_SECS 60 /* minimum spacing between recoveries */
#define AQ_INIT_MAX_RETRIES 5 /* re-init attempts after a failed init */
/* Temperature here is post-trip; the PHY is already dropping to low power. */
static void
aq_thermal_report_shutdown(struct aq_dev *aq_dev)
{
struct aq_hw *hw = &aq_dev->hw;
int temp_mc, limit_mc;
bool have_temp, have_limit;
have_temp = hw->fw_ops->get_temp(hw, &temp_mc) == 0;
have_limit = hw->fw_ops->get_thermal_limit(hw, &limit_mc) == 0;
if (have_temp)
aq_dev->thermal_temp_mc = temp_mc;
/* The F/W also exposes a cold_temperature hysteresis point. */
aq_dev->thermal_recover_mc = have_limit ?
limit_mc - AQ_THERMAL_HYSTERESIS_MC : AQ_THERMAL_RECOVER_MC;
if (have_temp && have_limit)
device_printf(aq_dev->dev, "PHY thermal shutdown; "
"limit %d C, temp %d C; holding link down until it cools\n",
limit_mc / 1000, temp_mc / 1000);
else if (have_temp)
device_printf(aq_dev->dev, "PHY thermal shutdown; "
"temp %d C; holding link down until it cools\n",
temp_mc / 1000);
else
device_printf(aq_dev->dev, "PHY thermal shutdown; "
"holding link down until it cools\n");
}
/* Pre-trip warning: the PHY is hot but the firmware has not shut it down. */
static void
aq_thermal_report_hot(struct aq_dev *aq_dev)
{
struct aq_hw *hw = &aq_dev->hw;
int temp_mc;
bool hot;
if (hw->fw_ops->get_phy_hot_warning == NULL ||
hw->fw_ops->get_phy_hot_warning(hw, &hot) != 0)
return;
/* Report each edge once; the bit stays set for the whole excursion. */
if (hot == aq_dev->phy_hot_last)
return;
aq_dev->phy_hot_last = hot;
if (!hot) {
device_printf(aq_dev->dev, "PHY temperature normal again\n");
return;
}
if (hw->fw_ops->get_temp(hw, &temp_mc) == 0)
device_printf(aq_dev->dev, "PHY over-temperature warning, "
"temp %d C\n", temp_mc / 1000);
else
device_printf(aq_dev->dev, "PHY over-temperature warning\n");
}
/* reconcile thermal arm with knob value */
static void
aq_thermal_apply(struct aq_dev *aq_dev)
{
struct aq_hw *hw = &aq_dev->hw;
bool enable;
int err;
/* A queued re-init would undo the write; the poll after it applies. */
if (hw->fw_ops->thermal_arm == NULL || aq_dev->reset_pending)
return;
enable = aq_dev->thermal_shutdown_enabled;
err = hw->fw_ops->thermal_arm(hw, enable);
/* No sensor to arm against; the knob is absent on such an adapter. */
if (err == ENOTSUP)
return;
/* Print only on a change, so a lasting failure is reported once. */
if (err == aq_dev->thermal_arm_err)
return;
aq_dev->thermal_arm_err = err;
if (err != 0)
device_printf(aq_dev->dev, "could not %s PHY thermal shutdown, "
"error %d\n", enable ? "arm" : "disarm", err);
else
device_printf(aq_dev->dev, "PHY thermal shutdown %s\n",
enable ? "armed" : "disarmed");
}
/* Recover after cooldown: A1 needs a PHY reset then re-init, A2 re-inits alone. */
static void
aq_thermal_poll(struct aq_dev *aq_dev)
{
struct aq_hw *hw = &aq_dev->hw;
uint16_t fault;
int temp_mc;
switch (aq_dev->thermal_state) {
case AQ_THERMAL_NORMAL:
aq_thermal_apply(aq_dev);
aq_thermal_report_hot(aq_dev);
if (aq_dev->linkup)
return;
/* The F/W raises the fault a poll after it drops the link. */
if (hw->fw_ops->get_phy_fault(hw, &fault) != 0 || fault == 0)
return;
/* Report each code once; do not mask a later shutdown. */
if (fault == aq_dev->phy_fault_last)
return;
aq_dev->phy_fault_last = fault;
if (fault != AQ_PHY_FAULT_THERMAL_SHUTDOWN) {
device_printf(aq_dev->dev,
"PHY fault 0x%04x\n", fault);
return;
}
aq_thermal_report_shutdown(aq_dev);
aq_dev->thermal_state = AQ_THERMAL_COOLING;
return;
case AQ_THERMAL_COOLING:
if (hw->fw_ops->get_temp(hw, &temp_mc) != 0 ||
temp_mc > aq_dev->thermal_recover_mc)
return;
aq_dev->thermal_temp_mc = temp_mc;
if (hw->fw_ops->phy_reset != NULL) {
if (hw->fw_ops->phy_reset(hw) != 0)
return;
aq_dev->thermal_settle = 0;
aq_dev->thermal_state = AQ_THERMAL_SETTLING;
return;
}
/* No PHY reset needed (A2): re-init below restores the link. */
break;
case AQ_THERMAL_SETTLING:
if (++aq_dev->thermal_settle < AQ_THERMAL_SETTLE_POLLS)
return;
break;
}
/* Space attempts out: recovery costs a re-init and a renegotiation. */
if ((int)(ticks - aq_dev->thermal_retry_ticks) < 0)
return;
aq_dev->thermal_retry_ticks = ticks + AQ_THERMAL_RETRY_SECS * hz;
device_printf(aq_dev->dev, "PHY cooled to %d C; restoring "
"link\n", aq_dev->thermal_temp_mc / 1000);
aq_dev->thermal_state = AQ_THERMAL_NORMAL;
aq_dev->reset_pending = true;
iflib_request_reset(aq_dev->ctx);
iflib_admin_intr_deferred(aq_dev->ctx);
}
static const char *
aq_fc_string(const struct aq_hw_fc_info *fc)
{
if (fc->fc_rx && fc->fc_tx)
return ("rx/tx");
if (fc->fc_rx)
return ("rx");
if (fc->fc_tx)
return ("tx");
return ("none");
}
static void
aq_link_report_up(struct aq_dev *aq_dev, uint32_t link_speed,
const struct aq_hw_fc_info *fc)
{
struct aq_hw *hw = &aq_dev->hw;
struct aq_hw_link_info info;
if (hw->fw_ops->get_link_info != NULL &&
hw->fw_ops->get_link_info(hw, &info) == 0) {
device_printf(aq_dev->dev, "link UP: speed=%u, %s-duplex, "
"flowcontrol %s, EEE %s\n", link_speed,
info.full_duplex ? "full" : "half", aq_fc_string(fc),
info.eee ? "on" : "off");
return;
}
device_printf(aq_dev->dev, "link UP: speed=%u, flowcontrol %s\n",
link_speed, aq_fc_string(fc));
}
/*
* The firmware raises a PHY fault a poll after it drops the link, so a
* thermal trip usually reports only its temperature here; aq_thermal_poll()
* names it on the following poll.
*/
static void
aq_link_report_down(struct aq_dev *aq_dev)
{
struct aq_hw *hw = &aq_dev->hw;
struct aq_hw_link_info info;
struct sbuf sb;
char detail[96];
int temp_mc;
uint16_t fault;
sbuf_new(&sb, detail, sizeof(detail), SBUF_FIXEDLEN);
if (hw->fw_ops->get_phy_fault != NULL &&
hw->fw_ops->get_phy_fault(hw, &fault) == 0 && fault != 0)
sbuf_printf(&sb, ", PHY fault 0x%04x", fault);
if (hw->fw_ops->get_link_info != NULL &&
hw->fw_ops->get_link_info(hw, &info) == 0)
sbuf_printf(&sb, ", F/W link state %u", info.state);
if (hw->fw_ops->get_temp != NULL &&
hw->fw_ops->get_temp(hw, &temp_mc) == 0)
sbuf_printf(&sb, ", temp %d C", temp_mc / 1000);
if (sbuf_finish(&sb) != 0)
device_printf(aq_dev->dev, "link DOWN\n");
else
device_printf(aq_dev->dev, "link DOWN%s\n", sbuf_data(&sb));
sbuf_delete(&sb);
}
void
aq_if_update_admin_status(if_ctx_t ctx)
{
struct aq_dev *aq_dev = iflib_get_softc(ctx);
struct aq_hw *hw = &aq_dev->hw;
uint32_t link_speed;
bool running;
struct aq_hw_fc_info fc_neg;
int err;
err = aq_hw_get_link_state(hw, &link_speed, &fc_neg);
if (err != 0 && !aq_dev->link_read_failed) {
aq_dev->link_read_failed = true;
device_printf(aq_dev->dev, "link state read failed, error %d; "
"holding the last known state\n", err);
} else if (err == 0 && aq_dev->link_read_failed) {
aq_dev->link_read_failed = false;
device_printf(aq_dev->dev, "link state read recovered\n");
}
/* A stopped or half-initialized interface has no link. */
running = iflib_is_running(ctx);
if (!running || aq_dev->init_failed)
link_speed = 0;
/* A retrain can change the speed without ever dropping the link. */
if (err == 0 && link_speed != 0U &&
(!aq_dev->linkup || link_speed != aq_dev->link_speed)) {
if (!aq_dev->linkup) {
aq_link_report_up(aq_dev, link_speed, &fc_neg);
aq_dev->phy_fault_last = 0;
} else
device_printf(aq_dev->dev, "link speed=%d\n",
link_speed);
aq_dev->linkup = 1;
aq_dev->link_speed = link_speed;
/* turn on/off RX Pause in RPB */
rpb_rx_xoff_en_per_tc_set(hw, fc_neg.fc_rx, 0);
iflib_link_state_change(ctx, LINK_STATE_UP, IF_Mbps(link_speed));
aq_mediastatus_update(aq_dev, link_speed, &fc_neg);
/* update ITR settings according new link speed */
if (aq_hw_interrupt_moderation_set(hw) != 0)
device_printf(aq_dev->dev,
"could not update interrupt moderation\n");
} else if (err == 0 && link_speed == 0U && aq_dev->linkup) {
aq_link_report_down(aq_dev);
aq_dev->linkup = 0;
aq_dev->link_speed = 0;
/* turn off RX Pause in RPB */
rpb_rx_xoff_en_per_tc_set(hw, 0, 0);
iflib_link_state_change(ctx, LINK_STATE_DOWN, 0);
aq_mediastatus_update(aq_dev, link_speed, &fc_neg);
}
/* A stopped interface must not be re-initialized behind the operator. */
if (!running)
return;
/* Re-arming while a reset is queued would re-init once too often. */
if (aq_dev->init_failed) {
if (aq_dev->reset_pending)
return;
if (aq_dev->init_retries < AQ_INIT_MAX_RETRIES) {
aq_dev->init_retries++;
aq_dev->reset_pending = true;
iflib_request_reset(ctx);
} else if (aq_dev->init_retries == AQ_INIT_MAX_RETRIES) {
aq_dev->init_retries++;
device_printf(aq_dev->dev, "initialization failed; "
"giving up after %d retries, link held down\n",
AQ_INIT_MAX_RETRIES);
}
return;
}
if (hw->fw_ops->get_phy_fault != NULL)
aq_thermal_poll(aq_dev);
aq_update_hw_stats(aq_dev);
}
/**************************************************************************/
/* interrupt service routine (Top half) */
/**************************************************************************/
int
aq_isr_rx(void *arg)
{
struct aq_ring *ring = arg;
struct aq_dev *aq_dev = ring->dev;
struct aq_hw *hw = &aq_dev->hw;
itr_irq_status_clearlsw_set(hw, BIT(ring->msix));
AQ_HW_FLUSH(hw);
counter_u64_add(ring->stats.irq, 1);
return (FILTER_SCHEDULE_THREAD);
}
/**************************************************************************/
/* interrupt service routine (Top half) */
/**************************************************************************/
int
aq_linkstat_isr(void *arg)
{
struct aq_dev *aq_dev = arg;
struct aq_hw *hw = &aq_dev->hw;
itr_irq_status_clearlsw_set(hw, BIT(aq_dev->msix));
AQ_HW_FLUSH(hw);
iflib_admin_intr_deferred(aq_dev->ctx);
return (FILTER_HANDLED);
}
|