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#include "host.h"
57
58#define SCIC_SDS_MPC_RECONFIGURATION_TIMEOUT (10)
59#define SCIC_SDS_APC_RECONFIGURATION_TIMEOUT (10)
60#define SCIC_SDS_APC_WAIT_LINK_UP_NOTIFICATION (1000)
61
62enum SCIC_SDS_APC_ACTIVITY {
63 SCIC_SDS_APC_SKIP_PHY,
64 SCIC_SDS_APC_ADD_PHY,
65 SCIC_SDS_APC_START_TIMER,
66
67 SCIC_SDS_APC_ACTIVITY_MAX
68};
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86static s32 sci_sas_address_compare(
87 struct sci_sas_address address_one,
88 struct sci_sas_address address_two)
89{
90 if (address_one.high > address_two.high) {
91 return 1;
92 } else if (address_one.high < address_two.high) {
93 return -1;
94 } else if (address_one.low > address_two.low) {
95 return 1;
96 } else if (address_one.low < address_two.low) {
97 return -1;
98 }
99
100
101 return 0;
102}
103
104
105
106
107
108
109
110
111
112
113
114
115static struct isci_port *sci_port_configuration_agent_find_port(
116 struct isci_host *ihost,
117 struct isci_phy *iphy)
118{
119 u8 i;
120 struct sci_sas_address port_sas_address;
121 struct sci_sas_address port_attached_device_address;
122 struct sci_sas_address phy_sas_address;
123 struct sci_sas_address phy_attached_device_address;
124
125
126
127
128
129
130 sci_phy_get_sas_address(iphy, &phy_sas_address);
131 sci_phy_get_attached_sas_address(iphy, &phy_attached_device_address);
132
133 for (i = 0; i < ihost->logical_port_entries; i++) {
134 struct isci_port *iport = &ihost->ports[i];
135
136 sci_port_get_sas_address(iport, &port_sas_address);
137 sci_port_get_attached_sas_address(iport, &port_attached_device_address);
138
139 if (sci_sas_address_compare(port_sas_address, phy_sas_address) == 0 &&
140 sci_sas_address_compare(port_attached_device_address, phy_attached_device_address) == 0)
141 return iport;
142 }
143
144 return NULL;
145}
146
147
148
149
150
151
152
153
154
155
156
157
158
159static enum sci_status sci_port_configuration_agent_validate_ports(
160 struct isci_host *ihost,
161 struct sci_port_configuration_agent *port_agent)
162{
163 struct sci_sas_address first_address;
164 struct sci_sas_address second_address;
165
166
167
168
169 if (port_agent->phy_valid_port_range[0].max_index != 0 ||
170 port_agent->phy_valid_port_range[1].max_index != 1 ||
171 port_agent->phy_valid_port_range[2].max_index != 2 ||
172 port_agent->phy_valid_port_range[3].max_index != 3)
173 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
174
175
176
177
178 if (port_agent->phy_valid_port_range[0].min_index == 0 &&
179 port_agent->phy_valid_port_range[1].min_index == 0 &&
180 port_agent->phy_valid_port_range[2].min_index == 0 &&
181 port_agent->phy_valid_port_range[3].min_index == 0)
182 return SCI_SUCCESS;
183
184
185
186
187
188
189 if (port_agent->phy_valid_port_range[2].min_index == 1) {
190 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
191 }
192
193
194
195
196
197 sci_phy_get_sas_address(&ihost->phys[0], &first_address);
198 sci_phy_get_sas_address(&ihost->phys[3], &second_address);
199
200 if (sci_sas_address_compare(first_address, second_address) == 0) {
201 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
202 }
203
204
205
206
207
208 if (port_agent->phy_valid_port_range[0].min_index == 0 &&
209 port_agent->phy_valid_port_range[1].min_index == 1) {
210 sci_phy_get_sas_address(&ihost->phys[0], &first_address);
211 sci_phy_get_sas_address(&ihost->phys[2], &second_address);
212
213 if (sci_sas_address_compare(first_address, second_address) == 0) {
214 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
215 }
216 }
217
218
219
220
221
222 if (port_agent->phy_valid_port_range[2].min_index == 2 &&
223 port_agent->phy_valid_port_range[3].min_index == 3) {
224 sci_phy_get_sas_address(&ihost->phys[1], &first_address);
225 sci_phy_get_sas_address(&ihost->phys[3], &second_address);
226
227 if (sci_sas_address_compare(first_address, second_address) == 0) {
228 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
229 }
230 }
231
232 return SCI_SUCCESS;
233}
234
235
236
237
238
239
240
241static enum sci_status
242sci_mpc_agent_validate_phy_configuration(struct isci_host *ihost,
243 struct sci_port_configuration_agent *port_agent)
244{
245 u32 phy_mask;
246 u32 assigned_phy_mask;
247 struct sci_sas_address sas_address;
248 struct sci_sas_address phy_assigned_address;
249 u8 port_index;
250 u8 phy_index;
251
252 assigned_phy_mask = 0;
253 sas_address.high = 0;
254 sas_address.low = 0;
255
256 for (port_index = 0; port_index < SCI_MAX_PORTS; port_index++) {
257 phy_mask = ihost->oem_parameters.ports[port_index].phy_mask;
258
259 if (!phy_mask)
260 continue;
261
262
263
264 if ((phy_mask & ~assigned_phy_mask) == 0) {
265 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
266 }
267
268
269 for (phy_index = 0; phy_index < SCI_MAX_PHYS; phy_index++) {
270 if ((phy_mask & (1 << phy_index)) == 0)
271 continue;
272 sci_phy_get_sas_address(&ihost->phys[phy_index],
273 &sas_address);
274
275
276
277
278
279 port_agent->phy_valid_port_range[phy_index].min_index = port_index;
280 port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
281
282 if (phy_index != port_index) {
283 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
284 }
285
286 break;
287 }
288
289
290
291
292
293
294 while (phy_index < SCI_MAX_PHYS) {
295 if ((phy_mask & (1 << phy_index)) == 0)
296 continue;
297 sci_phy_get_sas_address(&ihost->phys[phy_index],
298 &phy_assigned_address);
299
300 if (sci_sas_address_compare(sas_address, phy_assigned_address) != 0) {
301
302
303
304 return SCI_FAILURE_UNSUPPORTED_PORT_CONFIGURATION;
305 }
306
307 port_agent->phy_valid_port_range[phy_index].min_index = port_index;
308 port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
309
310 sci_port_add_phy(&ihost->ports[port_index],
311 &ihost->phys[phy_index]);
312
313 assigned_phy_mask |= (1 << phy_index);
314 phy_index++;
315 }
316
317 }
318
319 return sci_port_configuration_agent_validate_ports(ihost, port_agent);
320}
321
322static void mpc_agent_timeout(unsigned long data)
323{
324 u8 index;
325 struct sci_timer *tmr = (struct sci_timer *)data;
326 struct sci_port_configuration_agent *port_agent;
327 struct isci_host *ihost;
328 unsigned long flags;
329 u16 configure_phy_mask;
330
331 port_agent = container_of(tmr, typeof(*port_agent), timer);
332 ihost = container_of(port_agent, typeof(*ihost), port_agent);
333
334 spin_lock_irqsave(&ihost->scic_lock, flags);
335
336 if (tmr->cancel)
337 goto done;
338
339 port_agent->timer_pending = false;
340
341
342 configure_phy_mask = ~port_agent->phy_configured_mask & port_agent->phy_ready_mask;
343
344 for (index = 0; index < SCI_MAX_PHYS; index++) {
345 struct isci_phy *iphy = &ihost->phys[index];
346
347 if (configure_phy_mask & (1 << index)) {
348 port_agent->link_up_handler(ihost, port_agent,
349 phy_get_non_dummy_port(iphy),
350 iphy);
351 }
352 }
353
354done:
355 spin_unlock_irqrestore(&ihost->scic_lock, flags);
356}
357
358static void sci_mpc_agent_link_up(struct isci_host *ihost,
359 struct sci_port_configuration_agent *port_agent,
360 struct isci_port *iport,
361 struct isci_phy *iphy)
362{
363
364
365
366
367 if (!iport)
368 return;
369
370 port_agent->phy_ready_mask |= (1 << iphy->phy_index);
371 sci_port_link_up(iport, iphy);
372 if ((iport->active_phy_mask & (1 << iphy->phy_index)))
373 port_agent->phy_configured_mask |= (1 << iphy->phy_index);
374}
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393static void sci_mpc_agent_link_down(
394 struct isci_host *ihost,
395 struct sci_port_configuration_agent *port_agent,
396 struct isci_port *iport,
397 struct isci_phy *iphy)
398{
399 if (iport != NULL) {
400
401
402
403
404
405
406
407 port_agent->phy_ready_mask &= ~(1 << iphy->phy_index);
408 port_agent->phy_configured_mask &= ~(1 << iphy->phy_index);
409
410
411
412
413
414
415
416 if ((port_agent->phy_configured_mask == 0x0000) &&
417 (port_agent->phy_ready_mask != 0x0000) &&
418 !port_agent->timer_pending) {
419 port_agent->timer_pending = true;
420
421 sci_mod_timer(&port_agent->timer,
422 SCIC_SDS_MPC_RECONFIGURATION_TIMEOUT);
423 }
424
425 sci_port_link_down(iport, iphy);
426 }
427}
428
429
430
431
432static enum sci_status
433sci_apc_agent_validate_phy_configuration(struct isci_host *ihost,
434 struct sci_port_configuration_agent *port_agent)
435{
436 u8 phy_index;
437 u8 port_index;
438 struct sci_sas_address sas_address;
439 struct sci_sas_address phy_assigned_address;
440
441 phy_index = 0;
442
443 while (phy_index < SCI_MAX_PHYS) {
444 port_index = phy_index;
445
446
447 sci_phy_get_sas_address(&ihost->phys[phy_index],
448 &sas_address);
449
450 while (++phy_index < SCI_MAX_PHYS) {
451 sci_phy_get_sas_address(&ihost->phys[phy_index],
452 &phy_assigned_address);
453
454
455 if (sci_sas_address_compare(sas_address, phy_assigned_address) == 0) {
456 port_agent->phy_valid_port_range[phy_index].min_index = port_index;
457 port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
458 } else {
459 port_agent->phy_valid_port_range[phy_index].min_index = phy_index;
460 port_agent->phy_valid_port_range[phy_index].max_index = phy_index;
461 break;
462 }
463 }
464 }
465
466 return sci_port_configuration_agent_validate_ports(ihost, port_agent);
467}
468
469
470
471
472
473
474
475static void sci_apc_agent_start_timer(struct sci_port_configuration_agent *port_agent,
476 u32 timeout)
477{
478 port_agent->timer_pending = true;
479 sci_mod_timer(&port_agent->timer, timeout);
480}
481
482static void sci_apc_agent_configure_ports(struct isci_host *ihost,
483 struct sci_port_configuration_agent *port_agent,
484 struct isci_phy *iphy,
485 bool start_timer)
486{
487 u8 port_index;
488 enum sci_status status;
489 struct isci_port *iport;
490 enum SCIC_SDS_APC_ACTIVITY apc_activity = SCIC_SDS_APC_SKIP_PHY;
491
492 iport = sci_port_configuration_agent_find_port(ihost, iphy);
493
494 if (iport) {
495 if (sci_port_is_valid_phy_assignment(iport, iphy->phy_index))
496 apc_activity = SCIC_SDS_APC_ADD_PHY;
497 else
498 apc_activity = SCIC_SDS_APC_SKIP_PHY;
499 } else {
500
501
502
503
504
505
506 for (port_index = port_agent->phy_valid_port_range[iphy->phy_index].min_index;
507 port_index <= port_agent->phy_valid_port_range[iphy->phy_index].max_index;
508 port_index++) {
509
510 iport = &ihost->ports[port_index];
511
512
513 if (sci_port_is_valid_phy_assignment(iport, iphy->phy_index)) {
514
515
516
517
518 if (iport->active_phy_mask > (1 << iphy->phy_index)) {
519 apc_activity = SCIC_SDS_APC_SKIP_PHY;
520 break;
521 }
522
523
524
525
526
527 if (iport->physical_port_index == iphy->phy_index) {
528
529
530
531
532
533 if (apc_activity != SCIC_SDS_APC_START_TIMER) {
534 apc_activity = SCIC_SDS_APC_ADD_PHY;
535 }
536
537 break;
538 }
539
540
541
542
543
544 if (iport->active_phy_mask == 0) {
545 apc_activity = SCIC_SDS_APC_START_TIMER;
546 }
547 } else if (iport->active_phy_mask != 0) {
548
549
550
551
552 apc_activity = SCIC_SDS_APC_SKIP_PHY;
553 }
554 }
555 }
556
557
558
559
560
561
562
563
564 if (
565 (start_timer == false)
566 && (apc_activity == SCIC_SDS_APC_START_TIMER)
567 ) {
568 apc_activity = SCIC_SDS_APC_ADD_PHY;
569 }
570
571 switch (apc_activity) {
572 case SCIC_SDS_APC_ADD_PHY:
573 status = sci_port_add_phy(iport, iphy);
574
575 if (status == SCI_SUCCESS) {
576 port_agent->phy_configured_mask |= (1 << iphy->phy_index);
577 }
578 break;
579
580 case SCIC_SDS_APC_START_TIMER:
581 sci_apc_agent_start_timer(port_agent,
582 SCIC_SDS_APC_WAIT_LINK_UP_NOTIFICATION);
583 break;
584
585 case SCIC_SDS_APC_SKIP_PHY:
586 default:
587
588 break;
589 }
590}
591
592
593
594
595
596
597
598
599
600
601
602
603
604static void sci_apc_agent_link_up(struct isci_host *ihost,
605 struct sci_port_configuration_agent *port_agent,
606 struct isci_port *iport,
607 struct isci_phy *iphy)
608{
609 u8 phy_index = iphy->phy_index;
610
611 if (!iport) {
612
613 port_agent->phy_ready_mask |= 1 << phy_index;
614 sci_apc_agent_start_timer(port_agent,
615 SCIC_SDS_APC_WAIT_LINK_UP_NOTIFICATION);
616 } else {
617
618 port_agent->phy_ready_mask |= 1 << phy_index;
619 sci_port_link_up(iport, iphy);
620 }
621}
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636static void sci_apc_agent_link_down(
637 struct isci_host *ihost,
638 struct sci_port_configuration_agent *port_agent,
639 struct isci_port *iport,
640 struct isci_phy *iphy)
641{
642 port_agent->phy_ready_mask &= ~(1 << iphy->phy_index);
643
644 if (!iport)
645 return;
646 if (port_agent->phy_configured_mask & (1 << iphy->phy_index)) {
647 enum sci_status status;
648
649 status = sci_port_remove_phy(iport, iphy);
650
651 if (status == SCI_SUCCESS)
652 port_agent->phy_configured_mask &= ~(1 << iphy->phy_index);
653 }
654}
655
656
657static void apc_agent_timeout(unsigned long data)
658{
659 u32 index;
660 struct sci_timer *tmr = (struct sci_timer *)data;
661 struct sci_port_configuration_agent *port_agent;
662 struct isci_host *ihost;
663 unsigned long flags;
664 u16 configure_phy_mask;
665
666 port_agent = container_of(tmr, typeof(*port_agent), timer);
667 ihost = container_of(port_agent, typeof(*ihost), port_agent);
668
669 spin_lock_irqsave(&ihost->scic_lock, flags);
670
671 if (tmr->cancel)
672 goto done;
673
674 port_agent->timer_pending = false;
675
676 configure_phy_mask = ~port_agent->phy_configured_mask & port_agent->phy_ready_mask;
677
678 if (!configure_phy_mask)
679 goto done;
680
681 for (index = 0; index < SCI_MAX_PHYS; index++) {
682 if ((configure_phy_mask & (1 << index)) == 0)
683 continue;
684
685 sci_apc_agent_configure_ports(ihost, port_agent,
686 &ihost->phys[index], false);
687 }
688
689 if (is_controller_start_complete(ihost))
690 sci_controller_transition_to_ready(ihost, SCI_SUCCESS);
691
692done:
693 spin_unlock_irqrestore(&ihost->scic_lock, flags);
694}
695
696
697
698
699
700
701
702
703
704
705
706
707
708void sci_port_configuration_agent_construct(
709 struct sci_port_configuration_agent *port_agent)
710{
711 u32 index;
712
713 port_agent->phy_configured_mask = 0x00;
714 port_agent->phy_ready_mask = 0x00;
715
716 port_agent->link_up_handler = NULL;
717 port_agent->link_down_handler = NULL;
718
719 port_agent->timer_pending = false;
720
721 for (index = 0; index < SCI_MAX_PORTS; index++) {
722 port_agent->phy_valid_port_range[index].min_index = 0;
723 port_agent->phy_valid_port_range[index].max_index = 0;
724 }
725}
726
727bool is_port_config_apc(struct isci_host *ihost)
728{
729 return ihost->port_agent.link_up_handler == sci_apc_agent_link_up;
730}
731
732enum sci_status sci_port_configuration_agent_initialize(
733 struct isci_host *ihost,
734 struct sci_port_configuration_agent *port_agent)
735{
736 enum sci_status status;
737 enum sci_port_configuration_mode mode;
738
739 mode = ihost->oem_parameters.controller.mode_type;
740
741 if (mode == SCIC_PORT_MANUAL_CONFIGURATION_MODE) {
742 status = sci_mpc_agent_validate_phy_configuration(
743 ihost, port_agent);
744
745 port_agent->link_up_handler = sci_mpc_agent_link_up;
746 port_agent->link_down_handler = sci_mpc_agent_link_down;
747
748 sci_init_timer(&port_agent->timer, mpc_agent_timeout);
749 } else {
750 status = sci_apc_agent_validate_phy_configuration(
751 ihost, port_agent);
752
753 port_agent->link_up_handler = sci_apc_agent_link_up;
754 port_agent->link_down_handler = sci_apc_agent_link_down;
755
756 sci_init_timer(&port_agent->timer, apc_agent_timeout);
757 }
758
759 return status;
760}
761