Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/*-
 * Copyright (c) 2015,2016 Annapurna Labs Ltd. and affiliates
 * All rights reserved.
 *
 * Developed by Semihalf.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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/cdefs.h>
__FBSDID("$FreeBSD$");

#include "al_init_eth_kr.h"
#include "al_serdes.h"

/**
 *  Ethernet
 *  @{
 * @file   al_init_eth_kr.c
 *
 * @brief  auto-negotiation and link training algorithms and state machines
 *
 * The link training algorithm implemented in this file going over the
 * coefficients and looking for the best eye measurement possible for every one
 * of them. it's using state machine to move between the different states.
 * the state machine has 3 parts:
 *	- preparation - waiting till the link partner (lp) will be ready and
 *			change his state to preset.
 *	- measurement (per coefficient) - issue decrement for the coefficient
 *			under control till the eye measurement not increasing
 *			and remains in the optimum.
 *	- completion - indicate the receiver is ready and wait for the lp to
 *		       finish his work.
 */

/* TODO: fix with more reasonable numbers */
/* timeout in mSec before auto-negotiation will be terminated */
#define AL_ETH_KR_AN_TIMEOUT		(500)
#define AL_ETH_KR_EYE_MEASURE_TIMEOUT	(100)
/* timeout in uSec before the process will be terminated */
#define AL_ETH_KR_FRAME_LOCK_TIMEOUT	(500 * 1000)
#define AL_ETH_KR_LT_DONE_TIMEOUT	(500 * 1000)
/* number of times the receiver and transmitter tasks will be called before the
 * algorithm will be terminated */
#define AL_ETH_KR_LT_MAX_ROUNDS		(50000)

/* mac algorithm state machine */
enum al_eth_kr_mac_lt_state {
	TX_INIT = 0,	/* start of all */
	WAIT_BEGIN,	/* wait for initial training lock */
	DO_PRESET,	/* issue PRESET to link partner */
	DO_HOLD,	/* issue HOLD to link partner */
	/* preparation is done, start testing the coefficient. */
	QMEASURE,	/* EyeQ measurement. */
	QCHECK,		/* Check if measurement shows best value. */
	DO_NEXT_TRY,	/* issue DEC command to coeff for next measurement. */
	END_STEPS,	/* perform last steps to go back to optimum. */
	END_STEPS_HOLD,	/* perform last steps HOLD command. */
	COEFF_DONE,	/* done with the current coefficient updates.
			 * Check if another should be done. */
	/* end of training to all coefficients */
	SET_READY,	/* indicate local receiver ready */
	TX_DONE		/* transmit process completed, training can end. */
};

static const char * const al_eth_kr_mac_sm_name[] = {
	"TX_INIT", "WAIT_BEGIN", "DO_PRESET",
	"DO_HOLD", "QMEASURE", "QCHECK",
	"DO_NEXT_TRY", "END_STEPS", "END_STEPS_HOLD",
	"COEFF_DONE", "SET_READY", "TX_DONE"
};

/* Constants used for the measurement. */
enum al_eth_kr_coef {
	AL_ETH_KR_COEF_C_MINUS,
	AL_ETH_KR_COEF_C_ZERO,
	AL_ETH_KR_COEF_C_PLUS,
};

/*
 * test coefficients from COEFF_TO_MANIPULATE to COEFF_TO_MANIPULATE_LAST.
 */
#define COEFF_TO_MANIPULATE AL_ETH_KR_COEF_C_MINUS
#define COEFF_TO_MANIPULATE_LAST AL_ETH_KR_COEF_C_MINUS
#define QARRAY_SIZE	3 /**< how many entries we want in our history array. */

struct al_eth_kr_data {
	struct al_hal_eth_adapter	*adapter;
	struct al_serdes_grp_obj	*serdes_obj;
	enum al_serdes_lane		lane;

	/* Receiver side data */
	struct al_eth_kr_status_report_data status_report; /* report to response */
	struct al_eth_kr_coef_up_data last_lpcoeff; /* last coeff received */

	/* Transmitter side data */
	enum al_eth_kr_mac_lt_state algo_state;	/* Statemachine. */
	unsigned int qarray[QARRAY_SIZE];	/* EyeQ measurements history */
	/* How many entries in the array are valid for compares yet. */
	unsigned int qarray_cnt;
	enum al_eth_kr_coef curr_coeff;
	/*
	 * Status of coefficient during the last
	 * DEC/INC command (before issuing HOLD again).
	 */
	unsigned int coeff_status_step;
	unsigned int end_steps_cnt;     /* Number of end steps needed */
};

static int
al_eth_kr_an_run(struct al_eth_kr_data *kr_data, struct al_eth_an_adv *an_adv,
    struct al_eth_an_adv *an_partner_adv)
{
	int rc;
	boolean_t page_received = FALSE;
	boolean_t an_completed = FALSE;
	boolean_t error = FALSE;
	int timeout = AL_ETH_KR_AN_TIMEOUT;

	rc = al_eth_kr_an_init(kr_data->adapter, an_adv);
	if (rc != 0) {
		al_err("%s %s autonegotiation init failed\n",
		    kr_data->adapter->name, __func__);
		return (rc);
	}

	rc = al_eth_kr_an_start(kr_data->adapter, AL_ETH_AN__LT_LANE_0,
	    FALSE, TRUE);
	if (rc != 0) {
		al_err("%s %s autonegotiation enable failed\n",
		    kr_data->adapter->name, __func__);
		return (rc);
	}

	do {
		DELAY(10000);
		timeout -= 10;
		if (timeout <= 0) {
			al_info("%s %s autonegotiation failed on timeout\n",
			    kr_data->adapter->name, __func__);

			return (ETIMEDOUT);
		}

		al_eth_kr_an_status_check(kr_data->adapter, &page_received,
		    &an_completed, &error);
	} while (page_received == FALSE);

	if (error != 0) {
		al_info("%s %s autonegotiation failed (status error)\n",
		    kr_data->adapter->name, __func__);

		return (EIO);
	}

	al_eth_kr_an_read_adv(kr_data->adapter, an_partner_adv);

	al_dbg("%s %s autonegotiation completed. error = %d\n",
	    kr_data->adapter->name, __func__, error);

	return (0);
}

/***************************** receiver side *********************************/
static enum al_eth_kr_cl72_cstate
al_eth_lt_coeff_set(struct al_eth_kr_data *kr_data,
    enum al_serdes_tx_deemph_param param, uint32_t op)
{
	enum al_eth_kr_cl72_cstate status = 0;

	switch (op) {
	case AL_PHY_KR_COEF_UP_HOLD:
		/* no need to update the serdes - return not updated*/
		status = C72_CSTATE_NOT_UPDATED;
		break;
	case AL_PHY_KR_COEF_UP_INC:
		status = C72_CSTATE_UPDATED;

		if (kr_data->serdes_obj->tx_deemph_inc(
					kr_data->serdes_obj,
					kr_data->lane,
					param) == 0)
			status = C72_CSTATE_MAX;
		break;
	case AL_PHY_KR_COEF_UP_DEC:
		status = C72_CSTATE_UPDATED;

		if (kr_data->serdes_obj->tx_deemph_dec(
					kr_data->serdes_obj,
					kr_data->lane,
					param) == 0)
			status = C72_CSTATE_MIN;
		break;
	default: /* 3=reserved */
		break;
	}

	return (status);
}

/*
 * Inspect the received coefficient update request and update all coefficients
 * in the serdes accordingly.
 */
static void
al_eth_coeff_req_handle(struct al_eth_kr_data *kr_data,
    struct al_eth_kr_coef_up_data *lpcoeff)
{
	struct al_eth_kr_status_report_data *report = &kr_data->status_report;

	/* First check for Init and Preset commands. */
	if ((lpcoeff->preset != 0) || (lpcoeff->initialize) != 0) {
		kr_data->serdes_obj->tx_deemph_preset(
					kr_data->serdes_obj,
					kr_data->lane);

		/*
		 * in case of preset c(0) should be set to maximum and both c(1)
		 * and c(-1) should be updated
		 */
		report->c_minus = C72_CSTATE_UPDATED;

		report->c_plus = C72_CSTATE_UPDATED;

		report->c_zero = C72_CSTATE_MAX;

		return;
	}

	/*
	 * in case preset and initialize are false need to perform per
	 * coefficient action.
	 */
	report->c_minus = al_eth_lt_coeff_set(kr_data,
	    AL_SERDES_TX_DEEMP_C_MINUS, lpcoeff->c_minus);

	report->c_zero = al_eth_lt_coeff_set(kr_data,
	    AL_SERDES_TX_DEEMP_C_ZERO, lpcoeff->c_zero);

	report->c_plus = al_eth_lt_coeff_set(kr_data,
	    AL_SERDES_TX_DEEMP_C_PLUS, lpcoeff->c_plus);

	al_dbg("%s: c(0) = 0x%x c(-1) = 0x%x c(1) = 0x%x\n",
	    __func__, report->c_zero, report->c_plus, report->c_minus);
}

static void
al_eth_kr_lt_receiver_task_init(struct al_eth_kr_data *kr_data)
{

	al_memset(&kr_data->last_lpcoeff, 0,
	    sizeof(struct al_eth_kr_coef_up_data));
	al_memset(&kr_data->status_report, 0,
	    sizeof(struct al_eth_kr_status_report_data));
}

static boolean_t
al_eth_lp_coeff_up_change(struct al_eth_kr_data *kr_data,
    struct al_eth_kr_coef_up_data *lpcoeff)
{
	struct al_eth_kr_coef_up_data *last_lpcoeff = &kr_data->last_lpcoeff;

	if (al_memcmp(last_lpcoeff, lpcoeff,
	    sizeof(struct al_eth_kr_coef_up_data)) == 0) {
		return (FALSE);
	}

	al_memcpy(last_lpcoeff, lpcoeff, sizeof(struct al_eth_kr_coef_up_data));

	return (TRUE);
}

/*
 * Run the receiver task for one cycle.
 * The receiver task continuously inspects the received coefficient update
 * requests and acts upon.
 *
 * @return <0 if error occur
 */
static int
al_eth_kr_lt_receiver_task_run(struct al_eth_kr_data *kr_data)
{
	struct al_eth_kr_coef_up_data new_lpcoeff;

	/*
	 * First inspect status of the link. It may have dropped frame lock as
	 * the remote did some reconfiguration of its serdes.
	 * Then we simply have nothing to do and return immediately as caller
	 * will call us continuously until lock comes back.
	 */

	if (al_eth_kr_receiver_frame_lock_get(kr_data->adapter,
	    AL_ETH_AN__LT_LANE_0) != 0) {
		return (0);
	}

	/* check if a new update command was received */
	al_eth_lp_coeff_up_get(kr_data->adapter,
	    AL_ETH_AN__LT_LANE_0, &new_lpcoeff);

	if (al_eth_lp_coeff_up_change(kr_data, &new_lpcoeff) != 0) {
		/* got some new coefficient update request. */
		al_eth_coeff_req_handle(kr_data, &new_lpcoeff);
	}

	return (0);
}

/******************************** transmitter side ***************************/
static int
al_eth_kr_lt_transmitter_task_init(struct al_eth_kr_data *kr_data)
{
	int i;
	int rc;
	unsigned int temp_val;

	for (i = 0; i < QARRAY_SIZE; i++)
		kr_data->qarray[i] = 0;

	kr_data->qarray_cnt = 0;
	kr_data->algo_state = TX_INIT;
	kr_data->curr_coeff = COEFF_TO_MANIPULATE;  /* first coeff to test. */
	kr_data->coeff_status_step  = C72_CSTATE_NOT_UPDATED;
	kr_data->end_steps_cnt = QARRAY_SIZE-1;  /* go back to first entry */

	/*
	 * Perform measure eye here to run the rx equalizer
	 * for the first time to get init values
	 */
	rc = kr_data->serdes_obj->eye_measure_run(
				kr_data->serdes_obj,
				kr_data->lane,
				AL_ETH_KR_EYE_MEASURE_TIMEOUT,
				&temp_val);
	if (rc != 0) {
		al_warn("%s: Failed to run Rx equalizer (rc = 0x%x)\n",
		    __func__, rc);

		return (rc);
	}

	return (0);
}

static boolean_t
al_eth_kr_lt_all_not_updated(struct al_eth_kr_status_report_data *report)
{

	if ((report->c_zero == C72_CSTATE_NOT_UPDATED) &&
	    (report->c_minus == C72_CSTATE_NOT_UPDATED) &&
	    (report->c_plus == C72_CSTATE_NOT_UPDATED)) {
		return (TRUE);
	}

	return (FALSE);
}

static void
al_eth_kr_lt_coef_set(struct al_eth_kr_coef_up_data *ldcoeff,
    enum al_eth_kr_coef coef, enum al_eth_kr_cl72_coef_op op)
{

	switch (coef) {
	case AL_ETH_KR_COEF_C_MINUS:
		ldcoeff->c_minus = op;
		break;
	case AL_ETH_KR_COEF_C_PLUS:
		ldcoeff->c_plus = op;
		break;
	case AL_ETH_KR_COEF_C_ZERO:
		ldcoeff->c_zero = op;
		break;
	}
}

static enum al_eth_kr_cl72_cstate
al_eth_kr_lt_coef_report_get(struct al_eth_kr_status_report_data *report,
    enum al_eth_kr_coef coef)
{

	switch (coef) {
	case AL_ETH_KR_COEF_C_MINUS:
		return (report->c_minus);
	case AL_ETH_KR_COEF_C_PLUS:
		return (report->c_plus);
	case AL_ETH_KR_COEF_C_ZERO:
		return (report->c_zero);
	}

	return (0);
}

/*
 * Run the transmitter_task for one cycle.
 *
 * @return <0 if error occurs
 */
static int
al_eth_kr_lt_transmitter_task_run(struct al_eth_kr_data *kr_data)
{
	struct al_eth_kr_status_report_data report;
	unsigned int coeff_status_cur;
	struct al_eth_kr_coef_up_data ldcoeff = { 0, 0, 0, 0, 0 };
	unsigned int val;
	int i;
	enum al_eth_kr_mac_lt_state nextstate;
	int rc = 0;

	/*
	 * do nothing if currently there is no frame lock (which may happen
	 * when remote updates its analogs).
	 */
	if (al_eth_kr_receiver_frame_lock_get(kr_data->adapter,
	    AL_ETH_AN__LT_LANE_0) == 0) {
		return (0);
	}

	al_eth_lp_status_report_get(kr_data->adapter,
	    AL_ETH_AN__LT_LANE_0, &report);

	/* extract curr status of the coefficient in use */
	coeff_status_cur = al_eth_kr_lt_coef_report_get(&report,
	    kr_data->curr_coeff);

	nextstate = kr_data->algo_state; /* default we stay in curr state; */

	switch (kr_data->algo_state) {
	case TX_INIT:
		/* waiting for start */
		if (al_eth_kr_startup_proto_prog_get(kr_data->adapter,
		    AL_ETH_AN__LT_LANE_0) != 0) {
			/* training is on and frame lock */
			nextstate = WAIT_BEGIN;
		}
		break;
	case WAIT_BEGIN:
		kr_data->qarray_cnt = 0;
		kr_data->curr_coeff = COEFF_TO_MANIPULATE;
		kr_data->coeff_status_step = C72_CSTATE_NOT_UPDATED;
		coeff_status_cur = C72_CSTATE_NOT_UPDATED;
		kr_data->end_steps_cnt = QARRAY_SIZE-1;

		/* Wait for not_updated for all coefficients from remote */
		if (al_eth_kr_lt_all_not_updated(&report) != 0) {
			ldcoeff.preset = TRUE;
			nextstate = DO_PRESET;
		}
		break;
	case DO_PRESET:
		/*
		 * Send PRESET and wait for for updated for all
		 * coefficients from remote
		 */
		if (al_eth_kr_lt_all_not_updated(&report) == 0)
			nextstate = DO_HOLD;
		else /* as long as the lp didn't response to the preset
		      * we should continue sending it */
			ldcoeff.preset = TRUE;
		break;
	case DO_HOLD:
		/*
		 * clear the PRESET, issue HOLD command and wait for
		 * hold handshake
		 */
		if (al_eth_kr_lt_all_not_updated(&report) != 0)
			nextstate = QMEASURE;
		break;

	case QMEASURE:
		/* makes a measurement and fills the new value into the array */
		rc = kr_data->serdes_obj->eye_measure_run(
					kr_data->serdes_obj,
					kr_data->lane,
					AL_ETH_KR_EYE_MEASURE_TIMEOUT,
					&val);
		if (rc != 0) {
			al_warn("%s: Rx eye measurement failed\n", __func__);

			return (rc);
		}

		al_dbg("%s: Rx Measure eye returned 0x%x\n", __func__, val);

		/* put the new value into the array at the top. */
		for (i = 0; i < QARRAY_SIZE-1; i++)
			kr_data->qarray[i] = kr_data->qarray[i+1];

		kr_data->qarray[QARRAY_SIZE-1] = val;

		if (kr_data->qarray_cnt < QARRAY_SIZE)
			kr_data->qarray_cnt++;

		nextstate = QCHECK;
		break;
	case QCHECK:
		/* check if we reached the best link quality yet. */
		if (kr_data->qarray_cnt < QARRAY_SIZE) {
			/* keep going until at least the history is
			 * filled. check that we can keep going or if
			 * coefficient has already reached minimum.
			 */

			if (kr_data->coeff_status_step == C72_CSTATE_MIN)
				nextstate = COEFF_DONE;
			else {
				/*
				 * request a DECREMENT of the
				 * coefficient under control
				 */
				al_eth_kr_lt_coef_set(&ldcoeff,
				    kr_data->curr_coeff, AL_PHY_KR_COEF_UP_DEC);

				nextstate = DO_NEXT_TRY;
			}
		} else {
			/*
			 * check if current value and last both are worse than
			 * the 2nd last. This we take as an ending condition
			 * assuming the minimum was reached two tries before
			 * so we will now go back to that point.
			 */
			if ((kr_data->qarray[0] < kr_data->qarray[1]) &&
			    (kr_data->qarray[0] < kr_data->qarray[2])) {
				/*
				 * request a INCREMENT of the
				 * coefficient under control
				 */
				al_eth_kr_lt_coef_set(&ldcoeff,
				    kr_data->curr_coeff, AL_PHY_KR_COEF_UP_INC);

				/* start going back to the maximum */
				nextstate = END_STEPS;
				if (kr_data->end_steps_cnt > 0)
					kr_data->end_steps_cnt--;
			} else {
				if (kr_data->coeff_status_step ==
				    C72_CSTATE_MIN) {
					nextstate = COEFF_DONE;
				} else {
					/*
					 * request a DECREMENT of the
					 * coefficient under control
					 */
					al_eth_kr_lt_coef_set(&ldcoeff,
					    kr_data->curr_coeff,
					    AL_PHY_KR_COEF_UP_DEC);

					nextstate = DO_NEXT_TRY;
				}
			}
		}
		break;
	case DO_NEXT_TRY:
		/*
		 * save the status when we issue the DEC step to the remote,
		 * before the HOLD is done again.
		 */
		kr_data->coeff_status_step = coeff_status_cur;

		if (coeff_status_cur != C72_CSTATE_NOT_UPDATED)
			nextstate = DO_HOLD;  /* go to next measurement round */
		else
			al_eth_kr_lt_coef_set(&ldcoeff,
			    kr_data->curr_coeff, AL_PHY_KR_COEF_UP_DEC);
		break;
	/*
	 * Coefficient iteration completed, go back to the optimum step
	 * In this algorithm we assume 2 before curr was best hence need to do
	 * two INC runs.
	 */
	case END_STEPS:
		if (coeff_status_cur != C72_CSTATE_NOT_UPDATED)
			nextstate = END_STEPS_HOLD;
		else
			al_eth_kr_lt_coef_set(&ldcoeff,
			    kr_data->curr_coeff, AL_PHY_KR_COEF_UP_INC);
		break;
	case END_STEPS_HOLD:
		if (coeff_status_cur == C72_CSTATE_NOT_UPDATED) {
			if (kr_data->end_steps_cnt != 0) {
				/*
				 * request a INCREMENT of the
				 * coefficient under control
				 */
				al_eth_kr_lt_coef_set(&ldcoeff,
				    kr_data->curr_coeff, AL_PHY_KR_COEF_UP_INC);

				/* go 2nd time - dec the end step count */
				nextstate = END_STEPS;

				if (kr_data->end_steps_cnt > 0)
					kr_data->end_steps_cnt--;

			} else {
				nextstate = COEFF_DONE;
			}
		}
		break;
	case COEFF_DONE:
		/*
		 * now this coefficient is done.
		 * We can now either choose to finish here,
		 * or keep going with another coefficient.
		 */
		if ((int)kr_data->curr_coeff < COEFF_TO_MANIPULATE_LAST) {
			int i;

			for (i = 0; i < QARRAY_SIZE; i++)
				kr_data->qarray[i] = 0;

			kr_data->qarray_cnt = 0;
			kr_data->end_steps_cnt = QARRAY_SIZE-1;
			kr_data->coeff_status_step = C72_CSTATE_NOT_UPDATED;
			kr_data->curr_coeff++;

			al_dbg("[%s]: doing next coefficient: %d ---\n\n",
			    kr_data->adapter->name, kr_data->curr_coeff);

			nextstate = QMEASURE;
		} else {
			nextstate = SET_READY;
		}
		break;
	case SET_READY:
		/*
		 * our receiver is ready for data.
		 * no training will occur any more.
		 */
		kr_data->status_report.receiver_ready = TRUE;
		/*
		 * in addition to the status we transmit, we also must tell our
		 * local hardware state-machine that we are done, so the
		 * training can eventually complete when the remote indicates
		 * it is ready also. The hardware will then automatically
		 * give control to the PCS layer completing training.
		 */
		al_eth_receiver_ready_set(kr_data->adapter,
		    AL_ETH_AN__LT_LANE_0);

		nextstate = TX_DONE;
		break;
	case TX_DONE:
		break;  /* nothing else to do */
	default:
		nextstate = kr_data->algo_state;
		break;
	}

	/*
	 * The status we want to transmit to remote.
	 * Note that the status combines the receiver status of all coefficients
	 * with the transmitter's rx ready status.
	 */
	if (kr_data->algo_state != nextstate) {
		al_dbg("[%s] [al_eth_kr_lt_transmit_run] STM changes %s -> %s: "
		    " Qarray=%d/%d/%d\n", kr_data->adapter->name,
		    al_eth_kr_mac_sm_name[kr_data->algo_state],
		    al_eth_kr_mac_sm_name[nextstate],
		    kr_data->qarray[0], kr_data->qarray[1], kr_data->qarray[2]);
	}

	kr_data->algo_state = nextstate;

	/*
	 * write fields for transmission into hardware.
	 * Important: this must be done always, as the receiver may have
	 * received update commands and wants to return its status.
	 */
	al_eth_ld_coeff_up_set(kr_data->adapter, AL_ETH_AN__LT_LANE_0, &ldcoeff);
	al_eth_ld_status_report_set(kr_data->adapter, AL_ETH_AN__LT_LANE_0,
	    &kr_data->status_report);

	return (0);
}

/*****************************************************************************/
static int
al_eth_kr_run_lt(struct al_eth_kr_data *kr_data)
{
	unsigned int cnt;
	int ret = 0;
	boolean_t page_received = FALSE;
	boolean_t an_completed = FALSE;
	boolean_t error = FALSE;
	boolean_t training_failure = FALSE;

	al_eth_kr_lt_initialize(kr_data->adapter, AL_ETH_AN__LT_LANE_0);

	if (al_eth_kr_lt_frame_lock_wait(kr_data->adapter, AL_ETH_AN__LT_LANE_0,
	    AL_ETH_KR_FRAME_LOCK_TIMEOUT) == TRUE) {
		/*
		 * when locked, for the first time initialize the receiver and
		 * transmitter tasks to prepare it for detecting coefficient
		 * update requests.
		 */
		al_eth_kr_lt_receiver_task_init(kr_data);
		ret = al_eth_kr_lt_transmitter_task_init(kr_data);
		if (ret != 0)
			goto error;

		cnt = 0;
		do {
			ret = al_eth_kr_lt_receiver_task_run(kr_data);
			if (ret != 0)
				break; /* stop the link training */

			ret = al_eth_kr_lt_transmitter_task_run(kr_data);
			if (ret != 0)
				break;  /* stop the link training */

			cnt++;
			DELAY(100);

		} while ((al_eth_kr_startup_proto_prog_get(kr_data->adapter,
		    AL_ETH_AN__LT_LANE_0)) && (cnt <= AL_ETH_KR_LT_MAX_ROUNDS));

		training_failure =
		    al_eth_kr_training_status_fail_get(kr_data->adapter,
		    AL_ETH_AN__LT_LANE_0);
		al_dbg("[%s] training ended after %d rounds, failed = %s\n",
		    kr_data->adapter->name, cnt,
		    (training_failure) ? "Yes" : "No");
		if (training_failure || cnt > AL_ETH_KR_LT_MAX_ROUNDS) {
			al_warn("[%s] Training Fail: status: %s, timeout: %s\n",
			    kr_data->adapter->name,
			    (training_failure) ? "Failed" : "OK",
			    (cnt > AL_ETH_KR_LT_MAX_ROUNDS) ? "Yes" : "No");

			/*
			 * note: link is now disabled,
			 * until training becomes disabled (see below).
			 */
			ret = EIO;
			goto error;
		}

	} else {
		al_info("[%s] FAILED: did not achieve initial frame lock...\n",
		    kr_data->adapter->name);

		ret = EIO;
		goto error;
	}

	/*
	 * ensure to stop link training at the end to allow normal PCS
	 * datapath to operate in case of training failure.
	 */
	al_eth_kr_lt_stop(kr_data->adapter, AL_ETH_AN__LT_LANE_0);

	cnt = AL_ETH_KR_LT_DONE_TIMEOUT;
	while (an_completed == FALSE) {
		al_eth_kr_an_status_check(kr_data->adapter, &page_received,
		    &an_completed, &error);
		DELAY(1);
		if ((cnt--) == 0) {
			al_info("%s: wait for an complete timeout!\n", __func__);
			ret = ETIMEDOUT;
			goto error;
		}
	}

error:
	al_eth_kr_an_stop(kr_data->adapter);

	return (ret);
}

/* execute Autonegotiation process */
int al_eth_an_lt_execute(struct al_hal_eth_adapter	*adapter,
			 struct al_serdes_grp_obj	*serdes_obj,
			 enum al_serdes_lane		lane,
			 struct al_eth_an_adv		*an_adv,
			 struct al_eth_an_adv		*partner_adv)
{
	struct al_eth_kr_data kr_data;
	int rc;
	struct al_serdes_adv_rx_params rx_params;

	al_memset(&kr_data, 0, sizeof(struct al_eth_kr_data));

	kr_data.adapter = adapter;
	kr_data.serdes_obj = serdes_obj;
	kr_data.lane = lane;

	/*
	 * the link training progress will run rx equalization so need to make
	 * sure rx parameters is not been override
	 */
	rx_params.override = FALSE;
	kr_data.serdes_obj->rx_advanced_params_set(
					kr_data.serdes_obj,
					kr_data.lane,
					&rx_params);

	rc = al_eth_kr_an_run(&kr_data, an_adv, partner_adv);
	if (rc != 0) {
		al_eth_kr_lt_stop(adapter, AL_ETH_AN__LT_LANE_0);
		al_eth_kr_an_stop(adapter);
		al_dbg("%s: auto-negotiation failed!\n", __func__);
		return (rc);
	}

	if (partner_adv->technology != AL_ETH_AN_TECH_10GBASE_KR) {
		al_eth_kr_lt_stop(adapter, AL_ETH_AN__LT_LANE_0);
		al_eth_kr_an_stop(adapter);
		al_dbg("%s: link partner isn't 10GBASE_KR.\n", __func__);
		return (rc);
	}

	rc = al_eth_kr_run_lt(&kr_data);
	if (rc != 0) {
		al_eth_kr_lt_stop(adapter, AL_ETH_AN__LT_LANE_0);
		al_eth_kr_an_stop(adapter);
		al_dbg("%s: Link-training failed!\n", __func__);
		return (rc);
	}

	return (0);
}