File: fru.c

package info (click to toggle)
openipmi 2.0.37-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,100 kB
  • sloc: ansic: 140,279; python: 8,801; sh: 5,723; perl: 5,394; makefile: 490
file content (1973 lines) | stat: -rw-r--r-- 46,282 bytes parent folder | download
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
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
/*
 * fru.c
 *
 * IPMI code for handling FRUs
 *
 * Author: MontaVista Software, Inc.
 *         Corey Minyard <minyard@mvista.com>
 *         source@mvista.com
 *
 * Copyright 2002,2003 MontaVista Software Inc.
 *
 * Note that this file was originally written by Thomas Kanngieser
 * <thomas.kanngieser@fci.com> of FORCE Computers, but I've pretty
 * much gutted it and rewritten it, nothing really remained the same.
 * Thomas' code was helpful, though and many thanks go to him.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *
 *  THIS SOFTWARE IS PROVIDED ``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.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>

#include <OpenIPMI/ipmiif.h>
#include <OpenIPMI/ipmi_fru.h>
#include <OpenIPMI/ipmi_err.h>
#include <OpenIPMI/ipmi_msgbits.h>

#include <OpenIPMI/internal/locked_list.h>
#include <OpenIPMI/internal/ipmi_domain.h>
#include <OpenIPMI/internal/ipmi_int.h>
#include <OpenIPMI/internal/ipmi_utils.h>
#include <OpenIPMI/internal/ipmi_oem.h>
#include <OpenIPMI/internal/ipmi_fru.h>

#define MAX_FRU_DATA_FETCH 32
#define FRU_DATA_FETCH_DECR 8
#define MIN_FRU_DATA_FETCH 16

#define MAX_FRU_DATA_WRITE 16
#define MAX_FRU_WRITE_RETRIES 30

#define MAX_FRU_FETCH_RETRIES 5

#define IPMI_FRU_ATTR_NAME "ipmi_fru"

/*
 * A note of FRUs, fru attributes, and locking.
 *
 * Because we keep a list of FRUs, that makes locking a lot more
 * complicated.  While we are deleting a FRU another thread can come
 * along and iterate and find it.  The lock on the locked list is used
 * along with the FRU lock to prevent this from happening.  Since in
 * this situation, the locked list lock is held when the FRU is
 * referenced, when we destroy the FRU we make sure that it wasn't
 * resurrected after being deleted from this list.
 */

/* Record used for FRU writing. */
typedef struct fru_update_s fru_update_t;
struct fru_update_s
{
    unsigned short offset;
    unsigned short length;
    fru_update_t   *next;
};

/* Operations registered by the decode for a FRU. */
typedef struct ipmi_fru_op_s
{
    /* Called to free all the data associated with fru record data. */
    void (*cleanup_recs)(ipmi_fru_t *fru);

    /* Called when the FRU data has been written, to mark all the data
       as unchanged from the FRU contents. */
    void (*write_complete)(ipmi_fru_t *fru);

    /* Called to write any changed data into the fru and mark what is
       changed. */
    int (*write)(ipmi_fru_t *fru);

    /* Get the root node for this FRU. */
    int (*get_root_node)(ipmi_fru_t      *fru,
			 const char      **name,
			 ipmi_fru_node_t **rnode);
} ipmi_fru_op_t;

struct ipmi_fru_s
{
    char name[IPMI_FRU_NAME_LEN+1];
    int deleted;

    unsigned int refcount;

    /* Is the FRU being read or written? */
    int in_use;

    ipmi_lock_t *lock;

    ipmi_addr_t  addr;
    unsigned int addr_len;

    void                          *setup_data;
    i_ipmi_fru_setup_data_clean_cb setup_data_cleanup;

    ipmi_domain_id_t     domain_id;
    unsigned char        is_logical;
    unsigned char        device_address;
    unsigned char        device_id;
    unsigned char        lun;
    unsigned char        private_bus;
    unsigned char        channel;

    unsigned int        fetch_mask;

    uint32_t last_timestamp;
    int      fetch_retries;

    ipmi_fru_fetched_cb fetched_handler;
    ipmi_fru_cb         domain_fetched_handler;
    void                *fetched_cb_data;

    ipmi_fru_destroyed_cb destroy_handler;
    void                  *destroy_cb_data;

    int           access_by_words;
    unsigned char *data;
    unsigned int  data_len;
    unsigned int  curr_pos;
    unsigned int  curr_write_len;
    int           write_prepared;
    int           saved_err;

    int           fetch_size;

    /* Is this in the list of FRUs? */
    int in_frulist;

    /* The records for writing. */
    fru_update_t *update_recs;
    fru_update_t *update_recs_tail;

    /* The last send command for writing */
    unsigned int  last_cmd_len;
    unsigned int  retry_count;

    os_handler_t *os_hnd;

    /* If the FRU is a "normal" fru type, for backwards
       compatability. */
    int  normal_fru;

    char *fru_rec_type;
    void *rec_data;
    ipmi_fru_op_t ops;

    /* FRU locking handling */
    i_ipmi_fru_get_timestamp_cb  timestamp_cb;
    i_ipmi_fru_prepare_write_cb  prepare_write_cb;
    i_ipmi_fru_write_cb          write_cb;
    i_ipmi_fru_complete_write_cb complete_write_cb;

    char iname[IPMI_FRU_NAME_LEN+1];

    unsigned int options;
};

#define FRU_DOMAIN_NAME(fru) (fru ? fru->iname : "")

static void final_fru_destroy(ipmi_fru_t *fru);
static void fetch_complete(ipmi_domain_t *domain, ipmi_fru_t *fru, int err);

/***********************************************************************
 *
 * general utilities
 *
 **********************************************************************/
void
i_ipmi_fru_lock(ipmi_fru_t *fru)
{
    ipmi_lock(fru->lock);
}

void
i_ipmi_fru_unlock(ipmi_fru_t *fru)
{
    ipmi_unlock(fru->lock);
}

/*
 * Must already be holding the FRU lock to call this.
 */
static void
fru_get(ipmi_fru_t *fru)
{
    fru->refcount++;
}

void
i_ipmi_fru_ref_nolock(ipmi_fru_t *fru)
{
    fru->refcount++;
}

static void
fru_put(ipmi_fru_t *fru)
{
    i_ipmi_fru_lock(fru);
    fru->refcount--;
    if (fru->refcount == 0) {
	final_fru_destroy(fru);
	return;
    }
    i_ipmi_fru_unlock(fru);
}

void
ipmi_fru_ref(ipmi_fru_t *fru)
{
    i_ipmi_fru_lock(fru);
    fru_get(fru);
    i_ipmi_fru_unlock(fru);
}

void
ipmi_fru_deref(ipmi_fru_t *fru)
{
    fru_put(fru);
}

/************************************************************************
 *
 * Decode registration handling
 *
 ************************************************************************/

static locked_list_t *fru_decode_handlers;

int
i_ipmi_fru_register_decoder(ipmi_fru_err_op op)
{
    if (!locked_list_add(fru_decode_handlers, op, NULL))
	return ENOMEM;
    return 0;
}

int
i_ipmi_fru_deregister_decoder(ipmi_fru_err_op op)
{
    if (!locked_list_remove(fru_decode_handlers, op, NULL))
	return ENODEV;
    return 0;
}

typedef struct fru_decode_s
{
    ipmi_fru_t *fru;
    int        err;
} fru_decode_t;

static int
fru_call_decoder(void *cb_data, void *item1, void *item2)
{
    fru_decode_t    *info = cb_data;
    ipmi_fru_err_op op = item1;
    int             err;

    err = op(info->fru);
    if (!err) {
	info->err = 0;
	return LOCKED_LIST_ITER_STOP;
    } else
	return LOCKED_LIST_ITER_CONTINUE;
}

static int
fru_call_decoders(ipmi_fru_t *fru)
{
    fru_decode_t info;

    info.err = ENOSYS;
    info.fru = fru;
    locked_list_iterate(fru_decode_handlers, fru_call_decoder, &info);
    return info.err;
}

void
i_ipmi_fru_set_op_cleanup_recs(ipmi_fru_t *fru, ipmi_fru_void_op op)
{
    fru->ops.cleanup_recs = op;
}

void
i_ipmi_fru_set_op_write_complete(ipmi_fru_t *fru, ipmi_fru_void_op op)
{
    fru->ops.write_complete = op;
}

void
i_ipmi_fru_set_op_write(ipmi_fru_t *fru, ipmi_fru_err_op op)
{
    fru->ops.write = op;
}

void
i_ipmi_fru_set_op_get_root_node(ipmi_fru_t                *fru,
			       ipmi_fru_get_root_node_op op)
{
    fru->ops.get_root_node = op;
}


/***********************************************************************
 *
 * FRU configuration
 *
 **********************************************************************/
int
i_ipmi_fru_set_get_timestamp_handler(ipmi_fru_t                 *fru,
				     i_ipmi_fru_get_timestamp_cb handler)
{
    fru->timestamp_cb = handler;
    return 0;
}

int
i_ipmi_fru_set_prepare_write_handler(ipmi_fru_t                 *fru,
				     i_ipmi_fru_prepare_write_cb handler)
{
    fru->prepare_write_cb = handler;
    return 0;
}

int
i_ipmi_fru_set_write_handler(ipmi_fru_t         *fru,
			     i_ipmi_fru_write_cb handler)
{
    fru->write_cb = handler;
    return 0;
}

int
i_ipmi_fru_set_complete_write_handler(ipmi_fru_t                  *fru,
				      i_ipmi_fru_complete_write_cb handler)
{
    fru->complete_write_cb = handler;
    return 0;
}

void
i_ipmi_fru_get_addr(ipmi_fru_t *fru, ipmi_addr_t *addr, unsigned int *addr_len)
{
    *addr = fru->addr;
    *addr_len = fru->addr_len;
}

void
i_ipmi_fru_set_setup_data(ipmi_fru_t                    *fru,
			  void                          *data,
			  i_ipmi_fru_setup_data_clean_cb cleanup)
{
    fru->setup_data = data;
    fru->setup_data_cleanup = cleanup;
}

void *
i_ipmi_fru_get_setup_data(ipmi_fru_t *fru)
{
    return fru->setup_data;
}

static int
fru_normal_write_done(ipmi_domain_t *domain, ipmi_msgi_t *rspi)
{
    ipmi_msg_t      *msg = &rspi->msg;
    ipmi_fru_t      *fru = rspi->data1;
    unsigned char   *data = msg->data;
    i_ipmi_fru_op_cb cb = rspi->data2;
    int             err = 0;

    if (data[0]) {
	err = IPMI_IPMI_ERR_VAL(data[0]);
	goto out;
    }

    if (msg->data_len < 2) {
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%sfru.c(fru_normal_write_done): "
		 "FRU write response too small",
		 FRU_DOMAIN_NAME(fru));
	err = EINVAL;
	goto out;
    }

    if ((unsigned int) (data[1] << fru->access_by_words)
	!= (fru->last_cmd_len - 3))
    {
	/* Write was incomplete for some reason.  Just go on but issue
	   a warning. */
	ipmi_log(IPMI_LOG_WARNING,
		 "%sfru.c(fru_normal_write_done): "
		 "Incomplete writing FRU data, write %d, expected %d",
		 FRU_DOMAIN_NAME(fru),
		 data[1] << fru->access_by_words, fru->last_cmd_len-3);
    }

 out:
    cb(fru, domain, err);
    return IPMI_MSG_ITEM_NOT_USED;
}

static int
fru_normal_write(ipmi_fru_t      *fru,
		 ipmi_domain_t   *domain,
		 unsigned char   *data,
		 unsigned int    data_len,
		 i_ipmi_fru_op_cb done)
{
    ipmi_msg_t msg;

    msg.netfn = IPMI_STORAGE_NETFN;
    msg.cmd = IPMI_WRITE_FRU_DATA_CMD;
    msg.data = data;
    msg.data_len = data_len;

    return ipmi_send_command_addr(domain,
				  &fru->addr, fru->addr_len,
				  &msg,
				  fru_normal_write_done,
				  fru,
				  done);
}

void
ipmi_fru_set_options(ipmi_fru_t *fru, unsigned int options)
{
    fru->options = options;
}

unsigned int
ipmi_fru_get_options(ipmi_fru_t *fru)
{
    return fru->options;
}

/***********************************************************************
 *
 * FRU allocation and destruction
 *
 **********************************************************************/

static void
final_fru_destroy(ipmi_fru_t *fru)
{
    if (fru->in_frulist) {
	int                rv;
	ipmi_domain_attr_t *attr;
	locked_list_t      *frul;

	fru->in_frulist = 0;
	rv = ipmi_domain_id_find_attribute(fru->domain_id, IPMI_FRU_ATTR_NAME,
					   &attr);
	if (!rv) {
	    fru->refcount++;
	    i_ipmi_fru_unlock(fru);
	    frul = ipmi_domain_attr_get_data(attr);
	    locked_list_remove(frul, fru, NULL);
	    ipmi_domain_attr_put(attr);
	    i_ipmi_fru_lock(fru);
	    /* While we were unlocked, someone may have come in and
	       grabbed the FRU by iterating the list of FRUs.  That's
	       ok, we just let them handle the destruction since this
	       code will not be entered again. */
	    if (fru->refcount != 1) {
		fru->refcount--;
		i_ipmi_fru_unlock(fru);
		return;
	    }
	}
    }
    i_ipmi_fru_unlock(fru);

    /* No one else can be referencing this here, so it is safe to
       release the lock now. */

    if (fru->destroy_handler)
	fru->destroy_handler(fru, fru->destroy_cb_data);

    if (fru->ops.cleanup_recs)
	fru->ops.cleanup_recs(fru);

    while (fru->update_recs) {
	fru_update_t *to_free = fru->update_recs;
	fru->update_recs = to_free->next;
	ipmi_mem_free(to_free);
    }
    if (fru->setup_data_cleanup)
	fru->setup_data_cleanup(fru, fru->setup_data);
    ipmi_destroy_lock(fru->lock);
    ipmi_mem_free(fru);
}

int
ipmi_fru_destroy_internal(ipmi_fru_t            *fru,
			  ipmi_fru_destroyed_cb handler,
			  void                  *cb_data)
{
    if (fru->in_frulist)
	return EPERM;

    i_ipmi_fru_lock(fru);
    fru->destroy_handler = handler;
    fru->destroy_cb_data = cb_data;
    fru->deleted = 1;
    i_ipmi_fru_unlock(fru);

    fru_put(fru);
    return 0;
}

int
ipmi_fru_destroy(ipmi_fru_t            *fru,
		 ipmi_fru_destroyed_cb handler,
		 void                  *cb_data)
{
    ipmi_domain_attr_t *attr;
    locked_list_t      *frul;
    int                rv;

    i_ipmi_fru_lock(fru);
    if (fru->in_frulist) {
	rv = ipmi_domain_id_find_attribute(fru->domain_id, IPMI_FRU_ATTR_NAME,
					   &attr);
	if (rv) {
	    i_ipmi_fru_unlock(fru);
	    return rv;
	}
	fru->in_frulist = 0;
	i_ipmi_fru_unlock(fru);

	frul = ipmi_domain_attr_get_data(attr);
	if (! locked_list_remove(frul, fru, NULL)) {
	    /* Not in the list, it's already been removed. */
	    ipmi_domain_attr_put(attr);
	    i_ipmi_fru_unlock(fru);
	    return EINVAL;
	}
	ipmi_domain_attr_put(attr);
	fru_put(fru); /* It's not in the list any more. */
    } else {
	/* User can't destroy FRUs he didn't allocate. */
	i_ipmi_fru_unlock(fru);
	return EPERM;
    }

    return ipmi_fru_destroy_internal(fru, handler, cb_data);
}

static int start_logical_fru_fetch(ipmi_domain_t *domain, ipmi_fru_t *fru);
static int start_physical_fru_fetch(ipmi_domain_t *domain, ipmi_fru_t *fru);

static int
destroy_fru(void *cb_data, void *item1, void *item2)
{
    ipmi_fru_t *fru = item1;

    /* Users are responsible for handling their own FRUs, we don't
       delete here, just mark not in the list. */
    i_ipmi_fru_lock(fru);
    fru->in_frulist = 0;
    i_ipmi_fru_unlock(fru);
    return LOCKED_LIST_ITER_CONTINUE;
}

static void
fru_attr_destroy(void *cb_data, void *data)
{
    locked_list_t *frul = data;

    locked_list_iterate(frul, destroy_fru, NULL);
    locked_list_destroy(frul);
}

static int
fru_attr_init(ipmi_domain_t *domain, void *cb_data, void **data)
{
    locked_list_t *frul;
    
    frul = locked_list_alloc(ipmi_domain_get_os_hnd(domain));
    if (!frul)
	return ENOMEM;

    *data = frul;
    return 0;
}

static int
start_fru_fetch(ipmi_fru_t *fru, ipmi_domain_t *domain)
{
    int rv;

    fru->curr_pos = 0;

    if (fru->is_logical)
	rv = start_logical_fru_fetch(domain, fru);
    else
	rv = start_physical_fru_fetch(domain, fru);

    return rv;
}

static void
fetch_got_timestamp(ipmi_fru_t    *fru,
		    ipmi_domain_t *domain,
		    int           err,
		    uint32_t      timestamp)
{
    int rv;
    i_ipmi_fru_lock(fru);
    if (fru->deleted) {
	fetch_complete(domain, fru, ECANCELED);
	goto out;
    }

    if (err) {
	fetch_complete(domain, fru, err);
	goto out;
    }

    fru->last_timestamp = timestamp;
    rv = start_fru_fetch(fru, domain);
    if (rv) {
	fetch_complete(domain, fru, rv);
	goto out;
    }
    i_ipmi_fru_unlock(fru);
 out:
    return;
}

static int
ipmi_fru_alloc_internal(ipmi_domain_t       *domain,
			unsigned char       is_logical,
			unsigned char       device_address,
			unsigned char       device_id,
			unsigned char       lun,
			unsigned char       private_bus,
			unsigned char       channel,
			unsigned char       fetch_mask,
			ipmi_fru_fetched_cb fetched_handler,
			void                *fetched_cb_data,
			ipmi_fru_t          **new_fru)
{
    ipmi_fru_t       *fru;
    int              err;
    int              len, p;
    ipmi_ipmb_addr_t *ipmb;

    fru = ipmi_mem_alloc(sizeof(*fru));
    if (!fru)
	return ENOMEM;
    memset(fru, 0, sizeof(*fru));

    err = ipmi_create_lock(domain, &fru->lock);
    if (err) {
	ipmi_mem_free(fru);
	return err;
    }

    /* Refcount starts at 2 because we start a fetch immediately. */
    fru->refcount = 2;
    fru->in_use = 1;

    fru->domain_id = ipmi_domain_convert_to_id(domain);
    fru->is_logical = is_logical;
    fru->device_address = device_address;
    fru->device_id = device_id;
    fru->lun = lun;
    fru->private_bus = private_bus;
    fru->channel = channel;
    fru->fetch_mask = fetch_mask;
    fru->fetch_size = MAX_FRU_DATA_FETCH;
    fru->os_hnd = ipmi_domain_get_os_hnd(domain);
    fru->write_cb = fru_normal_write;

    len = sizeof(fru->name);
    p = ipmi_domain_get_name(domain, fru->name, len);
    len -= p;
    snprintf(fru->name+p, len, ".%d", ipmi_domain_get_unique_num(domain));

    snprintf(fru->iname, sizeof(fru->iname), "%s.%d.%x.%d.%d.%d.%d ",
	     DOMAIN_NAME(domain), is_logical, device_address, device_id, lun,
	     private_bus, channel);

    fru->fetched_handler = fetched_handler;
    fru->fetched_cb_data = fetched_cb_data;

    fru->deleted = 0;

    ipmb = (ipmi_ipmb_addr_t *) &fru->addr;
    ipmb->addr_type = IPMI_IPMB_ADDR_TYPE;
    ipmb->channel = fru->channel;
    ipmb->slave_addr = fru->device_address;
    ipmb->lun = fru->lun;
    fru->addr_len = sizeof(*ipmb);

    err = i_ipmi_domain_fru_call_special_setup(domain, is_logical,
					       device_address, device_id,
					       lun, private_bus, channel,
					       fru);
    if (err)
	goto out_err;

    i_ipmi_fru_lock(fru);
    if (fru->timestamp_cb) {
	err = fru->timestamp_cb(fru, domain, fetch_got_timestamp);
	if (err)
	    goto out_err;
    } else {
	err = start_fru_fetch(fru, domain);
	if (err)
	    goto out_err;
    }

    *new_fru = fru;
    return 0;

 out_err:
    i_ipmi_fru_unlock(fru);
    ipmi_destroy_lock(fru->lock);
    ipmi_mem_free(fru);
    return err;
}

int
ipmi_domain_fru_alloc(ipmi_domain_t *domain,
		      unsigned char is_logical,
		      unsigned char device_address,
		      unsigned char device_id,
		      unsigned char lun,
		      unsigned char private_bus,
		      unsigned char channel,
		      ipmi_fru_cb   fetched_handler,
		      void          *fetched_cb_data,
		      ipmi_fru_t    **new_fru)
{
    ipmi_fru_t         *nfru;
    int                rv;
    ipmi_domain_attr_t *attr;
    locked_list_t      *frul;

    rv = ipmi_domain_register_attribute(domain, IPMI_FRU_ATTR_NAME,
					fru_attr_init,
					fru_attr_destroy,
					NULL,
					&attr);
    if (rv)
	return rv;
    frul = ipmi_domain_attr_get_data(attr);

    /* Be careful with locking, a FRU fetch is already going on when
       the alloc_internal function returns. */
    locked_list_lock(frul);
    rv = ipmi_fru_alloc_internal(domain, is_logical, device_address,
				 device_id, lun, private_bus, channel,
				 IPMI_FRU_ALL_AREA_MASK, NULL, NULL, &nfru);
    if (rv) {
	locked_list_unlock(frul);
	ipmi_domain_attr_put(attr);
	return rv;
    }

    nfru->in_frulist = 1;

    if (! locked_list_add_nolock(frul, nfru, NULL)) {
	locked_list_unlock(frul);
	nfru->fetched_handler = NULL;
	ipmi_fru_destroy(nfru, NULL, NULL);
	ipmi_domain_attr_put(attr);
	return ENOMEM;
    }
    nfru->domain_fetched_handler = fetched_handler;
    nfru->fetched_cb_data = fetched_cb_data;
    i_ipmi_fru_unlock(nfru);
    locked_list_unlock(frul);
    ipmi_domain_attr_put(attr);

    if (new_fru)
	*new_fru = nfru;
    return 0;
}

int
ipmi_fru_alloc(ipmi_domain_t       *domain,
	       unsigned char       is_logical,
	       unsigned char       device_address,
	       unsigned char       device_id,
	       unsigned char       lun,
	       unsigned char       private_bus,
	       unsigned char       channel,
	       ipmi_fru_fetched_cb fetched_handler,
	       void                *fetched_cb_data,
	       ipmi_fru_t          **new_fru)
{
    ipmi_fru_t         *nfru;
    int                rv;
    ipmi_domain_attr_t *attr;
    locked_list_t      *frul;

    rv = ipmi_domain_register_attribute(domain, IPMI_FRU_ATTR_NAME,
					fru_attr_init,
					fru_attr_destroy,
					NULL,
					&attr);
    if (rv)
	return rv;
    frul = ipmi_domain_attr_get_data(attr);

    /* Be careful with locking, a FRU fetch is already going on when
       the alloc_internal function returns. */
    locked_list_lock(frul);
    rv = ipmi_fru_alloc_internal(domain, is_logical, device_address,
				 device_id, lun, private_bus, channel,
				 IPMI_FRU_ALL_AREA_MASK,
				 fetched_handler, fetched_cb_data, &nfru);
    if (rv) {
	ipmi_domain_attr_put(attr);
	locked_list_unlock(frul);
	return rv;
    }

    nfru->in_frulist = 1;

    if (! locked_list_add_nolock(frul, nfru, NULL)) {
	locked_list_unlock(frul);
	nfru->fetched_handler = NULL;
	ipmi_fru_destroy(nfru, NULL, NULL);
	ipmi_domain_attr_put(attr);
	return ENOMEM;
    }
    i_ipmi_fru_unlock(nfru);
    locked_list_unlock(frul);
    ipmi_domain_attr_put(attr);

    if (new_fru)
	*new_fru = nfru;
    return 0;
}

int
ipmi_fru_alloc_notrack(ipmi_domain_t *domain,
		       unsigned char is_logical,
		       unsigned char device_address,
		       unsigned char device_id,
		       unsigned char lun,
		       unsigned char private_bus,
		       unsigned char channel,
		       unsigned char fetch_mask,
		       ipmi_ifru_cb  fetched_handler,
		       void          *fetched_cb_data,
		       ipmi_fru_t    **new_fru)
{
    ipmi_fru_t *nfru;
    int        rv;

    rv = ipmi_fru_alloc_internal(domain, is_logical, device_address,
				 device_id, lun, private_bus, channel,
				 fetch_mask, NULL, NULL, &nfru);
    if (rv)
	return rv;
    nfru->domain_fetched_handler = fetched_handler;
    nfru->fetched_cb_data = fetched_cb_data;
    i_ipmi_fru_unlock(nfru);

    if (new_fru)
	*new_fru = nfru;
    return 0;
}

/***********************************************************************
 *
 * FRU Raw data reading
 *
 **********************************************************************/

static void
fetch_complete(ipmi_domain_t *domain, ipmi_fru_t *fru, int err)
{
    if (!err) {
	i_ipmi_fru_unlock(fru);
	err = fru_call_decoders(fru);
	if (err) {
	    ipmi_log(IPMI_LOG_ERR_INFO,
		     "%sfru.c(fetch_complete):"
		     " Unable to decode FRU information",
		     i_ipmi_fru_get_iname(fru));
	}
	i_ipmi_fru_lock(fru);
    }

    if (fru->data)
	ipmi_mem_free(fru->data);
    fru->data = NULL;
    fru->in_use = 0;
    i_ipmi_fru_unlock(fru);

    if (fru->fetched_handler)
	fru->fetched_handler(fru, err, fru->fetched_cb_data);
    else if (fru->domain_fetched_handler)
	fru->domain_fetched_handler(domain, fru, err, fru->fetched_cb_data);

    fru_put(fru);
}

static int request_next_data(ipmi_domain_t *domain,
			     ipmi_fru_t    *fru,
			     ipmi_addr_t   *addr,
			     unsigned int  addr_len);

static void
end_fru_fetch(ipmi_fru_t    *fru,
	      ipmi_domain_t *domain,
	      int           err,
	      uint32_t      timestamp)
{
    int rv;

    i_ipmi_fru_lock(fru);
    if (fru->deleted) {
	fetch_complete(domain, fru, ECANCELED);
	goto out;
    }

    if (err) {
	fetch_complete(domain, fru, err);
	goto out;
    }

    if (fru->last_timestamp != timestamp) {
	fru->fetch_retries++;
	if (fru->fetch_retries > MAX_FRU_FETCH_RETRIES)
	    fetch_complete(domain, fru, EAGAIN);
	else {
	    ipmi_mem_free(fru->data);
	    fru->data = NULL;
	    i_ipmi_fru_unlock(fru);
	    fru->last_timestamp = timestamp;
	    rv = start_fru_fetch(fru, domain);
	    if (rv)
		fetch_complete(domain, fru, rv);
	}
    } else
	fetch_complete(domain, fru, 0);

 out:
    return;
}

static int
fru_data_handler(ipmi_domain_t *domain, ipmi_msgi_t *rspi)
{
    ipmi_addr_t   *addr = &rspi->addr;
    unsigned int  addr_len = rspi->addr_len;
    ipmi_msg_t    *msg = &rspi->msg;
    ipmi_fru_t    *fru = rspi->data1;
    unsigned char *data = msg->data;
    int           count;
    int           err;

    i_ipmi_fru_lock(fru);

    if (fru->deleted) {
	fetch_complete(domain, fru, ECANCELED);
	goto out;
    }

    /* The timeout and unknown errors should not be necessary, but
       some broken systems just don't return anything if the response
       is too big. */
    if (((data[0] == IPMI_CANNOT_RETURN_REQ_LENGTH_CC)
	 || (data[0] == IPMI_REQUESTED_DATA_LENGTH_EXCEEDED_CC)
	 || (data[0] == IPMI_REQUEST_DATA_LENGTH_INVALID_CC)
	 || (data[0] == IPMI_TIMEOUT_CC)
	 || (data[0] == IPMI_UNKNOWN_ERR_CC))
	&& (fru->fetch_size > MIN_FRU_DATA_FETCH))
    {
	/* System couldn't support the given size, try decreasing and
	   starting again. */
	fru->fetch_size -= FRU_DATA_FETCH_DECR;
	err = request_next_data(domain, fru, addr, addr_len);
	if (err) {
	    ipmi_log(IPMI_LOG_ERR_INFO,
		     "%sfru.c(fru_data_handler): "
		     "Error requesting next FRU data (2)",
		     FRU_DOMAIN_NAME(fru));
	    fetch_complete(domain, fru, err);
	    goto out;
	}
	goto out_unlock;
    }

    if (data[0] != 0) {
	if (fru->curr_pos >= 8) {
	    /* Some screwy cards give more size in the info than they
	       really have, if we have enough, try to process it. */
	    ipmi_log(IPMI_LOG_WARNING,
		     "%sfru.c(fru_data_handler): "
		     "IPMI error getting FRU data: %x",
		     FRU_DOMAIN_NAME(fru), data[0]);
	    fru->data_len = fru->curr_pos;
	    if (fru->timestamp_cb) {
		err = fru->timestamp_cb(fru, domain, end_fru_fetch);
		if (err)
		    fetch_complete(domain, fru, err);
		else
		    goto out_unlock;
	    } else {
		fetch_complete(domain, fru, 0);
	    }
	} else {
	    ipmi_log(IPMI_LOG_ERR_INFO,
		     "%sfru.c(fru_data_handler): "
		     "IPMI error getting FRU data: %x",
		     FRU_DOMAIN_NAME(fru), data[0]);
	    fetch_complete(domain, fru, IPMI_IPMI_ERR_VAL(data[0]));
	}
	goto out;
    }

    if (msg->data_len < 2) {
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%sfru.c(fru_data_handler): "
		 "FRU data response too small",
		 FRU_DOMAIN_NAME(fru));
	fetch_complete(domain, fru, EINVAL);
	goto out;
    }

    count = data[1] << fru->access_by_words;

    if (count == 0) {
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%sfru.c(fru_data_handler): "
		 "FRU got zero-sized data, must make progress!",
		 FRU_DOMAIN_NAME(fru));
	fetch_complete(domain, fru, EINVAL);
	goto out;
    }

    if (count > msg->data_len-2) {
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%sfru.c(fru_data_handler): "
		 "FRU data count mismatch",
		 FRU_DOMAIN_NAME(fru));
	fetch_complete(domain, fru, EINVAL);
	goto out;
    }

    memcpy(fru->data+fru->curr_pos, data+2, count);
    fru->curr_pos += count;

    if (fru->curr_pos < fru->data_len) {
	/* More to fetch. */
	err = request_next_data(domain, fru, addr, addr_len);
	if (err) {
	    ipmi_log(IPMI_LOG_ERR_INFO,
		     "%sfru.c(fru_data_handler): "
		     "Error requesting next FRU data",
		     FRU_DOMAIN_NAME(fru));
	    fetch_complete(domain, fru, err);
	    goto out;
	}
    } else {
	if (fru->timestamp_cb) {
	    err = fru->timestamp_cb(fru, domain, end_fru_fetch);
	    if (err) {
		fetch_complete(domain, fru, err);
		goto out;
	    }
	} else {
	    fetch_complete(domain, fru, 0);
	    goto out;
	}
    }

 out_unlock:
    i_ipmi_fru_unlock(fru);
 out:
    return IPMI_MSG_ITEM_NOT_USED;
}

static int
request_next_data(ipmi_domain_t *domain,
		  ipmi_fru_t    *fru,
		  ipmi_addr_t   *addr,
		  unsigned int  addr_len)
{
    unsigned char cmd_data[4];
    ipmi_msg_t    msg;
    int           to_read;

    /* We only request as much as we have to.  Don't always reqeust
       the maximum amount, some machines don't like this. */
    to_read = fru->data_len - fru->curr_pos;
    if (to_read > fru->fetch_size)
	to_read = fru->fetch_size;

    cmd_data[0] = fru->device_id;
    ipmi_set_uint16(cmd_data+1, fru->curr_pos >> fru->access_by_words);
    cmd_data[3] = to_read >> fru->access_by_words;
    msg.netfn = IPMI_STORAGE_NETFN;
    msg.cmd = IPMI_READ_FRU_DATA_CMD;
    msg.data = cmd_data;
    msg.data_len = 4;

    return ipmi_send_command_addr(domain,
				  addr, addr_len,
				  &msg,
				  fru_data_handler,
				  fru,
				  NULL);
}

static int
fru_inventory_area_handler(ipmi_domain_t *domain, ipmi_msgi_t *rspi)
{
    ipmi_addr_t   *addr = &rspi->addr;
    unsigned int  addr_len = rspi->addr_len;
    ipmi_msg_t    *msg = &rspi->msg;
    ipmi_fru_t    *fru = rspi->data1;
    unsigned char *data = msg->data;
    int           err;

    i_ipmi_fru_lock(fru);

    if (fru->deleted) {
	fetch_complete(domain, fru, ECANCELED);
	goto out;
    }

    if (data[0] != 0) {
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%sfru.c(fru_inventory_area_handler): "
		 "IPMI error getting FRU inventory area: %x",
		 FRU_DOMAIN_NAME(fru), data[0]);
	fetch_complete(domain, fru, IPMI_IPMI_ERR_VAL(data[0]));
	goto out;
    }

    if (msg->data_len < 4) {
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%sfru.c(fru_inventory_area_handler): "
		 "FRU inventory area too small",
		 FRU_DOMAIN_NAME(fru));
	fetch_complete(domain, fru, EINVAL);
	goto out;
    }

    fru->data_len = ipmi_get_uint16(data+1);
    fru->access_by_words = data[3] & 1;

    if (fru->data_len < 8) {
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%sfru.c(fru_inventory_area_handler): "
		 "FRU space less than the header",
		 FRU_DOMAIN_NAME(fru));
	fetch_complete(domain, fru, EMSGSIZE);
	goto out;
    }

    fru->data = ipmi_mem_alloc(fru->data_len);
    if (!fru->data) {
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%sfru.c(fru_inventory_area_handler): "
		 "Error allocating FRU data",
		 FRU_DOMAIN_NAME(fru));
	fetch_complete(domain, fru, ENOMEM);
	goto out;
    }

    err = request_next_data(domain, fru, addr, addr_len);
    if (err) {
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%sfru.c(fru_inventory_area_handler): "
		 "Error requesting next FRU data",
		 FRU_DOMAIN_NAME(fru));
	fetch_complete(domain, fru, err);
	goto out;
    }

    i_ipmi_fru_unlock(fru);
 out:
    return IPMI_MSG_ITEM_NOT_USED;
}

static int
start_logical_fru_fetch(ipmi_domain_t *domain, ipmi_fru_t *fru)
{
    unsigned char    cmd_data[1];
    ipmi_msg_t       msg;

    cmd_data[0] = fru->device_id;
    msg.netfn = IPMI_STORAGE_NETFN;
    msg.cmd = IPMI_GET_FRU_INVENTORY_AREA_INFO_CMD;
    msg.data = cmd_data;
    msg.data_len = 1;

    return ipmi_send_command_addr(domain,
				  &fru->addr, fru->addr_len,
				  &msg,
				  fru_inventory_area_handler,
				  fru,
				  NULL);
}

static int
start_physical_fru_fetch(ipmi_domain_t *domain, ipmi_fru_t *fru)
{
    /* FIXME - this is going to suck, but needs to be implemented. */
    return ENOSYS;
}

/***********************************************************************
 *
 * FRU writing
 *
 **********************************************************************/

int
i_ipmi_fru_new_update_record(ipmi_fru_t   *fru,
			     unsigned int offset,
			     unsigned int length)
{
    fru_update_t *urec;

    if (length == 0) {
	ipmi_log(IPMI_LOG_WARNING,
		 "fru.c(i_ipmi_fru_new_update_record): "
		 "zero-length update record written");
	return 0;
    }
    urec = ipmi_mem_alloc(sizeof(*urec));
    if (!urec)
	return ENOMEM;
    if (fru->access_by_words) {
	/* This handles the (really stupid) word access mode.  If the
	   address is odd, back it up one.  If the length is odd,
	   increment by one. */
	if (offset & 1) {
	    offset -= 1;
	    length += 1;
	}
	urec->offset = offset;
	if (length & 1) {
	    length += 1;
	}
	urec->length = length;
    } else {
	urec->offset = offset;
	urec->length = length;
    }
    urec->next = NULL;
    if (fru->update_recs)
	fru->update_recs_tail->next = urec;
    else
	fru->update_recs = urec;
    fru->update_recs_tail = urec;
    return 0;
}

static int next_fru_write(ipmi_domain_t *domain, ipmi_fru_t *fru);
void write_complete(ipmi_domain_t *domain, ipmi_fru_t *fru, int err);

void
write_complete2(ipmi_fru_t *fru, ipmi_domain_t *domain, int err)
{
    i_ipmi_fru_lock(fru);
    write_complete(domain, fru, err);
}

void
write_complete(ipmi_domain_t *domain, ipmi_fru_t *fru, int err)
{
    if (domain && fru->write_prepared) {
	fru->saved_err = err;
	fru->write_prepared = 0;
	err = fru->complete_write_cb(fru, domain, err, fru->last_timestamp,
				     write_complete2);
	if (!err) {
	    i_ipmi_fru_unlock(fru);
	    return;
	}
    }

    if (fru->saved_err) {
	err = fru->saved_err;
	fru->saved_err = 0;
    }

    if (!err) {
	/* If we succeed, set everything unchanged. */
	if (fru->ops.write_complete)
	    fru->ops.write_complete(fru);
    }
    if (fru->data)
	ipmi_mem_free(fru->data);
    fru->data = NULL;

    fru->in_use = 0;
    i_ipmi_fru_unlock(fru);

    if (fru->domain_fetched_handler)
	fru->domain_fetched_handler(domain, fru, err, fru->fetched_cb_data);

    fru_put(fru);
}

static void
fru_write_handler(ipmi_fru_t    *fru,
		  ipmi_domain_t *domain,
		  int           err)
{
    int rv;

    i_ipmi_fru_lock(fru);

    /* Note that for safety, we do not stop a fru write on deletion. */

    if (err == IPMI_IPMI_ERR_VAL(0x81)) {
	/* Got a busy response.  Try again if we haven't run out of
	   retries. */
	if (fru->retry_count >= MAX_FRU_WRITE_RETRIES) {
	    write_complete(domain, fru, err);
	    goto out;
	}
	fru->retry_count++;
	goto retry_write;
    } else if (err) {
	ipmi_log(IPMI_LOG_ERR_INFO,
		 "%sfru.c(fru_write_handler): "
		 "IPMI error writing FRU data: %x",
		 FRU_DOMAIN_NAME(fru), err);
	write_complete(domain, fru, err);
	goto out;
    }

    fru->update_recs->length -= fru->curr_write_len;
    if (fru->update_recs->length > 0) {
	fru->update_recs->offset += fru->curr_write_len;
    } else {
	fru_update_t *to_free = fru->update_recs;
	fru->update_recs = to_free->next;
	ipmi_mem_free(to_free);
    }

 retry_write:
    if (fru->update_recs) {
	/* More to do. */
	rv = next_fru_write(domain, fru);
	if (rv) {
	    write_complete(domain, fru, rv);
	    goto out;
	}
    } else {
	write_complete(domain, fru, 0);
	goto out;
    }

    i_ipmi_fru_unlock(fru);
 out:
    return;
}

static int
next_fru_write(ipmi_domain_t *domain, ipmi_fru_t *fru)
{
    unsigned char data[MAX_FRU_DATA_WRITE+4];
    int           offset, length = 0, left, noff, tlen;

    noff = fru->update_recs->offset;
    offset = noff;
    left = MAX_FRU_DATA_WRITE;
    while ((left > 0) && (noff == fru->update_recs->offset))
    {
	if (left < fru->update_recs->length)
	    tlen = left;
	else
	    tlen = fru->update_recs->length;

	noff += tlen;
	length += tlen;
	left -= tlen;
	fru->curr_write_len = tlen;
    }

    fru->retry_count = 0;
    data[0] = fru->device_id;
    ipmi_set_uint16(data+1, offset >> fru->access_by_words);
    memcpy(data+3, fru->data+offset, length);
    fru->last_cmd_len = length + 3;
    return fru->write_cb(fru, domain, data, length+3, fru_write_handler);
}

static void
fru_write_timestamp_done(ipmi_fru_t    *fru,
			 ipmi_domain_t *domain,
			 int           err,
			 uint32_t      timestamp)
{
    int rv;

    i_ipmi_fru_lock(fru);

    if (fru->deleted) {
	write_complete(domain, fru, ECANCELED);
	goto out;
    }

    if (err) {
	write_complete(domain, fru, err);
	goto out;
    }

    rv = next_fru_write(domain, fru);
    if (rv) {
	write_complete(domain, fru, rv);
	goto out;
    }
    i_ipmi_fru_unlock(fru);

 out:
    return;
}

static void
fru_write_start_timestamp_check(ipmi_fru_t    *fru,
				ipmi_domain_t *domain,
				int           err)
{
    int rv;

    i_ipmi_fru_lock(fru);

    if (fru->deleted) {
	write_complete(domain, fru, ECANCELED);
	goto out;
    }

    if (err) {
	write_complete(domain, fru, err);
	goto out;
    }

    fru->write_prepared = 1;

    if (fru->timestamp_cb)
	rv = fru->timestamp_cb(fru, domain, fru_write_timestamp_done);
    else
	rv = next_fru_write(domain, fru);
    if (rv) {
	write_complete(domain, fru, rv);
	goto out;
    }
    i_ipmi_fru_unlock(fru);

 out:
    return;
}

typedef struct start_domain_fru_write_s
{
    ipmi_fru_t *fru;
    int        rv;
} start_domain_fru_write_t;

void
start_domain_fru_write(ipmi_domain_t *domain, void *cb_data)
{
    start_domain_fru_write_t *info = cb_data;
    ipmi_fru_t               *fru = info->fru;


    /* We allocate and format the entire FRU data.  We do this because
       of the stupid word access capability, which means we cannot
       necessarily do byte-aligned writes.  Because of that, we might
       have to have the byte before or after the actual one being
       written, and it may come from a different data field. */
    fru->data = ipmi_mem_alloc(fru->data_len);
    if (!fru->data) {
	info->rv = ENOMEM;
	goto out_unlock;
    }
    memset(fru->data, 0, fru->data_len);

    info->rv = fru->ops.write(fru);
    if (info->rv)
	goto out_unlock;

    if (!fru->update_recs) {
	/* No data changed, no write is needed. */
	ipmi_mem_free(fru->data);
	fru->data = NULL;
	fru->in_use = 0;
	i_ipmi_fru_unlock(fru);

	if (fru->domain_fetched_handler)
	    fru->domain_fetched_handler(domain, fru, 0, fru->fetched_cb_data);
	return;
    }

    fru_get(fru);
    fru->write_prepared = 0;

    if (fru->prepare_write_cb)
	info->rv = fru->prepare_write_cb(fru, domain, fru->last_timestamp,
					 fru_write_start_timestamp_check);
    else if (fru->timestamp_cb)
	info->rv = fru->timestamp_cb(fru, domain, fru_write_timestamp_done);
    else
	info->rv = next_fru_write(domain, fru);

    if (info->rv)
	fru_put(fru);

 out_unlock:
    if (info->rv) {
	if (fru->data) {
	    ipmi_mem_free(fru->data);
	    fru->data = NULL;
	}
	fru->in_use = 0;
    }
    i_ipmi_fru_unlock(fru);
}

int
ipmi_fru_write(ipmi_fru_t *fru, ipmi_fru_cb done, void *cb_data)
{
    int                      rv;
    start_domain_fru_write_t info = {fru, 0};

    if (!fru->ops.write)
	return ENOSYS;

    i_ipmi_fru_lock(fru);
    if (fru->in_use) {
	/* Something else is happening with the FRU, error this
	   operation. */
	i_ipmi_fru_unlock(fru);
	return EAGAIN;
    }

    fru->in_use = 1;

    fru->domain_fetched_handler = done;
    fru->fetched_cb_data = cb_data;

    /* Data is fully encoded and the update records are in place.
       Start the write process. */
    rv = ipmi_domain_pointer_cb(fru->domain_id, start_domain_fru_write, &info);
    if (!rv)
	rv = info.rv;
    else {
	fru->in_use = 0;
	i_ipmi_fru_unlock(fru);
    }

    return rv;

}

/***********************************************************************
 *
 * Misc stuff.
 *
 **********************************************************************/
ipmi_domain_id_t
ipmi_fru_get_domain_id(ipmi_fru_t *fru)
{
    return fru->domain_id;
}

void
ipmi_fru_data_free(char *data)
{
    ipmi_mem_free(data);
}

unsigned int
ipmi_fru_get_data_length(ipmi_fru_t *fru)
{
    return fru->data_len;
}

int
ipmi_fru_get_name(ipmi_fru_t *fru, char *name, int length)
{
    int  slen;

    if (length <= 0)
	return 0;

    /* Never changes, no lock needed. */
    slen = strlen(fru->name);
    if (slen == 0) {
	if (name)
	    *name = '\0';
	goto out;
    }

    if (name) {
	memcpy(name, fru->name, slen);
	name[slen] = '\0';
    }
 out:
    return slen;
}

typedef struct iterate_frus_info_s
{
    ipmi_fru_ptr_cb handler;
    void            *cb_data;
} iterate_frus_info_t;

static int
frus_handler(void *cb_data, void *item1, void *item2)
{
    iterate_frus_info_t *info = cb_data;
    info->handler(item1, info->cb_data);
    fru_put(item1);
    return LOCKED_LIST_ITER_CONTINUE;
}

static int
frus_prefunc(void *cb_data, void *item1, void *item2)
{
    ipmi_fru_t *fru = item1;
    ipmi_lock(fru->lock);
    fru_get(fru);
    ipmi_unlock(fru->lock);
    return LOCKED_LIST_ITER_CONTINUE;
}

void
ipmi_fru_iterate_frus(ipmi_domain_t   *domain,
		      ipmi_fru_ptr_cb handler,
		      void            *cb_data)
{
    iterate_frus_info_t info;
    ipmi_domain_attr_t  *attr;
    locked_list_t       *frus;
    int                 rv;

    rv = ipmi_domain_find_attribute(domain, IPMI_FRU_ATTR_NAME,
				    &attr);
    if (rv)
	return;
    frus = ipmi_domain_attr_get_data(attr);

    info.handler = handler;
    info.cb_data = cb_data;
    locked_list_iterate_prefunc(frus, frus_prefunc, frus_handler, &info);
    ipmi_domain_attr_put(attr);
}

/************************************************************************
 *
 * FRU node handling
 *
 ************************************************************************/

int
ipmi_fru_get_root_node(ipmi_fru_t      *fru,
		       const char      **name,
		       ipmi_fru_node_t **node)
{
    if (!fru->ops.get_root_node)
	return ENOSYS;
    return fru->ops.get_root_node(fru, name, node);
}

struct ipmi_fru_node_s
{
    ipmi_lock_t                    *lock;
    unsigned int                   refcount;

    void                           *data;
    void                           *data2;
    ipmi_fru_oem_node_get_field_cb get_field;
    ipmi_fru_oem_node_set_field_cb set_field;
    ipmi_fru_oem_node_settable_cb  settable;
    ipmi_fru_oem_node_subtype_cb   get_subtype;
    ipmi_fru_oem_node_enum_val_cb  get_enum;
    ipmi_fru_oem_node_cb           destroy;
};

ipmi_fru_node_t *
i_ipmi_fru_node_alloc(ipmi_fru_t *fru)
{
    ipmi_fru_node_t *node = ipmi_mem_alloc(sizeof(*node));
    int             rv;

    if (!node)
	return NULL;
    memset(node, 0, sizeof(*node));

    rv = ipmi_create_lock_os_hnd(fru->os_hnd, &node->lock);
    if (rv) {
	ipmi_mem_free(node);
	return NULL;
    }

    node->refcount = 1;
    return node;
}

void
ipmi_fru_get_node(ipmi_fru_node_t *node)
{
    ipmi_lock(node->lock);
    node->refcount++;
    ipmi_unlock(node->lock);
}

void
ipmi_fru_put_node(ipmi_fru_node_t *node)
{
    ipmi_lock(node->lock);
    if (node->refcount > 1) {
	node->refcount--;
	ipmi_unlock(node->lock);
	return;
    }
    ipmi_unlock(node->lock);

    if (node->destroy)
	node->destroy(node);
    ipmi_destroy_lock(node->lock);
    ipmi_mem_free(node);
}

int
ipmi_fru_node_get_field(ipmi_fru_node_t           *node,
			unsigned int              index,
			const char                **name,
			enum ipmi_fru_data_type_e *dtype,
			int                       *intval,
			time_t                    *time,
			double                    *floatval,
			char                      **data,
			unsigned int              *data_len,
			ipmi_fru_node_t           **sub_node)
{
    return node->get_field(node, index, name, dtype, intval, time,
			   floatval, data, data_len, sub_node);
}

int
ipmi_fru_node_set_field(ipmi_fru_node_t           *node,
			unsigned int              index,
			enum ipmi_fru_data_type_e dtype,
			int                       intval,
			time_t                    time,
			double                    floatval,
			char                      *data,
			unsigned int              data_len)
{
    if (!node->set_field)
	return ENOSYS;
    return node->set_field(node, index, dtype, intval, time,
			   floatval, data, data_len);
}

int
ipmi_fru_node_settable(ipmi_fru_node_t           *node,
		       unsigned int              index)
{
    if (!node->set_field)
	return ENOSYS;
    if (!node->settable)
	return 0;
    return node->settable(node, index);
}

int
ipmi_fru_node_get_subtype(ipmi_fru_node_t           *node,
			  enum ipmi_fru_data_type_e *dtype)
{
    if (!node->get_subtype)
	return ENOSYS;
    return node->get_subtype(node, dtype);
}

int
ipmi_fru_node_get_enum_val(ipmi_fru_node_t *node,
			   unsigned int    index,
			   int             *pos,
			   int             *nextpos,
			   const char      **data)
{
    if (!node->get_enum)
	return ENOSYS;
    return node->get_enum(node, index, pos, nextpos, data);
}

void *
i_ipmi_fru_node_get_data(ipmi_fru_node_t *node)
{
    return node->data;
}

void
i_ipmi_fru_node_set_data(ipmi_fru_node_t *node, void *data)
{
    node->data = data;
}

void *
i_ipmi_fru_node_get_data2(ipmi_fru_node_t *node)
{
    return node->data2;
}

void
i_ipmi_fru_node_set_data2(ipmi_fru_node_t *node, void *data2)
{
    node->data2 = data2;
}

void
i_ipmi_fru_node_set_destructor(ipmi_fru_node_t      *node,
			       ipmi_fru_oem_node_cb destroy)
{
    node->destroy = destroy;
}

void
i_ipmi_fru_node_set_get_field(ipmi_fru_node_t                *node,
			      ipmi_fru_oem_node_get_field_cb get_field)
{
    node->get_field = get_field;
}

void
i_ipmi_fru_node_set_set_field(ipmi_fru_node_t                *node,
			      ipmi_fru_oem_node_set_field_cb set_field)
{
    node->set_field = set_field;
}

void
i_ipmi_fru_node_set_settable(ipmi_fru_node_t               *node,
			     ipmi_fru_oem_node_settable_cb settable)
{
    node->settable = settable;
}

void
i_ipmi_fru_node_set_get_subtype(ipmi_fru_node_t              *node,
				ipmi_fru_oem_node_subtype_cb get_subtype)
{
    node->get_subtype = get_subtype;
}

void
i_ipmi_fru_node_set_get_enum(ipmi_fru_node_t               *node,
			     ipmi_fru_oem_node_enum_val_cb get_enum)
{
    node->get_enum = get_enum;
}


/************************************************************************
 *
 * Misc external interfaces
 *
 ************************************************************************/

void *
i_ipmi_fru_get_rec_data(ipmi_fru_t *fru)
{
    return fru->rec_data;
}

void
i_ipmi_fru_set_rec_data(ipmi_fru_t *fru, void *rec_data)
{
    if (fru->rec_data && fru->ops.cleanup_recs)
	fru->ops.cleanup_recs(fru);
    fru->rec_data = rec_data;
}

char *
i_ipmi_fru_get_iname(ipmi_fru_t *fru)
{
    return FRU_DOMAIN_NAME(fru);
}

unsigned int
i_ipmi_fru_get_fetch_mask(ipmi_fru_t *fru)
{
    return fru->fetch_mask;
}

void *
i_ipmi_fru_get_data_ptr(ipmi_fru_t *fru)
{
    return fru->data;
}
unsigned int
i_ipmi_fru_get_data_len(ipmi_fru_t *fru)
{
    return fru->data_len;
}

int
i_ipmi_fru_is_normal_fru(ipmi_fru_t *fru)
{
    return fru->normal_fru;
}

void
i_ipmi_fru_set_is_normal_fru(ipmi_fru_t *fru, int val)
{
    fru->normal_fru = val;
}

/************************************************************************
 *
 * Init/shutdown
 *
 ************************************************************************/

int
i_ipmi_fru_init(void)
{
    if (fru_decode_handlers)
	return 0;

    fru_decode_handlers = locked_list_alloc(ipmi_get_global_os_handler());
    if (!fru_decode_handlers)
	return ENOMEM;
    return 0;
}

void
i_ipmi_fru_shutdown(void)
{
    if (fru_decode_handlers) {
	locked_list_destroy(fru_decode_handlers);
	fru_decode_handlers = NULL;
    }
}