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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
|
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2026 LeapIO Tech Inc.
*
* LeapRAID storage and RAID controller driver.
*/
#include <scsi/scsi_host.h>
#include "leapraid_func.h"
static struct leapraid_topo_node *leapraid_transport_topo_node_by_sas_addr(
struct leapraid_adapter *adapter,
u64 sas_addr,
struct leapraid_card_port *card_port)
{
if (adapter->dev_topo.card.sas_address == sas_addr)
return &adapter->dev_topo.card;
return leapraid_exp_find_by_sas_address(adapter, sas_addr, card_port);
}
static u8 leapraid_get_port_id_by_expander(struct leapraid_adapter *adapter,
struct sas_rphy *rphy)
{
struct leapraid_topo_node *topo_node_exp;
unsigned long flags;
u8 port_id = 0xFF;
spin_lock_irqsave(&adapter->dev_topo.topo_node_lock, flags);
list_for_each_entry(topo_node_exp, &adapter->dev_topo.exp_list, list) {
if (topo_node_exp->rphy == rphy) {
port_id = topo_node_exp->card_port->port_id;
break;
}
}
spin_unlock_irqrestore(&adapter->dev_topo.topo_node_lock, flags);
return port_id;
}
static u8 leapraid_get_port_id_by_end_dev(struct leapraid_adapter *adapter,
struct sas_rphy *rphy)
{
struct leapraid_sas_dev *sas_dev;
unsigned long flags;
u8 port_id = 0xFF;
spin_lock_irqsave(&adapter->dev_topo.sas_dev_lock, flags);
sas_dev = leapraid_hold_lock_get_sas_dev_by_addr_and_rphy(
adapter,
rphy->identify.sas_address,
rphy);
if (sas_dev) {
port_id = sas_dev->card_port->port_id;
leapraid_sdev_put(sas_dev);
}
spin_unlock_irqrestore(&adapter->dev_topo.sas_dev_lock, flags);
return port_id;
}
static u8 leapraid_transport_get_port_id_by_rphy(
struct leapraid_adapter *adapter,
struct sas_rphy *rphy)
{
if (!rphy)
return 0xFF;
switch (rphy->identify.device_type) {
case SAS_EDGE_EXPANDER_DEVICE:
case SAS_FANOUT_EXPANDER_DEVICE:
return leapraid_get_port_id_by_expander(adapter, rphy);
case SAS_END_DEVICE:
return leapraid_get_port_id_by_end_dev(adapter, rphy);
default:
return 0xFF;
}
}
static enum sas_linkrate leapraid_transport_convert_phy_link_rate(u8 link_rate)
{
unsigned int i;
#define SAS_RATE_12G SAS_LINK_RATE_12_0_GBPS
const struct linkrate_map {
u8 in;
enum sas_linkrate out;
} linkrate_table[] = {
{
LEAPRAID_SAS_NEG_LINK_RATE_1_5,
SAS_LINK_RATE_1_5_GBPS
},
{
LEAPRAID_SAS_NEG_LINK_RATE_3_0,
SAS_LINK_RATE_3_0_GBPS
},
{
LEAPRAID_SAS_NEG_LINK_RATE_6_0,
SAS_LINK_RATE_6_0_GBPS
},
{
LEAPRAID_SAS_NEG_LINK_RATE_12_0,
SAS_RATE_12G
},
{
LEAPRAID_SAS_NEG_LINK_RATE_PHY_DISABLED,
SAS_PHY_DISABLED
},
{
LEAPRAID_SAS_NEG_LINK_RATE_NEGOTIATION_FAILED,
SAS_LINK_RATE_FAILED
},
{
LEAPRAID_SAS_NEG_LINK_RATE_PORT_SELECTOR,
SAS_SATA_PORT_SELECTOR
},
{
LEAPRAID_SAS_NEG_LINK_RATE_SMP_RESETTING,
SAS_LINK_RATE_UNKNOWN
},
{
LEAPRAID_SAS_NEG_LINK_RATE_SATA_OOB_COMPLETE,
SAS_LINK_RATE_UNKNOWN
},
{
LEAPRAID_SAS_NEG_LINK_RATE_UNKNOWN_LINK_RATE,
SAS_LINK_RATE_UNKNOWN
},
};
for (i = 0; i < ARRAY_SIZE(linkrate_table); i++)
if (linkrate_table[i].in == link_rate)
return linkrate_table[i].out;
return SAS_LINK_RATE_UNKNOWN;
}
static void leapraid_set_identify_protocol_flags(u32 dev_info,
struct sas_identify *identify)
{
unsigned int i;
const struct protocol_mapping {
u32 mask;
u32 *target;
u32 protocol;
} mappings[] = {
{
LEAPRAID_DEVTYP_SSP_INIT,
&identify->initiator_port_protocols,
SAS_PROTOCOL_SSP
},
{
LEAPRAID_DEVTYP_STP_INIT,
&identify->initiator_port_protocols,
SAS_PROTOCOL_STP
},
{
LEAPRAID_DEVTYP_SMP_INIT,
&identify->initiator_port_protocols,
SAS_PROTOCOL_SMP
},
{
LEAPRAID_DEVTYP_SATA_HOST,
&identify->initiator_port_protocols,
SAS_PROTOCOL_SATA
},
{
LEAPRAID_DEVTYP_SSP_TGT,
&identify->target_port_protocols,
SAS_PROTOCOL_SSP
},
{
LEAPRAID_DEVTYP_STP_TGT,
&identify->target_port_protocols,
SAS_PROTOCOL_STP
},
{
LEAPRAID_DEVTYP_SMP_TGT,
&identify->target_port_protocols,
SAS_PROTOCOL_SMP
},
{
LEAPRAID_DEVTYP_SATA_DEV,
&identify->target_port_protocols,
SAS_PROTOCOL_SATA
},
};
for (i = 0; i < ARRAY_SIZE(mappings); i++)
if (dev_info & mappings[i].mask && mappings[i].target)
*mappings[i].target |= mappings[i].protocol;
}
static int leapraid_transport_set_identify(struct leapraid_adapter *adapter,
u16 hdl,
struct sas_identify *identify)
{
union cfg_param_1 cfgp1 = {0};
union cfg_param_2 cfgp2 = {0};
struct leapraid_sas_dev_p0 sas_dev_pg0;
u32 dev_info;
if ((adapter->access_ctrl.shost_recovering &&
!adapter->scan_dev_desc.driver_loading) ||
adapter->access_ctrl.pcie_recovering) {
dev_warn(&adapter->pdev->dev,
"%s: Failed, shost_recovering=%d pcie_recovering=%d\n",
__func__,
adapter->access_ctrl.shost_recovering,
adapter->access_ctrl.pcie_recovering);
return -EFAULT;
}
cfgp1.form = LEAPRAID_SAS_DEV_CFG_PGAD_HDL;
cfgp2.handle = hdl;
if (leapraid_op_config_page(adapter, &sas_dev_pg0, cfgp1,
cfgp2, GET_SAS_DEVICE_PG0))
return -ENXIO;
memset(identify, 0, sizeof(struct sas_identify));
dev_info = le32_to_cpu(sas_dev_pg0.dev_info);
identify->sas_address = le64_to_cpu(sas_dev_pg0.sas_address);
identify->phy_identifier = sas_dev_pg0.phy_num;
switch (dev_info & LEAPRAID_DEVTYP_MASK_DEV_TYPE) {
case LEAPRAID_DEVTYP_NO_DEV:
identify->device_type = SAS_PHY_UNUSED;
break;
case LEAPRAID_DEVTYP_END_DEV:
identify->device_type = SAS_END_DEVICE;
break;
case LEAPRAID_DEVTYP_EDGE_EXPANDER:
identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
break;
case LEAPRAID_DEVTYP_FANOUT_EXPANDER:
identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
break;
}
leapraid_set_identify_protocol_flags(dev_info, identify);
return 0;
}
static void leapraid_transport_exp_set_edev(struct leapraid_adapter *adapter,
void *data_out,
struct sas_expander_device *edev)
{
struct leapraid_smp_passthrough_rep *smp_passthrough_rep;
struct leapraid_rep_manu_reply *rep_manu_reply;
u8 *component_id;
smp_passthrough_rep =
(void *)&adapter->driver_cmds.transport_cmd.reply;
if (le16_to_cpu(smp_passthrough_rep->resp_data_len) !=
sizeof(struct leapraid_rep_manu_reply))
return;
rep_manu_reply = data_out + sizeof(struct leapraid_rep_manu_request);
memcpy(edev->vendor_id, rep_manu_reply->vendor_identification,
SAS_EXPANDER_VENDOR_ID_LEN);
edev->vendor_id[SAS_EXPANDER_VENDOR_ID_LEN] = '\0';
memcpy(edev->product_id, rep_manu_reply->product_identification,
SAS_EXPANDER_PRODUCT_ID_LEN);
edev->product_id[SAS_EXPANDER_PRODUCT_ID_LEN] = '\0';
memcpy(edev->product_rev, rep_manu_reply->product_revision_level,
SAS_EXPANDER_PRODUCT_REV_LEN);
edev->product_rev[SAS_EXPANDER_PRODUCT_REV_LEN] = '\0';
edev->level = rep_manu_reply->sas_format & 1;
if (edev->level) {
memcpy(edev->component_vendor_id,
rep_manu_reply->comp_vendor_identification,
SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
edev->component_vendor_id[
SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN] = '\0';
component_id = (u8 *)&rep_manu_reply->component_id;
edev->component_id = component_id[0] << 8 | component_id[1];
edev->component_revision_id =
rep_manu_reply->component_revision_level;
}
}
static int leapraid_transport_exp_report_manu(struct leapraid_adapter *adapter,
u64 sas_address,
struct sas_expander_device *edev,
u8 port_id)
{
struct leapraid_smp_passthrough_req *smp_passthrough_req;
struct leapraid_rep_manu_request *rep_manu_request;
dma_addr_t h2c_dma_addr;
dma_addr_t c2h_dma_addr;
bool issue_reset = false;
void *data_out = NULL;
u16 inter_taskid;
size_t c2h_size;
size_t h2c_size;
void *psge;
int rc;
if (adapter->access_ctrl.shost_recovering ||
adapter->access_ctrl.pcie_recovering) {
dev_warn(&adapter->pdev->dev,
"%s: Failed, shost_recovering=%d pcie_recovering=%d\n",
__func__,
adapter->access_ctrl.shost_recovering,
adapter->access_ctrl.pcie_recovering);
return -EFAULT;
}
mutex_lock(&adapter->driver_cmds.transport_cmd.mutex);
adapter->driver_cmds.transport_cmd.status = LEAPRAID_CMD_PENDING;
rc = leapraid_check_adapter_is_op(adapter, LEAPRAID_DB_WAIT_OP_SHORT,
__func__);
if (rc)
goto out_cleanup;
h2c_size = sizeof(struct leapraid_rep_manu_request);
c2h_size = sizeof(struct leapraid_rep_manu_reply);
data_out = dma_alloc_coherent(&adapter->pdev->dev,
h2c_size + c2h_size,
&h2c_dma_addr,
GFP_ATOMIC);
if (!data_out) {
rc = -ENOMEM;
goto out_cleanup;
}
rep_manu_request = data_out;
rep_manu_request->smp_frame_type =
SMP_REPORT_MANUFACTURER_INFORMATION_FRAME_TYPE;
rep_manu_request->function = SMP_REPORT_MANUFACTURER_INFORMATION_FUNC;
rep_manu_request->allocated_response_length = 0;
rep_manu_request->request_length = 0;
inter_taskid = adapter->driver_cmds.transport_cmd.inter_taskid;
smp_passthrough_req = leapraid_get_task_desc(adapter, inter_taskid);
memset(smp_passthrough_req, 0,
sizeof(struct leapraid_smp_passthrough_req));
smp_passthrough_req->func = LEAPRAID_FUNC_SMP_PASSTHROUGH;
smp_passthrough_req->physical_port = port_id;
smp_passthrough_req->sas_address = cpu_to_le64(sas_address);
smp_passthrough_req->req_data_len = cpu_to_le16(h2c_size);
psge = &smp_passthrough_req->sgl;
c2h_dma_addr = h2c_dma_addr + sizeof(struct leapraid_rep_manu_request);
leapraid_build_ieee_sg(adapter, psge, h2c_dma_addr, h2c_size,
c2h_dma_addr, c2h_size);
init_completion(&adapter->driver_cmds.transport_cmd.done);
leapraid_fire_task(adapter, inter_taskid);
wait_for_completion_timeout(&adapter->driver_cmds.transport_cmd.done,
LEAPRAID_TRANSPORT_CMD_TIMEOUT * HZ);
if (!(adapter->driver_cmds.transport_cmd.status & LEAPRAID_CMD_DONE)) {
rc = -ETIMEDOUT;
dev_err(&adapter->pdev->dev,
"%s: SMP passthrough timeout, st=0x%x\n",
__func__, adapter->driver_cmds.transport_cmd.status);
leapraid_log_req_context(adapter, inter_taskid,
smp_passthrough_req);
if (!(adapter->driver_cmds.transport_cmd.status &
LEAPRAID_CMD_RESET))
issue_reset = true;
goto hard_reset;
}
if (adapter->driver_cmds.transport_cmd.status &
LEAPRAID_CMD_REPLY_VALID)
leapraid_transport_exp_set_edev(adapter, data_out, edev);
hard_reset:
if (issue_reset) {
dev_info(&adapter->pdev->dev, "%s:%d: call hard_reset\n",
__func__, __LINE__);
rc = leapraid_hard_reset_handler(adapter, FULL_RESET);
}
out_cleanup:
adapter->driver_cmds.transport_cmd.status = LEAPRAID_CMD_NOT_USED;
if (data_out)
dma_free_coherent(&adapter->pdev->dev, h2c_size + c2h_size,
data_out, h2c_dma_addr);
mutex_unlock(&adapter->driver_cmds.transport_cmd.mutex);
return rc;
}
static void leapraid_transport_del_port(struct leapraid_adapter *adapter,
struct leapraid_sas_port *sas_port)
{
dev_info(&sas_port->port->dev,
"Remove port: SAS addr=0x%016llx\n",
(unsigned long long)sas_port->remote_identify.sas_address);
switch (sas_port->remote_identify.device_type) {
case SAS_END_DEVICE:
leapraid_sas_dev_remove_by_sas_address(
adapter,
sas_port->remote_identify.sas_address,
sas_port->card_port);
break;
case SAS_EDGE_EXPANDER_DEVICE:
case SAS_FANOUT_EXPANDER_DEVICE:
leapraid_exp_rm(adapter, sas_port->remote_identify.sas_address,
sas_port->card_port);
break;
default:
break;
}
}
static void leapraid_transport_del_phy(struct leapraid_adapter *adapter,
struct leapraid_sas_port *sas_port,
struct leapraid_card_phy *card_phy)
{
dev_info(&card_phy->phy->dev,
"Remove PHY: SAS addr=0x%016llx, phy=%d\n",
(unsigned long long)sas_port->remote_identify.sas_address,
card_phy->phy_id);
list_del(&card_phy->port_siblings);
sas_port->phys_num--;
sas_port_delete_phy(sas_port->port, card_phy->phy);
card_phy->phy_is_assigned = 0;
}
static void leapraid_transport_add_phy(struct leapraid_adapter *adapter,
struct leapraid_sas_port *sas_port,
struct leapraid_card_phy *card_phy)
{
dev_info(&card_phy->phy->dev,
"Add PHY: SAS addr=0x%016llx, phy=%d\n",
(unsigned long long)sas_port->remote_identify.sas_address,
card_phy->phy_id);
list_add_tail(&card_phy->port_siblings, &sas_port->phy_list);
sas_port->phys_num++;
sas_port_add_phy(sas_port->port, card_phy->phy);
card_phy->phy_is_assigned = 1;
}
void leapraid_transport_attach_phy_to_port(
struct leapraid_adapter *adapter,
struct leapraid_topo_node *topo_node,
struct leapraid_card_phy *card_phy,
u64 sas_address,
struct leapraid_card_port *card_port)
{
struct leapraid_sas_port *sas_port;
struct leapraid_card_phy *card_phy_srch;
if (card_phy->phy_is_assigned)
return;
if (!card_port)
return;
list_for_each_entry(sas_port, &topo_node->sas_port_list, port_list) {
if (sas_port->remote_identify.sas_address != sas_address)
continue;
if (sas_port->card_port != card_port)
continue;
list_for_each_entry(card_phy_srch, &sas_port->phy_list,
port_siblings) {
if (card_phy_srch == card_phy)
return;
}
leapraid_transport_add_phy(adapter, sas_port, card_phy);
return;
}
}
void leapraid_transport_detach_phy_to_port(
struct leapraid_adapter *adapter,
struct leapraid_topo_node *topo_node,
struct leapraid_card_phy *target_card_phy)
{
struct leapraid_sas_port *sas_port, *sas_port_next;
struct leapraid_card_phy *cur_card_phy;
if (!target_card_phy->phy_is_assigned)
return;
list_for_each_entry_safe(sas_port, sas_port_next,
&topo_node->sas_port_list, port_list) {
list_for_each_entry(cur_card_phy, &sas_port->phy_list,
port_siblings) {
if (cur_card_phy != target_card_phy)
continue;
if (sas_port->phys_num == 1 &&
!adapter->access_ctrl.shost_recovering)
leapraid_transport_del_port(adapter, sas_port);
else
leapraid_transport_del_phy(adapter, sas_port,
target_card_phy);
return;
}
}
}
static void leapraid_detach_phy_from_old_port(
struct leapraid_adapter *adapter,
struct leapraid_topo_node *topo_node,
u64 sas_address,
struct leapraid_card_port *card_port)
{
int i;
for (i = 0; i < topo_node->phys_num; i++) {
if (topo_node->card_phy[i].remote_identify.sas_address !=
sas_address ||
topo_node->card_phy[i].card_port != card_port)
continue;
if (topo_node->card_phy[i].phy_is_assigned)
leapraid_transport_detach_phy_to_port(
adapter,
topo_node,
&topo_node->card_phy[i]);
}
}
static struct leapraid_sas_port *leapraid_prepare_sas_port(
struct leapraid_adapter *adapter,
u16 handle, u64 sas_address,
struct leapraid_card_port *card_port,
struct leapraid_topo_node **out_topo_node)
{
struct leapraid_topo_node *topo_node;
struct leapraid_sas_port *sas_port;
unsigned long flags;
sas_port = kzalloc_obj(*sas_port);
if (!sas_port) {
dev_warn(&adapter->pdev->dev,
"%s: Failed alloc sas_port\n", __func__);
return NULL;
}
INIT_LIST_HEAD(&sas_port->port_list);
INIT_LIST_HEAD(&sas_port->phy_list);
spin_lock_irqsave(&adapter->dev_topo.topo_node_lock, flags);
topo_node = leapraid_transport_topo_node_by_sas_addr(adapter,
sas_address,
card_port);
spin_unlock_irqrestore(&adapter->dev_topo.topo_node_lock, flags);
if (!topo_node) {
dev_err(&adapter->pdev->dev,
"%s: Failed to locate parent for SAS 0x%016llx!\n",
__func__, sas_address);
goto out_cleanup;
}
if (leapraid_transport_set_identify(adapter, handle,
&sas_port->remote_identify))
goto out_cleanup;
if (sas_port->remote_identify.device_type == SAS_PHY_UNUSED) {
dev_warn(&adapter->pdev->dev,
"%s: Failed, device type is SAS_PHY_UNUSED\n",
__func__);
goto out_cleanup;
}
sas_port->card_port = card_port;
*out_topo_node = topo_node;
return sas_port;
out_cleanup:
kfree(sas_port);
return NULL;
}
static int leapraid_bind_phys_and_vphy(struct leapraid_adapter *adapter,
struct leapraid_sas_port *sas_port,
struct leapraid_topo_node *topo_node,
struct leapraid_card_port *card_port,
struct leapraid_vphy **out_vphy)
{
struct leapraid_vphy *vphy = NULL;
int i;
for (i = 0; i < topo_node->phys_num; i++) {
if (topo_node->card_phy[i].remote_identify.sas_address !=
sas_port->remote_identify.sas_address ||
topo_node->card_phy[i].card_port != card_port)
continue;
list_add_tail(&topo_node->card_phy[i].port_siblings,
&sas_port->phy_list);
sas_port->phys_num++;
if (topo_node->hdl <= adapter->dev_topo.card.phys_num) {
if (!topo_node->card_phy[i].vphy) {
card_port->phy_mask |= BIT(i);
continue;
}
vphy = leapraid_get_vphy_by_phy(card_port, i);
if (!vphy)
return LEAPRAID_OPERATION_FAILED;
}
}
*out_vphy = vphy;
return sas_port->phys_num ? 0 : LEAPRAID_OPERATION_FAILED;
}
static struct sas_rphy *leapraid_create_and_register_rphy(
struct leapraid_adapter *adapter,
struct leapraid_sas_port *sas_port,
struct leapraid_topo_node *topo_node,
struct leapraid_card_port *card_port,
struct leapraid_vphy *vphy)
{
struct leapraid_sas_dev *sas_dev = NULL;
struct leapraid_card_phy *card_phy;
struct sas_port *port;
struct sas_rphy *rphy;
if (sas_port->remote_identify.device_type == SAS_END_DEVICE) {
sas_dev = leapraid_get_sas_dev_by_addr(
adapter,
sas_port->remote_identify.sas_address,
card_port);
if (!sas_dev)
return NULL;
sas_dev->pend_sas_rphy_add = 1;
}
if (!topo_node->parent_dev) {
dev_warn(&adapter->pdev->dev,
"%s: topo_node parent device is NULL\n", __func__);
goto cleanup_sas_dev;
}
port = sas_port_alloc_num(topo_node->parent_dev);
if (!port) {
dev_err(&adapter->pdev->dev,
"%s: Failed to allocate SAS port\n", __func__);
goto cleanup_sas_dev;
}
if (sas_port_add(port)) {
dev_err(&adapter->pdev->dev,
"%s: Failed to add SAS port\n", __func__);
goto out_delete_port;
}
list_for_each_entry(card_phy, &sas_port->phy_list, port_siblings) {
sas_port_add_phy(port, card_phy->phy);
card_phy->phy_is_assigned = 1;
card_phy->card_port = card_port;
}
if (sas_dev) {
rphy = sas_end_device_alloc(port);
sas_dev->rphy = rphy;
if (topo_node->hdl <= adapter->dev_topo.card.phys_num) {
if (!vphy)
card_port->sas_address = sas_dev->sas_addr;
else
vphy->sas_address = sas_dev->sas_addr;
}
} else {
rphy = sas_expander_alloc(
port,
sas_port->remote_identify.device_type);
if (topo_node->hdl <= adapter->dev_topo.card.phys_num)
card_port->sas_address =
sas_port->remote_identify.sas_address;
}
if (!rphy) {
dev_err(&adapter->pdev->dev,
"%s: Failed to allocate RPHY\n", __func__);
goto cleanup_card_phy;
}
rphy->identify = sas_port->remote_identify;
if (sas_rphy_add(rphy)) {
dev_err(&adapter->pdev->dev,
"%s: Failed to add RPHY\n", __func__);
if (sas_dev)
sas_dev->rphy = NULL;
sas_rphy_free(rphy);
goto cleanup_card_phy;
}
sas_port->port = port;
if (sas_dev) {
sas_dev->pend_sas_rphy_add = 0;
leapraid_sdev_put(sas_dev);
}
return rphy;
cleanup_card_phy:
if (topo_node->hdl <= adapter->dev_topo.card.phys_num) {
if (!vphy)
card_port->sas_address = 0;
else
vphy->sas_address = 0;
}
list_for_each_entry(card_phy, &sas_port->phy_list, port_siblings) {
card_phy->phy_is_assigned = 0;
card_phy->card_port = NULL;
}
out_delete_port:
sas_port_delete(port);
cleanup_sas_dev:
if (sas_dev) {
sas_dev->pend_sas_rphy_add = 0;
leapraid_sdev_put(sas_dev);
}
return NULL;
}
struct leapraid_sas_port *leapraid_transport_port_add(
struct leapraid_adapter *adapter,
u16 hdl, u64 sas_address,
struct leapraid_card_port *card_port)
{
struct leapraid_card_phy *card_phy, *card_phy_next;
struct leapraid_topo_node *topo_node = NULL;
struct leapraid_sas_port *sas_port;
struct leapraid_vphy *vphy = NULL;
struct sas_rphy *rphy;
unsigned long flags;
if (!card_port) {
dev_warn(&adapter->pdev->dev,
"%s: Invalid card_port\n", __func__);
return NULL;
}
sas_port = leapraid_prepare_sas_port(adapter, hdl, sas_address,
card_port, &topo_node);
if (!sas_port)
return NULL;
leapraid_detach_phy_from_old_port(
adapter,
topo_node,
sas_port->remote_identify.sas_address,
card_port);
if (leapraid_bind_phys_and_vphy(adapter, sas_port, topo_node,
card_port, &vphy)) {
dev_err(&adapter->pdev->dev,
"%s: Failed to bind phy to vphy\n", __func__);
goto out_fail;
}
rphy = leapraid_create_and_register_rphy(adapter, sas_port, topo_node,
card_port, vphy);
if (!rphy) {
dev_err(&adapter->pdev->dev,
"%s: Failed to create rphy\n", __func__);
goto out_fail;
}
dev_info(&rphy->dev,
"%s: Added dev: hdl=0x%04x, SAS addr=0x%016llx\n",
__func__, hdl,
(unsigned long long)sas_port->remote_identify.sas_address);
sas_port->rphy = rphy;
spin_lock_irqsave(&adapter->dev_topo.topo_node_lock, flags);
list_add_tail(&sas_port->port_list, &topo_node->sas_port_list);
spin_unlock_irqrestore(&adapter->dev_topo.topo_node_lock, flags);
if (sas_port->remote_identify.device_type ==
SAS_EDGE_EXPANDER_DEVICE ||
sas_port->remote_identify.device_type ==
SAS_FANOUT_EXPANDER_DEVICE)
leapraid_transport_exp_report_manu(
adapter,
sas_port->remote_identify.sas_address,
rphy_to_expander_device(rphy),
card_port->port_id);
return sas_port;
out_fail:
list_for_each_entry_safe(card_phy, card_phy_next,
&sas_port->phy_list, port_siblings) {
if (topo_node->hdl <= adapter->dev_topo.card.phys_num &&
!card_phy->vphy)
card_port->phy_mask &= ~BIT(card_phy->phy_id);
list_del_init(&card_phy->port_siblings);
}
kfree(sas_port);
return NULL;
}
static struct leapraid_sas_port *leapraid_find_and_remove_sas_port(
struct leapraid_topo_node *topo_node,
u64 sas_address,
struct leapraid_card_port *remove_card_port,
bool *found)
{
struct leapraid_sas_port *sas_port, *sas_port_next;
list_for_each_entry_safe(sas_port, sas_port_next,
&topo_node->sas_port_list, port_list) {
if (sas_port->remote_identify.sas_address != sas_address)
continue;
if (sas_port->card_port != remove_card_port)
continue;
*found = true;
list_del(&sas_port->port_list);
return sas_port;
}
return NULL;
}
static void leapraid_cleanup_card_port_and_vphys(
struct leapraid_adapter *adapter,
u64 sas_address,
struct leapraid_card_port *remove_card_port)
{
struct leapraid_card_port *card_port, *card_port_next;
struct leapraid_vphy *vphy, *vphy_next;
if (remove_card_port->vphys_mask) {
list_for_each_entry_safe(vphy, vphy_next,
&remove_card_port->vphys_list, list) {
if (vphy->sas_address != sas_address)
continue;
dev_dbg(&adapter->pdev->dev,
"%s: Remove vphy=%p from port=%p port_id=%d\n",
__func__, vphy, remove_card_port,
remove_card_port->port_id);
remove_card_port->vphys_mask &= ~vphy->phy_mask;
list_del(&vphy->list);
kfree(vphy);
}
if (!remove_card_port->vphys_mask &&
!remove_card_port->sas_address) {
dev_dbg(&adapter->pdev->dev,
"%s: Remove empty hba_port: %p, port_id=%d\n",
__func__,
remove_card_port,
remove_card_port->port_id);
list_del(&remove_card_port->list);
kfree(remove_card_port);
remove_card_port = NULL;
}
}
list_for_each_entry_safe(card_port, card_port_next,
&adapter->dev_topo.card_port_list, list) {
if (card_port != remove_card_port)
continue;
if (card_port->sas_address != sas_address)
continue;
if (!remove_card_port->vphys_mask) {
dev_dbg(&adapter->pdev->dev,
"%s: Remove hba_port: %p, port_id=%d\n",
__func__, card_port, card_port->port_id);
list_del(&card_port->list);
kfree(card_port);
} else {
dev_dbg(&adapter->pdev->dev,
"Clear sas_address of port=%p, port_id=%d\n",
card_port, card_port->port_id);
remove_card_port->sas_address = 0;
}
break;
}
}
static void leapraid_clear_topo_node_phys(struct leapraid_topo_node *topo_node,
u64 sas_address)
{
int i;
for (i = 0; i < topo_node->phys_num; i++)
if (topo_node->card_phy[i].remote_identify.sas_address ==
sas_address) {
memset(&topo_node->card_phy[i].remote_identify, 0,
sizeof(struct sas_identify));
topo_node->card_phy[i].vphy = 0;
}
}
void leapraid_transport_port_remove(
struct leapraid_adapter *adapter,
u64 sas_address,
u64 sas_address_parent,
struct leapraid_card_port *remove_card_port)
{
struct leapraid_card_phy *card_phy, *card_phy_next;
struct leapraid_sas_port *sas_port;
struct leapraid_topo_node *topo_node;
unsigned long flags;
bool found = false;
if (!remove_card_port)
return;
spin_lock_irqsave(&adapter->dev_topo.topo_node_lock, flags);
topo_node = leapraid_transport_topo_node_by_sas_addr(adapter,
sas_address_parent,
remove_card_port);
if (!topo_node) {
spin_unlock_irqrestore(&adapter->dev_topo.topo_node_lock,
flags);
return;
}
sas_port = leapraid_find_and_remove_sas_port(topo_node, sas_address,
remove_card_port, &found);
if (!found) {
spin_unlock_irqrestore(&adapter->dev_topo.topo_node_lock,
flags);
return;
}
if (topo_node->hdl <= adapter->dev_topo.card.phys_num &&
adapter->adapter_attr.enable_mp)
leapraid_cleanup_card_port_and_vphys(adapter, sas_address,
remove_card_port);
leapraid_clear_topo_node_phys(topo_node, sas_address);
spin_unlock_irqrestore(&adapter->dev_topo.topo_node_lock, flags);
list_for_each_entry_safe(card_phy, card_phy_next,
&sas_port->phy_list, port_siblings) {
card_phy->phy_is_assigned = 0;
if (!adapter->access_ctrl.host_removing)
sas_port_delete_phy(sas_port->port, card_phy->phy);
list_del(&card_phy->port_siblings);
}
if (!adapter->access_ctrl.host_removing)
sas_port_delete(sas_port->port);
dev_dbg(&adapter->pdev->dev,
"%s: Removed sas_port for SAS addr=0x%016llx\n",
__func__, (unsigned long long)sas_address);
kfree(sas_port);
}
static void leapraid_init_sas_or_exp_phy(struct leapraid_adapter *adapter,
struct leapraid_card_phy *card_phy,
struct sas_phy *phy,
struct leapraid_sas_phy_p0 *phy_pg0,
struct leapraid_exp_p1 *exp_pg1)
{
if (exp_pg1 && phy_pg0)
return;
if (!exp_pg1 && !phy_pg0)
return;
phy->identify = card_phy->identify;
phy->identify.phy_identifier = card_phy->phy_id;
phy->negotiated_linkrate = phy_pg0 ?
leapraid_transport_convert_phy_link_rate(
phy_pg0->neg_link_rate &
LEAPRAID_SAS_NEG_LINK_RATE_MASK_PHYSICAL) :
leapraid_transport_convert_phy_link_rate(
exp_pg1->neg_link_rate &
LEAPRAID_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
phy->minimum_linkrate_hw = phy_pg0 ?
leapraid_transport_convert_phy_link_rate(
phy_pg0->hw_link_rate &
LEAPRAID_SAS_HWRATE_MIN_RATE_MASK) :
leapraid_transport_convert_phy_link_rate(
exp_pg1->hw_link_rate &
LEAPRAID_SAS_HWRATE_MIN_RATE_MASK);
phy->maximum_linkrate_hw = phy_pg0 ?
leapraid_transport_convert_phy_link_rate(
phy_pg0->hw_link_rate >>
LEAPRAID_SAS_NEG_LINK_RATE_SHIFT) :
leapraid_transport_convert_phy_link_rate(
exp_pg1->hw_link_rate >>
LEAPRAID_SAS_NEG_LINK_RATE_SHIFT);
phy->minimum_linkrate = phy_pg0 ?
leapraid_transport_convert_phy_link_rate(
phy_pg0->p_link_rate &
LEAPRAID_SAS_PRATE_MIN_RATE_MASK) :
leapraid_transport_convert_phy_link_rate(
exp_pg1->p_link_rate &
LEAPRAID_SAS_PRATE_MIN_RATE_MASK);
phy->maximum_linkrate = phy_pg0 ?
leapraid_transport_convert_phy_link_rate(
phy_pg0->p_link_rate >>
LEAPRAID_SAS_NEG_LINK_RATE_SHIFT) :
leapraid_transport_convert_phy_link_rate(
exp_pg1->p_link_rate >>
LEAPRAID_SAS_NEG_LINK_RATE_SHIFT);
phy->hostdata = card_phy->card_port;
}
void leapraid_transport_add_card_phy(struct leapraid_adapter *adapter,
struct leapraid_card_phy *card_phy,
struct leapraid_sas_phy_p0 *phy_pg0,
struct device *parent_dev)
{
struct sas_phy *phy;
int ret;
INIT_LIST_HEAD(&card_phy->port_siblings);
phy = sas_phy_alloc(parent_dev, card_phy->phy_id);
if (!phy) {
dev_err(&adapter->pdev->dev,
"%s: sas_phy_alloc failed!\n", __func__);
return;
}
if (leapraid_transport_set_identify(adapter, card_phy->hdl,
&card_phy->identify)) {
dev_err(&adapter->pdev->dev,
"%s: Set PHY handle identify failed!\n", __func__);
sas_phy_free(phy);
return;
}
card_phy->attached_hdl = le16_to_cpu(phy_pg0->attached_dev_hdl);
if (card_phy->attached_hdl) {
ret = leapraid_transport_set_identify(
adapter,
card_phy->attached_hdl,
&card_phy->remote_identify);
if (ret) {
dev_err(&adapter->pdev->dev,
"%s: Set PHY attached hdl identify failed!\n",
__func__);
sas_phy_free(phy);
return;
}
}
leapraid_init_sas_or_exp_phy(adapter, card_phy, phy, phy_pg0, NULL);
if (sas_phy_add(phy)) {
dev_err(&adapter->pdev->dev,
"%s: SAS PHY add failed!\n", __func__);
sas_phy_free(phy);
return;
}
card_phy->phy = phy;
}
int leapraid_transport_add_exp_phy(struct leapraid_adapter *adapter,
struct leapraid_card_phy *card_phy,
struct leapraid_exp_p1 *exp_pg1,
struct device *parent_dev)
{
struct sas_phy *phy;
int ret;
INIT_LIST_HEAD(&card_phy->port_siblings);
phy = sas_phy_alloc(parent_dev, card_phy->phy_id);
if (!phy) {
dev_err(&adapter->pdev->dev,
"%s: sas_phy_alloc failed!\n", __func__);
return -EFAULT;
}
if (leapraid_transport_set_identify(adapter, card_phy->hdl,
&card_phy->identify)) {
dev_err(&adapter->pdev->dev,
"%s: Set PHY hdl identify failed!\n", __func__);
sas_phy_free(phy);
return -EFAULT;
}
card_phy->attached_hdl = le16_to_cpu(exp_pg1->attached_dev_hdl);
if (card_phy->attached_hdl) {
ret = leapraid_transport_set_identify(
adapter,
card_phy->attached_hdl,
&card_phy->remote_identify);
if (ret)
dev_warn(&adapter->pdev->dev,
"%s: Set PHY attached hdl identify failed!\n",
__func__);
}
leapraid_init_sas_or_exp_phy(adapter, card_phy, phy, NULL, exp_pg1);
if (sas_phy_add(phy)) {
dev_err(&adapter->pdev->dev,
"%s: SAS PHY add failed!\n", __func__);
sas_phy_free(phy);
return -EFAULT;
}
card_phy->phy = phy;
return 0;
}
void leapraid_transport_update_links(
struct leapraid_adapter *adapter,
u64 sas_address, u16 hdl, u8 phy_index,
u8 link_rate, struct leapraid_card_port *target_card_port)
{
struct leapraid_topo_node *topo_node;
struct leapraid_card_phy *card_phy;
struct leapraid_card_port *card_port;
unsigned long flags;
if (adapter->access_ctrl.shost_recovering ||
adapter->access_ctrl.pcie_recovering)
return;
spin_lock_irqsave(&adapter->dev_topo.topo_node_lock, flags);
topo_node = leapraid_transport_topo_node_by_sas_addr(adapter,
sas_address,
target_card_port);
if (!topo_node) {
spin_unlock_irqrestore(&adapter->dev_topo.topo_node_lock,
flags);
return;
}
card_phy = &topo_node->card_phy[phy_index];
card_phy->attached_hdl = hdl;
spin_unlock_irqrestore(&adapter->dev_topo.topo_node_lock, flags);
if (hdl && link_rate >= LEAPRAID_SAS_NEG_LINK_RATE_1_5) {
leapraid_transport_set_identify(adapter, hdl,
&card_phy->remote_identify);
if (topo_node->hdl <= adapter->dev_topo.card.phys_num &&
adapter->adapter_attr.enable_mp) {
list_for_each_entry(card_port,
&adapter->dev_topo.card_port_list,
list) {
if (card_port->sas_address == sas_address &&
card_port == target_card_port)
card_port->phy_mask |=
BIT(card_phy->phy_id);
}
}
leapraid_transport_attach_phy_to_port(
adapter,
topo_node,
card_phy,
card_phy->remote_identify.sas_address,
target_card_port);
} else {
memset(&card_phy->remote_identify, 0,
sizeof(struct sas_identify));
}
if (card_phy->phy)
card_phy->phy->negotiated_linkrate =
leapraid_transport_convert_phy_link_rate(link_rate);
}
static int leapraid_dma_map_buffer(struct device *dev, struct bsg_buffer *buf,
dma_addr_t *dma_addr,
size_t *dma_len, void **p)
{
if (buf->sg_cnt > 1) {
*p = dma_alloc_coherent(dev, buf->payload_len, dma_addr,
GFP_KERNEL);
if (!*p)
return -ENOMEM;
*dma_len = buf->payload_len;
} else {
if (!dma_map_sg(dev, buf->sg_list, 1, DMA_BIDIRECTIONAL)) {
dev_err(dev,
"%s: Failed to map sg buffer for dma\n",
__func__);
return -ENOMEM;
}
*dma_addr = sg_dma_address(buf->sg_list);
*dma_len = sg_dma_len(buf->sg_list);
*p = NULL;
}
return 0;
}
static void leapraid_dma_unmap_buffer(struct device *dev,
struct bsg_buffer *buf,
dma_addr_t dma_addr,
void *p)
{
if (p)
dma_free_coherent(dev, buf->payload_len, p, dma_addr);
else
dma_unmap_sg(dev, buf->sg_list, 1, DMA_BIDIRECTIONAL);
}
static void leapraid_build_smp_task(struct leapraid_adapter *adapter,
struct sas_rphy *rphy,
dma_addr_t h2c_dma_addr, size_t h2c_size,
dma_addr_t c2h_dma_addr, size_t c2h_size)
{
struct leapraid_smp_passthrough_req *smp_passthrough_req;
u16 inter_taskid;
void *psge;
inter_taskid = adapter->driver_cmds.transport_cmd.inter_taskid;
smp_passthrough_req = leapraid_get_task_desc(adapter, inter_taskid);
memset(smp_passthrough_req, 0, sizeof(*smp_passthrough_req));
smp_passthrough_req->func = LEAPRAID_FUNC_SMP_PASSTHROUGH;
smp_passthrough_req->physical_port =
leapraid_transport_get_port_id_by_rphy(adapter, rphy);
smp_passthrough_req->sas_address = (rphy) ?
cpu_to_le64(rphy->identify.sas_address) :
cpu_to_le64(adapter->dev_topo.card.sas_address);
smp_passthrough_req->req_data_len =
cpu_to_le16(h2c_size - LEAPRAID_SMP_FRAME_HEADER_SIZE);
psge = &smp_passthrough_req->sgl;
leapraid_build_ieee_sg(adapter, psge, h2c_dma_addr,
h2c_size - LEAPRAID_SMP_FRAME_HEADER_SIZE,
c2h_dma_addr,
c2h_size - LEAPRAID_SMP_FRAME_HEADER_SIZE);
}
static int leapraid_send_smp_req(struct leapraid_adapter *adapter)
{
const struct leapraid_smp_passthrough_req *smp_passthrough_req;
u16 inter_taskid;
dev_dbg(&adapter->pdev->dev,
"%s: Sending smp request\n", __func__);
inter_taskid = adapter->driver_cmds.transport_cmd.inter_taskid;
smp_passthrough_req = leapraid_get_task_desc(adapter, inter_taskid);
init_completion(&adapter->driver_cmds.transport_cmd.done);
leapraid_fire_task(adapter, inter_taskid);
wait_for_completion_timeout(&adapter->driver_cmds.transport_cmd.done,
LEAPRAID_TRANSPORT_CMD_TIMEOUT * HZ);
if (!(adapter->driver_cmds.transport_cmd.status & LEAPRAID_CMD_DONE)) {
dev_err(&adapter->pdev->dev, "%s: timeout, st=0x%x\n",
__func__, adapter->driver_cmds.transport_cmd.status);
leapraid_log_req_context(adapter, inter_taskid,
smp_passthrough_req);
if (!(adapter->driver_cmds.transport_cmd.status &
LEAPRAID_CMD_RESET)) {
dev_dbg(&adapter->pdev->dev,
"%s:%d: call hard_reset\n",
__func__, __LINE__);
leapraid_hard_reset_handler(adapter, FULL_RESET);
return -ETIMEDOUT;
}
}
dev_dbg(&adapter->pdev->dev, "%s: SMP request complete\n", __func__);
if (!(adapter->driver_cmds.transport_cmd.status &
LEAPRAID_CMD_REPLY_VALID)) {
dev_err(&adapter->pdev->dev,
"%s: SMP request no reply\n", __func__);
return -ENXIO;
}
return 0;
}
static void leapraid_handle_smp_rep(struct leapraid_adapter *adapter,
struct bsg_job *job, void *addr_in,
unsigned int *reslen)
{
struct leapraid_smp_passthrough_rep *smp_passthrough_rep;
smp_passthrough_rep =
(void *)&adapter->driver_cmds.transport_cmd.reply;
dev_dbg(&adapter->pdev->dev, "%s: Response data len=%d\n",
__func__, le16_to_cpu(smp_passthrough_rep->resp_data_len));
memcpy(job->reply, smp_passthrough_rep, sizeof(*smp_passthrough_rep));
job->reply_len = sizeof(*smp_passthrough_rep);
*reslen = le16_to_cpu(smp_passthrough_rep->resp_data_len);
if (addr_in)
sg_copy_from_buffer(job->reply_payload.sg_list,
job->reply_payload.sg_cnt, addr_in,
job->reply_payload.payload_len);
}
static void leapraid_transport_smp_handler(struct bsg_job *job,
struct Scsi_Host *shost,
struct sas_rphy *rphy)
{
struct leapraid_adapter *adapter = shost_priv(shost);
dma_addr_t c2h_dma_addr;
dma_addr_t h2c_dma_addr;
void *addr_in = NULL;
void *addr_out = NULL;
size_t c2h_size;
size_t h2c_size;
int rc;
unsigned int reslen = 0;
if (adapter->access_ctrl.shost_recovering ||
adapter->access_ctrl.pcie_recovering) {
dev_err(&adapter->pdev->dev,
"%s: Failed, shost_recovering=%d pcie_recovering=%d\n",
__func__,
adapter->access_ctrl.shost_recovering,
adapter->access_ctrl.pcie_recovering);
rc = -EFAULT;
goto exit_bsg_job;
}
rc = mutex_lock_interruptible(&adapter->driver_cmds
.transport_cmd.mutex);
if (rc)
goto exit_bsg_job;
adapter->driver_cmds.transport_cmd.status = LEAPRAID_CMD_PENDING;
rc = leapraid_dma_map_buffer(&adapter->pdev->dev,
&job->request_payload,
&h2c_dma_addr, &h2c_size, &addr_out);
if (rc)
goto release_lock;
if (h2c_size < LEAPRAID_SMP_FRAME_HEADER_SIZE) {
dev_err(&adapter->pdev->dev,
"%s: Invalid SMP request payload size=%zu\n",
__func__, h2c_size);
rc = -EINVAL;
goto free_req_buf;
}
if (addr_out)
sg_copy_to_buffer(job->request_payload.sg_list,
job->request_payload.sg_cnt, addr_out,
job->request_payload.payload_len);
rc = leapraid_dma_map_buffer(&adapter->pdev->dev, &job->reply_payload,
&c2h_dma_addr, &c2h_size, &addr_in);
if (rc)
goto free_req_buf;
if (c2h_size < LEAPRAID_SMP_FRAME_HEADER_SIZE) {
dev_err(&adapter->pdev->dev,
"%s: Invalid SMP reply payload size=%zu\n",
__func__, c2h_size);
rc = -EINVAL;
goto free_rep_buf;
}
rc = leapraid_check_adapter_is_op(adapter, LEAPRAID_DB_WAIT_OP_SHORT,
__func__);
if (rc)
goto free_rep_buf;
leapraid_build_smp_task(adapter, rphy, h2c_dma_addr,
h2c_size, c2h_dma_addr, c2h_size);
rc = leapraid_send_smp_req(adapter);
if (rc)
goto free_rep_buf;
leapraid_handle_smp_rep(adapter, job, addr_in, &reslen);
free_rep_buf:
leapraid_dma_unmap_buffer(&adapter->pdev->dev, &job->reply_payload,
c2h_dma_addr, addr_in);
free_req_buf:
leapraid_dma_unmap_buffer(&adapter->pdev->dev, &job->request_payload,
h2c_dma_addr, addr_out);
release_lock:
adapter->driver_cmds.transport_cmd.status = LEAPRAID_CMD_NOT_USED;
mutex_unlock(&adapter->driver_cmds.transport_cmd.mutex);
exit_bsg_job:
bsg_job_done(job, rc, reslen);
}
struct sas_function_template leapraid_transport_functions = {
.smp_handler = leapraid_transport_smp_handler,
};
struct scsi_transport_template *leapraid_transport_template;
|